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