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"
38 /* See definition in probe.h. */
40 struct symtabs_and_lines
41 parse_probes (char **argptr, struct linespec_result *canonical)
43 char *arg_start, *arg_end, *arg;
44 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
45 struct cleanup *cleanup;
46 struct symtabs_and_lines result;
47 struct objfile *objfile;
48 struct program_space *pspace;
49 const struct probe_ops *probe_ops;
58 probe_ops = probe_linespec_to_ops (&cs);
59 gdb_assert (probe_ops != NULL);
62 arg = skip_spaces (arg);
64 error (_("argument to `%s' missing"), arg_start);
66 arg_end = skip_to_space (arg);
68 /* We make a copy here so we can write over parts with impunity. */
69 arg = savestring (arg, arg_end - arg);
70 cleanup = make_cleanup (xfree, arg);
72 /* Extract each word from the argument, separated by ":"s. */
73 p = strchr (arg, ':');
76 /* This is `-p name'. */
84 p = strchr (hold, ':');
87 /* This is `-p provider:name'. */
93 /* This is `-p objfile:provider:name'. */
95 objfile_namestr = arg;
102 error (_("no probe name specified"));
103 if (provider && *provider == '\0')
104 error (_("invalid provider name"));
105 if (objfile_namestr && *objfile_namestr == '\0')
106 error (_("invalid objfile name"));
109 ALL_PSPACE_OBJFILES (pspace, objfile)
111 VEC (probe_p) *probes;
115 if (!objfile->sf || !objfile->sf->sym_probe_fns)
119 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
120 && FILENAME_CMP (lbasename (objfile_name (objfile)),
121 objfile_namestr) != 0)
124 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
126 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
128 struct symtab_and_line *sal;
130 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
133 if (provider && strcmp (probe->provider, provider) != 0)
136 if (strcmp (probe->name, name) != 0)
140 result.sals = xrealloc (result.sals,
142 * sizeof (struct symtab_and_line));
143 sal = &result.sals[result.nelts - 1];
147 sal->pc = probe->address;
148 sal->explicit_pc = 1;
149 sal->section = find_pc_overlay (sal->pc);
150 sal->pspace = pspace;
155 if (result.nelts == 0)
157 throw_error (NOT_FOUND_ERROR,
158 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
159 objfile_namestr ? objfile_namestr : _("<any>"),
160 provider ? provider : _("<any>"),
166 canonical->special_display = 1;
167 canonical->pre_expanded = 1;
168 canonical->addr_string = savestring (*argptr, arg_end - *argptr);
172 do_cleanups (cleanup);
177 /* See definition in probe.h. */
180 find_probes_in_objfile (struct objfile *objfile, const char *provider,
183 VEC (probe_p) *probes, *result = NULL;
187 if (!objfile->sf || !objfile->sf->sym_probe_fns)
190 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
191 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
193 if (strcmp (probe->provider, provider) != 0)
196 if (strcmp (probe->name, name) != 0)
199 VEC_safe_push (probe_p, result, probe);
205 /* See definition in probe.h. */
208 find_probe_by_pc (CORE_ADDR pc)
210 struct objfile *objfile;
212 ALL_OBJFILES (objfile)
214 VEC (probe_p) *probes;
218 if (!objfile->sf || !objfile->sf->sym_probe_fns)
221 /* If this proves too inefficient, we can replace with a hash. */
222 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
223 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
224 if (probe->address == pc)
233 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
234 If POPS is not NULL, only probes of this certain probe_ops will match.
235 Each argument is a regexp, or NULL, which matches anything. */
237 static VEC (probe_p) *
238 collect_probes (char *objname, char *provider, char *probe_name,
239 const struct probe_ops *pops)
241 struct objfile *objfile;
242 VEC (probe_p) *result = NULL;
243 struct cleanup *cleanup, *cleanup_temps;
244 regex_t obj_pat, prov_pat, probe_pat;
246 cleanup = make_cleanup (VEC_cleanup (probe_p), &result);
248 cleanup_temps = make_cleanup (null_cleanup, NULL);
249 if (provider != NULL)
250 compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
251 if (probe_name != NULL)
252 compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
254 compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
256 ALL_OBJFILES (objfile)
258 VEC (probe_p) *probes;
262 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
267 if (regexec (&obj_pat, objfile_name (objfile), 0, NULL, 0) != 0)
271 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
273 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
275 if (pops != NULL && probe->pops != pops)
279 && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
283 && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
286 VEC_safe_push (probe_p, result, probe);
290 do_cleanups (cleanup_temps);
291 discard_cleanups (cleanup);
295 /* A qsort comparison function for probe_p objects. */
298 compare_probes (const void *a, const void *b)
300 const struct probe *pa = *((const struct probe **) a);
301 const struct probe *pb = *((const struct probe **) b);
304 v = strcmp (pa->provider, pb->provider);
308 v = strcmp (pa->name, pb->name);
312 if (pa->address < pb->address)
314 if (pa->address > pb->address)
317 return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
320 /* Helper function that generate entries in the ui_out table being
321 crafted by `info_probes_for_ops'. */
324 gen_ui_out_table_header_info (VEC (probe_p) *probes,
325 const struct probe_ops *p)
327 /* `headings' refers to the names of the columns when printing `info
329 VEC (info_probe_column_s) *headings = NULL;
331 info_probe_column_s *column;
332 size_t headings_size;
335 gdb_assert (p != NULL);
337 if (p->gen_info_probes_table_header == NULL
338 && p->gen_info_probes_table_values == NULL)
341 gdb_assert (p->gen_info_probes_table_header != NULL
342 && p->gen_info_probes_table_values != NULL);
344 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
345 p->gen_info_probes_table_header (&headings);
347 headings_size = VEC_length (info_probe_column_s, headings);
350 VEC_iterate (info_probe_column_s, headings, ix, column);
355 size_t size_max = strlen (column->print_name);
357 for (jx = 0; VEC_iterate (probe_p, probes, jx, probe); ++jx)
359 /* `probe_fields' refers to the values of each new field that this
360 probe will display. */
361 VEC (const_char_ptr) *probe_fields = NULL;
366 if (probe->pops != p)
369 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
370 p->gen_info_probes_table_values (probe, &probe_fields);
372 gdb_assert (VEC_length (const_char_ptr, probe_fields)
375 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
378 /* It is valid to have a NULL value here, which means that the
379 backend does not have something to write and this particular
380 field should be skipped. */
384 size_max = max (strlen (val), size_max);
389 ui_out_table_header (current_uiout, size_max, ui_left,
390 column->field_name, column->print_name);
396 /* Helper function to print extra information about a probe and an objfile
397 represented by PROBE. */
400 print_ui_out_info (struct probe *probe)
404 /* `values' refers to the actual values of each new field in the output
405 of `info probe'. `headings' refers to the names of each new field. */
406 VEC (const_char_ptr) *values = NULL;
407 VEC (info_probe_column_s) *headings = NULL;
408 info_probe_column_s *column;
411 gdb_assert (probe != NULL);
412 gdb_assert (probe->pops != NULL);
414 if (probe->pops->gen_info_probes_table_header == NULL
415 && probe->pops->gen_info_probes_table_values == NULL)
418 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
419 && probe->pops->gen_info_probes_table_values != NULL);
421 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
422 make_cleanup (VEC_cleanup (const_char_ptr), &values);
424 probe->pops->gen_info_probes_table_header (&headings);
425 probe->pops->gen_info_probes_table_values (probe, &values);
427 gdb_assert (VEC_length (info_probe_column_s, headings)
428 == VEC_length (const_char_ptr, values));
431 VEC_iterate (info_probe_column_s, headings, ix, column);
434 const char *val = VEC_index (const_char_ptr, values, j++);
437 ui_out_field_skip (current_uiout, column->field_name);
439 ui_out_field_string (current_uiout, column->field_name, val);
445 /* Helper function that returns the number of extra fields which POPS will
449 get_number_extra_fields (const struct probe_ops *pops)
451 VEC (info_probe_column_s) *headings = NULL;
455 if (pops->gen_info_probes_table_header == NULL)
458 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
459 pops->gen_info_probes_table_header (&headings);
461 n = VEC_length (info_probe_column_s, headings);
468 /* See comment in probe.h. */
471 info_probes_for_ops (char *arg, int from_tty, const struct probe_ops *pops)
473 char *provider, *probe_name = NULL, *objname = NULL;
474 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
475 VEC (probe_p) *probes;
477 int ui_out_extra_fields = 0;
479 size_t size_name = strlen ("Name");
480 size_t size_objname = strlen ("Object");
481 size_t size_provider = strlen ("Provider");
483 struct gdbarch *gdbarch = get_current_arch ();
485 /* Do we have a `provider:probe:objfile' style of linespec? */
486 provider = extract_arg (&arg);
489 make_cleanup (xfree, provider);
491 probe_name = extract_arg (&arg);
494 make_cleanup (xfree, probe_name);
496 objname = extract_arg (&arg);
498 make_cleanup (xfree, objname);
504 const struct probe_ops *po;
507 /* If the probe_ops is NULL, it means the user has requested a "simple"
508 `info probes', i.e., she wants to print all information about all
509 probes. For that, we have to identify how many extra fields we will
510 need to add in the ui_out table.
512 To do that, we iterate over all probe_ops, querying each one about
513 its extra fields, and incrementing `ui_out_extra_fields' to reflect
516 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
517 ui_out_extra_fields += get_number_extra_fields (po);
520 ui_out_extra_fields = get_number_extra_fields (pops);
522 probes = collect_probes (objname, provider, probe_name, pops);
523 make_cleanup (VEC_cleanup (probe_p), &probes);
524 make_cleanup_ui_out_table_begin_end (current_uiout,
525 4 + ui_out_extra_fields,
526 VEC_length (probe_p, probes),
529 if (!VEC_empty (probe_p, probes))
530 qsort (VEC_address (probe_p, probes), VEC_length (probe_p, probes),
531 sizeof (probe_p), compare_probes);
533 /* What's the size of an address in our architecture? */
534 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
536 /* Determining the maximum size of each field (`provider', `name' and
538 for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
540 size_name = max (strlen (probe->name), size_name);
541 size_provider = max (strlen (probe->provider), size_provider);
542 size_objname = max (strlen (objfile_name (probe->objfile)), size_objname);
545 ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
547 ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
548 ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
552 const struct probe_ops *po;
555 /* We have to generate the table header for each new probe type that we
557 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
558 gen_ui_out_table_header_info (probes, po);
561 gen_ui_out_table_header_info (probes, pops);
563 ui_out_table_header (current_uiout, size_objname, ui_left, "object",
565 ui_out_table_body (current_uiout);
567 for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
569 struct cleanup *inner;
571 inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
573 ui_out_field_string (current_uiout, "provider", probe->provider);
574 ui_out_field_string (current_uiout, "name", probe->name);
575 ui_out_field_core_addr (current_uiout, "addr",
576 get_objfile_arch (probe->objfile),
581 const struct probe_ops *po;
584 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
586 if (probe->pops == po)
587 print_ui_out_info (probe);
590 print_ui_out_info (probe);
592 ui_out_field_string (current_uiout, "object",
593 objfile_name (probe->objfile));
594 ui_out_text (current_uiout, "\n");
599 any_found = !VEC_empty (probe_p, probes);
600 do_cleanups (cleanup);
603 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
606 /* Implementation of the `info probes' command. */
609 info_probes_command (char *arg, int from_tty)
611 info_probes_for_ops (arg, from_tty, NULL);
614 /* See comments in probe.h. */
617 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
619 return probe->pops->get_probe_argument_count (probe, frame);
622 /* See comments in probe.h. */
625 can_evaluate_probe_arguments (struct probe *probe)
627 return probe->pops->can_evaluate_probe_arguments (probe);
630 /* See comments in probe.h. */
633 evaluate_probe_argument (struct probe *probe, unsigned n,
634 struct frame_info *frame)
636 return probe->pops->evaluate_probe_argument (probe, n, frame);
639 /* See comments in probe.h. */
642 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
647 probe = find_probe_by_pc (get_frame_pc (frame));
651 n_args = get_probe_argument_count (probe, frame);
655 return evaluate_probe_argument (probe, n, frame);
658 /* See comment in probe.h. */
660 const struct probe_ops *
661 probe_linespec_to_ops (const char **linespecp)
664 const struct probe_ops *probe_ops;
666 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
667 if (probe_ops->is_linespec (linespecp))
673 /* See comment in probe.h. */
676 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
678 const char *s = *linespecp;
679 const char *const *csp;
681 for (csp = keywords; *csp; csp++)
683 const char *keyword = *csp;
684 size_t len = strlen (keyword);
686 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
688 *linespecp += len + 1;
696 /* Implementation of `is_linespec' method for `struct probe_ops'. */
699 probe_any_is_linespec (const char **linespecp)
701 static const char *const keywords[] = { "-p", "-probe", NULL };
703 return probe_is_linespec_by_keyword (linespecp, keywords);
706 /* Dummy method used for `probe_ops_any'. */
709 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
711 /* No probes can be provided by this dummy backend. */
714 /* Operations associated with a generic probe. */
716 const struct probe_ops probe_ops_any =
718 probe_any_is_linespec,
719 probe_any_get_probes,
722 /* See comments in probe.h. */
724 struct cmd_list_element **
725 info_probes_cmdlist_get (void)
727 static struct cmd_list_element *info_probes_cmdlist;
729 if (info_probes_cmdlist == NULL)
730 add_prefix_cmd ("probes", class_info, info_probes_command,
732 Show available static probes.\n\
733 Usage: info probes [all|TYPE [ARGS]]\n\
734 TYPE specifies the type of the probe, and can be one of the following:\n\
736 If you specify TYPE, there may be additional arguments needed by the\n\
738 If you do not specify any argument, or specify `all', then the command\n\
739 will show information about all types of probes."),
740 &info_probes_cmdlist, "info probes ",
741 0/*allow-unknown*/, &infolist);
743 return &info_probes_cmdlist;
746 VEC (probe_ops_cp) *all_probe_ops;
748 void _initialize_probe (void);
751 _initialize_probe (void)
753 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
755 add_cmd ("all", class_info, info_probes_command,
757 Show information about all type of probes."),
758 info_probes_cmdlist_get ());