]> sourceware.org Git - systemtap.git/blob - main.cxx
Merge branch 'master' into dsmith/task_finder2
[systemtap.git] / main.cxx
1 // systemtap translator/driver
2 // Copyright (C) 2005-2011 Red Hat Inc.
3 // Copyright (C) 2005 IBM Corp.
4 // Copyright (C) 2006 Intel Corporation.
5 //
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
9 // later version.
10
11 #include "config.h"
12 #include "staptree.h"
13 #include "parse.h"
14 #include "elaborate.h"
15 #include "translate.h"
16 #include "buildrun.h"
17 #include "session.h"
18 #include "hash.h"
19 #include "cache.h"
20 #include "util.h"
21 #include "coveragedb.h"
22 #include "rpm_finder.h"
23 #include "task_finder.h"
24 #include "csclient.h"
25 #include "remote.h"
26 #include "tapsets.h"
27
28 #include <libintl.h>
29 #include <locale.h>
30
31 #include "stap-probe.h"
32
33 #include <cstdlib>
34
35 extern "C" {
36 #include <glob.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <sys/utsname.h>
40 #include <sys/times.h>
41 #include <sys/time.h>
42 #include <sys/stat.h>
43 #include <time.h>
44 #include <unistd.h>
45 }
46
47 using namespace std;
48
49 static void
50 uniq_list(list<string>& l)
51 {
52 set<string> s;
53 list<string>::iterator i = l.begin();
54 while (i != l.end())
55 if (s.insert(*i).second)
56 ++i;
57 else
58 i = l.erase(i);
59 }
60
61 static void
62 printscript(systemtap_session& s, ostream& o)
63 {
64 if (s.listing_mode)
65 {
66 // We go through some heroic measures to produce clean output.
67 // Record the alias and probe pointer as <name, set<derived_probe *> >
68 map<string,set<derived_probe *> > probe_list;
69
70 // Pre-process the probe alias
71 for (unsigned i=0; i<s.probes.size(); i++)
72 {
73 if (pending_interrupts) return;
74
75 derived_probe* p = s.probes[i];
76 // NB: p->basest() is not so interesting;
77 // p->almost_basest() doesn't quite work, so ...
78 vector<probe*> chain;
79 p->collect_derivation_chain (chain);
80 probe* second = (chain.size()>1) ? chain[chain.size()-2] : chain[0];
81
82 if (s.verbose > 5) {
83 p->printsig(cerr); cerr << endl;
84 cerr << "chain[" << chain.size() << "]:" << endl;
85 for (unsigned j=0; j<chain.size(); j++)
86 {
87 cerr << " [" << j << "]: " << endl;
88 cerr << "\tlocations[" << chain[j]->locations.size() << "]:" << endl;
89 for (unsigned k=0; k<chain[j]->locations.size(); k++)
90 {
91 cerr << "\t [" << k << "]: ";
92 chain[j]->locations[k]->print(cerr);
93 cerr << endl;
94 }
95 const probe_alias *a = chain[j]->get_alias();
96 if (a)
97 {
98 cerr << "\taliases[" << a->alias_names.size() << "]:" << endl;
99 for (unsigned k=0; k<a->alias_names.size(); k++)
100 {
101 cerr << "\t [" << k << "]: ";
102 a->alias_names[k]->print(cerr);
103 cerr << endl;
104 }
105 }
106 }
107 }
108
109 stringstream tmps;
110 const probe_alias *a = second->get_alias();
111 if (a)
112 {
113 assert (a->alias_names.size() >= 1);
114 a->alias_names[0]->print(tmps); // XXX: [0] is arbitrary; perhaps print all
115 }
116 else
117 {
118 assert (second->locations.size() >= 1);
119 second->locations[0]->print(tmps); // XXX: [0] is less arbitrary here, but still ...
120 }
121 string pp = tmps.str();
122
123 // Now duplicate-eliminate. An alias may have expanded to
124 // several actual derived probe points, but we only want to
125 // print the alias head name once.
126 probe_list[pp].insert(p);
127 }
128
129 // print probe name and variables if there
130 for (map<string, set<derived_probe *> >::iterator it=probe_list.begin(); it!=probe_list.end(); ++it)
131 {
132 o << it->first; // probe name or alias
133
134 // Print the locals and arguments for -L mode only
135 if (s.listing_mode_vars)
136 {
137 map<string,unsigned> var_count; // format <"name:type",count>
138 map<string,unsigned> arg_count;
139 list<string> var_list;
140 list<string> arg_list;
141 // traverse set<derived_probe *> to collect all locals and arguments
142 for (set<derived_probe *>::iterator ix=it->second.begin(); ix!=it->second.end(); ++ix)
143 {
144 derived_probe* p = *ix;
145 // collect available locals of the probe
146 for (unsigned j=0; j<p->locals.size(); j++)
147 {
148 stringstream tmps;
149 vardecl* v = p->locals[j];
150 v->printsig (tmps);
151 var_count[tmps.str()]++;
152 var_list.push_back(tmps.str());
153 }
154 // collect arguments of the probe if there
155 list<string> arg_set;
156 p->getargs(arg_set);
157 for (list<string>::iterator ia=arg_set.begin(); ia!=arg_set.end(); ++ia) {
158 arg_count[*ia]++;
159 arg_list.push_back(*ia);
160 }
161 }
162
163 uniq_list(arg_list);
164 uniq_list(var_list);
165
166 // print the set-intersection only
167 for (list<string>::iterator ir=var_list.begin(); ir!=var_list.end(); ++ir)
168 if (var_count.find(*ir)->second == it->second.size()) // print locals
169 o << " " << *ir;
170 for (list<string>::iterator ir=arg_list.begin(); ir!=arg_list.end(); ++ir)
171 if (arg_count.find(*ir)->second == it->second.size()) // print arguments
172 o << " " << *ir;
173 }
174 o << endl;
175 }
176 }
177 else
178 {
179 if (s.embeds.size() > 0)
180 o << _("# global embedded code") << endl;
181 for (unsigned i=0; i<s.embeds.size(); i++)
182 {
183 if (pending_interrupts) return;
184 embeddedcode* ec = s.embeds[i];
185 ec->print (o);
186 o << endl;
187 }
188
189 if (s.globals.size() > 0)
190 o << _("# globals") << endl;
191 for (unsigned i=0; i<s.globals.size(); i++)
192 {
193 if (pending_interrupts) return;
194 vardecl* v = s.globals[i];
195 v->printsig (o);
196 if (s.verbose && v->init)
197 {
198 o << " = ";
199 v->init->print(o);
200 }
201 o << endl;
202 }
203
204 if (s.functions.size() > 0)
205 o << _("# functions") << endl;
206 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
207 {
208 if (pending_interrupts) return;
209 functiondecl* f = it->second;
210 f->printsig (o);
211 o << endl;
212 if (f->locals.size() > 0)
213 o << _(" # locals") << endl;
214 for (unsigned j=0; j<f->locals.size(); j++)
215 {
216 vardecl* v = f->locals[j];
217 o << " ";
218 v->printsig (o);
219 o << endl;
220 }
221 if (s.verbose)
222 {
223 f->body->print (o);
224 o << endl;
225 }
226 }
227
228 if (s.probes.size() > 0)
229 o << _("# probes") << endl;
230 for (unsigned i=0; i<s.probes.size(); i++)
231 {
232 if (pending_interrupts) return;
233 derived_probe* p = s.probes[i];
234 p->printsig (o);
235 o << endl;
236 if (p->locals.size() > 0)
237 o << _(" # locals") << endl;
238 for (unsigned j=0; j<p->locals.size(); j++)
239 {
240 vardecl* v = p->locals[j];
241 o << " ";
242 v->printsig (o);
243 o << endl;
244 }
245 if (s.verbose)
246 {
247 p->body->print (o);
248 o << endl;
249 }
250 }
251 }
252 }
253
254
255 int pending_interrupts;
256
257 extern "C"
258 void handle_interrupt (int sig)
259 {
260 // This might be nice, but we don't know our current verbosity...
261 // clog << _F("Received signal %d", sig) << endl << flush;
262 kill_stap_spawn(sig);
263 pending_interrupts ++;
264 // Absorb the first two signals. This used to be one, but when
265 // stap is run under sudo, and then interrupted, sudo relays a
266 // redundant copy of the signal to stap, leading to an unclean shutdown.
267 if (pending_interrupts > 2) // XXX: should be configurable? time-based?
268 {
269 char msg[] = "Too many interrupts received, exiting.\n";
270 int rc = write (2, msg, sizeof(msg)-1);
271 if (rc) {/* Do nothing; we don't care if our last gasp went out. */ ;}
272 _exit (1);
273 }
274 }
275
276
277 void
278 setup_signals (sighandler_t handler)
279 {
280 struct sigaction sa;
281
282 memset(&sa, 0, sizeof(sa));
283 sa.sa_handler = handler;
284 sigemptyset (&sa.sa_mask);
285 if (handler != SIG_IGN)
286 {
287 sigaddset (&sa.sa_mask, SIGHUP);
288 sigaddset (&sa.sa_mask, SIGPIPE);
289 sigaddset (&sa.sa_mask, SIGINT);
290 sigaddset (&sa.sa_mask, SIGTERM);
291 sigaddset (&sa.sa_mask, SIGXFSZ);
292 sigaddset (&sa.sa_mask, SIGXCPU);
293 }
294 sa.sa_flags = SA_RESTART;
295
296 sigaction (SIGHUP, &sa, NULL);
297 sigaction (SIGPIPE, &sa, NULL);
298 sigaction (SIGINT, &sa, NULL);
299 sigaction (SIGTERM, &sa, NULL);
300 sigaction (SIGXFSZ, &sa, NULL);
301 sigaction (SIGXCPU, &sa, NULL);
302 }
303
304 int parse_kernel_config (systemtap_session &s)
305 {
306 // PR10702: pull config options
307 string kernel_config_file = s.kernel_build_tree + "/.config";
308 struct stat st;
309 int rc = stat(kernel_config_file.c_str(), &st);
310 if (rc != 0)
311 {
312 clog << _F("Checking \"%s\" failed with error: %s",
313 kernel_config_file.c_str(), strerror(errno)) << endl;
314 find_devel_rpms(s, s.kernel_build_tree.c_str());
315 missing_rpm_list_print(s,"-devel");
316 return rc;
317 }
318
319 ifstream kcf (kernel_config_file.c_str());
320 string line;
321 while (getline (kcf, line))
322 {
323 if (!startswith(line, "CONFIG_")) continue;
324 size_t off = line.find('=');
325 if (off == string::npos) continue;
326 string key = line.substr(0, off);
327 string value = line.substr(off+1, string::npos);
328 s.kernel_config[key] = value;
329 }
330 if (s.verbose > 2)
331 clog << _F("Parsed kernel \"%s\", ", kernel_config_file.c_str())
332 << _F(ngettext("containing %zu tuple", "containing %zu tuples",
333 s.kernel_config.size()), s.kernel_config.size()) << endl;
334
335 kcf.close();
336 return 0;
337 }
338
339
340 int parse_kernel_exports (systemtap_session &s)
341 {
342 string kernel_exports_file = s.kernel_build_tree + "/Module.symvers";
343 struct stat st;
344 int rc = stat(kernel_exports_file.c_str(), &st);
345 if (rc != 0)
346 {
347 clog << _F("Checking \"%s\" failed with error: %s\nEnsure kernel development headers & makefiles are installed",
348 kernel_exports_file.c_str(), strerror(errno)) << endl;
349 return rc;
350 }
351
352 ifstream kef (kernel_exports_file.c_str());
353 string line;
354 while (getline (kef, line))
355 {
356 vector<string> tokens;
357 tokenize (line, tokens, "\t");
358 if (tokens.size() == 4 &&
359 tokens[2] == "vmlinux" &&
360 tokens[3].substr(0,13) == string("EXPORT_SYMBOL"))
361 s.kernel_exports.insert (tokens[1]);
362 // RHEL4 Module.symvers file only has 3 tokens. No
363 // 'EXPORT_SYMBOL' token at the end of the line.
364 else if (tokens.size() == 3 && tokens[2] == "vmlinux")
365 s.kernel_exports.insert (tokens[1]);
366 }
367 if (s.verbose > 2)
368 clog << _F(ngettext("Parsed kernel %s, which contained one vmlinux export",
369 "Parsed kernel %s, which contained %zu vmlinux exports",
370 s.kernel_exports.size()), kernel_exports_file.c_str(),
371 s.kernel_exports.size()) << endl;
372
373 kef.close();
374 return 0;
375 }
376
377
378 static int
379 create_temp_dir (systemtap_session &s)
380 {
381 if (!s.tmpdir.empty())
382 return 0;
383
384 // Create a temporary directory to build within.
385 // Be careful with this, as "tmpdir" is "rm -rf"'d at the end.
386 const char * tmpdir_env = getenv("TMPDIR");
387 if (!tmpdir_env)
388 tmpdir_env = "/tmp";
389
390 string stapdir = "/stapXXXXXX";
391 string tmpdirt = tmpdir_env + stapdir;
392 const char *tmpdir_name = mkdtemp((char *)tmpdirt.c_str());
393 if (! tmpdir_name)
394 {
395 const char* e = strerror(errno);
396 //TRANSLATORS: we can't make the directory due to the error
397 cerr << _F("ERROR: cannot create temporary directory (\" %s \"): %s", tmpdirt.c_str(), e) << endl;
398 return 1;
399 }
400 else
401 s.tmpdir = tmpdir_name;
402
403 if (s.verbose>1)
404 clog << _F("Created temporary directory \"%s\"", s.tmpdir.c_str()) << endl;
405 return 0;
406 }
407
408 static void
409 remove_temp_dir(systemtap_session &s)
410 {
411 if (!s.tmpdir.empty())
412 {
413 if (s.keep_tmpdir && !s.tmpdir_opt_set)
414 clog << _F("Keeping temporary directory \"%s\"", s.tmpdir.c_str()) << endl;
415 else if (!s.tmpdir_opt_set)
416 {
417 // Mask signals while we're deleting the temporary directory.
418 stap_sigmasker masked;
419
420 // Remove the temporary directory.
421 vector<string> cleanupcmd;
422 cleanupcmd.push_back("rm");
423 cleanupcmd.push_back("-rf");
424 cleanupcmd.push_back(s.tmpdir);
425
426 (void) stap_system(s.verbose, cleanupcmd);
427 s.tmpdir.clear();
428 }
429 }
430 }
431
432 // Compilation passes 0 through 4
433 static int
434 passes_0_4 (systemtap_session &s)
435 {
436 int rc = 0;
437
438 // If we don't know the release, there's no hope either locally or on a server.
439 if (s.kernel_release.empty())
440 {
441 if (s.kernel_build_tree.empty())
442 cerr << _("ERROR: kernel release isn't specified") << endl;
443 else
444 cerr << _F("ERROR: kernel release isn't found in \"%s\"",
445 s.kernel_build_tree.c_str()) << endl;
446 return 1;
447 }
448
449 // Create a temporary directory to build within.
450 // Be careful with this, as "s.tmpdir" is "rm -rf"'d at the end.
451 rc = create_temp_dir (s);
452 if (rc)
453 return rc;
454
455 // Perform passes 0 through 4 using a compile server?
456 if (! s.specified_servers.empty ())
457 {
458 #if HAVE_NSS
459 compile_server_client client (s);
460 int rc = client.passes_0_4 ();
461 // Need to give a user a better diagnostic, if she didn't
462 // even ask for a server
463 if (rc && s.automatic_server_mode) {
464 cerr << _("Note: --use-server --unprivileged was selected because of stapusr membership.") << endl;
465 }
466 return rc;
467 #else
468 cerr << _("WARNING: Without NSS, using a compile-server is not supported by this version of systemtap") << endl;
469 // This cannot be an attempt to use a server after a local compile failed
470 // since --use-server-on-error is locked to 'no' if we don't have
471 // NSS.
472 assert (! s.try_server ());
473 #endif
474 }
475
476 // PASS 0: setting up
477 s.verbose = s.perpass_verbose[0];
478 PROBE1(stap, pass0__start, &s);
479
480 // For PR1477, we used to override $PATH and $LC_ALL and other stuff
481 // here. We seem to use complete pathnames in
482 // buildrun.cxx/tapsets.cxx now, so this is not necessary. Further,
483 // it interferes with util.cxx:find_executable(), used for $PATH
484 // resolution.
485
486 s.kernel_base_release.assign(s.kernel_release, 0, s.kernel_release.find('-'));
487
488 // Now that no further changes to s.kernel_build_tree can occur, let's use it.
489 if ((rc = parse_kernel_config (s)) != 0)
490 {
491 // Try again with a server
492 s.set_try_server ();
493 return rc;
494 }
495
496 if ((rc = parse_kernel_exports (s)) != 0)
497 {
498 // Try again with a server
499 s.set_try_server ();
500 return rc;
501 }
502
503 // Create the name of the C source file within the temporary
504 // directory. Note the _src prefix, explained in
505 // buildrun.cxx:compile_pass()
506 s.translated_source = string(s.tmpdir) + "/" + s.module_name + "_src.c";
507
508 PROBE1(stap, pass0__end, &s);
509
510 struct tms tms_before;
511 times (& tms_before);
512 struct timeval tv_before;
513 gettimeofday (&tv_before, NULL);
514
515 // PASS 1a: PARSING USER SCRIPT
516 PROBE1(stap, pass1a__start, &s);
517
518 struct stat user_file_stat;
519 int user_file_stat_rc = -1;
520
521 if (s.script_file == "-")
522 {
523 s.user_file = parse (s, cin, s.guru_mode);
524 user_file_stat_rc = fstat (STDIN_FILENO, & user_file_stat);
525 }
526 else if (s.script_file != "")
527 {
528 s.user_file = parse (s, s.script_file, s.guru_mode);
529 user_file_stat_rc = stat (s.script_file.c_str(), & user_file_stat);
530 }
531 else
532 {
533 istringstream ii (s.cmdline_script);
534 s.user_file = parse (s, ii, s.guru_mode);
535 }
536 if (s.user_file == 0)
537 {
538 // Syntax errors already printed.
539 rc ++;
540 }
541
542 // Construct arch / kernel-versioning search path
543 vector<string> version_suffixes;
544 string kvr = s.kernel_release;
545 const string& arch = s.architecture;
546 // add full kernel-version-release (2.6.NN-FOOBAR) + arch
547 version_suffixes.push_back ("/" + kvr + "/" + arch);
548 version_suffixes.push_back ("/" + kvr);
549 // add kernel version (2.6.NN) + arch
550 if (kvr != s.kernel_base_release) {
551 kvr = s.kernel_base_release;
552 version_suffixes.push_back ("/" + kvr + "/" + arch);
553 version_suffixes.push_back ("/" + kvr);
554 }
555 // add kernel family (2.6) + arch
556 string::size_type dot1_index = kvr.find ('.');
557 string::size_type dot2_index = kvr.rfind ('.');
558 while (dot2_index > dot1_index && dot2_index != string::npos) {
559 kvr.erase(dot2_index);
560 version_suffixes.push_back ("/" + kvr + "/" + arch);
561 version_suffixes.push_back ("/" + kvr);
562 dot2_index = kvr.rfind ('.');
563 }
564 // add architecture search path
565 version_suffixes.push_back("/" + arch);
566 // add empty string as last element
567 version_suffixes.push_back ("");
568
569 // PASS 1b: PARSING LIBRARY SCRIPTS
570 PROBE1(stap, pass1b__start, &s);
571
572 set<pair<dev_t, ino_t> > seen_library_files;
573
574 for (unsigned i=0; i<s.include_path.size(); i++)
575 {
576 // now iterate upon it
577 for (unsigned k=0; k<version_suffixes.size(); k++)
578 {
579 glob_t globbuf;
580 string dir = s.include_path[i] + version_suffixes[k] + "/*.stp";
581 int r = glob(dir.c_str (), 0, NULL, & globbuf);
582 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
583 rc ++;
584 // GLOB_NOMATCH is acceptable
585
586 unsigned prev_s_library_files = s.library_files.size();
587
588 for (unsigned j=0; j<globbuf.gl_pathc; j++)
589 {
590 if (pending_interrupts)
591 break;
592
593 struct stat tapset_file_stat;
594 int stat_rc = stat (globbuf.gl_pathv[j], & tapset_file_stat);
595 if (stat_rc == 0 && user_file_stat_rc == 0 &&
596 user_file_stat.st_dev == tapset_file_stat.st_dev &&
597 user_file_stat.st_ino == tapset_file_stat.st_ino)
598 {
599 cerr
600 << _F("usage error: tapset file '%s' cannot be run directly as a session script.",
601 globbuf.gl_pathv[j]) << endl;
602 rc ++;
603 }
604
605 // PR11949: duplicate-eliminate tapset files
606 if (stat_rc == 0)
607 {
608 pair<dev_t,ino_t> here = make_pair(tapset_file_stat.st_dev,
609 tapset_file_stat.st_ino);
610 if (seen_library_files.find(here) != seen_library_files.end())
611 continue;
612 seen_library_files.insert (here);
613 }
614
615 // XXX: privilege only for /usr/share/systemtap?
616 stapfile* f = parse (s, globbuf.gl_pathv[j], true);
617 if (f == 0 && !s.suppress_warnings)
618 s.print_warning("tapset '" + string(globbuf.gl_pathv[j])
619 + "' has errors, and will be skipped.");
620 else
621 s.library_files.push_back (f);
622 }
623
624 unsigned next_s_library_files = s.library_files.size();
625 if (s.verbose>1 && globbuf.gl_pathc > 0)
626 //TRANSLATORS: Searching through directories, 'processed' means 'examined so far'
627 clog << _F("Searched: \" %s \", found: %zu, processed: %u",
628 dir.c_str(), globbuf.gl_pathc,
629 (next_s_library_files-prev_s_library_files)) << endl;
630
631 globfree (& globbuf);
632 }
633 }
634 if (s.num_errors())
635 rc ++;
636
637 if (rc == 0 && s.last_pass == 1)
638 {
639 cout << _("# parse tree dump") << endl;
640 s.user_file->print (cout);
641 cout << endl;
642 if (s.verbose)
643 for (unsigned i=0; i<s.library_files.size(); i++)
644 {
645 s.library_files[i]->print (cout);
646 cout << endl;
647 }
648 }
649
650 struct tms tms_after;
651 times (& tms_after);
652 unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
653 struct timeval tv_after;
654 gettimeofday (&tv_after, NULL);
655
656 #define TIMESPRINT "in " << \
657 (tms_after.tms_cutime + tms_after.tms_utime \
658 - tms_before.tms_cutime - tms_before.tms_utime) * 1000 / (_sc_clk_tck) << "usr/" \
659 << (tms_after.tms_cstime + tms_after.tms_stime \
660 - tms_before.tms_cstime - tms_before.tms_stime) * 1000 / (_sc_clk_tck) << "sys/" \
661 << ((tv_after.tv_sec - tv_before.tv_sec) * 1000 + \
662 ((long)tv_after.tv_usec - (long)tv_before.tv_usec) / 1000) << "real ms."
663
664 // syntax errors, if any, are already printed
665 if (s.verbose)
666 {
667 clog << "Pass 1: parsed user script and "
668 << s.library_files.size()
669 << " library script(s) "
670 << getmemusage()
671 << TIMESPRINT
672 << endl;
673 }
674
675 if (rc && !s.listing_mode)
676 cerr << _("Pass 1: parse failed. Try again with another '--vp 1' option.") << endl;
677 //cerr << "Pass 1: parse failed. "
678 // << "Try again with another '--vp 1' option."
679 // << endl;
680
681 PROBE1(stap, pass1__end, &s);
682
683 if (rc || s.last_pass == 1 || pending_interrupts) return rc;
684
685 times (& tms_before);
686 gettimeofday (&tv_before, NULL);
687
688 // PASS 2: ELABORATION
689 s.verbose = s.perpass_verbose[1];
690 PROBE1(stap, pass2__start, &s);
691 rc = semantic_pass (s);
692
693 // Dump a list of known probe point types, if requested.
694 if (s.dump_probe_types)
695 s.pattern_root->dump (s);
696
697 if (s.listing_mode || (rc == 0 && s.last_pass == 2))
698 printscript(s, cout);
699
700 times (& tms_after);
701 gettimeofday (&tv_after, NULL);
702
703 if (s.verbose) clog << "Pass 2: analyzed script: "
704 << s.probes.size() << " probe(s), "
705 << s.functions.size() << " function(s), "
706 << s.embeds.size() << " embed(s), "
707 << s.globals.size() << " global(s) "
708 << getmemusage()
709 << TIMESPRINT
710 << endl;
711
712 if (rc && !s.listing_mode && !s.try_server ())
713 cerr << _("Pass 2: analysis failed. Try again with another '--vp 01' option.") << endl;
714 //cerr << "Pass 2: analysis failed. "
715 // << "Try again with another '--vp 01' option."
716 // << endl;
717
718 /* Print out list of missing files. XXX should be "if (rc)" ? */
719 missing_rpm_list_print(s,"-debuginfo");
720
721 PROBE1(stap, pass2__end, &s);
722
723 if (rc || s.listing_mode || s.last_pass == 2 || pending_interrupts) return rc;
724
725 rc = prepare_translate_pass (s);
726 if (rc || pending_interrupts) return rc;
727
728 // Generate hash. There isn't any point in generating the hash
729 // if last_pass is 2, since we'll quit before using it.
730 if (s.use_script_cache)
731 {
732 ostringstream o;
733 unsigned saved_verbose;
734
735 {
736 // Make sure we're in verbose mode, so that printscript()
737 // will output function/probe bodies.
738 saved_verbose = s.verbose;
739 s.verbose = 3;
740 printscript(s, o); // Print script to 'o'
741 s.verbose = saved_verbose;
742 }
743
744 // Generate hash
745 find_script_hash (s, o.str());
746
747 // See if we can use cached source/module.
748 if (get_script_from_cache(s))
749 {
750 // We may still need to build uprobes, if it's not also cached.
751 if (s.need_uprobes)
752 rc = uprobes_pass(s);
753
754 // If our last pass isn't 5, we're done (since passes 3 and
755 // 4 just generate what we just pulled out of the cache).
756 if (rc || s.last_pass < 5 || pending_interrupts)
757 return rc;
758
759 // Short-circuit to pass 5.
760 return 0;
761 }
762 }
763
764 // PASS 3: TRANSLATION
765 s.verbose = s.perpass_verbose[2];
766 times (& tms_before);
767 gettimeofday (&tv_before, NULL);
768 PROBE1(stap, pass3__start, &s);
769
770 rc = translate_pass (s);
771 if (! rc && s.last_pass == 3)
772 {
773 ifstream i (s.translated_source.c_str());
774 cout << i.rdbuf();
775 }
776
777 times (& tms_after);
778 gettimeofday (&tv_after, NULL);
779
780 if (s.verbose)
781 clog << "Pass 3: translated to C into \""
782 << s.translated_source
783 << "\" "
784 << getmemusage()
785 << TIMESPRINT
786 << endl;
787
788 if (rc && ! s.try_server ())
789 cerr << _("Pass 3: translation failed. Try again with another '--vp 001' option.") << endl;
790 //cerr << "Pass 3: translation failed. "
791 // << "Try again with another '--vp 001' option."
792 // << endl;
793
794 PROBE1(stap, pass3__end, &s);
795
796 if (rc || s.last_pass == 3 || pending_interrupts) return rc;
797
798 // PASS 4: COMPILATION
799 s.verbose = s.perpass_verbose[3];
800 times (& tms_before);
801 gettimeofday (&tv_before, NULL);
802 PROBE1(stap, pass4__start, &s);
803
804 if (s.use_cache)
805 {
806 find_stapconf_hash(s);
807 get_stapconf_from_cache(s);
808 }
809 rc = compile_pass (s);
810 if (! rc && s.last_pass == 4)
811 {
812 cout << ((s.hash_path == "") ? (s.module_name + string(".ko")) : s.hash_path);
813 cout << endl;
814 }
815
816 times (& tms_after);
817 gettimeofday (&tv_after, NULL);
818
819 if (s.verbose) clog << "Pass 4: compiled C into \""
820 << s.module_name << ".ko"
821 << "\" "
822 << TIMESPRINT
823 << endl;
824
825 if (rc && ! s.try_server ())
826 cerr << _("Pass 4: compilation failed. Try again with another '--vp 0001' option.") << endl;
827 //cerr << "Pass 4: compilation failed. "
828 // << "Try again with another '--vp 0001' option."
829 // << endl;
830 else
831 {
832 // Update cache. Cache cleaning is kicked off at the beginning of this function.
833 if (s.use_script_cache)
834 add_script_to_cache(s);
835 if (s.use_cache)
836 add_stapconf_to_cache(s);
837
838 // We may need to save the module in $CWD if the cache was
839 // inaccessible for some reason.
840 if (! s.use_script_cache && s.last_pass == 4)
841 s.save_module = true;
842
843 // Copy module to the current directory.
844 if (s.save_module && !pending_interrupts)
845 {
846 string module_src_path = s.tmpdir + "/" + s.module_name + ".ko";
847 string module_dest_path = s.module_name + ".ko";
848 copy_file(module_src_path, module_dest_path, s.verbose > 1);
849 }
850 }
851
852 PROBE1(stap, pass4__end, &s);
853
854 return rc;
855 }
856
857 static int
858 pass_5 (systemtap_session &s, vector<remote*> targets)
859 {
860 // PASS 5: RUN
861 s.verbose = s.perpass_verbose[4];
862 struct tms tms_before;
863 times (& tms_before);
864 struct timeval tv_before;
865 gettimeofday (&tv_before, NULL);
866 // NB: this message is a judgement call. The other passes don't emit
867 // a "hello, I'm starting" message, but then the others aren't interactive
868 // and don't take an indefinite amount of time.
869 PROBE1(stap, pass5__start, &s);
870 if (s.verbose) clog << _("Pass 5: starting run.") << endl;
871 int rc = remote::run(targets);
872 struct tms tms_after;
873 times (& tms_after);
874 unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
875 struct timeval tv_after;
876 gettimeofday (&tv_after, NULL);
877 if (s.verbose) clog << "Pass 5: run completed "
878 << TIMESPRINT
879 << endl;
880
881 if (rc)
882 cerr << _("Pass 5: run failed. Try again with another '--vp 00001' option.") << endl;
883 //cerr << "Pass 5: run failed. "
884 // << "Try again with another '--vp 00001' option."
885 // << endl;
886 else
887 // Interrupting pass-5 to quit is normal, so we want an EXIT_SUCCESS below.
888 pending_interrupts = 0;
889
890 PROBE1(stap, pass5__end, &s);
891
892 return rc;
893 }
894
895 static void
896 cleanup (systemtap_session &s, int rc)
897 {
898 // PASS 6: cleaning up
899 PROBE1(stap, pass6__start, &s);
900
901 for (systemtap_session::session_map_t::iterator it = s.subsessions.begin();
902 it != s.subsessions.end(); ++it)
903 cleanup (*it->second, rc);
904
905 // update the database information
906 if (!rc && s.tapset_compile_coverage && !pending_interrupts) {
907 #ifdef HAVE_LIBSQLITE3
908 update_coverage_db(s);
909 #else
910 cerr << _("Coverage database not available without libsqlite3") << endl;
911 #endif
912 }
913
914 // Clean up temporary directory. Obviously, be careful with this.
915 remove_temp_dir (s);
916
917 PROBE1(stap, pass6__end, &s);
918 }
919
920 static int
921 passes_0_4_again_with_server (systemtap_session &s)
922 {
923 // Not a server and not already using a server.
924 assert (! s.client_options);
925 assert (s.specified_servers.empty ());
926
927 // Specify default server(s).
928 s.specified_servers.push_back ("");
929
930 // Remove the previous temporary directory and start fresh.
931 remove_temp_dir (s);
932
933 // Try to compile again, using the server
934 clog << _("Attempting compilation using a compile server")
935 << endl;
936 int rc = passes_0_4 (s);
937 return rc;
938 }
939
940 int
941 main (int argc, char * const argv [])
942 {
943 // Initialize defaults.
944 systemtap_session s;
945
946 setlocale (LC_ALL, "");
947 bindtextdomain (PACKAGE, LOCALEDIR);
948 textdomain (PACKAGE);
949
950 // Set up our handler to catch routine signals, to allow clean
951 // and reasonably timely exit.
952 setup_signals(&handle_interrupt);
953
954 // Process the command line.
955 int rc = s.parse_cmdline (argc, argv);
956 if (rc != 0)
957 exit (rc);
958
959 // Check for options conflicts. Exits if errors are detected.
960 s.check_options (argc, argv);
961
962 // arguments parsed; get down to business
963 if (s.verbose > 1)
964 s.version ();
965
966 // Some of the remote methods need to write temporary data, so go ahead
967 // and create the main tempdir now.
968 rc = create_temp_dir (s);
969
970 // Prepare connections for each specified remote target.
971 vector<remote*> targets;
972 if (s.remote_uris.empty())
973 s.remote_uris.push_back("direct:");
974 for (unsigned i = 0; rc == 0 && i < s.remote_uris.size(); ++i)
975 {
976 remote *target = remote::create(s, s.remote_uris[i]);
977 if (target)
978 targets.push_back(target);
979 else
980 rc = 1;
981 }
982
983 // Discover and loop over each unique session created by the remote targets.
984 set<systemtap_session*> sessions;
985 for (unsigned i = 0; i < targets.size(); ++i)
986 sessions.insert(targets[i]->get_session());
987 for (set<systemtap_session*>::iterator it = sessions.begin();
988 rc == 0 && !pending_interrupts && it != sessions.end(); ++it)
989 {
990 systemtap_session& ss = **it;
991 if (ss.verbose > 1)
992 clog << _F("Session arch: %s release: %s",
993 ss.architecture.c_str(), ss.kernel_release.c_str()) << endl;
994
995 // If requested, query server status. This is independent of other tasks.
996 query_server_status (ss);
997
998 // If requested, manage trust of servers. This is independent of other tasks.
999 manage_server_trust (ss);
1000
1001 // Run the passes only if a script has been specified. The requirement for
1002 // a script has already been checked in systemtap_session::check_options.
1003 // Run the passes also if a dump of supported probe types has been requested via a server.
1004 if (ss.have_script || (ss.dump_probe_types && ! s.specified_servers.empty ()))
1005 {
1006 // Run passes 0-4 for each unique session,
1007 // either locally or using a compile-server.
1008 ss.init_try_server ();
1009 if ((rc = passes_0_4 (ss)))
1010 {
1011 // Compilation failed.
1012 // Try again using a server if appropriate.
1013 if (ss.try_server ())
1014 rc = passes_0_4_again_with_server (ss);
1015 }
1016 }
1017 else if (ss.dump_probe_types)
1018 {
1019 // Dump a list of known probe point types, if requested.
1020 register_standard_tapsets(ss);
1021 ss.pattern_root->dump (ss);
1022 }
1023 }
1024
1025 // Run pass 5, if requested
1026 if (rc == 0 && s.have_script && s.last_pass >= 5 && ! pending_interrupts)
1027 rc = pass_5 (s, targets);
1028
1029 // Pass 6. Cleanup
1030 for (unsigned i = 0; i < targets.size(); ++i)
1031 delete targets[i];
1032 cleanup (s, rc);
1033
1034 return (rc||pending_interrupts) ? EXIT_FAILURE : EXIT_SUCCESS;
1035 }
1036
1037 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.096211 seconds and 6 git commands to generate.