summaryrefslogtreecommitdiffstats
path: root/kdbg/gdbdriver.cpp
blob: 16efe9ada600452f377fd24b9c22735cc54dc1ac (plain)
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
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
/*
 * Copyright Johannes Sixt
 * This file is licensed under the GNU General Public License Version 2.
 * See the file COPYING in the toplevel directory of the source directory.
 */

#include "gdbdriver.h"
#include "exprwnd.h"
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tdelocale.h>			/* i18n */
#include <ctype.h>
#include <stdlib.h>			/* strtol, atoi */
#include <string.h>			/* strcpy */

#include "assert.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mydebug.h"

static void skipString(const char*& p);
static void skipNested(const char*& s, char opening, char closing);
static ExprValue* parseVar(const char*& s);
static bool parseName(const char*& s, TQString& name, VarTree::NameKind& kind);
static bool parseValue(const char*& s, ExprValue* variable);
static bool parseNested(const char*& s, ExprValue* variable);
static bool parseVarSeq(const char*& s, ExprValue* variable);
static bool parseValueSeq(const char*& s, ExprValue* variable);

#define PROMPT "(kdbg)"
#define PROMPT_LEN 6
#define PROMPT_LAST_CHAR ')'		/* needed when searching for prompt string */


// TODO: make this cmd info stuff non-static to allow multiple
// simultaneous gdbs to run!

struct GdbCmdInfo {
    DbgCommand cmd;
    const char* fmt;			/* format string */
    enum Args {
	argNone, argString, argNum,
	argStringNum, argNumString,
	argString2, argNum2
    } argsNeeded;
};

#if 0
// This is  how the TQString data print statement generally looks like.
// It is set by KDebugger via setPrintTQStringDataCmd().

static const char printTQStringStructFmt[] =
		// if the string data is junk, fail early
		"print ($qstrunicode=($qstrdata=(%s))->unicode)?"
		// print an array of shorts
		"(*(unsigned short*)$qstrunicode)@"
		// limit the length
		"(($qstrlen=(unsigned int)($qstrdata->len))>100?100:$qstrlen)"
		// if unicode data is 0, report a special value
		":1==0\n";
#endif
static const char printTQStringStructFmt[] = "print (0?\"%s\":$kdbgundef)\n";

/*
 * The following array of commands must be sorted by the DC* values,
 * because they are used as indices.
 */
static GdbCmdInfo cmds[] = {
    { DCinitialize, "", GdbCmdInfo::argNone },
    { DCtty, "tty %s\n", GdbCmdInfo::argString },
    { DCexecutable, "file \"%s\"\n", GdbCmdInfo::argString },
    { DCtargetremote, "target remote %s\n", GdbCmdInfo::argString },
#ifdef __FreeBSD__
    { DCcorefile, "target FreeBSD-core %s\n", GdbCmdInfo::argString },
#else
    { DCcorefile, "target core %s\n", GdbCmdInfo::argString },
#endif
    { DCattach, "attach %s\n", GdbCmdInfo::argString },
    { DCinfolinemain, "kdbg_infolinemain\n", GdbCmdInfo::argNone },
    { DCinfolocals, "kdbg__alllocals\n", GdbCmdInfo::argNone },
    { DCinforegisters, "info all-registers\n", GdbCmdInfo::argNone},
    { DCexamine, "x %s %s\n", GdbCmdInfo::argString2 },
    { DCinfoline, "info line %s:%d\n", GdbCmdInfo::argStringNum },
    { DCdisassemble, "disassemble %s %s\n", GdbCmdInfo::argString2 },
    { DCsetargs, "set args %s\n", GdbCmdInfo::argString },
    { DCsetenv, "set env %s %s\n", GdbCmdInfo::argString2 },
    { DCunsetenv, "unset env %s\n", GdbCmdInfo::argString },
    { DCsetoption, "setoption %s %d\n", GdbCmdInfo::argStringNum},
    { DCcd, "cd %s\n", GdbCmdInfo::argString },
    { DCbt, "bt\n", GdbCmdInfo::argNone },
    { DCrun, "run\n", GdbCmdInfo::argNone },
    { DCcont, "cont\n", GdbCmdInfo::argNone },
    { DCstep, "step\n", GdbCmdInfo::argNone },
    { DCstepi, "stepi\n", GdbCmdInfo::argNone },
    { DCnext, "next\n", GdbCmdInfo::argNone },
    { DCnexti, "nexti\n", GdbCmdInfo::argNone },
    { DCfinish, "finish\n", GdbCmdInfo::argNone },
    { DCuntil, "until %s:%d\n", GdbCmdInfo::argStringNum },
    { DCkill, "kill\n", GdbCmdInfo::argNone },
    { DCbreaktext, "break %s\n", GdbCmdInfo::argString },
    { DCbreakline, "break %s:%d\n", GdbCmdInfo::argStringNum },
    { DCtbreakline, "tbreak %s:%d\n", GdbCmdInfo::argStringNum },
    { DCbreakaddr, "break *%s\n", GdbCmdInfo::argString },
    { DCtbreakaddr, "tbreak *%s\n", GdbCmdInfo::argString },
    { DCwatchpoint, "watch %s\n", GdbCmdInfo::argString },
    { DCdelete, "delete %d\n", GdbCmdInfo::argNum },
    { DCenable, "enable %d\n", GdbCmdInfo::argNum },
    { DCdisable, "disable %d\n", GdbCmdInfo::argNum },
    { DCprint, "print %s\n", GdbCmdInfo::argString },
    { DCprintDeref, "print *(%s)\n", GdbCmdInfo::argString },
    { DCprintStruct, "print %s\n", GdbCmdInfo::argString },
    { DCprintTQStringStruct, printTQStringStructFmt, GdbCmdInfo::argString},
    { DCframe, "frame %d\n", GdbCmdInfo::argNum },
    { DCfindType, "whatis %s\n", GdbCmdInfo::argString },
    { DCinfosharedlib, "info sharedlibrary\n", GdbCmdInfo::argNone },
    { DCthread, "thread %d\n", GdbCmdInfo::argNum },
    { DCinfothreads, "info threads\n", GdbCmdInfo::argNone },
    { DCinfobreak, "info breakpoints\n", GdbCmdInfo::argNone },
    { DCcondition, "condition %d %s\n", GdbCmdInfo::argNumString},
    { DCsetpc, "set variable $pc=%s\n", GdbCmdInfo::argString },
    { DCignore, "ignore %d %d\n", GdbCmdInfo::argNum2},
    { DCprintWChar, "print ($s=%s)?*$s@wcslen($s):0x0\n", GdbCmdInfo::argString },
    { DCsetvariable, "set variable %s=%s\n", GdbCmdInfo::argString2 },
};

#define NUM_CMDS (int(sizeof(cmds)/sizeof(cmds[0])))
#define MAX_FMTLEN 200

GdbDriver::GdbDriver() :
	DebuggerDriver()
{
    strcpy(m_prompt, PROMPT);
    m_promptMinLen = PROMPT_LEN;
    m_promptLastChar = PROMPT_LAST_CHAR;

#ifndef NDEBUG
    // check command info array
    const char* perc;
    for (int i = 0; i < NUM_CMDS; i++) {
	// must be indexable by DbgCommand values, i.e. sorted by DbgCommand values
	assert(i == cmds[i].cmd);
	// a format string must be associated
	assert(cmds[i].fmt != 0);
	assert(strlen(cmds[i].fmt) <= MAX_FMTLEN);
	// format string must match arg specification
	switch (cmds[i].argsNeeded) {
	case GdbCmdInfo::argNone:
	    assert(strchr(cmds[i].fmt, '%') == 0);
	    break;
	case GdbCmdInfo::argString:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 's');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	case GdbCmdInfo::argNum:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 'd');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	case GdbCmdInfo::argStringNum:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 's');
	    perc = strchr(perc+2, '%');
	    assert(perc != 0 && perc[1] == 'd');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	case GdbCmdInfo::argNumString:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 'd');
	    perc = strchr(perc+2, '%');
	    assert(perc != 0 && perc[1] == 's');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	case GdbCmdInfo::argString2:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 's');
	    perc = strchr(perc+2, '%');
	    assert(perc != 0 && perc[1] == 's');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	case GdbCmdInfo::argNum2:
	    perc = strchr(cmds[i].fmt, '%');
	    assert(perc != 0 && perc[1] == 'd');
	    perc = strchr(perc+2, '%');
	    assert(perc != 0 && perc[1] == 'd');
	    assert(strchr(perc+2, '%') == 0);
	    break;
	}
    }
    assert(strlen(printTQStringStructFmt) <= MAX_FMTLEN);
#endif
}

GdbDriver::~GdbDriver()
{
}


TQString GdbDriver::driverName() const
{
    return "GDB";
}

TQString GdbDriver::defaultGdb()
{
    return
	"gdb"
	" --fullname"	/* to get standard file names each time the prog stops */
	" --nx";	/* do not execute initialization files */
}

TQString GdbDriver::defaultInvocation() const
{
    if (m_defaultCmd.isEmpty()) {
	return defaultGdb();
    } else {
	return m_defaultCmd;
    }
}

