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