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
|
// systemtap translator/driver
// Copyright (C) 2005-2009 Red Hat Inc.
// Copyright (C) 2005 IBM Corp.
// Copyright (C) 2006 Intel Corporation.
//
// 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 "staptree.h"
#include "parse.h"
#include "elaborate.h"
#include "translate.h"
#include "buildrun.h"
#include "session.h"
#include "hash.h"
#include "cache.h"
#include "util.h"
#include "coveragedb.h"
#include "git_version.h"
#include "rpm_finder.h"
#include "task_finder.h"
#include "sys/sdt.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cerrno>
#include <cstdlib>
#include <limits.h>
extern "C" {
#include <glob.h>
#include <unistd.h>
#include <signal.h>
#include <sys/utsname.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include <elfutils/libdwfl.h>
#include <getopt.h>
}
using namespace std;
#define PATH_TBD string("__TBD__")
void
version ()
{
clog
<< "SystemTap translator/driver "
<< "(version " << VERSION << "/" << dwfl_version (NULL)
<< " " << GIT_MESSAGE << ")" << endl
<< "Copyright (C) 2005-2009 Red Hat, Inc. and others" << endl
<< "This is free software; see the source for copying conditions." << endl;
}
void
usage (systemtap_session& s, int exitcode)
{
version ();
clog
<< endl
<< "Usage: stap [options] FILE Run script in file."
<< endl
<< " or: stap [options] - Run script on stdin."
<< endl
<< " or: stap [options] -e SCRIPT Run given script."
<< endl
<< " or: stap [options] -l PROBE List matching probes."
<< endl
<< " or: stap [options] -L PROBE List matching probes and local variables."
<< endl
<< endl
<< "Options:" << endl
<< " -- end of translator options, script options follow" << endl
<< " -h show help" << endl
<< " -V show version" << endl
<< " -p NUM stop after pass NUM 1-5, instead of " << s.last_pass << endl
<< " (parse, elaborate, translate, compile, run)" << endl
<< " -v add verbosity to all passes" << endl
<< " --vp {N}+ add per-pass verbosity [";
for (unsigned i=0; i<5; i++)
clog << (s.perpass_verbose[i] <= 9 ? s.perpass_verbose[i] : 9);
clog
<< "]" << endl
<< " -k keep temporary directory" << endl
<< " -u unoptimized translation" << (s.unoptimized ? " [set]" : "") << endl
<< " -w suppress warnings" << (s.suppress_warnings ? " [set]" : "") << endl
<< " -g guru mode" << (s.guru_mode ? " [set]" : "") << endl
<< " -P prologue-searching for function probes"
<< (s.prologue_searching ? " [set]" : "") << endl
<< " -b bulk (percpu file) mode" << (s.bulk_mode ? " [set]" : "") << endl
<< " -s NUM buffer size in megabytes, instead of " << s.buffer_size << endl
<< " -I DIR look in DIR for additional .stp script files";
if (s.include_path.size() == 0)
clog << endl;
else
clog << ", in addition to" << endl;
for (unsigned i=0; i<s.include_path.size(); i++)
clog << " " << s.include_path[i] << endl;
clog
<< " -D NM=VAL emit macro definition into generated C code" << endl
<< " -B NM=VAL pass option to kbuild make" << endl
<< " -R DIR look in DIR for runtime, instead of" << endl
<< " " << s.runtime_path << endl
<< " -r DIR cross-compile to kernel with given build tree; or else" << endl
<< " -r RELEASE cross-compile to kernel /lib/modules/RELEASE/build, instead of" << endl
<< " " << s.kernel_build_tree << endl
<< " -a ARCH cross-compile to given architecture, instead of " << s.architecture << endl
<< " -m MODULE set probe module name, instead of " << endl
<< " " << s.module_name << endl
<< " -o FILE send script output to file, instead of stdout. This supports" << endl
<< " strftime(3) formats for FILE" << endl
<< " -c CMD start the probes, run CMD, and exit when it finishes" << endl
<< " -x PID sets target() to PID" << endl
<< " -F run as on-file flight recorder with -o." << endl
<< " run as on-memory flight recorder without -o." << endl
<< " -S size[,n] set maximum of the size and the number of files." << endl
<< " -d OBJECT add unwind/symbol data for OBJECT file";
if (s.unwindsym_modules.size() == 0)
clog << endl;
else
clog << ", in addition to" << endl;
{
vector<string> syms (s.unwindsym_modules.begin(), s.unwindsym_modules.end());
for (unsigned i=0; i<syms.size(); i++)
clog << " " << syms[i] << endl;
}
clog
<< " -t collect probe timing information" << endl
#ifdef HAVE_LIBSQLITE3
<< " -q generate information on tapset coverage" << endl
#endif /* HAVE_LIBSQLITE3 */
<< " --unprivileged" << endl
<< " restrict usage to features available to unprivileged users" << endl
#if 0 /* PR6864: disable temporarily; should merge with -d somehow */
<< " --kelf make do with symbol table from vmlinux" << endl
<< " --kmap[=FILE]" << endl
<< " make do with symbol table from nm listing" << endl
#endif
// Formerly present --ignore-{vmlinux,dwarf} options are for testsuite use
// only, and don't belong in the eyesight of a plain user.
<< " --skip-badvars" << endl
<< " overlook context of bad $ variables" << endl
<< endl
;
exit (exitcode);
}
static void
printscript(systemtap_session& s, ostream& o)
{
if (s.listing_mode)
{
// We go through some heroic measures to produce clean output.
set<string> seen;
for (unsigned i=0; i<s.probes.size(); i++)
{
if (pending_interrupts) return;
derived_probe* p = s.probes[i];
// NB: p->basest() is not so interesting;
// p->almost_basest() doesn't quite work, so ...
vector<probe*> chain;
p->collect_derivation_chain (chain);
probe* second = (chain.size()>1) ? chain[chain.size()-2] : chain[0];
#if 0
cerr << "\tchain[" << chain.size() << "]:" << endl;
for (unsigned i=0; i<chain.size(); i++)
{ cerr << "\t"; chain[i]->printsig(cerr); cerr << endl; }
#endif
stringstream tmps;
const probe_alias *a = second->get_alias();
if (a)
{
assert (a->alias_names.size() >= 1);
a->alias_names[0]->print(tmps); // XXX: [0] is arbitrary; perhaps print all
}
else
{
assert (second->locations.size() >= 1);
second->locations[0]->print(tmps); // XXX: [0] is less arbitrary here, but still ...
}
string pp = tmps.str();
// Now duplicate-eliminate. An alias may have expanded to
// several actual derived probe points, but we only want to
// print the alias head name once.
if (seen.find (pp) == seen.end())
{
o << pp;
// Print the locals for -L mode only
if (s.listing_mode_vars)
{
for (unsigned j=0; j<p->locals.size(); j++)
{
o << " ";
vardecl* v = p->locals[j];
v->printsig (o);
}
// Print arguments of probe if there
p->printargs(o);
}
o << endl;
seen.insert (pp);
}
}
}
else
{
if (s.embeds.size() > 0)
o << "# global embedded code" << endl;
for (unsigned i=0; i<s.embeds.size(); i++)
{
if (pending_interrupts) return;
embeddedcode* ec = s.embeds[i];
ec->print (o);
o << endl;
}
if (s.globals.size() > 0)
o << "# globals" << endl;
for (unsigned i=0; i<s.globals.size(); i++)
{
if (pending_interrupts) return;
vardecl* v = s.globals[i];
v->printsig (o);
if (s.verbose && v->init)
{
o << " = ";
v->init->print(o);
}
o << endl;
}
if (s.functions.size() > 0)
o << "# functions" << endl;
for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
{
if (pending_interrupts) return;
functiondecl* f = it->second;
f->printsig (o);
o << endl;
if (f->locals.size() > 0)
o << " # locals" << endl;
for (unsigned j=0; j<f->locals.size(); j++)
{
vardecl* v = f->locals[j];
o << " ";
v->printsig (o);
o << endl;
}
if (s.verbose)
{
f->body->print (o);
o << endl;
}
}
if (s.probes.size() > 0)
o << "# probes" << endl;
for (unsigned i=0; i<s.probes.size(); i++)
{
if (pending_interrupts) return;
derived_probe* p = s.probes[i];
p->printsig (o);
o << endl;
if (p->locals.size() > 0)
o << " # locals" << endl;
for (unsigned j=0; j<p->locals.size(); j++)
{
vardecl* v = p->locals[j];
o << " ";
v->printsig (o);
o << endl;
}
if (s.verbose)
{
p->body->print (o);
o << endl;
}
}
}
}
int pending_interrupts;
extern "C"
void handle_interrupt (int sig)
{
kill_stap_spawn(sig);
pending_interrupts ++;
if (pending_interrupts > 1) // XXX: should be configurable? time-based?
{
char msg[] = "Too many interrupts received, exiting.\n";
int rc = write (2, msg, sizeof(msg)-1);
if (rc) {/* Do nothing; we don't care if our last gasp went out. */ ;}
_exit (1);
}
}
void
setup_signals (sighandler_t handler)
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset (&sa.sa_mask);
if (handler != SIG_IGN)
{
sigaddset (&sa.sa_mask, SIGHUP);
sigaddset (&sa.sa_mask, SIGPIPE);
sigaddset (&sa.sa_mask, SIGINT);
sigaddset (&sa.sa_mask, SIGTERM);
}
sa.sa_flags = SA_RESTART;
sigaction (SIGHUP, &sa, NULL);
sigaction (SIGPIPE, &sa, NULL);
sigaction (SIGINT, &sa, NULL);
sigaction (SIGTERM, &sa, NULL);
}
void
setup_kernel_release (systemtap_session &s, const char* kstr) {
if (kstr[0] == '/') // fully specified path
{
s.kernel_build_tree = kstr;
string version_file_name = s.kernel_build_tree + "/include/config/kernel.release";
// The file include/config/kernel.release within the
// build tree is used to pull out the version information
ifstream version_file (version_file_name.c_str());
if (version_file.fail ())
{
cerr << "Missing " << version_file_name << endl;
exit(1);
}
else
{
char c;
s.kernel_release = "";
while (version_file.get(c) && c != '\n')
s.kernel_release.push_back(c);
}
}
else
{
s.kernel_release = string (kstr);
s.kernel_build_tree = "/lib/modules/" + s.kernel_release + "/build";
}
}
static void
checkOptions (systemtap_session &s)
{
bool optionsConflict = false;
if((s.cmd != "") && (s.target_pid))
{
cerr << "You can't specify -c and -x options together." <<endl;
optionsConflict = true;
}
if (s.unprivileged)
{
if (s.guru_mode)
{
cerr << "You can't specify -g and --unprivileged together." << endl;
optionsConflict = true;
}
if (s.include_path.size () > 1)
{
cerr << "You can't specify -I and --unprivileged together." << endl;
optionsConflict = true;
}
if (s.runtime_path != string(PKGDATADIR) + "/runtime")
{
cerr << "You can't use -R to specify an alternate runtime path when --unprivileged is specified." << endl;
optionsConflict = true;
}
if (s.kernel_build_tree.substr(0, 13) != "/lib/modules/")
{
cerr << "You can't use -r to specify a kernel release which is not installed when --unprivileged is specified." << endl;
optionsConflict = true;
}
if (! s.macros.empty ())
{
cerr << "You can't specify -D and --unprivileged together." << endl;
optionsConflict = true;
}
if (getenv ("SYSTEMTAP_TAPSET"))
{
cerr << "The environment variable SYSTEMTAP_TAPSET can not be defined when --unprivileged is specified." << endl;
optionsConflict = true;
}
if (getenv ("SYSTEMTAP_RUNTIME"))
{
cerr << "The environment variable SYSTEMTAP_RUNTIME can not be defined when --unprivileged is specified." << endl;
optionsConflict = true;
}
if (getenv ("SYSTEMTAP_DEBUGINFO_PATH"))
{
cerr << "The environment variable SYSTEMTAP_DEBUGINFO_PATH can not be defined when --unprivileged is specified." << endl;
optionsConflict = true;
}
}
if (!s.kernel_symtab_path.empty())
{
if (s.consult_symtab)
{
cerr << "You can't specify --kelf and --kmap together." << endl;
optionsConflict = true;
}
s.consult_symtab = true;
if (s.kernel_symtab_path == PATH_TBD)
s.kernel_symtab_path = string("/boot/System.map-") + s.kernel_release;
}
if (optionsConflict)
usage (s, 1);
}
int
main (int argc, char * const argv [])
{
string cmdline_script; // -e PROGRAM
string script_file; // FILE
bool have_script = false;
bool save_module = false;
// Initialize defaults
systemtap_session s;
struct utsname buf;
(void) uname (& buf);
s.kernel_release = string (buf.release);
s.kernel_build_tree = "/lib/modules/" + s.kernel_release + "/build";
// PR4186: Copy logic from coreutils uname (uname -i) to squash
// i?86->i386. Actually, copy logic from linux top-level Makefile
// to squash uname -m -> $(SUBARCH).
string machine = buf.machine;
if (machine == "i486") machine = "i386";
else if (machine == "i586") machine = "i386";
else if (machine == "i686") machine = "i386";
else if (machine == "sun4u") machine = "sparc64";
else if (machine.substr(0,3) == "arm") machine = "arm";
else if (machine == "sa110") machine = "arm";
else if (machine == "s390x") machine = "s390";
else if (machine.substr(0,3) == "ppc") machine = "powerpc";
else if (machine.substr(0,4) == "mips") machine = "mips";
else if (machine.substr(0,3) == "sh2") machine = "sh";
else if (machine.substr(0,3) == "sh3") machine = "sh";
else if (machine.substr(0,3) == "sh4") machine = "sh";
s.architecture = machine;
for (unsigned i=0; i<5; i++) s.perpass_verbose[i]=0;
s.timing = false;
s.guru_mode = false;
s.bulk_mode = false;
s.unoptimized = false;
s.suppress_warnings = false;
s.listing_mode = false;
s.listing_mode_vars = false;
#ifdef ENABLE_PROLOGUES
s.prologue_searching = true;
#else
s.prologue_searching = false;
#endif
s.buffer_size = 0;
s.last_pass = 5;
s.module_name = "stap_" + stringify(getpid());
s.stapconf_name = "stapconf_" + stringify(getpid()) + ".h";
s.output_file = ""; // -o FILE
s.keep_tmpdir = false;
s.cmd = "";
s.target_pid = 0;
s.perfmon=0;
s.symtab = false;
s.use_cache = true;
s.tapset_compile_coverage = false;
s.need_uprobes = false;
s.consult_symtab = false;
s.ignore_vmlinux = false;
s.ignore_dwarf = false;
s.load_only = false;
s.skip_badvars = false;
s.unprivileged = false;
// Location of our signing certificate.
// If we're root, use the database in SYSCONFDIR, otherwise
// use the one in our $HOME directory. */
if (getuid() == 0)
s.cert_db_path = SYSCONFDIR "/systemtap/ssl/server";
else
s.cert_db_path = getenv("HOME") + string ("/.systemtap/ssl/server");
const char* s_p = getenv ("SYSTEMTAP_TAPSET");
if (s_p != NULL)
{
s.include_path.push_back (s_p);
}
else
{
s.include_path.push_back (string(PKGDATADIR) + "/tapset");
}
const char* s_r = getenv ("SYSTEMTAP_RUNTIME");
if (s_r != NULL)
s.runtime_path = s_r;
else
s.runtime_path = string(PKGDATADIR) + "/runtime";
const char* s_d = getenv ("SYSTEMTAP_DIR");
if (s_d != NULL)
s.data_path = s_d;
else
s.data_path = get_home_directory() + string("/.systemtap");
if (create_dir(s.data_path.c_str()) == 1)
{
const char* e = strerror (errno);
if (! s.suppress_warnings)
cerr << "Warning: failed to create systemtap data directory (\""
<< s.data_path << "\"): " << e
<< ", disabling cache support." << endl;
s.use_cache = false;
}
if (s.use_cache)
{
s.cache_path = s.data_path + "/cache";
if (create_dir(s.cache_path.c_str()) == 1)
{
const char* e = strerror (errno);
if (! s.suppress_warnings)
cerr << "Warning: failed to create cache directory (\""
<< s.cache_path << "\"): " << e
<< ", disabling cache support." << endl;
s.use_cache = false;
}
}
// Location of our signing certificate.
// If we're root, use the database in SYSCONFDIR, otherwise
// use the one in s.data_path. */
if (geteuid() == 0)
s.cert_db_path = SYSCONFDIR "/systemtap/ssl/server";
else
s.cert_db_path = s.data_path + "/ssl/server";
const char* s_tc = getenv ("SYSTEMTAP_COVERAGE");
if (s_tc != NULL)
s.tapset_compile_coverage = true;
const char* s_kr = getenv ("SYSTEMTAP_RELEASE");
if (s_kr != NULL) {
setup_kernel_release(s, s_kr);
}
while (true)
{
int long_opt;
char * num_endptr;
#define LONG_OPT_KELF 1
#define LONG_OPT_KMAP 2
#define LONG_OPT_IGNORE_VMLINUX 3
#define LONG_OPT_IGNORE_DWARF 4
#define LONG_OPT_VERBOSE_PASS 5
#define LONG_OPT_SKIP_BADVARS 6
#define LONG_OPT_UNPRIVILEGED 7
// NB: also see find_hash(), usage(), switch stmt below, stap.1 man page
static struct option long_options[] = {
{ "kelf", 0, &long_opt, LONG_OPT_KELF },
{ "kmap", 2, &long_opt, LONG_OPT_KMAP },
{ "ignore-vmlinux", 0, &long_opt, LONG_OPT_IGNORE_VMLINUX },
{ "ignore-dwarf", 0, &long_opt, LONG_OPT_IGNORE_DWARF },
{ "skip-badvars", 0, &long_opt, LONG_OPT_SKIP_BADVARS },
{ "vp", 1, &long_opt, LONG_OPT_VERBOSE_PASS },
{ "unprivileged", 0, &long_opt, LONG_OPT_UNPRIVILEGED },
{ NULL, 0, NULL, 0 }
};
int grc = getopt_long (argc, argv, "hVvtp:I:e:o:R:r:a:m:kgPc:x:D:bs:uqwl:d:L:FS:B:",
long_options, NULL);
if (grc < 0)
break;
switch (grc)
{
case 'V':
version ();
exit (0);
case 'v':
for (unsigned i=0; i<5; i++)
s.perpass_verbose[i] ++;
break;
case 't':
s.timing = true;
break;
case 'w':
s.suppress_warnings = true;
break;
case 'p':
if (s.listing_mode)
{
cerr << "Listing (-l) mode implies pass 2." << endl;
usage (s, 1);
}
s.last_pass = (int)strtoul(optarg, &num_endptr, 10);
if (*num_endptr != '\0' || s.last_pass < 1 || s.last_pass > 5)
{
cerr << "Invalid pass number (should be 1-5)." << endl;
usage (s, 1);
}
break;
case 'I':
s.include_path.push_back (string (optarg));
break;
case 'd':
s.unwindsym_modules.insert (string (optarg));
// PR10228: trigger task-finder logic early if -d /USER-MODULE/ given.
if (optarg[0] == '/')
enable_task_finder (s);
break;
case 'e':
if (have_script)
{
cerr << "Only one script can be given on the command line."
<< endl;
usage (s, 1);
}
cmdline_script = string (optarg);
have_script = true;
break;
case 'o':
s.output_file = string (optarg);
break;
case 'R':
s.runtime_path = string (optarg);
break;
case 'm':
s.module_name = string (optarg);
save_module = true;
{
string::size_type len = s.module_name.length();
// If the module name ends with '.ko', chop it off since
// modutils doesn't like modules named 'foo.ko.ko'.
if (len > 3 && s.module_name.substr(len - 3, 3) == ".ko")
{
s.module_name.erase(len - 3);
len -= 3;
cerr << "Truncating module name to '" << s.module_name
<< "'" << endl;
}
// Make sure an empty module name wasn't specified (-m "")
if (len == 0)
{
cerr << "Module name cannot be empty." << endl;
usage (s, 1);
}
// Make sure the module name is only composed of the
// following chars: [_a-zA-Z0-9]
const string identchars("_" "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789");
if (s.module_name.find_first_not_of(identchars) != string::npos)
{
cerr << "Invalid module name (must only be composed of"
" characters [_a-zA-Z0-9])." << endl;
usage (s, 1);
}
// Make sure module name isn't too long.
if (s.module_name.size() >= (MODULE_NAME_LEN - 1))
{
s.module_name.resize(MODULE_NAME_LEN - 1);
cerr << "Truncating module name to '" << s.module_name
<< "'" << endl;
}
}
s.use_cache = false;
break;
case 'r':
setup_kernel_release(s, optarg);
break;
case 'a':
s.architecture = string(optarg);
break;
case 'k':
s.keep_tmpdir = true;
s.use_cache = false; /* User wants to keep a usable build tree. */
break;
case 'g':
s.guru_mode = true;
break;
case 'P':
s.prologue_searching = true;
break;
case 'b':
s.bulk_mode = true;
break;
case 'u':
s.unoptimized = true;
break;
case 's':
s.buffer_size = (int) strtoul (optarg, &num_endptr, 10);
if (*num_endptr != '\0' || s.buffer_size < 1 || s.buffer_size > 4095)
{
cerr << "Invalid buffer size (should be 1-4095)." << endl;
usage (s, 1);
}
break;
case 'c':
s.cmd = string (optarg);
break;
case 'x':
s.target_pid = (int) strtoul(optarg, &num_endptr, 10);
if (*num_endptr != '\0')
{
cerr << "Invalid target process ID number." << endl;
usage (s, 1);
}
break;
case 'D':
s.macros.push_back (string (optarg));
break;
case 'S':
s.size_option = string (optarg);
break;
case 'q':
s.tapset_compile_coverage = true;
break;
case 'h':
usage (s, 0);
break;
case 'L':
s.listing_mode_vars = true;
s.unoptimized = true; // This causes retention of variables for listing_mode
case 'l':
s.suppress_warnings = true;
s.listing_mode = true;
s.last_pass = 2;
if (have_script)
{
cerr << "Only one script can be given on the command line."
<< endl;
usage (s, 1);
}
cmdline_script = string("probe ") + string(optarg) + " {}";
have_script = true;
break;
case 'F':
s.load_only = true;
break;
case 'B':
s.kbuildflags.push_back (string (optarg));
break;
case 0:
switch (long_opt)
{
case LONG_OPT_KELF:
s.consult_symtab = true;
break;
case LONG_OPT_KMAP:
// Leave s.consult_symtab unset for now, to ease error checking.
if (!s.kernel_symtab_path.empty())
{
cerr << "You can't specify multiple --kmap options." << endl;
usage(s, 1);
}
if (optarg)
s.kernel_symtab_path = optarg;
else
s.kernel_symtab_path = PATH_TBD;
break;
case LONG_OPT_IGNORE_VMLINUX:
s.ignore_vmlinux = true;
break;
case LONG_OPT_IGNORE_DWARF:
s.ignore_dwarf = true;
break;
case LONG_OPT_VERBOSE_PASS:
{
bool ok = true;
if (strlen(optarg) < 1 || strlen(optarg) > 5)
ok = false;
if (ok)
for (unsigned i=0; i<strlen(optarg); i++)
if (isdigit (optarg[i]))
s.perpass_verbose[i] += optarg[i]-'0';
else
ok = false;
if (! ok)
{
cerr << "Invalid --vp argument: it takes 1 to 5 digits." << endl;
usage (s, 1);
}
// NB: we don't do this: s.last_pass = strlen(optarg);
break;
}
case LONG_OPT_SKIP_BADVARS:
s.skip_badvars = true;
break;
case LONG_OPT_UNPRIVILEGED:
s.unprivileged = true;
break;
default:
cerr << "Internal error parsing command arguments." << endl;
usage(s, 1);
}
break;
default:
usage (s, 1);
break;
}
}
// Check for options conflicts.
checkOptions (s);
// Warn in case the target kernel release doesn't match the running one.
if (s.last_pass > 4 &&
(string(buf.release) != s.kernel_release ||
machine != s.architecture)) // NB: squashed ARCH by PR4186 logic
{
if(! s.suppress_warnings)
cerr << "WARNING: kernel release/architecture mismatch with host forces last-pass 4." << endl;
s.last_pass = 4;
}
for (int i = optind; i < argc; i++)
{
if (! have_script)
{
script_file = string (argv[i]);
have_script = true;
}
else
s.args.push_back (string (argv[i]));
}
// need a user file
if (! have_script)
{
cerr << "A script must be specified." << endl;
usage(s, 1);
}
// translate path of runtime to absolute path
if (s.runtime_path[0] != '/')
{
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)))
{
s.runtime_path = string(cwd) + "/" + s.runtime_path;
}
}
int rc = 0;
// PASS 0: setting up
s.verbose = s.perpass_verbose[0];
STAP_PROBE1(stap, pass0__start, &s);
// For PR1477, we used to override $PATH and $LC_ALL and other stuff
// here. We seem to use complete pathnames in
// buildrun.cxx/tapsets.cxx now, so this is not necessary. Further,
// it interferes with util.cxx:find_executable(), used for $PATH
// resolution.
s.kernel_base_release.assign(s.kernel_release, 0, s.kernel_release.find('-'));
// arguments parsed; get down to business
if (s.verbose > 1)
{
version ();
clog << "Session arch: " << s.architecture
<< " release: " << s.kernel_release
<< endl;
}
// Create a temporary directory to build within.
// Be careful with this, as "s.tmpdir" is "rm -rf"'d at the end.
{
const char* tmpdir_env = getenv("TMPDIR");
if (! tmpdir_env)
tmpdir_env = "/tmp";
string stapdir = "/stapXXXXXX";
string tmpdirt = tmpdir_env + stapdir;
mode_t mask = umask(0);
const char* tmpdir = mkdtemp((char *)tmpdirt.c_str());
umask(mask);
if (! tmpdir)
{
const char* e = strerror (errno);
cerr << "ERROR: cannot create temporary directory (\"" << tmpdirt << "\"): " << e << endl;
exit (1); // die
}
else
s.tmpdir = tmpdir;
if (s.verbose>1)
clog << "Created temporary directory \"" << s.tmpdir << "\"" << endl;
}
// Create the name of the C source file within the temporary
// directory.
s.translated_source = string(s.tmpdir) + "/" + s.module_name + ".c";
// Set up our handler to catch routine signals, to allow clean
// and reasonably timely exit.
setup_signals(&handle_interrupt);
STAP_PROBE1(stap, pass0__end, &s);
struct tms tms_before;
times (& tms_before);
struct timeval tv_before;
gettimeofday (&tv_before, NULL);
// PASS 1a: PARSING USER SCRIPT
STAP_PROBE1(stap, pass1a__start, &s);
struct stat user_file_stat;
int user_file_stat_rc = -1;
if (script_file == "-")
{
s.user_file = parser::parse (s, cin, s.guru_mode);
user_file_stat_rc = fstat (STDIN_FILENO, & user_file_stat);
}
else if (script_file != "")
{
s.user_file = parser::parse (s, script_file, s.guru_mode);
user_file_stat_rc = stat (script_file.c_str(), & user_file_stat);
}
else
{
istringstream ii (cmdline_script);
s.user_file = parser::parse (s, ii, s.guru_mode);
}
if (s.user_file == 0)
// syntax errors already printed
rc ++;
// Construct arch / kernel-versioning search path
vector<string> version_suffixes;
string kvr = s.kernel_release;
const string& arch = s.architecture;
// add full kernel-version-release (2.6.NN-FOOBAR) + arch
version_suffixes.push_back ("/" + kvr + "/" + arch);
version_suffixes.push_back ("/" + kvr);
// add kernel version (2.6.NN) + arch
if (kvr != s.kernel_base_release) {
kvr = s.kernel_base_release;
version_suffixes.push_back ("/" + kvr + "/" + arch);
version_suffixes.push_back ("/" + kvr);
}
// add kernel family (2.6) + arch
string::size_type dot1_index = kvr.find ('.');
string::size_type dot2_index = kvr.rfind ('.');
while (dot2_index > dot1_index && dot2_index != string::npos) {
kvr.erase(dot2_index);
version_suffixes.push_back ("/" + kvr + "/" + arch);
version_suffixes.push_back ("/" + kvr);
dot2_index = kvr.rfind ('.');
}
// add architecture search path
version_suffixes.push_back("/" + arch);
// add empty string as last element
version_suffixes.push_back ("");
// PASS 1b: PARSING LIBRARY SCRIPTS
STAP_PROBE1(stap, pass1b__start, &s);
for (unsigned i=0; i<s.include_path.size(); i++)
{
// now iterate upon it
for (unsigned k=0; k<version_suffixes.size(); k++)
{
glob_t globbuf;
string dir = s.include_path[i] + version_suffixes[k] + "/*.stp";
int r = glob(dir.c_str (), 0, NULL, & globbuf);
if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
rc ++;
// GLOB_NOMATCH is acceptable
if (s.verbose>1 && globbuf.gl_pathc > 0)
clog << "Searched '" << dir << "', "
<< "found " << globbuf.gl_pathc << endl;
for (unsigned j=0; j<globbuf.gl_pathc; j++)
{
if (pending_interrupts)
break;
// XXX: privilege only for /usr/share/systemtap?
stapfile* f = parser::parse (s, globbuf.gl_pathv[j], true);
if (f == 0)
rc ++;
else
s.library_files.push_back (f);
struct stat tapset_file_stat;
int stat_rc = stat (globbuf.gl_pathv[j], & tapset_file_stat);
if (stat_rc == 0 && user_file_stat_rc == 0 &&
user_file_stat.st_dev == tapset_file_stat.st_dev &&
user_file_stat.st_ino == tapset_file_stat.st_ino)
{
clog << "usage error: tapset file '" << globbuf.gl_pathv[j]
<< "' cannot be run directly as a session script." << endl;
rc ++;
}
}
globfree (& globbuf);
}
}
if (rc == 0 && s.last_pass == 1)
{
cout << "# parse tree dump" << endl;
s.user_file->print (cout);
cout << endl;
if (s.verbose)
for (unsigned i=0; i<s.library_files.size(); i++)
{
s.library_files[i]->print (cout);
cout << endl;
}
}
struct tms tms_after;
times (& tms_after);
unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
struct timeval tv_after;
gettimeofday (&tv_after, NULL);
#define TIMESPRINT \
(tms_after.tms_cutime + tms_after.tms_utime \
- tms_before.tms_cutime - tms_before.tms_utime) * 1000 / (_sc_clk_tck) << "usr/" \
<< (tms_after.tms_cstime + tms_after.tms_stime \
- tms_before.tms_cstime - tms_before.tms_stime) * 1000 / (_sc_clk_tck) << "sys/" \
<< ((tv_after.tv_sec - tv_before.tv_sec) * 1000 + \
((long)tv_after.tv_usec - (long)tv_before.tv_usec) / 1000) << "real ms."
// syntax errors, if any, are already printed
if (s.verbose)
{
clog << "Pass 1: parsed user script and "
<< s.library_files.size()
<< " library script(s) in "
<< TIMESPRINT
<< endl;
}
if (rc && !s.listing_mode)
cerr << "Pass 1: parse failed. "
<< "Try again with another '--vp 1' option."
<< endl;
STAP_PROBE1(stap, pass1__end, &s);
if (rc || s.last_pass == 1 || pending_interrupts) goto cleanup;
times (& tms_before);
gettimeofday (&tv_before, NULL);
// PASS 2: ELABORATION
s.verbose = s.perpass_verbose[1];
STAP_PROBE1(stap, pass2__start, &s);
rc = semantic_pass (s);
if (s.listing_mode || (rc == 0 && s.last_pass == 2))
printscript(s, cout);
times (& tms_after);
gettimeofday (&tv_after, NULL);
if (s.verbose) clog << "Pass 2: analyzed script: "
<< s.probes.size() << " probe(s), "
<< s.functions.size() << " function(s), "
<< s.embeds.size() << " embed(s), "
<< s.globals.size() << " global(s) in "
<< TIMESPRINT
<< endl;
if (rc && !s.listing_mode)
cerr << "Pass 2: analysis failed. "
<< "Try again with another '--vp 01' option."
<< endl;
// Generate hash. There isn't any point in generating the hash
// if last_pass is 2, since we'll quit before using it.
else if (s.last_pass != 2 && s.use_cache)
{
ostringstream o;
unsigned saved_verbose;
{
// Make sure we're in verbose mode, so that printscript()
// will output function/probe bodies.
saved_verbose = s.verbose;
s.verbose = 3;
printscript(s, o); // Print script to 'o'
s.verbose = saved_verbose;
}
// Generate hash
find_hash (s, o.str());
// See if we can use cached source/module.
if (get_from_cache(s))
{
// If our last pass isn't 5, we're done (since passes 3 and
// 4 just generate what we just pulled out of the cache).
if (s.last_pass < 5 || pending_interrupts) goto cleanup;
// Short-circuit to pass 5.
goto pass_5;
}
}
/* Print out list of missing files */
missing_rpm_list_print(s);
STAP_PROBE1(stap, pass2__end, &s);
if (rc || s.listing_mode || s.last_pass == 2 || pending_interrupts) goto cleanup;
// PASS 3: TRANSLATION
s.verbose = s.perpass_verbose[2];
times (& tms_before);
gettimeofday (&tv_before, NULL);
STAP_PROBE1(stap, pass3__start, &s);
rc = translate_pass (s);
if (rc == 0 && s.last_pass == 3)
{
ifstream i (s.translated_source.c_str());
cout << i.rdbuf();
}
times (& tms_after);
gettimeofday (&tv_after, NULL);
if (s.verbose) clog << "Pass 3: translated to C into \""
<< s.translated_source
<< "\" in "
<< TIMESPRINT
<< endl;
if (rc)
cerr << "Pass 3: translation failed. "
<< "Try again with another '--vp 001' option."
<< endl;
STAP_PROBE1(stap, pass3__end, &s);
if (rc || s.last_pass == 3 || pending_interrupts) goto cleanup;
// PASS 4: COMPILATION
s.verbose = s.perpass_verbose[3];
times (& tms_before);
gettimeofday (&tv_before, NULL);
STAP_PROBE1(stap, pass4__start, &s);
rc = compile_pass (s);
if (rc == 0 && s.last_pass == 4)
{
cout << ((s.hash_path == "") ? (s.module_name + string(".ko")) : s.hash_path);
cout << endl;
}
times (& tms_after);
gettimeofday (&tv_after, NULL);
if (s.verbose) clog << "Pass 4: compiled C into \""
<< s.module_name << ".ko"
<< "\" in "
<< TIMESPRINT
<< endl;
if (rc)
cerr << "Pass 4: compilation failed. "
<< "Try again with another '--vp 0001' option."
<< endl;
else
{
// Update cache. Cache cleaning is kicked off at the beginning of this function.
if (s.use_cache)
add_to_cache(s);
// We may need to save the module in $CWD if the cache was
// inaccessible for some reason.
if (! s.use_cache && s.last_pass == 4)
save_module = true;
// Copy module to the current directory.
if (save_module && !pending_interrupts)
{
string module_src_path = s.tmpdir + "/" + s.module_name + ".ko";
string module_dest_path = s.module_name + ".ko";
if (s.verbose > 1)
clog << "Copying " << module_src_path << " to "
<< module_dest_path << endl;
if (copy_file(module_src_path.c_str(), module_dest_path.c_str()) != 0)
cerr << "Copy failed (\"" << module_src_path << "\" to \""
<< module_dest_path << "\"): " << strerror(errno) << endl;
#if HAVE_NSS
// Save the signature as well.
assert (! s.cert_db_path.empty());
module_src_path += ".sgn";
if (file_exists (module_src_path))
{
module_dest_path += ".sgn";
if (s.verbose > 1)
clog << "Copying " << module_src_path << " to "
<< module_dest_path << endl;
if (copy_file(module_src_path.c_str(), module_dest_path.c_str()) != 0)
cerr << "Copy failed (\"" << module_src_path << "\" to \""
<< module_dest_path << "\"): " << strerror(errno) << endl;
}
#endif
}
}
STAP_PROBE1(stap, pass4__end, &s);
if (rc || s.last_pass == 4 || pending_interrupts) goto cleanup;
// PASS 5: RUN
pass_5:
s.verbose = s.perpass_verbose[4];
times (& tms_before);
gettimeofday (&tv_before, NULL);
// NB: this message is a judgement call. The other passes don't emit
// a "hello, I'm starting" message, but then the others aren't interactive
// and don't take an indefinite amount of time.
STAP_PROBE1(stap, pass5__start, &s);
if (s.verbose) clog << "Pass 5: starting run." << endl;
rc = run_pass (s);
times (& tms_after);
gettimeofday (&tv_after, NULL);
if (s.verbose) clog << "Pass 5: run completed in "
<< TIMESPRINT
<< endl;
if (rc)
cerr << "Pass 5: run failed. "
<< "Try again with another '--vp 00001' option."
<< endl;
// if (rc) goto cleanup;
STAP_PROBE1(stap, pass5__end, &s);
// PASS 6: cleaning up
cleanup:
STAP_PROBE1(stap, pass6__start, &s);
// update the database information
if (!rc && s.tapset_compile_coverage && !pending_interrupts) {
#ifdef HAVE_LIBSQLITE3
update_coverage_db(s);
#else
cerr << "Coverage database not available without libsqlite3" << endl;
#endif
}
// Clean up temporary directory. Obviously, be careful with this.
if (s.tmpdir == "")
; // do nothing
else
{
if (s.keep_tmpdir)
clog << "Keeping temporary directory \"" << s.tmpdir << "\"" << endl;
else
{
// Ignore signals while we're deleting the temporary directory.
setup_signals (SIG_IGN);
// Remove the temporary directory.
string cleanupcmd = "rm -rf ";
cleanupcmd += s.tmpdir;
(void) stap_system (s.verbose, cleanupcmd);
}
}
STAP_PROBE1(stap, pass6__end, &s);
return (rc||pending_interrupts) ? EXIT_FAILURE : EXIT_SUCCESS;
}
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
|