S390: Add target descriptions for vector register sets
[binutils-gdb.git] / gdb / probe.c
blobdce2b25967de09b3b74127ae41ef003356db1b32
1 /* Generic static probe support for GDB.
3 Copyright (C) 2012-2015 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 "defs.h"
21 #include "probe.h"
22 #include "command.h"
23 #include "cli/cli-cmds.h"
24 #include "cli/cli-utils.h"
25 #include "objfiles.h"
26 #include "symtab.h"
27 #include "progspace.h"
28 #include "filenames.h"
29 #include "linespec.h"
30 #include "gdb_regex.h"
31 #include "frame.h"
32 #include "arch-utils.h"
33 #include "value.h"
34 #include "ax.h"
35 #include "ax-gdb.h"
36 #include <ctype.h>
38 typedef struct bound_probe bound_probe_s;
39 DEF_VEC_O (bound_probe_s);
43 /* See definition in probe.h. */
45 struct symtabs_and_lines
46 parse_probes (char **argptr, struct linespec_result *canonical)
48 char *arg_start, *arg_end, *arg;
49 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
50 struct cleanup *cleanup;
51 struct symtabs_and_lines result;
52 struct objfile *objfile;
53 struct program_space *pspace;
54 const struct probe_ops *probe_ops;
55 const char *cs;
57 result.sals = NULL;
58 result.nelts = 0;
60 arg_start = *argptr;
62 cs = *argptr;
63 probe_ops = probe_linespec_to_ops (&cs);
64 if (probe_ops == NULL)
65 error (_("'%s' is not a probe linespec"), arg_start);
67 arg = (char *) cs;
68 arg = skip_spaces (arg);
69 if (!*arg)
70 error (_("argument to `%s' missing"), arg_start);
72 arg_end = skip_to_space (arg);
74 /* We make a copy here so we can write over parts with impunity. */
75 arg = savestring (arg, arg_end - arg);
76 cleanup = make_cleanup (xfree, arg);
78 /* Extract each word from the argument, separated by ":"s. */
79 p = strchr (arg, ':');
80 if (p == NULL)
82 /* This is `-p name'. */
83 name = arg;
85 else
87 char *hold = p + 1;
89 *p = '\0';
90 p = strchr (hold, ':');
91 if (p == NULL)
93 /* This is `-p provider:name'. */
94 provider = arg;
95 name = hold;
97 else
99 /* This is `-p objfile:provider:name'. */
100 *p = '\0';
101 objfile_namestr = arg;
102 provider = hold;
103 name = p + 1;
107 if (*name == '\0')
108 error (_("no probe name specified"));
109 if (provider && *provider == '\0')
110 error (_("invalid provider name"));
111 if (objfile_namestr && *objfile_namestr == '\0')
112 error (_("invalid objfile name"));
114 ALL_PSPACES (pspace)
115 ALL_PSPACE_OBJFILES (pspace, objfile)
117 VEC (probe_p) *probes;
118 struct probe *probe;
119 int ix;
121 if (!objfile->sf || !objfile->sf->sym_probe_fns)
122 continue;
124 if (objfile_namestr
125 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
126 && FILENAME_CMP (lbasename (objfile_name (objfile)),
127 objfile_namestr) != 0)
128 continue;
130 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
132 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
134 struct symtab_and_line *sal;
136 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
137 continue;
139 if (provider && strcmp (probe->provider, provider) != 0)
140 continue;
142 if (strcmp (probe->name, name) != 0)
143 continue;
145 ++result.nelts;
146 result.sals = xrealloc (result.sals,
147 result.nelts
148 * sizeof (struct symtab_and_line));
149 sal = &result.sals[result.nelts - 1];
151 init_sal (sal);
153 sal->pc = get_probe_address (probe, objfile);
154 sal->explicit_pc = 1;
155 sal->section = find_pc_overlay (sal->pc);
156 sal->pspace = pspace;
157 sal->probe = probe;
158 sal->objfile = objfile;
162 if (result.nelts == 0)
164 throw_error (NOT_FOUND_ERROR,
165 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
166 objfile_namestr ? objfile_namestr : _("<any>"),
167 provider ? provider : _("<any>"),
168 name);
171 if (canonical)
173 canonical->special_display = 1;
174 canonical->pre_expanded = 1;
175 canonical->addr_string = savestring (*argptr, arg_end - *argptr);
178 *argptr = arg_end;
179 do_cleanups (cleanup);
181 return result;
184 /* See definition in probe.h. */
186 VEC (probe_p) *
187 find_probes_in_objfile (struct objfile *objfile, const char *provider,
188 const char *name)
190 VEC (probe_p) *probes, *result = NULL;
191 int ix;
192 struct probe *probe;
194 if (!objfile->sf || !objfile->sf->sym_probe_fns)
195 return NULL;
197 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
198 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
200 if (strcmp (probe->provider, provider) != 0)
201 continue;
203 if (strcmp (probe->name, name) != 0)
204 continue;
206 VEC_safe_push (probe_p, result, probe);
209 return result;
212 /* See definition in probe.h. */
214 struct bound_probe
215 find_probe_by_pc (CORE_ADDR pc)
217 struct objfile *objfile;
218 struct bound_probe result;
220 result.objfile = NULL;
221 result.probe = NULL;
223 ALL_OBJFILES (objfile)
225 VEC (probe_p) *probes;
226 int ix;
227 struct probe *probe;
229 if (!objfile->sf || !objfile->sf->sym_probe_fns
230 || objfile->sect_index_text == -1)
231 continue;
233 /* If this proves too inefficient, we can replace with a hash. */
234 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
235 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
236 if (get_probe_address (probe, objfile) == pc)
238 result.objfile = objfile;
239 result.probe = probe;
240 return result;
244 return result;
249 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
250 If POPS is not NULL, only probes of this certain probe_ops will match.
251 Each argument is a regexp, or NULL, which matches anything. */
253 static VEC (bound_probe_s) *
254 collect_probes (char *objname, char *provider, char *probe_name,
255 const struct probe_ops *pops)
257 struct objfile *objfile;
258 VEC (bound_probe_s) *result = NULL;
259 struct cleanup *cleanup, *cleanup_temps;
260 regex_t obj_pat, prov_pat, probe_pat;
262 cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
264 cleanup_temps = make_cleanup (null_cleanup, NULL);
265 if (provider != NULL)
266 compile_rx_or_error (&prov_pat, provider, _("Invalid provider regexp"));
267 if (probe_name != NULL)
268 compile_rx_or_error (&probe_pat, probe_name, _("Invalid probe regexp"));
269 if (objname != NULL)
270 compile_rx_or_error (&obj_pat, objname, _("Invalid object file regexp"));
272 ALL_OBJFILES (objfile)
274 VEC (probe_p) *probes;
275 struct probe *probe;
276 int ix;
278 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
279 continue;
281 if (objname)
283 if (regexec (&obj_pat, objfile_name (objfile), 0, NULL, 0) != 0)
284 continue;
287 probes = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
289 for (ix = 0; VEC_iterate (probe_p, probes, ix, probe); ix++)
291 struct bound_probe bound;
293 if (pops != NULL && probe->pops != pops)
294 continue;
296 if (provider
297 && regexec (&prov_pat, probe->provider, 0, NULL, 0) != 0)
298 continue;
300 if (probe_name
301 && regexec (&probe_pat, probe->name, 0, NULL, 0) != 0)
302 continue;
304 bound.objfile = objfile;
305 bound.probe = probe;
306 VEC_safe_push (bound_probe_s, result, &bound);
310 do_cleanups (cleanup_temps);
311 discard_cleanups (cleanup);
312 return result;
315 /* A qsort comparison function for bound_probe_s objects. */
317 static int
318 compare_probes (const void *a, const void *b)
320 const struct bound_probe *pa = (const struct bound_probe *) a;
321 const struct bound_probe *pb = (const struct bound_probe *) b;
322 int v;
324 v = strcmp (pa->probe->provider, pb->probe->provider);
325 if (v)
326 return v;
328 v = strcmp (pa->probe->name, pb->probe->name);
329 if (v)
330 return v;
332 if (pa->probe->address < pb->probe->address)
333 return -1;
334 if (pa->probe->address > pb->probe->address)
335 return 1;
337 return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
340 /* Helper function that generate entries in the ui_out table being
341 crafted by `info_probes_for_ops'. */
343 static void
344 gen_ui_out_table_header_info (VEC (bound_probe_s) *probes,
345 const struct probe_ops *p)
347 /* `headings' refers to the names of the columns when printing `info
348 probes'. */
349 VEC (info_probe_column_s) *headings = NULL;
350 struct cleanup *c;
351 info_probe_column_s *column;
352 size_t headings_size;
353 int ix;
355 gdb_assert (p != NULL);
357 if (p->gen_info_probes_table_header == NULL
358 && p->gen_info_probes_table_values == NULL)
359 return;
361 gdb_assert (p->gen_info_probes_table_header != NULL
362 && p->gen_info_probes_table_values != NULL);
364 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
365 p->gen_info_probes_table_header (&headings);
367 headings_size = VEC_length (info_probe_column_s, headings);
369 for (ix = 0;
370 VEC_iterate (info_probe_column_s, headings, ix, column);
371 ++ix)
373 struct bound_probe *probe;
374 int jx;
375 size_t size_max = strlen (column->print_name);
377 for (jx = 0; VEC_iterate (bound_probe_s, probes, jx, probe); ++jx)
379 /* `probe_fields' refers to the values of each new field that this
380 probe will display. */
381 VEC (const_char_ptr) *probe_fields = NULL;
382 struct cleanup *c2;
383 const char *val;
384 int kx;
386 if (probe->probe->pops != p)
387 continue;
389 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
390 p->gen_info_probes_table_values (probe->probe, &probe_fields);
392 gdb_assert (VEC_length (const_char_ptr, probe_fields)
393 == headings_size);
395 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
396 ++kx)
398 /* It is valid to have a NULL value here, which means that the
399 backend does not have something to write and this particular
400 field should be skipped. */
401 if (val == NULL)
402 continue;
404 size_max = max (strlen (val), size_max);
406 do_cleanups (c2);
409 ui_out_table_header (current_uiout, size_max, ui_left,
410 column->field_name, column->print_name);
413 do_cleanups (c);
416 /* Helper function to print not-applicable strings for all the extra
417 columns defined in a probe_ops. */
419 static void
420 print_ui_out_not_applicables (const struct probe_ops *pops)
422 struct cleanup *c;
423 VEC (info_probe_column_s) *headings = NULL;
424 info_probe_column_s *column;
425 int ix;
427 if (pops->gen_info_probes_table_header == NULL)
428 return;
430 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
431 pops->gen_info_probes_table_header (&headings);
433 for (ix = 0;
434 VEC_iterate (info_probe_column_s, headings, ix, column);
435 ++ix)
436 ui_out_field_string (current_uiout, column->field_name, _("n/a"));
438 do_cleanups (c);
441 /* Helper function to print extra information about a probe and an objfile
442 represented by PROBE. */
444 static void
445 print_ui_out_info (struct probe *probe)
447 int ix;
448 int j = 0;
449 /* `values' refers to the actual values of each new field in the output
450 of `info probe'. `headings' refers to the names of each new field. */
451 VEC (const_char_ptr) *values = NULL;
452 VEC (info_probe_column_s) *headings = NULL;
453 info_probe_column_s *column;
454 struct cleanup *c;
456 gdb_assert (probe != NULL);
457 gdb_assert (probe->pops != NULL);
459 if (probe->pops->gen_info_probes_table_header == NULL
460 && probe->pops->gen_info_probes_table_values == NULL)
461 return;
463 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
464 && probe->pops->gen_info_probes_table_values != NULL);
466 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
467 make_cleanup (VEC_cleanup (const_char_ptr), &values);
469 probe->pops->gen_info_probes_table_header (&headings);
470 probe->pops->gen_info_probes_table_values (probe, &values);
472 gdb_assert (VEC_length (info_probe_column_s, headings)
473 == VEC_length (const_char_ptr, values));
475 for (ix = 0;
476 VEC_iterate (info_probe_column_s, headings, ix, column);
477 ++ix)
479 const char *val = VEC_index (const_char_ptr, values, j++);
481 if (val == NULL)
482 ui_out_field_skip (current_uiout, column->field_name);
483 else
484 ui_out_field_string (current_uiout, column->field_name, val);
487 do_cleanups (c);
490 /* Helper function that returns the number of extra fields which POPS will
491 need. */
493 static int
494 get_number_extra_fields (const struct probe_ops *pops)
496 VEC (info_probe_column_s) *headings = NULL;
497 struct cleanup *c;
498 int n;
500 if (pops->gen_info_probes_table_header == NULL)
501 return 0;
503 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
504 pops->gen_info_probes_table_header (&headings);
506 n = VEC_length (info_probe_column_s, headings);
508 do_cleanups (c);
510 return n;
513 /* Helper function that returns 1 if there is a probe in PROBES
514 featuring the given POPS. It returns 0 otherwise. */
516 static int
517 exists_probe_with_pops (VEC (bound_probe_s) *probes,
518 const struct probe_ops *pops)
520 struct bound_probe *probe;
521 int ix;
523 for (ix = 0; VEC_iterate (bound_probe_s, probes, ix, probe); ++ix)
524 if (probe->probe->pops == pops)
525 return 1;
527 return 0;
530 /* Helper function that parses a probe linespec of the form [PROVIDER
531 [PROBE [OBJNAME]]] from the provided string STR. */
533 static void
534 parse_probe_linespec (const char *str, char **provider,
535 char **probe_name, char **objname)
537 *probe_name = *objname = NULL;
539 *provider = extract_arg_const (&str);
540 if (*provider != NULL)
542 *probe_name = extract_arg_const (&str);
543 if (*probe_name != NULL)
544 *objname = extract_arg_const (&str);
548 /* See comment in probe.h. */
550 void
551 info_probes_for_ops (const char *arg, int from_tty,
552 const struct probe_ops *pops)
554 char *provider, *probe_name = NULL, *objname = NULL;
555 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
556 VEC (bound_probe_s) *probes;
557 int i, any_found;
558 int ui_out_extra_fields = 0;
559 size_t size_addr;
560 size_t size_name = strlen ("Name");
561 size_t size_objname = strlen ("Object");
562 size_t size_provider = strlen ("Provider");
563 size_t size_type = strlen ("Type");
564 struct bound_probe *probe;
565 struct gdbarch *gdbarch = get_current_arch ();
567 parse_probe_linespec (arg, &provider, &probe_name, &objname);
568 make_cleanup (xfree, provider);
569 make_cleanup (xfree, probe_name);
570 make_cleanup (xfree, objname);
572 probes = collect_probes (objname, provider, probe_name, pops);
573 make_cleanup (VEC_cleanup (probe_p), &probes);
575 if (pops == NULL)
577 const struct probe_ops *po;
578 int ix;
580 /* If the probe_ops is NULL, it means the user has requested a "simple"
581 `info probes', i.e., she wants to print all information about all
582 probes. For that, we have to identify how many extra fields we will
583 need to add in the ui_out table.
585 To do that, we iterate over all probe_ops, querying each one about
586 its extra fields, and incrementing `ui_out_extra_fields' to reflect
587 that number. But note that we ignore the probe_ops for which no probes
588 are defined with the given search criteria. */
590 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
591 if (exists_probe_with_pops (probes, po))
592 ui_out_extra_fields += get_number_extra_fields (po);
594 else
595 ui_out_extra_fields = get_number_extra_fields (pops);
597 make_cleanup_ui_out_table_begin_end (current_uiout,
598 5 + ui_out_extra_fields,
599 VEC_length (bound_probe_s, probes),
600 "StaticProbes");
602 if (!VEC_empty (bound_probe_s, probes))
603 qsort (VEC_address (bound_probe_s, probes),
604 VEC_length (bound_probe_s, probes),
605 sizeof (bound_probe_s), compare_probes);
607 /* What's the size of an address in our architecture? */
608 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
610 /* Determining the maximum size of each field (`type', `provider',
611 `name' and `objname'). */
612 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
614 const char *probe_type = probe->probe->pops->type_name (probe->probe);
616 size_type = max (strlen (probe_type), size_type);
617 size_name = max (strlen (probe->probe->name), size_name);
618 size_provider = max (strlen (probe->probe->provider), size_provider);
619 size_objname = max (strlen (objfile_name (probe->objfile)), size_objname);
622 ui_out_table_header (current_uiout, size_type, ui_left, "type", _("Type"));
623 ui_out_table_header (current_uiout, size_provider, ui_left, "provider",
624 _("Provider"));
625 ui_out_table_header (current_uiout, size_name, ui_left, "name", _("Name"));
626 ui_out_table_header (current_uiout, size_addr, ui_left, "addr", _("Where"));
628 if (pops == NULL)
630 const struct probe_ops *po;
631 int ix;
633 /* We have to generate the table header for each new probe type
634 that we will print. Note that this excludes probe types not
635 having any defined probe with the search criteria. */
636 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
637 if (exists_probe_with_pops (probes, po))
638 gen_ui_out_table_header_info (probes, po);
640 else
641 gen_ui_out_table_header_info (probes, pops);
643 ui_out_table_header (current_uiout, size_objname, ui_left, "object",
644 _("Object"));
645 ui_out_table_body (current_uiout);
647 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
649 struct cleanup *inner;
650 const char *probe_type = probe->probe->pops->type_name (probe->probe);
652 inner = make_cleanup_ui_out_tuple_begin_end (current_uiout, "probe");
654 ui_out_field_string (current_uiout, "type",probe_type);
655 ui_out_field_string (current_uiout, "provider", probe->probe->provider);
656 ui_out_field_string (current_uiout, "name", probe->probe->name);
657 ui_out_field_core_addr (current_uiout, "addr",
658 probe->probe->arch,
659 get_probe_address (probe->probe, probe->objfile));
661 if (pops == NULL)
663 const struct probe_ops *po;
664 int ix;
666 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
667 ++ix)
668 if (probe->probe->pops == po)
669 print_ui_out_info (probe->probe);
670 else if (exists_probe_with_pops (probes, po))
671 print_ui_out_not_applicables (po);
673 else
674 print_ui_out_info (probe->probe);
676 ui_out_field_string (current_uiout, "object",
677 objfile_name (probe->objfile));
678 ui_out_text (current_uiout, "\n");
680 do_cleanups (inner);
683 any_found = !VEC_empty (bound_probe_s, probes);
684 do_cleanups (cleanup);
686 if (!any_found)
687 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
690 /* Implementation of the `info probes' command. */
692 static void
693 info_probes_command (char *arg, int from_tty)
695 info_probes_for_ops (arg, from_tty, NULL);
698 /* Implementation of the `enable probes' command. */
700 static void
701 enable_probes_command (char *arg, int from_tty)
703 char *provider, *probe_name = NULL, *objname = NULL;
704 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
705 VEC (bound_probe_s) *probes;
706 struct bound_probe *probe;
707 int i;
709 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
710 make_cleanup (xfree, provider);
711 make_cleanup (xfree, probe_name);
712 make_cleanup (xfree, objname);
714 probes = collect_probes (objname, provider, probe_name, NULL);
715 if (VEC_empty (bound_probe_s, probes))
717 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
718 do_cleanups (cleanup);
719 return;
722 /* Enable the selected probes, provided their backends support the
723 notion of enabling a probe. */
724 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
726 const struct probe_ops *pops = probe->probe->pops;
728 if (pops->enable_probe != NULL)
730 pops->enable_probe (probe->probe);
731 ui_out_message (current_uiout, 0,
732 _("Probe %s:%s enabled.\n"),
733 probe->probe->provider, probe->probe->name);
735 else
736 ui_out_message (current_uiout, 0,
737 _("Probe %s:%s cannot be enabled.\n"),
738 probe->probe->provider, probe->probe->name);
741 do_cleanups (cleanup);
744 /* Implementation of the `disable probes' command. */
746 static void
747 disable_probes_command (char *arg, int from_tty)
749 char *provider, *probe_name = NULL, *objname = NULL;
750 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
751 VEC (bound_probe_s) *probes;
752 struct bound_probe *probe;
753 int i;
755 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
756 make_cleanup (xfree, provider);
757 make_cleanup (xfree, probe_name);
758 make_cleanup (xfree, objname);
760 probes = collect_probes (objname, provider, probe_name, NULL /* pops */);
761 if (VEC_empty (bound_probe_s, probes))
763 ui_out_message (current_uiout, 0, _("No probes matched.\n"));
764 do_cleanups (cleanup);
765 return;
768 /* Disable the selected probes, provided their backends support the
769 notion of enabling a probe. */
770 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
772 const struct probe_ops *pops = probe->probe->pops;
774 if (pops->disable_probe != NULL)
776 pops->disable_probe (probe->probe);
777 ui_out_message (current_uiout, 0,
778 _("Probe %s:%s disabled.\n"),
779 probe->probe->provider, probe->probe->name);
781 else
782 ui_out_message (current_uiout, 0,
783 _("Probe %s:%s cannot be disabled.\n"),
784 probe->probe->provider, probe->probe->name);
787 do_cleanups (cleanup);
790 /* See comments in probe.h. */
792 CORE_ADDR
793 get_probe_address (struct probe *probe, struct objfile *objfile)
795 return probe->pops->get_probe_address (probe, objfile);
798 /* See comments in probe.h. */
800 unsigned
801 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
803 return probe->pops->get_probe_argument_count (probe, frame);
806 /* See comments in probe.h. */
809 can_evaluate_probe_arguments (struct probe *probe)
811 return probe->pops->can_evaluate_probe_arguments (probe);
814 /* See comments in probe.h. */
816 struct value *
817 evaluate_probe_argument (struct probe *probe, unsigned n,
818 struct frame_info *frame)
820 return probe->pops->evaluate_probe_argument (probe, n, frame);
823 /* See comments in probe.h. */
825 struct value *
826 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
828 struct bound_probe probe;
829 unsigned n_args;
831 probe = find_probe_by_pc (get_frame_pc (frame));
832 if (!probe.probe)
833 return NULL;
835 n_args = get_probe_argument_count (probe.probe, frame);
836 if (n >= n_args)
837 return NULL;
839 return evaluate_probe_argument (probe.probe, n, frame);
842 /* See comment in probe.h. */
844 const struct probe_ops *
845 probe_linespec_to_ops (const char **linespecp)
847 int ix;
848 const struct probe_ops *probe_ops;
850 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
851 if (probe_ops->is_linespec (linespecp))
852 return probe_ops;
854 return NULL;
857 /* See comment in probe.h. */
860 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
862 const char *s = *linespecp;
863 const char *const *csp;
865 for (csp = keywords; *csp; csp++)
867 const char *keyword = *csp;
868 size_t len = strlen (keyword);
870 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
872 *linespecp += len + 1;
873 return 1;
877 return 0;
880 /* Implementation of `is_linespec' method for `struct probe_ops'. */
882 static int
883 probe_any_is_linespec (const char **linespecp)
885 static const char *const keywords[] = { "-p", "-probe", NULL };
887 return probe_is_linespec_by_keyword (linespecp, keywords);
890 /* Dummy method used for `probe_ops_any'. */
892 static void
893 probe_any_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
895 /* No probes can be provided by this dummy backend. */
898 /* Operations associated with a generic probe. */
900 const struct probe_ops probe_ops_any =
902 probe_any_is_linespec,
903 probe_any_get_probes,
906 /* See comments in probe.h. */
908 struct cmd_list_element **
909 info_probes_cmdlist_get (void)
911 static struct cmd_list_element *info_probes_cmdlist;
913 if (info_probes_cmdlist == NULL)
914 add_prefix_cmd ("probes", class_info, info_probes_command,
915 _("\
916 Show available static probes.\n\
917 Usage: info probes [all|TYPE [ARGS]]\n\
918 TYPE specifies the type of the probe, and can be one of the following:\n\
919 - stap\n\
920 If you specify TYPE, there may be additional arguments needed by the\n\
921 subcommand.\n\
922 If you do not specify any argument, or specify `all', then the command\n\
923 will show information about all types of probes."),
924 &info_probes_cmdlist, "info probes ",
925 0/*allow-unknown*/, &infolist);
927 return &info_probes_cmdlist;
932 /* This is called to compute the value of one of the $_probe_arg*
933 convenience variables. */
935 static struct value *
936 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
937 void *data)
939 struct frame_info *frame = get_selected_frame (_("No frame selected"));
940 CORE_ADDR pc = get_frame_pc (frame);
941 int sel = (int) (uintptr_t) data;
942 struct bound_probe pc_probe;
943 const struct sym_probe_fns *pc_probe_fns;
944 unsigned n_args;
946 /* SEL == -1 means "_probe_argc". */
947 gdb_assert (sel >= -1);
949 pc_probe = find_probe_by_pc (pc);
950 if (pc_probe.probe == NULL)
951 error (_("No probe at PC %s"), core_addr_to_string (pc));
953 n_args = get_probe_argument_count (pc_probe.probe, frame);
954 if (sel == -1)
955 return value_from_longest (builtin_type (arch)->builtin_int, n_args);
957 if (sel >= n_args)
958 error (_("Invalid probe argument %d -- probe has %u arguments available"),
959 sel, n_args);
961 return evaluate_probe_argument (pc_probe.probe, sel, frame);
964 /* This is called to compile one of the $_probe_arg* convenience
965 variables into an agent expression. */
967 static void
968 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
969 struct axs_value *value, void *data)
971 CORE_ADDR pc = expr->scope;
972 int sel = (int) (uintptr_t) data;
973 struct bound_probe pc_probe;
974 const struct sym_probe_fns *pc_probe_fns;
975 int n_args;
976 struct frame_info *frame = get_selected_frame (NULL);
978 /* SEL == -1 means "_probe_argc". */
979 gdb_assert (sel >= -1);
981 pc_probe = find_probe_by_pc (pc);
982 if (pc_probe.probe == NULL)
983 error (_("No probe at PC %s"), core_addr_to_string (pc));
985 n_args = get_probe_argument_count (pc_probe.probe, frame);
987 if (sel == -1)
989 value->kind = axs_rvalue;
990 value->type = builtin_type (expr->gdbarch)->builtin_int;
991 ax_const_l (expr, n_args);
992 return;
995 gdb_assert (sel >= 0);
996 if (sel >= n_args)
997 error (_("Invalid probe argument %d -- probe has %d arguments available"),
998 sel, n_args);
1000 pc_probe.probe->pops->compile_to_ax (pc_probe.probe, expr, value, sel);
1003 static const struct internalvar_funcs probe_funcs =
1005 compute_probe_arg,
1006 compile_probe_arg,
1007 NULL
1011 VEC (probe_ops_cp) *all_probe_ops;
1013 void _initialize_probe (void);
1015 void
1016 _initialize_probe (void)
1018 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
1020 create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1021 (void *) (uintptr_t) -1);
1022 create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1023 (void *) (uintptr_t) 0);
1024 create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1025 (void *) (uintptr_t) 1);
1026 create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1027 (void *) (uintptr_t) 2);
1028 create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1029 (void *) (uintptr_t) 3);
1030 create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1031 (void *) (uintptr_t) 4);
1032 create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1033 (void *) (uintptr_t) 5);
1034 create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1035 (void *) (uintptr_t) 6);
1036 create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1037 (void *) (uintptr_t) 7);
1038 create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1039 (void *) (uintptr_t) 8);
1040 create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1041 (void *) (uintptr_t) 9);
1042 create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1043 (void *) (uintptr_t) 10);
1044 create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1045 (void *) (uintptr_t) 11);
1047 add_cmd ("all", class_info, info_probes_command,
1048 _("\
1049 Show information about all type of probes."),
1050 info_probes_cmdlist_get ());
1052 add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
1053 Enable probes.\n\
1054 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
1055 Each argument is a regular expression, used to select probes.\n\
1056 PROVIDER matches probe provider names.\n\
1057 NAME matches the probe names.\n\
1058 OBJECT matches the executable or shared library name.\n\
1059 If you do not specify any argument then the command will enable\n\
1060 all defined probes."),
1061 &enablelist);
1063 add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
1064 Disable probes.\n\
1065 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
1066 Each argument is a regular expression, used to select probes.\n\
1067 PROVIDER matches probe provider names.\n\
1068 NAME matches the probe names.\n\
1069 OBJECT matches the executable or shared library name.\n\
1070 If you do not specify any argument then the command will disable\n\
1071 all defined probes."),
1072 &disablelist);