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