summaryrefslogtreecommitdiffstats
path: root/kdbg/debugger.cpp
blob: 4d8680352bf758746df5f0db0e9895e8f556b190 (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
/*
 * 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 "debugger.h"
#include "dbgdriver.h"
#include "pgmargs.h"
#include "typetable.h"
#include "exprwnd.h"
#include "pgmsettings.h"
#include "programconfig.h"
#include <tqregexp.h>
#include <tqfileinfo.h>
#include <tqlistbox.h>
#include <tqstringlist.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdelocale.h>			/* i18n */
#include <tdemessagebox.h>
#include <ctype.h>
#include <stdlib.h>			/* strtol, atoi */
#ifdef HAVE_UNISTD_H
#include <unistd.h>			/* sleep(3) */
#endif
#include "mydebug.h"


KDebugger::KDebugger(TQWidget* parent,
		     ExprWnd* localVars,
		     ExprWnd* watchVars,
		     TQListBox* backtrace) :
	TQObject(parent, "debugger"),
	m_ttyLevel(ttyFull),
	m_memoryFormat(MDTword | MDThex),
	m_haveExecutable(false),
	m_programActive(false),
	m_programRunning(false),
	m_sharedLibsListed(false),
	m_typeTable(0),
	m_programConfig(0),
	m_d(0),
	m_localVariables(*localVars),
	m_watchVariables(*watchVars),
	m_btWindow(*backtrace)
{
    m_envVars.setAutoDelete(true);

    connect(&m_localVariables, SIGNAL(expanded(TQListViewItem*)),
	    SLOT(slotExpanding(TQListViewItem*)));
    connect(&m_watchVariables, SIGNAL(expanded(TQListViewItem*)),
	    SLOT(slotExpanding(TQListViewItem*)));
    connect(&m_localVariables, SIGNAL(editValueCommitted(VarTree*, const TQString&)),
	    SLOT(slotValueEdited(VarTree*, const TQString&)));
    connect(&m_watchVariables, SIGNAL(editValueCommitted(VarTree*, const TQString&)),
	    SLOT(slotValueEdited(VarTree*, const TQString&)));

    connect(&m_btWindow, SIGNAL(highlighted(int)), SLOT(gotoFrame(int)));

    emit updateUI();
}

KDebugger::~KDebugger()
{
    if (m_programConfig != 0) {
	saveProgramSettings();
	m_programConfig->sync();
	delete m_programConfig;
    }

    delete m_typeTable;
}


void KDebugger::saveSettings(TDEConfig* /*config*/)
{
}

void KDebugger::restoreSettings(TDEConfig* /*config*/)
{
}


//////////////////////////////////////////////////////////////////////
// external interface

const char GeneralGroup[] = "General";
const char DebuggerCmdStr[] = "DebuggerCmdStr";
const char TTYLevelEntry[] = "TTYLevel";
const char KDebugger::DriverNameEntry[] = "DriverName";

bool KDebugger::debugProgram(const TQString& name,
			     DebuggerDriver* driver)
{
    if (m_d != 0 && m_d->isRunning())
    {
	TQApplication::setOverrideCursor(waitCursor);

	stopDriver();

	TQApplication::restoreOverrideCursor();

	if (m_d->isRunning() || m_haveExecutable) {
	    /* timed out! We can't really do anything useful now */
	    TRACE("timed out while waiting for gdb to die!");
	    return false;
	}
	delete m_d;
	m_d = 0;
    }

    // wire up the driver
    connect(driver, SIGNAL(activateFileLine(const TQString&,int,const DbgAddr&)),
	    this, SIGNAL(activateFileLine(const TQString&,int,const DbgAddr&)));
    connect(driver, SIGNAL(processExited(TDEProcess*)), SLOT(gdbExited(TDEProcess*)));
    connect(driver, SIGNAL(commandReceived(CmdQueueItem*,const char*)),
	    SLOT(parse(CmdQueueItem*,const char*)));
    connect(driver, SIGNAL(wroteStdin(TDEProcess*)), SIGNAL(updateUI()));
    connect(driver, SIGNAL(inferiorRunning()), SLOT(slotInferiorRunning()));
    connect(driver, SIGNAL(enterIdleState()), SLOT(backgroundUpdate()));
    connect(driver, SIGNAL(enterIdleState()), SIGNAL(updateUI()));
    connect(&m_localVariables, SIGNAL(removingItem(VarTree*)),
	    driver, SLOT(dequeueCmdByVar(VarTree*)));
    connect(&m_watchVariables, SIGNAL(removingItem(VarTree*)),
	    driver, SLOT(dequeueCmdByVar(VarTree*)));

    // create the program settings object
    openProgramConfig(name);

    // get debugger command from per-program settings
    if (m_programConfig != 0) {
	m_programConfig->setGroup(GeneralGroup);
	m_debuggerCmd = readDebuggerCmd();
	// get terminal emulation level
	m_ttyLevel = TTYLevel(m_programConfig->readNumEntry(TTYLevelEntry, ttyFull));
    }
    // the rest is read in later in the handler of DCexecutable

    m_d = driver;

    if (!startDriver()) {
	TRACE("startDriver failed");
	m_d = 0;
	return false;
    }

    TRACE("before file cmd");
    m_d->executeCmd(DCexecutable, name);
    m_executable = name;

    // set remote target
    if (!m_remoteDevice.isEmpty()) {
	m_d->executeCmd(DCtargetremote, m_remoteDevice);
	m_d->queueCmd(DCbt, DebuggerDriver::TQMoverride);
	m_d->queueCmd(DCframe, 0, DebuggerDriver::TQMnormal);
	m_programActive = true;
	m_haveExecutable = true;
    }

    // create a type table
    m_typeTable = new ProgramTypeTable;
    m_sharedLibsListed = false;

    emit updateUI();

    return true;
}

void KDebugger::shutdown()
{
    // shut down debugger driver
    if (m_d != 0 && m_d->isRunning())
    {
	stopDriver();
    }
}

void KDebugger::useCoreFile(TQString corefile, bool batch)
{
    m_corefile = corefile;
    if (!batch) {
	CmdQueueItem* cmd = loadCoreFile();
	cmd->m_byUser = true;
    }
}

void KDebugger::setAttachPid(const TQString& pid)
{
    m_attachedPid = pid;
}

void KDebugger::programRun()
{
    if (!isReady())
	return;

    // when program is active, but not a core file, continue
    // otherwise run the program
    if (m_programActive && m_corefile.isEmpty()) {
	// gdb command: continue
	m_d->executeCmd(DCcont, true);
    } else {
	// gdb command: run
	m_d->executeCmd(DCrun, true);
	m_corefile = TQString();
	m_programActive = true;
    }
    m_programRunning = true;
}

void KDebugger::attachProgram(const TQString& pid)
{
    if (!isReady())
	return;

    m_attachedPid = pid;
    TRACE("Attaching to " + m_attachedPid);
    m_d->executeCmd(DCattach, m_attachedPid);
    m_programActive = true;
    m_programRunning = true;
}

void KDebugger::programRunAgain()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCrun, true);
	m_corefile = TQString();
	m_programRunning = true;
    }
}

void KDebugger::programStep()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCstep, true);
	m_programRunning = true;
    }
}

void KDebugger::programNext()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCnext, true);
	m_programRunning = true;
    }
}

void KDebugger::programStepi()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCstepi, true);
	m_programRunning = true;
    }
}

void KDebugger::programNexti()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCnexti, true);
	m_programRunning = true;
    }
}

void KDebugger::programFinish()
{
    if (canSingleStep()) {
	m_d->executeCmd(DCfinish, true);
	m_programRunning = true;
    }
}

void KDebugger::programKill()
{
    if (haveExecutable() && isProgramActive()) {
	if (m_programRunning) {
	    m_d->interruptInferior();
	}
	// this is an emergency command; flush queues
	m_d->flushCommands(true);
	m_d->executeCmd(DCkill, true);
    }
}

bool KDebugger::runUntil(const TQString& fileName, int lineNo)
{
    if (isReady() && m_programActive && !m_programRunning) {
	// strip off directory part of file name
	TQString file = fileName;
	int offset = file.findRev("/");
	if (offset >= 0) {
	    file.remove(0, offset+1);
	}
	m_d->executeCmd(DCuntil, file, lineNo, true);
	m_programRunning = true;
	return true;
    } else {
	return false;
    }
}

