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