TQStringList GdbDriver::boolOptionList() const
{
    // no options
    return TQStringList();
}

bool GdbDriver::startup(TQString cmdStr)
{
    if (!DebuggerDriver::startup(cmdStr))
	return false;

    static const char gdbInitialize[] =
	/*
	 * Work around buggy gdbs that do command line editing even if they
	 * are not on a tty. The readline library echos every command back
	 * in this case, which is confusing for us.
	 */
	"set editing off\n"
	"set confirm off\n"
	"set print static-members off\n"
	"set print asm-demangle on\n"
	/*
	 * Don't assume that program functions invoked from a watch expression
	 * always succeed.
	 */
	"set unwindonsignal on\n"
	/*
	 * Write a short macro that prints all locals: local variables and
	 * function arguments.
	 */
	"define kdbg__alllocals\n"
	"info locals\n"			/* local vars supersede args with same name */
	"info args\n"			/* therefore, arguments must come last */
	"end\n"
	/*
	 * Work around a bug in gdb-6.3: "info line main" crashes gdb.
	 */
	"define kdbg_infolinemain\n"
	"list\n"
	"info line\n"
	"end\n"
	// change prompt string and synchronize with gdb
	"set prompt " PROMPT "\n"
	;

    executeCmdString(DCinitialize, gdbInitialize, false);

    // assume that TQString::null is ok
    cmds[DCprintTQStringStruct].fmt = printTQStringStructFmt;

    return true;
}

void GdbDriver::commandFinished(CmdQueueItem* cmd)
{
    // command string must be committed
    if (!cmd->m_committed) {
	// not commited!
	TRACE("calling " + (__PRETTY_FUNCTION__ + (" with uncommited command:\n\t" +
	      cmd->m_cmdString)));
	return;
    }

    switch (cmd->m_cmd) {
    case DCinitialize:
	{
	    /*
	     * Check for GDB 7.1 or later; the syntax for the disassemble
	     * command has changed.
	     * This RE picks the last version number in the first line,
	     * because at least OpenSUSE writes its own version number
	     * in the first line (but before GDB's version number).
	     */
	    TQRegExp re(
		" "			// must be preceded by space
		"[(]?"			// SLES 10 embeds in parentheses
		"(\\d+)\\.(\\d+)"	// major, minor
		"[^ ]*\\n"		// no space until end of line
		);
	    int pos = re.search(m_output);
	    const char* disass = "disassemble %s %s\n";
	    if (pos >= 0) {
		int major = re.cap(1).toInt();
		int minor = re.cap(2).toInt();
		if (major > 7 || (major == 7 && minor >= 1))
		{
		    disass = "disassemble %s, %s\n";
		}
	    }
	    cmds[DCdisassemble].fmt = disass;
	}
	break;
    default:;
    }

    /* ok, the command is ready */
    emit commandReceived(cmd, m_output);

    switch (cmd->m_cmd) {
    case DCcorefile:
    case DCinfolinemain:
    case DCframe:
    case DCattach:
    case DCrun:
    case DCcont:
    case DCstep:
    case DCstepi:
    case DCnext:
    case DCnexti:
    case DCfinish:
    case DCuntil:
	parseMarker();
    default:;
    }
}

/*
 * The --fullname option makes gdb send a special normalized sequence print
 * each time the program stops and at some other points. The sequence has
 * the form "\032\032filename:lineno:charoffset:(beg|middle):address".
 */
void GdbDriver::parseMarker()
{
    char* startMarker = strstr(m_output, "\032\032");
    if (startMarker == 0)
	return;

    // extract the marker
    startMarker += 2;
    TRACE(TQString("found marker: ") + startMarker);
    char* endMarker = strchr(startMarker, '\n');
    if (endMarker == 0)
	return;

    *endMarker = '\0';

    // extract filename and line number
    static TQRegExp MarkerRE(":[0-9]+:[0-9]+:[begmidl]+:0x");

    int len;
    int lineNoStart = MarkerRE.match(startMarker, 0, &len);
    if (lineNoStart >= 0) {
	int lineNo = atoi(startMarker + lineNoStart+1);

	// get address
	const char* addrStart = startMarker + lineNoStart + len - 2;
	DbgAddr address = TQString(addrStart).stripWhiteSpace();

	// now show the window
	startMarker[lineNoStart] = '\0';   /* split off file name */
	emit activateFileLine(startMarker, lineNo-1, address);
    }
}


/*
 * Escapes characters that might lead to problems when they appear on gdb's
 * command line.
 */
static void normalizeStringArg(TQString& arg)
{
    /*
     * Remove trailing backslashes. This approach is a little simplistic,
     * but we know that there is at the moment no case where a trailing
     * backslash would make sense.
     */
    while (!arg.isEmpty() && arg[arg.length()-1] == '\\') {
	arg = arg.left(arg.length()-1);
    }
}


TQString GdbDriver::makeCmdString(DbgCommand cmd, TQString strArg)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString);

    normalizeStringArg(strArg);

    if (cmd == DCcd) {
	// need the working directory when parsing the output
	m_programWD = strArg;
    } else if (cmd == DCsetargs && !m_redirect.isEmpty()) {
	/*
	 * Use saved redirection. We prepend it in front of the user's
	 * arguments so that the user can override the redirections.
	*/
	strArg = m_redirect + " " + strArg;
    }

    TQString cmdString;
    cmdString.sprintf(cmds[cmd].fmt, strArg.latin1());
    return cmdString;
}

TQString GdbDriver::makeCmdString(DbgCommand cmd, int intArg)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum);

    TQString cmdString;
    cmdString.sprintf(cmds[cmd].fmt, intArg);
    return cmdString;
}

TQString GdbDriver::makeCmdString(DbgCommand cmd, TQString strArg, int intArg)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum ||
	   cmds[cmd].argsNeeded == GdbCmdInfo::argNumString ||
	   cmd == DCexamine ||
	   cmd == DCtty);

    normalizeStringArg(strArg);

    TQString cmdString;

    if (cmd == DCtty)
    {
	/*
	 * intArg specifies which channels should be redirected to
	 * /dev/null. It is a value or'ed together from RDNstdin,
	 * RDNstdout, RDNstderr. We store the value for a later DCsetargs
	 * command.
	 * 
	 * Note: We rely on that after the DCtty a DCsetargs will follow,
	 * which will ultimately apply the redirection.
	 */
	static const char* const runRedir[8] = {
	    "",
	    "</dev/null",
	    ">/dev/null",
	    "</dev/null >/dev/null",
	    "2>/dev/null",
	    "</dev/null 2>/dev/null",
	    ">/dev/null 2>&1",
	    "</dev/null >/dev/null 2>&1"
	};
	if (strArg.isEmpty())
	    intArg = 7;			/* failsafe if no tty */
	m_redirect = runRedir[intArg & 7];

	return makeCmdString(DCtty, strArg);   /* note: no problem if strArg empty */
    }

    if (cmd == DCexamine) {
	// make a format specifier from the intArg
	static const char size[16] = {
	    '\0', 'b', 'h', 'w', 'g'
	};
	static const char format[16] = {
	    '\0', 'x', 'd', 'u', 'o', 't',
	    'a',  'c', 'f', 's', 'i'
	};
	assert(MDTsizemask == 0xf);	/* lowest 4 bits */
	assert(MDTformatmask == 0xf0);	/* next 4 bits */
	int count = 16;			/* number of entities to print */
	char sizeSpec = size[intArg & MDTsizemask];
	char formatSpec = format[(intArg & MDTformatmask) >> 4];
	assert(sizeSpec != '\0');
	assert(formatSpec != '\0');
	// adjust count such that 16 lines are printed
	switch (intArg & MDTformatmask) {
	case MDTstring: case MDTinsn:
	    break;			/* no modification needed */
	default:
	    // all cases drop through:
	    switch (intArg & MDTsizemask) {
	    case MDTbyte:
	    case MDThalfword:
		count *= 2;
	    case MDTword:
		count *= 2;
	    case MDTgiantword:
		count *= 2;
	    }
	    break;
	}
	TQString spec;
	spec.sprintf("/%d%c%c", count, sizeSpec, formatSpec);

	return makeCmdString(DCexamine, spec, strArg);
    }

    if (cmds[cmd].argsNeeded == GdbCmdInfo::argStringNum)
    {
	// line numbers are zero-based
	if (cmd == DCuntil || cmd == DCbreakline ||
	    cmd == DCtbreakline || cmd == DCinfoline)
	{
	    intArg++;
	}
	if (cmd == DCinfoline)
	{
	    // must split off file name part
	    int slash = strArg.findRev('/');
	    if (slash >= 0)
		strArg = strArg.right(strArg.length()-slash-1);
	}
	cmdString.sprintf(cmds[cmd].fmt, strArg.latin1(), intArg);
    }
    else
    {
	cmdString.sprintf(cmds[cmd].fmt, intArg, strArg.latin1());
    }
    return cmdString;
}

TQString GdbDriver::makeCmdString(DbgCommand cmd, TQString strArg1, TQString strArg2)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argString2);

    normalizeStringArg(strArg1);
    normalizeStringArg(strArg2);

    TQString cmdString;
    cmdString.sprintf(cmds[cmd].fmt, strArg1.latin1(), strArg2.latin1());
    return cmdString;
}

