probe.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* Generic static probe support for GDB.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "probe.h"
  16. #include "command.h"
  17. #include "cli/cli-cmds.h"
  18. #include "cli/cli-utils.h"
  19. #include "objfiles.h"
  20. #include "symtab.h"
  21. #include "progspace.h"
  22. #include "filenames.h"
  23. #include "linespec.h"
  24. #include "gdbsupport/gdb_regex.h"
  25. #include "frame.h"
  26. #include "arch-utils.h"
  27. #include "value.h"
  28. #include "ax.h"
  29. #include "ax-gdb.h"
  30. #include "location.h"
  31. #include <ctype.h>
  32. #include <algorithm>
  33. #include "gdbsupport/gdb_optional.h"
  34. /* Class that implements the static probe methods for "any" probe. */
  35. class any_static_probe_ops : public static_probe_ops
  36. {
  37. public:
  38. /* See probe.h. */
  39. bool is_linespec (const char **linespecp) const override;
  40. /* See probe.h. */
  41. void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
  42. struct objfile *objfile) const override;
  43. /* See probe.h. */
  44. const char *type_name () const override;
  45. /* See probe.h. */
  46. std::vector<struct info_probe_column> gen_info_probes_table_header
  47. () const override;
  48. };
  49. /* Static operations associated with a generic probe. */
  50. const any_static_probe_ops any_static_probe_ops {};
  51. /* A helper for parse_probes that decodes a probe specification in
  52. SEARCH_PSPACE. It appends matching SALs to RESULT. */
  53. static void
  54. parse_probes_in_pspace (const static_probe_ops *spops,
  55. struct program_space *search_pspace,
  56. const char *objfile_namestr,
  57. const char *provider,
  58. const char *name,
  59. std::vector<symtab_and_line> *result)
  60. {
  61. for (objfile *objfile : search_pspace->objfiles ())
  62. {
  63. if (!objfile->sf || !objfile->sf->sym_probe_fns)
  64. continue;
  65. if (objfile_namestr
  66. && FILENAME_CMP (objfile_name (objfile), objfile_namestr) != 0
  67. && FILENAME_CMP (lbasename (objfile_name (objfile)),
  68. objfile_namestr) != 0)
  69. continue;
  70. const std::vector<std::unique_ptr<probe>> &probes
  71. = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
  72. for (auto &p : probes)
  73. {
  74. if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
  75. continue;
  76. if (provider != NULL && p->get_provider () != provider)
  77. continue;
  78. if (p->get_name () != name)
  79. continue;
  80. symtab_and_line sal;
  81. sal.pc = p->get_relocated_address (objfile);
  82. sal.explicit_pc = 1;
  83. sal.section = find_pc_overlay (sal.pc);
  84. sal.pspace = search_pspace;
  85. sal.prob = p.get ();
  86. sal.objfile = objfile;
  87. result->push_back (std::move (sal));
  88. }
  89. }
  90. }
  91. /* See definition in probe.h. */
  92. std::vector<symtab_and_line>
  93. parse_probes (const struct event_location *location,
  94. struct program_space *search_pspace,
  95. struct linespec_result *canonical)
  96. {
  97. char *arg_end, *arg;
  98. char *objfile_namestr = NULL, *provider = NULL, *name, *p;
  99. const char *arg_start, *cs;
  100. gdb_assert (event_location_type (location) == PROBE_LOCATION);
  101. arg_start = get_probe_location (location);
  102. cs = arg_start;
  103. const static_probe_ops *spops = probe_linespec_to_static_ops (&cs);
  104. if (spops == NULL)
  105. error (_("'%s' is not a probe linespec"), arg_start);
  106. arg = (char *) cs;
  107. arg = skip_spaces (arg);
  108. if (!*arg)
  109. error (_("argument to `%s' missing"), arg_start);
  110. arg_end = skip_to_space (arg);
  111. /* We make a copy here so we can write over parts with impunity. */
  112. std::string copy (arg, arg_end - arg);
  113. arg = &copy[0];
  114. /* Extract each word from the argument, separated by ":"s. */
  115. p = strchr (arg, ':');
  116. if (p == NULL)
  117. {
  118. /* This is `-p name'. */
  119. name = arg;
  120. }
  121. else
  122. {
  123. char *hold = p + 1;
  124. *p = '\0';
  125. p = strchr (hold, ':');
  126. if (p == NULL)
  127. {
  128. /* This is `-p provider:name'. */
  129. provider = arg;
  130. name = hold;
  131. }
  132. else
  133. {
  134. /* This is `-p objfile:provider:name'. */
  135. *p = '\0';
  136. objfile_namestr = arg;
  137. provider = hold;
  138. name = p + 1;
  139. }
  140. }
  141. if (*name == '\0')
  142. error (_("no probe name specified"));
  143. if (provider && *provider == '\0')
  144. error (_("invalid provider name"));
  145. if (objfile_namestr && *objfile_namestr == '\0')
  146. error (_("invalid objfile name"));
  147. std::vector<symtab_and_line> result;
  148. if (search_pspace != NULL)
  149. {
  150. parse_probes_in_pspace (spops, search_pspace, objfile_namestr,
  151. provider, name, &result);
  152. }
  153. else
  154. {
  155. for (struct program_space *pspace : program_spaces)
  156. parse_probes_in_pspace (spops, pspace, objfile_namestr,
  157. provider, name, &result);
  158. }
  159. if (result.empty ())
  160. {
  161. throw_error (NOT_FOUND_ERROR,
  162. _("No probe matching objfile=`%s', provider=`%s', name=`%s'"),
  163. objfile_namestr ? objfile_namestr : _("<any>"),
  164. provider ? provider : _("<any>"),
  165. name);
  166. }
  167. if (canonical)
  168. {
  169. std::string canon (arg_start, arg_end - arg_start);
  170. canonical->special_display = 1;
  171. canonical->pre_expanded = 1;
  172. canonical->location = new_probe_location (std::move (canon));
  173. }
  174. return result;
  175. }
  176. /* See definition in probe.h. */
  177. std::vector<probe *>
  178. find_probes_in_objfile (struct objfile *objfile, const char *provider,
  179. const char *name)
  180. {
  181. std::vector<probe *> result;
  182. if (!objfile->sf || !objfile->sf->sym_probe_fns)
  183. return result;
  184. const std::vector<std::unique_ptr<probe>> &probes
  185. = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
  186. for (auto &p : probes)
  187. {
  188. if (p->get_provider () != provider)
  189. continue;
  190. if (p->get_name () != name)
  191. continue;
  192. result.push_back (p.get ());
  193. }
  194. return result;
  195. }
  196. /* See definition in probe.h. */
  197. struct bound_probe
  198. find_probe_by_pc (CORE_ADDR pc)
  199. {
  200. struct bound_probe result;
  201. result.objfile = NULL;
  202. result.prob = NULL;
  203. for (objfile *objfile : current_program_space->objfiles ())
  204. {
  205. if (!objfile->sf || !objfile->sf->sym_probe_fns
  206. || objfile->sect_index_text == -1)
  207. continue;
  208. /* If this proves too inefficient, we can replace with a hash. */
  209. const std::vector<std::unique_ptr<probe>> &probes
  210. = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
  211. for (auto &p : probes)
  212. if (p->get_relocated_address (objfile) == pc)
  213. {
  214. result.objfile = objfile;
  215. result.prob = p.get ();
  216. return result;
  217. }
  218. }
  219. return result;
  220. }
  221. /* Make a vector of probes matching OBJNAME, PROVIDER, and PROBE_NAME.
  222. If SPOPS is not &any_static_probe_ops, only probes related to this
  223. specific static probe ops will match. Each argument is a regexp,
  224. or NULL, which matches anything. */
  225. static std::vector<bound_probe>
  226. collect_probes (const std::string &objname, const std::string &provider,
  227. const std::string &probe_name, const static_probe_ops *spops)
  228. {
  229. std::vector<bound_probe> result;
  230. gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
  231. if (!provider.empty ())
  232. prov_pat.emplace (provider.c_str (), REG_NOSUB,
  233. _("Invalid provider regexp"));
  234. if (!probe_name.empty ())
  235. probe_pat.emplace (probe_name.c_str (), REG_NOSUB,
  236. _("Invalid probe regexp"));
  237. if (!objname.empty ())
  238. obj_pat.emplace (objname.c_str (), REG_NOSUB,
  239. _("Invalid object file regexp"));
  240. for (objfile *objfile : current_program_space->objfiles ())
  241. {
  242. if (! objfile->sf || ! objfile->sf->sym_probe_fns)
  243. continue;
  244. if (obj_pat)
  245. {
  246. if (obj_pat->exec (objfile_name (objfile), 0, NULL, 0) != 0)
  247. continue;
  248. }
  249. const std::vector<std::unique_ptr<probe>> &probes
  250. = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
  251. for (auto &p : probes)
  252. {
  253. if (spops != &any_static_probe_ops && p->get_static_ops () != spops)
  254. continue;
  255. if (prov_pat
  256. && prov_pat->exec (p->get_provider ().c_str (), 0, NULL, 0) != 0)
  257. continue;
  258. if (probe_pat
  259. && probe_pat->exec (p->get_name ().c_str (), 0, NULL, 0) != 0)
  260. continue;
  261. result.emplace_back (p.get (), objfile);
  262. }
  263. }
  264. return result;
  265. }
  266. /* A qsort comparison function for bound_probe_s objects. */
  267. static bool
  268. compare_probes (const bound_probe &a, const bound_probe &b)
  269. {
  270. int v;
  271. v = a.prob->get_provider ().compare (b.prob->get_provider ());
  272. if (v != 0)
  273. return v < 0;
  274. v = a.prob->get_name ().compare (b.prob->get_name ());
  275. if (v != 0)
  276. return v < 0;
  277. if (a.prob->get_address () != b.prob->get_address ())
  278. return a.prob->get_address () < b.prob->get_address ();
  279. return strcmp (objfile_name (a.objfile), objfile_name (b.objfile)) < 0;
  280. }
  281. /* Helper function that generate entries in the ui_out table being
  282. crafted by `info_probes_for_ops'. */
  283. static void
  284. gen_ui_out_table_header_info (const std::vector<bound_probe> &probes,
  285. const static_probe_ops *spops)
  286. {
  287. /* `headings' refers to the names of the columns when printing `info
  288. probes'. */
  289. gdb_assert (spops != NULL);
  290. std::vector<struct info_probe_column> headings
  291. = spops->gen_info_probes_table_header ();
  292. for (const info_probe_column &column : headings)
  293. {
  294. size_t size_max = strlen (column.print_name);
  295. for (const bound_probe &probe : probes)
  296. {
  297. /* `probe_fields' refers to the values of each new field that this
  298. probe will display. */
  299. if (probe.prob->get_static_ops () != spops)
  300. continue;
  301. std::vector<const char *> probe_fields
  302. = probe.prob->gen_info_probes_table_values ();
  303. gdb_assert (probe_fields.size () == headings.size ());
  304. for (const char *val : probe_fields)
  305. {
  306. /* It is valid to have a NULL value here, which means that the
  307. backend does not have something to write and this particular
  308. field should be skipped. */
  309. if (val == NULL)
  310. continue;
  311. size_max = std::max (strlen (val), size_max);
  312. }
  313. }
  314. current_uiout->table_header (size_max, ui_left,
  315. column.field_name, column.print_name);
  316. }
  317. }
  318. /* Helper function to print not-applicable strings for all the extra
  319. columns defined in a static_probe_ops. */
  320. static void
  321. print_ui_out_not_applicables (const static_probe_ops *spops)
  322. {
  323. std::vector<struct info_probe_column> headings
  324. = spops->gen_info_probes_table_header ();
  325. for (const info_probe_column &column : headings)
  326. current_uiout->field_string (column.field_name, _("n/a"));
  327. }
  328. /* Helper function to print extra information about a probe and an objfile
  329. represented by PROBE. */
  330. static void
  331. print_ui_out_info (probe *probe)
  332. {
  333. /* `values' refers to the actual values of each new field in the output
  334. of `info probe'. `headings' refers to the names of each new field. */
  335. gdb_assert (probe != NULL);
  336. std::vector<struct info_probe_column> headings
  337. = probe->get_static_ops ()->gen_info_probes_table_header ();
  338. std::vector<const char *> values
  339. = probe->gen_info_probes_table_values ();
  340. gdb_assert (headings.size () == values.size ());
  341. for (int ix = 0; ix < headings.size (); ++ix)
  342. {
  343. struct info_probe_column column = headings[ix];
  344. const char *val = values[ix];
  345. if (val == NULL)
  346. current_uiout->field_skip (column.field_name);
  347. else
  348. current_uiout->field_string (column.field_name, val);
  349. }
  350. }
  351. /* Helper function that returns the number of extra fields which POPS will
  352. need. */
  353. static int
  354. get_number_extra_fields (const static_probe_ops *spops)
  355. {
  356. return spops->gen_info_probes_table_header ().size ();
  357. }
  358. /* Helper function that returns true if there is a probe in PROBES
  359. featuring the given SPOPS. It returns false otherwise. */
  360. static bool
  361. exists_probe_with_spops (const std::vector<bound_probe> &probes,
  362. const static_probe_ops *spops)
  363. {
  364. for (const bound_probe &probe : probes)
  365. if (probe.prob->get_static_ops () == spops)
  366. return true;
  367. return false;
  368. }
  369. /* Helper function that parses a probe linespec of the form [PROVIDER
  370. [PROBE [OBJNAME]]] from the provided string STR. */
  371. static void
  372. parse_probe_linespec (const char *str, std::string *provider,
  373. std::string *probe_name, std::string *objname)
  374. {
  375. *probe_name = *objname = "";
  376. *provider = extract_arg (&str);
  377. if (!provider->empty ())
  378. {
  379. *probe_name = extract_arg (&str);
  380. if (!probe_name->empty ())
  381. *objname = extract_arg (&str);
  382. }
  383. }
  384. /* See comment in probe.h. */
  385. void
  386. info_probes_for_spops (const char *arg, int from_tty,
  387. const static_probe_ops *spops)
  388. {
  389. std::string provider, probe_name, objname;
  390. int any_found;
  391. int ui_out_extra_fields = 0;
  392. size_t size_addr;
  393. size_t size_name = strlen ("Name");
  394. size_t size_objname = strlen ("Object");
  395. size_t size_provider = strlen ("Provider");
  396. size_t size_type = strlen ("Type");
  397. struct gdbarch *gdbarch = get_current_arch ();
  398. parse_probe_linespec (arg, &provider, &probe_name, &objname);
  399. std::vector<bound_probe> probes
  400. = collect_probes (objname, provider, probe_name, spops);
  401. if (spops == &any_static_probe_ops)
  402. {
  403. /* If SPOPS is &any_static_probe_ops, it means the user has
  404. requested a "simple" `info probes', i.e., she wants to print
  405. all information about all probes. For that, we have to
  406. identify how many extra fields we will need to add in the
  407. ui_out table.
  408. To do that, we iterate over all static_probe_ops, querying
  409. each one about its extra fields, and incrementing
  410. `ui_out_extra_fields' to reflect that number. But note that
  411. we ignore the static_probe_ops for which no probes are
  412. defined with the given search criteria. */
  413. for (const static_probe_ops *po : all_static_probe_ops)
  414. if (exists_probe_with_spops (probes, po))
  415. ui_out_extra_fields += get_number_extra_fields (po);
  416. }
  417. else
  418. ui_out_extra_fields = get_number_extra_fields (spops);
  419. {
  420. ui_out_emit_table table_emitter (current_uiout,
  421. 5 + ui_out_extra_fields,
  422. probes.size (), "StaticProbes");
  423. std::sort (probes.begin (), probes.end (), compare_probes);
  424. /* What's the size of an address in our architecture? */
  425. size_addr = gdbarch_addr_bit (gdbarch) == 64 ? 18 : 10;
  426. /* Determining the maximum size of each field (`type', `provider',
  427. `name' and `objname'). */
  428. for (const bound_probe &probe : probes)
  429. {
  430. const char *probe_type = probe.prob->get_static_ops ()->type_name ();
  431. size_type = std::max (strlen (probe_type), size_type);
  432. size_name = std::max (probe.prob->get_name ().size (), size_name);
  433. size_provider = std::max (probe.prob->get_provider ().size (),
  434. size_provider);
  435. size_objname = std::max (strlen (objfile_name (probe.objfile)),
  436. size_objname);
  437. }
  438. current_uiout->table_header (size_type, ui_left, "type", _("Type"));
  439. current_uiout->table_header (size_provider, ui_left, "provider",
  440. _("Provider"));
  441. current_uiout->table_header (size_name, ui_left, "name", _("Name"));
  442. current_uiout->table_header (size_addr, ui_left, "addr", _("Where"));
  443. if (spops == &any_static_probe_ops)
  444. {
  445. /* We have to generate the table header for each new probe type
  446. that we will print. Note that this excludes probe types not
  447. having any defined probe with the search criteria. */
  448. for (const static_probe_ops *po : all_static_probe_ops)
  449. if (exists_probe_with_spops (probes, po))
  450. gen_ui_out_table_header_info (probes, po);
  451. }
  452. else
  453. gen_ui_out_table_header_info (probes, spops);
  454. current_uiout->table_header (size_objname, ui_left, "object", _("Object"));
  455. current_uiout->table_body ();
  456. for (const bound_probe &probe : probes)
  457. {
  458. const char *probe_type = probe.prob->get_static_ops ()->type_name ();
  459. ui_out_emit_tuple tuple_emitter (current_uiout, "probe");
  460. current_uiout->field_string ("type", probe_type);
  461. current_uiout->field_string ("provider", probe.prob->get_provider ());
  462. current_uiout->field_string ("name", probe.prob->get_name ());
  463. current_uiout->field_core_addr ("addr", probe.prob->get_gdbarch (),
  464. probe.prob->get_relocated_address
  465. (probe.objfile));
  466. if (spops == &any_static_probe_ops)
  467. {
  468. for (const static_probe_ops *po : all_static_probe_ops)
  469. {
  470. if (probe.prob->get_static_ops () == po)
  471. print_ui_out_info (probe.prob);
  472. else if (exists_probe_with_spops (probes, po))
  473. print_ui_out_not_applicables (po);
  474. }
  475. }
  476. else
  477. print_ui_out_info (probe.prob);
  478. current_uiout->field_string ("object",
  479. objfile_name (probe.objfile));
  480. current_uiout->text ("\n");
  481. }
  482. any_found = !probes.empty ();
  483. }
  484. if (!any_found)
  485. current_uiout->message (_("No probes matched.\n"));
  486. }
  487. /* Implementation of the `info probes' command. */
  488. static void
  489. info_probes_command (const char *arg, int from_tty)
  490. {
  491. info_probes_for_spops (arg, from_tty, &any_static_probe_ops);
  492. }
  493. /* Implementation of the `enable probes' command. */
  494. static void
  495. enable_probes_command (const char *arg, int from_tty)
  496. {
  497. std::string provider, probe_name, objname;
  498. parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
  499. std::vector<bound_probe> probes
  500. = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
  501. if (probes.empty ())
  502. {
  503. current_uiout->message (_("No probes matched.\n"));
  504. return;
  505. }
  506. /* Enable the selected probes, provided their backends support the
  507. notion of enabling a probe. */
  508. for (const bound_probe &probe: probes)
  509. {
  510. if (probe.prob->get_static_ops ()->can_enable ())
  511. {
  512. probe.prob->enable ();
  513. current_uiout->message (_("Probe %s:%s enabled.\n"),
  514. probe.prob->get_provider ().c_str (),
  515. probe.prob->get_name ().c_str ());
  516. }
  517. else
  518. current_uiout->message (_("Probe %s:%s cannot be enabled.\n"),
  519. probe.prob->get_provider ().c_str (),
  520. probe.prob->get_name ().c_str ());
  521. }
  522. }
  523. /* Implementation of the `disable probes' command. */
  524. static void
  525. disable_probes_command (const char *arg, int from_tty)
  526. {
  527. std::string provider, probe_name, objname;
  528. parse_probe_linespec ((const char *) arg, &provider, &probe_name, &objname);
  529. std::vector<bound_probe> probes
  530. = collect_probes (objname, provider, probe_name, &any_static_probe_ops);
  531. if (probes.empty ())
  532. {
  533. current_uiout->message (_("No probes matched.\n"));
  534. return;
  535. }
  536. /* Disable the selected probes, provided their backends support the
  537. notion of enabling a probe. */
  538. for (const bound_probe &probe : probes)
  539. {
  540. if (probe.prob->get_static_ops ()->can_enable ())
  541. {
  542. probe.prob->disable ();
  543. current_uiout->message (_("Probe %s:%s disabled.\n"),
  544. probe.prob->get_provider ().c_str (),
  545. probe.prob->get_name ().c_str ());
  546. }
  547. else
  548. current_uiout->message (_("Probe %s:%s cannot be disabled.\n"),
  549. probe.prob->get_provider ().c_str (),
  550. probe.prob->get_name ().c_str ());
  551. }
  552. }
  553. /* See comments in probe.h. */
  554. struct value *
  555. probe_safe_evaluate_at_pc (struct frame_info *frame, unsigned n)
  556. {
  557. struct bound_probe probe;
  558. unsigned n_args;
  559. probe = find_probe_by_pc (get_frame_pc (frame));
  560. if (!probe.prob)
  561. return NULL;
  562. n_args = probe.prob->get_argument_count (get_frame_arch (frame));
  563. if (n >= n_args)
  564. return NULL;
  565. return probe.prob->evaluate_argument (n, frame);
  566. }
  567. /* See comment in probe.h. */
  568. const struct static_probe_ops *
  569. probe_linespec_to_static_ops (const char **linespecp)
  570. {
  571. for (const static_probe_ops *ops : all_static_probe_ops)
  572. if (ops->is_linespec (linespecp))
  573. return ops;
  574. return NULL;
  575. }
  576. /* See comment in probe.h. */
  577. int
  578. probe_is_linespec_by_keyword (const char **linespecp, const char *const *keywords)
  579. {
  580. const char *s = *linespecp;
  581. const char *const *csp;
  582. for (csp = keywords; *csp; csp++)
  583. {
  584. const char *keyword = *csp;
  585. size_t len = strlen (keyword);
  586. if (strncmp (s, keyword, len) == 0 && isspace (s[len]))
  587. {
  588. *linespecp += len + 1;
  589. return 1;
  590. }
  591. }
  592. return 0;
  593. }
  594. /* Implementation of `is_linespec' method. */
  595. bool
  596. any_static_probe_ops::is_linespec (const char **linespecp) const
  597. {
  598. static const char *const keywords[] = { "-p", "-probe", NULL };
  599. return probe_is_linespec_by_keyword (linespecp, keywords);
  600. }
  601. /* Implementation of 'get_probes' method. */
  602. void
  603. any_static_probe_ops::get_probes (std::vector<std::unique_ptr<probe>> *probesp,
  604. struct objfile *objfile) const
  605. {
  606. /* No probes can be provided by this dummy backend. */
  607. }
  608. /* Implementation of the 'type_name' method. */
  609. const char *
  610. any_static_probe_ops::type_name () const
  611. {
  612. return NULL;
  613. }
  614. /* Implementation of the 'gen_info_probes_table_header' method. */
  615. std::vector<struct info_probe_column>
  616. any_static_probe_ops::gen_info_probes_table_header () const
  617. {
  618. return std::vector<struct info_probe_column> ();
  619. }
  620. /* See comments in probe.h. */
  621. struct cmd_list_element **
  622. info_probes_cmdlist_get (void)
  623. {
  624. static struct cmd_list_element *info_probes_cmdlist;
  625. if (info_probes_cmdlist == NULL)
  626. add_prefix_cmd ("probes", class_info, info_probes_command,
  627. _("\
  628. Show available static probes.\n\
  629. Usage: info probes [all|TYPE [ARGS]]\n\
  630. TYPE specifies the type of the probe, and can be one of the following:\n\
  631. - stap\n\
  632. If you specify TYPE, there may be additional arguments needed by the\n\
  633. subcommand.\n\
  634. If you do not specify any argument, or specify `all', then the command\n\
  635. will show information about all types of probes."),
  636. &info_probes_cmdlist, 0/*allow-unknown*/, &infolist);
  637. return &info_probes_cmdlist;
  638. }
  639. /* This is called to compute the value of one of the $_probe_arg*
  640. convenience variables. */
  641. static struct value *
  642. compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
  643. void *data)
  644. {
  645. struct frame_info *frame = get_selected_frame (_("No frame selected"));
  646. CORE_ADDR pc = get_frame_pc (frame);
  647. int sel = (int) (uintptr_t) data;
  648. struct bound_probe pc_probe;
  649. unsigned n_args;
  650. /* SEL == -1 means "_probe_argc". */
  651. gdb_assert (sel >= -1);
  652. pc_probe = find_probe_by_pc (pc);
  653. if (pc_probe.prob == NULL)
  654. error (_("No probe at PC %s"), core_addr_to_string (pc));
  655. n_args = pc_probe.prob->get_argument_count (arch);
  656. if (sel == -1)
  657. return value_from_longest (builtin_type (arch)->builtin_int, n_args);
  658. if (sel >= n_args)
  659. error (_("Invalid probe argument %d -- probe has %u arguments available"),
  660. sel, n_args);
  661. return pc_probe.prob->evaluate_argument (sel, frame);
  662. }
  663. /* This is called to compile one of the $_probe_arg* convenience
  664. variables into an agent expression. */
  665. static void
  666. compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
  667. struct axs_value *value, void *data)
  668. {
  669. CORE_ADDR pc = expr->scope;
  670. int sel = (int) (uintptr_t) data;
  671. struct bound_probe pc_probe;
  672. int n_args;
  673. /* SEL == -1 means "_probe_argc". */
  674. gdb_assert (sel >= -1);
  675. pc_probe = find_probe_by_pc (pc);
  676. if (pc_probe.prob == NULL)
  677. error (_("No probe at PC %s"), core_addr_to_string (pc));
  678. n_args = pc_probe.prob->get_argument_count (expr->gdbarch);
  679. if (sel == -1)
  680. {
  681. value->kind = axs_rvalue;
  682. value->type = builtin_type (expr->gdbarch)->builtin_int;
  683. ax_const_l (expr, n_args);
  684. return;
  685. }
  686. gdb_assert (sel >= 0);
  687. if (sel >= n_args)
  688. error (_("Invalid probe argument %d -- probe has %d arguments available"),
  689. sel, n_args);
  690. pc_probe.prob->compile_to_ax (expr, value, sel);
  691. }
  692. static const struct internalvar_funcs probe_funcs =
  693. {
  694. compute_probe_arg,
  695. compile_probe_arg,
  696. };
  697. std::vector<const static_probe_ops *> all_static_probe_ops;
  698. void _initialize_probe ();
  699. void
  700. _initialize_probe ()
  701. {
  702. all_static_probe_ops.push_back (&any_static_probe_ops);
  703. create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
  704. (void *) (uintptr_t) -1);
  705. create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
  706. (void *) (uintptr_t) 0);
  707. create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
  708. (void *) (uintptr_t) 1);
  709. create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
  710. (void *) (uintptr_t) 2);
  711. create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
  712. (void *) (uintptr_t) 3);
  713. create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
  714. (void *) (uintptr_t) 4);
  715. create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
  716. (void *) (uintptr_t) 5);
  717. create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
  718. (void *) (uintptr_t) 6);
  719. create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
  720. (void *) (uintptr_t) 7);
  721. create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
  722. (void *) (uintptr_t) 8);
  723. create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
  724. (void *) (uintptr_t) 9);
  725. create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
  726. (void *) (uintptr_t) 10);
  727. create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
  728. (void *) (uintptr_t) 11);
  729. add_cmd ("all", class_info, info_probes_command,
  730. _("\
  731. Show information about all type of probes."),
  732. info_probes_cmdlist_get ());
  733. add_cmd ("probes", class_breakpoint, enable_probes_command, _("\
  734. Enable probes.\n\
  735. Usage: enable probes [PROVIDER [NAME [OBJECT]]]\n\
  736. Each argument is a regular expression, used to select probes.\n\
  737. PROVIDER matches probe provider names.\n\
  738. NAME matches the probe names.\n\
  739. OBJECT matches the executable or shared library name.\n\
  740. If you do not specify any argument then the command will enable\n\
  741. all defined probes."),
  742. &enablelist);
  743. add_cmd ("probes", class_breakpoint, disable_probes_command, _("\
  744. Disable probes.\n\
  745. Usage: disable probes [PROVIDER [NAME [OBJECT]]]\n\
  746. Each argument is a regular expression, used to select probes.\n\
  747. PROVIDER matches probe provider names.\n\
  748. NAME matches the probe names.\n\
  749. OBJECT matches the executable or shared library name.\n\
  750. If you do not specify any argument then the command will disable\n\
  751. all defined probes."),
  752. &disablelist);
  753. }