Add a comment for the ARM_F{0..7}_REGNUM registers
[binutils-gdb.git] / gdb / stap-probe.c
blob1a5ba5a1b3e090575ff1d45ea94dbe19e9728b2e
1 /* SystemTap probe support for GDB.
3 Copyright (C) 2012-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "stap-probe.h"
22 #include "probe.h"
23 #include "ui-out.h"
24 #include "objfiles.h"
25 #include "arch-utils.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "filenames.h"
29 #include "value.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "complaints.h"
33 #include "cli/cli-utils.h"
34 #include "linespec.h"
35 #include "user-regs.h"
36 #include "parser-defs.h"
37 #include "language.h"
38 #include "elf-bfd.h"
40 #include <ctype.h>
42 /* The name of the SystemTap section where we will find information about
43 the probes. */
45 #define STAP_BASE_SECTION_NAME ".stapsdt.base"
47 /* Should we display debug information for the probe's argument expression
48 parsing? */
50 static unsigned int stap_expression_debug = 0;
52 /* The various possibilities of bitness defined for a probe's argument.
54 The relationship is:
56 - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness.
57 - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'.
58 - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'.
59 - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'.
60 - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'.
61 - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'.
62 - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'.
63 - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'.
64 - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */
66 enum stap_arg_bitness
68 STAP_ARG_BITNESS_UNDEFINED,
69 STAP_ARG_BITNESS_8BIT_UNSIGNED,
70 STAP_ARG_BITNESS_8BIT_SIGNED,
71 STAP_ARG_BITNESS_16BIT_UNSIGNED,
72 STAP_ARG_BITNESS_16BIT_SIGNED,
73 STAP_ARG_BITNESS_32BIT_UNSIGNED,
74 STAP_ARG_BITNESS_32BIT_SIGNED,
75 STAP_ARG_BITNESS_64BIT_UNSIGNED,
76 STAP_ARG_BITNESS_64BIT_SIGNED,
79 /* The following structure represents a single argument for the probe. */
81 struct stap_probe_arg
83 /* Constructor for stap_probe_arg. */
84 stap_probe_arg (enum stap_arg_bitness bitness_, struct type *atype_,
85 expression_up &&aexpr_)
86 : bitness (bitness_), atype (atype_), aexpr (std::move (aexpr_))
89 /* The bitness of this argument. */
90 enum stap_arg_bitness bitness;
92 /* The corresponding `struct type *' to the bitness. */
93 struct type *atype;
95 /* The argument converted to an internal GDB expression. */
96 expression_up aexpr;
99 /* Class that implements the static probe methods for "stap" probes. */
101 class stap_static_probe_ops : public static_probe_ops
103 public:
104 /* We need a user-provided constructor to placate some compilers.
105 See PR build/24937. */
106 stap_static_probe_ops ()
110 /* See probe.h. */
111 bool is_linespec (const char **linespecp) const override;
113 /* See probe.h. */
114 void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
115 struct objfile *objfile) const override;
117 /* See probe.h. */
118 const char *type_name () const override;
120 /* See probe.h. */
121 std::vector<struct info_probe_column> gen_info_probes_table_header
122 () const override;
125 /* SystemTap static_probe_ops. */
127 const stap_static_probe_ops stap_static_probe_ops {};
129 class stap_probe : public probe
131 public:
132 /* Constructor for stap_probe. */
133 stap_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_,
134 struct gdbarch *arch_, CORE_ADDR sem_addr, const char *args_text)
135 : probe (std::move (name_), std::move (provider_), address_, arch_),
136 m_sem_addr (sem_addr),
137 m_have_parsed_args (false), m_unparsed_args_text (args_text)
140 /* See probe.h. */
141 CORE_ADDR get_relocated_address (struct objfile *objfile) override;
143 /* See probe.h. */
144 unsigned get_argument_count (struct gdbarch *gdbarch) override;
146 /* See probe.h. */
147 bool can_evaluate_arguments () const override;
149 /* See probe.h. */
150 struct value *evaluate_argument (unsigned n,
151 struct frame_info *frame) override;
153 /* See probe.h. */
154 void compile_to_ax (struct agent_expr *aexpr,
155 struct axs_value *axs_value,
156 unsigned n) override;
158 /* See probe.h. */
159 void set_semaphore (struct objfile *objfile,
160 struct gdbarch *gdbarch) override;
162 /* See probe.h. */
163 void clear_semaphore (struct objfile *objfile,
164 struct gdbarch *gdbarch) override;
166 /* See probe.h. */
167 const static_probe_ops *get_static_ops () const override;
169 /* See probe.h. */
170 std::vector<const char *> gen_info_probes_table_values () const override;
172 /* Return argument N of probe.
174 If the probe's arguments have not been parsed yet, parse them. If
175 there are no arguments, throw an exception (error). Otherwise,
176 return the requested argument. */
177 struct stap_probe_arg *get_arg_by_number (unsigned n,
178 struct gdbarch *gdbarch)
180 if (!m_have_parsed_args)
181 this->parse_arguments (gdbarch);
183 gdb_assert (m_have_parsed_args);
184 if (m_parsed_args.empty ())
185 internal_error (__FILE__, __LINE__,
186 _("Probe '%s' apparently does not have arguments, but \n"
187 "GDB is requesting its argument number %u anyway. "
188 "This should not happen. Please report this bug."),
189 this->get_name ().c_str (), n);
191 if (n > m_parsed_args.size ())
192 internal_error (__FILE__, __LINE__,
193 _("Probe '%s' has %d arguments, but GDB is requesting\n"
194 "argument %u. This should not happen. Please\n"
195 "report this bug."),
196 this->get_name ().c_str (),
197 (int) m_parsed_args.size (), n);
199 return &m_parsed_args[n];
202 /* Function which parses an argument string from the probe,
203 correctly splitting the arguments and storing their information
204 in properly ways.
206 Consider the following argument string (x86 syntax):
208 `4@%eax 4@$10'
210 We have two arguments, `%eax' and `$10', both with 32-bit
211 unsigned bitness. This function basically handles them, properly
212 filling some structures with this information. */
213 void parse_arguments (struct gdbarch *gdbarch);
215 private:
216 /* If the probe has a semaphore associated, then this is the value of
217 it, relative to SECT_OFF_DATA. */
218 CORE_ADDR m_sem_addr;
220 /* True if the arguments have been parsed. */
221 bool m_have_parsed_args;
223 /* The text version of the probe's arguments, unparsed. */
224 const char *m_unparsed_args_text;
226 /* Information about each argument. This is an array of `stap_probe_arg',
227 with each entry representing one argument. This is only valid if
228 M_ARGS_PARSED is true. */
229 std::vector<struct stap_probe_arg> m_parsed_args;
232 /* When parsing the arguments, we have to establish different precedences
233 for the various kinds of asm operators. This enumeration represents those
234 precedences.
236 This logic behind this is available at
237 <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using
238 the command "info '(as)Infix Ops'". */
240 enum stap_operand_prec
242 /* Lowest precedence, used for non-recognized operands or for the beginning
243 of the parsing process. */
244 STAP_OPERAND_PREC_NONE = 0,
246 /* Precedence of logical OR. */
247 STAP_OPERAND_PREC_LOGICAL_OR,
249 /* Precedence of logical AND. */
250 STAP_OPERAND_PREC_LOGICAL_AND,
252 /* Precedence of additive (plus, minus) and comparative (equal, less,
253 greater-than, etc) operands. */
254 STAP_OPERAND_PREC_ADD_CMP,
256 /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
257 logical NOT). */
258 STAP_OPERAND_PREC_BITWISE,
260 /* Precedence of multiplicative operands (multiplication, division,
261 remainder, left shift and right shift). */
262 STAP_OPERAND_PREC_MUL
265 static void stap_parse_argument_1 (struct stap_parse_info *p, bool has_lhs,
266 enum stap_operand_prec prec);
268 static void stap_parse_argument_conditionally (struct stap_parse_info *p);
270 /* Returns true if *S is an operator, false otherwise. */
272 static bool stap_is_operator (const char *op);
274 static void
275 show_stapexpressiondebug (struct ui_file *file, int from_tty,
276 struct cmd_list_element *c, const char *value)
278 fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"),
279 value);
282 /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
283 if the operator code was not recognized. */
285 static enum stap_operand_prec
286 stap_get_operator_prec (enum exp_opcode op)
288 switch (op)
290 case BINOP_LOGICAL_OR:
291 return STAP_OPERAND_PREC_LOGICAL_OR;
293 case BINOP_LOGICAL_AND:
294 return STAP_OPERAND_PREC_LOGICAL_AND;
296 case BINOP_ADD:
297 case BINOP_SUB:
298 case BINOP_EQUAL:
299 case BINOP_NOTEQUAL:
300 case BINOP_LESS:
301 case BINOP_LEQ:
302 case BINOP_GTR:
303 case BINOP_GEQ:
304 return STAP_OPERAND_PREC_ADD_CMP;
306 case BINOP_BITWISE_IOR:
307 case BINOP_BITWISE_AND:
308 case BINOP_BITWISE_XOR:
309 case UNOP_LOGICAL_NOT:
310 return STAP_OPERAND_PREC_BITWISE;
312 case BINOP_MUL:
313 case BINOP_DIV:
314 case BINOP_REM:
315 case BINOP_LSH:
316 case BINOP_RSH:
317 return STAP_OPERAND_PREC_MUL;
319 default:
320 return STAP_OPERAND_PREC_NONE;
324 /* Given S, read the operator in it. Return the EXP_OPCODE which
325 represents the operator detected, or throw an error if no operator
326 was found. */
328 static enum exp_opcode
329 stap_get_opcode (const char **s)
331 const char c = **s;
332 enum exp_opcode op;
334 *s += 1;
336 switch (c)
338 case '*':
339 op = BINOP_MUL;
340 break;
342 case '/':
343 op = BINOP_DIV;
344 break;
346 case '%':
347 op = BINOP_REM;
348 break;
350 case '<':
351 op = BINOP_LESS;
352 if (**s == '<')
354 *s += 1;
355 op = BINOP_LSH;
357 else if (**s == '=')
359 *s += 1;
360 op = BINOP_LEQ;
362 else if (**s == '>')
364 *s += 1;
365 op = BINOP_NOTEQUAL;
367 break;
369 case '>':
370 op = BINOP_GTR;
371 if (**s == '>')
373 *s += 1;
374 op = BINOP_RSH;
376 else if (**s == '=')
378 *s += 1;
379 op = BINOP_GEQ;
381 break;
383 case '|':
384 op = BINOP_BITWISE_IOR;
385 if (**s == '|')
387 *s += 1;
388 op = BINOP_LOGICAL_OR;
390 break;
392 case '&':
393 op = BINOP_BITWISE_AND;
394 if (**s == '&')
396 *s += 1;
397 op = BINOP_LOGICAL_AND;
399 break;
401 case '^':
402 op = BINOP_BITWISE_XOR;
403 break;
405 case '!':
406 op = UNOP_LOGICAL_NOT;
407 break;
409 case '+':
410 op = BINOP_ADD;
411 break;
413 case '-':
414 op = BINOP_SUB;
415 break;
417 case '=':
418 gdb_assert (**s == '=');
419 op = BINOP_EQUAL;
420 break;
422 default:
423 error (_("Invalid opcode in expression `%s' for SystemTap"
424 "probe"), *s);
427 return op;
430 /* Given the bitness of the argument, represented by B, return the
431 corresponding `struct type *', or throw an error if B is
432 unknown. */
434 static struct type *
435 stap_get_expected_argument_type (struct gdbarch *gdbarch,
436 enum stap_arg_bitness b,
437 const char *probe_name)
439 switch (b)
441 case STAP_ARG_BITNESS_UNDEFINED:
442 if (gdbarch_addr_bit (gdbarch) == 32)
443 return builtin_type (gdbarch)->builtin_uint32;
444 else
445 return builtin_type (gdbarch)->builtin_uint64;
447 case STAP_ARG_BITNESS_8BIT_UNSIGNED:
448 return builtin_type (gdbarch)->builtin_uint8;
450 case STAP_ARG_BITNESS_8BIT_SIGNED:
451 return builtin_type (gdbarch)->builtin_int8;
453 case STAP_ARG_BITNESS_16BIT_UNSIGNED:
454 return builtin_type (gdbarch)->builtin_uint16;
456 case STAP_ARG_BITNESS_16BIT_SIGNED:
457 return builtin_type (gdbarch)->builtin_int16;
459 case STAP_ARG_BITNESS_32BIT_SIGNED:
460 return builtin_type (gdbarch)->builtin_int32;
462 case STAP_ARG_BITNESS_32BIT_UNSIGNED:
463 return builtin_type (gdbarch)->builtin_uint32;
465 case STAP_ARG_BITNESS_64BIT_SIGNED:
466 return builtin_type (gdbarch)->builtin_int64;
468 case STAP_ARG_BITNESS_64BIT_UNSIGNED:
469 return builtin_type (gdbarch)->builtin_uint64;
471 default:
472 error (_("Undefined bitness for probe '%s'."), probe_name);
473 break;
477 /* Helper function to check for a generic list of prefixes. GDBARCH
478 is the current gdbarch being used. S is the expression being
479 analyzed. If R is not NULL, it will be used to return the found
480 prefix. PREFIXES is the list of expected prefixes.
482 This function does a case-insensitive match.
484 Return true if any prefix has been found, false otherwise. */
486 static bool
487 stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s,
488 const char **r, const char *const *prefixes)
490 const char *const *p;
492 if (prefixes == NULL)
494 if (r != NULL)
495 *r = "";
497 return true;
500 for (p = prefixes; *p != NULL; ++p)
501 if (strncasecmp (s, *p, strlen (*p)) == 0)
503 if (r != NULL)
504 *r = *p;
506 return true;
509 return false;
512 /* Return true if S points to a register prefix, false otherwise. For
513 a description of the arguments, look at stap_is_generic_prefix. */
515 static bool
516 stap_is_register_prefix (struct gdbarch *gdbarch, const char *s,
517 const char **r)
519 const char *const *t = gdbarch_stap_register_prefixes (gdbarch);
521 return stap_is_generic_prefix (gdbarch, s, r, t);
524 /* Return true if S points to a register indirection prefix, false
525 otherwise. For a description of the arguments, look at
526 stap_is_generic_prefix. */
528 static bool
529 stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s,
530 const char **r)
532 const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch);
534 return stap_is_generic_prefix (gdbarch, s, r, t);
537 /* Return true if S points to an integer prefix, false otherwise. For
538 a description of the arguments, look at stap_is_generic_prefix.
540 This function takes care of analyzing whether we are dealing with
541 an expected integer prefix, or, if there is no integer prefix to be
542 expected, whether we are dealing with a digit. It does a
543 case-insensitive match. */
545 static bool
546 stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s,
547 const char **r)
549 const char *const *t = gdbarch_stap_integer_prefixes (gdbarch);
550 const char *const *p;
552 if (t == NULL)
554 /* A NULL value here means that integers do not have a prefix.
555 We just check for a digit then. */
556 if (r != NULL)
557 *r = "";
559 return isdigit (*s) > 0;
562 for (p = t; *p != NULL; ++p)
564 size_t len = strlen (*p);
566 if ((len == 0 && isdigit (*s))
567 || (len > 0 && strncasecmp (s, *p, len) == 0))
569 /* Integers may or may not have a prefix. The "len == 0"
570 check covers the case when integers do not have a prefix
571 (therefore, we just check if we have a digit). The call
572 to "strncasecmp" covers the case when they have a
573 prefix. */
574 if (r != NULL)
575 *r = *p;
577 return true;
581 return false;
584 /* Helper function to check for a generic list of suffixes. If we are
585 not expecting any suffixes, then it just returns 1. If we are
586 expecting at least one suffix, then it returns true if a suffix has
587 been found, false otherwise. GDBARCH is the current gdbarch being
588 used. S is the expression being analyzed. If R is not NULL, it
589 will be used to return the found suffix. SUFFIXES is the list of
590 expected suffixes. This function does a case-insensitive
591 match. */
593 static bool
594 stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s,
595 const char **r, const char *const *suffixes)
597 const char *const *p;
598 bool found = false;
600 if (suffixes == NULL)
602 if (r != NULL)
603 *r = "";
605 return true;
608 for (p = suffixes; *p != NULL; ++p)
609 if (strncasecmp (s, *p, strlen (*p)) == 0)
611 if (r != NULL)
612 *r = *p;
614 found = true;
615 break;
618 return found;
621 /* Return true if S points to an integer suffix, false otherwise. For
622 a description of the arguments, look at
623 stap_generic_check_suffix. */
625 static bool
626 stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s,
627 const char **r)
629 const char *const *p = gdbarch_stap_integer_suffixes (gdbarch);
631 return stap_generic_check_suffix (gdbarch, s, r, p);
634 /* Return true if S points to a register suffix, false otherwise. For
635 a description of the arguments, look at
636 stap_generic_check_suffix. */
638 static bool
639 stap_check_register_suffix (struct gdbarch *gdbarch, const char *s,
640 const char **r)
642 const char *const *p = gdbarch_stap_register_suffixes (gdbarch);
644 return stap_generic_check_suffix (gdbarch, s, r, p);
647 /* Return true if S points to a register indirection suffix, false
648 otherwise. For a description of the arguments, look at
649 stap_generic_check_suffix. */
651 static bool
652 stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s,
653 const char **r)
655 const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch);
657 return stap_generic_check_suffix (gdbarch, s, r, p);
660 /* Function responsible for parsing a register operand according to
661 SystemTap parlance. Assuming:
663 RP = register prefix
664 RS = register suffix
665 RIP = register indirection prefix
666 RIS = register indirection suffix
668 Then a register operand can be:
670 [RIP] [RP] REGISTER [RS] [RIS]
672 This function takes care of a register's indirection, displacement and
673 direct access. It also takes into consideration the fact that some
674 registers are named differently inside and outside GDB, e.g., PPC's
675 general-purpose registers are represented by integers in the assembly
676 language (e.g., `15' is the 15th general-purpose register), but inside
677 GDB they have a prefix (the letter `r') appended. */
679 static void
680 stap_parse_register_operand (struct stap_parse_info *p)
682 /* Simple flag to indicate whether we have seen a minus signal before
683 certain number. */
684 bool got_minus = false;
685 /* Flags to indicate whether this register access is being displaced and/or
686 indirected. */
687 bool disp_p = false;
688 bool indirect_p = false;
689 struct gdbarch *gdbarch = p->gdbarch;
690 /* Needed to generate the register name as a part of an expression. */
691 struct stoken str;
692 /* Variables used to extract the register name from the probe's
693 argument. */
694 const char *start;
695 const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
696 const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
697 const char *reg_prefix;
698 const char *reg_ind_prefix;
699 const char *reg_suffix;
700 const char *reg_ind_suffix;
702 /* Checking for a displacement argument. */
703 if (*p->arg == '+')
705 /* If it's a plus sign, we don't need to do anything, just advance the
706 pointer. */
707 ++p->arg;
709 else if (*p->arg == '-')
711 got_minus = true;
712 ++p->arg;
715 if (isdigit (*p->arg))
717 /* The value of the displacement. */
718 long displacement;
719 char *endp;
721 disp_p = true;
722 displacement = strtol (p->arg, &endp, 10);
723 p->arg = endp;
725 /* Generating the expression for the displacement. */
726 write_exp_elt_opcode (&p->pstate, OP_LONG);
727 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
728 write_exp_elt_longcst (&p->pstate, displacement);
729 write_exp_elt_opcode (&p->pstate, OP_LONG);
730 if (got_minus)
731 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
734 /* Getting rid of register indirection prefix. */
735 if (stap_is_register_indirection_prefix (gdbarch, p->arg, &reg_ind_prefix))
737 indirect_p = true;
738 p->arg += strlen (reg_ind_prefix);
741 if (disp_p && !indirect_p)
742 error (_("Invalid register displacement syntax on expression `%s'."),
743 p->saved_arg);
745 /* Getting rid of register prefix. */
746 if (stap_is_register_prefix (gdbarch, p->arg, &reg_prefix))
747 p->arg += strlen (reg_prefix);
749 /* Now we should have only the register name. Let's extract it and get
750 the associated number. */
751 start = p->arg;
753 /* We assume the register name is composed by letters and numbers. */
754 while (isalnum (*p->arg))
755 ++p->arg;
757 std::string regname (start, p->arg - start);
759 /* We only add the GDB's register prefix/suffix if we are dealing with
760 a numeric register. */
761 if (isdigit (*start))
763 if (gdb_reg_prefix != NULL)
764 regname = gdb_reg_prefix + regname;
766 if (gdb_reg_suffix != NULL)
767 regname += gdb_reg_suffix;
770 int regnum = user_reg_map_name_to_regnum (gdbarch, regname.c_str (),
771 regname.size ());
773 /* Is this a valid register name? */
774 if (regnum == -1)
775 error (_("Invalid register name `%s' on expression `%s'."),
776 regname.c_str (), p->saved_arg);
778 /* Check if there's any special treatment that the arch-specific
779 code would like to perform on the register name. */
780 if (gdbarch_stap_adjust_register_p (gdbarch))
782 std::string newregname
783 = gdbarch_stap_adjust_register (gdbarch, p, regname, regnum);
785 if (regname != newregname)
787 /* This is just a check we perform to make sure that the
788 arch-dependent code has provided us with a valid
789 register name. */
790 regnum = user_reg_map_name_to_regnum (gdbarch, newregname.c_str (),
791 newregname.size ());
793 if (regnum == -1)
794 internal_error (__FILE__, __LINE__,
795 _("Invalid register name '%s' after replacing it"
796 " (previous name was '%s')"),
797 newregname.c_str (), regname.c_str ());
799 regname = newregname;
803 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
804 str.ptr = regname.c_str ();
805 str.length = regname.size ();
806 write_exp_string (&p->pstate, str);
807 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
809 if (indirect_p)
811 if (disp_p)
812 write_exp_elt_opcode (&p->pstate, BINOP_ADD);
814 /* Casting to the expected type. */
815 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
816 write_exp_elt_type (&p->pstate, lookup_pointer_type (p->arg_type));
817 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
819 write_exp_elt_opcode (&p->pstate, UNOP_IND);
822 /* Getting rid of the register name suffix. */
823 if (stap_check_register_suffix (gdbarch, p->arg, &reg_suffix))
824 p->arg += strlen (reg_suffix);
825 else
826 error (_("Missing register name suffix on expression `%s'."),
827 p->saved_arg);
829 /* Getting rid of the register indirection suffix. */
830 if (indirect_p)
832 if (stap_check_register_indirection_suffix (gdbarch, p->arg,
833 &reg_ind_suffix))
834 p->arg += strlen (reg_ind_suffix);
835 else
836 error (_("Missing indirection suffix on expression `%s'."),
837 p->saved_arg);
841 /* This function is responsible for parsing a single operand.
843 A single operand can be:
845 - an unary operation (e.g., `-5', `~2', or even with subexpressions
846 like `-(2 + 1)')
847 - a register displacement, which will be treated as a register
848 operand (e.g., `-4(%eax)' on x86)
849 - a numeric constant, or
850 - a register operand (see function `stap_parse_register_operand')
852 The function also calls special-handling functions to deal with
853 unrecognized operands, allowing arch-specific parsers to be
854 created. */
856 static void
857 stap_parse_single_operand (struct stap_parse_info *p)
859 struct gdbarch *gdbarch = p->gdbarch;
860 const char *int_prefix = NULL;
862 /* We first try to parse this token as a "special token". */
863 if (gdbarch_stap_parse_special_token_p (gdbarch)
864 && (gdbarch_stap_parse_special_token (gdbarch, p) != 0))
866 /* If the return value of the above function is not zero,
867 it means it successfully parsed the special token.
869 If it is NULL, we try to parse it using our method. */
870 return;
873 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
875 char c = *p->arg;
876 /* We use this variable to do a lookahead. */
877 const char *tmp = p->arg;
878 bool has_digit = false;
880 /* Skipping signal. */
881 ++tmp;
883 /* This is an unary operation. Here is a list of allowed tokens
884 here:
886 - numeric literal;
887 - number (from register displacement)
888 - subexpression (beginning with `(')
890 We handle the register displacement here, and the other cases
891 recursively. */
892 if (p->inside_paren_p)
893 tmp = skip_spaces (tmp);
895 while (isdigit (*tmp))
897 /* We skip the digit here because we are only interested in
898 knowing what kind of unary operation this is. The digit
899 will be handled by one of the functions that will be
900 called below ('stap_parse_argument_conditionally' or
901 'stap_parse_register_operand'). */
902 ++tmp;
903 has_digit = true;
906 if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp,
907 NULL))
909 /* If we are here, it means it is a displacement. The only
910 operations allowed here are `-' and `+'. */
911 if (c != '-' && c != '+')
912 error (_("Invalid operator `%c' for register displacement "
913 "on expression `%s'."), c, p->saved_arg);
915 stap_parse_register_operand (p);
917 else
919 /* This is not a displacement. We skip the operator, and
920 deal with it when the recursion returns. */
921 ++p->arg;
922 stap_parse_argument_conditionally (p);
923 if (c == '-')
924 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
925 else if (c == '~')
926 write_exp_elt_opcode (&p->pstate, UNOP_COMPLEMENT);
929 else if (isdigit (*p->arg))
931 /* A temporary variable, needed for lookahead. */
932 const char *tmp = p->arg;
933 char *endp;
934 long number;
936 /* We can be dealing with a numeric constant, or with a register
937 displacement. */
938 number = strtol (tmp, &endp, 10);
939 tmp = endp;
941 if (p->inside_paren_p)
942 tmp = skip_spaces (tmp);
944 /* If "stap_is_integer_prefix" returns true, it means we can
945 accept integers without a prefix here. But we also need to
946 check whether the next token (i.e., "tmp") is not a register
947 indirection prefix. */
948 if (stap_is_integer_prefix (gdbarch, p->arg, NULL)
949 && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
951 const char *int_suffix;
953 /* We are dealing with a numeric constant. */
954 write_exp_elt_opcode (&p->pstate, OP_LONG);
955 write_exp_elt_type (&p->pstate,
956 builtin_type (gdbarch)->builtin_long);
957 write_exp_elt_longcst (&p->pstate, number);
958 write_exp_elt_opcode (&p->pstate, OP_LONG);
960 p->arg = tmp;
962 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
963 p->arg += strlen (int_suffix);
964 else
965 error (_("Invalid constant suffix on expression `%s'."),
966 p->saved_arg);
968 else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
969 stap_parse_register_operand (p);
970 else
971 error (_("Unknown numeric token on expression `%s'."),
972 p->saved_arg);
974 else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
976 /* We are dealing with a numeric constant. */
977 long number;
978 char *endp;
979 const char *int_suffix;
981 p->arg += strlen (int_prefix);
982 number = strtol (p->arg, &endp, 10);
983 p->arg = endp;
985 write_exp_elt_opcode (&p->pstate, OP_LONG);
986 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
987 write_exp_elt_longcst (&p->pstate, number);
988 write_exp_elt_opcode (&p->pstate, OP_LONG);
990 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
991 p->arg += strlen (int_suffix);
992 else
993 error (_("Invalid constant suffix on expression `%s'."),
994 p->saved_arg);
996 else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
997 || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
998 stap_parse_register_operand (p);
999 else
1000 error (_("Operator `%c' not recognized on expression `%s'."),
1001 *p->arg, p->saved_arg);
1004 /* This function parses an argument conditionally, based on single or
1005 non-single operands. A non-single operand would be a parenthesized
1006 expression (e.g., `(2 + 1)'), and a single operand is anything that
1007 starts with `-', `~', `+' (i.e., unary operators), a digit, or
1008 something recognized by `gdbarch_stap_is_single_operand'. */
1010 static void
1011 stap_parse_argument_conditionally (struct stap_parse_info *p)
1013 gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));
1015 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary. */
1016 || isdigit (*p->arg)
1017 || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
1018 stap_parse_single_operand (p);
1019 else if (*p->arg == '(')
1021 /* We are dealing with a parenthesized operand. It means we
1022 have to parse it as it was a separate expression, without
1023 left-side or precedence. */
1024 ++p->arg;
1025 p->arg = skip_spaces (p->arg);
1026 ++p->inside_paren_p;
1028 stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
1030 --p->inside_paren_p;
1031 if (*p->arg != ')')
1032 error (_("Missign close-paren on expression `%s'."),
1033 p->saved_arg);
1035 ++p->arg;
1036 if (p->inside_paren_p)
1037 p->arg = skip_spaces (p->arg);
1039 else
1040 error (_("Cannot parse expression `%s'."), p->saved_arg);
1043 /* Helper function for `stap_parse_argument'. Please, see its comments to
1044 better understand what this function does. */
1046 static void
1047 stap_parse_argument_1 (struct stap_parse_info *p, bool has_lhs,
1048 enum stap_operand_prec prec)
1050 /* This is an operator-precedence parser.
1052 We work with left- and right-sides of expressions, and
1053 parse them depending on the precedence of the operators
1054 we find. */
1056 gdb_assert (p->arg != NULL);
1058 if (p->inside_paren_p)
1059 p->arg = skip_spaces (p->arg);
1061 if (!has_lhs)
1063 /* We were called without a left-side, either because this is the
1064 first call, or because we were called to parse a parenthesized
1065 expression. It doesn't really matter; we have to parse the
1066 left-side in order to continue the process. */
1067 stap_parse_argument_conditionally (p);
1070 /* Start to parse the right-side, and to "join" left and right sides
1071 depending on the operation specified.
1073 This loop shall continue until we run out of characters in the input,
1074 or until we find a close-parenthesis, which means that we've reached
1075 the end of a sub-expression. */
1076 while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
1078 const char *tmp_exp_buf;
1079 enum exp_opcode opcode;
1080 enum stap_operand_prec cur_prec;
1082 if (!stap_is_operator (p->arg))
1083 error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
1084 p->saved_arg);
1086 /* We have to save the current value of the expression buffer because
1087 the `stap_get_opcode' modifies it in order to get the current
1088 operator. If this operator's precedence is lower than PREC, we
1089 should return and not advance the expression buffer pointer. */
1090 tmp_exp_buf = p->arg;
1091 opcode = stap_get_opcode (&tmp_exp_buf);
1093 cur_prec = stap_get_operator_prec (opcode);
1094 if (cur_prec < prec)
1096 /* If the precedence of the operator that we are seeing now is
1097 lower than the precedence of the first operator seen before
1098 this parsing process began, it means we should stop parsing
1099 and return. */
1100 break;
1103 p->arg = tmp_exp_buf;
1104 if (p->inside_paren_p)
1105 p->arg = skip_spaces (p->arg);
1107 /* Parse the right-side of the expression. */
1108 stap_parse_argument_conditionally (p);
1110 /* While we still have operators, try to parse another
1111 right-side, but using the current right-side as a left-side. */
1112 while (*p->arg != '\0' && stap_is_operator (p->arg))
1114 enum exp_opcode lookahead_opcode;
1115 enum stap_operand_prec lookahead_prec;
1117 /* Saving the current expression buffer position. The explanation
1118 is the same as above. */
1119 tmp_exp_buf = p->arg;
1120 lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
1121 lookahead_prec = stap_get_operator_prec (lookahead_opcode);
1123 if (lookahead_prec <= prec)
1125 /* If we are dealing with an operator whose precedence is lower
1126 than the first one, just abandon the attempt. */
1127 break;
1130 /* Parse the right-side of the expression, but since we already
1131 have a left-side at this point, set `has_lhs' to 1. */
1132 stap_parse_argument_1 (p, 1, lookahead_prec);
1135 write_exp_elt_opcode (&p->pstate, opcode);
1139 /* Parse a probe's argument.
1141 Assuming that:
1143 LP = literal integer prefix
1144 LS = literal integer suffix
1146 RP = register prefix
1147 RS = register suffix
1149 RIP = register indirection prefix
1150 RIS = register indirection suffix
1152 This routine assumes that arguments' tokens are of the form:
1154 - [LP] NUMBER [LS]
1155 - [RP] REGISTER [RS]
1156 - [RIP] [RP] REGISTER [RS] [RIS]
1157 - If we find a number without LP, we try to parse it as a literal integer
1158 constant (if LP == NULL), or as a register displacement.
1159 - We count parenthesis, and only skip whitespaces if we are inside them.
1160 - If we find an operator, we skip it.
1162 This function can also call a special function that will try to match
1163 unknown tokens. It will return the expression_up generated from
1164 parsing the argument. */
1166 static expression_up
1167 stap_parse_argument (const char **arg, struct type *atype,
1168 struct gdbarch *gdbarch)
1170 /* We need to initialize the expression buffer, in order to begin
1171 our parsing efforts. We use language_c here because we may need
1172 to do pointer arithmetics. */
1173 struct stap_parse_info p (*arg, atype, language_def (language_c),
1174 gdbarch);
1176 stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
1178 gdb_assert (p.inside_paren_p == 0);
1180 /* Casting the final expression to the appropriate type. */
1181 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
1182 write_exp_elt_type (&p.pstate, atype);
1183 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
1185 p.arg = skip_spaces (p.arg);
1186 *arg = p.arg;
1188 return p.pstate.release ();
1191 /* Implementation of 'parse_arguments' method. */
1193 void
1194 stap_probe::parse_arguments (struct gdbarch *gdbarch)
1196 const char *cur;
1198 gdb_assert (!m_have_parsed_args);
1199 cur = m_unparsed_args_text;
1200 m_have_parsed_args = true;
1202 if (cur == NULL || *cur == '\0' || *cur == ':')
1203 return;
1205 while (*cur != '\0')
1207 enum stap_arg_bitness bitness;
1208 bool got_minus = false;
1210 /* We expect to find something like:
1212 N@OP
1214 Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so
1215 we check it here. If we don't find it, go to the next
1216 state. */
1217 if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@')
1218 || (isdigit (cur[0]) && cur[1] == '@'))
1220 if (*cur == '-')
1222 /* Discard the `-'. */
1223 ++cur;
1224 got_minus = true;
1227 /* Defining the bitness. */
1228 switch (*cur)
1230 case '1':
1231 bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED
1232 : STAP_ARG_BITNESS_8BIT_UNSIGNED);
1233 break;
1235 case '2':
1236 bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED
1237 : STAP_ARG_BITNESS_16BIT_UNSIGNED);
1238 break;
1240 case '4':
1241 bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
1242 : STAP_ARG_BITNESS_32BIT_UNSIGNED);
1243 break;
1245 case '8':
1246 bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
1247 : STAP_ARG_BITNESS_64BIT_UNSIGNED);
1248 break;
1250 default:
1252 /* We have an error, because we don't expect anything
1253 except 1, 2, 4 and 8. */
1254 warning (_("unrecognized bitness %s%c' for probe `%s'"),
1255 got_minus ? "`-" : "`", *cur,
1256 this->get_name ().c_str ());
1257 return;
1260 /* Discard the number and the `@' sign. */
1261 cur += 2;
1263 else
1264 bitness = STAP_ARG_BITNESS_UNDEFINED;
1266 struct type *atype
1267 = stap_get_expected_argument_type (gdbarch, bitness,
1268 this->get_name ().c_str ());
1270 expression_up expr = stap_parse_argument (&cur, atype, gdbarch);
1272 if (stap_expression_debug)
1273 dump_raw_expression (expr.get (), gdb_stdlog,
1274 "before conversion to prefix form");
1276 prefixify_expression (expr.get ());
1278 if (stap_expression_debug)
1279 dump_prefix_expression (expr.get (), gdb_stdlog);
1281 m_parsed_args.emplace_back (bitness, atype, std::move (expr));
1283 /* Start it over again. */
1284 cur = skip_spaces (cur);
1288 /* Helper function to relocate an address. */
1290 static CORE_ADDR
1291 relocate_address (CORE_ADDR address, struct objfile *objfile)
1293 return address + objfile->data_section_offset ();
1296 /* Implementation of the get_relocated_address method. */
1298 CORE_ADDR
1299 stap_probe::get_relocated_address (struct objfile *objfile)
1301 return relocate_address (this->get_address (), objfile);
1304 /* Given PROBE, returns the number of arguments present in that probe's
1305 argument string. */
1307 unsigned
1308 stap_probe::get_argument_count (struct gdbarch *gdbarch)
1310 if (!m_have_parsed_args)
1312 if (this->can_evaluate_arguments ())
1313 this->parse_arguments (gdbarch);
1314 else
1316 static bool have_warned_stap_incomplete = false;
1318 if (!have_warned_stap_incomplete)
1320 warning (_(
1321 "The SystemTap SDT probe support is not fully implemented on this target;\n"
1322 "you will not be able to inspect the arguments of the probes.\n"
1323 "Please report a bug against GDB requesting a port to this target."));
1324 have_warned_stap_incomplete = true;
1327 /* Marking the arguments as "already parsed". */
1328 m_have_parsed_args = true;
1332 gdb_assert (m_have_parsed_args);
1333 return m_parsed_args.size ();
1336 /* Return true if OP is a valid operator inside a probe argument, or
1337 false otherwise. */
1339 static bool
1340 stap_is_operator (const char *op)
1342 bool ret = true;
1344 switch (*op)
1346 case '*':
1347 case '/':
1348 case '%':
1349 case '^':
1350 case '!':
1351 case '+':
1352 case '-':
1353 case '<':
1354 case '>':
1355 case '|':
1356 case '&':
1357 break;
1359 case '=':
1360 if (op[1] != '=')
1361 ret = false;
1362 break;
1364 default:
1365 /* We didn't find any operator. */
1366 ret = false;
1369 return ret;
1372 /* Implement the `can_evaluate_arguments' method. */
1374 bool
1375 stap_probe::can_evaluate_arguments () const
1377 struct gdbarch *gdbarch = this->get_gdbarch ();
1379 /* For SystemTap probes, we have to guarantee that the method
1380 stap_is_single_operand is defined on gdbarch. If it is not, then it
1381 means that argument evaluation is not implemented on this target. */
1382 return gdbarch_stap_is_single_operand_p (gdbarch);
1385 /* Evaluate the probe's argument N (indexed from 0), returning a value
1386 corresponding to it. Assertion is thrown if N does not exist. */
1388 struct value *
1389 stap_probe::evaluate_argument (unsigned n, struct frame_info *frame)
1391 struct stap_probe_arg *arg;
1392 int pos = 0;
1393 struct gdbarch *gdbarch = get_frame_arch (frame);
1395 arg = this->get_arg_by_number (n, gdbarch);
1396 return evaluate_subexp_standard (arg->atype, arg->aexpr.get (), &pos,
1397 EVAL_NORMAL);
1400 /* Compile the probe's argument N (indexed from 0) to agent expression.
1401 Assertion is thrown if N does not exist. */
1403 void
1404 stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value,
1405 unsigned n)
1407 struct stap_probe_arg *arg;
1408 union exp_element *pc;
1410 arg = this->get_arg_by_number (n, expr->gdbarch);
1412 pc = arg->aexpr->elts;
1413 gen_expr (arg->aexpr.get (), &pc, expr, value);
1415 require_rvalue (expr, value);
1416 value->type = arg->atype;
1420 /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
1421 address. SET is zero if the semaphore should be cleared, or one if
1422 it should be set. This is a helper function for
1423 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */
1425 static void
1426 stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1428 gdb_byte bytes[sizeof (LONGEST)];
1429 /* The ABI specifies "unsigned short". */
1430 struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1431 ULONGEST value;
1433 /* Swallow errors. */
1434 if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1436 warning (_("Could not read the value of a SystemTap semaphore."));
1437 return;
1440 enum bfd_endian byte_order = type_byte_order (type);
1441 value = extract_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order);
1442 /* Note that we explicitly don't worry about overflow or
1443 underflow. */
1444 if (set)
1445 ++value;
1446 else
1447 --value;
1449 store_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order, value);
1451 if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1452 warning (_("Could not write the value of a SystemTap semaphore."));
1455 /* Implementation of the 'set_semaphore' method.
1457 SystemTap semaphores act as reference counters, so calls to this
1458 function must be paired with calls to 'clear_semaphore'.
1460 This function and 'clear_semaphore' race with another tool
1461 changing the probes, but that is too rare to care. */
1463 void
1464 stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1466 if (m_sem_addr == 0)
1467 return;
1468 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
1471 /* Implementation of the 'clear_semaphore' method. */
1473 void
1474 stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1476 if (m_sem_addr == 0)
1477 return;
1478 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
1481 /* Implementation of the 'get_static_ops' method. */
1483 const static_probe_ops *
1484 stap_probe::get_static_ops () const
1486 return &stap_static_probe_ops;
1489 /* Implementation of the 'gen_info_probes_table_values' method. */
1491 std::vector<const char *>
1492 stap_probe::gen_info_probes_table_values () const
1494 const char *val = NULL;
1496 if (m_sem_addr != 0)
1497 val = print_core_address (this->get_gdbarch (), m_sem_addr);
1499 return std::vector<const char *> { val };
1502 /* Helper function that parses the information contained in a
1503 SystemTap's probe. Basically, the information consists in:
1505 - Probe's PC address;
1506 - Link-time section address of `.stapsdt.base' section;
1507 - Link-time address of the semaphore variable, or ZERO if the
1508 probe doesn't have an associated semaphore;
1509 - Probe's provider name;
1510 - Probe's name;
1511 - Probe's argument format. */
1513 static void
1514 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
1515 std::vector<std::unique_ptr<probe>> *probesp,
1516 CORE_ADDR base)
1518 bfd *abfd = objfile->obfd;
1519 int size = bfd_get_arch_size (abfd) / 8;
1520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1521 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1523 /* Provider and the name of the probe. */
1524 const char *provider = (const char *) &el->data[3 * size];
1525 const char *name = ((const char *)
1526 memchr (provider, '\0',
1527 (char *) el->data + el->size - provider));
1528 /* Making sure there is a name. */
1529 if (name == NULL)
1531 complaint (_("corrupt probe name when reading `%s'"),
1532 objfile_name (objfile));
1534 /* There is no way to use a probe without a name or a provider, so
1535 returning here makes sense. */
1536 return;
1538 else
1539 ++name;
1541 /* Retrieving the probe's address. */
1542 CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type);
1544 /* Link-time sh_addr of `.stapsdt.base' section. */
1545 CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type);
1547 /* Semaphore address. */
1548 CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
1550 address += base - base_ref;
1551 if (sem_addr != 0)
1552 sem_addr += base - base_ref;
1554 /* Arguments. We can only extract the argument format if there is a valid
1555 name for this probe. */
1556 const char *probe_args = ((const char*)
1557 memchr (name, '\0',
1558 (char *) el->data + el->size - name));
1560 if (probe_args != NULL)
1561 ++probe_args;
1563 if (probe_args == NULL
1564 || (memchr (probe_args, '\0', (char *) el->data + el->size - name)
1565 != el->data + el->size - 1))
1567 complaint (_("corrupt probe argument when reading `%s'"),
1568 objfile_name (objfile));
1569 /* If the argument string is NULL, it means some problem happened with
1570 it. So we return. */
1571 return;
1574 stap_probe *ret = new stap_probe (std::string (name), std::string (provider),
1575 address, gdbarch, sem_addr, probe_args);
1577 /* Successfully created probe. */
1578 probesp->emplace_back (ret);
1581 /* Helper function which tries to find the base address of the SystemTap
1582 base section named STAP_BASE_SECTION_NAME. */
1584 static void
1585 get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
1587 asection **ret = (asection **) obj;
1589 if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1590 && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1591 *ret = sect;
1594 /* Helper function which iterates over every section in the BFD file,
1595 trying to find the base address of the SystemTap base section.
1596 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1598 static int
1599 get_stap_base_address (bfd *obfd, bfd_vma *base)
1601 asection *ret = NULL;
1603 bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);
1605 if (ret == NULL)
1607 complaint (_("could not obtain base address for "
1608 "SystemTap section on objfile `%s'."),
1609 obfd->filename);
1610 return 0;
1613 if (base != NULL)
1614 *base = ret->vma;
1616 return 1;
1619 /* Implementation of the 'is_linespec' method. */
1621 bool
1622 stap_static_probe_ops::is_linespec (const char **linespecp) const
1624 static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1626 return probe_is_linespec_by_keyword (linespecp, keywords);
1629 /* Implementation of the 'get_probes' method. */
1631 void
1632 stap_static_probe_ops::get_probes
1633 (std::vector<std::unique_ptr<probe>> *probesp,
1634 struct objfile *objfile) const
1636 /* If we are here, then this is the first time we are parsing the
1637 SystemTap probe's information. We basically have to count how many
1638 probes the objfile has, and then fill in the necessary information
1639 for each one. */
1640 bfd *obfd = objfile->obfd;
1641 bfd_vma base;
1642 struct sdt_note *iter;
1643 unsigned save_probesp_len = probesp->size ();
1645 if (objfile->separate_debug_objfile_backlink != NULL)
1647 /* This is a .debug file, not the objfile itself. */
1648 return;
1651 if (elf_tdata (obfd)->sdt_note_head == NULL)
1653 /* There isn't any probe here. */
1654 return;
1657 if (!get_stap_base_address (obfd, &base))
1659 /* There was an error finding the base address for the section.
1660 Just return NULL. */
1661 return;
1664 /* Parsing each probe's information. */
1665 for (iter = elf_tdata (obfd)->sdt_note_head;
1666 iter != NULL;
1667 iter = iter->next)
1669 /* We first have to handle all the information about the
1670 probe which is present in the section. */
1671 handle_stap_probe (objfile, iter, probesp, base);
1674 if (save_probesp_len == probesp->size ())
1676 /* If we are here, it means we have failed to parse every known
1677 probe. */
1678 complaint (_("could not parse SystemTap probe(s) from inferior"));
1679 return;
1683 /* Implementation of the type_name method. */
1685 const char *
1686 stap_static_probe_ops::type_name () const
1688 return "stap";
1691 /* Implementation of the 'gen_info_probes_table_header' method. */
1693 std::vector<struct info_probe_column>
1694 stap_static_probe_ops::gen_info_probes_table_header () const
1696 struct info_probe_column stap_probe_column;
1698 stap_probe_column.field_name = "semaphore";
1699 stap_probe_column.print_name = _("Semaphore");
1701 return std::vector<struct info_probe_column> { stap_probe_column };
1704 /* Implementation of the `info probes stap' command. */
1706 static void
1707 info_probes_stap_command (const char *arg, int from_tty)
1709 info_probes_for_spops (arg, from_tty, &stap_static_probe_ops);
1712 void _initialize_stap_probe ();
1713 void
1714 _initialize_stap_probe ()
1716 all_static_probe_ops.push_back (&stap_static_probe_ops);
1718 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
1719 &stap_expression_debug,
1720 _("Set SystemTap expression debugging."),
1721 _("Show SystemTap expression debugging."),
1722 _("When non-zero, the internal representation "
1723 "of SystemTap expressions will be printed."),
1724 NULL,
1725 show_stapexpressiondebug,
1726 &setdebuglist, &showdebuglist);
1728 add_cmd ("stap", class_info, info_probes_stap_command,
1729 _("\
1730 Show information about SystemTap static probes.\n\
1731 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1732 Each argument is a regular expression, used to select probes.\n\
1733 PROVIDER matches probe provider names.\n\
1734 NAME matches the probe names.\n\
1735 OBJECT matches the executable or shared library name."),
1736 info_probes_cmdlist_get ());