TQString GdbDriver::makeCmdString(DbgCommand cmd, int intArg1, int intArg2)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNum2);

    TQString cmdString;
    cmdString.sprintf(cmds[cmd].fmt, intArg1, intArg2);
    return cmdString;
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, bool clearLow)
{
    assert(cmd >= 0 && cmd < NUM_CMDS);
    assert(cmds[cmd].argsNeeded == GdbCmdInfo::argNone);

    if (cmd == DCrun) {
	m_haveCoreFile = false;
    }

    return executeCmdString(cmd, cmds[cmd].fmt, clearLow);
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, TQString strArg,
				    bool clearLow)
{
    return executeCmdString(cmd, makeCmdString(cmd, strArg), clearLow);
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg,
				    bool clearLow)
{

    return executeCmdString(cmd, makeCmdString(cmd, intArg), clearLow);
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, TQString strArg, int intArg,
				    bool clearLow)
{
    return executeCmdString(cmd, makeCmdString(cmd, strArg, intArg), clearLow);
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, TQString strArg1, TQString strArg2,
				    bool clearLow)
{
    return executeCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), clearLow);
}

CmdQueueItem* GdbDriver::executeCmd(DbgCommand cmd, int intArg1, int intArg2,
				    bool clearLow)
{
    return executeCmdString(cmd, makeCmdString(cmd, intArg1, intArg2), clearLow);
}

CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, QueueMode mode)
{
    return queueCmdString(cmd, cmds[cmd].fmt, mode);
}

CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, TQString strArg,
				  QueueMode mode)
{
    return queueCmdString(cmd, makeCmdString(cmd, strArg), mode);
}

CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, int intArg,
				  QueueMode mode)
{
    return queueCmdString(cmd, makeCmdString(cmd, intArg), mode);
}

CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, TQString strArg, int intArg,
				  QueueMode mode)
{
    return queueCmdString(cmd, makeCmdString(cmd, strArg, intArg), mode);
}

CmdQueueItem* GdbDriver::queueCmd(DbgCommand cmd, TQString strArg1, TQString strArg2,
				  QueueMode mode)
{
    return queueCmdString(cmd, makeCmdString(cmd, strArg1, strArg2), mode);
}

void GdbDriver::terminate()
{
    kill(SIGTERM);
    m_state = DSidle;
}

void GdbDriver::detachAndTerminate()
{
    kill(SIGINT);
    flushCommands();
    executeCmdString(DCinitialize, "detach\nquit\n", true);
}

void GdbDriver::interruptInferior()
{
    kill(SIGINT);
    // remove accidentally queued commands
    flushHiPriQueue();
}

static bool isErrorExpr(const char* output)
{
    return
	strncmp(output, "Cannot access memory at", 23) == 0 ||
	strncmp(output, "Attempt to dereference a generic pointer", 40) == 0 ||
	strncmp(output, "Attempt to take contents of ", 28) == 0 ||
	strncmp(output, "Attempt to use a type name as an expression", 43) == 0 ||
	strncmp(output, "There is no member or method named", 34) == 0 ||
	strncmp(output, "A parse error in expression", 27) == 0 ||
	strncmp(output, "No symbol \"", 11) == 0 ||
	strncmp(output, "Internal error: ", 16) == 0;
}

/**
 * Returns true if the output is an error message. If wantErrorValue is
 * true, a new ExprValue object is created and filled with the error message.
 * If there are warnings, they are skipped and output points past the warnings
 * on return (even if there \e are errors).
 */
static bool parseErrorMessage(const char*& output,
			      ExprValue*& variable, bool wantErrorValue)
{
    // skip warnings
    while (strncmp(output, "warning:", 8) == 0)
    {
	const char* end = strchr(output+8, '\n');
	if (end == 0)
	    output += strlen(output);
	else
	    output = end+1;
    }

    if (isErrorExpr(output))
    {
	if (wantErrorValue) {
	    // put the error message as value in the variable
	    variable = new ExprValue(TQString(), VarTree::NKplain);
	    const char* endMsg = strchr(output, '\n');
	    if (endMsg == 0)
		endMsg = output + strlen(output);
	    variable->m_value = TQString::fromLatin1(output, endMsg-output);
	} else {
	    variable = 0;
	}
	return true;
    }
    return false;
}

#if TQT_VERSION >= 300
union TQt2TQChar {
    short s;
    struct {
	uchar row;
	uchar cell;
    } qch;
};
#endif

void GdbDriver::setPrintTQStringDataCmd(const char* cmd)
{
    // don't accept the command if it is empty
    if (cmd == 0 || *cmd == '\0')
	return;
    assert(strlen(cmd) <= MAX_FMTLEN);
    cmds[DCprintTQStringStruct].fmt = cmd;
}

ExprValue* GdbDriver::parseTQCharArray(const char* output, bool wantErrorValue, bool qt3like)
{
    ExprValue* variable = 0;

    /*
     * Parse off white space. gdb sometimes prints white space first if the
     * printed array leaded to an error.
     */
    while (isspace(*output))
	output++;

    // special case: empty string (0 repetitions)
    if (strncmp(output, "Invalid number 0 of repetitions", 31) == 0)
    {
	variable = new ExprValue(TQString(), VarTree::NKplain);
	variable->m_value = "\"\"";
	return variable;
    }

    // check for error conditions
    if (parseErrorMessage(output, variable, wantErrorValue))
	return variable;

    // parse the array

    // find '='
    const char* p = output;
    p = strchr(p, '=');
    if (p == 0) {
	goto error;
    }
    // skip white space
    do {
	p++;
    } while (isspace(*p));

    if (*p == '{')
    {
	// this is the real data
	p++;				/* skip '{' */

	// parse the array
	TQString result;
	TQString repeatCount;
	enum { wasNothing, wasChar, wasRepeat } lastThing = wasNothing;
	/*
	 * A matrix for separators between the individual "things"
	 * that are added to the string. The first index is a bool,
	 * the second index is from the enum above.
	 */
	static const char* separator[2][3] = {
	    { "\"", 0,       ", \"" },	/* normal char is added */
	    { "'",  "\", '", ", '" }	/* repeated char is added */
	};

	while (isdigit(*p)) {
	    // parse a number
	    char* end;
	    unsigned short value = (unsigned short) strtoul(p, &end, 0);
	    if (end == p)
		goto error;		/* huh? no valid digits */
	    // skip separator and search for a repeat count
	    p = end;
	    while (isspace(*p) || *p == ',')
		p++;
	    bool repeats = strncmp(p, "<repeats ", 9) == 0;
	    if (repeats) {
		const char* start = p;
		p = strchr(p+9, '>');	/* search end and advance */
		if (p == 0)
		    goto error;
		p++;			/* skip '>' */
		repeatCount = TQString::fromLatin1(start, p-start);
		while (isspace(*p) || *p == ',')
		    p++;
	    }
	    // p is now at the next char (or the end)

	    // interpret the value as a TQChar
	    // TODO: make cross-architecture compatible
	    TQChar ch;
	    if (qt3like) {
		ch = TQChar(value);
	    } else {
#if TQT_VERSION < 300
		(unsigned short&)ch = value;
#else
		TQt2TQChar c;
		c.s = value;
		ch.setRow(c.qch.row);
		ch.setCell(c.qch.cell);
#endif
	    }

	    // escape a few frequently used characters
	    char escapeCode = '\0';
	    switch (ch.latin1()) {
	    case '\n': escapeCode = 'n'; break;
	    case '\r': escapeCode = 'r'; break;
	    case '\t': escapeCode = 't'; break;
	    case '\b': escapeCode = 'b'; break;
	    case '\"': escapeCode = '\"'; break;
	    case '\\': escapeCode = '\\'; break;
	    case '\0': if (value == 0) { escapeCode = '0'; } break;
	    }

	    // add separator
	    result += separator[repeats][lastThing];
	    // add char
	    if (escapeCode != '\0') {
		result += '\\';
		ch = escapeCode;
	    }
	    result += ch;

	    // fixup repeat count and lastThing
	    if (repeats) {
		result += "' ";
		result += repeatCount;
		lastThing = wasRepeat;
	    } else {
		lastThing = wasChar;
	    }
	}
	if (*p != '}')
	    goto error;

	// closing quote
	if (lastThing == wasChar)
	    result += "\"";

	// assign the value
	variable = new ExprValue(TQString(), VarTree::NKplain);
	variable->m_value = result;
    }
    else if (strncmp(p, "true", 4) == 0)
    {
	variable = new ExprValue(TQString(), VarTree::NKplain);
	variable->m_value = "TQString::null";
    }
    else if (strncmp(p, "false", 5) == 0)
    {
	variable = new ExprValue(TQString(), VarTree::NKplain);
	variable->m_value = "(null)";
    }
    else
	goto error;
    return variable;

error:
    if (wantErrorValue) {
	variable = new ExprValue(TQString(), VarTree::NKplain);
	variable->m_value = "internal parse error";
    }
    return variable;
}

