1 /* Generic static probe support for GDB.
3 Copyright (C) 2012-2013 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_name = 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'. */
102 error (_("no probe name specified"));
103 if (provider && *provider == '\0')
104 error (_("invalid provider name"));
105 if (objfile_name && *objfile_name == '\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_name) != 0
120 && FILENAME_CMP (lbasename (objfile->name), objfile_name) != 0)
123 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
125 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
127 struct symtab_and_line *sal;
129 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
132 if (provider && strcmp (probe->provider, provider) != 0)
135 if (strcmp (probe->name, name) != 0)
139 result.sals = xrealloc (result.sals,
141 * sizeof (struct symtab_and_line));
142 sal = &result.sals[result.nelts - 1];
146 sal->pc = probe->address;
147 sal->explicit_pc = 1;
148 sal->section = find_pc_overlay (sal->pc);
149 sal->pspace = pspace;
154 if (result.nelts == 0)
156 throw_error (NOT_FOUND_ERROR,
157 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
158 objfile_name ? objfile_name : _("<any>"),
159 provider ? provider : _("<any>"),
165 canonical->special_display = 1;
166 canonical->pre_expanded = 1;
167 canonical->addr_string = savestring (*argptr, arg_end - *argptr);
171 do_cleanups (cleanup);
176 /* See definition in probe.h. */
179 find_probes_in_objfile (struct objfile *objfile, const char *provider,
182 VEC (probe_p) *probes, *result = NULL;
186 if (!objfile->sf || !objfile->sf->sym_probe_fns)
189 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
190 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
192 if (strcmp (probe->provider, provider) != 0)
195 if (strcmp (probe->name, name) != 0)
198 VEC_safe_push (probe_p, result, probe);
204 /* See definition in probe.h. */
207 find_probe_by_pc (CORE_ADDR pc)
209 struct objfile *objfile;
211 ALL_OBJFILES (objfile)
213 VEC (probe_p) *probes;
217 if (!objfile->sf || !objfile->sf->sym_probe_fns)
220 /* If this proves too inefficient, we can replace with a hash. */
221 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
222 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
223 if (probe->address == pc)
232 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
233 If POPS is not NULL, only probes of this certain probe_ops will match.
234 Each argument is a regexp, or NULL, which matches anything. */
236 static VEC (probe_p) *
237 collect_probes (char *objname, char *provider, char *probe_name,
238 const struct probe_ops *pops)
240 struct objfile *objfile;
241 VEC (probe_p) *result = NULL;
242 struct cleanup *cleanup, *cleanup_temps;
243 regex_t obj_pat, prov_pat, probe_pat;
245 cleanup = make_cleanup (VEC_cleanup (probe_p), &result);
247 cleanup_temps = make_cleanup (null_cleanup, NULL);
248 compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
249 compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
250 compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
252 ALL_OBJFILES (objfile)
254 VEC (probe_p) *probes;
258 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
263 if (regexec (&obj_pat, objfile->name, 0, NULL, 0) != 0)
267 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
269 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
271 if (pops != NULL && probe->pops != pops)
275 && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
279 && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
282 VEC_safe_push (probe_p, result, probe);
286 do_cleanups (cleanup_temps);
287 discard_cleanups (cleanup);
291 /* A qsort comparison function for probe_p objects. */
294 compare_probes (const void *a, const void *b)
296 const struct probe *pa = *((const struct probe **) a);
297 const struct probe *pb = *((const struct probe **) b);
300 v = strcmp (pa->provider, pb->provider);
304 v = strcmp (pa->name, pb->name);
308 if (pa->address < pb->address)
310 if (pa->address > pb->address)
313 return strcmp (pa->objfile->name, pb->objfile->name);
316 /* Helper function that generate entries in the ui_out table being
317 crafted by `info_probes_for_ops'. */
320 gen_ui_out_table_header_info (VEC (probe_p) *probes,
321 const struct probe_ops *p)
323 /* `headings' refers to the names of the columns when printing `info
325 VEC (info_probe_column_s) *headings = NULL;
327 info_probe_column_s *column;
328 size_t headings_size;
331 gdb_assert (p != NULL);
333 if (p->gen_info_probes_table_header == NULL
334 && p->gen_info_probes_table_values == NULL)
337 gdb_assert (p->gen_info_probes_table_header != NULL
338 && p->gen_info_probes_table_values != NULL);
340 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
341 p->gen_info_probes_table_header (&headings);
343 headings_size = VEC_length (info_probe_column_s, headings);
346 VEC_iterate (info_probe_column_s, headings, ix, column);
351 size_t size_max = strlen (column->print_name);
353 for (jx = 0; VEC_iterate (probe_p, probes, jx, probe); ++jx)
355 /* `probe_fields' refers to the values of each new field that this
356 probe will display. */
357 VEC (const_char_ptr) *probe_fields = NULL;
362 if (probe->pops != p)
365 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
366 p->gen_info_probes_table_values (probe, &probe_fields);
368 gdb_assert (VEC_length (const_char_ptr, probe_fields)
371 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
374 /* It is valid to have a NULL value here, which means that the
375 backend does not have something to write and this particular
376 field should be skipped. */
380 size_max = max (strlen (val), size_max);
385 ui_out_table_header (current_uiout, size_max, ui_left,
386 column->field_name, column->print_name);
392 /* Helper function to print extra information about a probe and an objfile
393 represented by PROBE. */
396 print_ui_out_info (struct probe *probe)
400 /* `values' refers to the actual values of each new field in the output
401 of `info probe'. `headings' refers to the names of each new field. */
402 VEC (const_char_ptr) *values = NULL;
403 VEC (info_probe_column_s) *headings = NULL;
404 info_probe_column_s *column;
407 gdb_assert (probe != NULL);
408 gdb_assert (probe->pops != NULL);
410 if (probe->pops->gen_info_probes_table_header == NULL
411 && probe->pops->gen_info_probes_table_values == NULL)
414 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
415 && probe->pops->gen_info_probes_table_values != NULL);
417 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
418 make_cleanup (VEC_cleanup (const_char_ptr), &values);
420 probe->pops->gen_info_probes_table_header (&headings);
421 probe->pops->gen_info_probes_table_values (probe, &values);
423 gdb_assert (VEC_length (info_probe_column_s, headings)
424 == VEC_length (const_char_ptr, values));
427 VEC_iterate (info_probe_column_s, headings, ix, column);
430 const char *val = VEC_index (const_char_ptr, values, j++);
433 ui_out_field_skip (current_uiout, column->field_name);
435 ui_out_field_string (current_uiout, column->field_name, val);
441 /* Helper function that returns the number of extra fields which POPS will
445 get_number_extra_fields (const struct probe_ops *pops)
447 VEC (info_probe_column_s) *headings = NULL;
451 if (pops->gen_info_probes_table_header == NULL)
454 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
455 pops->gen_info_probes_table_header (&headings);
457 n = VEC_length (info_probe_column_s, headings);
464 /* See comment in probe.h. */
467 info_probes_for_ops (char *arg, int from_tty, const struct probe_ops *pops)
469 char *provider, *probe_name = NULL, *objname = NULL;
470 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
471 VEC (probe_p) *probes;
473 int ui_out_extra_fields = 0;
475 size_t size_name = strlen ("Name");
476 size_t size_objname = strlen ("Object");
477 size_t size_provider = strlen ("Provider");
479 struct gdbarch *gdbarch = get_current_arch ();
481 /* Do we have a `provider:probe:objfile' style of linespec? */
482 provider = extract_arg (&arg);
485 make_cleanup (xfree, provider);
487 probe_name = extract_arg (&arg);
490 make_cleanup (xfree, probe_name);
492 objname = extract_arg (&arg);
494 make_cleanup (xfree, objname);
500 const struct probe_ops *po;
503 /* If the probe_ops is NULL, it means the user has requested a "simple"
504 `info probes', i.e., she wants to print all information about all
505 probes. For that, we have to identify how many extra fields we will
506 need to add in the ui_out table.
508 To do that, we iterate over all probe_ops, querying each one about
509 its extra fields, and incrementing `ui_out_extra_fields' to reflect
512 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
513 ui_out_extra_fields += get_number_extra_fields (po);
516 ui_out_extra_fields = get_number_extra_fields (pops);
518 probes = collect_probes (objname, provider, probe_name, pops);
519 make_cleanup (VEC_cleanup (probe_p), &probes);
520 make_cleanup_ui_out_table_begin_end (current_uiout,
521 4 + ui_out_extra_fields,
522 VEC_length (probe_p, probes),
525 if (!VEC_empty (probe_p, probes))
526 qsort (VEC_address (probe_p, probes), VEC_length (probe_p, probes),
527 sizeof (probe_p), compare_probes);
529 /* What's the size of an address in our architecture? */
530 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
532 /* Determining the maximum size of each field (`provider', `name' and
534 for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
536 size_name = max (strlen (probe->name), size_name);
537 size_provider = max (strlen (probe->provider), size_provider);
538 size_objname = max (strlen (probe->objfile->name), size_objname);
541 ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
543 ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
544 ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
548 const struct probe_ops *po;
551 /* We have to generate the table header for each new probe type that we
553 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
554 gen_ui_out_table_header_info (probes, po);
557 gen_ui_out_table_header_info (probes, pops);
559 ui_out_table_header (current_uiout, size_objname, ui_left, "object",
561 ui_out_table_body (current_uiout);
563 for (i = 0; VEC_iterate (probe_p, probes, i, probe); ++i)
565 struct cleanup *inner;
567 inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
569 ui_out_field_string (current_uiout, "provider", probe->provider);
570 ui_out_field_string (current_uiout, "name", probe->name);
571 ui_out_field_core_addr (current_uiout, "addr",
572 get_objfile_arch (probe->objfile),
577 const struct probe_ops *po;
580 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
582 if (probe->pops == po)
583 print_ui_out_info (probe);
586 print_ui_out_info (probe);
588 ui_out_field_string (current_uiout, "object", probe->objfile->name);
589 ui_out_text (current_uiout, "\n");
594 any_found = !VEC_empty (probe_p, probes);
595 do_cleanups (cleanup);
598 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
601 /* Implementation of the `info probes' command. */
604 info_probes_command (char *arg, int from_tty)
606 info_probes_for_ops (arg, from_tty, NULL);
609 /* See comments in probe.h. */
612 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
615 const struct sym_probe_fns *probe_fns;
618 probe = find_probe_by_pc (get_frame_pc (frame));
622 gdb_assert (probe->objfile != NULL);
623 gdb_assert (probe->objfile->sf != NULL);
624 gdb_assert (probe->objfile->sf->sym_probe_fns != NULL);
626 probe_fns = probe->objfile->sf->sym_probe_fns;
627 n_args = probe_fns->sym_get_probe_argument_count (probe);
632 return probe_fns->sym_evaluate_probe_argument (probe, n);
635 /* See comment in probe.h. */
637 const struct probe_ops *
638 probe_linespec_to_ops (const char **linespecp)
641 const struct probe_ops *probe_ops;
643 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
644 if (probe_ops->is_linespec (linespecp))
650 /* See comment in probe.h. */
653 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
655 const char *s = *linespecp;
656 const char *const *csp;
658 for (csp = keywords; *csp; csp++)
660 const char *keyword = *csp;
661 size_t len = strlen (keyword);
663 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
665 *linespecp += len + 1;
673 /* Implementation of `is_linespec' method for `struct probe_ops'. */
676 probe_any_is_linespec (const char **linespecp)
678 static const char *const keywords[] = { "-p", "-probe", NULL };
680 return probe_is_linespec_by_keyword (linespecp, keywords);
683 /* Dummy method used for `probe_ops_any'. */
686 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
688 /* No probes can be provided by this dummy backend. */
691 /* Operations associated with a generic probe. */
693 const struct probe_ops probe_ops_any =
695 probe_any_is_linespec,
696 probe_any_get_probes,
699 /* See comments in probe.h. */
701 struct cmd_list_element **
702 info_probes_cmdlist_get (void)
704 static struct cmd_list_element *info_probes_cmdlist;
706 if (info_probes_cmdlist == NULL)
707 add_prefix_cmd ("probes", class_info, info_probes_command,
709 Show available static probes.\n\
710 Usage: info probes [all|TYPE [ARGS]]\n\
711 TYPE specifies the type of the probe, and can be one of the following:\n\
713 If you specify TYPE, there may be additional arguments needed by the\n\
715 If you do not specify any argument, or specify `all', then the command\n\
716 will show information about all types of probes."),
717 &info_probes_cmdlist, "info probes ",
718 0/*allow-unknown*/, &infolist);
720 return &info_probes_cmdlist;
723 VEC (probe_ops_cp) *all_probe_ops;
725 void _initialize_probe (void);
728 _initialize_probe (void)
730 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
732 add_cmd ("all", class_info, info_probes_command,
734 Show information about all type of probes."),
735 info_probes_cmdlist_get ());