void KDebugger::programBreak()
{
    if (m_haveExecutable && m_programRunning) {
	m_d->interruptInferior();
    }
}

void KDebugger::programArgs(TQWidget* parent)
{
    if (m_haveExecutable) {
	TQStringList allOptions = m_d->boolOptionList();
	PgmArgs dlg(parent, m_executable, m_envVars, allOptions);
	dlg.setArgs(m_programArgs);
	dlg.setWd(m_programWD);
	dlg.setOptions(m_boolOptions);
	if (dlg.exec()) {
	    updateProgEnvironment(dlg.args(), dlg.wd(),
				  dlg.envVars(), dlg.options());
	}
    }
}

void KDebugger::programSettings(TQWidget* parent)
{
    if (!m_haveExecutable)
	return;

    ProgramSettings dlg(parent, m_executable);

    dlg.m_chooseDriver.setDebuggerCmd(m_debuggerCmd);
    dlg.m_output.setTTYLevel(m_ttyLevel);

    if (dlg.exec() == TQDialog::Accepted)
    {
	m_debuggerCmd = dlg.m_chooseDriver.debuggerCmd();
	m_ttyLevel = TTYLevel(dlg.m_output.ttyLevel());
    }
}

bool KDebugger::setBreakpoint(TQString file, int lineNo,
			      const DbgAddr& address, bool temporary)
{
    if (!isReady()) {
	return false;
    }

    BrkptIterator bp = breakpointByFilePos(file, lineNo, address);
    if (bp == m_brkpts.end())
    {
	/*
	 * No such breakpoint, so set a new one. If we have an address, we
	 * set the breakpoint exactly there. Otherwise we use the file name
	 * plus line no.
	 */
	Breakpoint* bp = new Breakpoint;
	bp->temporary = temporary;

	if (address.isEmpty())
	{
	    bp->fileName = file;
	    bp->lineNo = lineNo;
	}
	else
	{
	    bp->address = address;
	}
	setBreakpoint(bp, false);
    }
    else
    {
	/*
	 * If the breakpoint is disabled, enable it; if it's enabled,
	 * delete that breakpoint.
	 */
	if (bp->enabled) {
	    deleteBreakpoint(bp);
	} else {
	    enableDisableBreakpoint(bp);
	}
    }
    return true;
}

void KDebugger::setBreakpoint(Breakpoint* bp, bool queueOnly)
{
    CmdQueueItem* cmd = executeBreakpoint(bp, queueOnly);
    cmd->m_brkpt = bp;	// used in newBreakpoint()
}

CmdQueueItem* KDebugger::executeBreakpoint(const Breakpoint* bp, bool queueOnly)
{
    CmdQueueItem* cmd;
    if (!bp->text.isEmpty())
    {
	/*
	 * The breakpoint was set using the text box in the breakpoint
	 * list. This is the only way in which watchpoints are set.
	 */
	if (bp->type == Breakpoint::watchpoint) {
	    cmd = m_d->executeCmd(DCwatchpoint, bp->text);
	} else {
	    cmd = m_d->executeCmd(DCbreaktext, bp->text);
	}
    }
    else if (bp->address.isEmpty())
    {
	// strip off directory part of file name
	TQString file = bp->fileName;
	int offset = file.findRev("/");
	if (offset >= 0) {
	    file.remove(0, offset+1);
	}
	if (queueOnly) {
	    cmd = m_d->queueCmd(bp->temporary ? DCtbreakline : DCbreakline,
				file, bp->lineNo, DebuggerDriver::TQMoverride);
	} else {
	    cmd = m_d->executeCmd(bp->temporary ? DCtbreakline : DCbreakline,
				  file, bp->lineNo);
	}
    }
    else
    {
	if (queueOnly) {
	    cmd = m_d->queueCmd(bp->temporary ? DCtbreakaddr : DCbreakaddr,
				bp->address.asString(), DebuggerDriver::TQMoverride);
	} else {
	    cmd = m_d->executeCmd(bp->temporary ? DCtbreakaddr : DCbreakaddr,
				  bp->address.asString());
	}
    }
    return cmd;
}

bool KDebugger::enableDisableBreakpoint(TQString file, int lineNo,
					const DbgAddr& address)
{
    BrkptIterator bp = breakpointByFilePos(file, lineNo, address);
    return enableDisableBreakpoint(bp);
}

bool KDebugger::enableDisableBreakpoint(BrkptIterator bp)
{
    if (bp == m_brkpts.end())
	return false;

    /*
     * Toggle enabled/disabled state.
     * 
     * The driver is not bothered if we are modifying an orphaned
     * breakpoint.
     */
    if (!bp->isOrphaned()) {
	if (!canChangeBreakpoints()) {
	    return false;
	}
	m_d->executeCmd(bp->enabled ? DCdisable : DCenable, bp->id);
    } else {
	bp->enabled = !bp->enabled;
	emit breakpointsChanged();
    }
    return true;
}

bool KDebugger::conditionalBreakpoint(BrkptIterator bp,
				      const TQString& condition,
				      int ignoreCount)
{
    if (bp == m_brkpts.end())
	return false;

    /*
     * Change the condition and ignore count.
     *
     * The driver is not bothered if we are removing an orphaned
     * breakpoint.
     */

    if (!bp->isOrphaned()) {
	if (!canChangeBreakpoints()) {
	    return false;
	}

	bool changed = false;

	if (bp->condition != condition) {
	    // change condition
	    m_d->executeCmd(DCcondition, condition, bp->id);
	    changed = true;
	}
	if (bp->ignoreCount != ignoreCount) {
	    // change ignore count
	    m_d->executeCmd(DCignore, bp->id, ignoreCount);
	    changed = true;
	}
	if (changed) {
	    // get the changes
	    m_d->queueCmd(DCinfobreak, DebuggerDriver::TQMoverride);
	}
    } else {
	bp->condition = condition;
	bp->ignoreCount = ignoreCount;
	emit breakpointsChanged();
    }
    return true;
}

bool KDebugger::deleteBreakpoint(BrkptIterator bp)
{
    if (bp == m_brkpts.end())
	return false;

    /*
     * Remove the breakpoint.
     *
     * The driver is not bothered if we are removing an orphaned
     * breakpoint.
     */
    if (!bp->isOrphaned()) {
	if (!canChangeBreakpoints()) {
	    return false;
	}
	m_d->executeCmd(DCdelete, bp->id);
    } else {
	m_brkpts.erase(bp);
	emit breakpointsChanged();
    }
    return false;
}

bool KDebugger::canSingleStep()
{
    return isReady() && m_programActive && !m_programRunning;
}

bool KDebugger::canChangeBreakpoints()
{
    return isReady() && !m_programRunning;
}

bool KDebugger::canStart()
{
    return isReady() && !m_programActive;
}

bool KDebugger::isReady() const 
{
    return m_haveExecutable &&
	m_d != 0 && m_d->canExecuteImmediately();
}

bool KDebugger::isIdle() const
{
    return m_d == 0 || m_d->isIdle();
}


//////////////////////////////////////////////////////////
// debugger driver

bool KDebugger::startDriver()
{
    emit debuggerStarting();		/* must set m_inferiorTerminal */

    /*
     * If the per-program command string is empty, use the global setting
     * (which might also be empty, in which case the driver uses its
     * default).
     */
    m_explicitKill = false;
    if (!m_d->startup(m_debuggerCmd)) {
	return false;
    }

    /*
     * If we have an output terminal, we use it. Otherwise we will run the
     * program with input and output redirected to /dev/null. Other
     * redirections are also necessary depending on the tty emulation
     * level.
     */
    int redirect = RDNstdin|RDNstdout|RDNstderr;	/* redirect everything */
    if (!m_inferiorTerminal.isEmpty()) {
	switch (m_ttyLevel) {
	default:
	case ttyNone:
	    // redirect everything
	    break;
	case ttySimpleOutputOnly:
	    redirect = RDNstdin;
	    break;
	case ttyFull:
	    redirect = 0;
	    break;
	}
    }
    m_d->executeCmd(DCtty, m_inferiorTerminal, redirect);

    return true;
}

