File: interactive.cxx

package info (click to toggle)
systemtap 5.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,964 kB
  • sloc: cpp: 80,838; ansic: 54,757; xml: 49,725; exp: 43,665; sh: 11,527; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (2074 lines) | stat: -rw-r--r-- 54,707 bytes parent folder | download
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
// systemtap interactive mode
// Copyright (C) 2015 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "config.h"
#include "interactive.h"
#include "session.h"
#include "util.h"
#include "staptree.h"
#include "parse.h"
#include "csclient.h"
#include "client-nss.h"

#include "stap-probe.h"

#include <cstdlib>
#include <stack>
#include <sstream>
#include <iterator>
#include <ext/stdio_filebuf.h>

using namespace std;
using namespace __gnu_cxx;

extern "C" {
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <ctype.h>
#include <sys/wait.h>

#if HAVE_LIBSQLITE3
  #include <sqlite3.h>
#endif
}

// FIXME: these declarations don't really belong here.
extern int
passes_0_4 (systemtap_session &s);
extern int
pass_5_1 (systemtap_session &s, vector<remote*> targets);
extern int
pass_5_2 (systemtap_session &s, vector<remote*> targets);

static int
forked_passes_0_4 (systemtap_session &s);

static int
forked_parse_pass (systemtap_session &s, vector<string> &script);
static int
forked_semantic_pass (systemtap_session &s, vector<string> &script);

// Ask user a y-or-n question and return 1 iff answer is yes.  The
// prompt argument should end in "? ". Note that this is a simplified
// version of gdb's defaulted_query() function.
#if HAVE_LIBSQLITE3
struct metafile {
  string name;
  string title;
  string description;
  string path;
};
#endif

enum query_default { no_default,	// There isn't a default.
		     default_yes,	// The default is "yes".
		     default_no };	// THe default is "no".

int
query (const char *prompt, query_default qdefault)
{
  int def_value;
  char def_answer, not_def_answer;
  const char *y_string, *n_string;
  int retval;
    
  // Set up according to which answer is the default.
  if (qdefault == no_default)
    {
      def_value = 1;
      def_answer = 'Y';
      not_def_answer = 'N';
      y_string = "y";
      n_string = "n";
    }
  else if (qdefault == default_yes)
    {
      def_value = 1;
      def_answer = 'Y';
      not_def_answer = 'N';
      y_string = "[y]";
      n_string = "n";
    }
  else
    {
      def_value = 0;
      def_answer = 'N';
      not_def_answer = 'Y';
      y_string = "y";
      n_string = "[n]";
    }

  // If input isn't coming from the user directly, just say what
  // question we're asking, and then answer the default automatically.
  if (! isatty(fileno(stdin)))
    {
      clog << prompt
	   << _F("(%s or %s) [answered %c; input not from terminal]\n", 
		 y_string, n_string, def_answer);
      return def_value;
    }

  while (1)
    {
      char *response, answer;

      response = readline(_F("%s(%s or %s) ", prompt, y_string,
			     n_string).c_str());
      if (response == NULL)		// EOF
	{
	  clog << _F("EOF [assumed %c]\n", def_answer);
	  retval = def_value;
	  break;
	}

      answer = toupper(response[0]);
      free (response);

      // Check answer.  For the non-default, the user must specify the
      // non-default explicitly.
      if (answer == not_def_answer)
	{
	  retval = !def_value;
	  break;
	}
      // Otherwise, if a default was specified, the user may either
      // specify the required input or have it default by entering
      // nothing.
      if (answer == def_answer
	  || (qdefault != no_default && answer == '\0'))
	{
	  retval = def_value;
	  break;
	}
      // Invalid entries are not defaulted and require another selection.
      clog << _F("Please answer %s or %s.\n", y_string, n_string);
    }
  return retval;
}

// Class that describes an interactive command or an option for the
// set/show commands.
class cmdopt
{
protected:
  string _help_text;

public:
  virtual ~cmdopt() { }
  string name;				// command/option name 
  string usage;				// command usage (includes options)
  vector<string> aliases;

  // help_text() returns the help text for a command/option
  virtual string help_text(size_t indent __attribute ((unused))) const
  {
    return _help_text;
  }

  // handler() is the code associated with a command/option
  virtual bool handler(systemtap_session &s, vector<string> &tokens,
		       string &input) = 0;

  bool accept(const string& input) const;
};

bool cmdopt::accept(const string &input) const
{
  if (input == name)
    return true;
  for (auto it = aliases.begin(); it != aliases.end(); ++it)
    {
      if (input == *it)
        return true;
    }
  return false;
}

typedef vector<cmdopt*> cmdopt_vector;
typedef vector<cmdopt*>::const_iterator cmdopt_vector_const_iterator;
typedef vector<cmdopt*>::iterator cmdopt_vector_iterator;

// A vector of all commands.
static cmdopt_vector command_vec;
// A vector of all commands that take options.
static cmdopt_vector option_command_vec;
// A vector of all options;
static cmdopt_vector option_vec;
// A vector containing the user's script, one probe/function/etc. per
// string.
static vector<string> script_vec;
static bool auto_analyze = true;

struct match_item;
typedef map<string, match_item*> match_item_map;
typedef map<string, match_item*>::const_iterator match_item_map_const_iterator;
typedef map<string, match_item*>::iterator match_item_map_iterator;
typedef map<string, match_item*>::reverse_iterator match_item_map_rev_iterator;
typedef map<string, match_item*>::const_reverse_iterator match_item_map_const_rev_iterator;

struct match_item
{
    match_item() { terminal = false; }
    ~match_item();

    string match_text;
    string regexp;
    bool terminal;
    match_item_map sub_matches;

    bool full_match(const string &text);
    bool partial_match(const string &text);
};

static void
delete_match_map_items(match_item_map *map)
{
    match_item_map_iterator it = map->begin();
    while (it != map->end())
    {
	match_item *item = it->second;
	map->erase(it++);
	delete item;
    }
}

match_item::~match_item()
{
    delete_match_map_items(&sub_matches);
}

// match_item::full_match() looks for a "full" match. A full match
// completely matches an item. Examples are:
//
//    USER INPUT		MATCH ITEM
//    'kernel'			'kernel'
//    'kernel("sys_read")'	'kernel(string)'
//    'process(123)'		'process(number)'
//    'function("main")'	'function(string)'
//
// '(number)' and '(string)' matching are done via regexps.
bool
match_item::full_match(const string &text)
{
    if (regexp.empty())
	return (text == match_text);

    vector<string> matches;
    size_t len = match_text.length();
    if (len < text.length() && text.substr(0, len) == match_text
	&& regexp_match(text.substr(len), regexp, matches) == 0)
	return true;
    return false;
}