static ExprValue* parseVar(const char*& s)
{
    const char* p = s;
    
    // skip whitespace
    while (isspace(*p))
	p++;

    TQString name;
    VarTree::NameKind kind;
    /*
     * Detect anonymouse struct values: The 'name =' part is missing:
     *    s = { a = 1, { b = 2 }}
     * Note that this detection works only inside structs when the anonymous
     * struct is not the first member:
     *    s = {{ a = 1 }, b = 2}
     * This is misparsed (by parseNested()) because it is mistakenly
     * interprets the second opening brace as the first element of an array
     * of structs.
     */
    if (*p == '{')
    {
	name = i18n("<anonymous struct or union>");
	kind = VarTree::NKanonymous;
    }
    else
    {
	if (!parseName(p, name, kind)) {
	    return 0;
	}

	// go for '='
	while (isspace(*p))
	    p++;
	if (*p != '=') {
	    TRACE(TQString().sprintf("parse error: = not found after %s", (const char*)name));
	    return 0;
	}
	// skip the '=' and more whitespace
	p++;
	while (isspace(*p))
	    p++;
    }

    ExprValue* variable = new ExprValue(name, kind);
    
    if (!parseValue(p, variable)) {
	delete variable;
	return 0;
    }
    s = p;
    return variable;
}

static void skipNested(const char*& s, char opening, char closing)
{
    const char* p = s;

    // parse a nested type
    int nest = 1;
    p++;
    /*
     * Search for next matching `closing' char, skipping nested pairs of
     * `opening' and `closing'.
     */
    while (*p && nest > 0) {
	if (*p == opening) {
	    nest++;
	} else if (*p == closing) {
	    nest--;
	}
	p++;
    }
    if (nest != 0) {
	TRACE(TQString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
    }
    s = p;
}

/**
 * This function skips text that is delimited by nested angle bracktes, '<>'.
 * A complication arises because the delimited text can contain the names of
 * operator<<, operator>>, operator<, and operator>, which have to be treated
 * specially so that they do not count towards the nesting of '<>'.
 * This function assumes that the delimited text does not contain strings.
 */
static void skipNestedAngles(const char*& s)
{
    const char* p = s;

    int nest = 1;
    p++;		// skip the initial '<'
    while (*p && nest > 0)
    {
	// Below we can check for p-s >= 9 instead of 8 because
	// *s is '<' and cannot be part of "operator".
	if (*p == '<')
	{
	    if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
		if (p[1] == '<')
		    p++;
	    } else {
		nest++;
	    }
	}
	else if (*p == '>')
	{
	    if (p-s >= 9 && strncmp(p-8, "operator", 8) == 0) {
		if (p[1] == '>')
		    p++;
	    } else {
		nest--;
	    }
	}
	p++;
    }
    if (nest != 0) {
	TRACE(TQString().sprintf("parse error: mismatching <> at %-20.20s", s));
    }
    s = p;
}

/**
 * Find the end of line that is not inside braces
 */
static void findEnd(const char*& s)
{
    const char* p = s;
    while (*p && *p!='\n') {
	while (*p && *p!='\n' && *p!='{')
	    p++;
	if (*p=='{') {
	    p++;
	    skipNested(p, '{', '}'); p--;
	}
    }
    s = p;
}

static bool isNumberish(const char ch)
{
    return (ch>='0' && ch<='9') || ch=='.' || ch=='x'; 
}

void skipString(const char*& p)
{
moreStrings:
    // opening quote
    char quote = *p++;
    while (*p != quote) {
	if (*p == '\\') {
	    // skip escaped character
	    // no special treatment for octal values necessary
	    p++;
	}
	// simply return if no more characters
	if (*p == '\0')
	    return;
	p++;
    }
    // closing quote
    p++;
    /*
     * Strings can consist of several parts, some of which contain repeated
     * characters.
     */
    if (quote == '\'') {
	// look ahaead for <repeats 123 times>
	const char* q = p+1;
	while (isspace(*q))
	    q++;
	if (strncmp(q, "<repeats ", 9) == 0) {
	    p = q+9;
	    while (*p != '\0' && *p != '>')
		p++;
	    if (*p != '\0') {
		p++;			/* skip the '>' */
	    }
	}
    }
    // is the string continued?
    if (*p == ',')
    {
	// look ahead for another quote
	const char* q = p+1;
	while (isspace(*q))
	    q++;
	if (*q == '"' || *q == '\'') {
	    // yes!
	    p = q;
	    goto moreStrings;
	}

	// some strings can end in <incomplete sequence ...>
	if (strncmp(q, "<incomplete sequence", 20) == 0)
	{
	    p = q+20;
	    while (*p != '\0' && *p != '>')
		p++;
	    if (*p != '\0') {
		p++;		/* skip the '>' */
	    }
	}
    }
    /* very long strings are followed by `...' */
    if (*p == '.' && p[1] == '.' && p[2] == '.') {
	p += 3;
    }
}

static void skipNestedWithString(const char*& s, char opening, char closing)
{
    const char* p = s;

    // parse a nested expression
    int nest = 1;
    p++;
    /*
     * Search for next matching `closing' char, skipping nested pairs of
     * `opening' and `closing' as well as strings.
     */
    while (*p && nest > 0) {
	if (*p == opening) {
	    nest++;
	} else if (*p == closing) {
	    nest--;
	} else if (*p == '\'' || *p == '\"') {
	    skipString(p);
	    continue;
	}
	p++;
    }
    if (nest > 0) {
	TRACE(TQString().sprintf("parse error: mismatching %c%c at %-20.20s", opening, closing, s));
    }
    s = p;
}

inline void skipName(const char*& p)
{
    // allow : (for enumeration values) and $ and . (for _vtbl.)
    while (isalnum(*p) || *p == '_' || *p == ':' || *p == '$' || *p == '.')
	p++;
}

static bool parseName(const char*& s, TQString& name, VarTree::NameKind& kind)
{
    kind = VarTree::NKplain;

    const char* p = s;
    // examples of names:
    //  name
    //  <Object>
    //  <string<a,b<c>,7> >

    if (*p == '<') {
	skipNestedAngles(p);
	name = TQString::fromLatin1(s, p - s);
	kind = VarTree::NKtype;
    }
    else
    {
	// name, which might be "static"; allow dot for "_vtbl."
	skipName(p);
	if (p == s) {
	    TRACE(TQString().sprintf("parse error: not a name %-20.20s", s));
	    return false;
	}
	int len = p - s;
	if (len == 6 && strncmp(s, "static", 6) == 0) {
	    kind = VarTree::NKstatic;

	    // its a static variable, name comes now
	    while (isspace(*p))
		p++;
	    s = p;
	    skipName(p);
	    if (p == s) {
		TRACE(TQString().sprintf("parse error: not a name after static %-20.20s", s));
		return false;
	    }
	    len = p - s;
	}
	name = TQString::fromLatin1(s, len);
    }
    // return the new position
    s = p;
    return true;
}