void KDebugger::stopDriver()
{
    m_explicitKill = true;

    if (m_attachedPid.isEmpty()) {
	m_d->terminate();
    } else {
	m_d->detachAndTerminate();
    }

    /*
     * We MUST wait until the slot gdbExited() has been called. But to
     * avoid a deadlock, we wait only for some certain maximum time. Should
     * this timeout be reached, the only reasonable thing one could do then
     * is exiting kdbg.
     */
    kapp->processEvents(1000);		/* ideally, this will already shut it down */
    int maxTime = 20;			/* about 20 seconds */
    while (m_haveExecutable && maxTime > 0) {
	// give gdb time to die (and send a SIGCLD)
	::sleep(1);
	--maxTime;
	kapp->processEvents(1000);
    }
}

void KDebugger::gdbExited(TDEProcess*)
{
    /*
     * Save settings, but only if gdb has already processed "info line
     * main", otherwise we would save an empty config file, because it
     * isn't read in until then!
     */
    if (m_programConfig != 0) {
	if (m_haveExecutable) {
	    saveProgramSettings();
	    m_programConfig->sync();
	}
	delete m_programConfig;
	m_programConfig = 0;
    }

    // erase types
    delete m_typeTable;
    m_typeTable = 0;

    if (m_explicitKill) {
	TRACE(m_d->driverName() + " exited normally");
    } else {
	TQString msg = i18n("%1 exited unexpectedly.\n"
			   "Restart the session (e.g. with File|Executable).");
	KMessageBox::error(parentWidget(), msg.arg(m_d->driverName()));
    }

    // reset state
    m_haveExecutable = false;
    m_executable = "";
    m_programActive = false;
    m_programRunning = false;
    m_explicitKill = false;
    m_debuggerCmd = TQString();		/* use global setting at next start! */
    m_attachedPid = TQString();		/* we are no longer attached to a process */
    m_ttyLevel = ttyFull;
    m_brkpts.clear();

    // erase PC
    emit updatePC(TQString(), -1, DbgAddr(), 0);
}

TQString KDebugger::getConfigForExe(const TQString& name)
{
    TQFileInfo fi(name);
    TQString pgmConfigFile = fi.dirPath(true);
    if (!pgmConfigFile.isEmpty()) {
	pgmConfigFile += '/';
    }
    pgmConfigFile += ".kdbgrc." + fi.fileName();
    TRACE("program config file = " + pgmConfigFile);
    return pgmConfigFile;
}

void KDebugger::openProgramConfig(const TQString& name)
{
    ASSERT(m_programConfig == 0);

    TQString pgmConfigFile = getConfigForExe(name);

    m_programConfig = new ProgramConfig(pgmConfigFile);
}

const char EnvironmentGroup[] = "Environment";
const char WatchGroup[] = "Watches";
const char FileVersion[] = "FileVersion";
const char ProgramArgs[] = "ProgramArgs";
const char WorkingDirectory[] = "WorkingDirectory";
const char OptionsSelected[] = "OptionsSelected";
const char Variable[] = "Var%d";
const char Value[] = "Value%d";
const char ExprFmt[] = "Expr%d";

void KDebugger::saveProgramSettings()
{
    ASSERT(m_programConfig != 0);
    m_programConfig->setGroup(GeneralGroup);
    m_programConfig->writeEntry(FileVersion, 1);
    m_programConfig->writeEntry(ProgramArgs, m_programArgs);
    m_programConfig->writeEntry(WorkingDirectory, m_programWD);
    m_programConfig->writeEntry(OptionsSelected, m_boolOptions);
    m_programConfig->writeEntry(DebuggerCmdStr, m_debuggerCmd);
    m_programConfig->writeEntry(TTYLevelEntry, int(m_ttyLevel));
    TQString driverName;
    if (m_d != 0)
	driverName = m_d->driverName();
    m_programConfig->writeEntry(DriverNameEntry, driverName);

    // write environment variables
    m_programConfig->deleteGroup(EnvironmentGroup);
    m_programConfig->setGroup(EnvironmentGroup);
    TQDictIterator<EnvVar> it = m_envVars;
    EnvVar* var;
    TQString varName;
    TQString varValue;
    for (int i = 0; (var = it) != 0; ++it, ++i) {
	varName.sprintf(Variable, i);
	varValue.sprintf(Value, i);
	m_programConfig->writeEntry(varName, it.currentKey());
	m_programConfig->writeEntry(varValue, var->value);
    }

    saveBreakpoints(m_programConfig);

    // watch expressions
    // first get rid of whatever was in this group
    m_programConfig->deleteGroup(WatchGroup);
    // then start a new group
    m_programConfig->setGroup(WatchGroup);
    VarTree* item = m_watchVariables.firstChild();
    int watchNum = 0;
    for (; item != 0; item = item->nextSibling(), ++watchNum) {
	varName.sprintf(ExprFmt, watchNum);
	m_programConfig->writeEntry(varName, item->getText());
    }

    // give others a chance
    emit saveProgramSpecific(m_programConfig);
}

void KDebugger::overrideProgramArguments(const TQString& args)
{
    ASSERT(m_programConfig != 0);
    m_programConfig->setGroup(GeneralGroup);
    m_programConfig->writeEntry(ProgramArgs, args);
}

void KDebugger::restoreProgramSettings()
{
    ASSERT(m_programConfig != 0);
    m_programConfig->setGroup(GeneralGroup);
    /*
     * We ignore file version for now we will use it in the future to
     * distinguish different versions of this configuration file.
     */
    // m_debuggerCmd has been read in already
    // m_ttyLevel has been read in already
    TQString pgmArgs = m_programConfig->readEntry(ProgramArgs);
    TQString pgmWd = m_programConfig->readEntry(WorkingDirectory);
    TQStringList boolOptions = m_programConfig->readListEntry(OptionsSelected);
    m_boolOptions = TQStringList();

    // read environment variables
    m_programConfig->setGroup(EnvironmentGroup);
    m_envVars.clear();
    TQDict<EnvVar> pgmVars;
    EnvVar* var;
    TQString varName;
    TQString varValue;
    for (int i = 0;; ++i) {
	varName.sprintf(Variable, i);
	varValue.sprintf(Value, i);
	if (!m_programConfig->hasKey(varName)) {
	    /* entry not present, assume that we've hit them all */
	    break;
	}
	TQString name = m_programConfig->readEntry(varName);
	if (name.isEmpty()) {
	    // skip empty names
	    continue;
	}
	var = new EnvVar;
	var->value = m_programConfig->readEntry(varValue);
	var->status = EnvVar::EVnew;
	pgmVars.insert(name, var);
    }

    updateProgEnvironment(pgmArgs, pgmWd, pgmVars, boolOptions);

    restoreBreakpoints(m_programConfig);

    // watch expressions
    m_programConfig->setGroup(WatchGroup);
    m_watchVariables.clear();
    for (int i = 0;; ++i) {
	varName.sprintf(ExprFmt, i);
	if (!m_programConfig->hasKey(varName)) {
	    /* entry not present, assume that we've hit them all */
	    break;
	}
	TQString expr = m_programConfig->readEntry(varName);
	if (expr.isEmpty()) {
	    // skip empty expressions
	    continue;
	}
	addWatch(expr);
    }

    // give others a chance
    emit restoreProgramSpecific(m_programConfig);
}

/**
 * Reads the debugger command line from the program settings. The config
 * group must have been set by the caller.
 */
TQString KDebugger::readDebuggerCmd()
{
    TQString debuggerCmd = m_programConfig->readEntry(DebuggerCmdStr);

    // always let the user confirm the debugger cmd if we are root
    if (::geteuid() == 0)
    {
	if (!debuggerCmd.isEmpty()) {
	    TQString msg = i18n(
		"The settings for this program specify "
		"the following debugger command:\n%1\n"
		"Shall this command be used?");
	    if (KMessageBox::warningYesNo(parentWidget(), msg.arg(debuggerCmd))
		!= KMessageBox::Yes)
	    {
		// don't use it
		debuggerCmd = TQString();
	    }
	}
    }
    return debuggerCmd;
}

/*
 * Breakpoints are saved one per group.
 */
const char BPGroup[] = "Breakpoint %d";
const char File[] = "File";
const char Line[] = "Line";
const char Text[] = "Text";
const char Address[] = "Address";
const char Temporary[] = "Temporary";
const char Enabled[] = "Enabled";
const char Condition[] = "Condition";

