Update readelf's display of RELR sections to include the number of locations relocated
[binutils-gdb.git] / gdb / probe.c
blobb13baf00e714522ad38bf6c676a30629a873eaa7
1 /* Generic static probe support for GDB.
3 Copyright (C) 2012-2024 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/>. */
20 #include "probe.h"
21 #include "command.h"
22 #include "cli/cli-cmds.h"
23 #include "cli/cli-utils.h"
24 #include "objfiles.h"
25 #include "symtab.h"
26 #include "progspace.h"
27 #include "filenames.h"
28 #include "linespec.h"
29 #include "gdbsupport/gdb_regex.h"
30 #include "frame.h"
31 #include "arch-utils.h"
32 #include "value.h"
33 #include "ax.h"
34 #include "ax-gdb.h"
35 #include "location.h"
36 #include <ctype.h>
37 #include <algorithm>
38 #include <optional>
40 /* Class that implements the static probe methods for "any" probe. */
42 class any_static_probe_ops : public static_probe_ops
44 public:
45 /* See probe.h. */
46 bool is_linespec (const char **linespecp) const override;
48 /* See probe.h. */
49 void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
50 struct objfile *objfile) const override;
52 /* See probe.h. */
53 const char *type_name () const override;
55 /* See probe.h. */
56 std::vector<struct info_probe_column> gen_info_probes_table_header
57 () const override;
60 /* Static operations associated with a generic probe. */
62 const any_static_probe_ops any_static_probe_ops {};
64 /* A helper for parse_probes that decodes a probe specification in
65 SEARCH_PSPACE. It appends matching SALs to RESULT. */
67 static void
68 parse_probes_in_pspace (const static_probe_ops *spops,
69 struct program_space *search_pspace,
70 const char *objfile_namestr,
71 const char *provider,
72 const char *name,
73 std::vector<symtab_and_line> *result)
75 for (objfile *objfile : search_pspace->objfiles ())
77 if (!objfile->sf || !objfile->sf->sym_probe_fns)
78 continue;
80 if (objfile_namestr
81 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
82 && FILENAME_CMP (lbasename (objfile_name (objfile)),
83 objfile_namestr) != 0)
84 continue;
86 const std::vector<std::unique_ptr<probe>> &probes
87 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
89 for (auto &p : probes)
91 if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
92 continue;
94 if (provider != NULL && p->get_provider () != provider)
95 continue;
97 if (p->get_name () != name)
98 continue;
100 symtab_and_line sal;
101 sal.pc = p->get_relocated_address (objfile);
102 sal.explicit_pc = 1;
103 sal.section = find_pc_overlay (sal.pc);
104 sal.pspace = search_pspace;
105 sal.prob = p.get ();
106 sal.objfile = objfile;
108 result->push_back (std::move (sal));
113 /* See definition in probe.h. */
115 std::vector<symtab_and_line>
116 parse_probes (const location_spec *locspec,
117 struct program_space *search_pspace,
118 struct linespec_result *canonical)
120 char *arg_end, *arg;
121 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
122 const char *arg_start, *cs;
124 gdb_assert (locspec->type () == PROBE_LOCATION_SPEC);
125 arg_start = locspec->to_string ();
127 cs = arg_start;
128 const static_probe_ops *spops = probe_linespec_to_static_ops (&cs);
129 if (spops == NULL)
130 error (_("'%s' is not a probe linespec"), arg_start);
132 arg = (char *) cs;
133 arg = skip_spaces (arg);
134 if (!*arg)
135 error (_("argument to `%s' missing"), arg_start);
137 arg_end = skip_to_space (arg);
139 /* We make a copy here so we can write over parts with impunity. */
140 std::string copy (arg, arg_end - arg);
141 arg = &copy[0];
143 /* Extract each word from the argument, separated by ":"s. */
144 p = strchr (arg, ':');
145 if (p == NULL)
147 /* This is `-p name'. */
148 name = arg;
150 else
152 char *hold = p + 1;
154 *p = '\0';
155 p = strchr (hold, ':');
156 if (p == NULL)
158 /* This is `-p provider:name'. */
159 provider = arg;
160 name = hold;
162 else
164 /* This is `-p objfile:provider:name'. */
165 *p = '\0';
166 objfile_namestr = arg;
167 provider = hold;
168 name = p + 1;
172 if (*name == '\0')
173 error (_("no probe name specified"));
174 if (provider && *provider == '\0')
175 error (_("invalid provider name"));
176 if (objfile_namestr && *objfile_namestr == '\0')
177 error (_("invalid objfile name"));
179 std::vector<symtab_and_line> result;
180 if (search_pspace != NULL)
182 parse_probes_in_pspace (spops, search_pspace, objfile_namestr,
183 provider, name, &result);
185 else
187 for (struct program_space *pspace : program_spaces)
188 parse_probes_in_pspace (spops, pspace, objfile_namestr,
189 provider, name, &result);
192 if (result.empty ())
194 throw_error (NOT_FOUND_ERROR,
195 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
196 objfile_namestr ? objfile_namestr : _("<any>"),
197 provider ? provider : _("<any>"),
198 name);
201 if (canonical)
203 std::string canon (arg_start, arg_end - arg_start);
204 canonical->special_display = 1;
205 canonical->pre_expanded = 1;
206 canonical->locspec = new_probe_location_spec (std::move (canon));
209 return result;
212 /* See definition in probe.h. */
214 std::vector<probe *>
215 find_probes_in_objfile (struct objfile *objfile, const char *provider,
216 const char *name)
218 std::vector<probe *> result;
220 if (!objfile->sf || !objfile->sf->sym_probe_fns)
221 return result;
223 const std::vector<std::unique_ptr<probe>> &probes
224 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
225 for (auto &p : probes)
227 if (p->get_provider () != provider)
228 continue;
230 if (p->get_name () != name)
231 continue;
233 result.push_back (p.get ());
236 return result;
239 /* See definition in probe.h. */
241 struct bound_probe
242 find_probe_by_pc (CORE_ADDR pc)
244 struct bound_probe result;
246 result.objfile = NULL;
247 result.prob = NULL;
249 for (objfile *objfile : current_program_space->objfiles ())
251 if (!objfile->sf || !objfile->sf->sym_probe_fns
252 || objfile->sect_index_text == -1)
253 continue;
255 /* If this proves too inefficient, we can replace with a hash. */
256 const std::vector<std::unique_ptr<probe>> &probes
257 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
258 for (auto &p : probes)
259 if (p->get_relocated_address (objfile) == pc)
261 result.objfile = objfile;
262 result.prob = p.get ();
263 return result;
267 return result;
272 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
273 If SPOPS is not &any_static_probe_ops, only probes related to this
274 specific static probe ops will match. Each argument is a regexp,
275 or NULL, which matches anything. */
277 static std::vector<bound_probe>
278 collect_probes (const std::string &objname, const std::string &provider,
279 const std::string &probe_name, const static_probe_ops *spops)
281 std::vector<bound_probe> result;
282 std::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
284 if (!provider.empty ())
285 prov_pat.emplace (provider.c_str (), REG_NOSUB,
286 _("Invalid provider regexp"));
287 if (!probe_name.empty ())
288 probe_pat.emplace (probe_name.c_str (), REG_NOSUB,
289 _("Invalid probe regexp"));
290 if (!objname.empty ())
291 obj_pat.emplace (objname.c_str (), REG_NOSUB,
292 _("Invalid object file regexp"));
294 for (objfile *objfile : current_program_space->objfiles ())
296 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
297 continue;
299 if (obj_pat)
301 if (obj_pat->exec (objfile_name (objfile), 0, NULL, 0) != 0)
302 continue;
305 const std::vector<std::unique_ptr<probe>> &probes
306 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
308 for (auto &p : probes)
310 if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
311 continue;
313 if (prov_pat
314 && prov_pat->exec (p->get_provider ().c_str (), 0, NULL, 0) != 0)
315 continue;
317 if (probe_pat
318 && probe_pat->exec (p->get_name ().c_str (), 0, NULL, 0) != 0)
319 continue;
321 result.emplace_back (p.get (), objfile);
325 return result;
328 /* A qsort comparison function for bound_probe_s objects. */
330 static bool
331 compare_probes (const bound_probe &a, const bound_probe &b)
333 int v;
335 v = a.prob->get_provider ().compare (b.prob->get_provider ());
336 if (v != 0)
337 return v < 0;
339 v = a.prob->get_name ().compare (b.prob->get_name ());
340 if (v != 0)
341 return v < 0;
343 if (a.prob->get_address () != b.prob->get_address ())
344 return a.prob->get_address () < b.prob->get_address ();
346 return strcmp (objfile_name (a.objfile), objfile_name (b.objfile)) < 0;
349 /* Helper function that generate entries in the ui_out table being
350 crafted by `info_probes_for_ops'. */
352 static void
353 gen_ui_out_table_header_info (const std::vector<bound_probe> &probes,
354 const static_probe_ops *spops)
356 /* `headings' refers to the names of the columns when printing `info
357 probes'. */
358 gdb_assert (spops != NULL);
360 std::vector<struct info_probe_column> headings
361 = spops->gen_info_probes_table_header ();
363 for (const info_probe_column &column : headings)
365 size_t size_max = strlen (column.print_name);
367 for (const bound_probe &probe : probes)
369 /* `probe_fields' refers to the values of each new field that this
370 probe will display. */
372 if (probe.prob->get_static_ops () != spops)
373 continue;
375 std::vector<const char *> probe_fields
376 = probe.prob->gen_info_probes_table_values ();
378 gdb_assert (probe_fields.size () == headings.size ());
380 for (const char *val : probe_fields)
382 /* It is valid to have a NULL value here, which means that the
383 backend does not have something to write and this particular
384 field should be skipped. */
385 if (val == NULL)
386 continue;
388 size_max = std::max (strlen (val), size_max);
392 current_uiout->table_header (size_max, ui_left,
393 column.field_name, column.print_name);
397 /* Helper function to print not-applicable strings for all the extra
398 columns defined in a static_probe_ops. */
400 static void
401 print_ui_out_not_applicables (const static_probe_ops *spops)
403 std::vector<struct info_probe_column> headings
404 = spops->gen_info_probes_table_header ();
406 for (const info_probe_column &column : headings)
407 current_uiout->field_string (column.field_name, _("n/a"));
410 /* Helper function to print extra information about a probe and an objfile
411 represented by PROBE. */
413 static void
414 print_ui_out_info (probe *probe)
416 /* `values' refers to the actual values of each new field in the output
417 of `info probe'. `headings' refers to the names of each new field. */
418 gdb_assert (probe != NULL);
419 std::vector<struct info_probe_column> headings
420 = probe->get_static_ops ()->gen_info_probes_table_header ();
421 std::vector<const char *> values
422 = probe->gen_info_probes_table_values ();
424 gdb_assert (headings.size () == values.size ());
426 for (int ix = 0; ix < headings.size (); ++ix)
428 struct info_probe_column column = headings[ix];
429 const char *val = values[ix];
431 if (val == NULL)
432 current_uiout->field_skip (column.field_name);
433 else
434 current_uiout->field_string (column.field_name, val);
438 /* Helper function that returns the number of extra fields which POPS will
439 need. */
441 static int
442 get_number_extra_fields (const static_probe_ops *spops)
444 return spops->gen_info_probes_table_header ().size ();
447 /* Helper function that returns true if there is a probe in PROBES
448 featuring the given SPOPS. It returns false otherwise. */
450 static bool
451 exists_probe_with_spops (const std::vector<bound_probe> &probes,
452 const static_probe_ops *spops)
454 for (const bound_probe &probe : probes)
455 if (probe.prob->get_static_ops () == spops)
456 return true;
458 return false;
461 /* Helper function that parses a probe linespec of the form [PROVIDER
462 [PROBE [OBJNAME]]] from the provided string STR. */
464 static void
465 parse_probe_linespec (const char *str, std::string *provider,
466 std::string *probe_name, std::string *objname)
468 *probe_name = *objname = "";
470 *provider = extract_arg (&str);
471 if (!provider->empty ())
473 *probe_name = extract_arg (&str);
474 if (!probe_name->empty ())
475 *objname = extract_arg (&str);
479 /* See comment in probe.h. */
481 void
482 info_probes_for_spops (const char *arg, int from_tty,
483 const static_probe_ops *spops)
485 std::string provider, probe_name, objname;
486 int any_found;
487 int ui_out_extra_fields = 0;
488 size_t size_addr;
489 size_t size_name = strlen ("Name");
490 size_t size_objname = strlen ("Object");
491 size_t size_provider = strlen ("Provider");
492 size_t size_type = strlen ("Type");
493 struct gdbarch *gdbarch = get_current_arch ();
495 parse_probe_linespec (arg, &provider, &probe_name, &objname);
497 std::vector<bound_probe> probes
498 = collect_probes (objname, provider, probe_name, spops);
500 if (spops == &any_static_probe_ops)
502 /* If SPOPS is &any_static_probe_ops, it means the user has
503 requested a "simple" `info probes', i.e., she wants to print
504 all information about all probes. For that, we have to
505 identify how many extra fields we will need to add in the
506 ui_out table.
508 To do that, we iterate over all static_probe_ops, querying
509 each one about its extra fields, and incrementing
510 `ui_out_extra_fields' to reflect that number. But note that
511 we ignore the static_probe_ops for which no probes are
512 defined with the given search criteria. */
514 for (const static_probe_ops *po : all_static_probe_ops)
515 if (exists_probe_with_spops (probes, po))
516 ui_out_extra_fields += get_number_extra_fields (po);
518 else
519 ui_out_extra_fields = get_number_extra_fields (spops);
522 ui_out_emit_table table_emitter (current_uiout,
523 5 + ui_out_extra_fields,
524 probes.size (), "StaticProbes");
526 std::sort (probes.begin (), probes.end (), compare_probes);
528 /* What's the size of an address in our architecture? */
529 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
531 /* Determining the maximum size of each field (`type', `provider',
532 `name' and `objname'). */
533 for (const bound_probe &probe : probes)
535 const char *probe_type = probe.prob->get_static_ops ()->type_name ();
537 size_type = std::max (strlen (probe_type), size_type);
538 size_name = std::max (probe.prob->get_name ().size (), size_name);
539 size_provider = std::max (probe.prob->get_provider ().size (),
540 size_provider);
541 size_objname = std::max (strlen (objfile_name (probe.objfile)),
542 size_objname);
545 current_uiout->table_header (size_type, ui_left, "type", _("Type"));
546 current_uiout->table_header (size_provider, ui_left, "provider",
547 _("Provider"));
548 current_uiout->table_header (size_name, ui_left, "name", _("Name"));
549 current_uiout->table_header (size_addr, ui_left, "addr", _("Where"));
551 if (spops == &any_static_probe_ops)
553 /* We have to generate the table header for each new probe type
554 that we will print. Note that this excludes probe types not
555 having any defined probe with the search criteria. */
556 for (const static_probe_ops *po : all_static_probe_ops)
557 if (exists_probe_with_spops (probes, po))
558 gen_ui_out_table_header_info (probes, po);
560 else
561 gen_ui_out_table_header_info (probes, spops);
563 current_uiout->table_header (size_objname, ui_left, "object", _("Object"));
564 current_uiout->table_body ();
566 for (const bound_probe &probe : probes)
568 const char *probe_type = probe.prob->get_static_ops ()->type_name ();
570 ui_out_emit_tuple tuple_emitter (current_uiout, "probe");
572 current_uiout->field_string ("type", probe_type);
573 current_uiout->field_string ("provider", probe.prob->get_provider ());
574 current_uiout->field_string ("name", probe.prob->get_name ());
575 current_uiout->field_core_addr ("addr", probe.prob->get_gdbarch (),
576 probe.prob->get_relocated_address
577 (probe.objfile));
579 if (spops == &any_static_probe_ops)
581 for (const static_probe_ops *po : all_static_probe_ops)
583 if (probe.prob->get_static_ops () == po)
584 print_ui_out_info (probe.prob);
585 else if (exists_probe_with_spops (probes, po))
586 print_ui_out_not_applicables (po);
589 else
590 print_ui_out_info (probe.prob);
592 current_uiout->field_string ("object",
593 objfile_name (probe.objfile));
594 current_uiout->text ("\n");
597 any_found = !probes.empty ();
600 if (!any_found)
601 current_uiout->message (_("No probes matched.\n"));
604 /* Implementation of the `info probes' command. */
606 static void
607 info_probes_command (const char *arg, int from_tty)
609 info_probes_for_spops (arg, from_tty, &any_static_probe_ops);
612 /* Implementation of the `enable probes' command. */
614 static void
615 enable_probes_command (const char *arg, int from_tty)
617 std::string provider, probe_name, objname;
619 parse_probe_linespec (arg, &provider, &probe_name, &objname);
621 std::vector<bound_probe> probes
622 = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
623 if (probes.empty ())
625 current_uiout->message (_("No probes matched.\n"));
626 return;
629 /* Enable the selected probes, provided their backends support the
630 notion of enabling a probe. */
631 for (const bound_probe &probe: probes)
633 if (probe.prob->get_static_ops ()->can_enable ())
635 probe.prob->enable ();
636 current_uiout->message (_("Probe %s:%s enabled.\n"),
637 probe.prob->get_provider ().c_str (),
638 probe.prob->get_name ().c_str ());
640 else
641 current_uiout->message (_("Probe %s:%s cannot be enabled.\n"),
642 probe.prob->get_provider ().c_str (),
643 probe.prob->get_name ().c_str ());
647 /* Implementation of the `disable probes' command. */
649 static void
650 disable_probes_command (const char *arg, int from_tty)
652 std::string provider, probe_name, objname;
654 parse_probe_linespec (arg, &provider, &probe_name, &objname);
656 std::vector<bound_probe> probes
657 = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
658 if (probes.empty ())
660 current_uiout->message (_("No probes matched.\n"));
661 return;
664 /* Disable the selected probes, provided their backends support the
665 notion of enabling a probe. */
666 for (const bound_probe &probe : probes)
668 if (probe.prob->get_static_ops ()->can_enable ())
670 probe.prob->disable ();
671 current_uiout->message (_("Probe %s:%s disabled.\n"),
672 probe.prob->get_provider ().c_str (),
673 probe.prob->get_name ().c_str ());
675 else
676 current_uiout->message (_("Probe %s:%s cannot be disabled.\n"),
677 probe.prob->get_provider ().c_str (),
678 probe.prob->get_name ().c_str ());
682 static bool ignore_probes_p = false;
683 static bool ignore_probes_idx = 0;
684 static bool ignore_probes_verbose_p;
685 static std::optional<compiled_regex> ignore_probes_prov_pat[2];
686 static std::optional<compiled_regex> ignore_probes_name_pat[2];
687 static std::optional<compiled_regex> ignore_probes_obj_pat[2];
689 /* See comments in probe.h. */
691 bool
692 ignore_probe_p (const char *provider, const char *name,
693 const char *objfile_name, const char *type)
695 if (!ignore_probes_p)
696 return false;
698 std::optional<compiled_regex> &re_prov
699 = ignore_probes_prov_pat[ignore_probes_idx];
700 std::optional<compiled_regex> &re_name
701 = ignore_probes_name_pat[ignore_probes_idx];
702 std::optional<compiled_regex> &re_obj
703 = ignore_probes_obj_pat[ignore_probes_idx];
705 bool res
706 = ((!re_prov
707 || re_prov->exec (provider, 0, NULL, 0) == 0)
708 && (!re_name
709 || re_name->exec (name, 0, NULL, 0) == 0)
710 && (!re_obj
711 || re_obj->exec (objfile_name, 0, NULL, 0) == 0));
713 if (res && ignore_probes_verbose_p)
714 gdb_printf (gdb_stdlog, _("Ignoring %s probe %s %s in %s.\n"),
715 type, provider, name, objfile_name);
717 return res;
720 /* Implementation of the `maintenance ignore-probes' command. */
722 static void
723 ignore_probes_command (const char *arg, int from_tty)
725 std::string ignore_provider, ignore_probe_name, ignore_objname;
727 bool verbose_p = false;
728 if (arg != nullptr)
730 const char *idx = arg;
731 std::string s = extract_arg (&idx);
733 if (strcmp (s.c_str (), "-reset") == 0)
735 if (*idx != '\0')
736 error (_("-reset: no arguments allowed"));
738 ignore_probes_p = false;
739 gdb_printf (gdb_stdout, _("ignore-probes filter has been reset\n"));
740 return;
743 if (strcmp (s.c_str (), "-verbose") == 0
744 || strcmp (s.c_str (), "-v") == 0)
746 verbose_p = true;
747 arg = idx;
751 parse_probe_linespec (arg, &ignore_provider, &ignore_probe_name,
752 &ignore_objname);
754 /* Parse the regular expressions, making sure that the old regular
755 expressions are still valid if an exception is throw. */
756 int new_ignore_probes_idx = 1 - ignore_probes_idx;
757 std::optional<compiled_regex> &re_prov
758 = ignore_probes_prov_pat[new_ignore_probes_idx];
759 std::optional<compiled_regex> &re_name
760 = ignore_probes_name_pat[new_ignore_probes_idx];
761 std::optional<compiled_regex> &re_obj
762 = ignore_probes_obj_pat[new_ignore_probes_idx];
763 re_prov.reset ();
764 re_name.reset ();
765 re_obj.reset ();
766 if (!ignore_provider.empty ())
767 re_prov.emplace (ignore_provider.c_str (), REG_NOSUB,
768 _("Invalid provider regexp"));
769 if (!ignore_probe_name.empty ())
770 re_name.emplace (ignore_probe_name.c_str (), REG_NOSUB,
771 _("Invalid probe regexp"));
772 if (!ignore_objname.empty ())
773 re_obj.emplace (ignore_objname.c_str (), REG_NOSUB,
774 _("Invalid object file regexp"));
775 ignore_probes_idx = new_ignore_probes_idx;
777 ignore_probes_p = true;
778 ignore_probes_verbose_p = verbose_p;
779 gdb_printf (gdb_stdout, _("ignore-probes filter has been set to:\n"));
780 gdb_printf (gdb_stdout, _("PROVIDER: '%s'\n"), ignore_provider.c_str ());
781 gdb_printf (gdb_stdout, _("PROBE_NAME: '%s'\n"), ignore_probe_name.c_str ());
782 gdb_printf (gdb_stdout, _("OBJNAME: '%s'\n"), ignore_objname.c_str ());
785 /* See comments in probe.h. */
787 struct value *
788 probe_safe_evaluate_at_pc (const frame_info_ptr &frame, unsigned n)
790 struct bound_probe probe;
791 unsigned n_args;
793 probe = find_probe_by_pc (get_frame_pc (frame));
794 if (!probe.prob)
795 return NULL;
797 n_args = probe.prob->get_argument_count (get_frame_arch (frame));
798 if (n >= n_args)
799 return NULL;
801 return probe.prob->evaluate_argument (n, frame);
804 /* See comment in probe.h. */
806 const struct static_probe_ops *
807 probe_linespec_to_static_ops (const char **linespecp)
809 for (const static_probe_ops *ops : all_static_probe_ops)
810 if (ops->is_linespec (linespecp))
811 return ops;
813 return NULL;
816 /* See comment in probe.h. */
819 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
821 const char *s = *linespecp;
822 const char *const *csp;
824 for (csp = keywords; *csp; csp++)
826 const char *keyword = *csp;
827 size_t len = strlen (keyword);
829 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
831 *linespecp += len + 1;
832 return 1;
836 return 0;
839 /* Implementation of `is_linespec' method. */
841 bool
842 any_static_probe_ops::is_linespec (const char **linespecp) const
844 static const char *const keywords[] = { "-p", "-probe", NULL };
846 return probe_is_linespec_by_keyword (linespecp, keywords);
849 /* Implementation of 'get_probes' method. */
851 void
852 any_static_probe_ops::get_probes (std::vector<std::unique_ptr<probe>> *probesp,
853 struct objfile *objfile) const
855 /* No probes can be provided by this dummy backend. */
858 /* Implementation of the 'type_name' method. */
860 const char *
861 any_static_probe_ops::type_name () const
863 return NULL;
866 /* Implementation of the 'gen_info_probes_table_header' method. */
868 std::vector<struct info_probe_column>
869 any_static_probe_ops::gen_info_probes_table_header () const
871 return std::vector<struct info_probe_column> ();
874 /* See comments in probe.h. */
876 struct cmd_list_element **
877 info_probes_cmdlist_get (void)
879 static struct cmd_list_element *info_probes_cmdlist;
881 if (info_probes_cmdlist == NULL)
882 add_prefix_cmd ("probes", class_info, info_probes_command,
883 _("\
884 Show available static probes.\n\
885 Usage: info probes [all|TYPE [ARGS]]\n\
886 TYPE specifies the type of the probe, and can be one of the following:\n\
887 - stap\n\
888 If you specify TYPE, there may be additional arguments needed by the\n\
889 subcommand.\n\
890 If you do not specify any argument, or specify `all', then the command\n\
891 will show information about all types of probes."),
892 &info_probes_cmdlist, 0/*allow-unknown*/, &infolist);
894 return &info_probes_cmdlist;
899 /* This is called to compute the value of one of the $_probe_arg*
900 convenience variables. */
902 static struct value *
903 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
904 void *data)
906 frame_info_ptr frame = get_selected_frame (_("No frame selected"));
907 CORE_ADDR pc = get_frame_pc (frame);
908 int sel = (int) (uintptr_t) data;
909 struct bound_probe pc_probe;
910 unsigned n_args;
912 /* SEL == -1 means "_probe_argc". */
913 gdb_assert (sel >= -1);
915 pc_probe = find_probe_by_pc (pc);
916 if (pc_probe.prob == NULL)
917 error (_("No probe at PC %s"), core_addr_to_string (pc));
919 n_args = pc_probe.prob->get_argument_count (arch);
920 if (sel == -1)
921 return value_from_longest (builtin_type (arch)->builtin_int, n_args);
923 if (sel >= n_args)
924 error (_("Invalid probe argument %d -- probe has %u arguments available"),
925 sel, n_args);
927 return pc_probe.prob->evaluate_argument (sel, frame);
930 /* This is called to compile one of the $_probe_arg* convenience
931 variables into an agent expression. */
933 static void
934 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
935 struct axs_value *value, void *data)
937 CORE_ADDR pc = expr->scope;
938 int sel = (int) (uintptr_t) data;
939 struct bound_probe pc_probe;
940 int n_args;
942 /* SEL == -1 means "_probe_argc". */
943 gdb_assert (sel >= -1);
945 pc_probe = find_probe_by_pc (pc);
946 if (pc_probe.prob == NULL)
947 error (_("No probe at PC %s"), core_addr_to_string (pc));
949 n_args = pc_probe.prob->get_argument_count (expr->gdbarch);
951 if (sel == -1)
953 value->kind = axs_rvalue;
954 value->type = builtin_type (expr->gdbarch)->builtin_int;
955 ax_const_l (expr, n_args);
956 return;
959 gdb_assert (sel >= 0);
960 if (sel >= n_args)
961 error (_("Invalid probe argument %d -- probe has %d arguments available"),
962 sel, n_args);
964 pc_probe.prob->compile_to_ax (expr, value, sel);
967 static const struct internalvar_funcs probe_funcs =
969 compute_probe_arg,
970 compile_probe_arg,
974 std::vector<const static_probe_ops *> all_static_probe_ops;
976 void _initialize_probe ();
977 void
978 _initialize_probe ()
980 all_static_probe_ops.push_back (&any_static_probe_ops);
982 create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
983 (void *) (uintptr_t) -1);
984 create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
985 (void *) (uintptr_t) 0);
986 create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
987 (void *) (uintptr_t) 1);
988 create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
989 (void *) (uintptr_t) 2);
990 create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
991 (void *) (uintptr_t) 3);
992 create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
993 (void *) (uintptr_t) 4);
994 create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
995 (void *) (uintptr_t) 5);
996 create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
997 (void *) (uintptr_t) 6);
998 create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
999 (void *) (uintptr_t) 7);
1000 create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1001 (void *) (uintptr_t) 8);
1002 create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1003 (void *) (uintptr_t) 9);
1004 create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1005 (void *) (uintptr_t) 10);
1006 create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1007 (void *) (uintptr_t) 11);
1009 add_cmd ("all", class_info, info_probes_command,
1010 _("\
1011 Show information about all type of probes."),
1012 info_probes_cmdlist_get ());
1014 add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
1015 Enable probes.\n\
1016 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
1017 Each argument is a regular expression, used to select probes.\n\
1018 PROVIDER matches probe provider names.\n\
1019 NAME matches the probe names.\n\
1020 OBJECT matches the executable or shared library name.\n\
1021 If you do not specify any argument then the command will enable\n\
1022 all defined probes."),
1023 &enablelist);
1025 add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
1026 Disable probes.\n\
1027 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
1028 Each argument is a regular expression, used to select probes.\n\
1029 PROVIDER matches probe provider names.\n\
1030 NAME matches the probe names.\n\
1031 OBJECT matches the executable or shared library name.\n\
1032 If you do not specify any argument then the command will disable\n\
1033 all defined probes."),
1034 &disablelist);
1036 add_cmd ("ignore-probes", class_maintenance, ignore_probes_command, _("\
1037 Ignore probes.\n\
1038 Usage: maintenance ignore-probes [-v|-verbose] [PROVIDER [NAME [OBJECT]]]\n\
1039 maintenance ignore-probes -reset\n\
1040 Each argument is a regular expression, used to select probes.\n\
1041 PROVIDER matches probe provider names.\n\
1042 NAME matches the probe names.\n\
1043 OBJECT matches the executable or shared library name.\n\
1044 If you do not specify any argument then the command will ignore\n\
1045 all defined probes. To reset the ignore-probes filter, use the -reset form.\n\
1046 Only supported for SystemTap probes."),
1047 &maintenancelist);