// match_item::partial_match() looks for a "partial" match. A partial
// match looks like:
//
//    USER INPUT		MATCH ITEM
//    'ke'			'kernel',
//    'proc'			'process', 'process(number)', and
//				'process(string)'
bool
match_item::partial_match(const string &text)
{
    // You can't really do a partial regexp match, so we won't even
    // try. Just match the starting static text of the match_item.
    size_t len = text.length();
    if (len == 0)
	return true;
    return (match_text.compare(0, len, text) == 0);
}

static match_item_map probe_map;

static string saved_token;
static vector<remote*> *saved_targets;

static void interactive_usage();

//
// Supported commands.
// 

class help_cmd: public cmdopt
{
public:
  help_cmd()
  {
    name = usage = "help";
    aliases = { "h", "?" };
    _help_text = "Print command list or description of a specific command.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens __attribute ((unused)),
	       string &input __attribute ((unused)))
  {
    tokens.erase(tokens.begin());
    if (tokens.size() == 0) {
        interactive_usage();
        return false;
    }

    for (auto it = command_vec.begin();
	 it != command_vec.end(); ++it)
      {
        if ((*it)->accept(tokens[0])) {
	  cout << (*it)->usage << ": " << (*it)->help_text((*it)->usage.size()) << endl;
          return false;
	}
      }

    cout <<"Undefined command \""<< tokens[0]<< "\". Try \"help\" or \"?\" for a full list of commands." << endl;
    return false;
  }
};

class list_cmd : public cmdopt
{
public:
  list_cmd()
  {
    name = usage = "list";
    aliases = { "l" };
    _help_text = "Display the current script.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens __attribute ((unused)),
	       string &input __attribute ((unused)))
  {
    // FIXME: We will want to use 'printscript' here, once we store
    // parser output instead of strings.
    size_t width = 1;
    size_t len = script_vec.size();
    if (len >= 10000) { len /= 10000; width += 4; }
    if (len >= 100) { len /= 100; width += 2; }
    if (len >= 10) { len /= 10; width += 1; }
    size_t i = 1;
    for (vector<string>::const_iterator it = script_vec.begin();
	 it != script_vec.end(); ++it)
    {
	clog << setw(width) << i++ << ": " << (*it) << endl;
    }
    return false;
  }
};

class set_cmd: public cmdopt
{
public:
  set_cmd()
  {
    name = "set";
    usage = "set OPTION VALUE";
    _help_text = "Set option value. Supported options are:";
  }
  string help_text(size_t indent __attribute ((unused))) const
  {
    ostringstream buffer;
    size_t width = 1;

    // Find biggest option "name" field.
    for (cmdopt_vector_const_iterator it = option_vec.begin();
	 it != option_vec.end(); ++it)
      {
	if ((*it)->name.size() > width)
	  width = (*it)->name.size();
      }

    // Add each option to the output.
    buffer << _help_text;
    for (cmdopt_vector_iterator it = option_vec.begin();
	 it != option_vec.end(); ++it)
      {
	buffer << endl << setw(4) << " ";
	buffer << setw(width) << left << (*it)->name << " -- "
	       << (*it)->help_text(0);
      }
    return buffer.str();
  }
  bool handler(systemtap_session &s, vector<string> &tokens, string &input)
  {
    bool option_found = false;
    if (tokens.size() < 3)
      {
	cout  << "Invalid command" << endl;
	cout << usage << ": " << help_text(0) <<endl;
	return false;
      }

    // Search the option list for the option to display.
    for (cmdopt_vector_iterator it = option_vec.begin();
	 it != option_vec.end(); ++it)
      {
	if ((*it)->accept(tokens[1]))
	{
	  option_found = true;
	  (*it)->handler(s, tokens, input);
	  break;
	}
      }
    if (!option_found)
      {
	cout << "Invalid option name" << endl;
	cout << usage << ": " << help_text(0) <<endl;
      }
    return false;
  }
};

