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