Make probe_ops::get_probes fill an std::vector
[deliverable/binutils-gdb.git] / gdb / probe.c
1 /* Generic static probe support for GDB.
2
3 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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 "location.h"
37 #include <ctype.h>
38 #include <algorithm>
39 #include "common/gdb_optional.h"
40
41 typedef struct bound_probe bound_probe_s;
42 DEF_VEC_O (bound_probe_s);
43
44 \f
45
46 /* A helper for parse_probes that decodes a probe specification in
47 SEARCH_PSPACE. It appends matching SALs to RESULT. */
48
49 static void
50 parse_probes_in_pspace (const struct probe_ops *probe_ops,
51 struct program_space *search_pspace,
52 const char *objfile_namestr,
53 const char *provider,
54 const char *name,
55 std::vector<symtab_and_line> *result)
56 {
57 struct objfile *objfile;
58
59 ALL_PSPACE_OBJFILES (search_pspace, objfile)
60 {
61 if (!objfile->sf || !objfile->sf->sym_probe_fns)
62 continue;
63
64 if (objfile_namestr
65 && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
66 && FILENAME_CMP (lbasename (objfile_name (objfile)),
67 objfile_namestr) != 0)
68 continue;
69
70 const std::vector<probe *> &probes
71 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
72
73 for (struct probe *probe : probes)
74 {
75 if (probe_ops != &probe_ops_any && probe->pops != probe_ops)
76 continue;
77
78 if (provider && strcmp (probe->provider, provider) != 0)
79 continue;
80
81 if (strcmp (probe->name, name) != 0)
82 continue;
83
84 symtab_and_line sal;
85 sal.pc = get_probe_address (probe, objfile);
86 sal.explicit_pc = 1;
87 sal.section = find_pc_overlay (sal.pc);
88 sal.pspace = search_pspace;
89 sal.probe = probe;
90 sal.objfile = objfile;
91
92 result->push_back (std::move (sal));
93 }
94 }
95 }
96
97 /* See definition in probe.h. */
98
99 std::vector<symtab_and_line>
100 parse_probes (const struct event_location *location,
101 struct program_space *search_pspace,
102 struct linespec_result *canonical)
103 {
104 char *arg_end, *arg;
105 char *objfile_namestr = NULL, *provider = NULL, *name, *p;
106 struct cleanup *cleanup;
107 const struct probe_ops *probe_ops;
108 const char *arg_start, *cs;
109
110 gdb_assert (event_location_type (location) == PROBE_LOCATION);
111 arg_start = get_probe_location (location);
112
113 cs = arg_start;
114 probe_ops = probe_linespec_to_ops (&cs);
115 if (probe_ops == NULL)
116 error (_("'%s' is not a probe linespec"), arg_start);
117
118 arg = (char *) cs;
119 arg = skip_spaces (arg);
120 if (!*arg)
121 error (_("argument to `%s' missing"), arg_start);
122
123 arg_end = skip_to_space (arg);
124
125 /* We make a copy here so we can write over parts with impunity. */
126 arg = savestring (arg, arg_end - arg);
127 cleanup = make_cleanup (xfree, arg);
128
129 /* Extract each word from the argument, separated by ":"s. */
130 p = strchr (arg, ':');
131 if (p == NULL)
132 {
133 /* This is `-p name'. */
134 name = arg;
135 }
136 else
137 {
138 char *hold = p + 1;
139
140 *p = '\0';
141 p = strchr (hold, ':');
142 if (p == NULL)
143 {
144 /* This is `-p provider:name'. */
145 provider = arg;
146 name = hold;
147 }
148 else
149 {
150 /* This is `-p objfile:provider:name'. */
151 *p = '\0';
152 objfile_namestr = arg;
153 provider = hold;
154 name = p + 1;
155 }
156 }
157
158 if (*name == '\0')
159 error (_("no probe name specified"));
160 if (provider && *provider == '\0')
161 error (_("invalid provider name"));
162 if (objfile_namestr && *objfile_namestr == '\0')
163 error (_("invalid objfile name"));
164
165 std::vector<symtab_and_line> result;
166 if (search_pspace != NULL)
167 {
168 parse_probes_in_pspace (probe_ops, search_pspace, objfile_namestr,
169 provider, name, &result);
170 }
171 else
172 {
173 struct program_space *pspace;
174
175 ALL_PSPACES (pspace)
176 parse_probes_in_pspace (probe_ops, pspace, objfile_namestr,
177 provider, name, &result);
178 }
179
180 if (result.empty ())
181 {
182 throw_error (NOT_FOUND_ERROR,
183 _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
184 objfile_namestr ? objfile_namestr : _("<any>"),
185 provider ? provider : _("<any>"),
186 name);
187 }
188
189 if (canonical)
190 {
191 char *canon;
192
193 canon = savestring (arg_start, arg_end - arg_start);
194 make_cleanup (xfree, canon);
195 canonical->special_display = 1;
196 canonical->pre_expanded = 1;
197 canonical->location = new_probe_location (canon);
198 }
199
200 do_cleanups (cleanup);
201
202 return result;
203 }
204
205 /* See definition in probe.h. */
206
207 VEC (probe_p) *
208 find_probes_in_objfile (struct objfile *objfile, const char *provider,
209 const char *name)
210 {
211 VEC (probe_p) *result = NULL;
212
213 if (!objfile->sf || !objfile->sf->sym_probe_fns)
214 return NULL;
215
216 const std::vector<probe *> &probes
217 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
218 for (struct probe *probe : probes)
219 {
220 if (strcmp (probe->provider, provider) != 0)
221 continue;
222
223 if (strcmp (probe->name, name) != 0)
224 continue;
225
226 VEC_safe_push (probe_p, result, probe);
227 }
228
229 return result;
230 }
231
232 /* See definition in probe.h. */
233
234 struct bound_probe
235 find_probe_by_pc (CORE_ADDR pc)
236 {
237 struct objfile *objfile;
238 struct bound_probe result;
239
240 result.objfile = NULL;
241 result.probe = NULL;
242
243 ALL_OBJFILES (objfile)
244 {
245 if (!objfile->sf || !objfile->sf->sym_probe_fns
246 || objfile->sect_index_text == -1)
247 continue;
248
249 /* If this proves too inefficient, we can replace with a hash. */
250 const std::vector<probe *> &probes
251 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
252 for (struct probe *probe : probes)
253 if (get_probe_address (probe, objfile) == pc)
254 {
255 result.objfile = objfile;
256 result.probe = probe;
257 return result;
258 }
259 }
260
261 return result;
262 }
263
264 \f
265
266 /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
267 If POPS is not NULL, only probes of this certain probe_ops will match.
268 Each argument is a regexp, or NULL, which matches anything. */
269
270 static VEC (bound_probe_s) *
271 collect_probes (const std::string &objname, const std::string &provider,
272 const std::string &probe_name, const struct probe_ops *pops)
273 {
274 struct objfile *objfile;
275 VEC (bound_probe_s) *result = NULL;
276 struct cleanup *cleanup;
277 gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
278
279 cleanup = make_cleanup (VEC_cleanup (bound_probe_s), &result);
280
281 if (!provider.empty ())
282 prov_pat.emplace (provider.c_str (), REG_NOSUB,
283 _("Invalid provider regexp"));
284 if (!probe_name.empty ())
285 probe_pat.emplace (probe_name.c_str (), REG_NOSUB,
286 _("Invalid probe regexp"));
287 if (!objname.empty ())
288 obj_pat.emplace (objname.c_str (), REG_NOSUB,
289 _("Invalid object file regexp"));
290
291 ALL_OBJFILES (objfile)
292 {
293 if (! objfile->sf || ! objfile->sf->sym_probe_fns)
294 continue;
295
296 if (obj_pat)
297 {
298 if (obj_pat->exec (objfile_name (objfile), 0, NULL, 0) != 0)
299 continue;
300 }
301
302 const std::vector<probe *> &probes
303 = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
304
305 for (struct probe *probe : probes)
306 {
307 struct bound_probe bound;
308
309 if (pops != NULL && probe->pops != pops)
310 continue;
311
312 if (prov_pat
313 && prov_pat->exec (probe->provider, 0, NULL, 0) != 0)
314 continue;
315
316 if (probe_pat
317 && probe_pat->exec (probe->name, 0, NULL, 0) != 0)
318 continue;
319
320 bound.objfile = objfile;
321 bound.probe = probe;
322 VEC_safe_push (bound_probe_s, result, &bound);
323 }
324 }
325
326 discard_cleanups (cleanup);
327 return result;
328 }
329
330 /* A qsort comparison function for bound_probe_s objects. */
331
332 static int
333 compare_probes (const void *a, const void *b)
334 {
335 const struct bound_probe *pa = (const struct bound_probe *) a;
336 const struct bound_probe *pb = (const struct bound_probe *) b;
337 int v;
338
339 v = strcmp (pa->probe->provider, pb->probe->provider);
340 if (v)
341 return v;
342
343 v = strcmp (pa->probe->name, pb->probe->name);
344 if (v)
345 return v;
346
347 if (pa->probe->address < pb->probe->address)
348 return -1;
349 if (pa->probe->address > pb->probe->address)
350 return 1;
351
352 return strcmp (objfile_name (pa->objfile), objfile_name (pb->objfile));
353 }
354
355 /* Helper function that generate entries in the ui_out table being
356 crafted by `info_probes_for_ops'. */
357
358 static void
359 gen_ui_out_table_header_info (VEC (bound_probe_s) *probes,
360 const struct probe_ops *p)
361 {
362 /* `headings' refers to the names of the columns when printing `info
363 probes'. */
364 VEC (info_probe_column_s) *headings = NULL;
365 struct cleanup *c;
366 info_probe_column_s *column;
367 size_t headings_size;
368 int ix;
369
370 gdb_assert (p != NULL);
371
372 if (p->gen_info_probes_table_header == NULL
373 && p->gen_info_probes_table_values == NULL)
374 return;
375
376 gdb_assert (p->gen_info_probes_table_header != NULL
377 && p->gen_info_probes_table_values != NULL);
378
379 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
380 p->gen_info_probes_table_header (&headings);
381
382 headings_size = VEC_length (info_probe_column_s, headings);
383
384 for (ix = 0;
385 VEC_iterate (info_probe_column_s, headings, ix, column);
386 ++ix)
387 {
388 struct bound_probe *probe;
389 int jx;
390 size_t size_max = strlen (column->print_name);
391
392 for (jx = 0; VEC_iterate (bound_probe_s, probes, jx, probe); ++jx)
393 {
394 /* `probe_fields' refers to the values of each new field that this
395 probe will display. */
396 VEC (const_char_ptr) *probe_fields = NULL;
397 struct cleanup *c2;
398 const char *val;
399 int kx;
400
401 if (probe->probe->pops != p)
402 continue;
403
404 c2 = make_cleanup (VEC_cleanup (const_char_ptr), &probe_fields);
405 p->gen_info_probes_table_values (probe->probe, &probe_fields);
406
407 gdb_assert (VEC_length (const_char_ptr, probe_fields)
408 == headings_size);
409
410 for (kx = 0; VEC_iterate (const_char_ptr, probe_fields, kx, val);
411 ++kx)
412 {
413 /* It is valid to have a NULL value here, which means that the
414 backend does not have something to write and this particular
415 field should be skipped. */
416 if (val == NULL)
417 continue;
418
419 size_max = std::max (strlen (val), size_max);
420 }
421 do_cleanups (c2);
422 }
423
424 current_uiout->table_header (size_max, ui_left,
425 column->field_name, column->print_name);
426 }
427
428 do_cleanups (c);
429 }
430
431 /* Helper function to print not-applicable strings for all the extra
432 columns defined in a probe_ops. */
433
434 static void
435 print_ui_out_not_applicables (const struct probe_ops *pops)
436 {
437 struct cleanup *c;
438 VEC (info_probe_column_s) *headings = NULL;
439 info_probe_column_s *column;
440 int ix;
441
442 if (pops->gen_info_probes_table_header == NULL)
443 return;
444
445 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
446 pops->gen_info_probes_table_header (&headings);
447
448 for (ix = 0;
449 VEC_iterate (info_probe_column_s, headings, ix, column);
450 ++ix)
451 current_uiout->field_string (column->field_name, _("n/a"));
452
453 do_cleanups (c);
454 }
455
456 /* Helper function to print extra information about a probe and an objfile
457 represented by PROBE. */
458
459 static void
460 print_ui_out_info (struct probe *probe)
461 {
462 int ix;
463 int j = 0;
464 /* `values' refers to the actual values of each new field in the output
465 of `info probe'. `headings' refers to the names of each new field. */
466 VEC (const_char_ptr) *values = NULL;
467 VEC (info_probe_column_s) *headings = NULL;
468 info_probe_column_s *column;
469 struct cleanup *c;
470
471 gdb_assert (probe != NULL);
472 gdb_assert (probe->pops != NULL);
473
474 if (probe->pops->gen_info_probes_table_header == NULL
475 && probe->pops->gen_info_probes_table_values == NULL)
476 return;
477
478 gdb_assert (probe->pops->gen_info_probes_table_header != NULL
479 && probe->pops->gen_info_probes_table_values != NULL);
480
481 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
482 make_cleanup (VEC_cleanup (const_char_ptr), &values);
483
484 probe->pops->gen_info_probes_table_header (&headings);
485 probe->pops->gen_info_probes_table_values (probe, &values);
486
487 gdb_assert (VEC_length (info_probe_column_s, headings)
488 == VEC_length (const_char_ptr, values));
489
490 for (ix = 0;
491 VEC_iterate (info_probe_column_s, headings, ix, column);
492 ++ix)
493 {
494 const char *val = VEC_index (const_char_ptr, values, j++);
495
496 if (val == NULL)
497 current_uiout->field_skip (column->field_name);
498 else
499 current_uiout->field_string (column->field_name, val);
500 }
501
502 do_cleanups (c);
503 }
504
505 /* Helper function that returns the number of extra fields which POPS will
506 need. */
507
508 static int
509 get_number_extra_fields (const struct probe_ops *pops)
510 {
511 VEC (info_probe_column_s) *headings = NULL;
512 struct cleanup *c;
513 int n;
514
515 if (pops->gen_info_probes_table_header == NULL)
516 return 0;
517
518 c = make_cleanup (VEC_cleanup (info_probe_column_s), &headings);
519 pops->gen_info_probes_table_header (&headings);
520
521 n = VEC_length (info_probe_column_s, headings);
522
523 do_cleanups (c);
524
525 return n;
526 }
527
528 /* Helper function that returns 1 if there is a probe in PROBES
529 featuring the given POPS. It returns 0 otherwise. */
530
531 static int
532 exists_probe_with_pops (VEC (bound_probe_s) *probes,
533 const struct probe_ops *pops)
534 {
535 struct bound_probe *probe;
536 int ix;
537
538 for (ix = 0; VEC_iterate (bound_probe_s, probes, ix, probe); ++ix)
539 if (probe->probe->pops == pops)
540 return 1;
541
542 return 0;
543 }
544
545 /* Helper function that parses a probe linespec of the form [PROVIDER
546 [PROBE [OBJNAME]]] from the provided string STR. */
547
548 static void
549 parse_probe_linespec (const char *str, std::string *provider,
550 std::string *probe_name, std::string *objname)
551 {
552 *probe_name = *objname = "";
553
554 *provider = extract_arg (&str);
555 if (!provider->empty ())
556 {
557 *probe_name = extract_arg (&str);
558 if (!probe_name->empty ())
559 *objname = extract_arg (&str);
560 }
561 }
562
563 /* See comment in probe.h. */
564
565 void
566 info_probes_for_ops (const char *arg, int from_tty,
567 const struct probe_ops *pops)
568 {
569 std::string provider, probe_name, objname;
570 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
571 VEC (bound_probe_s) *probes;
572 int i, any_found;
573 int ui_out_extra_fields = 0;
574 size_t size_addr;
575 size_t size_name = strlen ("Name");
576 size_t size_objname = strlen ("Object");
577 size_t size_provider = strlen ("Provider");
578 size_t size_type = strlen ("Type");
579 struct bound_probe *probe;
580 struct gdbarch *gdbarch = get_current_arch ();
581
582 parse_probe_linespec (arg, &provider, &probe_name, &objname);
583
584 probes = collect_probes (objname, provider, probe_name, pops);
585 make_cleanup (VEC_cleanup (probe_p), &probes);
586
587 if (pops == NULL)
588 {
589 const struct probe_ops *po;
590 int ix;
591
592 /* If the probe_ops is NULL, it means the user has requested a "simple"
593 `info probes', i.e., she wants to print all information about all
594 probes. For that, we have to identify how many extra fields we will
595 need to add in the ui_out table.
596
597 To do that, we iterate over all probe_ops, querying each one about
598 its extra fields, and incrementing `ui_out_extra_fields' to reflect
599 that number. But note that we ignore the probe_ops for which no probes
600 are defined with the given search criteria. */
601
602 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
603 if (exists_probe_with_pops (probes, po))
604 ui_out_extra_fields += get_number_extra_fields (po);
605 }
606 else
607 ui_out_extra_fields = get_number_extra_fields (pops);
608
609 {
610 ui_out_emit_table table_emitter (current_uiout,
611 5 + ui_out_extra_fields,
612 VEC_length (bound_probe_s, probes),
613 "StaticProbes");
614
615 if (!VEC_empty (bound_probe_s, probes))
616 qsort (VEC_address (bound_probe_s, probes),
617 VEC_length (bound_probe_s, probes),
618 sizeof (bound_probe_s), compare_probes);
619
620 /* What's the size of an address in our architecture? */
621 size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
622
623 /* Determining the maximum size of each field (`type', `provider',
624 `name' and `objname'). */
625 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
626 {
627 const char *probe_type = probe->probe->pops->type_name (probe->probe);
628
629 size_type = std::max (strlen (probe_type), size_type);
630 size_name = std::max (strlen (probe->probe->name), size_name);
631 size_provider = std::max (strlen (probe->probe->provider), size_provider);
632 size_objname = std::max (strlen (objfile_name (probe->objfile)),
633 size_objname);
634 }
635
636 current_uiout->table_header (size_type, ui_left, "type", _("Type"));
637 current_uiout->table_header (size_provider, ui_left, "provider",
638 _("Provider"));
639 current_uiout->table_header (size_name, ui_left, "name", _("Name"));
640 current_uiout->table_header (size_addr, ui_left, "addr", _("Where"));
641
642 if (pops == NULL)
643 {
644 const struct probe_ops *po;
645 int ix;
646
647 /* We have to generate the table header for each new probe type
648 that we will print. Note that this excludes probe types not
649 having any defined probe with the search criteria. */
650 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po); ++ix)
651 if (exists_probe_with_pops (probes, po))
652 gen_ui_out_table_header_info (probes, po);
653 }
654 else
655 gen_ui_out_table_header_info (probes, pops);
656
657 current_uiout->table_header (size_objname, ui_left, "object", _("Object"));
658 current_uiout->table_body ();
659
660 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
661 {
662 const char *probe_type = probe->probe->pops->type_name (probe->probe);
663
664 ui_out_emit_tuple tuple_emitter (current_uiout, "probe");
665
666 current_uiout->field_string ("type",probe_type);
667 current_uiout->field_string ("provider", probe->probe->provider);
668 current_uiout->field_string ("name", probe->probe->name);
669 current_uiout->field_core_addr (
670 "addr", probe->probe->arch,
671 get_probe_address (probe->probe, probe->objfile));
672
673 if (pops == NULL)
674 {
675 const struct probe_ops *po;
676 int ix;
677
678 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, po);
679 ++ix)
680 if (probe->probe->pops == po)
681 print_ui_out_info (probe->probe);
682 else if (exists_probe_with_pops (probes, po))
683 print_ui_out_not_applicables (po);
684 }
685 else
686 print_ui_out_info (probe->probe);
687
688 current_uiout->field_string ("object",
689 objfile_name (probe->objfile));
690 current_uiout->text ("\n");
691 }
692
693 any_found = !VEC_empty (bound_probe_s, probes);
694 }
695 do_cleanups (cleanup);
696
697 if (!any_found)
698 current_uiout->message (_("No probes matched.\n"));
699 }
700
701 /* Implementation of the `info probes' command. */
702
703 static void
704 info_probes_command (char *arg, int from_tty)
705 {
706 info_probes_for_ops (arg, from_tty, NULL);
707 }
708
709 /* Implementation of the `enable probes' command. */
710
711 static void
712 enable_probes_command (char *arg, int from_tty)
713 {
714 std::string provider, probe_name, objname;
715 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
716 VEC (bound_probe_s) *probes;
717 struct bound_probe *probe;
718 int i;
719
720 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
721
722 probes = collect_probes (objname, provider, probe_name, NULL);
723 if (VEC_empty (bound_probe_s, probes))
724 {
725 current_uiout->message (_("No probes matched.\n"));
726 do_cleanups (cleanup);
727 return;
728 }
729
730 /* Enable the selected probes, provided their backends support the
731 notion of enabling a probe. */
732 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
733 {
734 const struct probe_ops *pops = probe->probe->pops;
735
736 if (pops->enable_probe != NULL)
737 {
738 pops->enable_probe (probe->probe);
739 current_uiout->message (_("Probe %s:%s enabled.\n"),
740 probe->probe->provider, probe->probe->name);
741 }
742 else
743 current_uiout->message (_("Probe %s:%s cannot be enabled.\n"),
744 probe->probe->provider, probe->probe->name);
745 }
746
747 do_cleanups (cleanup);
748 }
749
750 /* Implementation of the `disable probes' command. */
751
752 static void
753 disable_probes_command (char *arg, int from_tty)
754 {
755 std::string provider, probe_name, objname;
756 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
757 VEC (bound_probe_s) *probes;
758 struct bound_probe *probe;
759 int i;
760
761 parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
762
763 probes = collect_probes (objname, provider, probe_name, NULL /* pops */);
764 if (VEC_empty (bound_probe_s, probes))
765 {
766 current_uiout->message (_("No probes matched.\n"));
767 do_cleanups (cleanup);
768 return;
769 }
770
771 /* Disable the selected probes, provided their backends support the
772 notion of enabling a probe. */
773 for (i = 0; VEC_iterate (bound_probe_s, probes, i, probe); ++i)
774 {
775 const struct probe_ops *pops = probe->probe->pops;
776
777 if (pops->disable_probe != NULL)
778 {
779 pops->disable_probe (probe->probe);
780 current_uiout->message (_("Probe %s:%s disabled.\n"),
781 probe->probe->provider, probe->probe->name);
782 }
783 else
784 current_uiout->message (_("Probe %s:%s cannot be disabled.\n"),
785 probe->probe->provider, probe->probe->name);
786 }
787
788 do_cleanups (cleanup);
789 }
790
791 /* See comments in probe.h. */
792
793 CORE_ADDR
794 get_probe_address (struct probe *probe, struct objfile *objfile)
795 {
796 return probe->pops->get_probe_address (probe, objfile);
797 }
798
799 /* See comments in probe.h. */
800
801 unsigned
802 get_probe_argument_count (struct probe *probe, struct frame_info *frame)
803 {
804 return probe->pops->get_probe_argument_count (probe, frame);
805 }
806
807 /* See comments in probe.h. */
808
809 int
810 can_evaluate_probe_arguments (struct probe *probe)
811 {
812 return probe->pops->can_evaluate_probe_arguments (probe);
813 }
814
815 /* See comments in probe.h. */
816
817 struct value *
818 evaluate_probe_argument (struct probe *probe, unsigned n,
819 struct frame_info *frame)
820 {
821 return probe->pops->evaluate_probe_argument (probe, n, frame);
822 }
823
824 /* See comments in probe.h. */
825
826 struct value *
827 probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
828 {
829 struct bound_probe probe;
830 unsigned n_args;
831
832 probe = find_probe_by_pc (get_frame_pc (frame));
833 if (!probe.probe)
834 return NULL;
835
836 n_args = get_probe_argument_count (probe.probe, frame);
837 if (n >= n_args)
838 return NULL;
839
840 return evaluate_probe_argument (probe.probe, n, frame);
841 }
842
843 /* See comment in probe.h. */
844
845 const struct probe_ops *
846 probe_linespec_to_ops (const char **linespecp)
847 {
848 int ix;
849 const struct probe_ops *probe_ops;
850
851 for (ix = 0; VEC_iterate (probe_ops_cp, all_probe_ops, ix, probe_ops); ix++)
852 if (probe_ops->is_linespec (linespecp))
853 return probe_ops;
854
855 return NULL;
856 }
857
858 /* See comment in probe.h. */
859
860 int
861 probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
862 {
863 const char *s = *linespecp;
864 const char *const *csp;
865
866 for (csp = keywords; *csp; csp++)
867 {
868 const char *keyword = *csp;
869 size_t len = strlen (keyword);
870
871 if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
872 {
873 *linespecp += len + 1;
874 return 1;
875 }
876 }
877
878 return 0;
879 }
880
881 /* Implementation of `is_linespec' method for `struct probe_ops'. */
882
883 static int
884 probe_any_is_linespec (const char **linespecp)
885 {
886 static const char *const keywords[] = { "-p", "-probe", NULL };
887
888 return probe_is_linespec_by_keyword (linespecp, keywords);
889 }
890
891 /* Dummy method used for `probe_ops_any'. */
892
893 static void
894 probe_any_get_probes (std::vector<probe *> *probesp, struct objfile *objfile)
895 {
896 /* No probes can be provided by this dummy backend. */
897 }
898
899 /* Operations associated with a generic probe. */
900
901 const struct probe_ops probe_ops_any =
902 {
903 probe_any_is_linespec,
904 probe_any_get_probes,
905 };
906
907 /* See comments in probe.h. */
908
909 struct cmd_list_element **
910 info_probes_cmdlist_get (void)
911 {
912 static struct cmd_list_element *info_probes_cmdlist;
913
914 if (info_probes_cmdlist == NULL)
915 add_prefix_cmd ("probes", class_info, info_probes_command,
916 _("\
917 Show available static probes.\n\
918 Usage: info probes [all|TYPE [ARGS]]\n\
919 TYPE specifies the type of the probe, and can be one of the following:\n\
920 - stap\n\
921 If you specify TYPE, there may be additional arguments needed by the\n\
922 subcommand.\n\
923 If you do not specify any argument, or specify `all', then the command\n\
924 will show information about all types of probes."),
925 &info_probes_cmdlist, "info probes ",
926 0/*allow-unknown*/, &infolist);
927
928 return &info_probes_cmdlist;
929 }
930
931 \f
932
933 /* This is called to compute the value of one of the $_probe_arg*
934 convenience variables. */
935
936 static struct value *
937 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
938 void *data)
939 {
940 struct frame_info *frame = get_selected_frame (_("No frame selected"));
941 CORE_ADDR pc = get_frame_pc (frame);
942 int sel = (int) (uintptr_t) data;
943 struct bound_probe pc_probe;
944 const struct sym_probe_fns *pc_probe_fns;
945 unsigned n_args;
946
947 /* SEL == -1 means "_probe_argc". */
948 gdb_assert (sel >= -1);
949
950 pc_probe = find_probe_by_pc (pc);
951 if (pc_probe.probe == NULL)
952 error (_("No probe at PC %s"), core_addr_to_string (pc));
953
954 n_args = get_probe_argument_count (pc_probe.probe, frame);
955 if (sel == -1)
956 return value_from_longest (builtin_type (arch)->builtin_int, n_args);
957
958 if (sel >= n_args)
959 error (_("Invalid probe argument %d -- probe has %u arguments available"),
960 sel, n_args);
961
962 return evaluate_probe_argument (pc_probe.probe, sel, frame);
963 }
964
965 /* This is called to compile one of the $_probe_arg* convenience
966 variables into an agent expression. */
967
968 static void
969 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
970 struct axs_value *value, void *data)
971 {
972 CORE_ADDR pc = expr->scope;
973 int sel = (int) (uintptr_t) data;
974 struct bound_probe pc_probe;
975 const struct sym_probe_fns *pc_probe_fns;
976 int n_args;
977 struct frame_info *frame = get_selected_frame (NULL);
978
979 /* SEL == -1 means "_probe_argc". */
980 gdb_assert (sel >= -1);
981
982 pc_probe = find_probe_by_pc (pc);
983 if (pc_probe.probe == NULL)
984 error (_("No probe at PC %s"), core_addr_to_string (pc));
985
986 n_args = get_probe_argument_count (pc_probe.probe, frame);
987
988 if (sel == -1)
989 {
990 value->kind = axs_rvalue;
991 value->type = builtin_type (expr->gdbarch)->builtin_int;
992 ax_const_l (expr, n_args);
993 return;
994 }
995
996 gdb_assert (sel >= 0);
997 if (sel >= n_args)
998 error (_("Invalid probe argument %d -- probe has %d arguments available"),
999 sel, n_args);
1000
1001 pc_probe.probe->pops->compile_to_ax (pc_probe.probe, expr, value, sel);
1002 }
1003
1004 static const struct internalvar_funcs probe_funcs =
1005 {
1006 compute_probe_arg,
1007 compile_probe_arg,
1008 NULL
1009 };
1010
1011
1012 VEC (probe_ops_cp) *all_probe_ops;
1013
1014 void
1015 _initialize_probe (void)
1016 {
1017 VEC_safe_push (probe_ops_cp, all_probe_ops, &probe_ops_any);
1018
1019 create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1020 (void *) (uintptr_t) -1);
1021 create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1022 (void *) (uintptr_t) 0);
1023 create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1024 (void *) (uintptr_t) 1);
1025 create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1026 (void *) (uintptr_t) 2);
1027 create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1028 (void *) (uintptr_t) 3);
1029 create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1030 (void *) (uintptr_t) 4);
1031 create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1032 (void *) (uintptr_t) 5);
1033 create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1034 (void *) (uintptr_t) 6);
1035 create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1036 (void *) (uintptr_t) 7);
1037 create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1038 (void *) (uintptr_t) 8);
1039 create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1040 (void *) (uintptr_t) 9);
1041 create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1042 (void *) (uintptr_t) 10);
1043 create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1044 (void *) (uintptr_t) 11);
1045
1046 add_cmd ("all", class_info, info_probes_command,
1047 _("\
1048 Show information about all type of probes."),
1049 info_probes_cmdlist_get ());
1050
1051 add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
1052 Enable probes.\n\
1053 Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
1054 Each argument is a regular expression, used to select probes.\n\
1055 PROVIDER matches probe provider names.\n\
1056 NAME matches the probe names.\n\
1057 OBJECT matches the executable or shared library name.\n\
1058 If you do not specify any argument then the command will enable\n\
1059 all defined probes."),
1060 &enablelist);
1061
1062 add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
1063 Disable probes.\n\
1064 Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
1065 Each argument is a regular expression, used to select probes.\n\
1066 PROVIDER matches probe provider names.\n\
1067 NAME matches the probe names.\n\
1068 OBJECT matches the executable or shared library name.\n\
1069 If you do not specify any argument then the command will disable\n\
1070 all defined probes."),
1071 &disablelist);
1072
1073 }
This page took 0.050802 seconds and 5 git commands to generate.