1 /* SystemTap probe support for GDB.
3 Copyright (C) 2012-2019 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/>. */
21 #include "stap-probe.h"
23 #include "gdbsupport/vec.h"
26 #include "arch-utils.h"
29 #include "filenames.h"
33 #include "complaints.h"
34 #include "cli/cli-utils.h"
36 #include "user-regs.h"
37 #include "parser-defs.h"
43 /* The name of the SystemTap section where we will find information about
46 #define STAP_BASE_SECTION_NAME ".stapsdt.base"
48 /* Should we display debug information for the probe's argument expression
51 static unsigned int stap_expression_debug
= 0;
53 /* The various possibilities of bitness defined for a probe's argument.
57 - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness.
58 - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'.
59 - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'.
60 - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'.
61 - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'.
62 - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'.
63 - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'.
64 - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'.
65 - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */
69 STAP_ARG_BITNESS_UNDEFINED
,
70 STAP_ARG_BITNESS_8BIT_UNSIGNED
,
71 STAP_ARG_BITNESS_8BIT_SIGNED
,
72 STAP_ARG_BITNESS_16BIT_UNSIGNED
,
73 STAP_ARG_BITNESS_16BIT_SIGNED
,
74 STAP_ARG_BITNESS_32BIT_UNSIGNED
,
75 STAP_ARG_BITNESS_32BIT_SIGNED
,
76 STAP_ARG_BITNESS_64BIT_UNSIGNED
,
77 STAP_ARG_BITNESS_64BIT_SIGNED
,
80 /* The following structure represents a single argument for the probe. */
84 /* Constructor for stap_probe_arg. */
85 stap_probe_arg (enum stap_arg_bitness bitness_
, struct type
*atype_
,
86 expression_up
&&aexpr_
)
87 : bitness (bitness_
), atype (atype_
), aexpr (std::move (aexpr_
))
90 /* The bitness of this argument. */
91 enum stap_arg_bitness bitness
;
93 /* The corresponding `struct type *' to the bitness. */
96 /* The argument converted to an internal GDB expression. */
100 /* Class that implements the static probe methods for "stap" probes. */
102 class stap_static_probe_ops
: public static_probe_ops
106 bool is_linespec (const char **linespecp
) const override
;
109 void get_probes (std::vector
<std::unique_ptr
<probe
>> *probesp
,
110 struct objfile
*objfile
) const override
;
113 const char *type_name () const override
;
116 std::vector
<struct info_probe_column
> gen_info_probes_table_header
120 /* SystemTap static_probe_ops. */
122 const stap_static_probe_ops stap_static_probe_ops
{};
124 class stap_probe
: public probe
127 /* Constructor for stap_probe. */
128 stap_probe (std::string
&&name_
, std::string
&&provider_
, CORE_ADDR address_
,
129 struct gdbarch
*arch_
, CORE_ADDR sem_addr
, const char *args_text
)
130 : probe (std::move (name_
), std::move (provider_
), address_
, arch_
),
131 m_sem_addr (sem_addr
),
132 m_have_parsed_args (false), m_unparsed_args_text (args_text
)
136 CORE_ADDR
get_relocated_address (struct objfile
*objfile
) override
;
139 unsigned get_argument_count (struct frame_info
*frame
) override
;
142 bool can_evaluate_arguments () const override
;
145 struct value
*evaluate_argument (unsigned n
,
146 struct frame_info
*frame
) override
;
149 void compile_to_ax (struct agent_expr
*aexpr
,
150 struct axs_value
*axs_value
,
151 unsigned n
) override
;
154 void set_semaphore (struct objfile
*objfile
,
155 struct gdbarch
*gdbarch
) override
;
158 void clear_semaphore (struct objfile
*objfile
,
159 struct gdbarch
*gdbarch
) override
;
162 const static_probe_ops
*get_static_ops () const override
;
165 std::vector
<const char *> gen_info_probes_table_values () const override
;
167 /* Return argument N of probe.
169 If the probe's arguments have not been parsed yet, parse them. If
170 there are no arguments, throw an exception (error). Otherwise,
171 return the requested argument. */
172 struct stap_probe_arg
*get_arg_by_number (unsigned n
,
173 struct gdbarch
*gdbarch
)
175 if (!m_have_parsed_args
)
176 this->parse_arguments (gdbarch
);
178 gdb_assert (m_have_parsed_args
);
179 if (m_parsed_args
.empty ())
180 internal_error (__FILE__
, __LINE__
,
181 _("Probe '%s' apparently does not have arguments, but \n"
182 "GDB is requesting its argument number %u anyway. "
183 "This should not happen. Please report this bug."),
184 this->get_name ().c_str (), n
);
186 if (n
> m_parsed_args
.size ())
187 internal_error (__FILE__
, __LINE__
,
188 _("Probe '%s' has %d arguments, but GDB is requesting\n"
189 "argument %u. This should not happen. Please\n"
191 this->get_name ().c_str (),
192 (int) m_parsed_args
.size (), n
);
194 return &m_parsed_args
[n
];
197 /* Function which parses an argument string from the probe,
198 correctly splitting the arguments and storing their information
201 Consider the following argument string (x86 syntax):
205 We have two arguments, `%eax' and `$10', both with 32-bit
206 unsigned bitness. This function basically handles them, properly
207 filling some structures with this information. */
208 void parse_arguments (struct gdbarch
*gdbarch
);
211 /* If the probe has a semaphore associated, then this is the value of
212 it, relative to SECT_OFF_DATA. */
213 CORE_ADDR m_sem_addr
;
215 /* True if the arguments have been parsed. */
216 bool m_have_parsed_args
;
218 /* The text version of the probe's arguments, unparsed. */
219 const char *m_unparsed_args_text
;
221 /* Information about each argument. This is an array of `stap_probe_arg',
222 with each entry representing one argument. This is only valid if
223 M_ARGS_PARSED is true. */
224 std::vector
<struct stap_probe_arg
> m_parsed_args
;
227 /* When parsing the arguments, we have to establish different precedences
228 for the various kinds of asm operators. This enumeration represents those
231 This logic behind this is available at
232 <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using
233 the command "info '(as)Infix Ops'". */
235 enum stap_operand_prec
237 /* Lowest precedence, used for non-recognized operands or for the beginning
238 of the parsing process. */
239 STAP_OPERAND_PREC_NONE
= 0,
241 /* Precedence of logical OR. */
242 STAP_OPERAND_PREC_LOGICAL_OR
,
244 /* Precedence of logical AND. */
245 STAP_OPERAND_PREC_LOGICAL_AND
,
247 /* Precedence of additive (plus, minus) and comparative (equal, less,
248 greater-than, etc) operands. */
249 STAP_OPERAND_PREC_ADD_CMP
,
251 /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
253 STAP_OPERAND_PREC_BITWISE
,
255 /* Precedence of multiplicative operands (multiplication, division,
256 remainder, left shift and right shift). */
257 STAP_OPERAND_PREC_MUL
260 static void stap_parse_argument_1 (struct stap_parse_info
*p
, bool has_lhs
,
261 enum stap_operand_prec prec
);
263 static void stap_parse_argument_conditionally (struct stap_parse_info
*p
);
265 /* Returns true if *S is an operator, false otherwise. */
267 static bool stap_is_operator (const char *op
);
270 show_stapexpressiondebug (struct ui_file
*file
, int from_tty
,
271 struct cmd_list_element
*c
, const char *value
)
273 fprintf_filtered (file
, _("SystemTap Probe expression debugging is %s.\n"),
277 /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
278 if the operator code was not recognized. */
280 static enum stap_operand_prec
281 stap_get_operator_prec (enum exp_opcode op
)
285 case BINOP_LOGICAL_OR
:
286 return STAP_OPERAND_PREC_LOGICAL_OR
;
288 case BINOP_LOGICAL_AND
:
289 return STAP_OPERAND_PREC_LOGICAL_AND
;
299 return STAP_OPERAND_PREC_ADD_CMP
;
301 case BINOP_BITWISE_IOR
:
302 case BINOP_BITWISE_AND
:
303 case BINOP_BITWISE_XOR
:
304 case UNOP_LOGICAL_NOT
:
305 return STAP_OPERAND_PREC_BITWISE
;
312 return STAP_OPERAND_PREC_MUL
;
315 return STAP_OPERAND_PREC_NONE
;
319 /* Given S, read the operator in it. Return the EXP_OPCODE which
320 represents the operator detected, or throw an error if no operator
323 static enum exp_opcode
324 stap_get_opcode (const char **s
)
379 op
= BINOP_BITWISE_IOR
;
383 op
= BINOP_LOGICAL_OR
;
388 op
= BINOP_BITWISE_AND
;
392 op
= BINOP_LOGICAL_AND
;
397 op
= BINOP_BITWISE_XOR
;
401 op
= UNOP_LOGICAL_NOT
;
413 gdb_assert (**s
== '=');
418 error (_("Invalid opcode in expression `%s' for SystemTap"
425 /* Given the bitness of the argument, represented by B, return the
426 corresponding `struct type *', or throw an error if B is
430 stap_get_expected_argument_type (struct gdbarch
*gdbarch
,
431 enum stap_arg_bitness b
,
432 const char *probe_name
)
436 case STAP_ARG_BITNESS_UNDEFINED
:
437 if (gdbarch_addr_bit (gdbarch
) == 32)
438 return builtin_type (gdbarch
)->builtin_uint32
;
440 return builtin_type (gdbarch
)->builtin_uint64
;
442 case STAP_ARG_BITNESS_8BIT_UNSIGNED
:
443 return builtin_type (gdbarch
)->builtin_uint8
;
445 case STAP_ARG_BITNESS_8BIT_SIGNED
:
446 return builtin_type (gdbarch
)->builtin_int8
;
448 case STAP_ARG_BITNESS_16BIT_UNSIGNED
:
449 return builtin_type (gdbarch
)->builtin_uint16
;
451 case STAP_ARG_BITNESS_16BIT_SIGNED
:
452 return builtin_type (gdbarch
)->builtin_int16
;
454 case STAP_ARG_BITNESS_32BIT_SIGNED
:
455 return builtin_type (gdbarch
)->builtin_int32
;
457 case STAP_ARG_BITNESS_32BIT_UNSIGNED
:
458 return builtin_type (gdbarch
)->builtin_uint32
;
460 case STAP_ARG_BITNESS_64BIT_SIGNED
:
461 return builtin_type (gdbarch
)->builtin_int64
;
463 case STAP_ARG_BITNESS_64BIT_UNSIGNED
:
464 return builtin_type (gdbarch
)->builtin_uint64
;
467 error (_("Undefined bitness for probe '%s'."), probe_name
);
472 /* Helper function to check for a generic list of prefixes. GDBARCH
473 is the current gdbarch being used. S is the expression being
474 analyzed. If R is not NULL, it will be used to return the found
475 prefix. PREFIXES is the list of expected prefixes.
477 This function does a case-insensitive match.
479 Return true if any prefix has been found, false otherwise. */
482 stap_is_generic_prefix (struct gdbarch
*gdbarch
, const char *s
,
483 const char **r
, const char *const *prefixes
)
485 const char *const *p
;
487 if (prefixes
== NULL
)
495 for (p
= prefixes
; *p
!= NULL
; ++p
)
496 if (strncasecmp (s
, *p
, strlen (*p
)) == 0)
507 /* Return true if S points to a register prefix, false otherwise. For
508 a description of the arguments, look at stap_is_generic_prefix. */
511 stap_is_register_prefix (struct gdbarch
*gdbarch
, const char *s
,
514 const char *const *t
= gdbarch_stap_register_prefixes (gdbarch
);
516 return stap_is_generic_prefix (gdbarch
, s
, r
, t
);
519 /* Return true if S points to a register indirection prefix, false
520 otherwise. For a description of the arguments, look at
521 stap_is_generic_prefix. */
524 stap_is_register_indirection_prefix (struct gdbarch
*gdbarch
, const char *s
,
527 const char *const *t
= gdbarch_stap_register_indirection_prefixes (gdbarch
);
529 return stap_is_generic_prefix (gdbarch
, s
, r
, t
);
532 /* Return true if S points to an integer prefix, false otherwise. For
533 a description of the arguments, look at stap_is_generic_prefix.
535 This function takes care of analyzing whether we are dealing with
536 an expected integer prefix, or, if there is no integer prefix to be
537 expected, whether we are dealing with a digit. It does a
538 case-insensitive match. */
541 stap_is_integer_prefix (struct gdbarch
*gdbarch
, const char *s
,
544 const char *const *t
= gdbarch_stap_integer_prefixes (gdbarch
);
545 const char *const *p
;
549 /* A NULL value here means that integers do not have a prefix.
550 We just check for a digit then. */
554 return isdigit (*s
) > 0;
557 for (p
= t
; *p
!= NULL
; ++p
)
559 size_t len
= strlen (*p
);
561 if ((len
== 0 && isdigit (*s
))
562 || (len
> 0 && strncasecmp (s
, *p
, len
) == 0))
564 /* Integers may or may not have a prefix. The "len == 0"
565 check covers the case when integers do not have a prefix
566 (therefore, we just check if we have a digit). The call
567 to "strncasecmp" covers the case when they have a
579 /* Helper function to check for a generic list of suffixes. If we are
580 not expecting any suffixes, then it just returns 1. If we are
581 expecting at least one suffix, then it returns true if a suffix has
582 been found, false otherwise. GDBARCH is the current gdbarch being
583 used. S is the expression being analyzed. If R is not NULL, it
584 will be used to return the found suffix. SUFFIXES is the list of
585 expected suffixes. This function does a case-insensitive
589 stap_generic_check_suffix (struct gdbarch
*gdbarch
, const char *s
,
590 const char **r
, const char *const *suffixes
)
592 const char *const *p
;
595 if (suffixes
== NULL
)
603 for (p
= suffixes
; *p
!= NULL
; ++p
)
604 if (strncasecmp (s
, *p
, strlen (*p
)) == 0)
616 /* Return true if S points to an integer suffix, false otherwise. For
617 a description of the arguments, look at
618 stap_generic_check_suffix. */
621 stap_check_integer_suffix (struct gdbarch
*gdbarch
, const char *s
,
624 const char *const *p
= gdbarch_stap_integer_suffixes (gdbarch
);
626 return stap_generic_check_suffix (gdbarch
, s
, r
, p
);
629 /* Return true if S points to a register suffix, false otherwise. For
630 a description of the arguments, look at
631 stap_generic_check_suffix. */
634 stap_check_register_suffix (struct gdbarch
*gdbarch
, const char *s
,
637 const char *const *p
= gdbarch_stap_register_suffixes (gdbarch
);
639 return stap_generic_check_suffix (gdbarch
, s
, r
, p
);
642 /* Return true if S points to a register indirection suffix, false
643 otherwise. For a description of the arguments, look at
644 stap_generic_check_suffix. */
647 stap_check_register_indirection_suffix (struct gdbarch
*gdbarch
, const char *s
,
650 const char *const *p
= gdbarch_stap_register_indirection_suffixes (gdbarch
);
652 return stap_generic_check_suffix (gdbarch
, s
, r
, p
);
655 /* Function responsible for parsing a register operand according to
656 SystemTap parlance. Assuming:
660 RIP = register indirection prefix
661 RIS = register indirection suffix
663 Then a register operand can be:
665 [RIP] [RP] REGISTER [RS] [RIS]
667 This function takes care of a register's indirection, displacement and
668 direct access. It also takes into consideration the fact that some
669 registers are named differently inside and outside GDB, e.g., PPC's
670 general-purpose registers are represented by integers in the assembly
671 language (e.g., `15' is the 15th general-purpose register), but inside
672 GDB they have a prefix (the letter `r') appended. */
675 stap_parse_register_operand (struct stap_parse_info
*p
)
677 /* Simple flag to indicate whether we have seen a minus signal before
679 bool got_minus
= false;
680 /* Flags to indicate whether this register access is being displaced and/or
683 bool indirect_p
= false;
684 struct gdbarch
*gdbarch
= p
->gdbarch
;
685 /* Needed to generate the register name as a part of an expression. */
687 /* Variables used to extract the register name from the probe's
690 const char *gdb_reg_prefix
= gdbarch_stap_gdb_register_prefix (gdbarch
);
691 const char *gdb_reg_suffix
= gdbarch_stap_gdb_register_suffix (gdbarch
);
692 const char *reg_prefix
;
693 const char *reg_ind_prefix
;
694 const char *reg_suffix
;
695 const char *reg_ind_suffix
;
697 /* Checking for a displacement argument. */
700 /* If it's a plus sign, we don't need to do anything, just advance the
704 else if (*p
->arg
== '-')
710 if (isdigit (*p
->arg
))
712 /* The value of the displacement. */
717 displacement
= strtol (p
->arg
, &endp
, 10);
720 /* Generating the expression for the displacement. */
721 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
722 write_exp_elt_type (&p
->pstate
, builtin_type (gdbarch
)->builtin_long
);
723 write_exp_elt_longcst (&p
->pstate
, displacement
);
724 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
726 write_exp_elt_opcode (&p
->pstate
, UNOP_NEG
);
729 /* Getting rid of register indirection prefix. */
730 if (stap_is_register_indirection_prefix (gdbarch
, p
->arg
, ®_ind_prefix
))
733 p
->arg
+= strlen (reg_ind_prefix
);
736 if (disp_p
&& !indirect_p
)
737 error (_("Invalid register displacement syntax on expression `%s'."),
740 /* Getting rid of register prefix. */
741 if (stap_is_register_prefix (gdbarch
, p
->arg
, ®_prefix
))
742 p
->arg
+= strlen (reg_prefix
);
744 /* Now we should have only the register name. Let's extract it and get
745 the associated number. */
748 /* We assume the register name is composed by letters and numbers. */
749 while (isalnum (*p
->arg
))
752 std::string
regname (start
, p
->arg
- start
);
754 /* We only add the GDB's register prefix/suffix if we are dealing with
755 a numeric register. */
756 if (isdigit (*start
))
758 if (gdb_reg_prefix
!= NULL
)
759 regname
= gdb_reg_prefix
+ regname
;
761 if (gdb_reg_suffix
!= NULL
)
762 regname
+= gdb_reg_suffix
;
765 int regnum
= user_reg_map_name_to_regnum (gdbarch
, regname
.c_str (),
768 /* Is this a valid register name? */
770 error (_("Invalid register name `%s' on expression `%s'."),
771 regname
.c_str (), p
->saved_arg
);
773 /* Check if there's any special treatment that the arch-specific
774 code would like to perform on the register name. */
775 if (gdbarch_stap_adjust_register_p (gdbarch
))
777 std::string oldregname
= regname
;
779 gdbarch_stap_adjust_register (gdbarch
, p
, regname
, regnum
);
781 if (regname
!= oldregname
)
783 /* This is just a check we perform to make sure that the
784 arch-dependent code has provided us with a valid
786 regnum
= user_reg_map_name_to_regnum (gdbarch
, regname
.c_str (),
790 internal_error (__FILE__
, __LINE__
,
791 _("Invalid register name '%s' after replacing it"
792 " (previous name was '%s')"),
793 regname
.c_str (), oldregname
.c_str ());
797 write_exp_elt_opcode (&p
->pstate
, OP_REGISTER
);
798 str
.ptr
= regname
.c_str ();
799 str
.length
= regname
.size ();
800 write_exp_string (&p
->pstate
, str
);
801 write_exp_elt_opcode (&p
->pstate
, OP_REGISTER
);
806 write_exp_elt_opcode (&p
->pstate
, BINOP_ADD
);
808 /* Casting to the expected type. */
809 write_exp_elt_opcode (&p
->pstate
, UNOP_CAST
);
810 write_exp_elt_type (&p
->pstate
, lookup_pointer_type (p
->arg_type
));
811 write_exp_elt_opcode (&p
->pstate
, UNOP_CAST
);
813 write_exp_elt_opcode (&p
->pstate
, UNOP_IND
);
816 /* Getting rid of the register name suffix. */
817 if (stap_check_register_suffix (gdbarch
, p
->arg
, ®_suffix
))
818 p
->arg
+= strlen (reg_suffix
);
820 error (_("Missing register name suffix on expression `%s'."),
823 /* Getting rid of the register indirection suffix. */
826 if (stap_check_register_indirection_suffix (gdbarch
, p
->arg
,
828 p
->arg
+= strlen (reg_ind_suffix
);
830 error (_("Missing indirection suffix on expression `%s'."),
835 /* This function is responsible for parsing a single operand.
837 A single operand can be:
839 - an unary operation (e.g., `-5', `~2', or even with subexpressions
841 - a register displacement, which will be treated as a register
842 operand (e.g., `-4(%eax)' on x86)
843 - a numeric constant, or
844 - a register operand (see function `stap_parse_register_operand')
846 The function also calls special-handling functions to deal with
847 unrecognized operands, allowing arch-specific parsers to be
851 stap_parse_single_operand (struct stap_parse_info
*p
)
853 struct gdbarch
*gdbarch
= p
->gdbarch
;
854 const char *int_prefix
= NULL
;
856 /* We first try to parse this token as a "special token". */
857 if (gdbarch_stap_parse_special_token_p (gdbarch
)
858 && (gdbarch_stap_parse_special_token (gdbarch
, p
) != 0))
860 /* If the return value of the above function is not zero,
861 it means it successfully parsed the special token.
863 If it is NULL, we try to parse it using our method. */
867 if (*p
->arg
== '-' || *p
->arg
== '~' || *p
->arg
== '+')
870 /* We use this variable to do a lookahead. */
871 const char *tmp
= p
->arg
;
872 bool has_digit
= false;
874 /* Skipping signal. */
877 /* This is an unary operation. Here is a list of allowed tokens
881 - number (from register displacement)
882 - subexpression (beginning with `(')
884 We handle the register displacement here, and the other cases
886 if (p
->inside_paren_p
)
887 tmp
= skip_spaces (tmp
);
889 while (isdigit (*tmp
))
891 /* We skip the digit here because we are only interested in
892 knowing what kind of unary operation this is. The digit
893 will be handled by one of the functions that will be
894 called below ('stap_parse_argument_conditionally' or
895 'stap_parse_register_operand'). */
900 if (has_digit
&& stap_is_register_indirection_prefix (gdbarch
, tmp
,
903 /* If we are here, it means it is a displacement. The only
904 operations allowed here are `-' and `+'. */
905 if (c
!= '-' && c
!= '+')
906 error (_("Invalid operator `%c' for register displacement "
907 "on expression `%s'."), c
, p
->saved_arg
);
909 stap_parse_register_operand (p
);
913 /* This is not a displacement. We skip the operator, and
914 deal with it when the recursion returns. */
916 stap_parse_argument_conditionally (p
);
918 write_exp_elt_opcode (&p
->pstate
, UNOP_NEG
);
920 write_exp_elt_opcode (&p
->pstate
, UNOP_COMPLEMENT
);
923 else if (isdigit (*p
->arg
))
925 /* A temporary variable, needed for lookahead. */
926 const char *tmp
= p
->arg
;
930 /* We can be dealing with a numeric constant, or with a register
932 number
= strtol (tmp
, &endp
, 10);
935 if (p
->inside_paren_p
)
936 tmp
= skip_spaces (tmp
);
938 /* If "stap_is_integer_prefix" returns true, it means we can
939 accept integers without a prefix here. But we also need to
940 check whether the next token (i.e., "tmp") is not a register
941 indirection prefix. */
942 if (stap_is_integer_prefix (gdbarch
, p
->arg
, NULL
)
943 && !stap_is_register_indirection_prefix (gdbarch
, tmp
, NULL
))
945 const char *int_suffix
;
947 /* We are dealing with a numeric constant. */
948 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
949 write_exp_elt_type (&p
->pstate
,
950 builtin_type (gdbarch
)->builtin_long
);
951 write_exp_elt_longcst (&p
->pstate
, number
);
952 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
956 if (stap_check_integer_suffix (gdbarch
, p
->arg
, &int_suffix
))
957 p
->arg
+= strlen (int_suffix
);
959 error (_("Invalid constant suffix on expression `%s'."),
962 else if (stap_is_register_indirection_prefix (gdbarch
, tmp
, NULL
))
963 stap_parse_register_operand (p
);
965 error (_("Unknown numeric token on expression `%s'."),
968 else if (stap_is_integer_prefix (gdbarch
, p
->arg
, &int_prefix
))
970 /* We are dealing with a numeric constant. */
973 const char *int_suffix
;
975 p
->arg
+= strlen (int_prefix
);
976 number
= strtol (p
->arg
, &endp
, 10);
979 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
980 write_exp_elt_type (&p
->pstate
, builtin_type (gdbarch
)->builtin_long
);
981 write_exp_elt_longcst (&p
->pstate
, number
);
982 write_exp_elt_opcode (&p
->pstate
, OP_LONG
);
984 if (stap_check_integer_suffix (gdbarch
, p
->arg
, &int_suffix
))
985 p
->arg
+= strlen (int_suffix
);
987 error (_("Invalid constant suffix on expression `%s'."),
990 else if (stap_is_register_prefix (gdbarch
, p
->arg
, NULL
)
991 || stap_is_register_indirection_prefix (gdbarch
, p
->arg
, NULL
))
992 stap_parse_register_operand (p
);
994 error (_("Operator `%c' not recognized on expression `%s'."),
995 *p
->arg
, p
->saved_arg
);
998 /* This function parses an argument conditionally, based on single or
999 non-single operands. A non-single operand would be a parenthesized
1000 expression (e.g., `(2 + 1)'), and a single operand is anything that
1001 starts with `-', `~', `+' (i.e., unary operators), a digit, or
1002 something recognized by `gdbarch_stap_is_single_operand'. */
1005 stap_parse_argument_conditionally (struct stap_parse_info
*p
)
1007 gdb_assert (gdbarch_stap_is_single_operand_p (p
->gdbarch
));
1009 if (*p
->arg
== '-' || *p
->arg
== '~' || *p
->arg
== '+' /* Unary. */
1010 || isdigit (*p
->arg
)
1011 || gdbarch_stap_is_single_operand (p
->gdbarch
, p
->arg
))
1012 stap_parse_single_operand (p
);
1013 else if (*p
->arg
== '(')
1015 /* We are dealing with a parenthesized operand. It means we
1016 have to parse it as it was a separate expression, without
1017 left-side or precedence. */
1019 p
->arg
= skip_spaces (p
->arg
);
1020 ++p
->inside_paren_p
;
1022 stap_parse_argument_1 (p
, 0, STAP_OPERAND_PREC_NONE
);
1024 --p
->inside_paren_p
;
1026 error (_("Missign close-paren on expression `%s'."),
1030 if (p
->inside_paren_p
)
1031 p
->arg
= skip_spaces (p
->arg
);
1034 error (_("Cannot parse expression `%s'."), p
->saved_arg
);
1037 /* Helper function for `stap_parse_argument'. Please, see its comments to
1038 better understand what this function does. */
1041 stap_parse_argument_1 (struct stap_parse_info
*p
, bool has_lhs
,
1042 enum stap_operand_prec prec
)
1044 /* This is an operator-precedence parser.
1046 We work with left- and right-sides of expressions, and
1047 parse them depending on the precedence of the operators
1050 gdb_assert (p
->arg
!= NULL
);
1052 if (p
->inside_paren_p
)
1053 p
->arg
= skip_spaces (p
->arg
);
1057 /* We were called without a left-side, either because this is the
1058 first call, or because we were called to parse a parenthesized
1059 expression. It doesn't really matter; we have to parse the
1060 left-side in order to continue the process. */
1061 stap_parse_argument_conditionally (p
);
1064 /* Start to parse the right-side, and to "join" left and right sides
1065 depending on the operation specified.
1067 This loop shall continue until we run out of characters in the input,
1068 or until we find a close-parenthesis, which means that we've reached
1069 the end of a sub-expression. */
1070 while (*p
->arg
!= '\0' && *p
->arg
!= ')' && !isspace (*p
->arg
))
1072 const char *tmp_exp_buf
;
1073 enum exp_opcode opcode
;
1074 enum stap_operand_prec cur_prec
;
1076 if (!stap_is_operator (p
->arg
))
1077 error (_("Invalid operator `%c' on expression `%s'."), *p
->arg
,
1080 /* We have to save the current value of the expression buffer because
1081 the `stap_get_opcode' modifies it in order to get the current
1082 operator. If this operator's precedence is lower than PREC, we
1083 should return and not advance the expression buffer pointer. */
1084 tmp_exp_buf
= p
->arg
;
1085 opcode
= stap_get_opcode (&tmp_exp_buf
);
1087 cur_prec
= stap_get_operator_prec (opcode
);
1088 if (cur_prec
< prec
)
1090 /* If the precedence of the operator that we are seeing now is
1091 lower than the precedence of the first operator seen before
1092 this parsing process began, it means we should stop parsing
1097 p
->arg
= tmp_exp_buf
;
1098 if (p
->inside_paren_p
)
1099 p
->arg
= skip_spaces (p
->arg
);
1101 /* Parse the right-side of the expression. */
1102 stap_parse_argument_conditionally (p
);
1104 /* While we still have operators, try to parse another
1105 right-side, but using the current right-side as a left-side. */
1106 while (*p
->arg
!= '\0' && stap_is_operator (p
->arg
))
1108 enum exp_opcode lookahead_opcode
;
1109 enum stap_operand_prec lookahead_prec
;
1111 /* Saving the current expression buffer position. The explanation
1112 is the same as above. */
1113 tmp_exp_buf
= p
->arg
;
1114 lookahead_opcode
= stap_get_opcode (&tmp_exp_buf
);
1115 lookahead_prec
= stap_get_operator_prec (lookahead_opcode
);
1117 if (lookahead_prec
<= prec
)
1119 /* If we are dealing with an operator whose precedence is lower
1120 than the first one, just abandon the attempt. */
1124 /* Parse the right-side of the expression, but since we already
1125 have a left-side at this point, set `has_lhs' to 1. */
1126 stap_parse_argument_1 (p
, 1, lookahead_prec
);
1129 write_exp_elt_opcode (&p
->pstate
, opcode
);
1133 /* Parse a probe's argument.
1137 LP = literal integer prefix
1138 LS = literal integer suffix
1140 RP = register prefix
1141 RS = register suffix
1143 RIP = register indirection prefix
1144 RIS = register indirection suffix
1146 This routine assumes that arguments' tokens are of the form:
1149 - [RP] REGISTER [RS]
1150 - [RIP] [RP] REGISTER [RS] [RIS]
1151 - If we find a number without LP, we try to parse it as a literal integer
1152 constant (if LP == NULL), or as a register displacement.
1153 - We count parenthesis, and only skip whitespaces if we are inside them.
1154 - If we find an operator, we skip it.
1156 This function can also call a special function that will try to match
1157 unknown tokens. It will return the expression_up generated from
1158 parsing the argument. */
1160 static expression_up
1161 stap_parse_argument (const char **arg
, struct type
*atype
,
1162 struct gdbarch
*gdbarch
)
1164 /* We need to initialize the expression buffer, in order to begin
1165 our parsing efforts. We use language_c here because we may need
1166 to do pointer arithmetics. */
1167 struct stap_parse_info
p (*arg
, atype
, language_def (language_c
),
1170 stap_parse_argument_1 (&p
, 0, STAP_OPERAND_PREC_NONE
);
1172 gdb_assert (p
.inside_paren_p
== 0);
1174 /* Casting the final expression to the appropriate type. */
1175 write_exp_elt_opcode (&p
.pstate
, UNOP_CAST
);
1176 write_exp_elt_type (&p
.pstate
, atype
);
1177 write_exp_elt_opcode (&p
.pstate
, UNOP_CAST
);
1179 p
.arg
= skip_spaces (p
.arg
);
1182 return p
.pstate
.release ();
1185 /* Implementation of 'parse_arguments' method. */
1188 stap_probe::parse_arguments (struct gdbarch
*gdbarch
)
1192 gdb_assert (!m_have_parsed_args
);
1193 cur
= m_unparsed_args_text
;
1194 m_have_parsed_args
= true;
1196 if (cur
== NULL
|| *cur
== '\0' || *cur
== ':')
1199 while (*cur
!= '\0')
1201 enum stap_arg_bitness bitness
;
1202 bool got_minus
= false;
1204 /* We expect to find something like:
1208 Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so
1209 we check it here. If we don't find it, go to the next
1211 if ((cur
[0] == '-' && isdigit (cur
[1]) && cur
[2] == '@')
1212 || (isdigit (cur
[0]) && cur
[1] == '@'))
1216 /* Discard the `-'. */
1221 /* Defining the bitness. */
1225 bitness
= (got_minus
? STAP_ARG_BITNESS_8BIT_SIGNED
1226 : STAP_ARG_BITNESS_8BIT_UNSIGNED
);
1230 bitness
= (got_minus
? STAP_ARG_BITNESS_16BIT_SIGNED
1231 : STAP_ARG_BITNESS_16BIT_UNSIGNED
);
1235 bitness
= (got_minus
? STAP_ARG_BITNESS_32BIT_SIGNED
1236 : STAP_ARG_BITNESS_32BIT_UNSIGNED
);
1240 bitness
= (got_minus
? STAP_ARG_BITNESS_64BIT_SIGNED
1241 : STAP_ARG_BITNESS_64BIT_UNSIGNED
);
1246 /* We have an error, because we don't expect anything
1247 except 1, 2, 4 and 8. */
1248 warning (_("unrecognized bitness %s%c' for probe `%s'"),
1249 got_minus
? "`-" : "`", *cur
,
1250 this->get_name ().c_str ());
1254 /* Discard the number and the `@' sign. */
1258 bitness
= STAP_ARG_BITNESS_UNDEFINED
;
1261 = stap_get_expected_argument_type (gdbarch
, bitness
,
1262 this->get_name ().c_str ());
1264 expression_up expr
= stap_parse_argument (&cur
, atype
, gdbarch
);
1266 if (stap_expression_debug
)
1267 dump_raw_expression (expr
.get (), gdb_stdlog
,
1268 "before conversion to prefix form");
1270 prefixify_expression (expr
.get ());
1272 if (stap_expression_debug
)
1273 dump_prefix_expression (expr
.get (), gdb_stdlog
);
1275 m_parsed_args
.emplace_back (bitness
, atype
, std::move (expr
));
1277 /* Start it over again. */
1278 cur
= skip_spaces (cur
);
1282 /* Helper function to relocate an address. */
1285 relocate_address (CORE_ADDR address
, struct objfile
*objfile
)
1287 return address
+ ANOFFSET (objfile
->section_offsets
,
1288 SECT_OFF_DATA (objfile
));
1291 /* Implementation of the get_relocated_address method. */
1294 stap_probe::get_relocated_address (struct objfile
*objfile
)
1296 return relocate_address (this->get_address (), objfile
);
1299 /* Given PROBE, returns the number of arguments present in that probe's
1303 stap_probe::get_argument_count (struct frame_info
*frame
)
1305 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1307 if (!m_have_parsed_args
)
1309 if (this->can_evaluate_arguments ())
1310 this->parse_arguments (gdbarch
);
1313 static bool have_warned_stap_incomplete
= false;
1315 if (!have_warned_stap_incomplete
)
1318 "The SystemTap SDT probe support is not fully implemented on this target;\n"
1319 "you will not be able to inspect the arguments of the probes.\n"
1320 "Please report a bug against GDB requesting a port to this target."));
1321 have_warned_stap_incomplete
= true;
1324 /* Marking the arguments as "already parsed". */
1325 m_have_parsed_args
= true;
1329 gdb_assert (m_have_parsed_args
);
1330 return m_parsed_args
.size ();
1333 /* Return true if OP is a valid operator inside a probe argument, or
1337 stap_is_operator (const char *op
)
1362 /* We didn't find any operator. */
1369 /* Implement the `can_evaluate_arguments' method. */
1372 stap_probe::can_evaluate_arguments () const
1374 struct gdbarch
*gdbarch
= this->get_gdbarch ();
1376 /* For SystemTap probes, we have to guarantee that the method
1377 stap_is_single_operand is defined on gdbarch. If it is not, then it
1378 means that argument evaluation is not implemented on this target. */
1379 return gdbarch_stap_is_single_operand_p (gdbarch
);
1382 /* Evaluate the probe's argument N (indexed from 0), returning a value
1383 corresponding to it. Assertion is thrown if N does not exist. */
1386 stap_probe::evaluate_argument (unsigned n
, struct frame_info
*frame
)
1388 struct stap_probe_arg
*arg
;
1390 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1392 arg
= this->get_arg_by_number (n
, gdbarch
);
1393 return evaluate_subexp_standard (arg
->atype
, arg
->aexpr
.get (), &pos
,
1397 /* Compile the probe's argument N (indexed from 0) to agent expression.
1398 Assertion is thrown if N does not exist. */
1401 stap_probe::compile_to_ax (struct agent_expr
*expr
, struct axs_value
*value
,
1404 struct stap_probe_arg
*arg
;
1405 union exp_element
*pc
;
1407 arg
= this->get_arg_by_number (n
, expr
->gdbarch
);
1409 pc
= arg
->aexpr
->elts
;
1410 gen_expr (arg
->aexpr
.get (), &pc
, expr
, value
);
1412 require_rvalue (expr
, value
);
1413 value
->type
= arg
->atype
;
1417 /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
1418 address. SET is zero if the semaphore should be cleared, or one if
1419 it should be set. This is a helper function for
1420 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */
1423 stap_modify_semaphore (CORE_ADDR address
, int set
, struct gdbarch
*gdbarch
)
1425 gdb_byte bytes
[sizeof (LONGEST
)];
1426 /* The ABI specifies "unsigned short". */
1427 struct type
*type
= builtin_type (gdbarch
)->builtin_unsigned_short
;
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."));
1440 value
= extract_unsigned_integer (bytes
, TYPE_LENGTH (type
),
1441 gdbarch_byte_order (gdbarch
));
1442 /* Note that we explicitly don't worry about overflow or
1449 store_unsigned_integer (bytes
, TYPE_LENGTH (type
),
1450 gdbarch_byte_order (gdbarch
), value
);
1452 if (target_write_memory (address
, bytes
, TYPE_LENGTH (type
)) != 0)
1453 warning (_("Could not write the value of a SystemTap semaphore."));
1456 /* Implementation of the 'set_semaphore' method.
1458 SystemTap semaphores act as reference counters, so calls to this
1459 function must be paired with calls to 'clear_semaphore'.
1461 This function and 'clear_semaphore' race with another tool
1462 changing the probes, but that is too rare to care. */
1465 stap_probe::set_semaphore (struct objfile
*objfile
, struct gdbarch
*gdbarch
)
1467 stap_modify_semaphore (relocate_address (m_sem_addr
, objfile
), 1, gdbarch
);
1470 /* Implementation of the 'clear_semaphore' method. */
1473 stap_probe::clear_semaphore (struct objfile
*objfile
, struct gdbarch
*gdbarch
)
1475 stap_modify_semaphore (relocate_address (m_sem_addr
, objfile
), 0, gdbarch
);
1478 /* Implementation of the 'get_static_ops' method. */
1480 const static_probe_ops
*
1481 stap_probe::get_static_ops () const
1483 return &stap_static_probe_ops
;
1486 /* Implementation of the 'gen_info_probes_table_values' method. */
1488 std::vector
<const char *>
1489 stap_probe::gen_info_probes_table_values () const
1491 const char *val
= NULL
;
1493 if (m_sem_addr
!= 0)
1494 val
= print_core_address (this->get_gdbarch (), m_sem_addr
);
1496 return std::vector
<const char *> { val
};
1499 /* Helper function that parses the information contained in a
1500 SystemTap's probe. Basically, the information consists in:
1502 - Probe's PC address;
1503 - Link-time section address of `.stapsdt.base' section;
1504 - Link-time address of the semaphore variable, or ZERO if the
1505 probe doesn't have an associated semaphore;
1506 - Probe's provider name;
1508 - Probe's argument format. */
1511 handle_stap_probe (struct objfile
*objfile
, struct sdt_note
*el
,
1512 std::vector
<std::unique_ptr
<probe
>> *probesp
,
1515 bfd
*abfd
= objfile
->obfd
;
1516 int size
= bfd_get_arch_size (abfd
) / 8;
1517 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1518 struct type
*ptr_type
= builtin_type (gdbarch
)->builtin_data_ptr
;
1520 /* Provider and the name of the probe. */
1521 const char *provider
= (const char *) &el
->data
[3 * size
];
1522 const char *name
= ((const char *)
1523 memchr (provider
, '\0',
1524 (char *) el
->data
+ el
->size
- provider
));
1525 /* Making sure there is a name. */
1528 complaint (_("corrupt probe name when reading `%s'"),
1529 objfile_name (objfile
));
1531 /* There is no way to use a probe without a name or a provider, so
1532 returning here makes sense. */
1538 /* Retrieving the probe's address. */
1539 CORE_ADDR address
= extract_typed_address (&el
->data
[0], ptr_type
);
1541 /* Link-time sh_addr of `.stapsdt.base' section. */
1542 CORE_ADDR base_ref
= extract_typed_address (&el
->data
[size
], ptr_type
);
1544 /* Semaphore address. */
1545 CORE_ADDR sem_addr
= extract_typed_address (&el
->data
[2 * size
], ptr_type
);
1547 address
+= base
- base_ref
;
1549 sem_addr
+= base
- base_ref
;
1551 /* Arguments. We can only extract the argument format if there is a valid
1552 name for this probe. */
1553 const char *probe_args
= ((const char*)
1555 (char *) el
->data
+ el
->size
- name
));
1557 if (probe_args
!= NULL
)
1560 if (probe_args
== NULL
1561 || (memchr (probe_args
, '\0', (char *) el
->data
+ el
->size
- name
)
1562 != el
->data
+ el
->size
- 1))
1564 complaint (_("corrupt probe argument when reading `%s'"),
1565 objfile_name (objfile
));
1566 /* If the argument string is NULL, it means some problem happened with
1567 it. So we return. */
1571 stap_probe
*ret
= new stap_probe (std::string (name
), std::string (provider
),
1572 address
, gdbarch
, sem_addr
, probe_args
);
1574 /* Successfully created probe. */
1575 probesp
->emplace_back (ret
);
1578 /* Helper function which tries to find the base address of the SystemTap
1579 base section named STAP_BASE_SECTION_NAME. */
1582 get_stap_base_address_1 (bfd
*abfd
, asection
*sect
, void *obj
)
1584 asection
**ret
= (asection
**) obj
;
1586 if ((sect
->flags
& (SEC_DATA
| SEC_ALLOC
| SEC_HAS_CONTENTS
))
1587 && sect
->name
&& !strcmp (sect
->name
, STAP_BASE_SECTION_NAME
))
1591 /* Helper function which iterates over every section in the BFD file,
1592 trying to find the base address of the SystemTap base section.
1593 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1596 get_stap_base_address (bfd
*obfd
, bfd_vma
*base
)
1598 asection
*ret
= NULL
;
1600 bfd_map_over_sections (obfd
, get_stap_base_address_1
, (void *) &ret
);
1604 complaint (_("could not obtain base address for "
1605 "SystemTap section on objfile `%s'."),
1616 /* Implementation of the 'is_linespec' method. */
1619 stap_static_probe_ops::is_linespec (const char **linespecp
) const
1621 static const char *const keywords
[] = { "-pstap", "-probe-stap", NULL
};
1623 return probe_is_linespec_by_keyword (linespecp
, keywords
);
1626 /* Implementation of the 'get_probes' method. */
1629 stap_static_probe_ops::get_probes
1630 (std::vector
<std::unique_ptr
<probe
>> *probesp
,
1631 struct objfile
*objfile
) const
1633 /* If we are here, then this is the first time we are parsing the
1634 SystemTap probe's information. We basically have to count how many
1635 probes the objfile has, and then fill in the necessary information
1637 bfd
*obfd
= objfile
->obfd
;
1639 struct sdt_note
*iter
;
1640 unsigned save_probesp_len
= probesp
->size ();
1642 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
1644 /* This is a .debug file, not the objfile itself. */
1648 if (elf_tdata (obfd
)->sdt_note_head
== NULL
)
1650 /* There isn't any probe here. */
1654 if (!get_stap_base_address (obfd
, &base
))
1656 /* There was an error finding the base address for the section.
1657 Just return NULL. */
1661 /* Parsing each probe's information. */
1662 for (iter
= elf_tdata (obfd
)->sdt_note_head
;
1666 /* We first have to handle all the information about the
1667 probe which is present in the section. */
1668 handle_stap_probe (objfile
, iter
, probesp
, base
);
1671 if (save_probesp_len
== probesp
->size ())
1673 /* If we are here, it means we have failed to parse every known
1675 complaint (_("could not parse SystemTap probe(s) from inferior"));
1680 /* Implementation of the type_name method. */
1683 stap_static_probe_ops::type_name () const
1688 /* Implementation of the 'gen_info_probes_table_header' method. */
1690 std::vector
<struct info_probe_column
>
1691 stap_static_probe_ops::gen_info_probes_table_header () const
1693 struct info_probe_column stap_probe_column
;
1695 stap_probe_column
.field_name
= "semaphore";
1696 stap_probe_column
.print_name
= _("Semaphore");
1698 return std::vector
<struct info_probe_column
> { stap_probe_column
};
1701 /* Implementation of the `info probes stap' command. */
1704 info_probes_stap_command (const char *arg
, int from_tty
)
1706 info_probes_for_spops (arg
, from_tty
, &stap_static_probe_ops
);
1710 _initialize_stap_probe (void)
1712 all_static_probe_ops
.push_back (&stap_static_probe_ops
);
1714 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance
,
1715 &stap_expression_debug
,
1716 _("Set SystemTap expression debugging."),
1717 _("Show SystemTap expression debugging."),
1718 _("When non-zero, the internal representation "
1719 "of SystemTap expressions will be printed."),
1721 show_stapexpressiondebug
,
1722 &setdebuglist
, &showdebuglist
);
1724 add_cmd ("stap", class_info
, info_probes_stap_command
,
1726 Show information about SystemTap static probes.\n\
1727 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1728 Each argument is a regular expression, used to select probes.\n\
1729 PROVIDER matches probe provider names.\n\
1730 NAME matches the probe names.\n\
1731 OBJECT matches the executable or shared library name."),
1732 info_probes_cmdlist_get ());