class show_cmd: public cmdopt
{
public:
  show_cmd()
  {
    name = "show";
    usage = "show OPTION";
    _help_text = "Show option value.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens, string &input)
  {
    bool option_found = false;

    if (tokens.size() == 1)
      {
        // Show all options.
	for (cmdopt_vector_iterator it = option_vec.begin();
	     it != option_vec.end(); ++it)
	  {
	    (*it)->handler(s, tokens, input);
	  }
	return false;
      }
    else if (tokens.size() != 2)
      {
	cout << endl << "Invalid command" << endl;
        cout << usage << ": " << help_text(0) <<endl;
	return false;
      }

    // Search the option list for the option to display.
    for (cmdopt_vector_iterator it = option_vec.begin();
	 it != option_vec.end(); ++it)
      {
	if ((*it)->accept(tokens[1]))
	  {
	    option_found = true;
	    (*it)->handler(s, tokens, input);
	    break;
	  }
      }
    if (!option_found)
      {
	cout << "Invalid option name" << endl << "Use \"help set\" for a list of options." <<endl;
      }
    return false;
  }
};

class quit_cmd : public cmdopt
{
public:
  quit_cmd()
  {
    name = usage = "quit";
    aliases = { "q" };
    _help_text = "Quit systemtap.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens __attribute ((unused)),
	       string &input __attribute ((unused)))
  {
    return true;
  }
};

class add_cmd: public cmdopt
{
public:
  add_cmd()
  {
    name = usage = "add";
    aliases = { "a" };
    _help_text = "Add a global, probe, or function.";
  }
  bool handler(systemtap_session &s,
	       vector<string> &tokens __attribute ((unused)),
	       string &input)
  {
    vector<string> tmp_script_vec;

    // NB: At some point, we might store stap's parser output instead
    // of just a string.

    // Skip past the add command itself by removing the 1st token.
    tokens.erase(tokens.begin());
    if (tokens.size() == 0) {
      cout << name << ": " << _help_text << endl;
      return false;
    }

    // Find the "add" command text and skip past it.  Note that we're
    // using the "raw" (not tokenized) input, so that if someone was
    // trying to print "1    2", we preserve those embedded spaces.
    string::size_type add_pos = input.find("add");
    if (add_pos == string::npos)
	return false;			// shouldn't happen...
    string::size_type input_pos = input.find_first_not_of(" ", add_pos + 3);
    if (input_pos == string::npos)
	return false;			// shouldn't happen...

    // Add the rest of the line to the temporary script vector.
    string line = input.substr(input_pos);
    tmp_script_vec.push_back(line);

    // Try to parse the input. If it worked, we're done (since the
    // user entered an entire probe/function/global).
    int rc = forked_parse_pass(s, tmp_script_vec);
    if (rc != 0)
      {
	// If parsing above didn't work, we've either got a syntax
	// error or the user hasn't completed the function or probe
	// he's entering. So, prompt for more input.
	while (1)
	  {
	    char *response;
	    response = readline("stap>>> ");
	    if (response == NULL)		// EOF
	      {
		clog << endl;
		break;
	      }

	    // Try to parse the entire "add" command input.
	    tmp_script_vec.push_back(response);
	    int rc = forked_parse_pass(s, tmp_script_vec);
	    if (rc == 0)
	      break;
	  }
      }

    // Add the "add" command input to the script.
    script_vec.insert(script_vec.end(), tmp_script_vec.begin(),
		      tmp_script_vec.end());
    if (auto_analyze)
      (void)forked_semantic_pass(s, script_vec);
    return false;
  }
};

class delete_cmd: public cmdopt
{
public:
  delete_cmd()
  {
    name = "delete";
    aliases = { "d" };
    usage = "delete LINE_NUM_RANGE";
    _help_text = "Delete script line(s) by line numbers. To delete a single line, specify LINE_NUM_RANGE as \"10\". To delete a range of lines, specify LINE_NUM_RANGE as \"12-20\". To delete the entire script, give no argument.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    // FIXME 2: Unlike gdb, our numbers get rearranged after a
    // delete. Example:
    //
    //   stap> list
    //   1: probe begin { exit() }
    //   2: probe end { printf("end\n") }
    //   stap> delete 1
    //   stap> list
    //   1: probe end { printf("end\n") }
    //
    // We could fix this if we stored the numbers along with the
    // script lines.

    if (tokens.size() == 1)
      {
	if (query("Delete entire script? ", no_default))
	  {
	    // Delete all probes/functions.
	    script_vec.clear();
	  }
	return false;
      }
    // At this point, we've either got a single number or a range. So,
    // the max number of extra tokens we could have would be 3 (NUM1 -
    // NUM2). 
    else if (tokens.size() > 4)
      {
	cout << endl << "Invalid command" << endl;
	interactive_usage();
	return false;
      }

    // If we've got more than 1 token, smash them together.
    string line_number_range = tokens[1];
    if (tokens.size() == 3)
	line_number_range += tokens[2];
    if (tokens.size() == 4)
	line_number_range += tokens[3];

    // Look for the 1st number in the (potential) range.
    char *end;
    long val;
    const char *lnr = line_number_range.c_str();

    errno = 0;
    val = strtol (lnr, &end, 10);
    if (errno != 0 || (*end != '\0' && *end != '-') || val < 0)
      {
	cout << _("Invalid starting script line range value.") << endl;
	return false;
      }

    // Does this script line exist?
    size_t line_start = val - 1;
    if (line_start > script_vec.size())
      {
	cout << _F("No line %ld present in script.", val) << endl;
	return false;
      }
      
    size_t line_end = line_start;
    if (*end == '-')
      {
	errno = 0;
	char *start = end + 1;
	val = strtol (start, &end, 10);
	if (errno != 0 || *end != '\0' || val < 0)
	  {
	    cout << _("Invalid ending script line range value.") << endl;
	    return false;
	  }
	line_end = val;

	if (line_end > script_vec.size())
	  {
	    cout << _F("No line %lu present in script.",
		       (unsigned long)line_end) << endl;
	    return false;
	  }
	else if (line_end < line_start)
	  {
	    cout << _("Invalid script line range value - starting line is greater than ending line.") << endl;
	    return false;
	  }
      }

    // Delete script line(s).
    if (line_start == line_end)
      script_vec.erase(script_vec.begin() + line_start);
    else
      script_vec.erase(script_vec.begin() + line_start,
		       script_vec.begin() + line_end);
    if (auto_analyze)
      (void)forked_semantic_pass(s, script_vec);
    return false;
  }
};

class load_cmd : public cmdopt
{
public:
  load_cmd()
  {
    name = "load";
    usage = "load FILE";
    _help_text = "Load a script from a file into the current session.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    if (tokens.size() != 2)
      {
	cout << "FILE must be specified." << endl;
        cout << usage << ": " << help_text(0) <<endl;
	return false;
      }

    if (!script_vec.empty())
      {
        if (query("Script exists. Overwrite existing script? ", no_default))
          script_vec.clear();
        else
          return false;
      }

    // Originally, we called stap's parser here to read in the
    // script. However, doing so discards comments, preprocessor
    // directives, and rearranges the script. So, let's just read the
    // script as a series of strings.
    ifstream f(tokens[1].c_str(), ios::in);
    if (f.fail())
      {
	cout << endl
	     << _F("File '%s' couldn't be opened for reading.",
		   tokens[1].c_str()) << endl;
	return false;
      }
	
    string line;
    while (getline(f, line))
      {
	script_vec.push_back(line);
      }
    f.close();
    return false;
  }
};

class save_cmd : public cmdopt
{
public:
  save_cmd()
  {
    name = "save";
    usage = "save FILE";
    _help_text = "Save a script to a file from the current session.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    if (tokens.size() != 2)
      {
	cout << "FILE must be specified." << endl;
        cout << usage << ": " << help_text(0) <<endl;
	return false;
      }

    ofstream f(tokens[1].c_str(), ios::out);
    if (f.fail())
      {
	cout << endl
	     << _F("File '%s' couldn't be opened for writing.",
		   tokens[1].c_str()) << endl;
	return false;
      }

    string script;
    if (!script_vec.empty())
      script = join(script_vec, "\n");
    f << script << endl;
    f.close();
    return false;
  }
};

class run_cmd : public cmdopt
{
public:
  run_cmd()
  {
    name = usage = "run";
    aliases = { "r" };
    _help_text = "Run the current script.";
  }
  bool handler(systemtap_session &s,
	       vector<string> &tokens __attribute ((unused)),
	       string &input __attribute ((unused)))
  {
    int rc = 0;
    // logging treshold
    unsigned int lt = 2;
    if (script_vec.empty())
      {
	clog << "No script specified." << endl;
	return false;
      }

    // FIXME: there isn't any real point to calling
    // systemtap_session::clone() here, since it doesn't (usually)
    // create a new session, but returns a cached session. So, we'll
    // just use the current session.
    s.cmdline_script = join(script_vec, "\n");
    s.have_script = true;
    if (s.build_as != "")
      {
        // PR30321: Privilege separation
        // Fork the stap process in two:  An unprivileged child, and a privileged parent
        // Child will run passes 1-4 and part of pass 5 (up to preparing staprun cmdline
        // Parent will wait, spawn staprun (second part of pass 5), and finish.
        pid_t frkrc = fork();
        if (frkrc == -1)
        {
          clog << _("ERROR: Fork failed.  Terminating...") << endl;
          return EXIT_FAILURE;
        }
        else if (frkrc == 0)
        {
          // Child process (unprivileged)
          rc = run_unprivileged(s.build_as, s.build_as_uid, s.build_as_gid, s.verbose);
          if (rc != EXIT_SUCCESS)
            return rc;
          if (s.verbose >= lt)
            clog << _F("Child pid=%d, uid=%d, euid=%d, gid=%d, egid=%d\n",
                       getpid(), getuid(), geteuid(), getgid(), getegid());

          rc = forked_passes_0_4(s);
// #if 0
//     if (rc)
//     {
// 	// Compilation failed.
// 	// Try again using a server if appropriate.
// 	if (s.try_server ())
// 	    rc = passes_0_4_again_with_server (s);
//     }
// #endif
          if (rc || s.perpass_verbose[0] >= 1)
            s.explain_auto_options ();

          // Run pass 5, if passes 0-4 worked.
          if (rc == 0 && s.last_pass >= 5 && !pending_interrupts)
              rc = pass_5_1 (s, *saved_targets);
          _exit(rc);
        }
        else
        {
          // Parent process (privileged)
          if (s.verbose >= lt)
            clog << _F("Parent pid=%d, uid=%d, euid=%d, gid=%d, egid=%d\n",
                       getpid(), getuid(), geteuid(), getgid(), getegid());
          int wstatus;
          (void)waitpid(frkrc, &wstatus, 0);
          rc = WEXITSTATUS(wstatus);
          if (s.verbose >= lt)
              clog << _("Child finished.") << endl;
        }
        rc = pass_5_2 (s, *saved_targets);
      }
    else
      {
        // --build-as wasn't specified, no need to fork
        rc = forked_passes_0_4(s);
// #if 0
//     if (rc)
//     {
// 	// Compilation failed.
// 	// Try again using a server if appropriate.
// 	if (s.try_server ())
// 	    rc = passes_0_4_again_with_server (s);
//     }
// #endif
        if (rc || s.perpass_verbose[0] >= 1)
          s.explain_auto_options ();

        // Run pass 5, if passes 0-4 worked.
        if (rc == 0 && s.last_pass >= 5 && !pending_interrupts)
          {
            rc = pass_5_1 (s, *saved_targets);
            rc = pass_5_2 (s, *saved_targets);
          }
      }
    s.reset_tmp_dir();
    return false;
  }
};

class edit_cmd : public cmdopt
{
public:
  edit_cmd()
  {
    name = usage = "edit";
    aliases = { "e" };
    _help_text = "Edit the current script. Uses EDITOR environment variable contents as editor (or ex as default).";
  }
  bool handler(systemtap_session &s,
	       vector<string> &tokens __attribute ((unused)),
	       string &input __attribute ((unused)))
  {
    const char *editor;
    char temp_path[] = "/tmp/stapXXXXXX.stp";
    int fd;

    // Get EDITOR value.
    if ((editor = getenv ("EDITOR")) == NULL)
      editor = "/bin/ex";

    // Get a temporary file.
    fd = mkstemps(temp_path, 4);
    if (fd < 0)
      {
	cout << endl
	     << _F("Temporary file '%s' couldn't be opened: %s",
		   temp_path, strerror(errno)) << endl;
	return false;
      }

    // Write the script contents to the temporary file.
    if (!script_vec.empty())
      {
	string script = join(script_vec, "\n");
	if (write(fd, script.c_str(), script.length()) < 0)
	  {
	    cout << endl
		 << _F("Writing to temporary file '%s' failed: %s",
		       temp_path, strerror(errno)) << endl;
	    (void)close(fd);
	    (void)unlink(temp_path);
	    return false;
	  }
      }

    // Run the editor on the temporary file.
    vector<string> edit_cmd;
    edit_cmd.push_back(editor);
    edit_cmd.push_back(temp_path);
    if (stap_system(s.verbose, "edit", edit_cmd, false, false) != 0)
      {
	// Assume stap_system() reported an error.
	(void)close(fd);
	(void)unlink(temp_path);
	return false;
      }

    // Read the new script contents. First, rewind the fd.
    if (lseek(fd, 0, SEEK_SET) < 0)
      {
	cout << endl
	     << _F("Rewinding the temporary file fd failed: %s",
		   strerror(errno)) << endl;
	(void)close(fd);
	(void)unlink(temp_path);
	return false;
      }
    
    script_vec.clear();
    
    // Read the new file contents. We'd like to use C++ stream
    // operations here, but there isn't a easy way to convert a file
    // descriptor to a C++ stream.
    FILE *fp = fdopen(fd, "r");
    if (fp == NULL)
      {
	cout << endl
	     << _F("Converting the file descriptor to a stream failed: %s",
		   strerror(errno)) << endl;
	(void)close(fd);
	(void)unlink(temp_path);
	return false;
      }

    char line[2048];
    while (fgets(line, sizeof(line), fp) != NULL)
      {
	// Zap the ending '\n'.
	size_t len = strlen(line);
	if (line[len - 1] == '\n')
	  line[len - 1] = '\0';
	script_vec.push_back(line);
      }

    (void)fclose(fp);
    (void)unlink(temp_path);
    if (auto_analyze && !script_vec.empty())
      (void)forked_semantic_pass(s, script_vec);
    return false;
  }
};

#if HAVE_LIBSQLITE3
class sample_cmd: public cmdopt
{
public:
  sample_cmd()
  {
    name = "sample";
    usage = "sample STATEMENT";
    _help_text = "Search and load an example script using criteria specified in STATEMENT. STATEMENT can contain any number of case insensitive keywords seperated by OR, AND or NOT (case sensitive). Wildcards can be postfixed to any number of keywords. STATEMENT can contain anything supported by FTS full-text index queries. Example: sample socket AND tcp* OR disk NOT virtual";
  }
  bool handler (systemtap_session &s __attribute ((unused)),
                vector<string> &tokens __attribute ((unused)),
                string &input __attribute ((unused)))
  {
    if (tokens.size() == 1)
      {
        cout << usage << ": " << _help_text << endl;
        return false;
      }

    sqlite3* db;
    int rc;
    vector <metafile>  metalist;

    string dirstr = PKGDATADIR;
    dirstr.append("/examples/");

    rc = sqlite3_open((dirstr + "metadatabase.db").c_str(), &db);
    if (rc)
      {
	cout << "Error connecting to meta database. RC: " << rc << endl;
	return false;
      }

    string sqlstr = "SELECT name, title, description, path FROM metavirt WHERE metavirt MATCH ?;";
    sqlite3_stmt* sqlstmt;
    string tokenstring = tokens[1];
    for (unsigned it = 2; it < tokens.size(); it++)
      {
        tokenstring.append(" " + tokens[it]);
      }

    rc = sqlite3_prepare_v2(db, sqlstr.c_str(), sqlstr.size() + tokenstring.size(), &sqlstmt, NULL);
    if (rc)
      {
        cout << "Error preparing SQL statement. RC: " << rc << endl;
        return false;
      }

    rc = sqlite3_bind_text(sqlstmt, 1, tokenstring.c_str(), tokenstring.size(), 0);
    if (rc)
      {
        cout << "Error binding SQL statement. RC: " << rc << endl;
        return false;
      }

    metafile file;
    while (1)
      {
	rc = sqlite3_step(sqlstmt);
	if (rc == SQLITE_ROW)
	  {
	    file.name = (char*)sqlite3_column_text(sqlstmt,0);
            file.title = (char*)sqlite3_column_text(sqlstmt,1);
            file.description = (char*)sqlite3_column_text(sqlstmt,2);
            file.path = (char*)sqlite3_column_text(sqlstmt,3);
            metalist.push_back(file);
	  }
	else if (rc == SQLITE_DONE)
	  break;
	else
	  {
            cout << "Incomplete statement." << endl;
	    return false;
	  }
      }
    sqlite3_finalize(sqlstmt);

    if (metalist.size() == 0)
      {
        cout << "No sample scripts found using \"" << tokenstring << "\"." << endl;
	cout << "Note, AND, OR and NOT are case sensitive." << endl;
        return false;
      }

    char* response;
    metafile selection;
    while (selection.name == "")
      {
	cout << "Enter a script name for a description or use q to quit:" << endl;
        for (unsigned it=0; it < metalist.size();it++)
	  cout << "  " << metalist[it].name << ":  " << metalist[it].title <<  endl;

	response = readline(_F("%s","stap sample>> ").c_str());

        if (response == NULL) // EOF
          {
            cout << endl;
            return false;
          }
        if (*response == 'q')
          return false;

       for (unsigned i=0; i < metalist.size();i++)
	 {
	   if (metalist[i].name.find(response) != string::npos)
	     selection = metalist[i];
         }
         if (selection.name == "")
	   {
            cout << "Unknown selection." << endl;
	    continue;
           }
	 cout << endl << "Title: " << selection.title << endl << "Name: " << selection.name << endl;
	 cout << "Description: " << selection.description << endl;
         if (!query("Would you like to load this example? ", no_default))
	   {
	     selection.name = "";
	    if (!query("Would you like to select another related script? ", no_default))
	      return false;
           }
      }

    tokens.erase(tokens.begin() + 1, tokens.begin() + tokens.size());
    auto load = new load_cmd;
    tokens.push_back(dirstr + selection.path + "/" + selection.name);
    load -> handler(s, tokens, input);

    sqlite3_close(db);
    return false;
  }
};
#endif

//
// Supported options for the "set" and "show" commands.
// 

class keep_tmpdir_opt: public cmdopt
{
public:
  keep_tmpdir_opt()
  {
    name = "keep_tmpdir";
    _help_text = "Keep temporary directory.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.keep_tmpdir = (tokens[2] != "0");
    else
      cout << name << ": " << s.keep_tmpdir << endl;
    return false;
  }
};

class last_pass_opt: public cmdopt
{
public:
  last_pass_opt()
  {
      name = "last_pass";
      _help_text = "Stop after pass NUM 1-5.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      {
	char *end;
	long val;

	errno = 0;
	val = strtol (tokens[2].c_str(), &end, 10);
	if (errno != 0 || *end != '\0' || val < 1 || val > 5)
	  cout << "Invalid option value (should be 1-5)" << endl;
	else
	  s.last_pass = val;
      }
    else
      cout << name << ": " << s.last_pass << endl;
    return false;
  }
};

