2 // Copyright (C) 2005-2019 Red Hat Inc.
3 // Copyright (C) 2005-2008 Intel Corporation.
4 // Copyright (C) 2010 Novell Corporation.
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
13 #include "elaborate.h"
14 #include "translate.h"
18 #include "dwarf_wrappers.h"
19 #include "setupdwfl.h"
20 #include "task_finder.h"
21 #include "runtime/k_syms.h"
23 #include "stapregex.h"
24 #include "stringtable.h"
38 #include <elfutils/libdwfl.h>
39 #include <elfutils/libdw.h>
41 #define __STDC_FORMAT_MACROS
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)
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\";")
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
);
74 struct c_unparser
: public unparser
, public visitor
76 systemtap_session
* session
;
79 derived_probe
* current_probe
;
80 functiondecl
* current_function
;
82 const functioncall
* assigned_functioncall
;
83 const string
* assigned_functioncall_retval
;
85 unsigned tmpvar_counter
;
86 unsigned label_counter
;
87 unsigned action_counter
;
89 bool already_checked_action_count
;
91 varuse_collecting_visitor vcv_needs_global_locks
;
93 map
<string
, probe
*> probe_contents
;
95 map
<pair
<bool, string
>, string
> compiled_printfs
;
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
) {}
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&) {}
108 // If we've seen a dupe, return it; else remember this and return NULL.
109 probe
*get_probe_dupe (derived_probe
*dp
);
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
);
127 void emit_probe (derived_probe
* v
);
128 void emit_probe_condition_update(derived_probe
* v
);
129 void emit_unlocks ();
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
);
137 // for use by stats (pmap) foreach
138 set
<string
> aggregations_active
;
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
);
145 // for use by looping constructs
146 vector
<string
> loop_break_labels
;
147 vector
<string
> loop_continue_labels
;
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
);
154 string
c_arg_define (const string
& e
);
155 string
c_arg_undef (const string
& e
);
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
);
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
);
169 void c_declare(exp_type ty
, const string
&ident
);
170 void c_declare_static(exp_type ty
, const string
&ident
);
172 void c_strcat (const string
& lvalue
, const string
& rvalue
);
173 void c_strcat (const string
& lvalue
, expression
* rvalue
);
175 void c_strcpy (const string
& lvalue
, const string
& rvalue
);
176 void c_strcpy (const string
& lvalue
, expression
* rvalue
);
178 bool is_local (vardecl
const* r
, token
const* tok
);
180 tmpvar
gensym(exp_type ty
);
181 aggvar
gensym_aggregate();
183 var
getvar(vardecl
* v
, token
const* tok
= NULL
);
184 itervar
getiter(symbol
* s
);
185 mapvar
getmap(vardecl
* v
, token
const* tok
= NULL
);
187 void load_map_indices(arrayindex
* e
,
188 vector
<tmpvar
> & idx
);
190 var
* load_aggregate (expression
*e
, aggvar
& agg
);
191 string
histogram_index_check(var
& vase
, tmpvar
& idx
) const;
193 void collect_map_index_types(vector
<vardecl
* > const & vars
,
194 set
< pair
<vector
<exp_type
>, exp_type
> > & types
);
196 void record_actions (unsigned actions
, const token
* tok
, bool update
=false);
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
);
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
*) { }
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); }
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
259 set
<string
> declared_vars
;
261 c_tmpcounter (c_unparser
* p
):
262 c_unparser(p
->session
, &null_o
), parent (p
)
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
;
269 void emit_function (functiondecl
* fd
);
270 void emit_probe (derived_probe
* dp
);
272 const string
& get_compiled_printf (bool print_to_stream
,
273 const string
& format
) cxx_override
;
275 void start_compound_statement (const char*, statement
*) cxx_override
;
276 void close_compound_statement (const char*, statement
*) cxx_override
;
278 void wrap_compound_visit (expression
*e
) cxx_override
;
279 void wrap_compound_visit (statement
*s
) cxx_override
;
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
);
287 struct c_unparser_assignment
:
288 public throwing_visitor
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
) {}
301 void prepare_rvalue (interned_string op
,
305 void c_assignop(tmpvar
& res
,
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
);
318 ostream
& operator<<(ostream
& o
, var
const & v
);
322 Some clarification on the runtime structures involved in statistics:
324 The basic type for collecting statistics in the runtime is struct
325 stat_data. This contains the count, min, max, sum, and possibly
328 There are two places struct stat_data shows up.
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.
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
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.
357 // Required for accurate mangling:
367 mutable bool declaration_needed
;
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)
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)
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)
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)
395 bool is_local() const
400 statistic_decl
const & sdecl() const
405 void assert_hist_compatible(hist_op
const & hop
)
407 // Semantic checks in elaborate should have caught this if it was
408 // false. This is just a double-check.
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
);
418 case statistic_decl::logarithmic
:
419 assert(hop
.htype
== hist_log
);
420 assert(hop
.params
.size() == 0);
422 case statistic_decl::none
:
427 exp_type
type() const
432 string
c_name() const
437 return u
->c_localname(name
);
439 return u
->c_globalname(name
);
442 string
stat_op_tokens() const
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
) + ", ";
463 if (declaration_needed
)
465 u
->var_declare (name
, *this);
466 declaration_needed
= false;
470 return "l->" + c_name();
472 return "global(" + c_name() + ")";
475 virtual string
hist() const
477 assert (ty
== pe_stats
);
478 assert (sd
.type
!= statistic_decl::none
);
479 return "(&(" + value() + "->hist))";
482 virtual string
buckets() const
484 assert (ty
== pe_stats
);
485 assert (sd
.type
!= statistic_decl::none
);
486 return "(" + value() + "->hist.buckets)";
495 return ""; // module_param
497 return value() + "[0] = '\\0';";
500 return ""; // module_param
502 return value() + " = 0;";
505 // See also mapvar::init().
508 throw SEMANTIC_ERROR(_F("unsupported local stats init for %s", value().c_str()));
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;";
516 case statistic_decl::none
:
517 prefix
+= string("KEY_HIST_TYPE, HIST_NONE, ");
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
) + ", ";
527 case statistic_decl::logarithmic
:
528 prefix
+= string("KEY_HIST_TYPE, HIST_LOG, ");
532 throw SEMANTIC_ERROR(_F("unsupported stats type for %s", value().c_str()));
536 prefix
= prefix
+ ")); ";
537 return string (prefix
+ suffix
);
541 throw SEMANTIC_ERROR(_F("unsupported initializer for %s", value().c_str()));
551 return ""; // no action required
553 return "_stp_stat_del (" + value () + ");";
555 throw SEMANTIC_ERROR(_F("unsupported deallocator for %s", value().c_str()));
559 virtual void declare(c_unparser
&c
) const
561 c
.c_declare(ty
, c_name());
565 ostream
& operator<<(ostream
& o
, var
const & v
)
567 return o
<< v
.value();
571 c_tmpcounter::var_declare (string
const& name
, var
const& v
)
573 if (declared_vars
.insert(name
).second
)
580 stmt_expr(c_unparser
& c
) : c(c
)
582 c
.o
->newline() << "({";
587 c
.o
->newline(-1) << "})";
597 string override_value
;
600 tmpvar(c_unparser
*u
, exp_type ty
, unsigned & counter
)
601 : var(u
, true, ty
, counter
),
605 tmpvar(const var
& source
)
606 : var(source
), overridden(false)
609 void override(const string
&value
)
612 override_value
= value
;
623 return override_value
;
629 ostream
& operator<<(ostream
& o
, tmpvar
const & v
)
631 return o
<< v
.value();
637 aggvar(c_unparser
*u
, unsigned & counter
)
638 : var(u
, true, pe_stats
, counter
)
643 assert (type() == pe_stats
);
644 return value() + " = NULL;";
647 void declare(c_unparser
&c
) const cxx_override
649 assert (type() == pe_stats
);
650 c
.o
->newline() << "struct stat_data *" << name
<< ";";
653 string
get_hist (var
& index
) const
655 return "(" + value() + "->histogram[" + index
.value() + "])";
662 vector
<exp_type
> index_types
;
665 mapvar (c_unparser
*u
,
666 bool local
, exp_type ty
,
667 statistic_decl
const & sd
,
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
)
676 static string
shortname(exp_type e
);
677 static string
key_typename(exp_type e
);
678 static string
value_typename(exp_type e
);
680 string
keysym () const
683 vector
<exp_type
> tmp
= index_types
;
684 tmp
.push_back (type ());
685 for (unsigned i
= 0; i
< tmp
.size(); ++i
)
699 throw SEMANTIC_ERROR(_("unknown type of map"));
706 string
function_keysym(string
const & fname
, bool pre_agg
=false) const
708 string mtype
= (is_parallel() && !pre_agg
) ? "pmap" : "map";
709 string result
= "_stp_" + mtype
+ "_" + fname
+ "_" + keysym();
713 string
call_prefix (string
const & fname
, vector
<tmpvar
> const & indices
, bool pre_agg
=false) const
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
)
719 if (indices
[i
].type() != index_types
[i
])
720 throw SEMANTIC_ERROR(_("index type mismatch"));
722 result
+= indices
[i
].value();
728 bool is_parallel() const
730 return type() == pe_stats
;
733 string
stat_op_tokens() const
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
) + ", ";
752 string
stat_op_parms() const
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";
763 string
calculate_aggregate() const
766 throw SEMANTIC_ERROR(_("aggregating non-parallel map type"));
768 return function_keysym("agg") + " (" + value() + ")";
771 string
fetch_existing_aggregate() const
774 throw SEMANTIC_ERROR(_("fetching aggregate of non-parallel map type"));
776 return "_stp_pmap_get_agg(" + value() + ")";
779 string
del (vector
<tmpvar
> const & indices
) const
781 return (call_prefix("del", indices
) + ")");
784 string
exists (vector
<tmpvar
> const & indices
) const
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)");
792 throw SEMANTIC_ERROR(_("checking existence of an unsupported map type"));
795 string
get (vector
<tmpvar
> const & indices
, bool pre_agg
=false) const
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
) + ")";
805 throw SEMANTIC_ERROR(_("getting a value from an unsupported map type"));
808 string
add (vector
<tmpvar
> const & indices
, tmpvar
const & val
) const
810 string res
= "{ int rc = ";
812 // impedance matching: empty strings -> NULL
813 if (type() == pe_stats
)
814 res
+= (call_prefix("add", indices
) + ", " + val
.value() + ", " + stat_op_parms() + ")");
816 throw SEMANTIC_ERROR(_("adding a value of an unsupported map type"));
818 res
+= "; if (unlikely(rc)) { c->last_error = ";
820 lex_cast(maxsize
> 0 ?
821 "size limit (" + lex_cast(maxsize
) + ")" : "MAXMAPENTRIES")
822 + "\"; goto out; }}";
827 string
set (vector
<tmpvar
> const & indices
, tmpvar
const & val
) const
829 string res
= "{ int rc = ";
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() + ")");
838 throw SEMANTIC_ERROR(_("setting a value of an unsupported map type"));
840 res
+= "; if (unlikely(rc)) { c->last_error = ";
842 lex_cast(maxsize
> 0 ?
843 "size limit (" + lex_cast(maxsize
) + ")" : "MAXMAPENTRIES")
844 + "\"; goto out; }}";
851 assert (ty
== pe_stats
);
852 assert (sd
.type
!= statistic_decl::none
);
853 return "(&(" + fetch_existing_aggregate() + "->hist))";
856 string
buckets() const
858 assert (ty
== pe_stats
);
859 assert (sd
.type
!= statistic_decl::none
);
860 return "(" + fetch_existing_aggregate() + "->hist.buckets)";
866 throw SEMANTIC_ERROR(_F("unsupported local map init for %s", value().c_str()));
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, " : "");
874 // See also var::init().
876 // Check for errors during allocation.
877 string suffix
= "if (" + value () + " == NULL) rc = -ENOMEM;";
879 if (type() == pe_stats
)
881 switch (sdecl().type
)
883 case statistic_decl::none
:
884 prefix
= prefix
+ "KEY_HIST_TYPE, HIST_NONE, ";
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
) + ", ";
895 case statistic_decl::logarithmic
:
896 prefix
= prefix
+ "KEY_HIST_TYPE, HIST_LOG, ";
903 prefix
= prefix
+ ")); ";
904 return (prefix
+ suffix
);
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
915 return "_stp_pmap_del (" + value() + ");";
917 return "_stp_map_del (" + value() + ");";
927 itervar (c_unparser
*u
, symbol
* e
, unsigned & counter
)
928 : var(u
, true, e
->referent
->type
, counter
)
930 if (type() == pe_unknown
)
931 throw SEMANTIC_ERROR(_("iterating over unknown reference type"), e
->tok
);
934 void declare(c_unparser
&c
) const cxx_override
936 c
.o
->newline() << "struct map_node *" << name
<< ";";
939 string
start (mapvar
const & mv
) const
943 if (mv
.type() != type())
944 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::start()"));
946 if (mv
.is_parallel())
947 return "_stp_map_start (" + mv
.fetch_existing_aggregate() + ")";
949 return "_stp_map_start (" + mv
.value() + ")";
952 string
next (mapvar
const & mv
) const
954 if (mv
.type() != type())
955 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::next()"));
957 if (mv
.is_parallel())
958 return "_stp_map_iter (" + mv
.fetch_existing_aggregate() + ", " + value() + ")";
960 return "_stp_map_iter (" + mv
.value() + ", " + value() + ")";
963 // Cannot handle deleting and iterating on pmaps
964 string
del_next (mapvar
const & mv
) const
966 if (mv
.type() != type())
967 throw SEMANTIC_ERROR(_("inconsistent iterator type in itervar::next()"));
969 if (mv
.is_parallel())
970 throw SEMANTIC_ERROR(_("deleting a value of an unsupported map type"));
972 return "_stp_map_iterdel (" + mv
.value() + ", " + value() + ")";
975 string
get_key (mapvar
const& mv
, exp_type ty
, unsigned i
) const
977 // bug translator/1175: runtime uses base index 1 for the first dimension
978 // see also mapval::get
982 return mv
.function_keysym("key_get_int64", true)
983 + " (" + value() + ", " + lex_cast(i
+1) + ")";
985 // impedance matching: NULL -> empty strings
986 return "(" + mv
.function_keysym("key_get_str", true)
987 + " (" + value() + ", " + lex_cast(i
+1) + ") ?: \"\")";
989 throw SEMANTIC_ERROR(_("illegal key type"));
993 string
get_value (mapvar
const& mv
, exp_type ty
) const
996 throw SEMANTIC_ERROR(_("inconsistent iterator value in itervar::get_value()"));
1001 return mv
.function_keysym("get_int64", true) + " ("+ value() + ")";
1003 // impedance matching: NULL -> empty strings
1004 return "(" + mv
.function_keysym("get_str", true) + " ("+ value() + ") ?: \"\")";
1006 return mv
.function_keysym("get_stat_data", true) + " ("+ value() + ")";
1008 throw SEMANTIC_ERROR(_("illegal value type"));
1013 ostream
& operator<<(ostream
& o
, itervar
const & v
)
1015 return o
<< v
.value();
1018 // ------------------------------------------------------------------------
1020 // translator_output moved to translator-output.cxx
1022 // ------------------------------------------------------------------------
1024 struct unmodified_fnargs_checker
: public nop_visitor
1027 bool has_unmodified_fnargs
;
1029 unmodified_fnargs_checker ():
1030 is_embedded(false), has_unmodified_fnargs(false)
1033 void visit_embeddedcode (embeddedcode
*e
)
1036 if (e
->tagged_p("/* unmodified-fnargs */"))
1037 has_unmodified_fnargs
= true;
1042 is_unmodified_string_fnarg (systemtap_session
* sess
, functiondecl
* fd
, vardecl
* v
)
1044 if (sess
->unoptimized
|| v
->type
!= pe_string
)
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
;
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());
1059 // If we've seen a dupe, return it; else remember this and return NULL.
1061 c_unparser::get_probe_dupe (derived_probe
*dp
)
1063 if (session
->unoptimized
)
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>";
1071 // which would make comparisons impossible.
1075 dp
->print_dupe_stamp (oss
);
1076 dp
->body
->print(oss
);
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
;
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.
1088 pair
<map
<string
, probe
*>::iterator
, bool> const& inserted
=
1089 probe_contents
.insert(make_pair(oss
.str(), dp
));
1091 if (inserted
.second
)
1092 return NULL
; // it's new!
1094 // Already seen it; here's the old one:
1095 return inserted
.first
->second
;
1099 c_unparser::emit_common_header ()
1101 c_tmpcounter
ct (this);
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 {";
1110 // Common state held shared by probes.
1111 o
->newline(1) << "#include \"common_probe_context.h\"";
1113 // PR10516: probe locals
1114 o
->newline() << "union {";
1117 for (unsigned i
=0; i
<session
->probes
.size(); i
++)
1118 ct
.emit_probe (session
->probes
[i
]);
1120 o
->newline(-1) << "} probe_locals;";
1122 // PR10516: function locals
1123 o
->newline() << "union {";
1126 for (map
<string
,functiondecl
*>::iterator it
= session
->functions
.begin();
1127 it
!= session
->functions
.end(); it
++)
1128 ct
.emit_function (it
->second
);
1130 o
->newline(-1) << "} locals [MAXNESTING+1];";
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.
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).
1140 // Try to catch a crazy user dude passing in -DMAXNESTING=-1, leading to a [0]-sized
1142 o
->newline() << "#if MAXNESTING < 0";
1143 o
->newline() << "#error \"MAXNESTING must be positive\"";
1144 o
->newline() << "#endif";
1146 // Use a separate union for compiled-printf locals, no nesting required.
1147 emit_compiled_printf_locals ();
1149 o
->newline(-1) << "};\n"; // end of struct context
1151 o
->newline() << "#include \"runtime_context.h\"";
1153 emit_map_type_instantiations ();
1155 emit_compiled_printfs();
1157 if (!session
->runtime_usermode_p())
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);";
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) << "}";
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;";
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";
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 */";
1196 o
->newline(0) << "#include \"namespaces.h\"";
1203 c_unparser::declare_compiled_printf (bool print_to_stream
, const string
& format
)
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);
1213 c_unparser::get_compiled_printf (bool print_to_stream
, const string
& format
)
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"));
1223 c_tmpcounter::get_compiled_printf (bool print_to_stream
, const string
& format
)
1225 parent
->declare_compiled_printf (print_to_stream
, format
);
1226 return parent
->get_compiled_printf (print_to_stream
, format
);
1230 c_unparser::emit_compiled_printf_locals ()
1232 o
->newline() << "#ifndef STP_LEGACY_PRINT";
1233 o
->newline() << "union {";
1235 map
<pair
<bool, string
>, string
>::iterator it
;
1236 for (it
= compiled_printfs
.begin(); it
!= compiled_printfs
.end(); ++it
)
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
);
1244 o
->newline() << "struct " << name
<< "_locals {";
1248 vector
<print_format::format_component
>::const_iterator c
;
1249 for (c
= components
.begin(); c
!= components
.end(); ++c
)
1251 if (c
->type
== print_format::conv_literal
)
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
++ << ";";
1260 // Output the actual argument.
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
++ << ";";
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
++ << ";";
1280 assert(false); // XXX
1286 if (!print_to_stream
)
1287 o
->newline() << "char * __retvalue;";
1289 o
->newline(-1) << "} " << name
<< ";";
1291 o
->newline(-1) << "} printf_locals;";
1292 o
->newline() << "#endif // STP_LEGACY_PRINT";
1296 c_unparser::emit_compiled_printfs ()
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
)
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
);
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;";
1323 o
->newline() << "(void) width;";
1324 o
->newline() << "(void) precision;";
1325 o
->newline() << "(void) ptr_value;";
1326 o
->newline() << "(void) num_bytes;";
1328 if (print_to_stream
)
1330 // Compute the buffer size needed for these arguments.
1332 o
->newline() << "num_bytes = 0;";
1333 vector
<print_format::format_component
>::const_iterator c
;
1334 for (c
= components
.begin(); c
!= components
.end(); ++c
)
1336 if (c
->type
== print_format::conv_literal
)
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'
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);";
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);";
1365 string value
= "l->arg" + lex_cast(arg_ix
++);
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)
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
<< ");";
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
<< ");";
1389 case print_format::conv_char
:
1390 o
->newline() << "num_bytes += _stp_vsprint_char_size("
1391 << value
<< ", width, " << c
->flags
<< ");";
1394 case print_format::conv_string
:
1395 o
->newline() << "num_bytes += _stp_vsprint_memory_size("
1396 << value
<< ", width, precision, 's', "
1397 << c
->flags
<< ");";
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
<< ");";
1409 case print_format::conv_binary
:
1410 o
->newline() << "num_bytes += _stp_vsprint_binary_size("
1411 << value
<< ", width, precision);";
1415 assert(false); // XXX
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;";
1424 else // !print_to_stream
1426 // String results are a known buffer and size;
1427 o
->newline() << "str = l->__retvalue;";
1428 o
->newline() << "end = str + MAXSTRINGLEN - 1;";
1431 o
->newline() << "if (str && str <= end) {";
1434 // Generate code to print the actual arguments.
1436 vector
<print_format::format_component
>::const_iterator c
;
1437 for (c
= components
.begin(); c
!= components
.end(); ++c
)
1439 if (c
->type
== print_format::conv_literal
)
1441 literal_string
ls(c
->literal_string
);
1442 o
->newline() << "src = ";
1443 visit_literal_string(&ls
);
1445 o
->newline() << "while (*src && str <= end)";
1446 o
->newline(1) << "*str++ = *src++;";
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);";
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);";
1471 string value
= "l->arg" + lex_cast(arg_ix
++);
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)
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
<< ");";
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
<< ");";
1495 case print_format::conv_char
:
1496 o
->newline() << "str = _stp_vsprint_char(str, end, "
1497 << value
<< ", width, " << c
->flags
<< ");";
1500 case print_format::conv_string
:
1501 o
->newline() << "str = _stp_vsprint_memory(str, end, "
1502 << value
<< ", width, precision, 's', "
1503 << c
->flags
<< ");";
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)) {";
1515 if (print_to_stream
)
1516 o
->newline() << "_stp_unreserve_bytes(num_bytes);";
1517 o
->newline() << "return;";
1518 o
->newline(-1) << "}";
1521 case print_format::conv_binary
:
1522 o
->newline() << "str = _stp_vsprint_binary(str, end, "
1523 << value
<< ", width, precision, "
1524 << c
->flags
<< ");";
1528 assert(false); // XXX
1533 if (!print_to_stream
)
1535 o
->newline() << "if (str <= end)";
1536 o
->newline(1) << "*str = '\\0';";
1537 o
->newline(-1) << "else";
1538 o
->newline(1) << "*end = '\\0';";
1542 o
->newline(-1) << "}";
1544 o
->newline(-1) << "}";
1546 o
->newline() << "#endif // STP_LEGACY_PRINT";
1551 c_unparser::emit_global_param (vardecl
*v
)
1553 // Only true globals can be params, not private variables.
1554 if (!v
->name
.starts_with("__global_")) return;
1556 // Only non-synthetic globals can be params.
1557 if (v
->synthetic
) return;
1559 string global
= c_globalname (v
->name
);
1560 interned_string param
= v
->name
.substr(sizeof("__global_") - 1);
1562 // For dyninst, use the emit_global_init_* functionality instead.
1563 assert (!session
->runtime_usermode_p());
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
1572 // Emit module_params for this global, if its type is convenient.
1573 if (v
->arity
== 0 && v
->type
== pe_long
)
1575 o
->newline() << "module_param_named (" << param
<< ", "
1576 << "global(" << global
<< "), int64_t, 0);";
1578 else if (v
->arity
== 0 && v
->type
== pe_string
)
1580 // NB: no special copying is needed.
1581 o
->newline() << "module_param_string (" << param
<< ", "
1582 << "global(" << global
<< "), MAXSTRINGLEN, 0);";
1588 c_unparser::emit_global_init_setters ()
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) {";
1594 for (unsigned i
=0; i
<session
->globals
.size(); i
++)
1596 vardecl
* v
= session
->globals
[i
];
1597 if (v
->arity
> 0) continue;
1598 if (v
->type
!= pe_string
&& v
->type
!= pe_long
) continue;
1600 // Only true globals can be params, not private variables.
1601 if (!v
->name
.starts_with("__global_")) continue;
1603 string global
= c_globalname (v
->name
);
1604 interned_string param
= v
->name
.substr(sizeof("__global_") - 1);
1606 // Do not mangle v->name for the comparison!
1607 o
->line() << "if (0 == strcmp(name,\"" << param
<< "\"))" << " {";
1610 if (v
->type
== pe_string
)
1612 c_assign("stp_global_init." + global
, "value", pe_string
, "BUG: global module param", v
->tok
);
1613 o
->newline() << "return 0;";
1617 o
->newline() << "return set_int64_t(value, &stp_global_init." << global
<< ");";
1620 o
->newline(-1) << "} else ";
1623 // Call the runtime function that handles session attributes, like
1625 o
->line() << "return stp_session_attribute_setter(name, value);";
1626 o
->newline(-1) << "}";
1632 c_unparser::emit_global (vardecl
*v
)
1634 string vn
= c_globalname (v
->name
);
1638 type
= (v
->type
== pe_stats
) ? "PMAP" : "MAP";
1640 type
= c_typename (v
->type
);
1642 if (session
->runtime_usermode_p())
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
;
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*";
1655 o
->newline() << "union {";
1656 o
->newline(1) << casted_type
<< " " << vn
<< "_typed;";
1657 o
->newline() << stored_type
<< " " << vn
<< ";";
1658 o
->newline(-1) << "};";
1661 o
->newline() << type
<< " " << vn
<< ";";
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";
1672 c_unparser::emit_global_init (vardecl
*v
)
1674 // We can only statically initialize some scalars.
1675 if (v
->arity
== 0 && v
->init
)
1677 o
->newline() << "." << c_globalname (v
->name
) << " = ";
1678 v
->init
->visit(this);
1681 else if (v
->arity
== 0 && session
->runtime_usermode_p())
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: ""
1690 // The lock and lock_skip_count are handled in emit_module_init.
1695 c_unparser::emit_global_init_type (vardecl
*v
)
1697 // We can only statically initialize some scalars.
1698 if (v
->arity
== 0) // ... although we still allow !v->init here.
1700 o
->newline() << c_typename(v
->type
) << " " << c_globalname(v
->name
) << ";";
1706 c_unparser::emit_functionsig (functiondecl
* v
)
1708 o
->newline() << "static void " << c_funcname(v
->name
)
1709 << " (struct context * __restrict__ c);";
1714 c_unparser::emit_kernel_module_init ()
1716 if (session
->runtime_usermode_p())
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
1724 vector
<derived_probe_group
*> g
= all_session_groups (*session
);
1725 for (unsigned i
=0; i
<g
.size(); i
++)
1727 g
[i
]->emit_kernel_module_init (*session
);
1729 o
->newline() << "if (rc) {";
1733 for (int j
=i
-1; j
>=0; j
--)
1734 g
[j
]->emit_kernel_module_exit (*session
);
1736 o
->newline() << "goto out;";
1737 o
->newline(-1) << "}";
1739 o
->newline(-1) << "out:";
1741 o
->newline() << "return rc;";
1742 o
->newline(-1) << "}\n";
1743 o
->assert_0_indent();
1748 c_unparser::emit_kernel_module_exit ()
1750 if (session
->runtime_usermode_p())
1754 o
->newline() << "static void systemtap_kernel_module_exit (void) {";
1755 o
->newline(1) << "int i=0, j=0;"; // for derived_probe_group use
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();
1764 (*i
)->emit_kernel_module_exit (*session
);
1766 o
->newline(-1) << "}\n";
1767 o
->assert_0_indent();
1772 c_unparser::emit_module_init ()
1774 vector
<derived_probe_group
*> g
= all_session_groups (*session
);
1775 for (unsigned i
=0; i
<g
.size(); i
++)
1777 g
[i
]->emit_module_decls (*session
);
1778 o
->assert_0_indent();
1781 o
->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
1782 o
->newline() << "#include \"linux/stp_tracepoint.c\"";
1783 o
->newline() << "#endif";
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 = \"\";";
1792 // NB: This block of initialization only makes sense in kernel
1793 if (! session
->runtime_usermode_p())
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.
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";
1809 // The systemtap_module_init() function must be run in
1810 // non-atomic context, since several functions might need to
1812 o
->newline() << "might_sleep();";
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.
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.
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)\", "
1828 << lex_cast_qstring (session
->kernel_release
)
1830 o
->newline() << "rc = -EINVAL;";
1831 o
->newline(-1) << "}";
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\", "
1837 << "utsname()->version, "
1840 o
->newline() << "rc = -EINVAL;";
1841 o
->newline(-1) << "}";
1842 o
->newline() << "#endif";
1843 o
->newline() << "#endif";
1845 // perform buildid-based checking if able
1846 o
->newline() << "if (_stp_module_check()) rc = -EINVAL;";
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) << "}";
1876 o
->newline(-1) << "}";
1878 o
->newline() << "if (rc) goto out;";
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) << "}";
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!
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) << "}";
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";
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";
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";
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;";
1936 // Allocate context structures.
1937 o
->newline() << "rc = _stp_runtime_contexts_alloc();";
1938 o
->newline() << "if (rc != 0)";
1939 o
->newline(1) << "goto out;";
1942 for (unsigned i
=0; i
<session
->globals
.size(); i
++)
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
);
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.
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) << "}";
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";
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()
1977 // In dyninst mode, we need to know when all the globals have been
1978 // allocated and we're ready to run probe registration.
1981 o
->newline() << "rc = stp_session_init_finished();";
1982 o
->newline() << "if (rc) goto out;";
1985 if (!session
->runtime_usermode_p())
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";
1995 // Run all probe registrations. This actually runs begin probes.
1997 for (unsigned i
=0; i
<g
.size(); i
++)
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);";
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);";
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) << "}";
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);";
2022 // Run all post-session starting code.
2023 for (unsigned i
=0; i
<g
.size(); i
++)
2025 g
[i
]->emit_module_post_init (*session
);
2028 if (!session
->runtime_usermode_p())
2030 o
->newline() << "#ifdef STP_ON_THE_FLY_TIMER_ENABLE";
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;";
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.
2042 // for each derived probe...
2043 bool start_timer
= false;
2044 for (unsigned i
=0; i
<session
->probes
.size() && !start_timer
; i
++)
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())
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
)
2059 if ((*it
)->group
&& (*it
)->group
->otf_supported(*session
))
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);";
2073 o
->newline() << "#endif /* STP_ON_THE_FLY_TIMER_ENABLE */";
2076 o
->newline() << "return 0;";
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:";
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
++)
2088 vardecl
* v
= session
->globals
[i
];
2089 if (v
->index_types
.size() > 0)
2090 o
->newline() << getmap (v
).fini();
2092 o
->newline() << getvar (v
).fini();
2095 // For any partially registered/unregistered kernel facilities.
2096 o
->newline() << "atomic_set (session_state(), STAP_SESSION_STOPPED);";
2097 o
->newline() << "stp_synchronize_sched();";
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";
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";
2109 // Free up the context memory after an error too
2110 o
->newline() << "_stp_runtime_contexts_free();";
2112 o
->newline() << "return rc;";
2113 o
->newline(-1) << "}\n";
2118 c_unparser::emit_module_refresh ()
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
2124 if (!session
->runtime_usermode_p())
2126 o
->newline() << "#if defined(STP_TIMING)";
2127 o
->newline() << "cycles_t cycles_atstart = get_cycles();";
2128 o
->newline() << "#endif";
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);";
2137 /* If we're not in STARTING/RUNNING state, don't try doing any work.
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);";
2146 o
->newline() << "#endif";
2148 if (!session
->runtime_usermode_p())
2149 o
->newline() << "mutex_unlock(&module_refresh_mutex);";
2151 o
->newline() << "return;";
2152 o
->newline(-1) << "}";
2154 o
->newline() << "(void) i;";
2155 o
->newline() << "(void) j;";
2157 vector
<derived_probe_group
*> g
= all_session_groups (*session
);
2158 for (unsigned i
=0; i
<g
.size(); i
++)
2160 g
[i
]->emit_module_refresh (*session
);
2163 if (!session
->runtime_usermode_p())
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;";
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";
2179 if (!session
->runtime_usermode_p())
2180 o
->newline() << "mutex_unlock(&module_refresh_mutex);";
2182 o
->newline(-1) << "}\n";
2187 c_unparser::emit_module_exit ()
2189 o
->newline() << "static void systemtap_module_exit (void) {";
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;";
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);";
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!
2209 if (!session
->runtime_usermode_p())
2211 o
->newline() << "#ifdef STP_ON_THE_FLY_TIMER_ENABLE";
2212 o
->newline() << "hrtimer_cancel(&module_refresh_timer);";
2213 o
->newline() << "#endif";
2216 // cargo cult prologue ... hope to flush any pending workqueue items too
2217 o
->newline() << "stp_synchronize_sched();";
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);";
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();
2231 (*i
)->emit_module_exit (*session
); // NB: runs "end" probes
2233 if (!session
->runtime_usermode_p())
2234 o
->newline() << "mutex_unlock(&module_refresh_mutex);";
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.
2239 // cargo cult prologue
2240 o
->newline() << "stp_synchronize_sched();";
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();";
2247 // cargo cult epilogue
2248 o
->newline() << "atomic_set (session_state(), STAP_SESSION_STOPPED);";
2249 o
->newline() << "stp_synchronize_sched();";
2251 // XXX: might like to have an escape hatch, in case some probe is
2252 // genuinely stuck somehow
2254 for (unsigned i
=0; i
<session
->globals
.size(); i
++)
2256 vardecl
* v
= session
->globals
[i
];
2257 if (v
->index_types
.size() > 0)
2258 o
->newline() << getmap (v
).fini();
2260 o
->newline() << getvar (v
).fini();
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())
2268 o
->newline() << "_stp_runtime_contexts_free();";
2272 o
->newline() << "struct context* __restrict__ c;";
2273 o
->newline() << "c = _stp_runtime_entryfn_get_context();";
2276 // teardown tracepoints (if needed)
2277 o
->newline() << "#ifdef STAP_NEED_TRACEPOINTS";
2278 o
->newline() << " stp_tracepoint_exit();";
2279 o
->newline() << "#endif";
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";
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();";
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) << "}";
2320 if (!session
->runtime_usermode_p())
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
2338 o
->newline() << "_stp_print_flush();";
2339 o
->newline() << "#endif";
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
++)
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);";
2353 o
->newline(-1) << "}";
2354 o
->newline() << "_stp_print_flush();";
2355 o
->newline () << "#endif";
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
++)
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);";
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) << "}";
2389 // NB: PR13386 needs to restore preemption-blocking counts
2390 o
->newline() << "preempt_enable_no_resched();";
2392 // In dyninst mode, now we're done with the contexts, transport, everything!
2393 if (session
->runtime_usermode_p())
2395 o
->newline() << "_stp_runtime_entryfn_put_context(c);";
2396 o
->newline() << "_stp_dyninst_transport_shutdown();";
2397 o
->newline() << "_stp_runtime_contexts_free();";
2400 o
->newline(-1) << "}\n";
2403 struct max_action_info
: public functioncall_traversing_visitor
2405 max_action_info(systemtap_session
& s
): sess(s
), statement_count(0) {}
2407 systemtap_session
& sess
;
2408 unsigned statement_count
;
2409 static const unsigned max_statement_count
= ~0;
2411 void add_stmt_count (unsigned val
)
2413 statement_count
= (statement_count
> max_statement_count
- val
) ? max_statement_count
: statement_count
+ val
;
2415 void add_max_stmt_count () { statement_count
= max_statement_count
; }
2416 bool statement_count_finite() { return statement_count
< max_statement_count
; }
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
)
2423 traversing_visitor::visit_expr_statement(stmt
); // which will trigger visit_functioncall, if applicable
2425 void visit_if_statement (if_statement
*stmt
)
2428 stmt
->condition
->visit(this);
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
)
2437 stmt
->elseblock
->visit(& tmp_visitor_else
);
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
);
2445 void note_recursive_functioncall (functioncall
*) { add_max_stmt_count(); }
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); }
2456 c_tmpcounter::emit_function (functiondecl
* fd
)
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();
2465 translator_output
*o
= parent
->o
;
2467 // indent the dummy output as if we were already in a block
2468 this->o
->indent (1);
2470 o
->newline() << "struct " << c_funcname (fd
->name
) << "_locals {";
2473 for (unsigned j
=0; j
<fd
->locals
.size(); j
++)
2475 vardecl
* v
= fd
->locals
[j
];
2478 if (fd
->mangle_oldstyle
)
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) << "; };";
2489 o
->newline() << c_typename (v
->type
) << " "
2490 << c_localname (v
->name
) << ";";
2492 } catch (const semantic_error
& e
) {
2493 semantic_error
e2 (e
);
2494 if (e2
.tok1
== 0) e2
.tok1
= v
->tok
;
2499 for (unsigned j
=0; j
<fd
->formal_args
.size(); j
++)
2501 vardecl
* v
= fd
->formal_args
[j
];
2504 v
->char_ptr_arg
= (is_unmodified_string_fnarg (session
, fd
, v
));
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
;
2511 if (fd
->mangle_oldstyle
)
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) << "; };";
2522 o
->newline() << (v
->char_ptr_arg
? "const char *" : c_typename (v
->type
))
2523 << " " << c_localname (v
->name
) << ";";
2525 } catch (const semantic_error
& e
) {
2526 semantic_error
e2 (e
);
2527 if (e2
.tok1
== 0) e2
.tok1
= v
->tok
;
2532 fd
->body
->visit (this);
2534 if (fd
->type
== pe_unknown
)
2535 o
->newline() << "/* no return value */";
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
))
2545 o
->newline(-1) << "} " << c_funcname (fd
->name
) << ";";
2547 // finish dummy indentation
2548 this->o
->indent (-1);
2549 this->o
->assert_0_indent ();
2551 declared_vars
.clear();
2552 this->current_function
= 0;
2553 this->already_checked_action_count
= false;
2557 c_unparser::emit_function (functiondecl
* v
)
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;
2565 o
->newline() << "static void " << c_funcname (v
->name
)
2566 << " (struct context* __restrict__ c) {";
2569 o
->newline() << "__label__ deref_fault;";
2570 o
->newline() << "__label__ out;";
2572 << "struct " << c_funcname (v
->name
) << "_locals * "
2573 << " __restrict__ l = "
2574 << "& c->locals[c->nesting+1]." << c_funcname (v
->name
) // NB: nesting+1
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 ...
2582 for (unsigned i
= 0; i
< v
->locals
.size(); i
++) {
2583 o
->newline() << c_arg_define(v
->locals
[i
]->name
); // #define STAP_ARG_foo ...
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";
2589 // set this, in case embedded-c code sets last_error but doesn't otherwise identify itself
2591 o
->newline() << "c->last_stmt = " << lex_cast_qstring(*v
->tok
) << ";";
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.
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) << "}";
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)";
2610 // initialize locals
2611 // XXX: optimization: use memset instead
2612 for (unsigned i
=0; i
<v
->locals
.size(); i
++)
2614 if (v
->locals
[i
]->index_types
.size() > 0) // array?
2615 throw SEMANTIC_ERROR (_("array locals not supported, missing global declaration?"),
2618 o
->newline() << getvar (v
->locals
[i
]).init();
2621 // initialize return value, if any
2622 if (v
->type
!= pe_unknown
)
2624 var retvalue
= var(this, true, v
->type
, "__retvalue", false); // not mangled
2625 o
->newline() << retvalue
.init();
2631 o
->newline() << "#define STAP_RETURN(v) do { STAP_RETVALUE = (int64_t) (v); "
2632 "goto out; } while(0)";
2637 "#define STAP_RETURN(v) do { strlcpy(STAP_RETVALUE, (v), MAXSTRINGLEN); "
2638 "goto out; } while(0)";
2642 o
->newline() << "#define STAP_RETURN() do { goto out; } while(0)";
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
2650 max_action_info
mai (*session
);
2651 v
->body
->visit (&mai
);
2653 if (mai
.statement_count_finite() && !session
->suppress_time_limits
2654 && !session
->unoptimized
) // this is a finite-statement-count function
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;
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";
2667 this->current_function
= 0;
2669 record_actions(0, v
->body
->tok
, true);
2671 o
->newline(-1) << "deref_fault: __attribute__((unused));";
2672 o
->newline(0) << "out: __attribute__((unused));";
2674 // Function prologue: this is why we redirect the "return" above.
2675 // Decrement nesting level.
2676 o
->newline(1) << "c->nesting --;";
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
2684 for (unsigned i
= 0; i
< v
->locals
.size(); i
++) {
2685 o
->newline() << c_arg_undef(v
->locals
[i
]->name
); // #undef STAP_ARG_foo
2687 o
->newline() << "#undef STAP_RETVALUE";
2688 o
->newline(-1) << "}\n";
2690 this->current_function
= 0;
2691 this->already_checked_action_count
= false;
2695 c_tmpcounter::emit_probe (derived_probe
* dp
)
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();
2704 if (get_probe_dupe (dp
) == NULL
)
2706 translator_output
*o
= parent
->o
;
2708 // indent the dummy output as if we were already in a block
2709 this->o
->indent (1);
2711 o
->newline() << "struct " << dp
->name() << "_locals {";
2713 for (unsigned j
=0; j
<dp
->locals
.size(); j
++)
2715 vardecl
* v
= dp
->locals
[j
];
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
;
2727 dp
->body
->visit (this);
2729 // finish by visiting conditions of affected probes to match
2730 // c_unparser::emit_probe()
2731 if (!dp
->probes_with_affected_conditions
.empty())
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);
2739 o
->newline(-1) << "} " << dp
->name() << ";";
2741 // finish dummy indentation
2742 this->o
->indent (-1);
2743 this->o
->assert_0_indent ();
2746 declared_vars
.clear();
2747 this->current_probe
= 0;
2748 this->already_checked_action_count
= false;
2751 #define DUPMETHOD_CALL 0
2752 #define DUPMETHOD_ALIAS 0
2753 #define DUPMETHOD_RENAME 1
2757 c_unparser::emit_probe (derived_probe
* v
)
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;
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
2768 probe
*dupe
= get_probe_dupe (v
);
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());
2778 // This one emits a direct call to the first copy.
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
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*.
2796 #error "Unknown duplicate elimination method"
2799 else // This probe is unique. Remember it and output it.
2802 o
->newline() << "static void " << v
->name() << " (struct context * __restrict__ c) ";
2806 o
->newline() << "__label__ deref_fault;";
2807 o
->newline() << "__label__ out;";
2809 // emit static read/write lock decls for global variables
2810 if (v
->needs_global_locks ())
2812 varuse_collecting_visitor
vut(*session
);
2813 v
->body
->visit (& vut
);
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
)
2826 assert((*it
)->sole_location()->condition
!= NULL
);
2827 (*it
)->sole_location()->condition
->visit (& vut
);
2830 emit_lock_decls (vut
);
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
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
);
2843 // emit probe local initialization block
2845 v
->emit_probe_local_init(*this->session
, o
);
2847 // emit all read/write locks for global variables
2848 if (v
->needs_global_locks ())
2851 // initialize locals
2852 for (unsigned j
=0; j
<v
->locals
.size(); j
++)
2854 if (v
->locals
[j
]->synthetic
)
2856 if (v
->locals
[j
]->index_types
.size() > 0) // array?
2857 throw SEMANTIC_ERROR (_("array locals not supported, missing global declaration?"),
2859 else if (v
->locals
[j
]->type
== pe_long
)
2860 o
->newline() << "l->" << c_localname (v
->locals
[j
]->name
)
2862 else if (v
->locals
[j
]->type
== pe_string
)
2863 o
->newline() << "l->" << c_localname (v
->locals
[j
]->name
)
2866 throw SEMANTIC_ERROR (_("unsupported local variable type"),
2870 v
->initialize_probe_context_vars (o
);
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
;
2878 if (mai
.statement_count_finite() && !session
->suppress_time_limits
2879 && !session
->unoptimized
) // this is a finite-statement-count probe
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;
2887 v
->body
->visit (this);
2889 record_actions(0, v
->body
->tok
, true);
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
2898 if (!v
->probes_with_affected_conditions
.empty())
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
);
2906 if (v
->needs_global_locks ())
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";
2915 this->current_probe
= 0;
2916 this->already_checked_action_count
= false;
2919 // Updates the cond_enabled field and sets need_module_refresh if it was
2922 c_unparser::emit_probe_condition_update(derived_probe
* v
)
2924 unsigned i
= v
->session_index
;
2925 assert(i
< session
->probes
.size());
2927 expression
*cond
= v
->sole_location()->condition
;
2930 string cond_enabled
= "stap_probes[" + lex_cast(i
) + "].cond_enabled";
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.
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);
2941 o
->newline(1) << cond_enabled
<< " ^= 1;"; // toggle it
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);";
2948 o
->newline(-1) << "}";
2952 c_unparser::emit_lock_decls(const varuse_collecting_visitor
& vut
)
2954 unsigned numvars
= 0;
2956 if (session
->verbose
> 1)
2957 clog
<< "probe " << current_probe
->session_index
<< " "
2958 "('" << *current_probe
->sole_location() << "') locks";
2960 // We can only make this static in kernel mode. In stapdyn mode,
2961 // the globals and their locks are in shared memory.
2963 if (!session
->runtime_usermode_p())
2964 o
->line() << "static ";
2965 o
->line() << "const struct stp_probe_lock locks[] = {";
2968 for (unsigned i
= 0; i
< session
->globals
.size(); i
++)
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;
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.
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;
2988 written_p
= vcv_needs_global_locks
.written
.count(v
) > 0;
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
)
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) << "},";
3009 if (session
->verbose
> 1)
3010 clog
<< " " << v
->name
<< "[" << (read_p
? "r" : "")
3011 << (write_p
? "w" : "") << "]";
3014 o
->newline(-1) << "};";
3016 if (session
->verbose
> 1)
3019 clog
<< _(" nothing");
3026 c_unparser::emit_locks()
3028 o
->newline() << "if (!stp_lock_probe(locks, ARRAY_SIZE(locks)))";
3029 o
->newline(1) << "return;";
3035 c_unparser::emit_unlocks()
3037 o
->newline() << "stp_unlock_probe(locks, ARRAY_SIZE(locks));";
3042 c_unparser::collect_map_index_types(vector
<vardecl
*> const & vars
,
3043 set
< pair
<vector
<exp_type
>, exp_type
> > & types
)
3045 for (unsigned i
= 0; i
< vars
.size(); ++i
)
3047 vardecl
*v
= vars
[i
];
3050 types
.insert(make_pair(v
->index_types
, v
->type
));
3056 mapvar::value_typename(exp_type e
)
3067 throw SEMANTIC_ERROR(_("array type is neither string nor long"));
3072 mapvar::key_typename(exp_type e
)
3081 throw SEMANTIC_ERROR(_("array key is neither string nor long"));
3086 mapvar::shortname(exp_type e
)
3095 throw SEMANTIC_ERROR(_("array type is neither string nor long"));
3100 c_unparser::map_keytypes(vardecl
* v
)
3103 vector
<exp_type
> types
= v
->index_types
;
3104 types
.push_back (v
->type
);
3105 for (unsigned i
= 0; i
< types
.size(); ++i
)
3119 throw SEMANTIC_ERROR(_("unknown type of map"));
3127 c_unparser::emit_map_type_instantiations ()
3129 set
< pair
<vector
<exp_type
>, exp_type
> > types
;
3131 collect_map_index_types(session
->globals
, types
);
3133 for (unsigned i
= 0; i
< session
->probes
.size(); ++i
)
3134 collect_map_index_types(session
->probes
[i
]->locals
, types
);
3136 for (map
<string
,functiondecl
*>::iterator it
= session
->functions
.begin(); it
!= session
->functions
.end(); it
++)
3137 collect_map_index_types(it
->second
->locals
, types
);
3140 o
->newline() << "#include \"alloc.c\"";
3142 for (set
< pair
<vector
<exp_type
>, exp_type
> >::const_iterator i
= types
.begin();
3143 i
!= types
.end(); ++i
)
3145 o
->newline() << "#define VALUE_TYPE " << mapvar::value_typename(i
->second
);
3146 for (unsigned j
= 0; j
< i
->first
.size(); ++j
)
3148 string ktype
= mapvar::key_typename(i
->first
.at(j
));
3149 o
->newline() << "#define KEY" << (j
+1) << "_TYPE " << ktype
;
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
)
3159 o
->newline() << "#undef KEY" << (j
+1) << "_TYPE";
3164 o
->newline() << "#include \"map.c\"";
3170 c_unparser::c_typename (exp_type e
)
3174 case pe_long
: return string("int64_t");
3175 case pe_string
: return string("string_t");
3176 case pe_stats
: return string("Stat");
3179 throw SEMANTIC_ERROR (_("cannot expand unknown type"));
3184 // XXX: the below is just for the sake of example; it's possibly
3185 // better to expose the hash function in hash.cxx
3188 // do_hash (const char *e)
3190 // unsigned int foo = 0;
3192 // foo *= 101; foo += *e; e++;
3199 c_unparser::c_localname (const string
& e
, bool mangle_oldstyle
)
3201 if (strverscmp(session
->compatible
.c_str(), "1.8") < 0 || mangle_oldstyle
)
3202 return e
; // old mangling behaviour
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()));
3211 c_unparser::c_globalname (const string
& e
)
3213 // XXX uncomment to test custom mangling:
3214 // return "s_" + e + "_" + lex_cast(do_hash(e.c_str()));
3220 c_unparser::c_funcname (const string
& e
)
3222 // XXX uncomment to test custom mangling:
3223 // return "function_" + e + "_" + lex_cast(do_hash(e.c_str()));
3224 return "function_" + e
;
3229 c_unparser::c_arg_define (const string
& e
)
3231 return "#define STAP_ARG_" + e
+ " THIS->" + c_localname(e
);
3236 c_unparser::c_arg_undef (const string
& e
)
3238 return "#undef STAP_ARG_" + e
;
3242 c_unparser::c_global_write_def(vardecl
* v
)
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;})";
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)";
3264 c_unparser::c_global_read_def(vardecl
* v
)
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__)";
3274 o
->newline() << "#define STAP_GLOBAL_GET_" << v
->unmangled_name
<< "() "
3275 << "global(" << c_globalname(v
->name
) << ")";
3280 c_unparser::c_global_write_undef(vardecl
* v
)
3282 o
->newline() << "#undef STAP_GLOBAL_SET_" << v
->unmangled_name
;
3286 c_unparser::c_global_read_undef(vardecl
* v
)
3288 o
->newline() << "#undef STAP_GLOBAL_GET_" << v
->unmangled_name
;
3292 c_unparser::c_assign (var
& lvalue
, const string
& rvalue
, const token
*tok
)
3294 switch (lvalue
.type())
3297 c_strcpy(lvalue
.value(), rvalue
);
3300 o
->newline() << lvalue
<< " = " << rvalue
<< ";";
3303 throw SEMANTIC_ERROR (_("unknown lvalue type in assignment"), tok
);
3309 c_unparser::c_assign(tmpvar
& t
, expression
*e
, const char* msg
)
3311 // We don't really need a tmpvar if the expression is a literal.
3312 // (NB: determined by the expression itself, not tok->type!)
3314 if (dynamic_cast<literal
*>(e
))
3316 // We need to use the visitors to get proper C values, like
3317 // "((int64_t)5LL)" for numbers and escaped characters in strings.
3319 // Create a fake output stream so we can grab the string output.
3321 translator_output
tmp_o(oss
);
3323 // Temporarily swap out the real translator_output stream with our
3325 translator_output
*saved_o
= o
;
3328 // Visit the expression then restore the original output stream
3332 // All instances of this tmpvar will use the literal value.
3333 t
.override (oss
.str());
3336 c_assign (t
.value(), e
, msg
);
3339 struct expression_is_functioncall
: public nop_visitor
3341 functioncall
* fncall
;
3342 expression_is_functioncall ()
3345 void visit_functioncall (functioncall
* e
)
3352 c_unparser::c_assign (const string
& lvalue
, expression
* rvalue
,
3355 if (rvalue
->type
== pe_long
)
3357 o
->newline() << lvalue
<< " = ";
3358 rvalue
->visit (this);
3361 else if (rvalue
->type
== pe_string
)
3363 expression_is_functioncall eif
;
3364 rvalue
->visit(& eif
);
3365 if (!session
->unoptimized
&& eif
.fncall
)
3367 const functioncall
* saved_fncall
= assigned_functioncall
;
3368 const string
* saved_retval
= assigned_functioncall_retval
;
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);
3378 assigned_functioncall
= saved_fncall
;
3379 assigned_functioncall_retval
= saved_retval
;
3383 // will call rvalue->visit()
3384 c_strcpy (lvalue
, rvalue
);
3389 string fullmsg
= string(msg
) + _(" type unsupported");
3390 throw SEMANTIC_ERROR (fullmsg
, rvalue
->tok
);
3396 c_unparser::c_assign (const string
& lvalue
, const string
& rvalue
,
3397 exp_type type
, const char* msg
, const token
* tok
)
3399 if (type
== pe_long
)
3401 o
->newline() << lvalue
<< " = " << rvalue
<< ";";
3403 else if (type
== pe_string
)
3405 c_strcpy (lvalue
, rvalue
);
3409 string fullmsg
= string(msg
) + _(" type unsupported");
3410 throw SEMANTIC_ERROR (fullmsg
, tok
);
3416 c_unparser_assignment::c_assignop(tmpvar
& res
,
3418 tmpvar
const & rval
,
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:
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
3430 // we'd like to work with a local tmpvar so we can overwrite it in
3431 // some optimized cases
3433 translator_output
* o
= parent
->o
;
3435 if (res
.type() == pe_string
)
3438 throw SEMANTIC_ERROR (_("post assignment on strings not supported"),
3442 parent
->c_strcpy (lval
.value(), rval
.value());
3443 // no need for second copy
3446 else if (op
== ".=")
3448 parent
->c_strcat (lval
.value(), rval
.value());
3452 throw SEMANTIC_ERROR (_F("string assignment operator %s unsupported",
3453 op
.to_string().c_str()), tok
);
3455 else if (op
== "<<<")
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
;
3463 assert(lval
.type() == pe_stats
);
3464 assert(rval
.type() == pe_long
);
3465 assert(res
.type() == pe_long
);
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
<< ");";
3473 else if (res
.type() == pe_long
)
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
3482 // compute the modify portion of a modify-accumulate
3484 unsigned oplen
= op
.size();
3486 macop
= "*error*"; // special shortcuts below
3487 else if (op
== "++" || op
== "+=")
3489 else if (op
== "--" || op
== "-=")
3491 else if (oplen
> 1 && op
[oplen
-1] == '=') // for *=, <<=, etc...
3495 throw SEMANTIC_ERROR (_("unknown macop for assignment"), tok
);
3499 if (macop
== "/" || macop
== "%" || op
== "=")
3500 throw SEMANTIC_ERROR (_("invalid post-mode operator"), tok
);
3502 o
->newline() << res
<< " = " << lval
<< ";";
3504 if (macop
== "+=" || macop
== "-=")
3505 o
->newline() << lval
<< " " << macop
<< " " << rval
<< ";";
3507 o
->newline() << lval
<< " = " << res
<< " " << macop
<< " " << rval
<< ";";
3511 if (op
== "=") // shortcut simple assignment
3513 o
->newline() << lval
<< " = " << rval
<< ";";
3518 if (macop
== "/=" || macop
== "%=")
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
<< ");";
3531 o
->newline() << lval
<< " " << macop
<< " " << rval
<< ";";
3537 throw SEMANTIC_ERROR (_("assignment type not yet implemented"), tok
);
3542 c_unparser::c_declare(exp_type ty
, const string
&ident
)
3544 o
->newline() << c_typename (ty
) << " " << ident
<< ";";
3549 c_unparser::c_declare_static(exp_type ty
, const string
&ident
)
3551 o
->newline() << "static " << c_typename (ty
) << " " << ident
<< ";";
3556 c_unparser::c_strcpy (const string
& lvalue
, const string
& rvalue
)
3558 o
->newline() << "strlcpy ("
3560 << rvalue
<< ", MAXSTRINGLEN);";
3565 c_unparser::c_strcpy (const string
& lvalue
, expression
* rvalue
)
3567 o
->newline() << "strlcpy (" << lvalue
<< ", ";
3568 rvalue
->visit (this);
3569 o
->line() << ", MAXSTRINGLEN);";
3574 c_unparser::c_strcat (const string
& lvalue
, const string
& rvalue
)
3576 o
->newline() << "strlcat ("
3578 << rvalue
<< ", MAXSTRINGLEN);";
3583 c_unparser::c_strcat (const string
& lvalue
, expression
* rvalue
)
3585 o
->newline() << "strlcat (" << lvalue
<< ", ";
3586 rvalue
->visit (this);
3587 o
->line() << ", MAXSTRINGLEN);";
3592 c_unparser::is_local(vardecl
const *r
, token
const *tok
)
3596 for (unsigned i
=0; i
<current_probe
->locals
.size(); i
++)
3598 if (current_probe
->locals
[i
] == r
)
3602 else if (current_function
)
3604 for (unsigned i
=0; i
<current_function
->locals
.size(); i
++)
3606 if (current_function
->locals
[i
] == r
)
3610 for (unsigned i
=0; i
<current_function
->formal_args
.size(); i
++)
3612 if (current_function
->formal_args
[i
] == r
)
3617 for (unsigned i
=0; i
<session
->globals
.size(); i
++)
3619 if (session
->globals
[i
] == r
)
3624 throw SEMANTIC_ERROR (_("unresolved symbol"), tok
);
3626 throw SEMANTIC_ERROR (_("unresolved symbol: ") + (string
)r
->name
);
3631 c_unparser::gensym(exp_type ty
)
3633 return tmpvar (this, ty
, tmpvar_counter
);
3637 c_unparser::gensym_aggregate()
3639 return aggvar (this, tmpvar_counter
);
3644 c_unparser::getvar(vardecl
*v
, token
const *tok
)
3646 bool loc
= is_local (v
, tok
);
3648 return var (this, loc
, v
->type
, v
->name
);
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())
3656 return var (this, loc
, v
->type
, sd
, v
->name
);
3662 c_unparser::getmap(vardecl
*v
, token
const *tok
)
3665 throw SEMANTIC_ERROR(_("attempt to use scalar where map expected"), tok
);
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())
3671 return mapvar (this, is_local (v
, tok
), v
->type
, sd
,
3672 v
->name
, v
->index_types
, v
->maxsize
, v
->wrap
);
3677 c_unparser::getiter(symbol
*s
)
3679 return itervar (this, s
, tmpvar_counter
);
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.
3687 c_unparser::record_actions (unsigned actions
, const token
* tok
, bool update
)
3689 action_counter
+= actions
;
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
)
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
;
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.
3705 o
->newline() << "c->last_stmt = " << lex_cast_qstring(*tok
) << ";";
3707 o
->newline() << "goto out;";
3708 o
->newline(-1) << "}";
3715 c_unparser::visit_block (block
*s
)
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
);
3723 o
->newline() << "{";
3726 for (unsigned i
=0; i
<s
->statements
.size(); i
++)
3730 wrap_compound_visit (s
->statements
[i
]);
3733 catch (const semantic_error
& e
)
3735 session
->print_error (e
);
3738 o
->newline(-1) << "}";
3740 close_compound_statement ("block_statement", s
);
3744 void c_unparser::visit_try_block (try_block
*s
)
3746 record_actions(0, s
->tok
, true); // flush prior actions
3748 start_compound_statement ("try_block", s
);
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;";
3756 assert (!session
->unoptimized
|| s
->try_block
); // dead_stmtexpr_remover would zap it
3759 wrap_compound_visit (s
->try_block
);
3760 record_actions(0, s
->try_block
->tok
, true); // flush accumulated actions
3762 o
->newline() << "goto normal_fallthrough;";
3764 o
->newline() << "deref_fault: __attribute__((unused));";
3765 o
->newline() << "out: __attribute__((unused));";
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) << "}";
3772 o
->newline() << "if (likely(c->last_error == NULL)) goto out;";
3774 if (s
->catch_error_var
)
3776 var
cev(getvar(s
->catch_error_var
->referent
, s
->catch_error_var
->tok
));
3777 c_strcpy (cev
.value(), "c->last_error");
3779 o
->newline() << "c->last_error = NULL;";
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);
3787 wrap_compound_visit (s
->catch_block
);
3788 record_actions(0, s
->catch_block
->tok
, true); // flush accumulated actions
3791 o
->newline() << "normal_fallthrough:";
3792 o
->newline() << ";"; // to have _some_ statement
3793 o
->newline(-1) << "}";
3795 close_compound_statement ("try_block", s
);
3800 c_unparser::visit_embeddedcode (embeddedcode
*s
)
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() << "{";
3808 // if (1 || s->tagged_p ("CATCH_DEREF_FAULT"))
3809 // o->newline() << "__label__ deref_fault;";
3811 vector
<vardecl
*> read_defs
;
3812 vector
<vardecl
*> write_defs
;
3813 for (unsigned i
= 0; i
< session
->globals
.size(); i
++)
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
+ " */"))
3821 c_global_read_def(v
);
3822 read_defs
.push_back(v
);
3824 if (s
->tagged_p("/* pragma:write:" + name
+ " */"))
3826 c_global_write_def(v
);
3827 write_defs
.push_back(v
);
3831 o
->newline(1) << s
->code
;
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
);
3839 // if (1 || s->tagged_p ("CATCH_DEREF_FAULT"))
3840 // o->newline() << ";";
3842 o
->newline() << "}";
3847 c_unparser::visit_null_statement (null_statement
*)
3849 o
->newline() << "/* null */;";
3854 c_unparser::visit_expr_statement (expr_statement
*s
)
3856 o
->newline() << "(void) ";
3857 s
->value
->visit (this);
3859 record_actions(1, s
->tok
);
3864 c_tmpcounter::wrap_compound_visit (statement
*s
)
3868 std::ostream::pos_type before_struct_pos
;
3869 std::ostream::pos_type after_struct_pos
;
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
);
3877 c_tmpcounter::wrap_compound_visit (expression
*e
)
3881 std::ostream::pos_type before_struct_pos
;
3882 std::ostream::pos_type after_struct_pos
;
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
);
3890 c_tmpcounter::start_struct_def (std::ostream::pos_type
&before
,
3891 std::ostream::pos_type
&after
, const token
* tok
)
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
) << " */";
3907 c_tmpcounter::close_struct_def (std::ostream::pos_type before
,
3908 std::ostream::pos_type after
)
3910 // meant to be used with ::start_struct_def. remove the struct if empty.
3911 translator_output
*o
= parent
->o
;
3913 if (after
== o
->tellp())
3916 o
->newline() << "};";
3920 c_tmpcounter::start_compound_statement (const char* tag
, statement
*s
)
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
) << " */";
3931 c_tmpcounter::close_compound_statement (const char*, statement
*)
3933 translator_output
*o
= parent
->o
;
3934 o
->newline(-1) << "};";
3939 c_unparser::visit_if_statement (if_statement
*s
)
3941 record_actions(1, s
->tok
, true);
3943 start_compound_statement ("if_statement", s
);
3945 o
->newline() << "if (";
3947 wrap_compound_visit (s
->condition
);
3951 wrap_compound_visit (s
->thenblock
);
3952 record_actions(0, s
->thenblock
->tok
, true);
3953 o
->newline(-1) << "}";
3956 o
->newline() << "else {";
3958 wrap_compound_visit (s
->elseblock
);
3959 record_actions(0, s
->elseblock
->tok
, true);
3960 o
->newline(-1) << "}";
3963 close_compound_statement ("if_statement", s
);
3968 c_unparser::visit_for_loop (for_loop
*s
)
3970 string ctr
= lex_cast (label_counter
++);
3971 string toplabel
= "top_" + ctr
;
3972 string contlabel
= "continue_" + ctr
;
3973 string breaklabel
= "break_" + ctr
;
3975 start_compound_statement ("for_loop", s
);
3978 wrap_compound_visit (s
->init
);
3979 record_actions(1, s
->tok
, true);
3982 o
->newline(-1) << toplabel
<< ":";
3984 // Emit an explicit action here to cover the act of iteration.
3985 // Equivalently, it can stand for the evaluation of the condition
3988 record_actions(1, s
->tok
);
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
<< ";";
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 ();
4005 o
->newline(-1) << contlabel
<< ":";
4007 wrap_compound_visit (s
->incr
);
4008 o
->newline() << "goto " << toplabel
<< ";";
4011 o
->newline(-1) << breaklabel
<< ":";
4012 o
->newline(1) << "; /* dummy statement */";
4014 close_compound_statement ("for_loop", s
);
4018 struct arrayindex_downcaster
4019 : public traversing_visitor
4023 arrayindex_downcaster (arrayindex
*& arr
)
4027 void visit_arrayindex (arrayindex
* e
)
4035 expression_is_arrayindex (expression
*e
,
4038 arrayindex
*h
= NULL
;
4039 arrayindex_downcaster
d(h
);
4041 if (static_cast<void*>(h
) == static_cast<void*>(e
))
4050 // Look for opportunities to used a saved value at the beginning of the loop
4052 c_unparser::visit_foreach_loop_value (foreach_loop
* s
, const string
& value
)
4054 bool stable_value
= false;
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]))
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.
4068 classify_indexable (s
->base
, array
, hist
);
4070 if (!(hist
&& get_symbol_within_expression(hist
->stat
)->referent
->arity
> 0))
4072 set
<vardecl
*> indexes
;
4073 for (unsigned i
=0; i
< s
->indexes
.size(); ++i
)
4074 indexes
.insert(s
->indexes
[i
]->referent
);
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;
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.
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
);
4099 s
->block
->visit (this);
4104 c_unparser::get_foreach_loop_value (arrayindex
* ai
, string
& value
)
4108 map
<string
,string
>::iterator it
= foreach_loop_values
.find(lex_cast(*ai
));
4109 if (it
== foreach_loop_values
.end())
4117 c_unparser::visit_foreach_loop (foreach_loop
*s
)
4121 classify_indexable (s
->base
, array
, hist
);
4123 string ctr
= lex_cast (label_counter
++);
4124 string toplabel
= "top_" + ctr
;
4125 string contlabel
= "continue_" + ctr
;
4126 string breaklabel
= "break_" + ctr
;
4130 mapvar mv
= getmap (array
->referent
, s
->tok
);
4133 // NB: structure parallels for_loop
4137 tmpvar
*res_limit
= NULL
;
4140 // Evaluate the limit expression once.
4141 res_limit
= new tmpvar(gensym(pe_long
));
4142 c_assign (*res_limit
, s
->limit
, "foreach limit");
4145 // aggregate array if required
4146 if (mv
.is_parallel())
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) << "}";
4155 // sort array if desired
4156 if (s
->sort_direction
)
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;
4172 sort_column
= lex_cast(s
->sort_column
);
4174 o
->newline() << "else"; // only sort if aggregation was ok
4177 o
->newline(1) << mv
.function_keysym("sortn", true) <<" ("
4178 << mv
.fetch_existing_aggregate() << ", "
4179 << *res_limit
<< ", " << sort_column
<< ", "
4180 << - s
->sort_direction
<< ");";
4184 o
->newline(1) << mv
.function_keysym("sort", true) <<" ("
4185 << mv
.fetch_existing_aggregate() << ", "
4186 << sort_column
<< ", "
4187 << - s
->sort_direction
<< ");";
4194 // sort array if desired
4195 if (s
->sort_direction
)
4199 o
->newline() << mv
.function_keysym("sortn") <<" ("
4200 << mv
.value() << ", "
4201 << *res_limit
<< ", " << s
->sort_column
<< ", "
4202 << - s
->sort_direction
<< ");";
4206 o
->newline() << mv
.function_keysym("sort") <<" ("
4207 << mv
.value() << ", "
4208 << s
->sort_column
<< ", "
4209 << - s
->sort_direction
<< ");";
4214 // NB: sort direction sense is opposite in runtime, thus the negation
4216 tmpvar
*limitv
= NULL
;
4219 // Create the loop limit variable here and initialize it.
4220 limitv
= new tmpvar(gensym (pe_long
));
4221 o
->newline() << *limitv
<< " = 0LL;";
4224 if (mv
.is_parallel())
4225 aggregations_active
.insert(mv
.value());
4227 itervar iv
= getiter (array
);
4228 o
->newline() << iv
<< " = " << iv
.start (mv
) << ";";
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
)
4236 if (s
->array_slice
[i
])
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
);
4243 array_slice_vars
.push_back(NULL
);
4246 record_actions(1, s
->tok
, true);
4249 o
->newline(-1) << toplabel
<< ":";
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.
4255 record_actions(1, s
->tok
);
4257 o
->newline() << "if (! (" << iv
<< ")) goto " << breaklabel
<< ";";
4260 loop_break_labels
.push_back (breaklabel
);
4261 loop_continue_labels
.push_back (contlabel
);
4262 o
->newline() << "{";
4267 // If we've been through LIMIT loop iterations, quit.
4268 o
->newline() << "if (" << *limitv
<< "++ >= " << *res_limit
4269 << ") goto " << breaklabel
<< ";";
4271 // We're done with limitv and res_limit.
4276 for (unsigned i
= 0; i
< s
->indexes
.size(); ++i
)
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
);
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())
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
)
4292 // only output a comparsion if the expression is not "*".
4293 if (s
->array_slice
[i
])
4295 o
->line() << " || ";
4296 if (s
->indexes
[i
]->type
== pe_string
)
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";
4304 else if (s
->indexes
[i
]->type
== pe_long
)
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
];
4313 throw SEMANTIC_ERROR (_("unexpected type"), s
->tok
);
4316 o
->line() << ") goto " << contlabel
<< ";"; // end of the if statment
4321 var v
= getvar (s
->value
->referent
);
4322 c_assign (v
, iv
.get_value (mv
, v
.type()), s
->tok
);
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 ();
4332 o
->newline(-1) << contlabel
<< ":";
4333 o
->newline(1) << iv
<< " = " << iv
.next (mv
) << ";";
4334 o
->newline() << "goto " << toplabel
<< ";";
4337 o
->newline(-1) << breaklabel
<< ":";
4338 o
->newline(1) << "; /* dummy statement */";
4340 if (mv
.is_parallel())
4341 aggregations_active
.erase(mv
.value());
4345 // Iterating over buckets in a histogram.
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
);
4352 tmpvar
*res_limit
= NULL
;
4353 tmpvar
*limitv
= NULL
;
4356 // Evaluate the limit expression once.
4357 res_limit
= new tmpvar(gensym(pe_long
));
4358 c_assign (*res_limit
, s
->limit
, "foreach limit");
4360 // Create the loop limit variable here and initialize it.
4361 limitv
= new tmpvar(gensym (pe_long
));
4362 o
->newline() << *limitv
<< " = 0LL;";
4365 var bucketvar
= getvar (s
->indexes
[0]->referent
);
4367 aggvar agg
= gensym_aggregate ();
4369 var
*v
= load_aggregate(hist
->stat
, agg
);
4370 v
->assert_hist_compatible(*hist
);
4372 record_actions(1, s
->tok
, true);
4373 o
->newline() << "for (" << bucketvar
<< " = 0; "
4374 << bucketvar
<< " < " << v
->buckets() << "; "
4375 << bucketvar
<< "++) { ";
4377 loop_break_labels
.push_back (breaklabel
);
4378 loop_continue_labels
.push_back (contlabel
);
4382 // If we've been through LIMIT loop iterations, quit.
4383 o
->newline() << "if (" << *limitv
<< "++ >= " << *res_limit
4386 // We're done with limitv and res_limit.
4393 var v
= getvar (s
->value
->referent
);
4394 c_assign (v
, agg
.get_hist (bucketvar
), s
->tok
);
4397 visit_foreach_loop_value(s
, agg
.get_hist(bucketvar
));
4398 record_actions(1, s
->block
->tok
, true);
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 ();
4414 c_unparser::visit_return_statement (return_statement
* s
)
4416 if (current_function
== 0)
4417 throw SEMANTIC_ERROR (_("cannot 'return' from probe"), s
->tok
);
4421 if (s
->value
->type
!= current_function
->type
)
4422 throw SEMANTIC_ERROR (_("return type mismatch"), current_function
->tok
,
4425 c_assign ("l->__retvalue", s
->value
, "return value");
4427 else if (current_function
->type
!= pe_unknown
)
4428 throw SEMANTIC_ERROR (_("return type mismatch"), current_function
->tok
,
4431 record_actions(1, s
->tok
, true);
4432 o
->newline() << "goto out;";
4437 c_unparser::visit_next_statement (next_statement
* s
)
4439 /* Set next flag to indicate to caller to call next alternative function */
4440 if (current_function
!= 0)
4441 o
->newline() << "c->next = 1;";
4443 record_actions(1, s
->tok
, true);
4444 o
->newline() << "goto out;";
4448 struct delete_statement_operand_visitor
:
4449 public throwing_visitor
4452 delete_statement_operand_visitor (c_unparser
*p
):
4453 throwing_visitor (_("invalid operand of delete expression")),
4456 void visit_symbol (symbol
* e
);
4457 void visit_arrayindex (arrayindex
* e
);
4461 delete_statement_operand_visitor::visit_symbol (symbol
* e
)
4463 translator_output
* o
= parent
->o
;
4464 assert (e
->referent
!= 0);
4465 if (e
->referent
->arity
> 0)
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 ();
4473 if (mvar
.is_parallel())
4474 o
->newline() << "_stp_pmap_clear (" << mvar
.value() << ");";
4476 o
->newline() << "_stp_map_clear (" << mvar
.value() << ");";
4480 var v
= parent
->getvar(e
->referent
, e
->tok
);
4484 o
->newline() << "_stp_stat_clear (" << v
.value() << ");";
4487 o
->newline() << v
.value() << " = 0;";
4490 o
->newline() << v
.value() << "[0] = '\\0';";
4494 throw SEMANTIC_ERROR(_("Cannot delete unknown expression type"), e
->tok
);
4500 delete_statement_operand_visitor::visit_arrayindex (arrayindex
* e
)
4504 classify_indexable (e
->base
, array
, hist
);
4505 translator_output
* o
= parent
->o
;
4509 bool array_slice
= false;
4510 for (unsigned i
= 0; i
< e
->indexes
.size(); i
++)
4511 if (e
->indexes
[i
] == NULL
)
4517 if (!array_slice
) // delete a single element
4520 parent
->load_map_indices (e
, idx
);
4521 mapvar mvar
= parent
->getmap (array
->referent
, e
->tok
);
4522 o
->newline() << mvar
.del (idx
) << ";";
4524 else // delete elements if they match the array slice.
4526 vardecl
* r
= array
->referent
;
4527 mapvar mvar
= parent
->getmap (r
, e
->tok
);
4528 itervar iv
= parent
->getiter(array
);
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
++)
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
);
4546 array_slice_vars
.push_back(NULL
);
4547 if (mvar
.is_parallel())
4549 tmpvar
*asvar
= new tmpvar(parent
->gensym(r
->index_types
[i
]));
4550 idx
.push_back(*asvar
);
4555 if (mvar
.is_parallel())
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) << "}";
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
;
4572 o
->newline() << iv
<< " = " << iv
.start(mvar
) << ";";
4573 o
->newline() << toplabel
<< ":";
4575 o
->newline(1) << "if (!(" << iv
<< ")){";
4576 o
->newline(1) << "goto " << breaklabel
<< ";}";
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
)
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";
4591 throw SEMANTIC_ERROR (_("unexpected type"), e
->tok
);
4596 // conditional is true, so delete item and go to the next item
4597 if (mvar
.is_parallel())
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
) << ";";
4610 o
->newline(1) << iv
<< " = " << iv
.del_next(mvar
) << ";";
4612 o
->newline(-1) << "} else";
4613 o
->newline(1) << iv
<< " = " << iv
.next(mvar
) << ";";
4615 o
->newline(-1) << "goto " << toplabel
<< ";";
4617 o
->newline(-1) << breaklabel
<< ":";
4618 o
->newline(1) << "; /* dummy statement */";
4624 throw SEMANTIC_ERROR(_("cannot delete histogram bucket entries\n"), e
->tok
);
4630 c_unparser::visit_delete_statement (delete_statement
* s
)
4632 delete_statement_operand_visitor
dv (this);
4633 s
->value
->visit (&dv
);
4634 record_actions(1, s
->tok
);
4639 c_unparser::visit_break_statement (break_statement
* s
)
4641 if (loop_break_labels
.empty())
4642 throw SEMANTIC_ERROR (_("cannot 'break' outside loop"), s
->tok
);
4644 record_actions(1, s
->tok
, true);
4645 o
->newline() << "goto " << loop_break_labels
.back() << ";";
4650 c_unparser::visit_continue_statement (continue_statement
* s
)
4652 if (loop_continue_labels
.empty())
4653 throw SEMANTIC_ERROR (_("cannot 'continue' outside loop"), s
->tok
);
4655 record_actions(1, s
->tok
, true);
4656 o
->newline() << "goto " << loop_continue_labels
.back() << ";";
4662 c_unparser::visit_literal_string (literal_string
* e
)
4664 interned_string v
= e
->value
;
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() << '\\' << '"';
4682 c_unparser::visit_literal_number (literal_number
* e
)
4684 // This looks ugly, but tries to be warning-free on 32- and 64-bit
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)";
4690 o
->line() << "((int64_t)" << e
->value
<< "LL)";
4695 c_unparser::visit_embedded_expr (embedded_expr
* e
)
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
++)
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
+ " */"))
4709 c_global_read_def(v
);
4710 read_defs
.push_back(v
);
4712 if (e
->tagged_p ("/* pragma:write:" + name
+ " */"))
4715 c_global_write_def(v
);
4716 write_defs
.push_back(v
);
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(); }), ";
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
<< "))";
4735 throw SEMANTIC_ERROR (_("expected numeric or string type"), e
->tok
);
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
);
4750 c_unparser::visit_binary_expression (binary_expression
* e
)
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
);
4765 e
->left
->visit (this);
4766 o
->line() << ") " << e
->op
<< " (";
4767 e
->right
->visit (this);
4770 else if (e
->op
== ">>" ||
4773 o
->line() << "((int64_t)(";
4774 e
->left
->visit (this);
4775 o
->line() << ") " << e
->op
<< " ((";
4776 e
->right
->visit (this);
4777 o
->line() << ") & 63))";
4779 else if (e
->op
== ">>>")
4781 o
->line() << "(int64_t)((uint64_t)(";
4782 e
->left
->visit (this);
4783 o
->line() << ") >> ((";
4784 e
->right
->visit (this);
4785 o
->line() << ") & 63))";
4787 else if (e
->op
== "/" ||
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
);
4798 c_assign (left
, e
->left
, "division");
4799 c_assign (right
, e
->right
, "division");
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
<< ");";
4810 o
->newline(-1) << "})";
4813 throw SEMANTIC_ERROR (_F("operator %s not yet implemented", string(e
->op
).c_str()), e
->tok
);
4818 c_unparser::visit_unary_expression (unary_expression
* e
)
4820 if (e
->type
!= pe_long
||
4821 e
->operand
->type
!= pe_long
)
4822 throw SEMANTIC_ERROR (_("expected numeric types"), e
->tok
);
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.
4833 o
->line() << "(int64_t)(0 " << e
->op
<< " (uint64_t)(";
4834 e
->operand
->visit (this);
4839 o
->line() << "(" << e
->op
<< " (";
4840 e
->operand
->visit (this);
4846 c_unparser::visit_logical_or_expr (logical_or_expr
* e
)
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
);
4854 e
->left
->visit (this);
4855 o
->line() << ") " << e
->op
<< " (";
4856 e
->right
->visit (this);
4862 c_unparser::visit_logical_and_expr (logical_and_expr
* e
)
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
);
4870 e
->left
->visit (this);
4871 o
->line() << ") " << e
->op
<< " (";
4872 e
->right
->visit (this);
4878 c_unparser::visit_array_in (array_in
* e
)
4882 classify_indexable (e
->operand
->base
, array
, hist
);
4886 stmt_expr
block(*this);
4888 tmpvar res
= gensym (pe_long
);
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
)
4900 if (!array_slice
) // checking for membership of a specific element
4902 load_map_indices (e
->operand
, idx
);
4903 // o->newline() << "c->last_stmt = " << lex_cast_qstring(*e->tok) << ";";
4905 mapvar mvar
= getmap (array
->referent
, e
->tok
);
4906 c_assign (res
, mvar
.exists(idx
), e
->tok
);
4908 o
->newline() << res
<< ";";
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
++)
4917 if (e
->operand
->indexes
[i
])
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
);
4924 array_slice_vars
.push_back(NULL
);
4927 mapvar mvar
= getmap (array
->referent
, e
->operand
->tok
);
4928 itervar iv
= getiter(array
);
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
)
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) << "}";
4944 string ctr
= lex_cast (label_counter
++);
4945 string toplabel
= "top_" + ctr
;
4946 string contlabel
= "continue_" + ctr
;
4947 string breaklabel
= "break_" + ctr
;
4949 o
->newline() << iv
<< " = " << iv
.start(mvar
) << ";";
4950 c_assign (res
, "0", e
->tok
); // set the default to 0
4952 o
->newline() << toplabel
<< ":";
4954 o
->newline(1) << "if (!(" << iv
<< "))";
4955 o
->newline(1) << "goto " << breaklabel
<< ";";
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
++)
4961 if (array_slice_vars
[i
] != NULL
)
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";
4971 throw SEMANTIC_ERROR (_("unexpected type"), e
->tok
);
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) << "}";
4981 // else, keep iterating
4982 o
->newline() << iv
<< " = " << iv
.next(mvar
) << ";";
4983 o
->newline() << "goto " << toplabel
<< ";";
4985 o
->newline(-1) << breaklabel
<< ":";
4986 o
->newline(1) << "; /* dummy statement */";
4987 o
->newline(-1) << res
<< ";";
4995 // 'foo in @hist_op(...)' is true iff
4996 // '@hist_op(...)[foo]' is nonzero
4998 // so we just delegate to the latter call, since int64_t is also
4999 // our boolean type.
5000 e
->operand
->visit(this);
5005 c_unparser::visit_regex_query (regex_query
* e
)
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) << ")";
5019 c_unparser::visit_compound_expression(compound_expression
* e
)
5022 e
->left
->visit (this);
5024 e
->right
->visit (this);
5029 c_unparser::visit_comparison (comparison
* e
)
5033 if (e
->left
->type
== pe_string
)
5035 if (e
->right
->type
!= pe_string
)
5036 throw SEMANTIC_ERROR (_("expected string types"), e
->tok
);
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.
5044 o
->line() << "(strncmp ((";
5045 e
->left
->visit (this);
5046 o
->line() << "), (";
5047 e
->right
->visit (this);
5048 o
->line() << "), MAXSTRINGLEN) " << e
->op
<< " 0)";
5050 else if (e
->left
->type
== pe_long
)
5052 if (e
->right
->type
!= pe_long
)
5053 throw SEMANTIC_ERROR (_("expected numeric types"), e
->tok
);
5056 e
->left
->visit (this);
5057 o
->line() << ") " << e
->op
<< " (";
5058 e
->right
->visit (this);
5062 throw SEMANTIC_ERROR (_("unexpected type"), e
->left
->tok
);
5069 c_unparser::visit_concatenation (concatenation
* e
)
5072 throw SEMANTIC_ERROR (_("unexpected concatenation operator"), e
->tok
);
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
);
5079 tmpvar t
= gensym (e
->type
);
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) << "})";
5092 c_unparser::visit_ternary_expression (ternary_expression
* e
)
5094 if (e
->cond
->type
!= pe_long
)
5095 throw SEMANTIC_ERROR (_("expected numeric condition"), e
->cond
->tok
);
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
);
5103 e
->cond
->visit (this);
5104 o
->line() << ") ? (";
5105 e
->truevalue
->visit (this);
5106 o
->line() << ") : (";
5107 e
->falsevalue
->visit (this);
5113 c_unparser::visit_assignment (assignment
* e
)
5117 if (e
->type
!= pe_long
)
5118 throw SEMANTIC_ERROR (_("non-number <<< expression"), e
->tok
);
5120 if (e
->left
->type
!= pe_stats
)
5121 throw SEMANTIC_ERROR (_("non-stats left operand to <<< expression"), e
->left
->tok
);
5123 if (e
->right
->type
!= pe_long
)
5124 throw SEMANTIC_ERROR (_("non-number right operand to <<< expression"), e
->right
->tok
);
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
);
5135 c_unparser_assignment
tav (this, e
->op
, e
->right
);
5136 e
->left
->visit (& tav
);
5141 c_unparser::visit_pre_crement (pre_crement
* e
)
5143 if (e
->type
!= pe_long
||
5144 e
->type
!= e
->operand
->type
)
5145 throw SEMANTIC_ERROR (_("expected numeric type"), e
->tok
);
5147 c_unparser_assignment
tav (this, e
->op
, false);
5148 e
->operand
->visit (& tav
);
5153 c_unparser::visit_post_crement (post_crement
* e
)
5155 if (e
->type
!= pe_long
||
5156 e
->type
!= e
->operand
->type
)
5157 throw SEMANTIC_ERROR (_("expected numeric type"), e
->tok
);
5159 c_unparser_assignment
tav (this, e
->op
, true);
5160 e
->operand
->visit (& tav
);
5165 c_unparser::visit_symbol (symbol
* e
)
5167 assert (e
->referent
!= 0);
5168 vardecl
* r
= e
->referent
;
5170 if (r
->index_types
.size() != 0)
5171 throw SEMANTIC_ERROR (_("invalid reference to array"), e
->tok
);
5173 var v
= getvar(r
, e
->tok
);
5178 c_unparser::visit_target_register (target_register
* e
)
5180 o
->line() << (e
->userspace_p
? "u_fetch_register(" : "k_fetch_register(")
5186 c_unparser::visit_target_deref (target_deref
* e
)
5193 o
->line() << "(int64_t)(int8_t)";
5196 o
->line() << "(int64_t)(int16_t)";
5199 o
->line() << "(int64_t)(int32_t)";
5207 o
->line() << (e
->userspace_p
? "uderef(" : "kderef(")
5208 << e
->size
<< ", (";
5209 e
->addr
->visit (this);
5214 c_unparser::visit_target_bitfield (target_bitfield
*)
5216 // These are all expanded much earlier.
5220 // Assignment expansion is tricky.
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).
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; })
5235 // (op is the plain operator portion of a combined calculate/assignment:
5236 // "+" for "+=", and so on. It is in the "macop" variable below.)
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.
5244 c_unparser_assignment::prepare_rvalue (interned_string op
,
5249 parent
->c_assign (rval
, rvalue
, "assignment");
5252 if (op
== "++" || op
== "--")
5253 // Here is part of the conversion proccess of turning "x++" to
5257 throw SEMANTIC_ERROR (_("need rvalue for assignment"), tok
);
5262 c_unparser_assignment::visit_symbol (symbol
*e
)
5264 stmt_expr
block(*parent
);
5265 translator_output
* o
= parent
->o
;
5267 assert (e
->referent
!= 0);
5268 if (e
->referent
->index_types
.size() != 0)
5269 throw SEMANTIC_ERROR (_("unexpected reference to array"), e
->tok
);
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
);
5276 prepare_rvalue (op
, rval
, e
->tok
);
5278 var lvar
= parent
->getvar (e
->referent
, e
->tok
);
5279 c_assignop (res
, lvar
, rval
, e
->tok
);
5281 o
->newline() << res
<< ";";
5285 c_unparser_assignment::visit_target_register (target_register
* e
)
5287 exp_type ty
= rvalue
? rvalue
->type
: e
->type
;
5288 assert(ty
== pe_long
);
5290 tmpvar rval
= parent
->gensym (pe_long
);
5291 prepare_rvalue (op
, rval
, e
->tok
);
5293 // Given how target_registers are created in loc2stap.cxx,
5294 // we should never see anything other than simple assignment.
5297 translator_output
* o
= parent
->o
;
5298 o
->newline() << (e
->userspace_p
? "u_store_register(" : "k_store_register(")
5299 << e
->regno
<< ", " << rval
<< ")";
5303 c_unparser_assignment::visit_target_deref (target_deref
* e
)
5305 exp_type ty
= rvalue
? rvalue
->type
: e
->type
;
5306 assert(ty
== pe_long
);
5308 tmpvar rval
= parent
->gensym (pe_long
);
5310 prepare_rvalue (op
, rval
, e
->tok
);
5312 // Given how target_deref are created in loc2stap.cxx,
5313 // we should never see anything other than simple assignment.
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
<< ")";
5324 c_unparser::visit_target_symbol (target_symbol
* e
)
5326 throw SEMANTIC_ERROR(_("cannot translate general target-symbol expression"), e
->tok
);
5331 c_unparser::visit_atvar_op (atvar_op
* e
)
5333 throw SEMANTIC_ERROR(_("cannot translate general @var expression"), e
->tok
);
5338 c_unparser::visit_cast_op (cast_op
* e
)
5340 throw SEMANTIC_ERROR(_("cannot translate general @cast expression"), e
->tok
);
5345 c_unparser::visit_autocast_op (autocast_op
* e
)
5347 throw SEMANTIC_ERROR(_("cannot translate general dereference expression"), e
->tok
);
5352 c_unparser::visit_defined_op (defined_op
* e
)
5354 throw SEMANTIC_ERROR(_("cannot translate general @defined expression"), e
->tok
);
5359 c_unparser::visit_entry_op (entry_op
* e
)
5361 throw SEMANTIC_ERROR(_("cannot translate general @entry expression"), e
->tok
);
5366 c_unparser::visit_perf_op (perf_op
* e
)
5368 throw SEMANTIC_ERROR(_("cannot translate general @perf expression"), e
->tok
);
5373 c_unparser::load_map_indices(arrayindex
*e
,
5374 vector
<tmpvar
> & idx
)
5378 classify_indexable (e
->base
, array
, hist
);
5384 assert (array
->referent
!= 0);
5385 vardecl
* r
= array
->referent
;
5387 if (r
->index_types
.size() == 0 ||
5388 r
->index_types
.size() != e
->indexes
.size())
5389 throw SEMANTIC_ERROR (_("invalid array reference"), e
->tok
);
5391 for (unsigned i
=0; i
<r
->index_types
.size(); i
++)
5393 if (r
->index_types
[i
] != e
->indexes
[i
]->type
)
5394 throw SEMANTIC_ERROR (_("array index type mismatch"), e
->indexes
[i
]->tok
);
5396 tmpvar ix
= gensym (r
->index_types
[i
]);
5397 c_assign (ix
, e
->indexes
[i
], "array index copy");
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");
5413 c_unparser::load_aggregate (expression
*e
, aggvar
& agg
)
5415 symbol
*sym
= get_symbol_within_expression (e
);
5417 if (sym
->referent
->type
!= pe_stats
)
5418 throw SEMANTIC_ERROR (_("unexpected aggregate of non-statistic"), sym
->tok
);
5421 if (sym
->referent
->arity
== 0)
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);";
5429 mapvar
*mv
= new mapvar(getmap(sym
->referent
, sym
->tok
));
5432 arrayindex
*arr
= NULL
;
5433 if (!expression_is_arrayindex (e
, arr
))
5434 throw SEMANTIC_ERROR(_("unexpected aggregate of non-arrayindex"), e
->tok
);
5436 // If we have a foreach_loop value, we don't need to index the map
5438 if (get_foreach_loop_value(arr
, agg_value
))
5439 o
->newline() << agg
<< " = " << agg_value
<< ";";
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
) << ";";
5455 c_unparser::histogram_index_check(var
& base
, tmpvar
& idx
) const
5457 return "((" + idx
.value() + " >= 0)"
5458 + " && (" + idx
.value() + " < " + base
.buckets() + "))";
5463 c_unparser::visit_arrayindex (arrayindex
* e
)
5465 // If we have a foreach_loop value, use it and call it a day!
5467 if (get_foreach_loop_value(e
, ai_value
))
5469 o
->line() << ai_value
;
5475 classify_indexable (e
->base
, array
, hist
);
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
);
5483 stmt_expr
block(*this);
5486 load_map_indices (e
, idx
);
5487 tmpvar res
= gensym (e
->type
);
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
);
5493 o
->newline() << res
<< ";";
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:
5504 // foo[getpid(), geteuid()] <<< 1
5506 // print @log_hist(foo[pid, euid])[bucket]
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.
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.
5523 stmt_expr
block(*this);
5525 aggvar agg
= gensym_aggregate ();
5528 load_map_indices (e
, idx
);
5529 tmpvar res
= gensym (e
->type
);
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
);
5535 var
*v
= load_aggregate(hist
->stat
, agg
);
5536 v
->assert_hist_compatible(*hist
);
5538 o
->newline() << "c->last_stmt = " << lex_cast_qstring(*e
->tok
) << ";";
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) << "}";
5555 o
->newline(-1) << "}";
5556 o
->newline() << res
<< ";";
5564 c_unparser_assignment::visit_arrayindex (arrayindex
*e
)
5568 classify_indexable (e
->base
, array
, hist
);
5573 stmt_expr
block(*parent
);
5575 translator_output
*o
= parent
->o
;
5577 if (array
->referent
->index_types
.size() == 0)
5578 throw SEMANTIC_ERROR (_("unexpected reference to scalar"), e
->tok
);
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
);
5587 // NB: because these expressions are nestable, emit this construct
5589 // ({ tmp0=(idx0); ... tmpN=(idxN); rvar=(rhs); lvar; res;
5591 // lvar = get (array,idx0...N); // if necessary
5592 // assignop (res, lvar, rvar);
5593 // set (array, idx0...N, lvar);
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
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:
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);
5611 // To simplify variable-allocation rules, we assign rvar to lvar and
5612 // res in this block as well, even though they are technically
5615 prepare_rvalue (op
, rvar
, e
->tok
);
5619 assert (e
->type
== pe_stats
);
5620 assert (rvalue
->type
== pe_long
);
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
) << ";";
5626 // no need for these dummy assignments
5627 // o->newline() << lvar << " = " << rvar << ";";
5628 // o->newline() << res << " = " << rvar << ";";
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
) << ";";
5640 o
->newline() << res
<< ";";
5644 throw SEMANTIC_ERROR(_("cannot assign to histogram buckets"), e
->tok
);
5650 c_unparser::visit_functioncall (functioncall
* e
)
5652 assert (!e
->referents
.empty());
5654 stmt_expr
block(*this);
5656 vector
<bool> cp_arg(e
->referents
.size(), true);
5657 for (unsigned fd
= 0; fd
< e
->referents
.size(); fd
++)
5659 functiondecl
* r
= e
->referents
[fd
];
5661 if (r
->formal_args
.size() != e
->args
.size())
5662 throw SEMANTIC_ERROR (_("invalid length argument list"), e
->tok
);
5664 for (unsigned i
= 0; i
< e
->args
.size(); i
++)
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
);
5671 // all alternative functions must be compatible if passing by
5673 for (unsigned i
= 0; i
< r
->formal_args
.size(); i
++)
5675 if (!r
->formal_args
[i
]->char_ptr_arg
)
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)))
5684 // compute actual arguments
5686 for (unsigned i
=0; i
<e
->args
.size(); i
++)
5688 tmpvar t
= gensym(e
->args
[i
]->type
);
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());
5695 c_assign (t
, e
->args
[i
],
5696 _("function actual argument evaluation"));
5700 // overloading execution logic for functioncall:
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
5708 // - copy in computed function arguments for overload_n
5709 // - make the functioncall for overload_n and overwrite return variable
5711 // - yield return value
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
);
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.)
5721 bool yield
= false; // set if statement expression is non void
5723 for (unsigned fd
= 0; fd
< e
->referents
.size(); fd
++)
5725 functiondecl
* r
= e
->referents
[fd
];
5727 // copy in actual arguments
5728 for (unsigned i
=0; i
<e
->args
.size(); i
++)
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() << ";";
5735 c_assign ("c->locals[c->nesting+1]." +
5736 c_funcname (r
->name
) + "." +
5737 c_localname (r
->formal_args
[i
]->name
),
5740 "function actual argument copy",
5743 // optimized string returns need a local storage pointer.
5744 bool pointer_ret
= (e
->type
== pe_string
&& !session
->unoptimized
);
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];";
5754 o
->newline() << c_funcname (r
->name
) << " (c);";
5755 o
->newline() << "if (unlikely(c->last_error || c->aborted)) goto out;";
5757 if (!already_checked_action_count
&& !session
->suppress_time_limits
5758 && !session
->unoptimized
)
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);
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
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
);
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
<< ";";
5791 if (e
->referents
.size() > 1)
5793 // end label and increment counter
5794 o
->newline() << "fc_end_" << fc_counter
++ << ":";
5797 if (e
->referents
.back()->has_next
)
5798 // check for aborted return from function; this could happen from non-overloaded ones too
5800 << "if (unlikely(c->next)) { "
5801 << "c->last_stmt = " << lex_cast_qstring(*e
->tok
) << "; "
5802 << "c->last_error = \"all functions exhausted\"; goto out; }";
5804 // return result from retvalue slot NB: this must be last, for the
5805 // enclosing statement-expression ({ ... }) to carry this value.
5807 o
->newline() << tmp_ret
.value() << ";";
5809 o
->newline() << "(void) 0;";
5813 // returns true if it should print directly to a stream
5815 preprocess_print_format(print_format
* e
, vector
<tmpvar
>& tmp
,
5816 vector
<print_format::format_component
>& components
,
5817 string
& format_string
)
5819 if (e
->print_with_format
)
5821 format_string
= e
->raw_components
;
5822 components
= e
->components
;
5827 if (e
->print_with_delim
)
5829 stringstream escaped_delim
;
5830 interned_string dstr
= e
->delimiter
;
5831 for (interned_string::const_iterator i
= dstr
.begin();
5832 i
!= dstr
.end(); ++i
)
5835 escaped_delim
<< '%';
5836 escaped_delim
<< *i
;
5838 delim
= escaped_delim
.str();
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
)
5847 if (i
> 0 && e
->print_with_delim
)
5849 switch (e
->args
[i
]->type
)
5853 throw SEMANTIC_ERROR(_("cannot print unknown expression type"), e
->args
[i
]->tok
);
5855 throw SEMANTIC_ERROR(_("cannot print a raw stats object"), e
->args
[i
]->tok
);
5864 if (e
->print_with_newline
)
5867 format_string
= format
.str();
5868 components
= print_format::string_to_components(format_string
);
5872 // optimize simple string prints
5873 if (e
->print_to_stream
&& tmp
.size() <= 1
5874 && format_string
.find("%%") == string::npos
)
5876 // just a plain format string itself, or
5877 // simply formatting a string verbatim.
5878 if (tmp
.empty() || format_string
== "%s")
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")
5886 tmp
[0].override(tmp
[0].value() + "\"\\n\"");
5896 c_unparser::visit_print_format (print_format
* e
)
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.
5904 stmt_expr
block(*this);
5905 aggvar agg
= gensym_aggregate ();
5907 var
*v
= load_aggregate(e
->hist
->stat
, agg
);
5908 v
->assert_hist_compatible(*e
->hist
);
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
)
5921 o
->newline(1) << "_stp_stat_print_histogram (" << v
->hist() << ", " << agg
.value() << ");";
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() << ";";
5937 stmt_expr
block(*this);
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
);
5945 // Compute actual arguments
5948 for (unsigned i
=0; i
<e
->args
.size(); i
++)
5950 tmpvar t
= gensym(e
->args
[i
]->type
);
5951 c_assign (t
, e
->args
[i
],
5952 "print format actual argument evaluation");
5956 // Allocate the result
5957 exp_type ty
= e
->print_to_stream
? pe_long
: pe_string
;
5958 tmpvar res
= gensym (ty
);
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
);
5966 // Make the [s]printf call...
5968 // Generate code to check that any pointer arguments are actually accessible.
5970 for (unsigned i
= 0; i
< components
.size(); ++i
) {
5971 if (components
[i
].type
== print_format::conv_literal
)
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
)
5981 (void) width_ix
; /* XXX: notused */
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
)
5988 const token
* prec_tok
= e
->tok
;
5991 mem_size
= tmp
[prec_ix
].value();
5992 prec_tok
= e
->args
[prec_ix
]->tok
;
5994 else if (components
[i
].prectype
== print_format::prec_static
&&
5995 components
[i
].precision
> 0)
5996 mem_size
= lex_cast(components
[i
].precision
) + "LL";
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) << "}";
6014 // Shortcuts for cases that aren't formatted at all
6015 if (e
->print_to_stream
)
6019 o
->newline() << "_stp_print_char (";
6021 o
->line() << tmp
[0].value() << ");";
6023 o
->line() << '"' << format_string_out
<< "\");";
6028 o
->newline() << "_stp_print (";
6030 o
->line() << tmp
[0].value() << ");";
6032 o
->line() << '"' << format_string_out
<< "\");";
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";
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
6051 res
.override("((int64_t)0LL)");
6053 o
->newline() << "c->printf_locals." << compiled_printf
6054 << ".__retvalue = " << res
.value() << ";";
6055 o
->newline() << compiled_printf
<< " (c);";
6057 o
->newline(-1) << "#else // STP_LEGACY_PRINT";
6060 // Generate the legacy call that goes through _stp_vsnprintf.
6061 if (e
->print_to_stream
)
6062 o
->newline() << "_stp_printf (";
6064 o
->newline() << "_stp_snprintf (" << res
.value() << ", MAXSTRINGLEN, ";
6065 o
->line() << '"' << format_string_out
<< '"';
6067 // Make sure arguments match the expected type of the format specifier.
6069 for (unsigned i
= 0; i
< components
.size(); ++i
)
6071 if (components
[i
].type
== print_format::conv_literal
)
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();
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();
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() << ";";
6098 c_unparser::visit_stat_op (stat_op
* e
)
6100 // Stat ops can be *applied* to two types of expression:
6102 // 1. An arrayindex expression on a pe_stats-valued array.
6104 // 2. A symbol of type pe_stats.
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.
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.
6115 stmt_expr
block(*this);
6116 aggvar agg
= gensym_aggregate ();
6117 tmpvar res
= gensym (pe_long
);
6118 var
*v
= load_aggregate(e
->stat
, agg
);
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))
6125 o
->newline() << "if (unlikely (" << agg
.value() << " == NULL))";
6127 c_assign(res
, "0", e
->tok
);
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) << "}";
6140 o
->newline() << "else";
6145 c_assign(res
, ("_stp_div64(NULL, " + agg
.value() + "->sum, "
6146 + agg
.value() + "->count)"),
6150 c_assign(res
, agg
.value() + "->count", e
->tok
);
6153 c_assign(res
, agg
.value() + "->sum", e
->tok
);
6156 c_assign(res
, agg
.value() + "->min", e
->tok
);
6159 c_assign(res
, agg
.value() + "->max", e
->tok
);
6162 c_assign(res
, agg
.value() + "->variance", e
->tok
);
6165 assert (0); // should not happen, as sc_none is only used in foreach sorts
6169 o
->newline() << res
<< ";";
6176 c_unparser::visit_hist_op (hist_op
*)
6178 // Hist ops can only occur in a limited set of circumstances:
6180 // 1. Inside an arrayindex expression, as the base referent. See
6181 // c_unparser::visit_arrayindex for handling of this case.
6183 // 2. Inside a foreach statement, as the base referent. See
6184 // c_unparser::visit_foreach_loop for handling this case.
6186 // 3. Inside a print_format expression, as the sole argument. See
6187 // c_unparser::visit_print_format for handling this case.
6189 // Note that none of these cases involves the c_unparser ever
6190 // visiting this node. We should not get here.
6196 typedef map
<Dwarf_Addr
,const char*> addrmap_t
; // NB: plain map, sorted by address
6198 struct unwindsym_dump_context
6200 systemtap_session
& session
;
6202 unsigned stp_module_index
;
6205 unsigned char *build_id_bits
;
6206 GElf_Addr build_id_vaddr
;
6208 unsigned long stp_kretprobe_trampoline_addr
;
6209 Dwarf_Addr stext_offset
;
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
6217 void *debug_frame_hdr
;
6218 size_t debug_frame_hdr_len
;
6219 Dwarf_Addr debug_frame_off
;
6223 size_t eh_frame_hdr_len
;
6225 Dwarf_Addr eh_frame_hdr_addr
;
6227 size_t debug_line_len
;
6229 set
<string
> undone_unwindsym_modules
;
6232 static bool need_byte_swap_for_target (const unsigned char e_ident
[])
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
);
6239 #error Bad host __BYTE_ORDER
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
,
6251 *debug_frame_hdr
= NULL
;
6252 *debug_frame_hdr_len
= 0;
6255 set
< pair
<Dwarf_Addr
, Dwarf_Off
> > fdes
;
6256 set
< pair
<Dwarf_Addr
, Dwarf_Off
> >::iterator it
;
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))
6270 Dwarf_CFI_Entry entry
;
6275 res
= dwarf_next_cfi (e_ident
, debug_frame
, false, off
, &next_off
,
6279 if (entry
.CIE_id
== DW_CIE_ID_64
)
6280 cies
++; // We can just ignore the CIEs.
6285 addr
= target_to_host_32((*((uint32_t *) entry
.fde
.start
)));
6287 addr
= target_to_host_64((*((uint64_t *) entry
.fde
.start
)));
6288 fdes
.insert(pair
<Dwarf_Addr
, Dwarf_Off
>(addr
, off
));
6292 ; // Great, all done.
6295 // Warn, but continue, backtracing will be slow...
6296 if (session
.verbose
> 2 && ! session
.suppress_warnings
)
6298 const char *modname
= dwfl_module_info (mod
, NULL
,
6301 session
.print_warning("Problem creating debug frame hdr for "
6302 + lex_cast_qstring(modname
)
6303 + ", " + dwarf_errmsg (-1));
6310 if (fdes
.size() > 0)
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",
6317 *debug_frame_off
= (*it
).first
- first_addr
;
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
;
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
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
++)
6336 *table
++ = host_to_target_32 ((*it
).first
);
6337 *table
++ = host_to_target_32 ((*it
).second
);
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
++)
6347 *table
++ = host_to_target_64 ((*it
).first
);
6348 *table
++ = host_to_target_64 ((*it
).second
);
6353 static set
<string
> vdso_paths
;
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
)
6370 Dwarf_Addr start
, bias
= 0;
6371 GElf_Ehdr
*ehdr
, ehdr_mem
;
6372 GElf_Shdr
*shdr
, shdr_mem
;
6374 Elf_Data
*data
= NULL
;
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
);
6383 bool eh_frame_seen
= false;
6384 bool eh_frame_hdr_seen
= false;
6385 while ((scn
= elf_nextscn(elf
, scn
)))
6387 shdr
= gelf_getshdr(scn
, &shdr_mem
);
6388 const char* scn_name
= elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
);
6390 && strcmp(scn_name
, ".eh_frame") == 0
6391 && shdr
->sh_type
== SHT_PROGBITS
)
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
;
6401 *eh_addr
= shdr
->sh_addr
;
6402 eh_frame_seen
= true;
6404 else if (!eh_frame_hdr_seen
6405 && strcmp(scn_name
, ".eh_frame_hdr") == 0
6406 && shdr
->sh_type
== SHT_PROGBITS
)
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
;
6414 *eh_frame_hdr_addr
= shdr
->sh_addr
;
6415 eh_frame_hdr_seen
= true;
6417 if (eh_frame_seen
&& eh_frame_hdr_seen
)
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
);
6426 while ((scn
= elf_nextscn(elf
, scn
)))
6428 shdr
= gelf_getshdr(scn
, &shdr_mem
);
6429 if (strcmp(elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
),
6430 ".debug_frame") == 0)
6432 data
= elf_rawdata(scn
, NULL
);
6433 *debug_frame
= data
->d_buf
;
6434 *debug_len
= data
->d_size
;
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
);
6446 dump_build_id (Dwfl_Module
*m
,
6447 unwindsym_dump_context
*c
,
6448 const char *name
, Dwarf_Addr
)
6450 string modname
= name
;
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
;
6457 if ((build_id_len
=dwfl_module_build_id(m
,
6458 (const unsigned char **)&build_id_bits
,
6459 &build_id_vaddr
)) > 0)
6461 if (modname
!= "kernel")
6463 Dwarf_Addr reloc_vaddr
= build_id_vaddr
;
6464 const char *secname
;
6467 i
= dwfl_module_relocate_address (m
, &reloc_vaddr
);
6468 DWFL_ASSERT ("dwfl_module_relocate_address reloc_vaddr", i
>= 0);
6470 secname
= dwfl_module_relocation_info (m
, i
, NULL
);
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.
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"));
6485 build_id_vaddr
= reloc_vaddr
;
6488 if (c
->session
.verbose
> 1)
6490 clog
<< _F("Found build-id in %s, length %d, start at %#" PRIx64
,
6491 name
, build_id_len
, build_id_vaddr
) << endl
;
6494 c
->build_id_len
= build_id_len
;
6495 c
->build_id_vaddr
= build_id_vaddr
;
6496 c
->build_id_bits
= build_id_bits
;
6503 dump_section_list (Dwfl_Module
*m
,
6504 unwindsym_dump_context
*c
,
6505 const char *name
, Dwarf_Addr
)
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.
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"
6517 string modname
= name
;
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
);
6524 // Look up the relocation basis for symbols
6525 int n
= dwfl_module_relocations (m
);
6526 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
6530 // ET_EXEC, no relocations.
6531 string secname
= ".absolute";
6532 unsigned size
= end
- start
;
6533 c
->seclist
.push_back (make_pair (secname
, size
));
6538 // kernel or shared library (ET_DYN).
6540 secname
= (modname
== "kernel") ? "_stext" : ".dynamic";
6541 unsigned size
= end
- start
;
6542 c
->seclist
.push_back (make_pair (secname
, size
));
6547 // ET_REL, kernel module.
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
)))
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
))
6563 size
= shdr
->sh_size
;
6564 const char* scn_name
= elf_strptr(elf
, ehdr
->e_shstrndx
,
6567 c
->seclist
.push_back (make_pair (secname
, size
));
6574 // Impossible... dflw_assert above will have triggered.
6575 return DWARF_CB_ABORT
;
6578 static void find_debug_frame_offset (Dwfl_Module
*m
,
6579 unwindsym_dump_context
*c
)
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
;
6588 dwfl_module_info (m
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
);
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
);
6595 while ((scn
= elf_nextscn(elf
, scn
)))
6597 shdr
= gelf_getshdr(scn
, &shdr_mem
);
6598 if (strcmp(elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
),
6599 ".debug_frame") == 0)
6601 data
= elf_rawdata(scn
, NULL
);
6606 if (!data
) // need this check since dwarf_next_cfi() doesn't do it
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;
6615 Dwarf_CFI_Entry entry
;
6620 res
= dwarf_next_cfi (ehdr
->e_ident
, data
, false, off
, &next_off
, &entry
);
6623 if (entry
.CIE_id
!= DW_CIE_ID_64
) // ignore CIEs
6627 addr
= (*((uint32_t *) entry
.fde
.start
));
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",
6634 c
->debug_frame_off
= addr
- first_addr
;
6644 dump_line_tables_check (void *data
, size_t data_len
)
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;
6651 while (ptr
< ((uint8_t *)data
+ data_len
))
6653 if (ptr
+ 4 > (uint8_t *)data
+ data_len
)
6654 return DWARF_CB_ABORT
;
6656 unit_length
= *((uint32_t *) ptr
);
6658 if (unit_length
== 0xffffffff)
6660 if (ptr
+ 8 > (uint8_t *)data
+ data_len
)
6661 return DWARF_CB_ABORT
;
6663 unit_length
= *((uint64_t *) ptr
);
6667 if ((ptr
+ unit_length
> (uint8_t *)data
+ data_len
) || unit_length
<= 2)
6668 return DWARF_CB_ABORT
;
6670 endunitptr
= ptr
+ unit_length
;
6672 version
= *((uint16_t *)ptr
);
6675 if (unit_length
<= (2 + length
))
6676 return DWARF_CB_ABORT
;
6680 header_length
= *((uint32_t *) ptr
);
6685 header_length
= *((uint64_t *) ptr
);
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
;
6694 // skip past min instr length, max ops per instr, and line base
6700 // check that the line range is not 0
6702 return DWARF_CB_ABORT
;
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
;
6710 // the initial checks stop here, before the directory table
6717 dump_line_tables (Dwfl_Module
*m
, unwindsym_dump_context
*c
,
6718 const char *, Dwarf_Addr
)
6721 Elf_Scn
* scn
= NULL
;
6723 GElf_Ehdr
*ehdr
, ehdr_mem
;
6724 GElf_Shdr
* shdr
, shdr_mem
;
6725 Dwarf_Addr bias
, start
;
6727 dwfl_module_info (m
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
);
6729 elf
= dwfl_module_getelf (m
, &bias
);
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
)))
6739 shdr
= gelf_getshdr(scn
, &shdr_mem
);
6740 if (strcmp(elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
),
6741 ".debug_line") == 0)
6743 data
= elf_rawdata(scn
, NULL
);
6744 if (dump_line_tables_check(data
->d_buf
, data
->d_size
) == DWARF_CB_ABORT
)
6746 c
->debug_line
= data
->d_buf
;
6747 c
->debug_line_len
= data
->d_size
;
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
);
6758 /* Some architectures create special local symbols that are not
6761 skippable_arch_symbol (GElf_Half e_machine
, const char *name
, GElf_Sym
*sym
)
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")))
6775 dump_symbol_tables (Dwfl_Module
*m
,
6776 unwindsym_dump_context
*c
,
6777 const char *modname
, Dwarf_Addr base
)
6779 // Use end as sanity check when resolving symbol addresses.
6781 dwfl_module_info (m
, NULL
, NULL
, &end
, NULL
, NULL
, NULL
, NULL
);
6783 int syments
= dwfl_module_getsymtab(m
);
6784 DWFL_ASSERT (_F("Getting symbol table for %s", modname
), syments
>= 0);
6786 // Look up the relocation basis for symbols
6787 int n
= dwfl_module_relocations (m
);
6788 DWFL_ASSERT ("dwfl_module_relocations", n
>= 0);
6790 /* Needed on ppc64, for function descriptors. */
6791 Dwarf_Addr elf_bias
;
6792 GElf_Ehdr
*ehdr
, ehdr_mem
;
6794 elf
= dwfl_module_getelf(m
, &elf_bias
);
6795 ehdr
= gelf_getehdr(elf
, &ehdr_mem
);
6797 // XXX: unfortunate duplication with tapsets.cxx:emit_address()
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");
6804 /* Set to bail early if we are just examining the kernel
6805 and don't need anything more. */
6807 for (int i
= 0; i
< syments
&& !done
; ++i
)
6809 if (pending_interrupts
)
6810 return DWARF_CB_ABORT
;
6815 const char *name
= dwfl_module_getsym(m
, i
, &sym
, &shndxp
);
6818 Dwarf_Addr sym_addr
= sym
.st_value
;
6820 // We always need two special values from the kernel.
6821 // _stext for extra_offset and kretprobe_trampoline_holder
6822 // for the unwinder.
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
))
6833 extra_offset
= sym_addr
;
6834 ki
= dwfl_module_relocate_address (m
, &extra_offset
);
6835 DWFL_ASSERT ("dwfl_module_relocate_address extra_offset",
6838 if (c
->session
.verbose
> 2)
6839 clog
<< _F("Found kernel _stext extra offset %#" PRIx64
,
6840 extra_offset
) << endl
;
6842 if (! c
->session
.need_symbols
6843 && (kretprobe_trampoline_addr
!= (unsigned long) -1
6844 || ! c
->session
.need_unwind
))
6847 else if (kretprobe_trampoline_addr
== (unsigned long) -1
6848 && c
->session
.need_unwind
6849 && ! strcmp(name
, "kretprobe_trampoline_holder"))
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);
6857 if (! c
->session
.need_symbols
6858 && extra_offset
!= 0)
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.
6878 const char *secname
= NULL
;
6879 unsigned secidx
= 0; /* Most things have just one section. */
6880 Dwarf_Addr func_desc_addr
= 0; /* Function descriptor */
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
6886 if (ehdr
->e_machine
== EM_PPC64
6887 && GELF_ST_TYPE (sym
.st_info
) == STT_FUNC
6888 && ehdr
->e_type
!= ET_REL
)
6890 Elf64_Addr opd_addr
;
6891 Dwarf_Addr opd_bias
;
6894 func_desc_addr
= sym_addr
;
6896 opd
= dwfl_module_address_section (m
, &sym_addr
, &opd_bias
);
6897 DWFL_ASSERT ("dwfl_module_address_section opd", opd
!= NULL
);
6899 Elf_Data
*opd_data
= elf_rawdata (opd
, NULL
);
6900 assert(opd_data
!= NULL
);
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");
6911 // So the real address of the function is...
6912 sym_addr
= opd_addr
+ opd_bias
;
6915 if (n
> 0) // only try to relocate if there exist relocation bases
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
);
6921 if (func_desc_addr
!= 0)
6922 dwfl_module_relocate_address (m
, &func_desc_addr
);
6925 if (n
== 1 && is_kernel
)
6927 // This is a symbol within a (possibly relocatable)
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
)
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.
6946 assert (secname
!= NULL
);
6947 // secname adequately set
6949 // NB: it may be an empty string for ET_DYN objects
6950 // like shared libraries, as their relocation base
6952 if (secname
[0] == '\0')
6953 secname
= ".dynamic";
6956 // Compute our section number
6957 for (secidx
= 0; secidx
< c
->seclist
.size(); secidx
++)
6958 if (c
->seclist
[secidx
].first
==secname
)
6961 if (secidx
== c
->seclist
.size()) // PR23747 not an error
6963 continue; // way back to the next symbol
6970 // sym_addr is absolute, as it must be since there are
6971 // no relocation bases
6972 secname
= ".absolute"; // sentinel
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
;
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
6997 dump_unwind_tables (Dwfl_Module
*m
,
6998 unwindsym_dump_context
*c
,
6999 const char *, Dwarf_Addr
)
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
,
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
)
7017 if (data
== NULL
|| len
== 0)
7020 if (len
> MAX_UNWIND_TABLE_SIZE
)
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
));
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
));
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";
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";
7045 for (size_t i
= 0; i
< len
; i
++)
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" << " ";
7053 if (table
== "debug_line")
7054 output
<< "#endif /* STP_NEED_LINE_DATA */\n";
7056 output
<< "#endif /* STP_USE_DWARF_UNWINDER && STP_NEED_UNWIND_DATA */\n";
7060 dump_unwindsym_cxt (Dwfl_Module
*m
,
7061 unwindsym_dump_context
*c
,
7062 const char *name
, Dwarf_Addr base
)
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
;
7080 dump_unwindsym_cxt_table(c
->session
, c
->output
, modname
, stpmod_idx
, "", 0,
7081 "debug_frame", debug_frame
, debug_len
);
7083 dump_unwindsym_cxt_table(c
->session
, c
->output
, modname
, stpmod_idx
, "", 0,
7084 "eh_frame", eh_frame
, eh_len
);
7086 dump_unwindsym_cxt_table(c
->session
, c
->output
, modname
, stpmod_idx
, "", 0,
7087 "eh_frame_hdr", eh_frame_hdr
, eh_frame_hdr_len
);
7089 dump_unwindsym_cxt_table(c
->session
, c
->output
, modname
, stpmod_idx
, "", 0,
7090 "debug_line", debug_line
, debug_line_len
);
7092 if (c
->session
.need_unwind
&& debug_frame
== NULL
&& eh_frame
== NULL
)
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));
7103 if (c
->session
.need_lines
&& debug_line
== NULL
)
7105 if (c
->session
.verbose
> 2)
7106 c
->session
.print_warning ("No debug line data for " + modname
+ ", " +
7110 for (unsigned secidx
= 0; secidx
< c
->seclist
.size(); secidx
++)
7112 c
->output
<< "static struct _stp_symbol "
7113 << "_stp_module_" << stpmod_idx
<< "_symbols_" << secidx
<< "[] = {\n";
7115 string secname
= c
->seclist
[secidx
].first
;
7116 Dwarf_Addr extra_offset
;
7117 extra_offset
= (secname
== "_stext") ? c
->stext_offset
: 0;
7119 // Only include symbols if they will be used
7120 if (c
->session
.need_symbols
)
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
++)
7127 // skip symbols that occur before our chosen base address
7128 if (it
->first
< extra_offset
)
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.
7139 c
->output
<< "};\n";
7141 /* For now output debug_frame index only in "magic" sections. */
7142 if (secname
== ".dynamic" || secname
== ".absolute"
7143 || secname
== ".text" || secname
== "_stext")
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
);
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
++)
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";
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"))
7168 c
->output
<< "#if defined(STP_USE_DWARF_UNWINDER)"
7169 << " && defined(STP_NEED_UNWIND_DATA)\n";
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";
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";
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";
7191 c
->output
<< ".debug_hdr = NULL,\n";
7192 c
->output
<< ".debug_hdr_len = 0,\n";
7193 if (c
->session
.need_lines
&& secname
== ".text")
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";
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";
7207 c
->output
<< "},\n";
7209 c
->output
<< "};\n";
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
);
7217 // For user space modules store canonical path.
7218 // For kernel modules just the name itself.
7219 string mainpath
= resolve_path(mainfile
);
7221 if (is_user_module(modname
)) // userspace
7222 mainname
= lex_cast_qstring (path_remove_sysroot(c
->session
,mainpath
));
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
));
7232 mainname
= lex_cast_qstring (modname
);
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
7242 if (debug_frame
!= NULL
)
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";
7251 c
->output
<< ".debug_frame = NULL,\n";
7252 c
->output
<< ".debug_frame_len = 0,\n";
7254 if (debug_frame
!= NULL
)
7255 c
->output
<< "#endif /* STP_USE_DWARF_UNWINDER && STP_NEED_UNWIND_DATA*/\n";
7257 if (eh_frame
!= NULL
)
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";
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";
7271 c
->output
<< ".unwind_hdr = NULL,\n";
7272 c
->output
<< ".unwind_hdr_len = 0,\n";
7274 c
->output
<< "#else\n";
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";
7284 if (debug_line
!= NULL
)
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";
7293 c
->output
<< ".debug_line = NULL,\n";
7294 c
->output
<< ".debug_line_len = 0,\n";
7296 if (debug_line
!= NULL
)
7297 c
->output
<< "#endif /* STP_NEED_LINE_DATA */\n";
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";
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.
7309 * http://sourceware.org/ml/systemtap/2009-q4/msg00574.html
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
;
7318 c
->output
<< "\",\n";
7319 c
->output
<< ".build_id_len = " << c
->build_id_len
<< ",\n";
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
)
7329 // ET_DYN: task finder gives the load address. ET_EXEC: this is absolute address
7331 c
->output
<< ".build_id_offset = 0x" << hex
7332 << c
->build_id_vaddr
/* - base */
7335 c
->output
<< ".build_id_len = 0,\n";
7337 //initialize the note section representing unloaded
7338 c
->output
<< ".notes_sect = 0,\n";
7340 c
->output
<< "};\n\n";
7342 c
->undone_unwindsym_modules
.erase (modname
);
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
);
7351 static void dump_kallsyms(unwindsym_dump_context
*c
)
7353 ifstream
kallsyms("/proc/kallsyms");
7354 unsigned stpmod_idx
= c
->stp_module_index
;
7357 Dwarf_Addr start
= 0;
7359 Dwarf_Addr prev
= 0;
7361 c
->output
<< "static struct _stp_symbol "
7362 << "_stp_module_" << stpmod_idx
<< "_symbols_" << 0 << "[] = {\n";
7364 while (getline(kallsyms
, line
))
7370 istringstream
iss(line
);
7372 iss
>> hex
>> addr
>> type
>> name
>> module
;
7374 if (name
== KERNEL_RELOC_SYMBOL
)
7376 else if (name
== "_end" || module
!= "")
7382 if (!start
|| addr
== 0 || prev
== addr
)
7385 c
->output
<< " { 0x" << hex
<< addr
- start
<< dec
7386 << ", " << lex_cast_qstring(name
) << " },\n";
7392 c
->output
<< "};\n";
7393 c
->output
<< "static struct _stp_section _stp_module_" << stpmod_idx
<< "_sections[] = {\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";
7408 c
->undone_unwindsym_modules
.erase("kernel");
7409 c
->stp_module_index
++;
7413 dump_unwindsyms (Dwfl_Module
*m
,
7414 void **userdata
__attribute__ ((unused
)),
7419 if (pending_interrupts
)
7420 return DWARF_CB_ABORT
;
7422 unwindsym_dump_context
*c
= (unwindsym_dump_context
*) arg
;
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())
7431 if (c
->session
.verbose
> 1)
7432 clog
<< "dump_unwindsyms " << name
7433 << " index=" << c
->stp_module_index
7434 << " base=0x" << hex
<< base
<< dec
<< endl
;
7436 // We want to extract several bits of information:
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
7443 int res
= DWARF_CB_OK
;
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
);
7451 if (res
== DWARF_CB_OK
)
7452 res
= dump_section_list(m
, c
, name
, base
);
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.
7458 if (res
== DWARF_CB_OK
7459 && (c
->session
.need_symbols
|| ! strcmp(name
, "kernel")))
7460 res
= dump_symbol_tables (m
, c
, name
, base
);
7462 c
->debug_frame
= NULL
;
7464 c
->debug_frame_hdr
= NULL
;
7465 c
->debug_frame_hdr_len
= 0;
7466 c
->debug_frame_off
= 0;
7468 c
->eh_frame_hdr
= NULL
;
7470 c
->eh_frame_hdr_len
= 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
);
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
);
7483 /* And finally dump everything collected in the output. */
7484 if (res
== DWARF_CB_OK
)
7485 res
= dump_unwindsym_cxt (m
, c
, name
, base
);
7487 if (res
== DWARF_CB_OK
)
7488 c
->stp_module_index
++;
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
&);
7500 add_unwindsym_iol_callback (set
<string
> *added
, const char *data
)
7502 added
->insert (string (data
));
7507 query_module (Dwfl_Module
*mod
,
7511 struct dwflpp
*dwflpp
)
7513 dwflpp
->focus_on_module(mod
, NULL
);
7519 add_unwindsym_ldd (systemtap_session
&s
)
7521 std::set
<std::string
> added
;
7523 for (std::set
<std::string
>::iterator it
= s
.unwindsym_modules
.begin();
7524 it
!= s
.unwindsym_modules
.end();
7527 string modname
= *it
;
7528 assert (modname
.length() != 0);
7529 if (! is_user_module (modname
)) continue;
7531 dwflpp
mod_dwflpp (s
, modname
, false);
7532 mod_dwflpp
.iterate_over_modules(&query_module
, &mod_dwflpp
);
7533 if (mod_dwflpp
.module
) // existing binary
7535 assert (mod_dwflpp
.module_name
!= "");
7536 mod_dwflpp
.iterate_over_libraries (&add_unwindsym_iol_callback
, &added
);
7540 s
.unwindsym_modules
.insert (added
.begin(), added
.end());
7543 static int find_vdso(const char *path
, const struct stat
*, int type
)
7547 /* Assume that if the path's basename starts with 'vdso' and
7548 * ends with '.so', it is the vdso.
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
, '/');
7558 ext
= strrchr(name
, '.');
7560 && strncmp("vdso", name
, 4) == 0
7561 && strcmp(".so", ext
) == 0)
7562 vdso_paths
.insert(path
);
7569 add_unwindsym_vdso (systemtap_session
&s
)
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).
7576 if (s
.kernel_build_tree
== string(s
.sysroot
+ "/lib/modules/"
7579 vdso_dir
= s
.sysroot
+ "/lib/modules/" + s
.kernel_release
+ "/vdso";
7581 vdso_dir
= s
.kernel_build_tree
+ "/arch/";
7584 clog
<< _("Searching for vdso candidates: ") << vdso_dir
<< endl
;
7586 ftw(vdso_dir
.c_str(), find_vdso
, 1);
7588 for (set
<string
>::iterator it
= vdso_paths
.begin();
7589 it
!= vdso_paths
.end();
7592 s
.unwindsym_modules
.insert(*it
);
7594 clog
<< _("vdso candidate: ") << *it
<< endl
;
7599 prepare_symbol_data (systemtap_session
& s
)
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
7611 emit_symbol_data (systemtap_session
& s
)
7613 string symfile
= "stap-symbols.h";
7615 s
.op
->newline() << "#include " << lex_cast_qstring (symfile
);
7617 ofstream
kallsyms_out ((s
.tmpdir
+ "/" + symfile
).c_str());
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 */
7627 NULL
, /* debug_frame */
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 */
7635 0, /* eh_frame_hdr_len */
7637 0, /* eh_frame_hdr_addr */
7638 NULL
, /* debug_line */
7639 0, /* debug_line_len */
7640 s
.unwindsym_modules
};
7642 // Micro optimization, mainly to speed up tiny regression tests
7643 // using just begin probe.
7644 if (s
.unwindsym_modules
.size () == 0)
7646 emit_symbol_data_done(&ctx
, s
);
7650 // ---- step 1: process any kernel modules listed
7651 set
<string
> offline_search_modules
;
7653 for (set
<string
>::iterator it
= s
.unwindsym_modules
.begin();
7654 it
!= s
.unwindsym_modules
.end();
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
);
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()); */
7672 assert_no_interrupts();
7673 if (ctx
.undone_unwindsym_modules
.empty()) break;
7674 off
= dwfl_getmodules (dwfl
, &dump_unwindsyms
, (void *) &ctx
, off
);
7677 DWFL_ASSERT("dwfl_getmodules", off
== 0);
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();
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
7694 assert_no_interrupts();
7695 if (ctx
.undone_unwindsym_modules
.empty()) break;
7696 off
= dwfl_getmodules (dwfl
, &dump_unwindsyms
, (void *) &ctx
, off
);
7699 DWFL_ASSERT("dwfl_getmodules", off
== 0);
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
);
7708 emit_symbol_data_done (&ctx
, s
);
7712 self_unwind_declarations(unwindsym_dump_context
*ctx
)
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";
7739 emit_symbol_data_done (unwindsym_dump_context
*ctx
, systemtap_session
& s
)
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 };";
7749 T_800
->assert_0_indent (); // flush to disk
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
++)
7757 ctx
->output
<< "& _stp_module_" << i
<< ",\n";
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";
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";
7768 ctx
->output
<< "0x" << hex
<< ctx
->stp_kretprobe_trampoline_addr
<< dec
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();
7776 s
.print_warning (_("missing unwind/symbol data for module '")
7780 struct recursion_info
: public traversing_visitor
7782 recursion_info (systemtap_session
& s
): sess(s
), nesting_max(0), recursive(false) {}
7783 systemtap_session
& sess
;
7784 unsigned nesting_max
;
7786 std::vector
<functiondecl
*> current_nesting
;
7788 void visit_functioncall (functioncall
* e
) {
7789 traversing_visitor::visit_functioncall (e
); // for arguments
7791 for (unsigned fd
= 0; fd
< e
->referents
.size(); fd
++)
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
)
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
;
7804 // check for (direct or mutual) recursion
7805 for (unsigned j
=0; j
<current_nesting
.size(); j
++)
7806 if (current_nesting
[j
] == referent
)
7809 if (sess
.verbose
> 3)
7810 clog
<< _F("identified recursive function: %s",
7811 referent
->name
.to_string().c_str()) << endl
;
7815 // non-recursive traversal
7816 current_nesting
.push_back (referent
);
7817 referent
->body
->visit (this);
7818 current_nesting
.pop_back ();
7824 void translate_runtime(systemtap_session
& s
)
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)"));
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]"));
7842 prepare_translate_pass (systemtap_session
& s
)
7847 prepare_symbol_data (s
);
7849 catch (const semantic_error
& e
)
7860 translate_pass (systemtap_session
& s
)
7864 s
.op
= new translator_output (s
.translated_source
);
7865 // additional outputs might be found in s.auxiliary_outputs
7866 c_unparser
cup (& s
);
7868 translate_runtime(s
);
7872 int64_t major
=0, minor
=0;
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
;
7884 catch (const runtime_error
&)
7886 throw SEMANTIC_ERROR(_F("parse error in compatibility version: %s", s
.compatible
.c_str()));
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";
7896 recursion_info
ri (s
);
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
++)
7903 functiondecl
*fd
= it
->second
;
7904 fd
->body
->visit (& ri
);
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;
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\"";
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
;
7926 // Generate a section containing a mask of the privilege levels required to load/run this
7928 s
.op
->newline() << "int stp_required_privilege "
7929 << "__attribute__ ((section (\"" << STAP_PRIVILEGE_SECTION
<<"\")))"
7930 << " = STP_PRIVILEGE;";
7932 s
.op
->newline() << "#ifndef MAXNESTING";
7933 s
.op
->newline() << "#define MAXNESTING " << nesting
;
7934 s
.op
->newline() << "#endif";
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
;
7941 s
.op
->newline() << "#define STP_SKIP_BADVARS " << (s
.skip_badvars
? 1 : 0);
7944 s
.op
->newline() << "#define STP_BULKMODE";
7946 if (s
.timing
|| s
.monitor
)
7947 s
.op
->newline() << "#define STP_TIMING";
7950 s
.op
->newline() << "#define STP_NEED_UNWIND_DATA 1";
7953 s
.op
->newline() << "#define STP_NEED_LINE_DATA 1";
7955 // Emit the total number of probes (not regarding merged probe handlers)
7956 s
.op
->newline() << "#define STP_PROBE_COUNT " << s
.probes
.size();
7958 // Emit systemtap_module_refresh() prototype so we can reference it
7959 s
.op
->newline() << "static void systemtap_module_refresh (const char* modname);";
7961 // Be sure to include runtime.h before any real code.
7962 s
.op
->newline() << "#include \"runtime.h\"";
7964 if (!s
.runtime_usermode_p())
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
7970 s
.op
->newline() << "#include <linux/mutex.h>";
7971 s
.op
->newline() << "static DEFINE_MUTEX(module_refresh_mutex);";
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";
7982 // Emit embeds ahead of time, in case they affect context layout
7983 for (unsigned i
=0; i
<s
.embeds
.size(); i
++)
7985 s
.op
->newline() << s
.embeds
[i
]->code
<< "\n";
7988 s
.up
->emit_common_header (); // context etc.
7991 s
.op
->newline() << "#include \"stack.c\"";
7993 if (s
.globals
.size()>0)
7995 s
.op
->newline() << "struct stp_globals {";
7997 for (unsigned i
=0; i
<s
.globals
.size(); i
++)
7999 s
.up
->emit_global (s
.globals
[i
]);
8001 s
.op
->newline(-1) << "};";
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.
8008 if (!s
.runtime_usermode_p ())
8009 s
.op
->newline() << "static struct stp_globals stp_global = {";
8012 s
.op
->newline() << "static struct {";
8014 for (unsigned i
=0; i
<s
.globals
.size(); i
++)
8016 assert_no_interrupts();
8017 s
.up
->emit_global_init_type (s
.globals
[i
]);
8019 s
.op
->newline(-1) << "} stp_global_init = {";
8022 for (unsigned i
=0; i
<s
.globals
.size(); i
++)
8024 assert_no_interrupts();
8025 s
.up
->emit_global_init (s
.globals
[i
]);
8027 s
.op
->newline(-1) << "};";
8029 s
.op
->assert_0_indent();
8032 // stp_runtime_session wants to incorporate globals, but it
8034 s
.op
->newline() << "struct stp_globals {};";
8036 // Common (static atomic) state of the stap session.
8038 s
.op
->newline() << "#include \"common_session_state.h\"";
8040 s
.op
->newline() << "#include \"probe_lock.h\" ";
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";
8046 for (map
<string
,stapdfa
*>::iterator it
= s
.dfas
.begin(); it
!= s
.dfas
.end(); it
++)
8048 assert_no_interrupts();
8052 it
->second
->emit_declaration (s
.op
);
8054 catch (const semantic_error
&e
)
8059 s
.op
->assert_0_indent();
8061 for (map
<string
,functiondecl
*>::iterator it
= s
.functions
.begin(); it
!= s
.functions
.end(); it
++)
8063 assert_no_interrupts();
8065 s
.up
->emit_functionsig (it
->second
);
8067 s
.op
->assert_0_indent();
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
++)
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());
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*))) \
8097 s.op->newline() << "const char " << #var << "[" << var##_max << "];"; \
8098 if (s.verbose > 2) \
8099 clog << "[]" << endl; \
8103 s.op->newline() << "const char * const " << #var << ";"; \
8104 if (s.verbose > 2) \
8105 clog << "*" << endl; \
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)";
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";
8122 s
.op
->newline() << "#ifdef STP_NEED_PROBE_NAME";
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) "
8133 s
.op
->newline(-1) << "} static stap_probes[];";
8134 s
.op
->assert_0_indent();
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
++)
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
);
8147 s
.op
->assert_0_indent();
8149 for (unsigned i
=0; i
<s
.probes
.size(); i
++)
8151 assert_no_interrupts();
8152 s
.up
->emit_probe (s
.probes
[i
]);
8154 s
.op
->assert_0_indent();
8156 s
.op
->newline() << "static struct stap_probe stap_probes[] = {";
8158 for (unsigned i
=0; i
<s
.probes
.size(); ++i
)
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()) << "),";
8167 s
.op
->newline(-1) << "};";
8169 if (s
.runtime_usermode_p())
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();
8179 for (map
<string
,functiondecl
*>::iterator it
= s
.functions
.begin(); it
!= s
.functions
.end(); it
++)
8181 assert_no_interrupts();
8183 s
.up
->emit_function (it
->second
);
8186 s
.op
->assert_0_indent();
8188 s
.up
->emit_module_init ();
8189 s
.op
->assert_0_indent();
8191 s
.up
->emit_module_refresh ();
8192 s
.op
->assert_0_indent();
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();
8202 emit_symbol_data (s
);
8204 s
.op
->newline() << "MODULE_DESCRIPTION(\"systemtap-generated probe\");";
8205 s
.op
->newline() << "MODULE_LICENSE(\"GPL\");";
8207 for (unsigned i
= 0; i
< s
.modinfos
.size(); i
++)
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
) << ");";
8216 s
.op
->assert_0_indent();
8218 if (s
.runtime_usermode_p())
8219 s
.up
->emit_global_init_setters();
8221 // PR10298: attempt to avoid collisions with symbols
8222 for (unsigned i
=0; i
<s
.globals
.size(); i
++)
8225 s
.up
->emit_global_param (s
.globals
[i
]);
8227 s
.op
->assert_0_indent();
8229 catch (const semantic_error
& e
)
8234 s
.op
->line() << "\n";
8240 for (unsigned i
=0; i
<s
.auxiliary_outputs
.size(); i
++)
8241 s
.auxiliary_outputs
[i
]->close();
8243 return rc
+ s
.num_errors();
8246 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */