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