class verbose_opt: public cmdopt
{
public:
  verbose_opt()
  {
    name = "verbose";
    _help_text = "Add verbosity to all passes.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      {
	char *end;
	long val;

	errno = 0;
	val = strtol (tokens[2].c_str(), &end, 10);
	if (errno != 0 || *end != '\0' || val < 0)
	  cout << "Invalid option value (should be greater than 0)" << endl;
	else
	  {
	    s.verbose = val;
	    for (unsigned i=0; i<5; i++)
	      s.perpass_verbose[i] = val;
	  }
      }
    else
      cout << name << ": " << s.verbose << endl;
    return false;
  }
};

class guru_mode_opt: public cmdopt
{
public:
  guru_mode_opt()
  {
    name = "guru_mode";
    _help_text = "Guru mode.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.guru_mode = (tokens[2] != "0");
    else
      cout << name << ": " << s.guru_mode << endl;
    return false;
  }
};

class suppress_warnings_opt: public cmdopt
{
public:
  suppress_warnings_opt()
  {
    name = "suppress_warnings";
    _help_text = "Suppress warnings.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.suppress_warnings = (tokens[2] != "0");
    else
      cout << name << ": " << s.suppress_warnings << endl;
    return false;
  }
};


class panic_warnings_opt: public cmdopt
{
public:
  panic_warnings_opt()
  {
    name = "panic_warnings";
    _help_text = "Turn warnings into errors.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.panic_warnings = (tokens[2] != "0");
    else
      cout << name << ": " << s.panic_warnings << endl;
    return false;
  }
};

