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