static bool parseValue(const char*& s, ExprValue* variable)
{
    variable->m_value = "";

repeat:
    if (*s == '{') {
	// Sometimes we find the following output:
	//  {<text variable, no debug info>} 0x40012000 <access>
	//  {<data variable, no debug info>}
	//  {<variable (not text or data), no debug info>}
	if (strncmp(s, "{<text variable, ", 17) == 0 ||
	    strncmp(s, "{<data variable, ", 17) == 0 ||
	    strncmp(s, "{<variable (not text or data), ", 31) == 0)
	{
	    const char* start = s;
	    skipNested(s, '{', '}');
	    variable->m_value = TQString::fromLatin1(start, s-start);
	    variable->m_value += ' ';	// add only a single space
	    while (isspace(*s))
		s++;
	    goto repeat;
	}
	else
	{
	    s++;
	    if (!parseNested(s, variable)) {
		return false;
	    }
	    // must be the closing brace
	    if (*s != '}') {
		TRACE("parse error: missing } of " +  variable->m_name);
		return false;
	    }
	    s++;
	    // final white space
	    while (isspace(*s))
		s++;
	}
    } else {
	// examples of leaf values (cannot be the empty string):
	//  123
	//  -123
	//  23.575e+37
	//  0x32a45
	//  @0x012ab4
	//  (DwContentType&) @0x8123456: {...}
	//  0x32a45 "text"
	//  10 '\n'
	//  <optimized out>
	//  0x823abc <Array<int> virtual table>
	//  (void (*)()) 0x8048480 <f(E *, char)>
	//  (E *) 0xbffff450
	//  red
	//  &parseP (HTMLClueV *, char *)
	//  Variable "x" is not available.
	//  The value of variable 'x' is distributed...
	//  -nan(0xfffff081defa0)

	const char*p = s;
    
	// check for type
	TQString type;
	if (*p == '(') {
	    skipNested(p, '(', ')');

	    while (isspace(*p))
		p++;
	    variable->m_value = TQString::fromLatin1(s, p - s);
	}

	bool reference = false;
	if (*p == '@') {
	    // skip reference marker
	    p++;
	    reference = true;
	}
	const char* start = p;
	if (*p == '-')
	    p++;

	// some values consist of more than one token
	bool checkMultiPart = false;

	if (p[0] == '0' && p[1] == 'x') {
	    // parse hex number
	    p += 2;
	    while (isxdigit(*p))
		p++;

	    /*
	     * Assume this is a pointer, but only if it's not a reference, since
	     * references can't be expanded.
	     */
	    if (!reference) {
		variable->m_varKind = VarTree::VKpointer;
	    } else {
		/*
		 * References are followed by a colon, in which case we'll
		 * find the value following the reference address.
		 */
		if (*p == ':') {
		    p++;
		} else {
		    // Paranoia. (Can this happen, i.e. reference not followed by ':'?)
		    reference = false;
		}
	    }
	    checkMultiPart = true;
	} else if (isdigit(*p)) {
	    // parse decimal number, possibly a float
	    while (isdigit(*p))
		p++;
	    if (*p == '.') {		/* TODO: obey i18n? */
		// In long arrays an integer may be followed by '...'.
		// We test for this situation and don't gobble the '...'.
		if (p[1] != '.' || p[0] != '.') {
		    // fractional part
		    p++;
		    while (isdigit(*p))
			p++;
		}
	    }
	    if (*p == 'e' || *p == 'E') {
		p++;
		// exponent
		if (*p == '-' || *p == '+')
		    p++;
		while (isdigit(*p))
		    p++;
	    }

	    // for char variables there is the char, eg. 10 '\n'
	    checkMultiPart = true;
	} else if (*p == '<') {
	    // e.g. <optimized out>
	    skipNestedAngles(p);
	} else if (*p == '"' || *p == '\'') {
	    // character may have multipart: '\000' <repeats 11 times>
	    checkMultiPart = *p == '\'';
	    // found a string
	    skipString(p);
	} else if (*p == '&') {
	    // function pointer
	    p++;
	    skipName(p);
	    while (isspace(*p)) {
		p++;
	    }
	    if (*p == '(') {
		skipNested(p, '(', ')');
	    }
	} else if (strncmp(p, "Variable \"", 10) == 0) {
	    // Variable "x" is not available.
	    p += 10;		// skip to "
	    skipName(p);
	    if (strncmp(p, "\" is not available.", 19) == 0) {
		p += 19;
	    }
	} else if (strncmp(p, "The value of variable '", 23) == 0) {
	    p += 23;
	    skipName(p);
	    const char* e = strchr(p, '.');
	    if (e == 0) {
		p += strlen(p);
	    } else {
		p = e+1;
	    }
	} else {
	    // must be an enumeration value
	    skipName(p);
	    // hmm, not necessarily: nan (floating point Not a Number)
	    // is followed by a number in ()
	    if (*p == '(')
		skipNested(p, '(', ')');
	}
	variable->m_value += TQString::fromLatin1(start, p - start);

	// remove line breaks from the value; this is ok since
	// string values never contain a literal line break
	variable->m_value.replace('\n', ' ');

	if (checkMultiPart) {
	    // white space
	    while (isspace(*p))
		p++;
	    // may be followed by a string or <...>
	    start = p;
	    
	    if (*p == '"' || *p == '\'') {
		skipString(p);
	    } else if (*p == '<') {
		// if this value is part of an array, it might be followed
		// by <repeats 15 times>, which we don't skip here
		if (strncmp(p, "<repeats ", 9) != 0)
		    skipNestedAngles(p);
	    }
	    if (p != start) {
		// there is always a blank before the string,
		// which we will include in the final string value
		variable->m_value += TQString::fromLatin1(start-1, (p - start)+1);
		// if this was a pointer, reset that flag since we 
		// now got the value
		variable->m_varKind = VarTree::VKsimple;
	    }
	}

	if (variable->m_value.length() == 0) {
	    TRACE("parse error: no value for " + variable->m_name);
	    return false;
	}

	// final white space
	while (isspace(*p))
	    p++;
	s = p;

	/*
	 * If this was a reference, the value follows. It might even be a
	 * composite variable!
	 */
	if (reference) {
	    goto repeat;
	}
    }

    return true;
}

static bool parseNested(const char*& s, ExprValue* variable)
{
    // could be a structure or an array
    while (isspace(*s))
	s++;

    const char* p = s;
    bool isStruct = false;
    /*
     * If there is a name followed by an = or an < -- which starts a type
     * name -- or "static", it is a structure
     */
    if (*p == '<' || *p == '}') {
	isStruct = true;
    } else if (strncmp(p, "static ", 7) == 0) {
	isStruct = true;
    } else if (isalpha(*p) || *p == '_' || *p == '$') {
	// look ahead for a comma after the name
	skipName(p);
	while (isspace(*p))
	    p++;
	if (*p == '=') {
	    isStruct = true;
	}
	p = s;				/* rescan the name */
    }
    if (isStruct) {
	if (!parseVarSeq(p, variable)) {
	    return false;
	}
	variable->m_varKind = VarTree::VKstruct;
    } else {
	if (!parseValueSeq(p, variable)) {
	    return false;
	}
	variable->m_varKind = VarTree::VKarray;
    }
    s = p;
    return true;
}

static bool parseVarSeq(const char*& s, ExprValue* variable)
{
    // parse a comma-separated sequence of variables
    ExprValue* var = variable;		/* var != 0 to indicate success if empty seq */
    for (;;) {
	if (*s == '}')
	    break;
	if (strncmp(s, "<No data fields>}", 17) == 0)
	{
	    // no member variables, so break out immediately
	    s += 16;			/* go to the closing brace */
	    break;
	}
	var = parseVar(s);
	if (var == 0)
	    break;			/* syntax error */
	variable->appendChild(var);
	if (*s != ',')
	    break;
	// skip the comma and whitespace
	s++;
	while (isspace(*s))
	    s++;
    }
    return var != 0;
}

static bool parseValueSeq(const char*& s, ExprValue* variable)
{
    // parse a comma-separated sequence of variables
    int index = 0;
    bool good;
    for (;;) {
	TQString name;
	name.sprintf("[%d]", index);
	ExprValue* var = new ExprValue(name, VarTree::NKplain);
	good = parseValue(s, var);
	if (!good) {
	    delete var;
	    return false;
	}
	// a value may be followed by "<repeats 45 times>"
	if (strncmp(s, "<repeats ", 9) == 0) {
	    s += 9;
	    char* end;
	    int l = strtol(s, &end, 10);
	    if (end == s || strncmp(end, " times>", 7) != 0) {
		// should not happen
		delete var;
		return false;
	    }
	    TRACE(TQString().sprintf("found <repeats %d times> in array", l));
	    // replace name and advance index
	    name.sprintf("[%d .. %d]", index, index+l-1);
	    var->m_name = name;
	    index += l;
	    // skip " times>" and space
	    s = end+7;
	    // possible final space
	    while (isspace(*s))
		s++;
	} else {
	    index++;
	}
	variable->appendChild(var);
	// long arrays may be terminated by '...'
	if (strncmp(s, "...", 3) == 0) {
	    s += 3;
	    ExprValue* var = new ExprValue("...", VarTree::NKplain);
	    var->m_value = i18n("<additional entries of the array suppressed>");
	    variable->appendChild(var);
	    break;
	}
	if (*s != ',') {
	    break;
	}
	// skip the comma and whitespace
	s++;
	while (isspace(*s))
	    s++;
	// sometimes there is a closing brace after a comma
//	if (*s == '}')
//	    break;
    }
    return true;
}

/**
 * Parses a stack frame.
 */
static void parseFrameInfo(const char*& s, TQString& func,
			   TQString& file, int& lineNo, DbgAddr& address)
{
    const char* p = s;

    // next may be a hexadecimal address
    if (*p == '0') {
	const char* start = p;
	p++;
	if (*p == 'x')
	    p++;
	while (isxdigit(*p))
	    p++;
	address = TQString::fromLatin1(start, p-start);
	if (strncmp(p, " in ", 4) == 0)
	    p += 4;
    } else {
	address = DbgAddr();
    }
    const char* start = p;
    // check for special signal handler frame
    if (strncmp(p, "<signal handler called>", 23) == 0) {
	func = TQString::fromLatin1(start, 23);
	file = TQString();
	lineNo = -1;
	s = p+23;
	if (*s == '\n')
	    s++;
	return;
    }

    /*
     * Skip the function name. It is terminated by a left parenthesis
     * which does not delimit "(anonymous namespace)" and which is
     * outside the angle brackets <> of template parameter lists
     * and is preceded by a space.
     */
    while (*p != '\0')
    {
	if (*p == '<') {
	    // check for operator<< and operator<
	    if (p-start >= 8 && strncmp(p-8, "operator", 8) == 0)
	    {
		p++;
		if (*p == '<')
		    p++;
	    }
	    else
	    {
		// skip template parameter list
		skipNestedAngles(p);
	    }
	} else if (*p == '(') {
	    // this skips "(anonymous namespace)" as well as the formal
	    // parameter list of the containing function if this is a member
	    // of a nested class
	    skipNestedWithString(p, '(', ')');
	} else if (*p == ' ') {
	    ++p;
	    if (*p == '(')
		break; // parameter list found
	} else {
	    p++;
	}
    }

    if (*p == '\0') {
	func = start;
	file = TQString();
	lineNo = -1;
	s = p;
	return;
    }
    /*
     * Skip parameters. But notice that for complicated conversion
     * functions (eg. "operator int(**)()()", ie. convert to pointer to
     * pointer to function) as well as operator()(...) we have to skip
     * additional pairs of parentheses. Furthermore, recent gdbs write the
     * demangled name followed by the arguments in a pair of parentheses,
     * where the demangled name can end in "const".
     */
    do {
	skipNestedWithString(p, '(', ')');
	while (isspace(*p))
	    p++;
	// skip "const"
	if (strncmp(p, "const", 5) == 0) {
	    p += 5;
	    while (isspace(*p))
		p++;
	}
    } while (*p == '(');

    // check for file position
    if (strncmp(p, "at ", 3) == 0) {
	p += 3;
	const char* fileStart = p;
	// go for the end of the line
	while (*p != '\0' && *p != '\n')
	    p++;
	// search back for colon
	const char* colon = p;
	do {
	    --colon;
	} while (*colon != ':');
	file = TQString::fromLatin1(fileStart, colon-fileStart);
	lineNo = atoi(colon+1)-1;
	// skip new-line
	if (*p != '\0')
	    p++;
    } else {
	// check for "from shared lib"
	if (strncmp(p, "from ", 5) == 0) {
	    p += 5;
	    // go for the end of the line
	    while (*p != '\0' && *p != '\n')
		p++;
	    // skip new-line
	    if (*p != '\0')
		p++;
	}
	file = "";
	lineNo = -1;
    }
    // construct the function name (including file info)
    if (*p == '\0') {
	func = start;
    } else {
	func = TQString::fromLatin1(start, p-start-1);	/* don't include \n */
    }
    s = p;

    /*
     * Replace \n (and whitespace around it) in func by a blank. We cannot
     * use TQString::simplifyWhiteSpace() for this because this would also
     * simplify space that belongs to a string arguments that gdb sometimes
     * prints in the argument lists of the function.
     */
    ASSERT(!isspace(func[0].latin1()));		/* there must be non-white before first \n */
    int nl = 0;
    while ((nl = func.find('\n', nl)) >= 0) {
	// search back to the beginning of the whitespace
	int startWhite = nl;
	do {
	    --startWhite;
	} while (isspace(func[startWhite].latin1()));
	startWhite++;
	// search forward to the end of the whitespace
	do {
	    nl++;
	} while (isspace(func[nl].latin1()));
	// replace
	func.replace(startWhite, nl-startWhite, " ");
	/* continue searching for more \n's at this place: */
	nl = startWhite+1;
    }
}


/**
 * Parses a stack frame including its frame number
 */
static bool parseFrame(const char*& s, int& frameNo, TQString& func,
		       TQString& file, int& lineNo, DbgAddr& address)
{
    // Example:
    //  #1  0x8048881 in Dl::Dl (this=0xbffff418, r=3214) at testfile.cpp:72
    //  Breakpoint 3, Cl::f(int) const (this=0xbffff3c0, x=17) at testfile.cpp:155

    // must start with a hash mark followed by number
    // or with "Breakpoint " followed by number and comma
    if (s[0] == '#') {
	if (!isdigit(s[1]))
	    return false;
	s++;				/* skip the hash mark */
    } else if (strncmp(s, "Breakpoint ", 11) == 0) {
	if (!isdigit(s[11]))
	    return false;
	s += 11;			/* skip "Breakpoint" */
    } else
	return false;

    // frame number
    frameNo = atoi(s);
    while (isdigit(*s))
	s++;
    // space and comma
    while (isspace(*s) || *s == ',')
	s++;
    parseFrameInfo(s, func, file, lineNo, address);
    return true;
}

void GdbDriver::parseBackTrace(const char* output, std::list<StackFrame>& stack)
{
    TQString func, file;
    int lineNo, frameNo;
    DbgAddr address;

    while (::parseFrame(output, frameNo, func, file, lineNo, address)) {
	stack.push_back(StackFrame());
	StackFrame* frm = &stack.back();
	frm->frameNo = frameNo;
	frm->fileName = file;
	frm->lineNo = lineNo;
	frm->address = address;
	frm->var = new ExprValue(func, VarTree::NKplain);
    }
}

bool GdbDriver::parseFrameChange(const char* output, int& frameNo,
				 TQString& file, int& lineNo, DbgAddr& address)
{
    TQString func;
    return ::parseFrame(output, frameNo, func, file, lineNo, address);
}


bool GdbDriver::parseBreakList(const char* output, std::list<Breakpoint>& brks)
{
    // skip first line, which is the headline
    const char* p = strchr(output, '\n');
    if (p == 0)
	return false;
    p++;
    if (*p == '\0')
	return false;

    // split up a line
    const char* end;
    char* dummy;
    while (*p != '\0') {
	Breakpoint bp;
	// get Num
	bp.id = strtol(p, &dummy, 10);	/* don't care about overflows */
	p = dummy;
	// get Type
	while (isspace(*p))
	    p++;
	if (strncmp(p, "breakpoint", 10) == 0) {
	    p += 10;
	} else if (strncmp(p, "hw watchpoint", 13) == 0) {
	    bp.type = Breakpoint::watchpoint;
	    p += 13;
	} else if (strncmp(p, "watchpoint", 10) == 0) {
	    bp.type = Breakpoint::watchpoint;
	    p += 10;
	}
	while (isspace(*p))
	    p++;
	if (*p == '\0')
	    break;
	// get Disp
	bp.temporary = *p++ == 'd';
	while (*p != '\0' && !isspace(*p))	/* "keep" or "del" */
	    p++;
	while (isspace(*p))
	    p++;
	if (*p == '\0')
	    break;
	// get Enb
	bp.enabled = *p++ == 'y';
	while (*p != '\0' && !isspace(*p))	/* "y" or "n" */
	    p++;
	while (isspace(*p))
	    p++;
	if (*p == '\0')
	    break;
	// the address, if present
	if (bp.type == Breakpoint::breakpoint &&
	    strncmp(p, "0x", 2) == 0)
	{
	    const char* start = p;
	    while (*p != '\0' && !isspace(*p))
		p++;
	    bp.address = TQString::fromLatin1(start, p-start);
	    while (isspace(*p) && *p != '\n')
		p++;
	    if (*p == '\0')
		break;
	}
	// remainder is location, hit and ignore count, condition
	end = strchr(p, '\n');
	if (end == 0) {
	    bp.location = p;
	    p += bp.location.length();
	} else {
	    bp.location = TQString::fromLatin1(p, end-p).stripWhiteSpace();
	    p = end+1;			/* skip over \n */
	}

	// may be continued in next line
	while (isspace(*p)) {	/* p points to beginning of line */
	    // skip white space at beginning of line
	    while (isspace(*p))
		p++;

	    // seek end of line
	    end = strchr(p, '\n');
	    if (end == 0)
		end = p+strlen(p);

	    if (strncmp(p, "breakpoint already hit", 22) == 0) {
		// extract the hit count
		p += 22;
		bp.hitCount = strtol(p, &dummy, 10);
		TRACE(TQString("hit count %1").arg(bp.hitCount));
	    } else if (strncmp(p, "stop only if ", 13) == 0) {
		// extract condition
		p += 13;
		bp.condition = TQString::fromLatin1(p, end-p).stripWhiteSpace();
		TRACE("condition: "+bp.condition);
	    } else if (strncmp(p, "ignore next ", 12) == 0) {
		// extract ignore count
		p += 12;
		bp.ignoreCount = strtol(p, &dummy, 10);
		TRACE(TQString("ignore count %1").arg(bp.ignoreCount));
	    } else {
		// indeed a continuation
		bp.location += " " + TQString::fromLatin1(p, end-p).stripWhiteSpace();
	    }
	    p = end;
	    if (*p != '\0')
		p++;			/* skip '\n' */
	}
	brks.push_back(bp);
    }
    return true;
}

std::list<ThreadInfo> GdbDriver::parseThreadList(const char* output)
{
    std::list<ThreadInfo> threads;
    if (strcmp(output, "\n") == 0 || strncmp(output, "No stack.", 9) == 0) {
	// no threads
	return threads;
    }

    const char* p = output;
    while (*p != '\0') {
	ThreadInfo thr;
	// seach look for thread id, watching out for  the focus indicator
	thr.hasFocus = false;
	while (isspace(*p))		/* may be \n from prev line: see "No stack" below */
	    p++;
	if (*p == '*') {
	    thr.hasFocus = true;
	    p++;
	    // there follows only whitespace
	}
	const char* end;
	char *temp_end = NULL; /* we need a non-const 'end' for strtol to use...*/
	thr.id = strtol(p, &temp_end, 10);
	end = temp_end;
	if (p == end) {
	    // syntax error: no number found; bail out
	    return threads;
	}
	p = end;

	// skip space
	while (isspace(*p))
	    p++;

	/*
	 * Now follows the thread's SYSTAG. It is terminated by two blanks.
	 */
	end = strstr(p, "  ");
	if (end == 0) {
	    // syntax error; bail out
	    return threads;
	}
	thr.threadName = TQString::fromLatin1(p, end-p);
	p = end+2;

	/*
	 * Now follows a standard stack frame. Sometimes, however, gdb
	 * catches a thread at an instant where it doesn't have a stack.
	 */
	if (strncmp(p, "[No stack.]", 11) != 0) {
	    ::parseFrameInfo(p, thr.function, thr.fileName, thr.lineNo, thr.address);
	} else {
	    thr.function = "[No stack]";
	    thr.lineNo = -1;
	    p += 11;			/* \n is skipped above */
	}

	threads.push_back(thr);
    }
    return threads;
}