class timing_opt: public cmdopt
{
public:
  timing_opt()
  {
    name = "timing";
    _help_text = "Collect probe timing information.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.timing = (tokens[2] != "0");
    else
      cout << name << ": " << s.timing << endl;
    return false;
  }
};

class unoptimized_opt: public cmdopt
{
public:
  unoptimized_opt()
  {
    name = "unoptimized";
    _help_text = "Unoptimized translation.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      s.unoptimized = (tokens[2] != "0");
    else
      cout << name << ": " << s.unoptimized << endl;
    return false;
  }
};

class target_pid_opt: public cmdopt
{
public:
  target_pid_opt()
  {
    name = "target_pid";
    _help_text = "Sets target() to PID.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      {
	char *end;
	unsigned long val;

	if (s.cmd != "")
	  {
	    cerr << _("You can't specify a target pid and a cmd together.")
		 << endl;
	    return false;
	  }

	errno = 0;
	val = strtoul (tokens[2].c_str(), &end, 10);
	if (errno != 0 || *end != '\0' || val < 1 || val > 5)
	  cout << _("Invalid target process ID number.") << endl;
	else
	  s.target_pid = val;
      }
    else
      cout << name << ": " << s.target_pid << endl;
    return false;
  }
};

class cmd_opt: public cmdopt
{
public:
  cmd_opt()
  {
    name = "cmd";
    _help_text = "Start the probes, run CMD, and exit when it finishes.";
  }
  bool handler(systemtap_session &s, vector<string> &tokens,
	       string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      {
	if (s.target_pid != 0)
	  {
	    cerr << _("You can't specify a target pid and a cmd together.")
		 << endl;
	    return false;
	  }

	s.cmd.clear();
	for (unsigned i = 2; i < tokens.size(); ++i)
	  {
	    // Paste the tokens back together.
	    if (!s.cmd.empty())
	      s.cmd += " ";
	    s.cmd += tokens[i];
	  }
	// If the string is quoted, remove the outer quotes.
	if ((s.cmd[0] == '"' || s.cmd[0] == '\'')
	    && s.cmd[0] == s.cmd[s.cmd.size() - 1])
	  {
	    s.cmd.erase(0, 1);
	    s.cmd.erase(s.cmd.size() - 1, 1);
	  }
      }
    else
      cout << name << ": \"" << s.cmd << "\"" << endl;
    return false;
  }
};

class auto_analyze_opt: public cmdopt
{
public:
  auto_analyze_opt()
  {
    name = "auto_analyze";
    _help_text = "Automatically analyze the current script after changes.";
  }
  bool handler(systemtap_session &s __attribute ((unused)),
	       vector<string> &tokens, string &input __attribute ((unused)))
  {
    bool set = (tokens[0] == "set");
    if (set)
      auto_analyze = (tokens[2] != "0");
    else
      cout << name << ": " << auto_analyze << endl;
    return false;
  }
};

static void
interactive_usage ()
{
  cout << "List of commands:" << endl << endl;

  // Find biggest "usage" field.
  size_t width = 1;
  for (cmdopt_vector_const_iterator it = command_vec.begin();
       it != command_vec.end(); ++it)
    {
      if ((*it)->usage.size() > width)
	  width = (*it)->usage.size();
    }
  // Print usage field and help text for each command.
  for (cmdopt_vector_const_iterator it = command_vec.begin();
       it != command_vec.end(); ++it)
    {
      cout << setw(width) << left << (*it)->usage << " -- "
	   << (*it)->help_text(width + 4) << endl;
    }
}

// Generator function for command completion.  STATE lets us know
// whether to start from scratch; without any state (i.e. STATE == 0),
// then we start at the top of the list.
static char *
command_generator(const char *text, int state)
{
  static size_t list_index, len;

  // If this is a new word to complete, initialize now.  This includes
  // saving the length of TEXT for efficiency, and initializing the
  // index variable to 0.
  if (!state)
  {
    list_index = 0;
    len = strlen(text);
  }

  // Return the next name which partially matches from the command list.
  while (list_index < command_vec.size())
  {
    cmdopt *cmd = command_vec[list_index];
    list_index++;
    if (strncmp(cmd->name.c_str(), text, len) == 0)
      return strdup(cmd->name.c_str());
  }

  // If no names matched, then return NULL.
  return NULL;
}

// Generator function for option completion.  STATE lets us know
// whether to start from scratch; without any state (i.e. STATE == 0),
// then we start at the top of the list.
static char *
option_generator(const char *text, int state)
{
  static size_t list_index, len;

  // If this is a new word to complete, initialize now.  This includes
  // saving the length of TEXT for efficiency, and initializing the
  // index variable to 0.
  if (!state)
  {
    list_index = 0;
    len = strlen(text);
  }

  // Return the next name which partially matches from the option list.
  while (list_index < option_vec.size())
  {
    cmdopt *opt = option_vec[list_index];
    list_index++;
    if (strncmp(opt->name.c_str(), text, len) == 0)
      return strdup(opt->name.c_str());
  }

  // If no names matched, then return NULL.
  return NULL;
}

#ifdef DEBUG
// An iterative process to traverse the parse tree, looking for
// partial matches.
static void
partial_matches(const char *text, match_item_map &map, vector<string> &matches)
{
    // Create an empty stack and push items to it that paritally match.
    std::stack< pair<string, match_item *> > stack;
    for (match_item_map_const_rev_iterator rit = map.rbegin();
	 rit != map.rend(); ++rit)
    {
	if (rit->second->partial_match(text))
	    stack.push(make_pair(rit->first, rit->second));
    }

    // Handle the stack.
    while (stack.empty() == false)
    {
	// Pop the top item from stack and handle it
	match_item *item = stack.top().second;
	string prefix = stack.top().first;
	stack.pop();

	if (item->terminal)
	    matches.push_back(prefix);
 
	// Push all children.
	for (match_item_map_const_rev_iterator rit = item->sub_matches.rbegin();
	     rit != item->sub_matches.rend(); ++rit)
	{
	    match_item *item = rit->second;
	    stack.push(make_pair(prefix + "." + rit->first, item));
	}
    }
}
#endif

static char *
probe_generator(const char *text, int state)
{
  static std::stack< pair<string, match_item *> > stack;

  // If this is a new word to complete, initialize everything we need.
  if (!state)
  {
    // OK, so this is the first time we're trying to expand this
    // word. We only get the last "word", but we need to know where we
    // are in the probe expansion. For example, is someone trying to
    // expand "ker", "fun" from "kernel.fun", or "re" from
    // "kernel.function("sys_foo").re"?
    //
    // We're going to "cheat" here, and reuse the 2nd token of the
    // line from where interactive_completion() saved it for us. We're
    // going to break down the 2nd token into its components.
    vector<string> tokens;
    tokenize(saved_token, tokens, ".");
	
    match_item_map *match_map = &probe_map;
    for (vector<string>::const_iterator it = tokens.begin();
	 it != tokens.end(); ++it)
    {
	bool found = false;
	for (match_item_map_const_iterator map_it = match_map->begin();
	     map_it != match_map->end(); ++map_it)
	{
	    if (map_it->second->full_match(*it))
	    {
		found = true;
		match_map = &(map_it->second->sub_matches);
		break;
	    }
	}
	if (! found)
	    break;
    }

    // Clean out the stack from a previous run. This shouldn't happen,
    // but let's be sure.
    while (stack.empty() == false)
    {
	stack.pop();
    }

    // Now we're at the right match_item sub_matches map. Process it by
    // pushing items to the stack that paritally match.
    for (match_item_map_const_rev_iterator rit = match_map->rbegin();
	 rit != match_map->rend(); ++rit)
    {
	if (rit->second->partial_match(text))
	    stack.push(make_pair(rit->first, rit->second));
    }
  }

  // Handle the stack.
  while (stack.empty() == false)
  {
      // Pop the top item from stack and handle it
      match_item *item = stack.top().second;
      string prefix = stack.top().first;
      stack.pop();

      // Push all children.
      for (match_item_map_const_rev_iterator rit = item->sub_matches.rbegin();
	   rit != item->sub_matches.rend(); ++rit)
      {
	  match_item *item = rit->second;
	  stack.push(make_pair(prefix + "." + rit->first, item));
      }

      // If this item is terminal, return it.
      if (item->terminal)
	  return strdup(prefix.c_str());
  }

  // If no names matched, then return NULL.
  return NULL;
}

