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
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
|
/******************************************************************************
* *
* N O T I C E *
* *
* Copyright Abandoned, 1987, Fred Fish *
* *
* *
* This previously copyrighted work has been placed into the public *
* domain by the author and may be freely used for any purpose, *
* private or commercial. *
* *
* Because of the number of inquiries I was receiving about the use *
* of this product in commercially developed works I have decided to *
* simply make it public domain to further its unrestricted use. I *
* specifically would be most happy to see this material become a *
* part of the standard Unix distributions by AT&T and the Berkeley *
* Computer Science Research Group, and a standard part of the GNU *
* system from the Free Software Foundation. *
* *
* I would appreciate it, as a courtesy, if this notice is left in *
* all copies and derivative works. Thank you. *
* *
* The author makes no warranty of any kind with respect to this *
* product and explicitly disclaims any implied warranties of mer- *
* chantability or fitness for any particular purpose. *
* *
******************************************************************************
*/
/*
* FILE
*
* dbug.c runtime support routines for dbug package
*
* SCCS
*
* @(#)dbug.c 1.25 7/25/89
*
* DESCRIPTION
*
* These are the runtime support routines for the dbug package.
* The dbug package has two main components; the user include
* file containing various macro definitions, and the runtime
* support routines which are called from the macro expansions.
*
* Externally visible functions in the runtime support module
* use the naming convention pattern "_db_xx...xx_", thus
* they are unlikely to collide with user defined function names.
*
* AUTHOR(S)
*
* Fred Fish (base code)
* Enhanced Software Technologies, Tempe, AZ
* asuvax!mcdphx!estinc!fnf
*
* Binayak Banerjee (profiling enhancements)
* seismo!bpa!sjuvax!bbanerje
*
* Michael Widenius:
* DBUG_DUMP - To dump a block of memory.
* PUSH_FLAG "O" - To be used insted of "o" if we
* want flushing after each write
* PUSH_FLAG "A" - as 'O', but we will append to the out file instead
* of creating a new one.
* Check of malloc on entry/exit (option "S")
*/
#ifdef DBUG_OFF
#undef DBUG_OFF
#endif
#include <my_global.h>
#include <m_string.h>
#include <errno.h>
#if defined(MSDOS) || defined(__WIN__)
#include <process.h>
#endif
#ifdef _DBUG_CONDITION_
#define _DBUG_START_CONDITION_ "d:t"
#else
#define _DBUG_START_CONDITION_ ""
#endif
/*
* Manifest constants that should not require any changes.
*/
#define EOS '\000' /* End Of String marker */
/*
* Manifest constants which may be "tuned" if desired.
*/
#define PRINTBUF 1024 /* Print buffer size */
#define INDENT 2 /* Indentation per trace level */
#define MAXDEPTH 200 /* Maximum trace depth default */
/*
* The following flags are used to determine which
* capabilities the user has enabled with the state
* push macro.
*/
#define TRACE_ON 000001 /* Trace enabled */
#define DEBUG_ON 000002 /* Debug enabled */
#define FILE_ON 000004 /* File name print enabled */
#define LINE_ON 000010 /* Line number print enabled */
#define DEPTH_ON 000020 /* Function nest level print enabled */
#define PROCESS_ON 000040 /* Process name print enabled */
#define NUMBER_ON 000100 /* Number each line of output */
#define PROFILE_ON 000200 /* Print out profiling code */
#define PID_ON 000400 /* Identify each line with process id */
#define SANITY_CHECK_ON 001000 /* Check safemalloc on DBUG_ENTER */
#define FLUSH_ON_WRITE 002000 /* Flush on every write */
#define TRACING (stack -> flags & TRACE_ON)
#define DEBUGGING (stack -> flags & DEBUG_ON)
#define PROFILING (stack -> flags & PROFILE_ON)
#define STREQ(a,b) (strcmp(a,b) == 0)
/*
* Typedefs to make things more obvious.
*/
#ifndef __WIN__
typedef int BOOLEAN;
#else
#define BOOLEAN BOOL
#endif
/*
* Make it easy to change storage classes if necessary.
*/
#define IMPORT extern /* Names defined externally */
#define EXPORT /* Allocated here, available globally */
#define AUTO auto /* Names to be allocated on stack */
#define REGISTER register /* Names to be placed in registers */
/*
* The default file for profiling. Could also add another flag
* (G?) which allowed the user to specify this.
*
* If the automatic variables get allocated on the stack in
* reverse order from their declarations, then define AUTOS_REVERSE.
* This is used by the code that keeps track of stack usage. For
* forward allocation, the difference in the dbug frame pointers
* represents stack used by the callee function. For reverse allocation,
* the difference represents stack used by the caller function.
*
*/
#define PROF_FILE "dbugmon.out"
#define PROF_EFMT "E\t%ld\t%s\n"
#define PROF_SFMT "S\t%lx\t%lx\t%s\n"
#define PROF_XFMT "X\t%ld\t%s\n"
#ifdef M_I386 /* predefined by xenix 386 compiler */
#define AUTOS_REVERSE 1
#endif
/*
* Variables which are available externally but should only
* be accessed via the macro package facilities.
*/
EXPORT FILE *_db_fp_ = (FILE *) 0; /* Output stream, default stderr */
EXPORT char *_db_process_ = (char*) "dbug"; /* Pointer to process name; argv[0] */
EXPORT FILE *_db_pfp_ = (FILE *)0; /* Profile stream, 'dbugmon.out' */
EXPORT BOOLEAN _db_on_ = FALSE; /* TRUE if debugging currently on */
EXPORT BOOLEAN _db_pon_ = FALSE; /* TRUE if profile currently on */
EXPORT BOOLEAN _no_db_ = FALSE; /* TRUE if no debugging at all */
/*
* Externally supplied functions.
*/
#ifndef HAVE_PERROR
static void perror (); /* Fake system/library error print routine */
#endif
IMPORT int _sanity(const char *file,uint line);
/*
* The user may specify a list of functions to trace or
* debug. These lists are kept in a linear linked list,
* a very simple implementation.
*/
struct link {
char *str; /* Pointer to link's contents */
struct link *next_link; /* Pointer to the next link */
};
/*
* Debugging states can be pushed or popped off of a
* stack which is implemented as a linked list. Note
* that the head of the list is the current state and the
* stack is pushed by adding a new state to the head of the
* list or popped by removing the first link.
*/
struct state {
int flags; /* Current state flags */
int maxdepth; /* Current maximum trace depth */
uint delay; /* Delay after each output line */
int sub_level; /* Sub this from code_state->level */
FILE *out_file; /* Current output stream */
FILE *prof_file; /* Current profiling stream */
char name[FN_REFLEN]; /* Name of output file */
struct link *functions; /* List of functions */
struct link *p_functions; /* List of profiled functions */
struct link *keywords; /* List of debug keywords */
struct link *processes; /* List of process names */
struct state *next_state; /* Next state in the list */
};
/*
* Local variables not seen by user.
*/
static my_bool init_done = FALSE; /* Set to TRUE when initialization done */
static struct state *stack=0;
typedef struct st_code_state {
const char *func; /* Name of current user function */
const char *file; /* Name of current user file */
char **framep; /* Pointer to current frame */
const char *jmpfunc; /* Remember current function for setjmp */
const char *jmpfile; /* Remember current file for setjmp */
int lineno; /* Current debugger output line number */
int level; /* Current function nesting level */
int disable_output; /* Set to it if output is disabled */
int jmplevel; /* Remember nesting level at setjmp () */
/*
* The following variables are used to hold the state information
* between the call to _db_pargs_() and _db_doprnt_(), during
* expansion of the DBUG_PRINT macro. This is the only macro
* that currently uses these variables.
*
* These variables are currently used only by _db_pargs_() and
* _db_doprnt_().
*/
uint u_line; /* User source code line number */
int locked; /* If locked with _db_lock_file */
const char *u_keyword; /* Keyword for current macro */
} CODE_STATE;
/* Parse a debug command string */
static struct link *ListParse(char *ctlp);
/* Make a fresh copy of a string */
static char *StrDup(const char *str);
/* Open debug output stream */
static void DBUGOpenFile(const char *name, int append);
#ifndef THREAD
/* Open profile output stream */
static FILE *OpenProfile(const char *name);
/* Profile if asked for it */
static BOOLEAN DoProfile(void);
#endif
/* Return current user time (ms) */
#ifndef THREAD
static unsigned long Clock (void);
#endif
/* Close debug output stream */
static void CloseFile(FILE *fp);
/* Push current debug state */
static void PushState(void);
/* Test for tracing enabled */
static BOOLEAN DoTrace(CODE_STATE *state);
/* Test to see if file is writable */
#if !(!defined(HAVE_ACCESS) || defined(MSDOS))
static BOOLEAN Writable(char *pathname);
/* Change file owner and group */
static void ChangeOwner(char *pathname);
/* Allocate memory for runtime support */
#endif
static char *DbugMalloc(size_t size);
/* Remove leading pathname components */
static char *BaseName(const char *pathname);
static void DoPrefix(uint line);
static void FreeList(struct link *linkp);
static void Indent(int indent);
static BOOLEAN InList(struct link *linkp,const char *cp);
static void dbug_flush(CODE_STATE *);
static void DbugExit(const char *why);
static int DelayArg(int value);
/* Supplied in Sys V runtime environ */
/* Break string into tokens */
static char *static_strtok(char *s1,pchar chr);
/*
* Miscellaneous printf format strings.
*/
#define ERR_MISSING_RETURN "%s: missing DBUG_RETURN or DBUG_VOID_RETURN macro in function \"%s\"\n"
#define ERR_OPEN "%s: can't open debug output stream \"%s\": "
#define ERR_CLOSE "%s: can't close debug file: "
#define ERR_ABORT "%s: debugger aborting because %s\n"
#define ERR_CHOWN "%s: can't change owner/group of \"%s\": "
/*
* Macros and defines for testing file accessibility under UNIX and MSDOS.
*/
#undef EXISTS
#if !defined(HAVE_ACCESS) || defined(MSDOS)
#define EXISTS(pathname) (FALSE) /* Assume no existance */
#define Writable(name) (TRUE)
#else
#define EXISTS(pathname) (access (pathname, F_OK) == 0)
#define WRITABLE(pathname) (access (pathname, W_OK) == 0)
#endif
#ifndef MSDOS
#define ChangeOwner(name)
#endif
/*
* Translate some calls among different systems.
*/
#if defined(unix) || defined(xenix) || defined(VMS) || defined(__NetBSD__)
# define Delay(A) sleep((uint) A)
#elif defined(AMIGA)
IMPORT int Delay (); /* Pause for given number of ticks */
#else
static int Delay(int ticks);
#endif
/*
** Macros to allow dbugging with threads
*/
#ifdef THREAD
#include <my_pthread.h>
pthread_mutex_t THR_LOCK_dbug;
static void init_dbug_state(void)
{
pthread_mutex_init(&THR_LOCK_dbug,MY_MUTEX_INIT_FAST);
}
static CODE_STATE *code_state(void)
{
CODE_STATE *state=0;
struct st_my_thread_var *tmp=my_thread_var;
if (tmp)
{
if (!(state=(CODE_STATE *) tmp->dbug))
{
state=(CODE_STATE*) DbugMalloc(sizeof(*state));
bzero((char*) state,sizeof(*state));
state->func="?func";
state->file="?file";
tmp->dbug=(gptr) state;
}
}
return state;
}
#else /* !THREAD */
#define init_dbug_state()
#define code_state() (&static_code_state)
#define pthread_mutex_lock(A) {}
#define pthread_mutex_unlock(A) {}
static CODE_STATE static_code_state=
{
"?func", "?file", NULL, NullS, NullS, 0,0,0,0,0,0, NullS
};
#endif
/*
* FUNCTION
*
* _db_push_ push current debugger state and set up new one
*
* SYNOPSIS
*
* VOID _db_push_ (control)
* char *control;
*
* DESCRIPTION
*
* Given pointer to a debug control string in "control", pushes
* the current debug state, parses the control string, and sets
* up a new debug state.
*
* The only attribute of the new state inherited from the previous
* state is the current function nesting level. This can be
* overridden by using the "r" flag in the control string.
*
* The debug control string is a sequence of colon separated fields
* as follows:
*
* <field_1>:<field_2>:...:<field_N>
*
* Each field consists of a mandatory flag character followed by
* an optional "," and comma separated list of modifiers:
*
* flag[,modifier,modifier,...,modifier]
*
* The currently recognized flag characters are:
*
* d Enable output from DBUG_<N> macros for
* for the current state. May be followed
* by a list of keywords which selects output
* only for the DBUG macros with that keyword.
* A null list of keywords implies output for
* all macros.
*
* D Delay after each debugger output line.
* The argument is the number of tenths of seconds
* to delay, subject to machine capabilities.
* I.E. -#D,20 is delay two seconds.
*
* f Limit debugging and/or tracing, and profiling to the
* list of named functions. Note that a null list will
* disable all functions. The appropriate "d" or "t"
* flags must still be given, this flag only limits their
* actions if they are enabled.
*
* F Identify the source file name for each
* line of debug or trace output.
*
* i Identify the process with the pid for each line of
* debug or trace output.
*
* g Enable profiling. Create a file called 'dbugmon.out'
* containing information that can be used to profile
* the program. May be followed by a list of keywords
* that select profiling only for the functions in that
* list. A null list implies that all functions are
* considered.
*
* L Identify the source file line number for
* each line of debug or trace output.
*
* n Print the current function nesting depth for
* each line of debug or trace output.
*
* N Number each line of dbug output.
*
* o Redirect the debugger output stream to the
* specified file. The default output is stderr.
*
* O As O but the file is really flushed between each
* write. When neaded the file is closed and reopened
* between each write.
*
* p Limit debugger actions to specified processes.
* A process must be identified with the
* DBUG_PROCESS macro and match one in the list
* for debugger actions to occur.
*
* P Print the current process name for each
* line of debug or trace output.
*
* r When pushing a new state, do not inherit
* the previous state's function nesting level.
* Useful when the output is to start at the
* left margin.
*
* S Do function _sanity(_file_,_line_) at each
* debugged function until _sanity() returns
* something that differs from 0.
* (Moustly used with safemalloc)
*
* t Enable function call/exit trace lines.
* May be followed by a list (containing only
* one modifier) giving a numeric maximum
* trace level, beyond which no output will
* occur for either debugging or tracing
* macros. The default is a compile time
* option.
*
* Some examples of debug control strings which might appear
* on a shell command line (the "-#" is typically used to
* introduce a control string to an application program) are:
*
* -#d:t
* -#d:f,main,subr1:F:L:t,20
* -#d,input,output,files:n
*
* For convenience, any leading "-#" is stripped off.
*
*/
void _db_push_ (const char *control)
{
reg1 char *scan;
reg2 struct link *temp;
CODE_STATE *state;
char *new_str;
if (! _db_fp_)
_db_fp_= stderr; /* Output stream, default stderr */
if (*control == '-')
{
if (*++control == '#')
control++;
}
if (*control)
_no_db_=0; /* We are using dbug after all */
new_str = StrDup (control);
PushState ();
state=code_state();
scan = static_strtok (new_str, ':');
for (; scan != NULL; scan = static_strtok ((char *)NULL, ':')) {
switch (*scan++) {
case 'd':
_db_on_ = TRUE;
stack -> flags |= DEBUG_ON;
if (*scan++ == ',') {
stack -> keywords = ListParse (scan);
}
break;
case 'D':
stack -> delay = 0;
if (*scan++ == ',') {
temp = ListParse (scan);
stack -> delay = DelayArg (atoi (temp -> str));
FreeList (temp);
}
break;
case 'f':
if (*scan++ == ',') {
stack -> functions = ListParse (scan);
}
break;
case 'F':
stack -> flags |= FILE_ON;
break;
case 'i':
stack -> flags |= PID_ON;
break;
#ifndef THREAD
case 'g':
_db_pon_ = TRUE;
if (OpenProfile(PROF_FILE))
{
stack -> flags |= PROFILE_ON;
if (*scan++ == ',')
stack -> p_functions = ListParse (scan);
}
break;
#endif
case 'L':
stack -> flags |= LINE_ON;
break;
case 'n':
stack -> flags |= DEPTH_ON;
break;
case 'N':
stack -> flags |= NUMBER_ON;
break;
case 'A':
case 'O':
stack -> flags |= FLUSH_ON_WRITE;
case 'a':
case 'o':
if (*scan++ == ',') {
temp = ListParse (scan);
DBUGOpenFile(temp -> str, (int) (scan[-2] == 'A' || scan[-2] == 'a'));
FreeList (temp);
} else {
DBUGOpenFile ("-",0);
}
break;
case 'p':
if (*scan++ == ',') {
stack -> processes = ListParse (scan);
}
break;
case 'P':
stack -> flags |= PROCESS_ON;
break;
case 'r':
stack->sub_level= state->level;
break;
case 't':
stack -> flags |= TRACE_ON;
if (*scan++ == ',') {
temp = ListParse (scan);
stack -> maxdepth = atoi (temp -> str);
FreeList (temp);
}
break;
case 'S':
stack -> flags |= SANITY_CHECK_ON;
break;
}
}
free (new_str);
}
/*
* FUNCTION
*
* _db_pop_ pop the debug stack
*
* DESCRIPTION
*
* Pops the debug stack, returning the debug state to its
* condition prior to the most recent _db_push_ invocation.
* Note that the pop will fail if it would remove the last
* valid state from the stack. This prevents user errors
* in the push/pop sequence from screwing up the debugger.
* Maybe there should be some kind of warning printed if the
* user tries to pop too many states.
*
*/
void _db_pop_ ()
{
reg1 struct state *discard;
discard = stack;
if (discard != NULL && discard -> next_state != NULL) {
stack = discard -> next_state;
_db_fp_ = stack -> out_file;
_db_pfp_ = stack -> prof_file;
if (discard -> keywords != NULL) {
FreeList (discard -> keywords);
}
if (discard -> functions != NULL) {
FreeList (discard -> functions);
}
if (discard -> processes != NULL) {
FreeList (discard -> processes);
}
if (discard -> p_functions != NULL) {
FreeList (discard -> p_functions);
}
CloseFile (discard -> out_file);
if (discard -> prof_file)
CloseFile (discard -> prof_file);
free ((char *) discard);
if (!(stack->flags & DEBUG_ON))
_db_on_=0;
}
else
{
_db_on_=0;
}
}
/*
* FUNCTION
*
* _db_enter_ process entry point to user function
*
* SYNOPSIS
*
* VOID _db_enter_ (_func_, _file_, _line_,
* _sfunc_, _sfile_, _slevel_, _sframep_)
* char *_func_; points to current function name
* char *_file_; points to current file name
* int _line_; called from source line number
* char **_sfunc_; save previous _func_
* char **_sfile_; save previous _file_
* int *_slevel_; save previous nesting level
* char ***_sframep_; save previous frame pointer
*
* DESCRIPTION
*
* Called at the beginning of each user function to tell
* the debugger that a new function has been entered.
* Note that the pointers to the previous user function
* name and previous user file name are stored on the
* caller's stack (this is why the ENTER macro must be
* the first "executable" code in a function, since it
* allocates these storage locations). The previous nesting
* level is also stored on the callers stack for internal
* self consistency checks.
*
* Also prints a trace line if tracing is enabled and
* increments the current function nesting depth.
*
* Note that this mechanism allows the debugger to know
* what the current user function is at all times, without
* maintaining an internal stack for the function names.
*
*/
void _db_enter_ (
const char *_func_,
const char *_file_,
uint _line_,
const char **_sfunc_,
const char **_sfile_,
uint *_slevel_,
char ***_sframep_ __attribute__((unused)))
{
reg1 CODE_STATE *state;
if (!_no_db_)
{
int save_errno=errno;
/*
Sasha: the test below is so we could call functions with DBUG_ENTER
before my_thread_init(). I needed this because I suspected corruption
of a block allocated by my_thread_init() itself, so I wanted to use
my_malloc()/my_free() in my_thread_init()/my_thread_end()
*/
if (!(state=code_state()))
return;
if (!init_done)
_db_push_ (_DBUG_START_CONDITION_);
*_sfunc_ = state->func;
*_sfile_ = state->file;
state->func =(char*) _func_;
state->file = (char*) _file_; /* BaseName takes time !! */
*_slevel_ = ++state->level;
#ifndef THREAD
*_sframep_ = state->framep;
state->framep = (char **) _sframep_;
if (DoProfile ())
{
long stackused;
if (*state->framep == NULL)
{
stackused = 0;
}
else
{
stackused = ((long)(*state->framep)) - ((long)(state->framep));
stackused = stackused > 0 ? stackused : -stackused;
}
(void) fprintf (_db_pfp_, PROF_EFMT , Clock (), state->func);
#ifdef AUTOS_REVERSE
(void) fprintf (_db_pfp_, PROF_SFMT, state->framep, stackused, *_sfunc_);
#else
(void) fprintf (_db_pfp_, PROF_SFMT, (ulong) state->framep, stackused,
state->func);
#endif
(void) fflush (_db_pfp_);
}
#endif
if (DoTrace(state))
{
if (!state->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
DoPrefix (_line_);
Indent (state -> level);
(void) fprintf (_db_fp_, ">%s\n", state->func);
dbug_flush (state); /* This does a unlock */
}
#ifdef SAFEMALLOC
if (stack->flags & SANITY_CHECK_ON && !state->disable_output)
if (_sanity(_file_,_line_)) /* Check of safemalloc */
stack -> flags &= ~SANITY_CHECK_ON;
#endif
errno=save_errno;
}
}
/*
* FUNCTION
*
* _db_return_ process exit from user function
*
* SYNOPSIS
*
* VOID _db_return_ (_line_, _sfunc_, _sfile_, _slevel_)
* int _line_; current source line number
* char **_sfunc_; where previous _func_ is to be retrieved
* char **_sfile_; where previous _file_ is to be retrieved
* int *_slevel_; where previous level was stashed
*
* DESCRIPTION
*
* Called just before user function executes an explicit or implicit
* return. Prints a trace line if trace is enabled, decrements
* the current nesting level, and restores the current function and
* file names from the defunct function's stack.
*
*/
void _db_return_ (
uint _line_,
const char **_sfunc_,
const char **_sfile_,
uint *_slevel_)
{
CODE_STATE *state;
if (!_no_db_)
{
int save_errno=errno;
if (!(state=code_state()))
return;
if (!init_done)
_db_push_ ("");
if (stack->flags & (TRACE_ON | DEBUG_ON | PROFILE_ON))
{
if (!state->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
if (state->level != (int) *_slevel_)
(void) fprintf (_db_fp_, ERR_MISSING_RETURN, _db_process_,
state->func);
else
{
#ifdef SAFEMALLOC
if (stack->flags & SANITY_CHECK_ON && !state->disable_output)
{
if (_sanity(*_sfile_,_line_))
stack->flags &= ~SANITY_CHECK_ON;
}
#endif
#ifndef THREAD
if (DoProfile ())
(void) fprintf (_db_pfp_, PROF_XFMT, Clock(), state->func);
#endif
if (DoTrace (state))
{
DoPrefix (_line_);
Indent (state->level);
(void) fprintf (_db_fp_, "<%s\n", state->func);
}
}
dbug_flush(state);
}
state->level = *_slevel_-1;
state->func = *_sfunc_;
state->file = *_sfile_;
#ifndef THREAD
if (state->framep != NULL)
state->framep = (char **) *state->framep;
#endif
errno=save_errno;
}
}
/*
* FUNCTION
*
* _db_pargs_ log arguments for subsequent use by _db_doprnt_()
*
* SYNOPSIS
*
* VOID _db_pargs_ (_line_, keyword)
* int _line_;
* char *keyword;
*
* DESCRIPTION
*
* The new universal printing macro DBUG_PRINT, which replaces
* all forms of the DBUG_N macros, needs two calls to runtime
* support routines. The first, this function, remembers arguments
* that are used by the subsequent call to _db_doprnt_().
*
*/
void _db_pargs_ (
uint _line_,
const char *keyword)
{
CODE_STATE *state=code_state();
/* Sasha: pre-my_thread_init() safety */
if (!state)
return;
state->u_line = _line_;
state->u_keyword = (char*) keyword;
}
/*
* FUNCTION
*
* _db_doprnt_ handle print of debug lines
*
* SYNOPSIS
*
* VOID _db_doprnt_ (format, va_alist)
* char *format;
* va_dcl;
*
* DESCRIPTION
*
* When invoked via one of the DBUG macros, tests the current keyword
* set by calling _db_pargs_() to see if that macro has been selected
* for processing via the debugger control string, and if so, handles
* printing of the arguments via the format string. The line number
* of the DBUG macro in the source is found in u_line.
*
* Note that the format string SHOULD NOT include a terminating
* newline, this is supplied automatically.
*
*/
#include <stdarg.h>
void _db_doprnt_ (const char *format,...)
{
va_list args;
CODE_STATE *state;
/* Sasha: pre-my_thread_init() safety */
if (!(state=code_state()))
return;
va_start(args,format);
if (_db_keyword_ (state->u_keyword)) {
int save_errno=errno;
if (!state->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
DoPrefix (state->u_line);
if (TRACING) {
Indent (state->level + 1);
} else {
(void) fprintf (_db_fp_, "%s: ", state->func);
}
(void) fprintf (_db_fp_, "%s: ", state->u_keyword);
(void) vfprintf (_db_fp_, format, args);
(void) fputc('\n',_db_fp_);
dbug_flush(state);
errno=save_errno;
}
va_end(args);
}
/*
* FUNCTION
*
* _db_dump_ dump a string in hex
*
* SYNOPSIS
*
* void _db_dump_ (_line_,keyword,memory,length)
* int _line_; current source line number
* char *keyword;
* char *memory; Memory to print
* int length; Bytes to print
*
* DESCRIPTION
* Dump N characters in a binary array.
* Is used to examine corrputed memory or arrays.
*/
void _db_dump_(
uint _line_,
const char *keyword,
const char *memory,
uint length)
{
int pos;
char dbuff[90];
CODE_STATE *state;
if (!(state=code_state()))
return;
if (_db_keyword_ ((char*) keyword))
{
if (!state->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
DoPrefix (_line_);
if (TRACING)
{
Indent (state->level + 1);
pos= min(max(state->level-stack->sub_level,0)*INDENT,80);
}
else
{
fprintf(_db_fp_, "%s: ", state->func);
}
sprintf(dbuff,"%s: Memory: 0x%lx Bytes: (%d)\n",
keyword,(ulong) memory, length);
(void) fputs(dbuff,_db_fp_);
pos=0;
while (length-- > 0)
{
uint tmp= *((unsigned char*) memory++);
if ((pos+=3) >= 80)
{
fputc('\n',_db_fp_);
pos=3;
}
fputc(_dig_vec_upper[((tmp >> 4) & 15)], _db_fp_);
fputc(_dig_vec_upper[tmp & 15], _db_fp_);
fputc(' ',_db_fp_);
}
(void) fputc('\n',_db_fp_);
dbug_flush(state);
}
}
/*
Enable/Disable output for this thread
SYNOPSIS
_db_output_()
flag 1 = enable output, 0 = disable_output
*/
void _db_output_(uint flag)
{
CODE_STATE *state;
if (!(state=code_state()))
return;
state->disable_output= !flag;
}
/*
* FUNCTION
*
* ListParse parse list of modifiers in debug control string
*
* SYNOPSIS
*
* static struct link *ListParse (ctlp)
* char *ctlp;
*
* DESCRIPTION
*
* Given pointer to a comma separated list of strings in "cltp",
* parses the list, building a list and returning a pointer to it.
* The original comma separated list is destroyed in the process of
* building the linked list, thus it had better be a duplicate
* if it is important.
*
* Note that since each link is added at the head of the list,
* the final list will be in "reverse order", which is not
* significant for our usage here.
*
*/
static struct link *ListParse (
char *ctlp)
{
REGISTER char *start;
REGISTER struct link *new_malloc;
REGISTER struct link *head;
head = NULL;
while (*ctlp != EOS) {
start = ctlp;
while (*ctlp != EOS && *ctlp != ',') {
ctlp++;
}
if (*ctlp == ',') {
*ctlp++ = EOS;
}
new_malloc = (struct link *) DbugMalloc (sizeof (struct link));
new_malloc -> str = StrDup (start);
new_malloc -> next_link = head;
head = new_malloc;
}
return (head);
}
/*
* FUNCTION
*
* InList test a given string for member of a given list
*
* SYNOPSIS
*
* static BOOLEAN InList (linkp, cp)
* struct link *linkp;
* char *cp;
*
* DESCRIPTION
*
* Tests the string pointed to by "cp" to determine if it is in
* the list pointed to by "linkp". Linkp points to the first
* link in the list. If linkp is NULL then the string is treated
* as if it is in the list (I.E all strings are in the null list).
* This may seem rather strange at first but leads to the desired
* operation if no list is given. The net effect is that all
* strings will be accepted when there is no list, and when there
* is a list, only those strings in the list will be accepted.
*
*/
static BOOLEAN InList (
struct link *linkp,
const char *cp)
{
REGISTER struct link *scan;
REGISTER BOOLEAN result;
if (linkp == NULL) {
result = TRUE;
} else {
result = FALSE;
for (scan = linkp; scan != NULL; scan = scan -> next_link) {
if (STREQ (scan -> str, cp)) {
result = TRUE;
break;
}
}
}
return (result);
}
/*
* FUNCTION
*
* PushState push current state onto stack and set up new one
*
* SYNOPSIS
*
* static VOID PushState ()
*
* DESCRIPTION
*
* Pushes the current state on the state stack, and initializes
* a new state. The only parameter inherited from the previous
* state is the function nesting level. This action can be
* inhibited if desired, via the "r" flag.
*
* The state stack is a linked list of states, with the new
* state added at the head. This allows the stack to grow
* to the limits of memory if necessary.
*
*/
static void PushState ()
{
REGISTER struct state *new_malloc;
if (!init_done)
{
init_dbug_state();
init_done=TRUE;
}
(void) code_state(); /* Alloc memory */
new_malloc = (struct state *) DbugMalloc(sizeof (struct state));
new_malloc -> flags = 0;
new_malloc -> delay = 0;
new_malloc -> maxdepth = MAXDEPTH;
new_malloc -> sub_level=0;
new_malloc -> out_file = stderr;
new_malloc -> prof_file = (FILE*) 0;
new_malloc -> functions = NULL;
new_malloc -> p_functions = NULL;
new_malloc -> keywords = NULL;
new_malloc -> processes = NULL;
new_malloc -> next_state = stack;
stack=new_malloc;
}
/*
* FUNCTION
*
* DoTrace check to see if tracing is current enabled
*
* SYNOPSIS
*
* static BOOLEAN DoTrace (stack)
*
* DESCRIPTION
*
* Checks to see if tracing is enabled based on whether the
* user has specified tracing, the maximum trace depth has
* not yet been reached, the current function is selected,
* and the current process is selected. Returns TRUE if
* tracing is enabled, FALSE otherwise.
*
*/
static BOOLEAN DoTrace (CODE_STATE *state)
{
reg2 BOOLEAN trace=FALSE;
if (TRACING && !state->disable_output &&
state->level <= stack -> maxdepth &&
InList (stack -> functions, state->func) &&
InList (stack -> processes, _db_process_))
trace = TRUE;
return (trace);
}
/*
* FUNCTION
*
* DoProfile check to see if profiling is current enabled
*
* SYNOPSIS
*
* static BOOLEAN DoProfile ()
*
* DESCRIPTION
*
* Checks to see if profiling is enabled based on whether the
* user has specified profiling, the maximum trace depth has
* not yet been reached, the current function is selected,
* and the current process is selected. Returns TRUE if
* profiling is enabled, FALSE otherwise.
*
*/
#ifndef THREAD
static BOOLEAN DoProfile ()
{
REGISTER BOOLEAN profile;
CODE_STATE *state;
state=code_state();
profile = FALSE;
if (PROFILING && !state->disable_output &&
state->level <= stack -> maxdepth &&
InList (stack -> p_functions, state->func) &&
InList (stack -> processes, _db_process_))
profile = TRUE;
return (profile);
}
#endif
/*
* FUNCTION
*
* _db_strict_keyword_ test keyword for member of keyword list
*
* SYNOPSIS
*
* BOOLEAN _db_strict_keyword_ (keyword)
* char *keyword;
*
* DESCRIPTION
*
* Similar to _db_keyword_, but keyword is NOT accepted if keyword list
* is empty. Used in DBUG_EXECUTE_IF() - for actions that must not be
* executed by default.
*
* Returns TRUE if keyword accepted, FALSE otherwise.
*
*/
BOOLEAN _db_strict_keyword_ (
const char *keyword)
{
if (stack -> keywords == NULL)
return FALSE;
return _db_keyword_ (keyword);
}
/*
* FUNCTION
*
* _db_keyword_ test keyword for member of keyword list
*
* SYNOPSIS
*
* BOOLEAN _db_keyword_ (keyword)
* char *keyword;
*
* DESCRIPTION
*
* Test a keyword to determine if it is in the currently active
* keyword list. As with the function list, a keyword is accepted
* if the list is null, otherwise it must match one of the list
* members. When debugging is not on, no keywords are accepted.
* After the maximum trace level is exceeded, no keywords are
* accepted (this behavior subject to change). Additionally,
* the current function and process must be accepted based on
* their respective lists.
*
* Returns TRUE if keyword accepted, FALSE otherwise.
*
*/
BOOLEAN _db_keyword_ (
const char *keyword)
{
REGISTER BOOLEAN result;
CODE_STATE *state;
if (!init_done)
_db_push_ ("");
/* Sasha: pre-my_thread_init() safety */
if (!(state=code_state()))
return FALSE;
result = FALSE;
if (DEBUGGING && !state->disable_output &&
state->level <= stack -> maxdepth &&
InList (stack -> functions, state->func) &&
InList (stack -> keywords, keyword) &&
InList (stack -> processes, _db_process_))
result = TRUE;
return (result);
}
/*
* FUNCTION
*
* Indent indent a line to the given indentation level
*
* SYNOPSIS
*
* static VOID Indent (indent)
* int indent;
*
* DESCRIPTION
*
* Indent a line to the given level. Note that this is
* a simple minded but portable implementation.
* There are better ways.
*
* Also, the indent must be scaled by the compile time option
* of character positions per nesting level.
*
*/
static void Indent (
int indent)
{
REGISTER int count;
indent= max(indent-1-stack->sub_level,0)*INDENT;
for (count = 0; count < indent ; count++)
{
if ((count % INDENT) == 0)
fputc('|',_db_fp_);
else
fputc(' ',_db_fp_);
}
}
/*
* FUNCTION
*
* FreeList free all memory associated with a linked list
*
* SYNOPSIS
*
* static VOID FreeList (linkp)
* struct link *linkp;
*
* DESCRIPTION
*
* Given pointer to the head of a linked list, frees all
* memory held by the list and the members of the list.
*
*/
static void FreeList (
struct link *linkp)
{
REGISTER struct link *old;
while (linkp != NULL) {
old = linkp;
linkp = linkp -> next_link;
if (old -> str != NULL) {
free (old -> str);
}
free ((char *) old);
}
}
/*
* FUNCTION
*
* StrDup make a duplicate of a string in new memory
*
* SYNOPSIS
*
* static char *StrDup (my_string)
* char *string;
*
* DESCRIPTION
*
* Given pointer to a string, allocates sufficient memory to make
* a duplicate copy, and copies the string to the newly allocated
* memory. Failure to allocated sufficient memory is immediately
* fatal.
*
*/
static char *StrDup (const char *str)
{
reg1 char *new_malloc;
new_malloc = DbugMalloc((size_t) strlen (str) + 1);
(void) strcpy (new_malloc, str);
return (new_malloc);
}
/*
* FUNCTION
*
* DoPrefix print debugger line prefix prior to indentation
*
* SYNOPSIS
*
* static VOID DoPrefix (_line_)
* int _line_;
*
* DESCRIPTION
*
* Print prefix common to all debugger output lines, prior to
* doing indentation if necessary. Print such information as
* current process name, current source file name and line number,
* and current function nesting depth.
*
*/
static void DoPrefix (
uint _line_)
{
CODE_STATE *state;
state=code_state();
state->lineno++;
if (stack -> flags & PID_ON) {
#ifdef THREAD
(void) fprintf (_db_fp_, "%-7s: ", my_thread_name());
#else
(void) fprintf (_db_fp_, "%5d: ", (int) getpid ());
#endif
}
if (stack -> flags & NUMBER_ON) {
(void) fprintf (_db_fp_, "%5d: ", state->lineno);
}
if (stack -> flags & PROCESS_ON) {
(void) fprintf (_db_fp_, "%s: ", _db_process_);
}
if (stack -> flags & FILE_ON) {
(void) fprintf (_db_fp_, "%14s: ", BaseName(state->file));
}
if (stack -> flags & LINE_ON) {
(void) fprintf (_db_fp_, "%5d: ", _line_);
}
if (stack -> flags & DEPTH_ON) {
(void) fprintf (_db_fp_, "%4d: ", state->level);
}
}
/*
* FUNCTION
*
* DBUGOpenFile open new output stream for debugger output
*
* SYNOPSIS
*
* static VOID DBUGOpenFile (name)
* char *name;
*
* DESCRIPTION
*
* Given name of a new file (or "-" for stdout) opens the file
* and sets the output stream to the new file.
*
*/
static void DBUGOpenFile (const char *name,int append)
{
REGISTER FILE *fp;
REGISTER BOOLEAN newfile;
if (name != NULL)
{
strmov(stack->name,name);
if (strcmp (name, "-") == 0)
{
_db_fp_ = stdout;
stack -> out_file = _db_fp_;
stack -> flags |= FLUSH_ON_WRITE;
}
else
{
if (!Writable((char*)name))
{
(void) fprintf (stderr, ERR_OPEN, _db_process_, name);
perror ("");
fflush(stderr);
}
else
{
newfile= !EXISTS (name);
if (!(fp = fopen(name, append ? "a+" : "w")))
{
(void) fprintf (stderr, ERR_OPEN, _db_process_, name);
perror ("");
fflush(stderr);
}
else
{
_db_fp_ = fp;
stack -> out_file = fp;
if (newfile) {
ChangeOwner (name);
}
}
}
}
}
}
/*
* FUNCTION
*
* OpenProfile open new output stream for profiler output
*
* SYNOPSIS
*
* static FILE *OpenProfile (name)
* char *name;
*
* DESCRIPTION
*
* Given name of a new file, opens the file
* and sets the profiler output stream to the new file.
*
* It is currently unclear whether the prefered behavior is
* to truncate any existing file, or simply append to it.
* The latter behavior would be desirable for collecting
* accumulated runtime history over a number of separate
* runs. It might take some changes to the analyzer program
* though, and the notes that Binayak sent with the profiling
* diffs indicated that append was the normal mode, but this
* does not appear to agree with the actual code. I haven't
* investigated at this time [fnf; 24-Jul-87].
*/
#ifndef THREAD
static FILE *OpenProfile (const char *name)
{
REGISTER FILE *fp;
REGISTER BOOLEAN newfile;
fp=0;
if (!Writable (name))
{
(void) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
perror ("");
dbug_flush(0);
(void) Delay (stack -> delay);
}
else
{
newfile= !EXISTS (name);
if (!(fp = fopen (name, "w")))
{
(void) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
perror ("");
dbug_flush(0);
}
else
{
_db_pfp_ = fp;
stack -> prof_file = fp;
if (newfile)
{
ChangeOwner (name);
}
}
}
return fp;
}
#endif
/*
* FUNCTION
*
* CloseFile close the debug output stream
*
* SYNOPSIS
*
* static VOID CloseFile (fp)
* FILE *fp;
*
* DESCRIPTION
*
* Closes the debug output stream unless it is standard output
* or standard error.
*
*/
static void CloseFile (
FILE *fp)
{
if (fp != stderr && fp != stdout) {
if (fclose (fp) == EOF) {
pthread_mutex_lock(&THR_LOCK_dbug);
(void) fprintf (_db_fp_, ERR_CLOSE, _db_process_);
perror ("");
dbug_flush(0);
}
}
}
/*
* FUNCTION
*
* DbugExit print error message and exit
*
* SYNOPSIS
*
* static VOID DbugExit (why)
* char *why;
*
* DESCRIPTION
*
* Prints error message using current process name, the reason for
* aborting (typically out of memory), and exits with status 1.
* This should probably be changed to use a status code
* defined in the user's debugger include file.
*
*/
static void DbugExit (const char *why)
{
(void) fprintf (stderr, ERR_ABORT, _db_process_, why);
(void) fflush (stderr);
exit (1);
}
/*
* FUNCTION
*
* DbugMalloc allocate memory for debugger runtime support
*
* SYNOPSIS
*
* static long *DbugMalloc (size)
* int size;
*
* DESCRIPTION
*
* Allocate more memory for debugger runtime support functions.
* Failure to to allocate the requested number of bytes is
* immediately fatal to the current process. This may be
* rather unfriendly behavior. It might be better to simply
* print a warning message, freeze the current debugger state,
* and continue execution.
*
*/
static char *DbugMalloc (size_t size)
{
register char *new_malloc;
if (!(new_malloc = (char*) malloc((size_t) size)))
DbugExit ("out of memory");
return (new_malloc);
}
/*
* As strtok but two separators in a row are changed to one
* separator (to allow directory-paths in dos).
*/
static char *static_strtok (char *s1, pchar separator)
{
static char *end = NULL;
reg1 char *rtnval,*cpy;
rtnval = NULL;
if (s1 != NULL)
end = s1;
if (end != NULL && *end != EOS)
{
rtnval=cpy=end;
do
{
if ((*cpy++ = *end++) == separator)
{
if (*end != separator)
{
cpy--; /* Point at separator */
break;
}
end++; /* Two separators in a row, skip one */
}
} while (*end != EOS);
*cpy=EOS; /* Replace last separator */
}
return (rtnval);
}
/*
* FUNCTION
*
* BaseName strip leading pathname components from name
*
* SYNOPSIS
*
* static char *BaseName (pathname)
* char *pathname;
*
* DESCRIPTION
*
* Given pointer to a complete pathname, locates the base file
* name at the end of the pathname and returns a pointer to
* it.
*
*/
static char *BaseName (const char *pathname)
{
register const char *base;
base = strrchr (pathname, FN_LIBCHAR);
if (base++ == NullS)
base = pathname;
return ((char*) base);
}
/*
* FUNCTION
*
* Writable test to see if a pathname is writable/creatable
*
* SYNOPSIS
*
* static BOOLEAN Writable (pathname)
* char *pathname;
*
* DESCRIPTION
*
* Because the debugger might be linked in with a program that
* runs with the set-uid-bit (suid) set, we have to be careful
* about opening a user named file for debug output. This consists
* of checking the file for write access with the real user id,
* or checking the directory where the file will be created.
*
* Returns TRUE if the user would normally be allowed write or
* create access to the named file. Returns FALSE otherwise.
*
*/
#ifndef Writable
static BOOLEAN Writable (
char *pathname)
{
REGISTER BOOLEAN granted;
REGISTER char *lastslash;
granted = FALSE;
if (EXISTS (pathname)) {
if (WRITABLE (pathname)) {
granted = TRUE;
}
} else {
lastslash = strrchr (pathname, '/');
if (lastslash != NULL) {
*lastslash = EOS;
} else {
pathname = ".";
}
if (WRITABLE (pathname)) {
granted = TRUE;
}
if (lastslash != NULL) {
*lastslash = '/';
}
}
return (granted);
}
#endif
/*
* FUNCTION
*
* ChangeOwner change owner to real user for suid programs
*
* SYNOPSIS
*
* static VOID ChangeOwner (pathname)
*
* DESCRIPTION
*
* For unix systems, change the owner of the newly created debug
* file to the real owner. This is strictly for the benefit of
* programs that are running with the set-user-id bit set.
*
* Note that at this point, the fact that pathname represents
* a newly created file has already been established. If the
* program that the debugger is linked to is not running with
* the suid bit set, then this operation is redundant (but
* harmless).
*
*/
#ifndef ChangeOwner
static void ChangeOwner (
char *pathname)
{
if (chown (pathname, getuid (), getgid ()) == -1)
{
(void) fprintf (stderr, ERR_CHOWN, _db_process_, pathname);
perror ("");
(void) fflush (stderr);
}
}
#endif
/*
* FUNCTION
*
* _db_setjmp_ save debugger environment
*
* SYNOPSIS
*
* VOID _db_setjmp_ ()
*
* DESCRIPTION
*
* Invoked as part of the user's DBUG_SETJMP macro to save
* the debugger environment in parallel with saving the user's
* environment.
*
*/
#ifdef HAVE_LONGJMP
EXPORT void _db_setjmp_ ()
{
CODE_STATE *state;
state=code_state();
state->jmplevel = state->level;
state->jmpfunc = state->func;
state->jmpfile = state->file;
}
/*
* FUNCTION
*
* _db_longjmp_ restore previously saved debugger environment
*
* SYNOPSIS
*
* VOID _db_longjmp_ ()
*
* DESCRIPTION
*
* Invoked as part of the user's DBUG_LONGJMP macro to restore
* the debugger environment in parallel with restoring the user's
* previously saved environment.
*
*/
EXPORT void _db_longjmp_ ()
{
CODE_STATE *state;
state=code_state();
state->level = state->jmplevel;
if (state->jmpfunc) {
state->func = state->jmpfunc;
}
if (state->jmpfile) {
state->file = state->jmpfile;
}
}
#endif
/*
* FUNCTION
*
* DelayArg convert D flag argument to appropriate value
*
* SYNOPSIS
*
* static int DelayArg (value)
* int value;
*
* DESCRIPTION
*
* Converts delay argument, given in tenths of a second, to the
* appropriate numerical argument used by the system to delay
* that that many tenths of a second. For example, on the
* amiga, there is a system call "Delay()" which takes an
* argument in ticks (50 per second). On unix, the sleep
* command takes seconds. Thus a value of "10", for one
* second of delay, gets converted to 50 on the amiga, and 1
* on unix. Other systems will need to use a timing loop.
*
*/
#ifdef AMIGA
#define HZ (50) /* Probably in some header somewhere */
#endif
static int DelayArg (
int value)
{
uint delayarg = 0;
#if (unix || xenix)
delayarg = value / 10; /* Delay is in seconds for sleep () */
#endif
#ifdef AMIGA
delayarg = (HZ * value) / 10; /* Delay in ticks for Delay () */
#endif
return (delayarg);
}
/*
* A dummy delay stub for systems that do not support delays.
* With a little work, this can be turned into a timing loop.
*/
#if ! defined(Delay) && ! defined(AMIGA)
static int Delay (
int ticks)
{
return ticks;
}
#endif
/*
* FUNCTION
*
* perror perror simulation for systems that don't have it
*
* SYNOPSIS
*
* static VOID perror (s)
* char *s;
*
* DESCRIPTION
*
* Perror produces a message on the standard error stream which
* provides more information about the library or system error
* just encountered. The argument string s is printed, followed
* by a ':', a blank, and then a message and a newline.
*
* An undocumented feature of the unix perror is that if the string
* 's' is a null string (NOT a NULL pointer!), then the ':' and
* blank are not printed.
*
* This version just complains about an "unknown system error".
*
*/
#ifndef HAVE_PERROR
static void perror (s)
char *s;
{
if (s && *s != EOS) {
(void) fprintf (stderr, "%s: ", s);
}
(void) fprintf (stderr, "<unknown system error>\n");
}
#endif /* HAVE_PERROR */
/* flush dbug-stream, free mutex lock & wait delay */
/* This is because some systems (MSDOS!!) dosn't flush fileheader */
/* and dbug-file isn't readable after a system crash !! */
static void dbug_flush(CODE_STATE *state)
{
#ifndef THREAD
if (stack->flags & FLUSH_ON_WRITE)
#endif
{
#if defined(MSDOS) || defined(__WIN__)
if (_db_fp_ != stdout && _db_fp_ != stderr)
{
if (!(freopen(stack->name,"a",_db_fp_)))
{
(void) fprintf(stderr, ERR_OPEN, _db_process_, stack->name);
fflush(stderr);
_db_fp_ = stdout;
stack -> out_file = _db_fp_;
stack -> flags|=FLUSH_ON_WRITE;
}
}
else
#endif
{
(void) fflush (_db_fp_);
if (stack->delay)
(void) Delay (stack->delay);
}
}
if (!state || !state->locked)
pthread_mutex_unlock(&THR_LOCK_dbug);
} /* dbug_flush */
void _db_lock_file()
{
CODE_STATE *state;
state=code_state();
pthread_mutex_lock(&THR_LOCK_dbug);
state->locked=1;
}
void _db_unlock_file()
{
CODE_STATE *state;
state=code_state();
state->locked=0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
/*
* Here we need the definitions of the clock routine. Add your
* own for whatever system that you have.
*/
#ifndef THREAD
#if defined(HAVE_GETRUSAGE)
#include <sys/param.h>
#include <sys/resource.h>
/* extern int getrusage(int, struct rusage *); */
/*
* Returns the user time in milliseconds used by this process so
* far.
*/
static unsigned long Clock ()
{
struct rusage ru;
(void) getrusage (RUSAGE_SELF, &ru);
return ((ru.ru_utime.tv_sec * 1000) + (ru.ru_utime.tv_usec / 1000));
}
#elif defined(MSDOS) || defined(__WIN__) || defined(OS2)
static ulong Clock()
{
return clock()*(1000/CLOCKS_PER_SEC);
}
#elif defined (amiga)
struct DateStamp { /* Yes, this is a hack, but doing it right */
long ds_Days; /* is incredibly ugly without splitting this */
long ds_Minute; /* off into a separate file */
long ds_Tick;
};
static int first_clock = TRUE;
static struct DateStamp begin;
static struct DateStamp elapsed;
static unsigned long Clock ()
{
register struct DateStamp *now;
register unsigned long millisec = 0;
extern VOID *AllocMem ();
now = (struct DateStamp *) AllocMem ((long) sizeof (struct DateStamp), 0L);
if (now != NULL) {
if (first_clock == TRUE) {
first_clock = FALSE;
(void) DateStamp (now);
begin = *now;
}
(void) DateStamp (now);
millisec = 24 * 3600 * (1000 / HZ) * (now -> ds_Days - begin.ds_Days);
millisec += 60 * (1000 / HZ) * (now -> ds_Minute - begin.ds_Minute);
millisec += (1000 / HZ) * (now -> ds_Tick - begin.ds_Tick);
(void) FreeMem (now, (long) sizeof (struct DateStamp));
}
return (millisec);
}
#else
static unsigned long Clock ()
{
return (0);
}
#endif /* RUSAGE */
#endif /* THREADS */
#ifdef NO_VARARGS
/*
* Fake vfprintf for systems that don't support it. If this
* doesn't work, you are probably SOL...
*/
static int vfprintf (stream, format, ap)
FILE *stream;
char *format;
va_list ap;
{
int rtnval;
ARGS_DCL;
ARG0 = va_arg (ap, ARGS_TYPE);
ARG1 = va_arg (ap, ARGS_TYPE);
ARG2 = va_arg (ap, ARGS_TYPE);
ARG3 = va_arg (ap, ARGS_TYPE);
ARG4 = va_arg (ap, ARGS_TYPE);
ARG5 = va_arg (ap, ARGS_TYPE);
ARG6 = va_arg (ap, ARGS_TYPE);
ARG7 = va_arg (ap, ARGS_TYPE);
ARG8 = va_arg (ap, ARGS_TYPE);
ARG9 = va_arg (ap, ARGS_TYPE);
rtnval = fprintf (stream, format, ARGS_LIST);
return (rtnval);
}
#endif /* NO_VARARGS */
|