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