// Attempt to complete on the contents of TEXT.  START and END bound
// the region of rl_line_buffer that contains the word to complete.
// TEXT is the word to complete.  We can use the entire contents of
// rl_line_buffer in case we want to do some simple parsing.  Return
// the array of matches, or NULL if there aren't any.
static char **
interactive_completion(const char *text, int start,
		       int end __attribute ((unused)))
{
  char **matches = (char **)NULL;

  // Setting 'rl_attempted_completion_over' to non-zero means to
  // suppress normal filename completion after the user-specified
  // completion function has been called.
  rl_attempted_completion_over = 1;

  // If this word is at the start of the line, then it is a command to
  // complete.
  if (start == 0)
    matches = rl_completion_matches(text, command_generator);
  else
  {
    const string delimiters = " \t";
    vector<string> tokens;

    tokenize(rl_line_buffer, tokens, delimiters);
    if (! tokens.size())
      return matches;

    if (tokens.size() <= 2)
    {
      // If we're in a command that takes options, then we've got an
      // option to complete, if we're on the 2nd token.
      for (cmdopt_vector_const_iterator it = option_command_vec.begin();
	   it != option_command_vec.end(); ++it)
      {
	if ((*it)->accept(tokens[0]))
	{
	  matches = rl_completion_matches(text, option_generator);
	  return matches;
	}
      }
    }

    // If we're in an "add" command...
    if (tokens.size() >= 2 && tokens[0] == "add")
    {
      // Perhaps we're in a probe declaration.
      if (tokens[1] == "probe")
      {
	// Save (possible) 2nd token for use by probe_generator().
	if (tokens.size() >= 3)
	  saved_token = tokens[2];
	else
	  saved_token = "";
	matches = rl_completion_matches(text, probe_generator);
      }
    }
    else if (tokens.size() == 2
	     && (tokens[0] == "load" || tokens[0] == "save"))
    {
      // Since the "load" and "save" commands *do* take a filename,
      // turn filename completion back on.
      rl_attempted_completion_over = 0;
    }
  }
  return matches;
}

static void
process_probe_list(istream &probe_stream, bool handle_regexps)
{
    while (! probe_stream.eof())
    {
	string line;
	match_item_map *match_map = &probe_map;
	size_t space_pos;

	getline(probe_stream, line);
	if (line.empty())
	    continue;
	
	// Delete from a space to the end. Probe aliases look like:
	//
	//    syscall.write = kernel.function("sys_write)
	space_pos = line.find(" ");
	if (space_pos != string::npos)
	    line.erase(space_pos);

	vector<string> tokens;
	tokenize(line, tokens, ".");

#ifdef DEBUG
	clog << "processing " << line << endl;
#endif
	for (vector<string>::const_iterator it = tokens.begin();
	     it != tokens.end(); ++it)
	{
	    if (match_map->count(*it) == 0)
	    {
		match_item *mi = new match_item;
		size_t number_pos = string::npos;
		size_t string_pos = string::npos;
	      
		if (handle_regexps)
		{
		    number_pos = (*it).find("(number)");
		    string_pos = (*it).find("(string)");
		}
		if (number_pos != string::npos)
		{
		    mi->match_text = (*it).substr(0, number_pos);
		    mi->regexp = "^\\([x0-9a-fA-F]+\\)$";
		}
		else if (string_pos != string::npos)
		{
		    mi->match_text = (*it).substr(0, string_pos);
		    mi->regexp = "^\\(\"[^\"]+\"\\)$";
		}
		else
		{
		    mi->match_text = (*it);
		}
		(*match_map)[*it] = mi;
		match_map = &(mi->sub_matches);
		mi->terminal = (*it == tokens.back());
	    }
	    else
	    {
		match_map = &((*match_map)[*it]->sub_matches);
	    }
	}
    }
}

// Fork a new process for the dirty work
static int
forked_passes_0_4 (systemtap_session &s)
{
  stringstream ss;
  pair<bool,int> ret = stap_fork_read(s.perpass_verbose[0], ss);

  if (ret.first) // Child fork
    {
      int rc = 1;
      try
        {
          rc = passes_0_4 (s);
          stdio_filebuf<char> buf(ret.second, ios_base::out);
          ostream o(&buf);
          if (rc == 0 && s.last_pass > 4)
            {
              o << s.module_name << endl;
              o << s.uprobes_path << endl;
            }
          o.flush();
        }
      catch (...)
        {
          // NB: no cleanup from the fork!
        }
      // FIXME: what about cleanup(), but only for this session?
      // i.e. tapset coverage, report_suppression, but not subsessions.
      exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
    }

  // For passes <= 4, everything was written to cout.
  // For pass 5, we need the module and maybe uprobes for staprun.
  if (s.last_pass > 4 && ret.second == 0)
    ss >> s.module_name >> s.uprobes_path;

  return ret.second;
}

static int
forked_parse_pass (systemtap_session &s, vector<string> &script)
{
  // Save current session info.
  int saved_last_pass = s.last_pass;
  unsigned saved_verbose = s.verbose;
  unsigned saved_perpass_verbose[5];
  for (unsigned i=0; i<5; i++)
    saved_perpass_verbose[i] = s.perpass_verbose[i];
  bool saved_monitor = s.monitor;
    
  // Set up session to only run pass 1.
  s.last_pass = 1;
  s.verbose = 0;
  for (unsigned i=0; i<5; i++)
    s.perpass_verbose[i] = 0;
  s.monitor = false;

  // Add the script
  s.cmdline_script = join(script, "\n");
  s.have_script = true;

  // We don't want to see any of the parser output, which normally
  // goes to 'cout' and 'cerr'. So, redirect where 'cout' and 'cerr'
  // go, run the command, then restore 'cout' and 'cerr'.
  //
  // Why don't we want to see the parser output? The user may have
  // just entered "function foo() {" and intends to continue adding
  // more lines to the function.
  ofstream file("/dev/null");
  streambuf *former_cout = cout.rdbuf(file.rdbuf());
  streambuf *former_cerr = cerr.rdbuf(file.rdbuf());
  int rc = forked_passes_0_4 (s);
  cout.rdbuf(former_cout);
  cerr.rdbuf(former_cerr);
  file.close();

  // Restore the session values we changed.
  s.last_pass = saved_last_pass;
  s.verbose = saved_verbose;
  for (unsigned i=0; i<5; i++)
    s.perpass_verbose[i] = saved_perpass_verbose[i];
  s.monitor = saved_monitor;

  return rc;
}

static int
forked_semantic_pass (systemtap_session &s, vector<string> &script)
{
  clog << _("Script analysis:") << endl;

  // Save current session info.
  int saved_last_pass = s.last_pass;
  unsigned saved_verbose = s.verbose;
  unsigned saved_perpass_verbose[5];
  for (unsigned i=0; i<5; i++)
    saved_perpass_verbose[i] = s.perpass_verbose[i];
  bool saved_monitor = s.monitor;
    
  // Set up session to only run through pass 2.
  s.last_pass = 2;
  s.verbose = 0;
  for (unsigned i=0; i<5; i++)
    s.perpass_verbose[i] = 0;
  s.monitor = false;

  // Add the script
  s.cmdline_script = join(script, "\n");
  s.have_script = true;

  // We want the output, so we won't bother with redirection.
  int rc = forked_passes_0_4 (s);

  // Restore the session values we changed.
  s.last_pass = saved_last_pass;
  s.verbose = saved_verbose;
  for (unsigned i=0; i<5; i++)
    s.perpass_verbose[i] = saved_perpass_verbose[i];
  s.monitor = saved_monitor;

  return rc;
}