void KDebugger::saveBreakpoints(ProgramConfig* config)
{
    TQString groupName;
    int i = 0;
    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->type == Breakpoint::watchpoint)
	    continue;			/* don't save watchpoints */
	groupName.sprintf(BPGroup, i++);

	/* remove remmants */
	config->deleteGroup(groupName);

	config->setGroup(groupName);
	if (!bp->text.isEmpty()) {
	    /*
	     * The breakpoint was set using the text box in the breakpoint
	     * list. We do not save the location by filename+line number,
	     * but instead honor what the user typed (a function name, for
	     * example, which could move between sessions).
	     */
	    config->writeEntry(Text, bp->text);
	} else if (!bp->fileName.isEmpty()) {
	    config->writeEntry(File, bp->fileName);
	    config->writeEntry(Line, bp->lineNo);
	    /*
	     * Addresses are hardly correct across sessions, so we don't
	     * save it.
	     */
	} else {
	    config->writeEntry(Address, bp->address.asString());
	}
	config->writeEntry(Temporary, bp->temporary);
	config->writeEntry(Enabled, bp->enabled);
	if (!bp->condition.isEmpty())
	    config->writeEntry(Condition, bp->condition);
	// we do not save the ignore count
    }
    // delete remaining groups
    // we recognize that a group is present if there is an Enabled entry
    for (;; i++) {
	groupName.sprintf(BPGroup, i);
	config->setGroup(groupName);
	if (!config->hasKey(Enabled)) {
	    /* group not present, assume that we've hit them all */
	    break;
	}
	config->deleteGroup(groupName);
    }
}

void KDebugger::restoreBreakpoints(ProgramConfig* config)
{
    TQString groupName;
    /*
     * We recognize the end of the list if there is no Enabled entry
     * present.
     */
    for (int i = 0;; i++) {
	groupName.sprintf(BPGroup, i);
	config->setGroup(groupName);
	if (!config->hasKey(Enabled)) {
	    /* group not present, assume that we've hit them all */
	    break;
	}
	Breakpoint* bp = new Breakpoint;
	bp->fileName = config->readEntry(File);
	bp->lineNo = config->readNumEntry(Line, -1);
	bp->text = config->readEntry(Text);
	bp->address = config->readEntry(Address);
	// check consistency
	if ((bp->fileName.isEmpty() || bp->lineNo < 0) &&
	    bp->text.isEmpty() &&
	    bp->address.isEmpty())
	{
	    delete bp;
	    continue;
	}
	bp->enabled = config->readBoolEntry(Enabled, true);
	bp->temporary = config->readBoolEntry(Temporary, false);
	bp->condition = config->readEntry(Condition);

	/*
	 * Add the breakpoint.
	 */
	setBreakpoint(bp, false);
	// the new breakpoint is disabled or conditionalized later
	// in newBreakpoint()
    }
    m_d->queueCmd(DCinfobreak, DebuggerDriver::TQMoverride);
}


// parse output of command cmd
void KDebugger::parse(CmdQueueItem* cmd, const char* output)
{
    ASSERT(cmd != 0);			/* queue mustn't be empty */

    TRACE(TQString(__PRETTY_FUNCTION__) + " parsing " + output);

    switch (cmd->m_cmd) {
    case DCtargetremote:
	// the output (if any) is uninteresting
    case DCsetargs:
    case DCtty:
	// there is no output
    case DCsetenv:
    case DCunsetenv:
    case DCsetoption:
	/* if value is empty, we see output, but we don't care */
	break;
    case DCcd:
	/* display gdb's message in the status bar */
	m_d->parseChangeWD(output, m_statusMessage);
	emit updateStatusMessage();
	break;
    case DCinitialize:
	break;
    case DCexecutable:
	if (m_d->parseChangeExecutable(output, m_statusMessage))
	{
	    // success; restore breakpoints etc.
	    if (m_programConfig != 0) {
		restoreProgramSettings();
	    }
	    // load file containing main() or core file
	    if (!m_corefile.isEmpty())
	    {
		// load core file
		loadCoreFile();
	    }
	    else if (!m_attachedPid.isEmpty())
	    {
		m_d->queueCmd(DCattach, m_attachedPid, DebuggerDriver::TQMoverride);
		m_programActive = true;
		m_programRunning = true;
	    }
	    else if (!m_remoteDevice.isEmpty())
	    {
		// handled elsewhere
	    }
	    else
	    {
		m_d->queueCmd(DCinfolinemain, DebuggerDriver::TQMnormal);
	    }
	    if (!m_statusMessage.isEmpty())
		emit updateStatusMessage();
	} else {
	    TQString msg = m_d->driverName() + ": " + m_statusMessage;
	    KMessageBox::sorry(parentWidget(), msg);
	    m_executable = "";
	    m_corefile = "";		/* don't process core file */
	    m_haveExecutable = false;
	}
	break;
    case DCcorefile:
	// in any event we have an executable at this point
	m_haveExecutable = true;
	if (m_d->parseCoreFile(output)) {
	    // loading a core is like stopping at a breakpoint
	    m_programActive = true;
	    handleRunCommands(output);
	    // do not reset m_corefile
	} else {
	    // report error
	    TQString msg = m_d->driverName() + ": " + TQString(output);
	    KMessageBox::sorry(parentWidget(), msg);

	    // if core file was loaded from command line, revert to info line main
	    if (!cmd->m_byUser) {
		m_d->queueCmd(DCinfolinemain, DebuggerDriver::TQMnormal);
	    }
	    m_corefile = TQString();	/* core file not available any more */
	}
	break;
    case DCinfolinemain:
	// ignore the output, marked file info follows
	m_haveExecutable = true;
	break;
    case DCinfolocals:
	// parse local variables
	if (output[0] != '\0') {
	    handleLocals(output);
	}
	break;
    case DCinforegisters:
	handleRegisters(output);
	break;
    case DCexamine:
	handleMemoryDump(output);
	break;
    case DCinfoline:
	handleInfoLine(cmd, output);
	break;
    case DCdisassemble:
	handleDisassemble(cmd, output);
	break;
    case DCframe:
	handleFrameChange(output);
	updateAllExprs();
	break;
    case DCbt:
	handleBacktrace(output);
	updateAllExprs();
	break;
    case DCprint:
	handlePrint(cmd, output);
	break;
    case DCprintDeref:
	handlePrintDeref(cmd, output);
	break;
    case DCattach:
	m_haveExecutable = true;
	// fall through
    case DCrun:
    case DCcont:
    case DCstep:
    case DCstepi:
    case DCnext:
    case DCnexti:
    case DCfinish:
    case DCuntil:
    case DCthread:
	handleRunCommands(output);
	break;
    case DCkill:
	m_programRunning = m_programActive = false;
	// erase PC
	emit updatePC(TQString(), -1, DbgAddr(), 0);
	break;
    case DCbreaktext:
    case DCbreakline:
    case DCtbreakline:
    case DCbreakaddr:
    case DCtbreakaddr:
    case DCwatchpoint:
	newBreakpoint(cmd, output);
	// fall through
    case DCdelete:
    case DCenable:
    case DCdisable:
	// these commands need immediate response
	m_d->queueCmd(DCinfobreak, DebuggerDriver::TQMoverrideMoreEqual);
	break;
    case DCinfobreak:
	// note: this handler must not enqueue a command, since
	// DCinfobreak is used at various different places.
	updateBreakList(output);
	break;
    case DCfindType:
	handleFindType(cmd, output);
	break;
    case DCprintStruct:
    case DCprintTQStringStruct:
    case DCprintWChar:
	handlePrintStruct(cmd, output);
	break;
    case DCinfosharedlib:
	handleSharedLibs(output);
	break;
    case DCcondition:
    case DCignore:
	// we are not interested in the output
	break;
    case DCinfothreads:
	handleThreadList(output);
	break;
    case DCsetpc:
	handleSetPC(output);
	break;
    case DCsetvariable:
	handleSetVariable(cmd, output);
	break;
    }
}

void KDebugger::backgroundUpdate()
{
    /*
     * If there are still expressions that need to be updated, then do so.
     */
    if (m_programActive)
	evalExpressions();
}

void KDebugger::handleRunCommands(const char* output)
{
    uint flags = m_d->parseProgramStopped(output, m_statusMessage);
    emit updateStatusMessage();

    m_programActive = flags & DebuggerDriver::SFprogramActive;

    // refresh files if necessary
    if (flags & DebuggerDriver::SFrefreshSource) {
	TRACE("re-reading files");
	emit executableUpdated();
    }

    /* 
     * Try to set any orphaned breakpoints now.
     */
    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->isOrphaned()) {
	    TRACE(TQString("re-trying brkpt loc: %2 file: %3 line: %1")
		    .arg(bp->lineNo).arg(bp->location, bp->fileName));
	    CmdQueueItem* cmd = executeBreakpoint(&*bp, true);
	    cmd->m_existingBrkpt = bp->id;	// used in newBreakpoint()
	    flags |= DebuggerDriver::SFrefreshBreak;
	}
    }

    /*
     * If we stopped at a breakpoint, we must update the breakpoint list
     * because the hit count changes. Also, if the breakpoint was temporary
     * it would go away now.
     */
    if ((flags & (DebuggerDriver::SFrefreshBreak|DebuggerDriver::SFrefreshSource)) ||
	stopMayChangeBreakList())
    {
	m_d->queueCmd(DCinfobreak, DebuggerDriver::TQMoverride);
    }

    /*
     * If we haven't listed the shared libraries yet, do so. We must do
     * this before we emit any commands that list variables, since the type
     * libraries depend on the shared libraries.
     */
    if (!m_sharedLibsListed) {
	// must be a high-priority command!
	m_d->executeCmd(DCinfosharedlib);
    }

    // get the backtrace if the program is running
    if (m_programActive) {
	m_d->queueCmd(DCbt, DebuggerDriver::TQMoverride);
    } else {
	// program finished: erase PC
	emit updatePC(TQString(), -1, DbgAddr(), 0);
	// dequeue any commands in the queues
	m_d->flushCommands();
    }

    /* Update threads list */
    if (m_programActive && (flags & DebuggerDriver::SFrefreshThreads)) {
	m_d->queueCmd(DCinfothreads, DebuggerDriver::TQMoverride);
    }

    m_programRunning = false;
    emit programStopped();
}

void KDebugger::slotInferiorRunning()
{
    m_programRunning = true;
}

void KDebugger::updateAllExprs()
{
    if (!m_programActive)
	return;

    // retrieve local variables
    m_d->queueCmd(DCinfolocals, DebuggerDriver::TQMoverride);

    // retrieve registers
    m_d->queueCmd(DCinforegisters, DebuggerDriver::TQMoverride);

    // get new memory dump
    if (!m_memoryExpression.isEmpty()) {
	queueMemoryDump(false);
    }

    // update watch expressions
    VarTree* item = m_watchVariables.firstChild();
    for (; item != 0; item = item->nextSibling()) {
	m_watchEvalExpr.push_back(item->getText());
    }
}

void KDebugger::updateProgEnvironment(const TQString& args, const TQString& wd,
				      const TQDict<EnvVar>& newVars,
				      const TQStringList& newOptions)
{
    m_programArgs = args;
    m_d->executeCmd(DCsetargs, m_programArgs);
    TRACE("new pgm args: " + m_programArgs + "\n");

    m_programWD = wd.stripWhiteSpace();
    if (!m_programWD.isEmpty()) {
	m_d->executeCmd(DCcd, m_programWD);
	TRACE("new wd: " + m_programWD + "\n");
    }

    // update environment variables
    TQDictIterator<EnvVar> it = newVars;
    EnvVar* val;
    for (; (val = it) != 0; ++it) {
	TQString var = it.currentKey();
	switch (val->status) {
	case EnvVar::EVnew:
	    m_envVars.insert(var, val);
	    // fall thru
	case EnvVar::EVdirty:
	    // the value must be in our list
	    ASSERT(m_envVars[var] == val);
	    // update value
	    m_d->executeCmd(DCsetenv, var, val->value);
	    break;
	case EnvVar::EVdeleted:
	    // must be in our list
	    ASSERT(m_envVars[var] == val);
	    // delete value
	    m_d->executeCmd(DCunsetenv, var);
	    m_envVars.remove(var);
	    break;
	default:
	    ASSERT(false);
	case EnvVar::EVclean:
	    // variable not changed
	    break;
	}
    }

    // update options
    TQStringList::ConstIterator oi;
    for (oi = newOptions.begin(); oi != newOptions.end(); ++oi)
    {
	if (m_boolOptions.findIndex(*oi) < 0) {
	    // the options is currently not set, so set it
	    m_d->executeCmd(DCsetoption, *oi, 1);
	} else {
	    // option is set, no action required, but move it to the end
	    m_boolOptions.remove(*oi);
	}
	m_boolOptions.append(*oi);
    }
    /*
     * Now all options that should be set are at the end of m_boolOptions.
     * If some options need to be unset, they are at the front of the list.
     * Here we unset and remove them.
     */
    while (m_boolOptions.count() > newOptions.count()) {
	m_d->executeCmd(DCsetoption, m_boolOptions.first(), 0);
	m_boolOptions.remove(m_boolOptions.begin());
    }
}

void KDebugger::handleLocals(const char* output)
{
    // retrieve old list of local variables
    TQStringList oldVars = m_localVariables.exprList();

    /*
     *  Get local variables.
     */
    std::list<ExprValue*> newVars;
    parseLocals(output, newVars);

    /*
     * Clear any old VarTree item pointers, so that later we don't access
     * dangling pointers.
     */
    m_localVariables.clearPendingUpdates();

    /*
     * Match old variables against new ones.
     */
    for (TQStringList::ConstIterator n = oldVars.begin(); n != oldVars.end(); ++n) {
	// lookup this variable in the list of new variables
	std::list<ExprValue*>::iterator v = newVars.begin();
	while (v != newVars.end() && (*v)->m_name != *n)
	    ++v;
	if (v == newVars.end()) {
	    // old variable not in the new variables
	    TRACE("old var deleted: " + *n);
	    VarTree* v = m_localVariables.topLevelExprByName(*n);
	    if (v != 0) {
		m_localVariables.removeExpr(v);
	    }
	} else {
	    // variable in both old and new lists: update
	    TRACE("update var: " + *n);
	    m_localVariables.updateExpr(*v, *m_typeTable);
	    // remove the new variable from the list
	    delete *v;
	    newVars.erase(v);
	}
    }
    // insert all remaining new variables
    while (!newVars.empty())
    {
	ExprValue* v = newVars.front();
	TRACE("new var: " + v->m_name);
	m_localVariables.insertExpr(v, *m_typeTable);
	delete v;
	newVars.pop_front();
    }
}

void KDebugger::parseLocals(const char* output, std::list<ExprValue*>& newVars)
{
    std::list<ExprValue*> vars;
    m_d->parseLocals(output, vars);

    TQString origName;			/* used in renaming variables */
    while (!vars.empty())
    {
	ExprValue* variable = vars.front();
	vars.pop_front();
	/*
	 * When gdb prints local variables, those from the innermost block
	 * come first. We run through the list of already parsed variables
	 * to find duplicates (ie. variables that hide local variables from
	 * a surrounding block). We keep the name of the inner variable, but
	 * rename those from the outer block so that, when the value is
	 * updated in the window, the value of the variable that is
	 * _visible_ changes the color!
	 */
	int block = 0;
	origName = variable->m_name;
	for (std::list<ExprValue*>::iterator v = newVars.begin(); v != newVars.end(); ++v) {
	    if (variable->m_name == (*v)->m_name) {
		// we found a duplicate, change name
		block++;
		TQString newName = origName + " (" + TQString().setNum(block) + ")";
		variable->m_name = newName;
	    }
	}
	newVars.push_back(variable);
    }
}

bool KDebugger::handlePrint(CmdQueueItem* cmd, const char* output)
{
    ASSERT(cmd->m_expr != 0);

    ExprValue* variable = m_d->parsePrintExpr(output, true);
    if (variable == 0)
	return false;

    // set expression "name"
    variable->m_name = cmd->m_expr->getText();

    {
	TRACE("update expr: " + cmd->m_expr->getText());
	cmd->m_exprWnd->updateExpr(cmd->m_expr, variable, *m_typeTable);
	delete variable;
    }

    evalExpressions();			/* enqueue dereferenced pointers */

    return true;
}

bool KDebugger::handlePrintDeref(CmdQueueItem* cmd, const char* output)
{
    ASSERT(cmd->m_expr != 0);

    ExprValue* variable = m_d->parsePrintExpr(output, true);
    if (variable == 0)
	return false;

    // set expression "name"
    variable->m_name = cmd->m_expr->getText();

    {
	/*
	 * We must insert a dummy parent, because otherwise variable's value
	 * would overwrite cmd->m_expr's value.
	 */
	ExprValue* dummyParent = new ExprValue(variable->m_name, VarTree::NKplain);
	dummyParent->m_varKind = VarTree::VKdummy;
	// the name of the parsed variable is the address of the pointer
	TQString addr = "*" + cmd->m_expr->value();
	variable->m_name = addr;
	variable->m_nameKind = VarTree::NKaddress;

	dummyParent->m_child = variable;
	// expand the first level for convenience
	variable->m_initiallyExpanded = true;
	TRACE("update ptr: " + cmd->m_expr->getText());
	cmd->m_exprWnd->updateExpr(cmd->m_expr, dummyParent, *m_typeTable);
	delete dummyParent;
    }

    evalExpressions();			/* enqueue dereferenced pointers */

    return true;
}

// parse the output of bt
void KDebugger::handleBacktrace(const char* output)
{
    // reduce flicker
    m_btWindow.setUpdatesEnabled(false);
    m_btWindow.clear();

    std::list<StackFrame> stack;
    m_d->parseBackTrace(output, stack);

    if (!stack.empty()) {
	std::list<StackFrame>::iterator frm = stack.begin();
	// first frame must set PC
	// note: frm->lineNo is zero-based
	emit updatePC(frm->fileName, frm->lineNo, frm->address, frm->frameNo);

	for (; frm != stack.end(); ++frm) {
	    TQString func;
	    if (frm->var != 0)
		func = frm->var->m_name;
	    else
		func = frm->fileName + ":" + TQString().setNum(frm->lineNo+1);
	    m_btWindow.insertItem(func);
	    TRACE("frame " + func + " (" + frm->fileName + ":" +
		  TQString().setNum(frm->lineNo+1) + ")");
	}
    }
    m_btWindow.setUpdatesEnabled(true);
    m_btWindow.repaint();
}

void KDebugger::gotoFrame(int frame)
{
    m_d->executeCmd(DCframe, frame);
}

void KDebugger::handleFrameChange(const char* output)
{
    TQString fileName;
    int frameNo;
    int lineNo;
    DbgAddr address;
    if (m_d->parseFrameChange(output, frameNo, fileName, lineNo, address)) {
	/* lineNo can be negative here if we can't find a file name */
	emit updatePC(fileName, lineNo, address, frameNo);
    } else {
	emit updatePC(fileName, -1, address, frameNo);
    }
}

void KDebugger::evalExpressions()
{
    // evaluate expressions in the following order:
    //   watch expressions
    //   pointers in local variables
    //   pointers in watch expressions
    //   types in local variables
    //   types in watch expressions
    //   struct members in local variables
    //   struct members in watch expressions
    VarTree* exprItem = 0;
    if (!m_watchEvalExpr.empty())
    {
	TQString expr = m_watchEvalExpr.front();
	m_watchEvalExpr.pop_front();
	exprItem = m_watchVariables.topLevelExprByName(expr);
    }
    if (exprItem != 0) {
	CmdQueueItem* cmd = m_d->queueCmd(DCprint, exprItem->getText(), DebuggerDriver::TQMoverride);
	// remember which expr this was
	cmd->m_expr = exprItem;
	cmd->m_exprWnd = &m_watchVariables;
    } else {
	ExprWnd* wnd;
#define POINTER(widget) \
		wnd = &widget; \
		exprItem = widget.nextUpdatePtr(); \
		if (exprItem != 0) goto pointer
#define STRUCT(widget) \
		wnd = &widget; \
		exprItem = widget.nextUpdateStruct(); \
		if (exprItem != 0) goto ustruct
#define TYPE(widget) \
		wnd = &widget; \
		exprItem = widget.nextUpdateType(); \
		if (exprItem != 0) goto type
    repeat:
	POINTER(m_localVariables);
	POINTER(m_watchVariables);
	STRUCT(m_localVariables);
	STRUCT(m_watchVariables);
	TYPE(m_localVariables);
	TYPE(m_watchVariables);
#undef POINTER
#undef STRUCT
#undef TYPE
	return;

	pointer:
	// we have an expression to send
	dereferencePointer(wnd, exprItem, false);
	return;

	ustruct:
	// paranoia
	if (exprItem->m_type == 0 || exprItem->m_type == TypeInfo::unknownType())
	    goto repeat;
	evalInitialStructExpression(exprItem, wnd, false);
	return;

	type:
	/*
	 * Sometimes a VarTree gets registered twice for a type update. So
	 * it may happen that it has already been updated. Hence, we ignore
	 * it here and go on to the next task.
	 */
	if (exprItem->m_type != 0)
	    goto repeat;
	determineType(wnd, exprItem);
    }
}

void KDebugger::dereferencePointer(ExprWnd* wnd, VarTree* exprItem,
				   bool immediate)
{
    ASSERT(exprItem->m_varKind == VarTree::VKpointer);

    TQString expr = exprItem->computeExpr();
    TRACE("dereferencing pointer: " + expr);
    CmdQueueItem* cmd;
    if (immediate) {
	cmd = m_d->queueCmd(DCprintDeref, expr, DebuggerDriver::TQMoverrideMoreEqual);
    } else {
	cmd = m_d->queueCmd(DCprintDeref, expr, DebuggerDriver::TQMoverride);
    }
    // remember which expr this was
    cmd->m_expr = exprItem;
    cmd->m_exprWnd = wnd;
}

void KDebugger::determineType(ExprWnd* wnd, VarTree* exprItem)
{
    ASSERT(exprItem->m_varKind == VarTree::VKstruct);

    TQString expr = exprItem->computeExpr();
    TRACE("get type of: " + expr);
    CmdQueueItem* cmd;
    cmd = m_d->queueCmd(DCfindType, expr, DebuggerDriver::TQMoverride);

    // remember which expr this was
    cmd->m_expr = exprItem;
    cmd->m_exprWnd = wnd;
}

void KDebugger::handleFindType(CmdQueueItem* cmd, const char* output)
{
    TQString type;
    if (m_d->parseFindType(output, type))
    {
	ASSERT(cmd != 0 && cmd->m_expr != 0);

	TypeInfo* info = m_typeTable->lookup(type);

	if (info == 0) {
	    /*
	     * We've asked gdb for the type of the expression in
	     * cmd->m_expr, but it returned a name we don't know. The base
	     * class (and member) types have been checked already (at the
	     * time when we parsed that particular expression). Now it's
	     * time to derive the type from the base classes as a last
	     * resort.
	     */
	    info = cmd->m_expr->inferTypeFromBaseClass();
	    // if we found a type through this method, register an alias
	    if (info != 0) {
		TRACE("infered alias: " + type);
		m_typeTable->registerAlias(type, info);
	    }
	}
	if (info == 0) {
            TRACE("unknown type "+type);
	    cmd->m_expr->m_type = TypeInfo::unknownType();
	} else {
	    cmd->m_expr->m_type = info;
	    /* since this node has a new type, we get its value immediately */
	    evalInitialStructExpression(cmd->m_expr, cmd->m_exprWnd, false);
	    return;
	}
    }

    evalExpressions();			/* queue more of them */
}

