]> sourceware.org Git - systemtap.git/blob - tapset-mark.cxx
Clean up and clarify semantic_error chaining
[systemtap.git] / tapset-mark.cxx
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
5 //
6 // This file is part of systemtap, and is free software. You can
7 // redistribute it and/or modify it under the terms of the GNU General
8 // Public License (GPL); either version 2, or (at your option) any
9 // later version.
10
11 #include "session.h"
12 #include "tapsets.h"
13 #include "translate.h"
14 #include "util.h"
15
16 #include <cerrno>
17 #include <cstdlib>
18 #include <cstring>
19 #include <string>
20
21 extern "C" {
22 #include <fnmatch.h>
23 }
24
25
26 using namespace std;
27 using namespace __gnu_cxx;
28
29
30 static const string TOK_KERNEL("kernel");
31 static const string TOK_MARK("mark");
32 static const string TOK_FORMAT("format");
33
34
35 // ------------------------------------------------------------------------
36 // statically inserted macro-based derived probes
37 // ------------------------------------------------------------------------
38
39 struct mark_arg
40 {
41 bool str;
42 bool isptr;
43 string c_type;
44 exp_type stp_type;
45 };
46
47 struct mark_derived_probe: public derived_probe
48 {
49 mark_derived_probe (systemtap_session &s,
50 const string& probe_name, const string& probe_format,
51 probe* base_probe, probe_point* location);
52
53 systemtap_session& sess;
54 string probe_name, probe_format;
55 vector <struct mark_arg *> mark_args;
56 bool target_symbol_seen;
57
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;
63
64 void parse_probe_format ();
65 };
66
67
68 struct mark_derived_probe_group: public generic_dpg<mark_derived_probe>
69 {
70 public:
71 void emit_module_decls (systemtap_session& s);
72 void emit_module_init (systemtap_session& s);
73 void emit_module_exit (systemtap_session& s);
74 };
75
76
77 struct mark_var_expanding_visitor: public var_expanding_visitor
78 {
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;
84 string probe_name;
85 vector <struct mark_arg *> &mark_args;
86 bool target_symbol_seen;
87
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);
91 };
92
93
94 void
95 mark_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
96 {
97 string argnum_s = e->base_name.substr(4,e->base_name.length()-4);
98 int argnum = atoi (argnum_s.c_str());
99
100 if (argnum < 1 || argnum > (int)mark_args.size())
101 throw semantic_error ("invalid marker argument number", e->tok);
102
103 if (is_active_lvalue (e))
104 throw semantic_error("write to marker parameter not permitted", e->tok);
105
106 e->assert_no_components("marker");
107
108 // Remember that we've seen a target variable.
109 target_symbol_seen = true;
110
111 e->probe_context_var = "__mark_arg" + lex_cast(argnum);
112 e->type = mark_args[argnum-1]->stp_type;
113 provide (e);
114 }
115
116
117 void
118 mark_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
119 {
120 string sname = e->base_name;
121
122 if (is_active_lvalue (e))
123 throw semantic_error("write to marker '" + sname + "' not permitted", e->tok);
124
125 e->assert_no_components("marker");
126
127 if (e->base_name == "$format" || e->base_name == "$name") {
128 string fname;
129 if (e->base_name == "$format") {
130 fname = string("_mark_format_get");
131 } else {
132 fname = string("_mark_name_get");
133 }
134
135 // Synthesize a functioncall.
136 functioncall* n = new functioncall;
137 n->tok = e->tok;
138 n->function = fname;
139 n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session
140 provide (n);
141 }
142 else if (e->base_name == "$$vars" || e->base_name == "$$parms")
143 {
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);
148
149 for (unsigned i = 0; i < mark_args.size(); ++i)
150 {
151 if (i > 0)
152 pf->raw_components += " ";
153 pf->raw_components += "$arg" + lex_cast(i+1);
154 target_symbol *tsym = new target_symbol;
155 tsym->tok = e->tok;
156 tsym->base_name = "$arg" + lex_cast(i+1);
157
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)
162 {
163 case pe_long:
164 pf->raw_components += mark_args[i]->isptr ? "=%p" : "=%#x";
165 break;
166 case pe_string:
167 pf->raw_components += "=%s";
168 break;
169 default:
170 pf->raw_components += "=%#x";
171 break;
172 }
173 pf->args.push_back(texp);
174 }
175 pf->components = print_format::string_to_components(pf->raw_components);
176 provide (pf);
177 }
178 }
179
180 void
181 mark_var_expanding_visitor::visit_target_symbol (target_symbol* e)
182 {
183 assert(e->base_name.size() > 0 && e->base_name[0] == '$');
184
185 try
186 {
187 if (e->addressof)
188 throw semantic_error("cannot take address of marker variable", e->tok);
189
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);
195 else
196 throw semantic_error ("invalid target symbol for marker, $argN, $name, $format, $$parms or $$vars expected",
197 e->tok);
198 }
199 catch (const semantic_error &er)
200 {
201 e->chain (er);
202 provide (e);
203 }
204 }
205
206
207 mark_derived_probe::mark_derived_probe (systemtap_session &s,
208 const string& p_n,
209 const string& p_f,
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)
214 {
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;
221
222 // expand the marker format
223 parse_probe_format();
224
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;
229
230 if (sess.verbose > 2)
231 clog << "marker-based " << name << " mark=" << probe_name
232 << " fmt='" << probe_format << "'" << endl;
233 }
234
235
236 static int
237 skip_atoi(const char **s)
238 {
239 int i = 0;
240 while (isdigit(**s))
241 i = i * 10 + *((*s)++) - '0';
242 return i;
243 }
244
245
246 void
247 mark_derived_probe::parse_probe_format()
248 {
249 const char *fmt = probe_format.c_str();
250 int qualifier; // 'h', 'l', or 'L' for integer fields
251 mark_arg *arg;
252
253 for (; *fmt ; ++fmt)
254 {
255 if (*fmt != '%')
256 {
257 /* Skip text */
258 continue;
259 }
260
261 repeat:
262 ++fmt;
263
264 // skip conversion flags (if present)
265 switch (*fmt)
266 {
267 case '-':
268 case '+':
269 case ' ':
270 case '#':
271 case '0':
272 goto repeat;
273 }
274
275 // skip minimum field witdh (if present)
276 if (isdigit(*fmt))
277 skip_atoi(&fmt);
278
279 // skip precision (if present)
280 if (*fmt == '.')
281 {
282 ++fmt;
283 if (isdigit(*fmt))
284 skip_atoi(&fmt);
285 }
286
287 // get the conversion qualifier (if present)
288 qualifier = -1;
289 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
290 {
291 qualifier = *fmt;
292 ++fmt;
293 if (qualifier == 'l' && *fmt == 'l')
294 {
295 qualifier = 'L';
296 ++fmt;
297 }
298 }
299
300 // get the conversion type
301 switch (*fmt)
302 {
303 case 'c':
304 arg = new mark_arg;
305 arg->str = false;
306 arg->isptr = false;
307 arg->c_type = "int";
308 arg->stp_type = pe_long;
309 mark_args.push_back(arg);
310 continue;
311
312 case 's':
313 arg = new mark_arg;
314 arg->str = true;
315 arg->isptr = false;
316 arg->c_type = "char *";
317 arg->stp_type = pe_string;
318 mark_args.push_back(arg);
319 continue;
320
321 case 'p':
322 arg = new mark_arg;
323 arg->str = false;
324 arg->isptr = true;
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);
332 continue;
333
334 case '%':
335 continue;
336
337 case 'o':
338 case 'X':
339 case 'x':
340 case 'd':
341 case 'i':
342 case 'u':
343 // fall through...
344 break;
345
346 default:
347 if (!*fmt)
348 --fmt;
349 continue;
350 }
351
352 arg = new mark_arg;
353 arg->str = false;
354 arg->isptr = false;
355 arg->stp_type = pe_long;
356 switch (qualifier)
357 {
358 case 'L':
359 arg->c_type = "long long";
360 break;
361
362 case 'l':
363 arg->c_type = "long";
364 break;
365
366 case 'h':
367 arg->c_type = "short";
368 break;
369
370 default:
371 arg->c_type = "int";
372 break;
373 }
374 mark_args.push_back(arg);
375 }
376 }
377
378
379 void
380 mark_derived_probe::join_group (systemtap_session& s)
381 {
382 if (! s.mark_derived_probes)
383 {
384 s.mark_derived_probes = new mark_derived_probe_group ();
385
386 // Make sure <linux/marker.h> is included early.
387 embeddedcode *ec = new embeddedcode;
388 ec->tok = NULL;
389 ec->code = string("#if ! defined(CONFIG_MARKERS)\n")
390 + string("#error \"Need CONFIG_MARKERS!\"\n")
391 + string("#endif\n")
392 + string("#include <linux/marker.h>\n");
393
394 s.embeds.push_back(ec);
395 }
396 s.mark_derived_probes->enroll (this);
397 }
398
399
400 void
401 mark_derived_probe::print_dupe_stamp (ostream& o)
402 {
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;
406 }
407
408
409 void
410 mark_derived_probe::emit_probe_context_vars (translator_output* o)
411 {
412 // If we haven't seen a target symbol for this probe, quit.
413 if (! target_symbol_seen)
414 return;
415
416 for (unsigned i = 0; i < mark_args.size(); i++)
417 {
418 string localname = "__mark_arg" + lex_cast(i+1);
419 switch (mark_args[i]->stp_type)
420 {
421 case pe_long:
422 o->newline() << "int64_t " << localname << ";";
423 break;
424 case pe_string:
425 o->newline() << "string_t " << localname << ";";
426 break;
427 default:
428 throw semantic_error ("cannot expand unknown type");
429 break;
430 }
431 }
432 }
433
434
435 void
436 mark_derived_probe::initialize_probe_context_vars (translator_output* o)
437 {
438 // If we haven't seen a target symbol for this probe, quit.
439 if (! target_symbol_seen)
440 return;
441
442 bool deref_fault_needed = false;
443 for (unsigned i = 0; i < mark_args.size(); i++)
444 {
445 string localname = "l->__mark_arg" + lex_cast(i+1);
446 switch (mark_args[i]->stp_type)
447 {
448 case pe_long:
449 o->newline() << localname << " = va_arg(*c->mark_va_list, "
450 << mark_args[i]->c_type << ");";
451 break;
452
453 case pe_string:
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;
462 break;
463
464 default:
465 throw semantic_error ("cannot expand unknown type");
466 break;
467 }
468 }
469 if (deref_fault_needed)
470 // Need to report errors?
471 o->newline() << "deref_fault: ;";
472 }
473
474 void
475 mark_derived_probe::getargs(std::list<std::string> &arg_set) const
476 {
477 for (unsigned i = 0; i < mark_args.size(); i++)
478 {
479 string localname = "$arg" + lex_cast(i+1);
480 switch (mark_args[i]->stp_type)
481 {
482 case pe_long:
483 arg_set.push_back(localname+":long");
484 break;
485 case pe_string:
486 arg_set.push_back(localname+":string");
487 break;
488 default:
489 arg_set.push_back(localname+":unknown");
490 break;
491 }
492 }
493 }
494
495
496 void
497 mark_derived_probe_group::emit_module_decls (systemtap_session& s)
498 {
499 if (probes.empty())
500 return;
501
502 s.op->newline() << "/* ---- marker probes ---- */";
503
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 *);";
509
510 s.op->newline(-1) << "} stap_marker_probes [" << probes.size() << "] = {";
511 s.op->indent(1);
512 for (unsigned i=0; i < probes.size(); i++)
513 {
514 s.op->newline () << "{";
515 s.op->line() << " .name=" << lex_cast_qstring(probes[i]->probe_name)
516 << ",";
517 s.op->line() << " .format=" << lex_cast_qstring(probes[i]->probe_format)
518 << ",";
519 s.op->line() << " .pp=" << lex_cast_qstring (*probes[i]->sole_location())
520 << ",";
521 s.op->line() << " .ph=&" << probes[i]->name;
522 s.op->line() << " },";
523 }
524 s.op->newline(-1) << "};";
525 s.op->newline();
526
527
528 // Emit the marker callback function
529 s.op->newline();
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;";
539
540 common_probe_entryfn_epilogue (s.op);
541 s.op->newline(-1) << "}";
542
543 return;
544 }
545
546
547 void
548 mark_derived_probe_group::emit_module_init (systemtap_session &s)
549 {
550 if (probes.size () == 0)
551 return;
552
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
566 }
567
568
569 void
570 mark_derived_probe_group::emit_module_exit (systemtap_session& s)
571 {
572 if (probes.empty())
573 return;
574
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
580 }
581
582
583 struct mark_builder: public derived_probe_builder
584 {
585 private:
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;
592
593 public:
594 mark_builder(): cache_initialized(false) {}
595
596 void build_no_more (systemtap_session &s)
597 {
598 if (! mark_cache.empty())
599 {
600 if (s.verbose > 3)
601 clog << "mark_builder releasing cache" << endl;
602 mark_cache.clear();
603 }
604 }
605
606 void build(systemtap_session & sess,
607 probe * base,
608 probe_point * location,
609 literal_map_t const & parameters,
610 vector<derived_probe *> & finished_results);
611 };
612
613
614 void
615 mark_builder::build(systemtap_session & sess,
616 probe * base,
617 probe_point *loc,
618 literal_map_t const & parameters,
619 vector<derived_probe *> & finished_results)
620 {
621 string mark_str_val;
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);
626 (void) has_mark_str;
627
628 if (! cache_initialized)
629 {
630 cache_initialized = true;
631 string module_markers_path = sess.kernel_build_tree + "/Module.markers";
632
633 ifstream module_markers;
634 module_markers.open(module_markers_path.c_str(), ifstream::in);
635 if (! module_markers)
636 {
637 if (sess.verbose>3)
638 clog << module_markers_path << " cannot be opened: "
639 << strerror(errno) << endl;
640 return;
641 }
642
643 string name, module, format;
644 do
645 {
646 module_markers >> name >> module;
647 getline(module_markers, format);
648
649 // trim leading whitespace
650 string::size_type notwhite = format.find_first_not_of(" \t");
651 format.erase(0, notwhite);
652
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)
656 format = " ";
657
658 if (sess.verbose>3)
659 clog << "'" << name << "' '" << module << "' '" << format
660 << "'" << endl;
661
662 if (mark_cache.count(name) > 0)
663 {
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;
671
672 ret = mark_cache.equal_range(name);
673 for (it = ret.first; it != ret.second; ++it)
674 {
675 if (format == it->second)
676 {
677 matching_format_string = true;
678 break;
679 }
680 }
681
682 if (! matching_format_string)
683 mark_cache.insert(pair<string,string>(name, format));
684 }
685 else
686 mark_cache.insert(pair<string,string>(name, format));
687 }
688 while (! module_markers.eof());
689 module_markers.close();
690 }
691
692 // Search marker list for matching markers
693 for (mark_cache_const_iterator_t it = mark_cache.begin();
694 it != mark_cache.end(); it++)
695 {
696 // Below, "rc" has negative polarity: zero iff matching.
697 int rc = fnmatch(mark_str_val.c_str(), it->first.c_str(), 0);
698 if (! rc)
699 {
700 bool add_result = true;
701
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))
705 add_result = false;
706
707 if (add_result)
708 {
709 derived_probe *dp
710 = new mark_derived_probe (sess,
711 it->first, it->second,
712 base, loc);
713 finished_results.push_back (dp);
714 }
715 }
716 }
717 }
718
719
720
721 void
722 register_tapset_mark(systemtap_session& s)
723 {
724 match_node* root = s.pattern_root;
725 derived_probe_builder *builder = new mark_builder();
726
727 root = root->bind(TOK_KERNEL);
728 root = root->bind_str(TOK_MARK);
729
730 root->bind(builder);
731 root->bind_str(TOK_FORMAT)->bind(builder);
732 }
733
734 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.07435 seconds and 6 git commands to generate.