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