void KDebugger::handlePrintStruct(CmdQueueItem* cmd, const char* output)
{
    VarTree* var = cmd->m_expr;
    ASSERT(var != 0);
    ASSERT(var->m_varKind == VarTree::VKstruct);

    ExprValue* partExpr;
    if (cmd->m_cmd == DCprintTQStringStruct) {
	partExpr = m_d->parseTQCharArray(output, false, m_typeTable->qCharIsShort());
    } else if (cmd->m_cmd == DCprintWChar) {
	partExpr = m_d->parseTQCharArray(output, false, true);
    } else {
	partExpr = m_d->parsePrintExpr(output, false);
    }
    bool errorValue =
	partExpr == 0 ||
	/* we only allow simple values at the moment */
	partExpr->m_child != 0;

    TQString partValue;
    if (errorValue)
    {
	partValue = "?""?""?";	// 2 question marks in a row would be a trigraph
    } else {
	partValue = partExpr->m_value;
    }
    delete partExpr;
    partExpr = 0;

    /*
     * Updating a struct value works like this: var->m_partialValue holds
     * the value that we have gathered so far (it's been initialized with
     * var->m_type->m_displayString[0] earlier). Each time we arrive here,
     * we append the printed result followed by the next
     * var->m_type->m_displayString to var->m_partialValue.
     * 
     * If the expression we just evaluated was a guard expression, and it
     * resulted in an error, we must not evaluate the real expression, but
     * go on to the next index. (We must still add the question marks to
     * the value).
     * 
     * Next, if this was the length expression, we still have not seen the
     * real expression, but the length of a TQString.
     */
    ASSERT(var->m_exprIndex >= 0 && var->m_exprIndex <= typeInfoMaxExpr);

    if (errorValue || !var->m_exprIndexUseGuard)
    {
	// add current partValue (which might be the question marks)
	var->m_partialValue += partValue;
	var->m_exprIndex++;		/* next part */
	var->m_exprIndexUseGuard = true;
	var->m_partialValue += var->m_type->m_displayString[var->m_exprIndex];
    }
    else
    {
	// this was a guard expression that succeeded
	// go for the real expression
	var->m_exprIndexUseGuard = false;
    }

    /* go for more sub-expressions if needed */
    if (var->m_exprIndex < var->m_type->m_numExprs) {
	/* queue a new print command with quite high priority */
	evalStructExpression(var, cmd->m_exprWnd, true);
	return;
    }

    cmd->m_exprWnd->updateStructValue(var);

    evalExpressions();			/* enqueue dereferenced pointers */
}

/* queues the first printStruct command for a struct */
void KDebugger::evalInitialStructExpression(VarTree* var, ExprWnd* wnd, bool immediate)
{
    var->m_exprIndex = 0;
    if (var->m_type != TypeInfo::wchartType())
    {
	var->m_exprIndexUseGuard = true;
	var->m_partialValue = var->m_type->m_displayString[0];
	evalStructExpression(var, wnd, immediate);
    }
    else
    {
	var->m_exprIndexUseGuard = false;
	TQString expr = var->computeExpr();
	CmdQueueItem* cmd = m_d->queueCmd(DCprintWChar, expr,
				immediate  ?  DebuggerDriver::TQMoverrideMoreEqual
				: DebuggerDriver::TQMoverride);
	// remember which expression this was
	cmd->m_expr = var;
	cmd->m_exprWnd = wnd;
    }
}

/** queues a printStruct command; var must have been initialized correctly */
void KDebugger::evalStructExpression(VarTree* var, ExprWnd* wnd, bool immediate)
{
    TQString base = var->computeExpr();
    TQString expr;
    if (var->m_exprIndexUseGuard) {
	expr = var->m_type->m_guardStrings[var->m_exprIndex];
	if (expr.isEmpty()) {
	    // no guard, omit it and go to expression
	    var->m_exprIndexUseGuard = false;
	}
    }
    if (!var->m_exprIndexUseGuard) {
	expr = var->m_type->m_exprStrings[var->m_exprIndex];
    }

    expr.replace("%s", base);

    DbgCommand dbgCmd = DCprintStruct;
    // check if this is a TQString::Data
    if (expr.left(15) == "/TQString::Data ")
    {
	if (m_typeTable->parseTQt2TQStrings())
	{
	    expr = expr.mid(15, expr.length());	/* strip off /TQString::Data */
	    dbgCmd = DCprintTQStringStruct;
	} else {
	    /*
	     * This should not happen: the type libraries should be set up
	     * in a way that this can't happen. If this happens
	     * nevertheless it means that, eg., tdecore was loaded but qt2
	     * was not (only qt2 enables the TQString feature).
	     */
	    // TODO: remove this "print"; queue the next printStruct instead
	    expr = "*0";
	}
    }
    TRACE("evalStruct: " + expr + (var->m_exprIndexUseGuard ? " // guard" : " // real"));
    CmdQueueItem* cmd = m_d->queueCmd(dbgCmd, expr,
				      immediate  ?  DebuggerDriver::TQMoverrideMoreEqual
				      : DebuggerDriver::TQMnormal);

    // remember which expression this was
    cmd->m_expr = var;
    cmd->m_exprWnd = wnd;
}

void KDebugger::handleSharedLibs(const char* output)
{
    // parse the table of shared libraries
    m_sharedLibs = m_d->parseSharedLibs(output);
    m_sharedLibsListed = true;

    // get type libraries
    m_typeTable->loadLibTypes(m_sharedLibs);

    // hand over the TQString data cmd
    m_d->setPrintTQStringDataCmd(m_typeTable->printTQStringDataCmd());
}

CmdQueueItem* KDebugger::loadCoreFile()
{
    return m_d->queueCmd(DCcorefile, m_corefile, DebuggerDriver::TQMoverride);
}

void KDebugger::slotExpanding(TQListViewItem* item)
{
    VarTree* exprItem = static_cast<VarTree*>(item);
    if (exprItem->m_varKind != VarTree::VKpointer) {
	return;
    }
    ExprWnd* wnd = static_cast<ExprWnd*>(item->listView());
    dereferencePointer(wnd, exprItem, true);
}

// add the expression in the edit field to the watch expressions
void KDebugger::addWatch(const TQString& t)
{
    TQString expr = t.stripWhiteSpace();
    // don't add a watched expression again
    if (expr.isEmpty() || m_watchVariables.topLevelExprByName(expr) != 0)
	return;
    ExprValue e(expr, VarTree::NKplain);
    m_watchVariables.insertExpr(&e, *m_typeTable);

    // if we are boring ourselves, send down the command
    if (m_programActive) {
	m_watchEvalExpr.push_back(expr);
	if (m_d->isIdle()) {
	    evalExpressions();
	}
    }
}

// delete a toplevel watch expression
void KDebugger::slotDeleteWatch()
{
    // delete only allowed while debugger is idle; or else we might delete
    // the very expression the debugger is currently working on...
    if (m_d == 0 || !m_d->isIdle())
	return;

    VarTree* item = m_watchVariables.currentItem();
    if (item == 0 || !item->isToplevelExpr())
	return;

    // remove the variable from the list to evaluate
    TQStringList::iterator i = m_watchEvalExpr.find(item->getText());
    if (i != m_watchEvalExpr.end()) {
	m_watchEvalExpr.erase(i);
    }
    m_watchVariables.removeExpr(item);
    // item is invalid at this point!
}

void KDebugger::handleRegisters(const char* output)
{
    emit registersChanged(m_d->parseRegisters(output));
}

/*
 * The output of the DCbreak* commands has more accurate information about
 * the file and the line number.
 *
 * All newly set breakpoints are inserted in the m_brkpts, even those that
 * were not set sucessfully. The unsuccessful breakpoints ("orphaned
 * breakpoints") are assigned negative ids, and they are tried to set later
 * when the program stops again at a breakpoint.
 */
void KDebugger::newBreakpoint(CmdQueueItem* cmd, const char* output)
{
    BrkptIterator bp;
    if (cmd->m_brkpt != 0) {
	// a new breakpoint, put it in the list
	assert(cmd->m_brkpt->id == 0);
	m_brkpts.push_back(*cmd->m_brkpt);
	delete cmd->m_brkpt;
	bp = m_brkpts.end();
	--bp;
    } else {
	// an existing breakpoint was retried
	assert(cmd->m_existingBrkpt != 0);
	bp = breakpointById(cmd->m_existingBrkpt);
	if (bp == m_brkpts.end())
	    return;
    }

    // parse the output to determine success or failure
    int id;
    TQString file;
    int lineNo;
    TQString address;
    if (!m_d->parseBreakpoint(output, id, file, lineNo, address))
    {
	/*
	 * Failure, the breakpoint could not be set. If this is a new
	 * breakpoint, assign it a negative id. We look for the minimal id
	 * of all breakpoints (that are already in the list) to get the new
	 * id.
	 */
	if (bp->id == 0)
	{
	    int minId = 0;
	    for (BrkptIterator i = m_brkpts.begin(); i != m_brkpts.end(); ++i) {
		if (i->id < minId)
		    minId = i->id;
	    }
	    bp->id = minId-1;
	}
	return;
    }

    // The breakpoint was successfully set.
    if (bp->id <= 0)
    {
	// this is a new or orphaned breakpoint:
	// set the remaining properties
	if (!bp->enabled) {
	    m_d->executeCmd(DCdisable, id);
	}
	if (!bp->condition.isEmpty()) {
	    m_d->executeCmd(DCcondition, bp->condition, id);
	}
    }

    bp->id = id;
    bp->fileName = file;
    bp->lineNo = lineNo;
    if (!address.isEmpty())
	bp->address = address;
}

