1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
| ;;; -*-Mode:LISP; Package:SYSTEM-INTERNALS-*-
; ** (c) Copyright 1980 Massachusetts Institute of Technology **
;; Error handler. DLW 1/5/78
;; Note: ETE = Error Table Entry. Documented in EHR.
(DECLARE (SPECIAL EH-REAL-CURRENT-PROCESS ;What CURRENT-PROCESS was at entry
;to EH-SECOND-LEVEL, which may have bound it to NIL.
))
(DEFVAR EH-PRINLEVEL 2) ;These are used when printing error messages
(DEFVAR EH-PRINLENGTH 4) ; and values of variables in frames.
;; The error table, read from LISPM1;UCADR nnnTBL into MICROCODE-ERROR-TABLE,
;; describes the symbolic meaning of certain microcode pcs.
;; Its data is rearranged into other variables below.
;; EH-ERROR-TABLE relates the micro pc to a symbolic name of error,
;; called an ETE, which will then have properties saying how to handle
;; the error. The properties are defined in LISPM2;EHR.
;; ETE stands for error table entry, although that is only accurate for
;; microcode errors. It is a list whose car is a symbol which has
;; some interesting properties for recovering from and reporting the error.
;; They are set up in the file LISPM2;EHR > (Error Handler Routines...)
;; Actual error table read in from LISPM1;LCADR nnnTBL.
(DEFVAR MICROCODE-ERROR-TABLE)
;; Ucode version number to which the loaded value of MICROCODE-ERROR-TABLE pertains.
(DEFVAR MICROCODE-ERROR-TABLE-VERSION-NUMBER 0)
;; EH-ASSURE-TABLE-PROCESSED looks at MICROCODE-ERROR-TABLE
;; and produces these lists.
(DEFVAR EH-CALLS-SUB-LIST) ;Alist of micropcs to symbols.
(DEFVAR EH-RESTART-LIST) ;Alist of symbols to micropcs.
(DEFVAR EH-ERROR-TABLE) ;List of ETEs.
(DEFVAR EH-ERROR-TABLE-NUMBER -1) ;Microcode version number for EH-ERROR-TABLE.
;; An error immediately runs the first level error handler stack group
;; whose job is to initialize a second level error handler stack group
;; in which the error handler actually runs.
;; EH-SECOND-LEVEL-COUNT is a count for giving each second level error handler a distinct name.
(DEFVAR EH-SECOND-LEVEL-COUNT 0)
;; Error handler stack groups that were exited and can be reused.
(DEFVAR EH-FREE-SECOND-LEVEL-SG-LIST NIL)
;; Last second level error handler to be running.
;; This is so that each of them can tell
;; when it is returned to whether some other one
;; has been running in the meanwhile.
(DEFVAR EH-LAST-SECOND-LEVEL-SG NIL)
;; This variable is bound to T in every second-level error handler to identify them.
(DEFVAR EH-RUNNING T)
(DEFVAR ERROR-HANDLER-IO NIL) ;If non-NIL, stream EH should use
;; Conditions. Condition handlers are run by the second level error handler.
;; These variables are part of the mechanism by which condition handlers
;; ask to proceed from the error.
(DEFVAR EH-CONDITION-PROCEED-FLAG) ;Communicate from condition handlers to EH-PROCEED.
(DEFVAR EH-CONDITION-PROCEED-VALUE) ;See EH-GET-OBJECT.
;; EH-ERRSET-STATUS is T within an errset.
;; EH-ERRSET-PRINT-MSG is T if the error message should be printed anyway.
;; ERRSET is T if the error handler should be entered despite being in an errset.
(DEFVAR EH-ERRSET-STATUS NIL)
(DEFVAR EH-ERRSET-PRINT-MSG NIL)
(DEFVAR ERRSET NIL)
;; This is funcalled after printing the error message.
(DEFVAR ERROR-MESSAGE-HOOK NIL)
;; This is the dispatch table of error handler commands, a 4 by 220 array.
(DEFVAR EH-DISPATCH)
;; This is a list, setq'd in this file, from which EH-DISPATCH is initialized.
(DEFVAR EH-DISPATCH-LIST)
;; This file will set EH-DISPATCH-LIST; make sure EH-DISPATCH is recomputed from it
;; by EH-ASSURE-DISPATCH-SET-UP.
(MAKUNBOUND 'EH-DISPATCH)
;; Here are the error handler's main operating parameters.
(DEFVAR EH-SG) ;The stack group that got the error.
(DEFVAR EH-CURRENT-FRAME) ;The SG-AP of the frame that the error handler is looking at.
(DEFVAR EH-ORIGINAL-FRAME) ;The SG-AP of the frame that got the error.
;; This is a random gensymmed object which is returned
;; from EH-EVAL to indicate that an error occurred within.
(DEFVAR EH-ERROR-FLAG (NCONS NIL))
;; Number of levels of backtrace to print automatically upon error.
(DEFVAR EH-ERROR-MESSAGE-BACKTRACE-LENGTH 3)
;; Number of instructions to disassemble for M-L, etc., if we
;; can't determine the amount of room on the screen.
(DEFVAR EH-DISASSEMBLE-INSTRUCTION-COUNT 20)
;; Calls to these functions should not be mentioned as frames
;; when stack-censoring is going on in interpreted functions.
;; This should include all functions that have "E args and are open-compiled.
;; *EVAL and APPLY-LAMBDA are there for peculiar reasons.
(DEFVAR EH-UNINTERESTING-FUNCTIONS '(*EVAL APPLY-LAMBDA COND SETQ PROG GO DO DO-NAMED
MULTIPLE-VALUE MULTIPLE-VALUE-LIST
MULTIPLE-VALUE-RETURN AND OR STORE))
;;; These datatypes are OK to call print on
(DEFVAR EH-GOOD-TYPES '(DTP-SYMBOL DTP-FIX DTP-EXTENDED-NUMBER
DTP-SMALL-FLONUM
DTP-LIST DTP-U-ENTRY DTP-FEF-POINTER DTP-ARRAY-POINTER
DTP-STACK-GROUP DTP-CLOSURE DTP-ENTITY))
;;; These point to something (as opposed to being Inums)
(DEFVAR EH-POINTER-TYPES '(DTP-NULL DTP-SYMBOL DTP-SYMBOL-HEADER DTP-EXTENDED-NUMBER
DTP-GC-FORWARD DTP-EXTERNAL-VALUE-CELL-POINTER DTP-ONE-Q-FORWARD
DTP-HEADER-FORWARD DTP-LOCATIVE DTP-LIST
DTP-FEF-POINTER DTP-ARRAY-POINTER DTP-STACK-GROUP
DTP-CLOSURE DTP-SELECT-METHOD DTP-INSTANCE DTP-INSTANCE-HEADER
DTP-ENTITY))
;; This is a temporary kludge, which will be fixed by modifying the installed
;; LISP-REINITIALIZE when this thing gets installed.
(SETQ %INITIALLY-DISABLE-TRAPPING NIL)
;; Save a stack group's state on its stack so we can use it and then restore the state.
;; The information goes on the pdl in a fake frame belonging to the function EH-FOOTHOLD.
;; Each Q is saved as 2 words (pointer and tag) to avoid data type problems.
;; You must call this before pushing a call block, even if calling EH-RUN-SG-GOODBYE,
;; in order to clean up the QBBFL and the U-STACK Q's.
(DEFUN EH-SAVE-SG-STATE (SG &AUX P NEW-AP RP PP)
(SETQ RP (SG-REGULAR-PDL SG)
PP (SG-REGULAR-PDL-POINTER SG))
(SETQ NEW-AP (+ PP %LP-CALL-BLOCK-LENGTH))
(ASET (DPB (- NEW-AP (SG-IPMARK SG)) %%LP-CLS-DELTA-TO-OPEN-BLOCK
(DPB (- NEW-AP (SG-AP SG)) %%LP-CLS-DELTA-TO-ACTIVE-BLOCK
0))
RP (+ NEW-AP %LP-CALL-STATE))
(ASET 0 RP (+ NEW-AP %LP-EXIT-STATE))
(ASET 0 RP (+ NEW-AP %LP-ENTRY-STATE))
(ASET #'EH-FOOTHOLD RP (+ NEW-AP %LP-FEF))
(SETQ PP (1+ NEW-AP))
(DO I 0 (1+ I) (> I SG-PDL-PHASE)
(SETQ P (AP-LEADER SG I))
(ASET (IF (MEMQ (Q-DATA-TYPES (%P-DATA-TYPE P)) EH-POINTER-TYPES)
(%P-CONTENTS-AS-LOCATIVE P)
(%P-POINTER P))
RP PP)
(ASET (%P-LDB %%Q-ALL-BUT-POINTER P)
RP (1+ PP))
(SETQ PP (+ PP 2)))
(SETF (SG-REGULAR-PDL-POINTER SG) (1- PP)) ;Index of last valid word
(SETF (SG-FLAGS-QBBFL SG) 0) ;Clear QBBFL left over from previous frame
(SETF (SG-IPMARK SG) NEW-AP)
(SETF (SG-AP SG) NEW-AP))
;;; This function isn't called, it just exists to name state-save frames
(DEFUN EH-FOOTHOLD () NIL)
;; Pop the saved state from the pdl into the current state.
(DEFUN EH-RESTORE-SG-STATE (SG)
(LET ((PP (EH-PREVIOUS-ACTIVE SG (EH-PREVIOUS-ACTIVE SG (SG-AP SG))))
(RP (SG-REGULAR-PDL SG)))
(AND (NULL PP)
(FERROR NIL "~S state not saved" SG))
(OR (EQ (AREF RP PP) #'EH-FOOTHOLD)
(FERROR NIL "Saved state for ~S at ~S[~S] clobbered." SG RP PP))
(SETQ PP (1+ PP))
(DO I 0 (1+ I) (> I SG-PDL-PHASE)
(%P-STORE-TAG-AND-POINTER (AP-LEADER SG I)
(AREF RP (1+ PP))
(AREF RP PP))
(SETQ PP (+ PP 2)))))
;;; Low level routines for manipulating the stacks of a stack group.
;;; Call EH-SAVE-SG-STATE before calling any of these.
(DEFUN EH-REGPDL-PUSH (X SG &AUX PP)
(SETQ PP (1+ (SG-REGULAR-PDL-POINTER SG)))
(AS-1 X (SG-REGULAR-PDL SG) PP)
(%P-STORE-CDR-CODE (AP-1 (SG-REGULAR-PDL SG) PP) CDR-NEXT)
(SETF (SG-REGULAR-PDL-POINTER SG) PP)
(SETF (SG-PDL-PHASE SG) (1+ (SG-PDL-PHASE SG)))
X)
(DEFUN EH-REGPDL-POP (SG &AUX PP)
(SETF (SG-PDL-PHASE SG) (1- (SG-PDL-PHASE SG)))
(SETQ PP (SG-REGULAR-PDL-POINTER SG))
(SETF (SG-REGULAR-PDL-POINTER SG) (1- PP))
(AR-1 (SG-REGULAR-PDL SG) PP))
(DEFUN EH-SPECPDL-PUSH (X FLAG SG &AUX PP PDL)
(SETQ PP (1+ (SG-SPECIAL-PDL-POINTER SG)))
(SETF (SG-SPECIAL-PDL-POINTER SG) PP)
(SETQ PDL (SG-SPECIAL-PDL SG))
(AS-1 X PDL PP)
(%P-STORE-FLAG-BIT (AP-1 PDL PP) FLAG)
X)
(DEFUN EH-SPECPDL-POP (SG &AUX PP)
(SETQ PP (SG-SPECIAL-PDL-POINTER SG))
(SETF (SG-SPECIAL-PDL-POINTER SG) (1- PP))
(AR-1 (SG-SPECIAL-PDL SG) PP))
;; This simulates the CBM (or P3ZERO) routine in the microcode.
;; It is what a CALL instruction does.
;; You must call EH-SAVE-SG-STATE before calling this.
(DEFUN EH-OPEN-CALL-BLOCK (SG DESTINATION FUNCTION &AUX PP NEW-IPMARK)
(SETQ PP (SG-REGULAR-PDL-POINTER SG))
(SETQ NEW-IPMARK (+ PP %LP-CALL-BLOCK-LENGTH))
(EH-REGPDL-PUSH (DPB (- NEW-IPMARK (SG-IPMARK SG)) %%LP-CLS-DELTA-TO-OPEN-BLOCK
(DPB (- NEW-IPMARK (SG-AP SG)) %%LP-CLS-DELTA-TO-ACTIVE-BLOCK
(DPB DESTINATION %%LP-CLS-DESTINATION 0)))
SG)
(EH-REGPDL-PUSH 0 SG)
(EH-REGPDL-PUSH 0 SG)
(EH-REGPDL-PUSH FUNCTION SG)
(SETF (SG-IPMARK SG) NEW-IPMARK))
;; Running things in the other stack group.
;; Call a function in another stack group and return the value it "returns".
;; Actually, the function should call this stack group back with the "value" as argument.
;; If the value is the symbol LOSE, we throw to EH-QUIT.
;; The call block and args should already be on the regpdl of the other stack group,
;; hence EH-SAVE-SG-STATE should be outside this function.
;; Nothing will automatically make the function know who you are;
;; provide your own stack group as an argument to it if necessary.
;; Before returning, EH-RESTORE-SG-STATE is done since it's generally desired.
(DEFUN EH-RUN-SG (SG &AUX RESULT)
(%P-STORE-CDR-CODE (AP-1 (SG-REGULAR-PDL SG) ;Terminate arg list assumed there
(SG-REGULAR-PDL-POINTER SG))
CDR-NIL)
(SETF (SG-CURRENT-STATE SG) SG-STATE-INVOKE-CALL-ON-RETURN)
(SETQ EH-LAST-SECOND-LEVEL-SG %CURRENT-STACK-GROUP)
(SETF (SG-FLAGS-MAR-MODE SG) 0) ;Turn off the MAR (why??)
(FUNCALL SG)
(SETQ RESULT (CAR %CURRENT-STACK-GROUP-CALLING-ARGS-POINTER))
(EH-RESTORE-SG-STATE SG)
(COND (EH-RUNNING
(COND ((NEQ %CURRENT-STACK-GROUP EH-LAST-SECOND-LEVEL-SG)
(TERPRI)
(EH-PRINT-ERROR-MESSAGE SG (SG-TRAP-TAG SG) T)))
(SETQ EH-LAST-SECOND-LEVEL-SG %CURRENT-STACK-GROUP)))
(COND ((EQ RESULT 'LOSE)
(*THROW 'EH-QUIT NIL)))
RESULT)
;; Restart a stack group and mark the error handler stack group as free.
;; This is used for throwing, etc.
(DEFUN EH-RUN-SG-GOODBYE (SG)
(%P-STORE-CDR-CODE (AP-1 (SG-REGULAR-PDL SG) ;Terminate arg list
(SG-REGULAR-PDL-POINTER SG))
CDR-NIL)
(SETF (SG-CURRENT-STATE SG) SG-STATE-INVOKE-CALL-ON-RETURN)
(WITHOUT-INTERRUPTS
(COND (EH-RUNNING
(EH-FREE %CURRENT-STACK-GROUP)))
(FUNCALL SG)))
;;Smash a SG so that it reinvokes its current function with the same args when resumed.
;; For the time being, the function had better not have pushed a micro-stack.
;; Any binding block pushed will not get unwound immediately, but will be
;; "concatenated." For the moment, tho, PROCESS-WAIT is the only fctn this should
;; be used on, and these screws shouldnt affect it.
(DEFUN EH-REINVOKE (SG &AUX RP AP NEW-AP NARGS)
(PROG NIL
(SETQ RP (SG-REGULAR-PDL SG)
AP (SG-AP SG)
NEW-AP (EH-PREVIOUS-ACTIVE SG AP)
NARGS (LDB %%LP-ENS-NUM-ARGS-SUPPLIED (AR-1 RP (+ AP %LP-ENTRY-STATE))))
; (COND ((NOT (ZEROP (LDB %%LP-EXS-MICRO-STACK-SAVED (AR-1 RP (+ AP %LP-EXIT-STATE))))) ))
; (COND ((NOT (ZEROP (LDB %%LP-EXS-BINDING-BLOCK-PUSHED (AR-1 RP (+ AP %LP-EXIT-STATE)))))
; ))
L (COND ((> (SG-REGULAR-PDL-POINTER SG) (+ AP NARGS)) ;Pop any extra stuff beyond args.
(EH-REGPDL-POP SG)
(GO L)))
(SETF (SG-IPMARK SG) AP)
(SETF (SG-AP SG) NEW-AP)
(SETF (SG-CURRENT-STATE SG) SG-STATE-INVOKE-CALL-ON-RETURN)))
;; Mark a second level error handler stack group as available for re-use.
(DEFUN EH-FREE (SG)
(WITHOUT-INTERRUPTS
(PUSH SG EH-FREE-SECOND-LEVEL-SG-LIST)
(AND CURRENT-PROCESS (<- CURRENT-PROCESS ':REVOKE-RUN-REASON SG))))
;; Unwind the stack group until the M-AP is DEST-AP.
;; If GOODBYE-P is T, it returns the specified value from that frame,
;; otherwise it comes back to the EH.
(DEFUN EH-UNWIND-TO-FRAME (SG DEST-AP GOODBYE-P &OPTIONAL VALUE (LABEL T) &AUX N)
(SETQ N (DO ((AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG AP))
(N 1 (1+ N)))
((= AP DEST-AP) N)))
(EH-UNWIND SG LABEL VALUE N (IF GOODBYE-P NIL %CURRENT-STACK-GROUP)
(IF GOODBYE-P 'FREE 'CALL))
(cond ((null goodbye-p) ;Flush the call back to this SG, and try to get
(setf (sg-ap sg) ; things in phase again.
(setq eh-current-frame
(setq eh-original-frame (eh-previous-active sg (sg-ap sg)))))
(dotimes (i 4)
(eh-regpdl-pop sg))))
)
;; The CONTINUATION is a function called with one argument in the newly-reset
;; stack-group. ARGUMENT is that argument.
;; If PROCESS-P, rather than doing it now, in this process, we simply
;; leave the stack-group in such a state that the next time it is called,
;; e.g. by the scheduler, it will do it.
(DEFUN EH-UNWIND-SG (SG CONTINUATION ARGUMENT PROCESS-P)
(LET ((ST (SG-CURRENT-STATE SG)))
(COND ((NOT (OR (= ST SG-STATE-AWAITING-INITIAL-CALL)
(= ST 0)))
(EH-UNWIND SG T ARGUMENT NIL CONTINUATION (IF PROCESS-P 'SETUP 'CALL)))
(T ;SG has not been run, don't unwind, but do leave in same state
(STACK-GROUP-PRESET SG CONTINUATION ARGUMENT)
(OR PROCESS-P (FUNCALL SG))))
(OR PROCESS-P (SETF (SG-CURRENT-STATE SG) SG-STATE-EXHAUSTED))))
;; Eval a form in a specified stack group using a foothold.
(DEFUN EH-EVAL (SG FORM &AUX (PREV-FH (SG-FOOTHOLD-DATA SG)))
(EH-SAVE-SG-STATE SG)
(SETF (SG-FOOTHOLD-DATA SG) (SG-AP SG))
(EH-OPEN-CALL-BLOCK SG 0 'EH-FH-EVALER)
(EH-REGPDL-PUSH FORM SG)
(EH-REGPDL-PUSH + SG)
(EH-REGPDL-PUSH * SG)
(EH-REGPDL-PUSH %CURRENT-STACK-GROUP SG)
(EH-REGPDL-PUSH EH-RUNNING SG)
(EH-REGPDL-PUSH PREV-FH SG)
(EH-RUN-SG SG))
(DEFUN EH-APPLY (SG FUNCTION ARGUMENTS &AUX (PREV-FH (SG-FOOTHOLD-DATA SG)))
(EH-SAVE-SG-STATE SG)
(SETF (SG-FOOTHOLD-DATA SG) (SG-AP SG))
(EH-OPEN-CALL-BLOCK SG 0 'EH-FH-APPLIER)
(EH-REGPDL-PUSH FUNCTION SG)
(EH-REGPDL-PUSH ARGUMENTS SG)
(EH-REGPDL-PUSH + SG)
(EH-REGPDL-PUSH * SG)
(EH-REGPDL-PUSH %CURRENT-STACK-GROUP SG)
(EH-REGPDL-PUSH EH-RUNNING SG)
(EH-REGPDL-PUSH PREV-FH SG)
(EH-RUN-SG SG))
(DEFUN EH-THROW (SG LABEL VALUE &OPTIONAL IGNORE)
(EH-SAVE-SG-STATE SG)
(EH-OPEN-CALL-BLOCK SG 0 'EH-FH-THROWER)
(EH-REGPDL-PUSH LABEL SG)
(EH-REGPDL-PUSH VALUE SG)
(EH-RUN-SG-GOODBYE SG))
(DEFUN EH-UNWIND (SG LABEL VALUE COUNT ACTION DISPOSAL)
"DISPOSAL is SETUP just to set up the call, CALL to make the call and not free the EH,
FREE to make the call and free the EH"
(EH-SAVE-SG-STATE SG)
(AND COUNT (SETQ COUNT (1+ COUNT))) ;Make up for the frame pushed by EH-SAVE-SG-STATE.
(EH-OPEN-CALL-BLOCK SG 0 'EH-FH-UNWINDER)
(EH-REGPDL-PUSH LABEL SG)
(EH-REGPDL-PUSH VALUE SG)
(EH-REGPDL-PUSH COUNT SG)
(EH-REGPDL-PUSH ACTION SG)
(%P-STORE-CDR-CODE (AP-1 (SG-REGULAR-PDL SG) ;Terminate arg list
(SG-REGULAR-PDL-POINTER SG))
CDR-NIL)
(SETF (SG-CURRENT-STATE SG) SG-STATE-INVOKE-CALL-ON-RETURN)
(WITHOUT-INTERRUPTS
(AND EH-RUNNING (EQ DISPOSAL 'FREE)
(EH-FREE %CURRENT-STACK-GROUP))
(OR (EQ DISPOSAL 'SETUP) (FUNCALL SG))))
;; The EH-FH functions are those intended to run in the other stack group.
;; Those that come back should be started up with EH-RUN-SG.
;; They must be given the error handler stack group as an argument
;; so that they can call it back. This they must do without making any other
;; intervening active call blocks on the stack, so that the foothold data
;; can be found from the SG-AP when it returns. They must also be given EH-RUNNING as
;; an argument, so that if it is T they can do an unwind protect that
;; does EH-FREE on the stack group that they aren't going to return to in that case.
;; They must also be given the previous foothold's offset so that
;; SG-FOOTHOLD-DATA can be reset in case of a throw.
;; Those that do not come back should be started up with EH-RUN-SG-GOODBYE.
(DEFUN EH-FH-APPLIER (FN ARGS NEW-+ NEW-* SG EH-P PREV-FH)
(UNWIND-PROTECT
(LET ((KBD-SIMULATED-CLOCK-FCN-LIST '(TV-BLINKER-CLOCK))
(+ NEW-+) (* NEW-*) EVALHOOK)
(*CATCH 'EH-FOOTHOLD
(FUNCALL SG (MULTIPLE-VALUE-LIST (APPLY FN ARGS))))
;; This is in case the catch catches.
(FUNCALL SG 'LOSE))
;; This is reached only if we throw through this frame.
(SETF (SG-FOOTHOLD-DATA %CURRENT-STACK-GROUP) PREV-FH)
(AND EH-P (EH-FREE SG))))
(DEFUN EH-FH-EVALER (FORM NEW-+ NEW-* SG EH-P PREV-FH)
(UNWIND-PROTECT
(LET ((KBD-SIMULATED-CLOCK-FCN-LIST '(TV-BLINKER-CLOCK))
(+ NEW-+) (* NEW-*) EVALHOOK)
(*CATCH 'EH-FOOTHOLD
(FUNCALL SG (MULTIPLE-VALUE-LIST (EVAL FORM))))
;; This is in case the catch catches.
(FUNCALL SG 'LOSE))
(SETF (SG-FOOTHOLD-DATA %CURRENT-STACK-GROUP) PREV-FH)
(AND EH-P (EH-FREE SG))))
(DEFUN EH-FH-THROWER (LABEL VALUE)
(*THROW LABEL VALUE))
(DEFUN EH-FH-UNWINDER (LABEL VALUE COUNT ACTION)
(*UNWIND-STACK LABEL VALUE COUNT ACTION))
;; Various utility ANALYSIS functions.
;; These functions take an SG and an AP, and return the AP
;; for the previous open or active stack frame.
;Result is NIL if this is the bottom frame
(DEFUN EH-PREVIOUS-OPEN (SG AP)
(LET ((DELTA (RP-DELTA-TO-OPEN-BLOCK (SG-REGULAR-PDL SG) AP)))
(COND ((ZEROP DELTA) NIL)
(T (- AP DELTA)))))
;Result is NIL if this is the bottom frame
(DEFUN EH-PREVIOUS-ACTIVE (SG AP)
(LET ((DELTA (RP-DELTA-TO-ACTIVE-BLOCK (SG-REGULAR-PDL SG) AP)))
(COND ((ZEROP DELTA) NIL)
(T (- AP DELTA)))))
;; Returns NIL if there is no next.
(DEFUN EH-NEXT-OPEN (SG AP)
(DO ((THIS-AP (SG-AP SG) (EH-PREVIOUS-OPEN SG THIS-AP))
(NEXT-AP NIL THIS-AP))
((= THIS-AP AP) NEXT-AP)))
;; Returns NIL if there is no next.
(DEFUN EH-NEXT-ACTIVE (SG AP)
(DO ((THIS-AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG THIS-AP))
(NEXT-AP NIL THIS-AP))
((= THIS-AP AP) NEXT-AP)))
;; Scan several active frames up or down from a given one.
;; We return two values; the first is the offset of the frame found,
;; and the second is T if the specified number of frames were found
;; before the top or bottom of the stack.
(DEFUN EH-NEXT-NTH-ACTIVE (SG FRAME &OPTIONAL (COUNT 1))
(COND ((= COUNT 0) FRAME)
((MINUSP COUNT)
(DO ((P FRAME (EH-PREVIOUS-ACTIVE SG P))
(I 0 (1- I))
(PP NIL P))
(())
(AND (OR (NULL P) (= I COUNT))
(RETURN (OR P PP) P))))
(T (DO ((P FRAME (EH-NEXT-ACTIVE SG P))
(I 0 (1+ I))
(PP NIL P))
(())
(AND (OR (NULL P) (= I COUNT))
(RETURN (OR P PP) P))))))
(DEFUN EH-FEF-INSTRUCTION (FEF PC)
(LET ((IDX (// PC 2)))
(COND ((ZEROP (LOGAND 1 PC))
(%P-LDB-OFFSET %%Q-LOW-HALF FEF IDX))
((%P-LDB-OFFSET %%Q-HIGH-HALF FEF IDX)))))
;Takes a functional object, and returns a Lisp object which is its "name".
(DEFUN EH-FUNCTION-NAME (FUNCTION)
(SELECT (%DATA-TYPE FUNCTION)
(DTP-FEF-POINTER (FEF-NAME FUNCTION))
(DTP-U-ENTRY (MICRO-CODE-ENTRY-NAME-AREA (%POINTER FUNCTION)))
(DTP-LIST (COND ((EQ (CAR FUNCTION) 'NAMED-LAMBDA)
(IF (ATOM (CADR FUNCTION)) (CADR FUNCTION)
(CAADR FUNCTION)))
(T FUNCTION)))
((DTP-CLOSURE DTP-ENTITY) (EH-FUNCTION-NAME (%MAKE-POINTER DTP-LIST (CAR FUNCTION))))
(DTP-STACK-GROUP (SG-NAME FUNCTION))
(DTP-SYMBOL FUNCTION)
(OTHERWISE FUNCTION)))
;; Scan several active frames up or down from a given one,
;; being smart about calls to interpreted functions.
;; We return two values; the first is the offset of the frame found,
;; and the second is T if the specified number of frames were found
;; before the top or bottom of the stack.
(DEFUN EH-NEXT-NTH-INTERESTING-ACTIVE (SG FRAME &OPTIONAL (COUNT 1))
(COND ((= COUNT 0) FRAME)
((MINUSP COUNT)
(DO ((P FRAME (EH-PREVIOUS-INTERESTING-ACTIVE SG P))
(I 0 (1- I))
(PP NIL P))
(())
(AND (OR (NULL P) (= I COUNT))
(RETURN (OR P PP) P))))
(T (DO ((P FRAME (EH-NEXT-INTERESTING-ACTIVE SG P))
(I 0 (1+ I))
(PP NIL P))
(())
(AND (OR (NULL P) (= I COUNT))
(RETURN (OR P PP) P))))))
;; Return the next frame, counting all the actual frames of parts of an
;; interpreted function as if they were one frame.
(DEFUN EH-NEXT-INTERESTING-ACTIVE (SG AP &AUX (RP (SG-REGULAR-PDL SG)))
(COND ((ATOM (RP-FUNCTION-WORD RP AP))
(EH-NEXT-ACTIVE SG AP))
(T (DO ((NEW-AP (EH-NEXT-ACTIVE SG AP) (EH-NEXT-ACTIVE SG NEW-AP)))
((OR (NULL NEW-AP)
(NOT (MEMQ (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP NEW-AP))
EH-UNINTERESTING-FUNCTIONS)))
NEW-AP)))))
(DEFUN EH-PREVIOUS-INTERESTING-ACTIVE (SG AP)
(EH-OUT-TO-INTERESTING-ACTIVE SG (EH-PREVIOUS-ACTIVE SG AP)))
;; Given a frame, find out if it is one of the frames of a call to an interpreted function.
;; If so, return the outermost frame of this call to the interpreted function.
;; If not, return the original frame.
(DEFUN EH-OUT-TO-INTERESTING-ACTIVE (SG AP &AUX (RP (SG-REGULAR-PDL SG)))
(COND ((NULL AP) NIL)
((NOT (MEMQ (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP AP)) EH-UNINTERESTING-FUNCTIONS))
AP)
(T (DO ((NEW-AP AP (EH-PREVIOUS-ACTIVE SG NEW-AP)))
((OR (NULL NEW-AP)
(NOT (MEMQ (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP NEW-AP))
EH-UNINTERESTING-FUNCTIONS)))
(COND ((NULL NEW-AP) AP)
((ATOM (RP-FUNCTION-WORD RP NEW-AP)) AP)
(T NEW-AP)))))))
;; Assuming that AP points to the outermost actual frame
;; of a call to an interpreted function, return its innermost active *EVAL frame.
(DEFUN EH-INNERMOST-UNINTERESTING-ACTIVE-EVAL (SG AP &AUX (RP (SG-REGULAR-PDL SG)))
(DO ((NEW-AP AP FOLLOWING-AP)
(FOLLOWING-AP) (LAST-EVAL-AP))
(())
(AND (EQ (RP-FUNCTION-WORD RP NEW-AP) (FUNCTION *EVAL))
(SETQ LAST-EVAL-AP NEW-AP))
(SETQ FOLLOWING-AP (EH-NEXT-ACTIVE SG NEW-AP))
(OR (AND FOLLOWING-AP
(MEMQ (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP FOLLOWING-AP))
EH-UNINTERESTING-FUNCTIONS))
(RETURN LAST-EVAL-AP))))
;; Return a name for the "function" to tell the user about
;; corresponding to the macro instruction in which an error happened.
(DEFUN EH-ERRING-FUNCTION (SG)
(LET ((AP (SG-AP SG))
(RP (SG-REGULAR-PDL SG)))
(LET ((FUNCTION (RP-FUNCTION-WORD RP AP))
(PC (1- (RP-EXIT-PC RP AP))))
(SELECT (%DATA-TYPE FUNCTION)
(DTP-U-ENTRY
(MICRO-CODE-ENTRY-NAME-AREA (%POINTER FUNCTION)))
(DTP-FEF-POINTER
(LET ((INST (EH-FEF-INSTRUCTION FUNCTION PC)))
(LET ((OP (LDB 1104 INST))
(DEST (LDB 1503 INST))
(DISP (LDB 0011 INST)))
(COND ((< OP 11)
(NTH OP '(FUNCALL FUNCALL MOVE-INSTRUCTION CAR
CDR CADR CDDR CDAR CAAR)))
((= OP 11)
(NTH DEST '(ND1-UNUSED *PLUS *DIF *TIMES ;*'s to avoid confusion with
*QUO *LOGAND *LOGXOR *LOGIOR))) ;argument-number
((= OP 12)
(NTH DEST '(= > < EQ CDR CDDR 1+ 1-)))
((= OP 13)
(NTH DEST '(ND3-UNUSED BIND BIND SET-NIL SET-ZERO PUSH-E MOVEM POP)))
((= OP 14)
'A-BRANCH-INSTRUCTION)
((< DISP 100) 'LIST)
((< DISP 200) 'LIST-IN-AREA)
((< DISP 220) 'UNBIND)
((< DISP 240) 'A-POP-PDL-INSTRUCTION)
(T (MICRO-CODE-SYMBOL-NAME-AREA (- DISP 200)))))))
(OTHERWISE FUNCTION)))))
;; Return the name of the localno'th local of function, or nil if unavailable or none such.
;; This is only meaningful for fefs if localno > 0.
;; If localno = 0 it will get the name of the rest arg, if there is one,
;; for any type of function.
(DEFUN EH-LOCAL-NAME (FUNCTION LOCALNO &AUX ARGL)
(COND ((= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER)
(COMPILER:DISASSEMBLE-LOCAL-NAME FUNCTION LOCALNO))
((AND (ZEROP LOCALNO)
(SETQ ARGL (COND ((LISTP FUNCTION)
(SELECTQ (CAR FUNCTION)
(LAMBDA (CADR FUNCTION))
(NAMED-LAMBDA (CADDR FUNCTION))))
(T (ARGLIST FUNCTION T)))))
(CADR (MEMQ '&REST ARGL)))))
;; Return the name of the argno'th arg of function, or nil if
;; not known or function doesn't want that many args.
;; Rest args don't count.
(DEFUN EH-ARG-NAME (FUNCTION ARGNO &AUX ARGL)
(COND ((= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER)
(COMPILER:DISASSEMBLE-ARG-NAME FUNCTION ARGNO))
((SETQ ARGL (COND ((LISTP FUNCTION)
(SELECTQ (CAR FUNCTION)
(LAMBDA (CADR FUNCTION))
(NAMED-LAMBDA (CADDR FUNCTION))))
(T (ARGLIST FUNCTION T))))
(DO ((ARGL ARGL (CDR ARGL))
(I ARGNO))
((OR (NULL ARGL) (EQ (CAR ARGL) '&AUX) (EQ (CAR ARGL) '&REST)))
(OR (MEMQ (CAR ARGL) LAMBDA-LIST-KEYWORDS)
(COND (( I 0)
(RETURN (CAR ARGL)))
(T (SETQ I (1- I)))))))))
;; Get the value of the rest arg in a given frame.
;; The first value is the value of the rest arg (nil if the frame has none).
;; The second value is T if the function expects to have one.
;; The third value indicates a rest arg explicitly passed as one;
;; it can conceivably be T even if the second is nil, if something strange
;; has happened, and an extraneous rest arg has been passed.
(DEFUN EH-REST-ARG-VALUE (SG FRAME &AUX
(RP (SG-REGULAR-PDL SG))
(AP FRAME)
LEXPR-CALL ARGS-INFO REST-ARG NARGS-EXPECTED
(FUNCTION (RP-FUNCTION-WORD RP AP))
(NARGS-SUPPLIED (RP-NUMBER-ARGS-SUPPLIED RP AP)))
(COND ((OR (= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER) (LISTP FUNCTION))
(SETQ ARGS-INFO (ARGS-INFO FUNCTION))
(SETQ REST-ARG (LDB-TEST 2402 ARGS-INFO))
(SETQ NARGS-EXPECTED (LDB %%ARG-DESC-MAX-ARGS ARGS-INFO))))
(AND (NOT (ZEROP (RP-ADI-PRESENT RP AP)))
(DO I (- AP %LP-CALL-BLOCK-LENGTH) (- I 2) NIL
(SELECT (LDB %%ADI-TYPE (AREF RP I))
((ADI-FEXPR-CALL ADI-LEXPR-CALL)
(RETURN (SETQ LEXPR-CALL T)))) ;Last arg supplied is a rest arg
(AND (ZEROP (%P-FLAG-BIT (ALOC RP (1- I))))
(RETURN NIL))))
(PROG () (RETURN
(COND (LEXPR-CALL (AREF RP (+ AP NARGS-SUPPLIED)))
((LISTP FUNCTION)
(COND ((> NARGS-SUPPLIED NARGS-EXPECTED)
(%MAKE-POINTER DTP-LIST
(AP-1 RP (+ AP NARGS-EXPECTED 1))))
(T NIL)))
(T (AREF RP (+ AP (RP-LOCAL-BLOCK-ORIGIN RP AP)))))
REST-ARG
LEXPR-CALL)))
;; Return the number of spread args present in a given frame.
;; This will not count any args which are part of a rest arg.
(DEFUN EH-NUMBER-OF-SPREAD-ARGS (SG FRAME &AUX
(RP (SG-REGULAR-PDL SG)) (AP FRAME)
ARGS-INFO REST-ARG-P NARGS-EXPECTED NARGS-VISIBLE
(FUNCTION (RP-FUNCTION-WORD RP AP))
(NARGS-SUPPLIED (RP-NUMBER-ARGS-SUPPLIED RP AP)))
(COND ((OR (= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER) (LISTP FUNCTION))
(SETQ ARGS-INFO (ARGS-INFO FUNCTION))
(SETQ REST-ARG-P (LDB-TEST 2402 ARGS-INFO))
(SETQ NARGS-EXPECTED (LDB %%ARG-DESC-MAX-ARGS ARGS-INFO))))
;; See if this is a lexpr-call. If so, the last "arg" is a rest arg, so decrement nargs.
(AND (NOT (ZEROP (RP-ADI-PRESENT RP AP)))
(DO I (- AP %LP-CALL-BLOCK-LENGTH) (- I 2) NIL
(SELECT (LDB %%ADI-TYPE (AREF RP I))
((ADI-FEXPR-CALL ADI-LEXPR-CALL)
(RETURN (SETQ NARGS-SUPPLIED (1- NARGS-SUPPLIED)))))
(AND (ZEROP (%P-FLAG-BIT (ALOC RP (1- I))))
(RETURN NIL))))
;; The args that can be asked for are the ones supplied,
;; except that FEFs make slots for all args they expect whether supplied or not.
(SETQ NARGS-VISIBLE
(COND ((= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER)
(MAX NARGS-SUPPLIED NARGS-EXPECTED))
(T NARGS-SUPPLIED)))
;; If function is known to take a rest arg, any unexpected args
;; are part of it, so they don't count as there this way.
(AND REST-ARG-P (> NARGS-SUPPLIED NARGS-EXPECTED)
(SETQ NARGS-VISIBLE NARGS-EXPECTED))
NARGS-VISIBLE)
;; Return the value of the argno'th spread arg in a given frame, or nil if there is none.
;; The second value is T if that number arg is present in the frame.
(DEFUN EH-ARG-VALUE (SG FRAME ARGNO &AUX (RP (SG-REGULAR-PDL SG)) (AP FRAME))
(PROG ()
(OR (MINUSP ARGNO) ( ARGNO (EH-NUMBER-OF-SPREAD-ARGS SG FRAME))
(RETURN (AREF RP (+ AP ARGNO 1)) T))))
;; These functions know about the location tags used in the ERROR-TABLE
;; entries, and how to creates locatives to them, fetch from them,
;; and store into them.
;; There is the issue that the contents may be illegal datatypes.
;; Have to think about if there are screw cases, etc.
; Analysis
(DEFUN EH-CONTENTS (LOC SG)
(SELECTQ LOC
(M-A (SG-AC-A SG))
(M-B (SG-AC-B SG))
(M-C (SG-AC-C SG))
(M-D (SG-AC-C SG))
(M-E (SG-AC-E SG))
(M-T (SG-AC-T SG))
(M-R (SG-AC-R SG))
(M-Q (SG-AC-Q SG))
(M-I (SG-AC-I SG))
(M-J (SG-AC-J SG))
(M-S (SG-AC-S SG))
(M-K (SG-AC-K SG))
(A-QCSTKG SG)
(A-SG-PREVIOUS-STACK-GROUP (SG-PREVIOUS-STACK-GROUP SG))
(PP (AR-1 (SG-REGULAR-PDL SG) (SG-REGULAR-PDL-POINTER SG)))
(RMD (%P-CONTENTS-OFFSET (SG-SAVED-VMA SG) 0))
(OTHERWISE
(COND ((AND (LISTP LOC) (EQ (CAR LOC) 'PP))
(AR-1 (SG-REGULAR-PDL SG) (+ (SG-REGULAR-PDL-POINTER SG) (CADR LOC))))
((EH-BAD-HACKER LOC "Unknown tag"))))))
;; Metamorphosis
(DEFUN EH-STORE (X LOC SG)
(SELECTQ LOC
(M-A (SETF (SG-AC-A SG) X))
(M-B (SETF (SG-AC-B SG) X))
(M-C (SETF (SG-AC-C SG) X))
(M-D (SETF (SG-AC-C SG) X))
(M-E (SETF (SG-AC-E SG) X))
(M-T (SETF (SG-AC-T SG) X))
(M-R (SETF (SG-AC-R SG) X))
(M-Q (SETF (SG-AC-Q SG) X))
(M-I (SETF (SG-AC-I SG) X))
(M-J (SETF (SG-AC-J SG) X))
(M-S (SETF (SG-AC-S SG) X))
(M-K (SETF (SG-AC-K SG) X))
(A-QCSTKG (ERROR T "You can't store in this!"))
(A-SG-PREVIOUS-STACK-GROUP (SETF (SG-PREVIOUS-STACK-GROUP SG) X))
(PP (AS-1 X (SG-REGULAR-PDL SG) (SG-REGULAR-PDL-POINTER SG)))
(RMD (%P-STORE-CONTENTS (SG-SAVED-VMA SG) X)) ;Offset???
(OTHERWISE
(COND ((AND (LISTP LOC) (EQ (CAR LOC) 'PP))
(AS-1 X (SG-REGULAR-PDL SG) (+ (SG-REGULAR-PDL-POINTER SG) (CADR LOC))))
((EH-BAD-HACKER LOC "Unknown tag"))))))
;; Getllocativepointersis
(DEFUN EH-LOCATE (LOC SG)
(SELECTQ LOC
(M-A (LOCF (SG-AC-A SG)))
(M-B (LOCF (SG-AC-B SG)))
(M-C (LOCF (SG-AC-C SG)))
(M-D (LOCF (SG-AC-D SG)))
(M-E (LOCF (SG-AC-E SG)))
(M-T (LOCF (SG-AC-T SG)))
(M-R (LOCF (SG-AC-R SG)))
(M-Q (LOCF (SG-AC-Q SG)))
(M-I (LOCF (SG-AC-I SG)))
(M-J (LOCF (SG-AC-J SG)))
(M-S (LOCF (SG-AC-S SG)))
(M-K (LOCF (SG-AC-K SG)))
(A-QCSTKG (%MAKE-POINTER DTP-LOCATIVE SG))
(A-SG-PREVIOUS-STACK-GROUP (LOCF (SG-PREVIOUS-STACK-GROUP SG)))
(PP (AP-1 (SG-REGULAR-PDL SG) (SG-REGULAR-PDL-POINTER SG)))
(RMD (%MAKE-POINTER DTP-LOCATIVE (SG-SAVED-VMA SG)))
(OTHERWISE
(COND ((AND (LISTP LOC) (EQ (CAR LOC) 'PP))
(AP-1 (SG-REGULAR-PDL SG) (+ (SG-REGULAR-PDL-POINTER SG) (CADR LOC))))
((EH-BAD-HACKER LOC "Unknown tag"))))))
;; Various initialization routines.
(DEFUN EH-ASSURE-TABLE-LOADED ()
(COND ((NOT (AND (BOUNDP 'MICROCODE-ERROR-TABLE-VERSION-NUMBER)
(= MICROCODE-ERROR-TABLE-VERSION-NUMBER %MICROCODE-VERSION-NUMBER)))
(EH-LOAD-ERROR-TABLE)
(OR (AND (BOUNDP 'MICROCODE-ERROR-TABLE-VERSION-NUMBER)
(= MICROCODE-ERROR-TABLE-VERSION-NUMBER %MICROCODE-VERSION-NUMBER))
(BREAK 'CANNOT-GET-ERROR-TABLE T)))))
(DEFUN EH-LOAD-ERROR-TABLE (&AUX (FUDGED-USER-ID-FLAG NIL))
(COND ((OR (NULL USER-ID) (STRING-EQUAL USER-ID ""))
(SETQ FUDGED-USER-ID-FLAG T)
(LOGIN "ERROR" T)))
(LOAD (FORMAT NIL "DSK:LISPM1;UCADR ~DTBL" %MICROCODE-VERSION-NUMBER)
"SYSTEM-INTERNALS")
(AND FUDGED-USER-ID-FLAG (LOGOUT)))
;; Divides up MICROCODE-ERROR-TABLE into EH-CALLS-SUB-LIST, EH-RESTART-LIST,
;; and EH-ERROR-TABLE.
(DEFUN EH-ASSURE-TABLE-PROCESSED ()
(COND ((NOT (= MICROCODE-ERROR-TABLE-VERSION-NUMBER EH-ERROR-TABLE-NUMBER))
(SETQ EH-ERROR-TABLE NIL
EH-CALLS-SUB-LIST NIL
EH-RESTART-LIST NIL)
(DO ET MICROCODE-ERROR-TABLE (CDR ET) (NULL ET)
(SELECTQ (CADAR ET)
(RESTART (PUSH (CONS (CADDAR ET) (1+ (CAAR ET))) EH-RESTART-LIST))
(CALLS-SUB (PUSH (CONS (CAAR ET) (CADDAR ET)) EH-CALLS-SUB-LIST))
(OTHERWISE (PUSH (CAR ET) EH-ERROR-TABLE))))
(SETQ EH-ERROR-TABLE-NUMBER MICROCODE-ERROR-TABLE-VERSION-NUMBER)
)))
(DEFUN EH-ASSURE-DISPATCH-SET-UP ()
(COND ((NOT (BOUNDP 'EH-DISPATCH))
(SETQ EH-DISPATCH (MAKE-ARRAY NIL 'ART-Q '(4 220)))
(SETUP-KEYBOARD-DISPATCH-TABLE EH-DISPATCH EH-DISPATCH-LIST))))
;; Call this when it is apparent that some hacker set things up wrong.
(DEFUN EH-BAD-HACKER (&REST ARGS)
(FORMAT T "~%~%Foo, a hacker has screwn up somewhere. Error:~%")
(DO AL ARGS (CDR AL) (NULL AL) (PRINC (CAR AL)) (TYO #\SP))
(TERPRI) (TERPRI))
;; Turn on error trapping mode.
(DEFUN ENABLE-TRAPPING (&OPTIONAL (X 1))
(SETQ %MODE-FLAGS (DPB X %%M-FLAGS-TRAP-ENABLE %MODE-FLAGS)))
(DEFUN TRAPPING-ENABLED-P NIL
(NOT (ZEROP (LDB %%M-FLAGS-TRAP-ENABLE %MODE-FLAGS))))
(DEFUN EH-P-PRIN1 (LOCATIVE &AUX)
(LET ((DTP (Q-DATA-TYPES (%P-DATA-TYPE LOCATIVE))))
(COND ((MEMQ DTP EH-GOOD-TYPES)
(OR (LISTP (ERRSET (PRIN1 (CAR LOCATIVE)) NIL))
(PRINC "<<Error in printing!>>")))
(T (FORMAT T "#<~A ~O>" DTP (%P-POINTER LOCATIVE))))))
;; Initialize the error handler at warm boot time.
(ADD-INITIALIZATION "EH-INITIALIZE" '(EH-INITIALIZE) '(WARM))
(DEFUN EH-INITIALIZE ()
(SETQ EH-RUNNING NIL)
(SETQ EH-ERRSET-STATUS NIL) ;Set to T if an errset exists and should be obeyed
(EH-ASSURE-TABLE-LOADED) ;Gets the right UCONS/UCADR TABLE file loaded.
(EH-ASSURE-TABLE-PROCESSED) ;Processes the contents of UCONS/UCADR TABLE.
;;; Waiting until the first error to do these things loses
;;; because they let other processes run, which could get errors and crash the machine.
)
;; This is the function that runs in the first level error handler
;; It is called only at boot time. From then on it just keeps coroutining.
(DEFUN LISP-ERROR-HANDLER (&AUX M SG SG2 CONDITION
(INHIBIT-SCHEDULING-FLAG T))
;; Return to boot code. We are called back by the first error.
(SETQ M (FUNCALL %CURRENT-STACK-GROUP-PREVIOUS-STACK-GROUP NIL))
(DO ((ERRSET-FLAG NIL NIL) ;These must be reinitialized each time through the loop!
(ERRSET-PRINT-MSG NIL NIL))
(NIL) ;Do forever, once for each error
(SETQ SG %CURRENT-STACK-GROUP-PREVIOUS-STACK-GROUP)
(EH-ASSURE-DISPATCH-SET-UP) ;Set up command dispatch table.
;; Compute and store the ETE for this error.
(SETF (SG-TRAP-TAG SG)
(COND (M M)
(T (CDR (ASSQ (SG-TRAP-MICRO-PC SG) EH-ERROR-TABLE)))))
(SETF (SG-PROCESSING-ERROR-FLAG SG) 0) ;Re-enable error trapping in that SG
(SETQ CONDITION (GET (CAR (SG-TRAP-TAG SG)) 'EH-SIGNAL))
(AND CONDITION (SETQ CONDITION (FUNCALL CONDITION SG (SG-TRAP-TAG SG))))
;; All branches of this COND must end in funcalling some other SG.
(SETQ M
(COND ((AND (NOT (SYMEVAL-IN-STACK-GROUP 'ERRSET SG))
(SETQ ERRSET-FLAG (SYMEVAL-IN-STACK-GROUP 'EH-ERRSET-STATUS SG))
(NOT (SETQ ERRSET-PRINT-MSG
(SYMEVAL-IN-STACK-GROUP 'EH-ERRSET-PRINT-MSG SG)))
(NOT (AND CONDITION (EH-CONDITION-HANDLED-P SG (CAR CONDITION)))))
;; If we are in an errset, and don't want the message, throw now.
(EH-THROW SG 'ERRSET-CATCH NIL T))
(T
;; Otherwise, obtain a second level error handler sg
;; and tell it what to work on.
(SETQ SG2 (OR (POP EH-FREE-SECOND-LEVEL-SG-LIST)
(MAKE-STACK-GROUP
(STRING-APPEND "SECOND-LEVEL-ERROR-HANDLER-"
(FORMAT NIL "~D"
(SETQ EH-SECOND-LEVEL-COUNT
(1+ EH-SECOND-LEVEL-COUNT))))
':SAFE 0
':SPECIAL-PDL-SIZE 1000))) ;Otherwise ZDT loses.
(STACK-GROUP-PRESET SG2 'EH-SECOND-LEVEL
SG M ERRSET-FLAG ERRSET-PRINT-MSG CONDITION)
(FUNCALL SG2))))))
;; Invoke the error handler to look at a particular stack group.
;; A window or process may also be supplied, and a stack group found from it.
;; Supplying NIL means find a process which is waiting to be looked at.
;; If a process is supplied or known, it is arrested while we are invoked.
;; This works differently from real errors; it just runs the error handler
;; in the same stack group and process that EH is called in; EH-RUNNING is NOT set.
;; The catch tag EH-EXIT is used to return from EH.
(DEFUN EH (&OPTIONAL PROCESS &AUX PKG SG ARREST-REASON
EH-ORIGINAL-FRAME EH-CURRENT-FRAME
EH-CONDITION-PROCEED-VALUE EH-CONDITION-PROCEED-FLAG)
(COND ((NULL PROCESS)
(DOLIST (P ACTIVE-PROCESSES)
(COND ((STRING-EQUAL (PROCESS-WHOSTATE (CAR P)) "Error")
(FORMAT T "~&Selecting ~S~%" (CAR P))
(<- (CAR P) ':SELECT)
(PROCESS-WAIT "Select" #'(LAMBDA () (EQ CURRENT-PROCESS SELECTED-PROCESS)))
(RETURN NIL)))))
(T
;; If arg is a window or stream, extract process from it.
(OR (MEMQ (TYPEP PROCESS) '(ARRAY PROCESS))
(SETQ PROCESS (FUNCALL PROCESS ':PROCESS)))
;; If arg is process or was converted to one, stop it.
(COND ((EQ (TYPEP PROCESS) 'PROCESS)
(<- PROCESS ':ARREST-REASON CURRENT-PROCESS)
(SETQ ARREST-REASON CURRENT-PROCESS)
(SETQ SG (PROCESS-STACK-GROUP PROCESS)))
(T (SETQ SG PROCESS PROCESS NIL)))
(SETQ EH-ORIGINAL-FRAME (SG-AP SG))
(SETQ EH-CURRENT-FRAME (EH-OUT-TO-INTERESTING-ACTIVE SG EH-ORIGINAL-FRAME))
;; Although we get the package each time around the r-e-p loop, we must get it
;; here as well, so that when the error message is printed it will be in the
;; right package.
(SETQ PKG (SYMEVAL-IN-STACK-GROUP 'PACKAGE SG)) ;Use same package as at point of error
(UNWIND-PROTECT
(PROGN
(*CATCH 'EH-QUIT
(*CATCH 'TOP-LEVEL
(PKG-BIND (IF (EQ (TYPEP PKG) 'PACKAGE) PKG ;If it looks reasonably trustworthy
"USER")
(OR (LISTP
(ERRSET (PROGN
(FORMAT T "~&~S Backtrace: " SG)
(EH-SHORT-BACKTRACE SG NIL 3)
(EH-SHOW-FUNCTION-AND-ARGS SG))))
(PRINC "<<Error printing frame!>>")))))
(*CATCH 'EH-EXIT (EH-COMMAND-LOOP SG (SG-TRAP-TAG SG))))
(AND ARREST-REASON (<- PROCESS ':REVOKE-ARREST-REASON ARREST-REASON))))))
;; This is the function at the top level in each second level error handler sg.
(DEFUN EH-SECOND-LEVEL (SG M ERRSET-FLAG ERRSET-PRINT-MSG CONDITION
&AUX PKG (ERRSET NIL) (EH-RUNNING T) (ETE (SG-TRAP-TAG SG))
BREAK-FLAG
(TERMINAL-IO (OR ERROR-HANDLER-IO
(SYMEVAL-IN-STACK-GROUP 'TERMINAL-IO SG)))
(STANDARD-INPUT SYN-TERMINAL-IO)
(STANDARD-OUTPUT SYN-TERMINAL-IO)
(QUERY-IO SYN-TERMINAL-IO)
;; In case we want to set CURRENT-PROCESS to nil.
(CURRENT-PROCESS CURRENT-PROCESS)
;; And some things will wonder what it had been.
(EH-REAL-CURRENT-PROCESS CURRENT-PROCESS)
EH-ORIGINAL-FRAME EH-CURRENT-FRAME
EH-CONDITION-PROCEED-VALUE EH-CONDITION-PROCEED-FLAG)
;; If error was in the scheduler, set CURRENT-PROCESS to nil
;; so that KBD-TYI won't wait for selection, etc.
(COND ((EQ SG SCHEDULER-STACK-GROUP)
(SETQ CURRENT-PROCESS NIL)))
(OR (NULL CURRENT-PROCESS) ;If not running in the scheduler, give us a run reason.
; in case we died after becoming inactive, before
; getting back to the scheduler.
(<- CURRENT-PROCESS ':RUN-REASON %CURRENT-STACK-GROUP))
;; Try to see if TERMINAL-IO is reasonable and if not fix it.
(COND ((NULL (ERRSET (FUNCALL TERMINAL-IO ':WHICH-OPERATIONS) NIL))
(SETQ TERMINAL-IO (TV-MAKE-STREAM CONSOLE-IO-PC-PPR))
(SETF (PC-PPR-OUTPUT-HOLD-FLAG CONSOLE-IO-PC-PPR) 0)
(TV-HOME CONSOLE-IO-PC-PPR)
(TV-CLEAR-EOL CONSOLE-IO-PC-PPR)
(FORMAT T " ---> TERMINAL-IO clobbered, using CONSOLE-IO-PC-PPR <---~%")))
(COND ((EQ (CAR M) ':BREAK)
(EH-RESTORE-SG-STATE SG) ;Restore state saved at FORCE-BREAK
(COND ((CADR M) ;If re-invoke, do so.
(EH-REINVOKE SG)))))
(*CATCH 'EH-QUIT
(*CATCH 'TOP-LEVEL
(PROGN
;; If we have a condition to signal, do so (in the debugged stack group)
;; and maybe return or restart if it says so.
(AND CONDITION
(LET ((TEM1 (EH-APPLY SG #'SIGNAL CONDITION))
(EH-CONDITION-PROCEED-FLAG T)
EH-CONDITION-PROCEED-VALUE)
(COND ((EQ (CAR TEM1) 'RETURN)
(SETQ EH-CONDITION-PROCEED-VALUE (CADR TEM1))
(EH-PROCEED SG ETE))
((EQ (CAR TEM1) 'ERROR-RESTART)
(EH-ERROR-RESTART SG ETE)))))
;; If non-printing errset, throw to it once condition is processed.
(AND ERRSET-FLAG (NOT ERRSET-PRINT-MSG)
(EH-THROW SG 'ERRSET-CATCH NIL))
;; Otherwise, decide whether to break or to go to top level.
(SETQ BREAK-FLAG (OR (NOT (FBOUNDP 'EH-BREAK-P)) (EH-BREAK-P SG ETE CONDITION)))
(SETQ EH-ORIGINAL-FRAME (SG-AP SG))
(SETQ EH-CURRENT-FRAME
(EH-NEXT-NTH-ACTIVE SG EH-ORIGINAL-FRAME
(- (OR (GET (CAR M) 'EH-BACKTRACE-SKIP) 0))))
(SETQ EH-CURRENT-FRAME (EH-OUT-TO-INTERESTING-ACTIVE SG EH-CURRENT-FRAME))
;; Although we get the package each time around the r-e-p loop, we must get it
;; here as well, so that when the error message is printed it will be in the
;; right package.
(SETQ PKG (SYMEVAL-IN-STACK-GROUP 'PACKAGE SG)) ;Use same package as at point of error
(PKG-BIND (IF (EQ (TYPEP PKG) 'PACKAGE) PKG ;If it looks reasonably trustworthy
"USER")
(OR (LISTP
(ERRSET (PROGN (COND ((EQ (CAR M) ':BREAK)
(TERPRI)
(PRINC ">>BREAK")
(TERPRI))
((OR ERRSET-FLAG (NULL BREAK-FLAG))
(TERPRI)
(EH-PRINT-ERROR-MESSAGE SG ETE T))
(T (EH-SHOW SG ETE))))
NIL))
(PRINC "<<Error printing error message!>>")))
(AND ERRSET-FLAG (NOT (EQ (CAR M) ':BREAK))
(EH-THROW SG 'ERRSET-CATCH NIL))
;; If this error isn't interesting to break on,
;; return to top level, or to innermost error break loop.
(OR BREAK-FLAG (EH-THROW-ONE-ERROR SG ETE)))))
(SETQ EH-LAST-SECOND-LEVEL-SG %CURRENT-STACK-GROUP)
(EH-COMMAND-LOOP SG ETE)) ;SG-TRAP-TAG is part of the state restored by
;EH-RESTORE-SG-STATE in case of BREAK. Thus, it does
;not win to have EH-COMMAND-LOOP refetch it.
;; Decide whether an error break loop is useful for this error.
(DEFUN EH-BREAK-P (SG IGNORE CONDITION)
(OR (NULL (SG-FOOTHOLD-DATA SG))
(NOT (SELECTQ (CAR CONDITION)
(:WRONG-NUMBER-OF-ARGUMENTS
(EQ (LET ((AP (EH-PREVIOUS-ACTIVE SG (SG-AP SG)))
(RP (SG-REGULAR-PDL SG)))
(RP-FUNCTION-WORD RP AP))
(FUNCTION *EVAL)))
((:UNDEFINED-VARIABLE :UNDEFINED-FUNCTION)
(EQ (LET ((AP (SG-AP SG))
(RP (SG-REGULAR-PDL SG)))
(RP-FUNCTION-WORD RP AP))
(FUNCTION *EVAL)))))))
(DEFUN EH-COMMAND-LOOP (EH-SG ETE &AUX CHAR FUNCTION SEXP
(EVALHOOK NIL) PKG
(BASE 8) (IBASE 8) (*NOPOINT NIL) (PACKAGE PACKAGE)
(KBD-TYI-HOOK 'EH-KBD-TYI-HOOK)
;; If you know why this is done, make this comment say why
(KBD-SIMULATED-CLOCK-FCN-LIST '(TV-BLINKER-CLOCK)))
;; If it was the mouse process that got the error, the user isn't
;; likely to be able to select this process, so just grab the keyboard
;; so as to have a chance of debugging. Also if an error occurs while
;; initializing the system, grab the keyboard since normal mechanisms
;; such as the mouse may be broken.
(COND ((OR (NOT (BOUNDP 'MOUSE-PROCESS)) ;Error during system booting
(EQ CURRENT-PROCESS MOUSE-PROCESS)
SYSTEM-BEING-INITIALIZED-FLAG)
(FORMAT T "~%---> Changing SELECTED-PROCESS from ~S to ~S <---~%"
SELECTED-PROCESS CURRENT-PROCESS)
(SETQ SELECTED-PROCESS CURRENT-PROCESS)))
;; Discard type-ahead - unless we are going to wait for the user to say (EH).
(AND (OR (NULL CURRENT-PROCESS)
(EQ CURRENT-PROCESS SELECTED-PROCESS))
(FUNCALL STANDARD-INPUT ':CLEAR-INPUT))
(DO ((NUMERIC-ARG)
(-)
(+ (SYMEVAL-IN-STACK-GROUP '- EH-SG))
(* (SYMEVAL-IN-STACK-GROUP '* EH-SG)))
(())
(SETQ PKG (SYMEVAL-IN-STACK-GROUP 'PACKAGE EH-SG)) ;Get package again in case user
(SETQ PACKAGE (IF (EQ (TYPEP PKG) 'PACKAGE) PKG ; does a PKG-GOTO
(PKG-FIND-PACKAGE "USER")))
;; Wait until we are selected, in a way that shows up in PEEK.
(COND ((AND CURRENT-PROCESS
(NEQ CURRENT-PROCESS SELECTED-PROCESS))
(PROCESS-WAIT "Error" #'(LAMBDA () (EQ CURRENT-PROCESS SELECTED-PROCESS)))
(OR (LISTP
(ERRSET (PROGN (EH-SHOW EH-SG ETE) NIL)))
(PRINC "<<Error printing error message!>>"))))
(*CATCH 'TOP-LEVEL
(*CATCH 'EH-QUIT
(PROGN
(OR NUMERIC-ARG
(FORMAT T "~&"))
;; Read the next control character or sexp, with combined rubout processing.
(MULTIPLE-VALUE (CHAR SEXP)
(EH-COMMAND-LOOP-READ))
;; If it's a control character, look it up.
(SETQ FUNCTION (AND CHAR (EH-COMMAND-LOOKUP CHAR)))
;; If it's a character, execute the definition or complain.
(COND ((EQ FUNCTION 'EH-NUMBER-COMMAND)
(SETQ NUMERIC-ARG
(LET ((DIGIT (- (LDB %%KBD-CHAR CHAR) #/0)))
(COND ((NULL NUMERIC-ARG) DIGIT)
(T (+ DIGIT (* 10. NUMERIC-ARG)))))))
(FUNCTION
(COND (NUMERIC-ARG
(FUNCALL FUNCTION EH-SG ETE NUMERIC-ARG))
(T (FUNCALL FUNCTION EH-SG ETE)))
(SETQ NUMERIC-ARG NIL))
(CHAR (PRINC "??"))
;; If there was no char, there was a sexp, so eval it.
(T
(LET ((RESULTS (EH-EVAL EH-SG (SETQ - SEXP))))
(SETQ + -)
(COND ((NEQ RESULTS EH-ERROR-FLAG)
(SETQ * (CAR RESULTS))
(MAPC 'PRINT RESULTS))))))
)))))
;; This is what KBD-TYI should do with all control characters read by anyone
;; inside the error handler. BREAK breaks, C-G throws to the eh top level,
;; C-Z is not special (unlike the usual situation).
(DEFUN EH-KBD-TYI-HOOK (CH)
(AND (OR (= CH (+ 400 #/G)) (= CH (+ 400 #/g)))
(PRINC "G") (THROW NIL TOP-LEVEL))
(COND ((= CH 201) (BREAK BREAK T) NIL)
(T CH)))
;; Read from STANDARD-INPUT either a control-character (or ? or Help)
;; or a s-expression. Return CHAR or return NIL and the s-expression.
(DEFUN EH-COMMAND-LOOP-READ ()
(PROG (CHAR SEXP FLAG)
RETRY
;; Read a character.
(SETQ CHAR (FUNCALL STANDARD-INPUT ':TYI))
;; Now, if the char is special, echo and return it.
(COND ((OR (LDB-TEST %%KBD-CONTROL-META CHAR)
(= CHAR #\HELP)
(= CHAR #/?))
(FORMAT T "~C" CHAR)
(RETURN CHAR))
((= CHAR #\RUBOUT) (GO RETRY))) ;Ignore rubouts
;; Otherwise, unread it and read an s-exp instead.
(FUNCALL STANDARD-INPUT ':UNTYI CHAR)
(COND ((AND (NOT RUBOUT-HANDLER)
(MEMQ ':RUBOUT-HANDLER (FUNCALL STANDARD-INPUT ':WHICH-OPERATIONS)))
(MULTIPLE-VALUE (SEXP FLAG)
(FUNCALL STANDARD-INPUT ':RUBOUT-HANDLER '((:FULL-RUBOUT :FULL-RUBOUT))
#'READ-FOR-TOP-LEVEL))
(AND (EQ FLAG ':FULL-RUBOUT) (GO RETRY))
(RETURN NIL SEXP))
;; If stream has no rubout handler, degrade gracefully.
((RETURN NIL (READ-FOR-TOP-LEVEL))))))
(DEFUN EH-COMMAND-LOOKUP (CHAR &AUX TEM)
(SETQ TEM (AR-2 EH-DISPATCH (LDB %%KBD-CONTROL-META CHAR) (LDB %%KBD-CHAR CHAR)))
(DO ()
((NLISTP TEM) TEM)
(SETQ TEM (AR-2 EH-DISPATCH (FIRST TEM) (SECOND TEM)))))
;CONDITION-HANDLERS is a list of handling specs, each of which
;contains first either a condition name, a list of such names, or
;NIL meaning all condition names, and second
;a function to call to handle the condition.
;When you signal a condition with (SIGNAL condition-name info info info...)
;condition-handlers is searched for an element that applies to this
;condition name, and that element's function is called
;with the same arguments that signal was given (however many there were).
;If the function's first value is NIL, this means that the condition
;has not been handled, and the remaining handlers on the list should
;be given the chance to look at it. Otherwise, the function's one
;or two values are returned by SIGNAL.
(declare (special condition-handlers))
(setq condition-handlers nil)
(defun signal (&rest args)
(multiple-value-call (signal-1 condition-handlers args)))
(defun signal-1 (handler-list condition-list &aux (cname (car condition-list)) tem1 tem2)
(do ((handler-list handler-list (cdr handler-list))
(h))
((null handler-list) nil)
(setq h (car handler-list))
(cond ((cond ((null (car h)) T)
((nlistp (car h))
(eq (car h) cname))
(T (memq cname (car h))))
(multiple-value (tem1 tem2)
(apply (cadr h) condition-list))
(and tem1 (return tem1 tem2))))))
(defmacro-displace condition-bind (handlers . body)
`(let ((condition-handlers (append ',handlers condition-handlers)))
. ,body))
;; Does a stack group have anything that could try to handle this condition?
(DEFUN EH-CONDITION-HANDLED-P (SG CONDITION)
(DOLIST (H (SYMEVAL-IN-STACK-GROUP 'CONDITION-HANDLERS SG))
(AND (COND ((NULL (CAR H)) T)
((NLISTP (CAR H)) (EQ (CAR H) CONDITION))
(T (MEMQ CONDITION (CAR H))))
(RETURN T))))
;; Utility function used by the top level, and various commands.
;; Print out the error message of the error.
(DEFUN EH-PRINT-ERROR-MESSAGE (SG ETE &OPTIONAL BRIEF-FLAG
&AUX (PRINLEVEL EH-PRINLEVEL)
(PRINLENGTH EH-PRINLENGTH)
INFORM)
(EH-PRINT-DEBUGGING-ERROR-MESSAGE SG ETE) ;For debugging only.
(SETQ INFORM (GET (CAR ETE) 'EH-INFORM))
(COND ((NULL INFORM)
(FORMAT T "There is no error message provided for this error!~%"))
(T (FUNCALL INFORM SG ETE)))
(COND ((NOT BRIEF-FLAG)
(FORMAT T "~&While in the function ")
(EH-SHORT-BACKTRACE SG
NIL
(OR (GET (CAR ETE) 'EH-BACKTRACE-LENGTH)
EH-ERROR-MESSAGE-BACKTRACE-LENGTH)
(OR (GET (CAR ETE) 'EH-BACKTRACE-SKIP)
0))
(TERPRI)
(AND ;Check for user message hook
(SETQ INFORM (SYMEVAL-IN-STACK-GROUP 'ERROR-MESSAGE-HOOK SG))
(FUNCALL INFORM)))) ;Call user function to explain
(FORMAT T "~&")) ;Compensate if it forgot to CRLF
(DEFUN EH-PRINT-DEBUGGING-ERROR-MESSAGE (SG ETE &AUX TEM FLAG)
(COND ((NEQ (CAR ETE) 'FERROR)
(FORMAT T ">>TRAP ~A ~A"
(SG-TRAP-MICRO-PC SG) ETE)
(COND ;; Process the micro-stack of the active frame, if any
((LDB-TEST %%LP-EXS-MICRO-STACK-SAVED
(RP-EXIT-WORD (SG-REGULAR-PDL SG) (SG-AP SG)))
(DO I (SG-SPECIAL-PDL-POINTER SG) (1- I) NIL
(LET ((PC (AREF (SG-SPECIAL-PDL SG) I)))
(SETQ TEM (ASSQ (1- (%POINTER PC)) EH-CALLS-SUB-LIST))
(COND (TEM
(OR FLAG (FORMAT T " ->"))
(SETQ FLAG T)
(FORMAT T " ~A " (CDR TEM)))))
(OR (ZEROP (%P-FLAG-BIT (ALOC (SG-SPECIAL-PDL SG) I)))
(RETURN NIL)))))
(TERPRI))
(T (FORMAT T ">>ERROR: "))))
(DEFUN EH-CONSTANT-FORM-P (X)
(COND ((SYMBOLP X)
(OR (EQ X 'T) (NULL X)))
((LISTP X) (EQ (CAR X) 'QUOTE))
(T T)))
;; This is the function used by proceed routines to ask for
;; a form to read and eval. When proceeding is done under control
;; of a condition handler, this function arranges to get the
;; object supplied by the handler instead of asking.
(DEFUN EH-GET-OBJECT (PROMPT &AUX FORM OBJECT)
(COND ((EQ EH-CONDITION-PROCEED-FLAG T)
(SETQ EH-CONDITION-PROCEED-FLAG 'GOBBLED)
EH-CONDITION-PROCEED-VALUE)
((EQ EH-CONDITION-PROCEED-FLAG 'GOBBLED)
(FERROR NIL "EH-GET-OBJECT called twice by proceed routine for ~S"
(CAR (SG-TRAP-TAG EH-SG))))
(T
(DO ()
((PROGN
(FORMAT T "~A~%" PROMPT)
(SETQ FORM (READ-FOR-TOP-LEVEL))
(SETQ OBJECT (CAR (EH-EVAL EH-SG FORM)))
(TERPRI)
(COND ((EH-CONSTANT-FORM-P FORM) T)
((NEQ CURRENT-PROCESS SELECTED-PROCESS) T)
(T (FORMAT T "The object is ~S, ok? " OBJECT)
(Y-OR-N-P))))
OBJECT)))))
;; Backtrace.
;; These initial implementations make no attempt to censor the stack,
;; but clearly something will be needed for interpreted functions.
;; Both versions take arguments the same way. The first is the SG, and the
;; second is ignored so that the functions may be used as commands.
;; They will print no more than N frames if N is present.
;; If SKIP is nonzero, the first that many frames are skipped before the
;; N frames are printed. Used to put them in curly brackets, but that was
;; fairly useless.
;; This prints out like the Maclisp BAKTRACE, and does not TERPRI at beginning
;; nor end.
(DEFUN EH-SHORT-BACKTRACE (SG IGNORE &OPTIONAL (N 777777) (SKIP 0) UNINTERESTING-FLAG)
(DO ((AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG AP))
(RP (SG-REGULAR-PDL SG)))
((OR (NULL AP) (MINUSP (SETQ SKIP (1- SKIP))))
(DO ((AP AP (FUNCALL (COND (UNINTERESTING-FLAG 'EH-PREVIOUS-ACTIVE)
(T 'EH-PREVIOUS-INTERESTING-ACTIVE))
SG AP))
(I 0 (1+ I)))
((OR (>= I N) (NULL AP)) NIL)
(OR (ZEROP I) (PRINC " "))
(PRIN1 (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP AP)))))))
(DEFUN EH-FULL-BACKTRACE (SG IGNORE &OPTIONAL (N 777777) (SKIP 0) UNINTERESTING-FLAG)
(DO ((AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG AP)))
((OR (NULL AP) (MINUSP (SETQ SKIP (1- SKIP))))
(DO ((AP AP (FUNCALL (COND (UNINTERESTING-FLAG 'EH-PREVIOUS-ACTIVE)
(T 'EH-PREVIOUS-INTERESTING-ACTIVE))
SG AP))
(I 0 (1+ I)))
((OR (>= I N) (NULL AP)) NIL)
(EH-PRINT-FUNCTION-AND-ARGS SG AP)))))
(DEFUN EH-FULL-BACKTRACE-UNINTERESTING (SG IGNORE &OPTIONAL (N 777777) (SKIP 0))
(EH-FULL-BACKTRACE SG NIL N SKIP T))
;Return list of the function and args that were invoked (as best as it can).
(DEFUN EH-GET-FRAME-FUNCTION-AND-ARGS (SG AP
&AUX FUNCTION NARGS-SUPPLIED
(RP (SG-REGULAR-PDL SG))
LEXPR-CALL REST-ARG-P REST-ARG-VALUE ANS)
(SETQ FUNCTION (RP-FUNCTION-WORD RP AP)
NARGS-SUPPLIED (RP-NUMBER-ARGS-SUPPLIED RP AP)) ;Really slots on stack
(MULTIPLE-VALUE (REST-ARG-VALUE REST-ARG-P LEXPR-CALL)
(EH-REST-ARG-VALUE SG AP))
;; Print the individual args.
(DO ((I NARGS-SUPPLIED (1- I))) ;CONS THEM UP IN REVERSE ORDER
((ZEROP I))
(SETQ ANS (CONS (AREF RP (+ AP I)) ANS))) ;+1 -1
;; NCONC the rest arg if any.
(COND ((OR REST-ARG-P LEXPR-CALL)
(SETQ ANS (NCONC ANS (APPEND REST-ARG-VALUE NIL)))))
(CONS FUNCTION ANS))
(DEFUN EH-PRINT-FUNCTION-AND-ARGS (SG AP &AUX FUNCTION (RP (SG-REGULAR-PDL SG)))
(SETQ FUNCTION (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP AP)))
(ERRSET (FORMAT T "~%~S:" FUNCTION) NIL)
(AND (= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER)
(FORMAT T " (P.C. = ~O)" ;Note that this displays the return-pc,
(RP-EXIT-PC RP AP))) ; which is one greater than the D-LAST.
(TERPRI)
(EH-PRINT-FRAME-ARGS SG AP 3))
;; Returns T if it displayed the rest arg, NIL otherwise. See EH-SHOW-ALL-MACRO.
(DEFUN EH-PRINT-FRAME-ARGS (SG AP INDENT
&AUX (PRINLEVEL EH-PRINLEVEL)
(PRINLENGTH EH-PRINLENGTH)
FUNCTION NARGS-SUPPLIED NARGS-TO-PRINT
(RP (SG-REGULAR-PDL SG))
NARGS-EXPECTED NARGS-REQUIRED
LEXPR-CALL REST-ARG-P REST-ARG-VALUE)
(SETQ FUNCTION (RP-FUNCTION-WORD RP AP)
NARGS-SUPPLIED (RP-NUMBER-ARGS-SUPPLIED RP AP))
(COND ((OR (= (%DATA-TYPE FUNCTION) DTP-FEF-POINTER) (LISTP FUNCTION))
(SETQ NARGS-REQUIRED
(LDB %%ARG-DESC-MIN-ARGS (ARGS-INFO FUNCTION)))
(SETQ NARGS-EXPECTED
(LDB %%ARG-DESC-MAX-ARGS (ARGS-INFO FUNCTION)))))
(MULTIPLE-VALUE (REST-ARG-VALUE REST-ARG-P LEXPR-CALL)
(EH-REST-ARG-VALUE SG AP))
(SETQ NARGS-TO-PRINT (EH-NUMBER-OF-SPREAD-ARGS SG AP))
;; Print the individual args.
(DOTIMES (I NARGS-TO-PRINT)
(AND (= I NARGS-SUPPLIED)
(COND ((AND NARGS-REQUIRED (< I NARGS-REQUIRED))
(FORMAT T " --Missing args:--~%"))
(T
(FORMAT T " --Defaulted args:--~%")))) ;These "args" weren't supplied
(AND NARGS-EXPECTED (= I NARGS-EXPECTED) ;Called with too many args
(FORMAT T " --Extraneous args:--~%"))
(FORMAT T "~VTArg ~D" INDENT I)
(EH-DISPLAY-ARG-NAME " (~A)" FUNCTION I)
;; Print the arg value unless the arg is missing (val is garbage).
(OR (AND NARGS-REQUIRED
(> NARGS-REQUIRED NARGS-SUPPLIED)
( I NARGS-SUPPLIED))
(PROGN (PRINC ": ")
(ERRSET (PRIN1 (AREF RP (+ AP I 1))) NIL)))
(TERPRI))
;; Print the rest arg if any.
(COND (REST-ARG-P
(FORMAT T "~VTRest arg" INDENT)
(EH-DISPLAY-LOCAL-NAME " (~A)" FUNCTION 0)
(PRINC ": "))
(LEXPR-CALL
(FORMAT T "~VTExtraneous Rest Arg: " INDENT)))
(COND ((OR REST-ARG-P LEXPR-CALL)
(ERRSET (PRIN1 REST-ARG-VALUE) NIL)
(TERPRI)))
REST-ARG-P)
(DEFUN EH-DISPLAY-LOCAL-NAME (FORMAT-STRING FUNCTION LOCALNO &AUX NAME)
(SETQ NAME (EH-LOCAL-NAME FUNCTION LOCALNO))
(AND NAME (FORMAT T FORMAT-STRING NAME)))
(DEFUN EH-DISPLAY-ARG-NAME (FORMAT-STRING FUNCTION ARGNO &AUX NAME)
(SETQ NAME (EH-ARG-NAME FUNCTION ARGNO))
(AND NAME (FORMAT T FORMAT-STRING NAME)))
;; Commands in the dispatch table. These are given the SG and the ETE.
;; Any command which wants to return out of the error handler should
;; do a throw to EH-FINISHED after restarting the erring stack group.
;; Basic commands for inspecting specific stack frames.
;; UP means closer to the top of the stack, DOWN means the base of the stack.
;; Control-P, <^>
(DEFUN EH-UP-STACK (SG IGNORE &OPTIONAL COUNT SHOW-ALL-FLAG REVERSE-FLAG UNINTERESTING-FLAG
&AUX AP COUNT1)
(SETQ COUNT1 (OR COUNT 1))
(AND REVERSE-FLAG (SETQ COUNT1 (- COUNT1)))
(SETQ AP (FUNCALL (COND (UNINTERESTING-FLAG 'EH-NEXT-NTH-ACTIVE)
(T 'EH-NEXT-NTH-INTERESTING-ACTIVE))
SG EH-CURRENT-FRAME COUNT1))
(COND ((= AP EH-CURRENT-FRAME)
(FORMAT T (COND (REVERSE-FLAG
"You are already at the bottom of the stack.~%")
(T "You are already at the top of the stack.~%"))))
(T (SETQ EH-CURRENT-FRAME AP)
(COND ((NOT SHOW-ALL-FLAG) (EH-SHOW-FUNCTION-AND-ARGS SG))
(T (EH-SHOW-ALL SG)))))
NIL)
;; Control-N, <line>
(DEFUN EH-DOWN-STACK (SG ETE &OPTIONAL COUNT)
(EH-UP-STACK SG ETE COUNT NIL T))
;; Meta-P.
(DEFUN EH-UP-STACK-ALL (SG ETE &OPTIONAL COUNT)
(EH-UP-STACK SG ETE COUNT T))
;; Meta-N.
(DEFUN EH-DOWN-STACK-ALL (SG ETE &OPTIONAL COUNT)
(EH-UP-STACK SG ETE COUNT T T))
;; Meta->.
(DEFUN EH-TOP-STACK (SG &REST IGNORE)
(SETQ EH-CURRENT-FRAME (EH-OUT-TO-INTERESTING-ACTIVE SG (SG-AP SG)))
(EH-SHOW-FUNCTION-AND-ARGS SG)
NIL)
;; Meta-<.
(DEFUN EH-BOTTOM-STACK (SG &REST IGNORE)
(SETQ EH-CURRENT-FRAME
(DO ((AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG AP))
(PREV-AP NIL AP))
((NULL AP) PREV-AP)))
(EH-SHOW-FUNCTION-AND-ARGS SG)
NIL)
;; Control-Meta-P.
(DEFUN EH-UP-STACK-UNINTERESTING (SG ETE &OPTIONAL COUNT)
(EH-UP-STACK SG ETE COUNT NIL NIL T))
;; Control-Meta-N.
(DEFUN EH-DOWN-STACK-UNINTERESTING (SG ETE &OPTIONAL COUNT)
(EH-UP-STACK SG ETE COUNT NIL T T))
;; Control-Meta-U.
(DEFUN EH-UP-TO-INTERESTING (SG IGNORE &OPTIONAL IGNORE)
(SETQ EH-CURRENT-FRAME (EH-OUT-TO-INTERESTING-ACTIVE SG EH-CURRENT-FRAME))
(EH-SHOW-FUNCTION-AND-ARGS SG)
NIL)
;; Control-L, form.
(DEFUN EH-CLEAR-AND-SHOW (SG ETE &REST IGNORE)
(FUNCALL STANDARD-OUTPUT ':CLEAR-SCREEN)
(EH-PRINT-ERROR-MESSAGE SG ETE)
(EH-SHOW-FUNCTION-AND-ARGS SG)
NIL)
;; This is how the error message is printed when the error handler starts up.
(DEFUN EH-SHOW (SG ETE &REST IGNORE)
(TERPRI)
(EH-PRINT-ERROR-MESSAGE SG ETE)
(EH-SHOW-FUNCTION-AND-ARGS SG)
NIL)
;; Meta-L.
(DEFUN EH-CLEAR-AND-SHOW-ALL (SG &REST IGNORE)
(EH-SHOW-ALL SG)
NIL)
;; EH-SHOW-FUNCTION-AND-ARGS is reguar printing tty stuff.
;; EH-SHOW-ALL clears the screen and then fill is up.
;; The guts of the commands on the previous page.
(DEFUN EH-SHOW-FUNCTION-AND-ARGS (SG)
(EH-PRINT-FUNCTION-AND-ARGS SG EH-CURRENT-FRAME))
(DEFUN EH-SHOW-ALL (SG &AUX RP FUNCTION)
(SETQ RP (SG-REGULAR-PDL SG)
FUNCTION (RP-FUNCTION-WORD RP EH-CURRENT-FRAME))
(FUNCALL STANDARD-OUTPUT ':CLEAR-SCREEN)
(SELECT (%DATA-TYPE FUNCTION)
(DTP-FEF-POINTER (EH-SHOW-ALL-MACRO SG RP FUNCTION))
(OTHERWISE (EH-SHOW-FUNCTION-AND-ARGS SG))))
(DEFUN EH-SHOW-ALL-MACRO (SG RP FUNCTION &AUX N-LOCALS PC-NOW NAME REST-ARG-PRINTED
PC-PPR LH PC-PPR-HEIGHT TEM LIM-PC)
(SETQ N-LOCALS (FEF-NUMBER-OF-LOCALS FUNCTION)
NAME (FEF-NAME FUNCTION)
PC-NOW (RP-EXIT-PC RP EH-CURRENT-FRAME)
LIM-PC (COMPILER:DISASSEMBLE-LIM-PC FUNCTION))
;; Print the header, including the underlined function name
(FORMAT T "~14XMacro-compiled frame. Frame address = ~O~%~%~S~2%"
EH-CURRENT-FRAME NAME)
;; Print the arguments, including the rest-arg which is the first local
(SETQ REST-ARG-PRINTED (EH-PRINT-FRAME-ARGS SG EH-CURRENT-FRAME 0))
;; Print the rest of the locals
(DO ((I 0 (1+ I))
(J (+ (RP-LOCAL-BLOCK-ORIGIN RP EH-CURRENT-FRAME) EH-CURRENT-FRAME) (1+ J)))
((>= I N-LOCALS))
(COND ((NOT (AND REST-ARG-PRINTED (ZEROP I))) ;Don't show rest arg twice
(FORMAT T "Local ~D" I)
(EH-DISPLAY-LOCAL-NAME " (~A)" FUNCTION I)
(LET ((PRINLEVEL EH-PRINLEVEL)
(PRINLENGTH EH-PRINLENGTH))
(FORMAT T ": ~S~%" (AREF RP J))))))
(FORMAT T "~%Disassembled code:")
;; Figure out how many instructions will fit in the pc ppr we are using.
(COND ((MEMQ ':PC-PPR (FUNCALL STANDARD-OUTPUT ':WHICH-OPERATIONS))
(SETQ PC-PPR (FUNCALL STANDARD-OUTPUT ':PC-PPR))
(SETQ PC-PPR-HEIGHT (- (PC-PPR-BOTTOM-LIMIT PC-PPR) ;The -20 is to prevent
(PC-PPR-TOP-MARGIN PC-PPR) ; **MORE** from going off.
20)
LH (PC-PPR-LINE-HEIGHT PC-PPR))
(MULTIPLE-VALUE (NIL TEM) (FUNCALL STANDARD-OUTPUT ':READ-CURSORPOS ':PIXEL))
(SETQ TEM (- (// (- PC-PPR-HEIGHT TEM) LH) 3)))) ;Number of lines to play with.
;; Disassemble assuming we have TEM lines of space to use.
(SETQ TEM (MAX EH-DISASSEMBLE-INSTRUCTION-COUNT (OR TEM 0)))
(DO ((I 0 (1+ I))
(PC (MAX (FEF-INITIAL-PC FUNCTION) (- PC-NOW (// TEM 2)))
(+ PC (COMPILER:DISASSEMBLE-INSTRUCTION-LENGTH FUNCTION PC))))
((OR (>= I TEM) (>= PC LIM-PC))
(COND ((= PC PC-NOW) ;If arrow should point after all code,
(TERPRI) (PRINC "=> ")))
)
(TERPRI)
(PRINC (COND ((= PC PC-NOW) "=> ")
(T " ")))
(COMPILER:DISASSEMBLE-INSTRUCTION FUNCTION PC)))
;; Other commands.
;; Control-Z.
(DEFUN EH-TOP-LEVEL-THROW (SG ETE &OPTIONAL (COUNT 1))
ETE COUNT
(COND ((NULL EH-RUNNING)
;; Explicitly invoked error handler => return from it.
(*THROW 'EH-EXIT NIL))
((EQ SG SCHEDULER-STACK-GROUP)
(FORMAT T "~&Restarting the scheduler.")
(COND (EH-REAL-CURRENT-PROCESS
(PROCESS-BLAST EH-REAL-CURRENT-PROCESS)
(FORMAT T "~%Blasting ~S so this won't happen again" EH-REAL-CURRENT-PROCESS)))
(STACK-GROUP-PRESET SG (FUNCTION PROCESS-SCHEDULER))
(EH-RUN-SG-GOODBYE SG))
((EQ CURRENT-PROCESS KBD-INTERRUPT-PROCESS)
(FORMAT T "~&Restarting the keyboard interrupt process.")
(PROCESS-PRESET KBD-INTERRUPT-PROCESS 'KBD-PROCESS-TOP-LEVEL)
(EH-RUN-SG-GOODBYE SG))
(T
(OR (EQ SG (PROCESS-INITIAL-STACK-GROUP CURRENT-PROCESS))
(EH-UNWIND-SG SG %CURRENT-STACK-GROUP NIL NIL))
(EH-THROW (PROCESS-INITIAL-STACK-GROUP CURRENT-PROCESS) 'TOP-LEVEL NIL)))
NIL)
;; Meta-Z command. Throw one level of error handler in this stack group.
(DEFUN EH-THROW-ONE-ERROR (SG ETE)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL))
((SG-FOOTHOLD-DATA SG)
(EH-THROW SG 'EH-FOOTHOLD NIL))
(T (EH-TOP-LEVEL-THROW SG ETE))))
;; Control-T.
(DEFUN EH-THROW-COMMAND (SG &REST IGNORE)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL)))
(FORMAT T "Throw a value to a tag.~%")
(EH-THROW SG
(EH-GET-OBJECT "What is the tag?")
(EH-GET-OBJECT "What value do you wish to throw?"))
NIL)
;; ?, <help>
(DEFUN EH-HELP (&REST IGNORE)
(FORMAT T "~&~A~%"
" You are in the error handler. If you type in a Lisp form, it will be
evaluated, and the result printed. You may also type one of the following:
<help> or ? gets this text. To just get back to top level, type a Z.
N or <line> goes down a frame, P or <return> goes up. N and P
are similar but give more info. L or <form> clears screen and retypes info,
L clears screen and types more info. < goes to top of stack, >
goes to the bottom.
B gives a brief backtrace, B a fuller one. A prints the arglist of the
function in the current frame.
N, P and B resemble N, P and B except that they show
all the internal EVALs, PROGs, CONDs, etc. of interpreted code.
A prints an argument to the current function, and sets * to be that argument
to let you do more complicated things with it. + is set to a locative to that argument,
should you want to modify it. L does likewise for the function's locals.
R returns a value from the current frame. R offers to reinvoke the current
frame with the originally supplied arguments (as best as they can be determined).
T throws to a specific tag.
C corrects the error and continues; it may ask you to input a value.
C continues from an ERROR-RESTART special form.
Z is like Z, but when there are recursive errors it only pops one level.
While in the error hander, G quits back to the error handler top level."
))
;; Control-R.
(DEFUN EH-RETURN-A-VALUE (SG &REST IGNORE)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL)))
(FORMAT T "Return a value from the function ~S.~%"
(EH-FUNCTION-NAME (RP-FUNCTION-WORD (SG-REGULAR-PDL SG) EH-CURRENT-FRAME)))
(EH-UNWIND-TO-FRAME SG EH-CURRENT-FRAME T
(EH-GET-OBJECT "Form to evaluate and return:"))
NIL)
;; Meta-R.
(DEFUN EH-RETURN-MANY-VALUES (&REST IGNORE)
);WRITE THIS
;; Control-Meta-R
(DEFUN EH-RETURN-REINVOKATION (SG &REST IGNORE &AUX FORM OBJECT)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL)))
(FORMAT T "Evaluating ~S, OK?"
(SETQ FORM (EH-GET-FRAME-FUNCTION-AND-ARGS SG EH-CURRENT-FRAME)))
(COND ((Y-OR-N-P)
(EH-UNWIND-TO-FRAME SG EH-CURRENT-FRAME NIL NIL) ;Unwind other crap, so as to
;unlock things, establish correct environment, etc. Unfortunately, it doesnt
;quite win, ie the frame being returnned from is not unwound until
;after the new value has been obtained.
(SETQ OBJECT (CAR (EH-APPLY EH-SG (CAR FORM) (CDR FORM)))) ;reinvoke the frob.
(EH-UNWIND-TO-FRAME SG EH-CURRENT-FRAME T OBJECT))) ;Return the value.
NIL)
;; Control-A.
(DEFUN EH-ARGLIST (SG &REST IGNORE)
(LET ((FUNCTION (RP-FUNCTION-WORD (SG-REGULAR-PDL SG) EH-CURRENT-FRAME)))
(FORMAT T "Argument list for ~S is ~A.~%"
(EH-FUNCTION-NAME FUNCTION)
(ARGLIST FUNCTION)))
NIL)
;; Control-C.
(DEFUN EH-PROCEED (SG ETE &REST IGNORE &AUX TEM OBJ)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL)))
(COND ((AND (EQ (CAR ETE) 'FERROR)
(CADR ETE))
(SETQ OBJ (COND ((SETQ TEM (GET (FOURTH ETE) 'EH-PROCEED)) ;Condition's property
(FUNCALL TEM SG ETE))
(T
(AND (EQ (CADR ETE) T)
(EH-GET-OBJECT
"Form to evaluate and return from FERROR//CERROR:")))))
;; Now restart the stack group, causing CERROR to return OBJ
(WITHOUT-INTERRUPTS
(EH-FREE %CURRENT-STACK-GROUP)
(FUNCALL SG OBJ)))
((AND (EQ (CAR ETE) ':BREAK) ;Break
(CADR ETE)) ; and want retry.
(WITHOUT-INTERRUPTS ;Everything set at EH-SECOND-LEVEL. Do it.
(EH-FREE %CURRENT-STACK-GROUP)
(FUNCALL SG OBJ)))
(T
(LET ((FUNCTION (GET (FIRST ETE) 'EH-PROCEED)))
(COND ((NULL FUNCTION)
(FORMAT T "There is no way to proceed from this error.~%"))
(T ;; Call the specific proceed routine. It should call EH-RESTART
;; to mung the micro-stack appropriately.
(FUNCALL FUNCTION SG ETE)
;; Now restart the stack group, as that routine left it.
(SETF (SG-CURRENT-STATE SG) SG-STATE-RESUMABLE)
(WITHOUT-INTERRUPTS
(EH-FREE %CURRENT-STACK-GROUP)
(FUNCALL SG)))))))
NIL)
;; Meta-C.
(DEFUN EH-ERROR-RESTART (SG &REST IGNORE)
(COND ((NULL EH-RUNNING) (*THROW 'EH-QUIT NIL)))
(COND ((OR EH-CONDITION-PROCEED-FLAG
(Y-OR-N-P "Are you SURE you want to restart? "))
(EH-THROW SG 'ERROR-RESTART NIL)))
(FORMAT T "Flushed.~%"))
;; Control-S.
(DEFUN EH-SEARCH (SG IGNORE &OPTIONAL IGNORE FLAG &AUX KEY AP)
(FORMAT T "String to search for (end with RETURN):~%")
(SETQ KEY (READLINE))
(SETQ AP
(DO ((AP (SG-AP SG) (EH-PREVIOUS-ACTIVE SG AP))
(RP (SG-REGULAR-PDL SG))
(NAME)
)
((NULL AP) NIL)
(SETQ NAME (EH-FUNCTION-NAME (RP-FUNCTION-WORD RP AP)))
(SETQ NAME
(COND ((STRINGP NAME) NAME)
((SYMBOLP NAME) (STRING NAME))
(T (FORMAT NIL "~S" NAME))))
(AND (STRING-SEARCH KEY NAME)
(RETURN AP))))
(COND ((NULL AP)
(FORMAT T "Search failed.~%"))
(T
(SETQ EH-CURRENT-FRAME AP)
(COND ((NOT FLAG) (EH-SHOW-FUNCTION-AND-ARGS SG))
(T (EH-SHOW-ALL SG))))))
;; Meta-S.
(DEFUN EH-SEARCH-AND-SHOW-ALL (SG ETE &OPTIONAL (COUNT 1))
(EH-SEARCH SG ETE COUNT T))
;; Control-Meta-A
(DEFUN EH-GET-ARG (SG IGNORE &OPTIONAL (ARG 0) &AUX RP TEM)
(SETQ RP (SG-REGULAR-PDL SG)
TEM (+ ARG 1 EH-CURRENT-FRAME)
+ (AP-1 RP TEM)
* (AR-1 RP TEM))
(FORMAT T "~&~S" *))
;; Control-Meta-L
(DEFUN EH-GET-LOCAL (SG IGNORE &OPTIONAL (ARG 0) &AUX RP TEM)
(SETQ RP (SG-REGULAR-PDL SG)
TEM (+ ARG EH-CURRENT-FRAME (RP-LOCAL-BLOCK-ORIGIN RP EH-CURRENT-FRAME))
+ (AP-1 RP TEM)
* (AR-1 RP TEM))
(FORMAT T "~&~S" *))
;; The initial dispatch table.
(SETQ EH-DISPATCH-LIST '(
( (:REPEAT 77 NIL) ;Sail graphics.
EH-HELP ;?
(:REPEAT 105 NIL)
NIL ;Backnext.
EH-HELP ;Help.
NIL ;Rubout.
NIL ;Backspace.
NIL ;Tab.
EH-DOWN-STACK ;Line.
NIL ;Vt.
EH-CLEAR-AND-SHOW ;Form.
EH-UP-STACK ;Return.
(:REPEAT 2 NIL))
;; Control characters
(
(:REPEAT 40 NIL)
(:REPEAT 10 NIL)
NIL ;(
NIL ;)
(:REPEAT 6 NIL)
(:REPEAT 12 EH-NUMBER-COMMAND) ;digits
(:REPEAT 2 NIL)
NIL ;<
NIL ;=
NIL ;>
NIL ;?
NIL ;@
EH-ARGLIST ;A
EH-SHORT-BACKTRACE ;B
EH-PROCEED ;C
NIL ;D
NIL ;E
NIL ;F
NIL ;G
NIL ;H
NIL ;I
NIL ;J
NIL ;K
EH-CLEAR-AND-SHOW ;L
NIL ;M
EH-DOWN-STACK ;N
NIL ;O
EH-UP-STACK ;P
NIL ;Q
EH-RETURN-A-VALUE ;R
EH-SEARCH ;S
EH-THROW-COMMAND ;T
NIL ;U
NIL ;V
NIL ;W
NIL ;X
NIL ;Y
EH-TOP-LEVEL-THROW ;Z
(:REPEAT 6 NIL)
(:REPEAT-EVAL 32 (LIST 1 (+ 101 SI:RPCNT))) ;Map lower case into upper case.
(:REPEAT 5 NIL)
(:REPEAT 7 NIL)
NIL
(:REPEAT 5 NIL)
NIL
(:REPEAT 2 NIL))
;; Meta characters
(
(:REPEAT 40 NIL)
(:REPEAT 10 NIL)
NIL ;(
NIL ;)
(:REPEAT 6 NIL)
(:REPEAT 12 EH-NUMBER-COMMAND) ;digits
(:REPEAT 2 NIL)
EH-TOP-STACK ;<
NIL ;=
EH-BOTTOM-STACK ;>
NIL ;?
NIL ;@
NIL ;A
EH-FULL-BACKTRACE ;B
EH-ERROR-RESTART ;C
NIL ;D
NIL ;E
NIL ;F
NIL ;G
NIL ;H
NIL ;I
NIL ;J
NIL ;K
EH-CLEAR-AND-SHOW-ALL ;L
NIL ;M
EH-DOWN-STACK-ALL ;N
NIL ;O
EH-UP-STACK-ALL ;P
NIL ;Q
EH-RETURN-MANY-VALUES ;R
EH-SEARCH-AND-SHOW-ALL ;S
NIL ;T
NIL ;U
NIL ;V
NIL ;W
NIL ;X
NIL ;Y
EH-THROW-ONE-ERROR ;Z
(:REPEAT 6 NIL)
(:REPEAT-EVAL 32 (LIST 2 (+ 101 SI:RPCNT))) ;Map lower case into upper case.
(:REPEAT 5 NIL)
(:REPEAT 7 NIL)
NIL
(:REPEAT 5 NIL)
NIL
(:REPEAT 2 NIL))
;Control-Meta characters
(
(:REPEAT 40 NIL)
(:REPEAT 10 NIL)
NIL ;(
NIL ;)
(:REPEAT 6 NIL)
(:REPEAT 12 EH-NUMBER-COMMAND) ;digits
(:REPEAT 2 NIL)
NIL ;<
NIL ;=
NIL ;>
NIL ;?
NIL ;@
EH-GET-ARG ;A
EH-FULL-BACKTRACE-UNINTERESTING ;B
NIL ;C
NIL ;D
NIL ;E
NIL ;F
NIL ;G
NIL ;H
NIL ;I
NIL ;J
NIL ;K
EH-GET-LOCAL ;L
NIL ;M
EH-DOWN-STACK-UNINTERESTING ;N
NIL ;O
EH-UP-STACK-UNINTERESTING ;P
NIL ;Q
EH-RETURN-REINVOKATION ;R
NIL ;S
NIL ;T
EH-UP-TO-INTERESTING ;U
NIL ;V
NIL ;W
NIL ;X
NIL ;Y
NIL ;Z
(:REPEAT 6 NIL)
(:REPEAT-EVAL 32 (LIST 3 (+ 101 SI:RPCNT))) ;MAP LOWER CASE INTO UPPER CASE
(:REPEAT 5 NIL)
(:REPEAT 7 NIL)
NIL
(:REPEAT 5 NIL)
NIL
(:REPEAT 2 NIL))
))
;;; Find the value of a symbol in the binding environment of a specified stack group.
;;; Note that this cannot get an error even if the sg is in some funny state, unlike
;;; EH-EVAL.
(DEFUN SYMEVAL-IN-STACK-GROUP (SYM SG)
(COND ((EQ SG %CURRENT-STACK-GROUP) (SYMEVAL SYM))
(T
(DO-NAMED RESULT
((VCL (VALUE-CELL-LOCATION SYM))
(SP (SG-SPECIAL-PDL SG))
(SPP (SG-SPECIAL-PDL-POINTER SG)))
()
(OR ( SPP 0)
(ZEROP (SG-IN-SWAPPED-STATE SG)) ;If its bindings are swapped out
(DO ((I SPP (1- I)) ;then search through them
(P))
(( I 0))
(SETQ P (AP-1-CAREFUL SP I))
(SELECT (%P-DATA-TYPE P)
(DTP-LOCATIVE ;If this is a binding pair
(SETQ P (%MAKE-POINTER-OFFSET DTP-LOCATIVE P -1))
(IF (EQ (AREF SP I) VCL) ;and is for this variable, then return
(RETURN-FROM RESULT (CAR P)) ;the saved value, invz'ing if necc
(SETQ I (1- I)))) ;Space over second Q of binding pair
(OTHERWISE )))) ;Ignore non-binding blocks
;; The variable isn't bound in that stack group, so we want its global value.
;; Must ignore bindings in our own stack group.
(SETQ SP (SG-SPECIAL-PDL %CURRENT-STACK-GROUP)
SPP (GET-OWN-SPECIAL-PDL-POINTER SP))
(DO ((VAL (SYMEVAL SYM))
(I SPP (1- I))
(P))
(( I 0) (RETURN-FROM RESULT VAL))
(SETQ P (AP-1-CAREFUL SP I))
(SELECT (%P-DATA-TYPE P)
(DTP-LOCATIVE
(SETQ P (%MAKE-POINTER-OFFSET DTP-LOCATIVE P -1))
(IF (EQ (AREF SP I) VCL)
(SETQ VAL (CAR P)))
(SETQ I (1- I)))
(OTHERWISE )))))))
;Get the special-pdl pointer for the running SG
(DEFUN GET-OWN-SPECIAL-PDL-POINTER (SP)
(- (1- (%STRUCTURE-BOXED-SIZE SP))
(+ (ARRAY-LEADER-LENGTH SP) 3 (%P-LDB-OFFSET %%ARRAY-LONG-LENGTH-FLAG SP 0))))
;An ALOC that only works for 1-dimensional arrays. It avoids referencing the
;word pointed to since if the special-pdl pointer being used is confused that
;might be an external-value-cell-pointer, causing an error. This doesn't
;do bounds checking.
(DEFUN AP-1-CAREFUL (ARRAY INDEX)
(%MAKE-POINTER-OFFSET DTP-LOCATIVE
ARRAY
(+ INDEX 1 (%P-LDB-OFFSET %%ARRAY-LONG-LENGTH-FLAG ARRAY 0))))
|