1 /* Generic static probe support for GDB.
3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "cli/cli-cmds.h"
24 #include "cli/cli-utils.h"
27 #include "progspace.h"
28 #include "filenames.h"
29 #include "exceptions.h"
31 #include "gdb_regex.h"
33 #include "arch-utils.h"
36 typedef struct bound_probe bound_probe_s;
37 DEF_VEC_O (bound_probe_s);
41 /* See definition in probe.h. */
43 struct symtabs_and_lines
44 parse_probes (char **argptr, struct linespec_result *canonical)
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;
61 probe_ops = probe_linespec_to_ops (&cs);
62 gdb_assert (probe_ops != NULL);
65 arg = skip_spaces (arg);
67 error (_("argument to `%s' missing"), arg_start);
69 arg_end = skip_to_space (arg);
71 /* We make a copy here so we can write over parts with impunity. */
72 arg = savestring (arg, arg_end - arg);
73 cleanup = make_cleanup (xfree, arg);
75 /* Extract each word from the argument, separated by ":"s. */
76 p = strchr (arg, ':');
79 /* This is `-p name'. */
87 p = strchr (hold, ':');
90 /* This is `-p provider:name'. */
96 /* This is `-p objfile:provider:name'. */
98 objfile_namestr = arg;
105 error (_("no probe name specified"));
106 if (provider && *provider == '\0')
107 error (_("invalid provider name"));
108 if (objfile_namestr && *objfile_namestr == '\0')
109 error (_("invalid objfile name"));
112 ALL_PSPACE_OBJFILES (pspace, objfile)
114 VEC (probe_p) *probes;
118 if (!objfile->sf || !objfile->sf->sym_probe_fns)
122 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
123 && FILENAME_CMP (lbasename (objfile_name (objfile)),
124 objfile_namestr) != 0)
127 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
129 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
131 struct symtab_and_line *sal;
133 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
136 if (provider && strcmp (probe->provider, provider) != 0)
139 if (strcmp (probe->name, name) != 0)
143 result.sals = xrealloc (result.sals,
145 * sizeof (struct symtab_and_line));
146 sal = &result.sals[result.nelts - 1];
150 sal->pc = get_probe_address (probe, objfile);
151 sal->explicit_pc = 1;
152 sal->section = find_pc_overlay (sal->pc);
153 sal->pspace = pspace;
155 sal->objfile = objfile;
159 if (result.nelts == 0)
161 throw_error (NOT_FOUND_ERROR,
162 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
163 objfile_namestr ? objfile_namestr : _("<any>"),
164 provider ? provider : _("<any>"),
170 canonical->special_display = 1;
171 canonical->pre_expanded = 1;
172 canonical->addr_string = savestring (*argptr, arg_end - *argptr);
176 do_cleanups (cleanup);
181 /* See definition in probe.h. */
184 find_probes_in_objfile (struct objfile *objfile, const char *provider,
187 VEC (probe_p) *probes, *result = NULL;
191 if (!objfile->sf || !objfile->sf->sym_probe_fns)
194 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
195 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
197 if (strcmp (probe->provider, provider) != 0)
200 if (strcmp (probe->name, name) != 0)
203 VEC_safe_push (probe_p, result, probe);
209 /* See definition in probe.h. */
212 find_probe_by_pc (CORE_ADDR pc)
214 struct objfile *objfile;
215 struct bound_probe result;
217 result.objfile = NULL;
220 ALL_OBJFILES (objfile)
222 VEC (probe_p) *probes;
226 if (!objfile->sf || !objfile->sf->sym_probe_fns
227 || objfile->sect_index_text == -1)
230 /* If this proves too inefficient, we can replace with a hash. */
231 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
232 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
233 if (get_probe_address (probe, objfile) == pc)
235 result.objfile = objfile;
236 result.probe = probe;
246 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
247 If POPS is not NULL, only probes of this certain probe_ops will match.
248 Each argument is a regexp, or NULL, which matches anything. */
250 static VEC (bound_probe_s) *
251 collect_probes (char *objname, char *provider, char *probe_name,
252 const struct probe_ops *pops)
254 struct objfile *objfile;
255 VEC (bound_probe_s) *result = NULL;
256 struct cleanup *cleanup, *cleanup_temps;
257 regex_t obj_pat, prov_pat, probe_pat;
259 cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
261 cleanup_temps = make_cleanup (null_cleanup, NULL);
262 if (provider != NULL)
263 compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
264 if (probe_name != NULL)
265 compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
267 compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
269 ALL_OBJFILES (objfile)
271 VEC (probe_p) *probes;
275 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
280 if (regexec (&obj_pat, objfile_name (objfile), 0, NULL, 0) != 0)
284 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
286 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
288 struct bound_probe bound;
290 if (pops != NULL && probe->pops != pops)
294 && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
298 && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
301 bound.objfile = objfile;
303 VEC_safe_push (bound_probe_s, result, &bound);
307 do_cleanups (cleanup_temps);
308 discard_cleanups (cleanup);
312 /* A qsort comparison function for bound_probe_s objects. */
315 compare_probes (const void *a, const void *b)
317 const struct bound_probe *pa = (const struct bound_probe *) a;
318 const struct bound_probe *pb = (const struct bound_probe *) b;
321 v = strcmp (pa->probe->provider, pb->probe->provider);
325 v = strcmp (pa->probe->name, pb->probe->name);
329 if (pa->probe->address < pb->probe->address)
331 if (pa->probe->address > pb->probe->address)
334 return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
337 /* Helper function that generate entries in the ui_out table being
338 crafted by `info_probes_for_ops'. */
341 gen_ui_out_table_header_info (VEC (bound_probe_s) *probes,
342 const struct probe_ops *p)
344 /* `headings' refers to the names of the columns when printing `info
346 VEC (info_probe_column_s) *headings = NULL;
348 info_probe_column_s *column;
349 size_t headings_size;
352 gdb_assert (p != NULL);
354 if (p->gen_info_probes_table_header == NULL
355 && p->gen_info_probes_table_values == NULL)
358 gdb_assert (p->gen_info_probes_table_header != NULL
359 && p->gen_info_probes_table_values != NULL);
361 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
362 p->gen_info_probes_table_header (&headings);
364 headings_size = VEC_length (info_probe_column_s, headings);
367 VEC_iterate (info_probe_column_s, headings, ix, column);
370 struct bound_probe *probe;
372 size_t size_max = strlen (column->print_name);
374 for (jx = 0; VEC_iterate (bound_probe_s, probes, jx, probe); ++jx)
376 /* `probe_fields' refers to the values of each new field that this
377 probe will display. */
378 VEC (const_char_ptr) *probe_fields = NULL;
383 if (probe->probe->pops != p)
386 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
387 p->gen_info_probes_table_values (probe->probe, &probe_fields);
389 gdb_assert (VEC_length (const_char_ptr, probe_fields)
392 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
395 /* It is valid to have a NULL value here, which means that the
396 backend does not have something to write and this particular
397 field should be skipped. */
401 size_max = max (strlen (val), size_max);
406 ui_out_table_header (current_uiout, size_max, ui_left,
407 column->field_name, column->print_name);
413 /* Helper function to print extra information about a probe and an objfile
414 represented by PROBE. */
417 print_ui_out_info (struct probe *probe)
421 /* `values' refers to the actual values of each new field in the output
422 of `info probe'. `headings' refers to the names of each new field. */
423 VEC (const_char_ptr) *values = NULL;
424 VEC (info_probe_column_s) *headings = NULL;
425 info_probe_column_s *column;
428 gdb_assert (probe != NULL);
429 gdb_assert (probe->pops != NULL);
431 if (probe->pops->gen_info_probes_table_header == NULL
432 && probe->pops->gen_info_probes_table_values == NULL)
435 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
436 && probe->pops->gen_info_probes_table_values != NULL);
438 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
439 make_cleanup (VEC_cleanup (const_char_ptr), &values);
441 probe->pops->gen_info_probes_table_header (&headings);
442 probe->pops->gen_info_probes_table_values (probe, &values);
444 gdb_assert (VEC_length (info_probe_column_s, headings)
445 == VEC_length (const_char_ptr, values));
448 VEC_iterate (info_probe_column_s, headings, ix, column);
451 const char *val = VEC_index (const_char_ptr, values, j++);
454 ui_out_field_skip (current_uiout, column->field_name);
456 ui_out_field_string (current_uiout, column->field_name, val);
462 /* Helper function that returns the number of extra fields which POPS will
466 get_number_extra_fields (const struct probe_ops *pops)
468 VEC (info_probe_column_s) *headings = NULL;
472 if (pops->gen_info_probes_table_header == NULL)
475 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
476 pops->gen_info_probes_table_header (&headings);
478 n = VEC_length (info_probe_column_s, headings);
485 /* See comment in probe.h. */
488 info_probes_for_ops (char *arg, int from_tty, const struct probe_ops *pops)
490 char *provider, *probe_name = NULL, *objname = NULL;
491 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
492 VEC (bound_probe_s) *probes;
494 int ui_out_extra_fields = 0;
496 size_t size_name = strlen ("Name");
497 size_t size_objname = strlen ("Object");
498 size_t size_provider = strlen ("Provider");
499 struct bound_probe *probe;
500 struct gdbarch *gdbarch = get_current_arch ();
502 /* Do we have a `provider:probe:objfile' style of linespec? */
503 provider = extract_arg (&arg);
506 make_cleanup (xfree, provider);
508 probe_name = extract_arg (&arg);
511 make_cleanup (xfree, probe_name);
513 objname = extract_arg (&arg);
515 make_cleanup (xfree, objname);
521 const struct probe_ops *po;
524 /* If the probe_ops is NULL, it means the user has requested a "simple"
525 `info probes', i.e., she wants to print all information about all
526 probes. For that, we have to identify how many extra fields we will
527 need to add in the ui_out table.
529 To do that, we iterate over all probe_ops, querying each one about
530 its extra fields, and incrementing `ui_out_extra_fields' to reflect
533 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
534 ui_out_extra_fields += get_number_extra_fields (po);
537 ui_out_extra_fields = get_number_extra_fields (pops);
539 probes = collect_probes (objname, provider, probe_name, pops);
540 make_cleanup (VEC_cleanup (probe_p), &probes);
541 make_cleanup_ui_out_table_begin_end (current_uiout,
542 4 + ui_out_extra_fields,
543 VEC_length (bound_probe_s, probes),
546 if (!VEC_empty (bound_probe_s, probes))
547 qsort (VEC_address (bound_probe_s, probes),
548 VEC_length (bound_probe_s, probes),
549 sizeof (bound_probe_s), compare_probes);
551 /* What's the size of an address in our architecture? */
552 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
554 /* Determining the maximum size of each field (`provider', `name' and
556 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
558 size_name = max (strlen (probe->probe->name), size_name);
559 size_provider = max (strlen (probe->probe->provider), size_provider);
560 size_objname = max (strlen (objfile_name (probe->objfile)), size_objname);
563 ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
565 ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
566 ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
570 const struct probe_ops *po;
573 /* We have to generate the table header for each new probe type that we
575 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
576 gen_ui_out_table_header_info (probes, po);
579 gen_ui_out_table_header_info (probes, pops);
581 ui_out_table_header (current_uiout, size_objname, ui_left, "object",
583 ui_out_table_body (current_uiout);
585 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
587 struct cleanup *inner;
589 inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
591 ui_out_field_string (current_uiout, "provider", probe->probe->provider);
592 ui_out_field_string (current_uiout, "name", probe->probe->name);
593 ui_out_field_core_addr (current_uiout, "addr",
595 get_probe_address (probe->probe, probe->objfile));
599 const struct probe_ops *po;
602 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
604 if (probe->probe->pops == po)
605 print_ui_out_info (probe->probe);
608 print_ui_out_info (probe->probe);
610 ui_out_field_string (current_uiout, "object",
611 objfile_name (probe->objfile));
612 ui_out_text (current_uiout, "\n");
617 any_found = !VEC_empty (bound_probe_s, probes);
618 do_cleanups (cleanup);
621 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
624 /* Implementation of the `info probes' command. */
627 info_probes_command (char *arg, int from_tty)
629 info_probes_for_ops (arg, from_tty, NULL);
632 /* See comments in probe.h. */
635 get_probe_address (struct probe *probe, struct objfile *objfile)
637 return probe->pops->get_probe_address (probe, objfile);
640 /* See comments in probe.h. */
643 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
645 return probe->pops->get_probe_argument_count (probe, frame);
648 /* See comments in probe.h. */
651 can_evaluate_probe_arguments (struct probe *probe)
653 return probe->pops->can_evaluate_probe_arguments (probe);
656 /* See comments in probe.h. */
659 evaluate_probe_argument (struct probe *probe, unsigned n,
660 struct frame_info *frame)
662 return probe->pops->evaluate_probe_argument (probe, n, frame);
665 /* See comments in probe.h. */
668 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
670 struct bound_probe probe;
673 probe = find_probe_by_pc (get_frame_pc (frame));
677 n_args = get_probe_argument_count (probe.probe, frame);
681 return evaluate_probe_argument (probe.probe, n, frame);
684 /* See comment in probe.h. */
686 const struct probe_ops *
687 probe_linespec_to_ops (const char **linespecp)
690 const struct probe_ops *probe_ops;
692 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
693 if (probe_ops->is_linespec (linespecp))
699 /* See comment in probe.h. */
702 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
704 const char *s = *linespecp;
705 const char *const *csp;
707 for (csp = keywords; *csp; csp++)
709 const char *keyword = *csp;
710 size_t len = strlen (keyword);
712 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
714 *linespecp += len + 1;
722 /* Implementation of `is_linespec' method for `struct probe_ops'. */
725 probe_any_is_linespec (const char **linespecp)
727 static const char *const keywords[] = { "-p", "-probe", NULL };
729 return probe_is_linespec_by_keyword (linespecp, keywords);
732 /* Dummy method used for `probe_ops_any'. */
735 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
737 /* No probes can be provided by this dummy backend. */
740 /* Operations associated with a generic probe. */
742 const struct probe_ops probe_ops_any =
744 probe_any_is_linespec,
745 probe_any_get_probes,
748 /* See comments in probe.h. */
750 struct cmd_list_element **
751 info_probes_cmdlist_get (void)
753 static struct cmd_list_element *info_probes_cmdlist;
755 if (info_probes_cmdlist == NULL)
756 add_prefix_cmd ("probes", class_info, info_probes_command,
758 Show available static probes.\n\
759 Usage: info probes [all|TYPE [ARGS]]\n\
760 TYPE specifies the type of the probe, and can be one of the following:\n\
762 If you specify TYPE, there may be additional arguments needed by the\n\
764 If you do not specify any argument, or specify `all', then the command\n\
765 will show information about all types of probes."),
766 &info_probes_cmdlist, "info probes ",
767 0/*allow-unknown*/, &infolist);
769 return &info_probes_cmdlist;
772 VEC (probe_ops_cp) *all_probe_ops;
774 void _initialize_probe (void);
777 _initialize_probe (void)
779 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
781 add_cmd ("all", class_info, info_probes_command,
783 Show information about all type of probes."),
784 info_probes_cmdlist_get ());