-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.cxx
2007 lines (1805 loc) · 51.2 KB
/
interactive.cxx
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
// 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>
#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 (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)))
{
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;
int 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 (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";
int fd;
// Get EDITOR value.
if ((editor = getenv ("EDITOR")) == NULL)
editor = "/bin/ex";
// Get a temporary file.
fd = mkstemp(temp_path);
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;