static bool parseNewBreakpoint(const char* o, int& id,
			       TQString& file, int& lineNo, TQString& address);
static bool parseNewWatchpoint(const char* o, int& id,
			       TQString& expr);

bool GdbDriver::parseBreakpoint(const char* output, int& id,
				TQString& file, int& lineNo, TQString& address)
{
    const char* o = output;
    // skip lines of that begin with "(Cannot find"
    while (strncmp(o, "(Cannot find", 12) == 0) {
	o = strchr(o, '\n');
	if (o == 0)
	    return false;
	o++;				/* skip newline */
    }

    if (strncmp(o, "Breakpoint ", 11) == 0) {
	output += 11;			/* skip "Breakpoint " */
	return ::parseNewBreakpoint(output, id, file, lineNo, address);
    } else if (strncmp(o, "Hardware watchpoint ", 20) == 0) {
	output += 20;
	return ::parseNewWatchpoint(output, id, address);
    } else if (strncmp(o, "Watchpoint ", 11) == 0) {
	output += 11;
	return ::parseNewWatchpoint(output, id, address);
    }
    return false;
}

static bool parseNewBreakpoint(const char* o, int& id,
			       TQString& file, int& lineNo, TQString& address)
{
    // breakpoint id
    char* p;
    id = strtoul(o, &p, 10);
    if (p == o)
	return false;

    // check for the address
    if (strncmp(p, " at 0x", 6) == 0) {
	char* start = p+4;	       /* skip " at ", but not 0x */
	p += 6;
	while (isxdigit(*p))
	    ++p;
	address = TQString::fromLatin1(start, p-start);
    }
    
    // file name
    char* fileStart = strstr(p, "file ");
    if (fileStart == 0)
	return !address.isEmpty();     /* parse error only if there's no address */
    fileStart += 5;
    
    // line number
    char* numStart = strstr(fileStart, ", line ");
    TQString fileName = TQString::fromLatin1(fileStart, numStart-fileStart);
    numStart += 7;
    int line = strtoul(numStart, &p, 10);
    if (numStart == p)
	return false;

    file = fileName;
    lineNo = line-1;			/* zero-based! */
    return true;
}

static bool parseNewWatchpoint(const char* o, int& id,
			       TQString& expr)
{
    // watchpoint id
    char* p;
    id = strtoul(o, &p, 10);
    if (p == o)
	return false;

    if (strncmp(p, ": ", 2) != 0)
	return false;
    p += 2;

    // all the rest on the line is the expression
    expr = TQString::fromLatin1(p, strlen(p)).stripWhiteSpace();
    return true;
}

void GdbDriver::parseLocals(const char* output, std::list<ExprValue*>& newVars)
{
    // check for possible error conditions
    if (strncmp(output, "No symbol table", 15) == 0)
    {
	return;
    }

    while (*output != '\0') {
	while (isspace(*output))
	    output++;
	if (*output == '\0')
	    break;
	// skip occurrences of "No locals" and "No args"
	if (strncmp(output, "No locals", 9) == 0 ||
	    strncmp(output, "No arguments", 12) == 0)
	{
	    output = strchr(output, '\n');
	    if (output == 0) {
		break;
	    }
	    continue;
	}

	ExprValue* variable = parseVar(output);
	if (variable == 0) {
	    break;
	}
	// do not add duplicates
	for (std::list<ExprValue*>::iterator o = newVars.begin(); o != newVars.end(); ++o) {
	    if ((*o)->m_name == variable->m_name) {
		delete variable;
		goto skipDuplicate;
	    }
	}
	newVars.push_back(variable);
    skipDuplicate:;
    }
}

ExprValue* GdbDriver::parsePrintExpr(const char* output, bool wantErrorValue)
{
    ExprValue* var = 0;
    // check for error conditions
    if (!parseErrorMessage(output, var, wantErrorValue))
    {
	// parse the variable
	var = parseVar(output);
    }
    return var;
}

bool GdbDriver::parseChangeWD(const char* output, TQString& message)
{
    bool isGood = false;
    message = TQString(output).simplifyWhiteSpace();
    if (message.isEmpty()) {
	message = i18n("New working directory: ") + m_programWD;
	isGood = true;
    }
    return isGood;
}

bool GdbDriver::parseChangeExecutable(const char* output, TQString& message)
{
    message = output;

    m_haveCoreFile = false;

    /*
     * Lines starting with the following do not indicate errors:
     *     Using host libthread_db
     *     (no debugging symbols found)
     */
    while (strncmp(output, "Reading symbols from", 20) == 0 ||
	   strncmp(output, "done.", 5) == 0 ||
	   strncmp(output, "Missing separate debuginfo", 26) == 0 ||
	   strncmp(output, "Try: ", 5) == 0 ||
	   strncmp(output, "Using host libthread_db", 23) == 0 ||
	   strncmp(output, "(no debugging symbols found)", 28) == 0)
    {
	// this line is good, go to the next one
	const char* end = strchr(output, '\n');
	if (end == 0)
	    output += strlen(output);
	else
	    output = end+1;
    }

    /*
     * If we've parsed all lines, there was no error.
     */
    return output[0] == '\0';
}

bool GdbDriver::parseCoreFile(const char* output)
{
    // if command succeeded, gdb emits a line starting with "#0 "
    m_haveCoreFile = strstr(output, "\n#0 ") != 0;
    return m_haveCoreFile;
}

uint GdbDriver::parseProgramStopped(const char* output, TQString& message)
{
    // optionally: "program changed, rereading symbols",
    // followed by:
    // "Program exited normally"
    // "Program terminated with wignal SIGSEGV"
    // "Program received signal SIGINT" or other signal
    // "Breakpoint..."
    
    // go through the output, line by line, checking what we have
    const char* start = output - 1;
    uint flags = SFprogramActive;
    message = TQString();
    do {
	start++;			/* skip '\n' */

	if (strncmp(start, "Program ", 8) == 0 ||
	    strncmp(start, "ptrace: ", 8) == 0) {
	    /*
	     * When we receive a signal, the program remains active.
	     *
	     * Special: If we "stopped" in a corefile, the string "Program
	     * terminated with signal"... is displayed. (Normally, we see
	     * "Program received signal"... when a signal happens.)
	     */
	    if (strncmp(start, "Program exited", 14) == 0 ||
		(strncmp(start, "Program terminated", 18) == 0 && !m_haveCoreFile) ||
		strncmp(start, "ptrace: ", 8) == 0)
	    {
		flags &= ~SFprogramActive;
	    }

	    // set message
	    const char* endOfMessage = strchr(start, '\n');
	    if (endOfMessage == 0)
		endOfMessage = start + strlen(start);
	    message = TQString::fromLatin1(start, endOfMessage-start);
	} else if (strncmp(start, "Breakpoint ", 11) == 0) {
	    /*
	     * We stopped at a (permanent) breakpoint (gdb doesn't tell us
	     * that it stopped at a temporary breakpoint).
	     */
	    flags |= SFrefreshBreak;
	} else if (strstr(start, "re-reading symbols.") != 0) {
	    flags |= SFrefreshSource;
	}

	// next line, please
	start = strchr(start, '\n');
    } while (start != 0);

    /*
     * Gdb only notices when new threads have appeared, but not when a
     * thread finishes. So we always have to assume that the list of
     * threads has changed.
     */
    flags |= SFrefreshThreads;

    return flags;
}

TQStringList GdbDriver::parseSharedLibs(const char* output)
{
    TQStringList shlibs;
    if (strncmp(output, "No shared libraries loaded", 26) == 0)
	return shlibs;

    // parse the table of shared libraries

    // strip off head line
    output = strchr(output, '\n');
    if (output == 0)
	return shlibs;
    output++;				/* skip '\n' */
    TQString shlibName;
    while (*output != '\0') {
	// format of a line is
	// 0x404c5000  0x40580d90  Yes         /lib/libc.so.5
	// 3 blocks of non-space followed by space
	for (int i = 0; *output != '\0' && i < 3; i++) {
	    while (*output != '\0' && !isspace(*output)) {	/* non-space */
		output++;
	    }
	    while (isspace(*output)) {	/* space */
		output++;
	    }
	}
	if (*output == '\0')
	    return shlibs;
	const char* start = output;
	output = strchr(output, '\n');
	if (output == 0)
	    output = start + strlen(start);
	shlibName = TQString::fromLatin1(start, output-start);
	if (*output != '\0')
	    output++;
	shlibs.append(shlibName);
	TRACE("found shared lib " + shlibName);
    }
    return shlibs;
}

