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