1 // systemtap translator/driver
2 // Copyright (C) 2005-2016 Red Hat Inc.
3 // Copyright (C) 2005 IBM Corp.
4 // Copyright (C) 2006 Intel Corporation.
6 // This file is part of systemtap, and is free software. You can
7 // redistribute it and/or modify it under the terms of the GNU General
8 // Public License (GPL); either version 2, or (at your option) any
14 #include "elaborate.h"
15 #include "translate.h"
21 #include "coveragedb.h"
22 #include "rpm_finder.h"
23 #include "task_finder.h"
27 #include "setupdwfl.h"
28 #ifdef HAVE_LIBREADLINE
29 #include "interactive.h"
37 #include "stap-probe.h"
46 #include <sys/utsname.h>
47 #include <sys/times.h>
58 uniq_list(list
<string
>& l
)
61 list
<string
>::iterator i
= l
.begin();
63 if (s
.insert(*i
).second
)
70 printscript(systemtap_session
& s
, ostream
& o
)
72 if (s
.dump_mode
== systemtap_session::dump_matched_probes
||
73 s
.dump_mode
== systemtap_session::dump_matched_probes_vars
)
75 // We go through some heroic measures to produce clean output.
76 // Record the alias and probe pointer as <name, set<derived_probe *> >
77 map
<string
,set
<derived_probe
*> > probe_list
;
79 // Pre-process the probe alias
80 for (unsigned i
=0; i
<s
.probes
.size(); i
++)
82 assert_no_interrupts();
84 derived_probe
* p
= s
.probes
[i
];
86 p
->collect_derivation_chain (chain
);
89 p
->printsig(cerr
); cerr
<< endl
;
90 cerr
<< "chain[" << chain
.size() << "]:" << endl
;
91 for (unsigned j
=0; j
<chain
.size(); j
++)
93 cerr
<< " [" << j
<< "]: " << endl
;
94 cerr
<< "\tlocations[" << chain
[j
]->locations
.size() << "]:" << endl
;
95 for (unsigned k
=0; k
<chain
[j
]->locations
.size(); k
++)
97 cerr
<< "\t [" << k
<< "]: ";
98 chain
[j
]->locations
[k
]->print(cerr
);
101 const probe_alias
*a
= chain
[j
]->get_alias();
104 cerr
<< "\taliases[" << a
->alias_names
.size() << "]:" << endl
;
105 for (unsigned k
=0; k
<a
->alias_names
.size(); k
++)
107 cerr
<< "\t [" << k
<< "]: ";
108 a
->alias_names
[k
]->print(cerr
);
115 const string
& pp
= lex_cast(*p
->script_location());
117 // PR16730: We should only list probes that can be traced back to the
118 // user's spec, not any auxiliary probes in the tapsets.
119 // Also, do not want to the probes that are from the additional
120 // scripts (-E SCRIPT) to be listed.
121 if (!s
.is_primary_probe(p
))
124 // Now duplicate-eliminate. An alias may have expanded to
125 // several actual derived probe points, but we only want to
126 // print the alias head name once.
127 probe_list
[pp
].insert(p
);
130 // print probe name and variables if there
131 for (map
<string
, set
<derived_probe
*> >::iterator it
=probe_list
.begin(); it
!=probe_list
.end(); ++it
)
133 o
<< it
->first
; // probe name or alias
135 // Print the locals and arguments for -L mode only
136 if (s
.dump_mode
== systemtap_session::dump_matched_probes_vars
)
138 map
<string
,unsigned> var_count
; // format <"name:type",count>
139 map
<string
,unsigned> arg_count
;
140 list
<string
> var_list
;
141 list
<string
> arg_list
;
142 // traverse set<derived_probe *> to collect all locals and arguments
143 for (set
<derived_probe
*>::iterator ix
=it
->second
.begin(); ix
!=it
->second
.end(); ++ix
)
145 derived_probe
* p
= *ix
;
146 // collect available locals of the probe
147 for (unsigned j
=0; j
<p
->locals
.size(); j
++)
150 vardecl
* v
= p
->locals
[j
];
152 var_count
[tmps
.str()]++;
153 var_list
.push_back(tmps
.str());
155 // collect arguments of the probe if there
156 list
<string
> arg_set
;
158 for (list
<string
>::iterator ia
=arg_set
.begin(); ia
!=arg_set
.end(); ++ia
) {
160 arg_list
.push_back(*ia
);
167 // print the set-intersection only
168 for (list
<string
>::iterator ir
=var_list
.begin(); ir
!=var_list
.end(); ++ir
)
169 if (var_count
.find(*ir
)->second
== it
->second
.size()) // print locals
171 for (list
<string
>::iterator ir
=arg_list
.begin(); ir
!=arg_list
.end(); ++ir
)
172 if (arg_count
.find(*ir
)->second
== it
->second
.size()) // print arguments
180 if (s
.embeds
.size() > 0)
181 o
<< _("# global embedded code") << endl
;
182 for (unsigned i
=0; i
<s
.embeds
.size(); i
++)
184 assert_no_interrupts();
185 embeddedcode
* ec
= s
.embeds
[i
];
190 if (s
.globals
.size() > 0)
191 o
<< _("# globals") << endl
;
192 for (unsigned i
=0; i
<s
.globals
.size(); i
++)
194 assert_no_interrupts();
195 vardecl
* v
= s
.globals
[i
];
197 if (s
.verbose
&& v
->init
)
205 if (s
.functions
.size() > 0)
206 o
<< _("# functions") << endl
;
207 for (map
<string
,functiondecl
*>::iterator it
= s
.functions
.begin(); it
!= s
.functions
.end(); it
++)
209 assert_no_interrupts();
210 functiondecl
* f
= it
->second
;
213 if (f
->locals
.size() > 0)
214 o
<< _(" # locals") << endl
;
215 for (unsigned j
=0; j
<f
->locals
.size(); j
++)
217 vardecl
* v
= f
->locals
[j
];
229 if (s
.probes
.size() > 0)
230 o
<< _("# probes") << endl
;
231 for (unsigned i
=0; i
<s
.probes
.size(); i
++)
233 assert_no_interrupts();
234 derived_probe
* p
= s
.probes
[i
];
237 if (p
->locals
.size() > 0)
238 o
<< _(" # locals") << endl
;
239 for (unsigned j
=0; j
<p
->locals
.size(); j
++)
241 vardecl
* v
= p
->locals
[j
];
256 int pending_interrupts
;
259 void handle_interrupt (int)
261 // This might be nice, but we don't know our current verbosity...
262 // clog << _F("Received signal %d", sig) << endl << flush;
263 kill_stap_spawn(SIGTERM
);
265 pending_interrupts
++;
266 // Absorb the first two signals. This used to be one, but when
267 // stap is run under sudo, and then interrupted, sudo relays a
268 // redundant copy of the signal to stap, leading to an unclean shutdown.
269 if (pending_interrupts
> 2) // XXX: should be configurable? time-based?
271 char msg
[] = "Too many interrupts received, exiting.\n";
272 int rc
= write (2, msg
, sizeof(msg
)-1);
273 if (rc
) {/* Do nothing; we don't care if our last gasp went out. */ ;}
280 setup_signals (sighandler_t handler
)
284 memset(&sa
, 0, sizeof(sa
));
285 sa
.sa_handler
= handler
;
286 sigemptyset (&sa
.sa_mask
);
287 if (handler
!= SIG_IGN
)
289 sigaddset (&sa
.sa_mask
, SIGHUP
);
290 sigaddset (&sa
.sa_mask
, SIGPIPE
);
291 sigaddset (&sa
.sa_mask
, SIGINT
);
292 sigaddset (&sa
.sa_mask
, SIGTERM
);
293 sigaddset (&sa
.sa_mask
, SIGXFSZ
);
294 sigaddset (&sa
.sa_mask
, SIGXCPU
);
296 sa
.sa_flags
= SA_RESTART
;
298 sigaction (SIGHUP
, &sa
, NULL
);
299 sigaction (SIGPIPE
, &sa
, NULL
);
300 sigaction (SIGINT
, &sa
, NULL
);
301 sigaction (SIGTERM
, &sa
, NULL
);
302 sigaction (SIGXFSZ
, &sa
, NULL
);
303 sigaction (SIGXCPU
, &sa
, NULL
);
308 sdt_benchmark_thread(unsigned long i
)
310 PROBE(stap
, benchmark__thread__start
);
312 PROBE1(stap
, benchmark
, i
);
313 PROBE(stap
, benchmark__thread__end
);
318 run_sdt_benchmark(systemtap_session
& s
)
320 unsigned long loops
= s
.benchmark_sdt_loops
?: 10000000;
321 unsigned long threads
= s
.benchmark_sdt_threads
?: 1;
324 clog
<< _F("Beginning SDT benchmark with %lu loops in %lu threads.",
325 loops
, threads
) << endl
;
327 struct tms tms_before
, tms_after
;
328 struct timeval tv_before
, tv_after
;
329 unsigned _sc_clk_tck
= sysconf (_SC_CLK_TCK
);
330 times (& tms_before
);
331 gettimeofday (&tv_before
, NULL
);
333 PROBE(stap
, benchmark__start
);
335 vector
<thread
> handles
;
336 for (unsigned long i
= 0; i
< threads
; ++i
)
337 handles
.push_back(thread(sdt_benchmark_thread
, loops
));
338 for (unsigned long i
= 0; i
< threads
; ++i
)
341 PROBE(stap
, benchmark__end
);
344 gettimeofday (&tv_after
, NULL
);
346 clog
<< _F("Completed SDT benchmark in %ldusr/%ldsys/%ldreal ms.",
347 (long)(tms_after
.tms_utime
- tms_before
.tms_utime
) * 1000 / _sc_clk_tck
,
348 (long)(tms_after
.tms_stime
- tms_before
.tms_stime
) * 1000 / _sc_clk_tck
,
349 (long)((tv_after
.tv_sec
- tv_before
.tv_sec
) * 1000 +
350 ((long)tv_after
.tv_usec
- (long)tv_before
.tv_usec
) / 1000))
357 // Compilation passes 0 through 4
359 passes_0_4 (systemtap_session
&s
)
363 // If we don't know the release, there's no hope either locally or on a server.
364 if (s
.kernel_release
.empty())
366 if (s
.kernel_build_tree
.empty())
367 cerr
<< _("ERROR: kernel release isn't specified") << endl
;
369 cerr
<< _F("ERROR: kernel release isn't found in \"%s\"",
370 s
.kernel_build_tree
.c_str()) << endl
;
374 // Perform passes 0 through 4 using a compile server?
375 if (! s
.specified_servers
.empty ())
378 compile_server_client
client (s
);
379 return client
.passes_0_4 ();
381 s
.print_warning(_("Without NSS, using a compile-server is not supported by this version of systemtap"));
383 // This cannot be an attempt to use a server after a local compile failed
384 // since --use-server-on-error is locked to 'no' if we don't have
386 assert (! s
.try_server ());
387 s
.print_warning(_("Ignoring --use-server"));
391 // PASS 0: setting up
392 s
.verbose
= s
.perpass_verbose
[0];
393 PROBE1(stap
, pass0__start
, &s
);
395 // For PR1477, we used to override $PATH and $LC_ALL and other stuff
396 // here. We seem to use complete pathnames in
397 // buildrun.cxx/tapsets.cxx now, so this is not necessary. Further,
398 // it interferes with util.cxx:find_executable(), used for $PATH
401 s
.kernel_base_release
.assign(s
.kernel_release
, 0, s
.kernel_release
.find('-'));
403 // Update various paths to include the sysroot, if provided.
404 if (!s
.sysroot
.empty())
406 if (s
.update_release_sysroot
&& !s
.sysroot
.empty())
407 s
.kernel_build_tree
= s
.sysroot
+ s
.kernel_build_tree
;
408 debuginfo_path_insert_sysroot(s
.sysroot
);
411 // Now that no further changes to s.kernel_build_tree can occur, let's use it.
412 if (s
.runtime_mode
== systemtap_session::kernel_runtime
) {
413 if ((rc
= s
.parse_kernel_config ()) != 0
414 || (rc
= s
.parse_kernel_exports ()) != 0
415 || (rc
= s
.parse_kernel_functions ()) != 0)
417 // Try again with a server
423 // Create the name of the C source file within the temporary
424 // directory. Note the _src prefix, explained in
425 // buildrun.cxx:compile_pass()
426 s
.translated_source
= string(s
.tmpdir
) + "/" + s
.module_name
+ "_src.c";
428 PROBE1(stap
, pass0__end
, &s
);
430 struct tms tms_before
;
431 times (& tms_before
);
432 struct timeval tv_before
;
433 gettimeofday (&tv_before
, NULL
);
435 // PASS 1a: PARSING LIBRARY SCRIPTS
436 PROBE1(stap
, pass1a__start
, &s
);
438 if (! s
.pass_1a_complete
)
440 // We need to handle the library scripts first because this pass
441 // gathers information on .stpm files that might be needed to
442 // parse the user script.
444 // We need to first ascertain the status of the user script, though.
445 struct stat user_file_stat
;
446 int user_file_stat_rc
= -1;
448 if (s
.script_file
== "-")
450 user_file_stat_rc
= fstat (STDIN_FILENO
, & user_file_stat
);
452 else if (s
.script_file
!= "")
454 user_file_stat_rc
= stat (s
.script_file
.c_str(), & user_file_stat
);
456 // otherwise, rc is 0 for a command line script
458 vector
<string
> version_suffixes
;
459 if (s
.runtime_mode
== systemtap_session::kernel_runtime
)
461 // Construct kernel-versioning search path
462 string kvr
= s
.kernel_release
;
464 // add full kernel-version-release (2.6.NN-FOOBAR)
465 version_suffixes
.push_back ("/" + kvr
);
467 // add kernel version (2.6.NN)
468 if (kvr
!= s
.kernel_base_release
)
470 kvr
= s
.kernel_base_release
;
471 version_suffixes
.push_back ("/" + kvr
);
474 // add kernel family (2.6)
475 string::size_type dot1_index
= kvr
.find ('.');
476 string::size_type dot2_index
= kvr
.rfind ('.');
477 while (dot2_index
> dot1_index
&& dot2_index
!= string::npos
)
479 kvr
.erase(dot2_index
);
480 version_suffixes
.push_back ("/" + kvr
);
481 dot2_index
= kvr
.rfind ('.');
485 // add empty string as last element
486 version_suffixes
.push_back ("");
488 // Add arch variants of every path, just before each
489 const string
& arch
= s
.architecture
;
490 for (unsigned i
=0; i
<version_suffixes
.size(); i
+=2)
491 version_suffixes
.insert(version_suffixes
.begin() + i
,
492 version_suffixes
[i
] + "/" + arch
);
494 // Add runtime variants of every path, before everything else
495 string runtime_prefix
;
496 if (s
.runtime_mode
== systemtap_session::kernel_runtime
)
497 runtime_prefix
= "/linux";
498 else if (s
.runtime_mode
== systemtap_session::dyninst_runtime
)
499 runtime_prefix
= "/dyninst";
500 if (!runtime_prefix
.empty())
501 for (unsigned i
=0; i
<version_suffixes
.size(); i
+=2)
502 version_suffixes
.insert(version_suffixes
.begin() + i
/2,
503 runtime_prefix
+ version_suffixes
[i
]);
505 // First, parse .stpm files on the include path. We need to have the
506 // resulting macro definitions available for parsing library files,
507 // but since .stpm files can consist only of '@define' constructs,
508 // we can parse each one without reference to the others.
509 set
<pair
<dev_t
, ino_t
> > seen_library_macro_files
;
510 set
<string
> seen_library_macro_files_names
;
512 for (unsigned i
=0; i
<s
.include_path
.size(); i
++)
514 // now iterate upon it
515 for (unsigned k
=0; k
<version_suffixes
.size(); k
++)
518 string dir
= s
.include_path
[i
] + version_suffixes
[k
] + "/*.stpm";
519 int r
= glob(dir
.c_str (), 0, NULL
, & globbuf
);
520 if (r
== GLOB_NOSPACE
|| r
== GLOB_ABORTED
)
522 // GLOB_NOMATCH is acceptable
524 unsigned prev_s_library_files
= s
.library_files
.size();
526 for (unsigned j
=0; j
<globbuf
.gl_pathc
; j
++)
528 assert_no_interrupts();
530 struct stat tapset_file_stat
;
531 int stat_rc
= stat (globbuf
.gl_pathv
[j
], & tapset_file_stat
);
532 if (stat_rc
== 0 && user_file_stat_rc
== 0 &&
533 user_file_stat
.st_dev
== tapset_file_stat
.st_dev
&&
534 user_file_stat
.st_ino
== tapset_file_stat
.st_ino
)
537 << _F("usage error: macro tapset file '%s' cannot be run directly as a session script.",
538 globbuf
.gl_pathv
[j
]) << endl
;
542 // PR11949: duplicate-eliminate tapset files
545 pair
<dev_t
,ino_t
> here
= make_pair(tapset_file_stat
.st_dev
,
546 tapset_file_stat
.st_ino
);
547 if (seen_library_macro_files
.find(here
) != seen_library_macro_files
.end())
550 clog
<< _F("Skipping tapset \"%s\", duplicate inode.", globbuf
.gl_pathv
[j
]) << endl
;
553 seen_library_macro_files
.insert (here
);
556 // PR12443: duplicate-eliminate harder
557 string full_path
= globbuf
.gl_pathv
[j
];
558 string tapset_base
= s
.include_path
[i
]; // not dir; it has arch suffixes too
559 if (full_path
.size() > tapset_base
.size())
561 string tail_part
= full_path
.substr(tapset_base
.size());
562 if (seen_library_macro_files_names
.find (tail_part
) != seen_library_macro_files_names
.end())
565 clog
<< _F("Skipping tapset \"%s\", duplicate name.", globbuf
.gl_pathv
[j
]) << endl
;
568 seen_library_macro_files_names
.insert (tail_part
);
572 clog
<< _F("Processing tapset \"%s\"", globbuf
.gl_pathv
[j
]) << endl
;
574 stapfile
* f
= parse_library_macros (s
, globbuf
.gl_pathv
[j
]);
576 s
.print_warning(_F("macro tapset \"%s\" has errors, and will be skipped.", string(globbuf
.gl_pathv
[j
]).c_str()));
578 s
.library_files
.push_back (f
);
581 unsigned next_s_library_files
= s
.library_files
.size();
582 if (s
.verbose
>1 && globbuf
.gl_pathc
> 0)
583 //TRANSLATORS: Searching through directories, 'processed' means 'examined so far'
584 clog
<< _F("Searched for library macro files: \"%s\", found: %zu, processed: %u",
585 dir
.c_str(), globbuf
.gl_pathc
,
586 (next_s_library_files
-prev_s_library_files
)) << endl
;
592 // Next, gather and parse the library files.
593 set
<pair
<dev_t
, ino_t
> > seen_library_files
;
594 set
<string
> seen_library_files_names
;
596 for (unsigned i
=0; i
<s
.include_path
.size(); i
++)
598 unsigned tapset_flags
= pf_guru
| pf_squash_errors
;
600 // The first path is special, as it's the builtin tapset.
601 // Allow all features no matter what s.compatible says.
603 tapset_flags
|= pf_no_compatible
;
605 // now iterate upon it
606 for (unsigned k
=0; k
<version_suffixes
.size(); k
++)
609 string dir
= s
.include_path
[i
] + version_suffixes
[k
] + "/*.stp";
610 int r
= glob(dir
.c_str (), 0, NULL
, & globbuf
);
611 if (r
== GLOB_NOSPACE
|| r
== GLOB_ABORTED
)
613 // GLOB_NOMATCH is acceptable
615 unsigned prev_s_library_files
= s
.library_files
.size();
617 for (unsigned j
=0; j
<globbuf
.gl_pathc
; j
++)
619 assert_no_interrupts();
621 struct stat tapset_file_stat
;
622 int stat_rc
= stat (globbuf
.gl_pathv
[j
], & tapset_file_stat
);
623 if (stat_rc
== 0 && user_file_stat_rc
== 0 &&
624 user_file_stat
.st_dev
== tapset_file_stat
.st_dev
&&
625 user_file_stat
.st_ino
== tapset_file_stat
.st_ino
)
628 << _F("usage error: tapset file '%s' cannot be run directly as a session script.",
629 globbuf
.gl_pathv
[j
]) << endl
;
633 // PR11949: duplicate-eliminate tapset files
636 pair
<dev_t
,ino_t
> here
= make_pair(tapset_file_stat
.st_dev
,
637 tapset_file_stat
.st_ino
);
638 if (seen_library_files
.find(here
) != seen_library_files
.end())
641 clog
<< _F("Skipping tapset \"%s\", duplicate inode.", globbuf
.gl_pathv
[j
]) << endl
;
644 seen_library_files
.insert (here
);
647 // PR12443: duplicate-eliminate harder
648 string full_path
= globbuf
.gl_pathv
[j
];
649 string tapset_base
= s
.include_path
[i
]; // not dir; it has arch suffixes too
650 if (full_path
.size() > tapset_base
.size())
652 string tail_part
= full_path
.substr(tapset_base
.size());
653 if (seen_library_files_names
.find (tail_part
) != seen_library_files_names
.end())
656 clog
<< _F("Skipping tapset \"%s\", duplicate name.", globbuf
.gl_pathv
[j
]) << endl
;
659 seen_library_files_names
.insert (tail_part
);
663 clog
<< _F("Processing tapset \"%s\"", globbuf
.gl_pathv
[j
]) << endl
;
665 // NB: we don't need to restrict privilege only for
666 // /usr/share/systemtap, i.e., excluding
667 // user-specified $XDG_DATA_DIRS. That's because
668 // stapdev gets root-equivalent privileges anyway;
669 // stapsys and stapusr use a remote compilation with
670 // a trusted environment, where client-side
671 // $XDG_DATA_DIRS are not passed.
673 stapfile
* f
= parse (s
, globbuf
.gl_pathv
[j
], tapset_flags
);
675 s
.print_warning(_F("tapset \"%s\" has errors, and will be skipped", string(globbuf
.gl_pathv
[j
]).c_str()));
677 s
.library_files
.push_back (f
);
680 unsigned next_s_library_files
= s
.library_files
.size();
681 if (s
.verbose
>1 && globbuf
.gl_pathc
> 0)
682 //TRANSLATORS: Searching through directories, 'processed' means 'examined so far'
683 clog
<< _F("Searched: \"%s\", found: %zu, processed: %u",
684 dir
.c_str(), globbuf
.gl_pathc
,
685 (next_s_library_files
-prev_s_library_files
)) << endl
;
687 globfree (& globbuf
);
693 // Now that we've made it through pass 1a, remember this so we
694 // don't have to do this again in interactive mode. This doesn't
695 // effect non-interactive mode.
696 s
.pass_1a_complete
= true;
699 // PASS 1b: PARSING USER SCRIPT
700 PROBE1(stap
, pass1b__start
, &s
);
702 // Only try to parse a user script if the user provided one, or if we have to
703 // make one (as is the case for listing mode). Otherwise, s.user_script
705 if (!s
.script_file
.empty() ||
706 !s
.cmdline_script
.empty() ||
707 s
.dump_mode
== systemtap_session::dump_matched_probes
||
708 s
.dump_mode
== systemtap_session::dump_matched_probes_vars
)
710 unsigned user_flags
= s
.guru_mode
? pf_guru
: 0;
711 user_flags
|= pf_user_file
;
712 if (s
.script_file
== "-")
714 s
.user_files
.push_back (parse (s
, "<input>", cin
, user_flags
));
716 else if (s
.script_file
!= "")
718 s
.user_files
.push_back (parse (s
, s
.script_file
, user_flags
));
720 else if (s
.cmdline_script
!= "")
722 istringstream
ii (s
.cmdline_script
);
723 s
.user_files
.push_back(parse (s
, "<input>", ii
, user_flags
));
727 istringstream
ii ("probe " + s
.dump_matched_pattern
+ " {}");
728 s
.user_files
.push_back (parse (s
, "<input>", ii
, user_flags
));
731 // parses the additional script(s) (-E script). does so even if in listing
732 // mode, incase there is something special in the additional script(s),
733 // like a macro or alias. give them a unique name to differentiate the
734 // scripts that were inputted.
736 for (vector
<string
>::iterator script
= s
.additional_scripts
.begin(); script
!= s
.additional_scripts
.end(); script
++)
738 string input_name
= "<input" + lex_cast(count
) + ">";
739 istringstream
ii (*script
);
740 s
.user_files
.push_back(parse (s
, input_name
, ii
, user_flags
));
744 for(vector
<stapfile
*>::iterator it
= s
.user_files
.begin(); it
!= s
.user_files
.end(); it
++)
748 // Syntax errors already printed.
753 else if (s
.cmdline_script
.empty() &&
754 s
.dump_mode
== systemtap_session::dump_none
) // -e ''
756 cerr
<< _("Input file '<input>' is empty.") << endl
;
760 // Dump a list of probe aliases picked up, if requested
761 if (s
.dump_mode
== systemtap_session::dump_probe_aliases
)
764 vector
<stapfile
*>::const_iterator file
;
765 for (file
= s
.library_files
.begin();
766 file
!= s
.library_files
.end(); ++file
)
768 vector
<probe_alias
*>::const_iterator alias
;
769 for (alias
= (*file
)->aliases
.begin();
770 alias
!= (*file
)->aliases
.end(); ++alias
)
773 (*alias
)->printsig(ss
);
774 string str
= ss
.str();
775 if (!s
.verbose
&& startswith(str
, "_"))
781 set
<string
>::iterator alias
;
782 for (alias
= aliases
.begin();
783 alias
!= aliases
.end(); ++alias
)
785 cout
<< *alias
<< endl
;
788 // Dump the parse tree if this is the last pass
789 else if (rc
== 0 && s
.last_pass
== 1)
791 cout
<< _("# parse tree dump") << endl
;
792 for (vector
<stapfile
*>::iterator it
= s
.user_files
.begin(); it
!= s
.user_files
.end(); it
++)
796 for (unsigned i
=0; i
<s
.library_files
.size(); i
++)
798 s
.library_files
[i
]->print (cout
);
803 struct tms tms_after
;
805 unsigned _sc_clk_tck
= sysconf (_SC_CLK_TCK
);
806 struct timeval tv_after
;
807 gettimeofday (&tv_after
, NULL
);
809 #define TIMESPRINT _("in ") << \
810 (tms_after.tms_cutime + tms_after.tms_utime \
811 - tms_before.tms_cutime - tms_before.tms_utime) * 1000 / (_sc_clk_tck) << "usr/" \
812 << (tms_after.tms_cstime + tms_after.tms_stime \
813 - tms_before.tms_cstime - tms_before.tms_stime) * 1000 / (_sc_clk_tck) << "sys/" \
814 << ((tv_after.tv_sec - tv_before.tv_sec) * 1000 + \
815 ((long)tv_after.tv_usec - (long)tv_before.tv_usec) / 1000) << "real ms."
817 // syntax errors, if any, are already printed
820 // XXX also include a count of helper macro files loaded (.stpm)?
821 int n
= int(s
.library_files
.size());
822 clog
<< _("Pass 1: parsed user script and ")
823 << _NF("%d library script ", "%d library scripts ", n
, n
)
829 if (rc
&& !s
.dump_mode
)
830 cerr
<< _("Pass 1: parse failed. [man error::pass1]") << endl
;
832 PROBE1(stap
, pass1__end
, &s
);
834 assert_no_interrupts();
835 if (rc
|| s
.last_pass
== 1 ||
836 s
.dump_mode
== systemtap_session::dump_probe_aliases
)
839 times (& tms_before
);
840 gettimeofday (&tv_before
, NULL
);
842 // PASS 2: ELABORATION
843 s
.verbose
= s
.perpass_verbose
[1];
844 PROBE1(stap
, pass2__start
, &s
);
845 rc
= semantic_pass (s
);
847 // Dump a list of known probe point types, if requested.
848 if (s
.dump_mode
== systemtap_session::dump_probe_types
)
849 s
.pattern_root
->dump (s
);
850 // Dump a list of functions we picked up, if requested.
851 else if (s
.dump_mode
== systemtap_session::dump_functions
)
853 map
<string
,functiondecl
*>::const_iterator func
;
854 for (func
= s
.functions
.begin();
855 func
!= s
.functions
.end(); ++func
)
857 functiondecl
& curfunc
= *func
->second
;
858 if (curfunc
.synthetic
)
860 if (!startswith(curfunc
.name
, "__global_"))
862 if (!s
.verbose
&& startswith(curfunc
.name
, "__global__"))
864 curfunc
.printsigtags(cout
, s
.verbose
>0 /* all_tags */ );
868 // Dump the whole script if requested, or if we stop at 2
869 else if (s
.dump_mode
== systemtap_session::dump_matched_probes
||
870 s
.dump_mode
== systemtap_session::dump_matched_probes_vars
||
871 (rc
== 0 && s
.last_pass
== 2))
872 printscript(s
, cout
);
875 gettimeofday (&tv_after
, NULL
);
878 int np
= s
.probes
.size();
879 int nf
= s
.functions
.size();
880 int ne
= s
.embeds
.size();
881 int ng
= s
.globals
.size();
882 clog
<< _("Pass 2: analyzed script: ")
883 << _NF("%d probe, ", "%d probes, ", np
, np
)
884 << _NF("%d function, ", "%d functions, ", nf
, nf
)
885 << _NF("%d embed, ", "%d embeds, ", ne
, ne
)
886 << _NF("%d global ", "%d globals ", ng
, ng
)
892 missing_rpm_list_print(s
, "-debuginfo");
894 if (rc
&& !s
.dump_mode
&& !s
.try_server ())
895 cerr
<< _("Pass 2: analysis failed. [man error::pass2]") << endl
;
897 PROBE1(stap
, pass2__end
, &s
);
899 assert_no_interrupts();
900 // NB: none of the dump modes need to go beyond pass-2. If this changes, break
901 // into individual modes here.
902 if (rc
|| s
.last_pass
== 2 || s
.dump_mode
)
905 rc
= prepare_translate_pass (s
);
906 assert_no_interrupts();
909 // Generate hash. There isn't any point in generating the hash
910 // if last_pass is 2, since we'll quit before using it.
911 if (s
.use_script_cache
)
914 unsigned saved_verbose
;
917 // Make sure we're in verbose mode, so that printscript()
918 // will output function/probe bodies.
919 saved_verbose
= s
.verbose
;
921 printscript(s
, o
); // Print script to 'o'
922 s
.verbose
= saved_verbose
;
926 find_script_hash (s
, o
.str());
928 // See if we can use cached source/module.
929 if (get_script_from_cache(s
))
931 // We may still need to build uprobes, if it's not also cached.
933 rc
= uprobes_pass(s
);
935 // If our last pass isn't 5, we're done (since passes 3 and
936 // 4 just generate what we just pulled out of the cache).
937 assert_no_interrupts();
938 if (rc
|| s
.last_pass
< 5) return rc
;
940 // Short-circuit to pass 5.
945 // PASS 3: TRANSLATION
946 s
.verbose
= s
.perpass_verbose
[2];
947 times (& tms_before
);
948 gettimeofday (&tv_before
, NULL
);
949 PROBE1(stap
, pass3__start
, &s
);
951 rc
= translate_pass (s
);
952 if (! rc
&& s
.last_pass
== 3)
954 ifstream
i (s
.translated_source
.c_str());
959 gettimeofday (&tv_after
, NULL
);
962 clog
<< _("Pass 3: translated to C into \"")
963 << s
.translated_source
969 if (rc
&& ! s
.try_server ())
970 cerr
<< _("Pass 3: translation failed. [man error::pass3]") << endl
;
972 PROBE1(stap
, pass3__end
, &s
);
974 assert_no_interrupts();
975 if (rc
|| s
.last_pass
== 3) return rc
;
977 // PASS 4: COMPILATION
978 s
.verbose
= s
.perpass_verbose
[3];
979 times (& tms_before
);
980 gettimeofday (&tv_before
, NULL
);
981 PROBE1(stap
, pass4__start
, &s
);
985 find_stapconf_hash(s
);
986 get_stapconf_from_cache(s
);
988 rc
= compile_pass (s
);
989 if (! rc
&& s
.last_pass
== 4)
991 cout
<< ((s
.hash_path
== "") ? s
.module_filename() : s
.hash_path
);
996 gettimeofday (&tv_after
, NULL
);
998 if (s
.verbose
) clog
<< _("Pass 4: compiled C into \"")
999 << s
.module_filename()
1004 if (rc
&& ! s
.try_server ())
1005 cerr
<< _("Pass 4: compilation failed. [man error::pass4]") << endl
;
1009 // Update cache. Cache cleaning is kicked off at the beginning of this function.
1010 if (s
.use_script_cache
)
1011 add_script_to_cache(s
);
1012 if (s
.use_cache
&& !s
.runtime_usermode_p())
1013 add_stapconf_to_cache(s
);
1015 // We may need to save the module in $CWD if the cache was
1016 // inaccessible for some reason.
1017 if (! s
.use_script_cache
&& s
.last_pass
== 4)
1018 s
.save_module
= true;
1020 // Copy module to the current directory.
1021 if (s
.save_module
&& !pending_interrupts
)
1023 string module_src_path
= s
.tmpdir
+ "/" + s
.module_filename();
1024 string module_dest_path
= s
.module_filename();
1025 copy_file(module_src_path
, module_dest_path
, s
.verbose
> 1);
1028 // Copy uprobes module to the current directory.
1029 if (s
.save_uprobes
&& !s
.uprobes_path
.empty() && !pending_interrupts
)
1031 rc
= create_dir("uprobes");
1033 copy_file(s
.uprobes_path
, "uprobes/uprobes.ko", s
.verbose
> 1);
1037 PROBE1(stap
, pass4__end
, &s
);
1043 pass_5 (systemtap_session
&s
, vector
<remote
*> targets
)
1046 s
.verbose
= s
.perpass_verbose
[4];
1047 struct tms tms_before
;
1048 times (& tms_before
);
1049 struct timeval tv_before
;
1050 gettimeofday (&tv_before
, NULL
);
1051 // NB: this message is a judgement call. The other passes don't emit
1052 // a "hello, I'm starting" message, but then the others aren't interactive
1053 // and don't take an indefinite amount of time.
1054 PROBE1(stap
, pass5__start
, &s
);
1055 if (s
.verbose
) clog
<< _("Pass 5: starting run.") << endl
;
1056 int rc
= remote::run(targets
);
1057 struct tms tms_after
;
1058 times (& tms_after
);
1059 unsigned _sc_clk_tck
= sysconf (_SC_CLK_TCK
);
1060 struct timeval tv_after
;
1061 gettimeofday (&tv_after
, NULL
);
1062 if (s
.verbose
) clog
<< _("Pass 5: run completed ")
1067 cerr
<< _("Pass 5: run failed. [man error::pass5]") << endl
;
1069 // Interrupting pass-5 to quit is normal, so we want an EXIT_SUCCESS below.
1070 pending_interrupts
= 0;
1072 PROBE1(stap
, pass5__end
, &s
);
1078 cleanup (systemtap_session
&s
, int rc
)
1080 // PASS 6: cleaning up
1081 PROBE1(stap
, pass6__start
, &s
);
1083 for (systemtap_session::session_map_t::iterator it
= s
.subsessions
.begin();
1084 it
!= s
.subsessions
.end(); ++it
)
1085 cleanup (*it
->second
, rc
);
1087 // update the database information
1088 if (!rc
&& s
.tapset_compile_coverage
&& !pending_interrupts
) {
1089 #ifdef HAVE_LIBSQLITE3
1090 update_coverage_db(s
);
1092 cerr
<< _("Coverage database not available without libsqlite3") << endl
;
1096 s
.report_suppression();
1098 PROBE1(stap
, pass6__end
, &s
);
1102 passes_0_4_again_with_server (systemtap_session
&s
)
1104 // Not a server and not already using a server.
1105 assert (! s
.client_options
);
1106 assert (s
.specified_servers
.empty ());
1108 // Specify default server(s).
1109 s
.specified_servers
.push_back ("");
1111 // Reset the previous temporary directory and start fresh
1114 // Try to compile again, using the server
1115 clog
<< _("Attempting compilation using a compile server")
1118 int rc
= passes_0_4 (s
);
1123 main (int argc
, char * const argv
[])
1125 // Initialize defaults.
1127 systemtap_session s
;
1129 setlocale (LC_ALL
, "");
1130 bindtextdomain (PACKAGE
, LOCALEDIR
);
1131 textdomain (PACKAGE
);
1134 // Set up our handler to catch routine signals, to allow clean
1135 // and reasonably timely exit.
1136 setup_signals(&handle_interrupt
);
1138 // PR13520: Parse $SYSTEMTAP_DIR/rc for extra options
1139 string rc_file
= s
.data_path
+ "/rc";
1140 ifstream
rcf (rc_file
.c_str());
1143 memset (& words
, 0, sizeof(words
));
1146 while (getline (rcf
, rcline
))
1148 rc
= wordexp (rcline
.c_str(), & words
, WRDE_NOCMD
|WRDE_UNDEF
|
1149 (linecount
> 0 ? WRDE_APPEND
: 0));
1150 // NB: WRDE_APPEND automagically reallocates words.* as more options are added.
1156 int extended_argc
= words
.we_wordc
+ argc
;
1157 char **extended_argv
= (char**) calloc (extended_argc
+ 1, sizeof(char*));
1158 if (rc
|| !extended_argv
)
1160 clog
<< _F("Error processing extra options in %s", rc_file
.c_str());
1161 return EXIT_FAILURE
;
1163 // Copy over the arguments *by reference*, first the ones from the rc file.
1164 char **p
= & extended_argv
[0];
1166 for (unsigned i
=0; i
<words
.we_wordc
; i
++) *p
++ = words
.we_wordv
[i
];
1167 for (int j
=1; j
<argc
; j
++) *p
++ = argv
[j
];
1170 // Process the command line.
1171 rc
= s
.parse_cmdline (extended_argc
, extended_argv
);
1175 // Create the temp dir.
1178 if (words
.we_wordc
> 0 && s
.verbose
> 1)
1179 clog
<< _F("Extra options in %s: %d\n", rc_file
.c_str(), (int)words
.we_wordc
);
1181 // Check for options conflicts. Exits if errors are detected.
1182 s
.check_options (extended_argc
, extended_argv
);
1184 // We don't need these strings any more.
1186 free (extended_argv
);
1188 // arguments parsed; get down to business
1192 // Need to send the verbose message here, rather than in the session ctor, since
1193 // we didn't know if verbose was set.
1194 if (rc
== 0 && s
.verbose
>1)
1195 clog
<< _F("Created temporary directory \"%s\"", s
.tmpdir
.c_str()) << endl
;
1197 // Run the benchmark and quit right away.
1198 if (s
.benchmark_sdt_loops
|| s
.benchmark_sdt_threads
)
1199 return run_sdt_benchmark(s
);
1201 // Prepare connections for each specified remote target.
1202 vector
<remote
*> targets
;
1203 bool fake_remote
=false;
1204 if (s
.remote_uris
.empty())
1207 s
.remote_uris
.push_back("direct:");
1209 for (unsigned i
= 0; rc
== 0 && i
< s
.remote_uris
.size(); ++i
)
1211 // PR13354: pass remote id#/url only in non --remote=HOST cases
1212 remote
*target
= remote::create(s
, s
.remote_uris
[i
],
1213 fake_remote
? -1 : (int)i
);
1215 targets
.push_back(target
);
1220 // Discover and loop over each unique session created by the remote targets.
1221 set
<systemtap_session
*> sessions
;
1222 for (unsigned i
= 0; i
< targets
.size(); ++i
)
1223 sessions
.insert(targets
[i
]->get_session());
1225 // FIXME: For now, only attempt local interactive use.
1226 if (s
.interactive_mode
&& fake_remote
)
1228 #ifdef HAVE_LIBREADLINE
1229 rc
= interactive_mode (s
, targets
);
1234 for (set
<systemtap_session
*>::iterator it
= sessions
.begin();
1235 rc
== 0 && !pending_interrupts
&& it
!= sessions
.end(); ++it
)
1237 systemtap_session
& ss
= **it
;
1239 clog
<< _F("Session arch: %s release: %s",
1240 ss
.architecture
.c_str(), ss
.kernel_release
.c_str())
1244 // If requested, query server status. This is independent
1246 query_server_status (ss
);
1248 // If requested, manage trust of servers. This is
1249 // independent of other tasks.
1250 manage_server_trust (ss
);
1253 // Run the passes only if a script has been specified or
1254 // if we're dumping something. The requirement for a
1255 // script has already been checked in
1256 // systemtap_session::check_options.
1257 if (ss
.have_script
|| ss
.dump_mode
)
1259 // Run passes 0-4 for each unique session, either
1260 // locally or using a compile-server.
1261 ss
.init_try_server ();
1262 if ((rc
= passes_0_4 (ss
)))
1264 // Compilation failed.
1265 // Try again using a server if appropriate.
1266 if (ss
.try_server ())
1267 rc
= passes_0_4_again_with_server (ss
);
1269 if (rc
|| s
.perpass_verbose
[0] >= 1)
1270 s
.explain_auto_options ();
1274 // Run pass 5, if requested
1275 if (rc
== 0 && s
.have_script
&& s
.last_pass
>= 5 && ! pending_interrupts
)
1276 rc
= pass_5 (s
, targets
);
1280 for (unsigned i
= 0; i
< targets
.size(); ++i
)
1284 assert_no_interrupts();
1285 return (rc
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
1287 catch (const interrupt_exception
& e
) {
1288 // User entered ctrl-c, exit quietly.
1289 return EXIT_FAILURE
;
1291 catch (const exit_exception
& e
) {
1292 // Exiting for any quiet reason.
1295 catch (const exception
&e
) {
1296 // Some other uncaught exception.
1297 cerr
<< e
.what() << endl
;
1298 return EXIT_FAILURE
;
1301 // Catch all other unknown exceptions.
1302 cerr
<< _("ERROR: caught unknown exception!") << endl
;
1303 return EXIT_FAILURE
;
1307 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */