]> sourceware.org Git - systemtap.git/blob - translate.cxx
buildrun.cxx: adapt to kernel 5.4+
[systemtap.git] / translate.cxx
1 // translation pass
2 // Copyright (C) 2005-2019 Red Hat Inc.
3 // Copyright (C) 2005-2008 Intel Corporation.
4 // Copyright (C) 2010 Novell Corporation.
5 //
6 // This file is part of systemtap, and is free software. You can
7 // redistribute it and/or modify it under the terms of the GNU General
8 // Public License (GPL); either version 2, or (at your option) any
9 // later version.
10
11 #include "config.h"
12 #include "staptree.h"
13 #include "elaborate.h"
14 #include "translate.h"
15 #include "session.h"
16 #include "tapsets.h"
17 #include "util.h"
18 #include "dwarf_wrappers.h"
19 #include "setupdwfl.h"
20 #include "task_finder.h"
21 #include "runtime/k_syms.h"
22 #include "dwflpp.h"
23 #include "stapregex.h"
24 #include "stringtable.h"
25
26 #include <byteswap.h>
27 #include <cstdlib>
28 #include <iostream>
29 #include <set>
30 #include <sstream>
31 #include <string>
32 #include <cassert>
33 #include <cstring>
34 #include <cerrno>
35
36 extern "C" {
37 #include <dwarf.h>
38 #include <elfutils/libdwfl.h>
39 #include <elfutils/libdw.h>
40 #include <ftw.h>
41 #define __STDC_FORMAT_MACROS
42 #include <inttypes.h>
43 }
44
45 // Max unwind table size (debug or eh) per module. Somewhat arbitrary
46 // limit (a bit more than twice the .debug_frame size of my local
47 // vmlinux for 2.6.31.4-83.fc12.x86_64).
48 // A larger value was recently found in a libxul.so build.
49 // ... and yet again in libxul.so, PR15162
50 // ... and yet again w.r.t. oracle db in private communication, 25289196
51 #define MAX_UNWIND_TABLE_SIZE (32 * 1024 * 1024)
52
53 #define STAP_T_01 _("\"Array overflow, check ")
54 #define STAP_T_02 _("\"MAXNESTING exceeded\";")
55 #define STAP_T_03 _("\"division by 0\";")
56 #define STAP_T_04 _("\"MAXACTION exceeded\";")
57 #define STAP_T_05 _("\"aggregation overflow in ")
58 #define STAP_T_06 _("\"empty aggregate\";")
59 #define STAP_T_07 _("\"histogram index out of range\";")
60
61 using namespace std;
62
63 class var;
64 struct tmpvar;
65 struct aggvar;
66 struct mapvar;
67 class itervar;
68
69 // A null-sink output stream, similar to /dev/null
70 // (no buffer -> badbit -> quietly suppressed output)
71 static ostream nullstream(NULL);
72 static translator_output null_o(nullstream);
73
74 struct c_unparser: public unparser, public visitor
75 {
76 systemtap_session* session;
77 translator_output* o;
78
79 derived_probe* current_probe;
80 functiondecl* current_function;
81
82 const functioncall* assigned_functioncall;
83 const string* assigned_functioncall_retval;
84
85 unsigned tmpvar_counter;
86 unsigned label_counter;
87 unsigned action_counter;
88 unsigned fc_counter;
89 bool already_checked_action_count;
90
91 varuse_collecting_visitor vcv_needs_global_locks;
92
93 map<string, probe*> probe_contents;
94
95 map<pair<bool, string>, string> compiled_printfs;
96
97 c_unparser (systemtap_session* ss, translator_output* op=NULL):
98 session (ss), o (op ?: ss->op), current_probe(0), current_function (0),
99 assigned_functioncall (0), assigned_functioncall_retval (0),
100 tmpvar_counter (0), label_counter (0), action_counter(0), fc_counter(0),
101 already_checked_action_count(false), vcv_needs_global_locks (*ss) {}
102 ~c_unparser () {}
103
104 // The main c_unparser doesn't write declarations as it traverses,
105 // but the c_tmpcounter subclass will.
106 virtual void var_declare(string const&, var const&) {}
107
108 // If we've seen a dupe, return it; else remember this and return NULL.
109 probe *get_probe_dupe (derived_probe *dp);
110
111 void emit_map_type_instantiations ();
112 void emit_common_header ();
113 void emit_global (vardecl* v);
114 void emit_global_init (vardecl* v);
115 void emit_global_init_type (vardecl *v);
116 void emit_global_param (vardecl* v);
117 void emit_global_init_setters ();
118 void emit_functionsig (functiondecl* v);
119 void emit_kernel_module_init ();
120 void emit_kernel_module_exit ();
121 void emit_module_init ();
122 void emit_module_refresh ();
123 void emit_module_exit ();
124 void emit_function (functiondecl* v);
125 void emit_lock_decls (const varuse_collecting_visitor& v);
126 void emit_locks ();
127 void emit_probe (derived_probe* v);
128 void emit_probe_condition_update(derived_probe* v);
129 void emit_unlocks ();
130
131 void emit_compiled_printfs ();
132 void emit_compiled_printf_locals ();
133 void declare_compiled_printf (bool print_to_stream, const string& format);
134 virtual const string& get_compiled_printf (bool print_to_stream,
135 const string& format);
136
137 // for use by stats (pmap) foreach
138 set<string> aggregations_active;
139
140 // values immediately available in foreach_loop iterations
141 map<string, string> foreach_loop_values;
142 void visit_foreach_loop_value (foreach_loop* s, const string& value="");
143 bool get_foreach_loop_value (arrayindex* ai, string& value);
144
145 // for use by looping constructs
146 vector<string> loop_break_labels;
147 vector<string> loop_continue_labels;
148
149 string c_typename (exp_type e);
150 virtual string c_localname (const string& e, bool mangle_oldstyle = false);
151 virtual string c_globalname (const string &e);
152 virtual string c_funcname (const string &e);
153
154 string c_arg_define (const string& e);
155 string c_arg_undef (const string& e);
156
157 string map_keytypes(vardecl* v);
158 void c_global_write_def(vardecl* v);
159 void c_global_read_def(vardecl* v);
160 void c_global_write_undef(vardecl* v);
161 void c_global_read_undef(vardecl* v);
162
163 void c_assign (var& lvalue, const string& rvalue, const token* tok);
164 void c_assign (tmpvar& lvalue, expression* rvalue, const char* msg);
165 void c_assign (const string& lvalue, expression* rvalue, const char* msg);
166 void c_assign (const string& lvalue, const string& rvalue, exp_type type,
167 const char* msg, const token* tok);
168
169 void c_declare(exp_type ty, const string &ident);
170 void c_declare_static(exp_type ty, const string &ident);
171
172 void c_strcat (const string& lvalue, const string& rvalue);
173 void c_strcat (const string& lvalue, expression* rvalue);
174
175 void c_strcpy (const string& lvalue, const string& rvalue);
176 void c_strcpy (const string& lvalue, expression* rvalue);
177
178 bool is_local (vardecl const* r, token const* tok);
179
180 tmpvar gensym(exp_type ty);
181 aggvar gensym_aggregate();
182
183 var getvar(vardecl* v, token const* tok = NULL);
184 itervar getiter(symbol* s);
185 mapvar getmap(vardecl* v, token const* tok = NULL);
186
187 void load_map_indices(arrayindex* e,
188 vector<tmpvar> & idx);
189
190 var* load_aggregate (expression *e, aggvar & agg);
191 string histogram_index_check(var & vase, tmpvar & idx) const;
192
193 void collect_map_index_types(vector<vardecl* > const & vars,
194 set< pair<vector<exp_type>, exp_type> > & types);
195
196 void record_actions (unsigned actions, const token* tok, bool update=false);
197
198 void visit_block (block* s);
199 void visit_try_block (try_block* s);
200 void visit_embeddedcode (embeddedcode* s);
201 void visit_null_statement (null_statement* s);
202 void visit_expr_statement (expr_statement* s);
203 void visit_if_statement (if_statement* s);
204 void visit_for_loop (for_loop* s);
205 void visit_foreach_loop (foreach_loop* s);
206 void visit_return_statement (return_statement* s);
207 void visit_delete_statement (delete_statement* s);
208 void visit_next_statement (next_statement* s);
209 void visit_break_statement (break_statement* s);
210 void visit_continue_statement (continue_statement* s);
211 void visit_literal_string (literal_string* e);
212 void visit_literal_number (literal_number* e);
213 void visit_embedded_expr (embedded_expr* e);
214 void visit_binary_expression (binary_expression* e);
215 void visit_unary_expression (unary_expression* e);
216 void visit_pre_crement (pre_crement* e);
217 void visit_post_crement (post_crement* e);
218 void visit_logical_or_expr (logical_or_expr* e);
219 void visit_logical_and_expr (logical_and_expr* e);
220 void visit_array_in (array_in* e);
221 void visit_regex_query (regex_query* e);
222 void visit_compound_expression(compound_expression* e);
223 void visit_comparison (comparison* e);
224 void visit_concatenation (concatenation* e);
225 void visit_ternary_expression (ternary_expression* e);
226 void visit_assignment (assignment* e);
227 void visit_symbol (symbol* e);
228 void visit_target_register (target_register* e);
229 void visit_target_deref (target_deref* e);
230 void visit_target_bitfield (target_bitfield* e);
231 void visit_target_symbol (target_symbol* e);
232 void visit_arrayindex (arrayindex* e);
233 void visit_functioncall (functioncall* e);
234 void visit_print_format (print_format* e);
235 void visit_stat_op (stat_op* e);
236 void visit_hist_op (hist_op* e);
237 void visit_cast_op (cast_op* e);
238 void visit_autocast_op (autocast_op* e);
239 void visit_atvar_op (atvar_op* e);
240 void visit_defined_op (defined_op* e);
241 void visit_entry_op (entry_op* e);
242 void visit_perf_op (perf_op* e);
243
244 // start/close statements with multiple independent child visits
245 virtual void start_compound_statement (const char*, statement*) { }
246 virtual void close_compound_statement (const char*, statement*) { }
247
248 // wrap one child visit of a compound statement
249 virtual void wrap_compound_visit (expression *e) { if (e) e->visit (this); }
250 virtual void wrap_compound_visit (statement *s) { if (s) s->visit (this); }
251 };
252
253 // A shadow visitor, meant to generate temporary variable declarations
254 // for function or probe bodies. The output is discarded, but we now do
255 // real work in var_declare().
256 struct c_tmpcounter cxx_final: public c_unparser
257 {
258 c_unparser* parent;
259 set<string> declared_vars;
260
261 c_tmpcounter (c_unparser* p):
262 c_unparser(p->session, &null_o), parent (p)
263 { }
264
265 // When vars are created *and used* (i.e. not overridden tmpvars) they call
266 // var_declare(), which will forward to the parent c_unparser for output;
267 void var_declare(string const&, var const& v) cxx_override;
268
269 void emit_function (functiondecl* fd);
270 void emit_probe (derived_probe* dp);
271
272 const string& get_compiled_printf (bool print_to_stream,
273 const string& format) cxx_override;
274
275 void start_compound_statement (const char*, statement*) cxx_override;
276 void close_compound_statement (const char*, statement*) cxx_override;
277
278 void wrap_compound_visit (expression *e) cxx_override;
279 void wrap_compound_visit (statement *s) cxx_override;
280
281 void start_struct_def (std::ostream::pos_type &before,
282 std::ostream::pos_type &after, const token* tok);
283 void close_struct_def (std::ostream::pos_type before,
284 std::ostream::pos_type after);
285 };
286
287 struct c_unparser_assignment:
288 public throwing_visitor
289 {
290 c_unparser* parent;
291 interned_string op;
292 expression* rvalue;
293 bool post; // true == value saved before modify operator
294 c_unparser_assignment (c_unparser* p, interned_string o, expression* e):
295 throwing_visitor ("invalid lvalue type"),
296 parent (p), op (o), rvalue (e), post (false) {}
297 c_unparser_assignment (c_unparser* p, interned_string o, bool pp):
298 throwing_visitor ("invalid lvalue type"),
299 parent (p), op (o), rvalue (0), post (pp) {}
300
301 void prepare_rvalue (interned_string op,
302 tmpvar & rval,
303 token const* tok);
304
305 void c_assignop(tmpvar & res,
306 var const & lvar,
307 tmpvar const & tmp,
308 token const* tok);
309
310 // The set of valid lvalues are limited.
311 void visit_symbol (symbol* e);
312 void visit_target_register (target_register* e);
313 void visit_target_deref (target_deref* e);
314 void visit_arrayindex (arrayindex* e);
315 };
316
317
318 ostream & operator<<(ostream & o, var const & v);
319
320
321 /*
322 Some clarification on the runtime structures involved in statistics:
323
324 The basic type for collecting statistics in the runtime is struct
325 stat_data. This contains the count, min, max, sum, and possibly
326 histogram fields.
327
328 There are two places struct stat_data shows up.
329
330 1. If you declare a statistic variable of any sort, you want to make
331 a struct _Stat. A struct _Stat* is also called a Stat. Struct _Stat
332 contains a per-CPU array of struct stat_data values, as well as a
333 struct stat_data which it aggregates into. Writes into a Struct
334 _Stat go into the per-CPU struct stat. Reads involve write-locking
335 the struct _Stat, aggregating into its aggregate struct stat_data,
336 unlocking, read-locking the struct _Stat, then reading values out of
337 the aggregate and unlocking.
338
339 2. If you declare a statistic-valued map, you want to make a
340 pmap. This is a per-CPU array of maps, each of which holds struct
341 stat_data values, as well as an aggregate *map*. Writes into a pmap
342 go into the per-CPU map. Reads involve write-locking the pmap,
343 aggregating into its aggregate map, unlocking, read-locking the
344 pmap, then reading values out of its aggregate (which is a normal
345 map) and unlocking.
346
347 Because, at the moment, the runtime does not support the concept of
348 a statistic which collects multiple histogram types, we may need to
349 instantiate one pmap or struct _Stat for each histogram variation
350 the user wants to track.
351 */
352
353 class var
354 {
355
356 protected:
357 // Required for accurate mangling:
358 c_unparser *u;
359
360 bool local;
361 exp_type ty;
362 statistic_decl sd;
363 string name;
364 bool do_mangle;
365
366 private:
367 mutable bool declaration_needed;
368
369 public:
370
371 var(c_unparser *u, bool local, exp_type ty,
372 statistic_decl const & sd, string const & name)
373 : u(u), local(local), ty(ty), sd(sd), name(name),
374 do_mangle(true), declaration_needed(false)
375 {}
376
377 var(c_unparser *u, bool local, exp_type ty, string const & name)
378 : u(u), local(local), ty(ty), name(name),
379 do_mangle(true), declaration_needed(false)
380 {}
381
382 var(c_unparser *u, bool local, exp_type ty,
383 string const & name, bool do_mangle)
384 : u(u), local(local), ty(ty), name(name),
385 do_mangle(do_mangle), declaration_needed(false)
386 {}
387
388 var(c_unparser *u, bool local, exp_type ty, unsigned & counter)
389 : u(u), local(local), ty(ty), name("__tmp" + lex_cast(counter++)),
390 do_mangle(false), declaration_needed(true)
391 {}
392
393 virtual ~var() {}
394
395 bool is_local() const
396 {
397 return local;
398 }
399
400 statistic_decl const & sdecl() const
401 {
402 return sd;
403 }
404
405 void assert_hist_compatible(hist_op const & hop)
406 {
407 // Semantic checks in elaborate should have caught this if it was
408 // false. This is just a double-check.
409 switch (sd.type)
410 {
411 case statistic_decl::linear:
412 assert(hop.htype == hist_linear);
413 assert(hop.params.size() == 3);
414 assert(hop.params[0] == sd.linear_low);
415 assert(hop.params[1] == sd.linear_high);
416 assert(hop.params[2] == sd.linear_step);
417 break;
418 case statistic_decl::logarithmic:
419 assert(hop.htype == hist_log);
420 assert(hop.params.size() == 0);
421 break;
422 case statistic_decl::none:
423 assert(false);
424 }
425 }
426
427 exp_type type() const
428 {
429 return ty;
430 }
431
432 string c_name() const
433 {
434 if (!do_mangle)
435 return name;
436 else if (local)
437 return u->c_localname(name);
438 else
439 return u->c_globalname(name);
440 }
441
442 string stat_op_tokens() const
443 {
444 string result = "";
445 if (sd.stat_ops & STAT_OP_COUNT)
446 result += "STAT_OP_COUNT, ";
447 if (sd.stat_ops & STAT_OP_SUM)
448 result += "STAT_OP_SUM, ";
449 if (sd.stat_ops & STAT_OP_MIN)
450 result += "STAT_OP_MIN, ";
451 if (sd.stat_ops & STAT_OP_MAX)
452 result += "STAT_OP_MAX, ";
453 if (sd.stat_ops & STAT_OP_AVG)
454 result += "STAT_OP_AVG, ";
455 if (sd.stat_ops & STAT_OP_VARIANCE)
456 result += "STAT_OP_VARIANCE, " + lex_cast(sd.bit_shift) + ", ";
457
458 return result;
459 }
460
461 string value() const
462 {
463 if (declaration_needed)
464 {
465 u->var_declare (name, *this);
466 declaration_needed = false;
467 }
468
469 if (local)
470 return "l->" + c_name();
471 else
472 return "global(" + c_name() + ")";
473 }
474
475 virtual string hist() const
476 {
477 assert (ty == pe_stats);
478 assert (sd.type != statistic_decl::none);
479 return "(&(" + value() + "->hist))";
480 }
481
482 virtual string buckets() const
483 {
484 assert (ty == pe_stats);
485 assert (sd.type != statistic_decl::none);
486 return "(" + value() + "->hist.buckets)";
487 }
488
489 string init() const
490 {
491 switch (type())
492 {
493 case pe_string:
494 if (! local)
495 return ""; // module_param
496 else
497 return value() + "[0] = '\\0';";
498 case pe_long:
499 if (! local)
500 return ""; // module_param
501 else
502 return value() + " = 0;";
503 case pe_stats:
504 {
505 // See also mapvar::init().
506
507 if (local)
508 throw SEMANTIC_ERROR(_F("unsupported local stats init for %s", value().c_str()));
509
510 string prefix = "global_set(" + c_name() + ", _stp_stat_init (" + stat_op_tokens();
511 // Check for errors during allocation.
512 string suffix = "if (" + value () + " == NULL) rc = -ENOMEM;";
513
514 switch (sd.type)
515 {
516 case statistic_decl::none:
517 prefix += string("KEY_HIST_TYPE, HIST_NONE, ");
518 break;
519
520 case statistic_decl::linear:
521 prefix += string("KEY_HIST_TYPE, HIST_LINEAR, ")
522 + lex_cast(sd.linear_low) + ", "
523 + lex_cast(sd.linear_high) + ", "
524 + lex_cast(sd.linear_step) + ", ";
525 break;
526
527 case statistic_decl::logarithmic:
528 prefix += string("KEY_HIST_TYPE, HIST_LOG, ");
529 break;
530
531 default:
532 throw SEMANTIC_ERROR(_F("unsupported stats type for %s", value().c_str()));
533 }
534
535 prefix += "NULL";
536 prefix = prefix + ")); ";
537 return string (prefix + suffix);
538 }
539
540 default:
541 throw SEMANTIC_ERROR(_F("unsupported initializer for %s", value().c_str()));
542 }
543 }
544
545 string fini () const
546 {
547 switch (type())
548 {
549 case pe_string:
550 case pe_long:
551 return ""; // no action required
552 case pe_stats:
553 return "_stp_stat_del (" + value () + ");";
554 default:
555 throw SEMANTIC_ERROR(_F("unsupported deallocator for %s", value().c_str()));
556 }
557 }
558
559 virtual void declare(c_unparser &c) const
560 {
561 c.c_declare(ty, c_name());
562 }
563 };
564
565 ostream & operator<<(ostream & o, var const & v)
566 {
567 return o << v.value();
568 }
569
570 void
571 c_tmpcounter::var_declare (string const& name, var const& v)
572 {
573 if (declared_vars.insert(name).second)
574 v.declare (*parent);
575 }
576
577 struct stmt_expr
578 {
579 c_unparser & c;
580 stmt_expr(c_unparser & c) : c(c)
581 {
582 c.o->newline() << "({";
583 c.o->indent(1);
584 }
585 ~stmt_expr()
586 {
587 c.o->newline(-1) << "})";
588 }
589 };
590
591
592 struct tmpvar
593 : public var
594 {
595 protected:
596 bool overridden;
597 string override_value;
598
599 public:
600 tmpvar(c_unparser *u, exp_type ty, unsigned & counter)
601 : var(u, true, ty, counter),
602 overridden(false)
603 {}
604
605 tmpvar(const var& source)
606 : var(source), overridden(false)
607 {}
608
609 void override(const string &value)
610 {
611 overridden = true;
612 override_value = value;
613 }
614
615 bool is_overridden()
616 {
617 return overridden;
618 }
619
620 string value() const
621 {
622 if (overridden)
623 return override_value;
624 else
625 return var::value();
626 }
627 };
628
629 ostream & operator<<(ostream & o, tmpvar const & v)
630 {
631 return o << v.value();
632 }
633
634 struct aggvar
635 : public var
636 {
637 aggvar(c_unparser *u, unsigned & counter)
638 : var(u, true, pe_stats, counter)
639 {}
640
641 string init() const
642 {
643 assert (type() == pe_stats);
644 return value() + " = NULL;";
645 }
646
647 void declare(c_unparser &c) const cxx_override
648 {
649 assert (type() == pe_stats);
650 c.o->newline() << "struct stat_data *" << name << ";";
651 }
652
653 string get_hist (var& index) const
654 {
655 return "(" + value() + "->histogram[" + index.value() + "])";
656 }
657 };
658
659 struct mapvar
660 : public var
661 {
662 vector<exp_type> index_types;
663 int maxsize;
664 bool wrap;
665 mapvar (c_unparser *u,
666 bool local, exp_type ty,
667 statistic_decl const & sd,
668 string const & name,
669 vector<exp_type> const & index_types,
670 int maxsize, bool wrap)
671 : var (u, local, ty, sd, name),
672 index_types (index_types),
673 maxsize (maxsize), wrap(wrap)
674 {}
675
676 static string shortname(exp_type e);
677 static string key_typename(exp_type e);
678 static string value_typename(exp_type e);
679
680 string keysym () const
681 {
682 string result;
683 vector<exp_type> tmp = index_types;
684 tmp.push_back (type ());
685 for (unsigned i = 0; i < tmp.size(); ++i)
686 {
687 switch (tmp[i])
688 {
689 case pe_long:
690 result += 'i';
691 break;
692 case pe_string:
693 result += 's';
694 break;
695 case pe_stats:
696 result += 'x';
697 break;
698 default:
699 throw SEMANTIC_ERROR(_("unknown type of map"));
700 break;
701 }
702 }
703 return result;
704 }
705
706 string function_keysym(string const & fname, bool pre_agg=false) const
707 {
708 string mtype = (is_parallel() && !pre_agg) ? "pmap" : "map";
709 string result = "_stp_" + mtype + "_" + fname + "_" + keysym();
710 return result;
711 }
712
713 string call_prefix (string const & fname, vector<tmpvar> const & indices, bool pre_agg=false) const
714 {
715 string result = function_keysym(fname, pre_agg) + " (";
716 result += pre_agg? fetch_existing_aggregate() : value();
717 for (unsigned i = 0; i < indices.size(); ++i)
718 {
719 if (indices[i].type() != index_types[i])
720 throw SEMANTIC_ERROR(_("index type mismatch"));
721 result += ", ";
722 result += indices[i].value();
723 }
724
725 return result;
726 }
727
728 bool is_parallel() const
729 {
730 return type() == pe_stats;
731 }
732
733 string stat_op_tokens() const
734 {
735 string result = "";
736 if (sd.stat_ops & STAT_OP_COUNT)
737 result += "STAT_OP_COUNT, ";
738 if (sd.stat_ops & STAT_OP_SUM)
739 result += "STAT_OP_SUM, ";
740 if (sd.stat_ops & STAT_OP_MIN)
741 result += "STAT_OP_MIN, ";
742 if (sd.stat_ops & STAT_OP_MAX)
743 result += "STAT_OP_MAX, ";
744 if (sd.stat_ops & STAT_OP_AVG)
745 result += "STAT_OP_AVG, ";
746 if (sd.stat_ops & STAT_OP_VARIANCE)
747 result += "STAT_OP_VARIANCE, " + lex_cast(sd.bit_shift) + ", ";
748
749 return result;
750 }
751
752 string stat_op_parms() const
753 {
754 string result = "";
755 result += (sd.stat_ops & (STAT_OP_COUNT|STAT_OP_AVG|STAT_OP_VARIANCE)) ? "1, " : "0, ";
756 result += (sd.stat_ops & (STAT_OP_SUM|STAT_OP_AVG|STAT_OP_VARIANCE)) ? "1, " : "0, ";
757 result += (sd.stat_ops & STAT_OP_MIN) ? "1, " : "0, ";
758 result += (sd.stat_ops & STAT_OP_MAX) ? "1, " : "0, ";
759 result += (sd.stat_ops & STAT_OP_VARIANCE) ? "1" : "0";
760 return result;
761 }
762
763 string calculate_aggregate() const
764 {
765 if (!is_parallel())
766 throw SEMANTIC_ERROR(_("aggregating non-parallel map type"));
767
768 return function_keysym("agg") + " (" + value() + ")";
769 }
770
771 string fetch_existing_aggregate() const
772 {
773 if (!is_parallel())
774 throw SEMANTIC_ERROR(_("fetching aggregate of non-parallel map type"));
775
776 return "_stp_pmap_get_agg(" + value() + ")";
777 }
778
779 string del (vector<tmpvar> const & indices) const
780 {
781 return (call_prefix("del", indices) + ")");
782 }
783
784 string exists (vector<tmpvar> const & indices) const
785 {
786 if (type() == pe_long || type() == pe_string)
787 return (call_prefix("exists", indices) + ")");
788 else if (type() == pe_stats)
789 return ("((uintptr_t)" + call_prefix("get", indices)
790 + ") != (uintptr_t) 0)");
791 else
792 throw SEMANTIC_ERROR(_("checking existence of an unsupported map type"));
793 }
794
795 string get (vector<tmpvar> const & indices, bool pre_agg=false) const
796 {
797 // see also itervar::get_key
798 if (type() == pe_string)
799 // impedance matching: NULL -> empty strings
800 return ("({ char *v = " + call_prefix("get", indices, pre_agg) + ");"
801 + "if (!v) v = \"\"; v; })");
802 else if (type() == pe_long || type() == pe_stats)
803 return call_prefix("get", indices, pre_agg) + ")";
804 else
805 throw SEMANTIC_ERROR(_("getting a value from an unsupported map type"));
806 }
807
808 string add (vector<tmpvar> const & indices, tmpvar const & val) const
809 {
810 string res = "{ int rc = ";
811
812 // impedance matching: empty strings -> NULL
813 if (type() == pe_stats)
814 res += (call_prefix("add", indices) + ", " + val.value() + ", " + stat_op_parms() + ")");
815 else
816 throw SEMANTIC_ERROR(_("adding a value of an unsupported map type"));
817
818 res += "; if (unlikely(rc)) { c->last_error = ";
819 res += STAP_T_01 +
820 lex_cast(maxsize > 0 ?
821 "size limit (" + lex_cast(maxsize) + ")" : "MAXMAPENTRIES")
822 + "\"; goto out; }}";
823
824 return res;
825 }
826
827 string set (vector<tmpvar> const & indices, tmpvar const & val) const
828 {
829 string res = "{ int rc = ";
830
831 // impedance matching: empty strings -> NULL
832 if (type() == pe_string)
833 res += (call_prefix("set", indices)
834 + ", (" + val.value() + "[0] ? " + val.value() + " : NULL))");
835 else if (type() == pe_long)
836 res += (call_prefix("set", indices) + ", " + val.value() + ")");
837 else
838 throw SEMANTIC_ERROR(_("setting a value of an unsupported map type"));
839
840 res += "; if (unlikely(rc)) { c->last_error = ";
841 res += STAP_T_01 +
842 lex_cast(maxsize > 0 ?
843 "size limit (" + lex_cast(maxsize) + ")" : "MAXMAPENTRIES")
844 + "\"; goto out; }}";
845
846 return res;
847 }
848
849 string hist() const
850 {
851 assert (ty == pe_stats);
852 assert (sd.type != statistic_decl::none);
853 return "(&(" + fetch_existing_aggregate() + "->hist))";
854 }
855
856 string buckets() const
857 {
858 assert (ty == pe_stats);
859 assert (sd.type != statistic_decl::none);
860 return "(" + fetch_existing_aggregate() + "->hist.buckets)";
861 }
862
863 string init () const
864 {
865 if (local)
866 throw SEMANTIC_ERROR(_F("unsupported local map init for %s", value().c_str()));
867
868 string prefix = "global_set(" + c_name() + ", ";
869 prefix += function_keysym("new") + " ("
870 + (is_parallel() ? stat_op_tokens() : "")
871 + "KEY_MAPENTRIES, " + (maxsize > 0 ? lex_cast(maxsize) : "MAXMAPENTRIES") + ", "
872 + ((wrap == true) ? "KEY_STAT_WRAP, " : "");
873
874 // See also var::init().
875
876 // Check for errors during allocation.
877 string suffix = "if (" + value () + " == NULL) rc = -ENOMEM;";
878
879 if (type() == pe_stats)
880 {
881 switch (sdecl().type)
882 {
883 case statistic_decl::none:
884 prefix = prefix + "KEY_HIST_TYPE, HIST_NONE, ";
885 break;
886
887 case statistic_decl::linear:
888 // FIXME: check for "reasonable" values in linear stats
889 prefix = prefix + "KEY_HIST_TYPE, HIST_LINEAR, "
890 + lex_cast(sdecl().linear_low) + ", "
891 + lex_cast(sdecl().linear_high) + ", "
892 + lex_cast(sdecl().linear_step) + ", ";
893 break;
894
895 case statistic_decl::logarithmic:
896 prefix = prefix + "KEY_HIST_TYPE, HIST_LOG, ";
897 break;
898 }
899 }
900
901 prefix += "NULL";
902
903 prefix = prefix + ")); ";
904 return (prefix + suffix);
905 }
906
907 string fini () const
908 {
909 // NB: fini() is safe to call even for globals that have not
910 // successfully initialized (that is to say, on NULL pointers),
911 // because the runtime specifically tolerates that in its _del
912 // functions.
913
914 if (is_parallel())
915 return "_stp_pmap_del (" + value() + ");";
916 else
917 return "_stp_map_del (" + value() + ");";
918 }
919 };
920
921
922 class itervar
923 : public var
924 {
925 public:
926
927 itervar (c_unparser *u, symbol* e, unsigned & counter)
928 : var(u, true, e->referent->type, counter)
929 {
930 if (type() == pe_unknown)
931 throw SEMANTIC_ERROR(_("iterating over unknown reference type"), e->tok);
932 }
933
934 void declare(c_unparser &c) const cxx_override
935 {
936 c.o->newline() << "struct map_node *" << name << ";";
937 }
938
939 string start (mapvar const & mv) const
940 {
941 string res;
942
943 if (mv.type() != type())
944 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::start()"));
945
946 if (mv.is_parallel())
947 return "_stp_map_start (" + mv.fetch_existing_aggregate() + ")";
948 else
949 return "_stp_map_start (" + mv.value() + ")";
950 }
951
952 string next (mapvar const & mv) const
953 {
954 if (mv.type() != type())
955 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::next()"));
956
957 if (mv.is_parallel())
958 return "_stp_map_iter (" + mv.fetch_existing_aggregate() + ", " + value() + ")";
959 else
960 return "_stp_map_iter (" + mv.value() + ", " + value() + ")";
961 }
962
963 // Cannot handle deleting and iterating on pmaps
964 string del_next (mapvar const & mv) const
965 {
966 if (mv.type() != type())
967 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::next()"));
968
969 if (mv.is_parallel())
970 throw SEMANTIC_ERROR(_("deleting a value of an unsupported map type"));
971 else
972 return "_stp_map_iterdel (" + mv.value() + ", " + value() + ")";
973 }
974
975 string get_key (mapvar const& mv, exp_type ty, unsigned i) const
976 {
977 // bug translator/1175: runtime uses base index 1 for the first dimension
978 // see also mapval::get
979 switch (ty)
980 {
981 case pe_long:
982 return mv.function_keysym("key_get_int64", true)
983 + " (" + value() + ", " + lex_cast(i+1) + ")";
984 case pe_string:
985 // impedance matching: NULL -> empty strings
986 return "(" + mv.function_keysym("key_get_str", true)
987 + " (" + value() + ", " + lex_cast(i+1) + ") ?: \"\")";
988 default:
989 throw SEMANTIC_ERROR(_("illegal key type"));
990 }
991 }
992
993 string get_value (mapvar const& mv, exp_type ty) const
994 {
995 if (ty != type())
996 throw SEMANTIC_ERROR(_("inconsistent iterator value in itervar::get_value()"));
997
998 switch (ty)
999 {
1000 case pe_long:
1001 return mv.function_keysym("get_int64", true) + " ("+ value() + ")";
1002 case pe_string:
1003 // impedance matching: NULL -> empty strings
1004 return "(" + mv.function_keysym("get_str", true) + " ("+ value() + ") ?: \"\")";
1005 case pe_stats:
1006 return mv.function_keysym("get_stat_data", true) + " ("+ value() + ")";
1007 default:
1008 throw SEMANTIC_ERROR(_("illegal value type"));
1009 }
1010 }
1011 };
1012
1013 ostream & operator<<(ostream & o, itervar const & v)
1014 {
1015 return o << v.value();
1016 }
1017
1018 // ------------------------------------------------------------------------
1019
1020 // translator_output moved to translator-output.cxx
1021
1022 // ------------------------------------------------------------------------
1023
1024 struct unmodified_fnargs_checker : public nop_visitor
1025 {
1026 bool is_embedded;
1027 bool has_unmodified_fnargs;
1028
1029 unmodified_fnargs_checker ():
1030 is_embedded(false), has_unmodified_fnargs(false)
1031 {}
1032
1033 void visit_embeddedcode (embeddedcode *e)
1034 {
1035 is_embedded = true;
1036 if (e->tagged_p("/* unmodified-fnargs */"))
1037 has_unmodified_fnargs = true;
1038 }
1039 };
1040
1041 bool
1042 is_unmodified_string_fnarg (systemtap_session* sess, functiondecl* fd, vardecl* v)
1043 {
1044 if (sess->unoptimized || v->type != pe_string)
1045 return false;
1046
1047 // if it's an embedded function, trust whether it has unmodified-fnargs
1048 unmodified_fnargs_checker ufc;
1049 fd->body->visit(& ufc);
1050 if (ufc.is_embedded)
1051 return ufc.has_unmodified_fnargs;
1052
1053 varuse_collecting_visitor vut (*sess);
1054 vut.current_function = fd;
1055 fd->body->visit(& vut);
1056 return (vut.written.find(v) == vut.written.end());
1057 }
1058
1059 // If we've seen a dupe, return it; else remember this and return NULL.
1060 probe *
1061 c_unparser::get_probe_dupe (derived_probe *dp)
1062 {
1063 if (session->unoptimized)
1064 return NULL;
1065
1066 // Notice we're using the probe body itself instead of the emitted C
1067 // probe body to compare probes. We need to do this because the
1068 // emitted C probe body has stuff in it like:
1069 // c->last_stmt = "identifier 'printf' at foo.stp:<line>:<column>";
1070 //
1071 // which would make comparisons impossible.
1072
1073 ostringstream oss;
1074
1075 dp->print_dupe_stamp (oss);
1076 dp->body->print(oss);
1077
1078 // Since the generated C changes based on whether or not the probe
1079 // needs locks around global variables, this needs to be reflected
1080 // here. We don't want to treat as duplicate the handlers of
1081 // begin/end and normal probes that differ only in need_global_locks.
1082 oss << "# needs_global_locks: " << dp->needs_global_locks () << endl;
1083
1084 // NB: dependent probe conditions *could* be listed here, but don't need to
1085 // be. That's because they're only dependent on the probe body, which is
1086 // already "hashed" in above.
1087
1088 pair<map<string, probe*>::iterator, bool> const& inserted =
1089 probe_contents.insert(make_pair(oss.str(), dp));
1090
1091 if (inserted.second)
1092 return NULL; // it's new!
1093
1094 // Already seen it; here's the old one:
1095 return inserted.first->second;
1096 }
1097
1098 void
1099 c_unparser::emit_common_header ()
1100 {
1101 c_tmpcounter ct (this);
1102
1103 o->newline();
1104
1105 // Per CPU context for probes. Includes common shared state held for
1106 // all probes (defined in common_probe_context), the probe locals (union)
1107 // and the function locals (union).
1108 o->newline() << "struct context {";
1109
1110 // Common state held shared by probes.
1111 o->newline(1) << "#include \"common_probe_context.h\"";
1112
1113 // PR10516: probe locals
1114 o->newline() << "union {";
1115 o->indent(1);
1116
1117 for (unsigned i=0; i<session->probes.size(); i++)
1118 ct.emit_probe (session->probes[i]);
1119
1120 o->newline(-1) << "} probe_locals;";
1121
1122 // PR10516: function locals
1123 o->newline() << "union {";
1124 o->indent(1);
1125
1126 for (map<string,functiondecl*>::iterator it = session->functions.begin();
1127 it != session->functions.end(); it++)
1128 ct.emit_function (it->second);
1129
1130 o->newline(-1) << "} locals [MAXNESTING+1];";
1131
1132 // NB: The +1 above for extra room for outgoing arguments of next nested function.
1133 // If MAXNESTING is set too small, the args will be written, but the MAXNESTING
1134 // check done at c_unparser::emit_function will reject.
1135 //
1136 // This policy wastes memory (one row of locals[] that cannot really
1137 // be used), but trades that for smaller code (not having to check
1138 // c->nesting against MAXNESTING at every call site).
1139
1140 // Try to catch a crazy user dude passing in -DMAXNESTING=-1, leading to a [0]-sized
1141 // locals[] array.
1142 o->newline() << "#if MAXNESTING < 0";
1143 o->newline() << "#error \"MAXNESTING must be positive\"";
1144 o->newline() << "#endif";
1145
1146 // Use a separate union for compiled-printf locals, no nesting required.
1147 emit_compiled_printf_locals ();
1148
1149 o->newline(-1) << "};\n"; // end of struct context
1150
1151 o->newline() << "#include \"runtime_context.h\"";
1152
1153 emit_map_type_instantiations ();
1154
1155 emit_compiled_printfs();
1156
1157 if (!session->runtime_usermode_p())
1158 {
1159 // Updated in probe handlers to signal that a module refresh is needed.
1160 // Checked and cleared by common epilogue after scheduling refresh work.
1161 o->newline( 0) << "static atomic_t need_module_refresh = ATOMIC_INIT(0);";
1162
1163 // We will use a workqueue to schedule module_refresh work when we need
1164 // to enable/disable probes.
1165 o->newline( 0) << "#include <linux/workqueue.h>";
1166 o->newline( 0) << "static struct work_struct module_refresher_work;";
1167 o->newline( 0) << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)";
1168 o->newline( 0) << "static void module_refresher(void *data) {";
1169 o->newline( 0) << "#else";
1170 o->newline( 0) << "static void module_refresher(struct work_struct *work) {";
1171 o->newline( 0) << "#endif";
1172 o->newline( 1) << "systemtap_module_refresh(NULL);";
1173 o->newline(-1) << "}";
1174
1175 o->newline( 0) << "#ifdef STP_ON_THE_FLY_TIMER_ENABLE";
1176 o->newline( 0) << "#include \"timer.h\"";
1177 o->newline( 0) << "static struct hrtimer module_refresh_timer;";
1178
1179 o->newline( 0) << "#ifndef STP_ON_THE_FLY_INTERVAL";
1180 o->newline( 0) << "#define STP_ON_THE_FLY_INTERVAL (100*1000*1000)"; // default to 100 ms
1181 o->newline( 0) << "#endif";
1182
1183 o->newline( 0) << "hrtimer_return_t module_refresh_timer_cb(struct hrtimer *timer) {";
1184 o->newline(+1) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
1185 // NB: one might like to invoke systemtap_module_refresh(NULL) directly from
1186 // here ... however hrtimers are called from an unsleepable context, so no can do.
1187 o->newline(+1) << "schedule_work(&module_refresher_work);";
1188 o->newline(-1) << "hrtimer_set_expires(timer,";
1189 o->newline( 0) << " ktime_add(hrtimer_get_expires(timer),";
1190 o->newline( 0) << " ktime_set(0, STP_ON_THE_FLY_INTERVAL))); ";
1191 o->newline( 0) << "return HRTIMER_RESTART;";
1192 o->newline(-1) << "}";
1193 o->newline( 0) << "#endif /* STP_ON_THE_FLY_TIMER_ENABLE */";
1194 }
1195
1196 o->newline(0) << "#include \"namespaces.h\"";
1197
1198 o->newline();
1199 }
1200
1201
1202 void
1203 c_unparser::declare_compiled_printf (bool print_to_stream, const string& format)
1204 {
1205 pair<bool, string> index (print_to_stream, format);
1206 map<pair<bool, string>, string>::iterator it = compiled_printfs.find(index);
1207 if (it == compiled_printfs.end())
1208 compiled_printfs[index] = (print_to_stream ? "stp_printf_" : "stp_sprintf_")
1209 + lex_cast(compiled_printfs.size() + 1);
1210 }
1211
1212 const string&
1213 c_unparser::get_compiled_printf (bool print_to_stream, const string& format)
1214 {
1215 map<pair<bool, string>, string>::iterator it =
1216 compiled_printfs.find(make_pair(print_to_stream, format));
1217 if (it == compiled_printfs.end())
1218 throw SEMANTIC_ERROR (_("internal error translating printf"));
1219 return it->second;
1220 }
1221
1222 const string&
1223 c_tmpcounter::get_compiled_printf (bool print_to_stream, const string& format)
1224 {
1225 parent->declare_compiled_printf (print_to_stream, format);
1226 return parent->get_compiled_printf (print_to_stream, format);
1227 }
1228
1229 void
1230 c_unparser::emit_compiled_printf_locals ()
1231 {
1232 o->newline() << "#ifndef STP_LEGACY_PRINT";
1233 o->newline() << "union {";
1234 o->indent(1);
1235 map<pair<bool, string>, string>::iterator it;
1236 for (it = compiled_printfs.begin(); it != compiled_printfs.end(); ++it)
1237 {
1238 bool print_to_stream = it->first.first;
1239 const string& format_string = it->first.second;
1240 const string& name = it->second;
1241 vector<print_format::format_component> components =
1242 print_format::string_to_components(format_string);
1243
1244 o->newline() << "struct " << name << "_locals {";
1245 o->indent(1);
1246
1247 size_t arg_ix = 0;
1248 vector<print_format::format_component>::const_iterator c;
1249 for (c = components.begin(); c != components.end(); ++c)
1250 {
1251 if (c->type == print_format::conv_literal)
1252 continue;
1253
1254 // Take note of the width and precision arguments, if any.
1255 if (c->widthtype == print_format::width_dynamic)
1256 o->newline() << "int64_t arg" << arg_ix++ << ";";
1257 if (c->prectype == print_format::prec_dynamic)
1258 o->newline() << "int64_t arg" << arg_ix++ << ";";
1259
1260 // Output the actual argument.
1261 switch (c->type)
1262 {
1263 case print_format::conv_pointer:
1264 case print_format::conv_number:
1265 case print_format::conv_char:
1266 case print_format::conv_memory:
1267 case print_format::conv_memory_hex:
1268 case print_format::conv_binary:
1269 o->newline() << "int64_t arg" << arg_ix++ << ";";
1270 break;
1271
1272 case print_format::conv_string:
1273 // NB: Since we know incoming strings are immutable, we can use
1274 // const char* rather than a private char[] copy. This is a
1275 // special case of the sort of optimizations desired in PR11528.
1276 o->newline() << "const char* arg" << arg_ix++ << ";";
1277 break;
1278
1279 default:
1280 assert(false); // XXX
1281 break;
1282 }
1283 }
1284
1285
1286 if (!print_to_stream)
1287 o->newline() << "char * __retvalue;";
1288
1289 o->newline(-1) << "} " << name << ";";
1290 }
1291 o->newline(-1) << "} printf_locals;";
1292 o->newline() << "#endif // STP_LEGACY_PRINT";
1293 }
1294
1295 void
1296 c_unparser::emit_compiled_printfs ()
1297 {
1298 o->newline() << "#ifndef STP_LEGACY_PRINT";
1299 map<pair<bool, string>, string>::iterator it;
1300 for (it = compiled_printfs.begin(); it != compiled_printfs.end(); ++it)
1301 {
1302 bool print_to_stream = it->first.first;
1303 const string& format_string = it->first.second;
1304 const string& name = it->second;
1305 vector<print_format::format_component> components =
1306 print_format::string_to_components(format_string);
1307
1308 o->newline();
1309
1310 // Might be nice to output the format string in a comment, but we'd have
1311 // to be extra careful about format strings not escaping the comment...
1312 o->newline() << "static void " << name
1313 << " (struct context* __restrict__ c) {";
1314 o->newline(1) << "struct " << name << "_locals * __restrict__ l = "
1315 << "& c->printf_locals." << name << ";";
1316 o->newline() << "char *str = NULL, *end = NULL;";
1317 o->newline() << "const char *src;";
1318 o->newline() << "int width;";
1319 o->newline() << "int precision;";
1320 o->newline() << "unsigned long ptr_value;";
1321 o->newline() << "int num_bytes;";
1322
1323 o->newline() << "(void) width;";
1324 o->newline() << "(void) precision;";
1325 o->newline() << "(void) ptr_value;";
1326 o->newline() << "(void) num_bytes;";
1327
1328 if (print_to_stream)
1329 {
1330 // Compute the buffer size needed for these arguments.
1331 size_t arg_ix = 0;
1332 o->newline() << "num_bytes = 0;";
1333 vector<print_format::format_component>::const_iterator c;
1334 for (c = components.begin(); c != components.end(); ++c)
1335 {
1336 if (c->type == print_format::conv_literal)
1337 {
1338 literal_string ls(c->literal_string);
1339 o->newline() << "num_bytes += sizeof(";
1340 visit_literal_string(&ls);
1341 o->line() << ") - 1;"; // don't count the '\0'
1342 continue;
1343 }
1344
1345 o->newline() << "width = ";
1346 if (c->widthtype == print_format::width_dynamic)
1347 o->line() << "clamp_t(int, l->arg" << arg_ix++
1348 << ", 0, STP_BUFFER_SIZE);";
1349 else if (c->widthtype == print_format::width_static)
1350 o->line() << "clamp_t(int, " << c->width
1351 << ", 0, STP_BUFFER_SIZE);";
1352 else
1353 o->line() << "-1;";
1354
1355 o->newline() << "precision = ";
1356 if (c->prectype == print_format::prec_dynamic)
1357 o->line() << "clamp_t(int, l->arg" << arg_ix++
1358 << ", 0, STP_BUFFER_SIZE);";
1359 else if (c->prectype == print_format::prec_static)
1360 o->line() << "clamp_t(int, " << c->precision
1361 << ", 0, STP_BUFFER_SIZE);";
1362 else
1363 o->line() << "-1;";
1364
1365 string value = "l->arg" + lex_cast(arg_ix++);
1366 switch (c->type)
1367 {
1368 case print_format::conv_pointer:
1369 // NB: stap < 1.3 had odd %p behavior... see _stp_vsnprintf
1370 if (strverscmp(session->compatible.c_str(), "1.3") < 0)
1371 {
1372 o->newline() << "ptr_value = " << value << ";";
1373 o->newline() << "if (width == -1)";
1374 o->newline(1) << "width = 2 + 2 * sizeof(void*);";
1375 o->newline(-1) << "precision = width - 2;";
1376 if (!c->test_flag(print_format::fmt_flag_left))
1377 o->newline() << "precision = min_t(int, precision, 2 * sizeof(void*));";
1378 o->newline() << "num_bytes += number_size(ptr_value, "
1379 << c->base << ", width, precision, " << c->flags << ");";
1380 break;
1381 }
1382 /* Fallthrough */
1383 // else fall-through to conv_number
1384 case print_format::conv_number:
1385 o->newline() << "num_bytes += number_size(" << value << ", "
1386 << c->base << ", width, precision, " << c->flags << ");";
1387 break;
1388
1389 case print_format::conv_char:
1390 o->newline() << "num_bytes += _stp_vsprint_char_size("
1391 << value << ", width, " << c->flags << ");";
1392 break;
1393
1394 case print_format::conv_string:
1395 o->newline() << "num_bytes += _stp_vsprint_memory_size("
1396 << value << ", width, precision, 's', "
1397 << c->flags << ");";
1398 break;
1399
1400 case print_format::conv_memory:
1401 case print_format::conv_memory_hex:
1402 o->newline() << "num_bytes += _stp_vsprint_memory_size("
1403 << "(const char*)(intptr_t)" << value
1404 << ", width, precision, '"
1405 << ((c->type == print_format::conv_memory) ? "m" : "M")
1406 << "', " << c->flags << ");";
1407 break;
1408
1409 case print_format::conv_binary:
1410 o->newline() << "num_bytes += _stp_vsprint_binary_size("
1411 << value << ", width, precision);";
1412 break;
1413
1414 default:
1415 assert(false); // XXX
1416 break;
1417 }
1418 }
1419
1420 o->newline() << "num_bytes = clamp(num_bytes, 0, STP_BUFFER_SIZE);";
1421 o->newline() << "str = (char*)_stp_reserve_bytes(num_bytes);";
1422 o->newline() << "end = str ? str + num_bytes - 1 : 0;";
1423 }
1424 else // !print_to_stream
1425 {
1426 // String results are a known buffer and size;
1427 o->newline() << "str = l->__retvalue;";
1428 o->newline() << "end = str + MAXSTRINGLEN - 1;";
1429 }
1430
1431 o->newline() << "if (str && str <= end) {";
1432 o->indent(1);
1433
1434 // Generate code to print the actual arguments.
1435 size_t arg_ix = 0;
1436 vector<print_format::format_component>::const_iterator c;
1437 for (c = components.begin(); c != components.end(); ++c)
1438 {
1439 if (c->type == print_format::conv_literal)
1440 {
1441 literal_string ls(c->literal_string);
1442 o->newline() << "src = ";
1443 visit_literal_string(&ls);
1444 o->line() << ";";
1445 o->newline() << "while (*src && str <= end)";
1446 o->newline(1) << "*str++ = *src++;";
1447 o->indent(-1);
1448 continue;
1449 }
1450
1451 o->newline() << "width = ";
1452 if (c->widthtype == print_format::width_dynamic)
1453 o->line() << "clamp_t(int, l->arg" << arg_ix++
1454 << ", 0, end - str + 1);";
1455 else if (c->widthtype == print_format::width_static)
1456 o->line() << "clamp_t(int, " << c->width
1457 << ", 0, end - str + 1);";
1458 else
1459 o->line() << "-1;";
1460
1461 o->newline() << "precision = ";
1462 if (c->prectype == print_format::prec_dynamic)
1463 o->line() << "clamp_t(int, l->arg" << arg_ix++
1464 << ", 0, end - str + 1);";
1465 else if (c->prectype == print_format::prec_static)
1466 o->line() << "clamp_t(int, " << c->precision
1467 << ", 0, end - str + 1);";
1468 else
1469 o->line() << "-1;";
1470
1471 string value = "l->arg" + lex_cast(arg_ix++);
1472 switch (c->type)
1473 {
1474 case print_format::conv_pointer:
1475 // NB: stap < 1.3 had odd %p behavior... see _stp_vsnprintf
1476 if (strverscmp(session->compatible.c_str(), "1.3") < 0)
1477 {
1478 o->newline() << "ptr_value = " << value << ";";
1479 o->newline() << "if (width == -1)";
1480 o->newline(1) << "width = 2 + 2 * sizeof(void*);";
1481 o->newline(-1) << "precision = width - 2;";
1482 if (!c->test_flag(print_format::fmt_flag_left))
1483 o->newline() << "precision = min_t(int, precision, 2 * sizeof(void*));";
1484 o->newline() << "str = number(str, end, ptr_value, "
1485 << c->base << ", width, precision, " << c->flags << ");";
1486 break;
1487 }
1488 /* Fallthrough */
1489 // else fall-through to conv_number
1490 case print_format::conv_number:
1491 o->newline() << "str = number(str, end, " << value << ", "
1492 << c->base << ", width, precision, " << c->flags << ");";
1493 break;
1494
1495 case print_format::conv_char:
1496 o->newline() << "str = _stp_vsprint_char(str, end, "
1497 << value << ", width, " << c->flags << ");";
1498 break;
1499
1500 case print_format::conv_string:
1501 o->newline() << "str = _stp_vsprint_memory(str, end, "
1502 << value << ", width, precision, 's', "
1503 << c->flags << ");";
1504 break;
1505
1506 case print_format::conv_memory:
1507 case print_format::conv_memory_hex:
1508 o->newline() << "str = _stp_vsprint_memory(str, end, "
1509 << "(const char*)(intptr_t)" << value
1510 << ", width, precision, '"
1511 << ((c->type == print_format::conv_memory) ? "m" : "M")
1512 << "', " << c->flags << ");";
1513 o->newline() << "if (unlikely(str == NULL)) {";
1514 o->indent(1);
1515 if (print_to_stream)
1516 o->newline() << "_stp_unreserve_bytes(num_bytes);";
1517 o->newline() << "return;";
1518 o->newline(-1) << "}";
1519 break;
1520
1521 case print_format::conv_binary:
1522 o->newline() << "str = _stp_vsprint_binary(str, end, "
1523 << value << ", width, precision, "
1524 << c->flags << ");";
1525 break;
1526
1527 default:
1528 assert(false); // XXX
1529 break;
1530 }
1531 }
1532
1533 if (!print_to_stream)
1534 {
1535 o->newline() << "if (str <= end)";
1536 o->newline(1) << "*str = '\\0';";
1537 o->newline(-1) << "else";
1538 o->newline(1) << "*end = '\\0';";
1539 o->indent(-1);
1540 }
1541
1542 o->newline(-1) << "}";
1543
1544 o->newline(-1) << "}";
1545 }
1546 o->newline() << "#endif // STP_LEGACY_PRINT";
1547 }
1548
1549
1550 void
1551 c_unparser::emit_global_param (vardecl *v)
1552 {
1553 // Only true globals can be params, not private variables.
1554 if (!v->name.starts_with("__global_")) return;
1555
1556 // Only non-synthetic globals can be params.
1557 if (v->synthetic) return;
1558
1559 string global = c_globalname (v->name);
1560 interned_string param = v->name.substr(sizeof("__global_") - 1);
1561
1562 // For dyninst, use the emit_global_init_* functionality instead.
1563 assert (!session->runtime_usermode_p());
1564
1565 // NB: systemtap globals can collide with linux macros,
1566 // e.g. VM_FAULT_MAJOR. We want the parameter name anyway. This
1567 // #undef is spit out at the end of the C file, so that removing the
1568 // definition won't affect any other embedded-C or generated code.
1569 // XXX: better not have a global variable named module_param_named etc.!
1570 o->newline() << "#undef " << param; // avoid colliding with non-mangled name
1571
1572 // Emit module_params for this global, if its type is convenient.
1573 if (v->arity == 0 && v->type == pe_long)
1574 {
1575 o->newline() << "module_param_named (" << param << ", "
1576 << "global(" << global << "), int64_t, 0);";
1577 }
1578 else if (v->arity == 0 && v->type == pe_string)
1579 {
1580 // NB: no special copying is needed.
1581 o->newline() << "module_param_string (" << param << ", "
1582 << "global(" << global << "), MAXSTRINGLEN, 0);";
1583 }
1584 }
1585
1586
1587 void
1588 c_unparser::emit_global_init_setters ()
1589 {
1590 // Hack for dyninst module params: setter function forms a little
1591 // linear lookup table ditty to find a global variable by name.
1592 o->newline() << "int stp_global_setter (const char *name, const char *value) {";
1593 o->newline(1);
1594 for (unsigned i=0; i<session->globals.size(); i++)
1595 {
1596 vardecl* v = session->globals[i];
1597 if (v->arity > 0) continue;
1598 if (v->type != pe_string && v->type != pe_long) continue;
1599
1600 // Only true globals can be params, not private variables.
1601 if (!v->name.starts_with("__global_")) continue;
1602
1603 string global = c_globalname (v->name);
1604 interned_string param = v->name.substr(sizeof("__global_") - 1);
1605
1606 // Do not mangle v->name for the comparison!
1607 o->line() << "if (0 == strcmp(name,\"" << param << "\"))" << " {";
1608
1609 o->indent(1);
1610 if (v->type == pe_string)
1611 {
1612 c_assign("stp_global_init." + global, "value", pe_string, "BUG: global module param", v->tok);
1613 o->newline() << "return 0;";
1614 }
1615 else
1616 {
1617 o->newline() << "return set_int64_t(value, &stp_global_init." << global << ");";
1618 }
1619
1620 o->newline(-1) << "} else ";
1621 }
1622
1623 // Call the runtime function that handles session attributes, like
1624 // log_level, etc.
1625 o->line() << "return stp_session_attribute_setter(name, value);";
1626 o->newline(-1) << "}";
1627 o->newline();
1628 }
1629
1630
1631 void
1632 c_unparser::emit_global (vardecl *v)
1633 {
1634 string vn = c_globalname (v->name);
1635
1636 string type;
1637 if (v->arity > 0)
1638 type = (v->type == pe_stats) ? "PMAP" : "MAP";
1639 else
1640 type = c_typename (v->type);
1641
1642 if (session->runtime_usermode_p())
1643 {
1644 // In stapdyn mode, the stat/map/pmap pointers are stored as offptr_t in
1645 // shared memory. However, we can keep a little type safety by emitting
1646 // FOO_typed and using typeof(FOO_typed) in the global() macros.
1647 bool offptr_p = (v->type == pe_stats) || (v->arity > 0);
1648 string stored_type = offptr_p ? "offptr_t" : type;
1649
1650 // NB: The casted_type is in the unused side of a __builtin_choose_expr
1651 // for non-offptr types, so it doesn't matter what we put for them, as
1652 // long as it passes syntax long enough for gcc to choose the other expr.
1653 string casted_type = offptr_p ? type : "void*";
1654
1655 o->newline() << "union {";
1656 o->newline(1) << casted_type << " " << vn << "_typed;";
1657 o->newline() << stored_type << " " << vn << ";";
1658 o->newline(-1) << "};";
1659 }
1660 else
1661 o->newline() << type << " " << vn << ";";
1662
1663 o->newline() << "rwlock_t " << vn << "_lock;";
1664 o->newline() << "#ifdef STP_TIMING";
1665 o->newline() << "atomic_t " << vn << "_lock_skip_count;";
1666 o->newline() << "atomic_t " << vn << "_lock_contention_count;";
1667 o->newline() << "#endif\n";
1668 }
1669
1670
1671 void
1672 c_unparser::emit_global_init (vardecl *v)
1673 {
1674 // We can only statically initialize some scalars.
1675 if (v->arity == 0 && v->init)
1676 {
1677 o->newline() << "." << c_globalname (v->name) << " = ";
1678 v->init->visit(this);
1679 o->line() << ",";
1680 }
1681 else if (v->arity == 0 && session->runtime_usermode_p())
1682 {
1683 // For dyninst: always try to put a default value into the initial
1684 // static structure, so we don't have to guess if it was customized.
1685 if (v->type == pe_long)
1686 o->newline() << "." << c_globalname (v->name) << " = 0,";
1687 else if (v->type == pe_string)
1688 o->newline() << "." << c_globalname (v->name) << " = { '\\0' },"; // XXX: ""
1689 }
1690 // The lock and lock_skip_count are handled in emit_module_init.
1691 }
1692
1693
1694 void
1695 c_unparser::emit_global_init_type (vardecl *v)
1696 {
1697 // We can only statically initialize some scalars.
1698 if (v->arity == 0) // ... although we still allow !v->init here.
1699 {
1700 o->newline() << c_typename(v->type) << " " << c_globalname(v->name) << ";";
1701 }
1702 }
1703
1704
1705 void
1706 c_unparser::emit_functionsig (functiondecl* v)
1707 {
1708 o->newline() << "static void " << c_funcname(v->name)
1709 << " (struct context * __restrict__ c);";
1710 }
1711
1712
1713 void
1714 c_unparser::emit_kernel_module_init ()
1715 {
1716 if (session->runtime_usermode_p())
1717 return;
1718
1719 o->newline();
1720 o->newline() << "static int systemtap_kernel_module_init (void) {";
1721 o->newline(1) << "int rc = 0;";
1722 o->newline() << "int i=0, j=0;"; // for derived_probe_group use
1723
1724 vector<derived_probe_group*> g = all_session_groups (*session);
1725 for (unsigned i=0; i<g.size(); i++)
1726 {
1727 g[i]->emit_kernel_module_init (*session);
1728
1729 o->newline() << "if (rc) {";
1730 o->indent(1);
1731 if (i>0)
1732 {
1733 for (int j=i-1; j>=0; j--)
1734 g[j]->emit_kernel_module_exit (*session);
1735 }
1736 o->newline() << "goto out;";
1737 o->newline(-1) << "}";
1738 }
1739 o->newline(-1) << "out:";
1740 o->indent(1);
1741 o->newline() << "return rc;";
1742 o->newline(-1) << "}\n";
1743 o->assert_0_indent();
1744 }
1745
1746
1747 void
1748 c_unparser::emit_kernel_module_exit ()
1749 {
1750 if (session->runtime_usermode_p())
1751 return;
1752
1753 o->newline();
1754 o->newline() << "static void systemtap_kernel_module_exit (void) {";
1755 o->newline(1) << "int i=0, j=0;"; // for derived_probe_group use
1756
1757 // We're processing the derived_probe_group list in reverse order.
1758 // This ensures that probe groups get unregistered in reverse order
1759 // of the way they were registered.
1760 vector<derived_probe_group*> g = all_session_groups (*session);
1761 for (vector<derived_probe_group*>::reverse_iterator i = g.rbegin();
1762 i != g.rend(); i++)
1763 {
1764 (*i)->emit_kernel_module_exit (*session);
1765 }
1766 o->newline(-1) << "}\n";
1767 o->assert_0_indent();
1768 }
1769
1770
1771 void
1772 c_unparser::emit_module_init ()
1773 {
1774 vector<derived_probe_group*> g = all_session_groups (*session);
1775 for (unsigned i=0; i<g.size(); i++)
1776 {
1777 g[i]->emit_module_decls (*session);
1778 o->assert_0_indent();
1779 }
1780
1781 o->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
1782 o->newline() << "#include \"linux/stp_tracepoint.c\"";
1783 o->newline() << "#endif";
1784
1785 o->newline();
1786 o->newline() << "static int systemtap_module_init (void) {";
1787 o->newline(1) << "int rc = 0;";
1788 o->newline() << "int cpu;";
1789 o->newline() << "int i=0, j=0;"; // for derived_probe_group use
1790 o->newline() << "const char *probe_point = \"\";";
1791
1792 // NB: This block of initialization only makes sense in kernel
1793 if (! session->runtime_usermode_p())
1794 {
1795 // XXX Plus, most of this code is completely static, so it probably should
1796 // move into the runtime, where kernel/dyninst is more easily separated.
1797
1798 // Compare actual and targeted kernel releases/machines. Sometimes
1799 // one may install the incorrect debuginfo or -devel RPM, and try to
1800 // run a probe compiled for a different version. Catch this early,
1801 // just in case modversions didn't.
1802 o->newline() << "{";
1803 o->newline() << "#ifndef STP_NO_VERREL_CHECK";
1804 o->newline(1) << "const char* release = UTS_RELEASE;";
1805 o->newline() << "#ifdef STAPCONF_GENERATED_COMPILE";
1806 o->newline() << "const char* version = UTS_VERSION;";
1807 o->newline() << "#endif";
1808
1809 // The systemtap_module_init() function must be run in
1810 // non-atomic context, since several functions might need to
1811 // sleep.
1812 o->newline() << "might_sleep();";
1813
1814 // NB: This UTS_RELEASE compile-time macro directly checks only that
1815 // the compile-time kbuild tree matches the compile-time debuginfo/etc.
1816 // It does not check the run time kernel value. However, this is
1817 // probably OK since the kbuild modversions system aims to prevent
1818 // mismatches between kbuild and runtime versions at module-loading time.
1819
1820 // o->newline() << "const char* machine = UTS_MACHINE;";
1821 // NB: We could compare UTS_MACHINE too, but on x86 it lies
1822 // (UTS_MACHINE=i386, but uname -m is i686). Sheesh.
1823
1824 o->newline() << "if (strcmp (release, "
1825 << lex_cast_qstring (session->kernel_release) << ")) {";
1826 o->newline(1) << "_stp_error (\"module release mismatch (%s vs %s)\", "
1827 << "release, "
1828 << lex_cast_qstring (session->kernel_release)
1829 << ");";
1830 o->newline() << "rc = -EINVAL;";
1831 o->newline(-1) << "}";
1832
1833 o->newline() << "#ifdef STAPCONF_GENERATED_COMPILE";
1834 o->newline() << "if (strcmp (utsname()->version, version)) {";
1835 o->newline(1) << "_stp_error (\"module version mismatch (%s vs %s), release %s\", "
1836 << "version, "
1837 << "utsname()->version, "
1838 << "release"
1839 << ");";
1840 o->newline() << "rc = -EINVAL;";
1841 o->newline(-1) << "}";
1842 o->newline() << "#endif";
1843 o->newline() << "#endif";
1844
1845 // perform buildid-based checking if able
1846 o->newline() << "if (_stp_module_check()) rc = -EINVAL;";
1847
1848 // Perform checking on the user's credentials vs those required to load/run this module.
1849 o->newline() << "if (_stp_privilege_credentials == 0) {";
1850 o->newline(1) << "if (STP_PRIVILEGE_CONTAINS(STP_PRIVILEGE, STP_PR_STAPDEV) ||";
1851 o->newline() << " STP_PRIVILEGE_CONTAINS(STP_PRIVILEGE, STP_PR_STAPUSR)) {";
1852 o->newline(1) << "_stp_privilege_credentials = STP_PRIVILEGE;";
1853 o->newline() << "#ifdef DEBUG_PRIVILEGE";
1854 o->newline(1) << "_dbug(\"User's privilege credentials default to %s\\n\",";
1855 o->newline() << " privilege_to_text(_stp_privilege_credentials));";
1856 o->newline(-1) << "#endif";
1857 o->newline(-1) << "}";
1858 o->newline() << "else {";
1859 o->newline(1) << "_stp_error (\"Unable to verify that you have the required privilege credentials to run this module (%s required). You must use staprun version 1.7 or higher.\",";
1860 o->newline() << " privilege_to_text(STP_PRIVILEGE));";
1861 o->newline() << "rc = -EINVAL;";
1862 o->newline(-1) << "}";
1863 o->newline(-1) << "}";
1864 o->newline() << "else {";
1865 o->newline(1) << "#ifdef DEBUG_PRIVILEGE";
1866 o->newline(1) << "_dbug(\"User's privilege credentials provided as %s\\n\",";
1867 o->newline() << " privilege_to_text(_stp_privilege_credentials));";
1868 o->newline(-1) << "#endif";
1869 o->newline() << "if (! STP_PRIVILEGE_CONTAINS(_stp_privilege_credentials, STP_PRIVILEGE)) {";
1870 o->newline(1) << "_stp_error (\"Your privilege credentials (%s) are insufficient to run this module (%s required).\",";
1871 o->newline () << " privilege_to_text(_stp_privilege_credentials), privilege_to_text(STP_PRIVILEGE));";
1872 o->newline() << "rc = -EINVAL;";
1873 o->newline(-1) << "}";
1874 o->newline(-1) << "}";
1875
1876 o->newline(-1) << "}";
1877
1878 o->newline() << "if (rc) goto out;";
1879 }
1880
1881 // Now that kernel version and permissions are correct,
1882 // initialize the global session states before anything else.
1883 o->newline() << "rc = stp_session_init();";
1884 o->newline() << "if (rc) {";
1885 o->newline(1) << "_stp_error (\"couldn't initialize the main session (rc %d)\", rc);";
1886 o->newline() << "goto out;";
1887 o->newline(-1) << "}";
1888
1889 // This signals any other probes that may be invoked in the next little
1890 // while to abort right away. Currently running probes are allowed to
1891 // terminate. These may set STAP_SESSION_ERROR!
1892 //
1893 // Note that this *must* be done after stp_session_init() is called,
1894 // since that initializes the dyninst session atomics. Note that we
1895 // don't want to run systemtap_module_init() twice.
1896 o->newline() << "if (atomic_cmpxchg(session_state(), STAP_SESSION_UNINITIALIZED, STAP_SESSION_STARTING) != STAP_SESSION_UNINITIALIZED) {";
1897 o->newline(1) << "_stp_error (\"session has already been initialized\");";
1898 // Note that here we don't want to jump to "out", since we don't
1899 // want to deregister anything, we just want to return.
1900 o->newline() << "return -EALREADY;";
1901 o->newline(-1) << "}";
1902
1903 // initialize gettimeofday (if needed)
1904 o->newline() << "#ifdef STAP_NEED_GETTIMEOFDAY";
1905 o->newline() << "rc = _stp_init_time();"; // Kick off the Big Bang.
1906 o->newline() << "if (rc) {";
1907 o->newline(1) << "_stp_error (\"couldn't initialize gettimeofday\");";
1908 o->newline() << "goto out;";
1909 o->newline(-1) << "}";
1910 o->newline() << "#endif";
1911
1912 // initialize tracepoints (if needed)
1913 o->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
1914 o->newline() << "rc = stp_tracepoint_init();";
1915 o->newline() << "if (rc) {";
1916 o->newline(1) << "_stp_error (\"couldn't initialize tracepoints\");";
1917 o->newline() << "goto out;";
1918 o->newline(-1) << "}";
1919 o->newline() << "#endif";
1920
1921 // initialize stack setup (if needed)
1922 o->newline() << "#ifdef STP_NEED_UNWIND_DATA";
1923 o->newline() << "rc = _stp_init_stack();";
1924 o->newline() << "if (rc) {";
1925 o->newline(1) << "_stp_error (\"couldn't initialize stack support\");";
1926 o->newline() << "goto out;";
1927 o->newline(-1) << "}";
1928 o->newline() << "#endif";
1929
1930 // NB: we don't need per-_stp_module task_finders, since a single common one
1931 // set up in runtime/sym.c's _stp_sym_init() will scan through all _stp_modules. XXX - check this!
1932 o->newline() << "(void) probe_point;";
1933 o->newline() << "(void) i;";
1934 o->newline() << "(void) j;";
1935
1936 // Allocate context structures.
1937 o->newline() << "rc = _stp_runtime_contexts_alloc();";
1938 o->newline() << "if (rc != 0)";
1939 o->newline(1) << "goto out;";
1940 o->indent(-1);
1941
1942 for (unsigned i=0; i<session->globals.size(); i++)
1943 {
1944 vardecl* v = session->globals[i];
1945 if (v->index_types.size() > 0)
1946 o->newline() << getmap (v).init();
1947 else if (session->runtime_usermode_p() && v->arity == 0
1948 && (v->type == pe_long || v->type == pe_string))
1949 c_assign(getvar (v).value(), "stp_global_init." + c_globalname(v->name), v->type, "BUG: global initialization", v->tok);
1950 else
1951 o->newline() << getvar (v).init();
1952 // NB: in case of failure of allocation, "rc" will be set to non-zero.
1953 // Allocation can in general continue.
1954
1955 o->newline() << "if (rc) {";
1956 o->newline(1) << "_stp_error (\"global variable '" << v->name << "' allocation failed\");";
1957 o->newline() << "goto out;";
1958 o->newline(-1) << "}";
1959
1960 o->newline() << "global_lock_init(" << c_globalname (v->name) << ");";
1961 o->newline() << "#ifdef STP_TIMING";
1962 o->newline() << "atomic_set(global_skipped(" << c_globalname (v->name) << "), 0);";
1963 o->newline() << "atomic_set(global_contended(" << c_globalname (v->name) << "), 0);";
1964 o->newline() << "#endif";
1965 }
1966
1967 // Print a message to the kernel log about this module. This is
1968 // intended to help debug problems with systemtap modules.
1969 if (! session->runtime_usermode_p())
1970 o->newline() << "_stp_print_kernel_info("
1971 << "\"" << escaped_literal_string(session->script_basename()) << "\""
1972 << ", \"" << VERSION
1973 << "/" << dwfl_version (NULL) << "\""
1974 << ", (num_online_cpus() * sizeof(struct context))"
1975 << ", " << session->probes.size()
1976 << ");";
1977 // In dyninst mode, we need to know when all the globals have been
1978 // allocated and we're ready to run probe registration.
1979 else
1980 {
1981 o->newline() << "rc = stp_session_init_finished();";
1982 o->newline() << "if (rc) goto out;";
1983 }
1984
1985 if (!session->runtime_usermode_p())
1986 {
1987 // Initialize workqueue needed for on-the-fly arming/disarming
1988 o->newline() << "#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)";
1989 o->newline() << "INIT_WORK(&module_refresher_work, module_refresher, NULL);";
1990 o->newline() << "#else";
1991 o->newline() << "INIT_WORK(&module_refresher_work, module_refresher);";
1992 o->newline() << "#endif";
1993 }
1994
1995 // Run all probe registrations. This actually runs begin probes.
1996
1997 for (unsigned i=0; i<g.size(); i++)
1998 {
1999 g[i]->emit_module_init (*session);
2000 // NB: this gives O(N**2) amount of code, but luckily there
2001 // are only seven or eight derived_probe_groups, so it's ok.
2002 o->newline() << "if (rc) {";
2003 // If a probe types's emit_module_init() wants to handle error
2004 // messages itself, it should set probe_point to NULL,
2005 o->newline(1) << "if (probe_point)";
2006 o->newline(1) << "_stp_error (\"probe %s registration error (rc %d)\", probe_point, rc);";
2007 o->indent(-1);
2008 // NB: we need to be in the error state so timers can shutdown cleanly,
2009 // and so end probes don't run. OTOH, error probes can run.
2010 o->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
2011 if (i>0)
2012 for (int j=i-1; j>=0; j--)
2013 g[j]->emit_module_exit (*session);
2014 o->newline() << "goto out;";
2015 o->newline(-1) << "}";
2016 }
2017
2018 // All registrations were successful. Consider the system started.
2019 // NB: only other valid state value is ERROR, in which case we don't
2020 o->newline() << "atomic_cmpxchg(session_state(), STAP_SESSION_STARTING, STAP_SESSION_RUNNING);";
2021
2022 // Run all post-session starting code.
2023 for (unsigned i=0; i<g.size(); i++)
2024 {
2025 g[i]->emit_module_post_init (*session);
2026 }
2027
2028 if (!session->runtime_usermode_p())
2029 {
2030 o->newline() << "#ifdef STP_ON_THE_FLY_TIMER_ENABLE";
2031
2032 // Initialize hrtimer needed for on-the-fly arming/disarming
2033 o->newline() << "hrtimer_init(&module_refresh_timer, CLOCK_MONOTONIC,";
2034 o->newline() << " HRTIMER_MODE_REL);";
2035 o->newline() << "module_refresh_timer.function = &module_refresh_timer_cb;";
2036
2037 // We check here if it's worth it to start the timer at all. We only need
2038 // the background timer if there is a probe which doesn't support directy
2039 // scheduling work (otf_safe_context() == false), but yet does affect the
2040 // condition of at least one probe which supports on-the-fly operations.
2041 {
2042 // for each derived probe...
2043 bool start_timer = false;
2044 for (unsigned i=0; i<session->probes.size() && !start_timer; i++)
2045 {
2046 // if it isn't safe in this probe type to directly schedule work,
2047 // and this probe could affect other probes...
2048 if (session->probes[i]->group
2049 && !session->probes[i]->group->otf_safe_context(*session)
2050 && !session->probes[i]->probes_with_affected_conditions.empty())
2051 {
2052 // and if any of those possible probes support on-the-fly operations,
2053 // then we'll need the timer
2054 for (set<derived_probe*>::const_iterator
2055 it = session->probes[i]->probes_with_affected_conditions.begin();
2056 it != session->probes[i]->probes_with_affected_conditions.end()
2057 && !start_timer; ++it)
2058 {
2059 if ((*it)->group && (*it)->group->otf_supported(*session))
2060 start_timer = true;
2061 }
2062 }
2063 }
2064
2065 if (start_timer)
2066 {
2067 o->newline() << "hrtimer_start(&module_refresh_timer,";
2068 o->newline() << " ktime_set(0, STP_ON_THE_FLY_INTERVAL),";
2069 o->newline() << " HRTIMER_MODE_REL);";
2070 }
2071 }
2072
2073 o->newline() << "#endif /* STP_ON_THE_FLY_TIMER_ENABLE */";
2074 }
2075
2076 o->newline() << "return 0;";
2077
2078 // Error handling path; by now all partially registered probe groups
2079 // have been unregistered.
2080 o->newline(-1) << "deref_fault: __attribute__((unused));";
2081 o->newline(0) << "out:";
2082 o->indent(1);
2083
2084 // If any registrations failed, we will need to deregister the globals,
2085 // as this is our only chance.
2086 for (unsigned i=0; i<session->globals.size(); i++)
2087 {
2088 vardecl* v = session->globals[i];
2089 if (v->index_types.size() > 0)
2090 o->newline() << getmap (v).fini();
2091 else
2092 o->newline() << getvar (v).fini();
2093 }
2094
2095 // For any partially registered/unregistered kernel facilities.
2096 o->newline() << "atomic_set (session_state(), STAP_SESSION_STOPPED);";
2097 o->newline() << "stp_synchronize_sched();";
2098
2099 // In case tracepoints were started, they need to be cleaned up
2100 o->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
2101 o->newline() << " stp_tracepoint_exit();";
2102 o->newline() << "#endif";
2103
2104 // In case gettimeofday was started, it needs to be stopped
2105 o->newline() << "#ifdef STAP_NEED_GETTIMEOFDAY";
2106 o->newline() << " _stp_kill_time();"; // An error is no cause to hurry...
2107 o->newline() << "#endif";
2108
2109 // Free up the context memory after an error too
2110 o->newline() << "_stp_runtime_contexts_free();";
2111
2112 o->newline() << "return rc;";
2113 o->newline(-1) << "}\n";
2114 }
2115
2116
2117 void
2118 c_unparser::emit_module_refresh ()
2119 {
2120 o->newline() << "static void systemtap_module_refresh (const char *modname) {";
2121 o->newline(1) << "int state;";
2122 o->newline() << "int i=0, j=0;"; // for derived_probe_group use
2123
2124 if (!session->runtime_usermode_p())
2125 {
2126 o->newline() << "#if defined(STP_TIMING)";
2127 o->newline() << "cycles_t cycles_atstart = get_cycles();";
2128 o->newline() << "#endif";
2129 }
2130
2131 // Ensure we're only doing the refreshing one at a time. NB: it's important
2132 // that we get the lock prior to checking the session_state, in case whoever
2133 // is holding the lock (e.g. systemtap_module_exit()) changes it.
2134 if (!session->runtime_usermode_p())
2135 o->newline() << "mutex_lock(&module_refresh_mutex);";
2136
2137 /* If we're not in STARTING/RUNNING state, don't try doing any work.
2138 PR16766 */
2139 o->newline() << "state = atomic_read (session_state());";
2140 o->newline() << "if (state != STAP_SESSION_RUNNING && state != STAP_SESSION_STARTING && state != STAP_SESSION_ERROR) {";
2141 // cannot _stp_warn etc. since we're not in probe context
2142 o->newline(1) << "#if defined(__KERNEL__)";
2143 o->newline() << "if (state != STAP_SESSION_STOPPING)";
2144 o->newline(1) << "printk (KERN_ERR \"stap module notifier triggered in unexpected state %d\\n\", state);";
2145 o->indent(-1);
2146 o->newline() << "#endif";
2147
2148 if (!session->runtime_usermode_p())
2149 o->newline() << "mutex_unlock(&module_refresh_mutex);";
2150
2151 o->newline() << "return;";
2152 o->newline(-1) << "}";
2153
2154 o->newline() << "(void) i;";
2155 o->newline() << "(void) j;";
2156
2157 vector<derived_probe_group*> g = all_session_groups (*session);
2158 for (unsigned i=0; i<g.size(); i++)
2159 {
2160 g[i]->emit_module_refresh (*session);
2161 }
2162
2163 if (!session->runtime_usermode_p())
2164 {
2165 // see also common_probe_entryfn_epilogue()
2166 o->newline() << "#if defined(STP_TIMING)";
2167 o->newline() << "if (likely(g_refresh_timing)) {";
2168 o->newline(1) << "cycles_t cycles_atend = get_cycles ();";
2169 o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
2170 o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
2171 o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
2172 o->indent(-1);
2173 // STP_TIMING requires min, max, avg (and thus count and sum) as well as variance.
2174 o->newline() << "_stp_stat_add(g_refresh_timing, cycles_elapsed, 1, 1, 1, 1, 1);";
2175 o->newline(-1) << "}";
2176 o->newline() << "#endif";
2177 }
2178
2179 if (!session->runtime_usermode_p())
2180 o->newline() << "mutex_unlock(&module_refresh_mutex);";
2181
2182 o->newline(-1) << "}\n";
2183 }
2184
2185
2186 void
2187 c_unparser::emit_module_exit ()
2188 {
2189 o->newline() << "static void systemtap_module_exit (void) {";
2190 // rc?
2191 o->newline(1) << "int i=0, j=0;"; // for derived_probe_group use
2192 o->newline() << "(void) i;";
2193 o->newline() << "(void) j;";
2194 // If we aborted startup, then everything has been cleaned up already, and
2195 // module_exit shouldn't even have been called. But since it might be, let's
2196 // beat a hasty retreat to avoid double uninitialization.
2197 o->newline() << "if (atomic_read (session_state()) == STAP_SESSION_STARTING)";
2198 o->newline(1) << "return;";
2199 o->indent(-1);
2200
2201 o->newline() << "if (atomic_read (session_state()) == STAP_SESSION_RUNNING)";
2202 // NB: only other valid state value is ERROR, in which case we don't
2203 o->newline(1) << "atomic_set (session_state(), STAP_SESSION_STOPPING);";
2204 o->indent(-1);
2205 // This signals any other probes that may be invoked in the next little
2206 // while to abort right away. Currently running probes are allowed to
2207 // terminate. These may set STAP_SESSION_ERROR!
2208
2209 if (!session->runtime_usermode_p())
2210 {
2211 o->newline() << "#ifdef STP_ON_THE_FLY_TIMER_ENABLE";
2212 o->newline() << "hrtimer_cancel(&module_refresh_timer);";
2213 o->newline() << "#endif";
2214 }
2215
2216 // cargo cult prologue ... hope to flush any pending workqueue items too
2217 o->newline() << "stp_synchronize_sched();";
2218
2219 // Get the lock before exiting to ensure there's no one in module_refresh
2220 // NB: this should't be able to happen, because both the module_refresh_timer
2221 // and the workqueue ought to have been shut down by now.
2222 if (!session->runtime_usermode_p())
2223 o->newline() << "mutex_lock(&module_refresh_mutex);";
2224
2225 // We're processing the derived_probe_group list in reverse
2226 // order. This ensures that probes get unregistered in reverse
2227 // order of the way they were registered.
2228 vector<derived_probe_group*> g = all_session_groups (*session);
2229 for (vector<derived_probe_group*>::reverse_iterator i = g.rbegin();
2230 i != g.rend(); i++)
2231 (*i)->emit_module_exit (*session); // NB: runs "end" probes
2232
2233 if (!session->runtime_usermode_p())
2234 o->newline() << "mutex_unlock(&module_refresh_mutex);";
2235
2236 // But some other probes may have launched too during unregistration.
2237 // Let's wait a while to make sure they're all done, done, done.
2238
2239 // cargo cult prologue
2240 o->newline() << "stp_synchronize_sched();";
2241
2242 // NB: systemtap_module_exit is assumed to be called from ordinary
2243 // user context, say during module unload. Among other things, this
2244 // means we can sleep a while.
2245 o->newline() << "_stp_runtime_context_wait();";
2246
2247 // cargo cult epilogue
2248 o->newline() << "atomic_set (session_state(), STAP_SESSION_STOPPED);";
2249 o->newline() << "stp_synchronize_sched();";
2250
2251 // XXX: might like to have an escape hatch, in case some probe is
2252 // genuinely stuck somehow
2253
2254 for (unsigned i=0; i<session->globals.size(); i++)
2255 {
2256 vardecl* v = session->globals[i];
2257 if (v->index_types.size() > 0)
2258 o->newline() << getmap (v).fini();
2259 else
2260 o->newline() << getvar (v).fini();
2261 }
2262
2263 // We're finished with the contexts if we're not in dyninst
2264 // mode. The dyninst mode needs the contexts, since print buffers
2265 // are stored there.
2266 if (!session->runtime_usermode_p())
2267 {
2268 o->newline() << "_stp_runtime_contexts_free();";
2269 }
2270 else
2271 {
2272 o->newline() << "struct context* __restrict__ c;";
2273 o->newline() << "c = _stp_runtime_entryfn_get_context();";
2274 }
2275
2276 // teardown tracepoints (if needed)
2277 o->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
2278 o->newline() << " stp_tracepoint_exit();";
2279 o->newline() << "#endif";
2280
2281 // teardown gettimeofday (if needed)
2282 o->newline() << "#ifdef STAP_NEED_GETTIMEOFDAY";
2283 o->newline() << " _stp_kill_time();"; // Go to a beach. Drink a beer.
2284 o->newline() << "#endif";
2285
2286 // NB: PR13386 points out that _stp_printf may be called from contexts
2287 // without already active preempt disabling, which breaks various uses
2288 // of smp_processor_id(). So we temporary block preemption around this
2289 // whole printing block. XXX: get_cpu() / put_cpu() may work just as well.
2290 o->newline() << "preempt_disable();";
2291
2292 // print per probe point timing/alibi statistics
2293 o->newline() << "#if defined(STP_TIMING) || defined(STP_ALIBI)";
2294 o->newline() << "_stp_printf(\"----- probe hit report: \\n\");";
2295 o->newline() << "for (i = 0; i < ARRAY_SIZE(stap_probes); ++i) {";
2296 o->newline(1) << "const struct stap_probe *const p = &stap_probes[i];";
2297 o->newline() << "#ifdef STP_ALIBI";
2298 o->newline() << "int alibi = atomic_read(probe_alibi(i));";
2299 o->newline() << "if (alibi)";
2300 o->newline(1) << "_stp_printf (\"%s, (%s), hits: %d,%s, index: %d\\n\",";
2301 o->newline(2) << "p->pp, p->location, alibi, p->derivation, i);";
2302 o->newline(-3) << "#endif"; // STP_ALIBI
2303 o->newline() << "#ifdef STP_TIMING";
2304 o->newline() << "if (likely (probe_timing(i))) {"; // NB: check for null stat object
2305 o->newline(1) << "struct stat_data *stats = _stp_stat_get (probe_timing(i), 0);";
2306 o->newline() << "if (stats->count) {";
2307 o->newline(1) << "int64_t avg = _stp_div64 (NULL, stats->sum, stats->count);";
2308 o->newline() << "_stp_printf (\"%s, (%s), hits: %lld, "
2309 << (!session->runtime_usermode_p() ? "cycles" : "nsecs")
2310 << ": %lldmin/%lldavg/%lldmax, variance: %lld,%s, index: %d\\n\",";
2311 o->newline(2) << "p->pp, p->location, (long long) stats->count,";
2312 o->newline() << "(long long) stats->min, (long long) avg, (long long) stats->max,";
2313 o->newline() << "(long long) stats->variance, p->derivation, i);";
2314 o->newline(-3) << "}";
2315 o->newline() << "_stp_stat_del (probe_timing(i));";
2316 o->newline(-1) << "}";
2317 o->newline() << "#endif"; // STP_TIMING
2318 o->newline(-1) << "}";
2319
2320 if (!session->runtime_usermode_p())
2321 {
2322 o->newline() << "#if defined(STP_TIMING)";
2323 o->newline() << "_stp_printf(\"----- refresh report:\\n\");";
2324 o->newline() << "if (likely (g_refresh_timing)) {";
2325 o->newline(1) << "struct stat_data *stats = _stp_stat_get (g_refresh_timing, 0);";
2326 o->newline() << "if (stats->count) {";
2327 o->newline(1) << "int64_t avg = _stp_div64 (NULL, stats->sum, stats->count);";
2328 o->newline() << "_stp_printf (\"hits: %lld, cycles: %lldmin/%lldavg/%lldmax, "
2329 << "variance: %lld\\n\",";
2330 o->newline(2) << "(long long) stats->count, (long long) stats->min, ";
2331 o->newline() << "(long long) avg, (long long) stats->max, (long long) stats->variance);";
2332 o->newline(-3) << "}";
2333 o->newline() << "_stp_stat_del (g_refresh_timing);";
2334 o->newline(-1) << "}";
2335 o->newline() << "#endif"; // STP_TIMING
2336 }
2337
2338 o->newline() << "_stp_print_flush();";
2339 o->newline() << "#endif";
2340
2341 //print lock contentions if non-zero
2342 o->newline() << "#ifdef STP_TIMING";
2343 o->newline() << "{";
2344 o->newline(1) << "int ctr;";
2345 for (unsigned i=0; i<session->globals.size(); i++)
2346 {
2347 string orig_vn = session->globals[i]->name;
2348 string vn = c_globalname (orig_vn);
2349 o->newline() << "ctr = atomic_read (global_contended(" << vn << "));";
2350 o->newline() << "if (ctr) _stp_printf(\"'%s' lock contention occurred %d times\\n\", "
2351 << lex_cast_qstring(orig_vn) << ", ctr);";
2352 }
2353 o->newline(-1) << "}";
2354 o->newline() << "_stp_print_flush();";
2355 o->newline () << "#endif";
2356
2357 // print final error/skipped counts if non-zero
2358 o->newline() << "if (atomic_read (skipped_count()) || "
2359 << "atomic_read (error_count()) || "
2360 << "atomic_read (skipped_count_reentrant())) {"; // PR9967
2361 o->newline(1) << "_stp_warn (\"Number of errors: %d, "
2362 << "skipped probes: %d\\n\", "
2363 << "(int) atomic_read (error_count()), "
2364 << "(int) atomic_read (skipped_count()));";
2365 o->newline() << "#ifdef STP_TIMING";
2366 o->newline() << "{";
2367 o->newline(1) << "int ctr;";
2368 for (unsigned i=0; i<session->globals.size(); i++)
2369 {
2370 string orig_vn = session->globals[i]->name;
2371 string vn = c_globalname (orig_vn);
2372 o->newline() << "ctr = atomic_read (global_skipped(" << vn << "));";
2373 o->newline() << "if (ctr) _stp_warn (\"Skipped due to global '%s' lock timeout: %d\\n\", "
2374 << lex_cast_qstring(orig_vn) << ", ctr);";
2375 }
2376 o->newline() << "ctr = atomic_read (skipped_count_lowstack());";
2377 o->newline() << "if (ctr) _stp_warn (\"Skipped due to low stack: %d\\n\", ctr);";
2378 o->newline() << "ctr = atomic_read (skipped_count_reentrant());";
2379 o->newline() << "if (ctr) _stp_warn (\"Skipped due to reentrancy: %d\\n\", ctr);";
2380 o->newline() << "ctr = atomic_read (skipped_count_uprobe_reg());";
2381 o->newline() << "if (ctr) _stp_warn (\"Skipped due to uprobe register failure: %d\\n\", ctr);";
2382 o->newline() << "ctr = atomic_read (skipped_count_uprobe_unreg());";
2383 o->newline() << "if (ctr) _stp_warn (\"Skipped due to uprobe unregister failure: %d\\n\", ctr);";
2384 o->newline(-1) << "}";
2385 o->newline () << "#endif";
2386 o->newline() << "_stp_print_flush();";
2387 o->newline(-1) << "}";
2388
2389 // NB: PR13386 needs to restore preemption-blocking counts
2390 o->newline() << "preempt_enable_no_resched();";
2391
2392 // In dyninst mode, now we're done with the contexts, transport, everything!
2393 if (session->runtime_usermode_p())
2394 {
2395 o->newline() << "_stp_runtime_entryfn_put_context(c);";
2396 o->newline() << "_stp_dyninst_transport_shutdown();";
2397 o->newline() << "_stp_runtime_contexts_free();";
2398 }
2399
2400 o->newline(-1) << "}\n";
2401 }
2402
2403 struct max_action_info: public functioncall_traversing_visitor
2404 {
2405 max_action_info(systemtap_session& s): sess(s), statement_count(0) {}
2406
2407 systemtap_session& sess;
2408 unsigned statement_count;
2409 static const unsigned max_statement_count = ~0;
2410
2411 void add_stmt_count (unsigned val)
2412 {
2413 statement_count = (statement_count > max_statement_count - val) ? max_statement_count : statement_count + val;
2414 }
2415 void add_max_stmt_count () { statement_count = max_statement_count; }
2416 bool statement_count_finite() { return statement_count < max_statement_count; }
2417
2418 void visit_for_loop (for_loop*) { add_max_stmt_count(); }
2419 void visit_foreach_loop (foreach_loop*) { add_max_stmt_count(); }
2420 void visit_expr_statement (expr_statement *stmt)
2421 {
2422 add_stmt_count(1);
2423 traversing_visitor::visit_expr_statement(stmt); // which will trigger visit_functioncall, if applicable
2424 }
2425 void visit_if_statement (if_statement *stmt)
2426 {
2427 add_stmt_count(1);
2428 stmt->condition->visit(this);
2429
2430 // Create new visitors for the two forks. Copy the nested[] set
2431 // to prevent infinite recursion for a function f () { if (a) f() }
2432 max_action_info tmp_visitor_then (*this);
2433 max_action_info tmp_visitor_else (*this);
2434 stmt->thenblock->visit(& tmp_visitor_then);
2435 if (stmt->elseblock)
2436 {
2437 stmt->elseblock->visit(& tmp_visitor_else);
2438 }
2439
2440 // Simply overwrite our copy of statement_count, since these
2441 // visitor copies already included our starting count.
2442 statement_count = max(tmp_visitor_then.statement_count, tmp_visitor_else.statement_count);
2443 }
2444
2445 void note_recursive_functioncall (functioncall *) { add_max_stmt_count(); }
2446
2447 void visit_null_statement (null_statement *) { add_stmt_count(1); }
2448 void visit_return_statement (return_statement *) { add_stmt_count(1); }
2449 void visit_delete_statement (delete_statement *) { add_stmt_count(1); }
2450 void visit_next_statement (next_statement *) { add_stmt_count(1); }
2451 void visit_break_statement (break_statement *) { add_stmt_count(1); }
2452 void visit_continue_statement (continue_statement *) { add_stmt_count(1); }
2453 };
2454
2455 void
2456 c_tmpcounter::emit_function (functiondecl* fd)
2457 {
2458 this->current_probe = 0;
2459 this->current_function = fd;
2460 this->tmpvar_counter = 0;
2461 this->action_counter = 0;
2462 this->already_checked_action_count = false;
2463 declared_vars.clear();
2464
2465 translator_output *o = parent->o;
2466
2467 // indent the dummy output as if we were already in a block
2468 this->o->indent (1);
2469
2470 o->newline() << "struct " << c_funcname (fd->name) << "_locals {";
2471 o->indent(1);
2472
2473 for (unsigned j=0; j<fd->locals.size(); j++)
2474 {
2475 vardecl* v = fd->locals[j];
2476 try
2477 {
2478 if (fd->mangle_oldstyle)
2479 {
2480 // PR14524: retain old way of referring to the locals
2481 o->newline() << "union { "
2482 << c_typename (v->type) << " "
2483 << c_localname (v->name) << "; "
2484 << c_typename (v->type) << " "
2485 << c_localname (v->name, true) << "; };";
2486 }
2487 else
2488 {
2489 o->newline() << c_typename (v->type) << " "
2490 << c_localname (v->name) << ";";
2491 }
2492 } catch (const semantic_error& e) {
2493 semantic_error e2 (e);
2494 if (e2.tok1 == 0) e2.tok1 = v->tok;
2495 throw e2;
2496 }
2497 }
2498
2499 for (unsigned j=0; j<fd->formal_args.size(); j++)
2500 {
2501 vardecl* v = fd->formal_args[j];
2502 try
2503 {
2504 v->char_ptr_arg = (is_unmodified_string_fnarg (session, fd, v));
2505
2506 if (v->char_ptr_arg && session->verbose > 2)
2507 clog << _F("variable %s for function %s will be passed by reference (char *)",
2508 v->name.to_string().c_str(),
2509 fd->unmangled_name.to_string().c_str()) << endl;
2510
2511 if (fd->mangle_oldstyle)
2512 {
2513 // PR14524: retain old way of referring to the locals
2514 o->newline() << "union { "
2515 << (v->char_ptr_arg ? "const char *" : c_typename (v->type))
2516 << " " << c_localname (v->name) << "; "
2517 << (v->char_ptr_arg ? "const char *" : c_typename (v->type))
2518 << " " << c_localname (v->name, true) << "; };";
2519 }
2520 else
2521 {
2522 o->newline() << (v->char_ptr_arg ? "const char *" : c_typename (v->type))
2523 << " " << c_localname (v->name) << ";";
2524 }
2525 } catch (const semantic_error& e) {
2526 semantic_error e2 (e);
2527 if (e2.tok1 == 0) e2.tok1 = v->tok;
2528 throw e2;
2529 }
2530 }
2531
2532 fd->body->visit (this);
2533
2534 if (fd->type == pe_unknown)
2535 o->newline() << "/* no return value */";
2536 else
2537 {
2538 bool as_charp = !session->unoptimized && fd->type == pe_string;
2539 if (as_charp && session->verbose > 2)
2540 clog << _F("return value for function %s will be passed by reference (char *)",
2541 fd->unmangled_name.to_string().c_str()) << endl;
2542 o->newline() << (as_charp ? "char *" : c_typename (fd->type))
2543 << " __retvalue;";
2544 }
2545 o->newline(-1) << "} " << c_funcname (fd->name) << ";";
2546
2547 // finish dummy indentation
2548 this->o->indent (-1);
2549 this->o->assert_0_indent ();
2550
2551 declared_vars.clear();
2552 this->current_function = 0;
2553 this->already_checked_action_count = false;
2554 }
2555
2556 void
2557 c_unparser::emit_function (functiondecl* v)
2558 {
2559 this->current_probe = 0;
2560 this->current_function = v;
2561 this->tmpvar_counter = 0;
2562 this->action_counter = 0;
2563 this->already_checked_action_count = false;
2564
2565 o->newline() << "static void " << c_funcname (v->name)
2566 << " (struct context* __restrict__ c) {";
2567 o->indent(1);
2568
2569 o->newline() << "__label__ deref_fault;";
2570 o->newline() << "__label__ out;";
2571 o->newline()
2572 << "struct " << c_funcname (v->name) << "_locals * "
2573 << " __restrict__ l = "
2574 << "& c->locals[c->nesting+1]." << c_funcname (v->name) // NB: nesting+1
2575 << ";";
2576 o->newline() << "(void) l;"; // make sure "l" is marked used
2577 o->newline() << "#define CONTEXT c";
2578 o->newline() << "#define THIS l";
2579 for (unsigned i = 0; i < v->formal_args.size(); i++) {
2580 o->newline() << c_arg_define(v->formal_args[i]->name); // #define STAP_ARG_foo ...
2581 }
2582 for (unsigned i = 0; i < v->locals.size(); i++) {
2583 o->newline() << c_arg_define(v->locals[i]->name); // #define STAP_ARG_foo ...
2584 }
2585 // define STAP_RETVALUE only if the function is non-void
2586 if (v->type != pe_unknown)
2587 o->newline() << "#define STAP_RETVALUE THIS->__retvalue";
2588
2589 // set this, in case embedded-c code sets last_error but doesn't otherwise identify itself
2590 if (v->tok)
2591 o->newline() << "c->last_stmt = " << lex_cast_qstring(*v->tok) << ";";
2592
2593 // check/increment nesting level
2594 // NB: incoming c->nesting level will be -1 (if we're called directly from a probe),
2595 // or 0...N (if we're called from another function). Incoming parameters are already
2596 // stored in c->locals[c->nesting+1]. See also ::emit_common_header() for more.
2597
2598 o->newline() << "if (unlikely (c->nesting+1 >= MAXNESTING)) {";
2599 o->newline(1) << "c->last_error = ";
2600 o->line() << STAP_T_02;
2601 o->newline() << "return;";
2602 o->newline(-1) << "} else {";
2603 o->newline(1) << "c->nesting ++;";
2604 o->newline(-1) << "}";
2605
2606 // initialize runtime overloading flag
2607 o->newline() << "c->next = 0;";
2608 o->newline() << "#define STAP_NEXT do { c->next = 1; goto out; } while(0)";
2609
2610 // initialize locals
2611 // XXX: optimization: use memset instead
2612 for (unsigned i=0; i<v->locals.size(); i++)
2613 {
2614 if (v->locals[i]->index_types.size() > 0) // array?
2615 throw SEMANTIC_ERROR (_("array locals not supported, missing global declaration?"),
2616 v->locals[i]->tok);
2617
2618 o->newline() << getvar (v->locals[i]).init();
2619 }
2620
2621 // initialize return value, if any
2622 if (v->type != pe_unknown)
2623 {
2624 var retvalue = var(this, true, v->type, "__retvalue", false); // not mangled
2625 o->newline() << retvalue.init();
2626 }
2627
2628 switch (v->type)
2629 {
2630 case pe_long:
2631 o->newline() << "#define STAP_RETURN(v) do { STAP_RETVALUE = (int64_t) (v); "
2632 "goto out; } while(0)";
2633 break;
2634
2635 case pe_string:
2636 o->newline() <<
2637 "#define STAP_RETURN(v) do { strlcpy(STAP_RETVALUE, (v), MAXSTRINGLEN); "
2638 "goto out; } while(0)";
2639 break;
2640
2641 default:
2642 o->newline() << "#define STAP_RETURN() do { goto out; } while(0)";
2643 break;
2644 }
2645
2646 o->newline() << "#define STAP_PRINTF(fmt, ...) do { _stp_printf(fmt, ##__VA_ARGS__); } while (0)";
2647 o->newline() << "#define STAP_ERROR(...) do { snprintf(CONTEXT->error_buffer, MAXSTRINGLEN, __VA_ARGS__); CONTEXT->last_error = CONTEXT->error_buffer; goto out; } while (0)";
2648 o->newline() << "#define return goto out"; // redirect embedded-C return
2649
2650 max_action_info mai (*session);
2651 v->body->visit (&mai);
2652
2653 if (mai.statement_count_finite() && !session->suppress_time_limits
2654 && !session->unoptimized) // this is a finite-statement-count function
2655 {
2656 o->newline() << "if (c->actionremaining < " << mai.statement_count
2657 << ") { c->last_error = " << STAP_T_04 << "goto out; }";
2658 this->already_checked_action_count = true;
2659 }
2660
2661 v->body->visit (this);
2662 o->newline() << "#undef return";
2663 o->newline() << "#undef STAP_PRINTF";
2664 o->newline() << "#undef STAP_ERROR";
2665 o->newline() << "#undef STAP_RETURN";
2666
2667 this->current_function = 0;
2668
2669 record_actions(0, v->body->tok, true);
2670
2671 o->newline(-1) << "deref_fault: __attribute__((unused));";
2672 o->newline(0) << "out: __attribute__((unused));";
2673
2674 // Function prologue: this is why we redirect the "return" above.
2675 // Decrement nesting level.
2676 o->newline(1) << "c->nesting --;";
2677
2678 o->newline() << "#undef CONTEXT";
2679 o->newline() << "#undef THIS";
2680 o->newline() << "#undef STAP_NEXT";
2681 for (unsigned i = 0; i < v->formal_args.size(); i++) {
2682 o->newline() << c_arg_undef(v->formal_args[i]->name); // #undef STAP_ARG_foo
2683 }
2684 for (unsigned i = 0; i < v->locals.size(); i++) {
2685 o->newline() << c_arg_undef(v->locals[i]->name); // #undef STAP_ARG_foo
2686 }
2687 o->newline() << "#undef STAP_RETVALUE";
2688 o->newline(-1) << "}\n";
2689
2690 this->current_function = 0;
2691 this->already_checked_action_count = false;
2692 }
2693
2694 void
2695 c_tmpcounter::emit_probe (derived_probe* dp)
2696 {
2697 this->current_function = 0;
2698 this->current_probe = dp;
2699 this->tmpvar_counter = 0;
2700 this->action_counter = 0;
2701 this->already_checked_action_count = false;
2702 declared_vars.clear();
2703
2704 if (get_probe_dupe (dp) == NULL)
2705 {
2706 translator_output *o = parent->o;
2707
2708 // indent the dummy output as if we were already in a block
2709 this->o->indent (1);
2710
2711 o->newline() << "struct " << dp->name() << "_locals {";
2712 o->indent(1);
2713 for (unsigned j=0; j<dp->locals.size(); j++)
2714 {
2715 vardecl* v = dp->locals[j];
2716 try
2717 {
2718 o->newline() << c_typename (v->type) << " "
2719 << c_localname (v->name) << ";";
2720 } catch (const semantic_error& e) {
2721 semantic_error e2 (e);
2722 if (e2.tok1 == 0) e2.tok1 = v->tok;
2723 throw e2;
2724 }
2725 }
2726
2727 dp->body->visit (this);
2728
2729 // finish by visiting conditions of affected probes to match
2730 // c_unparser::emit_probe()
2731 if (!dp->probes_with_affected_conditions.empty())
2732 {
2733 for (set<derived_probe*>::const_iterator
2734 it = dp->probes_with_affected_conditions.begin();
2735 it != dp->probes_with_affected_conditions.end(); ++it)
2736 (*it)->sole_location()->condition->visit(this);
2737 }
2738
2739 o->newline(-1) << "} " << dp->name() << ";";
2740
2741 // finish dummy indentation
2742 this->o->indent (-1);
2743 this->o->assert_0_indent ();
2744 }
2745
2746 declared_vars.clear();
2747 this->current_probe = 0;
2748 this->already_checked_action_count = false;
2749 }
2750
2751 #define DUPMETHOD_CALL 0
2752 #define DUPMETHOD_ALIAS 0
2753 #define DUPMETHOD_RENAME 1
2754
2755
2756 void
2757 c_unparser::emit_probe (derived_probe* v)
2758 {
2759 this->current_function = 0;
2760 this->current_probe = v;
2761 this->tmpvar_counter = 0;
2762 this->action_counter = 0;
2763 this->already_checked_action_count = false;
2764
2765 // If we about to emit a probe that is exactly the same as another
2766 // probe previously emitted, make the second probe just call the
2767 // first one.
2768 probe *dupe = get_probe_dupe (v);
2769 if (dupe != NULL)
2770 {
2771 // NB: Elision of context variable structs is a separate
2772 // operation which has already taken place by now.
2773 if (session->verbose > 1)
2774 clog << _F("%s elided, duplicates %s\n",
2775 v->name().c_str(), dupe->name().c_str());
2776
2777 #if DUPMETHOD_CALL
2778 // This one emits a direct call to the first copy.
2779 o->newline();
2780 o->newline() << "static void " << v->name() << " (struct context * __restrict__ c) ";
2781 o->newline() << "{ " << dupe->name() << " (c); }";
2782 #elif DUPMETHOD_ALIAS
2783 // This one defines a function alias, arranging gcc to emit
2784 // several equivalent symbols for the same function body.
2785 // For some reason, on gcc 4.1, this is twice as slow as
2786 // the CALL option.
2787 o->newline();
2788 o->newline() << "static void " << v->name() << " (struct context * __restrict__ c) ";
2789 o->line() << "__attribute__ ((alias (\"" << dupe->name() << "\")));";
2790 #elif DUPMETHOD_RENAME
2791 // This one is sneaky. It emits nothing for duplicate probe
2792 // handlers. It instead redirects subsequent references to the
2793 // probe handler function to the first copy, *by name*.
2794 v->id = dupe->id;
2795 #else
2796 #error "Unknown duplicate elimination method"
2797 #endif
2798 }
2799 else // This probe is unique. Remember it and output it.
2800 {
2801 o->newline();
2802 o->newline() << "static void " << v->name() << " (struct context * __restrict__ c) ";
2803 o->line () << "{";
2804 o->indent (1);
2805
2806 o->newline() << "__label__ deref_fault;";
2807 o->newline() << "__label__ out;";
2808
2809 // emit static read/write lock decls for global variables
2810 if (v->needs_global_locks ())
2811 {
2812 varuse_collecting_visitor vut(*session);
2813 v->body->visit (& vut);
2814
2815 // also visit any probe conditions which this current probe might
2816 // evaluate so that read locks are emitted as necessary: e.g. suppose
2817 // probe X if (a || b) {...} probe Y {a = ...} probe Z {b = ...}
2818 // then Y and Z will already write-lock a and b respectively, but they
2819 // also need a read-lock on b and a respectively, since they will read
2820 // them when evaluating the new cond_enabled field (see c_unparser::
2821 // emit_probe_condition_update()).
2822 for (set<derived_probe*>::const_iterator
2823 it = v->probes_with_affected_conditions.begin();
2824 it != v->probes_with_affected_conditions.end(); ++it)
2825 {
2826 assert((*it)->sole_location()->condition != NULL);
2827 (*it)->sole_location()->condition->visit (& vut);
2828 }
2829
2830 emit_lock_decls (vut);
2831 }
2832
2833 // initialize frame pointer
2834 o->newline() << "struct " << v->name() << "_locals * __restrict__ l = "
2835 << "& c->probe_locals." << v->name() << ";";
2836 o->newline() << "(void) l;"; // make sure "l" is marked used
2837
2838 // Emit runtime safety net for unprivileged mode.
2839 // NB: In usermode, the system restricts our privilege for us.
2840 if (!session->runtime_usermode_p())
2841 v->emit_privilege_assertion (o);
2842
2843 // emit probe local initialization block
2844
2845 v->emit_probe_local_init(*this->session, o);
2846
2847 // emit all read/write locks for global variables
2848 if (v->needs_global_locks ())
2849 emit_locks ();
2850
2851 // initialize locals
2852 for (unsigned j=0; j<v->locals.size(); j++)
2853 {
2854 if (v->locals[j]->synthetic)
2855 continue;
2856 if (v->locals[j]->index_types.size() > 0) // array?
2857 throw SEMANTIC_ERROR (_("array locals not supported, missing global declaration?"),
2858 v->locals[j]->tok);
2859 else if (v->locals[j]->type == pe_long)
2860 o->newline() << "l->" << c_localname (v->locals[j]->name)
2861 << " = 0;";
2862 else if (v->locals[j]->type == pe_string)
2863 o->newline() << "l->" << c_localname (v->locals[j]->name)
2864 << "[0] = '\\0';";
2865 else
2866 throw SEMANTIC_ERROR (_("unsupported local variable type"),
2867 v->locals[j]->tok);
2868 }
2869
2870 v->initialize_probe_context_vars (o);
2871
2872 max_action_info mai (*session);
2873 v->body->visit (&mai);
2874 if (session->verbose > 1)
2875 clog << _F("%d statements for probe %s", mai.statement_count,
2876 v->name().c_str()) << endl;
2877
2878 if (mai.statement_count_finite() && !session->suppress_time_limits
2879 && !session->unoptimized) // this is a finite-statement-count probe
2880 {
2881 o->newline() << "if (c->actionremaining < " << mai.statement_count
2882 << ") { c->last_error = " << STAP_T_04 << " goto out; }";
2883 this->already_checked_action_count = true;
2884 }
2885
2886
2887 v->body->visit (this);
2888
2889 record_actions(0, v->body->tok, true);
2890
2891 o->newline(-1) << "deref_fault: __attribute__((unused));";
2892 o->newline(0) << "out:";
2893 // NB: no need to uninitialize locals, except if arrays/stats can
2894 // someday be local
2895
2896 o->indent(1);
2897
2898 if (!v->probes_with_affected_conditions.empty())
2899 {
2900 for (set<derived_probe*>::const_iterator
2901 it = v->probes_with_affected_conditions.begin();
2902 it != v->probes_with_affected_conditions.end(); ++it)
2903 emit_probe_condition_update(*it);
2904 }
2905
2906 if (v->needs_global_locks ())
2907 emit_unlocks ();
2908
2909 // XXX: do this flush only if the body included a
2910 // print/printf/etc. routine!
2911 o->newline() << "_stp_print_flush();";
2912 o->newline(-1) << "}\n";
2913 }
2914
2915 this->current_probe = 0;
2916 this->already_checked_action_count = false;
2917 }
2918
2919 // Updates the cond_enabled field and sets need_module_refresh if it was
2920 // changed.
2921 void
2922 c_unparser::emit_probe_condition_update(derived_probe* v)
2923 {
2924 unsigned i = v->session_index;
2925 assert(i < session->probes.size());
2926
2927 expression *cond = v->sole_location()->condition;
2928 assert(cond);
2929
2930 string cond_enabled = "stap_probes[" + lex_cast(i) + "].cond_enabled";
2931
2932 // Concurrency note: we're safe modifying cond_enabled here since we emit
2933 // locks not only for globals we write to, but also for globals read in other
2934 // probes' whose conditions we visit below (see in c_unparser::emit_probe). So
2935 // we can be assured we're the only ones modifying cond_enabled.
2936
2937 o->newline() << "if (" << cond_enabled << " != ";
2938 o->line() << "!!"; // NB: turn general integer into boolean 1 or 0
2939 v->sole_location()->condition->visit(this);
2940 o->line() << ") {";
2941 o->newline(1) << cond_enabled << " ^= 1;"; // toggle it
2942
2943 // don't bother refreshing if on-the-fly not supported
2944 if (!session->runtime_usermode_p()
2945 && v->group && v->group->otf_supported(*session))
2946 o->newline() << "atomic_set(&need_module_refresh, 1);";
2947
2948 o->newline(-1) << "}";
2949 }
2950
2951 void
2952 c_unparser::emit_lock_decls(const varuse_collecting_visitor& vut)
2953 {
2954 unsigned numvars = 0;
2955
2956 if (session->verbose > 1)
2957 clog << "probe " << current_probe->session_index << " "
2958 "('" << *current_probe->sole_location() << "') locks";
2959
2960 // We can only make this static in kernel mode. In stapdyn mode,
2961 // the globals and their locks are in shared memory.
2962 o->newline();
2963 if (!session->runtime_usermode_p())
2964 o->line() << "static ";
2965 o->line() << "const struct stp_probe_lock locks[] = {";
2966 o->indent(1);
2967
2968 for (unsigned i = 0; i < session->globals.size(); i++)
2969 {
2970 vardecl* v = session->globals[i];
2971 bool read_p = vut.read.count(v) > 0;
2972 bool write_p = vut.written.count(v) > 0;
2973 if (!read_p && !write_p) continue;
2974
2975 bool written_p;
2976 if (v->type == pe_stats) // read and write locks are flipped
2977 // Specifically, a "<<<" to a stats object is considered a
2978 // "shared-lock" operation, since it's implicitly done
2979 // per-cpu. But a "@op(x)" extraction is an "exclusive-lock"
2980 // one, as is a (sorted or unsorted) foreach, so those cases
2981 // are excluded by the w & !r condition below.
2982 {
2983 if (write_p && !read_p) { read_p = true; write_p = false; }
2984 else if (read_p && !write_p) { read_p = false; write_p = true; }
2985 written_p = vcv_needs_global_locks.read.count(v) > 0;
2986 }
2987 else
2988 written_p = vcv_needs_global_locks.written.count(v) > 0;
2989
2990 // We don't need to read lock "read-mostly" global variables. A
2991 // "read-mostly" global variable is only written to within
2992 // probes that don't need global variable locking (such as
2993 // begin/end probes). If vcv_needs_global_locks doesn't mark
2994 // the global as written to, then we don't have to lock it
2995 // here to read it safely.
2996 if (!written_p && read_p && !write_p)
2997 continue;
2998
2999 o->newline() << "{";
3000 o->newline(1) << ".lock = global_lock(" + c_globalname(v->name) + "),";
3001 o->newline() << ".write_p = " << (write_p ? 1 : 0) << ",";
3002 o->newline() << "#ifdef STP_TIMING";
3003 o->newline() << ".skipped = global_skipped(" << c_globalname (v->name) << "),";
3004 o->newline() << ".contention = global_contended(" << c_globalname (v->name) << "),";
3005 o->newline() << "#endif";
3006 o->newline(-1) << "},";
3007
3008 numvars ++;
3009 if (session->verbose > 1)
3010 clog << " " << v->name << "[" << (read_p ? "r" : "")
3011 << (write_p ? "w" : "") << "]";
3012 }
3013
3014 o->newline(-1) << "};";
3015
3016 if (session->verbose > 1)
3017 {
3018 if (!numvars)
3019 clog << _(" nothing");
3020 clog << endl;
3021 }
3022 }
3023
3024
3025 void
3026 c_unparser::emit_locks()
3027 {
3028 o->newline() << "if (!stp_lock_probe(locks, ARRAY_SIZE(locks)))";
3029 o->newline(1) << "return;";
3030 o->indent(-1);
3031 }
3032
3033
3034 void
3035 c_unparser::emit_unlocks()
3036 {
3037 o->newline() << "stp_unlock_probe(locks, ARRAY_SIZE(locks));";
3038 }
3039
3040
3041 void
3042 c_unparser::collect_map_index_types(vector<vardecl *> const & vars,
3043 set< pair<vector<exp_type>, exp_type> > & types)
3044 {
3045 for (unsigned i = 0; i < vars.size(); ++i)
3046 {
3047 vardecl *v = vars[i];
3048 if (v->arity > 0)
3049 {
3050 types.insert(make_pair(v->index_types, v->type));
3051 }
3052 }
3053 }
3054
3055 string
3056 mapvar::value_typename(exp_type e)
3057 {
3058 switch (e)
3059 {
3060 case pe_long:
3061 return "INT64";
3062 case pe_string:
3063 return "STRING";
3064 case pe_stats:
3065 return "STAT";
3066 default:
3067 throw SEMANTIC_ERROR(_("array type is neither string nor long"));
3068 }
3069 }
3070
3071 string
3072 mapvar::key_typename(exp_type e)
3073 {
3074 switch (e)
3075 {
3076 case pe_long:
3077 return "INT64";
3078 case pe_string:
3079 return "STRING";
3080 default:
3081 throw SEMANTIC_ERROR(_("array key is neither string nor long"));
3082 }
3083 }
3084
3085 string
3086 mapvar::shortname(exp_type e)
3087 {
3088 switch (e)
3089 {
3090 case pe_long:
3091 return "i";
3092 case pe_string:
3093 return "s";
3094 default:
3095 throw SEMANTIC_ERROR(_("array type is neither string nor long"));
3096 }
3097 }
3098
3099 string
3100 c_unparser::map_keytypes(vardecl* v)
3101 {
3102 string result;
3103 vector<exp_type> types = v->index_types;
3104 types.push_back (v->type);
3105 for (unsigned i = 0; i < types.size(); ++i)
3106 {
3107 switch (types[i])
3108 {
3109 case pe_long:
3110 result += 'i';
3111 break;
3112 case pe_string:
3113 result += 's';
3114 break;
3115 case pe_stats:
3116 result += 'x';
3117 break;
3118 default:
3119 throw SEMANTIC_ERROR(_("unknown type of map"));
3120 break;
3121 }
3122 }
3123 return result;
3124 }
3125
3126 void
3127 c_unparser::emit_map_type_instantiations ()
3128 {
3129 set< pair<vector<exp_type>, exp_type> > types;
3130
3131 collect_map_index_types(session->globals, types);
3132
3133 for (unsigned i = 0; i < session->probes.size(); ++i)
3134 collect_map_index_types(session->probes[i]->locals, types);
3135
3136 for (map<string,functiondecl*>::iterator it = session->functions.begin(); it != session->functions.end(); it++)
3137 collect_map_index_types(it->second->locals, types);
3138
3139 if (!types.empty())
3140 o->newline() << "#include \"alloc.c\"";
3141
3142 for (set< pair<vector<exp_type>, exp_type> >::const_iterator i = types.begin();
3143 i != types.end(); ++i)
3144 {
3145 o->newline() << "#define VALUE_TYPE " << mapvar::value_typename(i->second);
3146 for (unsigned j = 0; j < i->first.size(); ++j)
3147 {
3148 string ktype = mapvar::key_typename(i->first.at(j));
3149 o->newline() << "#define KEY" << (j+1) << "_TYPE " << ktype;
3150 }
3151 /* For statistics, flag map-gen to pull in nested pmap-gen too. */
3152 if (i->second == pe_stats)
3153 o->newline() << "#define MAP_DO_PMAP 1";
3154 o->newline() << "#include \"map-gen.c\"";
3155 o->newline() << "#undef MAP_DO_PMAP";
3156 o->newline() << "#undef VALUE_TYPE";
3157 for (unsigned j = 0; j < i->first.size(); ++j)
3158 {
3159 o->newline() << "#undef KEY" << (j+1) << "_TYPE";
3160 }
3161 }
3162
3163 if (!types.empty())
3164 o->newline() << "#include \"map.c\"";
3165
3166 };
3167
3168
3169 string
3170 c_unparser::c_typename (exp_type e)
3171 {
3172 switch (e)
3173 {
3174 case pe_long: return string("int64_t");
3175 case pe_string: return string("string_t");
3176 case pe_stats: return string("Stat");
3177 case pe_unknown:
3178 default:
3179 throw SEMANTIC_ERROR (_("cannot expand unknown type"));
3180 }
3181 }
3182
3183
3184 // XXX: the below is just for the sake of example; it's possibly
3185 // better to expose the hash function in hash.cxx
3186
3187 // unsigned int
3188 // do_hash (const char *e)
3189 // {
3190 // unsigned int foo = 0;
3191 // while (*e) {
3192 // foo *= 101; foo += *e; e++;
3193 // }
3194 // return foo;
3195 // }
3196
3197
3198 string
3199 c_unparser::c_localname (const string& e, bool mangle_oldstyle)
3200 {
3201 if (strverscmp(session->compatible.c_str(), "1.8") < 0 || mangle_oldstyle)
3202 return e; // old mangling behaviour
3203 else
3204 // XXX: we may wish to invent and/or test other mangling schemes, e.g.:
3205 // return "l_" + e + "_" + lex_cast(do_hash(e.c_str()));
3206 return "l_" + e;
3207 }
3208
3209
3210 string
3211 c_unparser::c_globalname (const string& e)
3212 {
3213 // XXX uncomment to test custom mangling:
3214 // return "s_" + e + "_" + lex_cast(do_hash(e.c_str()));
3215 return "s_" + e;
3216 }
3217
3218
3219 string
3220 c_unparser::c_funcname (const string& e)
3221 {
3222 // XXX uncomment to test custom mangling:
3223 // return "function_" + e + "_" + lex_cast(do_hash(e.c_str()));
3224 return "function_" + e;
3225 }
3226
3227
3228 string
3229 c_unparser::c_arg_define (const string& e)
3230 {
3231 return "#define STAP_ARG_" + e + " THIS->" + c_localname(e);
3232 }
3233
3234
3235 string
3236 c_unparser::c_arg_undef (const string& e)
3237 {
3238 return "#undef STAP_ARG_" + e;
3239 }
3240
3241 void
3242 c_unparser::c_global_write_def(vardecl* v)
3243 {
3244 if (v->arity > 0)
3245 {
3246 o->newline() << "#define STAP_GLOBAL_SET_" << v->unmangled_name << "(...) "
3247 << "({int rc = _stp_map_set_" << map_keytypes(v)
3248 << "(global(" << c_globalname(v->name) << "), __VA_ARGS__); "
3249 << "if (unlikely(rc)) { c->last_error = " << STAP_T_01
3250 << lex_cast(v->maxsize > 0 ? "size limit (" + lex_cast(v->maxsize)
3251 + ")" : "MAXMAPENTRIES") + "\"; goto out; } rc;})";
3252 }
3253 else
3254 {
3255 o->newline() << "#define STAP_GLOBAL_SET_" << v->unmangled_name << "(val) ";
3256 if (v->type == pe_string)
3257 o->line() << "strlcpy(global(" << c_globalname(v->name) << "), val, MAXSTRINGLEN)";
3258 else if (v->type == pe_long)
3259 o->line() << "global_set(" << c_globalname(v->name) << ", val)";
3260 }
3261 }
3262
3263 void
3264 c_unparser::c_global_read_def(vardecl* v)
3265 {
3266 if (v->arity > 0)
3267 {
3268 o->newline() << "#define STAP_GLOBAL_GET_" << v->unmangled_name << "(...) "
3269 << "_stp_map_get_" << map_keytypes(v)
3270 << "(global(" << c_globalname(v->name) << "), __VA_ARGS__)";
3271 }
3272 else
3273 {
3274 o->newline() << "#define STAP_GLOBAL_GET_" << v->unmangled_name << "() "
3275 << "global(" << c_globalname(v->name) << ")";
3276 }
3277 }
3278
3279 void
3280 c_unparser::c_global_write_undef(vardecl* v)
3281 {
3282 o->newline() << "#undef STAP_GLOBAL_SET_" << v->unmangled_name;
3283 }
3284
3285 void
3286 c_unparser::c_global_read_undef(vardecl* v)
3287 {
3288 o->newline() << "#undef STAP_GLOBAL_GET_" << v->unmangled_name;
3289 }
3290
3291 void
3292 c_unparser::c_assign (var& lvalue, const string& rvalue, const token *tok)
3293 {
3294 switch (lvalue.type())
3295 {
3296 case pe_string:
3297 c_strcpy(lvalue.value(), rvalue);
3298 break;
3299 case pe_long:
3300 o->newline() << lvalue << " = " << rvalue << ";";
3301 break;
3302 default:
3303 throw SEMANTIC_ERROR (_("unknown lvalue type in assignment"), tok);
3304 }
3305 }
3306
3307
3308 void
3309 c_unparser::c_assign(tmpvar& t, expression *e, const char* msg)
3310 {
3311 // We don't really need a tmpvar if the expression is a literal.
3312 // (NB: determined by the expression itself, not tok->type!)
3313
3314 if (dynamic_cast<literal*>(e))
3315 {
3316 // We need to use the visitors to get proper C values, like
3317 // "((int64_t)5LL)" for numbers and escaped characters in strings.
3318
3319 // Create a fake output stream so we can grab the string output.
3320 ostringstream oss;
3321 translator_output tmp_o(oss);
3322
3323 // Temporarily swap out the real translator_output stream with our
3324 // fake one.
3325 translator_output *saved_o = o;
3326 o = &tmp_o;
3327
3328 // Visit the expression then restore the original output stream
3329 e->visit (this);
3330 o = saved_o;
3331
3332 // All instances of this tmpvar will use the literal value.
3333 t.override (oss.str());
3334 }
3335 else
3336 c_assign (t.value(), e, msg);
3337 }
3338
3339 struct expression_is_functioncall : public nop_visitor
3340 {
3341 functioncall* fncall;
3342 expression_is_functioncall ()
3343 : fncall(NULL) {}
3344
3345 void visit_functioncall (functioncall* e)
3346 {
3347 fncall = e;
3348 }
3349 };
3350
3351 void
3352 c_unparser::c_assign (const string& lvalue, expression* rvalue,
3353 const char* msg)
3354 {
3355 if (rvalue->type == pe_long)
3356 {
3357 o->newline() << lvalue << " = ";
3358 rvalue->visit (this);
3359 o->line() << ";";
3360 }
3361 else if (rvalue->type == pe_string)
3362 {
3363 expression_is_functioncall eif;
3364 rvalue->visit(& eif);
3365 if (!session->unoptimized && eif.fncall)
3366 {
3367 const functioncall* saved_fncall = assigned_functioncall;
3368 const string* saved_retval = assigned_functioncall_retval;
3369
3370 // let the functioncall know that the return value is being saved/used
3371 // and keep track of the lvalue, so that the retval assignment can
3372 // happen in ::visit_functioncall, to avoid complications with nesting.
3373 assigned_functioncall = eif.fncall;
3374 assigned_functioncall_retval = &lvalue;
3375 eif.fncall->visit (this);
3376 o->line() << ";";
3377
3378 assigned_functioncall = saved_fncall;
3379 assigned_functioncall_retval = saved_retval;
3380 }
3381 else
3382 {
3383 // will call rvalue->visit()
3384 c_strcpy (lvalue, rvalue);
3385 }
3386 }
3387 else
3388 {
3389 string fullmsg = string(msg) + _(" type unsupported");
3390 throw SEMANTIC_ERROR (fullmsg, rvalue->tok);
3391 }
3392 }
3393
3394
3395 void
3396 c_unparser::c_assign (const string& lvalue, const string& rvalue,
3397 exp_type type, const char* msg, const token* tok)
3398 {
3399 if (type == pe_long)
3400 {
3401 o->newline() << lvalue << " = " << rvalue << ";";
3402 }
3403 else if (type == pe_string)
3404 {
3405 c_strcpy (lvalue, rvalue);
3406 }
3407 else
3408 {
3409 string fullmsg = string(msg) + _(" type unsupported");
3410 throw SEMANTIC_ERROR (fullmsg, tok);
3411 }
3412 }
3413
3414
3415 void
3416 c_unparser_assignment::c_assignop(tmpvar & res,
3417 var const & lval,
3418 tmpvar const & rval,
3419 token const * tok)
3420 {
3421 // This is common code used by scalar and array-element assignments.
3422 // It assumes an operator-and-assignment (defined by the 'pre' and
3423 // 'op' fields of c_unparser_assignment) is taking place between the
3424 // following set of variables:
3425 //
3426 // res: the result of evaluating the expression, a temporary
3427 // lval: the lvalue of the expression, which may be damaged
3428 // rval: the rvalue of the expression, which is a temporary or constant
3429
3430 // we'd like to work with a local tmpvar so we can overwrite it in
3431 // some optimized cases
3432
3433 translator_output* o = parent->o;
3434
3435 if (res.type() == pe_string)
3436 {
3437 if (post)
3438 throw SEMANTIC_ERROR (_("post assignment on strings not supported"),
3439 tok);
3440 if (op == "=")
3441 {
3442 parent->c_strcpy (lval.value(), rval.value());
3443 // no need for second copy
3444 res = rval;
3445 }
3446 else if (op == ".=")
3447 {
3448 parent->c_strcat (lval.value(), rval.value());
3449 res = lval;
3450 }
3451 else
3452 throw SEMANTIC_ERROR (_F("string assignment operator %s unsupported",
3453 op.to_string().c_str()), tok);
3454 }
3455 else if (op == "<<<")
3456 {
3457 int stat_op_count = lval.sdecl().stat_ops & (STAT_OP_COUNT|STAT_OP_AVG|STAT_OP_VARIANCE);
3458 int stat_op_sum = lval.sdecl().stat_ops & (STAT_OP_SUM|STAT_OP_AVG|STAT_OP_VARIANCE);
3459 int stat_op_min = lval.sdecl().stat_ops & STAT_OP_MIN;
3460 int stat_op_max = lval.sdecl().stat_ops & STAT_OP_MAX;
3461 int stat_op_variance = lval.sdecl().stat_ops & STAT_OP_VARIANCE;
3462
3463 assert(lval.type() == pe_stats);
3464 assert(rval.type() == pe_long);
3465 assert(res.type() == pe_long);
3466
3467 o->newline() << "_stp_stat_add (" << lval << ", " << rval << ", " <<
3468 stat_op_count << ", " << stat_op_sum << ", " <<
3469 stat_op_min << ", " << stat_op_max << ", " <<
3470 stat_op_variance << ");";
3471 res = rval;
3472 }
3473 else if (res.type() == pe_long)
3474 {
3475 // a lot of operators come through this "gate":
3476 // - vanilla assignment "="
3477 // - stats aggregation "<<<"
3478 // - modify-accumulate "+=" and many friends
3479 // - pre/post-crement "++"/"--"
3480 // - "/" and "%" operators, but these need special handling in kernel
3481
3482 // compute the modify portion of a modify-accumulate
3483 string macop;
3484 unsigned oplen = op.size();
3485 if (op == "=")
3486 macop = "*error*"; // special shortcuts below
3487 else if (op == "++" || op == "+=")
3488 macop = "+=";
3489 else if (op == "--" || op == "-=")
3490 macop = "-=";
3491 else if (oplen > 1 && op[oplen-1] == '=') // for *=, <<=, etc...
3492 macop = op;
3493 else
3494 // internal error
3495 throw SEMANTIC_ERROR (_("unknown macop for assignment"), tok);
3496
3497 if (post)
3498 {
3499 if (macop == "/" || macop == "%" || op == "=")
3500 throw SEMANTIC_ERROR (_("invalid post-mode operator"), tok);
3501
3502 o->newline() << res << " = " << lval << ";";
3503
3504 if (macop == "+=" || macop == "-=")
3505 o->newline() << lval << " " << macop << " " << rval << ";";
3506 else
3507 o->newline() << lval << " = " << res << " " << macop << " " << rval << ";";
3508 }
3509 else
3510 {
3511 if (op == "=") // shortcut simple assignment
3512 {
3513 o->newline() << lval << " = " << rval << ";";
3514 res = rval;
3515 }
3516 else
3517 {
3518 if (macop == "/=" || macop == "%=")
3519 {
3520 o->newline() << "if (unlikely(!" << rval << ")) {";
3521 o->newline(1) << "c->last_error = ";
3522 o->line() << STAP_T_03;
3523 o->newline() << "c->last_stmt = " << lex_cast_qstring(*rvalue->tok) << ";";
3524 o->newline() << "goto out;";
3525 o->newline(-1) << "}";
3526 o->newline() << lval << " = "
3527 << ((macop == "/=") ? "_stp_div64" : "_stp_mod64")
3528 << " (NULL, " << lval << ", " << rval << ");";
3529 }
3530 else
3531 o->newline() << lval << " " << macop << " " << rval << ";";
3532 res = lval;
3533 }
3534 }
3535 }
3536 else
3537 throw SEMANTIC_ERROR (_("assignment type not yet implemented"), tok);
3538 }
3539
3540
3541 void
3542 c_unparser::c_declare(exp_type ty, const string &ident)
3543 {
3544 o->newline() << c_typename (ty) << " " << ident << ";";
3545 }
3546
3547
3548 void
3549 c_unparser::c_declare_static(exp_type ty, const string &ident)
3550 {
3551 o->newline() << "static " << c_typename (ty) << " " << ident << ";";
3552 }
3553
3554
3555 void
3556 c_unparser::c_strcpy (const string& lvalue, const string& rvalue)
3557 {
3558 o->newline() << "strlcpy ("
3559 << lvalue << ", "
3560 << rvalue << ", MAXSTRINGLEN);";
3561 }
3562
3563
3564 void
3565 c_unparser::c_strcpy (const string& lvalue, expression* rvalue)
3566 {
3567 o->newline() << "strlcpy (" << lvalue << ", ";
3568 rvalue->visit (this);
3569 o->line() << ", MAXSTRINGLEN);";
3570 }
3571
3572
3573 void
3574 c_unparser::c_strcat (const string& lvalue, const string& rvalue)
3575 {
3576 o->newline() << "strlcat ("
3577 << lvalue << ", "
3578 << rvalue << ", MAXSTRINGLEN);";
3579 }
3580
3581
3582 void
3583 c_unparser::c_strcat (const string& lvalue, expression* rvalue)
3584 {
3585 o->newline() << "strlcat (" << lvalue << ", ";
3586 rvalue->visit (this);
3587 o->line() << ", MAXSTRINGLEN);";
3588 }
3589
3590
3591 bool
3592 c_unparser::is_local(vardecl const *r, token const *tok)
3593 {
3594 if (current_probe)
3595 {
3596 for (unsigned i=0; i<current_probe->locals.size(); i++)
3597 {
3598 if (current_probe->locals[i] == r)
3599 return true;
3600 }
3601 }
3602 else if (current_function)
3603 {
3604 for (unsigned i=0; i<current_function->locals.size(); i++)
3605 {
3606 if (current_function->locals[i] == r)
3607 return true;
3608 }
3609
3610 for (unsigned i=0; i<current_function->formal_args.size(); i++)
3611 {
3612 if (current_function->formal_args[i] == r)
3613 return true;
3614 }
3615 }
3616
3617 for (unsigned i=0; i<session->globals.size(); i++)
3618 {
3619 if (session->globals[i] == r)
3620 return false;
3621 }
3622
3623 if (tok)
3624 throw SEMANTIC_ERROR (_("unresolved symbol"), tok);
3625 else
3626 throw SEMANTIC_ERROR (_("unresolved symbol: ") + (string)r->name);
3627 }
3628
3629
3630 tmpvar
3631 c_unparser::gensym(exp_type ty)
3632 {
3633 return tmpvar (this, ty, tmpvar_counter);
3634 }
3635
3636 aggvar
3637 c_unparser::gensym_aggregate()
3638 {
3639 return aggvar (this, tmpvar_counter);
3640 }
3641
3642
3643 var
3644 c_unparser::getvar(vardecl *v, token const *tok)
3645 {
3646 bool loc = is_local (v, tok);
3647 if (loc)
3648 return var (this, loc, v->type, v->name);
3649 else
3650 {
3651 statistic_decl sd;
3652 std::map<interned_string, statistic_decl>::const_iterator i;
3653 i = session->stat_decls.find(v->name);
3654 if (i != session->stat_decls.end())
3655 sd = i->second;
3656 return var (this, loc, v->type, sd, v->name);
3657 }
3658 }
3659
3660
3661 mapvar
3662 c_unparser::getmap(vardecl *v, token const *tok)
3663 {
3664 if (v->arity < 1)
3665 throw SEMANTIC_ERROR(_("attempt to use scalar where map expected"), tok);
3666 statistic_decl sd;
3667 std::map<interned_string, statistic_decl>::const_iterator i;
3668 i = session->stat_decls.find(v->name);
3669 if (i != session->stat_decls.end())
3670 sd = i->second;
3671 return mapvar (this, is_local (v, tok), v->type, sd,
3672 v->name, v->index_types, v->maxsize, v->wrap);
3673 }
3674
3675
3676 itervar
3677 c_unparser::getiter(symbol *s)
3678 {
3679 return itervar (this, s, tmpvar_counter);
3680 }
3681
3682
3683 // Queue up some actions to remove from actionremaining. Set update=true at
3684 // the end of basic blocks to actually update actionremaining and check it
3685 // against MAXACTION.
3686 void
3687 c_unparser::record_actions (unsigned actions, const token* tok, bool update)
3688 {
3689 action_counter += actions;
3690
3691 // Update if needed, or after queueing up a few actions, in case of very
3692 // large code sequences.
3693 if (((update && action_counter > 0) || action_counter >= 10/*<-arbitrary*/)
3694 && !session->suppress_time_limits && !already_checked_action_count)
3695 {
3696
3697 o->newline() << "c->actionremaining -= " << action_counter << ";";
3698 o->newline() << "if (unlikely (c->actionremaining <= 0)) {";
3699 o->newline(1) << "c->last_error = ";
3700 o->line() << STAP_T_04;
3701
3702 // XXX it really ought to be illegal for anything to be missing a token,
3703 // but until we're sure of that, we need to defend against NULL.
3704 if (tok)
3705 o->newline() << "c->last_stmt = " << lex_cast_qstring(*tok) << ";";
3706
3707 o->newline() << "goto out;";
3708 o->newline(-1) << "}";
3709 action_counter = 0;
3710 }
3711 }
3712
3713
3714 void
3715 c_unparser::visit_block (block *s)
3716 {
3717 // Key insight: individual statements of a block can reuse
3718 // temporary variable slots, since temporaries don't survive
3719 // statement boundaries. So we use gcc's anonymous union/struct
3720 // facility to explicitly overlay the temporaries.
3721 start_compound_statement ("block_statement", s);
3722
3723 o->newline() << "{";
3724 o->indent (1);
3725
3726 for (unsigned i=0; i<s->statements.size(); i++)
3727 {
3728 try
3729 {
3730 wrap_compound_visit (s->statements[i]);
3731 o->newline();
3732 }
3733 catch (const semantic_error& e)
3734 {
3735 session->print_error (e);
3736 }
3737 }
3738 o->newline(-1) << "}";
3739
3740 close_compound_statement ("block_statement", s);
3741 }
3742
3743
3744 void c_unparser::visit_try_block (try_block *s)
3745 {
3746 record_actions(0, s->tok, true); // flush prior actions
3747
3748 start_compound_statement ("try_block", s);
3749
3750 o->newline() << "{";
3751 o->newline(1) << "__label__ normal_fallthrough;";
3752 o->newline(1) << "{";
3753 o->newline() << "__label__ deref_fault;";
3754 o->newline() << "__label__ out;";
3755
3756 assert (!session->unoptimized || s->try_block); // dead_stmtexpr_remover would zap it
3757 if (s->try_block)
3758 {
3759 wrap_compound_visit (s->try_block);
3760 record_actions(0, s->try_block->tok, true); // flush accumulated actions
3761 }
3762 o->newline() << "goto normal_fallthrough;";
3763
3764 o->newline() << "deref_fault: __attribute__((unused));";
3765 o->newline() << "out: __attribute__((unused));";
3766
3767 // Close the scope of the above nested 'out' label, to make sure
3768 // that the catch block, should it encounter errors, does not resolve
3769 // a 'goto out;' to the above label, causing infinite looping.
3770 o->newline(-1) << "}";
3771
3772 o->newline() << "if (likely(c->last_error == NULL)) goto out;";
3773
3774 if (s->catch_error_var)
3775 {
3776 var cev(getvar(s->catch_error_var->referent, s->catch_error_var->tok));
3777 c_strcpy (cev.value(), "c->last_error");
3778 }
3779 o->newline() << "c->last_error = NULL;";
3780
3781 // Prevent the catch{} handler from even starting if MAXACTIONS have
3782 // already been used up. Add one for the act of catching too.
3783 record_actions(1, s->tok, true);
3784
3785 if (s->catch_block)
3786 {
3787 wrap_compound_visit (s->catch_block);
3788 record_actions(0, s->catch_block->tok, true); // flush accumulated actions
3789 }
3790
3791 o->newline() << "normal_fallthrough:";
3792 o->newline() << ";"; // to have _some_ statement
3793 o->newline(-1) << "}";
3794
3795 close_compound_statement ("try_block", s);
3796 }
3797
3798
3799 void
3800 c_unparser::visit_embeddedcode (embeddedcode *s)
3801 {
3802 // Automatically add a call to assert_is_myproc to any code tagged with
3803 // /* myproc-unprivileged */
3804 if (s->tagged_p ("/* myproc-unprivileged */"))
3805 o->newline() << "assert_is_myproc();";
3806 o->newline() << "{";
3807
3808 // if (1 || s->tagged_p ("CATCH_DEREF_FAULT"))
3809 // o->newline() << "__label__ deref_fault;";
3810
3811 vector<vardecl*> read_defs;
3812 vector<vardecl*> write_defs;
3813 for (unsigned i = 0; i < session->globals.size(); i++)
3814 {
3815 vardecl* v = session->globals[i];
3816 if (v->synthetic) continue; /* skip synthetic variables; embedded c can't access them. */
3817 string name = v->unmangled_name;
3818 assert (name != "");
3819 if (s->tagged_p("/* pragma:read:" + name + " */"))
3820 {
3821 c_global_read_def(v);
3822 read_defs.push_back(v);
3823 }
3824 if (s->tagged_p("/* pragma:write:" + name + " */"))
3825 {
3826 c_global_write_def(v);
3827 write_defs.push_back(v);
3828 }
3829 }
3830
3831 o->newline(1) << s->code;
3832 o->indent(-1);
3833
3834 for (vector<vardecl*>::const_iterator it = read_defs.begin(); it != read_defs.end(); ++it)
3835 c_global_read_undef(*it);
3836 for (vector<vardecl*>::const_iterator it = write_defs.begin(); it != write_defs.end(); ++it)
3837 c_global_write_undef(*it);
3838
3839 // if (1 || s->tagged_p ("CATCH_DEREF_FAULT"))
3840 // o->newline() << ";";
3841
3842 o->newline() << "}";
3843 }
3844
3845
3846 void
3847 c_unparser::visit_null_statement (null_statement *)
3848 {
3849 o->newline() << "/* null */;";
3850 }
3851
3852
3853 void
3854 c_unparser::visit_expr_statement (expr_statement *s)
3855 {
3856 o->newline() << "(void) ";
3857 s->value->visit (this);
3858 o->line() << ";";
3859 record_actions(1, s->tok);
3860 }
3861
3862
3863 void
3864 c_tmpcounter::wrap_compound_visit (statement *s)
3865 {
3866 if (!s) return;
3867
3868 std::ostream::pos_type before_struct_pos;
3869 std::ostream::pos_type after_struct_pos;
3870
3871 start_struct_def(before_struct_pos, after_struct_pos, s->tok);
3872 c_unparser::wrap_compound_visit (s);
3873 close_struct_def(before_struct_pos, after_struct_pos);
3874 }
3875
3876 void
3877 c_tmpcounter::wrap_compound_visit (expression *e)
3878 {
3879 if (!e) return;
3880
3881 std::ostream::pos_type before_struct_pos;
3882 std::ostream::pos_type after_struct_pos;
3883
3884 start_struct_def(before_struct_pos, after_struct_pos, e->tok);
3885 c_unparser::wrap_compound_visit (e);
3886 close_struct_def(before_struct_pos, after_struct_pos);
3887 }
3888
3889 void
3890 c_tmpcounter::start_struct_def (std::ostream::pos_type &before,
3891 std::ostream::pos_type &after, const token* tok)
3892 {
3893 // To avoid lots of empty structs, remember where we are now. Then,
3894 // output the struct start and remember that positon. If when we get
3895 // done with the statement we haven't moved, then we don't really need
3896 // the struct. To get rid of the struct start we output, we'll seek back
3897 // to where we were before we output the struct (done in ::close_struct_def).
3898 translator_output *o = parent->o;
3899 before = o->tellp();
3900 o->newline() << "struct { /* source: " << tok->location.file->name
3901 << ":" << lex_cast(tok->location.line) << " */";
3902 o->indent(1);
3903 after = o->tellp();
3904 }
3905
3906 void
3907 c_tmpcounter::close_struct_def (std::ostream::pos_type before,
3908 std::ostream::pos_type after)
3909 {
3910 // meant to be used with ::start_struct_def. remove the struct if empty.
3911 translator_output *o = parent->o;
3912 o->indent(-1);
3913 if (after == o->tellp())
3914 o->seekp(before);
3915 else
3916 o->newline() << "};";
3917 }
3918
3919 void
3920 c_tmpcounter::start_compound_statement (const char* tag, statement *s)
3921 {
3922 const source_loc& loc = s->tok->location;
3923 translator_output *o = parent->o;
3924 o->newline() << "union { /* " << tag << ": "
3925 << loc.file->name << ":"
3926 << lex_cast(loc.line) << " */";
3927 o->indent(1);
3928 }
3929
3930 void
3931 c_tmpcounter::close_compound_statement (const char*, statement *)
3932 {
3933 translator_output *o = parent->o;
3934 o->newline(-1) << "};";
3935 }
3936
3937
3938 void
3939 c_unparser::visit_if_statement (if_statement *s)
3940 {
3941 record_actions(1, s->tok, true);
3942
3943 start_compound_statement ("if_statement", s);
3944
3945 o->newline() << "if (";
3946 o->indent (1);
3947 wrap_compound_visit (s->condition);
3948 o->indent (-1);
3949 o->line() << ") {";
3950 o->indent (1);
3951 wrap_compound_visit (s->thenblock);
3952 record_actions(0, s->thenblock->tok, true);
3953 o->newline(-1) << "}";
3954 if (s->elseblock)
3955 {
3956 o->newline() << "else {";
3957 o->indent (1);
3958 wrap_compound_visit (s->elseblock);
3959 record_actions(0, s->elseblock->tok, true);
3960 o->newline(-1) << "}";
3961 }
3962
3963 close_compound_statement ("if_statement", s);
3964 }
3965
3966
3967 void
3968 c_unparser::visit_for_loop (for_loop *s)
3969 {
3970 string ctr = lex_cast (label_counter++);
3971 string toplabel = "top_" + ctr;
3972 string contlabel = "continue_" + ctr;
3973 string breaklabel = "break_" + ctr;
3974
3975 start_compound_statement ("for_loop", s);
3976
3977 // initialization
3978 wrap_compound_visit (s->init);
3979 record_actions(1, s->tok, true);
3980
3981 // condition
3982 o->newline(-1) << toplabel << ":";
3983
3984 // Emit an explicit action here to cover the act of iteration.
3985 // Equivalently, it can stand for the evaluation of the condition
3986 // expression.
3987 o->indent(1);
3988 record_actions(1, s->tok);
3989
3990 o->newline() << "if (! (";
3991 if (s->cond->type != pe_long)
3992 throw SEMANTIC_ERROR (_("expected numeric type"), s->cond->tok);
3993 wrap_compound_visit (s->cond);
3994 o->line() << ")) goto " << breaklabel << ";";
3995
3996 // body
3997 loop_break_labels.push_back (breaklabel);
3998 loop_continue_labels.push_back (contlabel);
3999 wrap_compound_visit (s->block);
4000 record_actions(0, s->block->tok, true);
4001 loop_break_labels.pop_back ();
4002 loop_continue_labels.pop_back ();
4003
4004 // iteration
4005 o->newline(-1) << contlabel << ":";
4006 o->indent(1);
4007 wrap_compound_visit (s->incr);
4008 o->newline() << "goto " << toplabel << ";";
4009
4010 // exit
4011 o->newline(-1) << breaklabel << ":";
4012 o->newline(1) << "; /* dummy statement */";
4013
4014 close_compound_statement ("for_loop", s);
4015 }
4016
4017
4018 struct arrayindex_downcaster
4019 : public traversing_visitor
4020 {
4021 arrayindex *& arr;
4022
4023 arrayindex_downcaster (arrayindex *& arr)
4024 : arr(arr)
4025 {}
4026
4027 void visit_arrayindex (arrayindex* e)
4028 {
4029 arr = e;
4030 }
4031 };
4032
4033
4034 static bool
4035 expression_is_arrayindex (expression *e,
4036 arrayindex *& hist)
4037 {
4038 arrayindex *h = NULL;
4039 arrayindex_downcaster d(h);
4040 e->visit (&d);
4041 if (static_cast<void*>(h) == static_cast<void*>(e))
4042 {
4043 hist = h;
4044 return true;
4045 }
4046 return false;
4047 }
4048
4049
4050 // Look for opportunities to used a saved value at the beginning of the loop
4051 void
4052 c_unparser::visit_foreach_loop_value (foreach_loop* s, const string& value)
4053 {
4054 bool stable_value = false;
4055
4056 // There are three possible cases that we might easily retrieve the value:
4057 // 1. foreach ([keys] in any_array_type)
4058 // 2. foreach (idx in @hist_*(stat))
4059 // 3. foreach (idx in @hist_*(stat[keys]))
4060 //
4061 // For 1 and 2, we just need to check that the keys/idx are const throughout
4062 // the loop. For 3, we'd have to check also that the arbitrary keys
4063 // expressions indexing the stat are const -- much harder, so I'm punting
4064 // that case for now.
4065
4066 symbol *array;
4067 hist_op *hist;
4068 classify_indexable (s->base, array, hist);
4069
4070 if (!(hist && get_symbol_within_expression(hist->stat)->referent->arity > 0))
4071 {
4072 set<vardecl*> indexes;
4073 for (unsigned i=0; i < s->indexes.size(); ++i)
4074 indexes.insert(s->indexes[i]->referent);
4075
4076 varuse_collecting_visitor v(*session);
4077 s->block->visit (&v);
4078 v.embedded_seen = false; // reset because we only care about the indexes
4079 if (v.side_effect_free_wrt(indexes))
4080 stable_value = true;
4081 }
4082
4083 if (stable_value)
4084 {
4085 // Rather than trying to compare arrayindexes to this foreach_loop
4086 // manually, we just create a fake arrayindex that would match the
4087 // foreach_loop, render it as a string, and later render encountered
4088 // arrayindexes as strings and compare.
4089 arrayindex ai;
4090 ai.base = s->base;
4091 for (unsigned i=0; i < s->indexes.size(); ++i)
4092 ai.indexes.push_back(s->indexes[i]);
4093 string loopai = lex_cast(ai);
4094 foreach_loop_values[loopai] = value;
4095 s->block->visit (this);
4096 foreach_loop_values.erase(loopai);
4097 }
4098 else
4099 s->block->visit (this);
4100 }
4101
4102
4103 bool
4104 c_unparser::get_foreach_loop_value (arrayindex* ai, string& value)
4105 {
4106 if (!ai)
4107 return false;
4108 map<string,string>::iterator it = foreach_loop_values.find(lex_cast(*ai));
4109 if (it == foreach_loop_values.end())
4110 return false;
4111 value = it->second;
4112 return true;
4113 }
4114
4115
4116 void
4117 c_unparser::visit_foreach_loop (foreach_loop *s)
4118 {
4119 symbol *array;
4120 hist_op *hist;
4121 classify_indexable (s->base, array, hist);
4122
4123 string ctr = lex_cast (label_counter++);
4124 string toplabel = "top_" + ctr;
4125 string contlabel = "continue_" + ctr;
4126 string breaklabel = "break_" + ctr;
4127
4128 if (array)
4129 {
4130 mapvar mv = getmap (array->referent, s->tok);
4131 vector<var> keys;
4132
4133 // NB: structure parallels for_loop
4134
4135 // initialization
4136
4137 tmpvar *res_limit = NULL;
4138 if (s->limit)
4139 {
4140 // Evaluate the limit expression once.
4141 res_limit = new tmpvar(gensym(pe_long));
4142 c_assign (*res_limit, s->limit, "foreach limit");
4143 }
4144
4145 // aggregate array if required
4146 if (mv.is_parallel())
4147 {
4148 o->newline() << "if (unlikely(NULL == " << mv.calculate_aggregate() << ")) {";
4149 o->newline(1) << "c->last_error = ";
4150 o->line() << STAP_T_05 << mv << "\";";
4151 o->newline() << "c->last_stmt = " << lex_cast_qstring(*s->tok) << ";";
4152 o->newline() << "goto out;";
4153 o->newline(-1) << "}";
4154
4155 // sort array if desired
4156 if (s->sort_direction)
4157 {
4158 string sort_column;
4159
4160 // If the user wanted us to sort by value, we'll sort by
4161 // @count or selected function instead for aggregates.
4162 // See runtime/map.c
4163 if (s->sort_column == 0)
4164 switch (s->sort_aggr) {
4165 default: case sc_none: case sc_count: sort_column = "SORT_COUNT"; break;
4166 case sc_sum: sort_column = "SORT_SUM"; break;
4167 case sc_min: sort_column = "SORT_MIN"; break;
4168 case sc_max: sort_column = "SORT_MAX"; break;
4169 case sc_average: sort_column = "SORT_AVG"; break;
4170 }
4171 else
4172 sort_column = lex_cast(s->sort_column);
4173
4174 o->newline() << "else"; // only sort if aggregation was ok
4175 if (s->limit)
4176 {
4177 o->newline(1) << mv.function_keysym("sortn", true) <<" ("
4178 << mv.fetch_existing_aggregate() << ", "
4179 << *res_limit << ", " << sort_column << ", "
4180 << - s->sort_direction << ");";
4181 }
4182 else
4183 {
4184 o->newline(1) << mv.function_keysym("sort", true) <<" ("
4185 << mv.fetch_existing_aggregate() << ", "
4186 << sort_column << ", "
4187 << - s->sort_direction << ");";
4188 }
4189 o->indent(-1);
4190 }
4191 }
4192 else
4193 {
4194 // sort array if desired
4195 if (s->sort_direction)
4196 {
4197 if (s->limit)
4198 {
4199 o->newline() << mv.function_keysym("sortn") <<" ("
4200 << mv.value() << ", "
4201 << *res_limit << ", " << s->sort_column << ", "
4202 << - s->sort_direction << ");";
4203 }
4204 else
4205 {
4206 o->newline() << mv.function_keysym("sort") <<" ("
4207 << mv.value() << ", "
4208 << s->sort_column << ", "
4209 << - s->sort_direction << ");";
4210 }
4211 }
4212 }
4213
4214 // NB: sort direction sense is opposite in runtime, thus the negation
4215
4216 tmpvar *limitv = NULL;
4217 if (s->limit)
4218 {
4219 // Create the loop limit variable here and initialize it.
4220 limitv = new tmpvar(gensym (pe_long));
4221 o->newline() << *limitv << " = 0LL;";
4222 }
4223
4224 if (mv.is_parallel())
4225 aggregations_active.insert(mv.value());
4226
4227 itervar iv = getiter (array);
4228 o->newline() << iv << " = " << iv.start (mv) << ";";
4229
4230 vector<tmpvar *> array_slice_vars;
4231 // store the the variables corresponding to the index of the array slice
4232 // as temporary variables
4233 if (!s->array_slice.empty())
4234 for (unsigned i = 0; i < s->array_slice.size(); ++i)
4235 {
4236 if (s->array_slice[i])
4237 {
4238 tmpvar *asvar = new tmpvar(gensym(s->array_slice[i]->type));
4239 c_assign(*asvar, s->array_slice[i], "array slice index");
4240 array_slice_vars.push_back(asvar);
4241 }
4242 else
4243 array_slice_vars.push_back(NULL);
4244 }
4245
4246 record_actions(1, s->tok, true);
4247
4248 // condition
4249 o->newline(-1) << toplabel << ":";
4250
4251 // Emit an explicit action here to cover the act of iteration.
4252 // Equivalently, it can stand for the evaluation of the
4253 // condition expression.
4254 o->indent(1);
4255 record_actions(1, s->tok);
4256
4257 o->newline() << "if (! (" << iv << ")) goto " << breaklabel << ";";
4258
4259 // body
4260 loop_break_labels.push_back (breaklabel);
4261 loop_continue_labels.push_back (contlabel);
4262 o->newline() << "{";
4263 o->indent (1);
4264
4265 if (s->limit)
4266 {
4267 // If we've been through LIMIT loop iterations, quit.
4268 o->newline() << "if (" << *limitv << "++ >= " << *res_limit
4269 << ") goto " << breaklabel << ";";
4270
4271 // We're done with limitv and res_limit.
4272 delete limitv;
4273 delete res_limit;
4274 }
4275
4276 for (unsigned i = 0; i < s->indexes.size(); ++i)
4277 {
4278 // copy the iter values into the specified locals
4279 var v = getvar (s->indexes[i]->referent);
4280 c_assign (v, iv.get_key (mv, v.type(), i), s->tok);
4281 }
4282
4283 // in the case that the user specified something like
4284 // foreach ([a,b] in foo[*, 123]), need to check that it iterates over
4285 // the specified values, ie b is alwasy going to be 123
4286 if (!s->array_slice.empty())
4287 {
4288 //add in the beginning portion of the if statement
4289 o->newline() << "if (0"; // in case all are wildcards
4290 for (unsigned i = 0; i < s->array_slice.size(); ++i)
4291
4292 // only output a comparsion if the expression is not "*".
4293 if (s->array_slice[i])
4294 {
4295 o->line() << " || ";
4296 if (s->indexes[i]->type == pe_string)
4297 {
4298 if (s->array_slice[i]->type != pe_string)
4299 throw SEMANTIC_ERROR (_("expected string types"), s->tok);
4300 o->line() << "strncmp(" << getvar (s->indexes[i]->referent)
4301 << ", " << *array_slice_vars[i];
4302 o->line() << ", MAXSTRINGLEN) !=0";
4303 }
4304 else if (s->indexes[i]->type == pe_long)
4305 {
4306 if (s->array_slice[i]->type != pe_long)
4307 throw SEMANTIC_ERROR (_("expected numeric types"), s->tok);
4308 o->line() << getvar (s->indexes[i]->referent) << " != "
4309 << *array_slice_vars[i];
4310 }
4311 else
4312 {
4313 throw SEMANTIC_ERROR (_("unexpected type"), s->tok);
4314 }
4315 }
4316 o->line() << ") goto " << contlabel << ";"; // end of the if statment
4317 }
4318
4319 if (s->value)
4320 {
4321 var v = getvar (s->value->referent);
4322 c_assign (v, iv.get_value (mv, v.type()), s->tok);
4323 }
4324
4325 visit_foreach_loop_value(s, iv.get_value(mv, array->type));
4326 record_actions(0, s->block->tok, true);
4327 o->newline(-1) << "}";
4328 loop_break_labels.pop_back ();
4329 loop_continue_labels.pop_back ();
4330
4331 // iteration
4332 o->newline(-1) << contlabel << ":";
4333 o->newline(1) << iv << " = " << iv.next (mv) << ";";
4334 o->newline() << "goto " << toplabel << ";";
4335
4336 // exit
4337 o->newline(-1) << breaklabel << ":";
4338 o->newline(1) << "; /* dummy statement */";
4339
4340 if (mv.is_parallel())
4341 aggregations_active.erase(mv.value());
4342 }
4343 else
4344 {
4345 // Iterating over buckets in a histogram.
4346
4347 // First make sure we have exactly one pe_long variable to use as
4348 // our bucket index.
4349 if (s->indexes.size() != 1 || s->indexes[0]->referent->type != pe_long)
4350 throw SEMANTIC_ERROR(_("Invalid indexing of histogram"), s->tok);
4351
4352 tmpvar *res_limit = NULL;
4353 tmpvar *limitv = NULL;
4354 if (s->limit)
4355 {
4356 // Evaluate the limit expression once.
4357 res_limit = new tmpvar(gensym(pe_long));
4358 c_assign (*res_limit, s->limit, "foreach limit");
4359
4360 // Create the loop limit variable here and initialize it.
4361 limitv = new tmpvar(gensym (pe_long));
4362 o->newline() << *limitv << " = 0LL;";
4363 }
4364
4365 var bucketvar = getvar (s->indexes[0]->referent);
4366
4367 aggvar agg = gensym_aggregate ();
4368
4369 var *v = load_aggregate(hist->stat, agg);
4370 v->assert_hist_compatible(*hist);
4371
4372 record_actions(1, s->tok, true);
4373 o->newline() << "for (" << bucketvar << " = 0; "
4374 << bucketvar << " < " << v->buckets() << "; "
4375 << bucketvar << "++) { ";
4376 o->newline(1);
4377 loop_break_labels.push_back (breaklabel);
4378 loop_continue_labels.push_back (contlabel);
4379
4380 if (s->limit)
4381 {
4382 // If we've been through LIMIT loop iterations, quit.
4383 o->newline() << "if (" << *limitv << "++ >= " << *res_limit
4384 << ") break;";
4385
4386 // We're done with limitv and res_limit.
4387 delete limitv;
4388 delete res_limit;
4389 }
4390
4391 if (s->value)
4392 {
4393 var v = getvar (s->value->referent);
4394 c_assign (v, agg.get_hist (bucketvar), s->tok);
4395 }
4396
4397 visit_foreach_loop_value(s, agg.get_hist(bucketvar));
4398 record_actions(1, s->block->tok, true);
4399
4400 o->newline(-1) << contlabel << ":";
4401 o->newline(1) << "continue;";
4402 o->newline(-1) << breaklabel << ":";
4403 o->newline(1) << "break;";
4404 o->newline(-1) << "}";
4405 loop_break_labels.pop_back ();
4406 loop_continue_labels.pop_back ();
4407
4408 delete v;
4409 }
4410 }
4411
4412
4413 void
4414 c_unparser::visit_return_statement (return_statement* s)
4415 {
4416 if (current_function == 0)
4417 throw SEMANTIC_ERROR (_("cannot 'return' from probe"), s->tok);
4418
4419 if (s->value)
4420 {
4421 if (s->value->type != current_function->type)
4422 throw SEMANTIC_ERROR (_("return type mismatch"), current_function->tok,
4423 s->tok);
4424
4425 c_assign ("l->__retvalue", s->value, "return value");
4426 }
4427 else if (current_function->type != pe_unknown)
4428 throw SEMANTIC_ERROR (_("return type mismatch"), current_function->tok,
4429 s->tok);
4430
4431 record_actions(1, s->tok, true);
4432 o->newline() << "goto out;";
4433 }
4434
4435
4436 void
4437 c_unparser::visit_next_statement (next_statement* s)
4438 {
4439 /* Set next flag to indicate to caller to call next alternative function */
4440 if (current_function != 0)
4441 o->newline() << "c->next = 1;";
4442
4443 record_actions(1, s->tok, true);
4444 o->newline() << "goto out;";
4445 }
4446
4447
4448 struct delete_statement_operand_visitor:
4449 public throwing_visitor
4450 {
4451 c_unparser *parent;
4452 delete_statement_operand_visitor (c_unparser *p):
4453 throwing_visitor (_("invalid operand of delete expression")),
4454 parent (p)
4455 {}
4456 void visit_symbol (symbol* e);
4457 void visit_arrayindex (arrayindex* e);
4458 };
4459
4460 void
4461 delete_statement_operand_visitor::visit_symbol (symbol* e)
4462 {
4463 translator_output* o = parent->o;
4464 assert (e->referent != 0);
4465 if (e->referent->arity > 0)
4466 {
4467 mapvar mvar = parent->getmap(e->referent, e->tok);
4468 /* NB: Memory deallocation/allocation operations
4469 are not generally safe.
4470 o->newline() << mvar.fini ();
4471 o->newline() << mvar.init ();
4472 */
4473 if (mvar.is_parallel())
4474 o->newline() << "_stp_pmap_clear (" << mvar.value() << ");";
4475 else
4476 o->newline() << "_stp_map_clear (" << mvar.value() << ");";
4477 }
4478 else
4479 {
4480 var v = parent->getvar(e->referent, e->tok);
4481 switch (e->type)
4482 {
4483 case pe_stats:
4484 o->newline() << "_stp_stat_clear (" << v.value() << ");";
4485 break;
4486 case pe_long:
4487 o->newline() << v.value() << " = 0;";
4488 break;
4489 case pe_string:
4490 o->newline() << v.value() << "[0] = '\\0';";
4491 break;
4492 case pe_unknown:
4493 default:
4494 throw SEMANTIC_ERROR(_("Cannot delete unknown expression type"), e->tok);
4495 }
4496 }
4497 }
4498
4499 void
4500 delete_statement_operand_visitor::visit_arrayindex (arrayindex* e)
4501 {
4502 symbol *array;
4503 hist_op *hist;
4504 classify_indexable (e->base, array, hist);
4505 translator_output* o = parent->o;
4506
4507 if (array)
4508 {
4509 bool array_slice = false;
4510 for (unsigned i = 0; i < e->indexes.size(); i ++)
4511 if (e->indexes[i] == NULL)
4512 {
4513 array_slice = true;
4514 break;
4515 }
4516
4517 if (!array_slice) // delete a single element
4518 {
4519 vector<tmpvar> idx;
4520 parent->load_map_indices (e, idx);
4521 mapvar mvar = parent->getmap (array->referent, e->tok);
4522 o->newline() << mvar.del (idx) << ";";
4523 }
4524 else // delete elements if they match the array slice.
4525 {
4526 vardecl* r = array->referent;
4527 mapvar mvar = parent->getmap (r, e->tok);
4528 itervar iv = parent->getiter(array);
4529
4530 // create tmpvars for the array indexes, storing NULL where there is
4531 // no specific value that the index should be
4532 vector<tmpvar *> array_slice_vars;
4533 vector<tmpvar> idx; // for the indexes if the variable is a pmap
4534 for (unsigned i=0; i<e->indexes.size(); i++)
4535 {
4536 if (e->indexes[i])
4537 {
4538 tmpvar *asvar = new tmpvar(parent->gensym(e->indexes[i]->type));
4539 parent->c_assign (*asvar, e->indexes[i], "tmp var");
4540 array_slice_vars.push_back(asvar);
4541 if (mvar.is_parallel())
4542 idx.push_back(*asvar);
4543 }
4544 else
4545 {
4546 array_slice_vars.push_back(NULL);
4547 if (mvar.is_parallel())
4548 {
4549 tmpvar *asvar = new tmpvar(parent->gensym(r->index_types[i]));
4550 idx.push_back(*asvar);
4551 }
4552 }
4553 }
4554
4555 if (mvar.is_parallel())
4556 {
4557 o->newline() << "if (unlikely(NULL == "
4558 << mvar.calculate_aggregate() << ")) {";
4559 o->newline(1) << "c->last_error = ";
4560 o->line() << STAP_T_05 << mvar << "\";";
4561 o->newline() << "c->last_stmt = "
4562 << lex_cast_qstring(*e->tok) << ";";
4563 o->newline() << "goto out;";
4564 o->newline(-1) << "}";
4565 }
4566
4567 // iterate through the map, deleting elements that match the array slice
4568 string ctr = lex_cast (parent->label_counter++);
4569 string toplabel = "top_" + ctr;
4570 string breaklabel = "break_" + ctr;
4571
4572 o->newline() << iv << " = " << iv.start(mvar) << ";";
4573 o->newline() << toplabel << ":";
4574
4575 o->newline(1) << "if (!(" << iv << ")){";
4576 o->newline(1) << "goto " << breaklabel << ";}";
4577
4578 // insert the comparison for keys that aren't wildcards
4579 o->newline(-1) << "if (1"; // in case all are wildcards
4580 for (unsigned i=0; i<array_slice_vars.size(); i++)
4581 if (array_slice_vars[i] != NULL)
4582 {
4583 if (array_slice_vars[i]->type() == pe_long)
4584 o->line() << " && " << *array_slice_vars[i] << " == "
4585 << iv.get_key(mvar, array_slice_vars[i]->type(), i);
4586 else if (array_slice_vars[i]->type() == pe_string)
4587 o->line() << " && strncmp(" << *array_slice_vars[i] << ", "
4588 << iv.get_key(mvar, array_slice_vars[i]->type(), i)
4589 << ", MAXSTRINGLEN) == 0";
4590 else
4591 throw SEMANTIC_ERROR (_("unexpected type"), e->tok);
4592 }
4593
4594 o->line() << ") {";
4595
4596 // conditional is true, so delete item and go to the next item
4597 if (mvar.is_parallel())
4598 {
4599 o->indent(1);
4600 // fills in the wildcards with the current iteration's (map) indexes
4601 for (unsigned i = 0; i<array_slice_vars.size(); i++)
4602 if (array_slice_vars[i] == NULL)
4603 parent->c_assign (idx[i].value(),
4604 iv.get_key(mvar, r->index_types[i], i),
4605 r->index_types[i], "tmpvar", e->tok);
4606 o->newline() << iv << " = " << iv.next(mvar) << ";";
4607 o->newline() << mvar.del(idx) << ";";
4608 }
4609 else
4610 o->newline(1) << iv << " = " << iv.del_next(mvar) << ";";
4611
4612 o->newline(-1) << "} else";
4613 o->newline(1) << iv << " = " << iv.next(mvar) << ";";
4614
4615 o->newline(-1) << "goto " << toplabel << ";";
4616
4617 o->newline(-1) << breaklabel<< ":";
4618 o->newline(1) << "; /* dummy statement */";
4619 o->indent(-1);
4620 }
4621 }
4622 else
4623 {
4624 throw SEMANTIC_ERROR(_("cannot delete histogram bucket entries\n"), e->tok);
4625 }
4626 }
4627
4628
4629 void
4630 c_unparser::visit_delete_statement (delete_statement* s)
4631 {
4632 delete_statement_operand_visitor dv (this);
4633 s->value->visit (&dv);
4634 record_actions(1, s->tok);
4635 }
4636
4637
4638 void
4639 c_unparser::visit_break_statement (break_statement* s)
4640 {
4641 if (loop_break_labels.empty())
4642 throw SEMANTIC_ERROR (_("cannot 'break' outside loop"), s->tok);
4643
4644 record_actions(1, s->tok, true);
4645 o->newline() << "goto " << loop_break_labels.back() << ";";
4646 }
4647
4648
4649 void
4650 c_unparser::visit_continue_statement (continue_statement* s)
4651 {
4652 if (loop_continue_labels.empty())
4653 throw SEMANTIC_ERROR (_("cannot 'continue' outside loop"), s->tok);
4654
4655 record_actions(1, s->tok, true);
4656 o->newline() << "goto " << loop_continue_labels.back() << ";";
4657 }
4658
4659
4660
4661 void
4662 c_unparser::visit_literal_string (literal_string* e)
4663 {
4664 interned_string v = e->value;
4665 o->line() << '"';
4666 for (unsigned i=0; i<v.size(); i++)
4667 // NB: The backslash character is specifically passed through as is.
4668 // This is because our parser treats "\" as an ordinary character, not
4669 // an escape sequence, leaving it to the C compiler (and this function)
4670 // to treat it as such. If we were to escape it, there would be no way
4671 // of generating C-level escapes from script code.
4672 // See also print_format::components_to_string and lex_cast_qstring
4673 if (v[i] == '"') // or other escapeworthy characters?
4674 o->line() << '\\' << '"';
4675 else
4676 o->line() << v[i];
4677 o->line() << '"';
4678 }
4679
4680
4681 void
4682 c_unparser::visit_literal_number (literal_number* e)
4683 {
4684 // This looks ugly, but tries to be warning-free on 32- and 64-bit
4685 // hosts.
4686 // NB: this needs to be signed!
4687 if (e->value == -9223372036854775807LL-1) // PR 5023
4688 o->line() << "((int64_t)" << (unsigned long long) e->value << "ULL)";
4689 else
4690 o->line() << "((int64_t)" << e->value << "LL)";
4691 }
4692
4693
4694 void
4695 c_unparser::visit_embedded_expr (embedded_expr* e)
4696 {
4697 bool has_defines = false;
4698 vector<vardecl*> read_defs;
4699 vector<vardecl*> write_defs;
4700 for (unsigned i = 0; i < session->globals.size(); i++)
4701 {
4702 vardecl* v = session->globals[i];
4703 if (v->synthetic) continue; /* skip synthetic variables; embedded c can't access them. */
4704 string name = v->unmangled_name;
4705 assert (name != "");
4706 if (e->tagged_p ("/* pragma:read:" + name + " */"))
4707 {
4708 has_defines = true;
4709 c_global_read_def(v);
4710 read_defs.push_back(v);
4711 }
4712 if (e->tagged_p ("/* pragma:write:" + name + " */"))
4713 {
4714 has_defines = true;
4715 c_global_write_def(v);
4716 write_defs.push_back(v);
4717 }
4718 }
4719
4720 if (has_defines)
4721 o->newline();
4722
4723 o->line() << "(";
4724
4725 // Automatically add a call to assert_is_myproc to any code tagged with
4726 // /* myproc-unprivileged */
4727 if (e->tagged_p ("/* myproc-unprivileged */"))
4728 o->line() << "({ assert_is_myproc(); }), ";
4729
4730 if (e->type == pe_long)
4731 o->line() << "((int64_t) (" << e->code << "))";
4732 else if (e->type == pe_string)
4733 o->line() << "((const char *) (" << e->code << "))";
4734 else
4735 throw SEMANTIC_ERROR (_("expected numeric or string type"), e->tok);
4736
4737 o->line() << ")";
4738
4739 for (vector<vardecl*>::const_iterator it = read_defs.begin(); it != read_defs.end(); ++it)
4740 c_global_read_undef(*it);
4741 for (vector<vardecl*>::const_iterator it = write_defs.begin(); it != write_defs.end(); ++it)
4742 c_global_write_undef(*it);
4743
4744 if (has_defines)
4745 o->newline();
4746 }
4747
4748
4749 void
4750 c_unparser::visit_binary_expression (binary_expression* e)
4751 {
4752 if (e->type != pe_long ||
4753 e->left->type != pe_long ||
4754 e->right->type != pe_long)
4755 throw SEMANTIC_ERROR (_("expected numeric types"), e->tok);
4756
4757 if (e->op == "+" ||
4758 e->op == "-" ||
4759 e->op == "*" ||
4760 e->op == "&" ||
4761 e->op == "|" ||
4762 e->op == "^")
4763 {
4764 o->line() << "((";
4765 e->left->visit (this);
4766 o->line() << ") " << e->op << " (";
4767 e->right->visit (this);
4768 o->line() << "))";
4769 }
4770 else if (e->op == ">>" ||
4771 e->op == "<<")
4772 {
4773 o->line() << "((int64_t)(";
4774 e->left->visit (this);
4775 o->line() << ") " << e->op << " ((";
4776 e->right->visit (this);
4777 o->line() << ") & 63))";
4778 }
4779 else if (e->op == ">>>")
4780 {
4781 o->line() << "(int64_t)((uint64_t)(";
4782 e->left->visit (this);
4783 o->line() << ") >> ((";
4784 e->right->visit (this);
4785 o->line() << ") & 63))";
4786 }
4787 else if (e->op == "/" ||
4788 e->op == "%")
4789 {
4790 // % and / need a division-by-zero check; and thus two temporaries
4791 // for proper evaluation order
4792 tmpvar left = gensym (pe_long);
4793 tmpvar right = gensym (pe_long);
4794
4795 o->line() << "({";
4796 o->indent(1);
4797
4798 c_assign (left, e->left, "division");
4799 c_assign (right, e->right, "division");
4800
4801 o->newline() << "if (unlikely(!" << right << ")) {";
4802 o->newline(1) << "c->last_error = ";
4803 o->line() << STAP_T_03;
4804 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
4805 o->newline() << "goto out;";
4806 o->newline(-1) << "}";
4807 o->newline() << ((e->op == "/") ? "_stp_div64" : "_stp_mod64")
4808 << " (NULL, " << left << ", " << right << ");";
4809
4810 o->newline(-1) << "})";
4811 }
4812 else
4813 throw SEMANTIC_ERROR (_F("operator %s not yet implemented", string(e->op).c_str()), e->tok);
4814 }
4815
4816
4817 void
4818 c_unparser::visit_unary_expression (unary_expression* e)
4819 {
4820 if (e->type != pe_long ||
4821 e->operand->type != pe_long)
4822 throw SEMANTIC_ERROR (_("expected numeric types"), e->tok);
4823
4824 if (e->op == "-")
4825 {
4826 // NB: Subtraction is special, since negative literals in the
4827 // script language show up as unary negations over positive
4828 // literals here. This makes it "exciting" for emitting pure
4829 // C since: - 0x8000_0000_0000_0000 ==> - (- 9223372036854775808)
4830 // This would constitute a signed overflow, which gcc warns on
4831 // unless -ftrapv/-J are in CFLAGS - which they're not.
4832
4833 o->line() << "(int64_t)(0 " << e->op << " (uint64_t)(";
4834 e->operand->visit (this);
4835 o->line() << "))";
4836 }
4837 else
4838 {
4839 o->line() << "(" << e->op << " (";
4840 e->operand->visit (this);
4841 o->line() << "))";
4842 }
4843 }
4844
4845 void
4846 c_unparser::visit_logical_or_expr (logical_or_expr* e)
4847 {
4848 if (e->type != pe_long ||
4849 e->left->type != pe_long ||
4850 e->right->type != pe_long)
4851 throw SEMANTIC_ERROR (_("expected numeric types"), e->tok);
4852
4853 o->line() << "((";
4854 e->left->visit (this);
4855 o->line() << ") " << e->op << " (";
4856 e->right->visit (this);
4857 o->line() << "))";
4858 }
4859
4860
4861 void
4862 c_unparser::visit_logical_and_expr (logical_and_expr* e)
4863 {
4864 if (e->type != pe_long ||
4865 e->left->type != pe_long ||
4866 e->right->type != pe_long)
4867 throw SEMANTIC_ERROR (_("expected numeric types"), e->tok);
4868
4869 o->line() << "((";
4870 e->left->visit (this);
4871 o->line() << ") " << e->op << " (";
4872 e->right->visit (this);
4873 o->line() << "))";
4874 }
4875
4876
4877 void
4878 c_unparser::visit_array_in (array_in* e)
4879 {
4880 symbol *array;
4881 hist_op *hist;
4882 classify_indexable (e->operand->base, array, hist);
4883
4884 if (array)
4885 {
4886 stmt_expr block(*this);
4887
4888 tmpvar res = gensym (pe_long);
4889 vector<tmpvar> idx;
4890
4891 // determine if the array index contains an asterisk
4892 bool array_slice = false;
4893 for (unsigned i = 0; i < e->operand->indexes.size(); i ++)
4894 if (e->operand->indexes[i] == NULL)
4895 {
4896 array_slice = true;
4897 break;
4898 }
4899
4900 if (!array_slice) // checking for membership of a specific element
4901 {
4902 load_map_indices (e->operand, idx);
4903 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
4904
4905 mapvar mvar = getmap (array->referent, e->tok);
4906 c_assign (res, mvar.exists(idx), e->tok);
4907
4908 o->newline() << res << ";";
4909 }
4910 else
4911 {
4912 // create tmpvars for the array indexes, storing NULL where there is
4913 // no specific value that the index should be
4914 vector<tmpvar *> array_slice_vars;
4915 for (unsigned i=0; i<e->operand->indexes.size(); i++)
4916 {
4917 if (e->operand->indexes[i])
4918 {
4919 tmpvar *asvar = new tmpvar(gensym(e->operand->indexes[i]->type));
4920 c_assign (*asvar, e->operand->indexes[i], "tmp var");
4921 array_slice_vars.push_back(asvar);
4922 }
4923 else
4924 array_slice_vars.push_back(NULL);
4925 }
4926
4927 mapvar mvar = getmap (array->referent, e->operand->tok);
4928 itervar iv = getiter(array);
4929 vector<tmpvar> idx;
4930
4931 // we may not need to aggregate if we're already in a foreach
4932 bool pre_agg = (aggregations_active.count(mvar.value()) > 0);
4933 if (mvar.is_parallel() && !pre_agg)
4934 {
4935 o->newline() << "if (unlikely(NULL == "
4936 << mvar.calculate_aggregate() << ")) {";
4937 o->newline(1) << "c->last_error = ";
4938 o->line() << STAP_T_05 << mvar << "\";";
4939 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
4940 o->newline() << "goto out;";
4941 o->newline(-1) << "}";
4942 }
4943
4944 string ctr = lex_cast (label_counter++);
4945 string toplabel = "top_" + ctr;
4946 string contlabel = "continue_" + ctr;
4947 string breaklabel = "break_" + ctr;
4948
4949 o->newline() << iv << " = " << iv.start(mvar) << ";";
4950 c_assign (res, "0", e->tok); // set the default to 0
4951
4952 o->newline() << toplabel << ":";
4953
4954 o->newline(1) << "if (!(" << iv << "))";
4955 o->newline(1) << "goto " << breaklabel << ";";
4956
4957 // generate code for comparing the keys to the index slice
4958 o->newline(-1) << "if (1"; // in case all are wildcards
4959 for (unsigned i=0; i<array_slice_vars.size(); i++)
4960 {
4961 if (array_slice_vars[i] != NULL)
4962 {
4963 if (array_slice_vars[i]->type() == pe_long)
4964 o->line() << " && " << *array_slice_vars[i] << " == "
4965 << iv.get_key(mvar, array_slice_vars[i]->type(), i);
4966 else if (array_slice_vars[i]->type() == pe_string)
4967 o->line() << " && strncmp(" << *array_slice_vars[i] << ", "
4968 << iv.get_key(mvar, array_slice_vars[i]->type(), i)
4969 << ", MAXSTRINGLEN) == 0";
4970 else
4971 throw SEMANTIC_ERROR (_("unexpected type"), e->tok);
4972 }
4973 }
4974 o->line() << "){";
4975 o->indent(1);
4976 // conditional is true, so set res and go to break
4977 c_assign (res, "1", e->tok);
4978 o->newline() << "goto " << breaklabel << ";";
4979 o->newline(-1) << "}";
4980
4981 // else, keep iterating
4982 o->newline() << iv << " = " << iv.next(mvar) << ";";
4983 o->newline() << "goto " << toplabel << ";";
4984
4985 o->newline(-1) << breaklabel<< ":";
4986 o->newline(1) << "; /* dummy statement */";
4987 o->newline(-1) << res << ";";
4988 }
4989
4990 }
4991 else
4992 {
4993 // By definition:
4994 //
4995 // 'foo in @hist_op(...)' is true iff
4996 // '@hist_op(...)[foo]' is nonzero
4997 //
4998 // so we just delegate to the latter call, since int64_t is also
4999 // our boolean type.
5000 e->operand->visit(this);
5001 }
5002 }
5003
5004 void
5005 c_unparser::visit_regex_query (regex_query* e)
5006 {
5007 o->line() << "(";
5008 o->indent(1);
5009 o->newline();
5010 if (e->op == "!~") o->line() << "!";
5011 stapdfa *dfa = session->dfas[e->right->value];
5012 dfa->emit_matchop_start (o);
5013 e->left->visit(this);
5014 dfa->emit_matchop_end (o);
5015 o->newline(-1) << ")";
5016 }
5017
5018 void
5019 c_unparser::visit_compound_expression(compound_expression* e)
5020 {
5021 o->line() << "(";
5022 e->left->visit (this);
5023 o->line() << ", ";
5024 e->right->visit (this);
5025 o->line() << ")";
5026 }
5027
5028 void
5029 c_unparser::visit_comparison (comparison* e)
5030 {
5031 o->line() << "(";
5032
5033 if (e->left->type == pe_string)
5034 {
5035 if (e->right->type != pe_string)
5036 throw SEMANTIC_ERROR (_("expected string types"), e->tok);
5037
5038 // PR13283 indicated that we may need a temporary variable to
5039 // store the operand strings, if e.g. they are both references
5040 // into function call __retvalue's, which overlap in memory.
5041 // ... but we now handle that inside the function call machinery,
5042 // which always returns an allocated temporary variable.
5043
5044 o->line() << "(strncmp ((";
5045 e->left->visit (this);
5046 o->line() << "), (";
5047 e->right->visit (this);
5048 o->line() << "), MAXSTRINGLEN) " << e->op << " 0)";
5049 }
5050 else if (e->left->type == pe_long)
5051 {
5052 if (e->right->type != pe_long)
5053 throw SEMANTIC_ERROR (_("expected numeric types"), e->tok);
5054
5055 o->line() << "((";
5056 e->left->visit (this);
5057 o->line() << ") " << e->op << " (";
5058 e->right->visit (this);
5059 o->line() << "))";
5060 }
5061 else
5062 throw SEMANTIC_ERROR (_("unexpected type"), e->left->tok);
5063
5064 o->line() << ")";
5065 }
5066
5067
5068 void
5069 c_unparser::visit_concatenation (concatenation* e)
5070 {
5071 if (e->op != ".")
5072 throw SEMANTIC_ERROR (_("unexpected concatenation operator"), e->tok);
5073
5074 if (e->type != pe_string ||
5075 e->left->type != pe_string ||
5076 e->right->type != pe_string)
5077 throw SEMANTIC_ERROR (_("expected string types"), e->tok);
5078
5079 tmpvar t = gensym (e->type);
5080
5081 o->line() << "({ ";
5082 o->indent(1);
5083 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5084 c_assign (t.value(), e->left, "assignment");
5085 c_strcat (t.value(), e->right);
5086 o->newline() << t << ";";
5087 o->newline(-1) << "})";
5088 }
5089
5090
5091 void
5092 c_unparser::visit_ternary_expression (ternary_expression* e)
5093 {
5094 if (e->cond->type != pe_long)
5095 throw SEMANTIC_ERROR (_("expected numeric condition"), e->cond->tok);
5096
5097 if (e->truevalue->type != e->falsevalue->type ||
5098 e->type != e->truevalue->type ||
5099 (e->truevalue->type != pe_long && e->truevalue->type != pe_string))
5100 throw SEMANTIC_ERROR (_("expected matching types"), e->tok);
5101
5102 o->line() << "((";
5103 e->cond->visit (this);
5104 o->line() << ") ? (";
5105 e->truevalue->visit (this);
5106 o->line() << ") : (";
5107 e->falsevalue->visit (this);
5108 o->line() << "))";
5109 }
5110
5111
5112 void
5113 c_unparser::visit_assignment (assignment* e)
5114 {
5115 if (e->op == "<<<")
5116 {
5117 if (e->type != pe_long)
5118 throw SEMANTIC_ERROR (_("non-number <<< expression"), e->tok);
5119
5120 if (e->left->type != pe_stats)
5121 throw SEMANTIC_ERROR (_("non-stats left operand to <<< expression"), e->left->tok);
5122
5123 if (e->right->type != pe_long)
5124 throw SEMANTIC_ERROR (_("non-number right operand to <<< expression"), e->right->tok);
5125
5126 }
5127 else
5128 {
5129 if (e->type != e->left->type)
5130 throw SEMANTIC_ERROR (_("type mismatch"), e->tok, e->left->tok);
5131 if (e->right->type != e->left->type)
5132 throw SEMANTIC_ERROR (_("type mismatch"), e->right->tok, e->left->tok);
5133 }
5134
5135 c_unparser_assignment tav (this, e->op, e->right);
5136 e->left->visit (& tav);
5137 }
5138
5139
5140 void
5141 c_unparser::visit_pre_crement (pre_crement* e)
5142 {
5143 if (e->type != pe_long ||
5144 e->type != e->operand->type)
5145 throw SEMANTIC_ERROR (_("expected numeric type"), e->tok);
5146
5147 c_unparser_assignment tav (this, e->op, false);
5148 e->operand->visit (& tav);
5149 }
5150
5151
5152 void
5153 c_unparser::visit_post_crement (post_crement* e)
5154 {
5155 if (e->type != pe_long ||
5156 e->type != e->operand->type)
5157 throw SEMANTIC_ERROR (_("expected numeric type"), e->tok);
5158
5159 c_unparser_assignment tav (this, e->op, true);
5160 e->operand->visit (& tav);
5161 }
5162
5163
5164 void
5165 c_unparser::visit_symbol (symbol* e)
5166 {
5167 assert (e->referent != 0);
5168 vardecl* r = e->referent;
5169
5170 if (r->index_types.size() != 0)
5171 throw SEMANTIC_ERROR (_("invalid reference to array"), e->tok);
5172
5173 var v = getvar(r, e->tok);
5174 o->line() << v;
5175 }
5176
5177 void
5178 c_unparser::visit_target_register (target_register* e)
5179 {
5180 o->line() << (e->userspace_p ? "u_fetch_register(" : "k_fetch_register(")
5181 << e->regno
5182 << ")";
5183 }
5184
5185 void
5186 c_unparser::visit_target_deref (target_deref* e)
5187 {
5188 if (e->signed_p)
5189 {
5190 switch (e->size)
5191 {
5192 case 1:
5193 o->line() << "(int64_t)(int8_t)";
5194 break;
5195 case 2:
5196 o->line() << "(int64_t)(int16_t)";
5197 break;
5198 case 4:
5199 o->line() << "(int64_t)(int32_t)";
5200 break;
5201 case 8:
5202 break;
5203 default:
5204 abort();
5205 }
5206 }
5207 o->line() << (e->userspace_p ? "uderef(" : "kderef(")
5208 << e->size << ", (";
5209 e->addr->visit (this);
5210 o->line() << "))";
5211 }
5212
5213 void
5214 c_unparser::visit_target_bitfield (target_bitfield*)
5215 {
5216 // These are all expanded much earlier.
5217 abort();
5218 }
5219
5220 // Assignment expansion is tricky.
5221 //
5222 // Because assignments are nestable expressions, we have
5223 // to emit C constructs that are nestable expressions too.
5224 // We have to evaluate the given expressions the proper number of times,
5225 // including array indices.
5226 // We have to lock the lvalue (if global) against concurrent modification,
5227 // especially with modify-assignment operations (+=, ++).
5228 // We have to check the rvalue (for division-by-zero checks).
5229
5230 // In the normal "pre=false" case, for (A op B) emit:
5231 // ({ tmp = B; check(B); lock(A); res = A op tmp; A = res; unlock(A); res; })
5232 // In the "pre=true" case, emit instead:
5233 // ({ tmp = B; check(B); lock(A); res = A; A = res op tmp; unlock(A); res; })
5234 //
5235 // (op is the plain operator portion of a combined calculate/assignment:
5236 // "+" for "+=", and so on. It is in the "macop" variable below.)
5237 //
5238 // For array assignments, additional temporaries are used for each
5239 // index, which are expanded before the "tmp=B" expression, in order
5240 // to consistently order evaluation of lhs before rhs.
5241 //
5242
5243 void
5244 c_unparser_assignment::prepare_rvalue (interned_string op,
5245 tmpvar & rval,
5246 token const * tok)
5247 {
5248 if (rvalue)
5249 parent->c_assign (rval, rvalue, "assignment");
5250 else
5251 {
5252 if (op == "++" || op == "--")
5253 // Here is part of the conversion proccess of turning "x++" to
5254 // "x += 1".
5255 rval.override("1");
5256 else
5257 throw SEMANTIC_ERROR (_("need rvalue for assignment"), tok);
5258 }
5259 }
5260
5261 void
5262 c_unparser_assignment::visit_symbol (symbol *e)
5263 {
5264 stmt_expr block(*parent);
5265 translator_output* o = parent->o;
5266
5267 assert (e->referent != 0);
5268 if (e->referent->index_types.size() != 0)
5269 throw SEMANTIC_ERROR (_("unexpected reference to array"), e->tok);
5270
5271 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5272 exp_type ty = rvalue ? rvalue->type : e->type;
5273 tmpvar rval = parent->gensym (ty);
5274 tmpvar res = parent->gensym (ty);
5275
5276 prepare_rvalue (op, rval, e->tok);
5277
5278 var lvar = parent->getvar (e->referent, e->tok);
5279 c_assignop (res, lvar, rval, e->tok);
5280
5281 o->newline() << res << ";";
5282 }
5283
5284 void
5285 c_unparser_assignment::visit_target_register (target_register* e)
5286 {
5287 exp_type ty = rvalue ? rvalue->type : e->type;
5288 assert(ty == pe_long);
5289
5290 tmpvar rval = parent->gensym (pe_long);
5291 prepare_rvalue (op, rval, e->tok);
5292
5293 // Given how target_registers are created in loc2stap.cxx,
5294 // we should never see anything other than simple assignment.
5295 assert(op == "=");
5296
5297 translator_output* o = parent->o;
5298 o->newline() << (e->userspace_p ? "u_store_register(" : "k_store_register(")
5299 << e->regno << ", " << rval << ")";
5300 }
5301
5302 void
5303 c_unparser_assignment::visit_target_deref (target_deref* e)
5304 {
5305 exp_type ty = rvalue ? rvalue->type : e->type;
5306 assert(ty == pe_long);
5307
5308 tmpvar rval = parent->gensym (pe_long);
5309
5310 prepare_rvalue (op, rval, e->tok);
5311
5312 // Given how target_deref are created in loc2stap.cxx,
5313 // we should never see anything other than simple assignment.
5314 assert(op == "=");
5315
5316 translator_output* o = parent->o;
5317 o->newline() << (e->userspace_p ? "store_uderef(" : "store_kderef(")
5318 << e->size << ", (";
5319 e->addr->visit (parent);
5320 o->line() << "), " << rval << ")";
5321 }
5322
5323 void
5324 c_unparser::visit_target_symbol (target_symbol* e)
5325 {
5326 throw SEMANTIC_ERROR(_("cannot translate general target-symbol expression"), e->tok);
5327 }
5328
5329
5330 void
5331 c_unparser::visit_atvar_op (atvar_op* e)
5332 {
5333 throw SEMANTIC_ERROR(_("cannot translate general @var expression"), e->tok);
5334 }
5335
5336
5337 void
5338 c_unparser::visit_cast_op (cast_op* e)
5339 {
5340 throw SEMANTIC_ERROR(_("cannot translate general @cast expression"), e->tok);
5341 }
5342
5343
5344 void
5345 c_unparser::visit_autocast_op (autocast_op* e)
5346 {
5347 throw SEMANTIC_ERROR(_("cannot translate general dereference expression"), e->tok);
5348 }
5349
5350
5351 void
5352 c_unparser::visit_defined_op (defined_op* e)
5353 {
5354 throw SEMANTIC_ERROR(_("cannot translate general @defined expression"), e->tok);
5355 }
5356
5357
5358 void
5359 c_unparser::visit_entry_op (entry_op* e)
5360 {
5361 throw SEMANTIC_ERROR(_("cannot translate general @entry expression"), e->tok);
5362 }
5363
5364
5365 void
5366 c_unparser::visit_perf_op (perf_op* e)
5367 {
5368 throw SEMANTIC_ERROR(_("cannot translate general @perf expression"), e->tok);
5369 }
5370
5371
5372 void
5373 c_unparser::load_map_indices(arrayindex *e,
5374 vector<tmpvar> & idx)
5375 {
5376 symbol *array;
5377 hist_op *hist;
5378 classify_indexable (e->base, array, hist);
5379
5380 if (array)
5381 {
5382 idx.clear();
5383
5384 assert (array->referent != 0);
5385 vardecl* r = array->referent;
5386
5387 if (r->index_types.size() == 0 ||
5388 r->index_types.size() != e->indexes.size())
5389 throw SEMANTIC_ERROR (_("invalid array reference"), e->tok);
5390
5391 for (unsigned i=0; i<r->index_types.size(); i++)
5392 {
5393 if (r->index_types[i] != e->indexes[i]->type)
5394 throw SEMANTIC_ERROR (_("array index type mismatch"), e->indexes[i]->tok);
5395
5396 tmpvar ix = gensym (r->index_types[i]);
5397 c_assign (ix, e->indexes[i], "array index copy");
5398 idx.push_back (ix);
5399 }
5400 }
5401 else
5402 {
5403 assert (e->indexes.size() == 1);
5404 assert (e->indexes[0]->type == pe_long);
5405 tmpvar ix = gensym (pe_long);
5406 c_assign (ix, e->indexes[0], "array index copy");
5407 idx.push_back(ix);
5408 }
5409 }
5410
5411
5412 var*
5413 c_unparser::load_aggregate (expression *e, aggvar & agg)
5414 {
5415 symbol *sym = get_symbol_within_expression (e);
5416
5417 if (sym->referent->type != pe_stats)
5418 throw SEMANTIC_ERROR (_("unexpected aggregate of non-statistic"), sym->tok);
5419
5420 var *v;
5421 if (sym->referent->arity == 0)
5422 {
5423 v = new var(getvar(sym->referent, sym->tok));
5424 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*sym->tok) << ";";
5425 o->newline() << agg << " = _stp_stat_get (" << *v << ", 0);";
5426 }
5427 else
5428 {
5429 mapvar *mv = new mapvar(getmap(sym->referent, sym->tok));
5430 v = mv;
5431
5432 arrayindex *arr = NULL;
5433 if (!expression_is_arrayindex (e, arr))
5434 throw SEMANTIC_ERROR(_("unexpected aggregate of non-arrayindex"), e->tok);
5435
5436 // If we have a foreach_loop value, we don't need to index the map
5437 string agg_value;
5438 if (get_foreach_loop_value(arr, agg_value))
5439 o->newline() << agg << " = " << agg_value << ";";
5440 else
5441 {
5442 vector<tmpvar> idx;
5443 load_map_indices (arr, idx);
5444 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*sym->tok) << ";";
5445 bool pre_agg = (aggregations_active.count(mv->value()) > 0);
5446 o->newline() << agg << " = " << mv->get(idx, pre_agg) << ";";
5447 }
5448 }
5449
5450 return v;
5451 }
5452
5453
5454 string
5455 c_unparser::histogram_index_check(var & base, tmpvar & idx) const
5456 {
5457 return "((" + idx.value() + " >= 0)"
5458 + " && (" + idx.value() + " < " + base.buckets() + "))";
5459 }
5460
5461
5462 void
5463 c_unparser::visit_arrayindex (arrayindex* e)
5464 {
5465 // If we have a foreach_loop value, use it and call it a day!
5466 string ai_value;
5467 if (get_foreach_loop_value(e, ai_value))
5468 {
5469 o->line() << ai_value;
5470 return;
5471 }
5472
5473 symbol *array;
5474 hist_op *hist;
5475 classify_indexable (e->base, array, hist);
5476
5477 if (array)
5478 {
5479 // Visiting an statistic-valued array in a non-lvalue context is prohibited.
5480 if (array->referent->type == pe_stats)
5481 throw SEMANTIC_ERROR (_("statistic-valued array in rvalue context"), e->tok);
5482
5483 stmt_expr block(*this);
5484
5485 vector<tmpvar> idx;
5486 load_map_indices (e, idx);
5487 tmpvar res = gensym (e->type);
5488
5489 mapvar mvar = getmap (array->referent, e->tok);
5490 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5491 c_assign (res, mvar.get(idx), e->tok);
5492
5493 o->newline() << res << ";";
5494 }
5495 else
5496 {
5497 // Note: this is a slightly tricker-than-it-looks allocation of
5498 // temporaries. The reason is that we're in the branch handling
5499 // histogram-indexing, and the histogram might be build over an
5500 // indexable entity itself. For example if we have:
5501 //
5502 // global foo
5503 // ...
5504 // foo[getpid(), geteuid()] <<< 1
5505 // ...
5506 // print @log_hist(foo[pid, euid])[bucket]
5507 //
5508 // We are looking at the @log_hist(...)[bucket] expression, so
5509 // allocating one tmpvar for calculating bucket (the "index" of
5510 // this arrayindex expression), and one tmpvar for storing the
5511 // result in, just as normal.
5512 //
5513 // But we are *also* going to call load_aggregate on foo, which
5514 // will itself require tmpvars for each of its indices. Since
5515 // this is not handled by delving into the subexpression (it
5516 // would be if hist were first-class in the type system, but
5517 // it's not) we we allocate all the tmpvars used in such a
5518 // subexpression up here: first our own aggvar, then our index
5519 // (bucket) tmpvar, then all the index tmpvars of our
5520 // pe_stat-valued subexpression, then our result.
5521
5522 assert(hist);
5523 stmt_expr block(*this);
5524
5525 aggvar agg = gensym_aggregate ();
5526
5527 vector<tmpvar> idx;
5528 load_map_indices (e, idx);
5529 tmpvar res = gensym (e->type);
5530
5531 // These should have faulted during elaboration if not true.
5532 if (idx.size() != 1 || idx[0].type() != pe_long)
5533 throw SEMANTIC_ERROR(_("Invalid indexing of histogram"), e->tok);
5534
5535 var *v = load_aggregate(hist->stat, agg);
5536 v->assert_hist_compatible(*hist);
5537
5538 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5539
5540 // PR 2142+2610: empty aggregates
5541 o->newline() << "if (unlikely (" << agg.value() << " == NULL)"
5542 << " || " << agg.value() << "->count == 0) {";
5543 o->newline(1) << "c->last_error = ";
5544 o->line() << STAP_T_06;
5545 o->newline() << "goto out;";
5546 o->newline(-1) << "} else {";
5547 o->newline(1) << "if (" << histogram_index_check(*v, idx[0]) << ")";
5548 o->newline(1) << res << " = " << agg << "->histogram[" << idx[0] << "];";
5549 o->newline(-1) << "else {";
5550 o->newline(1) << "c->last_error = ";
5551 o->line() << STAP_T_07;
5552 o->newline() << "goto out;";
5553 o->newline(-1) << "}";
5554
5555 o->newline(-1) << "}";
5556 o->newline() << res << ";";
5557
5558 delete v;
5559 }
5560 }
5561
5562
5563 void
5564 c_unparser_assignment::visit_arrayindex (arrayindex *e)
5565 {
5566 symbol *array;
5567 hist_op *hist;
5568 classify_indexable (e->base, array, hist);
5569
5570 if (array)
5571 {
5572
5573 stmt_expr block(*parent);
5574
5575 translator_output *o = parent->o;
5576
5577 if (array->referent->index_types.size() == 0)
5578 throw SEMANTIC_ERROR (_("unexpected reference to scalar"), e->tok);
5579
5580 vector<tmpvar> idx;
5581 parent->load_map_indices (e, idx);
5582 exp_type ty = rvalue ? rvalue->type : e->type;
5583 tmpvar rvar = parent->gensym (ty);
5584 tmpvar lvar = parent->gensym (ty);
5585 tmpvar res = parent->gensym (ty);
5586
5587 // NB: because these expressions are nestable, emit this construct
5588 // thusly:
5589 // ({ tmp0=(idx0); ... tmpN=(idxN); rvar=(rhs); lvar; res;
5590 // lock (array);
5591 // lvar = get (array,idx0...N); // if necessary
5592 // assignop (res, lvar, rvar);
5593 // set (array, idx0...N, lvar);
5594 // unlock (array);
5595 // res; })
5596 //
5597 // we store all indices in temporary variables to avoid nasty
5598 // reentrancy issues that pop up with nested expressions:
5599 // e.g. ++a[a[c]=5] could deadlock
5600 //
5601 //
5602 // There is an exception to the above form: if we're doign a <<< assigment to
5603 // a statistic-valued map, there's a special form we follow:
5604 //
5605 // ({ tmp0=(idx0); ... tmpN=(idxN); rvar=(rhs);
5606 // *no need to* lock (array);
5607 // _stp_map_add_stat (array, idx0...N, rvar);
5608 // *no need to* unlock (array);
5609 // rvar; })
5610 //
5611 // To simplify variable-allocation rules, we assign rvar to lvar and
5612 // res in this block as well, even though they are technically
5613 // superfluous.
5614
5615 prepare_rvalue (op, rvar, e->tok);
5616
5617 if (op == "<<<")
5618 {
5619 assert (e->type == pe_stats);
5620 assert (rvalue->type == pe_long);
5621
5622 mapvar mvar = parent->getmap (array->referent, e->tok);
5623 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5624 o->newline() << mvar.add (idx, rvar) << ";";
5625 res = rvar;
5626 // no need for these dummy assignments
5627 // o->newline() << lvar << " = " << rvar << ";";
5628 // o->newline() << res << " = " << rvar << ";";
5629 }
5630 else
5631 {
5632 mapvar mvar = parent->getmap (array->referent, e->tok);
5633 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5634 if (op != "=") // don't bother fetch slot if we will just overwrite it
5635 parent->c_assign (lvar, mvar.get(idx), e->tok);
5636 c_assignop (res, lvar, rvar, e->tok);
5637 o->newline() << mvar.set (idx, lvar) << ";";
5638 }
5639
5640 o->newline() << res << ";";
5641 }
5642 else
5643 {
5644 throw SEMANTIC_ERROR(_("cannot assign to histogram buckets"), e->tok);
5645 }
5646 }
5647
5648
5649 void
5650 c_unparser::visit_functioncall (functioncall* e)
5651 {
5652 assert (!e->referents.empty());
5653
5654 stmt_expr block(*this);
5655
5656 vector<bool> cp_arg(e->referents.size(), true);
5657 for (unsigned fd = 0; fd < e->referents.size(); fd++)
5658 {
5659 functiondecl* r = e->referents[fd];
5660
5661 if (r->formal_args.size() != e->args.size())
5662 throw SEMANTIC_ERROR (_("invalid length argument list"), e->tok);
5663
5664 for (unsigned i = 0; i < e->args.size(); i++)
5665 {
5666 if (r->formal_args[i]->type != e->args[i]->type)
5667 throw SEMANTIC_ERROR (_("function argument type mismatch"),
5668 e->args[i]->tok, r->formal_args[i]->tok);
5669 }
5670
5671 // all alternative functions must be compatible if passing by
5672 // char pointer
5673 for (unsigned i = 0; i < r->formal_args.size(); i++)
5674 {
5675 if (!r->formal_args[i]->char_ptr_arg)
5676 cp_arg[i] = false;
5677 }
5678 }
5679
5680 // NB: we store all actual arguments in temporary variables,
5681 // to avoid colliding sharing of context variables with
5682 // nested function calls: f(f(f(1)))
5683
5684 // compute actual arguments
5685 vector<tmpvar> tmp;
5686 for (unsigned i=0; i<e->args.size(); i++)
5687 {
5688 tmpvar t = gensym(e->args[i]->type);
5689
5690 symbol *sym_out;
5691 if (cp_arg[i] && e->args[i]->is_symbol(sym_out)
5692 && is_local(sym_out->referent, sym_out->tok))
5693 t.override(getvar(sym_out->referent, sym_out->tok).value());
5694 else
5695 c_assign (t, e->args[i],
5696 _("function actual argument evaluation"));
5697 tmp.push_back(t);
5698 }
5699
5700 // overloading execution logic for functioncall:
5701 //
5702 // - copy in computed function arguments for overload_0
5703 // - make the functioncall for overload_0 and overwrite return variable
5704 // - if context next flag is not set, goto fc_end
5705 // *
5706 // *
5707 // *
5708 // - copy in computed function arguments for overload_n
5709 // - make the functioncall for overload_n and overwrite return variable
5710 // fc_end:
5711 // - yield return value
5712
5713 // store the return value after the function arguments have been worked out
5714 // to avoid problems that may occure with nesting.
5715 tmpvar tmp_ret = gensym (e->type);
5716
5717 // NB: as per PR13283, it's important we always allocate a distinct
5718 // temporary value to receive the return value. (We can pass its
5719 // address by reference to the function if we like.)
5720
5721 bool yield = false; // set if statement expression is non void
5722
5723 for (unsigned fd = 0; fd < e->referents.size(); fd++)
5724 {
5725 functiondecl* r = e->referents[fd];
5726
5727 // copy in actual arguments
5728 for (unsigned i=0; i<e->args.size(); i++)
5729 {
5730 if (r->formal_args[i]->char_ptr_arg)
5731 o->newline() << "c->locals[c->nesting+1]." + c_funcname (r->name) + "."
5732 + c_localname (r->formal_args[i]->name) << " = "
5733 << tmp[i].value() << ";";
5734 else
5735 c_assign ("c->locals[c->nesting+1]." +
5736 c_funcname (r->name) + "." +
5737 c_localname (r->formal_args[i]->name),
5738 tmp[i].value(),
5739 e->args[i]->type,
5740 "function actual argument copy",
5741 e->args[i]->tok);
5742 }
5743 // optimized string returns need a local storage pointer.
5744 bool pointer_ret = (e->type == pe_string && !session->unoptimized);
5745 if (pointer_ret)
5746 {
5747 if (e == assigned_functioncall)
5748 tmp_ret.override (*assigned_functioncall_retval);
5749 o->newline() << "c->locals[c->nesting+1]." << c_funcname(r->name)
5750 << ".__retvalue = &" << tmp_ret.value() << "[0];";
5751 }
5752
5753 // call function
5754 o->newline() << c_funcname (r->name) << " (c);";
5755 o->newline() << "if (unlikely(c->last_error || c->aborted)) goto out;";
5756
5757 if (!already_checked_action_count && !session->suppress_time_limits
5758 && !session->unoptimized)
5759 {
5760 max_action_info mai (*session);
5761 r->body->visit(&mai);
5762 // if an unoptimized function/probe called an optimized function, then
5763 // increase the counter, since the subtraction isn't done within an
5764 // optimized function
5765 if(mai.statement_count_finite())
5766 record_actions (mai.statement_count, e->tok, true);
5767 }
5768
5769 if (r->type == pe_unknown || tmp_ret.is_overridden())
5770 // If we passed typechecking with pe_unknown, or if we directly assigned
5771 // the functioncall retval, then nothing will use this return value
5772 yield = false;
5773 else
5774 {
5775 if (!pointer_ret)
5776 {
5777 // overwrite the previous return value
5778 string value = "c->locals[c->nesting+1]." + c_funcname(r->name) + ".__retvalue";
5779 c_assign (tmp_ret.value(), value, e->type,
5780 _("function return result evaluation"), e->tok);
5781 }
5782 yield = true;
5783 }
5784
5785 if (e->referents.size() > 1 && r != e->referents.back())
5786 // branch to end of the enclosing statement-expression if one of the
5787 // function alternatives is selected
5788 o->newline() << "if (!c->next) goto fc_end_" << fc_counter << ";";
5789 }
5790
5791 if (e->referents.size() > 1)
5792 {
5793 // end label and increment counter
5794 o->newline() << "fc_end_" << fc_counter++ << ":";
5795 }
5796
5797 if (e->referents.back()->has_next)
5798 // check for aborted return from function; this could happen from non-overloaded ones too
5799 o->newline()
5800 << "if (unlikely(c->next)) { "
5801 << "c->last_stmt = " << lex_cast_qstring(*e->tok) << "; "
5802 << "c->last_error = \"all functions exhausted\"; goto out; }";
5803
5804 // return result from retvalue slot NB: this must be last, for the
5805 // enclosing statement-expression ({ ... }) to carry this value.
5806 if (yield)
5807 o->newline() << tmp_ret.value() << ";";
5808 else
5809 o->newline() << "(void) 0;";
5810 }
5811
5812
5813 // returns true if it should print directly to a stream
5814 static bool
5815 preprocess_print_format(print_format* e, vector<tmpvar>& tmp,
5816 vector<print_format::format_component>& components,
5817 string& format_string)
5818 {
5819 if (e->print_with_format)
5820 {
5821 format_string = e->raw_components;
5822 components = e->components;
5823 }
5824 else
5825 {
5826 string delim;
5827 if (e->print_with_delim)
5828 {
5829 stringstream escaped_delim;
5830 interned_string dstr = e->delimiter;
5831 for (interned_string::const_iterator i = dstr.begin();
5832 i != dstr.end(); ++i)
5833 {
5834 if (*i == '%')
5835 escaped_delim << '%';
5836 escaped_delim << *i;
5837 }
5838 delim = escaped_delim.str();
5839 }
5840
5841 // Synthesize a print-format string if the user didn't
5842 // provide one; the synthetic string simply contains one
5843 // directive for each argument.
5844 stringstream format;
5845 for (unsigned i = 0; i < e->args.size(); ++i)
5846 {
5847 if (i > 0 && e->print_with_delim)
5848 format << delim;
5849 switch (e->args[i]->type)
5850 {
5851 default:
5852 case pe_unknown:
5853 throw SEMANTIC_ERROR(_("cannot print unknown expression type"), e->args[i]->tok);
5854 case pe_stats:
5855 throw SEMANTIC_ERROR(_("cannot print a raw stats object"), e->args[i]->tok);
5856 case pe_long:
5857 format << "%d";
5858 break;
5859 case pe_string:
5860 format << "%s";
5861 break;
5862 }
5863 }
5864 if (e->print_with_newline)
5865 format << "\\n";
5866
5867 format_string = format.str();
5868 components = print_format::string_to_components(format_string);
5869 }
5870
5871
5872 // optimize simple string prints
5873 if (e->print_to_stream && tmp.size() <= 1
5874 && format_string.find("%%") == string::npos)
5875 {
5876 // just a plain format string itself, or
5877 // simply formatting a string verbatim.
5878 if (tmp.empty() || format_string == "%s")
5879 return true;
5880
5881 // just a string without formatting plus newline, and it's been
5882 // overridden with a literal, then we can token-paste the newline.
5883 // TODO could allow any prefix and suffix around "%s", C-escaped.
5884 if (tmp[0].is_overridden() && format_string == "%s\\n")
5885 {
5886 tmp[0].override(tmp[0].value() + "\"\\n\"");
5887 return true;
5888 }
5889 }
5890
5891 return false;
5892 }
5893
5894
5895 void
5896 c_unparser::visit_print_format (print_format* e)
5897 {
5898 // Print formats can contain a general argument list *or* a special
5899 // type of argument which gets its own processing: a single,
5900 // non-format-string'ed, histogram-type stat_op expression.
5901
5902 if (e->hist)
5903 {
5904 stmt_expr block(*this);
5905 aggvar agg = gensym_aggregate ();
5906
5907 var *v = load_aggregate(e->hist->stat, agg);
5908 v->assert_hist_compatible(*e->hist);
5909
5910 {
5911 // PR 2142+2610: empty aggregates
5912 o->newline() << "if (unlikely (" << agg.value() << " == NULL)"
5913 << " || " << agg.value() << "->count == 0) {";
5914 o->newline(1) << "c->last_error = ";
5915 o->line() << STAP_T_06;
5916 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
5917 o->newline() << "goto out;";
5918 o->newline(-1) << "} else";
5919 if (e->print_to_stream)
5920 {
5921 o->newline(1) << "_stp_stat_print_histogram (" << v->hist() << ", " << agg.value() << ");";
5922 o->indent(-1);
5923 }
5924 else
5925 {
5926 exp_type ty = pe_string;
5927 tmpvar res = gensym (ty);
5928 o->newline(1) << "_stp_stat_print_histogram_buf (" << res.value() << ", MAXSTRINGLEN, " << v->hist() << ", " << agg.value() << ");";
5929 o->newline(-1) << res.value() << ";";
5930 }
5931 }
5932
5933 delete v;
5934 }
5935 else
5936 {
5937 stmt_expr block(*this);
5938
5939 // PR10750: Enforce a reasonable limit on # of varargs
5940 // 32 varargs leads to max 256 bytes on the stack
5941 if (e->args.size() > 32)
5942 throw SEMANTIC_ERROR(_NF("additional argument to print", "too many arguments to print (%zu)",
5943 e->args.size(), e->args.size()), e->tok);
5944
5945 // Compute actual arguments
5946 vector<tmpvar> tmp;
5947
5948 for (unsigned i=0; i<e->args.size(); i++)
5949 {
5950 tmpvar t = gensym(e->args[i]->type);
5951 c_assign (t, e->args[i],
5952 "print format actual argument evaluation");
5953 tmp.push_back(t);
5954 }
5955
5956 // Allocate the result
5957 exp_type ty = e->print_to_stream ? pe_long : pe_string;
5958 tmpvar res = gensym (ty);
5959
5960 // Munge so we can find our compiled printf
5961 vector<print_format::format_component> components;
5962 string format_string, format_string_out;
5963 bool use_print = preprocess_print_format(e, tmp, components, format_string);
5964 format_string_out = print_format::components_to_string(components);
5965
5966 // Make the [s]printf call...
5967
5968 // Generate code to check that any pointer arguments are actually accessible.
5969 size_t arg_ix = 0;
5970 for (unsigned i = 0; i < components.size(); ++i) {
5971 if (components[i].type == print_format::conv_literal)
5972 continue;
5973
5974 /* Take note of the width and precision arguments, if any. */
5975 int width_ix = -1, prec_ix= -1;
5976 if (components[i].widthtype == print_format::width_dynamic)
5977 width_ix = arg_ix++;
5978 if (components[i].prectype == print_format::prec_dynamic)
5979 prec_ix = arg_ix++;
5980
5981 (void) width_ix; /* XXX: notused */
5982
5983 /* %m and %M need special care for digging into memory. */
5984 if (components[i].type == print_format::conv_memory
5985 || components[i].type == print_format::conv_memory_hex)
5986 {
5987 string mem_size;
5988 const token* prec_tok = e->tok;
5989 if (prec_ix != -1)
5990 {
5991 mem_size = tmp[prec_ix].value();
5992 prec_tok = e->args[prec_ix]->tok;
5993 }
5994 else if (components[i].prectype == print_format::prec_static &&
5995 components[i].precision > 0)
5996 mem_size = lex_cast(components[i].precision) + "LL";
5997 else
5998 mem_size = "1LL";
5999
6000 /* Limit how much can be printed at a time. (see also PR10490) */
6001 o->newline() << "c->last_stmt = " << lex_cast_qstring(*prec_tok) << ";";
6002 o->newline() << "if (" << mem_size << " > PAGE_SIZE) {";
6003 o->newline(1) << "snprintf(c->error_buffer, sizeof(c->error_buffer), "
6004 << "\"%lld is too many bytes for a memory dump\", (long long)"
6005 << mem_size << ");";
6006 o->newline() << "c->last_error = c->error_buffer;";
6007 o->newline() << "goto out;";
6008 o->newline(-1) << "}";
6009 }
6010
6011 ++arg_ix;
6012 }
6013
6014 // Shortcuts for cases that aren't formatted at all
6015 if (e->print_to_stream)
6016 {
6017 if (e->print_char)
6018 {
6019 o->newline() << "_stp_print_char (";
6020 if (tmp.size())
6021 o->line() << tmp[0].value() << ");";
6022 else
6023 o->line() << '"' << format_string_out << "\");";
6024 return;
6025 }
6026 if (use_print)
6027 {
6028 o->newline() << "_stp_print (";
6029 if (tmp.size())
6030 o->line() << tmp[0].value() << ");";
6031 else
6032 o->line() << '"' << format_string_out << "\");";
6033 return;
6034 }
6035 }
6036
6037 // The default it to use the new compiled-printf, but one can fall back
6038 // to the old code with -DSTP_LEGACY_PRINT if desired.
6039 o->newline() << "#ifndef STP_LEGACY_PRINT";
6040 o->indent(1);
6041
6042 // Copy all arguments to the compiled-printf's space, then call it
6043 const string& compiled_printf =
6044 get_compiled_printf (e->print_to_stream, format_string);
6045 for (unsigned i = 0; i < tmp.size(); ++i)
6046 o->newline() << "c->printf_locals." << compiled_printf
6047 << ".arg" << i << " = " << tmp[i].value() << ";";
6048 if (e->print_to_stream)
6049 // We'll just hardcode the result of 0 instead of using the
6050 // temporary.
6051 res.override("((int64_t)0LL)");
6052 else
6053 o->newline() << "c->printf_locals." << compiled_printf
6054 << ".__retvalue = " << res.value() << ";";
6055 o->newline() << compiled_printf << " (c);";
6056
6057 o->newline(-1) << "#else // STP_LEGACY_PRINT";
6058 o->indent(1);
6059
6060 // Generate the legacy call that goes through _stp_vsnprintf.
6061 if (e->print_to_stream)
6062 o->newline() << "_stp_printf (";
6063 else
6064 o->newline() << "_stp_snprintf (" << res.value() << ", MAXSTRINGLEN, ";
6065 o->line() << '"' << format_string_out << '"';
6066
6067 // Make sure arguments match the expected type of the format specifier.
6068 arg_ix = 0;
6069 for (unsigned i = 0; i < components.size(); ++i)
6070 {
6071 if (components[i].type == print_format::conv_literal)
6072 continue;
6073
6074 /* Cast the width and precision arguments, if any, to 'int'. */
6075 if (components[i].widthtype == print_format::width_dynamic)
6076 o->line() << ", (int)" << tmp[arg_ix++].value();
6077 if (components[i].prectype == print_format::prec_dynamic)
6078 o->line() << ", (int)" << tmp[arg_ix++].value();
6079
6080 /* The type of the %m argument is 'char*'. */
6081 if (components[i].type == print_format::conv_memory
6082 || components[i].type == print_format::conv_memory_hex)
6083 o->line() << ", (char*)(uintptr_t)" << tmp[arg_ix++].value();
6084 /* The type of the %c argument is 'int'. */
6085 else if (components[i].type == print_format::conv_char)
6086 o->line() << ", (int)" << tmp[arg_ix++].value();
6087 else if (arg_ix < tmp.size())
6088 o->line() << ", " << tmp[arg_ix++].value();
6089 }
6090 o->line() << ");";
6091 o->newline(-1) << "#endif // STP_LEGACY_PRINT";
6092 o->newline() << "if (unlikely(c->last_error || c->aborted)) goto out;";
6093 o->newline() << res.value() << ";";
6094 }
6095 }
6096
6097 void
6098 c_unparser::visit_stat_op (stat_op* e)
6099 {
6100 // Stat ops can be *applied* to two types of expression:
6101 //
6102 // 1. An arrayindex expression on a pe_stats-valued array.
6103 //
6104 // 2. A symbol of type pe_stats.
6105
6106 // FIXME: classify the expression the stat_op is being applied to,
6107 // call appropriate stp_get_stat() / stp_pmap_get_stat() helper,
6108 // then reach into resultant struct stat_data.
6109
6110 // FIXME: also note that summarizing anything is expensive, and we
6111 // really ought to pass a timeout handler into the summary routine,
6112 // check its response, possibly exit if it ran out of cycles.
6113
6114 {
6115 stmt_expr block(*this);
6116 aggvar agg = gensym_aggregate ();
6117 tmpvar res = gensym (pe_long);
6118 var *v = load_aggregate(e->stat, agg);
6119 {
6120 // PR 2142+2610: empty aggregates
6121 if ((e->ctype == sc_count) ||
6122 (e->ctype == sc_sum &&
6123 strverscmp(session->compatible.c_str(), "1.5") >= 0))
6124 {
6125 o->newline() << "if (unlikely (" << agg.value() << " == NULL))";
6126 o->indent(1);
6127 c_assign(res, "0", e->tok);
6128 o->indent(-1);
6129 }
6130 else
6131 {
6132 o->newline() << "if (unlikely (" << agg.value() << " == NULL)"
6133 << " || " << agg.value() << "->count == 0) {";
6134 o->newline(1) << "c->last_error = ";
6135 o->line() << STAP_T_06;
6136 o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
6137 o->newline() << "goto out;";
6138 o->newline(-1) << "}";
6139 }
6140 o->newline() << "else";
6141 o->indent(1);
6142 switch (e->ctype)
6143 {
6144 case sc_average:
6145 c_assign(res, ("_stp_div64(NULL, " + agg.value() + "->sum, "
6146 + agg.value() + "->count)"),
6147 e->tok);
6148 break;
6149 case sc_count:
6150 c_assign(res, agg.value() + "->count", e->tok);
6151 break;
6152 case sc_sum:
6153 c_assign(res, agg.value() + "->sum", e->tok);
6154 break;
6155 case sc_min:
6156 c_assign(res, agg.value() + "->min", e->tok);
6157 break;
6158 case sc_max:
6159 c_assign(res, agg.value() + "->max", e->tok);
6160 break;
6161 case sc_variance:
6162 c_assign(res, agg.value() + "->variance", e->tok);
6163 break;
6164 case sc_none:
6165 assert (0); // should not happen, as sc_none is only used in foreach sorts
6166 }
6167 o->indent(-1);
6168 }
6169 o->newline() << res << ";";
6170 delete v;
6171 }
6172 }
6173
6174
6175 void
6176 c_unparser::visit_hist_op (hist_op*)
6177 {
6178 // Hist ops can only occur in a limited set of circumstances:
6179 //
6180 // 1. Inside an arrayindex expression, as the base referent. See
6181 // c_unparser::visit_arrayindex for handling of this case.
6182 //
6183 // 2. Inside a foreach statement, as the base referent. See
6184 // c_unparser::visit_foreach_loop for handling this case.
6185 //
6186 // 3. Inside a print_format expression, as the sole argument. See
6187 // c_unparser::visit_print_format for handling this case.
6188 //
6189 // Note that none of these cases involves the c_unparser ever
6190 // visiting this node. We should not get here.
6191
6192 assert(false);
6193 }
6194
6195
6196 typedef map<Dwarf_Addr,const char*> addrmap_t; // NB: plain map, sorted by address
6197
6198 struct unwindsym_dump_context
6199 {
6200 systemtap_session& session;
6201 ostream& output;
6202 unsigned stp_module_index;
6203
6204 int build_id_len;
6205 unsigned char *build_id_bits;
6206 GElf_Addr build_id_vaddr;
6207
6208 unsigned long stp_kretprobe_trampoline_addr;
6209 Dwarf_Addr stext_offset;
6210
6211 vector<pair<string,unsigned> > seclist; // encountered relocation bases
6212 // (section names and sizes)
6213 map<unsigned, addrmap_t> addrmap; // per-relocation-base sorted addrmap
6214
6215 void *debug_frame;
6216 size_t debug_len;
6217 void *debug_frame_hdr;
6218 size_t debug_frame_hdr_len;
6219 Dwarf_Addr debug_frame_off;
6220 void *eh_frame;
6221 void *eh_frame_hdr;
6222 size_t eh_len;
6223 size_t eh_frame_hdr_len;
6224 Dwarf_Addr eh_addr;
6225 Dwarf_Addr eh_frame_hdr_addr;
6226 void *debug_line;
6227 size_t debug_line_len;
6228
6229 set<string> undone_unwindsym_modules;
6230 };
6231
6232 static bool need_byte_swap_for_target (const unsigned char e_ident[])
6233 {
6234 #if __BYTE_ORDER == __LITTLE_ENDIAN
6235 return (e_ident[EI_DATA] == ELFDATA2MSB);
6236 #elif __BYTE_ORDER == __BIG_ENDIAN
6237 return (e_ident[EI_DATA] == ELFDATA2LSB);
6238 #else
6239 #error Bad host __BYTE_ORDER
6240 #endif
6241 }
6242
6243 static void create_debug_frame_hdr (const unsigned char e_ident[],
6244 Elf_Data *debug_frame,
6245 void **debug_frame_hdr,
6246 size_t *debug_frame_hdr_len,
6247 Dwarf_Addr *debug_frame_off,
6248 systemtap_session& session,
6249 Dwfl_Module *mod)
6250 {
6251 *debug_frame_hdr = NULL;
6252 *debug_frame_hdr_len = 0;
6253
6254 int cies = 0;
6255 set< pair<Dwarf_Addr, Dwarf_Off> > fdes;
6256 set< pair<Dwarf_Addr, Dwarf_Off> >::iterator it;
6257
6258 // In the .debug_frame the FDE encoding is always DW_EH_PE_absptr.
6259 // So there is no need to read the CIEs. And the size is either 4
6260 // or 8, depending on the elf class from e_ident.
6261 int size = (e_ident[EI_CLASS] == ELFCLASS32) ? 4 : 8;
6262 bool need_byte_swap = need_byte_swap_for_target (e_ident);
6263 #define host_to_target_64(x) (need_byte_swap ? bswap_64((x)) : (x))
6264 #define host_to_target_32(x) (need_byte_swap ? bswap_32((x)) : (x))
6265 #define target_to_host_64(x) (need_byte_swap ? bswap_64((x)) : (x))
6266 #define target_to_host_32(x) (need_byte_swap ? bswap_32((x)) : (x))
6267
6268 int res = 0;
6269 Dwarf_Off off = 0;
6270 Dwarf_CFI_Entry entry;
6271
6272 while (res != 1)
6273 {
6274 Dwarf_Off next_off;
6275 res = dwarf_next_cfi (e_ident, debug_frame, false, off, &next_off,
6276 &entry);
6277 if (res == 0)
6278 {
6279 if (entry.CIE_id == DW_CIE_ID_64)
6280 cies++; // We can just ignore the CIEs.
6281 else
6282 {
6283 Dwarf_Addr addr;
6284 if (size == 4)
6285 addr = target_to_host_32((*((uint32_t *) entry.fde.start)));
6286 else
6287 addr = target_to_host_64((*((uint64_t *) entry.fde.start)));
6288 fdes.insert(pair<Dwarf_Addr, Dwarf_Off>(addr, off));
6289 }
6290 }
6291 else if (res > 0)
6292 ; // Great, all done.
6293 else
6294 {
6295 // Warn, but continue, backtracing will be slow...
6296 if (session.verbose > 2 && ! session.suppress_warnings)
6297 {
6298 const char *modname = dwfl_module_info (mod, NULL,
6299 NULL, NULL, NULL,
6300 NULL, NULL, NULL);
6301 session.print_warning("Problem creating debug frame hdr for "
6302 + lex_cast_qstring(modname)
6303 + ", " + dwarf_errmsg (-1));
6304 }
6305 return;
6306 }
6307 off = next_off;
6308 }
6309
6310 if (fdes.size() > 0)
6311 {
6312 it = fdes.begin();
6313 Dwarf_Addr first_addr = (*it).first;
6314 int res = dwfl_module_relocate_address (mod, &first_addr);
6315 DWFL_ASSERT ("create_debug_frame_hdr, dwfl_module_relocate_address",
6316 res >= 0);
6317 *debug_frame_off = (*it).first - first_addr;
6318 }
6319
6320 size_t total_size = 4 + (2 * size) + (2 * size * fdes.size());
6321 uint8_t *hdr = (uint8_t *) malloc(total_size);
6322 *debug_frame_hdr = hdr;
6323 *debug_frame_hdr_len = total_size;
6324
6325 hdr[0] = 1; // version
6326 hdr[1] = DW_EH_PE_absptr; // ptr encoding
6327 hdr[2] = (size == 4) ? DW_EH_PE_udata4 : DW_EH_PE_udata8; // count encoding
6328 hdr[3] = DW_EH_PE_absptr; // table encoding
6329 if (size == 4)
6330 {
6331 uint32_t *table = (uint32_t *)(hdr + 4);
6332 *table++ = host_to_target_32 ((uint32_t) 0); // eh_frame_ptr, unused
6333 *table++ = host_to_target_32 ((uint32_t) fdes.size());
6334 for (it = fdes.begin(); it != fdes.end(); it++)
6335 {
6336 *table++ = host_to_target_32 ((*it).first);
6337 *table++ = host_to_target_32 ((*it).second);
6338 }
6339 }
6340 else
6341 {
6342 uint64_t *table = (uint64_t *)(hdr + 4);
6343 *table++ = host_to_target_64 ((uint64_t) 0); // eh_frame_ptr, unused
6344 *table++ = host_to_target_64 ((uint64_t) fdes.size());
6345 for (it = fdes.begin(); it != fdes.end(); it++)
6346 {
6347 *table++ = host_to_target_64 ((*it).first);
6348 *table++ = host_to_target_64 ((*it).second);
6349 }
6350 }
6351 }
6352
6353 static set<string> vdso_paths;
6354
6355 // Get the .debug_frame end .eh_frame sections for the given module.
6356 // Also returns the lenght of both sections when found, plus the section
6357 // address (offset) of the eh_frame data. If a debug_frame is found, a
6358 // synthesized debug_frame_hdr is also returned.
6359 static void get_unwind_data (Dwfl_Module *m,
6360 void **debug_frame, void **eh_frame,
6361 size_t *debug_len, size_t *eh_len,
6362 Dwarf_Addr *eh_addr,
6363 void **eh_frame_hdr, size_t *eh_frame_hdr_len,
6364 void **debug_frame_hdr,
6365 size_t *debug_frame_hdr_len,
6366 Dwarf_Addr *debug_frame_off,
6367 Dwarf_Addr *eh_frame_hdr_addr,
6368 systemtap_session& session)
6369 {
6370 Dwarf_Addr start, bias = 0;
6371 GElf_Ehdr *ehdr, ehdr_mem;
6372 GElf_Shdr *shdr, shdr_mem;
6373 Elf_Scn *scn;
6374 Elf_Data *data = NULL;
6375 Elf *elf;
6376
6377 // fetch .eh_frame info preferably from main elf file.
6378 dwfl_module_info (m, NULL, &start, NULL, NULL, NULL, NULL, NULL);
6379 elf = dwfl_module_getelf(m, &bias);
6380 ehdr = gelf_getehdr(elf, &ehdr_mem);
6381
6382 scn = NULL;
6383 bool eh_frame_seen = false;
6384 bool eh_frame_hdr_seen = false;
6385 while ((scn = elf_nextscn(elf, scn)))
6386 {
6387 shdr = gelf_getshdr(scn, &shdr_mem);
6388 const char* scn_name = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
6389 if (!eh_frame_seen
6390 && strcmp(scn_name, ".eh_frame") == 0
6391 && shdr->sh_type == SHT_PROGBITS)
6392 {
6393 data = elf_rawdata(scn, NULL);
6394 *eh_frame = data->d_buf;
6395 *eh_len = data->d_size;
6396 // For ".dynamic" sections we want the offset, not absolute addr.
6397 // Note we don't trust dwfl_module_relocations() for ET_EXEC.
6398 if (ehdr->e_type != ET_EXEC && dwfl_module_relocations (m) > 0)
6399 *eh_addr = shdr->sh_addr - start + bias;
6400 else
6401 *eh_addr = shdr->sh_addr;
6402 eh_frame_seen = true;
6403 }
6404 else if (!eh_frame_hdr_seen
6405 && strcmp(scn_name, ".eh_frame_hdr") == 0
6406 && shdr->sh_type == SHT_PROGBITS)
6407 {
6408 data = elf_rawdata(scn, NULL);
6409 *eh_frame_hdr = data->d_buf;
6410 *eh_frame_hdr_len = data->d_size;
6411 if (ehdr->e_type != ET_EXEC && dwfl_module_relocations (m) > 0)
6412 *eh_frame_hdr_addr = shdr->sh_addr - start + bias;
6413 else
6414 *eh_frame_hdr_addr = shdr->sh_addr;
6415 eh_frame_hdr_seen = true;
6416 }
6417 if (eh_frame_seen && eh_frame_hdr_seen)
6418 break;
6419 }
6420
6421 // fetch .debug_frame info preferably from dwarf debuginfo file.
6422 elf = (dwarf_getelf (dwfl_module_getdwarf (m, &bias))
6423 ?: dwfl_module_getelf (m, &bias));
6424 ehdr = gelf_getehdr(elf, &ehdr_mem);
6425 scn = NULL;
6426 while ((scn = elf_nextscn(elf, scn)))
6427 {
6428 shdr = gelf_getshdr(scn, &shdr_mem);
6429 if (strcmp(elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name),
6430 ".debug_frame") == 0)
6431 {
6432 data = elf_rawdata(scn, NULL);
6433 *debug_frame = data->d_buf;
6434 *debug_len = data->d_size;
6435 break;
6436 }
6437 }
6438
6439 if (*debug_frame != NULL && *debug_len > 0)
6440 create_debug_frame_hdr (ehdr->e_ident, data,
6441 debug_frame_hdr, debug_frame_hdr_len,
6442 debug_frame_off, session, m);
6443 }
6444
6445 static int
6446 dump_build_id (Dwfl_Module *m,
6447 unwindsym_dump_context *c,
6448 const char *name, Dwarf_Addr)
6449 {
6450 string modname = name;
6451
6452 //extract build-id from debuginfo file
6453 int build_id_len = 0;
6454 unsigned char *build_id_bits;
6455 GElf_Addr build_id_vaddr;
6456
6457 if ((build_id_len=dwfl_module_build_id(m,
6458 (const unsigned char **)&build_id_bits,
6459 &build_id_vaddr)) > 0)
6460 {
6461 if (modname != "kernel")
6462 {
6463 Dwarf_Addr reloc_vaddr = build_id_vaddr;
6464 const char *secname;
6465 int i;
6466
6467 i = dwfl_module_relocate_address (m, &reloc_vaddr);
6468 DWFL_ASSERT ("dwfl_module_relocate_address reloc_vaddr", i >= 0);
6469
6470 secname = dwfl_module_relocation_info (m, i, NULL);
6471
6472 // assert same section name as in runtime/transport/symbols.c
6473 // NB: this is applicable only to module("...") probes.
6474 // process("...") ones may have relocation bases like '.dynamic',
6475 // and so we'll have to store not just a generic offset but
6476 // the relocation section/symbol name too: just like we do
6477 // for probe PC addresses themselves. We want to set build_id_vaddr for
6478 // user modules even though they will not have a secname.
6479
6480 if (modname[0] != '/')
6481 if (!secname || strcmp(secname, ".note.gnu.build-id"))
6482 throw SEMANTIC_ERROR (_("unexpected build-id reloc section ") +
6483 string(secname ?: "null"));
6484
6485 build_id_vaddr = reloc_vaddr;
6486 }
6487
6488 if (c->session.verbose > 1)
6489 {
6490 clog << _F("Found build-id in %s, length %d, start at %#" PRIx64,
6491 name, build_id_len, build_id_vaddr) << endl;
6492 }
6493
6494 c->build_id_len = build_id_len;
6495 c->build_id_vaddr = build_id_vaddr;
6496 c->build_id_bits = build_id_bits;
6497 }
6498
6499 return DWARF_CB_OK;
6500 }
6501
6502 static int
6503 dump_section_list (Dwfl_Module *m,
6504 unwindsym_dump_context *c,
6505 const char *name, Dwarf_Addr)
6506 {
6507 // Depending on ELF section names normally means you are doing it WRONG.
6508 // Sadly it seems we do need it for the kernel modules. Which are ET_REL
6509 // files, which are "dynamically loaded" by the kernel. We keep a section
6510 // list for them to know which symbol corresponds to which section.
6511 //
6512 // Luckily for the kernel, normal executables (ET_EXEC) or shared
6513 // libraries (ET_DYN) we don't need it. We just have one "section",
6514 // which we will just give the arbitrary names "_stext", ".absolute"
6515 // or ".dynamic"
6516
6517 string modname = name;
6518
6519 // Use start and end as to calculate size for _stext, .dynamic and
6520 // .absolute sections.
6521 Dwarf_Addr start, end;
6522 dwfl_module_info (m, NULL, &start, &end, NULL, NULL, NULL, NULL);
6523
6524 // Look up the relocation basis for symbols
6525 int n = dwfl_module_relocations (m);
6526 DWFL_ASSERT ("dwfl_module_relocations", n >= 0);
6527
6528 if (n == 0)
6529 {
6530 // ET_EXEC, no relocations.
6531 string secname = ".absolute";
6532 unsigned size = end - start;
6533 c->seclist.push_back (make_pair (secname, size));
6534 return DWARF_CB_OK;
6535 }
6536 else if (n == 1)
6537 {
6538 // kernel or shared library (ET_DYN).
6539 string secname;
6540 secname = (modname == "kernel") ? "_stext" : ".dynamic";
6541 unsigned size = end - start;
6542 c->seclist.push_back (make_pair (secname, size));
6543 return DWARF_CB_OK;
6544 }
6545 else if (n > 1)
6546 {
6547 // ET_REL, kernel module.
6548 string secname;
6549 unsigned size;
6550 Dwarf_Addr bias;
6551 GElf_Ehdr *ehdr, ehdr_mem;
6552 GElf_Shdr *shdr, shdr_mem;
6553 Elf *elf = dwfl_module_getelf(m, &bias);
6554 ehdr = gelf_getehdr(elf, &ehdr_mem);
6555 Elf_Scn *scn = NULL;
6556 while ((scn = elf_nextscn(elf, scn)))
6557 {
6558 // Just the "normal" sections with program bits please.
6559 shdr = gelf_getshdr(scn, &shdr_mem);
6560 if ((shdr->sh_type == SHT_PROGBITS || shdr->sh_type == SHT_NOBITS)
6561 && (shdr->sh_flags & SHF_ALLOC))
6562 {
6563 size = shdr->sh_size;
6564 const char* scn_name = elf_strptr(elf, ehdr->e_shstrndx,
6565 shdr->sh_name);
6566 secname = scn_name;
6567 c->seclist.push_back (make_pair (secname, size));
6568 }
6569 }
6570
6571 return DWARF_CB_OK;
6572 }
6573
6574 // Impossible... dflw_assert above will have triggered.
6575 return DWARF_CB_ABORT;
6576 }
6577
6578 static void find_debug_frame_offset (Dwfl_Module *m,
6579 unwindsym_dump_context *c)
6580 {
6581 Dwarf_Addr start, bias = 0;
6582 GElf_Ehdr *ehdr, ehdr_mem;
6583 GElf_Shdr *shdr, shdr_mem;
6584 Elf_Scn *scn = NULL;
6585 Elf_Data *data = NULL;
6586 Elf *elf;
6587
6588 dwfl_module_info (m, NULL, &start, NULL, NULL, NULL, NULL, NULL);
6589
6590 // fetch .debug_frame info preferably from dwarf debuginfo file.
6591 elf = (dwarf_getelf (dwfl_module_getdwarf (m, &bias))
6592 ?: dwfl_module_getelf (m, &bias));
6593 ehdr = gelf_getehdr(elf, &ehdr_mem);
6594
6595 while ((scn = elf_nextscn(elf, scn)))
6596 {
6597 shdr = gelf_getshdr(scn, &shdr_mem);
6598 if (strcmp(elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name),
6599 ".debug_frame") == 0)
6600 {
6601 data = elf_rawdata(scn, NULL);
6602 break;
6603 }
6604 }
6605
6606 if (!data) // need this check since dwarf_next_cfi() doesn't do it
6607 return;
6608
6609 // In the .debug_frame the FDE encoding is always DW_EH_PE_absptr.
6610 // So there is no need to read the CIEs. And the size is either 4
6611 // or 8, depending on the elf class from e_ident.
6612 int size = (ehdr->e_ident[EI_CLASS] == ELFCLASS32) ? 4 : 8;
6613 int res = 0;
6614 Dwarf_Off off = 0;
6615 Dwarf_CFI_Entry entry;
6616
6617 while (res != 1)
6618 {
6619 Dwarf_Off next_off;
6620 res = dwarf_next_cfi (ehdr->e_ident, data, false, off, &next_off, &entry);
6621 if (res == 0)
6622 {
6623 if (entry.CIE_id != DW_CIE_ID_64) // ignore CIEs
6624 {
6625 Dwarf_Addr addr;
6626 if (size == 4)
6627 addr = (*((uint32_t *) entry.fde.start));
6628 else
6629 addr = (*((uint64_t *) entry.fde.start));
6630 Dwarf_Addr first_addr = addr;
6631 int res = dwfl_module_relocate_address (m, &first_addr);
6632 DWFL_ASSERT ("find_debug_frame_offset, dwfl_module_relocate_address",
6633 res >= 0);
6634 c->debug_frame_off = addr - first_addr;
6635 }
6636 }
6637 else if (res < 1)
6638 return;
6639 off = next_off;
6640 }
6641 }
6642
6643 static int
6644 dump_line_tables_check (void *data, size_t data_len)
6645 {
6646 uint64_t unit_length = 0, header_length = 0;
6647 uint16_t version = 0;
6648 uint8_t *ptr = (uint8_t *)data, *endunitptr, opcode_base = 0;
6649 unsigned length = 4;
6650
6651 while (ptr < ((uint8_t *)data + data_len))
6652 {
6653 if (ptr + 4 > (uint8_t *)data + data_len)
6654 return DWARF_CB_ABORT;
6655
6656 unit_length = *((uint32_t *) ptr);
6657 ptr += 4;
6658 if (unit_length == 0xffffffff)
6659 {
6660 if (ptr + 8 > (uint8_t *)data + data_len)
6661 return DWARF_CB_ABORT;
6662 length = 8;
6663 unit_length = *((uint64_t *) ptr);
6664 ptr += 8;
6665 }
6666
6667 if ((ptr + unit_length > (uint8_t *)data + data_len) || unit_length <= 2)
6668 return DWARF_CB_ABORT;
6669
6670 endunitptr = ptr + unit_length;
6671
6672 version = *((uint16_t *)ptr);
6673 ptr += 2;
6674
6675 if (unit_length <= (2 + length))
6676 return DWARF_CB_ABORT;
6677
6678 if (length == 4)
6679 {
6680 header_length = *((uint32_t *) ptr);
6681 ptr += 4;
6682 }
6683 else
6684 {
6685 header_length = *((uint64_t *) ptr);
6686 ptr += 8;
6687 }
6688
6689 // safety check for the next few jumps
6690 if (header_length <= ((version >= 4 ? 5 : 4) + 2)
6691 || (unit_length - (2 + length) < header_length))
6692 return DWARF_CB_ABORT;
6693
6694 // skip past min instr length, max ops per instr, and line base
6695 if (version >= 4)
6696 ptr += 3;
6697 else
6698 ptr += 2;
6699
6700 // check that the line range is not 0
6701 if (*ptr == 0)
6702 return DWARF_CB_ABORT;
6703 ptr++;
6704
6705 // check that the header accomodates the std opcode lens section
6706 opcode_base = *((uint8_t *) ptr);
6707 if (header_length <= (uint64_t) (opcode_base + (version >= 4 ? 7 : 6)))
6708 return DWARF_CB_ABORT;
6709
6710 // the initial checks stop here, before the directory table
6711 ptr = endunitptr;
6712 }
6713 return DWARF_CB_OK;
6714 }
6715
6716 static void
6717 dump_line_tables (Dwfl_Module *m, unwindsym_dump_context *c,
6718 const char *, Dwarf_Addr)
6719 {
6720 Elf* elf;
6721 Elf_Scn* scn = NULL;
6722 Elf_Data* data;
6723 GElf_Ehdr *ehdr, ehdr_mem;
6724 GElf_Shdr* shdr, shdr_mem;
6725 Dwarf_Addr bias, start;
6726
6727 dwfl_module_info (m, NULL, &start, NULL, NULL, NULL, NULL, NULL);
6728
6729 elf = dwfl_module_getelf (m, &bias);
6730 if (elf == NULL)
6731 return;
6732
6733 // we do not have the index for debug_line, so we can't use elf_getscn()
6734 // instead, we need to seach through the sections for the correct one as in
6735 // get_unwind_data()
6736 ehdr = gelf_getehdr(elf, &ehdr_mem);
6737 while ((scn = elf_nextscn(elf, scn)))
6738 {
6739 shdr = gelf_getshdr(scn, &shdr_mem);
6740 if (strcmp(elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name),
6741 ".debug_line") == 0)
6742 {
6743 data = elf_rawdata(scn, NULL);
6744 if (dump_line_tables_check(data->d_buf, data->d_size) == DWARF_CB_ABORT)
6745 return;
6746 c->debug_line = data->d_buf;
6747 c->debug_line_len = data->d_size;
6748 break;
6749 }
6750 }
6751
6752 // still need to get some kind of information about the sec_load_offset for
6753 // kernel addresses if there is no unwind data
6754 if (c->debug_line_len > 0 && !c->session.need_unwind)
6755 find_debug_frame_offset (m, c);
6756 }
6757
6758 /* Some architectures create special local symbols that are not
6759 interesting. */
6760 static int
6761 skippable_arch_symbol (GElf_Half e_machine, const char *name, GElf_Sym *sym)
6762 {
6763 /* Filter out ARM mapping symbols */
6764 if (e_machine == EM_ARM
6765 && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
6766 && (! strcmp(name, "$a") || ! strcmp(name, "$t")
6767 || ! strcmp(name, "$t.x") || ! strcmp(name, "$d")
6768 || ! strcmp(name, "$v") || ! strcmp(name, "$d.realdata")))
6769 return 1;
6770
6771 return 0;
6772 }
6773
6774 static int
6775 dump_symbol_tables (Dwfl_Module *m,
6776 unwindsym_dump_context *c,
6777 const char *modname, Dwarf_Addr base)
6778 {
6779 // Use end as sanity check when resolving symbol addresses.
6780 Dwarf_Addr end;
6781 dwfl_module_info (m, NULL, NULL, &end, NULL, NULL, NULL, NULL);
6782
6783 int syments = dwfl_module_getsymtab(m);
6784 DWFL_ASSERT (_F("Getting symbol table for %s", modname), syments >= 0);
6785
6786 // Look up the relocation basis for symbols
6787 int n = dwfl_module_relocations (m);
6788 DWFL_ASSERT ("dwfl_module_relocations", n >= 0);
6789
6790 /* Needed on ppc64, for function descriptors. */
6791 Dwarf_Addr elf_bias;
6792 GElf_Ehdr *ehdr, ehdr_mem;
6793 Elf *elf;
6794 elf = dwfl_module_getelf(m, &elf_bias);
6795 ehdr = gelf_getehdr(elf, &ehdr_mem);
6796
6797 // XXX: unfortunate duplication with tapsets.cxx:emit_address()
6798
6799 // extra_offset is for the special kernel case.
6800 Dwarf_Addr extra_offset = 0;
6801 Dwarf_Addr kretprobe_trampoline_addr = (unsigned long) -1;
6802 int is_kernel = !strcmp(modname, "kernel");
6803
6804 /* Set to bail early if we are just examining the kernel
6805 and don't need anything more. */
6806 int done = 0;
6807 for (int i = 0; i < syments && !done; ++i)
6808 {
6809 if (pending_interrupts)
6810 return DWARF_CB_ABORT;
6811
6812 GElf_Sym sym;
6813 GElf_Word shndxp;
6814
6815 const char *name = dwfl_module_getsym(m, i, &sym, &shndxp);
6816 if (name)
6817 {
6818 Dwarf_Addr sym_addr = sym.st_value;
6819
6820 // We always need two special values from the kernel.
6821 // _stext for extra_offset and kretprobe_trampoline_holder
6822 // for the unwinder.
6823 if (is_kernel)
6824 {
6825 // NB: Yey, we found the kernel's _stext value.
6826 // Sess.sym_stext may be unset (0) at this point, since
6827 // there may have been no kernel probes set. We could
6828 // use tapsets.cxx:lookup_symbol_address(), but then
6829 // we're already iterating over the same data here...
6830 if (! strcmp(name, KERNEL_RELOC_SYMBOL))
6831 {
6832 int ki;
6833 extra_offset = sym_addr;
6834 ki = dwfl_module_relocate_address (m, &extra_offset);
6835 DWFL_ASSERT ("dwfl_module_relocate_address extra_offset",
6836 ki >= 0);
6837
6838 if (c->session.verbose > 2)
6839 clog << _F("Found kernel _stext extra offset %#" PRIx64,
6840 extra_offset) << endl;
6841
6842 if (! c->session.need_symbols
6843 && (kretprobe_trampoline_addr != (unsigned long) -1
6844 || ! c->session.need_unwind))
6845 done = 1;
6846 }
6847 else if (kretprobe_trampoline_addr == (unsigned long) -1
6848 && c->session.need_unwind
6849 && ! strcmp(name, "kretprobe_trampoline_holder"))
6850 {
6851 int ki;
6852 kretprobe_trampoline_addr = sym_addr;
6853 ki = dwfl_module_relocate_address(m,
6854 &kretprobe_trampoline_addr);
6855 DWFL_ASSERT ("dwfl_module_relocate_address, kretprobe_trampoline_addr", ki >= 0);
6856
6857 if (! c->session.need_symbols
6858 && extra_offset != 0)
6859 done = 1;
6860 }
6861 }
6862
6863 // We are only interested in "real" symbols.
6864 // We omit symbols that have suspicious addresses
6865 // (before base, or after end).
6866 if (!done && c->session.need_symbols
6867 && ! skippable_arch_symbol(ehdr->e_machine, name, &sym)
6868 && (GELF_ST_TYPE (sym.st_info) == STT_FUNC
6869 || (GELF_ST_TYPE (sym.st_info) == STT_NOTYPE
6870 && (ehdr->e_type == ET_REL // PR10206 ppc fn-desc in .opd
6871 || is_kernel)) // kernel entry functions are NOTYPE
6872 || GELF_ST_TYPE (sym.st_info) == STT_OBJECT) // PR10000: .data
6873 && !(sym.st_shndx == SHN_UNDEF // Value undefined,
6874 || shndxp == (GElf_Word) -1 // in a non-allocated section,
6875 || sym_addr >= end // beyond current module,
6876 || sym_addr < base)) // before first section.
6877 {
6878 const char *secname = NULL;
6879 unsigned secidx = 0; /* Most things have just one section. */
6880 Dwarf_Addr func_desc_addr = 0; /* Function descriptor */
6881
6882 /* PPC64 uses function descriptors.
6883 Note: for kernel ET_REL modules we rely on finding the
6884 .function symbols instead of going through the opd function
6885 descriptors. */
6886 if (ehdr->e_machine == EM_PPC64
6887 && GELF_ST_TYPE (sym.st_info) == STT_FUNC
6888 && ehdr->e_type != ET_REL)
6889 {
6890 Elf64_Addr opd_addr;
6891 Dwarf_Addr opd_bias;
6892 Elf_Scn *opd;
6893
6894 func_desc_addr = sym_addr;
6895
6896 opd = dwfl_module_address_section (m, &sym_addr, &opd_bias);
6897 DWFL_ASSERT ("dwfl_module_address_section opd", opd != NULL);
6898
6899 Elf_Data *opd_data = elf_rawdata (opd, NULL);
6900 assert(opd_data != NULL);
6901
6902 Elf_Data opd_in, opd_out;
6903 opd_out.d_buf = &opd_addr;
6904 opd_in.d_buf = (char *) opd_data->d_buf + sym_addr;
6905 opd_out.d_size = opd_in.d_size = sizeof (Elf64_Addr);
6906 opd_out.d_type = opd_in.d_type = ELF_T_ADDR;
6907 if (elf64_xlatetom (&opd_out, &opd_in,
6908 ehdr->e_ident[EI_DATA]) == NULL)
6909 throw runtime_error ("elf64_xlatetom failed");
6910
6911 // So the real address of the function is...
6912 sym_addr = opd_addr + opd_bias;
6913 }
6914
6915 if (n > 0) // only try to relocate if there exist relocation bases
6916 {
6917 int ki = dwfl_module_relocate_address (m, &sym_addr);
6918 DWFL_ASSERT ("dwfl_module_relocate_address sym_addr", ki >= 0);
6919 secname = dwfl_module_relocation_info (m, ki, NULL);
6920
6921 if (func_desc_addr != 0)
6922 dwfl_module_relocate_address (m, &func_desc_addr);
6923 }
6924
6925 if (n == 1 && is_kernel)
6926 {
6927 // This is a symbol within a (possibly relocatable)
6928 // kernel image.
6929
6930 // We only need the function symbols to identify kernel-mode
6931 // PC's, so we omit undefined or "fake" absolute addresses.
6932 // These fake absolute addresses occur in some older i386
6933 // kernels to indicate they are vDSO symbols, not real
6934 // functions in the kernel. We also omit symbols that have
6935 if (GELF_ST_TYPE (sym.st_info) == STT_FUNC
6936 && sym.st_shndx == SHN_ABS)
6937 continue;
6938
6939 secname = "_stext";
6940 // NB: don't subtract session.sym_stext, which could be
6941 // inconveniently NULL. Instead, sym_addr will get
6942 // compensated later via extra_offset.
6943 }
6944 else if (n > 0)
6945 {
6946 assert (secname != NULL);
6947 // secname adequately set
6948
6949 // NB: it may be an empty string for ET_DYN objects
6950 // like shared libraries, as their relocation base
6951 // is implicit.
6952 if (secname[0] == '\0')
6953 secname = ".dynamic";
6954 else
6955 {
6956 // Compute our section number
6957 for (secidx = 0; secidx < c->seclist.size(); secidx++)
6958 if (c->seclist[secidx].first==secname)
6959 break;
6960
6961 if (secidx == c->seclist.size()) // PR23747 not an error
6962 {
6963 continue; // way back to the next symbol
6964 }
6965 }
6966 }
6967 else
6968 {
6969 assert (n == 0);
6970 // sym_addr is absolute, as it must be since there are
6971 // no relocation bases
6972 secname = ".absolute"; // sentinel
6973 }
6974
6975 (c->addrmap[secidx])[sym_addr] = name;
6976 /* If we have a function descriptor, register that address
6977 under the same name */
6978 if (func_desc_addr != 0)
6979 (c->addrmap[secidx])[func_desc_addr] = name;
6980 }
6981 }
6982 }
6983
6984 if (is_kernel)
6985 {
6986 c->stext_offset = extra_offset;
6987 // Must be relative to actual kernel load address.
6988 if (kretprobe_trampoline_addr != (unsigned long) -1)
6989 c->stp_kretprobe_trampoline_addr = (kretprobe_trampoline_addr
6990 - extra_offset);
6991 }
6992
6993 return DWARF_CB_OK;
6994 }
6995
6996 static int
6997 dump_unwind_tables (Dwfl_Module *m,
6998 unwindsym_dump_context *c,
6999 const char *, Dwarf_Addr)
7000 {
7001 // Add unwind data to be included if it exists for this module.
7002 get_unwind_data (m, &c->debug_frame, &c->eh_frame,
7003 &c->debug_len, &c->eh_len,
7004 &c->eh_addr, &c->eh_frame_hdr, &c->eh_frame_hdr_len,
7005 &c->debug_frame_hdr, &c->debug_frame_hdr_len,
7006 &c->debug_frame_off, &c->eh_frame_hdr_addr,
7007 c->session);
7008 return DWARF_CB_OK;
7009 }
7010
7011 static void
7012 dump_unwindsym_cxt_table(systemtap_session& session, ostream& output,
7013 const string& modname, unsigned modindex,
7014 const string& secname, unsigned secindex,
7015 const string& table, void*& data, size_t& len)
7016 {
7017 if (data == NULL || len == 0)
7018 return;
7019
7020 if (len > MAX_UNWIND_TABLE_SIZE)
7021 {
7022 if (secname.empty())
7023 session.print_warning (_F("skipping module %s %s table (too big: %zi > %zi)",
7024 modname.c_str(), table.c_str(),
7025 len, (size_t)MAX_UNWIND_TABLE_SIZE));
7026 else
7027 session.print_warning (_F("skipping module %s, section %s %s table (too big: %zi > %zi)",
7028 modname.c_str(), secname.c_str(), table.c_str(),
7029 len, (size_t)MAX_UNWIND_TABLE_SIZE));
7030 data = NULL;
7031 len = 0;
7032 return;
7033 }
7034
7035 // if it is the debug_line data, do not need the unwind flags to be defined
7036 if(table == "debug_line")
7037 output << "#if defined(STP_NEED_LINE_DATA)\n";
7038 else
7039 output << "#if defined(STP_USE_DWARF_UNWINDER) && defined(STP_NEED_UNWIND_DATA)\n";
7040 output << "static uint8_t _stp_module_" << modindex << "_" << table;
7041 if (!secname.empty())
7042 output << "_" << secindex;
7043 output << "[] = \n";
7044 output << " {";
7045 for (size_t i = 0; i < len; i++)
7046 {
7047 int h = ((uint8_t *)data)[i];
7048 output << h << ","; // decimal is less wordy than hex
7049 if ((i + 1) % 16 == 0)
7050 output << "\n" << " ";
7051 }
7052 output << "};\n";
7053 if (table == "debug_line")
7054 output << "#endif /* STP_NEED_LINE_DATA */\n";
7055 else
7056 output << "#endif /* STP_USE_DWARF_UNWINDER && STP_NEED_UNWIND_DATA */\n";
7057 }
7058
7059 static int
7060 dump_unwindsym_cxt (Dwfl_Module *m,
7061 unwindsym_dump_context *c,
7062 const char *name, Dwarf_Addr base)
7063 {
7064 string modname = name;
7065 unsigned stpmod_idx = c->stp_module_index;
7066 void *debug_frame = c->debug_frame;
7067 size_t debug_len = c->debug_len;
7068 void *debug_frame_hdr = c->debug_frame_hdr;
7069 size_t debug_frame_hdr_len = c->debug_frame_hdr_len;
7070 Dwarf_Addr debug_frame_off = c->debug_frame_off;
7071 void *eh_frame = c->eh_frame;
7072 void *eh_frame_hdr = c->eh_frame_hdr;
7073 size_t eh_len = c->eh_len;
7074 size_t eh_frame_hdr_len = c->eh_frame_hdr_len;
7075 Dwarf_Addr eh_addr = c->eh_addr;
7076 Dwarf_Addr eh_frame_hdr_addr = c->eh_frame_hdr_addr;
7077 void *debug_line = c->debug_line;
7078 size_t debug_line_len = c->debug_line_len;
7079
7080 dump_unwindsym_cxt_table(c->session, c->output, modname, stpmod_idx, "", 0,
7081 "debug_frame", debug_frame, debug_len);
7082
7083 dump_unwindsym_cxt_table(c->session, c->output, modname, stpmod_idx, "", 0,
7084 "eh_frame", eh_frame, eh_len);
7085
7086 dump_unwindsym_cxt_table(c->session, c->output, modname, stpmod_idx, "", 0,
7087 "eh_frame_hdr", eh_frame_hdr, eh_frame_hdr_len);
7088
7089 dump_unwindsym_cxt_table(c->session, c->output, modname, stpmod_idx, "", 0,
7090 "debug_line", debug_line, debug_line_len);
7091
7092 if (c->session.need_unwind && debug_frame == NULL && eh_frame == NULL)
7093 {
7094 // There would be only a small benefit to warning. A user
7095 // likely can't do anything about this; backtraces for the
7096 // affected module would just get all icky heuristicy.
7097 // So only report in verbose mode.
7098 if (c->session.verbose > 2)
7099 c->session.print_warning ("No unwind data for " + modname
7100 + ", " + dwfl_errmsg (-1));
7101 }
7102
7103 if (c->session.need_lines && debug_line == NULL)
7104 {
7105 if (c->session.verbose > 2)
7106 c->session.print_warning ("No debug line data for " + modname + ", " +
7107 dwfl_errmsg (-1));
7108 }
7109
7110 for (unsigned secidx = 0; secidx < c->seclist.size(); secidx++)
7111 {
7112 c->output << "static struct _stp_symbol "
7113 << "_stp_module_" << stpmod_idx<< "_symbols_" << secidx << "[] = {\n";
7114
7115 string secname = c->seclist[secidx].first;
7116 Dwarf_Addr extra_offset;
7117 extra_offset = (secname == "_stext") ? c->stext_offset : 0;
7118
7119 // Only include symbols if they will be used
7120 if (c->session.need_symbols)
7121 {
7122 // We write out a *sorted* symbol table, so the runtime doesn't
7123 // have to sort them later.
7124 for (addrmap_t::iterator it = c->addrmap[secidx].begin();
7125 it != c->addrmap[secidx].end(); it++)
7126 {
7127 // skip symbols that occur before our chosen base address
7128 if (it->first < extra_offset)
7129 continue;
7130
7131 c->output << " { 0x" << hex << it->first-extra_offset << dec
7132 << ", " << lex_cast_qstring (it->second) << " },\n";
7133 // XXX: these literal strings all suffer ELF relocation bloat too.
7134 // See if the tapsets.cxx:dwarf_derived_probe_group::emit_module_decls
7135 // CALCIT hack could work here.
7136 }
7137 }
7138
7139 c->output << "};\n";
7140
7141 /* For now output debug_frame index only in "magic" sections. */
7142 if (secname == ".dynamic" || secname == ".absolute"
7143 || secname == ".text" || secname == "_stext")
7144 {
7145 dump_unwindsym_cxt_table(c->session, c->output, modname, stpmod_idx, secname, secidx,
7146 "debug_frame_hdr", debug_frame_hdr, debug_frame_hdr_len);
7147 }
7148 }
7149
7150 c->output << "static struct _stp_section _stp_module_" << stpmod_idx<< "_sections[] = {\n";
7151 // For the kernel, executables (ET_EXEC) or shared libraries (ET_DYN)
7152 // there is just one section that covers the whole address space of
7153 // the module. For kernel modules (ET_REL) there can be multiple
7154 // sections that get relocated separately.
7155 for (unsigned secidx = 0; secidx < c->seclist.size(); secidx++)
7156 {
7157 c->output << "{\n"
7158 << ".name = " << lex_cast_qstring(c->seclist[secidx].first) << ",\n"
7159 << ".size = 0x" << hex << c->seclist[secidx].second << dec << ",\n"
7160 << ".symbols = _stp_module_" << stpmod_idx << "_symbols_" << secidx << ",\n"
7161 << ".num_symbols = " << c->addrmap[secidx].size() << ",\n";
7162
7163 /* For now output debug_frame index only in "magic" sections. */
7164 string secname = c->seclist[secidx].first;
7165 if (debug_frame_hdr && (secname == ".dynamic" || secname == ".absolute"
7166 || secname == ".text" || secname == "_stext"))
7167 {
7168 c->output << "#if defined(STP_USE_DWARF_UNWINDER)"
7169 << " && defined(STP_NEED_UNWIND_DATA)\n";
7170
7171 c->output << ".debug_hdr = "
7172 << "_stp_module_" << stpmod_idx
7173 << "_debug_frame_hdr_" << secidx << ",\n";
7174 c->output << ".debug_hdr_len = " << debug_frame_hdr_len << ", \n";
7175
7176 Dwarf_Addr dwbias = 0;
7177 dwfl_module_getdwarf (m, &dwbias);
7178 c->output << ".sec_load_offset = 0x"
7179 << hex << debug_frame_off - dwbias << dec << "\n";
7180
7181 c->output << "#else\n";
7182 c->output << ".debug_hdr = NULL,\n";
7183 c->output << ".debug_hdr_len = 0,\n";
7184 c->output << ".sec_load_offset = 0\n";
7185 c->output << "#endif /* STP_USE_DWARF_UNWINDER"
7186 << " && STP_NEED_UNWIND_DATA */\n";
7187
7188 }
7189 else
7190 {
7191 c->output << ".debug_hdr = NULL,\n";
7192 c->output << ".debug_hdr_len = 0,\n";
7193 if (c->session.need_lines && secname == ".text")
7194 {
7195 c->output << "#if defined(STP_NEED_LINE_DATA)\n";
7196 Dwarf_Addr dwbias = 0;
7197 dwfl_module_getdwarf (m, &dwbias);
7198 c->output << ".sec_load_offset = 0x"
7199 << hex << debug_frame_off - dwbias << dec << "\n";
7200 c->output << "#else\n";
7201 }
7202 c->output << ".sec_load_offset = 0\n";
7203 if (c->session.need_lines && secname == ".text")
7204 c->output << "#endif /* STP_NEED_LINE_DATA */\n";
7205 }
7206
7207 c->output << "},\n";
7208 }
7209 c->output << "};\n";
7210
7211 // Get the canonical path of the main file for comparison at runtime.
7212 // When given directly by the user through -d or in case of the kernel
7213 // name and path might differ. path should be used for matching.
7214 const char *mainfile;
7215 dwfl_module_info (m, NULL, NULL, NULL, NULL, NULL, &mainfile, NULL);
7216
7217 // For user space modules store canonical path.
7218 // For kernel modules just the name itself.
7219 string mainpath = resolve_path(mainfile);
7220 string mainname;
7221 if (is_user_module(modname)) // userspace
7222 mainname = lex_cast_qstring (path_remove_sysroot(c->session,mainpath));
7223 else
7224 { // kernel module
7225
7226 // If the module name is the full path to the ko, then we have to retrieve
7227 // the actual name by which the module will be known inside the kernel.
7228 // Otherwise, section relocations would be mismatched.
7229 if (is_fully_resolved(modname, c->session.sysroot, c->session.sysenv))
7230 mainname = lex_cast_qstring (modname_from_path(modname));
7231 else
7232 mainname = lex_cast_qstring (modname);
7233 }
7234
7235 c->output << "static struct _stp_module _stp_module_" << stpmod_idx << " = {\n";
7236 c->output << ".name = " << mainname.c_str() << ",\n";
7237 c->output << ".path = " << lex_cast_qstring (path_remove_sysroot(c->session,mainpath)) << ",\n";
7238 c->output << ".eh_frame_addr = 0x" << hex << eh_addr << dec << ", \n";
7239 c->output << ".unwind_hdr_addr = 0x" << hex << eh_frame_hdr_addr
7240 << dec << ", \n";
7241
7242 if (debug_frame != NULL)
7243 {
7244 c->output << "#if defined(STP_USE_DWARF_UNWINDER) && defined(STP_NEED_UNWIND_DATA)\n";
7245 c->output << ".debug_frame = "
7246 << "_stp_module_" << stpmod_idx << "_debug_frame, \n";
7247 c->output << ".debug_frame_len = " << debug_len << ", \n";
7248 c->output << "#else\n";
7249 }
7250
7251 c->output << ".debug_frame = NULL,\n";
7252 c->output << ".debug_frame_len = 0,\n";
7253
7254 if (debug_frame != NULL)
7255 c->output << "#endif /* STP_USE_DWARF_UNWINDER && STP_NEED_UNWIND_DATA*/\n";
7256
7257 if (eh_frame != NULL)
7258 {
7259 c->output << "#if defined(STP_USE_DWARF_UNWINDER) && defined(STP_NEED_UNWIND_DATA)\n";
7260 c->output << ".eh_frame = "
7261 << "_stp_module_" << stpmod_idx << "_eh_frame, \n";
7262 c->output << ".eh_frame_len = " << eh_len << ", \n";
7263 if (eh_frame_hdr)
7264 {
7265 c->output << ".unwind_hdr = "
7266 << "_stp_module_" << stpmod_idx << "_eh_frame_hdr, \n";
7267 c->output << ".unwind_hdr_len = " << eh_frame_hdr_len << ", \n";
7268 }
7269 else
7270 {
7271 c->output << ".unwind_hdr = NULL,\n";
7272 c->output << ".unwind_hdr_len = 0,\n";
7273 }
7274 c->output << "#else\n";
7275 }
7276
7277 c->output << ".eh_frame = NULL,\n";
7278 c->output << ".eh_frame_len = 0,\n";
7279 c->output << ".unwind_hdr = NULL,\n";
7280 c->output << ".unwind_hdr_len = 0,\n";
7281 if (eh_frame != NULL)
7282 c->output << "#endif /* STP_USE_DWARF_UNWINDER && STP_NEED_UNWIND_DATA*/\n";
7283
7284 if (debug_line != NULL)
7285 {
7286 c->output << "#if defined(STP_NEED_LINE_DATA)\n";
7287 c->output << ".debug_line = "
7288 << "_stp_module_" << stpmod_idx << "_debug_line, \n";
7289 c->output << ".debug_line_len = " << debug_line_len << ", \n";
7290 c->output << "#else\n";
7291 }
7292
7293 c->output << ".debug_line = NULL,\n";
7294 c->output << ".debug_line_len = 0,\n";
7295
7296 if (debug_line != NULL)
7297 c->output << "#endif /* STP_NEED_LINE_DATA */\n";
7298
7299 c->output << ".sections = _stp_module_" << stpmod_idx << "_sections" << ",\n";
7300 c->output << ".num_sections = sizeof(_stp_module_" << stpmod_idx << "_sections)/"
7301 << "sizeof(struct _stp_section),\n";
7302
7303 /* Don't save build-id if it is located before _stext.
7304 * This probably means that build-id will not be loaded at all and
7305 * happens for example with ARM kernel. Allow user space modules since the
7306 * check fails for a shared object.
7307 *
7308 * See also:
7309 * http://sourceware.org/ml/systemtap/2009-q4/msg00574.html
7310 */
7311 if (c->build_id_len > 0
7312 && (modname != "kernel" || (c->build_id_vaddr > base + c->stext_offset))) {
7313 c->output << ".build_id_bits = (unsigned char *)\"" ;
7314 for (int j=0; j<c->build_id_len;j++)
7315 c->output << "\\x" << hex
7316 << (unsigned short) *(c->build_id_bits+j) << dec;
7317
7318 c->output << "\",\n";
7319 c->output << ".build_id_len = " << c->build_id_len << ",\n";
7320
7321 /* XXX: kernel data boot-time relocation works differently from text.
7322 This hack assumes that offset between _stext and build id
7323 stays constant after relocation, but that's not necessarily
7324 correct either. We may instead need a relocation basis different
7325 from _stext, such as __start_notes. */
7326 if (modname == "kernel")
7327 c->output << ".build_id_offset = 0x" << hex << c->build_id_vaddr - (base + c->stext_offset)
7328 << dec << ",\n";
7329 // ET_DYN: task finder gives the load address. ET_EXEC: this is absolute address
7330 else
7331 c->output << ".build_id_offset = 0x" << hex
7332 << c->build_id_vaddr /* - base */
7333 << dec << ",\n";
7334 } else
7335 c->output << ".build_id_len = 0,\n";
7336
7337 //initialize the note section representing unloaded
7338 c->output << ".notes_sect = 0,\n";
7339
7340 c->output << "};\n\n";
7341
7342 c->undone_unwindsym_modules.erase (modname);
7343
7344 // release various malloc'd tables
7345 // if (eh_frame_hdr) free (eh_frame_hdr); -- nope, this one comes from the elf image in memory
7346 if (debug_frame_hdr) free (debug_frame_hdr);
7347
7348 return DWARF_CB_OK;
7349 }
7350
7351 static void dump_kallsyms(unwindsym_dump_context *c)
7352 {
7353 ifstream kallsyms("/proc/kallsyms");
7354 unsigned stpmod_idx = c->stp_module_index;
7355 string line;
7356 unsigned size = 0;
7357 Dwarf_Addr start = 0;
7358 Dwarf_Addr end = 0;
7359 Dwarf_Addr prev = 0;
7360
7361 c->output << "static struct _stp_symbol "
7362 << "_stp_module_" << stpmod_idx << "_symbols_" << 0 << "[] = {\n";
7363
7364 while (getline(kallsyms, line))
7365 {
7366 Dwarf_Addr addr;
7367 string name;
7368 string module;
7369 char type;
7370 istringstream iss(line);
7371
7372 iss >> hex >> addr >> type >> name >> module;
7373
7374 if (name == KERNEL_RELOC_SYMBOL)
7375 start = addr;
7376 else if (name == "_end" || module != "")
7377 {
7378 end = prev;
7379 break;
7380 }
7381
7382 if (!start || addr == 0 || prev == addr)
7383 continue;
7384
7385 c->output << " { 0x" << hex << addr - start << dec
7386 << ", " << lex_cast_qstring(name) << " },\n";
7387
7388 size++;
7389 prev = addr;
7390 }
7391
7392 c->output << "};\n";
7393 c->output << "static struct _stp_section _stp_module_" << stpmod_idx << "_sections[] = {\n";
7394 c->output << "{\n"
7395 << ".name = " << lex_cast_qstring(KERNEL_RELOC_SYMBOL) << ",\n"
7396 << ".size = 0x" << hex << end - start << dec << ",\n"
7397 << ".symbols = _stp_module_" << stpmod_idx << "_symbols_" << 0 << ",\n"
7398 << ".num_symbols = " << size << ",\n";
7399 c->output << "},\n";
7400 c->output << "};\n";
7401 c->output << "static struct _stp_module _stp_module_" << stpmod_idx << " = {\n";
7402 c->output << ".name = " << lex_cast_qstring("kernel") << ",\n";
7403 c->output << ".sections = _stp_module_" << stpmod_idx << "_sections" << ",\n";
7404 c->output << ".num_sections = sizeof(_stp_module_" << stpmod_idx << "_sections)/"
7405 << "sizeof(struct _stp_section),\n";
7406 c->output << "};\n\n";
7407
7408 c->undone_unwindsym_modules.erase("kernel");
7409 c->stp_module_index++;
7410 }
7411
7412 static int
7413 dump_unwindsyms (Dwfl_Module *m,
7414 void **userdata __attribute__ ((unused)),
7415 const char *name,
7416 Dwarf_Addr base,
7417 void *arg)
7418 {
7419 if (pending_interrupts)
7420 return DWARF_CB_ABORT;
7421
7422 unwindsym_dump_context *c = (unwindsym_dump_context*) arg;
7423 assert (c);
7424
7425 // skip modules/files we're not actually interested in
7426 string modname = name;
7427 if (c->session.unwindsym_modules.find(modname)
7428 == c->session.unwindsym_modules.end())
7429 return DWARF_CB_OK;
7430
7431 if (c->session.verbose > 1)
7432 clog << "dump_unwindsyms " << name
7433 << " index=" << c->stp_module_index
7434 << " base=0x" << hex << base << dec << endl;
7435
7436 // We want to extract several bits of information:
7437 //
7438 // - parts of the program-header that map the file's physical offsets to the text section
7439 // - section table: just a list of section (relocation) base addresses
7440 // - symbol table of the text-like sections, with all addresses relativized to each base
7441 // - the contents of .debug_frame and/or .eh_frame section, for unwinding purposes
7442
7443 int res = DWARF_CB_OK;
7444
7445 c->build_id_len = 0;
7446 c->build_id_vaddr = 0;
7447 c->build_id_bits = NULL;
7448 res = dump_build_id (m, c, name, base);
7449
7450 c->seclist.clear();
7451 if (res == DWARF_CB_OK)
7452 res = dump_section_list(m, c, name, base);
7453
7454 // We always need to check the symbols of the kernel if we use it,
7455 // for the extra_offset (also used for build_ids) and possibly
7456 // stp_kretprobe_trampoline_addr for the dwarf unwinder.
7457 c->addrmap.clear();
7458 if (res == DWARF_CB_OK
7459 && (c->session.need_symbols || ! strcmp(name, "kernel")))
7460 res = dump_symbol_tables (m, c, name, base);
7461
7462 c->debug_frame = NULL;
7463 c->debug_len = 0;
7464 c->debug_frame_hdr = NULL;
7465 c->debug_frame_hdr_len = 0;
7466 c->debug_frame_off = 0;
7467 c->eh_frame = NULL;
7468 c->eh_frame_hdr = NULL;
7469 c->eh_len = 0;
7470 c->eh_frame_hdr_len = 0;
7471 c->eh_addr = 0;
7472 c->eh_frame_hdr_addr = 0;
7473 if (res == DWARF_CB_OK && c->session.need_unwind)
7474 res = dump_unwind_tables (m, c, name, base);
7475
7476 c->debug_line = NULL;
7477 c->debug_line_len = 0;
7478 if (res == DWARF_CB_OK && c->session.need_lines)
7479 // we dont set res = dump_line_tables() because unwindsym stuff should still
7480 // get dumped to the output even if gathering debug_line data fails
7481 (void) dump_line_tables (m, c, name, base);
7482
7483 /* And finally dump everything collected in the output. */
7484 if (res == DWARF_CB_OK)
7485 res = dump_unwindsym_cxt (m, c, name, base);
7486
7487 if (res == DWARF_CB_OK)
7488 c->stp_module_index++;
7489
7490 return res;
7491 }
7492
7493
7494 // Emit symbol table & unwind data, plus any calls needed to register
7495 // them with the runtime.
7496 void emit_symbol_data_done (unwindsym_dump_context*, systemtap_session&);
7497
7498
7499 void
7500 add_unwindsym_iol_callback (set<string> *added, const char *data)
7501 {
7502 added->insert (string (data));
7503 }
7504
7505
7506 static int
7507 query_module (Dwfl_Module *mod,
7508 void **,
7509 const char *,
7510 Dwarf_Addr,
7511 struct dwflpp *dwflpp)
7512 {
7513 dwflpp->focus_on_module(mod, NULL);
7514 return DWARF_CB_OK;
7515 }
7516
7517
7518 void
7519 add_unwindsym_ldd (systemtap_session &s)
7520 {
7521 std::set<std::string> added;
7522
7523 for (std::set<std::string>::iterator it = s.unwindsym_modules.begin();
7524 it != s.unwindsym_modules.end();
7525 it++)
7526 {
7527 string modname = *it;
7528 assert (modname.length() != 0);
7529 if (! is_user_module (modname)) continue;
7530
7531 dwflpp mod_dwflpp (s, modname, false);
7532 mod_dwflpp.iterate_over_modules(&query_module, &mod_dwflpp);
7533 if (mod_dwflpp.module) // existing binary
7534 {
7535 assert (mod_dwflpp.module_name != "");
7536 mod_dwflpp.iterate_over_libraries (&add_unwindsym_iol_callback, &added);
7537 }
7538 }
7539
7540 s.unwindsym_modules.insert (added.begin(), added.end());
7541 }
7542
7543 static int find_vdso(const char *path, const struct stat *, int type)
7544 {
7545 if (type == FTW_F)
7546 {
7547 /* Assume that if the path's basename starts with 'vdso' and
7548 * ends with '.so', it is the vdso.
7549 *
7550 * Note that this logic should match up with the logic in the
7551 * _stp_vma_match_vdso() function in runtime/vma.c. */
7552 const char *name = strrchr(path, '/');
7553 if (name)
7554 {
7555 const char *ext;
7556
7557 name++;
7558 ext = strrchr(name, '.');
7559 if (ext
7560 && strncmp("vdso", name, 4) == 0
7561 && strcmp(".so", ext) == 0)
7562 vdso_paths.insert(path);
7563 }
7564 }
7565 return 0;
7566 }
7567
7568 void
7569 add_unwindsym_vdso (systemtap_session &s)
7570 {
7571 // This is to disambiguate between -r REVISION vs -r BUILDDIR.
7572 // See also dwflsetup.c (setup_dwfl_kernel). In case of only
7573 // having the BUILDDIR we need to do a deep search (the specific
7574 // arch name dir in the kernel build tree is unknown).
7575 string vdso_dir;
7576 if (s.kernel_build_tree == string(s.sysroot + "/lib/modules/"
7577 + s.kernel_release
7578 + "/build"))
7579 vdso_dir = s.sysroot + "/lib/modules/" + s.kernel_release + "/vdso";
7580 else
7581 vdso_dir = s.kernel_build_tree + "/arch/";
7582
7583 if (s.verbose > 1)
7584 clog << _("Searching for vdso candidates: ") << vdso_dir << endl;
7585
7586 ftw(vdso_dir.c_str(), find_vdso, 1);
7587
7588 for (set<string>::iterator it = vdso_paths.begin();
7589 it != vdso_paths.end();
7590 it++)
7591 {
7592 s.unwindsym_modules.insert(*it);
7593 if (s.verbose > 1)
7594 clog << _("vdso candidate: ") << *it << endl;
7595 }
7596 }
7597
7598 static void
7599 prepare_symbol_data (systemtap_session& s)
7600 {
7601 // step 0: run ldd on any user modules if requested
7602 if (s.unwindsym_ldd)
7603 add_unwindsym_ldd (s);
7604 // step 0.5: add vdso(s) when vma tracker was requested
7605 if (vma_tracker_enabled (s))
7606 add_unwindsym_vdso (s);
7607 // NB: do this before the ctx.unwindsym_modules copy is taken
7608 }
7609
7610 void
7611 emit_symbol_data (systemtap_session& s)
7612 {
7613 string symfile = "stap-symbols.h";
7614
7615 s.op->newline() << "#include " << lex_cast_qstring (symfile);
7616
7617 ofstream kallsyms_out ((s.tmpdir + "/" + symfile).c_str());
7618
7619 vector<pair<string,unsigned> > seclist;
7620 map<unsigned, addrmap_t> addrmap;
7621 unwindsym_dump_context ctx = { s, kallsyms_out,
7622 0, /* module index */
7623 0, NULL, 0, /* build_id len, bits, vaddr */
7624 ~0UL, /* stp_kretprobe_trampoline_addr */
7625 0, /* stext_offset */
7626 seclist, addrmap,
7627 NULL, /* debug_frame */
7628 0, /* debug_len */
7629 NULL, /* debug_frame_hdr */
7630 0, /* debug_frame_hdr_len */
7631 0, /* debug_frame_off */
7632 NULL, /* eh_frame */
7633 NULL, /* eh_frame_hdr */
7634 0, /* eh_len */
7635 0, /* eh_frame_hdr_len */
7636 0, /* eh_addr */
7637 0, /* eh_frame_hdr_addr */
7638 NULL, /* debug_line */
7639 0, /* debug_line_len */
7640 s.unwindsym_modules };
7641
7642 // Micro optimization, mainly to speed up tiny regression tests
7643 // using just begin probe.
7644 if (s.unwindsym_modules.size () == 0)
7645 {
7646 emit_symbol_data_done(&ctx, s);
7647 return;
7648 }
7649
7650 // ---- step 1: process any kernel modules listed
7651 set<string> offline_search_modules;
7652 unsigned count;
7653 for (set<string>::iterator it = s.unwindsym_modules.begin();
7654 it != s.unwindsym_modules.end();
7655 it++)
7656 {
7657 string foo = *it;
7658 if (! is_user_module (foo)) /* Omit user-space, since we're only
7659 using this for kernel space
7660 offline searches. */
7661 offline_search_modules.insert (foo);
7662 }
7663 Dwfl *dwfl = setup_dwfl_kernel (offline_search_modules, &count, s);
7664 /* NB: It's not an error to find a few fewer modules than requested.
7665 There might be third-party modules loaded (e.g. uprobes). */
7666 /* DWFL_ASSERT("all kernel modules found",
7667 count >= offline_search_modules.size()); */
7668
7669 ptrdiff_t off = 0;
7670 do
7671 {
7672 assert_no_interrupts();
7673 if (ctx.undone_unwindsym_modules.empty()) break;
7674 off = dwfl_getmodules (dwfl, &dump_unwindsyms, (void *) &ctx, off);
7675 }
7676 while (off > 0);
7677 DWFL_ASSERT("dwfl_getmodules", off == 0);
7678 dwfl_end(dwfl);
7679
7680 // ---- step 2: process any user modules (files) listed
7681 for (std::set<std::string>::iterator it = s.unwindsym_modules.begin();
7682 it != s.unwindsym_modules.end();
7683 it++)
7684 {
7685 string modname = *it;
7686 assert (modname.length() != 0);
7687 if (! is_user_module (modname)) continue;
7688 Dwfl *dwfl = setup_dwfl_user (modname);
7689 if (dwfl != NULL) // tolerate missing data; will warn below
7690 {
7691 ptrdiff_t off = 0;
7692 do
7693 {
7694 assert_no_interrupts();
7695 if (ctx.undone_unwindsym_modules.empty()) break;
7696 off = dwfl_getmodules (dwfl, &dump_unwindsyms, (void *) &ctx, off);
7697 }
7698 while (off > 0);
7699 DWFL_ASSERT("dwfl_getmodules", off == 0);
7700 }
7701 dwfl_end(dwfl);
7702 }
7703
7704 // Use /proc/kallsyms if debuginfo not found.
7705 if (ctx.undone_unwindsym_modules.find("kernel") != ctx.undone_unwindsym_modules.end())
7706 dump_kallsyms(&ctx);
7707
7708 emit_symbol_data_done (&ctx, s);
7709 }
7710
7711 void
7712 self_unwind_declarations(unwindsym_dump_context *ctx)
7713 {
7714 ctx->output << "static uint8_t _stp_module_self_eh_frame [] = {0,};\n";
7715 ctx->output << "static struct _stp_symbol _stp_module_self_symbols_0[] = {{0},};\n";
7716 ctx->output << "static struct _stp_symbol _stp_module_self_symbols_1[] = {{0},};\n";
7717 ctx->output << "static struct _stp_section _stp_module_self_sections[] = {\n";
7718 ctx->output << "{.name = \".symtab\", .symbols = _stp_module_self_symbols_0, .num_symbols = 0},\n";
7719 ctx->output << "{.name = \".text\", .symbols = _stp_module_self_symbols_1, .num_symbols = 0},\n";
7720 ctx->output << "};\n";
7721 ctx->output << "static struct _stp_module _stp_module_self = {\n";
7722 ctx->output << ".name = \"stap_self_tmp_value\",\n";
7723 ctx->output << ".path = \"stap_self_tmp_value\",\n";
7724 ctx->output << ".num_sections = 2,\n";
7725 ctx->output << ".sections = _stp_module_self_sections,\n";
7726 ctx->output << ".eh_frame = _stp_module_self_eh_frame,\n";
7727 ctx->output << ".eh_frame_len = 0,\n";
7728 ctx->output << ".unwind_hdr_addr = 0x0,\n";
7729 ctx->output << ".unwind_hdr = NULL,\n";
7730 ctx->output << ".unwind_hdr_len = 0,\n";
7731 ctx->output << ".debug_frame = NULL,\n";
7732 ctx->output << ".debug_frame_len = 0,\n";
7733 ctx->output << ".debug_line = NULL,\n";
7734 ctx->output << ".debug_line_len = 0,\n";
7735 ctx->output << "};\n";
7736 }
7737
7738 void
7739 emit_symbol_data_done (unwindsym_dump_context *ctx, systemtap_session& s)
7740 {
7741 // Add a .eh_frame terminator dummy object file, much like
7742 // libgcc/crtstuff.c's EH_FRAME_SECTION_NAME closer. We need this in
7743 // order for runtime/sym.c
7744 translator_output *T_800 = s.op_create_auxiliary(true);
7745 T_800->newline() << "__extension__ unsigned int T_800 []"; // assumed 32-bits wide
7746 T_800->newline(1) << "__attribute__((used, section(\".eh_frame\"), aligned(4)))";
7747 T_800->newline() << "= { 0 };";
7748 T_800->newline(-1);
7749 T_800->assert_0_indent (); // flush to disk
7750
7751 // Print out a definition of the runtime's _stp_modules[] globals.
7752 ctx->output << "\n";
7753 self_unwind_declarations(ctx);
7754 ctx->output << "static struct _stp_module *_stp_modules [] = {\n";
7755 for (unsigned i=0; i<ctx->stp_module_index; i++)
7756 {
7757 ctx->output << "& _stp_module_" << i << ",\n";
7758 }
7759 ctx->output << "& _stp_module_self,\n";
7760 ctx->output << "};\n";
7761 ctx->output << "static const unsigned _stp_num_modules = ARRAY_SIZE(_stp_modules);\n";
7762
7763 ctx->output << "static unsigned long _stp_kretprobe_trampoline = ";
7764 // Special case for -1, which is invalid in hex if host width > target width.
7765 if (ctx->stp_kretprobe_trampoline_addr == (unsigned long) -1)
7766 ctx->output << "-1;\n";
7767 else
7768 ctx->output << "0x" << hex << ctx->stp_kretprobe_trampoline_addr << dec
7769 << ";\n";
7770
7771 // Some nonexistent modules may have been identified with "-d". Note them.
7772 if (! s.suppress_warnings)
7773 for (set<string>::iterator it = ctx->undone_unwindsym_modules.begin();
7774 it != ctx->undone_unwindsym_modules.end();
7775 it ++)
7776 s.print_warning (_("missing unwind/symbol data for module '")
7777 + (*it) + "'");
7778 }
7779
7780 struct recursion_info: public traversing_visitor
7781 {
7782 recursion_info (systemtap_session& s): sess(s), nesting_max(0), recursive(false) {}
7783 systemtap_session& sess;
7784 unsigned nesting_max;
7785 bool recursive;
7786 std::vector <functiondecl *> current_nesting;
7787
7788 void visit_functioncall (functioncall* e) {
7789 traversing_visitor::visit_functioncall (e); // for arguments
7790
7791 for (unsigned fd = 0; fd < e->referents.size(); fd++)
7792 {
7793 functiondecl* referent = e->referents[fd];
7794 // check for nesting level
7795 unsigned nesting_depth = current_nesting.size() + 1;
7796 if (nesting_max < nesting_depth)
7797 {
7798 if (sess.verbose > 3)
7799 clog << _F("identified max-nested function: %s (%d)",
7800 referent->name.to_string().c_str(), nesting_depth) << endl;
7801 nesting_max = nesting_depth;
7802 }
7803
7804 // check for (direct or mutual) recursion
7805 for (unsigned j=0; j<current_nesting.size(); j++)
7806 if (current_nesting[j] == referent)
7807 {
7808 recursive = true;
7809 if (sess.verbose > 3)
7810 clog << _F("identified recursive function: %s",
7811 referent->name.to_string().c_str()) << endl;
7812 return;
7813 }
7814
7815 // non-recursive traversal
7816 current_nesting.push_back (referent);
7817 referent->body->visit (this);
7818 current_nesting.pop_back ();
7819 }
7820 }
7821 };
7822
7823
7824 void translate_runtime(systemtap_session& s)
7825 {
7826 s.op->newline() << "#define STAP_MSG_RUNTIME_H_01 "
7827 << lex_cast_qstring(_("myproc-unprivileged tapset function called "
7828 "without is_myproc checking for pid %d (euid %d)"));
7829
7830 s.op->newline() << "#define STAP_MSG_LOC2C_01 "
7831 << lex_cast_qstring(_("read fault [man error::fault] at 0x%lx"));
7832 s.op->newline() << "#define STAP_MSG_LOC2C_02 "
7833 << lex_cast_qstring(_("write fault [man error::fault] at 0x%lx"));
7834 s.op->newline() << "#define STAP_MSG_LOC2C_03 "
7835 << lex_cast_qstring(_("divide by zero in DWARF operand (%s)"));
7836 s.op->newline() << "#define STAP_MSG_LOC2C_04 "
7837 << lex_cast_qstring(_("register access fault [man error::fault]"));
7838 }
7839
7840
7841 int
7842 prepare_translate_pass (systemtap_session& s)
7843 {
7844 int rc = 0;
7845 try
7846 {
7847 prepare_symbol_data (s);
7848 }
7849 catch (const semantic_error& e)
7850 {
7851 s.print_error (e);
7852 rc = 1;
7853 }
7854
7855 return rc;
7856 }
7857
7858
7859 int
7860 translate_pass (systemtap_session& s)
7861 {
7862 int rc = 0;
7863
7864 s.op = new translator_output (s.translated_source);
7865 // additional outputs might be found in s.auxiliary_outputs
7866 c_unparser cup (& s);
7867 s.up = & cup;
7868 translate_runtime(s);
7869
7870 try
7871 {
7872 int64_t major=0, minor=0;
7873 try
7874 {
7875 vector<string> versions;
7876 tokenize (s.compatible, versions, ".");
7877 if (versions.size() >= 1)
7878 major = lex_cast<int64_t> (versions[0]);
7879 if (versions.size() >= 2)
7880 minor = lex_cast<int64_t> (versions[1]);
7881 if (versions.size() >= 3 && s.verbose > 1)
7882 clog << _F("ignoring extra parts of compat version: %s", s.compatible.c_str()) << endl;
7883 }
7884 catch (const runtime_error&)
7885 {
7886 throw SEMANTIC_ERROR(_F("parse error in compatibility version: %s", s.compatible.c_str()));
7887 }
7888 if (major < 0 || major > 255 || minor < 0 || minor > 255)
7889 throw SEMANTIC_ERROR(_F("compatibility version out of range: %s", s.compatible.c_str()));
7890 s.op->newline() << "#define STAP_VERSION(a, b) ( ((a) << 8) + (b) )";
7891 s.op->newline() << "#ifndef STAP_COMPAT_VERSION";
7892 s.op->newline() << "#define STAP_COMPAT_VERSION STAP_VERSION("
7893 << major << ", " << minor << ")";
7894 s.op->newline() << "#endif";
7895
7896 recursion_info ri (s);
7897
7898 // NB: we start our traversal from the s.functions[] rather than the probes.
7899 // We assume that each function is called at least once, or else it would have
7900 // been elided already.
7901 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
7902 {
7903 functiondecl *fd = it->second;
7904 fd->body->visit (& ri);
7905 }
7906
7907 if (s.verbose > 1)
7908 clog << _F("function recursion-analysis: max-nesting %d %s", ri.nesting_max,
7909 (ri.recursive ? _(" recursive") : _(" non-recursive"))) << endl;
7910 unsigned nesting = ri.nesting_max + 1; /* to account for initial probe->function call */
7911 if (ri.recursive) nesting += 10;
7912
7913 // This is at the very top of the file.
7914 // All "static" defines (not dependend on session state).
7915 s.op->newline() << "#include \"runtime_defines.h\"";
7916 if (s.perf_derived_probes)
7917 s.op->newline() << "#define _HAVE_PERF_ 1";
7918 s.op->newline() << "#include \"linux/perf_read.h\"";
7919
7920 // Generated macros describing the privilege level required to load/run this module.
7921 s.op->newline() << "#define STP_PR_STAPUSR 0x" << hex << pr_stapusr << dec;
7922 s.op->newline() << "#define STP_PR_STAPSYS 0x" << hex << pr_stapsys << dec;
7923 s.op->newline() << "#define STP_PR_STAPDEV 0x" << hex << pr_stapdev << dec;
7924 s.op->newline() << "#define STP_PRIVILEGE 0x" << hex << s.privilege << dec;
7925
7926 // Generate a section containing a mask of the privilege levels required to load/run this
7927 // module.
7928 s.op->newline() << "int stp_required_privilege "
7929 << "__attribute__ ((section (\"" << STAP_PRIVILEGE_SECTION <<"\")))"
7930 << " = STP_PRIVILEGE;";
7931
7932 s.op->newline() << "#ifndef MAXNESTING";
7933 s.op->newline() << "#define MAXNESTING " << nesting;
7934 s.op->newline() << "#endif";
7935
7936 // Generated macros specifying how much storage is required for
7937 // regexp subexpressions. (TODOXXX Skip when there are no DFAs?)
7938 s.op->newline() << "#define STAPREGEX_MAX_MAP " << s.dfa_maxmap;
7939 s.op->newline() << "#define STAPREGEX_MAX_TAG " << s.dfa_maxtag;
7940
7941 s.op->newline() << "#define STP_SKIP_BADVARS " << (s.skip_badvars ? 1 : 0);
7942
7943 if (s.bulk_mode)
7944 s.op->newline() << "#define STP_BULKMODE";
7945
7946 if (s.timing || s.monitor)
7947 s.op->newline() << "#define STP_TIMING";
7948
7949 if (s.need_unwind)
7950 s.op->newline() << "#define STP_NEED_UNWIND_DATA 1";
7951
7952 if (s.need_lines)
7953 s.op->newline() << "#define STP_NEED_LINE_DATA 1";
7954
7955 // Emit the total number of probes (not regarding merged probe handlers)
7956 s.op->newline() << "#define STP_PROBE_COUNT " << s.probes.size();
7957
7958 // Emit systemtap_module_refresh() prototype so we can reference it
7959 s.op->newline() << "static void systemtap_module_refresh (const char* modname);";
7960
7961 // Be sure to include runtime.h before any real code.
7962 s.op->newline() << "#include \"runtime.h\"";
7963
7964 if (!s.runtime_usermode_p())
7965 {
7966 // When on-the-fly [dis]arming is used, module_refresh can be called from
7967 // both the module notifier, as well as when probes need to be
7968 // armed/disarmed. We need to protect it to ensure it's only run one at a
7969 // time.
7970 s.op->newline() << "#include <linux/mutex.h>";
7971 s.op->newline() << "static DEFINE_MUTEX(module_refresh_mutex);";
7972
7973 // For some probes, on-the-fly support is provided through a
7974 // background timer (module_refresh_timer). We need to disable that
7975 // part if hrtimers are not supported.
7976 s.op->newline() << "#include <linux/version.h>";
7977 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)";
7978 s.op->newline() << "#define STP_ON_THE_FLY_TIMER_ENABLE";
7979 s.op->newline() << "#endif";
7980 }
7981
7982 // Emit embeds ahead of time, in case they affect context layout
7983 for (unsigned i=0; i<s.embeds.size(); i++)
7984 {
7985 s.op->newline() << s.embeds[i]->code << "\n";
7986 }
7987
7988 s.up->emit_common_header (); // context etc.
7989
7990 if (s.need_unwind)
7991 s.op->newline() << "#include \"stack.c\"";
7992
7993 if (s.globals.size()>0)
7994 {
7995 s.op->newline() << "struct stp_globals {";
7996 s.op->indent(1);
7997 for (unsigned i=0; i<s.globals.size(); i++)
7998 {
7999 s.up->emit_global (s.globals[i]);
8000 }
8001 s.op->newline(-1) << "};";
8002
8003 // We only need to statically initialize globals in kernel modules,
8004 // where module parameters may want to override the script's value. In
8005 // stapdyn, the globals are actually part of the dynamic shared memory,
8006 // and the static structure is merely used as a source of default values.
8007 s.op->newline();
8008 if (!s.runtime_usermode_p ())
8009 s.op->newline() << "static struct stp_globals stp_global = {";
8010 else
8011 {
8012 s.op->newline() << "static struct {";
8013 s.op->indent(1);
8014 for (unsigned i=0; i<s.globals.size(); i++)
8015 {
8016 assert_no_interrupts();
8017 s.up->emit_global_init_type (s.globals[i]);
8018 }
8019 s.op->newline(-1) << "} stp_global_init = {";
8020 }
8021 s.op->newline(1);
8022 for (unsigned i=0; i<s.globals.size(); i++)
8023 {
8024 assert_no_interrupts();
8025 s.up->emit_global_init (s.globals[i]);
8026 }
8027 s.op->newline(-1) << "};";
8028
8029 s.op->assert_0_indent();
8030 }
8031 else
8032 // stp_runtime_session wants to incorporate globals, but it
8033 // can be empty
8034 s.op->newline() << "struct stp_globals {};";
8035
8036 // Common (static atomic) state of the stap session.
8037 s.op->newline();
8038 s.op->newline() << "#include \"common_session_state.h\"";
8039
8040 s.op->newline() << "#include \"probe_lock.h\" ";
8041
8042 s.op->newline() << "#ifdef STAP_NEED_GETTIMEOFDAY";
8043 s.op->newline() << "#include \"time.c\""; // Don't we all need more?
8044 s.op->newline() << "#endif";
8045
8046 for (map<string,stapdfa*>::iterator it = s.dfas.begin(); it != s.dfas.end(); it++)
8047 {
8048 assert_no_interrupts();
8049 s.op->newline();
8050 try
8051 {
8052 it->second->emit_declaration (s.op);
8053 }
8054 catch (const semantic_error &e)
8055 {
8056 s.print_error(e);
8057 }
8058 }
8059 s.op->assert_0_indent();
8060
8061 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
8062 {
8063 assert_no_interrupts();
8064 s.op->newline();
8065 s.up->emit_functionsig (it->second);
8066 }
8067 s.op->assert_0_indent();
8068
8069
8070 // Let's find some stats for the embedded pp strings. Maybe they
8071 // are small and uniform enough to justify putting char[MAX]'s into
8072 // the array instead of relocated char*'s.
8073 size_t pp_max = 0, pn_max = 0, location_max = 0, derivation_max = 0;
8074 size_t pp_tot = 0, pn_tot = 0, location_tot = 0, derivation_tot = 0;
8075 for (unsigned i=0; i<s.probes.size(); i++)
8076 {
8077 derived_probe* p = s.probes[i];
8078 #define DOIT(var,expr) do { \
8079 size_t var##_size = (expr) + 1; \
8080 var##_max = max (var##_max, var##_size); \
8081 var##_tot += var##_size; } while (0)
8082 DOIT(pp, lex_cast_qstring(*p->sole_location()).size());
8083 DOIT(pn, lex_cast_qstring(*p->script_location()).size());
8084 DOIT(location, lex_cast_qstring(p->tok->location).size());
8085 DOIT(derivation, lex_cast_qstring(p->derived_locations()).size());
8086 #undef DOIT
8087 }
8088
8089 // Decide whether it's worthwhile to use char[] or char* by comparing
8090 // the amount of average waste (max - avg) to the relocation data size
8091 // (3 native long words).
8092 #define CALCIT(var) \
8093 if (s.verbose > 2) \
8094 clog << "adapt " << #var << ":" << var##_max << "max - " << var##_tot << "/" << s.probes.size() << "tot =>"; \
8095 if ((var##_max-(var##_tot/s.probes.size())) < (3 * sizeof(void*))) \
8096 { \
8097 s.op->newline() << "const char " << #var << "[" << var##_max << "];"; \
8098 if (s.verbose > 2) \
8099 clog << "[]" << endl; \
8100 } \
8101 else \
8102 { \
8103 s.op->newline() << "const char * const " << #var << ";"; \
8104 if (s.verbose > 2) \
8105 clog << "*" << endl; \
8106 }
8107
8108 s.op->newline();
8109 s.op->newline() << "struct stap_probe {";
8110 s.op->newline(1) << "const size_t index;";
8111 s.op->newline() << "void (* const ph) (struct context*);";
8112 s.op->newline() << "unsigned cond_enabled:1;"; // just one bit required
8113 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_ALIBI)";
8114 CALCIT(location);
8115 CALCIT(derivation);
8116 s.op->newline() << "#define STAP_PROBE_INIT_TIMING(L, D) "
8117 << ".location=(L), .derivation=(D),";
8118 s.op->newline() << "#else";
8119 s.op->newline() << "#define STAP_PROBE_INIT_TIMING(L, D)";
8120 s.op->newline() << "#endif";
8121 CALCIT(pp);
8122 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
8123 CALCIT(pn);
8124 s.op->newline() << "#define STAP_PROBE_INIT_NAME(PN) .pn=(PN),";
8125 s.op->newline() << "#else";
8126 s.op->newline() << "#define STAP_PROBE_INIT_NAME(PN)";
8127 s.op->newline() << "#endif";
8128 s.op->newline() << "#define STAP_PROBE_INIT(I, PH, PP, PN, L, D) "
8129 << "{ .index=(I), .ph=(PH), .cond_enabled=1, .pp=(PP), "
8130 << "STAP_PROBE_INIT_NAME(PN) "
8131 << "STAP_PROBE_INIT_TIMING(L, D) "
8132 << "}";
8133 s.op->newline(-1) << "} static stap_probes[];";
8134 s.op->assert_0_indent();
8135 #undef CALCIT
8136
8137 // Run a varuse_collecting_visitor over probes that need global
8138 // variable locks. We'll use this information later in
8139 // emit_locks()/emit_unlocks().
8140 for (unsigned i=0; i<s.probes.size(); i++)
8141 {
8142 assert_no_interrupts();
8143 s.probes[i]->session_index = i;
8144 if (s.probes[i]->needs_global_locks())
8145 s.probes[i]->body->visit (&cup.vcv_needs_global_locks);
8146 }
8147 s.op->assert_0_indent();
8148
8149 for (unsigned i=0; i<s.probes.size(); i++)
8150 {
8151 assert_no_interrupts();
8152 s.up->emit_probe (s.probes[i]);
8153 }
8154 s.op->assert_0_indent();
8155
8156 s.op->newline() << "static struct stap_probe stap_probes[] = {";
8157 s.op->indent(1);
8158 for (unsigned i=0; i<s.probes.size(); ++i)
8159 {
8160 derived_probe* p = s.probes[i];
8161 s.op->newline() << "STAP_PROBE_INIT(" << i << ", &" << p->name() << ", "
8162 << lex_cast_qstring (*p->sole_location()) << ", "
8163 << lex_cast_qstring (*p->script_location()) << ", "
8164 << lex_cast_qstring (p->tok->location) << ", "
8165 << lex_cast_qstring (p->derived_locations()) << "),";
8166 }
8167 s.op->newline(-1) << "};";
8168
8169 if (s.runtime_usermode_p())
8170 {
8171 s.op->newline() << "static const char* stp_probe_point(size_t index) {";
8172 s.op->newline(1) << "if (index < ARRAY_SIZE(stap_probes))";
8173 s.op->newline(1) << "return stap_probes[index].pp;";
8174 s.op->newline(-1) << "return NULL;";
8175 s.op->newline(-1) << "}";
8176 s.op->assert_0_indent();
8177 }
8178
8179 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
8180 {
8181 assert_no_interrupts();
8182 s.op->newline();
8183 s.up->emit_function (it->second);
8184 }
8185
8186 s.op->assert_0_indent();
8187 s.op->newline();
8188 s.up->emit_module_init ();
8189 s.op->assert_0_indent();
8190 s.op->newline();
8191 s.up->emit_module_refresh ();
8192 s.op->assert_0_indent();
8193 s.op->newline();
8194 s.up->emit_module_exit ();
8195 s.op->assert_0_indent();
8196 s.up->emit_kernel_module_init ();
8197 s.op->assert_0_indent();
8198 s.up->emit_kernel_module_exit ();
8199 s.op->assert_0_indent();
8200 s.op->newline();
8201
8202 emit_symbol_data (s);
8203
8204 s.op->newline() << "MODULE_DESCRIPTION(\"systemtap-generated probe\");";
8205 s.op->newline() << "MODULE_LICENSE(\"GPL\");";
8206
8207 for (unsigned i = 0; i < s.modinfos.size(); i++)
8208 {
8209 const string& mi = s.modinfos[i];
8210 size_t loc = mi.find('=');
8211 string tag = mi.substr (0, loc);
8212 string value = mi.substr (loc+1);
8213 s.op->newline() << "MODULE_INFO(" << tag << "," << lex_cast_qstring(value) << ");";
8214 }
8215
8216 s.op->assert_0_indent();
8217
8218 if (s.runtime_usermode_p())
8219 s.up->emit_global_init_setters();
8220 else
8221 // PR10298: attempt to avoid collisions with symbols
8222 for (unsigned i=0; i<s.globals.size(); i++)
8223 {
8224 s.op->newline();
8225 s.up->emit_global_param (s.globals[i]);
8226 }
8227 s.op->assert_0_indent();
8228 }
8229 catch (const semantic_error& e)
8230 {
8231 s.print_error (e);
8232 }
8233
8234 s.op->line() << "\n";
8235
8236 delete s.op;
8237 s.op = 0;
8238 s.up = 0;
8239
8240 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
8241 s.auxiliary_outputs[i]->close();
8242
8243 return rc + s.num_errors();
8244 }
8245
8246 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.440488 seconds and 5 git commands to generate.