void KDebugger::updateBreakList(const char* output)
{
    // get the new list
    std::list<Breakpoint> brks;
    m_d->parseBreakList(output, brks);

    // merge existing information into the new list
    // then swap the old and new lists

    for (BrkptIterator bp = brks.begin(); bp != brks.end(); ++bp)
    {
	BrkptIterator i = breakpointById(bp->id);
	if (i != m_brkpts.end())
	{
	    // preserve accurate location information
	    // note that xsldbg doesn't have a location in
	    // the listed breakpoint if it has just been set
	    // therefore, we copy it as well if necessary
	    bp->text = i->text;
	    if (!i->fileName.isEmpty()) {
		bp->fileName = i->fileName;
		bp->lineNo = i->lineNo;
	    }
	}
    }

    // orphaned breakpoints must be copied
    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->isOrphaned())
	    brks.push_back(*bp);
    }

    m_brkpts.swap(brks);
    emit breakpointsChanged();
}

// look if there is at least one temporary breakpoint
// or a watchpoint
bool KDebugger::stopMayChangeBreakList() const
{
    for (BrkptROIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->temporary || bp->type == Breakpoint::watchpoint)
	    return true;
    }
    return false;
}

KDebugger::BrkptIterator KDebugger::breakpointByFilePos(TQString file, int lineNo,
					   const DbgAddr& address)
{
    // look for exact file name match
    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->lineNo == lineNo &&
	    bp->fileName == file &&
	    (address.isEmpty() || bp->address == address))
	{
	    return bp;
	}
    }
    // not found, so try basename
    // strip off directory part of file name
    int offset = file.findRev("/");
    file.remove(0, offset+1);

    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	// get base name of breakpoint's file
	TQString basename = bp->fileName;
	int offset = basename.findRev("/");
	if (offset >= 0) {
	    basename.remove(0, offset+1);
	}

	if (bp->lineNo == lineNo &&
	    basename == file &&
	    (address.isEmpty() || bp->address == address))
	{
	    return bp;
	}
    }

    // not found
    return m_brkpts.end();
}

KDebugger::BrkptIterator KDebugger::breakpointById(int id)
{
    for (BrkptIterator bp = m_brkpts.begin(); bp != m_brkpts.end(); ++bp)
    {
	if (bp->id == id) {
	    return bp;
	}
    }
    // not found
    return m_brkpts.end();
}

void KDebugger::slotValuePopup(const TQString& expr)
{
    // search the local variables for a match
    VarTree* v = m_localVariables.topLevelExprByName(expr);
    if (v == 0) {
	// not found, check watch expressions
	v = m_watchVariables.topLevelExprByName(expr);
	if (v == 0) {
	    // try a member of 'this'
	    v = m_localVariables.topLevelExprByName("this");
	    if (v != 0)
		v = ExprWnd::ptrMemberByName(v, expr);
	    if (v == 0) {
		// nothing found; do nothing
		return;
	    }
	}
    }

    // construct the tip
    TQString tip = v->getText() + " = ";
    if (!v->value().isEmpty())
    {
	tip += v->value();
    }
    else
    {
	// no value: we use some hint
	switch (v->m_varKind) {
	case VarTree::VKstruct:
	    tip += "{...}";
	    break;
	case VarTree::VKarray:
	    tip += "[...]";
	    break;
	default:
	    tip += "?""?""?";	// 2 question marks in a row would be a trigraph
	    break;
	}
    }
    emit valuePopup(tip);
}

void KDebugger::slotDisassemble(const TQString& fileName, int lineNo)
{
    if (m_haveExecutable) {
	CmdQueueItem* cmd = m_d->queueCmd(DCinfoline, fileName, lineNo,
					  DebuggerDriver::TQMoverrideMoreEqual);
	cmd->m_fileName = fileName;
	cmd->m_lineNo = lineNo;
    }
}

void KDebugger::handleInfoLine(CmdQueueItem* cmd, const char* output)
{
    TQString addrFrom, addrTo;
    if (cmd->m_lineNo >= 0) {
	// disassemble
	if (m_d->parseInfoLine(output, addrFrom, addrTo)) {
	    // got the address range, now get the real code
	    CmdQueueItem* c = m_d->queueCmd(DCdisassemble, addrFrom, addrTo,
					    DebuggerDriver::TQMoverrideMoreEqual);
	    c->m_fileName = cmd->m_fileName;
	    c->m_lineNo = cmd->m_lineNo;
	} else {
	    // no code
	    emit disassembled(cmd->m_fileName, cmd->m_lineNo, std::list<DisassembledCode>());
	}
    } else {
	// set program counter
	if (m_d->parseInfoLine(output, addrFrom, addrTo)) {
	    // move the program counter to the start address
	    m_d->executeCmd(DCsetpc, addrFrom);
	}
    }
}

void KDebugger::handleDisassemble(CmdQueueItem* cmd, const char* output)
{
    emit disassembled(cmd->m_fileName, cmd->m_lineNo,
		      m_d->parseDisassemble(output));
}

void KDebugger::handleThreadList(const char* output)
{
    emit threadsChanged(m_d->parseThreadList(output));
}

void KDebugger::setThread(int id)
{
    m_d->queueCmd(DCthread, id, DebuggerDriver::TQMoverrideMoreEqual);
}

void KDebugger::setMemoryExpression(const TQString& memexpr)
{
    m_memoryExpression = memexpr;

    // queue the new expression
    if (!m_memoryExpression.isEmpty() &&
	isProgramActive() &&
	!isProgramRunning())
    {
	queueMemoryDump(true);
    }
}

void KDebugger::queueMemoryDump(bool immediate)
{
    m_d->queueCmd(DCexamine, m_memoryExpression, m_memoryFormat,
		  immediate ? DebuggerDriver::TQMoverrideMoreEqual :
			      DebuggerDriver::TQMoverride);
}

void KDebugger::handleMemoryDump(const char* output)
{
    std::list<MemoryDump> memdump;
    TQString msg = m_d->parseMemoryDump(output, memdump);
    emit memoryDumpChanged(msg, memdump);
}

void KDebugger::setProgramCounter(const TQString& file, int line, const DbgAddr& addr)
{
    if (addr.isEmpty()) {
	// find address of the specified line
	CmdQueueItem* cmd = m_d->executeCmd(DCinfoline, file, line);
	cmd->m_lineNo = -1;		/* indicates "Set PC" UI command */
    } else {
	// move the program counter to that address
	m_d->executeCmd(DCsetpc, addr.asString());
    }
}

void KDebugger::handleSetPC(const char* /*output*/)
{
    // TODO: handle errors

    // now go to the top-most frame
    // this also modifies the program counter indicator in the UI
    gotoFrame(0);
}

void KDebugger::slotValueEdited(VarTree* expr, const TQString& text)
{
    if (text.simplifyWhiteSpace().isEmpty())
	return;			       /* no text entered: ignore request */

    ExprWnd* wnd = static_cast<ExprWnd*>(expr->listView());
    TRACE(TQString().sprintf("Changing %s to ",
			    wnd->name()) + text);

    // determine the lvalue to edit
    TQString lvalue = expr->computeExpr();
    CmdQueueItem* cmd = m_d->executeCmd(DCsetvariable, lvalue, text);
    cmd->m_expr = expr;
    cmd->m_exprWnd = wnd;
}

void KDebugger::handleSetVariable(CmdQueueItem* cmd, const char* output)
{
    TQString msg = m_d->parseSetVariable(output);
    if (!msg.isEmpty())
    {
	// there was an error; display it in the status bar
	m_statusMessage = msg;
	emit updateStatusMessage();
	return;
    }

    // get the new value
    TQString expr = cmd->m_expr->computeExpr();
    CmdQueueItem* printCmd =
	m_d->queueCmd(DCprint, expr, DebuggerDriver::TQMoverrideMoreEqual);
    printCmd->m_expr = cmd->m_expr;
    printCmd->m_exprWnd = cmd->m_exprWnd;
}


#include "debugger.moc"