//
// Interactive mode, passes 0 through 5 and back again.
//

int
interactive_mode (systemtap_session &s, vector<remote*> targets)
{
  string delimiters = " \t";
  bool input_handled;

  // Save the target vector.
  saved_targets = &targets;

  // Tell readline's completer we want a crack at the input first.
  rl_attempted_completion_function = interactive_completion;

  // FIXME: this has been massively simplified from the default.
  rl_completer_word_break_characters = (char *)" \t\n.{";

  // Set up command list, along with a list of commands that take
  // options.
  command_vec.push_back(new add_cmd);
  command_vec.push_back(new delete_cmd);
  command_vec.push_back(new list_cmd);
  command_vec.push_back(new edit_cmd);
  command_vec.push_back(new load_cmd);
  command_vec.push_back(new save_cmd);
  command_vec.push_back(new run_cmd);
  #if HAVE_LIBSQLITE3
    command_vec.push_back(new sample_cmd);
  #endif
  command_vec.push_back(new set_cmd);
  option_command_vec.push_back(command_vec.back());
  command_vec.push_back(new show_cmd);
  option_command_vec.push_back(command_vec.back());
  command_vec.push_back(new help_cmd);
  command_vec.push_back(new quit_cmd);

  // Set up set/show option list.
  option_vec.push_back(new keep_tmpdir_opt);
  option_vec.push_back(new last_pass_opt);
  option_vec.push_back(new verbose_opt);
  option_vec.push_back(new guru_mode_opt);
  option_vec.push_back(new suppress_warnings_opt);
  option_vec.push_back(new panic_warnings_opt);
  option_vec.push_back(new timing_opt);
  option_vec.push_back(new unoptimized_opt);
  option_vec.push_back(new target_pid_opt);
  option_vec.push_back(new cmd_opt);
  option_vec.push_back(new auto_analyze_opt);

  // Feed provided script into interactive buffer
  if (!s.script_file.empty())
    {
      ifstream ifs(s.script_file);
      string line;
      while (getline(ifs, line))
        script_vec.push_back(line);
    }
  else if (!s.cmdline_script.empty())
    script_vec.push_back(s.cmdline_script);
  for (auto it = s.additional_scripts.begin();
       it != s.additional_scripts.end(); ++it)
    script_vec.push_back(*it);
  s.script_file.clear();
  s.cmdline_script.clear();
  s.additional_scripts.clear();

  // FIXME: It might be better to wait to get the list of probes and
  // aliases until they are needed.

  // Save the original state of the session object.
  unsigned saved_verbose = s.verbose;
  unsigned saved_perpass_verbose[5];
  for (unsigned i=0; i<5; i++)
      saved_perpass_verbose[i] = s.perpass_verbose[i];
  int saved_last_pass = s.last_pass;
  bool saved_monitor = s.monitor;

#if HAVE_NSS
  // If requested, query server status. This is independent
  // of other tasks.
  nss_client_query_server_status (s);

  // If requested, manage trust of servers. This is
  // independent of other tasks.
  nss_client_manage_server_trust (s);
#endif
  s.init_try_server ();

  // Get the list of "base" probe types, the same output you'd get
  // from doing 'stap --dump-probe-types'.
  s.verbose = 0;
  for (unsigned i=0; i<5; i++)
      s.perpass_verbose[i] = 0;
  s.dump_mode = systemtap_session::dump_probe_types;
  s.last_pass = 2;
  s.monitor = false;

  // We want to capture the probe output, which normally goes to
  // 'cout'. So, redirect where 'cout' goes, run the command, then
  // restore 'cout'.
  stringstream probes;
  streambuf *former_buff = cout.rdbuf(probes.rdbuf());
  passes_0_4(s);
  cout.rdbuf(former_buff);

  // Now that we have the list of "base" probe types, call
  // process_probe_list() to turn that into our parse tree.
  process_probe_list(probes, true);

  // FIXME: It might be nice instead of completing to:
  //    process(number).function(string)
  // instead we did:
  //    process(PID).function("NAME")
  // i.e. the '(number)' and '(string)' fields were more descriptive.

  // Now we'll need to get all the probe aliases ("stap
  // --dump-probe-aliases").
  s.dump_mode = systemtap_session::dump_probe_aliases;
  
  // We want to capture the alias output, which normally goes to
  // 'cout'. So, redirect where 'cout' goes, run the command, then
  // restore 'cout'.
  stringstream aliases;
  former_buff = cout.rdbuf(aliases.rdbuf());
  passes_0_4(s);
  cout.rdbuf(former_buff);

  // Process the list of probe aliases.
  process_probe_list(aliases, false);

  // FIXME: We could also complete systemtap function names.

  // Restore the original state of the session object.
  s.dump_mode = systemtap_session::dump_none;
  s.verbose = saved_verbose;
  for (unsigned i=0; i<5; i++)
      s.perpass_verbose[i] = saved_perpass_verbose[i];
  s.last_pass = saved_last_pass;
  s.monitor = saved_monitor;
  s.clear_script_data();

#ifdef DEBUG
  {
      vector<string> matches;

      // Print tree.
      clog << "Dumping tree:" << endl;
      partial_matches("", probe_map, matches);
      for (vector<string>::const_iterator it = matches.begin();
	   it != matches.end(); ++it)
      {
	  clog << (*it) << endl;
      }
  }
#endif
  cout << "Example commands: 'sample', 'add', 'run', 'help'" << endl;
  while (1)
    {
      // PR19847: clear any pending interrupts from previous commands
      pending_interrupts = 0;

      char *line_tmp = readline("stap> ");
      if (line_tmp == NULL)		// C-d
        {
	  clog << endl;
	  if (query(_("Quit? "), default_yes))
	    break;
	  continue;
	}
      else if (*line_tmp)
	add_history(line_tmp);
      else
	{
	  cout << "Example commands: 'sample', 'add', 'run', 'help'" << endl;
          continue;
        }

      string line = string(line_tmp);
      free(line_tmp);

      vector<string> tokens;
      tokenize(line, tokens, delimiters);

      input_handled = false;
      if (tokens.size())
        {
	  bool quit = false;
	  // Search list for command to execute.
	  for (cmdopt_vector_iterator it = command_vec.begin();
	       it != command_vec.end(); ++it)
	    {
	      if ((*it)->accept(tokens[0]))
	        {
		  input_handled = true;
		  quit = (*it)->handler(s, tokens, line);
		  break;
		}
	    }
	    
	  if (input_handled && quit)
	    break;
	}

      // If it isn't a command, complain.
      if (!input_handled)
	clog << "Undefined command: \"" << tokens[0]
	     << "\". Try \"help\" or \"?\"." << endl;
    }
  delete_match_map_items(&probe_map);
  return 0;
}