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