dtrace-probe.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /* DTrace probe support for GDB.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. Contributed by Oracle, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "probe.h"
  17. #include "elf-bfd.h"
  18. #include "gdbtypes.h"
  19. #include "obstack.h"
  20. #include "objfiles.h"
  21. #include "complaints.h"
  22. #include "value.h"
  23. #include "ax.h"
  24. #include "ax-gdb.h"
  25. #include "language.h"
  26. #include "parser-defs.h"
  27. #include "inferior.h"
  28. #include "expop.h"
  29. /* The type of the ELF sections where we will find the DOF programs
  30. with information about probes. */
  31. #ifndef SHT_SUNW_dof
  32. # define SHT_SUNW_dof 0x6ffffff4
  33. #endif
  34. /* The following structure represents a single argument for the
  35. probe. */
  36. struct dtrace_probe_arg
  37. {
  38. dtrace_probe_arg (struct type *type_, std::string &&type_str_,
  39. expression_up &&expr_)
  40. : type (type_), type_str (std::move (type_str_)),
  41. expr (std::move (expr_))
  42. {}
  43. /* The type of the probe argument. */
  44. struct type *type;
  45. /* A string describing the type. */
  46. std::string type_str;
  47. /* The argument converted to an internal GDB expression. */
  48. expression_up expr;
  49. };
  50. /* The following structure represents an enabler for a probe. */
  51. struct dtrace_probe_enabler
  52. {
  53. /* Program counter where the is-enabled probe is installed. The
  54. contents (nops, whatever...) stored at this address are
  55. architecture dependent. */
  56. CORE_ADDR address;
  57. };
  58. /* Class that implements the static probe methods for "stap" probes. */
  59. class dtrace_static_probe_ops : public static_probe_ops
  60. {
  61. public:
  62. /* See probe.h. */
  63. bool is_linespec (const char **linespecp) const override;
  64. /* See probe.h. */
  65. void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
  66. struct objfile *objfile) const override;
  67. /* See probe.h. */
  68. const char *type_name () const override;
  69. /* See probe.h. */
  70. bool can_enable () const override
  71. {
  72. return true;
  73. }
  74. /* See probe.h. */
  75. std::vector<struct info_probe_column> gen_info_probes_table_header
  76. () const override;
  77. };
  78. /* DTrace static_probe_ops. */
  79. const dtrace_static_probe_ops dtrace_static_probe_ops {};
  80. /* The following structure represents a dtrace probe. */
  81. class dtrace_probe : public probe
  82. {
  83. public:
  84. /* Constructor for dtrace_probe. */
  85. dtrace_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_,
  86. struct gdbarch *arch_,
  87. std::vector<struct dtrace_probe_arg> &&args_,
  88. std::vector<struct dtrace_probe_enabler> &&enablers_)
  89. : probe (std::move (name_), std::move (provider_), address_, arch_),
  90. m_args (std::move (args_)),
  91. m_enablers (std::move (enablers_)),
  92. m_args_expr_built (false)
  93. {}
  94. /* See probe.h. */
  95. CORE_ADDR get_relocated_address (struct objfile *objfile) override;
  96. /* See probe.h. */
  97. unsigned get_argument_count (struct gdbarch *gdbarch) override;
  98. /* See probe.h. */
  99. bool can_evaluate_arguments () const override;
  100. /* See probe.h. */
  101. struct value *evaluate_argument (unsigned n,
  102. struct frame_info *frame) override;
  103. /* See probe.h. */
  104. void compile_to_ax (struct agent_expr *aexpr,
  105. struct axs_value *axs_value,
  106. unsigned n) override;
  107. /* See probe.h. */
  108. const static_probe_ops *get_static_ops () const override;
  109. /* See probe.h. */
  110. std::vector<const char *> gen_info_probes_table_values () const override;
  111. /* See probe.h. */
  112. void enable () override;
  113. /* See probe.h. */
  114. void disable () override;
  115. /* Return the Nth argument of the probe. */
  116. struct dtrace_probe_arg *get_arg_by_number (unsigned n,
  117. struct gdbarch *gdbarch);
  118. /* Build the GDB internal expression that, once evaluated, will
  119. calculate the values of the arguments of the probe. */
  120. void build_arg_exprs (struct gdbarch *gdbarch);
  121. /* Determine whether the probe is "enabled" or "disabled". A
  122. disabled probe is a probe in which one or more enablers are
  123. disabled. */
  124. bool is_enabled () const;
  125. private:
  126. /* A probe can have zero or more arguments. */
  127. std::vector<struct dtrace_probe_arg> m_args;
  128. /* A probe can have zero or more "enablers" associated with it. */
  129. std::vector<struct dtrace_probe_enabler> m_enablers;
  130. /* Whether the expressions for the arguments have been built. */
  131. bool m_args_expr_built;
  132. };
  133. /* DOF programs can contain an arbitrary number of sections of 26
  134. different types. In order to support DTrace USDT probes we only
  135. need to handle a subset of these section types, fortunately. These
  136. section types are defined in the following enumeration.
  137. See linux/dtrace/dof_defines.h for a complete list of section types
  138. along with their values. */
  139. enum dtrace_dof_sect_type
  140. {
  141. /* Null section. */
  142. DTRACE_DOF_SECT_TYPE_NONE = 0,
  143. /* A dof_ecbdesc_t. */
  144. DTRACE_DOF_SECT_TYPE_ECBDESC = 3,
  145. /* A string table. */
  146. DTRACE_DOF_SECT_TYPE_STRTAB = 8,
  147. /* A dof_provider_t */
  148. DTRACE_DOF_SECT_TYPE_PROVIDER = 15,
  149. /* Array of dof_probe_t */
  150. DTRACE_DOF_SECT_TYPE_PROBES = 16,
  151. /* An array of probe arg mappings. */
  152. DTRACE_DOF_SECT_TYPE_PRARGS = 17,
  153. /* An array of probe arg offsets. */
  154. DTRACE_DOF_SECT_TYPE_PROFFS = 18,
  155. /* An array of probe is-enabled offsets. */
  156. DTRACE_DOF_SECT_TYPE_PRENOFFS = 26
  157. };
  158. /* The following collection of data structures map the structure of
  159. DOF entities. Again, we only cover the subset of DOF used to
  160. implement USDT probes.
  161. See linux/dtrace/dof.h header for a complete list of data
  162. structures. */
  163. /* Offsets to index the dofh_ident[] array defined below. */
  164. enum dtrace_dof_ident
  165. {
  166. /* First byte of the magic number. */
  167. DTRACE_DOF_ID_MAG0 = 0,
  168. /* Second byte of the magic number. */
  169. DTRACE_DOF_ID_MAG1 = 1,
  170. /* Third byte of the magic number. */
  171. DTRACE_DOF_ID_MAG2 = 2,
  172. /* Fourth byte of the magic number. */
  173. DTRACE_DOF_ID_MAG3 = 3,
  174. /* An enum_dof_encoding value. */
  175. DTRACE_DOF_ID_ENCODING = 5
  176. };
  177. /* Possible values for dofh_ident[DOF_ID_ENCODING]. */
  178. enum dtrace_dof_encoding
  179. {
  180. /* The DOF program is little-endian. */
  181. DTRACE_DOF_ENCODE_LSB = 1,
  182. /* The DOF program is big-endian. */
  183. DTRACE_DOF_ENCODE_MSB = 2
  184. };
  185. /* A DOF header, which describes the contents of a DOF program: number
  186. of sections, size, etc. */
  187. struct dtrace_dof_hdr
  188. {
  189. /* Identification bytes (see above). */
  190. uint8_t dofh_ident[16];
  191. /* File attribute flags (if any). */
  192. uint32_t dofh_flags;
  193. /* Size of file header in bytes. */
  194. uint32_t dofh_hdrsize;
  195. /* Size of section header in bytes. */
  196. uint32_t dofh_secsize;
  197. /* Number of section headers. */
  198. uint32_t dofh_secnum;
  199. /* File offset of section headers. */
  200. uint64_t dofh_secoff;
  201. /* File size of loadable portion. */
  202. uint64_t dofh_loadsz;
  203. /* File size of entire DOF file. */
  204. uint64_t dofh_filesz;
  205. /* Reserved for future use. */
  206. uint64_t dofh_pad;
  207. };
  208. /* A DOF section, whose contents depend on its type. The several
  209. supported section types are described in the enum
  210. dtrace_dof_sect_type above. */
  211. struct dtrace_dof_sect
  212. {
  213. /* Section type (see the define above). */
  214. uint32_t dofs_type;
  215. /* Section data memory alignment. */
  216. uint32_t dofs_align;
  217. /* Section flags (if any). */
  218. uint32_t dofs_flags;
  219. /* Size of section entry (if table). */
  220. uint32_t dofs_entsize;
  221. /* DOF + offset points to the section data. */
  222. uint64_t dofs_offset;
  223. /* Size of section data in bytes. */
  224. uint64_t dofs_size;
  225. };
  226. /* A DOF provider, which is the provider of a probe. */
  227. struct dtrace_dof_provider
  228. {
  229. /* Link to a DTRACE_DOF_SECT_TYPE_STRTAB section. */
  230. uint32_t dofpv_strtab;
  231. /* Link to a DTRACE_DOF_SECT_TYPE_PROBES section. */
  232. uint32_t dofpv_probes;
  233. /* Link to a DTRACE_DOF_SECT_TYPE_PRARGS section. */
  234. uint32_t dofpv_prargs;
  235. /* Link to a DTRACE_DOF_SECT_TYPE_PROFFS section. */
  236. uint32_t dofpv_proffs;
  237. /* Provider name string. */
  238. uint32_t dofpv_name;
  239. /* Provider attributes. */
  240. uint32_t dofpv_provattr;
  241. /* Module attributes. */
  242. uint32_t dofpv_modattr;
  243. /* Function attributes. */
  244. uint32_t dofpv_funcattr;
  245. /* Name attributes. */
  246. uint32_t dofpv_nameattr;
  247. /* Args attributes. */
  248. uint32_t dofpv_argsattr;
  249. /* Link to a DTRACE_DOF_SECT_PRENOFFS section. */
  250. uint32_t dofpv_prenoffs;
  251. };
  252. /* A set of DOF probes and is-enabled probes sharing a base address
  253. and several attributes. The particular locations and attributes of
  254. each probe are maintained in arrays in several other DOF sections.
  255. See the comment in dtrace_process_dof_probe for details on how
  256. these attributes are stored. */
  257. struct dtrace_dof_probe
  258. {
  259. /* Probe base address or offset. */
  260. uint64_t dofpr_addr;
  261. /* Probe function string. */
  262. uint32_t dofpr_func;
  263. /* Probe name string. */
  264. uint32_t dofpr_name;
  265. /* Native argument type strings. */
  266. uint32_t dofpr_nargv;
  267. /* Translated argument type strings. */
  268. uint32_t dofpr_xargv;
  269. /* Index of first argument mapping. */
  270. uint32_t dofpr_argidx;
  271. /* Index of first offset entry. */
  272. uint32_t dofpr_offidx;
  273. /* Native argument count. */
  274. uint8_t dofpr_nargc;
  275. /* Translated argument count. */
  276. uint8_t dofpr_xargc;
  277. /* Number of offset entries for probe. */
  278. uint16_t dofpr_noffs;
  279. /* Index of first is-enabled offset. */
  280. uint32_t dofpr_enoffidx;
  281. /* Number of is-enabled offsets. */
  282. uint16_t dofpr_nenoffs;
  283. /* Reserved for future use. */
  284. uint16_t dofpr_pad1;
  285. /* Reserved for future use. */
  286. uint32_t dofpr_pad2;
  287. };
  288. /* DOF supports two different encodings: MSB (big-endian) and LSB
  289. (little-endian). The encoding is itself encoded in the DOF header.
  290. The following function returns an unsigned value in the host
  291. endianness. */
  292. #define DOF_UINT(dof, field) \
  293. extract_unsigned_integer ((gdb_byte *) &(field), \
  294. sizeof ((field)), \
  295. (((dof)->dofh_ident[DTRACE_DOF_ID_ENCODING] \
  296. == DTRACE_DOF_ENCODE_MSB) \
  297. ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE))
  298. /* The following macro applies a given byte offset to a DOF (a pointer
  299. to a dtrace_dof_hdr structure) and returns the resulting
  300. address. */
  301. #define DTRACE_DOF_PTR(dof, offset) (&((char *) (dof))[(offset)])
  302. /* The following macro returns a pointer to the beginning of a given
  303. section in a DOF object. The section is referred to by its index
  304. in the sections array. */
  305. #define DTRACE_DOF_SECT(dof, idx) \
  306. ((struct dtrace_dof_sect *) \
  307. DTRACE_DOF_PTR ((dof), \
  308. DOF_UINT ((dof), (dof)->dofh_secoff) \
  309. + ((idx) * DOF_UINT ((dof), (dof)->dofh_secsize))))
  310. /* Helper function to examine the probe described by the given PROBE
  311. and PROVIDER data structures and add it to the PROBESP vector.
  312. STRTAB, OFFTAB, EOFFTAB and ARGTAB are pointers to tables in the
  313. DOF program containing the attributes for the probe. */
  314. static void
  315. dtrace_process_dof_probe (struct objfile *objfile,
  316. struct gdbarch *gdbarch,
  317. std::vector<std::unique_ptr<probe>> *probesp,
  318. struct dtrace_dof_hdr *dof,
  319. struct dtrace_dof_probe *probe,
  320. struct dtrace_dof_provider *provider,
  321. char *strtab, char *offtab, char *eofftab,
  322. char *argtab, uint64_t strtab_size)
  323. {
  324. int i, j, num_probes, num_enablers;
  325. char *p;
  326. /* Each probe section can define zero or more probes of two
  327. different types:
  328. - probe->dofpr_noffs regular probes whose program counters are
  329. stored in 32bit words starting at probe->dofpr_addr +
  330. offtab[probe->dofpr_offidx].
  331. - probe->dofpr_nenoffs is-enabled probes whose program counters
  332. are stored in 32bit words starting at probe->dofpr_addr +
  333. eofftab[probe->dofpr_enoffidx].
  334. However is-enabled probes are not probes per-se, but an
  335. optimization hack that is implemented in the kernel in a very
  336. similar way than normal probes. This is how we support
  337. is-enabled probes on GDB:
  338. - Our probes are always DTrace regular probes.
  339. - Our probes can be associated with zero or more "enablers". The
  340. list of enablers is built from the is-enabled probes defined in
  341. the Probe section.
  342. - Probes having a non-empty list of enablers can be enabled or
  343. disabled using the `enable probe' and `disable probe' commands
  344. respectively. The `Enabled' column in the output of `info
  345. probes' will read `yes' if the enablers are activated, `no'
  346. otherwise.
  347. - Probes having an empty list of enablers are always enabled.
  348. The `Enabled' column in the output of `info probes' will
  349. read `always'.
  350. It follows that if there are DTrace is-enabled probes defined for
  351. some provider/name but no DTrace regular probes defined then the
  352. GDB user wont be able to enable/disable these conditionals. */
  353. num_probes = DOF_UINT (dof, probe->dofpr_noffs);
  354. if (num_probes == 0)
  355. return;
  356. /* Build the list of enablers for the probes defined in this Probe
  357. DOF section. */
  358. std::vector<struct dtrace_probe_enabler> enablers;
  359. num_enablers = DOF_UINT (dof, probe->dofpr_nenoffs);
  360. for (i = 0; i < num_enablers; i++)
  361. {
  362. struct dtrace_probe_enabler enabler;
  363. uint32_t enabler_offset
  364. = ((uint32_t *) eofftab)[DOF_UINT (dof, probe->dofpr_enoffidx) + i];
  365. enabler.address = DOF_UINT (dof, probe->dofpr_addr)
  366. + DOF_UINT (dof, enabler_offset);
  367. enablers.push_back (enabler);
  368. }
  369. for (i = 0; i < num_probes; i++)
  370. {
  371. uint32_t probe_offset
  372. = ((uint32_t *) offtab)[DOF_UINT (dof, probe->dofpr_offidx) + i];
  373. /* Set the provider and the name of the probe. */
  374. const char *probe_provider
  375. = strtab + DOF_UINT (dof, provider->dofpv_name);
  376. const char *name = strtab + DOF_UINT (dof, probe->dofpr_name);
  377. /* The probe address. */
  378. CORE_ADDR address
  379. = DOF_UINT (dof, probe->dofpr_addr) + DOF_UINT (dof, probe_offset);
  380. /* Number of arguments in the probe. */
  381. int probe_argc = DOF_UINT (dof, probe->dofpr_nargc);
  382. /* Store argument type descriptions. A description of the type
  383. of the argument is in the (J+1)th null-terminated string
  384. starting at 'strtab' + 'probe->dofpr_nargv'. */
  385. std::vector<struct dtrace_probe_arg> args;
  386. p = strtab + DOF_UINT (dof, probe->dofpr_nargv);
  387. for (j = 0; j < probe_argc; j++)
  388. {
  389. expression_up expr;
  390. /* Set arg.expr to ensure all fields in expr are initialized and
  391. the compiler will not warn when arg is used. */
  392. std::string type_str (p);
  393. /* Use strtab_size as a sentinel. */
  394. while (*p++ != '\0' && p - strtab < strtab_size)
  395. ;
  396. /* Try to parse a type expression from the type string. If
  397. this does not work then we set the type to `long
  398. int'. */
  399. struct type *type = builtin_type (gdbarch)->builtin_long;
  400. try
  401. {
  402. expr = parse_expression_with_language (type_str.c_str (),
  403. language_c);
  404. }
  405. catch (const gdb_exception_error &ex)
  406. {
  407. }
  408. if (expr != NULL && expr->first_opcode () == OP_TYPE)
  409. type = value_type (evaluate_type (expr.get ()));
  410. args.emplace_back (type, std::move (type_str), std::move (expr));
  411. }
  412. std::vector<struct dtrace_probe_enabler> enablers_copy = enablers;
  413. dtrace_probe *ret = new dtrace_probe (std::string (name),
  414. std::string (probe_provider),
  415. address, gdbarch,
  416. std::move (args),
  417. std::move (enablers_copy));
  418. /* Successfully created probe. */
  419. probesp->emplace_back (ret);
  420. }
  421. }
  422. /* Helper function to collect the probes described in the DOF program
  423. whose header is pointed by DOF and add them to the PROBESP vector.
  424. SECT is the ELF section containing the DOF program and OBJFILE is
  425. its containing object file. */
  426. static void
  427. dtrace_process_dof (asection *sect, struct objfile *objfile,
  428. std::vector<std::unique_ptr<probe>> *probesp,
  429. struct dtrace_dof_hdr *dof)
  430. {
  431. struct gdbarch *gdbarch = objfile->arch ();
  432. struct dtrace_dof_sect *section;
  433. int i;
  434. /* The first step is to check for the DOF magic number. If no valid
  435. DOF data is found in the section then a complaint is issued to
  436. the user and the section skipped. */
  437. if (dof->dofh_ident[DTRACE_DOF_ID_MAG0] != 0x7F
  438. || dof->dofh_ident[DTRACE_DOF_ID_MAG1] != 'D'
  439. || dof->dofh_ident[DTRACE_DOF_ID_MAG2] != 'O'
  440. || dof->dofh_ident[DTRACE_DOF_ID_MAG3] != 'F')
  441. goto invalid_dof_data;
  442. /* Make sure the encoding mark is either DTRACE_DOF_ENCODE_LSB or
  443. DTRACE_DOF_ENCODE_MSB. */
  444. if (dof->dofh_ident[DTRACE_DOF_ID_ENCODING] != DTRACE_DOF_ENCODE_LSB
  445. && dof->dofh_ident[DTRACE_DOF_ID_ENCODING] != DTRACE_DOF_ENCODE_MSB)
  446. goto invalid_dof_data;
  447. /* Make sure this DOF is not an enabling DOF, i.e. there are no ECB
  448. Description sections. */
  449. section = (struct dtrace_dof_sect *) DTRACE_DOF_PTR (dof,
  450. DOF_UINT (dof, dof->dofh_secoff));
  451. for (i = 0; i < DOF_UINT (dof, dof->dofh_secnum); i++, section++)
  452. if (section->dofs_type == DTRACE_DOF_SECT_TYPE_ECBDESC)
  453. return;
  454. /* Iterate over any section of type Provider and extract the probe
  455. information from them. If there are no "provider" sections on
  456. the DOF then we just return. */
  457. section = (struct dtrace_dof_sect *) DTRACE_DOF_PTR (dof,
  458. DOF_UINT (dof, dof->dofh_secoff));
  459. for (i = 0; i < DOF_UINT (dof, dof->dofh_secnum); i++, section++)
  460. if (DOF_UINT (dof, section->dofs_type) == DTRACE_DOF_SECT_TYPE_PROVIDER)
  461. {
  462. struct dtrace_dof_provider *provider = (struct dtrace_dof_provider *)
  463. DTRACE_DOF_PTR (dof, DOF_UINT (dof, section->dofs_offset));
  464. struct dtrace_dof_sect *strtab_s
  465. = DTRACE_DOF_SECT (dof, DOF_UINT (dof, provider->dofpv_strtab));
  466. struct dtrace_dof_sect *probes_s
  467. = DTRACE_DOF_SECT (dof, DOF_UINT (dof, provider->dofpv_probes));
  468. struct dtrace_dof_sect *args_s
  469. = DTRACE_DOF_SECT (dof, DOF_UINT (dof, provider->dofpv_prargs));
  470. struct dtrace_dof_sect *offsets_s
  471. = DTRACE_DOF_SECT (dof, DOF_UINT (dof, provider->dofpv_proffs));
  472. struct dtrace_dof_sect *eoffsets_s
  473. = DTRACE_DOF_SECT (dof, DOF_UINT (dof, provider->dofpv_prenoffs));
  474. char *strtab = DTRACE_DOF_PTR (dof, DOF_UINT (dof, strtab_s->dofs_offset));
  475. char *offtab = DTRACE_DOF_PTR (dof, DOF_UINT (dof, offsets_s->dofs_offset));
  476. char *eofftab = DTRACE_DOF_PTR (dof, DOF_UINT (dof, eoffsets_s->dofs_offset));
  477. char *argtab = DTRACE_DOF_PTR (dof, DOF_UINT (dof, args_s->dofs_offset));
  478. unsigned int entsize = DOF_UINT (dof, probes_s->dofs_entsize);
  479. int num_probes;
  480. if (DOF_UINT (dof, section->dofs_size)
  481. < sizeof (struct dtrace_dof_provider))
  482. {
  483. /* The section is smaller than expected, so do not use it.
  484. This has been observed on x86-solaris 10. */
  485. goto invalid_dof_data;
  486. }
  487. /* Very, unlikely, but could crash gdb if not handled
  488. properly. */
  489. if (entsize == 0)
  490. goto invalid_dof_data;
  491. num_probes = DOF_UINT (dof, probes_s->dofs_size) / entsize;
  492. for (i = 0; i < num_probes; i++)
  493. {
  494. struct dtrace_dof_probe *probe = (struct dtrace_dof_probe *)
  495. DTRACE_DOF_PTR (dof, DOF_UINT (dof, probes_s->dofs_offset)
  496. + (i * DOF_UINT (dof, probes_s->dofs_entsize)));
  497. dtrace_process_dof_probe (objfile,
  498. gdbarch, probesp,
  499. dof, probe,
  500. provider, strtab, offtab, eofftab, argtab,
  501. DOF_UINT (dof, strtab_s->dofs_size));
  502. }
  503. }
  504. return;
  505. invalid_dof_data:
  506. complaint (_("skipping section '%s' which does not contain valid DOF data."),
  507. sect->name);
  508. }
  509. /* Implementation of 'build_arg_exprs' method. */
  510. void
  511. dtrace_probe::build_arg_exprs (struct gdbarch *gdbarch)
  512. {
  513. size_t argc = 0;
  514. m_args_expr_built = true;
  515. /* Iterate over the arguments in the probe and build the
  516. corresponding GDB internal expression that will generate the
  517. value of the argument when executed at the PC of the probe. */
  518. for (dtrace_probe_arg &arg : m_args)
  519. {
  520. /* Initialize the expression builder. The language does not
  521. matter, since we are using our own parser. */
  522. expr_builder builder (current_language, gdbarch);
  523. /* The argument value, which is ABI dependent and casted to
  524. `long int'. */
  525. expr::operation_up op = gdbarch_dtrace_parse_probe_argument (gdbarch,
  526. argc);
  527. /* Casting to the expected type, but only if the type was
  528. recognized at probe load time. Otherwise the argument will
  529. be evaluated as the long integer passed to the probe. */
  530. if (arg.type != NULL)
  531. op = expr::make_operation<expr::unop_cast_operation> (std::move (op),
  532. arg.type);
  533. builder.set_operation (std::move (op));
  534. arg.expr = builder.release ();
  535. ++argc;
  536. }
  537. }
  538. /* Implementation of 'get_arg_by_number' method. */
  539. struct dtrace_probe_arg *
  540. dtrace_probe::get_arg_by_number (unsigned n, struct gdbarch *gdbarch)
  541. {
  542. if (!m_args_expr_built)
  543. this->build_arg_exprs (gdbarch);
  544. if (n > m_args.size ())
  545. internal_error (__FILE__, __LINE__,
  546. _("Probe '%s' has %d arguments, but GDB is requesting\n"
  547. "argument %u. This should not happen. Please\n"
  548. "report this bug."),
  549. this->get_name ().c_str (),
  550. (int) m_args.size (), n);
  551. return &m_args[n];
  552. }
  553. /* Implementation of the probe is_enabled method. */
  554. bool
  555. dtrace_probe::is_enabled () const
  556. {
  557. struct gdbarch *gdbarch = this->get_gdbarch ();
  558. for (const dtrace_probe_enabler &enabler : m_enablers)
  559. if (!gdbarch_dtrace_probe_is_enabled (gdbarch, enabler.address))
  560. return false;
  561. return true;
  562. }
  563. /* Implementation of the get_probe_address method. */
  564. CORE_ADDR
  565. dtrace_probe::get_relocated_address (struct objfile *objfile)
  566. {
  567. return this->get_address () + objfile->text_section_offset ();
  568. }
  569. /* Implementation of the get_argument_count method. */
  570. unsigned
  571. dtrace_probe::get_argument_count (struct gdbarch *gdbarch)
  572. {
  573. return m_args.size ();
  574. }
  575. /* Implementation of the can_evaluate_arguments method. */
  576. bool
  577. dtrace_probe::can_evaluate_arguments () const
  578. {
  579. struct gdbarch *gdbarch = this->get_gdbarch ();
  580. return gdbarch_dtrace_parse_probe_argument_p (gdbarch);
  581. }
  582. /* Implementation of the evaluate_argument method. */
  583. struct value *
  584. dtrace_probe::evaluate_argument (unsigned n,
  585. struct frame_info *frame)
  586. {
  587. struct gdbarch *gdbarch = this->get_gdbarch ();
  588. struct dtrace_probe_arg *arg;
  589. arg = this->get_arg_by_number (n, gdbarch);
  590. return evaluate_expression (arg->expr.get (), arg->type);
  591. }
  592. /* Implementation of the compile_to_ax method. */
  593. void
  594. dtrace_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value,
  595. unsigned n)
  596. {
  597. struct dtrace_probe_arg *arg;
  598. arg = this->get_arg_by_number (n, expr->gdbarch);
  599. arg->expr->op->generate_ax (arg->expr.get (), expr, value);
  600. require_rvalue (expr, value);
  601. value->type = arg->type;
  602. }
  603. /* Implementation of the 'get_static_ops' method. */
  604. const static_probe_ops *
  605. dtrace_probe::get_static_ops () const
  606. {
  607. return &dtrace_static_probe_ops;
  608. }
  609. /* Implementation of the gen_info_probes_table_values method. */
  610. std::vector<const char *>
  611. dtrace_probe::gen_info_probes_table_values () const
  612. {
  613. const char *val = NULL;
  614. if (m_enablers.empty ())
  615. val = "always";
  616. else if (!gdbarch_dtrace_probe_is_enabled_p (this->get_gdbarch ()))
  617. val = "unknown";
  618. else if (this->is_enabled ())
  619. val = "yes";
  620. else
  621. val = "no";
  622. return std::vector<const char *> { val };
  623. }
  624. /* Implementation of the enable method. */
  625. void
  626. dtrace_probe::enable ()
  627. {
  628. struct gdbarch *gdbarch = this->get_gdbarch ();
  629. /* Enabling a dtrace probe implies patching the text section of the
  630. running process, so make sure the inferior is indeed running. */
  631. if (inferior_ptid == null_ptid)
  632. error (_("No inferior running"));
  633. /* Fast path. */
  634. if (this->is_enabled ())
  635. return;
  636. /* Iterate over all defined enabler in the given probe and enable
  637. them all using the corresponding gdbarch hook. */
  638. for (const dtrace_probe_enabler &enabler : m_enablers)
  639. if (gdbarch_dtrace_enable_probe_p (gdbarch))
  640. gdbarch_dtrace_enable_probe (gdbarch, enabler.address);
  641. }
  642. /* Implementation of the disable_probe method. */
  643. void
  644. dtrace_probe::disable ()
  645. {
  646. struct gdbarch *gdbarch = this->get_gdbarch ();
  647. /* Disabling a dtrace probe implies patching the text section of the
  648. running process, so make sure the inferior is indeed running. */
  649. if (inferior_ptid == null_ptid)
  650. error (_("No inferior running"));
  651. /* Fast path. */
  652. if (!this->is_enabled ())
  653. return;
  654. /* Are we trying to disable a probe that does not have any enabler
  655. associated? */
  656. if (m_enablers.empty ())
  657. error (_("Probe %s:%s cannot be disabled: no enablers."),
  658. this->get_provider ().c_str (), this->get_name ().c_str ());
  659. /* Iterate over all defined enabler in the given probe and disable
  660. them all using the corresponding gdbarch hook. */
  661. for (dtrace_probe_enabler &enabler : m_enablers)
  662. if (gdbarch_dtrace_disable_probe_p (gdbarch))
  663. gdbarch_dtrace_disable_probe (gdbarch, enabler.address);
  664. }
  665. /* Implementation of the is_linespec method. */
  666. bool
  667. dtrace_static_probe_ops::is_linespec (const char **linespecp) const
  668. {
  669. static const char *const keywords[] = { "-pdtrace", "-probe-dtrace", NULL };
  670. return probe_is_linespec_by_keyword (linespecp, keywords);
  671. }
  672. /* Implementation of the get_probes method. */
  673. void
  674. dtrace_static_probe_ops::get_probes
  675. (std::vector<std::unique_ptr<probe>> *probesp,
  676. struct objfile *objfile) const
  677. {
  678. bfd *abfd = objfile->obfd;
  679. asection *sect = NULL;
  680. /* Do nothing in case this is a .debug file, instead of the objfile
  681. itself. */
  682. if (objfile->separate_debug_objfile_backlink != NULL)
  683. return;
  684. /* Iterate over the sections in OBJFILE looking for DTrace
  685. information. */
  686. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  687. {
  688. if (elf_section_data (sect)->this_hdr.sh_type == SHT_SUNW_dof)
  689. {
  690. bfd_byte *dof;
  691. /* Read the contents of the DOF section and then process it to
  692. extract the information of any probe defined into it. */
  693. if (bfd_malloc_and_get_section (abfd, sect, &dof) && dof != NULL)
  694. dtrace_process_dof (sect, objfile, probesp,
  695. (struct dtrace_dof_hdr *) dof);
  696. else
  697. complaint (_("could not obtain the contents of"
  698. "section '%s' in objfile `%s'."),
  699. bfd_section_name (sect), bfd_get_filename (abfd));
  700. xfree (dof);
  701. }
  702. }
  703. }
  704. /* Implementation of the type_name method. */
  705. const char *
  706. dtrace_static_probe_ops::type_name () const
  707. {
  708. return "dtrace";
  709. }
  710. /* Implementation of the gen_info_probes_table_header method. */
  711. std::vector<struct info_probe_column>
  712. dtrace_static_probe_ops::gen_info_probes_table_header () const
  713. {
  714. struct info_probe_column dtrace_probe_column;
  715. dtrace_probe_column.field_name = "enabled";
  716. dtrace_probe_column.print_name = _("Enabled");
  717. return std::vector<struct info_probe_column> { dtrace_probe_column };
  718. }
  719. /* Implementation of the `info probes dtrace' command. */
  720. static void
  721. info_probes_dtrace_command (const char *arg, int from_tty)
  722. {
  723. info_probes_for_spops (arg, from_tty, &dtrace_static_probe_ops);
  724. }
  725. void _initialize_dtrace_probe ();
  726. void
  727. _initialize_dtrace_probe ()
  728. {
  729. all_static_probe_ops.push_back (&dtrace_static_probe_ops);
  730. add_cmd ("dtrace", class_info, info_probes_dtrace_command,
  731. _("\
  732. Show information about DTrace static probes.\n\
  733. Usage: info probes dtrace [PROVIDER [NAME [OBJECT]]]\n\
  734. Each argument is a regular expression, used to select probes.\n\
  735. PROVIDER matches probe provider names.\n\
  736. NAME matches the probe names.\n\
  737. OBJECT matches the executable or shared library name."),
  738. info_probes_cmdlist_get ());
  739. }