bool GdbDriver::parseFindType(const char* output, TQString& type)
{
    if (strncmp(output, "type = ", 7) != 0)
	return false;

    /*
     * Everything else is the type. We strip off any leading "const" and any
     * trailing "&" on the grounds that neither affects the decoding of the
     * object. We also strip off all white-space from the type.
     */
    output += 7;
    if (strncmp(output, "const ", 6) == 0)
        output += 6;
    type = output;
    type.replace(TQRegExp("\\s+"), "");
    if (type.endsWith("&"))
        type.truncate(type.length() - 1);
    return true;
}

std::list<RegisterInfo> GdbDriver::parseRegisters(const char* output)
{
    std::list<RegisterInfo> regs;
    if (strncmp(output, "The program has no registers now", 32) == 0) {
	return regs;
    }

    TQString value;

    // parse register values
    while (*output != '\0')
    {
	RegisterInfo reg;
	// skip space at the start of the line
	while (isspace(*output))
	    output++;

	// register name
	const char* start = output;
	while (*output != '\0' && !isspace(*output))
	    output++;
	if (*output == '\0')
	    break;
	reg.regName = TQString::fromLatin1(start, output-start);

	// skip space
	while (isspace(*output))
	    output++;

	/*
	 * If we find a brace now, this is a vector register. We look for
	 * the closing brace and treat the result as cooked value.
	 */
	if (*output == '{')
	{
	    start = output;
	    skipNested(output, '{', '}');
	    value = TQString::fromLatin1(start, output-start).simplifyWhiteSpace();
	    // skip space, but not the end of line
	    while (isspace(*output) && *output != '\n')
		output++;
	    // get rid of the braces at the begining and the end
	    value.remove(0, 1);
	    if (value[value.length()-1] == '}') {
		value = value.left(value.length()-1);
	    }
	    // gdb 5.3 doesn't print a separate set of raw values
	    if (*output == '{') {
		// another set of vector follows
		// what we have so far is the raw value
		reg.rawValue = value;

		start = output;
		skipNested(output, '{', '}');
		value = TQString::fromLatin1(start, output-start).simplifyWhiteSpace();
	    } else {
		// for gdb 5.3
		// find first type that does not have an array, this is the RAW value
		const char* end=start;
		findEnd(end);
		const char* cur=start; 
		while (cur<end) {
		    while (*cur != '=' && cur<end)
			cur++;
		    cur++; 
		    while (isspace(*cur) && cur<end)
			cur++;
		    if (isNumberish(*cur)) {
			end=cur;
			while (*end && (*end!='}') && (*end!=',') && (*end!='\n'))
			    end++;
			TQString rawValue = TQString::fromLatin1(cur, end-cur).simplifyWhiteSpace();
			reg.rawValue = rawValue;

			if (rawValue.left(2)=="0x") {
			    // ok we have a raw value, now get it's type
			    end=cur-1;
			    while (isspace(*end) || *end=='=') end--;
			    end++;
			    cur=end-1;
			    while (*cur!='{' && *cur!=' ')
				cur--;
			    cur++;
			    reg.type = TQString::fromLatin1(cur, end-cur);
			}

			// end while loop 
			cur=end;
		    }
		}
		// skip to the end of line
		while (*output != '\0' && *output != '\n')
		    output++;
		// get rid of the braces at the begining and the end
		value.remove(0, 1);
		if (value[value.length()-1] == '}') {
		    value.truncate(value.length()-1);
		}
	    }
	    reg.cookedValue = value;
	}
	else
	{
	    // the rest of the line is the register value
	    start = output;
	    output = strchr(output,'\n');
	    if (output == 0)
		output = start + strlen(start);
	    value = TQString::fromLatin1(start, output-start).simplifyWhiteSpace();

	    /*
	     * We split the raw from the cooked values.
	     * Some modern gdbs explicitly say: "0.1234 (raw 0x3e4567...)".
	     * Here, the cooked value comes first, and the raw value is in
	     * the second part.
	     */
	    int pos = value.find(" (raw ");
	    if (pos >= 0)
	    {
		reg.cookedValue = value.left(pos);
		reg.rawValue = value.mid(pos+6);
		if (reg.rawValue.right(1) == ")")	// remove closing bracket
		    reg.rawValue.truncate(reg.rawValue.length()-1);
	    }
	    else
	    {
		/*
		* In other cases we split off the first token (separated by
		* whitespace). It is the raw value. The remainder of the line
		* is the cooked value.
		*/
		int pos = value.find(' ');
		if (pos < 0) {
		    reg.rawValue = value;
		    reg.cookedValue = TQString();
		} else {
		    reg.rawValue = value.left(pos);
		    reg.cookedValue = value.mid(pos+1);
		}
	    }
	}
	if (*output != '\0')
	    output++;			/* skip '\n' */

	regs.push_back(reg);
    }
    return regs;
}

bool GdbDriver::parseInfoLine(const char* output, TQString& addrFrom, TQString& addrTo)
{
    // "is at address" or "starts at address"
    const char* start = strstr(output, "s at address ");
    if (start == 0)
	return false;

    start += 13;
    const char* p = start;
    while (*p != '\0' && !isspace(*p))
	p++;
    addrFrom = TQString::fromLatin1(start, p-start);

    start = strstr(p, "and ends at ");
    if (start == 0) {
	addrTo = addrFrom;
	return true;
    }

    start += 12;
    p = start;
    while (*p != '\0' && !isspace(*p))
	p++;
    addrTo = TQString::fromLatin1(start, p-start);

    return true;
}

std::list<DisassembledCode> GdbDriver::parseDisassemble(const char* output)
{
    std::list<DisassembledCode> code;

    if (strncmp(output, "Dump of assembler", 17) != 0) {
	// error message?
	DisassembledCode c;
	c.code = output;
	code.push_back(c);
	return code;
    }

    // remove first line
    const char* p = strchr(output, '\n');
    if (p == 0)
	return code;			/* not a regular output */

    p++;

    // remove last line
    const char* end = strstr(output, "End of assembler");
    if (end == 0)
	end = p + strlen(p);

    // remove function offsets from the lines
    while (p != end)
    {
	DisassembledCode c;
	const char* start = p;
	// address
	while (p != end && !isspace(*p))
	    p++;
	c.address = TQString::fromLatin1(start, p-start);

	// function name (enclosed in '<>', followed by ':')
	while (p != end && *p != '<')
	    p++;
	if (*p == '<')
	    skipNestedAngles(p);
	if (*p == ':')
	    p++;

	// space until code
	while (p != end && isspace(*p))
	    p++;

	// code until end of line
	start = p;
	while (p != end && *p != '\n')
	    p++;
	if (p != end)			/* include '\n' */
	    p++;

	c.code = TQString::fromLatin1(start, p-start);
	code.push_back(c);
    }
    return code;
}

TQString GdbDriver::parseMemoryDump(const char* output, std::list<MemoryDump>& memdump)
{
    if (isErrorExpr(output)) {
	// error; strip space
	TQString msg = output;
	return msg.stripWhiteSpace();
    }

    const char* p = output;		/* save typing */

    // the address
    while (*p != 0) {
	MemoryDump md;

	const char* start = p;
	while (*p != '\0' && *p != ':' && !isspace(*p))
	    p++;
	md.address = TQString::fromLatin1(start, p-start);
	if (*p != ':') {
	    // parse function offset
	    while (isspace(*p))
		p++;
	    start = p;
	    while (*p != '\0' && !(*p == ':' && isspace(p[1])))
		p++;
	    md.address.fnoffs = TQString::fromLatin1(start, p-start);
	}
	if (*p == ':')
	    p++;
	// skip space; this may skip a new-line char!
	while (isspace(*p))
	    p++;
	// everything to the end of the line is the memory dump
	const char* end = strchr(p, '\n');
	if (end != 0) {
	    md.dump = TQString::fromLatin1(p, end-p);
	    p = end+1;
	} else {
	    md.dump = TQString::fromLatin1(p, strlen(p));
	    p += strlen(p);
	}
	memdump.push_back(md);
    }
    
    return TQString();
}

TQString GdbDriver::editableValue(VarTree* value)
{
    const char* s = value->value().latin1();

    // if the variable is a pointer value that contains a cast,
    // remove the cast
    if (*s == '(') {
	skipNested(s, '(', ')');
	// skip space
	while (isspace(*s))
	    ++s;
    }

repeat:
    const char* start = s;

    if (strncmp(s, "0x", 2) == 0)
    {
	s += 2;
	while (isxdigit(*s))
	    ++s;

	/*
	 * What we saw so far might have been a reference. If so, edit the
	 * referenced value. Otherwise, edit the pointer.
	 */
	if (*s == ':') {
	    // a reference
	    ++s;
	    goto repeat;
	}
	// a pointer
	// if it's a pointer to a string, remove the string
	const char* end = s;
	while (isspace(*s))
	    ++s;
	if (*s == '"') {
	    // a string
	    return TQString::fromLatin1(start, end-start);
	} else {
	    // other pointer
	    return TQString::fromLatin1(start, strlen(start));
	}
    }

    // else leave it unchanged (or stripped of the reference preamble)
    return s;
}

TQString GdbDriver::parseSetVariable(const char* output)
{
    // if there is any output, it is an error message
    TQString msg = output;
    return msg.stripWhiteSpace();
}


#include "gdbdriver.moc"