1 // tapset for kernel static markers
2 // Copyright (C) 2005-2010 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
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 "translate.h"
27 using namespace __gnu_cxx
;
30 static const string
TOK_KERNEL("kernel");
31 static const string
TOK_MARK("mark");
32 static const string
TOK_FORMAT("format");
35 // ------------------------------------------------------------------------
36 // statically inserted macro-based derived probes
37 // ------------------------------------------------------------------------
47 struct mark_derived_probe
: public derived_probe
49 mark_derived_probe (systemtap_session
&s
,
50 const string
& probe_name
, const string
& probe_format
,
51 probe
* base_probe
, probe_point
* location
);
53 systemtap_session
& sess
;
54 string probe_name
, probe_format
;
55 vector
<struct mark_arg
*> mark_args
;
56 bool target_symbol_seen
;
58 void join_group (systemtap_session
& s
);
59 void print_dupe_stamp (ostream
& o
);
60 void emit_probe_context_vars (translator_output
* o
);
61 void initialize_probe_context_vars (translator_output
* o
);
62 void getargs (std::list
<std::string
> &arg_set
) const;
64 void parse_probe_format ();
68 struct mark_derived_probe_group
: public generic_dpg
<mark_derived_probe
>
71 void emit_module_decls (systemtap_session
& s
);
72 void emit_module_init (systemtap_session
& s
);
73 void emit_module_exit (systemtap_session
& s
);
77 struct mark_var_expanding_visitor
: public var_expanding_visitor
79 mark_var_expanding_visitor(systemtap_session
& s
, const string
& pn
,
80 vector
<struct mark_arg
*> &mark_args
):
81 sess (s
), probe_name (pn
), mark_args (mark_args
),
82 target_symbol_seen (false) {}
83 systemtap_session
& sess
;
85 vector
<struct mark_arg
*> &mark_args
;
86 bool target_symbol_seen
;
88 void visit_target_symbol (target_symbol
* e
);
89 void visit_target_symbol_arg (target_symbol
* e
);
90 void visit_target_symbol_context (target_symbol
* e
);
95 mark_var_expanding_visitor::visit_target_symbol_arg (target_symbol
* e
)
97 string argnum_s
= e
->base_name
.substr(4,e
->base_name
.length()-4);
98 int argnum
= atoi (argnum_s
.c_str());
100 if (argnum
< 1 || argnum
> (int)mark_args
.size())
101 throw semantic_error ("invalid marker argument number", e
->tok
);
103 if (is_active_lvalue (e
))
104 throw semantic_error("write to marker parameter not permitted", e
->tok
);
106 e
->assert_no_components("marker");
108 // Remember that we've seen a target variable.
109 target_symbol_seen
= true;
111 e
->probe_context_var
= "__mark_arg" + lex_cast(argnum
);
112 e
->type
= mark_args
[argnum
-1]->stp_type
;
118 mark_var_expanding_visitor::visit_target_symbol_context (target_symbol
* e
)
120 string sname
= e
->base_name
;
122 if (is_active_lvalue (e
))
123 throw semantic_error("write to marker '" + sname
+ "' not permitted", e
->tok
);
125 e
->assert_no_components("marker");
127 if (e
->base_name
== "$format" || e
->base_name
== "$name") {
129 if (e
->base_name
== "$format") {
130 fname
= string("_mark_format_get");
132 fname
= string("_mark_name_get");
135 // Synthesize a functioncall.
136 functioncall
* n
= new functioncall
;
139 n
->referent
= 0; // NB: must not resolve yet, to ensure inclusion in session
142 else if (e
->base_name
== "$$vars" || e
->base_name
== "$$parms")
144 //copy from tracepoint
145 token
* pf_tok
= new token(*e
->tok
);
146 pf_tok
->content
= "sprintf";
147 print_format
* pf
= print_format::create(pf_tok
);
149 for (unsigned i
= 0; i
< mark_args
.size(); ++i
)
152 pf
->raw_components
+= " ";
153 pf
->raw_components
+= "$arg" + lex_cast(i
+1);
154 target_symbol
*tsym
= new target_symbol
;
156 tsym
->base_name
= "$arg" + lex_cast(i
+1);
158 tsym
->saved_conversion_error
= 0;
159 expression
*texp
= require (tsym
); //same treatment as tracepoint
160 assert (!tsym
->saved_conversion_error
);
161 switch (mark_args
[i
]->stp_type
)
164 pf
->raw_components
+= mark_args
[i
]->isptr
? "=%p" : "=%#x";
167 pf
->raw_components
+= "=%s";
170 pf
->raw_components
+= "=%#x";
173 pf
->args
.push_back(texp
);
175 pf
->components
= print_format::string_to_components(pf
->raw_components
);
181 mark_var_expanding_visitor::visit_target_symbol (target_symbol
* e
)
183 assert(e
->base_name
.size() > 0 && e
->base_name
[0] == '$');
188 throw semantic_error("cannot take address of marker variable", e
->tok
);
190 if (startswith(e
->base_name
, "$arg"))
191 visit_target_symbol_arg (e
);
192 else if (e
->base_name
== "$format" || e
->base_name
== "$name"
193 || e
->base_name
== "$$parms" || e
->base_name
== "$$vars")
194 visit_target_symbol_context (e
);
196 throw semantic_error ("invalid target symbol for marker, $argN, $name, $format, $$parms or $$vars expected",
199 catch (const semantic_error
&er
)
207 mark_derived_probe::mark_derived_probe (systemtap_session
&s
,
210 probe
* base
, probe_point
* loc
):
211 derived_probe (base
, new probe_point(*loc
) /* .components soon rewritten */),
212 sess (s
), probe_name (p_n
), probe_format (p_f
),
213 target_symbol_seen (false)
215 // create synthetic probe point name; preserve condition
216 vector
<probe_point::component
*> comps
;
217 comps
.push_back (new probe_point::component (TOK_KERNEL
));
218 comps
.push_back (new probe_point::component (TOK_MARK
, new literal_string (probe_name
)));
219 comps
.push_back (new probe_point::component (TOK_FORMAT
, new literal_string (probe_format
)));
220 this->sole_location()->components
= comps
;
222 // expand the marker format
223 parse_probe_format();
225 // Now expand the local variables in the probe body
226 mark_var_expanding_visitor
v (sess
, name
, mark_args
);
227 v
.replace (this->body
);
228 target_symbol_seen
= v
.target_symbol_seen
;
230 if (sess
.verbose
> 2)
231 clog
<< "marker-based " << name
<< " mark=" << probe_name
232 << " fmt='" << probe_format
<< "'" << endl
;
237 skip_atoi(const char **s
)
241 i
= i
* 10 + *((*s
)++) - '0';
247 mark_derived_probe::parse_probe_format()
249 const char *fmt
= probe_format
.c_str();
250 int qualifier
; // 'h', 'l', or 'L' for integer fields
264 // skip conversion flags (if present)
275 // skip minimum field witdh (if present)
279 // skip precision (if present)
287 // get the conversion qualifier (if present)
289 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L')
293 if (qualifier
== 'l' && *fmt
== 'l')
300 // get the conversion type
308 arg
->stp_type
= pe_long
;
309 mark_args
.push_back(arg
);
316 arg
->c_type
= "char *";
317 arg
->stp_type
= pe_string
;
318 mark_args
.push_back(arg
);
325 // This should really be 'void *'. But, then we'll get a
326 // compile error when we assign the void pointer to an
327 // integer without a cast. So, we use 'long' instead, since
328 // it should have the same size as 'void *'.
329 arg
->c_type
= "long";
330 arg
->stp_type
= pe_long
;
331 mark_args
.push_back(arg
);
355 arg
->stp_type
= pe_long
;
359 arg
->c_type
= "long long";
363 arg
->c_type
= "long";
367 arg
->c_type
= "short";
374 mark_args
.push_back(arg
);
380 mark_derived_probe::join_group (systemtap_session
& s
)
382 if (! s
.mark_derived_probes
)
384 s
.mark_derived_probes
= new mark_derived_probe_group ();
386 // Make sure <linux/marker.h> is included early.
387 embeddedcode
*ec
= new embeddedcode
;
389 ec
->code
= string("#if ! defined(CONFIG_MARKERS)\n")
390 + string("#error \"Need CONFIG_MARKERS!\"\n")
392 + string("#include <linux/marker.h>\n");
394 s
.embeds
.push_back(ec
);
396 s
.mark_derived_probes
->enroll (this);
401 mark_derived_probe::print_dupe_stamp (ostream
& o
)
403 if (target_symbol_seen
)
404 for (unsigned i
= 0; i
< mark_args
.size(); i
++)
405 o
<< mark_args
[i
]->c_type
<< " __mark_arg" << (i
+1) << endl
;
410 mark_derived_probe::emit_probe_context_vars (translator_output
* o
)
412 // If we haven't seen a target symbol for this probe, quit.
413 if (! target_symbol_seen
)
416 for (unsigned i
= 0; i
< mark_args
.size(); i
++)
418 string localname
= "__mark_arg" + lex_cast(i
+1);
419 switch (mark_args
[i
]->stp_type
)
422 o
->newline() << "int64_t " << localname
<< ";";
425 o
->newline() << "string_t " << localname
<< ";";
428 throw semantic_error ("cannot expand unknown type");
436 mark_derived_probe::initialize_probe_context_vars (translator_output
* o
)
438 // If we haven't seen a target symbol for this probe, quit.
439 if (! target_symbol_seen
)
442 bool deref_fault_needed
= false;
443 for (unsigned i
= 0; i
< mark_args
.size(); i
++)
445 string localname
= "l->__mark_arg" + lex_cast(i
+1);
446 switch (mark_args
[i
]->stp_type
)
449 o
->newline() << localname
<< " = va_arg(*c->mark_va_list, "
450 << mark_args
[i
]->c_type
<< ");";
454 // We're assuming that this is a kernel string (this code is
455 // basically the guts of kernel_string), not a user string.
456 o
->newline() << "{ " << mark_args
[i
]->c_type
457 << " tmp_str = va_arg(*c->mark_va_list, "
458 << mark_args
[i
]->c_type
<< ");";
459 o
->newline() << "deref_string (" << localname
460 << ", tmp_str, MAXSTRINGLEN); }";
461 deref_fault_needed
= true;
465 throw semantic_error ("cannot expand unknown type");
469 if (deref_fault_needed
)
470 // Need to report errors?
471 o
->newline() << "deref_fault: ;";
475 mark_derived_probe::getargs(std::list
<std::string
> &arg_set
) const
477 for (unsigned i
= 0; i
< mark_args
.size(); i
++)
479 string localname
= "$arg" + lex_cast(i
+1);
480 switch (mark_args
[i
]->stp_type
)
483 arg_set
.push_back(localname
+":long");
486 arg_set
.push_back(localname
+":string");
489 arg_set
.push_back(localname
+":unknown");
497 mark_derived_probe_group::emit_module_decls (systemtap_session
& s
)
502 s
.op
->newline() << "/* ---- marker probes ---- */";
504 s
.op
->newline() << "static struct stap_marker_probe {";
505 s
.op
->newline(1) << "const char * const name;";
506 s
.op
->newline() << "const char * const format;";
507 s
.op
->newline() << "const char * const pp;";
508 s
.op
->newline() << "void (* const ph) (struct context *);";
510 s
.op
->newline(-1) << "} stap_marker_probes [" << probes
.size() << "] = {";
512 for (unsigned i
=0; i
< probes
.size(); i
++)
514 s
.op
->newline () << "{";
515 s
.op
->line() << " .name=" << lex_cast_qstring(probes
[i
]->probe_name
)
517 s
.op
->line() << " .format=" << lex_cast_qstring(probes
[i
]->probe_format
)
519 s
.op
->line() << " .pp=" << lex_cast_qstring (*probes
[i
]->sole_location())
521 s
.op
->line() << " .ph=&" << probes
[i
]->name
;
522 s
.op
->line() << " },";
524 s
.op
->newline(-1) << "};";
528 // Emit the marker callback function
530 s
.op
->newline() << "static void enter_marker_probe (void *probe_data, void *call_data, const char *fmt, va_list *args) {";
531 s
.op
->newline(1) << "struct stap_marker_probe *smp = (struct stap_marker_probe *)probe_data;";
532 common_probe_entryfn_prologue (s
.op
, "STAP_SESSION_RUNNING", "smp->pp");
533 s
.op
->newline() << "c->marker_name = smp->name;";
534 s
.op
->newline() << "c->marker_format = smp->format;";
535 s
.op
->newline() << "c->mark_va_list = args;";
536 s
.op
->newline() << "(*smp->ph) (c);";
537 s
.op
->newline() << "c->mark_va_list = NULL;";
538 s
.op
->newline() << "c->data = NULL;";
540 common_probe_entryfn_epilogue (s
.op
);
541 s
.op
->newline(-1) << "}";
548 mark_derived_probe_group::emit_module_init (systemtap_session
&s
)
550 if (probes
.size () == 0)
553 s
.op
->newline() << "/* init marker probes */";
554 s
.op
->newline() << "for (i=0; i<" << probes
.size() << "; i++) {";
555 s
.op
->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];";
556 s
.op
->newline() << "probe_point = smp->pp;";
557 s
.op
->newline() << "rc = marker_probe_register(smp->name, smp->format, enter_marker_probe, smp);";
558 s
.op
->newline() << "if (rc) {";
559 s
.op
->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
560 s
.op
->newline(1) << "struct stap_marker_probe *smp2 = &stap_marker_probes[j];";
561 s
.op
->newline() << "marker_probe_unregister(smp2->name, enter_marker_probe, smp2);";
562 s
.op
->newline(-1) << "}";
563 s
.op
->newline() << "break;"; // don't attempt to register any more probes
564 s
.op
->newline(-1) << "}";
565 s
.op
->newline(-1) << "}"; // for loop
570 mark_derived_probe_group::emit_module_exit (systemtap_session
& s
)
575 s
.op
->newline() << "/* deregister marker probes */";
576 s
.op
->newline() << "for (i=0; i<" << probes
.size() << "; i++) {";
577 s
.op
->newline(1) << "struct stap_marker_probe *smp = &stap_marker_probes[i];";
578 s
.op
->newline() << "marker_probe_unregister(smp->name, enter_marker_probe, smp);";
579 s
.op
->newline(-1) << "}"; // for loop
583 struct mark_builder
: public derived_probe_builder
586 bool cache_initialized
;
587 typedef multimap
<string
, string
> mark_cache_t
;
588 typedef multimap
<string
, string
>::const_iterator mark_cache_const_iterator_t
;
589 typedef pair
<mark_cache_const_iterator_t
, mark_cache_const_iterator_t
>
590 mark_cache_const_iterator_pair_t
;
591 mark_cache_t mark_cache
;
594 mark_builder(): cache_initialized(false) {}
596 void build_no_more (systemtap_session
&s
)
598 if (! mark_cache
.empty())
601 clog
<< "mark_builder releasing cache" << endl
;
606 void build(systemtap_session
& sess
,
608 probe_point
* location
,
609 literal_map_t
const & parameters
,
610 vector
<derived_probe
*> & finished_results
);
615 mark_builder::build(systemtap_session
& sess
,
618 literal_map_t
const & parameters
,
619 vector
<derived_probe
*> & finished_results
)
622 bool has_mark_str
= get_param (parameters
, TOK_MARK
, mark_str_val
);
623 string mark_format_val
;
624 bool has_mark_format
= get_param (parameters
, TOK_FORMAT
, mark_format_val
);
625 assert (has_mark_str
);
628 if (! cache_initialized
)
630 cache_initialized
= true;
631 string module_markers_path
= sess
.kernel_build_tree
+ "/Module.markers";
633 ifstream module_markers
;
634 module_markers
.open(module_markers_path
.c_str(), ifstream::in
);
635 if (! module_markers
)
638 clog
<< module_markers_path
<< " cannot be opened: "
639 << strerror(errno
) << endl
;
643 string name
, module
, format
;
646 module_markers
>> name
>> module
;
647 getline(module_markers
, format
);
649 // trim leading whitespace
650 string::size_type notwhite
= format
.find_first_not_of(" \t");
651 format
.erase(0, notwhite
);
653 // If the format is empty, make sure we add back a space
654 // character, which is what MARK_NOARGS expands to.
655 if (format
.length() == 0)
659 clog
<< "'" << name
<< "' '" << module
<< "' '" << format
662 if (mark_cache
.count(name
) > 0)
664 // If we have 2 markers with the same we've got 2 cases:
665 // different format strings or duplicate format strings.
666 // If an existing marker in the cache doesn't have the
667 // same format string, add this marker.
668 mark_cache_const_iterator_pair_t ret
;
669 mark_cache_const_iterator_t it
;
670 bool matching_format_string
= false;
672 ret
= mark_cache
.equal_range(name
);
673 for (it
= ret
.first
; it
!= ret
.second
; ++it
)
675 if (format
== it
->second
)
677 matching_format_string
= true;
682 if (! matching_format_string
)
683 mark_cache
.insert(pair
<string
,string
>(name
, format
));
686 mark_cache
.insert(pair
<string
,string
>(name
, format
));
688 while (! module_markers
.eof());
689 module_markers
.close();
692 // Search marker list for matching markers
693 for (mark_cache_const_iterator_t it
= mark_cache
.begin();
694 it
!= mark_cache
.end(); it
++)
696 // Below, "rc" has negative polarity: zero iff matching.
697 int rc
= fnmatch(mark_str_val
.c_str(), it
->first
.c_str(), 0);
700 bool add_result
= true;
702 // Match format strings (if the user specified one)
703 if (has_mark_format
&& fnmatch(mark_format_val
.c_str(),
704 it
->second
.c_str(), 0))
710 = new mark_derived_probe (sess
,
711 it
->first
, it
->second
,
713 finished_results
.push_back (dp
);
722 register_tapset_mark(systemtap_session
& s
)
724 match_node
* root
= s
.pattern_root
;
725 derived_probe_builder
*builder
= new mark_builder();
727 root
= root
->bind(TOK_KERNEL
);
728 root
= root
->bind_str(TOK_MARK
);
731 root
->bind_str(TOK_FORMAT
)->bind(builder
);
734 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */