constify probe.c function
[deliverable/binutils-gdb.git] / gdb / probe.c
1 /* Generic static probe support for GDB.
2
3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "probe.h"
22 #include "command.h"
23 #include "cli/cli-cmds.h"
24 #include "cli/cli-utils.h"
25 #include "objfiles.h"
26 #include "symtab.h"
27 #include "progspace.h"
28 #include "filenames.h"
29 #include "exceptions.h"
30 #include "linespec.h"
31 #include "gdb_regex.h"
32 #include "frame.h"
33 #include "arch-utils.h"
34 #include <ctype.h>
35
36 typedef struct bound_probe bound_probe_s;
37 DEF_VEC_O (bound_probe_s);
38
39 \f
40
41 /* See definition in probe.h. */
42
43 struct symtabs_and_lines
44 parse_probes (char **argptr, struct linespec_result *canonical)
45 {
46 char *arg_start, *arg_end, *arg;
47 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
48 struct cleanup *cleanup;
49 struct symtabs_and_lines result;
50 struct objfile *objfile;
51 struct program_space *pspace;
52 const struct probe_ops *probe_ops;
53 const char *cs;
54
55 result.sals = NULL;
56 result.nelts = 0;
57
58 arg_start = *argptr;
59
60 cs = *argptr;
61 probe_ops = probe_linespec_to_ops (&cs);
62 if (probe_ops == NULL)
63 error (_("'%s' is not a probe linespec"), arg_start);
64
65 arg = (char *) cs;
66 arg = skip_spaces (arg);
67 if (!*arg)
68 error (_("argument to `%s' missing"), arg_start);
69
70 arg_end = skip_to_space (arg);
71
72 /* We make a copy here so we can write over parts with impunity. */
73 arg = savestring (arg, arg_end - arg);
74 cleanup = make_cleanup (xfree, arg);
75
76 /* Extract each word from the argument, separated by ":"s. */
77 p = strchr (arg, ':');
78 if (p == NULL)
79 {
80 /* This is `-p name'. */
81 name = arg;
82 }
83 else
84 {
85 char *hold = p + 1;
86
87 *p = '\0';
88 p = strchr (hold, ':');
89 if (p == NULL)
90 {
91 /* This is `-p provider:name'. */
92 provider = arg;
93 name = hold;
94 }
95 else
96 {
97 /* This is `-p objfile:provider:name'. */
98 *p = '\0';
99 objfile_namestr = arg;
100 provider = hold;
101 name = p + 1;
102 }
103 }
104
105 if (*name == '\0')
106 error (_("no probe name specified"));
107 if (provider && *provider == '\0')
108 error (_("invalid provider name"));
109 if (objfile_namestr && *objfile_namestr == '\0')
110 error (_("invalid objfile name"));
111
112 ALL_PSPACES (pspace)
113 ALL_PSPACE_OBJFILES (pspace, objfile)
114 {
115 VEC (probe_p) *probes;
116 struct probe *probe;
117 int ix;
118
119 if (!objfile->sf || !objfile->sf->sym_probe_fns)
120 continue;
121
122 if (objfile_namestr
123 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
124 && FILENAME_CMP (lbasename (objfile_name (objfile)),
125 objfile_namestr) != 0)
126 continue;
127
128 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
129
130 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
131 {
132 struct symtab_and_line *sal;
133
134 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
135 continue;
136
137 if (provider && strcmp (probe->provider, provider) != 0)
138 continue;
139
140 if (strcmp (probe->name, name) != 0)
141 continue;
142
143 ++result.nelts;
144 result.sals = xrealloc (result.sals,
145 result.nelts
146 * sizeof (struct symtab_and_line));
147 sal = &result.sals[result.nelts - 1];
148
149 init_sal (sal);
150
151 sal->pc = get_probe_address (probe, objfile);
152 sal->explicit_pc = 1;
153 sal->section = find_pc_overlay (sal->pc);
154 sal->pspace = pspace;
155 sal->probe = probe;
156 sal->objfile = objfile;
157 }
158 }
159
160 if (result.nelts == 0)
161 {
162 throw_error (NOT_FOUND_ERROR,
163 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
164 objfile_namestr ? objfile_namestr : _("<any>"),
165 provider ? provider : _("<any>"),
166 name);
167 }
168
169 if (canonical)
170 {
171 canonical->special_display = 1;
172 canonical->pre_expanded = 1;
173 canonical->addr_string = savestring (*argptr, arg_end - *argptr);
174 }
175
176 *argptr = arg_end;
177 do_cleanups (cleanup);
178
179 return result;
180 }
181
182 /* See definition in probe.h. */
183
184 VEC (probe_p) *
185 find_probes_in_objfile (struct objfile *objfile, const char *provider,
186 const char *name)
187 {
188 VEC (probe_p) *probes, *result = NULL;
189 int ix;
190 struct probe *probe;
191
192 if (!objfile->sf || !objfile->sf->sym_probe_fns)
193 return NULL;
194
195 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
196 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
197 {
198 if (strcmp (probe->provider, provider) != 0)
199 continue;
200
201 if (strcmp (probe->name, name) != 0)
202 continue;
203
204 VEC_safe_push (probe_p, result, probe);
205 }
206
207 return result;
208 }
209
210 /* See definition in probe.h. */
211
212 struct bound_probe
213 find_probe_by_pc (CORE_ADDR pc)
214 {
215 struct objfile *objfile;
216 struct bound_probe result;
217
218 result.objfile = NULL;
219 result.probe = NULL;
220
221 ALL_OBJFILES (objfile)
222 {
223 VEC (probe_p) *probes;
224 int ix;
225 struct probe *probe;
226
227 if (!objfile->sf || !objfile->sf->sym_probe_fns
228 || objfile->sect_index_text == -1)
229 continue;
230
231 /* If this proves too inefficient, we can replace with a hash. */
232 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
233 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
234 if (get_probe_address (probe, objfile) == pc)
235 {
236 result.objfile = objfile;
237 result.probe = probe;
238 return result;
239 }
240 }
241
242 return result;
243 }
244
245 \f
246
247 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
248 If POPS is not NULL, only probes of this certain probe_ops will match.
249 Each argument is a regexp, or NULL, which matches anything. */
250
251 static VEC (bound_probe_s) *
252 collect_probes (char *objname, char *provider, char *probe_name,
253 const struct probe_ops *pops)
254 {
255 struct objfile *objfile;
256 VEC (bound_probe_s) *result = NULL;
257 struct cleanup *cleanup, *cleanup_temps;
258 regex_t obj_pat, prov_pat, probe_pat;
259
260 cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
261
262 cleanup_temps = make_cleanup (null_cleanup, NULL);
263 if (provider != NULL)
264 compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
265 if (probe_name != NULL)
266 compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
267 if (objname != NULL)
268 compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
269
270 ALL_OBJFILES (objfile)
271 {
272 VEC (probe_p) *probes;
273 struct probe *probe;
274 int ix;
275
276 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
277 continue;
278
279 if (objname)
280 {
281 if (regexec (&obj_pat, objfile_name (objfile), 0, NULL, 0) != 0)
282 continue;
283 }
284
285 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
286
287 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
288 {
289 struct bound_probe bound;
290
291 if (pops != NULL && probe->pops != pops)
292 continue;
293
294 if (provider
295 && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
296 continue;
297
298 if (probe_name
299 && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
300 continue;
301
302 bound.objfile = objfile;
303 bound.probe = probe;
304 VEC_safe_push (bound_probe_s, result, &bound);
305 }
306 }
307
308 do_cleanups (cleanup_temps);
309 discard_cleanups (cleanup);
310 return result;
311 }
312
313 /* A qsort comparison function for bound_probe_s objects. */
314
315 static int
316 compare_probes (const void *a, const void *b)
317 {
318 const struct bound_probe *pa = (const struct bound_probe *) a;
319 const struct bound_probe *pb = (const struct bound_probe *) b;
320 int v;
321
322 v = strcmp (pa->probe->provider, pb->probe->provider);
323 if (v)
324 return v;
325
326 v = strcmp (pa->probe->name, pb->probe->name);
327 if (v)
328 return v;
329
330 if (pa->probe->address < pb->probe->address)
331 return -1;
332 if (pa->probe->address > pb->probe->address)
333 return 1;
334
335 return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
336 }
337
338 /* Helper function that generate entries in the ui_out table being
339 crafted by `info_probes_for_ops'. */
340
341 static void
342 gen_ui_out_table_header_info (VEC (bound_probe_s) *probes,
343 const struct probe_ops *p)
344 {
345 /* `headings' refers to the names of the columns when printing `info
346 probes'. */
347 VEC (info_probe_column_s) *headings = NULL;
348 struct cleanup *c;
349 info_probe_column_s *column;
350 size_t headings_size;
351 int ix;
352
353 gdb_assert (p != NULL);
354
355 if (p->gen_info_probes_table_header == NULL
356 && p->gen_info_probes_table_values == NULL)
357 return;
358
359 gdb_assert (p->gen_info_probes_table_header != NULL
360 && p->gen_info_probes_table_values != NULL);
361
362 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
363 p->gen_info_probes_table_header (&headings);
364
365 headings_size = VEC_length (info_probe_column_s, headings);
366
367 for (ix = 0;
368 VEC_iterate (info_probe_column_s, headings, ix, column);
369 ++ix)
370 {
371 struct bound_probe *probe;
372 int jx;
373 size_t size_max = strlen (column->print_name);
374
375 for (jx = 0; VEC_iterate (bound_probe_s, probes, jx, probe); ++jx)
376 {
377 /* `probe_fields' refers to the values of each new field that this
378 probe will display. */
379 VEC (const_char_ptr) *probe_fields = NULL;
380 struct cleanup *c2;
381 const char *val;
382 int kx;
383
384 if (probe->probe->pops != p)
385 continue;
386
387 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
388 p->gen_info_probes_table_values (probe->probe, &probe_fields);
389
390 gdb_assert (VEC_length (const_char_ptr, probe_fields)
391 == headings_size);
392
393 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
394 ++kx)
395 {
396 /* It is valid to have a NULL value here, which means that the
397 backend does not have something to write and this particular
398 field should be skipped. */
399 if (val == NULL)
400 continue;
401
402 size_max = max (strlen (val), size_max);
403 }
404 do_cleanups (c2);
405 }
406
407 ui_out_table_header (current_uiout, size_max, ui_left,
408 column->field_name, column->print_name);
409 }
410
411 do_cleanups (c);
412 }
413
414 /* Helper function to print extra information about a probe and an objfile
415 represented by PROBE. */
416
417 static void
418 print_ui_out_info (struct probe *probe)
419 {
420 int ix;
421 int j = 0;
422 /* `values' refers to the actual values of each new field in the output
423 of `info probe'. `headings' refers to the names of each new field. */
424 VEC (const_char_ptr) *values = NULL;
425 VEC (info_probe_column_s) *headings = NULL;
426 info_probe_column_s *column;
427 struct cleanup *c;
428
429 gdb_assert (probe != NULL);
430 gdb_assert (probe->pops != NULL);
431
432 if (probe->pops->gen_info_probes_table_header == NULL
433 && probe->pops->gen_info_probes_table_values == NULL)
434 return;
435
436 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
437 && probe->pops->gen_info_probes_table_values != NULL);
438
439 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
440 make_cleanup (VEC_cleanup (const_char_ptr), &values);
441
442 probe->pops->gen_info_probes_table_header (&headings);
443 probe->pops->gen_info_probes_table_values (probe, &values);
444
445 gdb_assert (VEC_length (info_probe_column_s, headings)
446 == VEC_length (const_char_ptr, values));
447
448 for (ix = 0;
449 VEC_iterate (info_probe_column_s, headings, ix, column);
450 ++ix)
451 {
452 const char *val = VEC_index (const_char_ptr, values, j++);
453
454 if (val == NULL)
455 ui_out_field_skip (current_uiout, column->field_name);
456 else
457 ui_out_field_string (current_uiout, column->field_name, val);
458 }
459
460 do_cleanups (c);
461 }
462
463 /* Helper function that returns the number of extra fields which POPS will
464 need. */
465
466 static int
467 get_number_extra_fields (const struct probe_ops *pops)
468 {
469 VEC (info_probe_column_s) *headings = NULL;
470 struct cleanup *c;
471 int n;
472
473 if (pops->gen_info_probes_table_header == NULL)
474 return 0;
475
476 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
477 pops->gen_info_probes_table_header (&headings);
478
479 n = VEC_length (info_probe_column_s, headings);
480
481 do_cleanups (c);
482
483 return n;
484 }
485
486 /* See comment in probe.h. */
487
488 void
489 info_probes_for_ops (const char *arg, int from_tty,
490 const struct probe_ops *pops)
491 {
492 char *provider, *probe_name = NULL, *objname = NULL;
493 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
494 VEC (bound_probe_s) *probes;
495 int i, any_found;
496 int ui_out_extra_fields = 0;
497 size_t size_addr;
498 size_t size_name = strlen ("Name");
499 size_t size_objname = strlen ("Object");
500 size_t size_provider = strlen ("Provider");
501 struct bound_probe *probe;
502 struct gdbarch *gdbarch = get_current_arch ();
503
504 /* Do we have a `provider:probe:objfile' style of linespec? */
505 provider = extract_arg_const (&arg);
506 if (provider)
507 {
508 make_cleanup (xfree, provider);
509
510 probe_name = extract_arg_const (&arg);
511 if (probe_name)
512 {
513 make_cleanup (xfree, probe_name);
514
515 objname = extract_arg_const (&arg);
516 if (objname)
517 make_cleanup (xfree, objname);
518 }
519 }
520
521 if (pops == NULL)
522 {
523 const struct probe_ops *po;
524 int ix;
525
526 /* If the probe_ops is NULL, it means the user has requested a "simple"
527 `info probes', i.e., she wants to print all information about all
528 probes. For that, we have to identify how many extra fields we will
529 need to add in the ui_out table.
530
531 To do that, we iterate over all probe_ops, querying each one about
532 its extra fields, and incrementing `ui_out_extra_fields' to reflect
533 that number. */
534
535 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
536 ui_out_extra_fields += get_number_extra_fields (po);
537 }
538 else
539 ui_out_extra_fields = get_number_extra_fields (pops);
540
541 probes = collect_probes (objname, provider, probe_name, pops);
542 make_cleanup (VEC_cleanup (probe_p), &probes);
543 make_cleanup_ui_out_table_begin_end (current_uiout,
544 4 + ui_out_extra_fields,
545 VEC_length (bound_probe_s, probes),
546 "StaticProbes");
547
548 if (!VEC_empty (bound_probe_s, probes))
549 qsort (VEC_address (bound_probe_s, probes),
550 VEC_length (bound_probe_s, probes),
551 sizeof (bound_probe_s), compare_probes);
552
553 /* What's the size of an address in our architecture? */
554 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
555
556 /* Determining the maximum size of each field (`provider', `name' and
557 `objname'). */
558 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
559 {
560 size_name = max (strlen (probe->probe->name), size_name);
561 size_provider = max (strlen (probe->probe->provider), size_provider);
562 size_objname = max (strlen (objfile_name (probe->objfile)), size_objname);
563 }
564
565 ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
566 _("Provider"));
567 ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
568 ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
569
570 if (pops == NULL)
571 {
572 const struct probe_ops *po;
573 int ix;
574
575 /* We have to generate the table header for each new probe type that we
576 will print. */
577 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
578 gen_ui_out_table_header_info (probes, po);
579 }
580 else
581 gen_ui_out_table_header_info (probes, pops);
582
583 ui_out_table_header (current_uiout, size_objname, ui_left, "object",
584 _("Object"));
585 ui_out_table_body (current_uiout);
586
587 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
588 {
589 struct cleanup *inner;
590
591 inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
592
593 ui_out_field_string (current_uiout, "provider", probe->probe->provider);
594 ui_out_field_string (current_uiout, "name", probe->probe->name);
595 ui_out_field_core_addr (current_uiout, "addr",
596 probe->probe->arch,
597 get_probe_address (probe->probe, probe->objfile));
598
599 if (pops == NULL)
600 {
601 const struct probe_ops *po;
602 int ix;
603
604 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
605 ++ix)
606 if (probe->probe->pops == po)
607 print_ui_out_info (probe->probe);
608 }
609 else
610 print_ui_out_info (probe->probe);
611
612 ui_out_field_string (current_uiout, "object",
613 objfile_name (probe->objfile));
614 ui_out_text (current_uiout, "\n");
615
616 do_cleanups (inner);
617 }
618
619 any_found = !VEC_empty (bound_probe_s, probes);
620 do_cleanups (cleanup);
621
622 if (!any_found)
623 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
624 }
625
626 /* Implementation of the `info probes' command. */
627
628 static void
629 info_probes_command (char *arg, int from_tty)
630 {
631 info_probes_for_ops (arg, from_tty, NULL);
632 }
633
634 /* See comments in probe.h. */
635
636 CORE_ADDR
637 get_probe_address (struct probe *probe, struct objfile *objfile)
638 {
639 return probe->pops->get_probe_address (probe, objfile);
640 }
641
642 /* See comments in probe.h. */
643
644 unsigned
645 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
646 {
647 return probe->pops->get_probe_argument_count (probe, frame);
648 }
649
650 /* See comments in probe.h. */
651
652 int
653 can_evaluate_probe_arguments (struct probe *probe)
654 {
655 return probe->pops->can_evaluate_probe_arguments (probe);
656 }
657
658 /* See comments in probe.h. */
659
660 struct value *
661 evaluate_probe_argument (struct probe *probe, unsigned n,
662 struct frame_info *frame)
663 {
664 return probe->pops->evaluate_probe_argument (probe, n, frame);
665 }
666
667 /* See comments in probe.h. */
668
669 struct value *
670 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
671 {
672 struct bound_probe probe;
673 unsigned n_args;
674
675 probe = find_probe_by_pc (get_frame_pc (frame));
676 if (!probe.probe)
677 return NULL;
678
679 n_args = get_probe_argument_count (probe.probe, frame);
680 if (n >= n_args)
681 return NULL;
682
683 return evaluate_probe_argument (probe.probe, n, frame);
684 }
685
686 /* See comment in probe.h. */
687
688 const struct probe_ops *
689 probe_linespec_to_ops (const char **linespecp)
690 {
691 int ix;
692 const struct probe_ops *probe_ops;
693
694 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
695 if (probe_ops->is_linespec (linespecp))
696 return probe_ops;
697
698 return NULL;
699 }
700
701 /* See comment in probe.h. */
702
703 int
704 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
705 {
706 const char *s = *linespecp;
707 const char *const *csp;
708
709 for (csp = keywords; *csp; csp++)
710 {
711 const char *keyword = *csp;
712 size_t len = strlen (keyword);
713
714 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
715 {
716 *linespecp += len + 1;
717 return 1;
718 }
719 }
720
721 return 0;
722 }
723
724 /* Implementation of `is_linespec' method for `struct probe_ops'. */
725
726 static int
727 probe_any_is_linespec (const char **linespecp)
728 {
729 static const char *const keywords[] = { "-p", "-probe", NULL };
730
731 return probe_is_linespec_by_keyword (linespecp, keywords);
732 }
733
734 /* Dummy method used for `probe_ops_any'. */
735
736 static void
737 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
738 {
739 /* No probes can be provided by this dummy backend. */
740 }
741
742 /* Operations associated with a generic probe. */
743
744 const struct probe_ops probe_ops_any =
745 {
746 probe_any_is_linespec,
747 probe_any_get_probes,
748 };
749
750 /* See comments in probe.h. */
751
752 struct cmd_list_element **
753 info_probes_cmdlist_get (void)
754 {
755 static struct cmd_list_element *info_probes_cmdlist;
756
757 if (info_probes_cmdlist == NULL)
758 add_prefix_cmd ("probes", class_info, info_probes_command,
759 _("\
760 Show available static probes.\n\
761 Usage: info probes [all|TYPE [ARGS]]\n\
762 TYPE specifies the type of the probe, and can be one of the following:\n\
763 - stap\n\
764 If you specify TYPE, there may be additional arguments needed by the\n\
765 subcommand.\n\
766 If you do not specify any argument, or specify `all', then the command\n\
767 will show information about all types of probes."),
768 &info_probes_cmdlist, "info probes ",
769 0/*allow-unknown*/, &infolist);
770
771 return &info_probes_cmdlist;
772 }
773
774 VEC (probe_ops_cp) *all_probe_ops;
775
776 void _initialize_probe (void);
777
778 void
779 _initialize_probe (void)
780 {
781 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
782
783 add_cmd ("all", class_info, info_probes_command,
784 _("\
785 Show information about all type of probes."),
786 info_probes_cmdlist_get ());
787 }
This page took 0.045778 seconds and 5 git commands to generate.