]> sourceware.org Git - systemtap.git/blob - main.cxx
Update parse_kernel_exports() for RHEL4.
[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 // RHEL4 Module.symvers file only has 3 tokens. No
362 // 'EXPORT_SYMBOL' token at the end of the line.
363 else if (tokens.size() == 3 && tokens[2] == "vmlinux")
364 s.kernel_exports.insert (tokens[1]);
365 }
366 if (s.verbose > 2)
367 clog << _F(ngettext("Parsed kernel %s, which contained one vmlinux export",
368 "Parsed kernel %s, which contained %zu vmlinux exports",
369 s.kernel_exports.size()), kernel_exports_file.c_str(),
370 s.kernel_exports.size()) << endl;
371
372 kef.close();
373 return 0;
374 }
375
376
377 static int
378 create_temp_dir (systemtap_session &s)
379 {
380 if (!s.tmpdir.empty())
381 return 0;
382
383 // Create a temporary directory to build within.
384 // Be careful with this, as "tmpdir" is "rm -rf"'d at the end.
385 const char * tmpdir_env = getenv("TMPDIR");
386 if (!tmpdir_env)
387 tmpdir_env = "/tmp";
388
389 string stapdir = "/stapXXXXXX";
390 string tmpdirt = tmpdir_env + stapdir;
391 const char *tmpdir_name = mkdtemp((char *)tmpdirt.c_str());
392 if (! tmpdir_name)
393 {
394 const char* e = strerror(errno);
395 //TRANSLATORS: we can't make the directory due to the error
396 cerr << _F("ERROR: cannot create temporary directory (\" %s \"): %s", tmpdirt.c_str(), e) << endl;
397 return 1;
398 }
399 else
400 s.tmpdir = tmpdir_name;
401
402 if (s.verbose>1)
403 clog << _F("Created temporary directory \"%s\"", s.tmpdir.c_str()) << endl;
404 return 0;
405 }
406
407 static void
408 remove_temp_dir(systemtap_session &s)
409 {
410 if (!s.tmpdir.empty())
411 {
412 if (s.keep_tmpdir && !s.tmpdir_opt_set)
413 clog << _F("Keeping temporary directory \"%s\"", s.tmpdir.c_str()) << endl;
414 else if (!s.tmpdir_opt_set)
415 {
416 // Mask signals while we're deleting the temporary directory.
417 stap_sigmasker masked;
418
419 // Remove the temporary directory.
420 vector<string> cleanupcmd;
421 cleanupcmd.push_back("rm");
422 cleanupcmd.push_back("-rf");
423 cleanupcmd.push_back(s.tmpdir);
424
425 (void) stap_system(s.verbose, cleanupcmd);
426 s.tmpdir.clear();
427 }
428 }
429 }
430
431 // Compilation passes 0 through 4
432 static int
433 passes_0_4 (systemtap_session &s)
434 {
435 int rc = 0;
436
437 // If we don't know the release, there's no hope either locally or on a server.
438 if (s.kernel_release.empty())
439 {
440 if (s.kernel_build_tree.empty())
441 cerr << _("ERROR: kernel release isn't specified") << endl;
442 else
443 cerr << _F("ERROR: kernel release isn't found in \"%s\"",
444 s.kernel_build_tree.c_str()) << endl;
445 return 1;
446 }
447
448 // Create a temporary directory to build within.
449 // Be careful with this, as "s.tmpdir" is "rm -rf"'d at the end.
450 rc = create_temp_dir (s);
451 if (rc)
452 return rc;
453
454 // Perform passes 0 through 4 using a compile server?
455 if (! s.specified_servers.empty ())
456 {
457 #if HAVE_NSS
458 compile_server_client client (s);
459 int rc = client.passes_0_4 ();
460 // Need to give a user a better diagnostic, if she didn't
461 // even ask for a server
462 if (rc && s.automatic_server_mode) {
463 cerr << _("Note: --use-server --unprivileged was selected because of stapusr membership.") << endl;
464 }
465 return rc;
466 #else
467 cerr << _("WARNING: Without NSS, using a compile-server is not supported by this version of systemtap") << endl;
468 // This cannot be an attempt to use a server after a local compile failed
469 // since --use-server-on-error is locked to 'no' if we don't have
470 // NSS.
471 assert (! s.try_server ());
472 #endif
473 }
474
475 // PASS 0: setting up
476 s.verbose = s.perpass_verbose[0];
477 PROBE1(stap, pass0__start, &s);
478
479 // For PR1477, we used to override $PATH and $LC_ALL and other stuff
480 // here. We seem to use complete pathnames in
481 // buildrun.cxx/tapsets.cxx now, so this is not necessary. Further,
482 // it interferes with util.cxx:find_executable(), used for $PATH
483 // resolution.
484
485 s.kernel_base_release.assign(s.kernel_release, 0, s.kernel_release.find('-'));
486
487 // Now that no further changes to s.kernel_build_tree can occur, let's use it.
488 if ((rc = parse_kernel_config (s)) != 0)
489 {
490 // Try again with a server
491 s.set_try_server ();
492 return rc;
493 }
494
495 if ((rc = parse_kernel_exports (s)) != 0)
496 {
497 // Try again with a server
498 s.set_try_server ();
499 return rc;
500 }
501
502 // Create the name of the C source file within the temporary
503 // directory.
504 s.translated_source = string(s.tmpdir) + "/" + s.module_name + ".c";
505
506 PROBE1(stap, pass0__end, &s);
507
508 struct tms tms_before;
509 times (& tms_before);
510 struct timeval tv_before;
511 gettimeofday (&tv_before, NULL);
512
513 // PASS 1a: PARSING USER SCRIPT
514 PROBE1(stap, pass1a__start, &s);
515
516 struct stat user_file_stat;
517 int user_file_stat_rc = -1;
518
519 if (s.script_file == "-")
520 {
521 s.user_file = parse (s, cin, s.guru_mode);
522 user_file_stat_rc = fstat (STDIN_FILENO, & user_file_stat);
523 }
524 else if (s.script_file != "")
525 {
526 s.user_file = parse (s, s.script_file, s.guru_mode);
527 user_file_stat_rc = stat (s.script_file.c_str(), & user_file_stat);
528 }
529 else
530 {
531 istringstream ii (s.cmdline_script);
532 s.user_file = parse (s, ii, s.guru_mode);
533 }
534 if (s.user_file == 0)
535 {
536 // Syntax errors already printed.
537 rc ++;
538 }
539
540 // Construct arch / kernel-versioning search path
541 vector<string> version_suffixes;
542 string kvr = s.kernel_release;
543 const string& arch = s.architecture;
544 // add full kernel-version-release (2.6.NN-FOOBAR) + arch
545 version_suffixes.push_back ("/" + kvr + "/" + arch);
546 version_suffixes.push_back ("/" + kvr);
547 // add kernel version (2.6.NN) + arch
548 if (kvr != s.kernel_base_release) {
549 kvr = s.kernel_base_release;
550 version_suffixes.push_back ("/" + kvr + "/" + arch);
551 version_suffixes.push_back ("/" + kvr);
552 }
553 // add kernel family (2.6) + arch
554 string::size_type dot1_index = kvr.find ('.');
555 string::size_type dot2_index = kvr.rfind ('.');
556 while (dot2_index > dot1_index && dot2_index != string::npos) {
557 kvr.erase(dot2_index);
558 version_suffixes.push_back ("/" + kvr + "/" + arch);
559 version_suffixes.push_back ("/" + kvr);
560 dot2_index = kvr.rfind ('.');
561 }
562 // add architecture search path
563 version_suffixes.push_back("/" + arch);
564 // add empty string as last element
565 version_suffixes.push_back ("");
566
567 // PASS 1b: PARSING LIBRARY SCRIPTS
568 PROBE1(stap, pass1b__start, &s);
569
570 set<pair<dev_t, ino_t> > seen_library_files;
571
572 for (unsigned i=0; i<s.include_path.size(); i++)
573 {
574 // now iterate upon it
575 for (unsigned k=0; k<version_suffixes.size(); k++)
576 {
577 glob_t globbuf;
578 string dir = s.include_path[i] + version_suffixes[k] + "/*.stp";
579 int r = glob(dir.c_str (), 0, NULL, & globbuf);
580 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
581 rc ++;
582 // GLOB_NOMATCH is acceptable
583
584 unsigned prev_s_library_files = s.library_files.size();
585
586 for (unsigned j=0; j<globbuf.gl_pathc; j++)
587 {
588 if (pending_interrupts)
589 break;
590
591 struct stat tapset_file_stat;
592 int stat_rc = stat (globbuf.gl_pathv[j], & tapset_file_stat);
593 if (stat_rc == 0 && user_file_stat_rc == 0 &&
594 user_file_stat.st_dev == tapset_file_stat.st_dev &&
595 user_file_stat.st_ino == tapset_file_stat.st_ino)
596 {
597 cerr
598 << _F("usage error: tapset file '%s' cannot be run directly as a session script.",
599 globbuf.gl_pathv[j]) << endl;
600 rc ++;
601 }
602
603 // PR11949: duplicate-eliminate tapset files
604 if (stat_rc == 0)
605 {
606 pair<dev_t,ino_t> here = make_pair(tapset_file_stat.st_dev,
607 tapset_file_stat.st_ino);
608 if (seen_library_files.find(here) != seen_library_files.end())
609 continue;
610 seen_library_files.insert (here);
611 }
612
613 // XXX: privilege only for /usr/share/systemtap?
614 stapfile* f = parse (s, globbuf.gl_pathv[j], true);
615 if (f == 0 && !s.suppress_warnings)
616 s.print_warning("tapset '" + string(globbuf.gl_pathv[j])
617 + "' has errors, and will be skipped.");
618 else
619 s.library_files.push_back (f);
620 }
621
622 unsigned next_s_library_files = s.library_files.size();
623 if (s.verbose>1 && globbuf.gl_pathc > 0)
624 //TRANSLATORS: Searching through directories, 'processed' means 'examined so far'
625 clog << _F("Searched: \" %s \", found: %zu, processed: %u",
626 dir.c_str(), globbuf.gl_pathc,
627 (next_s_library_files-prev_s_library_files)) << endl;
628
629 globfree (& globbuf);
630 }
631 }
632 if (s.num_errors())
633 rc ++;
634
635 if (rc == 0 && s.last_pass == 1)
636 {
637 cout << _("# parse tree dump") << endl;
638 s.user_file->print (cout);
639 cout << endl;
640 if (s.verbose)
641 for (unsigned i=0; i<s.library_files.size(); i++)
642 {
643 s.library_files[i]->print (cout);
644 cout << endl;
645 }
646 }
647
648 struct tms tms_after;
649 times (& tms_after);
650 unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
651 struct timeval tv_after;
652 gettimeofday (&tv_after, NULL);
653
654 #define TIMESPRINT "in " << \
655 (tms_after.tms_cutime + tms_after.tms_utime \
656 - tms_before.tms_cutime - tms_before.tms_utime) * 1000 / (_sc_clk_tck) << "usr/" \
657 << (tms_after.tms_cstime + tms_after.tms_stime \
658 - tms_before.tms_cstime - tms_before.tms_stime) * 1000 / (_sc_clk_tck) << "sys/" \
659 << ((tv_after.tv_sec - tv_before.tv_sec) * 1000 + \
660 ((long)tv_after.tv_usec - (long)tv_before.tv_usec) / 1000) << "real ms."
661
662 // syntax errors, if any, are already printed
663 if (s.verbose)
664 {
665 clog << "Pass 1: parsed user script and "
666 << s.library_files.size()
667 << " library script(s) "
668 << getmemusage()
669 << TIMESPRINT
670 << endl;
671 }
672
673 if (rc && !s.listing_mode)
674 cerr << _("Pass 1: parse failed. Try again with another '--vp 1' option.") << endl;
675 //cerr << "Pass 1: parse failed. "
676 // << "Try again with another '--vp 1' option."
677 // << endl;
678
679 PROBE1(stap, pass1__end, &s);
680
681 if (rc || s.last_pass == 1 || pending_interrupts) return rc;
682
683 times (& tms_before);
684 gettimeofday (&tv_before, NULL);
685
686 // PASS 2: ELABORATION
687 s.verbose = s.perpass_verbose[1];
688 PROBE1(stap, pass2__start, &s);
689 rc = semantic_pass (s);
690
691 if (s.listing_mode || (rc == 0 && s.last_pass == 2))
692 printscript(s, cout);
693
694 times (& tms_after);
695 gettimeofday (&tv_after, NULL);
696
697 if (s.verbose) clog << "Pass 2: analyzed script: "
698 << s.probes.size() << " probe(s), "
699 << s.functions.size() << " function(s), "
700 << s.embeds.size() << " embed(s), "
701 << s.globals.size() << " global(s) "
702 << getmemusage()
703 << TIMESPRINT
704 << endl;
705
706 if (rc && !s.listing_mode && !s.try_server ())
707 cerr << _("Pass 2: analysis failed. Try again with another '--vp 01' option.") << endl;
708 //cerr << "Pass 2: analysis failed. "
709 // << "Try again with another '--vp 01' option."
710 // << endl;
711
712 /* Print out list of missing files. XXX should be "if (rc)" ? */
713 missing_rpm_list_print(s,"-debuginfo");
714
715 PROBE1(stap, pass2__end, &s);
716
717 if (rc || s.listing_mode || s.last_pass == 2 || pending_interrupts) return rc;
718
719 rc = prepare_translate_pass (s);
720 if (rc || pending_interrupts) return rc;
721
722 // Generate hash. There isn't any point in generating the hash
723 // if last_pass is 2, since we'll quit before using it.
724 if (s.use_script_cache)
725 {
726 ostringstream o;
727 unsigned saved_verbose;
728
729 {
730 // Make sure we're in verbose mode, so that printscript()
731 // will output function/probe bodies.
732 saved_verbose = s.verbose;
733 s.verbose = 3;
734 printscript(s, o); // Print script to 'o'
735 s.verbose = saved_verbose;
736 }
737
738 // Generate hash
739 find_script_hash (s, o.str());
740
741 // See if we can use cached source/module.
742 if (get_script_from_cache(s))
743 {
744 // We may still need to build uprobes, if it's not also cached.
745 if (s.need_uprobes)
746 rc = uprobes_pass(s);
747
748 // If our last pass isn't 5, we're done (since passes 3 and
749 // 4 just generate what we just pulled out of the cache).
750 if (rc || s.last_pass < 5 || pending_interrupts)
751 return rc;
752
753 // Short-circuit to pass 5.
754 return 0;
755 }
756 }
757
758 // PASS 3: TRANSLATION
759 s.verbose = s.perpass_verbose[2];
760 times (& tms_before);
761 gettimeofday (&tv_before, NULL);
762 PROBE1(stap, pass3__start, &s);
763
764 rc = translate_pass (s);
765 if (! rc && s.last_pass == 3)
766 {
767 ifstream i (s.translated_source.c_str());
768 cout << i.rdbuf();
769 }
770
771 times (& tms_after);
772 gettimeofday (&tv_after, NULL);
773
774 if (s.verbose)
775 clog << "Pass 3: translated to C into \""
776 << s.translated_source
777 << "\" "
778 << getmemusage()
779 << TIMESPRINT
780 << endl;
781
782 if (rc && ! s.try_server ())
783 cerr << _("Pass 3: translation failed. Try again with another '--vp 001' option.") << endl;
784 //cerr << "Pass 3: translation failed. "
785 // << "Try again with another '--vp 001' option."
786 // << endl;
787
788 PROBE1(stap, pass3__end, &s);
789
790 if (rc || s.last_pass == 3 || pending_interrupts) return rc;
791
792 // PASS 4: COMPILATION
793 s.verbose = s.perpass_verbose[3];
794 times (& tms_before);
795 gettimeofday (&tv_before, NULL);
796 PROBE1(stap, pass4__start, &s);
797
798 if (s.use_cache)
799 {
800 find_stapconf_hash(s);
801 get_stapconf_from_cache(s);
802 }
803 rc = compile_pass (s);
804 if (! rc && s.last_pass == 4)
805 {
806 cout << ((s.hash_path == "") ? (s.module_name + string(".ko")) : s.hash_path);
807 cout << endl;
808 }
809
810 times (& tms_after);
811 gettimeofday (&tv_after, NULL);
812
813 if (s.verbose) clog << "Pass 4: compiled C into \""
814 << s.module_name << ".ko"
815 << "\" "
816 << TIMESPRINT
817 << endl;
818
819 if (rc && ! s.try_server ())
820 cerr << _("Pass 4: compilation failed. Try again with another '--vp 0001' option.") << endl;
821 //cerr << "Pass 4: compilation failed. "
822 // << "Try again with another '--vp 0001' option."
823 // << endl;
824 else
825 {
826 // Update cache. Cache cleaning is kicked off at the beginning of this function.
827 if (s.use_script_cache)
828 add_script_to_cache(s);
829 if (s.use_cache)
830 add_stapconf_to_cache(s);
831
832 // We may need to save the module in $CWD if the cache was
833 // inaccessible for some reason.
834 if (! s.use_script_cache && s.last_pass == 4)
835 s.save_module = true;
836
837 // Copy module to the current directory.
838 if (s.save_module && !pending_interrupts)
839 {
840 string module_src_path = s.tmpdir + "/" + s.module_name + ".ko";
841 string module_dest_path = s.module_name + ".ko";
842 copy_file(module_src_path, module_dest_path, s.verbose > 1);
843 }
844 }
845
846 PROBE1(stap, pass4__end, &s);
847
848 return rc;
849 }
850
851 static int
852 pass_5 (systemtap_session &s, vector<remote*> targets)
853 {
854 // PASS 5: RUN
855 s.verbose = s.perpass_verbose[4];
856 struct tms tms_before;
857 times (& tms_before);
858 struct timeval tv_before;
859 gettimeofday (&tv_before, NULL);
860 // NB: this message is a judgement call. The other passes don't emit
861 // a "hello, I'm starting" message, but then the others aren't interactive
862 // and don't take an indefinite amount of time.
863 PROBE1(stap, pass5__start, &s);
864 if (s.verbose) clog << _("Pass 5: starting run.") << endl;
865 int rc = remote::run(targets);
866 struct tms tms_after;
867 times (& tms_after);
868 unsigned _sc_clk_tck = sysconf (_SC_CLK_TCK);
869 struct timeval tv_after;
870 gettimeofday (&tv_after, NULL);
871 if (s.verbose) clog << "Pass 5: run completed "
872 << TIMESPRINT
873 << endl;
874
875 if (rc)
876 cerr << _("Pass 5: run failed. Try again with another '--vp 00001' option.") << endl;
877 //cerr << "Pass 5: run failed. "
878 // << "Try again with another '--vp 00001' option."
879 // << endl;
880 else
881 // Interrupting pass-5 to quit is normal, so we want an EXIT_SUCCESS below.
882 pending_interrupts = 0;
883
884 PROBE1(stap, pass5__end, &s);
885
886 return rc;
887 }
888
889 static void
890 cleanup (systemtap_session &s, int rc)
891 {
892 // PASS 6: cleaning up
893 PROBE1(stap, pass6__start, &s);
894
895 for (systemtap_session::session_map_t::iterator it = s.subsessions.begin();
896 it != s.subsessions.end(); ++it)
897 cleanup (*it->second, rc);
898
899 // update the database information
900 if (!rc && s.tapset_compile_coverage && !pending_interrupts) {
901 #ifdef HAVE_LIBSQLITE3
902 update_coverage_db(s);
903 #else
904 cerr << _("Coverage database not available without libsqlite3") << endl;
905 #endif
906 }
907
908 // Clean up temporary directory. Obviously, be careful with this.
909 remove_temp_dir (s);
910
911 PROBE1(stap, pass6__end, &s);
912 }
913
914 static int
915 passes_0_4_again_with_server (systemtap_session &s)
916 {
917 // Not a server and not already using a server.
918 assert (! s.client_options);
919 assert (s.specified_servers.empty ());
920
921 // Specify default server(s).
922 s.specified_servers.push_back ("");
923
924 // Remove the previous temporary directory and start fresh.
925 remove_temp_dir (s);
926
927 // Try to compile again, using the server
928 clog << _("Attempting compilation using a compile server")
929 << endl;
930 int rc = passes_0_4 (s);
931 return rc;
932 }
933
934 int
935 main (int argc, char * const argv [])
936 {
937 // Initialize defaults.
938 systemtap_session s;
939
940 setlocale (LC_ALL, "");
941 bindtextdomain (PACKAGE, LOCALEDIR);
942 textdomain (PACKAGE);
943
944 // Set up our handler to catch routine signals, to allow clean
945 // and reasonably timely exit.
946 setup_signals(&handle_interrupt);
947
948 // Process the command line.
949 int rc = s.parse_cmdline (argc, argv);
950 if (rc != 0)
951 exit (rc);
952
953 // Check for options conflicts. Exits if errors are detected.
954 s.check_options (argc, argv);
955
956 // arguments parsed; get down to business
957 if (s.verbose > 1)
958 s.version ();
959
960 // Some of the remote methods need to write temporary data, so go ahead
961 // and create the main tempdir now.
962 rc = create_temp_dir (s);
963
964 // Prepare connections for each specified remote target.
965 vector<remote*> targets;
966 if (s.remote_uris.empty())
967 s.remote_uris.push_back("direct:");
968 for (unsigned i = 0; rc == 0 && i < s.remote_uris.size(); ++i)
969 {
970 remote *target = remote::create(s, s.remote_uris[i]);
971 if (target)
972 targets.push_back(target);
973 else
974 rc = 1;
975 }
976
977 // Discover and loop over each unique session created by the remote targets.
978 set<systemtap_session*> sessions;
979 for (unsigned i = 0; i < targets.size(); ++i)
980 sessions.insert(targets[i]->get_session());
981 for (set<systemtap_session*>::iterator it = sessions.begin();
982 rc == 0 && !pending_interrupts && it != sessions.end(); ++it)
983 {
984 systemtap_session& ss = **it;
985 if (ss.verbose > 1)
986 clog << _F("Session arch: %s release: %s",
987 ss.architecture.c_str(), ss.kernel_release.c_str()) << endl;
988
989 // If requested, query server status. This is independent of other tasks.
990 query_server_status (ss);
991
992 // If requested, manage trust of servers. This is independent of other tasks.
993 manage_server_trust (ss);
994
995 // Run the passes only if a script has been specified. The requirement for
996 // a script has already been checked in systemtap_session::check_options.
997 if (ss.have_script)
998 {
999 // Run passes 0-4 for each unique session,
1000 // either locally or using a compile-server.
1001 ss.init_try_server ();
1002 if ((rc = passes_0_4 (ss)))
1003 {
1004 // Compilation failed.
1005 // Try again using a server if appropriate.
1006 if (ss.try_server ())
1007 rc = passes_0_4_again_with_server (ss);
1008 }
1009 }
1010 }
1011
1012 // Run pass 5, if requested
1013 if (rc == 0 && s.have_script && s.last_pass >= 5 && ! pending_interrupts)
1014 rc = pass_5 (s, targets);
1015
1016 // Pass 6. Cleanup
1017 for (unsigned i = 0; i < targets.size(); ++i)
1018 delete targets[i];
1019 cleanup (s, rc);
1020
1021 return (rc||pending_interrupts) ? EXIT_FAILURE : EXIT_SUCCESS;
1022 }
1023
1024 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.0927750000000001 seconds and 6 git commands to generate.