1 /* Generic static probe support for GDB.
3 Copyright (C) 2012-2019 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"
30 #include "gdb_regex.h"
32 #include "arch-utils.h"
39 #include "common/gdb_optional.h"
41 /* Class that implements the static probe methods for "any" probe. */
43 class any_static_probe_ops : public static_probe_ops
47 bool is_linespec (const char **linespecp) const override;
50 void get_probes (std::vector<probe *> *probesp,
51 struct objfile *objfile) const override;
54 const char *type_name () const override;
57 std::vector<struct info_probe_column> gen_info_probes_table_header
61 /* Static operations associated with a generic probe. */
63 const any_static_probe_ops any_static_probe_ops {};
65 /* A helper for parse_probes that decodes a probe specification in
66 SEARCH_PSPACE. It appends matching SALs to RESULT. */
69 parse_probes_in_pspace (const static_probe_ops *spops,
70 struct program_space *search_pspace,
71 const char *objfile_namestr,
74 std::vector<symtab_and_line> *result)
76 for (objfile *objfile : all_objfiles (search_pspace))
78 if (!objfile->sf || !objfile->sf->sym_probe_fns)
82 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
83 && FILENAME_CMP (lbasename (objfile_name (objfile)),
84 objfile_namestr) != 0)
87 const std::vector<probe *> &probes
88 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
90 for (probe *p : probes)
92 if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
95 if (provider != NULL && p->get_provider () != provider)
98 if (p->get_name () != name)
102 sal.pc = p->get_relocated_address (objfile);
104 sal.section = find_pc_overlay (sal.pc);
105 sal.pspace = search_pspace;
107 sal.objfile = objfile;
109 result->push_back (std::move (sal));
114 /* See definition in probe.h. */
116 std::vector<symtab_and_line>
117 parse_probes (const struct event_location *location,
118 struct program_space *search_pspace,
119 struct linespec_result *canonical)
122 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
123 const char *arg_start, *cs;
125 gdb_assert (event_location_type (location) == PROBE_LOCATION);
126 arg_start = get_probe_location (location);
129 const static_probe_ops *spops = probe_linespec_to_static_ops (&cs);
131 error (_("'%s' is not a probe linespec"), arg_start);
134 arg = skip_spaces (arg);
136 error (_("argument to `%s' missing"), arg_start);
138 arg_end = skip_to_space (arg);
140 /* We make a copy here so we can write over parts with impunity. */
141 std::string copy (arg, arg_end - arg);
144 /* Extract each word from the argument, separated by ":"s. */
145 p = strchr (arg, ':');
148 /* This is `-p name'. */
156 p = strchr (hold, ':');
159 /* This is `-p provider:name'. */
165 /* This is `-p objfile:provider:name'. */
167 objfile_namestr = arg;
174 error (_("no probe name specified"));
175 if (provider && *provider == '\0')
176 error (_("invalid provider name"));
177 if (objfile_namestr && *objfile_namestr == '\0')
178 error (_("invalid objfile name"));
180 std::vector<symtab_and_line> result;
181 if (search_pspace != NULL)
183 parse_probes_in_pspace (spops, search_pspace, objfile_namestr,
184 provider, name, &result);
188 struct program_space *pspace;
191 parse_probes_in_pspace (spops, pspace, objfile_namestr,
192 provider, name, &result);
197 throw_error (NOT_FOUND_ERROR,
198 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
199 objfile_namestr ? objfile_namestr : _("<any>"),
200 provider ? provider : _("<any>"),
206 std::string canon (arg_start, arg_end - arg_start);
207 canonical->special_display = 1;
208 canonical->pre_expanded = 1;
209 canonical->location = new_probe_location (canon.c_str ());
215 /* See definition in probe.h. */
218 find_probes_in_objfile (struct objfile *objfile, const char *provider,
221 std::vector<probe *> result;
223 if (!objfile->sf || !objfile->sf->sym_probe_fns)
226 const std::vector<probe *> &probes
227 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
228 for (probe *p : probes)
230 if (p->get_provider () != provider)
233 if (p->get_name () != name)
236 result.push_back (p);
242 /* See definition in probe.h. */
245 find_probe_by_pc (CORE_ADDR pc)
247 struct objfile *objfile;
248 struct bound_probe result;
250 result.objfile = NULL;
253 ALL_OBJFILES (objfile)
255 if (!objfile->sf || !objfile->sf->sym_probe_fns
256 || objfile->sect_index_text == -1)
259 /* If this proves too inefficient, we can replace with a hash. */
260 const std::vector<probe *> &probes
261 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
262 for (probe *p : probes)
263 if (p->get_relocated_address (objfile) == pc)
265 result.objfile = objfile;
276 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
277 If SPOPS is not &any_static_probe_ops, only probes related to this
278 specific static probe ops will match. Each argument is a regexp,
279 or NULL, which matches anything. */
281 static std::vector<bound_probe>
282 collect_probes (const std::string &objname, const std::string &provider,
283 const std::string &probe_name, const static_probe_ops *spops)
285 struct objfile *objfile;
286 std::vector<bound_probe> result;
287 gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
289 if (!provider.empty ())
290 prov_pat.emplace (provider.c_str (), REG_NOSUB,
291 _("Invalid provider regexp"));
292 if (!probe_name.empty ())
293 probe_pat.emplace (probe_name.c_str (), REG_NOSUB,
294 _("Invalid probe regexp"));
295 if (!objname.empty ())
296 obj_pat.emplace (objname.c_str (), REG_NOSUB,
297 _("Invalid object file regexp"));
299 ALL_OBJFILES (objfile)
301 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
306 if (obj_pat->exec (objfile_name (objfile), 0, NULL, 0) != 0)
310 const std::vector<probe *> &probes
311 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
313 for (probe *p : probes)
315 if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
319 && prov_pat->exec (p->get_provider ().c_str (), 0, NULL, 0) != 0)
323 && probe_pat->exec (p->get_name ().c_str (), 0, NULL, 0) != 0)
326 result.emplace_back (p, objfile);
333 /* A qsort comparison function for bound_probe_s objects. */
336 compare_probes (const bound_probe &a, const bound_probe &b)
340 v = a.prob->get_provider ().compare (b.prob->get_provider ());
344 v = a.prob->get_name ().compare (b.prob->get_name ());
348 if (a.prob->get_address () != b.prob->get_address ())
349 return a.prob->get_address () < b.prob->get_address ();
351 return strcmp (objfile_name (a.objfile), objfile_name (b.objfile)) < 0;
354 /* Helper function that generate entries in the ui_out table being
355 crafted by `info_probes_for_ops'. */
358 gen_ui_out_table_header_info (const std::vector<bound_probe> &probes,
359 const static_probe_ops *spops)
361 /* `headings' refers to the names of the columns when printing `info
363 gdb_assert (spops != NULL);
365 std::vector<struct info_probe_column> headings
366 = spops->gen_info_probes_table_header ();
368 for (const info_probe_column &column : headings)
370 size_t size_max = strlen (column.print_name);
372 for (const bound_probe &probe : probes)
374 /* `probe_fields' refers to the values of each new field that this
375 probe will display. */
377 if (probe.prob->get_static_ops () != spops)
380 std::vector<const char *> probe_fields
381 = probe.prob->gen_info_probes_table_values ();
383 gdb_assert (probe_fields.size () == headings.size ());
385 for (const char *val : probe_fields)
387 /* It is valid to have a NULL value here, which means that the
388 backend does not have something to write and this particular
389 field should be skipped. */
393 size_max = std::max (strlen (val), size_max);
397 current_uiout->table_header (size_max, ui_left,
398 column.field_name, column.print_name);
402 /* Helper function to print not-applicable strings for all the extra
403 columns defined in a static_probe_ops. */
406 print_ui_out_not_applicables (const static_probe_ops *spops)
408 std::vector<struct info_probe_column> headings
409 = spops->gen_info_probes_table_header ();
411 for (const info_probe_column &column : headings)
412 current_uiout->field_string (column.field_name, _("n/a"));
415 /* Helper function to print extra information about a probe and an objfile
416 represented by PROBE. */
419 print_ui_out_info (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 gdb_assert (probe != NULL);
424 std::vector<struct info_probe_column> headings
425 = probe->get_static_ops ()->gen_info_probes_table_header ();
426 std::vector<const char *> values
427 = probe->gen_info_probes_table_values ();
429 gdb_assert (headings.size () == values.size ());
431 for (int ix = 0; ix < headings.size (); ++ix)
433 struct info_probe_column column = headings[ix];
434 const char *val = values[ix];
437 current_uiout->field_skip (column.field_name);
439 current_uiout->field_string (column.field_name, val);
443 /* Helper function that returns the number of extra fields which POPS will
447 get_number_extra_fields (const static_probe_ops *spops)
449 return spops->gen_info_probes_table_header ().size ();
452 /* Helper function that returns true if there is a probe in PROBES
453 featuring the given SPOPS. It returns false otherwise. */
456 exists_probe_with_spops (const std::vector<bound_probe> &probes,
457 const static_probe_ops *spops)
459 for (const bound_probe &probe : probes)
460 if (probe.prob->get_static_ops () == spops)
466 /* Helper function that parses a probe linespec of the form [PROVIDER
467 [PROBE [OBJNAME]]] from the provided string STR. */
470 parse_probe_linespec (const char *str, std::string *provider,
471 std::string *probe_name, std::string *objname)
473 *probe_name = *objname = "";
475 *provider = extract_arg (&str);
476 if (!provider->empty ())
478 *probe_name = extract_arg (&str);
479 if (!probe_name->empty ())
480 *objname = extract_arg (&str);
484 /* See comment in probe.h. */
487 info_probes_for_spops (const char *arg, int from_tty,
488 const static_probe_ops *spops)
490 std::string provider, probe_name, objname;
492 int ui_out_extra_fields = 0;
494 size_t size_name = strlen ("Name");
495 size_t size_objname = strlen ("Object");
496 size_t size_provider = strlen ("Provider");
497 size_t size_type = strlen ("Type");
498 struct gdbarch *gdbarch = get_current_arch ();
500 parse_probe_linespec (arg, &provider, &probe_name, &objname);
502 std::vector<bound_probe> probes
503 = collect_probes (objname, provider, probe_name, spops);
505 if (spops == &any_static_probe_ops)
507 /* If SPOPS is &any_static_probe_ops, it means the user has
508 requested a "simple" `info probes', i.e., she wants to print
509 all information about all probes. For that, we have to
510 identify how many extra fields we will need to add in the
513 To do that, we iterate over all static_probe_ops, querying
514 each one about its extra fields, and incrementing
515 `ui_out_extra_fields' to reflect that number. But note that
516 we ignore the static_probe_ops for which no probes are
517 defined with the given search criteria. */
519 for (const static_probe_ops *po : all_static_probe_ops)
520 if (exists_probe_with_spops (probes, po))
521 ui_out_extra_fields += get_number_extra_fields (po);
524 ui_out_extra_fields = get_number_extra_fields (spops);
527 ui_out_emit_table table_emitter (current_uiout,
528 5 + ui_out_extra_fields,
529 probes.size (), "StaticProbes");
531 std::sort (probes.begin (), probes.end (), 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 (`type', `provider',
537 `name' and `objname'). */
538 for (const bound_probe &probe : probes)
540 const char *probe_type = probe.prob->get_static_ops ()->type_name ();
542 size_type = std::max (strlen (probe_type), size_type);
543 size_name = std::max (probe.prob->get_name ().size (), size_name);
544 size_provider = std::max (probe.prob->get_provider ().size (),
546 size_objname = std::max (strlen (objfile_name (probe.objfile)),
550 current_uiout->table_header (size_type, ui_left, "type", _("Type"));
551 current_uiout->table_header (size_provider, ui_left, "provider",
553 current_uiout->table_header (size_name, ui_left, "name", _("Name"));
554 current_uiout->table_header (size_addr, ui_left, "addr", _("Where"));
556 if (spops == &any_static_probe_ops)
558 /* We have to generate the table header for each new probe type
559 that we will print. Note that this excludes probe types not
560 having any defined probe with the search criteria. */
561 for (const static_probe_ops *po : all_static_probe_ops)
562 if (exists_probe_with_spops (probes, po))
563 gen_ui_out_table_header_info (probes, po);
566 gen_ui_out_table_header_info (probes, spops);
568 current_uiout->table_header (size_objname, ui_left, "object", _("Object"));
569 current_uiout->table_body ();
571 for (const bound_probe &probe : probes)
573 const char *probe_type = probe.prob->get_static_ops ()->type_name ();
575 ui_out_emit_tuple tuple_emitter (current_uiout, "probe");
577 current_uiout->field_string ("type", probe_type);
578 current_uiout->field_string ("provider",
579 probe.prob->get_provider ().c_str ());
580 current_uiout->field_string ("name", probe.prob->get_name ().c_str ());
581 current_uiout->field_core_addr ("addr", probe.prob->get_gdbarch (),
582 probe.prob->get_relocated_address
585 if (spops == &any_static_probe_ops)
587 for (const static_probe_ops *po : all_static_probe_ops)
589 if (probe.prob->get_static_ops () == po)
590 print_ui_out_info (probe.prob);
591 else if (exists_probe_with_spops (probes, po))
592 print_ui_out_not_applicables (po);
596 print_ui_out_info (probe.prob);
598 current_uiout->field_string ("object",
599 objfile_name (probe.objfile));
600 current_uiout->text ("\n");
603 any_found = !probes.empty ();
607 current_uiout->message (_("No probes matched.\n"));
610 /* Implementation of the `info probes' command. */
613 info_probes_command (const char *arg, int from_tty)
615 info_probes_for_spops (arg, from_tty, &any_static_probe_ops);
618 /* Implementation of the `enable probes' command. */
621 enable_probes_command (const char *arg, int from_tty)
623 std::string provider, probe_name, objname;
625 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
627 std::vector<bound_probe> probes
628 = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
631 current_uiout->message (_("No probes matched.\n"));
635 /* Enable the selected probes, provided their backends support the
636 notion of enabling a probe. */
637 for (const bound_probe &probe: probes)
639 if (probe.prob->get_static_ops ()->can_enable ())
641 probe.prob->enable ();
642 current_uiout->message (_("Probe %s:%s enabled.\n"),
643 probe.prob->get_provider ().c_str (),
644 probe.prob->get_name ().c_str ());
647 current_uiout->message (_("Probe %s:%s cannot be enabled.\n"),
648 probe.prob->get_provider ().c_str (),
649 probe.prob->get_name ().c_str ());
653 /* Implementation of the `disable probes' command. */
656 disable_probes_command (const char *arg, int from_tty)
658 std::string provider, probe_name, objname;
660 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
662 std::vector<bound_probe> probes
663 = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
666 current_uiout->message (_("No probes matched.\n"));
670 /* Disable the selected probes, provided their backends support the
671 notion of enabling a probe. */
672 for (const bound_probe &probe : probes)
674 if (probe.prob->get_static_ops ()->can_enable ())
676 probe.prob->disable ();
677 current_uiout->message (_("Probe %s:%s disabled.\n"),
678 probe.prob->get_provider ().c_str (),
679 probe.prob->get_name ().c_str ());
682 current_uiout->message (_("Probe %s:%s cannot be disabled.\n"),
683 probe.prob->get_provider ().c_str (),
684 probe.prob->get_name ().c_str ());
688 /* See comments in probe.h. */
691 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
693 struct bound_probe probe;
696 probe = find_probe_by_pc (get_frame_pc (frame));
700 n_args = probe.prob->get_argument_count (frame);
704 return probe.prob->evaluate_argument (n, frame);
707 /* See comment in probe.h. */
709 const struct static_probe_ops *
710 probe_linespec_to_static_ops (const char **linespecp)
712 for (const static_probe_ops *ops : all_static_probe_ops)
713 if (ops->is_linespec (linespecp))
719 /* See comment in probe.h. */
722 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
724 const char *s = *linespecp;
725 const char *const *csp;
727 for (csp = keywords; *csp; csp++)
729 const char *keyword = *csp;
730 size_t len = strlen (keyword);
732 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
734 *linespecp += len + 1;
742 /* Implementation of `is_linespec' method. */
745 any_static_probe_ops::is_linespec (const char **linespecp) const
747 static const char *const keywords[] = { "-p", "-probe", NULL };
749 return probe_is_linespec_by_keyword (linespecp, keywords);
752 /* Implementation of 'get_probes' method. */
755 any_static_probe_ops::get_probes (std::vector<probe *> *probesp,
756 struct objfile *objfile) const
758 /* No probes can be provided by this dummy backend. */
761 /* Implementation of the 'type_name' method. */
764 any_static_probe_ops::type_name () const
769 /* Implementation of the 'gen_info_probes_table_header' method. */
771 std::vector<struct info_probe_column>
772 any_static_probe_ops::gen_info_probes_table_header () const
774 return std::vector<struct info_probe_column> ();
777 /* See comments in probe.h. */
779 struct cmd_list_element **
780 info_probes_cmdlist_get (void)
782 static struct cmd_list_element *info_probes_cmdlist;
784 if (info_probes_cmdlist == NULL)
785 add_prefix_cmd ("probes", class_info, info_probes_command,
787 Show available static probes.\n\
788 Usage: info probes [all|TYPE [ARGS]]\n\
789 TYPE specifies the type of the probe, and can be one of the following:\n\
791 If you specify TYPE, there may be additional arguments needed by the\n\
793 If you do not specify any argument, or specify `all', then the command\n\
794 will show information about all types of probes."),
795 &info_probes_cmdlist, "info probes ",
796 0/*allow-unknown*/, &infolist);
798 return &info_probes_cmdlist;
803 /* This is called to compute the value of one of the $_probe_arg*
804 convenience variables. */
806 static struct value *
807 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
810 struct frame_info *frame = get_selected_frame (_("No frame selected"));
811 CORE_ADDR pc = get_frame_pc (frame);
812 int sel = (int) (uintptr_t) data;
813 struct bound_probe pc_probe;
816 /* SEL == -1 means "_probe_argc". */
817 gdb_assert (sel >= -1);
819 pc_probe = find_probe_by_pc (pc);
820 if (pc_probe.prob == NULL)
821 error (_("No probe at PC %s"), core_addr_to_string (pc));
823 n_args = pc_probe.prob->get_argument_count (frame);
825 return value_from_longest (builtin_type (arch)->builtin_int, n_args);
828 error (_("Invalid probe argument %d -- probe has %u arguments available"),
831 return pc_probe.prob->evaluate_argument (sel, frame);
834 /* This is called to compile one of the $_probe_arg* convenience
835 variables into an agent expression. */
838 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
839 struct axs_value *value, void *data)
841 CORE_ADDR pc = expr->scope;
842 int sel = (int) (uintptr_t) data;
843 struct bound_probe pc_probe;
845 struct frame_info *frame = get_selected_frame (NULL);
847 /* SEL == -1 means "_probe_argc". */
848 gdb_assert (sel >= -1);
850 pc_probe = find_probe_by_pc (pc);
851 if (pc_probe.prob == NULL)
852 error (_("No probe at PC %s"), core_addr_to_string (pc));
854 n_args = pc_probe.prob->get_argument_count (frame);
858 value->kind = axs_rvalue;
859 value->type = builtin_type (expr->gdbarch)->builtin_int;
860 ax_const_l (expr, n_args);
864 gdb_assert (sel >= 0);
866 error (_("Invalid probe argument %d -- probe has %d arguments available"),
869 pc_probe.prob->compile_to_ax (expr, value, sel);
872 static const struct internalvar_funcs probe_funcs =
880 std::vector<const static_probe_ops *> all_static_probe_ops;
883 _initialize_probe (void)
885 all_static_probe_ops.push_back (&any_static_probe_ops);
887 create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
888 (void *) (uintptr_t) -1);
889 create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
890 (void *) (uintptr_t) 0);
891 create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
892 (void *) (uintptr_t) 1);
893 create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
894 (void *) (uintptr_t) 2);
895 create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
896 (void *) (uintptr_t) 3);
897 create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
898 (void *) (uintptr_t) 4);
899 create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
900 (void *) (uintptr_t) 5);
901 create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
902 (void *) (uintptr_t) 6);
903 create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
904 (void *) (uintptr_t) 7);
905 create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
906 (void *) (uintptr_t) 8);
907 create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
908 (void *) (uintptr_t) 9);
909 create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
910 (void *) (uintptr_t) 10);
911 create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
912 (void *) (uintptr_t) 11);
914 add_cmd ("all", class_info, info_probes_command,
916 Show information about all type of probes."),
917 info_probes_cmdlist_get ());
919 add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
921 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
922 Each argument is a regular expression, used to select probes.\n\
923 PROVIDER matches probe provider names.\n\
924 NAME matches the probe names.\n\
925 OBJECT matches the executable or shared library name.\n\
926 If you do not specify any argument then the command will enable\n\
927 all defined probes."),
930 add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
932 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
933 Each argument is a regular expression, used to select probes.\n\
934 PROVIDER matches probe provider names.\n\
935 NAME matches the probe names.\n\
936 OBJECT matches the executable or shared library name.\n\
937 If you do not specify any argument then the command will disable\n\
938 all defined probes."),