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