]> sourceware.org Git - systemtap.git/blob - elaborate.cxx
buildrun.cxx: adapt to kernel 5.4+
[systemtap.git] / elaborate.cxx
1 // elaboration functions
2 // Copyright (C) 2005-2019 Red Hat Inc.
3 // Copyright (C) 2008 Intel Corporation
4 //
5 // This file is part of systemtap, and is free software. You can
6 // redistribute it and/or modify it under the terms of the GNU General
7 // Public License (GPL); either version 2, or (at your option) any
8 // later version.
9
10 #include "config.h"
11 #include "elaborate.h"
12 #include "translate.h"
13 #include "parse.h"
14 #include "tapsets.h"
15 #include "session.h"
16 #include "util.h"
17 #include "task_finder.h"
18 #include "stapregex.h"
19 #include "stringtable.h"
20
21 extern "C" {
22 #include <sys/utsname.h>
23 #include <fnmatch.h>
24 #define __STDC_FORMAT_MACROS
25 #include <inttypes.h>
26 }
27
28 #include <algorithm>
29 #include <fstream>
30 #include <map>
31 #include <cassert>
32 #include <set>
33 #include <vector>
34 #include <algorithm>
35 #include <iterator>
36 #include <climits>
37
38
39 using namespace std;
40
41
42 // ------------------------------------------------------------------------
43
44 // Used in probe_point condition construction. Either argument may be
45 // NULL; if both, return NULL too. Resulting expression is a deep
46 // copy for symbol resolution purposes.
47 expression* add_condition (expression* a, expression* b)
48 {
49 if (!a && !b) return 0;
50 if (! a) return deep_copy_visitor::deep_copy(b);
51 if (! b) return deep_copy_visitor::deep_copy(a);
52 logical_and_expr la;
53 la.op = "&&";
54 la.left = a;
55 la.right = b;
56 la.tok = a->tok; // or could be b->tok
57 return deep_copy_visitor::deep_copy(& la);
58 }
59
60 // ------------------------------------------------------------------------
61
62
63
64 derived_probe::derived_probe (probe *p, probe_point *l, bool rewrite_loc):
65 base (p), base_pp(l), group(NULL), sdt_semaphore_addr(0),
66 session_index((unsigned)-1)
67 {
68 assert (p);
69 this->tok = p->tok;
70 this->privileged = p->privileged;
71 this->synthetic = p->synthetic;
72 this->body = deep_copy_visitor::deep_copy(p->body);
73
74 assert (l);
75 // make a copy for subclasses which want to rewrite the location
76 if (rewrite_loc)
77 l = new probe_point(*l);
78 this->locations.push_back (l);
79 }
80
81
82 void
83 derived_probe::printsig (ostream& o, bool nest) const
84 {
85 probe::printsig (o);
86
87 if (nest)
88 printsig_nested (o);
89 }
90
91 void
92 derived_probe::printsig_nested (ostream& o) const
93 {
94 // We'd like to enclose the probe derivation chain in a /* */
95 // comment delimiter. But just printing /* base->printsig() */ is
96 // not enough, since base might itself be a derived_probe. So we,
97 // er, "cleverly" encode our nesting state as a formatting flag for
98 // the ostream.
99 ios::fmtflags f = o.flags (ios::internal);
100 if (f & ios::internal)
101 {
102 // already nested
103 o << " <- ";
104 base->printsig (o);
105 }
106 else
107 {
108 // outermost nesting
109 o << " /* <- ";
110 base->printsig (o);
111 o << " */";
112 }
113 // restore flags
114 (void) o.flags (f);
115 }
116
117
118 void
119 derived_probe::collect_derivation_chain (std::vector<probe*> &probes_list) const
120 {
121 probes_list.push_back(const_cast<derived_probe*>(this));
122 base->collect_derivation_chain(probes_list);
123 }
124
125
126 void
127 derived_probe::collect_derivation_pp_chain (std::vector<probe_point*> &pp_list) const
128 {
129 pp_list.push_back(const_cast<probe_point*>(this->sole_location()));
130 base->collect_derivation_pp_chain(pp_list);
131 }
132
133
134 string
135 derived_probe::derived_locations (bool firstFrom)
136 {
137 ostringstream o;
138 vector<probe_point*> reference_point;
139 collect_derivation_pp_chain(reference_point);
140 if (reference_point.size() > 0)
141 for(unsigned i=1; i<reference_point.size(); ++i)
142 {
143 if (firstFrom || i>1)
144 o << " from: ";
145 o << reference_point[i]->str(false); // no ?,!,etc
146 }
147 return o.str();
148 }
149
150
151 probe_point*
152 derived_probe::sole_location () const
153 {
154 if (locations.size() == 0 || locations.size() > 1)
155 throw SEMANTIC_ERROR (_N("derived_probe with no locations",
156 "derived_probe with too many locations",
157 locations.size()), this->tok);
158 else
159 return locations[0];
160 }
161
162
163 probe_point*
164 derived_probe::script_location () const
165 {
166 // This feeds function::pn() in the tapset, which is documented as the
167 // script-level probe point expression, *after wildcard expansion*.
168 vector<probe_point*> chain;
169 collect_derivation_pp_chain (chain);
170
171 // Go backwards until we hit the first well-formed probe point
172 for (int i=chain.size()-1; i>=0; i--)
173 if (chain[i]->well_formed)
174 return chain[i];
175
176 // If that didn't work, just fallback to -something-.
177 return sole_location();
178 }
179
180
181 void
182 derived_probe::emit_privilege_assertion (translator_output* o)
183 {
184 // Emit code which will cause compilation to fail if it is compiled in
185 // unprivileged mode.
186 o->newline() << "#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \\";
187 o->newline() << " ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS)";
188 o->newline() << "#error Internal Error: Probe ";
189 probe::printsig (o->line());
190 o->line() << " generated in --unprivileged mode";
191 o->newline() << "#endif";
192 }
193
194
195 void
196 derived_probe::emit_process_owner_assertion (translator_output* o)
197 {
198 // Emit code which will abort should the current target not belong to the
199 // user in unprivileged mode.
200 o->newline() << "#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \\";
201 o->newline() << " ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS)";
202 o->newline(1) << "if (! is_myproc ()) {";
203 o->newline(1) << "snprintf(c->error_buffer, sizeof(c->error_buffer),";
204 o->newline() << " \"Internal Error: Process %d does not belong to user %d in probe %s in --unprivileged mode\",";
205 o->newline() << " current->tgid, _stp_uid, c->probe_point);";
206 o->newline() << "c->last_error = c->error_buffer;";
207 // NB: since this check occurs before probe locking, its exit should
208 // not be a "goto out", which would attempt unlocking.
209 o->newline() << "return;";
210 o->newline(-1) << "}";
211 o->newline(-1) << "#endif";
212 }
213
214 void
215 derived_probe::print_dupe_stamp_unprivileged(ostream& o)
216 {
217 o << _("unprivileged users: authorized") << endl;
218 }
219
220 void
221 derived_probe::print_dupe_stamp_unprivileged_process_owner(ostream& o)
222 {
223 o << _("unprivileged users: authorized for process owner") << endl;
224 }
225
226 // ------------------------------------------------------------------------
227 // Members of derived_probe_builder
228
229 void
230 derived_probe_builder::build_with_suffix(systemtap_session &,
231 probe *,
232 probe_point *,
233 literal_map_t const &,
234 std::vector<derived_probe *> &,
235 std::vector<probe_point::component *>
236 const &) {
237 // XXX perhaps build the probe if suffix is empty?
238 // if (suffix.empty()) {
239 // build (sess, use, location, parameters, finished_results);
240 // return;
241 // }
242 throw SEMANTIC_ERROR (_("invalid suffix for probe"));
243 }
244
245 bool
246 derived_probe_builder::get_param (literal_map_t const & params,
247 interned_string key,
248 interned_string& value)
249 {
250 literal_map_t::const_iterator i = params.find (key);
251 if (i == params.end())
252 return false;
253 literal_string * ls = dynamic_cast<literal_string *>(i->second);
254 if (!ls)
255 return false;
256 value = ls->value;
257 return true;
258 }
259
260
261 bool
262 derived_probe_builder::get_param (literal_map_t const & params,
263 interned_string key,
264 int64_t& value)
265 {
266 literal_map_t::const_iterator i = params.find (key);
267 if (i == params.end())
268 return false;
269 if (i->second == NULL)
270 return false;
271 literal_number * ln = dynamic_cast<literal_number *>(i->second);
272 if (!ln)
273 return false;
274 value = ln->value;
275 return true;
276 }
277
278
279 bool
280 derived_probe_builder::has_null_param (literal_map_t const & params,
281 interned_string key)
282 {
283 literal_map_t::const_iterator i = params.find(key);
284 return (i != params.end() && i->second == NULL);
285 }
286
287 bool
288 derived_probe_builder::has_param (literal_map_t const & params,
289 interned_string key)
290 {
291 return (params.find(key) != params.end());
292 }
293
294 // ------------------------------------------------------------------------
295 // Members of match_key.
296
297 match_key::match_key(interned_string n)
298 : name(n),
299 have_parameter(false),
300 parameter_type(pe_unknown)
301 {
302 }
303
304 match_key::match_key(probe_point::component const & c)
305 : name(c.functor),
306 have_parameter(c.arg != NULL),
307 parameter_type(c.arg ? c.arg->type : pe_unknown)
308 {
309 }
310
311 match_key &
312 match_key::with_number()
313 {
314 have_parameter = true;
315 parameter_type = pe_long;
316 return *this;
317 }
318
319 match_key &
320 match_key::with_string()
321 {
322 have_parameter = true;
323 parameter_type = pe_string;
324 return *this;
325 }
326
327 string
328 match_key::str() const
329 {
330 string n = name;
331 if (have_parameter)
332 switch (parameter_type)
333 {
334 case pe_string: return n + "(string)";
335 case pe_long: return n + "(number)";
336 default: return n + "(...)";
337 }
338 return n;
339 }
340
341 bool
342 match_key::operator<(match_key const & other) const
343 {
344 return ((name < other.name)
345
346 || (name == other.name
347 && have_parameter < other.have_parameter)
348
349 || (name == other.name
350 && have_parameter == other.have_parameter
351 && parameter_type < other.parameter_type));
352 }
353
354
355 // NB: these are only used in the probe point name components, where
356 // only "*" is permitted.
357 //
358 // Within module("bar"), function("foo"), process("baz") strings, real
359 // wildcards are permitted too. See also util.h:contains_glob_chars
360
361 static bool
362 isglob(interned_string str)
363 {
364 return(str.find('*') != str.npos);
365 }
366
367 static bool
368 isdoubleglob(interned_string str)
369 {
370 return(str.find("**") != str.npos);
371 }
372
373 bool
374 match_key::globmatch(match_key const & other) const
375 {
376 const string & name_str = name;
377 const string & other_str = other.name;
378
379 return ((fnmatch(name_str.c_str(), other_str.c_str(), FNM_NOESCAPE) == 0)
380 && have_parameter == other.have_parameter
381 && parameter_type == other.parameter_type);
382 }
383
384 // ------------------------------------------------------------------------
385 // Members of match_node
386 // ------------------------------------------------------------------------
387
388 match_node::match_node() :
389 privilege(privilege_t (pr_stapdev | pr_stapsys))
390 {
391 }
392
393 match_node *
394 match_node::bind(match_key const & k)
395 {
396 if (k.name == "*")
397 throw SEMANTIC_ERROR(_("invalid use of wildcard probe point component"));
398
399 map<match_key, match_node *>::const_iterator i = sub.find(k);
400 if (i != sub.end())
401 return i->second;
402 match_node * n = new match_node();
403 sub.insert(make_pair(k, n));
404 return n;
405 }
406
407 void
408 match_node::bind(derived_probe_builder * e)
409 {
410 ends.push_back (e);
411 }
412
413 match_node *
414 match_node::bind(interned_string k)
415 {
416 return bind(match_key(k));
417 }
418
419 match_node *
420 match_node::bind_str(string const & k)
421 {
422 return bind(match_key(k).with_string());
423 }
424
425 match_node *
426 match_node::bind_num(string const & k)
427 {
428 return bind(match_key(k).with_number());
429 }
430
431 match_node *
432 match_node::bind_privilege(privilege_t p)
433 {
434 privilege = p;
435 return this;
436 }
437
438 void
439 match_node::find_and_build (systemtap_session& s,
440 probe* p, probe_point *loc, unsigned pos,
441 vector<derived_probe *>& results,
442 set<string>& builders)
443 {
444 save_and_restore<unsigned> costly(& s.suppress_costly_diagnostics,
445 s.suppress_costly_diagnostics + (loc->optional || loc->sufficient ? 1 : 0));
446
447 assert (pos <= loc->components.size());
448 if (pos == loc->components.size()) // matched all probe point components so far
449 {
450 if (ends.empty())
451 {
452 string alternatives;
453 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
454 alternatives += string(" ") + i->first.str();
455
456 throw SEMANTIC_ERROR (_F("probe point truncated (follow: %s)",
457 alternatives.c_str()),
458 loc->components.back()->tok);
459 }
460
461 if (! pr_contains (privilege, s.privilege))
462 {
463 throw SEMANTIC_ERROR (_F("probe point is not allowed for --privilege=%s",
464 pr_name (s.privilege)),
465 loc->components.back()->tok);
466 }
467
468 literal_map_t param_map;
469 for (unsigned i=0; i<pos; i++)
470 param_map[loc->components[i]->functor] = loc->components[i]->arg;
471 // maybe 0
472
473 unsigned int num_results = results.size();
474
475 // Iterate over all bound builders
476 for (unsigned k=0; k<ends.size(); k++)
477 {
478 derived_probe_builder *b = ends[k];
479 b->build (s, p, loc, param_map, results);
480 }
481
482 // Collect names of builders attempted for error reporting
483 if (results.size() == num_results)
484 {
485 for (unsigned k=0; k<ends.size(); k++)
486 builders.insert(ends[k]->name());
487 }
488 }
489 else if (isdoubleglob(loc->components[pos]->functor)) // ** wildcard?
490 {
491 unsigned int num_results = results.size();
492
493 // When faced with "foo**bar", we try "foo*bar" and "foo*.**bar"
494
495 const probe_point::component *comp = loc->components[pos];
496 string functor = comp->functor;
497 size_t glob_start = functor.find("**");
498 size_t glob_end = functor.find_first_not_of('*', glob_start);
499 string prefix = functor.substr(0, glob_start);
500 string suffix = ((glob_end != string::npos) ?
501 functor.substr(glob_end) : "");
502
503 // Synthesize "foo*bar"
504 probe_point *simple_pp = new probe_point(*loc);
505 probe_point::component *simple_comp = new probe_point::component(*comp);
506 simple_comp->functor = prefix + "*" + suffix;
507 simple_comp->from_glob = true;
508 simple_pp->components[pos] = simple_comp;
509 try
510 {
511 find_and_build (s, p, simple_pp, pos, results, builders);
512 }
513 catch (const semantic_error& e)
514 {
515 // Ignore semantic_errors.
516 }
517
518 // Cleanup if we didn't find anything
519 if (results.size() == num_results)
520 {
521 delete simple_pp;
522 delete simple_comp;
523 }
524
525 num_results = results.size();
526
527 // Synthesize "foo*.**bar"
528 // NB: any component arg should attach to the latter part only
529 probe_point *expanded_pp = new probe_point(*loc);
530 probe_point::component *expanded_comp_pre = new probe_point::component(*comp);
531 expanded_comp_pre->functor = prefix + "*";
532 expanded_comp_pre->from_glob = true;
533 expanded_comp_pre->arg = NULL;
534 probe_point::component *expanded_comp_post = new probe_point::component(*comp);
535 expanded_comp_post->functor = string("**") + suffix;
536 expanded_pp->components[pos] = expanded_comp_pre;
537 expanded_pp->components.insert(expanded_pp->components.begin() + pos + 1,
538 expanded_comp_post);
539 try
540 {
541 find_and_build (s, p, expanded_pp, pos, results, builders);
542 }
543 catch (const semantic_error& e)
544 {
545 // Ignore semantic_errors.
546 }
547
548 // Cleanup if we didn't find anything
549 if (results.size() == num_results)
550 {
551 delete expanded_pp;
552 delete expanded_comp_pre;
553 delete expanded_comp_post;
554 }
555
556 // Try suffix expansion only if no matches found:
557 if (num_results == results.size())
558 this->try_suffix_expansion (s, p, loc, pos, results);
559
560 if (! loc->optional && num_results == results.size())
561 {
562 // We didn't find any wildcard matches (since the size of
563 // the result vector didn't change). Throw an error.
564 string sugs = suggest_functors(s, functor);
565 throw SEMANTIC_ERROR (_F("probe point mismatch: didn't find any wildcard matches%s",
566 sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
567 comp->tok);
568 }
569 }
570 else if (isglob(loc->components[pos]->functor)) // wildcard?
571 {
572 match_key match (* loc->components[pos]);
573
574 // Call find_and_build for each possible match. Ignore errors -
575 // unless we don't find any match.
576 unsigned int num_results = results.size();
577 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
578 {
579 const match_key& subkey = i->first;
580 match_node* subnode = i->second;
581
582 assert_no_interrupts();
583
584 if (match.globmatch(subkey))
585 {
586 if (s.verbose > 2)
587 clog << _F("wildcard '%s' matched '%s'",
588 loc->components[pos]->functor.to_string().c_str(),
589 subkey.name.to_string().c_str()) << endl;
590
591 // When we have a wildcard, we need to create a copy of
592 // the probe point. Then we'll create a copy of the
593 // wildcard component, and substitute the non-wildcard
594 // functor.
595 probe_point *non_wildcard_pp = new probe_point(*loc);
596 probe_point::component *non_wildcard_component
597 = new probe_point::component(*loc->components[pos]);
598 non_wildcard_component->functor = subkey.name;
599 non_wildcard_component->from_glob = true;
600 non_wildcard_pp->components[pos] = non_wildcard_component;
601
602 // NB: probe conditions are not attached at the wildcard
603 // (component/functor) level, but at the overall
604 // probe_point level.
605
606 unsigned int inner_results = results.size();
607
608 // recurse (with the non-wildcard probe point)
609 try
610 {
611 subnode->find_and_build (s, p, non_wildcard_pp, pos+1,
612 results, builders);
613 }
614 catch (const semantic_error& e)
615 {
616 // Ignore semantic_errors while expanding wildcards.
617 // If we get done and nothing was expanded, the code
618 // following the loop will complain.
619 }
620
621 if (results.size() == inner_results)
622 {
623 // If this wildcard didn't match, cleanup.
624 delete non_wildcard_pp;
625 delete non_wildcard_component;
626 }
627 }
628 }
629
630 // Try suffix expansion only if no matches found:
631 if (num_results == results.size())
632 this->try_suffix_expansion (s, p, loc, pos, results);
633
634 if (! loc->optional && num_results == results.size())
635 {
636 // We didn't find any wildcard matches (since the size of
637 // the result vector didn't change). Throw an error.
638 string sugs = suggest_functors(s, loc->components[pos]->functor);
639 throw SEMANTIC_ERROR (_F("probe point mismatch: didn't find any wildcard matches%s",
640 sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
641 loc->components[pos]->tok);
642 }
643 }
644 else
645 {
646 match_key match (* loc->components[pos]);
647 sub_map_iterator_t i = sub.find (match);
648
649 if (i != sub.end()) // match found
650 {
651 match_node* subnode = i->second;
652 // recurse
653 subnode->find_and_build (s, p, loc, pos+1, results, builders);
654 return;
655 }
656
657 unsigned int num_results = results.size();
658 this->try_suffix_expansion (s, p, loc, pos, results);
659
660 // XXX: how to correctly report alternatives + position numbers
661 // for alias suffixes? file a separate PR to address the issue
662 if (! loc->optional && num_results == results.size())
663 {
664 // We didn't find any alias suffixes (since the size of the
665 // result vector didn't change). Throw an error.
666 string sugs = suggest_functors(s, loc->components[pos]->functor);
667 throw SEMANTIC_ERROR (_F("probe point mismatch%s",
668 sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
669 loc->components[pos]->tok);
670 }
671 }
672 }
673
674 string
675 match_node::suggest_functors(systemtap_session& s, string functor)
676 {
677 // only use prefix if globby (and prefix is non-empty)
678 size_t glob = functor.find('*');
679 if (glob != string::npos && glob != 0)
680 functor.erase(glob);
681 if (functor.empty())
682 return "";
683
684 // PR18577: There isn't any point in generating a suggestion list if
685 // we're not going to display it.
686 if ((s.dump_mode == systemtap_session::dump_matched_probes
687 || s.dump_mode == systemtap_session::dump_matched_probes_vars)
688 && s.verbose < 2)
689 return "";
690
691 set<string> functors;
692 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
693 {
694 string ftor = i->first.str();
695 if (ftor.find('(') != string::npos) // trim any parameter
696 ftor.erase(ftor.find('('));
697 functors.insert(ftor);
698 }
699 return levenshtein_suggest(functor, functors, 5); // print top 5
700 }
701
702 void
703 match_node::try_suffix_expansion (systemtap_session& s,
704 probe *p, probe_point *loc, unsigned pos,
705 vector<derived_probe *>& results)
706 {
707 // PR12210: match alias suffixes. If the components thus far
708 // have been matched, but there is an additional unknown
709 // suffix, we have a potential alias suffix on our hands. We
710 // need to expand the preceding components as probe aliases,
711 // reattach the suffix, and re-run derive_probes() on the
712 // resulting expansion. This is done by the routine
713 // build_with_suffix().
714
715 if (strverscmp(s.compatible.c_str(), "2.0") >= 0)
716 {
717 // XXX: technically, param_map isn't used here. So don't
718 // bother actually assembling it unless some
719 // derived_probe_builder appears that actually takes
720 // suffixes *and* consults parameters (currently no such
721 // builders exist).
722 literal_map_t param_map;
723 // for (unsigned i=0; i<pos; i++)
724 // param_map[loc->components[i]->functor] = loc->components[i]->arg;
725 // maybe 0
726
727 vector<probe_point::component *> suffix (loc->components.begin()+pos,
728 loc->components.end());
729
730 // Multiple derived_probe_builders may be bound at a
731 // match_node due to the possibility of multiply defined
732 // aliases.
733 for (unsigned k=0; k < ends.size(); k++)
734 {
735 derived_probe_builder *b = ends[k];
736 try
737 {
738 b->build_with_suffix (s, p, loc, param_map, results, suffix);
739 }
740 catch (const recursive_expansion_error &e)
741 {
742 // Re-throw:
743 throw semantic_error(e);
744 }
745 catch (const semantic_error &e)
746 {
747 // Adjust source coordinate and re-throw:
748 if (! loc->optional)
749 throw semantic_error(e.errsrc, e.what(), loc->components[pos]->tok);
750 }
751 }
752 }
753 }
754
755
756 void
757 match_node::build_no_more (systemtap_session& s)
758 {
759 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
760 i->second->build_no_more (s);
761 for (unsigned k=0; k<ends.size(); k++)
762 {
763 derived_probe_builder *b = ends[k];
764 b->build_no_more (s);
765 }
766 }
767
768 void
769 match_node::dump (systemtap_session &s, const string &name)
770 {
771 // Dump this node, if it is complete.
772 for (unsigned k=0; k<ends.size(); k++)
773 {
774 // Don't print aliases at all (for now) until we can figure out how to determine whether
775 // the probes they resolve to are ok in unprivileged mode.
776 if (ends[k]->is_alias ())
777 continue;
778
779 // In unprivileged mode, don't show the probes which are not allowed for unprivileged
780 // users.
781 if (pr_contains (privilege, s.privilege))
782 {
783 cout << name << endl;
784 break; // we need only print one instance.
785 }
786 }
787
788 // Recursively dump the children of this node
789 string dot;
790 if (! name.empty ())
791 dot = ".";
792 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
793 {
794 i->second->dump (s, name + dot + i->first.str());
795 }
796 }
797
798
799 // ------------------------------------------------------------------------
800 // Alias probes
801 // ------------------------------------------------------------------------
802
803 struct alias_derived_probe: public derived_probe
804 {
805 alias_derived_probe (probe* base, probe_point *l, const probe_alias *a,
806 const vector<probe_point::component *> *suffix = 0);
807 ~alias_derived_probe();
808
809 void upchuck () { throw SEMANTIC_ERROR (_("inappropriate"), this->tok); }
810
811 // Alias probes are immediately expanded to other derived_probe
812 // types, and are not themselves emitted or listed in
813 // systemtap_session.probes
814
815 void join_group (systemtap_session&) { upchuck (); }
816
817 virtual const probe_alias *get_alias () const { return alias; }
818 virtual probe_point *get_alias_loc () const { return alias_loc; }
819 virtual probe_point *sole_location () const;
820
821 private:
822 const probe_alias *alias; // Used to check for recursion
823 probe_point *alias_loc; // Hack to recover full probe name
824 };
825
826
827 alias_derived_probe::alias_derived_probe(probe *base, probe_point *l,
828 const probe_alias *a,
829 const vector<probe_point::component *>
830 *suffix):
831 derived_probe (base, l), alias(a)
832 {
833 // XXX pretty nasty -- this was cribbed from printscript() in main.cxx
834 assert (alias->alias_names.size() >= 1);
835 alias_loc = new probe_point(*alias->alias_names[0]); // XXX: [0] is arbitrary; it would make just as much sense to collect all of the names
836 alias_loc->well_formed = true;
837 vector<probe_point::component*>::const_iterator it;
838 for (it = suffix->begin(); it != suffix->end(); ++it)
839 {
840 alias_loc->components.push_back(*it);
841 if (isglob((*it)->functor))
842 alias_loc->well_formed = false; // needs further derivation
843 }
844 }
845
846 alias_derived_probe::~alias_derived_probe ()
847 {
848 delete alias_loc;
849 }
850
851
852 probe_point*
853 alias_derived_probe::sole_location () const
854 {
855 return const_cast<probe_point*>(alias_loc);
856 }
857
858
859 void
860 alias_expansion_builder::build(systemtap_session & sess,
861 probe * use,
862 probe_point * location,
863 literal_map_t const & parameters,
864 vector<derived_probe *> & finished_results)
865 {
866 vector<probe_point::component *> empty_suffix;
867 build_with_suffix (sess, use, location, parameters,
868 finished_results, empty_suffix);
869 }
870
871 void
872 alias_expansion_builder::build_with_suffix(systemtap_session & sess,
873 probe * use,
874 probe_point * location,
875 literal_map_t const &,
876 vector<derived_probe *>
877 & finished_results,
878 vector<probe_point::component *>
879 const & suffix)
880 {
881 // Don't build the alias expansion if infinite recursion is detected.
882 if (checkForRecursiveExpansion (use)) {
883 stringstream msg;
884 msg << _F("recursive loop in alias expansion of %s at %s",
885 lex_cast(*location).c_str(), lex_cast(location->components.front()->tok->location).c_str());
886 // semantic_errors thrown here might be ignored, so we need a special class:
887 throw recursive_expansion_error (msg.str());
888 // XXX The point of throwing this custom error is to suppress a
889 // cascade of "probe mismatch" messages that appear in addition to
890 // the error. The current approach suppresses most of the error
891 // cascade, but leaves one spurious error; in any case, the way
892 // this particular error is reported could be improved.
893 }
894
895 // We're going to build a new probe and wrap it up in an
896 // alias_expansion_probe so that the expansion loop recognizes it as
897 // such and re-expands its expansion.
898
899 alias_derived_probe * n = new alias_derived_probe (use, location /* soon overwritten */, this->alias, &suffix);
900 n->body = new block();
901
902 // The new probe gets a deep copy of the location list of the alias
903 // (with incoming condition joined) plus the suffix (if any),
904 n->locations.clear();
905 for (unsigned i=0; i<alias->locations.size(); i++)
906 {
907 probe_point *pp = new probe_point(*alias->locations[i]);
908 pp->components.insert(pp->components.end(), suffix.begin(), suffix.end());
909 pp->condition = add_condition (pp->condition, location->condition);
910 n->locations.push_back(pp);
911 }
912
913 // the token location of the alias,
914 n->tok = location->components.front()->tok;
915
916 // and statements representing the concatenation of the alias'
917 // body with the use's.
918 //
919 // NB: locals are *not* copied forward, from either alias or
920 // use. The expansion should have its locals re-inferred since
921 // there's concatenated code here and we only want one vardecl per
922 // resulting variable.
923
924 if (alias->epilogue_style)
925 n->body = new block (use->body, alias->body);
926 else
927 n->body = new block (alias->body, use->body);
928
929 unsigned old_num_results = finished_results.size();
930 // If expanding for an alias suffix, be sure to pass on any errors
931 // to the caller instead of printing them in derive_probes():
932 derive_probes (sess, n, finished_results, location->optional, !suffix.empty());
933
934 // Check whether we resolved something. If so, put the
935 // whole library into the queue if not already there.
936 if (finished_results.size() > old_num_results)
937 {
938 stapfile *f = alias->tok->location.file;
939 if (find (sess.files.begin(), sess.files.end(), f)
940 == sess.files.end())
941 sess.files.push_back (f);
942 }
943 }
944
945 bool
946 alias_expansion_builder::checkForRecursiveExpansion (probe *use)
947 {
948 // Collect the derivation chain of this probe.
949 vector<probe*>derivations;
950 use->collect_derivation_chain (derivations);
951
952 // Check all probe points in the alias expansion against the currently-being-expanded probe point
953 // of each of the probes in the derivation chain, looking for a match. This
954 // indicates infinite recursion.
955 // The first element of the derivation chain will be the derived_probe representing 'use', so
956 // start the search with the second element.
957 assert (derivations.size() > 0);
958 assert (derivations[0] == use);
959 for (unsigned d = 1; d < derivations.size(); ++d) {
960 if (use->get_alias() == derivations[d]->get_alias())
961 return true; // recursion detected
962 }
963 return false;
964 }
965
966
967 // ------------------------------------------------------------------------
968 // Pattern matching
969 // ------------------------------------------------------------------------
970
971 // The match-and-expand loop.
972 void
973 derive_probes (systemtap_session& s,
974 probe *p, vector<derived_probe*>& dps,
975 bool optional,
976 bool rethrow_errors)
977 {
978 // We need a static to track whether the current probe is optional so that
979 // even if we recurse into derive_probes with optional = false, errors will
980 // still be ignored. The undo_parent_optional bool ensures we reset the
981 // static at the same level we had it set.
982 static bool parent_optional = false;
983 bool undo_parent_optional = false;
984
985 if (optional && !parent_optional)
986 {
987 parent_optional = true;
988 undo_parent_optional = true;
989 }
990
991 vector <semantic_error> optional_errs;
992
993 for (unsigned i = 0; i < p->locations.size(); ++i)
994 {
995 set<string> builders;
996 assert_no_interrupts();
997
998 probe_point *loc = p->locations[i];
999
1000 if (s.verbose > 1)
1001 clog << "derive-probes (location #" << i << "): " << *loc << " of " << *p->tok << endl;
1002
1003 try
1004 {
1005 unsigned num_atbegin = dps.size();
1006
1007 try
1008 {
1009 s.pattern_root->find_and_build (s, p, loc, 0, dps, builders); // <-- actual derivation!
1010 }
1011 catch (const semantic_error& e)
1012 {
1013 if (!loc->optional && !parent_optional)
1014 throw semantic_error(e);
1015 else /* tolerate failure for optional probe */
1016 {
1017 // remember err, we will print it (in catch block) if any
1018 // non-optional loc fails to resolve
1019 semantic_error err(ERR_SRC, _("while resolving probe point"),
1020 loc->components[0]->tok, NULL, &e);
1021 optional_errs.push_back(err);
1022 continue;
1023 }
1024 }
1025
1026 unsigned num_atend = dps.size();
1027
1028 if (! (loc->optional||parent_optional) && // something required, but
1029 num_atbegin == num_atend) // nothing new derived!
1030 throw SEMANTIC_ERROR (_("no match"));
1031
1032 if (loc->sufficient && (num_atend > num_atbegin))
1033 {
1034 if (s.verbose > 1)
1035 {
1036 clog << "Probe point ";
1037 p->locations[i]->print(clog);
1038 clog << " sufficient, skipped";
1039 for (unsigned j = i+1; j < p->locations.size(); ++j)
1040 {
1041 clog << " ";
1042 p->locations[j]->print(clog);
1043 }
1044 clog << endl;
1045 }
1046 break; // we need not try to derive for any other locations
1047 }
1048 }
1049 catch (const semantic_error& e)
1050 {
1051 // The rethrow_errors parameter lets the caller decide an
1052 // alternative to printing the error. This is necessary when
1053 // calling derive_probes() recursively during expansion of
1054 // an alias with suffix -- any message printed here would
1055 // point to the alias declaration and not the invalid suffix
1056 // usage, so the caller needs to catch the error themselves
1057 // and print a more appropriate message.
1058 if (rethrow_errors)
1059 {
1060 throw semantic_error(e);
1061 }
1062 // Only output in dump mode if -vv is supplied:
1063 else if (!s.dump_mode || (s.verbose > 1))
1064 {
1065 // print this one manually first because it's more important than
1066 // the optional errs
1067 semantic_error err(ERR_SRC, _("while resolving probe point"),
1068 loc->components[0]->tok, NULL, &e);
1069 // provide some details about which builders were tried
1070 if (s.verbose > 0 && !builders.empty())
1071 {
1072 string msg;
1073 for (auto it = builders.begin(); it != builders.end(); ++it)
1074 {
1075 if (it != builders.begin())
1076 msg.append(", ");
1077 msg.append(*it);
1078 }
1079 semantic_error err2(ERR_SRC, _F("resolution failed in %s", msg.c_str()));
1080 err2.set_chain(err);
1081 s.print_error(err2);
1082 }
1083 else
1084 {
1085 s.print_error(err);
1086 }
1087
1088 // print optional errs accumulated while visiting other probe points
1089 for (vector<semantic_error>::const_iterator it = optional_errs.begin();
1090 it != optional_errs.end(); ++it)
1091 {
1092 s.print_error(*it);
1093 }
1094 }
1095 }
1096 }
1097
1098 if (undo_parent_optional)
1099 {
1100 parent_optional = false;
1101 }
1102 }
1103
1104
1105
1106 // ------------------------------------------------------------------------
1107 //
1108 // Indexable usage checks
1109 //
1110
1111 struct symbol_fetcher
1112 : public throwing_visitor
1113 {
1114 symbol *&sym;
1115
1116 symbol_fetcher (symbol *&sym): sym(sym)
1117 {}
1118
1119 void visit_symbol (symbol* e)
1120 {
1121 sym = e;
1122 }
1123
1124 void visit_arrayindex (arrayindex* e)
1125 {
1126 e->base->visit (this);
1127 }
1128
1129 void throwone (const token* t)
1130 {
1131 throw SEMANTIC_ERROR (_("Expecting symbol or array index expression"), t);
1132 }
1133 };
1134
1135 symbol *
1136 get_symbol_within_expression (expression *e)
1137 {
1138 symbol *sym = NULL;
1139 symbol_fetcher fetcher(sym);
1140 e->visit (&fetcher);
1141 return sym; // NB: may be null!
1142 }
1143
1144 static symbol *
1145 get_symbol_within_indexable (indexable *ix)
1146 {
1147 symbol *array = NULL;
1148 hist_op *hist = NULL;
1149 classify_indexable(ix, array, hist);
1150 if (array)
1151 return array;
1152 else
1153 return get_symbol_within_expression (hist->stat);
1154 }
1155
1156 struct mutated_var_collector
1157 : public traversing_visitor
1158 {
1159 set<vardecl *> * mutated_vars;
1160
1161 mutated_var_collector (set<vardecl *> * mm)
1162 : mutated_vars (mm)
1163 {}
1164
1165 void visit_assignment(assignment* e)
1166 {
1167 if (e->type == pe_stats && e->op == "<<<")
1168 {
1169 vardecl *vd = get_symbol_within_expression (e->left)->referent;
1170 if (vd)
1171 mutated_vars->insert (vd);
1172 }
1173 traversing_visitor::visit_assignment(e);
1174 }
1175
1176 void visit_arrayindex (arrayindex *e)
1177 {
1178 if (is_active_lvalue (e))
1179 {
1180 symbol *sym;
1181 if (e->base->is_symbol (sym))
1182 mutated_vars->insert (sym->referent);
1183 else
1184 throw SEMANTIC_ERROR(_("Assignment to read-only histogram bucket"), e->tok);
1185 }
1186 traversing_visitor::visit_arrayindex (e);
1187 }
1188 };
1189
1190
1191 struct no_var_mutation_during_iteration_check
1192 : public traversing_visitor
1193 {
1194 systemtap_session & session;
1195 map<functiondecl *,set<vardecl *> *> & function_mutates_vars;
1196 vector<vardecl *> vars_being_iterated;
1197
1198 no_var_mutation_during_iteration_check
1199 (systemtap_session & sess,
1200 map<functiondecl *,set<vardecl *> *> & fmv)
1201 : session(sess), function_mutates_vars (fmv)
1202 {}
1203
1204 void visit_arrayindex (arrayindex *e)
1205 {
1206 if (is_active_lvalue(e))
1207 {
1208 vardecl *vd = get_symbol_within_indexable (e->base)->referent;
1209 if (vd)
1210 {
1211 for (unsigned i = 0; i < vars_being_iterated.size(); ++i)
1212 {
1213 vardecl *v = vars_being_iterated[i];
1214 if (v == vd)
1215 {
1216 string err = _F("variable '%s' modified during 'foreach' iteration",
1217 v->unmangled_name.to_string().c_str());
1218 session.print_error (SEMANTIC_ERROR (err, e->tok));
1219 }
1220 }
1221 }
1222 }
1223 traversing_visitor::visit_arrayindex (e);
1224 }
1225
1226 void visit_functioncall (functioncall* e)
1227 {
1228 for (unsigned fd = 0; fd < e->referents.size(); fd++)
1229 {
1230 map<functiondecl *,set<vardecl *> *>::const_iterator i
1231 = function_mutates_vars.find (e->referents[fd]);
1232
1233 if (i != function_mutates_vars.end())
1234 {
1235 for (unsigned j = 0; j < vars_being_iterated.size(); ++j)
1236 {
1237 vardecl *m = vars_being_iterated[j];
1238 if (i->second->find (m) != i->second->end())
1239 {
1240 string err = _F("function call modifies var '%s' during 'foreach' iteration",
1241 m->unmangled_name.to_string().c_str());
1242 session.print_error (SEMANTIC_ERROR (err, e->tok));
1243 }
1244 }
1245 }
1246 }
1247
1248 traversing_visitor::visit_functioncall (e);
1249 }
1250
1251 void visit_foreach_loop(foreach_loop* s)
1252 {
1253 vardecl *vd = get_symbol_within_indexable (s->base)->referent;
1254
1255 if (vd)
1256 vars_being_iterated.push_back (vd);
1257
1258 traversing_visitor::visit_foreach_loop (s);
1259
1260 if (vd)
1261 vars_being_iterated.pop_back();
1262 }
1263 };
1264
1265
1266 // ------------------------------------------------------------------------
1267
1268 struct stat_decl_collector
1269 : public traversing_visitor
1270 {
1271 systemtap_session & session;
1272
1273 stat_decl_collector(systemtap_session & sess)
1274 : session(sess)
1275 {}
1276
1277 void visit_stat_op (stat_op* e)
1278 {
1279 symbol *sym = get_symbol_within_expression (e->stat);
1280 statistic_decl new_stat = statistic_decl();
1281 int bit_shift = (e->params.size() == 0) ? 0 : e->params[0];
1282 int stat_op = STAT_OP_NONE;
1283
1284 if ((bit_shift < 0) || (bit_shift > 62))
1285 throw SEMANTIC_ERROR (_F("bit shift (%d) out of range <0..62>",
1286 bit_shift),
1287 e->tok);
1288
1289 // The following helps to track which statistical operators are being
1290 // used with given global/local variable. This information later helps
1291 // to optimize the runtime behaviour.
1292
1293 if (e->ctype == sc_count)
1294 stat_op = STAT_OP_COUNT;
1295 else if (e->ctype == sc_sum)
1296 stat_op = STAT_OP_SUM;
1297 else if (e->ctype == sc_min)
1298 stat_op = STAT_OP_MIN;
1299 else if (e->ctype == sc_max)
1300 stat_op = STAT_OP_MAX;
1301 else if (e->ctype == sc_average)
1302 stat_op = STAT_OP_AVG;
1303 else if (e->ctype == sc_variance)
1304 stat_op = STAT_OP_VARIANCE;
1305
1306 new_stat.bit_shift = bit_shift;
1307 new_stat.stat_ops |= stat_op;
1308
1309 map<interned_string, statistic_decl>::iterator i = session.stat_decls.find(sym->name);
1310 if (i == session.stat_decls.end())
1311 session.stat_decls[sym->name] = new_stat;
1312 else
1313 {
1314 i->second.stat_ops |= stat_op;
1315
1316 // The @variance operator for given stat S (i.e. call to
1317 // _stp_stat_init()) is optionally parametrizeable
1318 // (@variance(S[, N]), where N is a bit shift (the default is
1319 // N=0). The bit_shift helps to improve precision if integer
1320 // arithemtic, namely divisions ().
1321 //
1322 // Ref: https://en.wikipedia.org/wiki/Scale_factor_%28computer_science%29
1323 //
1324 if (e->tok->content != "@variance")
1325 return;
1326 else if ((i->second.bit_shift != 0)
1327 && (i->second.bit_shift != bit_shift))
1328 {
1329 // FIXME: Support multiple co-declared bit shifts
1330 // (analogy to multiple co-declared histogram types)
1331 throw SEMANTIC_ERROR (_F("conflicting bit shifts declared (previously %d)",
1332 i->second.bit_shift),
1333 e->tok);
1334 }
1335 else
1336 {
1337 i->second.bit_shift = bit_shift;
1338 }
1339 }
1340 }
1341
1342 void visit_foreach_loop (foreach_loop* s)
1343 {
1344 symbol *array;
1345 hist_op *hist;
1346
1347 classify_indexable (s->base, array, hist);
1348
1349 if (array && array->type == pe_stats
1350 && s->sort_direction
1351 && s->sort_column == 0)
1352 {
1353 int stat_op = STAT_OP_NONE;
1354
1355 switch (s->sort_aggr) {
1356 default: case sc_none: case sc_count: stat_op = STAT_OP_COUNT; break;
1357 case sc_sum: stat_op = STAT_OP_SUM; break;
1358 case sc_min: stat_op = STAT_OP_MIN; break;
1359 case sc_max: stat_op = STAT_OP_MAX; break;
1360 case sc_average: stat_op = STAT_OP_AVG; break;
1361 }
1362
1363 map<interned_string, statistic_decl>::iterator i
1364 = session.stat_decls.find(array->name);
1365
1366 if (i == session.stat_decls.end())
1367 session.stat_decls[array->name] = statistic_decl(stat_op);
1368 else
1369 i->second.stat_ops |= stat_op;
1370 }
1371
1372 traversing_visitor::visit_foreach_loop (s);
1373 }
1374
1375 void visit_assignment (assignment* e)
1376 {
1377 if (e->op == "<<<")
1378 {
1379 symbol *sym = get_symbol_within_expression (e->left);
1380 if (session.stat_decls.find(sym->name) == session.stat_decls.end())
1381 session.stat_decls[sym->name] = statistic_decl();
1382 }
1383 else
1384 traversing_visitor::visit_assignment(e);
1385 }
1386
1387 void visit_hist_op (hist_op* e)
1388 {
1389 symbol *sym = get_symbol_within_expression (e->stat);
1390 statistic_decl new_stat;
1391
1392 if (e->htype == hist_linear)
1393 {
1394 new_stat.type = statistic_decl::linear;
1395 assert (e->params.size() == 3);
1396 new_stat.linear_low = e->params[0];
1397 new_stat.linear_high = e->params[1];
1398 new_stat.linear_step = e->params[2];
1399 }
1400 else
1401 {
1402 assert (e->htype == hist_log);
1403 new_stat.type = statistic_decl::logarithmic;
1404 assert (e->params.size() == 0);
1405 }
1406
1407 map<interned_string, statistic_decl>::iterator i = session.stat_decls.find(sym->name);
1408 if (i == session.stat_decls.end())
1409 session.stat_decls[sym->name] = new_stat;
1410 else
1411 {
1412 statistic_decl & old_stat = i->second;
1413 if (!(old_stat == new_stat))
1414 {
1415 if (old_stat.type == statistic_decl::none)
1416 {
1417 i->second.type = new_stat.type;
1418 i->second.linear_low = new_stat.linear_low;
1419 i->second.linear_high = new_stat.linear_high;
1420 i->second.linear_step = new_stat.linear_step;
1421 }
1422 else
1423 {
1424 // FIXME: Support multiple co-declared histogram types
1425 semantic_error se(ERR_SRC, _F("multiple histogram types declared on '%s'",
1426 sym->name.to_string().c_str()), e->tok);
1427 session.print_error (se);
1428 }
1429 }
1430 }
1431 }
1432
1433 };
1434
1435 static int
1436 semantic_pass_stats (systemtap_session & sess)
1437 {
1438 stat_decl_collector sdc(sess);
1439
1440 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
1441 it->second->body->visit (&sdc);
1442
1443 for (unsigned i = 0; i < sess.probes.size(); ++i)
1444 sess.probes[i]->body->visit (&sdc);
1445
1446 for (unsigned i = 0; i < sess.globals.size(); ++i)
1447 {
1448 vardecl *v = sess.globals[i];
1449 if (v->type == pe_stats)
1450 {
1451
1452 if (sess.stat_decls.find(v->name) == sess.stat_decls.end())
1453 {
1454 semantic_error se(ERR_SRC, _F("unable to infer statistic parameters for global '%s'",
1455 v->unmangled_name.to_string().c_str()));
1456 sess.print_error (se);
1457 }
1458 }
1459 }
1460
1461 return sess.num_errors();
1462 }
1463
1464 // ------------------------------------------------------------------------
1465
1466 // Enforce variable-related invariants: no modification of
1467 // a foreach()-iterated array.
1468 static int
1469 semantic_pass_vars (systemtap_session & sess)
1470 {
1471
1472 map<functiondecl *, set<vardecl *> *> fmv;
1473 no_var_mutation_during_iteration_check chk(sess, fmv);
1474
1475 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
1476 {
1477 functiondecl * fn = it->second;
1478 if (fn->body)
1479 {
1480 set<vardecl *> * m = new set<vardecl *>();
1481 mutated_var_collector mc (m);
1482 fn->body->visit (&mc);
1483 fmv[fn] = m;
1484 }
1485 }
1486
1487 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
1488 {
1489 functiondecl * fn = it->second;
1490 if (fn->body) fn->body->visit (&chk);
1491 }
1492
1493 for (unsigned i = 0; i < sess.probes.size(); ++i)
1494 {
1495 if (sess.probes[i]->body)
1496 sess.probes[i]->body->visit (&chk);
1497 }
1498
1499 return sess.num_errors();
1500 }
1501
1502
1503 // ------------------------------------------------------------------------
1504
1505 // Rewrite probe condition expressions into probe bodies. Tricky and
1506 // exciting business, this. This:
1507 //
1508 // probe foo if (g1 || g2) { ... }
1509 // probe bar { ... g1 ++ ... }
1510 //
1511 // becomes:
1512 //
1513 // probe foo { if (! (g1 || g2)) next; ... }
1514 // probe bar { ... g1 ++ ...;
1515 // if (g1 || g2) %{ enable_probe_foo %} else %{ disable_probe_foo %}
1516 // }
1517 //
1518 // In other words, we perform two transformations:
1519 // (1) Inline probe condition into its body.
1520 // (2) For each probe that modifies a global var in use in any probe's
1521 // condition, re-evaluate those probes' condition at the end of that
1522 // probe's body.
1523 //
1524 // Here, we do all of (1), and half of (2): we simply collect the dependency
1525 // info between probes, which the translator will use to emit the affected
1526 // probes' condition re-evaluation. The translator will also ensure that the
1527 // conditions are evaluated using the globals' starting values prior to any
1528 // probes starting.
1529
1530 // Adds the condition expression to the front of the probe's body
1531 static void
1532 derived_probe_condition_inline (derived_probe *p)
1533 {
1534 expression* e = p->sole_location()->condition;
1535 assert(e);
1536
1537 if_statement *ifs = new if_statement ();
1538 ifs->tok = e->tok;
1539 ifs->thenblock = new next_statement ();
1540 ifs->thenblock->tok = e->tok;
1541 ifs->elseblock = NULL;
1542 unary_expression *notex = new unary_expression ();
1543 notex->op = "!";
1544 notex->tok = e->tok;
1545 notex->operand = e;
1546 ifs->condition = notex;
1547 p->body = new block (ifs, p->body);
1548 }
1549
1550 static int
1551 semantic_pass_conditions (systemtap_session & sess)
1552 {
1553 map<derived_probe*, set<vardecl*> > vars_read_in_cond;
1554 map<derived_probe*, set<vardecl*> > vars_written_in_body;
1555
1556 // do a first pass through the probes to inline any condition, collect
1557 // globals being read
1558 for (unsigned i = 0; i < sess.probes.size(); ++i)
1559 {
1560 if (pending_interrupts) return 1;
1561
1562 derived_probe* p = sess.probes[i];
1563 expression* e = p->sole_location()->condition;
1564
1565 if (e)
1566 {
1567 varuse_collecting_visitor vcv_cond(sess);
1568 e->visit (& vcv_cond);
1569
1570 if (!vcv_cond.written.empty())
1571 sess.print_error (SEMANTIC_ERROR (_("probe condition must not "
1572 "modify any variables"),
1573 e->tok));
1574 else if (vcv_cond.embedded_seen)
1575 sess.print_error (SEMANTIC_ERROR (_("probe condition must not "
1576 "include impure embedded-C"),
1577 e->tok));
1578
1579 derived_probe_condition_inline(p);
1580
1581 if (! vcv_cond.read.empty()) { // insert only if nonempty
1582 vars_read_in_cond[p].insert(vcv_cond.read.begin(),
1583 vcv_cond.read.end());
1584 }
1585 }
1586 }
1587
1588 // skip all the rest of this processing if there are no conditions
1589 // that relate to state (global variables) at all
1590 if (sess.verbose > 2)
1591 clog << "number of probes with global-variable conditions: "
1592 << vars_read_in_cond.size() << endl;
1593 if (vars_read_in_cond.empty())
1594 return 0;
1595
1596 // do a second pass to see what probes write to any globals
1597 for (unsigned i = 0; i < sess.probes.size(); ++i)
1598 {
1599 if (pending_interrupts) return 1;
1600
1601 derived_probe* p = sess.probes[i];
1602
1603 varuse_collecting_visitor vcv_body(sess);
1604 p->body->visit (& vcv_body);
1605
1606 vars_written_in_body[p].insert(vcv_body.written.begin(),
1607 vcv_body.written.end());
1608 }
1609
1610 // do a third pass to collect affected probes
1611 for (unsigned i = 0; i < sess.probes.size(); ++i)
1612 {
1613 if (pending_interrupts) return 1;
1614
1615 derived_probe *p = sess.probes[i];
1616
1617 // for each variable this probe modifies...
1618 set<vardecl*>::const_iterator var;
1619 for (var = vars_written_in_body[p].begin();
1620 var != vars_written_in_body[p].end(); ++var)
1621 {
1622 // collect probes which could be affected
1623 for (unsigned j = 0; j < sess.probes.size(); ++j)
1624 {
1625 if (vars_read_in_cond[sess.probes[j]].count(*var))
1626 {
1627 if (!p->probes_with_affected_conditions.count(sess.probes[j]))
1628 {
1629 p->probes_with_affected_conditions.insert(sess.probes[j]);
1630 if (sess.verbose > 2)
1631 clog << "probe " << i << " can affect condition of "
1632 "probe " << j << endl;
1633 }
1634 }
1635 }
1636 }
1637 }
1638
1639 // PR18115: We create a begin probe which is artificially registered as
1640 // affecting every other probe. This will serve as the initializer so that
1641 // other probe types with false conditions can be skipped (or registered as
1642 // disabled) during module initialization.
1643
1644 set<derived_probe*> targets;
1645 for (unsigned i = 0; i < sess.probes.size(); ++i)
1646 if (!vars_read_in_cond[sess.probes[i]].empty())
1647 targets.insert(sess.probes[i]);
1648
1649 if (!targets.empty())
1650 {
1651 stringstream ss("probe begin {}");
1652
1653 // no good token to choose here... let's just use the condition expression
1654 // of one of the probes as the token
1655 const token *tok = (*targets.begin())->sole_location()->condition->tok;
1656
1657 probe *p = parse_synthetic_probe(sess, ss, tok);
1658 if (!p)
1659 throw SEMANTIC_ERROR (_("can't create cond initializer probe"), tok);
1660
1661 vector<derived_probe*> dps;
1662 derive_probes(sess, p, dps);
1663
1664 // there should only be one
1665 assert(dps.size() == 1);
1666
1667 derived_probe* dp = dps[0];
1668 dp->probes_with_affected_conditions.insert(targets.begin(),
1669 targets.end());
1670 sess.probes.push_back (dp);
1671
1672 // no need to manually do symresolution since body is empty
1673 }
1674
1675 return sess.num_errors();
1676 }
1677
1678 // ------------------------------------------------------------------------
1679
1680
1681 // Simple visitor that just goes through all embedded code blocks that
1682 // are available at the end all the optimizations to register any
1683 // relevant pragmas or other indicators found, so that session flags can
1684 // be set that can be inspected at translation time to trigger any
1685 // necessary initialization of code needed by the embedded code functions.
1686
1687 // This is only for pragmas that don't have any other side-effect than
1688 // needing some initialization at module init time. Currently handles
1689 // /* pragma:vma */ /* pragma:unwind */ /* pragma:symbols */ /* pragma:lines */
1690
1691 // /* pragma:uprobes */ is handled during the typeresolution_info pass.
1692 // /* pure */, /* unprivileged */. /* myproc-unprivileged */ and /* guru */
1693 // are handled by the varuse_collecting_visitor.
1694
1695 struct embeddedcode_info: public functioncall_traversing_visitor
1696 {
1697 protected:
1698 systemtap_session& session;
1699 void examine (const interned_string &, const token *tok);
1700
1701 public:
1702 embeddedcode_info (systemtap_session& s): session(s) { }
1703
1704 template <class Embeddish> void examine (Embeddish e, const token *tok);
1705
1706 void visit_embeddedcode (embeddedcode* c) { examine (c, c->tok); }
1707 void visit_embedded_expr (embedded_expr* e) { examine (e, e->tok); }
1708 };
1709
1710
1711 template <class Embeddish>
1712 void
1713 embeddedcode_info::examine (Embeddish e, const token *tok)
1714 {
1715 if (! vma_tracker_enabled(session)
1716 && e->tagged_p("/* pragma:vma */"))
1717 {
1718 if (session.verbose > 2)
1719 clog << _F("Turning on task_finder vma_tracker, pragma:vma found in %s",
1720 current_function->unmangled_name.to_string().c_str()) << endl;
1721
1722 // PR15052: stapdyn doesn't have VMA-tracking yet.
1723 if (session.runtime_usermode_p())
1724 throw SEMANTIC_ERROR(_("VMA-tracking is only supported by the kernel runtime (PR15052)"), tok);
1725
1726 enable_vma_tracker(session);
1727 }
1728
1729 if (! session.need_unwind
1730 && e->tagged_p("/* pragma:unwind */"))
1731 {
1732 if (session.verbose > 2)
1733 clog << _F("Turning on unwind support, pragma:unwind found in %s",
1734 current_function->unmangled_name.to_string().c_str()) << endl;
1735 session.need_unwind = true;
1736 }
1737
1738 if (! session.need_symbols
1739 && e->tagged_p("/* pragma:symbols */"))
1740 {
1741 if (session.verbose > 2)
1742 clog << _F("Turning on symbol data collecting, pragma:symbols found in %s",
1743 current_function->unmangled_name.to_string().c_str()) << endl;
1744 session.need_symbols = true;
1745 }
1746
1747 if (! session.need_lines
1748 && e->tagged_p("/* pragma:lines */"))
1749 {
1750 if (session.verbose > 2)
1751 clog << _F("Turning on debug line data collecting, pragma:lines found in %s",
1752 current_function->unmangled_name.to_string().c_str()) << endl;
1753 session.need_lines = true;
1754 }
1755 }
1756
1757 void embeddedcode_info_pass (systemtap_session& s)
1758 {
1759 embeddedcode_info eci (s);
1760 for (unsigned i=0; i<s.probes.size(); i++)
1761 s.probes[i]->body->visit (& eci);
1762
1763 for (map<string,functiondecl*>::iterator it = s.functions.begin();
1764 it != s.functions.end(); it++)
1765 it->second->body->visit (& eci);
1766 }
1767
1768 // ------------------------------------------------------------------------
1769
1770
1771 // Simple visitor that collects all the regular expressions in the
1772 // file and adds them to the session DFA table.
1773
1774 struct regex_collecting_visitor: public functioncall_traversing_visitor
1775 {
1776 protected:
1777 systemtap_session& session;
1778
1779 public:
1780 regex_collecting_visitor (systemtap_session& s): session(s) { }
1781
1782 void visit_regex_query (regex_query *q) {
1783 functioncall_traversing_visitor::visit_regex_query (q);
1784
1785 string re = q->right->value;
1786 regex_to_stapdfa (&session, re, q->right->tok);
1787 }
1788 };
1789
1790 // Go through the regex match invocations and generate corresponding DFAs.
1791 int gen_dfa_table (systemtap_session& s)
1792 {
1793 regex_collecting_visitor rcv(s);
1794
1795 for (unsigned i=0; i<s.probes.size(); i++)
1796 {
1797 try
1798 {
1799 s.probes[i]->body->visit (& rcv);
1800
1801 if (s.probes[i]->sole_location()->condition)
1802 s.probes[i]->sole_location()->condition->visit (& rcv);
1803 }
1804 catch (const semantic_error& e)
1805 {
1806 s.print_error (e);
1807 }
1808 }
1809
1810 return s.num_errors();
1811 }
1812
1813 // ------------------------------------------------------------------------
1814
1815
1816 static int semantic_pass_symbols (systemtap_session&);
1817 static int semantic_pass_optimize1 (systemtap_session&);
1818 static int semantic_pass_optimize2 (systemtap_session&);
1819 static int semantic_pass_types (systemtap_session&);
1820 static int semantic_pass_vars (systemtap_session&);
1821 static int semantic_pass_stats (systemtap_session&);
1822 static int semantic_pass_conditions (systemtap_session&);
1823
1824
1825 struct expression_build_no_more_visitor : public expression_visitor
1826 {
1827 // Clear extra details from every expression, like DWARF type info, so that
1828 // builders can safely release them in build_no_more. From here on out,
1829 // we're back to basic types only.
1830 void visit_expression(expression *e)
1831 {
1832 e->type_details.reset();
1833 }
1834 };
1835
1836 static void
1837 build_no_more (systemtap_session& s)
1838 {
1839 expression_build_no_more_visitor v;
1840
1841 for (unsigned i=0; i<s.probes.size(); i++)
1842 s.probes[i]->body->visit(&v);
1843
1844 for (map<string,functiondecl*>::iterator it = s.functions.begin();
1845 it != s.functions.end(); it++)
1846 it->second->body->visit(&v);
1847
1848 // Inform all derived_probe builders that we're done with
1849 // all resolution, so it's time to release caches.
1850 s.pattern_root->build_no_more (s);
1851 }
1852
1853
1854
1855 // Link up symbols to their declarations. Set the session's
1856 // files/probes/functions/globals vectors from the transitively
1857 // reached set of stapfiles in s.library_files, starting from
1858 // s.user_file. Perform automatic tapset inclusion and probe
1859 // alias expansion.
1860 static int
1861 semantic_pass_symbols (systemtap_session& s)
1862 {
1863 symresolution_info sym (s);
1864
1865 // If we're listing functions, then we need to include all the files. Probe
1866 // aliases won't be visited/derived so all we gain are the functions, global
1867 // variables, and any real probes (e.g. begin probes). NB: type resolution for
1868 // a specific function arg may fail if it could only be determined from a
1869 // function call in one of the skipped aliases.
1870 if (s.dump_mode == systemtap_session::dump_functions)
1871 {
1872 s.files.insert(s.files.end(), s.library_files.begin(),
1873 s.library_files.end());
1874 }
1875 else if (!s.user_files.empty())
1876 {
1877 // Normal run: seed s.files with user_files and let it grow through the
1878 // find_* functions. NB: s.files can grow during this iteration, so
1879 // size() can return gradually increasing numbers.
1880 s.files.insert (s.files.end(), s.user_files.begin(), s.user_files.end());
1881 }
1882
1883 for (unsigned i = 0; i < s.files.size(); i++)
1884 {
1885 assert_no_interrupts();
1886 stapfile* dome = s.files[i];
1887
1888 // Pass 1: add globals and functions to systemtap-session master list,
1889 // so the find_* functions find them
1890 //
1891 // NB: tapset global/function definitions may duplicate or conflict
1892 // with those already in s.globals/functions. We need to deconflict
1893 // here.
1894
1895 // PR24239: don't transcribe s.files .globals or .functions into
1896 // s.globals or s.functions here. Instead, symresolution_info::find_*
1897 // will have already done that, for only those functions / globals
1898 // that are actually transitively referenced from the end-user script.
1899
1900 // except in dump_* modes ... then we need to copy over the goods
1901 // regardless of transitive referencing.
1902 if (s.dump_mode == systemtap_session::dump_functions)
1903 {
1904 for (auto it = dome->functions.begin(); it != dome->functions.end(); it++)
1905 {
1906 functiondecl* v = *it;
1907 s.functions[v->name] = v;
1908 }
1909 }
1910
1911 if (s.verbose > 3)
1912 {
1913 for (auto it = dome->globals.begin(); it != dome->globals.end(); it++)
1914 if (std::find(s.globals.begin(), s.globals.end(), *it) == s.globals.end())
1915 clog << "excluded unused global " << (*it)->name << " from tapset " << dome->name << endl;
1916
1917 for (auto it = dome->functions.begin(); it != dome->functions.end(); it++)
1918 if (s.functions.find((*it)->name) == s.functions.end())
1919 clog << "excluded unused function " << (*it)->name << " from tapset " << dome->name << endl;
1920 }
1921
1922 // NB: embeds don't conflict with each other
1923 for (unsigned i=0; i<dome->embeds.size(); i++)
1924 s.embeds.push_back (dome->embeds[i]);
1925
1926 // Pass 2: derive probes and resolve any further symbols in the
1927 // derived results.
1928
1929 for (unsigned i=0; i<dome->probes.size(); i++)
1930 {
1931 assert_no_interrupts();
1932 probe* p = dome->probes [i];
1933 vector<derived_probe*> dps;
1934
1935 // much magic happens here: probe alias expansion, wildcard
1936 // matching, low-level derived_probe construction.
1937 derive_probes (s, p, dps);
1938
1939 for (unsigned j=0; j<dps.size(); j++)
1940 {
1941 assert_no_interrupts();
1942 derived_probe* dp = dps[j];
1943 s.probes.push_back (dp);
1944
1945 // We don't perform the group join for the probe yet, as
1946 // some may be elided after optimization.
1947
1948 if (s.verbose > 2)
1949 {
1950 clog << _("symbol resolution for derived-probe ");
1951 dp->printsig(clog);
1952 clog << endl;
1953 }
1954
1955 try
1956 {
1957 update_visitor_loop (s, s.code_filters, dp->body);
1958
1959 sym.current_function = 0;
1960 sym.current_probe = dp;
1961 dp->body->visit (& sym);
1962
1963 // Process the probe-point condition expression.
1964 sym.current_function = 0;
1965 sym.current_probe = 0;
1966 if (dp->sole_location()->condition)
1967 dp->sole_location()->condition->visit (& sym);
1968 }
1969 catch (const semantic_error& e)
1970 {
1971 s.print_error (e);
1972 }
1973 }
1974 }
1975
1976 // Pass 3: process functions - incl. the synthetic ones,
1977 // so s.functions[] rather than dome->functions[]
1978
1979 for (auto it = s.functions.begin(); it != s.functions.end(); it++)
1980 {
1981 assert_no_interrupts();
1982 functiondecl* fd = it->second;
1983 if (s.verbose > 2)
1984 clog << _("symbol resolution for function ") << fd->name << endl;
1985
1986 try
1987 {
1988 update_visitor_loop (s, s.code_filters, fd->body);
1989
1990 sym.current_function = fd;
1991 sym.current_probe = 0;
1992 fd->body->visit (& sym);
1993 }
1994 catch (const semantic_error& e)
1995 {
1996 s.print_error (e);
1997 }
1998 }
1999 }
2000
2001 if(s.systemtap_v_check){
2002 for(unsigned i=0;i<s.globals.size();i++){
2003 if(s.globals[i]->systemtap_v_conditional)
2004 s.print_warning(_("This global uses tapset constructs that are dependent on systemtap version"), s.globals[i]->tok);
2005 }
2006
2007 for(map<string, functiondecl*>::const_iterator i=s.functions.begin();i != s.functions.end();++i){
2008 if(i->second->systemtap_v_conditional)
2009 s.print_warning(_("This function uses tapset constructs that are dependent on systemtap version"), i->second->tok);
2010 }
2011
2012 for(unsigned i=0;i<s.probes.size();i++){
2013 vector<probe*> sysvc;
2014 s.probes[i]->collect_derivation_chain(sysvc);
2015 for(unsigned j=0;j<sysvc.size();j++){
2016 if(sysvc[j]->systemtap_v_conditional)
2017 s.print_warning(_("This probe uses tapset constructs that are dependent on systemtap version"), sysvc[j]->tok);
2018 if(sysvc[j]->get_alias() && sysvc[j]->get_alias()->systemtap_v_conditional)
2019 s.print_warning(_("This alias uses tapset constructs that are dependent on systemtap version"), sysvc[j]->get_alias()->tok);
2020 }
2021 }
2022 }
2023
2024 return s.num_errors(); // all those print_error calls
2025 }
2026
2027
2028 // Keep unread global variables for probe end value display.
2029 void add_global_var_display (systemtap_session& s)
2030 {
2031 // Don't generate synthetic end probes when in listing mode; it would clutter
2032 // up the list of probe points with "end ...". In fact, don't bother in any
2033 // dump mode at all, since it'll never be used.
2034 if (s.dump_mode) return;
2035
2036 // The bpf runtime currently lacks support for foreach statements which
2037 // this function might generate.
2038 if (s.runtime_mode == systemtap_session::bpf_runtime) return;
2039
2040 // User has specified not to display unread global variables.
2041 if (s.no_global_var_display) return;
2042
2043 varuse_collecting_visitor vut(s);
2044
2045 for (unsigned i=0; i<s.probes.size(); i++)
2046 {
2047 s.probes[i]->body->visit (& vut);
2048
2049 if (s.probes[i]->sole_location()->condition)
2050 s.probes[i]->sole_location()->condition->visit (& vut);
2051 }
2052
2053 for (unsigned g=0; g < s.globals.size(); g++)
2054 {
2055 vardecl* l = s.globals[g];
2056 if ((vut.read.find (l) != vut.read.end()
2057 && vut.used.find (l) != vut.used.end())
2058 || vut.written.find (l) == vut.written.end())
2059 continue;
2060
2061 // Don't generate synthetic end probes for unread globals
2062 // that were synthesized or declared only within tapsets.
2063 // (RHBZ 468139), but rather only within the end-user script.
2064
2065 if (l->synthetic)
2066 continue;
2067
2068 bool tapset_global = false;
2069 for (size_t m=0; m < s.library_files.size(); m++)
2070 {
2071 for (size_t n=0; n < s.library_files[m]->globals.size(); n++)
2072 {
2073 if (l->name == s.library_files[m]->globals[n]->name)
2074 {tapset_global = true; break;}
2075 }
2076 }
2077 if (tapset_global)
2078 continue;
2079
2080 stringstream code;
2081 code << "probe end {" << endl;
2082
2083 string format = l->unmangled_name;
2084
2085 string indexes;
2086 string foreach_value;
2087 if (!l->index_types.empty())
2088 {
2089 // Add index values to the printf format, and prepare
2090 // a simple list of indexes for passing around elsewhere
2091 format += "[";
2092 for (size_t i = 0; i < l->index_types.size(); ++i)
2093 {
2094 if (i > 0)
2095 {
2096 indexes += ",";
2097 format += ",";
2098 }
2099 indexes += "__idx" + lex_cast(i);
2100 if (l->index_types[i] == pe_string)
2101 format += "\\\"%#s\\\"";
2102 else
2103 format += "%#d";
2104 }
2105 format += "]";
2106
2107 // Iterate over all indexes in the array, sorted by decreasing value
2108 code << "foreach (";
2109 if (l->type != pe_stats)
2110 {
2111 foreach_value = "__val";
2112 code << foreach_value << " = ";
2113 }
2114 code << "[" << indexes << "] in " << l->unmangled_name << "-)" << endl;
2115 }
2116 else if (l->type == pe_stats)
2117 {
2118 // PR7053: Check scalar globals for empty aggregate
2119 code << "if (@count(" << l->unmangled_name << ") == 0)" << endl;
2120 code << "printf(\"" << l->unmangled_name << " @count=0x0\\n\")" << endl;
2121 code << "else" << endl;
2122 }
2123
2124 static const string stats[] = { "@count", "@min", "@max", "@sum", "@avg" };
2125 const string stats_format =
2126 (strverscmp(s.compatible.c_str(), "1.4") >= 0) ? "%#d" : "%#x";
2127
2128 // Fill in the printf format for values
2129 if (l->type == pe_stats)
2130 for (size_t i = 0; i < sizeof(stats)/sizeof(stats[0]); ++i)
2131 format += " " + stats[i] + "=" + stats_format;
2132 else if (l->type == pe_string)
2133 format += "=\\\"%#s\\\"";
2134 else
2135 {
2136
2137 // If l->type is pe_long, we need to differentiate between pointers and non-pointers.
2138
2139 string specifier = "=%d";
2140
2141 if (l->type_details != NULL && l->type_details->id() != 0)
2142 {
2143 exp_type_dwarf* type = (exp_type_dwarf*) l->type_details.get();
2144
2145 if (type->is_pointer || dwarf_tag(&type->die) == DW_TAG_pointer_type)
2146 specifier = "=%#x";
2147 }
2148
2149 format += specifier;
2150
2151 }
2152
2153 format += "\\n";
2154
2155 // Output the actual printf
2156 code << "printf (\"" << format << "\"";
2157
2158 // Feed indexes to the printf, and include them in the value
2159 string value = !foreach_value.empty() ? foreach_value : string(l->unmangled_name);
2160 if (!l->index_types.empty())
2161 {
2162 code << "," << indexes;
2163 if (foreach_value.empty())
2164 value += "[" + indexes + "]";
2165 }
2166
2167 // Feed the actual values to the printf
2168 if (l->type == pe_stats)
2169 for (size_t i = 0; i < sizeof(stats)/sizeof(stats[0]); ++i)
2170 code << "," << stats[i] << "(" << value << ")";
2171 else
2172 code << "," << value;
2173 code << ")" << endl;
2174
2175 // End of probe
2176 code << "}" << endl;
2177
2178 probe *p = parse_synthetic_probe (s, code, l->tok);
2179 if (!p)
2180 throw SEMANTIC_ERROR (_("can't create global var display"), l->tok);
2181
2182 vector<derived_probe*> dps;
2183 derive_probes (s, p, dps);
2184 for (unsigned i = 0; i < dps.size(); i++)
2185 {
2186 derived_probe* dp = dps[i];
2187 s.probes.push_back (dp);
2188 dp->join_group (s);
2189
2190 // Repopulate symbol and type info
2191 symresolution_info sym (s);
2192 sym.current_function = 0;
2193 sym.current_probe = dp;
2194 dp->body->visit (& sym);
2195 }
2196
2197 semantic_pass_types(s);
2198 // Mark that variable is read
2199 vut.read.insert (l);
2200 }
2201 }
2202
2203 static void gen_monitor_data(systemtap_session& s)
2204 {
2205 vardecl* v;
2206 embeddedcode* ec;
2207
2208 v = new vardecl;
2209 v->unmangled_name = v->name = "__global___monitor_module_start";
2210 v->set_arity(0, 0);
2211 v->type = pe_long;
2212 v->synthetic = true;
2213 s.globals.push_back(v);
2214
2215 ec = new embeddedcode;
2216 ec->code = "#define STAP_MONITOR_READ 8192\n"
2217 "static char _monitor_buf[STAP_MONITOR_READ];";
2218 s.embeds.push_back(ec);
2219
2220 functiondecl* fd = new functiondecl;
2221 fd->synthetic = true;
2222 fd->unmangled_name = fd->name = "__private___monitor_data_function_probes";
2223 fd->type = pe_string;
2224 v = new vardecl;
2225 v->type = pe_long;
2226 v->unmangled_name = v->name = "index";
2227 fd->formal_args.push_back(v);
2228 ec = new embeddedcode;
2229 string code;
2230 code = "/* unprivileged */ /* pure */"
2231 "const struct stap_probe *const p = &stap_probes[STAP_ARG_index];\n"
2232 "if (likely (probe_timing(STAP_ARG_index))) {\n"
2233 "struct stat_data *stats = _stp_stat_get (probe_timing(STAP_ARG_index), 0);\n"
2234 "if (stats->count) {\n"
2235 "int64_t avg = _stp_div64 (NULL, stats->sum, stats->count);\n"
2236 "snprintf(_monitor_buf, STAP_MONITOR_READ,\n"
2237 "\"\\\"index\\\": %zu, \\\"state\\\": \\\"%s\\\", \\\"hits\\\": %lld, "
2238 "\\\"min\\\": %lld, \\\"avg\\\": %lld, \\\"max\\\": %lld, \",\n"
2239 "p->index, p->cond_enabled ? \"on\" : \"off\", (long long) stats->count,\n"
2240 "(long long) stats->min, (long long) avg, (long long) stats->max);\n"
2241 "} else {\n"
2242 "snprintf(_monitor_buf, STAP_MONITOR_READ,\n"
2243 "\"\\\"index\\\": %zu, \\\"state\\\": \\\"%s\\\", \\\"hits\\\": %d, "
2244 "\\\"min\\\": %d, \\\"avg\\\": %d, \\\"max\\\": %d, \",\n"
2245 "p->index, p->cond_enabled ? \"on\" : \"off\", 0, 0, 0, 0);}}\n"
2246 "STAP_RETURN(_monitor_buf);\n";
2247 ec->code = code;
2248 fd->body = ec;
2249 s.functions[fd->name] = fd;
2250
2251 stringstream probe_code;
2252 probe_code << "probe begin {" << endl;
2253 probe_code << "__monitor_module_start = jiffies()" << endl;
2254 probe_code << "}" << endl;
2255
2256 probe* p = parse_synthetic_probe(s, probe_code, 0);
2257 if (!p)
2258 throw SEMANTIC_ERROR (_("can't create begin probe"), 0);
2259
2260 vector<derived_probe*> dps;
2261 derive_probes (s, p, dps);
2262
2263 derived_probe* dp = dps[0];
2264 s.probes.push_back (dp);
2265 dp->join_group (s);
2266
2267 // Repopulate symbol info
2268 symresolution_info sym (s);
2269 sym.current_function = 0;
2270 sym.current_probe = dp;
2271 dp->body->visit (&sym);
2272 }
2273
2274 static void monitor_mode_read(systemtap_session& s)
2275 {
2276 if (!s.monitor) return;
2277
2278 gen_monitor_data(s);
2279
2280 stringstream code;
2281
2282 unsigned long rough_max_json_size = 100 +
2283 s.globals.size() * 100 +
2284 s.probes.size() * 200;
2285
2286 code << "probe procfs(\"monitor_status\").read.maxsize(" << rough_max_json_size << ") {" << endl;
2287 code << "try {"; // absorb .= overflows!
2288 code << "elapsed = (jiffies()-__monitor_module_start)/HZ()" << endl;
2289 code << "hrs = elapsed/3600; mins = elapsed%3600/60; secs = elapsed%3600%60;" << endl;
2290 code << "$value .= sprintf(\"{\\n\")" << endl;
2291 code << "$value .= sprintf(\"\\\"uptime\\\": \\\"%02d:%02d:%02d\\\",\\n\", hrs, mins, secs)" << endl;
2292 code << "$value .= sprintf(\"\\\"uid\\\": \\\"%d\\\",\\n\", uid())" << endl;
2293 code << "$value .= sprintf(\"\\\"memory\\\": \\\"%s\\\",\\n\", module_size())" << endl;
2294 code << "$value .= sprintf(\"\\\"module_name\\\": \\\"%s\\\",\\n\", module_name())" << endl;
2295
2296 code << "$value .= sprintf(\"\\\"globals\\\": {\\n\")" << endl;
2297 for (auto it = s.globals.cbegin(); it != s.globals.cend(); ++it)
2298 {
2299 if ((*it)->synthetic) continue;
2300
2301 if (it != s.globals.cbegin())
2302 code << "$value .= sprintf(\",\\n\")" << endl;
2303
2304 code << "$value .= sprintf(\"\\\"%s\\\":\", \"" << (*it)->unmangled_name << "\")" << endl;
2305 if ((*it)->arity == 0)
2306 {
2307 if ((*it)->type == pe_stats)
2308 code << "$value .= sprintf(\"\\\"%d(count)\\\"\", @count(" << (*it)->name << "))" << endl;
2309 else if ((*it)->type == pe_string)
2310 code << "$value .= string_quoted(sprintf(\"\\\"%s\\\"\"," << (*it)->name << "))" << endl;
2311 else
2312 code << "$value .= string_quoted(sprint(" << (*it)->name << "))" << endl;
2313 }
2314 else if ((*it)->arity > 0)
2315 code << "$value .= sprintf(\"\\\"[%d]\\\"\", " << (*it)->maxsize << ")" << endl;
2316 }
2317 code << "$value .= sprintf(\"\\n},\\n\")" << endl;
2318
2319 code << "$value .= sprintf(\"\\\"probe_list\\\": [\\n\")" << endl;
2320 for (auto it = s.probes.cbegin(); it != s.probes.cend(); ++it)
2321 {
2322 if ((*it)->synthetic) continue;
2323
2324 if (it != s.probes.cbegin())
2325 code << "$value .= sprintf(\",\\n\")" << endl;
2326
2327 istringstream probe_point((*it)->sole_location()->str());
2328 string name;
2329 probe_point >> name;
2330 /* Escape quotes once for systemtap parser and once more for json parser */
2331 name = lex_cast_qstring(lex_cast_qstring(name));
2332
2333 code << "$value .= sprintf(\"{%s\", __private___monitor_data_function_probes("
2334 << it-s.probes.begin() << "))" << endl;
2335 code << "$value .= sprintf(\"\\\"name\\\": %s}\", " << name << ")" << endl;
2336 }
2337 code << "$value .= sprintf(\"\\n],\\n\")" << endl;
2338
2339 code << "$value .= sprintf(\"}\\n\")" << endl;
2340
2341 code << "} catch(ex) { warn(\"JSON construction error: \" . ex) }" << endl;
2342 code << "}" << endl;
2343 probe* p = parse_synthetic_probe(s, code, 0);
2344 if (!p)
2345 throw SEMANTIC_ERROR (_("can't create procfs probe"), 0);
2346
2347 vector<derived_probe*> dps;
2348 derive_probes (s, p, dps);
2349
2350 derived_probe* dp = dps[0];
2351 s.probes.push_back (dp);
2352 dp->join_group (s);
2353
2354 // Repopulate symbol info
2355 symresolution_info sym (s);
2356 sym.current_function = 0;
2357 sym.current_probe = dp;
2358 dp->body->visit (&sym);
2359
2360 // Resolve types for variables used in the new procfs probe
2361 semantic_pass_types(s);
2362 }
2363
2364 static void monitor_mode_write(systemtap_session& s)
2365 {
2366 if (!s.monitor) return;
2367
2368 for (auto it = s.probes.cbegin(); it != s.probes.cend(); ++it)
2369 {
2370 vardecl* v = new vardecl;
2371 v->unmangled_name = v->name = "__monitor_" + lex_cast(it-s.probes.begin()) + "_enabled";
2372 v->tok = (*it)->tok;
2373 v->set_arity(0, (*it)->tok);
2374 v->type = pe_long;
2375 v->init = new literal_number(1);
2376 v->synthetic = true;
2377 s.globals.push_back(v);
2378
2379 symbol* sym = new symbol;
2380 sym->name = v->name;
2381 sym->tok = v->tok;
2382 sym->type = pe_long;
2383 sym->referent = v;
2384
2385 if ((*it)->sole_location()->condition)
2386 {
2387 logical_and_expr *e = new logical_and_expr;
2388 e->tok = v->tok;
2389 e->left = sym;
2390 e->op = "&&";
2391 e->type = pe_long;
2392 e->right = (*it)->sole_location()->condition;
2393 (*it)->sole_location()->condition = e;
2394 }
2395 else
2396 {
2397 (*it)->sole_location()->condition = sym;
2398 }
2399 }
2400
2401 stringstream code;
2402
2403 code << "probe procfs(\"monitor_control\").write {" << endl;
2404
2405 code << "if ($value == \"clear\") {";
2406 for (auto it = s.globals.cbegin(); it != s.globals.cend(); ++it)
2407 {
2408 vardecl* v = *it;
2409
2410 if (v->synthetic) continue;
2411
2412 if (v->arity == 0 && v->init)
2413 {
2414 if (v->type == pe_long)
2415 {
2416 literal_number* ln = dynamic_cast<literal_number*>(v->init);
2417 if (ln == 0)
2418 throw SEMANTIC_ERROR (_("expected literal number"), 0);
2419 code << v->name << " = " << ln->value << endl;
2420 }
2421 else if (v->type == pe_string)
2422 {
2423 literal_string* ln = dynamic_cast<literal_string*>(v->init);
2424 if (ln == 0)
2425 throw SEMANTIC_ERROR (_("expected literal string"), 0);
2426 code << v->name << " = " << lex_cast_qstring(ln->value) << endl;
2427 }
2428 }
2429 else
2430 {
2431 // For scalar elements with no initial values, we reset to 0 or empty as
2432 // done with arrays and aggregates.
2433 code << "delete " << v->name << endl;
2434 }
2435 }
2436
2437 code << "} else if ($value == \"resume\") {" << endl;
2438 for (auto it = s.probes.cbegin(); it != s.probes.cend(); ++it)
2439 {
2440 code << " __monitor_" << it-s.probes.begin() << "_enabled" << " = 1" << endl;
2441 }
2442
2443 code << "} else if ($value == \"pause\") {" << endl;
2444 for (auto it = s.probes.cbegin(); it != s.probes.cend(); ++it)
2445 {
2446 code << " __monitor_" << it-s.probes.begin() << "_enabled" << " = 0" << endl;
2447 }
2448 code << "} else if ($value == \"quit\") {" << endl;
2449 code << " exit()" << endl;
2450 code << "}";
2451
2452 for (auto it = s.probes.cbegin(); it != s.probes.cend(); ++it)
2453 {
2454 code << " if ($value == \"" << it-s.probes.begin() << "\")"
2455 << " __monitor_" << it-s.probes.begin() << "_enabled" << " ^= 1" << endl;
2456 }
2457
2458 code << "}" << endl;
2459
2460 probe* p = parse_synthetic_probe(s, code, 0);
2461 if (!p)
2462 throw SEMANTIC_ERROR (_("can't create procfs probe"), 0);
2463
2464 vector<derived_probe*> dps;
2465 derive_probes (s, p, dps);
2466
2467 derived_probe* dp = dps[0];
2468 s.probes.push_back (dp);
2469 dp->join_group (s);
2470
2471 // Repopulate symbol info
2472 symresolution_info sym (s, /* omniscient-unmangled */ true);
2473 sym.current_function = 0;
2474 sym.current_probe = dp;
2475 dp->body->visit (&sym);
2476 }
2477
2478 static void setup_timeout(systemtap_session& s)
2479 {
2480 if (!s.timeout) return;
2481
2482 stringstream code;
2483 code << "probe timer.ms(" << s.timeout << ") {exit()}";
2484 probe* p = parse_synthetic_probe(s, code, 0);
2485 if (!p)
2486 throw SEMANTIC_ERROR (_("can't create timer probe"), 0);
2487
2488 vector<derived_probe*> dps;
2489 derive_probes (s, p, dps);
2490
2491 derived_probe* dp = dps[0];
2492 s.probes.push_back (dp);
2493 dp->join_group (s);
2494
2495 // Repopulate symbol info
2496 symresolution_info sym (s);
2497 sym.current_function = 0;
2498 sym.current_probe = dp;
2499 dp->body->visit (&sym);
2500 }
2501
2502 int
2503 semantic_pass (systemtap_session& s)
2504 {
2505 int rc = 0;
2506
2507 try
2508 {
2509 // FIXME: interactive mode, register_library_aliases handles
2510 // both aliases from library files *and* user scripts. It would
2511 // be nice to have them in separate lists and register them
2512 // separately.
2513 s.register_library_aliases();
2514 register_standard_tapsets(s);
2515
2516 if (rc == 0) setup_timeout(s);
2517 if (rc == 0) rc = semantic_pass_symbols (s);
2518 if (rc == 0) monitor_mode_write (s);
2519 if (rc == 0) rc = semantic_pass_conditions (s);
2520 if (rc == 0) rc = semantic_pass_optimize1 (s);
2521 if (rc == 0) rc = semantic_pass_types (s);
2522 if (rc == 0) rc = gen_dfa_table(s);
2523 if (rc == 0) add_global_var_display (s);
2524 if (rc == 0) monitor_mode_read(s);
2525 if (rc == 0) rc = semantic_pass_optimize2 (s);
2526 if (rc == 0) rc = semantic_pass_vars (s);
2527 if (rc == 0) rc = semantic_pass_stats (s);
2528 if (rc == 0) embeddedcode_info_pass (s);
2529 }
2530 catch (const semantic_error& e)
2531 {
2532 s.print_error (e);
2533 rc ++;
2534 }
2535
2536 bool no_primary_probes = true;
2537 for (unsigned i = 0; i < s.probes.size(); i++)
2538 if (s.is_primary_probe(s.probes[i]))
2539 no_primary_probes = false;
2540
2541 if (s.num_errors() == 0 && no_primary_probes && !s.dump_mode)
2542 {
2543 s.print_error(SEMANTIC_ERROR(_("no probes found")));
2544 rc ++;
2545 }
2546
2547 build_no_more (s);
2548
2549 // PR11443
2550 // NB: listing mode only cares whether we have any probes,
2551 // so all previous error conditions are disregarded.
2552 if (s.dump_mode == systemtap_session::dump_matched_probes ||
2553 s.dump_mode == systemtap_session::dump_matched_probes_vars)
2554 rc = no_primary_probes;
2555
2556 // If we're dumping functions, only error out if no functions were found
2557 if (s.dump_mode == systemtap_session::dump_functions)
2558 rc = s.functions.empty();
2559
2560 return rc;
2561 }
2562
2563
2564 // ------------------------------------------------------------------------
2565 // semantic processing: symbol resolution
2566
2567
2568 symresolution_info::symresolution_info (systemtap_session& s, bool omniscient_unmangled):
2569 session (s), unmangled_p(omniscient_unmangled), current_function (0), current_probe (0)
2570 {
2571 }
2572
2573
2574 void
2575 symresolution_info::visit_block (block* e)
2576 {
2577 for (unsigned i=0; i<e->statements.size(); i++)
2578 {
2579 try
2580 {
2581 e->statements[i]->visit (this);
2582 }
2583 catch (const semantic_error& e)
2584 {
2585 session.print_error (e);
2586 }
2587 }
2588 }
2589
2590
2591 void
2592 symresolution_info::visit_foreach_loop (foreach_loop* e)
2593 {
2594 for (unsigned i=0; i<e->indexes.size(); i++)
2595 e->indexes[i]->visit (this);
2596 for (unsigned i=0; i<e->array_slice.size(); i++)
2597 if (e->array_slice[i])
2598 e->array_slice[i]->visit(this);
2599
2600 symbol *array = NULL;
2601 hist_op *hist = NULL;
2602 classify_indexable (e->base, array, hist);
2603
2604 if (array)
2605 {
2606 if (!array->referent)
2607 {
2608 vardecl* d = find_var (array->name, e->indexes.size (), array->tok);
2609 if (d)
2610 {
2611 array->referent = d;
2612 array->name = d->name;
2613 }
2614 else
2615 {
2616 stringstream msg;
2617 msg << _F("unresolved arity-%zu global array %s, missing global declaration?",
2618 e->indexes.size(), array->name.to_string().c_str());
2619 throw SEMANTIC_ERROR (msg.str(), array->tok);
2620 }
2621 }
2622
2623 if (!e->array_slice.empty() && e->array_slice.size() != e->indexes.size())
2624 {
2625 stringstream msg;
2626 msg << _F("unresolved arity-%zu global array %s, missing global declaration?",
2627 e->array_slice.size(), array->name.to_string().c_str());
2628 throw SEMANTIC_ERROR (msg.str(), array->tok);
2629 }
2630 }
2631 else
2632 {
2633 assert (hist);
2634 hist->visit (this);
2635 }
2636
2637 if (e->value)
2638 e->value->visit (this);
2639
2640 if (e->limit)
2641 e->limit->visit (this);
2642
2643 e->block->visit (this);
2644 }
2645
2646
2647 struct
2648 delete_statement_symresolution_info:
2649 public traversing_visitor
2650 {
2651 symresolution_info *parent;
2652
2653 delete_statement_symresolution_info (symresolution_info *p):
2654 parent(p)
2655 {}
2656
2657 void visit_arrayindex (arrayindex* e)
2658 {
2659 parent->visit_arrayindex(e, true);
2660 }
2661
2662 void visit_functioncall (functioncall* e)
2663 {
2664 parent->visit_functioncall (e);
2665 }
2666
2667 void visit_symbol (symbol* e)
2668 {
2669 if (e->referent)
2670 return;
2671
2672 vardecl* d = parent->find_var (e->name, -1, e->tok);
2673 if (d)
2674 e->referent = d;
2675 else
2676 throw SEMANTIC_ERROR (_("unresolved array in delete statement"), e->tok);
2677 }
2678 };
2679
2680 void
2681 symresolution_info::visit_delete_statement (delete_statement* s)
2682 {
2683 delete_statement_symresolution_info di (this);
2684 s->value->visit (&di);
2685 }
2686
2687
2688 void
2689 symresolution_info::visit_symbol (symbol* e)
2690 {
2691 if (e->referent)
2692 return;
2693
2694 vardecl* d = find_var (e->name, 0, e->tok);
2695 if (d)
2696 {
2697 e->referent = d;
2698 e->name = d->name;
2699 }
2700 else
2701 {
2702 // new local
2703 vardecl* v = new vardecl;
2704 v->unmangled_name = v->name = e->name;
2705 v->tok = e->tok;
2706 v->set_arity(0, e->tok);
2707 if (current_function)
2708 current_function->locals.push_back (v);
2709 else if (current_probe)
2710 current_probe->locals.push_back (v);
2711 else
2712 // must be probe-condition expression
2713 throw SEMANTIC_ERROR (_("probe condition must not reference undeclared global"), e->tok);
2714 e->referent = v;
2715 }
2716 }
2717
2718
2719 void
2720 symresolution_info::visit_arrayindex (arrayindex* e)
2721 {
2722 visit_arrayindex(e, false);
2723 }
2724
2725 void
2726 symresolution_info::visit_arrayindex (arrayindex* e, bool wildcard_ok)
2727 {
2728 for (unsigned i=0; i<e->indexes.size(); i++)
2729 {
2730 // assuming that if NULL, it was originally a wildcard (*)
2731 if (e->indexes[i] == NULL)
2732 {
2733 if (!wildcard_ok)
2734 throw SEMANTIC_ERROR(_("wildcard not allowed in array index"), e->tok);
2735 }
2736 else
2737 e->indexes[i]->visit (this);
2738 }
2739
2740 symbol *array = NULL;
2741 hist_op *hist = NULL;
2742 classify_indexable(e->base, array, hist);
2743
2744 if (array)
2745 {
2746 if (array->referent)
2747 return;
2748
2749 vardecl* d = find_var (array->name, e->indexes.size (), array->tok);
2750 if (d)
2751 {
2752 array->referent = d;
2753 array->name = d->name;
2754 }
2755 else
2756 {
2757 stringstream msg;
2758 msg << _F("unresolved arity-%zu global array %s, missing global declaration?",
2759 e->indexes.size(), array->name.to_string().c_str());
2760 throw SEMANTIC_ERROR (msg.str(), e->tok);
2761 }
2762 }
2763 else
2764 {
2765 assert (hist);
2766 hist->visit (this);
2767 }
2768 }
2769
2770
2771 void
2772 symresolution_info::visit_array_in (array_in* e)
2773 {
2774 visit_arrayindex(e->operand, true);
2775 }
2776
2777
2778 void
2779 symresolution_info::visit_embeddedcode (embeddedcode* s)
2780 {
2781 // PR24239: must generate find_var references to globals
2782 // that are marked by pragma:{read|write}:var
2783
2784 if (s->code_referents == s->code)
2785 return; // already analyzed
2786
2787 size_t pos = 0;
2788 while (1)
2789 {
2790 pos = s->code.find("/* pragma:read:", pos);
2791 if (pos == string::npos)
2792 break;
2793 pos += strlen("/* pragma:read:");
2794 auto pos2 = s->code.find(" */", pos);
2795 if (pos2 == string::npos)
2796 break;
2797 auto var = s->code.substr(pos, pos2-pos);
2798 auto vd = find_var(var,-1,s->tok);
2799 if (vd == 0)
2800 throw SEMANTIC_ERROR (_F("unresolved pragma:read global %s", ((string)var).c_str()),
2801 s->tok);
2802 s->read_referents.push_back(vd);
2803 pos = pos2+3;
2804 }
2805
2806 // repeat for write
2807 pos = 0;
2808 while (1)
2809 {
2810 pos = s->code.find("/* pragma:write:", pos);
2811 if (pos == string::npos)
2812 break;
2813 pos += strlen("/* pragma:write:");
2814 auto pos2 = s->code.find(" */", pos);
2815 if (pos2 == string::npos)
2816 break;
2817 auto var = s->code.substr(pos, pos2-pos);
2818 auto vd = find_var(var,-1,s->tok);
2819 if (vd == 0)
2820 throw SEMANTIC_ERROR (_F("unresolved pragma:write global %s", ((string)var).c_str()),
2821 s->tok);
2822 s->write_referents.push_back(vd);
2823 pos = pos2+3;
2824 }
2825
2826 s->code_referents = s->code; // flag this object as not needing resolution again
2827 // NB: why not a bool? sure, could do that, but this way we can detect subsequent
2828 // changes to the code object, and be ready to recompute. It's at least harmless.
2829 }
2830
2831
2832 void
2833 symresolution_info::visit_embedded_expr (embedded_expr *e)
2834 {
2835 // PR24239: must generate find_var references to globals
2836 // that are marked by pragma:{read|write}:var
2837
2838 if (e->code_referents == e->code)
2839 return; // already analyzed
2840
2841 size_t pos = 0;
2842 while (1)
2843 {
2844 pos = e->code.find("/* pragma:read:", pos);
2845 if (pos == string::npos)
2846 break;
2847 pos += strlen("/* pragma:read:");
2848 auto pos2 = e->code.find(" */", pos);
2849 if (pos2 == string::npos)
2850 break;
2851 auto var = e->code.substr(pos, pos2-pos);
2852 auto vd = find_var(var,-1,e->tok);
2853 if (vd == 0)
2854 throw SEMANTIC_ERROR (_F("unresolved pragma:read global %s", ((string)var).c_str()),
2855 e->tok);
2856 e->read_referents.push_back(vd);
2857 pos = pos2+3;
2858 }
2859
2860 // repeat for write
2861 pos = 0;
2862 while (1)
2863 {
2864 pos = e->code.find("/* pragma:write:", pos);
2865 if (pos == string::npos)
2866 break;
2867 pos += strlen("/* pragma:write:");
2868 auto pos2 = e->code.find(" */", pos);
2869 if (pos2 == string::npos)
2870 break;
2871 auto var = e->code.substr(pos, pos2-pos);
2872 auto vd = find_var(var,-1,e->tok);
2873 if (vd == 0)
2874 throw SEMANTIC_ERROR (_F("unresolved pragma:write global %s", ((string)var).c_str()),
2875 e->tok);
2876 e->write_referents.push_back(vd);
2877 pos = pos2+3;
2878 }
2879
2880 e->code_referents = e->code; // flag this object as not needing resolution again
2881 // NB: why not a bool? sure, could do that, but this way we can detect subsequent
2882 // changes to the code object, and be ready to recompute. It's at least harmless.
2883 }
2884
2885
2886 void
2887 symresolution_info::visit_functioncall (functioncall* e)
2888 {
2889 // XXX: we could relax this, if we're going to examine the
2890 // vartracking data recursively. See testsuite/semko/fortytwo.stp.
2891 if (! (current_function || current_probe))
2892 {
2893 // must be probe-condition expression
2894 throw SEMANTIC_ERROR (_("probe condition must not reference function"), e->tok);
2895 }
2896
2897 for (unsigned i=0; i<e->args.size(); i++)
2898 e->args[i]->visit (this);
2899
2900 // already resolved?
2901 if (!e->referents.empty())
2902 return;
2903
2904 vector<functiondecl*> fds = find_functions (e, e->function, e->args.size (), e->tok);
2905 if (!fds.empty())
2906 {
2907 e->referents = fds;
2908 function_priority_order order;
2909 stable_sort(e->referents.begin(), e->referents.end(), order); // preserve declaration order
2910 e->function = e->referents[0]->name;
2911 }
2912 else
2913 {
2914 string sugs = levenshtein_suggest(e->function, collect_functions(), 5); // print 5 funcs
2915 throw SEMANTIC_ERROR(_F("unresolved function%s",
2916 sugs.empty() ? "" : (_(" (similar: ") + sugs + ")").c_str()),
2917 e->tok);
2918 }
2919
2920 // In monitor mode, tapset functions used in the synthetic probe are not resolved and added
2921 // to the master list at the same time as the other functions so we must add them here to
2922 // allow the translator to generate the functions in the module.
2923 if (session.monitor && session.functions.find(e->function) == session.functions.end())
2924 session.functions[e->function] = fds[0]; // no overload
2925 }
2926
2927 /* find_var will return an argument other than zero if the name matches the var
2928 * name ie, if the current local name matches the name passed to find_var.
2929 * arity=-1 means any.
2930 */
2931 vardecl*
2932 symresolution_info::find_var (const string& name, int arity, const token* tok)
2933 {
2934 if (current_function || current_probe)
2935 {
2936 // search locals
2937 vector<vardecl*>& locals = (current_function ?
2938 current_function->locals :
2939 current_probe->locals);
2940
2941
2942 for (unsigned i=0; i<locals.size(); i++)
2943 if (locals[i]->name == name)
2944 {
2945 if (session.verbose > 2)
2946 cerr << _F(" local %s is already defined",
2947 name.c_str()) << endl;
2948
2949 locals[i]->set_arity (arity, tok);
2950 return locals[i];
2951 }
2952 }
2953
2954 // search function formal parameters (for scalars)
2955 if (arity == 0 && current_function)
2956 for (unsigned i=0; i<current_function->formal_args.size(); i++)
2957 if (current_function->formal_args[i]->name == name)
2958 {
2959 // NB: no need to check arity here: formal args always scalar
2960 if (session.verbose > 2)
2961 cerr << _F(" local %s is formal parameter",
2962 name.c_str()) << endl;
2963
2964 current_function->formal_args[i]->set_arity (0, tok);
2965 return current_function->formal_args[i];
2966 }
2967
2968 // search processed globals
2969 string gname, pname;
2970 if (unmangled_p)
2971 {
2972 gname = pname = string(name);
2973 }
2974 else
2975 {
2976 gname = "__global_" + string(name);
2977 pname = "__private_" + detox_path(tok->location.file->name) + string(name);
2978 }
2979 for (unsigned i=0; i<session.globals.size(); i++)
2980 {
2981 if ((session.globals[i]->name == name && startswith(name, "__global_")) ||
2982 (session.globals[i]->name == gname) ||
2983 (session.globals[i]->name == pname))
2984 {
2985 if (session.verbose > 2)
2986 cerr << _F(" global %s is already defined",
2987 name.c_str()) << endl;
2988
2989 if (! session.suppress_warnings)
2990 {
2991 vardecl* v = session.globals[i];
2992 stapfile* f = tok->location.file;
2993 if (!session.is_user_file(f->name) && v->tok && v->tok->location.file != f && !f->synthetic)
2994 {
2995 session.print_warning (_F("cross-file global variable reference to %s from",
2996 lex_cast(*v->tok).c_str()), tok);
2997 }
2998 }
2999 session.globals[i]->set_arity (arity, tok);
3000 return session.globals[i];
3001 }
3002 }
3003
3004 // search chosen-tapset globals
3005 for (unsigned i=0; i<session.files.size(); i++)
3006 {
3007 stapfile* f = session.files[i];
3008 for (unsigned j=0; j<f->globals.size(); j++)
3009 {
3010 vardecl* g = f->globals[j];
3011 if ((g->name == gname) ||
3012 (g->name == pname)) // private global within tapset probe alias
3013 {
3014 if (session.verbose > 2)
3015 cerr << _F(" global %s is defined in chosen-tapset-file %s",
3016 name.c_str(), f->name.c_str()) << endl;
3017
3018 g->set_arity (arity, tok);
3019
3020 session.globals.push_back (g);
3021 return g;
3022 }
3023 }
3024 }
3025
3026
3027 // search not-yet-chosen library globals
3028 for (unsigned i=0; i<session.library_files.size(); i++)
3029 {
3030 stapfile* f = session.library_files[i];
3031 for (unsigned j=0; j<f->globals.size(); j++)
3032 {
3033 vardecl* g = f->globals[j];
3034 if ((g->name == gname) ||
3035 (g->name == pname)) // private global within tapset probe alias
3036 {
3037 if (session.verbose > 2)
3038 cerr << _F(" global %s is defined in new-tapset-file %s",
3039 name.c_str(), f->name.c_str()) << endl;
3040
3041 g->set_arity (arity, tok);
3042
3043 assert (find (session.files.begin(), session.files.end(), f) == session.files.end());
3044 session.files.push_back (f);
3045 session.globals.push_back (g);
3046
3047 return g;
3048 }
3049 }
3050 }
3051
3052 return 0;
3053 }
3054
3055
3056 class functioncall_security_check: public traversing_visitor
3057 {
3058 systemtap_session& session;
3059 functioncall* call;
3060 functiondecl* current_function;
3061 public:
3062 functioncall_security_check(systemtap_session&s, functioncall* c): session(s),call(c) {}
3063 void traverse (functiondecl* d)
3064 {
3065 current_function = d;
3066 current_function->body->visit(this);
3067 }
3068
3069 void visit_embeddedcode (embeddedcode *s)
3070 {
3071 // Don't allow embedded C functions in unprivileged mode unless
3072 // they are tagged with /* unprivileged */ or /* myproc-unprivileged */
3073 // or we're in a usermode runtime.
3074 if (! pr_contains (session.privilege, pr_stapdev) &&
3075 ! pr_contains (session.privilege, pr_stapsys) &&
3076 ! session.runtime_usermode_p () &&
3077 ! s->tagged_p ("/* unprivileged */") &&
3078 ! s->tagged_p ("/* myproc-unprivileged */"))
3079 throw SEMANTIC_ERROR (_F("function may not be used when --privilege=%s is specified",
3080 pr_name (session.privilege)),
3081 current_function->tok);
3082
3083 // Allow only /* bpf */ functions in bpf mode.
3084 if ((session.runtime_mode == systemtap_session::bpf_runtime)
3085 != (s->tagged_p ("/* bpf */")))
3086 {
3087 if (session.runtime_mode == systemtap_session::bpf_runtime)
3088 throw SEMANTIC_ERROR (_("function may not be used with bpf runtime"),
3089 current_function->tok);
3090 else
3091 throw SEMANTIC_ERROR (_("function requires bpf runtime"),
3092 current_function->tok);
3093 }
3094
3095 // Don't allow /* guru */ functions unless caller is privileged.
3096 if (!call->synthetic && !call->tok->location.file->privileged &&
3097 s->tagged_p ("/* guru */"))
3098 throw SEMANTIC_ERROR (_("function may not be used unless -g is specified"),
3099 call->tok);
3100 }
3101 };
3102
3103
3104 vector<functiondecl*>
3105 symresolution_info::find_functions (functioncall *call, const string& name, unsigned arity, const token *tok)
3106 {
3107 vector<functiondecl*> functions;
3108 functiondecl* last = 0; // used for error message
3109
3110 // the common path
3111
3112 // internal global functions bypassing the parser, such as __global_dwarf_tvar_[gs]et
3113 if ((session.functions.find(name) != session.functions.end()) && startswith(name, "__private_"))
3114 {
3115 functiondecl* fd = session.functions[name];
3116 assert (fd->name == name);
3117 if (fd->formal_args.size() == arity)
3118 functions.push_back(fd);
3119 else
3120 last = fd;
3121 }
3122
3123 // functions scanned by the parser are overloaded
3124 unsigned alternatives = session.overload_count[name];
3125 for (unsigned alt = 0; alt < alternatives; alt++)
3126 {
3127 bool found = false; // multiple inclusion guard
3128 string gname = "__global_" + string(name) + "__overload_" + lex_cast(alt);
3129 string pname = "__private_" + detox_path(tok->location.file->name) + string(name) +
3130 "__overload_" + lex_cast(alt);
3131
3132 // tapset or user script global functions coming from the parser
3133 if (!found && session.functions.find(gname) != session.functions.end())
3134 {
3135 functiondecl* fd = session.functions[gname];
3136 assert (fd->name == gname);
3137 if (fd->formal_args.size() == arity)
3138 {
3139 functions.push_back(fd);
3140 found = true;
3141 }
3142 else
3143 last = fd;
3144 }
3145
3146 // tapset or user script private functions coming from the parser
3147 if (!found && session.functions.find(pname) != session.functions.end())
3148 {
3149 functiondecl* fd = session.functions[pname];
3150 assert (fd->name == pname);
3151 if (fd->formal_args.size() == arity)
3152 {
3153 functions.push_back(fd);
3154 found = true;
3155 }
3156 else
3157 last = fd;
3158 }
3159
3160 // search chosen-tapset-file functions
3161 for (unsigned i=0; !found && i<session.files.size(); i++)
3162 {
3163 stapfile* f = session.files[i];
3164 for (unsigned j=0; !found && j<f->functions.size(); j++)
3165 {
3166 if ((f->functions[j]->name == gname) ||
3167 (f->functions[j]->name == pname))
3168 {
3169 if (f->functions[j]->formal_args.size() == arity)
3170 {
3171 // put library into the queue if not already there
3172 if (session.verbose > 2)
3173 cerr << _F(" function %s is defined in chosen-tapset-file %s",
3174 name.c_str(), f->name.c_str()) << endl;
3175
3176 functions.push_back(f->functions[j]);
3177 found = true;
3178 }
3179 else
3180 last = f->functions[j];
3181 }
3182 }
3183 }
3184
3185 // search not-yet-chosen library functions
3186 for (unsigned i=0; !found && i<session.library_files.size(); i++)
3187 {
3188 stapfile* f = session.library_files[i];
3189 for (unsigned j=0; !found && j<f->functions.size(); j++)
3190 {
3191 if ((f->functions[j]->name == gname) ||
3192 (f->functions[j]->name == pname))
3193 {
3194 if (f->functions[j]->formal_args.size() == arity)
3195 {
3196 // put library into the queue if not already there
3197 if (session.verbose > 2)
3198 cerr << _F(" function %s is defined in new-tapset-file %s",
3199 name.c_str(), f->name.c_str()) << endl;
3200
3201 assert (find (session.files.begin(), session.files.end(), f) == session.files.end());
3202 session.files.push_back (f);
3203
3204 functions.push_back(f->functions[j]);
3205 found = true;
3206 }
3207 else
3208 last = f->functions[j];
3209 }
3210 }
3211 }
3212 }
3213
3214 // suggest last found function with matching name
3215 if (last && functions.empty())
3216 {
3217 throw SEMANTIC_ERROR(_F("arity mismatch found (function '%s' takes %zu args)",
3218 name.c_str(), last->formal_args.size()), tok, last->tok);
3219 }
3220
3221 // check every function for safety/security constraints
3222 functioncall_security_check fsc(session, call);
3223 for (auto gi = functions.begin(); gi != functions.end(); gi++) {
3224 fsc.traverse(*gi);
3225
3226 // record this as a used function for later sym resolution
3227 functiondecl *f = *gi;
3228 const string& fname = f->name;
3229 functiondecl *f2 = session.functions[fname];
3230 if (f2 && f != f2)
3231 session.print_error (SEMANTIC_ERROR (_("conflicting functions"),
3232 f->tok, f2->tok));
3233 session.functions[fname] = f;
3234 }
3235
3236 return functions;
3237 }
3238
3239
3240 set<string>
3241 symresolution_info::collect_functions(void)
3242 {
3243 set<string> funcs;
3244
3245 for (map<string,functiondecl*>::const_iterator it = session.functions.begin();
3246 it != session.functions.end(); ++it)
3247 funcs.insert(it->second->unmangled_name);
3248
3249 // search library functions
3250 for (unsigned i=0; i<session.library_files.size(); i++)
3251 {
3252 stapfile* f = session.library_files[i];
3253 for (unsigned j=0; j<f->functions.size(); j++)
3254 if (! f->functions[j]->name.starts_with("__private_"))
3255 funcs.insert(f->functions[j]->unmangled_name);
3256 }
3257
3258 return funcs;
3259 }
3260
3261 // ------------------------------------------------------------------------
3262 // optimization
3263
3264
3265 // Do away with functiondecls that are never (transitively) called
3266 // from probes.
3267 //
3268 // PR24239: this will generally not trigger, since we avoid adding
3269 // uncalled functions to s.functions[].
3270 void semantic_pass_opt1 (systemtap_session& s, bool& relaxed_p)
3271 {
3272 functioncall_traversing_visitor ftv;
3273 for (unsigned i=0; i<s.probes.size(); i++)
3274 {
3275 s.probes[i]->body->visit (& ftv);
3276 if (s.probes[i]->sole_location()->condition)
3277 s.probes[i]->sole_location()->condition->visit (& ftv);
3278 }
3279 vector<functiondecl*> new_unused_functions;
3280 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
3281 {
3282 functiondecl* fd = it->second;
3283 if (ftv.seen.find(fd) == ftv.seen.end())
3284 {
3285 if (! fd->synthetic && s.is_user_file(fd->tok->location.file->name))
3286 s.print_warning (_F("Eliding unused function '%s'",
3287 fd->unmangled_name.to_string().c_str()),
3288 fd->tok);
3289 // s.functions.erase (it); // NB: can't, since we're already iterating upon it
3290 new_unused_functions.push_back (fd);
3291 relaxed_p = false;
3292 }
3293 }
3294 for (unsigned i=0; i<new_unused_functions.size(); i++)
3295 {
3296 map<string,functiondecl*>::iterator where = s.functions.find (new_unused_functions[i]->name);
3297 assert (where != s.functions.end());
3298 s.functions.erase (where);
3299 if (s.tapset_compile_coverage)
3300 s.unused_functions.push_back (new_unused_functions[i]);
3301 }
3302 }
3303
3304
3305 // ------------------------------------------------------------------------
3306
3307 // Do away with local & global variables that are never
3308 // written nor read.
3309 void semantic_pass_opt2 (systemtap_session& s, bool& relaxed_p, unsigned iterations)
3310 {
3311 varuse_collecting_visitor vut(s);
3312
3313 for (unsigned i=0; i<s.probes.size(); i++)
3314 {
3315 s.probes[i]->body->visit (& vut);
3316
3317 if (s.probes[i]->sole_location()->condition)
3318 s.probes[i]->sole_location()->condition->visit (& vut);
3319 }
3320
3321 // NB: Since varuse_collecting_visitor also traverses down
3322 // actually called functions, we don't need to explicitly
3323 // iterate over them. Uncalled ones should have been pruned
3324 // in _opt1 above.
3325 //
3326 // for (unsigned i=0; i<s.functions.size(); i++)
3327 // s.functions[i]->body->visit (& vut);
3328
3329 // Now in vut.read/written, we have a mixture of all locals, globals
3330
3331 for (unsigned i=0; i<s.probes.size(); i++)
3332 for (unsigned j=0; j<s.probes[i]->locals.size(); /* see below */)
3333 {
3334 vardecl* l = s.probes[i]->locals[j];
3335
3336 // skip over "special" locals
3337 if (l->synthetic) { j++; continue; }
3338
3339 if (vut.read.find (l) == vut.read.end() &&
3340 vut.written.find (l) == vut.written.end())
3341 {
3342 if (!l->tok->location.file->synthetic && s.is_user_file(l->tok->location.file->name))
3343 s.print_warning (_F("Eliding unused variable '%s'",
3344 l->unmangled_name.to_string().c_str()),
3345 l->tok);
3346 if (s.tapset_compile_coverage) {
3347 s.probes[i]->unused_locals.push_back
3348 (s.probes[i]->locals[j]);
3349 }
3350 s.probes[i]->locals.erase(s.probes[i]->locals.begin() + j);
3351 relaxed_p = false;
3352 // don't increment j
3353 }
3354 else
3355 {
3356 if (vut.written.find (l) == vut.written.end())
3357 if (iterations == 0 && ! s.suppress_warnings)
3358 {
3359 set<string> vars;
3360
3361 // like collect_functions()
3362 vector<vardecl*>::iterator it;
3363 for (it = s.probes[i]->locals.begin(); it != s.probes[i]->locals.end(); it++)
3364 vars.insert((*it)->unmangled_name);
3365 for (it = s.globals.begin(); it != s.globals.end(); it++)
3366 if (! (*it)->unmangled_name.starts_with("__private_"))
3367 vars.insert((*it)->unmangled_name);
3368
3369 vars.erase(l->name);
3370 string sugs = levenshtein_suggest(l->name, vars, 5); // suggest top 5 vars
3371 s.print_warning (_F("never-assigned local variable '%s'%s",
3372 l->unmangled_name.to_string().c_str(),
3373 (sugs.empty() ? "" :
3374 (_(" (similar: ") + sugs + ")")).c_str()), l->tok);
3375 }
3376 j++;
3377 }
3378 }
3379
3380 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
3381 {
3382 functiondecl *fd = it->second;
3383 for (unsigned j=0; j<fd->locals.size(); /* see below */)
3384 {
3385 vardecl* l = fd->locals[j];
3386 if (vut.read.find (l) == vut.read.end() &&
3387 vut.written.find (l) == vut.written.end())
3388 {
3389 if (!l->tok->location.file->synthetic && s.is_user_file(l->tok->location.file->name))
3390 s.print_warning (_F("Eliding unused variable '%s'",
3391 l->unmangled_name.to_string().c_str()),
3392 l->tok);
3393 if (s.tapset_compile_coverage) {
3394 fd->unused_locals.push_back (fd->locals[j]);
3395 }
3396 fd->locals.erase(fd->locals.begin() + j);
3397 relaxed_p = false;
3398 // don't increment j
3399 }
3400 else
3401 {
3402 if (vut.written.find (l) == vut.written.end())
3403 if (iterations == 0 && ! s.suppress_warnings)
3404 {
3405 set<string> vars;
3406 vector<vardecl*>::iterator it;
3407 for (it = fd->formal_args.begin() ;
3408 it != fd->formal_args.end(); it++)
3409 vars.insert((*it)->unmangled_name);
3410 for (it = fd->locals.begin(); it != fd->locals.end(); it++)
3411 vars.insert((*it)->unmangled_name);
3412 for (it = s.globals.begin(); it != s.globals.end(); it++)
3413 if (! (*it)->unmangled_name.starts_with("__private_"))
3414 vars.insert((*it)->unmangled_name);
3415
3416 vars.erase(l->name);
3417 string sugs = levenshtein_suggest(l->name, vars, 5); // suggest top 5 vars
3418 s.print_warning (_F("never-assigned local variable '%s'%s",
3419 l->unmangled_name.to_string().c_str(),
3420 (sugs.empty() ? "" :
3421 (_(" (similar: ") + sugs + ")")).c_str()), l->tok);
3422 }
3423
3424 j++;
3425 }
3426 }
3427 }
3428 for (unsigned i=0; i<s.globals.size(); /* see below */)
3429 {
3430 vardecl* l = s.globals[i];
3431 if (vut.read.find (l) == vut.read.end() &&
3432 vut.written.find (l) == vut.written.end())
3433 {
3434 if (!l->tok->location.file->synthetic && s.is_user_file(l->tok->location.file->name))
3435 s.print_warning (_F("Eliding unused variable '%s'",
3436 l->unmangled_name.to_string().c_str()),
3437 l->tok);
3438 if (s.tapset_compile_coverage) {
3439 s.unused_globals.push_back(s.globals[i]);
3440 }
3441 s.globals.erase(s.globals.begin() + i);
3442 relaxed_p = false;
3443 // don't increment i
3444 }
3445 else
3446 {
3447 if (vut.written.find (l) == vut.written.end() && ! l->init) // no initializer
3448 if (iterations == 0 && ! s.suppress_warnings)
3449 {
3450 // check if it was initialized on the command line via
3451 // a '-G' option
3452 bool init_by_gopt = false;
3453 string init_prefix (l->unmangled_name.to_string() + "=");
3454 for (auto gi = s.globalopts.begin(); gi != s.globalopts.end();
3455 gi++)
3456 if (! gi->compare(0, init_prefix.size(), init_prefix))
3457 {
3458 init_by_gopt = true;
3459 break;
3460 }
3461 if (! init_by_gopt)
3462 {
3463 set<string> vars;
3464 for (auto it = s.globals.begin(); it != s.globals.end();
3465 it++)
3466 if (l->name != (*it)->unmangled_name)
3467 if (! (*it)->unmangled_name.starts_with("__private_"))
3468 vars.insert((*it)->unmangled_name);
3469
3470 // suggest top 5 vars
3471 string sugs = levenshtein_suggest(l->name, vars, 5);
3472 s.print_warning (_F("never-assigned global variable '%s'%s",
3473 l->unmangled_name.to_string().c_str(),
3474 (sugs.empty() ? "" :
3475 (_(" (similar: ") + sugs + ")")).c_str()),
3476 l->tok);
3477 }
3478 }
3479
3480 i++;
3481 }
3482 }
3483 }
3484
3485
3486 // ------------------------------------------------------------------------
3487
3488 struct dead_assignment_remover: public update_visitor
3489 {
3490 systemtap_session& session;
3491 bool& relaxed_p;
3492 const varuse_collecting_visitor& vut;
3493
3494 dead_assignment_remover(systemtap_session& s, bool& r,
3495 const varuse_collecting_visitor& v):
3496 update_visitor(s.verbose), session(s), relaxed_p(r), vut(v) {}
3497
3498 void visit_assignment (assignment* e);
3499 void visit_try_block (try_block *s);
3500 };
3501
3502
3503 // symbol_fetcher augmented to allow target-symbol types, but NULLed.
3504 struct assignment_symbol_fetcher
3505 : public symbol_fetcher
3506 {
3507 const token* assignment_t;
3508
3509 assignment_symbol_fetcher (symbol *&sym, const token* a_t): symbol_fetcher(sym),
3510 assignment_t(a_t)
3511 {}
3512
3513 void visit_target_symbol (target_symbol*)
3514 {
3515 sym = NULL;
3516 }
3517
3518 void visit_atvar_op (atvar_op*)
3519 {
3520 sym = NULL;
3521 }
3522
3523 void visit_cast_op (cast_op*)
3524 {
3525 sym = NULL;
3526 }
3527
3528 void visit_autocast_op (autocast_op*)
3529 {
3530 sym = NULL;
3531 }
3532
3533 void visit_target_deref (target_deref*)
3534 {
3535 sym = NULL;
3536 }
3537
3538 void visit_target_register (target_register*)
3539 {
3540 sym = NULL;
3541 }
3542
3543 void throwone (const token* t)
3544 {
3545 if (t->type == tok_operator && t->content == ".")
3546 // guess someone misused . in $foo->bar.baz expression
3547 // XXX why are we only checking this in lvalues?
3548 throw SEMANTIC_ERROR (_("Expecting lvalue for assignment, try -> instead"),
3549 assignment_t, t);
3550 else
3551 throw SEMANTIC_ERROR (_("Expecting lvalue for assignment"), assignment_t, t);
3552 }
3553 };
3554
3555 symbol *
3556 get_assignment_symbol_within_expression (expression *e, const token *a_t)
3557 {
3558 symbol *sym = NULL;
3559 assignment_symbol_fetcher fetcher(sym, a_t);
3560 e->visit (&fetcher);
3561 return sym; // NB: may be null!
3562 }
3563
3564
3565 void
3566 dead_assignment_remover::visit_assignment (assignment* e)
3567 {
3568 replace (e->left);
3569 replace (e->right);
3570
3571 symbol* left = get_assignment_symbol_within_expression (e->left, e->tok);
3572 if (left) // not unresolved $target, so intended sideeffect may be elided
3573 {
3574 vardecl* leftvar = left->referent;
3575 if (vut.read.find(leftvar) == vut.read.end()) // var never read?
3576 {
3577 // NB: Not so fast! The left side could be an array whose
3578 // index expressions may have side-effects. This would be
3579 // OK if we could replace the array assignment with a
3580 // statement-expression containing all the index expressions
3581 // and the rvalue... but we can't.
3582 // Another possibility is that we have an unread global variable
3583 // which are kept for probe end value display.
3584
3585 bool is_global = false;
3586 vector<vardecl*>::iterator it;
3587 for (it = session.globals.begin(); it != session.globals.end(); it++)
3588 if (leftvar->name == (*it)->name)
3589 {
3590 is_global = true;
3591 break;
3592 }
3593
3594 varuse_collecting_visitor lvut(session);
3595 e->left->visit (& lvut);
3596 if (lvut.side_effect_free () && !is_global // XXX: use _wrt() once we track focal_vars
3597 && !leftvar->synthetic) // don't elide assignment to synthetic $context variables
3598 {
3599 /* PR 1119: NB: This is not necessary here. A write-only
3600 variable will also be elided soon at the next _opt2 iteration.
3601 if (e->left->tok->location.file->name == session.user_file->name) // !tapset
3602 session.print_warning("eliding write-only ", *e->left->tok);
3603 else
3604 */
3605 if (!e->left->tok->location.file->synthetic && session.is_user_file(e->left->tok->location.file->name))
3606 session.print_warning(_F("Eliding assignment to '%s'",
3607 leftvar->unmangled_name.to_string().c_str()), e->tok);
3608 provide (e->right); // goodbye assignment*
3609 relaxed_p = false;
3610 return;
3611 }
3612 }
3613 }
3614 provide (e);
3615 }
3616
3617
3618 void
3619 dead_assignment_remover::visit_try_block (try_block *s)
3620 {
3621 replace (s->try_block);
3622 if (s->catch_error_var)
3623 {
3624 vardecl* errvar = s->catch_error_var->referent;
3625 if (vut.read.find(errvar) == vut.read.end()) // never read?
3626 {
3627 if (session.verbose>2)
3628 clog << _F("Eliding unused error string catcher %s at %s",
3629 errvar->unmangled_name.to_string().c_str(),
3630 lex_cast(*s->tok).c_str()) << endl;
3631 s->catch_error_var = 0;
3632 }
3633 }
3634 replace (s->catch_block);
3635 provide (s);
3636 }
3637
3638
3639 // Let's remove assignments to variables that are never read. We
3640 // rewrite "(foo = expr)" as "(expr)". This makes foo a candidate to
3641 // be optimized away as an unused variable, and expr a candidate to be
3642 // removed as a side-effect-free statement expression. Wahoo!
3643 void semantic_pass_opt3 (systemtap_session& s, bool& relaxed_p)
3644 {
3645 // Recompute the varuse data, which will probably match the opt2
3646 // copy of the computation, except for those totally unused
3647 // variables that opt2 removed.
3648 varuse_collecting_visitor vut(s);
3649 for (unsigned i=0; i<s.probes.size(); i++)
3650 s.probes[i]->body->visit (& vut); // includes reachable functions too
3651
3652 dead_assignment_remover dar (s, relaxed_p, vut);
3653 // This instance may be reused for multiple probe/function body trims.
3654
3655 for (unsigned i=0; i<s.probes.size(); i++)
3656 dar.replace (s.probes[i]->body);
3657 for (map<string,functiondecl*>::iterator it = s.functions.begin();
3658 it != s.functions.end(); it++)
3659 dar.replace (it->second->body);
3660 // The rewrite operation is performed within the visitor.
3661
3662 // XXX: we could also zap write-only globals here
3663 }
3664
3665
3666 // ------------------------------------------------------------------------
3667
3668 struct dead_stmtexpr_remover: public update_visitor
3669 {
3670 systemtap_session& session;
3671 bool& relaxed_p;
3672 set<vardecl*> focal_vars; // vars considered subject to side-effects
3673
3674 dead_stmtexpr_remover(systemtap_session& s, bool& r):
3675 update_visitor(s.verbose), session(s), relaxed_p(r) {}
3676
3677 void visit_block (block *s);
3678 void visit_try_block (try_block *s);
3679 void visit_null_statement (null_statement *s);
3680 void visit_if_statement (if_statement* s);
3681 void visit_foreach_loop (foreach_loop *s);
3682 void visit_for_loop (for_loop *s);
3683 // XXX: and other places where stmt_expr's might be nested
3684
3685 void visit_expr_statement (expr_statement *s);
3686 };
3687
3688
3689 void
3690 dead_stmtexpr_remover::visit_null_statement (null_statement *s)
3691 {
3692 // easy!
3693 if (session.verbose>2)
3694 clog << _("Eliding side-effect-free null statement ") << *s->tok << endl;
3695 s = 0;
3696 provide (s);
3697 }
3698
3699
3700 void
3701 dead_stmtexpr_remover::visit_block (block *s)
3702 {
3703 vector<statement*> new_stmts;
3704 for (unsigned i=0; i<s->statements.size(); i++ )
3705 {
3706 statement* new_stmt = require (s->statements[i], true);
3707 if (new_stmt != 0)
3708 {
3709 // flatten nested blocks into this one
3710 block *b = dynamic_cast<block *>(new_stmt);
3711 if (b)
3712 {
3713 if (session.verbose>2)
3714 clog << _("Flattening nested block ") << *b->tok << endl;
3715 new_stmts.insert(new_stmts.end(),
3716 b->statements.begin(), b->statements.end());
3717 relaxed_p = false;
3718 }
3719 else
3720 new_stmts.push_back (new_stmt);
3721 }
3722 }
3723 if (new_stmts.size() == 0)
3724 {
3725 if (session.verbose>2)
3726 clog << _("Eliding side-effect-free empty block ") << *s->tok << endl;
3727 s = 0;
3728 }
3729 else if (new_stmts.size() == 1)
3730 {
3731 if (session.verbose>2)
3732 clog << _("Eliding side-effect-free singleton block ") << *s->tok << endl;
3733 provide (new_stmts[0]);
3734 return;
3735 }
3736 else
3737 s->statements = new_stmts;
3738 provide (s);
3739 }
3740
3741
3742 void
3743 dead_stmtexpr_remover::visit_try_block (try_block *s)
3744 {
3745 replace (s->try_block, true);
3746 replace (s->catch_block, true); // null catch{} is ok and useful
3747 if (s->try_block == 0)
3748 {
3749 if (session.verbose>2)
3750 clog << _("Eliding empty try {} block ") << *s->tok << endl;
3751 s = 0;
3752 }
3753 provide (s);
3754 }
3755
3756
3757 void
3758 dead_stmtexpr_remover::visit_if_statement (if_statement *s)
3759 {
3760 replace (s->thenblock, true);
3761 replace (s->elseblock, true);
3762
3763 if (s->thenblock == 0)
3764 {
3765 if (s->elseblock == 0)
3766 {
3767 // We may be able to elide this statement, if the condition
3768 // expression is side-effect-free.
3769 varuse_collecting_visitor vct(session);
3770 s->condition->visit(& vct);
3771 if (vct.side_effect_free ())
3772 {
3773 if (session.verbose>2)
3774 clog << _("Eliding side-effect-free if statement ")
3775 << *s->tok << endl;
3776 s = 0; // yeah, baby
3777 }
3778 else
3779 {
3780 // We can still turn it into a simple expr_statement though...
3781 if (session.verbose>2)
3782 clog << _("Creating simple evaluation from if statement ")
3783 << *s->tok << endl;
3784 expr_statement *es = new expr_statement;
3785 es->value = s->condition;
3786 es->tok = es->value->tok;
3787 provide (es);
3788 return;
3789 }
3790 }
3791 else
3792 {
3793 // For an else without a then, we can invert the condition logic to
3794 // avoid having a null statement in the thenblock
3795 if (session.verbose>2)
3796 clog << _("Inverting the condition of if statement ")
3797 << *s->tok << endl;
3798 unary_expression *ue = new unary_expression;
3799 ue->operand = s->condition;
3800 ue->tok = ue->operand->tok;
3801 ue->op = "!";
3802 s->condition = ue;
3803 s->thenblock = s->elseblock;
3804 s->elseblock = 0;
3805 }
3806 }
3807 provide (s);
3808 }
3809
3810 void
3811 dead_stmtexpr_remover::visit_foreach_loop (foreach_loop *s)
3812 {
3813 replace (s->block, true);
3814
3815 if (s->block == 0)
3816 {
3817 // XXX what if s->limit has side effects?
3818 // XXX what about s->indexes or s->value used outside the loop?
3819 if(session.verbose > 2)
3820 clog << _("Eliding side-effect-free foreach statement ") << *s->tok << endl;
3821 s = 0; // yeah, baby
3822 }
3823 provide (s);
3824 }
3825
3826 void
3827 dead_stmtexpr_remover::visit_for_loop (for_loop *s)
3828 {
3829 replace (s->block, true);
3830
3831 if (s->block == 0)
3832 {
3833 // We may be able to elide this statement, if the condition
3834 // expression is side-effect-free.
3835 varuse_collecting_visitor vct(session);
3836 if (s->init) s->init->visit(& vct);
3837 s->cond->visit(& vct);
3838 if (s->incr) s->incr->visit(& vct);
3839 if (vct.side_effect_free ())
3840 {
3841 if (session.verbose>2)
3842 clog << _("Eliding side-effect-free for statement ") << *s->tok << endl;
3843 s = 0; // yeah, baby
3844 }
3845 else
3846 {
3847 // Can't elide this whole statement; put a null in there.
3848 s->block = new null_statement(s->tok);
3849 }
3850 }
3851 provide (s);
3852 }
3853
3854
3855
3856 void
3857 dead_stmtexpr_remover::visit_expr_statement (expr_statement *s)
3858 {
3859 // Run a varuse query against the operand expression. If it has no
3860 // side-effects, replace the entire statement expression by a null
3861 // statement with the provide() call.
3862 //
3863 // Unlike many other visitors, we do *not* traverse this outermost
3864 // one into the expression subtrees. There is no need - no
3865 // expr_statement nodes will be found there. (Function bodies
3866 // need to be visited explicitly by our caller.)
3867 //
3868 // NB. While we don't share nodes in the parse tree, let's not
3869 // deallocate *s anyway, just in case...
3870
3871 varuse_collecting_visitor vut(session);
3872 s->value->visit (& vut);
3873
3874 if (vut.side_effect_free_wrt (focal_vars))
3875 {
3876 /* PR 1119: NB: this message is not a good idea here. It can
3877 name some arbitrary RHS expression of an assignment.
3878 if (s->value->tok->location.file->name == session.user_file->name) // not tapset
3879 session.print_warning("eliding never-assigned ", *s->value->tok);
3880 else
3881 */
3882 if (!s->value->tok->location.file->synthetic && session.is_user_file(s->value->tok->location.file->name))
3883 session.print_warning("Eliding side-effect-free expression ", s->tok);
3884
3885 // NB: this 0 pointer is invalid to leave around for any length of
3886 // time, but the parent parse tree objects above handle it.
3887 s = 0;
3888 relaxed_p = false;
3889 }
3890 provide (s);
3891 }
3892
3893
3894 void semantic_pass_opt4 (systemtap_session& s, bool& relaxed_p)
3895 {
3896 // Finally, let's remove some statement-expressions that have no
3897 // side-effect. These should be exactly those whose private varuse
3898 // visitors come back with an empty "written" and "embedded" lists.
3899
3900 dead_stmtexpr_remover duv (s, relaxed_p);
3901 // This instance may be reused for multiple probe/function body trims.
3902
3903 for (unsigned i=0; i<s.probes.size(); i++)
3904 {
3905 assert_no_interrupts();
3906
3907 derived_probe* p = s.probes[i];
3908
3909 duv.focal_vars.clear ();
3910 duv.focal_vars.insert (s.globals.begin(),
3911 s.globals.end());
3912 duv.focal_vars.insert (p->locals.begin(),
3913 p->locals.end());
3914
3915 duv.replace (p->body, true);
3916
3917 if (p->body == 0)
3918 {
3919 p->body = new null_statement(p->tok);
3920 s.empty_probes.insert(p);
3921 }
3922 }
3923
3924 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
3925 {
3926 assert_no_interrupts();
3927
3928 functiondecl* fn = it->second;
3929 duv.focal_vars.clear ();
3930 duv.focal_vars.insert (fn->locals.begin(),
3931 fn->locals.end());
3932 duv.focal_vars.insert (fn->formal_args.begin(),
3933 fn->formal_args.end());
3934 duv.focal_vars.insert (s.globals.begin(),
3935 s.globals.end());
3936
3937 duv.replace (fn->body, true);
3938 if (fn->body == 0)
3939 {
3940 s.print_warning (_F("side-effect-free function '%s'",
3941 fn->unmangled_name.to_string().c_str()),
3942 fn->tok);
3943
3944 fn->body = new null_statement(fn->tok);
3945
3946 // XXX: the next iteration of the outer optimization loop may
3947 // take this new null_statement away again, and thus give us a
3948 // fresh warning. It would be better if this fixup was performed
3949 // only after the relaxation iterations.
3950 // XXX: or else see bug #6469.
3951 }
3952 }
3953 }
3954
3955
3956 // ------------------------------------------------------------------------
3957
3958 // The goal of this visitor is to reduce top-level expressions in void context
3959 // into separate statements that evaluate each subcomponent of the expression.
3960 // The dead-statement-remover can later remove some parts if they have no side
3961 // effects.
3962 //
3963 // All expressions must be overridden here so we never visit their subexpressions
3964 // accidentally. Thus, the only visited expressions should be value of an
3965 // expr_statement.
3966 //
3967 // For an expression to replace its expr_statement with something else, it will
3968 // let the new statement provide(), and then provide(0) for itself. The
3969 // expr_statement will take this as a sign that it's been replaced.
3970 struct void_statement_reducer: public update_visitor
3971 {
3972 systemtap_session& session;
3973 bool& relaxed_p;
3974 set<vardecl*> focal_vars; // vars considered subject to side-effects
3975
3976 void_statement_reducer(systemtap_session& s, bool& r):
3977 update_visitor(s.verbose), session(s), relaxed_p(r) {}
3978
3979 void visit_expr_statement (expr_statement* s);
3980
3981 // expressions in conditional / loop controls are definitely a side effect,
3982 // but still recurse into the child statements
3983 void visit_if_statement (if_statement* s);
3984 void visit_for_loop (for_loop* s);
3985 void visit_foreach_loop (foreach_loop* s);
3986
3987 // these expressions get rewritten into their statement equivalents
3988 void visit_logical_or_expr (logical_or_expr* e);
3989 void visit_logical_and_expr (logical_and_expr* e);
3990 void visit_ternary_expression (ternary_expression* e);
3991
3992 // all of these can (usually) be reduced into simpler statements
3993 void visit_binary_expression (binary_expression* e);
3994 void visit_unary_expression (unary_expression* e);
3995 void visit_regex_query (regex_query* e); // XXX depends on subexpr extraction
3996 void visit_comparison (comparison* e);
3997 void visit_concatenation (concatenation* e);
3998 void visit_functioncall (functioncall* e);
3999 void visit_print_format (print_format* e);
4000 void visit_target_symbol (target_symbol* e);
4001 void visit_atvar_op (atvar_op* e);
4002 void visit_cast_op (cast_op* e);
4003 void visit_autocast_op (autocast_op* e);
4004 void visit_defined_op (defined_op* e);
4005
4006 // these are a bit hairy to grok due to the intricacies of indexables and
4007 // stats, so I'm chickening out and skipping them...
4008 void visit_array_in (array_in* e) { provide (e); }
4009 void visit_arrayindex (arrayindex* e) { provide (e); }
4010 void visit_stat_op (stat_op* e) { provide (e); }
4011 void visit_hist_op (hist_op* e) { provide (e); }
4012
4013 // these can't be reduced because they always have an effect
4014 void visit_return_statement (return_statement* s) { provide (s); }
4015 void visit_delete_statement (delete_statement* s) { provide (s); }
4016 void visit_pre_crement (pre_crement* e) { provide (e); }
4017 void visit_post_crement (post_crement* e) { provide (e); }
4018 void visit_assignment (assignment* e) { provide (e); }
4019
4020 private:
4021 void reduce_target_symbol (target_symbol* e, expression* operand=NULL);
4022 };
4023
4024
4025 void
4026 void_statement_reducer::visit_expr_statement (expr_statement* s)
4027 {
4028 replace (s->value, true);
4029
4030 // if the expression provides 0, that's our signal that a new
4031 // statement has been provided, so we shouldn't provide this one.
4032 if (s->value != 0)
4033 provide(s);
4034 }
4035
4036 void
4037 void_statement_reducer::visit_if_statement (if_statement* s)
4038 {
4039 // s->condition is never void
4040 replace (s->thenblock);
4041 replace (s->elseblock);
4042 provide (s);
4043 }
4044
4045 void
4046 void_statement_reducer::visit_for_loop (for_loop* s)
4047 {
4048 // s->init/cond/incr are never void
4049 replace (s->block);
4050 provide (s);
4051 }
4052
4053 void
4054 void_statement_reducer::visit_foreach_loop (foreach_loop* s)
4055 {
4056 // s->indexes/base/value/limit are never void
4057 replace (s->block);
4058 provide (s);
4059 }
4060
4061 void
4062 void_statement_reducer::visit_logical_or_expr (logical_or_expr* e)
4063 {
4064 // In void context, the evaluation of "a || b" is exactly like
4065 // "if (!a) b", so let's do that instead.
4066
4067 if (session.verbose>2)
4068 clog << _("Creating if statement from unused logical-or ")
4069 << *e->tok << endl;
4070
4071 if_statement *is = new if_statement;
4072 is->tok = e->tok;
4073 is->elseblock = 0;
4074
4075 unary_expression *ue = new unary_expression;
4076 ue->operand = e->left;
4077 ue->tok = e->tok;
4078 ue->op = "!";
4079 is->condition = ue;
4080
4081 expr_statement *es = new expr_statement;
4082 es->value = e->right;
4083 es->tok = es->value->tok;
4084 is->thenblock = es;
4085
4086 is->visit(this);
4087 relaxed_p = false;
4088 e = 0;
4089 provide (e);
4090 }
4091
4092 void
4093 void_statement_reducer::visit_logical_and_expr (logical_and_expr* e)
4094 {
4095 // In void context, the evaluation of "a && b" is exactly like
4096 // "if (a) b", so let's do that instead.
4097
4098 if (session.verbose>2)
4099 clog << _("Creating if statement from unused logical-and ")
4100 << *e->tok << endl;
4101
4102 if_statement *is = new if_statement;
4103 is->tok = e->tok;
4104 is->elseblock = 0;
4105 is->condition = e->left;
4106
4107 expr_statement *es = new expr_statement;
4108 es->value = e->right;
4109 es->tok = es->value->tok;
4110 is->thenblock = es;
4111
4112 is->visit(this);
4113 relaxed_p = false;
4114 e = 0;
4115 provide (e);
4116 }
4117
4118 void
4119 void_statement_reducer::visit_ternary_expression (ternary_expression* e)
4120 {
4121 // In void context, the evaluation of "a ? b : c" is exactly like
4122 // "if (a) b else c", so let's do that instead.
4123
4124 if (session.verbose>2)
4125 clog << _("Creating if statement from unused ternary expression ")
4126 << *e->tok << endl;
4127
4128 if_statement *is = new if_statement;
4129 is->tok = e->tok;
4130 is->condition = e->cond;
4131
4132 expr_statement *es = new expr_statement;
4133 es->value = e->truevalue;
4134 es->tok = es->value->tok;
4135 is->thenblock = es;
4136
4137 es = new expr_statement;
4138 es->value = e->falsevalue;
4139 es->tok = es->value->tok;
4140 is->elseblock = es;
4141
4142 is->visit(this);
4143 relaxed_p = false;
4144 e = 0;
4145 provide (e);
4146 }
4147
4148 void
4149 void_statement_reducer::visit_binary_expression (binary_expression* e)
4150 {
4151 // When the result of a binary operation isn't needed, it's just as good to
4152 // evaluate the operands as sequential statements in a block.
4153
4154 if (session.verbose>2)
4155 clog << _("Eliding unused binary ") << *e->tok << endl;
4156
4157 block *b = new block;
4158 b->tok = e->tok;
4159
4160 expr_statement *es = new expr_statement;
4161 es->value = e->left;
4162 es->tok = es->value->tok;
4163 b->statements.push_back(es);
4164
4165 es = new expr_statement;
4166 es->value = e->right;
4167 es->tok = es->value->tok;
4168 b->statements.push_back(es);
4169
4170 b->visit(this);
4171 relaxed_p = false;
4172 e = 0;
4173 provide (e);
4174 }
4175
4176 void
4177 void_statement_reducer::visit_unary_expression (unary_expression* e)
4178 {
4179 // When the result of a unary operation isn't needed, it's just as good to
4180 // evaluate the operand directly
4181
4182 if (session.verbose>2)
4183 clog << _("Eliding unused unary ") << *e->tok << endl;
4184
4185 relaxed_p = false;
4186 e->operand->visit(this);
4187 }
4188
4189 void
4190 void_statement_reducer::visit_regex_query (regex_query* e)
4191 {
4192 // TODOXXX After subexpression extraction is implemented,
4193 // regular expression matches *may* have side-effects in
4194 // terms of producing matched subexpressions, e.g.:
4195 //
4196 // str =~ "pat"; println(matched(0));
4197 //
4198 // It's debatable if we want to actually allow this, though.
4199
4200 // Treat e as a unary expression on the left operand -- since the
4201 // right hand side must be a literal (as verified by the parser),
4202 // evaluating it never has side effects.
4203
4204 if (session.verbose>2)
4205 clog << _("Eliding regex query ") << *e->tok << endl;
4206
4207 relaxed_p = false;
4208 e->left->visit(this);
4209 }
4210
4211 void
4212 void_statement_reducer::visit_comparison (comparison* e)
4213 {
4214 visit_binary_expression(e);
4215 }
4216
4217 void
4218 void_statement_reducer::visit_concatenation (concatenation* e)
4219 {
4220 visit_binary_expression(e);
4221 }
4222
4223 void
4224 void_statement_reducer::visit_functioncall (functioncall* e)
4225 {
4226 // If a function call is pure and its result ignored, we can elide the call
4227 // and just evaluate the arguments in sequence
4228
4229 if (e->args.empty())
4230 {
4231 provide (e);
4232 return;
4233 }
4234
4235 bool side_effect_free = true;
4236 for (unsigned i = 0; i < e->referents.size(); i++)
4237 {
4238 varuse_collecting_visitor vut(session);
4239 vut.seen.insert (e->referents[i]);
4240 vut.current_function = e->referents[i];
4241 e->referents[i]->body->visit (& vut);
4242 if (!vut.side_effect_free_wrt(focal_vars))
4243 {
4244 side_effect_free = false;
4245 break;
4246 }
4247 }
4248
4249 if (!side_effect_free)
4250 {
4251 provide (e);
4252 return;
4253 }
4254
4255 if (session.verbose>2)
4256 clog << _("Eliding side-effect-free function call ") << *e->tok << endl;
4257
4258 block *b = new block;
4259 b->tok = e->tok;
4260
4261 for (unsigned i=0; i<e->args.size(); i++ )
4262 {
4263 expr_statement *es = new expr_statement;
4264 es->value = e->args[i];
4265 es->tok = es->value->tok;
4266 b->statements.push_back(es);
4267 }
4268
4269 b->visit(this);
4270 relaxed_p = false;
4271 e = 0;
4272 provide (e);
4273 }
4274
4275 void
4276 void_statement_reducer::visit_print_format (print_format* e)
4277 {
4278 // When an sprint's return value is ignored, we can simply evaluate the
4279 // arguments in sequence
4280
4281 if (e->print_to_stream || !e->args.size())
4282 {
4283 provide (e);
4284 return;
4285 }
4286
4287 if (session.verbose>2)
4288 clog << _("Eliding unused print ") << *e->tok << endl;
4289
4290 block *b = new block;
4291 b->tok = e->tok;
4292
4293 for (unsigned i=0; i<e->args.size(); i++ )
4294 {
4295 expr_statement *es = new expr_statement;
4296 es->value = e->args[i];
4297 es->tok = es->value->tok;
4298 b->statements.push_back(es);
4299 }
4300
4301 b->visit(this);
4302 relaxed_p = false;
4303 e = 0;
4304 provide (e);
4305 }
4306
4307 void
4308 void_statement_reducer::reduce_target_symbol (target_symbol* e,
4309 expression* operand)
4310 {
4311 // When the result of any target_symbol isn't needed, it's just as good to
4312 // evaluate the operand and any array indexes directly
4313
4314 block *b = new block;
4315 b->tok = e->tok;
4316
4317 if (operand)
4318 {
4319 expr_statement *es = new expr_statement;
4320 es->value = operand;
4321 es->tok = es->value->tok;
4322 b->statements.push_back(es);
4323 }
4324
4325 for (unsigned i=0; i<e->components.size(); i++ )
4326 {
4327 if (e->components[i].type != target_symbol::comp_expression_array_index)
4328 continue;
4329
4330 expr_statement *es = new expr_statement;
4331 es->value = e->components[i].expr_index;
4332 es->tok = es->value->tok;
4333 b->statements.push_back(es);
4334 }
4335
4336 b->visit(this);
4337 relaxed_p = false;
4338 e = 0;
4339 provide (e);
4340 }
4341
4342 void
4343 void_statement_reducer::visit_atvar_op (atvar_op* e)
4344 {
4345 if (session.verbose>2)
4346 clog << _("Eliding unused target symbol ") << *e->tok << endl;
4347 reduce_target_symbol (e);
4348 }
4349
4350 void
4351 void_statement_reducer::visit_target_symbol (target_symbol* e)
4352 {
4353 if (session.verbose>2)
4354 clog << _("Eliding unused target symbol ") << *e->tok << endl;
4355 reduce_target_symbol (e);
4356 }
4357
4358 void
4359 void_statement_reducer::visit_cast_op (cast_op* e)
4360 {
4361 if (session.verbose>2)
4362 clog << _("Eliding unused typecast ") << *e->tok << endl;
4363 reduce_target_symbol (e, e->operand);
4364 }
4365
4366 void
4367 void_statement_reducer::visit_autocast_op (autocast_op* e)
4368 {
4369 if (session.verbose>2)
4370 clog << _("Eliding unused autocast ") << *e->tok << endl;
4371 reduce_target_symbol (e, e->operand);
4372 }
4373
4374
4375 void
4376 void_statement_reducer::visit_defined_op (defined_op* e)
4377 {
4378 // When the result of a @defined operation isn't needed, just elide
4379 // it entirely. Its operand $expression must already be
4380 // side-effect-free.
4381
4382 if (session.verbose>2)
4383 clog << _("Eliding unused check ") << *e->tok << endl;
4384
4385 relaxed_p = false;
4386 e = 0;
4387 provide (e);
4388 }
4389
4390 void semantic_pass_opt5 (systemtap_session& s, bool& relaxed_p)
4391 {
4392 // Let's simplify statements with unused computed values.
4393
4394 void_statement_reducer vuv (s, relaxed_p);
4395 // This instance may be reused for multiple probe/function body trims.
4396
4397 vuv.focal_vars.insert (s.globals.begin(), s.globals.end());
4398
4399 for (unsigned i=0; i<s.probes.size(); i++)
4400 vuv.replace (s.probes[i]->body);
4401 for (map<string,functiondecl*>::iterator it = s.functions.begin();
4402 it != s.functions.end(); it++)
4403 vuv.replace (it->second->body);
4404 }
4405
4406
4407
4408 void
4409 const_folder::get_literal(expression*& e,
4410 literal_number*& n,
4411 literal_string*& s)
4412 {
4413 replace (e);
4414 n = (e == last_number) ? last_number : NULL;
4415 s = (e == last_string) ? last_string : NULL;
4416 }
4417
4418 literal_number*
4419 const_folder::get_number(expression*& e)
4420 {
4421 replace (e);
4422 return (e == last_number) ? last_number : NULL;
4423 }
4424
4425 void
4426 const_folder::visit_literal_number (literal_number* e)
4427 {
4428 last_number = e;
4429 provide (e);
4430 }
4431
4432 literal_string*
4433 const_folder::get_string(expression*& e)
4434 {
4435 replace (e);
4436 return (e == last_string) ? last_string : NULL;
4437 }
4438
4439 void
4440 const_folder::visit_literal_string (literal_string* e)
4441 {
4442 last_string = e;
4443 provide (e);
4444 }
4445
4446 void
4447 const_folder::visit_if_statement (if_statement* s)
4448 {
4449 literal_number* cond = get_number (s->condition);
4450 if (!cond)
4451 {
4452 replace (s->thenblock);
4453 replace (s->elseblock);
4454 provide (s);
4455 }
4456 else
4457 {
4458 if (session.verbose>2)
4459 clog << _F("Collapsing constant-%" PRIi64 " if-statement %s",
4460 cond->value, lex_cast(*s->tok).c_str()) << endl;
4461 relaxed_p = false;
4462
4463 statement* n = cond->value ? s->thenblock : s->elseblock;
4464 if (n)
4465 n->visit (this);
4466 else
4467 provide (new null_statement (s->tok));
4468 }
4469 }
4470
4471 void
4472 const_folder::visit_for_loop (for_loop* s)
4473 {
4474 literal_number* cond = get_number (s->cond);
4475 if (!cond || cond->value)
4476 {
4477 replace (s->init);
4478 replace (s->incr);
4479 replace (s->block);
4480 provide (s);
4481 }
4482 else
4483 {
4484 if (session.verbose>2)
4485 clog << _("Collapsing constantly-false for-loop ") << *s->tok << endl;
4486 relaxed_p = false;
4487
4488 if (s->init)
4489 s->init->visit (this);
4490 else
4491 provide (new null_statement (s->tok));
4492 }
4493 }
4494
4495 void
4496 const_folder::visit_foreach_loop (foreach_loop* s)
4497 {
4498 literal_number* limit = get_number (s->limit);
4499 if (!limit || limit->value > 0)
4500 {
4501 for (unsigned i = 0; i < s->indexes.size(); ++i)
4502 replace (s->indexes[i]);
4503 replace (s->base);
4504 replace (s->value);
4505 replace (s->block);
4506 provide (s);
4507 }
4508 else
4509 {
4510 if (session.verbose>2)
4511 clog << _("Collapsing constantly-limited foreach-loop ") << *s->tok << endl;
4512 relaxed_p = false;
4513
4514 provide (new null_statement (s->tok));
4515 }
4516 }
4517
4518 void
4519 const_folder::visit_binary_expression (binary_expression* e)
4520 {
4521 int64_t value;
4522 literal_number* left = get_number (e->left);
4523 literal_number* right = get_number (e->right);
4524
4525 if (right && !right->value && (e->op == "/" || e->op == "%"))
4526 {
4527 // Give divide-by-zero a chance to be optimized out elsewhere,
4528 // and if not it will be a runtime error anyway...
4529 provide (e);
4530 return;
4531 }
4532
4533 if (left && right)
4534 {
4535 if (e->op == "+")
4536 value = left->value + right->value;
4537 else if (e->op == "-")
4538 value = left->value - right->value;
4539 else if (e->op == "*")
4540 value = left->value * right->value;
4541 else if (e->op == "&")
4542 value = left->value & right->value;
4543 else if (e->op == "|")
4544 value = left->value | right->value;
4545 else if (e->op == "^")
4546 value = left->value ^ right->value;
4547 else if (e->op == ">>")
4548 value = left->value >> max(min(right->value, (int64_t)64), (int64_t)0);
4549 else if (e->op == "<<")
4550 value = left->value << max(min(right->value, (int64_t)64), (int64_t)0);
4551 else if (e->op == "/")
4552 value = (left->value == LLONG_MIN && right->value == -1) ? LLONG_MIN :
4553 left->value / right->value;
4554 else if (e->op == "%")
4555 value = (left->value == LLONG_MIN && right->value == -1) ? 0 :
4556 left->value % right->value;
4557 else
4558 throw SEMANTIC_ERROR (_("unsupported binary operator ") + (string)e->op);
4559 }
4560
4561 else if ((left && ((left->value == 0 && (e->op == "*" || e->op == "&" ||
4562 e->op == ">>" || e->op == "<<" )) ||
4563 (left->value ==-1 && (e->op == "|" || e->op == ">>"))))
4564 ||
4565 (right && ((right->value == 0 && (e->op == "*" || e->op == "&")) ||
4566 (right->value == 1 && (e->op == "%")) ||
4567 (right->value ==-1 && (e->op == "%" || e->op == "|")))))
4568 {
4569 expression* other = left ? e->right : e->left;
4570 varuse_collecting_visitor vu(session);
4571 other->visit(&vu);
4572 if (!vu.side_effect_free())
4573 {
4574 provide (e);
4575 return;
4576 }
4577
4578 // we'll pass on type=pe_long inference to the expression
4579 if (other->type == pe_unknown)
4580 other->type = pe_long;
4581 else if (other->type != pe_long)
4582 {
4583 // this mismatch was not caught in the initial type resolution pass,
4584 // generate a mismatch (left doesn't match right) error
4585 typeresolution_info ti(session);
4586 ti.assert_resolvability = true; // need this to get it throw errors
4587 ti.mismatch_complexity = 1; // also needed to throw errors
4588 ti.mismatch(e);
4589 }
4590
4591 if (left)
4592 value = left->value;
4593 else if (e->op == "%")
4594 value = 0;
4595 else
4596 value = right->value;
4597 }
4598
4599 else if ((left && ((left->value == 0 && (e->op == "+" || e->op == "|" ||
4600 e->op == "^")) ||
4601 (left->value == 1 && (e->op == "*")) ||
4602 (left->value ==-1 && (e->op == "&"))))
4603 ||
4604 (right && ((right->value == 0 && (e->op == "+" || e->op == "-" ||
4605 e->op == "|" || e->op == "^")) ||
4606 (right->value == 1 && (e->op == "*" || e->op == "/")) ||
4607 (right->value ==-1 && (e->op == "&")) ||
4608 (right->value <= 0 && (e->op == ">>" || e->op == "<<")))))
4609 {
4610 if (session.verbose>2)
4611 clog << _("Collapsing constant-identity binary operator ") << *e->tok << endl;
4612 relaxed_p = false;
4613
4614 // we'll pass on type=pe_long inference to the expression
4615 expression* other = left ? e->right : e->left;
4616 if (other->type == pe_unknown)
4617 other->type = pe_long;
4618 else if (other->type != pe_long)
4619 {
4620 // this mismatch was not caught in the initial type resolution pass,
4621 // generate a mismatch (left doesn't match right) error
4622 typeresolution_info ti(session);
4623 ti.assert_resolvability = true; // need this to get it throw errors
4624 ti.mismatch_complexity = 1; // also needed to throw errors
4625 ti.mismatch(e);
4626 }
4627
4628 provide (other);
4629 return;
4630 }
4631
4632 else
4633 {
4634 provide (e);
4635 return;
4636 }
4637
4638 if (session.verbose>2)
4639 clog << _F("Collapsing constant-%" PRIi64 " binary operator %s",
4640 value, lex_cast(*e->tok).c_str()) << endl;
4641 relaxed_p = false;
4642
4643 literal_number* n = new literal_number(value);
4644 n->tok = e->tok;
4645 n->visit (this);
4646 }
4647
4648 void
4649 const_folder::visit_unary_expression (unary_expression* e)
4650 {
4651 literal_number* operand = get_number (e->operand);
4652 if (!operand)
4653 provide (e);
4654 else
4655 {
4656 if (session.verbose>2)
4657 clog << _("Collapsing constant unary ") << *e->tok << endl;
4658 relaxed_p = false;
4659
4660 literal_number* n = new literal_number (*operand);
4661 n->tok = e->tok;
4662 if (e->op == "+")
4663 ; // nothing to do
4664 else if (e->op == "-")
4665 n->value = -n->value;
4666 else if (e->op == "!")
4667 n->value = !n->value;
4668 else if (e->op == "~")
4669 n->value = ~n->value;
4670 else
4671 throw SEMANTIC_ERROR (_("unsupported unary operator ") + (string)e->op);
4672 n->visit (this);
4673 }
4674 }
4675
4676 void
4677 const_folder::visit_logical_or_expr (logical_or_expr* e)
4678 {
4679 int64_t value;
4680 literal_number* left = get_number (e->left);
4681 literal_number* right = get_number (e->right);
4682
4683 if (left && right)
4684 value = left->value || right->value;
4685
4686 else if ((left && left->value) || (right && right->value))
4687 {
4688 // If the const is on the left, we get to short-circuit the right
4689 // immediately. Otherwise, we can only eliminate the LHS if it's pure.
4690 if (right)
4691 {
4692 varuse_collecting_visitor vu(session);
4693 e->left->visit(&vu);
4694 if (!vu.side_effect_free())
4695 {
4696 provide (e);
4697 return;
4698 }
4699 }
4700
4701 value = 1;
4702 }
4703
4704 // We might also get rid of useless "0||x" and "x||0", except it does
4705 // normalize x to 0 or 1. We could change it to "!!x", but it's not clear
4706 // that this would gain us much.
4707
4708 else
4709 {
4710 provide (e);
4711 return;
4712 }
4713
4714 if (session.verbose>2)
4715 clog << _("Collapsing constant logical-OR ") << *e->tok << endl;
4716 relaxed_p = false;
4717
4718 literal_number* n = new literal_number(value);
4719 n->tok = e->tok;
4720 n->visit (this);
4721 }
4722
4723 void
4724 const_folder::visit_logical_and_expr (logical_and_expr* e)
4725 {
4726 int64_t value;
4727 literal_number* left = get_number (e->left);
4728 literal_number* right = get_number (e->right);
4729
4730 if (left && right)
4731 value = left->value && right->value;
4732
4733 else if ((left && !left->value) || (right && !right->value))
4734 {
4735 // If the const is on the left, we get to short-circuit the right
4736 // immediately. Otherwise, we can only eliminate the LHS if it's pure.
4737 if (right)
4738 {
4739 varuse_collecting_visitor vu(session);
4740 e->left->visit(&vu);
4741 if (!vu.side_effect_free())
4742 {
4743 provide (e);
4744 return;
4745 }
4746 }
4747
4748 value = 0;
4749 }
4750
4751 // We might also get rid of useless "1&&x" and "x&&1", except it does
4752 // normalize x to 0 or 1. We could change it to "!!x", but it's not clear
4753 // that this would gain us much.
4754
4755 else
4756 {
4757 provide (e);
4758 return;
4759 }
4760
4761 if (session.verbose>2)
4762 clog << _("Collapsing constant logical-AND ") << *e->tok << endl;
4763 relaxed_p = false;
4764
4765 literal_number* n = new literal_number(value);
4766 n->tok = e->tok;
4767 n->visit (this);
4768 }
4769
4770 void
4771 const_folder::visit_compound_expression (compound_expression* e)
4772 {
4773 replace(e->left, true);
4774 replace(e->right, false);
4775
4776 // If the LHS is pure, we can eliminate it.
4777 // ??? This is unlikely, given how these are created in loc2stap.cxx.
4778 if (e->left)
4779 {
4780 varuse_collecting_visitor vu(session);
4781 e->left->visit(&vu);
4782 if (!vu.side_effect_free())
4783 {
4784 provide(e);
4785 return;
4786 }
4787 }
4788
4789 if (session.verbose > 2)
4790 clog << _("Collapsing compound expression") << *e->tok << endl;
4791
4792 provide(e->right);
4793 }
4794
4795 void
4796 const_folder::visit_comparison (comparison* e)
4797 {
4798 int comp;
4799
4800 literal_number *left_num, *right_num;
4801 literal_string *left_str, *right_str;
4802 get_literal(e->left, left_num, left_str);
4803 get_literal(e->right, right_num, right_str);
4804
4805 if (left_str && right_str)
4806 comp = left_str->value.compare(right_str->value);
4807
4808 else if (left_num && right_num)
4809 comp = left_num->value < right_num->value ? -1 :
4810 left_num->value > right_num->value ? 1 : 0;
4811
4812 else if ((left_num && ((left_num->value == LLONG_MIN &&
4813 (e->op == "<=" || e->op == ">")) ||
4814 (left_num->value == LLONG_MAX &&
4815 (e->op == ">=" || e->op == "<"))))
4816 ||
4817 (right_num && ((right_num->value == LLONG_MIN &&
4818 (e->op == ">=" || e->op == "<")) ||
4819 (right_num->value == LLONG_MAX &&
4820 (e->op == "<=" || e->op == ">")))))
4821 {
4822 expression* other = left_num ? e->right : e->left;
4823 varuse_collecting_visitor vu(session);
4824 other->visit(&vu);
4825 if (!vu.side_effect_free())
4826 provide (e);
4827 else
4828 {
4829 if (session.verbose>2)
4830 clog << _("Collapsing constant-boundary comparison ") << *e->tok << endl;
4831 relaxed_p = false;
4832
4833 // ops <= and >= are true, < and > are false
4834 literal_number* n = new literal_number( e->op.length() == 2 );
4835 n->tok = e->tok;
4836 n->visit (this);
4837 }
4838 return;
4839 }
4840
4841 else
4842 {
4843 provide (e);
4844 return;
4845 }
4846
4847 if (session.verbose>2)
4848 clog << _("Collapsing constant comparison ") << *e->tok << endl;
4849 relaxed_p = false;
4850
4851 int64_t value;
4852 if (e->op == "==")
4853 value = comp == 0;
4854 else if (e->op == "!=")
4855 value = comp != 0;
4856 else if (e->op == "<")
4857 value = comp < 0;
4858 else if (e->op == ">")
4859 value = comp > 0;
4860 else if (e->op == "<=")
4861 value = comp <= 0;
4862 else if (e->op == ">=")
4863 value = comp >= 0;
4864 else
4865 throw SEMANTIC_ERROR (_("unsupported comparison operator ") + (string)e->op);
4866
4867 literal_number* n = new literal_number(value);
4868 n->tok = e->tok;
4869 n->visit (this);
4870 }
4871
4872 void
4873 const_folder::visit_concatenation (concatenation* e)
4874 {
4875 literal_string* left = get_string (e->left);
4876 literal_string* right = get_string (e->right);
4877
4878 if (left && right)
4879 {
4880 if (session.verbose>2)
4881 clog << _("Collapsing constant concatenation ") << *e->tok << endl;
4882 relaxed_p = false;
4883
4884 literal_string* n = new literal_string (*left);
4885 n->tok = e->tok;
4886 n->value = (string)n->value + (string)right->value;
4887 n->visit (this);
4888 }
4889 else if ((left && left->value.empty()) ||
4890 (right && right->value.empty()))
4891 {
4892 if (session.verbose>2)
4893 clog << _("Collapsing identity concatenation ") << *e->tok << endl;
4894 relaxed_p = false;
4895 provide(left ? e->right : e->left);
4896 }
4897 else
4898 provide (e);
4899 }
4900
4901 void
4902 const_folder::visit_ternary_expression (ternary_expression* e)
4903 {
4904 literal_number* cond = get_number (e->cond);
4905 if (!cond)
4906 {
4907 replace (e->truevalue);
4908 replace (e->falsevalue);
4909 provide (e);
4910 }
4911 else
4912 {
4913 if (session.verbose>2)
4914 clog << _F("Collapsing constant-%" PRIi64 " ternary %s",
4915 cond->value, lex_cast(*e->tok).c_str()) << endl;
4916 relaxed_p = false;
4917
4918 expression* n = cond->value ? e->truevalue : e->falsevalue;
4919 n->visit (this);
4920 }
4921 }
4922
4923 void
4924 const_folder::visit_defined_op (defined_op* e)
4925 {
4926 // If a @defined makes it this far, then it was not resolved by
4927 // previous efforts. We could assume that therefore it is a big fat
4928 // zero, but for the @defined(autocast) case PR18079, this just
4929 // means that we didn't know yet.
4930 int64_t value = 0;
4931 bool collapse_this = false;
4932
4933 /* PR20672: not true; we run a const_folder iteratively during
4934 initial probe body variable-expansion, when other @defined()s may
4935 be as-yet-unprocessed. We can't presume to map them to zero.
4936
4937 // We do know that plain target_symbols aren't going anywhere though.
4938 if (get_target_symbol (e->operand))
4939 {
4940 if (session.verbose>2)
4941 clog << _("Collapsing target_symbol @defined check ") << *e->tok << endl;
4942 collapse_this = true;
4943 }
4944 else
4945 */
4946 if (collapse_defines_p && relaxed_p)
4947 {
4948 if (session.verbose>2)
4949 clog << _("Collapsing untouched @defined check ") << *e->tok << endl;
4950
4951 // If we got to an expression with a known type, call it defined.
4952 if (e->operand->type != pe_unknown)
4953 value = 1;
4954 collapse_this = true;
4955 }
4956
4957 if (collapse_this)
4958 {
4959 // Don't be greedy... we'll only collapse one at a time so type
4960 // resolution can have another go at it.
4961 relaxed_p = false;
4962 literal_number* n = new literal_number (value);
4963 n->tok = e->tok;
4964 n->visit (this);
4965 }
4966 else
4967 {
4968 if (session.verbose>2)
4969 clog << _("Preserving unresolved @defined check ") << *e->tok << endl;
4970 provide (e);
4971 }
4972 }
4973
4974 target_symbol*
4975 const_folder::get_target_symbol(expression*& e)
4976 {
4977 replace (e);
4978 return (e == last_target_symbol) ? last_target_symbol : NULL;
4979 }
4980
4981 void
4982 const_folder::visit_target_symbol (target_symbol* e)
4983 {
4984 if (collapse_defines_p && session.skip_badvars)
4985 {
4986 // Upon user request for ignoring context, the symbol is replaced
4987 // with a literal 0 and a warning message displayed
4988 // ... but don't do this during early runs of the const_folder, only
4989 // during the final (collapse_defines_p) one. (Otherwise, during
4990 // a dwarf_var "@defined($foo) ? $foo : 0", the inner $foo could
4991 // get premature mapping to 0.
4992 //
4993 // XXX this ignores possible side-effects, e.g. in array indexes
4994 literal_number* ln_zero = new literal_number (0);
4995 ln_zero->tok = e->tok;
4996 provide (ln_zero);
4997 session.print_warning (_("Bad $context variable being substituted with literal 0"),
4998 e->tok);
4999 relaxed_p = false;
5000 }
5001 else
5002 {
5003 update_visitor::visit_target_symbol (e);
5004 last_target_symbol = e;
5005 }
5006 }
5007
5008 static int initial_typeres_pass(systemtap_session& s);
5009 static int semantic_pass_const_fold (systemtap_session& s, bool& relaxed_p)
5010 {
5011 // attempt an initial type resolution pass to see if there are any type
5012 // mismatches before we starting whisking away vars that get switched out
5013 // with a const.
5014
5015 // return if the initial type resolution pass reported errors (type mismatches)
5016 int rc = initial_typeres_pass(s);
5017 if (rc)
5018 {
5019 relaxed_p = true;
5020 return rc;
5021 }
5022
5023 // Let's simplify statements with constant values.
5024 const_folder cf (s, relaxed_p, true /* collapse remaining @defined()->0 now */ );
5025 // This instance may be reused for multiple probe/function body trims.
5026
5027 for (unsigned i=0; i<s.probes.size(); i++)
5028 cf.replace (s.probes[i]->body);
5029 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5030 it != s.functions.end(); it++)
5031 cf.replace (it->second->body);
5032 return 0;
5033 }
5034
5035
5036 struct dead_control_remover: public traversing_visitor
5037 {
5038 systemtap_session& session;
5039 bool& relaxed_p;
5040 statement* control;
5041
5042 dead_control_remover(systemtap_session& s, bool& r):
5043 session(s), relaxed_p(r), control(NULL) {}
5044
5045 void visit_block (block *b);
5046
5047 // When a block contains any of these, the following statements are dead.
5048 void visit_return_statement (return_statement* s) { control = s; }
5049 void visit_next_statement (next_statement* s) { control = s; }
5050 void visit_break_statement (break_statement* s) { control = s; }
5051 void visit_continue_statement (continue_statement* s) { control = s; }
5052 };
5053
5054
5055 void dead_control_remover::visit_block (block* b)
5056 {
5057 vector<statement*>& vs = b->statements;
5058 if (vs.size() == 0) /* else (size_t) size()-1 => very big */
5059 return;
5060 for (size_t i = 0; i < vs.size() - 1; ++i)
5061 {
5062 vs[i]->visit (this);
5063 if (vs[i] == control)
5064 {
5065 session.print_warning(_("statement will never be reached"),
5066 vs[i + 1]->tok);
5067 vs.erase(vs.begin() + i + 1, vs.end());
5068 relaxed_p = false;
5069 break;
5070 }
5071 }
5072 }
5073
5074
5075 static void semantic_pass_dead_control (systemtap_session& s, bool& relaxed_p)
5076 {
5077 // Let's remove code that follow unconditional control statements
5078
5079 dead_control_remover dc (s, relaxed_p);
5080
5081 for (unsigned i=0; i<s.probes.size(); i++)
5082 s.probes[i]->body->visit(&dc);
5083
5084 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5085 it != s.functions.end(); it++)
5086 it->second->body->visit(&dc);
5087 }
5088
5089
5090 // Looks for next statements in function declarations and marks
5091 // them.
5092 struct function_next_check : public traversing_visitor
5093 {
5094 functiondecl* current_function;
5095
5096 function_next_check()
5097 : current_function(0) { }
5098
5099 void visit_next_statement(next_statement*)
5100 {
5101 current_function->has_next = true;
5102 }
5103
5104 void visit_embeddedcode(embeddedcode* s)
5105 {
5106 if (s->tagged_p("STAP_NEXT;"))
5107 current_function->has_next = true;
5108 }
5109 };
5110
5111 struct dead_overload_remover : public traversing_visitor
5112 {
5113 systemtap_session& s;
5114 bool& relaxed_p;
5115
5116 dead_overload_remover(systemtap_session& sess,
5117 bool& r)
5118 : s(sess), relaxed_p(r) { }
5119
5120 void visit_functioncall(functioncall* e);
5121 };
5122
5123 void dead_overload_remover::visit_functioncall(functioncall *e)
5124 {
5125 unsigned reachable = 1;
5126 bool chained = true;
5127
5128 for (unsigned fd = 0; fd < e->referents.size(); fd++)
5129 {
5130 functiondecl* r = e->referents[fd];
5131
5132 // Note that this is not a sound inference but it suffices for most
5133 // cases. It may be the case that there is a 'next' statement in the
5134 // function that will never be executed by the control flow.
5135 // We simply use the presence of a 'next' statement as an indicator
5136 // of a potential fall through. Once a function can't be 'nexted' the
5137 // remaining functions are unreachable.
5138 if (chained && r->has_next)
5139 reachable++;
5140 else
5141 chained = false;
5142 }
5143
5144 if (reachable < e->referents.size())
5145 {
5146 for (unsigned fd = reachable; fd < e->referents.size(); fd++)
5147 {
5148 functiondecl* r = e->referents[fd];
5149 s.print_warning(_("instance of overloaded function will "
5150 "never be reached"), r->tok);
5151 }
5152 e->referents.erase(e->referents.begin()+reachable, e->referents.end());
5153 relaxed_p = false;
5154 }
5155 }
5156
5157 static void semantic_pass_overload(systemtap_session& s, bool& relaxed_p)
5158 {
5159 set<functiondecl*> function_next;
5160 function_next_check fnc;
5161
5162 for (auto it = s.functions.begin(); it != s.functions.end(); ++it)
5163 {
5164 functiondecl* fn = it->second;
5165 fnc.current_function = fn;
5166 fn->body->visit(&fnc);
5167 }
5168
5169 for (auto it = s.probes.begin(); it != s.probes.end(); ++it)
5170 {
5171 dead_overload_remover ovr(s, relaxed_p);
5172 (*it)->body->visit(&ovr);
5173 }
5174
5175 for (auto it = s.functions.begin(); it != s.functions.end(); ++it)
5176 {
5177 dead_overload_remover ovr(s, relaxed_p);
5178 it->second->body->visit(&ovr);
5179 }
5180 }
5181
5182
5183 struct duplicate_function_remover: public functioncall_traversing_visitor
5184 {
5185 systemtap_session& s;
5186 map<functiondecl*, functiondecl*>& duplicate_function_map;
5187
5188 duplicate_function_remover(systemtap_session& sess,
5189 map<functiondecl*, functiondecl*>&dfm):
5190 s(sess), duplicate_function_map(dfm) {};
5191
5192 void visit_functioncall (functioncall* e);
5193 };
5194
5195 void
5196 duplicate_function_remover::visit_functioncall (functioncall *e)
5197 {
5198 functioncall_traversing_visitor::visit_functioncall (e);
5199
5200 // If any of the current function call references points to a function that
5201 // is a duplicate, replace it.
5202 for (unsigned i = 0; i < e->referents.size(); i++)
5203 {
5204 functiondecl* referent = e->referents[i];
5205 if (duplicate_function_map.count(referent) != 0)
5206 {
5207 if (s.verbose>2)
5208 clog << _F("Changing %s reference to %s reference\n",
5209 referent->unmangled_name.to_string().c_str(),
5210 duplicate_function_map[referent]->unmangled_name.to_string().c_str());
5211 e->tok = duplicate_function_map[referent]->tok;
5212 e->function = duplicate_function_map[referent]->name;
5213 e->referents[i] = duplicate_function_map[referent];
5214 }
5215 }
5216 }
5217
5218 static string
5219 get_functionsig (functiondecl* f)
5220 {
5221 ostringstream s;
5222
5223 // Get the "name:args body" of the function in s. We have to
5224 // include the args since the function 'x1(a, b)' is different than
5225 // the function 'x2(b, a)' even if the bodies of the two functions
5226 // are exactly the same.
5227 f->printsig(s);
5228 f->body->print(s);
5229
5230 // printsig puts f->name + ':' on the front. Remove this
5231 // (otherwise, functions would never compare equal).
5232 string str = s.str().erase(0, f->unmangled_name.size() + 1);
5233
5234 // Return the function signature.
5235 return str;
5236 }
5237
5238 void semantic_pass_opt6 (systemtap_session& s, bool& relaxed_p)
5239 {
5240 // Walk through all the functions, looking for duplicates.
5241 map<string, functiondecl*> functionsig_map;
5242 map<functiondecl*, functiondecl*> duplicate_function_map;
5243
5244
5245 vector<functiondecl*> newly_zapped_functions;
5246 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
5247 {
5248 functiondecl *fd = it->second;
5249 string functionsig = get_functionsig(fd);
5250
5251 if (functionsig_map.count(functionsig) == 0)
5252 {
5253 // This function is unique. Remember it.
5254 functionsig_map[functionsig] = fd;
5255 }
5256 else
5257 {
5258 // This function is a duplicate.
5259 duplicate_function_map[fd] = functionsig_map[functionsig];
5260 newly_zapped_functions.push_back (fd);
5261 relaxed_p = false;
5262 }
5263 }
5264 for (unsigned i=0; i<newly_zapped_functions.size(); i++)
5265 {
5266 map<string,functiondecl*>::iterator where = s.functions.find (newly_zapped_functions[i]->name);
5267 assert (where != s.functions.end());
5268 s.functions.erase (where);
5269 }
5270
5271
5272 // If we have duplicate functions, traverse down the tree, replacing
5273 // the appropriate function calls.
5274 // duplicate_function_remover::visit_functioncall() handles the
5275 // details of replacing the function calls.
5276 if (duplicate_function_map.size() != 0)
5277 {
5278 duplicate_function_remover dfr (s, duplicate_function_map);
5279
5280 for (unsigned i=0; i < s.probes.size(); i++)
5281 s.probes[i]->body->visit(&dfr);
5282 }
5283 }
5284
5285 struct stable_analysis: public nop_visitor
5286 {
5287 bool stable;
5288 stable_analysis(): stable(false) {};
5289
5290 void visit_embeddedcode (embeddedcode* s);
5291 };
5292
5293 void stable_analysis::visit_embeddedcode (embeddedcode* s)
5294 {
5295 if (s->tagged_p("/* stable */"))
5296 stable = true;
5297 if (stable && !s->tagged_p("/* pure */"))
5298 throw SEMANTIC_ERROR(_("stable function must also be /* pure */"),
5299 s->tok);
5300 }
5301
5302 // Examines entire subtree for any stable functioncalls.
5303 struct stable_finder: public traversing_visitor
5304 {
5305 bool stable;
5306 set<string>& stable_fcs;
5307 stable_finder(set<string>&s): stable(false), stable_fcs(s) {};
5308 void visit_functioncall (functioncall* e);
5309 };
5310
5311 void stable_finder::visit_functioncall (functioncall* e)
5312 {
5313 if (stable_fcs.find(e->function) != stable_fcs.end())
5314 stable = true;
5315 traversing_visitor::visit_functioncall(e);
5316 }
5317
5318 // Examines current level of block for stable functioncalls.
5319 // Does not descend into sublevels.
5320 struct level_check: public traversing_visitor
5321 {
5322 bool stable;
5323 set<string>& stable_fcs;
5324 level_check(set<string>& s): stable(false), stable_fcs(s) {};
5325
5326 void visit_block (block* s);
5327 void visit_try_block (try_block *s);
5328 void visit_if_statement (if_statement* s);
5329 void visit_for_loop (for_loop* s);
5330 void visit_foreach_loop (foreach_loop* s);
5331 void visit_functioncall (functioncall* s);
5332 };
5333
5334 void level_check::visit_block (block*)
5335 {
5336 }
5337
5338 void level_check::visit_try_block (try_block* s)
5339 {
5340 if (s->catch_error_var)
5341 s->catch_error_var->visit(this);
5342 }
5343
5344 void level_check::visit_if_statement (if_statement* s)
5345 {
5346 s->condition->visit(this);
5347 }
5348
5349 void level_check::visit_for_loop (for_loop* s)
5350 {
5351 if (s->init) s->init->visit(this);
5352 s->cond->visit(this);
5353 if (s->incr) s->incr->visit(this);
5354 }
5355
5356 void level_check::visit_foreach_loop (foreach_loop* s)
5357 {
5358 s->base->visit(this);
5359
5360 for (unsigned i=0; i<s->indexes.size(); i++)
5361 s->indexes[i]->visit(this);
5362
5363 if (s->value)
5364 s->value->visit(this);
5365
5366 if (s->limit)
5367 s->limit->visit(this);
5368 }
5369
5370 void level_check::visit_functioncall (functioncall* e)
5371 {
5372 if (stable_fcs.find(e->function) != stable_fcs.end())
5373 stable = true;
5374 traversing_visitor::visit_functioncall(e);
5375 }
5376
5377 struct stable_functioncall_visitor: public update_visitor
5378 {
5379 systemtap_session& session;
5380 functiondecl* current_function;
5381 derived_probe* current_probe;
5382 set<string>& stable_fcs;
5383 set<string> scope_vars;
5384 map<string,vardecl*> new_vars;
5385 vector<pair<expr_statement*,block*> > new_stmts;
5386 unsigned loop_depth;
5387 block* top_scope;
5388 block* curr_scope;
5389 stable_functioncall_visitor(systemtap_session& s, set<string>& sfc):
5390 update_visitor(s.verbose),
5391 session(s), current_function(0), current_probe(0), stable_fcs(sfc),
5392 loop_depth(0), top_scope(0), curr_scope(0) {};
5393
5394 statement* convert_stmt(statement* s);
5395 void visit_block (block* s);
5396 void visit_try_block (try_block* s);
5397 void visit_if_statement (if_statement* s);
5398 void visit_for_loop (for_loop* s);
5399 void visit_foreach_loop (foreach_loop* s);
5400 void visit_functioncall (functioncall* e);
5401 };
5402
5403 statement* stable_functioncall_visitor::convert_stmt (statement* s)
5404 {
5405 if (top_scope == 0 &&
5406 (dynamic_cast<for_loop*>(s) || dynamic_cast<foreach_loop*>(s)))
5407 {
5408 stable_finder sf(stable_fcs);
5409 s->visit(&sf);
5410 if (sf.stable)
5411 {
5412 block* b = new block;
5413 b->tok = s->tok;
5414 b->statements.push_back(s);
5415 return b;
5416 }
5417 }
5418 else if (top_scope == 0 && !dynamic_cast<block*>(s))
5419 {
5420 level_check lc(stable_fcs);
5421 s->visit(&lc);
5422 if (lc.stable)
5423 {
5424 block* b = new block;
5425 b->tok = s->tok;
5426 b->statements.push_back(s);
5427 return b;
5428 }
5429 }
5430
5431 return s;
5432 }
5433
5434 void stable_functioncall_visitor::visit_block (block* s)
5435 {
5436 block* prev_top_scope = top_scope;
5437 block* prev_scope = curr_scope;
5438 if (loop_depth == 0)
5439 top_scope = s;
5440 curr_scope = s;
5441 set<string> current_vars = scope_vars;
5442
5443 update_visitor::visit_block(s);
5444
5445 if (loop_depth == 0)
5446 top_scope = prev_top_scope;
5447 curr_scope = prev_scope;
5448 scope_vars = current_vars;
5449 }
5450
5451 void stable_functioncall_visitor::visit_try_block (try_block* s)
5452 {
5453 if (s->try_block)
5454 s->try_block = convert_stmt(s->try_block);
5455 replace(s->try_block);
5456 replace(s->catch_error_var);
5457 if (s->catch_block)
5458 s->catch_block = convert_stmt(s->catch_block);
5459 replace(s->catch_block);
5460 provide(s);
5461 }
5462
5463 void stable_functioncall_visitor::visit_if_statement (if_statement* s)
5464 {
5465 block* prev_top_scope = top_scope;
5466
5467 if (loop_depth == 0)
5468 top_scope = 0;
5469 replace(s->condition);
5470 s->thenblock = convert_stmt(s->thenblock);
5471 replace(s->thenblock);
5472 if (loop_depth == 0)
5473 top_scope = 0;
5474 if (s->elseblock)
5475 s->elseblock = convert_stmt(s->elseblock);
5476 replace(s->elseblock);
5477 provide(s);
5478
5479 top_scope = prev_top_scope;
5480 }
5481
5482 void stable_functioncall_visitor::visit_for_loop (for_loop* s)
5483 {
5484 replace(s->init);
5485 replace(s->cond);
5486 replace(s->incr);
5487 loop_depth++;
5488 s->block = convert_stmt(s->block);
5489 replace(s->block);
5490 loop_depth--;
5491 provide(s);
5492 }
5493
5494 void stable_functioncall_visitor::visit_foreach_loop (foreach_loop* s)
5495 {
5496 for (unsigned i = 0; i < s->indexes.size(); ++i)
5497 replace(s->indexes[i]);
5498 replace(s->base);
5499 replace(s->value);
5500 replace(s->limit);
5501 loop_depth++;
5502 s->block = convert_stmt(s->block);
5503 replace(s->block);
5504 loop_depth--;
5505 provide(s);
5506 }
5507
5508 void stable_functioncall_visitor::visit_functioncall (functioncall* e)
5509 {
5510 for (unsigned i = 0; i < e->args.size(); ++i)
5511 replace (e->args[i]);
5512
5513 if (stable_fcs.find(e->function) != stable_fcs.end())
5514 {
5515 string name("__stable_");
5516 name.append(e->function).append("_value");
5517
5518 // Variable potentially not in scope since it is in a sibling block
5519 if (scope_vars.find(e->function) == scope_vars.end())
5520 {
5521 if (new_vars.find(e->function) == new_vars.end())
5522 {
5523 // New variable declaration to store result of function call
5524 vardecl* v = new vardecl;
5525 v->unmangled_name = v->name = name;
5526 v->tok = e->tok;
5527 v->set_arity(0, e->tok);
5528 v->type = e->type;
5529 if (current_function)
5530 current_function->locals.push_back(v);
5531 else
5532 current_probe->locals.push_back(v);
5533 new_vars[e->function] = v;
5534 }
5535
5536 symbol* sym = new symbol;
5537 sym->name = name;
5538 sym->tok = e->tok;
5539 sym->referent = new_vars[e->function];
5540 sym->type = e->type;
5541
5542 functioncall* fc = new functioncall;
5543 fc->tok = e->tok;
5544 fc->function = e->function;
5545 fc->referents = e->referents;
5546 fc->type = e->type;
5547
5548 assignment* a = new assignment;
5549 a->tok = e->tok;
5550 a->op = "=";
5551 a->left = sym;
5552 a->right = fc;
5553 a->type = e->type;
5554
5555 expr_statement* es = new expr_statement;
5556 es->tok = e->tok;
5557 es->value = a;
5558
5559 // Store location of the block to put new declaration.
5560 if (loop_depth != 0)
5561 {
5562 assert(top_scope);
5563 new_stmts.push_back(make_pair(es,top_scope));
5564 }
5565 else
5566 {
5567 assert(curr_scope);
5568 new_stmts.push_back(make_pair(es,curr_scope));
5569 }
5570
5571 scope_vars.insert(e->function);
5572
5573 provide(sym);
5574 }
5575 else
5576 {
5577 symbol* sym = new symbol;
5578 sym->name = name;
5579 sym->tok = e->tok;
5580 sym->referent = new_vars[e->function];
5581 sym->type = e->type;
5582 provide(sym);
5583 }
5584 return;
5585 }
5586
5587 provide(e);
5588 }
5589
5590 // Cache stable embedded-c functioncall results and replace
5591 // all calls with same name using that value to reduce duplicate
5592 // functioncall overhead. Functioncalls are pulled out of any
5593 // top-level loops and put into if/try blocks.
5594 void semantic_pass_opt7(systemtap_session& s)
5595 {
5596 set<string> stable_fcs;
5597 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5598 it != s.functions.end(); ++it)
5599 {
5600 functiondecl* fn = (*it).second;
5601 stable_analysis sa;
5602 fn->body->visit(&sa);
5603 if (sa.stable && fn->formal_args.size() == 0)
5604 stable_fcs.insert(fn->name);
5605 }
5606
5607 for (vector<derived_probe*>::iterator it = s.probes.begin();
5608 it != s.probes.end(); ++it)
5609 {
5610 stable_functioncall_visitor t(s, stable_fcs);
5611 t.current_probe = *it;
5612 (*it)->body = t.convert_stmt((*it)->body);
5613 t.replace((*it)->body);
5614
5615 for (vector<pair<expr_statement*,block*> >::iterator st = t.new_stmts.begin();
5616 st != t.new_stmts.end(); ++st)
5617 st->second->statements.insert(st->second->statements.begin(), st->first);
5618 }
5619
5620 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5621 it != s.functions.end(); ++it)
5622 {
5623 functiondecl* fn = (*it).second;
5624 stable_functioncall_visitor t(s, stable_fcs);
5625 t.current_function = fn;
5626 fn->body = t.convert_stmt(fn->body);
5627 t.replace(fn->body);
5628
5629 for (vector<pair<expr_statement*,block*> >::iterator st = t.new_stmts.begin();
5630 st != t.new_stmts.end(); ++st)
5631 st->second->statements.insert(st->second->statements.begin(), st->first);
5632 }
5633 }
5634
5635 static int
5636 semantic_pass_optimize1 (systemtap_session& s)
5637 {
5638 // In this pass, we attempt to rewrite probe/function bodies to
5639 // eliminate some blatantly unnecessary code. This is run before
5640 // type inference, but after symbol resolution and derived_probe
5641 // creation. We run an outer "relaxation" loop that repeats the
5642 // optimizations until none of them find anything to remove.
5643
5644 int rc = 0;
5645
5646 bool relaxed_p = false;
5647 unsigned iterations = 0;
5648 while (! relaxed_p)
5649 {
5650 // Save the old value of suppress_warnings, as we will be changing
5651 // it below.
5652 save_and_restore<bool> suppress_warnings(& s.suppress_warnings);
5653
5654 assert_no_interrupts();
5655
5656 relaxed_p = true; // until proven otherwise
5657
5658 // If the verbosity is high enough, always print warnings (overrides -w),
5659 // or if not, always suppress warnings for every itteration after the first.
5660 if(s.verbose > 2)
5661 s.suppress_warnings = false;
5662 else if (iterations > 0)
5663 s.suppress_warnings = true;
5664
5665 if (!s.unoptimized)
5666 {
5667 semantic_pass_opt1 (s, relaxed_p);
5668 semantic_pass_opt2 (s, relaxed_p, iterations); // produce some warnings only on iteration=0
5669 semantic_pass_opt3 (s, relaxed_p);
5670 semantic_pass_opt4 (s, relaxed_p);
5671 semantic_pass_opt5 (s, relaxed_p);
5672 }
5673
5674 // For listing mode, we need const-folding regardless of optimization so
5675 // that @defined expressions can be properly resolved. PR11360
5676 // We also want it in case variables are used in if/case expressions,
5677 // so enable always. PR11366
5678 // rc is incremented if there is an error that got reported.
5679 rc += semantic_pass_const_fold (s, relaxed_p);
5680
5681 if (!s.unoptimized)
5682 semantic_pass_dead_control (s, relaxed_p);
5683
5684 if (!s.unoptimized)
5685 semantic_pass_overload (s, relaxed_p);
5686
5687 iterations ++;
5688 }
5689
5690 // We will now remove probes that have empty handlers and join the remaining probes
5691 // with their groups. Do not elide probes when the unoptimziation flag is set.
5692
5693 vector<derived_probe*> non_empty_probes;
5694
5695 for (unsigned i = 0; i < s.probes.size(); i++)
5696 {
5697 derived_probe* p = s.probes[i];
5698
5699 if (s.unoptimized || s.dump_mode)
5700 p->join_group(s);
5701 else if (s.empty_probes.find(p) == s.empty_probes.end())
5702 {
5703 non_empty_probes.push_back(p);
5704 p->join_group(s);
5705 }
5706 else if (!s.timing && // PR10070
5707 !(p->base->tok->location.file->synthetic)) // don't warn for synthetic probes
5708 s.print_warning(_F("Probe '%s' has been elided",
5709 ((string) p->locations[0]->components[0]->functor).c_str()), p->tok);
5710 }
5711
5712 if (!s.unoptimized && !s.dump_mode)
5713 s.probes = non_empty_probes;
5714
5715 return rc;
5716 }
5717
5718
5719 static int
5720 semantic_pass_optimize2 (systemtap_session& s)
5721 {
5722 // This is run after type inference. We run an outer "relaxation"
5723 // loop that repeats the optimizations until none of them find
5724 // anything to remove.
5725
5726 int rc = 0;
5727
5728 // Save the old value of suppress_warnings, as we will be changing
5729 // it below.
5730 save_and_restore<bool> suppress_warnings(& s.suppress_warnings);
5731
5732 bool relaxed_p = false;
5733 unsigned iterations = 0;
5734 while (! relaxed_p)
5735 {
5736 assert_no_interrupts();
5737 relaxed_p = true; // until proven otherwise
5738
5739 // If the verbosity is high enough, always print warnings (overrides -w),
5740 // or if not, always suppress warnings for every itteration after the first.
5741 if(s.verbose > 2)
5742 s.suppress_warnings = false;
5743 else if (iterations > 0)
5744 s.suppress_warnings = true;
5745
5746 if (!s.unoptimized)
5747 semantic_pass_opt6 (s, relaxed_p);
5748
5749 iterations++;
5750 }
5751
5752 if (!s.unoptimized)
5753 semantic_pass_opt7(s);
5754
5755 return rc;
5756 }
5757
5758
5759
5760 // ------------------------------------------------------------------------
5761 // type resolution
5762
5763 struct autocast_expanding_visitor: public var_expanding_visitor
5764 {
5765 typeresolution_info& ti;
5766 autocast_expanding_visitor (systemtap_session& s, typeresolution_info& ti):
5767 var_expanding_visitor(s), ti(ti) {}
5768
5769 void resolve_functioncall (functioncall* fc)
5770 {
5771 // This is a very limited version of semantic_pass_symbols, but
5772 // we're late in the game at this point (after basic symbol
5773 // resolution already took place). We won't get a chance to
5774 // optimize, but for now the only functions we expect are
5775 // kernel/user_string from pretty-printing, which don't need
5776 // optimization.
5777
5778 systemtap_session& s = ti.session;
5779 size_t nfiles = s.files.size();
5780
5781 symresolution_info sym (s);
5782 sym.current_function = ti.current_function;
5783 sym.current_probe = ti.current_probe;
5784 fc->visit (&sym);
5785
5786 // NB: synthetic functions get tacked onto the origin file, so we won't
5787 // see them growing s.files[]. Traverse it directly.
5788 for (unsigned i = 0; i < fc->referents.size(); i++)
5789 {
5790 functiondecl* fd = fc->referents[i];
5791 sym.current_function = fd;
5792 sym.current_probe = 0;
5793 fd->body->visit (&sym);
5794 }
5795
5796 while (nfiles < s.files.size())
5797 {
5798 stapfile* dome = s.files[nfiles++];
5799 for (size_t i = 0; i < dome->functions.size(); ++i)
5800 {
5801 functiondecl* fd = dome->functions[i];
5802 sym.current_function = fd;
5803 sym.current_probe = 0;
5804 fd->body->visit (&sym);
5805 // NB: not adding to s.functions just yet...
5806 }
5807 }
5808
5809 // Add only the direct functions we need.
5810 functioncall_traversing_visitor ftv;
5811 fc->visit (&ftv);
5812 for (set<functiondecl*>::iterator it = ftv.seen.begin();
5813 it != ftv.seen.end(); ++it)
5814 {
5815 functiondecl* fd = *it;
5816 pair<map<string,functiondecl*>::iterator,bool> inserted =
5817 s.functions.insert (make_pair (fd->name, fd));
5818 if (!inserted.second && inserted.first->second != fd)
5819 throw SEMANTIC_ERROR
5820 (_F("resolved function '%s' conflicts with an existing function",
5821 fd->unmangled_name.to_string().c_str()), fc->tok);
5822 }
5823 }
5824
5825 void visit_autocast_op (autocast_op* e)
5826 {
5827 const bool lvalue = is_active_lvalue (e);
5828 const exp_type_ptr& details = e->operand->type_details;
5829 if (details && !e->saved_conversion_error)
5830 {
5831 functioncall* fc = details->expand (e, lvalue);
5832 if (fc)
5833 {
5834 ti.num_newly_resolved++;
5835
5836 resolve_functioncall (fc);
5837 // NB: at this stage, the functioncall object has one
5838 // argument too few if we're in lvalue context. It will
5839 // be filled in only later (as the
5840 // var_expanding_visitor::visit_assignment bit rolls
5841 // back up). But nevertheless we must resolve the fc,
5842 // otherwise, symresolution_info::visit_functioncall will
5843 // throw a mismatched-arity error. (semok/autocast08.stp)
5844
5845 if (lvalue)
5846 provide_lvalue_call (fc);
5847
5848 fc->visit (this);
5849 return;
5850 }
5851 }
5852 var_expanding_visitor::visit_autocast_op (e);
5853 }
5854 };
5855
5856
5857 struct initial_typeresolution_info : public typeresolution_info
5858 {
5859 initial_typeresolution_info (systemtap_session& s): typeresolution_info(s)
5860 {}
5861
5862 // these expressions are not supposed to make its way to the typeresolution
5863 // pass. they probably get substituted/replaced, but since this is an initial pass
5864 // and not all substitutions are done, replace the functions that throw errors.
5865 void visit_target_symbol (target_symbol*) {}
5866 void visit_atvar_op (atvar_op*) {}
5867 void visit_defined_op (defined_op*) {}
5868 void visit_entry_op (entry_op*) {}
5869 void visit_cast_op (cast_op*) {}
5870 };
5871
5872 static int initial_typeres_pass(systemtap_session& s)
5873 {
5874 // minimal type resolution based off of semantic_pass_types(), without
5875 // checking for complete type resolutions or autocast expanding
5876 initial_typeresolution_info ti(s);
5877
5878 ti.assert_resolvability = false;
5879 while (1)
5880 {
5881 assert_no_interrupts();
5882
5883 ti.num_newly_resolved = 0;
5884 ti.num_still_unresolved = 0;
5885 ti.num_available_autocasts = 0;
5886
5887 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5888 it != s.functions.end(); it++)
5889 {
5890 assert_no_interrupts();
5891
5892 functiondecl* fd = it->second;
5893 ti.current_probe = 0;
5894 ti.current_function = fd;
5895 ti.t = pe_unknown;
5896 fd->body->visit (& ti);
5897 }
5898
5899 for (unsigned j=0; j<s.probes.size(); j++)
5900 {
5901 assert_no_interrupts();
5902
5903 derived_probe* pn = s.probes[j];
5904 ti.current_function = 0;
5905 ti.current_probe = pn;
5906 ti.t = pe_unknown;
5907 pn->body->visit (& ti);
5908
5909 probe_point* pp = pn->sole_location();
5910 if (pp->condition)
5911 {
5912 ti.current_function = 0;
5913 ti.current_probe = 0;
5914 ti.t = pe_long; // NB: expected type
5915 pp->condition->visit (& ti);
5916 }
5917 }
5918 if (ti.num_newly_resolved == 0) // converged
5919 {
5920 // take into account that if there are mismatches, we'd want to know
5921 // about them incase they get whisked away, later in this process
5922 if (!ti.assert_resolvability && ti.mismatch_complexity > 0) // found a mismatch!!
5923 {
5924 ti.assert_resolvability = true; // report errors
5925 if (s.verbose > 0)
5926 ti.mismatch_complexity = 1; // print out mismatched but not unresolved type mismatches
5927 }
5928 else
5929 break;
5930 }
5931 else
5932 ti.mismatch_complexity = 0;
5933 }
5934
5935 return s.num_errors();
5936 }
5937
5938 static int
5939 semantic_pass_types (systemtap_session& s)
5940 {
5941 int rc = 0;
5942
5943 // next pass: type inference
5944 unsigned iterations = 0;
5945 typeresolution_info ti (s);
5946
5947 ti.assert_resolvability = false;
5948 while (1)
5949 {
5950 assert_no_interrupts();
5951
5952 iterations ++;
5953 ti.num_newly_resolved = 0;
5954 ti.num_still_unresolved = 0;
5955 ti.num_available_autocasts = 0;
5956
5957 for (map<string,functiondecl*>::iterator it = s.functions.begin();
5958 it != s.functions.end(); it++)
5959 try
5960 {
5961 assert_no_interrupts();
5962
5963 functiondecl* fd = it->second;
5964 ti.current_probe = 0;
5965 ti.current_function = fd;
5966 ti.t = pe_unknown;
5967
5968 fd->body->visit (& ti);
5969 // NB: we don't have to assert a known type for
5970 // functions here, to permit a "void" function.
5971 // The translator phase will omit the "retvalue".
5972 //
5973 // if (fd->type == pe_unknown)
5974 // ti.unresolved (fd->tok);
5975 for (unsigned i=0; i < fd->locals.size(); ++i)
5976 ti.check_local (fd->locals[i]);
5977
5978 // Check and run the autocast expanding visitor.
5979 if (ti.num_available_autocasts > 0)
5980 {
5981 autocast_expanding_visitor aev (s, ti);
5982 aev.replace (fd->body);
5983
5984 // PR18079, rerun the const-folder / dead-block-remover
5985 // if autocast evaluation enabled a @defined()
5986 if (! aev.relaxed())
5987 {
5988 bool relaxed_p = true;
5989 const_folder cf (s, relaxed_p);
5990 cf.replace (fd->body);
5991 if (! s.unoptimized)
5992 {
5993 dead_control_remover dc (s, relaxed_p);
5994 fd->body->visit (&dc);
5995 }
5996 (void) relaxed_p; // we judge success later by num_still_unresolved, not this flag
5997 }
5998
5999 ti.num_available_autocasts = 0;
6000 }
6001 }
6002 catch (const semantic_error& e)
6003 {
6004 throw SEMANTIC_ERROR(_F("while processing function %s",
6005 it->second->unmangled_name.to_string().c_str())).set_chain(e);
6006 }
6007
6008 for (unsigned j=0; j<s.probes.size(); j++)
6009 try
6010 {
6011 assert_no_interrupts();
6012
6013 derived_probe* pn = s.probes[j];
6014 ti.current_function = 0;
6015 ti.current_probe = pn;
6016 ti.t = pe_unknown;
6017
6018 pn->body->visit (& ti);
6019 for (unsigned i=0; i < pn->locals.size(); ++i)
6020 ti.check_local (pn->locals[i]);
6021
6022 // Check and run the autocast expanding visitor.
6023 if (ti.num_available_autocasts > 0)
6024 {
6025 autocast_expanding_visitor aev (s, ti);
6026 var_expand_const_fold_loop (s, pn->body, aev);
6027 // PR18079, rerun the const-folder / dead-block-remover
6028 // if autocast evaluation enabled a @defined()
6029 if (! s.unoptimized)
6030 {
6031 bool relaxed_p;
6032 dead_control_remover dc (s, relaxed_p);
6033 pn->body->visit (&dc);
6034 (void) relaxed_p; // we judge success later by num_still_unresolved, not this flag
6035 }
6036
6037 ti.num_available_autocasts = 0;
6038 }
6039
6040 probe_point* pp = pn->sole_location();
6041 if (pp->condition)
6042 {
6043 ti.current_function = 0;
6044 ti.current_probe = 0;
6045 ti.t = pe_long; // NB: expected type
6046 pp->condition->visit (& ti);
6047 }
6048 }
6049 catch (const semantic_error& e)
6050 {
6051 throw SEMANTIC_ERROR(_F("while processing probe %s",
6052 s.probes[j]->derived_locations(false).c_str())).set_chain(e);
6053 }
6054
6055 for (unsigned j=0; j<s.globals.size(); j++)
6056 {
6057 vardecl* gd = s.globals[j];
6058 if (gd->type == pe_unknown)
6059 ti.unresolved (gd->tok);
6060 if(gd->arity == 0 && gd->wrap == true)
6061 {
6062 throw SEMANTIC_ERROR(_("wrapping not supported for scalars"), gd->tok);
6063 }
6064 }
6065
6066 if (ti.num_newly_resolved == 0) // converged
6067 {
6068 if (ti.num_still_unresolved == 0)
6069 break; // successfully
6070 else if (! ti.assert_resolvability)
6071 {
6072 // PR18079, before we go asserting anything, try to nullify any
6073 // still-unresolved @defined ops.
6074 bool relaxed_p = true;
6075 const_folder cf (s, relaxed_p, true); // NB: true: collapse remaining @defined's
6076
6077 for (auto it = s.probes.begin(); it != s.probes.end(); ++it)
6078 cf.replace ((*it)->body);
6079 for (auto it = s.functions.begin(); it != s.functions.end(); ++it)
6080 cf.replace (it->second->body);
6081
6082 if (! s.unoptimized)
6083 semantic_pass_dead_control (s, relaxed_p);
6084
6085 if (! relaxed_p)
6086 ti.mismatch_complexity = 0; // reset for next pass
6087 else
6088 {
6089 ti.assert_resolvability = true; // last pass, with error msgs
6090 if (s.verbose > 0)
6091 ti.mismatch_complexity = 0; // print every kind of mismatch
6092 }
6093 }
6094 else
6095 { // unsuccessful conclusion
6096 rc ++;
6097 break;
6098 }
6099 }
6100 else
6101 ti.mismatch_complexity = 0; // reset for next pass
6102 }
6103
6104 return rc + s.num_errors();
6105 }
6106
6107
6108 struct exp_type_null : public exp_type_details
6109 {
6110 uintptr_t id () const { return 0; }
6111 bool expandable() const { return false; }
6112 functioncall *expand(autocast_op*, bool) { return NULL; }
6113 };
6114
6115 typeresolution_info::typeresolution_info (systemtap_session& s):
6116 session(s), num_newly_resolved(0), num_still_unresolved(0),
6117 num_available_autocasts(0),
6118 assert_resolvability(false), mismatch_complexity(0),
6119 current_function(0), current_probe(0), t(pe_unknown),
6120 null_type(make_shared<exp_type_null>())
6121 {
6122 }
6123
6124
6125 void
6126 typeresolution_info::visit_literal_number (literal_number* e)
6127 {
6128 assert (e->type == pe_long);
6129 if ((t == e->type) || (t == pe_unknown))
6130 return;
6131
6132 mismatch (e->tok, t, e->type);
6133 }
6134
6135
6136 void
6137 typeresolution_info::visit_literal_string (literal_string* e)
6138 {
6139 assert (e->type == pe_string);
6140 if ((t == e->type) || (t == pe_unknown))
6141 return;
6142
6143 mismatch (e->tok, t, e->type);
6144 }
6145
6146
6147 void
6148 typeresolution_info::visit_logical_or_expr (logical_or_expr *e)
6149 {
6150 visit_binary_expression (e);
6151 }
6152
6153
6154 void
6155 typeresolution_info::visit_logical_and_expr (logical_and_expr *e)
6156 {
6157 visit_binary_expression (e);
6158 }
6159
6160 void
6161 typeresolution_info::visit_regex_query (regex_query *e)
6162 {
6163 // NB: result of regex query is an integer!
6164 if (t == pe_stats || t == pe_string)
6165 invalid (e->tok, t);
6166
6167 t = pe_string;
6168 e->left->visit (this);
6169 t = pe_string;
6170 e->right->visit (this); // parser ensures this is a literal known at compile time
6171
6172 if (e->type == pe_unknown)
6173 {
6174 e->type = pe_long;
6175 resolved (e->tok, e->type);
6176 }
6177 }
6178
6179 void
6180 typeresolution_info::visit_compound_expression (compound_expression* e)
6181 {
6182 // Incoming type inference applies to the RHS.
6183 e->right->visit(this);
6184
6185 // No incoming data for the LHS.
6186 t = pe_unknown;
6187 e->left->visit(this);
6188 }
6189
6190
6191 void
6192 typeresolution_info::visit_comparison (comparison *e)
6193 {
6194 // NB: result of any comparison is an integer!
6195 if (t == pe_stats || t == pe_string)
6196 invalid (e->tok, t);
6197
6198 t = (e->right->type != pe_unknown) ? e->right->type : pe_unknown;
6199 e->left->visit (this);
6200 t = (e->left->type != pe_unknown) ? e->left->type : pe_unknown;
6201 e->right->visit (this);
6202
6203 if (e->left->type != pe_unknown &&
6204 e->right->type != pe_unknown &&
6205 e->left->type != e->right->type)
6206 mismatch (e);
6207
6208 if (e->type == pe_unknown)
6209 {
6210 e->type = pe_long;
6211 resolved (e->tok, e->type);
6212 }
6213 }
6214
6215
6216 void
6217 typeresolution_info::visit_concatenation (concatenation *e)
6218 {
6219 if (t != pe_unknown && t != pe_string)
6220 invalid (e->tok, t);
6221
6222 t = pe_string;
6223 e->left->visit (this);
6224 t = pe_string;
6225 e->right->visit (this);
6226
6227 if (e->type == pe_unknown)
6228 {
6229 e->type = pe_string;
6230 resolved (e->tok, e->type);
6231 }
6232 }
6233
6234
6235 void
6236 typeresolution_info::visit_assignment (assignment *e)
6237 {
6238 if (t == pe_stats)
6239 invalid (e->tok, t);
6240
6241 if (e->op == "<<<") // stats aggregation
6242 {
6243 if (t == pe_string)
6244 invalid (e->tok, t);
6245
6246 t = pe_stats;
6247 e->left->visit (this);
6248 t = pe_long;
6249 e->right->visit (this);
6250 if (e->type == pe_unknown ||
6251 e->type == pe_stats)
6252 {
6253 e->type = pe_long;
6254 resolved (e->tok, e->type);
6255 }
6256 }
6257
6258 else if (e->left->type == pe_stats)
6259 invalid (e->left->tok, e->left->type);
6260
6261 else if (e->right->type == pe_stats)
6262 invalid (e->right->tok, e->right->type);
6263
6264 else if (e->op == "+=" || // numeric only
6265 e->op == "-=" ||
6266 e->op == "*=" ||
6267 e->op == "/=" ||
6268 e->op == "%=" ||
6269 e->op == "&=" ||
6270 e->op == "^=" ||
6271 e->op == "|=" ||
6272 e->op == "<<=" ||
6273 e->op == ">>=" ||
6274 false)
6275 {
6276 visit_binary_expression (e);
6277 }
6278 else if (e->op == ".=" || // string only
6279 false)
6280 {
6281 if (t == pe_long || t == pe_stats)
6282 invalid (e->tok, t);
6283
6284 t = pe_string;
6285 e->left->visit (this);
6286 t = pe_string;
6287 e->right->visit (this);
6288 if (e->type == pe_unknown)
6289 {
6290 e->type = pe_string;
6291 resolved (e->tok, e->type);
6292 }
6293 }
6294 else if (e->op == "=") // overloaded = for string & numeric operands
6295 {
6296
6297 // logic similar to ternary_expression
6298 exp_type sub_type = t;
6299
6300 // Infer types across the l/r values
6301 if (sub_type == pe_unknown && e->type != pe_unknown)
6302 sub_type = e->type;
6303
6304 t = (sub_type != pe_unknown) ? sub_type :
6305 (e->right->type != pe_unknown) ? e->right->type :
6306 pe_unknown;
6307 e->left->visit (this);
6308 t = (sub_type != pe_unknown) ? sub_type :
6309 (e->left->type != pe_unknown) ? e->left->type :
6310 pe_unknown;
6311 e->right->visit (this);
6312
6313 if ((sub_type != pe_unknown) && (e->type == pe_unknown))
6314 {
6315 e->type = sub_type;
6316 resolved (e->tok, e->type);
6317 }
6318 if ((sub_type == pe_unknown) && (e->left->type != pe_unknown))
6319 {
6320 e->type = e->left->type;
6321 resolved (e->tok, e->type);
6322 }
6323
6324 if (e->left->type != pe_unknown &&
6325 e->right->type != pe_unknown &&
6326 e->left->type != e->right->type)
6327 mismatch (e);
6328
6329 // Propagate type details from the RHS to the assignment
6330 if (e->type == e->right->type &&
6331 e->right->type_details && !e->type_details)
6332 resolved_details(e->right->type_details, e->type_details);
6333
6334 // Propagate type details from the assignment to the LHS
6335 if (e->type == e->left->type && e->type_details)
6336 {
6337 if (e->left->type_details &&
6338 *e->left->type_details != *e->type_details &&
6339 *e->left->type_details != *null_type)
6340 resolved_details(null_type, e->left->type_details);
6341 else if (!e->left->type_details)
6342 resolved_details(e->type_details, e->left->type_details);
6343 }
6344 }
6345 else
6346 throw SEMANTIC_ERROR (_("internal error: unsupported assignment operator ") + (string)e->op);
6347 }
6348
6349
6350 void
6351 typeresolution_info::visit_embedded_expr (embedded_expr *e)
6352 {
6353 if (e->type == pe_unknown)
6354 {
6355 if (e->tagged_p ("/* string */"))
6356 e->type = pe_string;
6357 else // if (e->tagged_p ("/* long */"))
6358 e->type = pe_long;
6359
6360 resolved (e->tok, e->type);
6361 }
6362 }
6363
6364
6365 void
6366 typeresolution_info::visit_binary_expression (binary_expression* e)
6367 {
6368 if (t == pe_stats || t == pe_string)
6369 invalid (e->tok, t);
6370
6371 t = pe_long;
6372 e->left->visit (this);
6373 t = pe_long;
6374 e->right->visit (this);
6375
6376 if (e->left->type != pe_unknown &&
6377 e->right->type != pe_unknown &&
6378 e->left->type != e->right->type)
6379 mismatch (e);
6380
6381 if (e->type == pe_unknown)
6382 {
6383 e->type = pe_long;
6384 resolved (e->tok, e->type);
6385 }
6386 }
6387
6388
6389 void
6390 typeresolution_info::visit_pre_crement (pre_crement *e)
6391 {
6392 visit_unary_expression (e);
6393 }
6394
6395
6396 void
6397 typeresolution_info::visit_post_crement (post_crement *e)
6398 {
6399 visit_unary_expression (e);
6400 }
6401
6402
6403 void
6404 typeresolution_info::visit_unary_expression (unary_expression* e)
6405 {
6406 if (t == pe_stats || t == pe_string)
6407 invalid (e->tok, t);
6408
6409 t = pe_long;
6410 e->operand->visit (this);
6411
6412 if (e->type == pe_unknown)
6413 {
6414 e->type = pe_long;
6415 resolved (e->tok, e->type);
6416 }
6417 }
6418
6419
6420 void
6421 typeresolution_info::visit_ternary_expression (ternary_expression* e)
6422 {
6423 exp_type sub_type = t;
6424
6425 t = pe_long;
6426 e->cond->visit (this);
6427
6428 // Infer types across the true/false arms of the ternary expression.
6429
6430 if (sub_type == pe_unknown && e->type != pe_unknown)
6431 sub_type = e->type;
6432 t = sub_type;
6433 e->truevalue->visit (this);
6434 t = sub_type;
6435 e->falsevalue->visit (this);
6436
6437 if ((sub_type == pe_unknown) && (e->type != pe_unknown))
6438 ; // already resolved
6439 else if ((sub_type != pe_unknown) && (e->type == pe_unknown))
6440 {
6441 e->type = sub_type;
6442 resolved (e->tok, e->type);
6443 }
6444 else if ((sub_type == pe_unknown) && (e->truevalue->type != pe_unknown))
6445 {
6446 e->type = e->truevalue->type;
6447 resolved (e->tok, e->type);
6448 }
6449 else if ((sub_type == pe_unknown) && (e->falsevalue->type != pe_unknown))
6450 {
6451 e->type = e->falsevalue->type;
6452 resolved (e->tok, e->type);
6453 }
6454 else if (e->type != sub_type)
6455 mismatch (e->tok, sub_type, e->type);
6456
6457 // Propagate type details from both true/false branches
6458 if (!e->type_details &&
6459 e->type == e->truevalue->type && e->type == e->falsevalue->type &&
6460 e->truevalue->type_details && e->falsevalue->type_details &&
6461 *e->truevalue->type_details == *e->falsevalue->type_details)
6462 resolved_details(e->truevalue->type_details, e->type_details);
6463 }
6464
6465
6466 template <class Referrer, class Referent>
6467 void resolve_2types (Referrer* referrer, Referent* referent,
6468 typeresolution_info* r, exp_type t, bool accept_unknown = false)
6469 {
6470 exp_type& re_type = referrer->type;
6471 const token* re_tok = referrer->tok;
6472 exp_type& te_type = referent->type;
6473
6474 if (t != pe_unknown && re_type == t && re_type == te_type)
6475 ; // do nothing: all three e->types in agreement
6476 else if (t == pe_unknown && re_type != pe_unknown && re_type == te_type)
6477 ; // do nothing: two known e->types in agreement
6478 else if (re_type != pe_unknown && te_type != pe_unknown && re_type != te_type)
6479 r->mismatch (re_tok, re_type, referent); // referrer-referent
6480 else if (re_type != pe_unknown && t != pe_unknown && re_type != t)
6481 r->mismatch (re_tok, t, referent); // referrer-t
6482 else if (te_type != pe_unknown && t != pe_unknown && te_type != t)
6483 r->mismatch (re_tok, t, referent); // referent-t
6484 else if (re_type == pe_unknown && t != pe_unknown)
6485 {
6486 // propagate from upstream
6487 re_type = t;
6488 r->resolved (re_tok, re_type);
6489 // catch re_type/te_type mismatch later
6490 }
6491 else if (re_type == pe_unknown && te_type != pe_unknown)
6492 {
6493 // propagate from referent
6494 re_type = te_type;
6495 r->resolved (re_tok, re_type);
6496 // catch re_type/t mismatch later
6497 }
6498 else if (re_type != pe_unknown && te_type == pe_unknown)
6499 {
6500 // propagate to referent
6501 te_type = re_type;
6502 r->resolved (re_tok, re_type, referent);
6503 // catch re_type/t mismatch later
6504 }
6505 else if (! accept_unknown)
6506 r->unresolved (re_tok);
6507 }
6508
6509
6510 void
6511 typeresolution_info::visit_symbol (symbol* e)
6512 {
6513
6514 if (e->referent == 0) // should have been linked or rejected before now
6515 throw SEMANTIC_ERROR (_F("internal error: unresolved symbol '%s'",
6516 e->name.to_string().c_str()), e->tok);
6517
6518 resolve_2types (e, e->referent, this, t);
6519
6520 if (e->type == e->referent->type)
6521 {
6522
6523 // If both have type details, then they either must agree;
6524 // otherwise force them both to null.
6525 if (e->type_details && e->referent->type_details &&
6526 *e->type_details != *e->referent->type_details)
6527 {
6528 this->session.print_warning(_("Potential type mismatch in reassignment"), e->tok);
6529
6530 resolved_details(null_type, e->type_details);
6531 resolved_details(null_type, e->referent->type_details);
6532 }
6533 else if (e->type_details && !e->referent->type_details)
6534 resolved_details(e->type_details, e->referent->type_details);
6535 else if (!e->type_details && e->referent->type_details)
6536 resolved_details(e->referent->type_details, e->type_details);
6537
6538 }
6539
6540 }
6541
6542 void
6543 typeresolution_info::visit_target_register (target_register* e)
6544 {
6545 if (t != pe_long)
6546 invalid (e->tok, t);
6547 if (e->type == pe_unknown)
6548 {
6549 e->type = pe_long;
6550 resolved (e->tok, e->type);
6551 }
6552 }
6553
6554 void
6555 typeresolution_info::visit_target_deref (target_deref* e)
6556 {
6557 // Both the target_deref result as well as the address must both be longs.
6558 if (t != pe_long)
6559 invalid (e->tok, t);
6560 e->addr->visit (this);
6561
6562 if (e->type == pe_unknown)
6563 {
6564 e->type = pe_long;
6565 resolved (e->tok, e->type);
6566 }
6567 }
6568
6569 void
6570 typeresolution_info::visit_target_bitfield (target_bitfield*)
6571 {
6572 // These are all expanded much earlier.
6573 abort();
6574 }
6575
6576 void
6577 typeresolution_info::visit_target_symbol (target_symbol* e)
6578 {
6579 // This occurs only if a target symbol was not resolved over in
6580 // tapset.cxx land, that error was properly suppressed, and the
6581 // later unused-expression-elimination pass didn't get rid of it
6582 // either. So we have a target symbol that is believed to be of
6583 // genuine use, yet unresolved by the provider.
6584 //
6585 // PR18079, or it can happen if a $target expression is nested within
6586 // a @defined() test that has not yet been resolved (but can be soon).
6587 if (! assert_resolvability)
6588 {
6589 num_still_unresolved ++;
6590 return;
6591 }
6592
6593 if (session.verbose > 2)
6594 {
6595 clog << _("Resolution problem with ");
6596 if (current_function)
6597 {
6598 clog << "function " << current_function->name << endl;
6599 current_function->body->print (clog);
6600 clog << endl;
6601 }
6602 else if (current_probe)
6603 {
6604 clog << "probe " << *current_probe->sole_location() << endl;
6605 current_probe->body->print (clog);
6606 clog << endl;
6607 }
6608 else
6609 //TRANSLATORS: simply saying not an issue with a probe or function
6610 clog << _("other") << endl;
6611 }
6612
6613 if (e->saved_conversion_error)
6614 session.print_error (* (e->saved_conversion_error));
6615 else
6616 session.print_error (SEMANTIC_ERROR(_("unresolved target-symbol expression"), e->tok));
6617 }
6618
6619
6620 void
6621 typeresolution_info::visit_atvar_op (atvar_op* e)
6622 {
6623 // This occurs only if an @var() was not resolved over in
6624 // tapset.cxx land, that error was properly suppressed, and the
6625 // later unused-expression-elimination pass didn't get rid of it
6626 // either. So we have an @var() that is believed to be of
6627 // genuine use, yet unresolved by the provider.
6628
6629 if (session.verbose > 2)
6630 {
6631 clog << _("Resolution problem with ");
6632 if (current_function)
6633 {
6634 clog << "function " << current_function->name << endl;
6635 current_function->body->print (clog);
6636 clog << endl;
6637 }
6638 else if (current_probe)
6639 {
6640 clog << "probe " << *current_probe->sole_location() << endl;
6641 current_probe->body->print (clog);
6642 clog << endl;
6643 }
6644 else
6645 //TRANSLATORS: simply saying not an issue with a probe or function
6646 clog << _("other") << endl;
6647 }
6648
6649 if (e->saved_conversion_error)
6650 session.print_error (* (e->saved_conversion_error));
6651 else
6652 session.print_error (SEMANTIC_ERROR(_("unresolved @var() expression"), e->tok));
6653 }
6654
6655
6656 void
6657 typeresolution_info::visit_defined_op (defined_op* e)
6658 {
6659 // PR18079: if a @defined is still around, it may have a parameter that
6660 // wasn't resolvable one way or another earlier. Maybe an autocast_op.
6661 // Let's give it a visit just in case.
6662 e->operand->visit(this);
6663
6664 if (assert_resolvability)
6665 session.print_error (SEMANTIC_ERROR(_("unexpected @defined"), e->tok));
6666 else
6667 num_still_unresolved ++;
6668 }
6669
6670
6671 void
6672 typeresolution_info::visit_entry_op (entry_op* e)
6673 {
6674 throw SEMANTIC_ERROR(_("internal error: @entry is only valid in .return probes"), e->tok);
6675 }
6676
6677
6678 void
6679 typeresolution_info::visit_cast_op (cast_op* e)
6680 {
6681 // Like target_symbol, a cast_op shouldn't survive this far
6682 // unless it was not resolved and its value is really needed.
6683 if (e->saved_conversion_error)
6684 session.print_error (* (e->saved_conversion_error));
6685 else
6686 session.print_error (SEMANTIC_ERROR(_F("type definition '%s' not found in '%s'",
6687 e->type_name.to_string().c_str(),
6688 e->module.to_string().c_str()), e->tok));
6689 }
6690
6691
6692 void
6693 typeresolution_info::visit_autocast_op (autocast_op* e)
6694 {
6695 // Like cast_op, a implicit autocast_op shouldn't survive this far
6696 // unless it was not resolved and its value is really needed.
6697 if (assert_resolvability && e->saved_conversion_error)
6698 session.print_error (* (e->saved_conversion_error));
6699 else if (assert_resolvability)
6700 session.print_error (SEMANTIC_ERROR(_("unknown type in dereference"), e->tok));
6701
6702 t = pe_long;
6703 e->operand->visit (this);
6704
6705 num_still_unresolved++;
6706 if (e->operand->type_details &&
6707 e->operand->type_details->expandable())
6708 num_available_autocasts++;
6709 }
6710
6711
6712 void
6713 typeresolution_info::visit_perf_op (perf_op* e)
6714 {
6715 // A perf_op should already be resolved
6716 if (t == pe_stats || t == pe_string)
6717 invalid (e->tok, t);
6718
6719 e->type = pe_long;
6720 // XXX: ... but but but ... ::visit_defined_op interprets this ->type
6721 // as meaning that @defined(@perf("JUNK JUNK JUNK")) is valid.
6722 // The dwarf_var_expanding_visitor::visit_perf_op() code that validates
6723 // the JUNK parameter is not even called in time.
6724
6725 // (There is no real need to visit our operand - by parser
6726 // construction, it's always a string literal, with its type already
6727 // set.)
6728 t = pe_string;
6729 e->operand->visit (this);
6730 }
6731
6732
6733 void
6734 typeresolution_info::visit_arrayindex (arrayindex* e)
6735 {
6736
6737 symbol *array = NULL;
6738 hist_op *hist = NULL;
6739 classify_indexable(e->base, array, hist);
6740
6741 // Every hist_op has type [int]:int, that is to say, every hist_op
6742 // is a pseudo-one-dimensional integer array type indexed by
6743 // integers (bucket numbers).
6744
6745 if (hist)
6746 {
6747 if (e->indexes.size() != 1)
6748 unresolved (e->tok);
6749 t = pe_long;
6750 e->indexes[0]->visit (this);
6751 if (e->indexes[0]->type != pe_long)
6752 unresolved (e->tok);
6753 hist->visit (this);
6754 if (e->type != pe_long)
6755 {
6756 e->type = pe_long;
6757 resolved (e->tok, e->type);
6758 }
6759 return;
6760 }
6761
6762 // Now we are left with "normal" map inference and index checking.
6763
6764 assert (array);
6765 assert (array->referent != 0);
6766 resolve_2types (e, array->referent, this, t);
6767
6768 // now resolve the array indexes
6769
6770 // if (e->referent->index_types.size() == 0)
6771 // // redesignate referent as array
6772 // e->referent->set_arity (e->indexes.size ());
6773
6774 if (e->indexes.size() != array->referent->index_types.size())
6775 unresolved (e->tok); // symbol resolution should prevent this
6776 else for (unsigned i=0; i<e->indexes.size(); i++)
6777 {
6778 if (e->indexes[i])
6779 {
6780 expression* ee = e->indexes[i];
6781 exp_type& ft = array->referent->index_types [i];
6782 t = ft;
6783 ee->visit (this);
6784 exp_type at = ee->type;
6785
6786 if ((at == pe_string || at == pe_long) && ft == pe_unknown)
6787 {
6788 // propagate to formal type
6789 ft = at;
6790 resolved (ee->tok, ft, array->referent, i);
6791 }
6792 if (at == pe_stats)
6793 invalid (ee->tok, at);
6794 if (ft == pe_stats)
6795 invalid (ee->tok, ft);
6796 if (at != pe_unknown && ft != pe_unknown && ft != at)
6797 mismatch (ee->tok, ee->type, array->referent, i);
6798 if (at == pe_unknown)
6799 unresolved (ee->tok);
6800 }
6801 }
6802 }
6803
6804
6805 void
6806 typeresolution_info::visit_functioncall (functioncall* e)
6807 {
6808 if (e->referents.empty())
6809 throw SEMANTIC_ERROR (_F("internal error: unresolved function call to '%s'",
6810 e->function.to_string().c_str()), e->tok);
6811
6812 exp_type original = t;
6813 for (unsigned fd = 0; fd < e->referents.size(); fd++)
6814 {
6815 t = original; // type may be changed by overloaded functions so restore it
6816 functiondecl* referent = e->referents[fd];
6817 resolve_2types (e, referent, this, t, true); // accept unknown type
6818
6819 if (e->type == pe_stats)
6820 invalid (e->tok, e->type);
6821
6822 const exp_type_ptr& func_type = referent->type_details;
6823 if (func_type && referent->type == e->type
6824 && (!e->type_details || *func_type != *e->type_details))
6825 resolved_details(referent->type_details, e->type_details);
6826
6827 // now resolve the function parameters
6828 if (e->args.size() != referent->formal_args.size())
6829 unresolved (e->tok); // symbol resolution should prevent this
6830 else for (unsigned i=0; i<e->args.size(); i++)
6831 {
6832 expression* ee = e->args[i];
6833 exp_type& ft = referent->formal_args[i]->type;
6834 const token* fe_tok = referent->formal_args[i]->tok;
6835 t = ft;
6836 ee->visit (this);
6837 exp_type at = ee->type;
6838
6839 if (((at == pe_string) || (at == pe_long)) && ft == pe_unknown)
6840 {
6841 // propagate to formal arg
6842 ft = at;
6843 resolved (ee->tok, ft, referent->formal_args[i], i);
6844 }
6845 if (at == pe_stats)
6846 invalid (ee->tok, at);
6847 if (ft == pe_stats)
6848 invalid (fe_tok, ft);
6849 if (at != pe_unknown && ft != pe_unknown && ft != at)
6850 mismatch (ee->tok, ee->type, referent->formal_args[i], i);
6851 if (at == pe_unknown)
6852 unresolved (ee->tok);
6853 }
6854 }
6855 }
6856
6857
6858 void
6859 typeresolution_info::visit_block (block* e)
6860 {
6861 for (unsigned i=0; i<e->statements.size(); i++)
6862 {
6863 t = pe_unknown;
6864 e->statements[i]->visit (this);
6865 }
6866 }
6867
6868
6869 void
6870 typeresolution_info::visit_try_block (try_block* e)
6871 {
6872 if (e->try_block)
6873 e->try_block->visit (this);
6874 if (e->catch_error_var)
6875 {
6876 t = pe_string;
6877 e->catch_error_var->visit (this);
6878 }
6879 if (e->catch_block)
6880 e->catch_block->visit (this);
6881 }
6882
6883
6884 void
6885 typeresolution_info::visit_embeddedcode (embeddedcode* s)
6886 {
6887 // PR11573. If we have survived thus far with a piece of embedded
6888 // code that requires uprobes, we need to track this.
6889 //
6890 // This is an odd place for this check, as opposed
6891 // to a separate 'optimization' pass, or c_unparser::visit_embeddedcode
6892 // over yonder in pass 3. However, we want to do it during pass 2 so
6893 // that cached sessions also get the uprobes treatment.
6894 if (! session.need_uprobes
6895 && s->tagged_p ("/* pragma:uprobes */"))
6896 {
6897 if (session.verbose > 2)
6898 clog << _("Activating uprobes support because /* pragma:uprobes */ seen.") << endl;
6899 session.need_uprobes = true;
6900 }
6901
6902 // PR15065. Likewise, we need to detect /* pragma:tagged_dfa */
6903 // before the gen_dfa_table pass. Again, the typechecking part of
6904 // pass 2 is a good place for this.
6905 if (! session.need_tagged_dfa
6906 && s->tagged_p("/* pragma:tagged_dfa */"))
6907 {
6908 if (session.verbose > 2)
6909 clog << _("Turning on DFA subexpressions because /* pragma:tagged_dfa */ seen") << endl;
6910 session.need_tagged_dfa = true;
6911 // TODOXXX throw SEMANTIC_ERROR (_("Tagged DFA support is not yet available"), s->tok);
6912 }
6913 }
6914
6915
6916 void
6917 typeresolution_info::visit_if_statement (if_statement* e)
6918 {
6919 t = pe_long;
6920 e->condition->visit (this);
6921
6922 t = pe_unknown;
6923 e->thenblock->visit (this);
6924
6925 if (e->elseblock)
6926 {
6927 t = pe_unknown;
6928 e->elseblock->visit (this);
6929 }
6930 }
6931
6932
6933 void
6934 typeresolution_info::visit_for_loop (for_loop* e)
6935 {
6936 t = pe_unknown;
6937 if (e->init) e->init->visit (this);
6938 t = pe_long;
6939 e->cond->visit (this);
6940 t = pe_unknown;
6941 if (e->incr) e->incr->visit (this);
6942 t = pe_unknown;
6943 e->block->visit (this);
6944 }
6945
6946
6947 void
6948 typeresolution_info::visit_foreach_loop (foreach_loop* e)
6949 {
6950 // See also visit_arrayindex.
6951 // This is different in that, being a statement, we can't assign
6952 // a type to the outer array, only propagate to/from the indexes
6953
6954 // if (e->referent->index_types.size() == 0)
6955 // // redesignate referent as array
6956 // e->referent->set_arity (e->indexes.size ());
6957
6958 exp_type wanted_value = pe_unknown;
6959 symbol *array = NULL;
6960 hist_op *hist = NULL;
6961 classify_indexable(e->base, array, hist);
6962
6963 if (hist)
6964 {
6965 if (e->indexes.size() != 1)
6966 unresolved (e->tok);
6967 t = pe_long;
6968 e->indexes[0]->visit (this);
6969 if (e->indexes[0]->type != pe_long)
6970 unresolved (e->tok);
6971 hist->visit (this);
6972 wanted_value = pe_long;
6973 }
6974 else
6975 {
6976 assert (array);
6977 if (e->indexes.size() != array->referent->index_types.size())
6978 unresolved (e->tok); // symbol resolution should prevent this
6979 else
6980 {
6981 for (unsigned i=0; i<e->indexes.size(); i++)
6982 {
6983 expression* ee = e->indexes[i];
6984 exp_type& ft = array->referent->index_types [i];
6985 t = ft;
6986 ee->visit (this);
6987 exp_type at = ee->type;
6988
6989 if ((at == pe_string || at == pe_long) && ft == pe_unknown)
6990 {
6991 // propagate to formal type
6992 ft = at;
6993 resolved (ee->tok, ee->type, array->referent, i);
6994 }
6995 if (at == pe_stats)
6996 invalid (ee->tok, at);
6997 if (ft == pe_stats)
6998 invalid (ee->tok, ft);
6999 if (at != pe_unknown && ft != pe_unknown && ft != at)
7000 mismatch (ee->tok, ee->type, array->referent, i);
7001 if (at == pe_unknown)
7002 unresolved (ee->tok);
7003 }
7004 for (unsigned i=0; i<e->array_slice.size(); i++)
7005 if (e->array_slice[i])
7006 {
7007 expression* ee = e->array_slice[i];
7008 exp_type& ft = array->referent->index_types [i];
7009 t = ft;
7010 ee->visit (this);
7011 exp_type at = ee->type;
7012
7013 if ((at == pe_string || at == pe_long) && ft == pe_unknown)
7014 {
7015 // propagate to formal type
7016 ft = at;
7017 resolved (ee->tok, ee->type, array->referent, i);
7018 }
7019 if (at == pe_stats)
7020 invalid (ee->tok, at);
7021 if (ft == pe_stats)
7022 invalid (ee->tok, ft);
7023 if (at != pe_unknown && ft != pe_unknown && ft != at)
7024 mismatch (ee->tok, ee->type, array->referent, i);
7025 if (at == pe_unknown)
7026 unresolved (ee->tok);
7027 }
7028 }
7029 t = pe_unknown;
7030 array->visit (this);
7031 wanted_value = array->type;
7032 }
7033
7034 if (e->value)
7035 {
7036 if (wanted_value == pe_stats)
7037 invalid(e->value->tok, wanted_value);
7038 else if (wanted_value != pe_unknown)
7039 check_arg_type(wanted_value, e->value);
7040 else
7041 {
7042 t = pe_unknown;
7043 e->value->visit (this);
7044 }
7045 }
7046
7047 /* Prevent @sum etc. aggregate sorting on non-statistics arrays. */
7048 if (wanted_value != pe_unknown)
7049 if (e->sort_aggr != sc_none && wanted_value != pe_stats)
7050 invalid (array->tok, wanted_value);
7051
7052 if (e->limit)
7053 {
7054 t = pe_long;
7055 e->limit->visit (this);
7056 }
7057
7058 t = pe_unknown;
7059 e->block->visit (this);
7060 }
7061
7062
7063 void
7064 typeresolution_info::visit_null_statement (null_statement*)
7065 {
7066 }
7067
7068
7069 void
7070 typeresolution_info::visit_expr_statement (expr_statement* e)
7071 {
7072 t = pe_unknown;
7073 e->value->visit (this);
7074 }
7075
7076
7077 struct delete_statement_typeresolution_info:
7078 public throwing_visitor
7079 {
7080 typeresolution_info *parent;
7081 delete_statement_typeresolution_info (typeresolution_info *p):
7082 throwing_visitor (_("invalid operand of delete expression")),
7083 parent (p)
7084 {}
7085
7086 void visit_arrayindex (arrayindex* e)
7087 {
7088 parent->visit_arrayindex (e);
7089 }
7090
7091 void visit_symbol (symbol* e)
7092 {
7093 exp_type ignored = pe_unknown;
7094 assert (e->referent != 0);
7095 resolve_2types (e, e->referent, parent, ignored);
7096 }
7097 };
7098
7099
7100 void
7101 typeresolution_info::visit_delete_statement (delete_statement* e)
7102 {
7103 delete_statement_typeresolution_info di (this);
7104 t = pe_unknown;
7105 e->value->visit (&di);
7106 }
7107
7108
7109 void
7110 typeresolution_info::visit_next_statement (next_statement*)
7111 {
7112 }
7113
7114
7115 void
7116 typeresolution_info::visit_break_statement (break_statement*)
7117 {
7118 }
7119
7120
7121 void
7122 typeresolution_info::visit_continue_statement (continue_statement*)
7123 {
7124 }
7125
7126
7127 void
7128 typeresolution_info::visit_array_in (array_in* e)
7129 {
7130 // all unary operators only work on numerics
7131 exp_type t1 = t;
7132 t = pe_unknown; // array value can be anything
7133 e->operand->visit (this);
7134
7135 if (t1 == pe_unknown && e->type != pe_unknown)
7136 ; // already resolved
7137 else if (t1 == pe_string || t1 == pe_stats)
7138 mismatch (e->tok, t1, pe_long);
7139 else if (e->type == pe_unknown)
7140 {
7141 e->type = pe_long;
7142 resolved (e->tok, e->type);
7143 }
7144 }
7145
7146
7147 void
7148 typeresolution_info::visit_return_statement (return_statement* e)
7149 {
7150 // This is like symbol, where the referent is
7151 // the return value of the function.
7152
7153 // translation pass will print error
7154 if (current_function == 0)
7155 return;
7156
7157 exp_type& e_type = current_function->type;
7158 t = current_function->type;
7159
7160 if (!e->value)
7161 {
7162 if (e_type != pe_unknown)
7163 mismatch (e->tok, pe_unknown, current_function);
7164 return;
7165 }
7166
7167 e->value->visit (this);
7168
7169 if (e_type != pe_unknown && e->value->type != pe_unknown
7170 && e_type != e->value->type)
7171 mismatch (e->value->tok, e->value->type, current_function);
7172 if (e_type == pe_unknown &&
7173 (e->value->type == pe_long || e->value->type == pe_string))
7174 {
7175 // propagate non-statistics from value
7176 e_type = e->value->type;
7177 resolved (e->value->tok, e_type, current_function);
7178 }
7179 if (e->value->type == pe_stats)
7180 invalid (e->value->tok, e->value->type);
7181
7182 const exp_type_ptr& value_type = e->value->type_details;
7183 if (value_type && current_function->type == e->value->type)
7184 {
7185 exp_type_ptr& func_type = current_function->type_details;
7186 if (!func_type)
7187 // The function can take on the type details of the return value.
7188 resolved_details(value_type, func_type);
7189 else if (*func_type != *value_type && *func_type != *null_type)
7190 // Conflicting return types? NO TYPE FOR YOU!
7191 resolved_details(null_type, func_type);
7192 }
7193 }
7194
7195 void
7196 typeresolution_info::visit_print_format (print_format* e)
7197 {
7198 size_t unresolved_args = 0;
7199
7200 if (e->hist)
7201 {
7202 e->hist->visit(this);
7203 }
7204
7205 else if (e->print_with_format)
7206 {
7207 // If there's a format string, we can do both inference *and*
7208 // checking.
7209
7210 // First we extract the subsequence of formatting components
7211 // which are conversions (not just literal string components)
7212
7213 unsigned expected_num_args = 0;
7214 std::vector<print_format::format_component> components;
7215 for (size_t i = 0; i < e->components.size(); ++i)
7216 {
7217 if (e->components[i].type == print_format::conv_unspecified)
7218 throw SEMANTIC_ERROR (_("Unspecified conversion in print operator format string"),
7219 e->tok);
7220 else if (e->components[i].type == print_format::conv_literal)
7221 continue;
7222 components.push_back(e->components[i]);
7223 ++expected_num_args;
7224 if (e->components[i].widthtype == print_format::width_dynamic)
7225 ++expected_num_args;
7226 if (e->components[i].prectype == print_format::prec_dynamic)
7227 ++expected_num_args;
7228 }
7229
7230 // Then we check that the number of conversions and the number
7231 // of args agree.
7232
7233 if (expected_num_args != e->args.size())
7234 throw SEMANTIC_ERROR (_("Wrong number of args to formatted print operator"),
7235 e->tok);
7236
7237 // Then we check that the types of the conversions match the types
7238 // of the args.
7239 unsigned argno = 0;
7240 for (size_t i = 0; i < components.size(); ++i)
7241 {
7242 // Check the dynamic width, if specified
7243 if (components[i].widthtype == print_format::width_dynamic)
7244 {
7245 check_arg_type (pe_long, e->args[argno]);
7246 ++argno;
7247 }
7248
7249 // Check the dynamic precision, if specified
7250 if (components[i].prectype == print_format::prec_dynamic)
7251 {
7252 check_arg_type (pe_long, e->args[argno]);
7253 ++argno;
7254 }
7255
7256 exp_type wanted = pe_unknown;
7257
7258 switch (components[i].type)
7259 {
7260 case print_format::conv_unspecified:
7261 case print_format::conv_literal:
7262 assert (false);
7263 break;
7264
7265 case print_format::conv_pointer:
7266 case print_format::conv_number:
7267 case print_format::conv_binary:
7268 case print_format::conv_char:
7269 case print_format::conv_memory:
7270 case print_format::conv_memory_hex:
7271 wanted = pe_long;
7272 break;
7273
7274 case print_format::conv_string:
7275 wanted = pe_string;
7276 break;
7277 }
7278
7279 assert (wanted != pe_unknown);
7280 check_arg_type (wanted, e->args[argno]);
7281 ++argno;
7282 }
7283 }
7284 else
7285 {
7286 // Without a format string, the best we can do is require that
7287 // each argument resolve to a concrete type.
7288 for (size_t i = 0; i < e->args.size(); ++i)
7289 {
7290 t = pe_unknown;
7291 e->args[i]->visit (this);
7292 if (e->args[i]->type == pe_unknown)
7293 {
7294 unresolved (e->args[i]->tok);
7295 ++unresolved_args;
7296 }
7297 }
7298 }
7299
7300 if (unresolved_args == 0)
7301 {
7302 if (e->type == pe_unknown)
7303 {
7304 if (e->print_to_stream)
7305 e->type = pe_long;
7306 else
7307 e->type = pe_string;
7308 resolved (e->tok, e->type);
7309 }
7310 }
7311 else
7312 {
7313 e->type = pe_unknown;
7314 unresolved (e->tok);
7315 }
7316 }
7317
7318
7319 void
7320 typeresolution_info::visit_stat_op (stat_op* e)
7321 {
7322 t = pe_stats;
7323 e->stat->visit (this);
7324 if (e->type == pe_unknown)
7325 {
7326 e->type = pe_long;
7327 resolved (e->tok, e->type);
7328 }
7329 else if (e->type != pe_long)
7330 mismatch (e->tok, pe_long, e->type);
7331 }
7332
7333 void
7334 typeresolution_info::visit_hist_op (hist_op* e)
7335 {
7336 t = pe_stats;
7337 e->stat->visit (this);
7338 }
7339
7340
7341 void
7342 typeresolution_info::check_arg_type (exp_type wanted, expression* arg)
7343 {
7344 t = wanted;
7345 arg->visit (this);
7346
7347 if (arg->type == pe_unknown)
7348 {
7349 arg->type = wanted;
7350 resolved (arg->tok, arg->type);
7351 }
7352 else if (arg->type != wanted)
7353 {
7354 mismatch (arg->tok, wanted, arg->type);
7355 }
7356 }
7357
7358
7359 void
7360 typeresolution_info::check_local (vardecl* v)
7361 {
7362 if (v->arity != 0)
7363 {
7364 num_still_unresolved ++;
7365 if (assert_resolvability)
7366 session.print_error
7367 (SEMANTIC_ERROR (_("array locals not supported, missing global declaration? "), v->tok));
7368 }
7369
7370 if (v->type == pe_unknown)
7371 unresolved (v->tok);
7372 else if (v->type == pe_stats)
7373 {
7374 num_still_unresolved ++;
7375 if (assert_resolvability)
7376 session.print_error
7377 (SEMANTIC_ERROR (_("stat locals not supported, missing global declaration? "), v->tok));
7378 }
7379 else if (!(v->type == pe_long || v->type == pe_string))
7380 invalid (v->tok, v->type);
7381 }
7382
7383
7384 void
7385 typeresolution_info::unresolved (const token* tok)
7386 {
7387 num_still_unresolved ++;
7388
7389 if (assert_resolvability && mismatch_complexity <= 0)
7390 {
7391 stringstream msg;
7392 msg << _("unresolved type ");
7393 session.print_error (SEMANTIC_ERROR (msg.str(), tok));
7394 }
7395 }
7396
7397
7398 void
7399 typeresolution_info::invalid (const token* tok, exp_type pe)
7400 {
7401 num_still_unresolved ++;
7402
7403 if (assert_resolvability)
7404 {
7405 stringstream msg;
7406 if (tok && tok->type == tok_operator)
7407 msg << _("invalid operator");
7408 else
7409 msg << _("invalid type ") << pe;
7410 session.print_error (SEMANTIC_ERROR (msg.str(), tok));
7411 }
7412 }
7413
7414 void
7415 typeresolution_info::mismatch (const binary_expression* e)
7416 {
7417 num_still_unresolved ++;
7418
7419 if (assert_resolvability && mismatch_complexity <= 1)
7420 {
7421 stringstream msg;
7422 msg << _F("type mismatch: left and right sides don't agree (%s vs %s)",
7423 lex_cast(e->left->type).c_str(), lex_cast(e->right->type).c_str());
7424 session.print_error (SEMANTIC_ERROR (msg.str(), e->tok));
7425 }
7426 else if (!assert_resolvability)
7427 mismatch_complexity = max(1, mismatch_complexity);
7428 }
7429
7430 /* tok token where mismatch occurred
7431 * t1 type we expected (the 'good' type)
7432 * t2 type we received (the 'bad' type)
7433 * */
7434 void
7435 typeresolution_info::mismatch (const token* tok, exp_type t1, exp_type t2)
7436 {
7437 num_still_unresolved ++;
7438
7439 if (assert_resolvability && mismatch_complexity <= 2)
7440 {
7441 stringstream msg;
7442 msg << _F("type mismatch: expected %s", lex_cast(t1).c_str());
7443 if (t2 != pe_unknown)
7444 msg << _F(" but found %s", lex_cast(t2).c_str());
7445 session.print_error (SEMANTIC_ERROR (msg.str(), tok));
7446 }
7447 else if (!assert_resolvability)
7448 mismatch_complexity = max(2, mismatch_complexity);
7449 }
7450
7451 /* tok token where the mismatch happened
7452 * type type we received (the 'bad' type)
7453 * decl declaration of mismatched symbol
7454 * index if index-based (array index or function arg)
7455 * */
7456 void
7457 typeresolution_info::mismatch (const token *tok, exp_type type,
7458 const symboldecl* decl, int index)
7459 {
7460 num_still_unresolved ++;
7461
7462 if (assert_resolvability && mismatch_complexity <= 3)
7463 {
7464 assert(decl != NULL);
7465
7466 // If mismatch is against a function parameter from within the function
7467 // itself (rather than a function call), then the index will be -1. We
7468 // check here if the decl corresponds to one of the params and if so,
7469 // adjust the index.
7470 if (current_function != NULL && index == -1)
7471 {
7472 vector<vardecl*>& args = current_function->formal_args;
7473 for (unsigned i = 0; i < args.size() && index < 0; i++)
7474 if (args[i] == decl)
7475 index = i;
7476 }
7477
7478 // get the declaration's original type and token
7479 const resolved_type *original = NULL;
7480 for (vector<resolved_type>::const_iterator it = resolved_types.begin();
7481 it != resolved_types.end() && original == NULL; ++it)
7482 {
7483 if (it->decl == decl && it->index == index)
7484 original = &(*it);
7485 }
7486
7487 // print basic mismatch msg if we couldn't find the decl (this can happen
7488 // for explicitly typed decls e.g. myvar:long or for fabricated (already
7489 // resolved) decls e.g. __perf_read_*)
7490 if (original == NULL)
7491 {
7492 session.print_error (SEMANTIC_ERROR (
7493 _F("type mismatch: expected %s but found %s",
7494 lex_cast(type).c_str(),
7495 lex_cast(decl->type).c_str()),
7496 tok));
7497 return;
7498 }
7499
7500 // print where mismatch happened and chain with origin of decl type
7501 // resolution
7502 stringstream msg;
7503
7504 if (index >= 0)
7505 msg << _F("index %d ", index);
7506 msg << _F("type mismatch (%s)", lex_cast(type).c_str());
7507 semantic_error err(ERR_SRC, msg.str(), tok);
7508
7509 stringstream chain_msg;
7510 chain_msg << _("type");
7511 if (index >= 0)
7512 chain_msg << _F(" of index %d", index);
7513 chain_msg << _F(" was first inferred here (%s)",
7514 lex_cast(decl->type).c_str());
7515 semantic_error chain(ERR_SRC, chain_msg.str(), original->tok);
7516
7517 err.set_chain(chain);
7518 session.print_error (err);
7519 }
7520 else if (!assert_resolvability)
7521 mismatch_complexity = max(3, mismatch_complexity);
7522 }
7523
7524
7525 /* tok token where resolution occurred
7526 * type type to which we resolved
7527 * decl declaration of resolved symbol
7528 * index if index-based (array index or function arg)
7529 * */
7530 void
7531 typeresolution_info::resolved (const token *tok, exp_type t,
7532 const symboldecl* decl, int index)
7533 {
7534 num_newly_resolved ++;
7535
7536 if (session.verbose > 4)
7537 clog << "resolved type " << t << " to " << *tok << endl;
7538
7539 // We only use the resolved_types vector to give better mismatch messages
7540 // involving symbols. So don't bother adding it if we're not given a decl
7541 if (decl != NULL)
7542 {
7543 // As a fail-safe, if the decl & index is already in the vector, then
7544 // modify it instead of adding another one to ensure uniqueness. This
7545 // should never happen since we only call resolved once for each decl &
7546 // index, but better safe than sorry. (IE. if it does happen, better have
7547 // the latest resolution info for better mismatch reporting later).
7548 for (unsigned i = 0; i < resolved_types.size(); i++)
7549 {
7550 if (resolved_types[i].decl == decl
7551 && resolved_types[i].index == index)
7552 {
7553 resolved_types[i].tok = tok;
7554 return;
7555 }
7556 }
7557 resolved_type res(tok, decl, index);
7558 resolved_types.push_back(res);
7559 }
7560 }
7561
7562 void
7563 typeresolution_info::resolved_details (const exp_type_ptr& src,
7564 exp_type_ptr& dest)
7565 {
7566 num_newly_resolved ++;
7567 dest = src;
7568 }
7569
7570 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.434223 seconds and 5 git commands to generate.