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