[PATCH 30/57][Arm][GAS] Add support for MVE instructions: vqmovnt, vqmovnb, vqmovunt...
[binutils-gdb.git] / gdb / stap-probe.c
blobe70940c4878b578d499a56944e0220c294f1c46b
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/>. */
20 #include "defs.h"
21 #include "stap-probe.h"
22 #include "probe.h"
23 #include "common/vec.h"
24 #include "ui-out.h"
25 #include "objfiles.h"
26 #include "arch-utils.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29 #include "filenames.h"
30 #include "value.h"
31 #include "ax.h"
32 #include "ax-gdb.h"
33 #include "complaints.h"
34 #include "cli/cli-utils.h"
35 #include "linespec.h"
36 #include "user-regs.h"
37 #include "parser-defs.h"
38 #include "language.h"
39 #include "elf-bfd.h"
41 #include <ctype.h>
43 /* The name of the SystemTap section where we will find information about
44 the probes. */
46 #define STAP_BASE_SECTION_NAME ".stapsdt.base"
48 /* Should we display debug information for the probe's argument expression
49 parsing? */
51 static unsigned int stap_expression_debug = 0;
53 /* The various possibilities of bitness defined for a probe's argument.
55 The relationship is:
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@'. */
67 enum stap_arg_bitness
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. */
82 struct stap_probe_arg
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. */
94 struct type *atype;
96 /* The argument converted to an internal GDB expression. */
97 expression_up aexpr;
100 /* Class that implements the static probe methods for "stap" probes. */
102 class stap_static_probe_ops : public static_probe_ops
104 public:
105 /* See probe.h. */
106 bool is_linespec (const char **linespecp) const override;
108 /* See probe.h. */
109 void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
110 struct objfile *objfile) const override;
112 /* See probe.h. */
113 const char *type_name () const override;
115 /* See probe.h. */
116 std::vector<struct info_probe_column> gen_info_probes_table_header
117 () const override;
120 /* SystemTap static_probe_ops. */
122 const stap_static_probe_ops stap_static_probe_ops {};
124 class stap_probe : public probe
126 public:
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)
135 /* See probe.h. */
136 CORE_ADDR get_relocated_address (struct objfile *objfile) override;
138 /* See probe.h. */
139 unsigned get_argument_count (struct frame_info *frame) override;
141 /* See probe.h. */
142 bool can_evaluate_arguments () const override;
144 /* See probe.h. */
145 struct value *evaluate_argument (unsigned n,
146 struct frame_info *frame) override;
148 /* See probe.h. */
149 void compile_to_ax (struct agent_expr *aexpr,
150 struct axs_value *axs_value,
151 unsigned n) override;
153 /* See probe.h. */
154 void set_semaphore (struct objfile *objfile,
155 struct gdbarch *gdbarch) override;
157 /* See probe.h. */
158 void clear_semaphore (struct objfile *objfile,
159 struct gdbarch *gdbarch) override;
161 /* See probe.h. */
162 const static_probe_ops *get_static_ops () const override;
164 /* See probe.h. */
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"
190 "report this bug."),
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
199 in properly ways.
201 Consider the following argument string (x86 syntax):
203 `4@%eax 4@$10'
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);
210 private:
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
229 precedences.
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,
252 logical NOT). */
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, int has_lhs,
261 enum stap_operand_prec prec);
263 static void stap_parse_argument_conditionally (struct stap_parse_info *p);
265 /* Returns 1 if *S is an operator, zero otherwise. */
267 static int stap_is_operator (const char *op);
269 static void
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"),
274 value);
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)
283 switch (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;
291 case BINOP_ADD:
292 case BINOP_SUB:
293 case BINOP_EQUAL:
294 case BINOP_NOTEQUAL:
295 case BINOP_LESS:
296 case BINOP_LEQ:
297 case BINOP_GTR:
298 case BINOP_GEQ:
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;
307 case BINOP_MUL:
308 case BINOP_DIV:
309 case BINOP_REM:
310 case BINOP_LSH:
311 case BINOP_RSH:
312 return STAP_OPERAND_PREC_MUL;
314 default:
315 return STAP_OPERAND_PREC_NONE;
319 /* Given S, read the operator in it and fills the OP pointer with its code.
320 Return 1 on success, zero if the operator was not recognized. */
322 static enum exp_opcode
323 stap_get_opcode (const char **s)
325 const char c = **s;
326 enum exp_opcode op;
328 *s += 1;
330 switch (c)
332 case '*':
333 op = BINOP_MUL;
334 break;
336 case '/':
337 op = BINOP_DIV;
338 break;
340 case '%':
341 op = BINOP_REM;
342 break;
344 case '<':
345 op = BINOP_LESS;
346 if (**s == '<')
348 *s += 1;
349 op = BINOP_LSH;
351 else if (**s == '=')
353 *s += 1;
354 op = BINOP_LEQ;
356 else if (**s == '>')
358 *s += 1;
359 op = BINOP_NOTEQUAL;
361 break;
363 case '>':
364 op = BINOP_GTR;
365 if (**s == '>')
367 *s += 1;
368 op = BINOP_RSH;
370 else if (**s == '=')
372 *s += 1;
373 op = BINOP_GEQ;
375 break;
377 case '|':
378 op = BINOP_BITWISE_IOR;
379 if (**s == '|')
381 *s += 1;
382 op = BINOP_LOGICAL_OR;
384 break;
386 case '&':
387 op = BINOP_BITWISE_AND;
388 if (**s == '&')
390 *s += 1;
391 op = BINOP_LOGICAL_AND;
393 break;
395 case '^':
396 op = BINOP_BITWISE_XOR;
397 break;
399 case '!':
400 op = UNOP_LOGICAL_NOT;
401 break;
403 case '+':
404 op = BINOP_ADD;
405 break;
407 case '-':
408 op = BINOP_SUB;
409 break;
411 case '=':
412 gdb_assert (**s == '=');
413 op = BINOP_EQUAL;
414 break;
416 default:
417 error (_("Invalid opcode in expression `%s' for SystemTap"
418 "probe"), *s);
421 return op;
424 /* Given the bitness of the argument, represented by B, return the
425 corresponding `struct type *'. */
427 static struct type *
428 stap_get_expected_argument_type (struct gdbarch *gdbarch,
429 enum stap_arg_bitness b,
430 const char *probe_name)
432 switch (b)
434 case STAP_ARG_BITNESS_UNDEFINED:
435 if (gdbarch_addr_bit (gdbarch) == 32)
436 return builtin_type (gdbarch)->builtin_uint32;
437 else
438 return builtin_type (gdbarch)->builtin_uint64;
440 case STAP_ARG_BITNESS_8BIT_UNSIGNED:
441 return builtin_type (gdbarch)->builtin_uint8;
443 case STAP_ARG_BITNESS_8BIT_SIGNED:
444 return builtin_type (gdbarch)->builtin_int8;
446 case STAP_ARG_BITNESS_16BIT_UNSIGNED:
447 return builtin_type (gdbarch)->builtin_uint16;
449 case STAP_ARG_BITNESS_16BIT_SIGNED:
450 return builtin_type (gdbarch)->builtin_int16;
452 case STAP_ARG_BITNESS_32BIT_SIGNED:
453 return builtin_type (gdbarch)->builtin_int32;
455 case STAP_ARG_BITNESS_32BIT_UNSIGNED:
456 return builtin_type (gdbarch)->builtin_uint32;
458 case STAP_ARG_BITNESS_64BIT_SIGNED:
459 return builtin_type (gdbarch)->builtin_int64;
461 case STAP_ARG_BITNESS_64BIT_UNSIGNED:
462 return builtin_type (gdbarch)->builtin_uint64;
464 default:
465 error (_("Undefined bitness for probe '%s'."), probe_name);
466 break;
470 /* Helper function to check for a generic list of prefixes. GDBARCH
471 is the current gdbarch being used. S is the expression being
472 analyzed. If R is not NULL, it will be used to return the found
473 prefix. PREFIXES is the list of expected prefixes.
475 This function does a case-insensitive match.
477 Return 1 if any prefix has been found, zero otherwise. */
479 static int
480 stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s,
481 const char **r, const char *const *prefixes)
483 const char *const *p;
485 if (prefixes == NULL)
487 if (r != NULL)
488 *r = "";
490 return 1;
493 for (p = prefixes; *p != NULL; ++p)
494 if (strncasecmp (s, *p, strlen (*p)) == 0)
496 if (r != NULL)
497 *r = *p;
499 return 1;
502 return 0;
505 /* Return 1 if S points to a register prefix, zero otherwise. For a
506 description of the arguments, look at stap_is_generic_prefix. */
508 static int
509 stap_is_register_prefix (struct gdbarch *gdbarch, const char *s,
510 const char **r)
512 const char *const *t = gdbarch_stap_register_prefixes (gdbarch);
514 return stap_is_generic_prefix (gdbarch, s, r, t);
517 /* Return 1 if S points to a register indirection prefix, zero
518 otherwise. For a description of the arguments, look at
519 stap_is_generic_prefix. */
521 static int
522 stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s,
523 const char **r)
525 const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch);
527 return stap_is_generic_prefix (gdbarch, s, r, t);
530 /* Return 1 if S points to an integer prefix, zero otherwise. For a
531 description of the arguments, look at stap_is_generic_prefix.
533 This function takes care of analyzing whether we are dealing with
534 an expected integer prefix, or, if there is no integer prefix to be
535 expected, whether we are dealing with a digit. It does a
536 case-insensitive match. */
538 static int
539 stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s,
540 const char **r)
542 const char *const *t = gdbarch_stap_integer_prefixes (gdbarch);
543 const char *const *p;
545 if (t == NULL)
547 /* A NULL value here means that integers do not have a prefix.
548 We just check for a digit then. */
549 if (r != NULL)
550 *r = "";
552 return isdigit (*s);
555 for (p = t; *p != NULL; ++p)
557 size_t len = strlen (*p);
559 if ((len == 0 && isdigit (*s))
560 || (len > 0 && strncasecmp (s, *p, len) == 0))
562 /* Integers may or may not have a prefix. The "len == 0"
563 check covers the case when integers do not have a prefix
564 (therefore, we just check if we have a digit). The call
565 to "strncasecmp" covers the case when they have a
566 prefix. */
567 if (r != NULL)
568 *r = *p;
570 return 1;
574 return 0;
577 /* Helper function to check for a generic list of suffixes. If we are
578 not expecting any suffixes, then it just returns 1. If we are
579 expecting at least one suffix, then it returns 1 if a suffix has
580 been found, zero otherwise. GDBARCH is the current gdbarch being
581 used. S is the expression being analyzed. If R is not NULL, it
582 will be used to return the found suffix. SUFFIXES is the list of
583 expected suffixes. This function does a case-insensitive
584 match. */
586 static int
587 stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s,
588 const char **r, const char *const *suffixes)
590 const char *const *p;
591 int found = 0;
593 if (suffixes == NULL)
595 if (r != NULL)
596 *r = "";
598 return 1;
601 for (p = suffixes; *p != NULL; ++p)
602 if (strncasecmp (s, *p, strlen (*p)) == 0)
604 if (r != NULL)
605 *r = *p;
607 found = 1;
608 break;
611 return found;
614 /* Return 1 if S points to an integer suffix, zero otherwise. For a
615 description of the arguments, look at
616 stap_generic_check_suffix. */
618 static int
619 stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s,
620 const char **r)
622 const char *const *p = gdbarch_stap_integer_suffixes (gdbarch);
624 return stap_generic_check_suffix (gdbarch, s, r, p);
627 /* Return 1 if S points to a register suffix, zero otherwise. For a
628 description of the arguments, look at
629 stap_generic_check_suffix. */
631 static int
632 stap_check_register_suffix (struct gdbarch *gdbarch, const char *s,
633 const char **r)
635 const char *const *p = gdbarch_stap_register_suffixes (gdbarch);
637 return stap_generic_check_suffix (gdbarch, s, r, p);
640 /* Return 1 if S points to a register indirection suffix, zero
641 otherwise. For a description of the arguments, look at
642 stap_generic_check_suffix. */
644 static int
645 stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s,
646 const char **r)
648 const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch);
650 return stap_generic_check_suffix (gdbarch, s, r, p);
653 /* Function responsible for parsing a register operand according to
654 SystemTap parlance. Assuming:
656 RP = register prefix
657 RS = register suffix
658 RIP = register indirection prefix
659 RIS = register indirection suffix
661 Then a register operand can be:
663 [RIP] [RP] REGISTER [RS] [RIS]
665 This function takes care of a register's indirection, displacement and
666 direct access. It also takes into consideration the fact that some
667 registers are named differently inside and outside GDB, e.g., PPC's
668 general-purpose registers are represented by integers in the assembly
669 language (e.g., `15' is the 15th general-purpose register), but inside
670 GDB they have a prefix (the letter `r') appended. */
672 static void
673 stap_parse_register_operand (struct stap_parse_info *p)
675 /* Simple flag to indicate whether we have seen a minus signal before
676 certain number. */
677 int got_minus = 0;
678 /* Flags to indicate whether this register access is being displaced and/or
679 indirected. */
680 int disp_p = 0, indirect_p = 0;
681 struct gdbarch *gdbarch = p->gdbarch;
682 /* Needed to generate the register name as a part of an expression. */
683 struct stoken str;
684 /* Variables used to extract the register name from the probe's
685 argument. */
686 const char *start;
687 char *regname;
688 int len;
689 const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
690 int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0;
691 const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
692 int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0;
693 const char *reg_prefix;
694 const char *reg_ind_prefix;
695 const char *reg_suffix;
696 const char *reg_ind_suffix;
698 /* Checking for a displacement argument. */
699 if (*p->arg == '+')
701 /* If it's a plus sign, we don't need to do anything, just advance the
702 pointer. */
703 ++p->arg;
706 if (*p->arg == '-')
708 got_minus = 1;
709 ++p->arg;
712 if (isdigit (*p->arg))
714 /* The value of the displacement. */
715 long displacement;
716 char *endp;
718 disp_p = 1;
719 displacement = strtol (p->arg, &endp, 10);
720 p->arg = endp;
722 /* Generating the expression for the displacement. */
723 write_exp_elt_opcode (&p->pstate, OP_LONG);
724 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
725 write_exp_elt_longcst (&p->pstate, displacement);
726 write_exp_elt_opcode (&p->pstate, OP_LONG);
727 if (got_minus)
728 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
731 /* Getting rid of register indirection prefix. */
732 if (stap_is_register_indirection_prefix (gdbarch, p->arg, &reg_ind_prefix))
734 indirect_p = 1;
735 p->arg += strlen (reg_ind_prefix);
738 if (disp_p && !indirect_p)
739 error (_("Invalid register displacement syntax on expression `%s'."),
740 p->saved_arg);
742 /* Getting rid of register prefix. */
743 if (stap_is_register_prefix (gdbarch, p->arg, &reg_prefix))
744 p->arg += strlen (reg_prefix);
746 /* Now we should have only the register name. Let's extract it and get
747 the associated number. */
748 start = p->arg;
750 /* We assume the register name is composed by letters and numbers. */
751 while (isalnum (*p->arg))
752 ++p->arg;
754 len = p->arg - start;
756 regname = (char *) alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1);
757 regname[0] = '\0';
759 /* We only add the GDB's register prefix/suffix if we are dealing with
760 a numeric register. */
761 if (gdb_reg_prefix && isdigit (*start))
763 strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len);
764 strncpy (regname + gdb_reg_prefix_len, start, len);
766 if (gdb_reg_suffix)
767 strncpy (regname + gdb_reg_prefix_len + len,
768 gdb_reg_suffix, gdb_reg_suffix_len);
770 len += gdb_reg_prefix_len + gdb_reg_suffix_len;
772 else
773 strncpy (regname, start, len);
775 regname[len] = '\0';
777 /* Is this a valid register name? */
778 if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
779 error (_("Invalid register name `%s' on expression `%s'."),
780 regname, p->saved_arg);
782 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
783 str.ptr = regname;
784 str.length = len;
785 write_exp_string (&p->pstate, str);
786 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
788 if (indirect_p)
790 if (disp_p)
791 write_exp_elt_opcode (&p->pstate, BINOP_ADD);
793 /* Casting to the expected type. */
794 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
795 write_exp_elt_type (&p->pstate, lookup_pointer_type (p->arg_type));
796 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
798 write_exp_elt_opcode (&p->pstate, UNOP_IND);
801 /* Getting rid of the register name suffix. */
802 if (stap_check_register_suffix (gdbarch, p->arg, &reg_suffix))
803 p->arg += strlen (reg_suffix);
804 else
805 error (_("Missing register name suffix on expression `%s'."),
806 p->saved_arg);
808 /* Getting rid of the register indirection suffix. */
809 if (indirect_p)
811 if (stap_check_register_indirection_suffix (gdbarch, p->arg,
812 &reg_ind_suffix))
813 p->arg += strlen (reg_ind_suffix);
814 else
815 error (_("Missing indirection suffix on expression `%s'."),
816 p->saved_arg);
820 /* This function is responsible for parsing a single operand.
822 A single operand can be:
824 - an unary operation (e.g., `-5', `~2', or even with subexpressions
825 like `-(2 + 1)')
826 - a register displacement, which will be treated as a register
827 operand (e.g., `-4(%eax)' on x86)
828 - a numeric constant, or
829 - a register operand (see function `stap_parse_register_operand')
831 The function also calls special-handling functions to deal with
832 unrecognized operands, allowing arch-specific parsers to be
833 created. */
835 static void
836 stap_parse_single_operand (struct stap_parse_info *p)
838 struct gdbarch *gdbarch = p->gdbarch;
839 const char *int_prefix = NULL;
841 /* We first try to parse this token as a "special token". */
842 if (gdbarch_stap_parse_special_token_p (gdbarch))
843 if (gdbarch_stap_parse_special_token (gdbarch, p) != 0)
845 /* If the return value of the above function is not zero,
846 it means it successfully parsed the special token.
848 If it is NULL, we try to parse it using our method. */
849 return;
852 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
854 char c = *p->arg;
855 /* We use this variable to do a lookahead. */
856 const char *tmp = p->arg;
857 int has_digit = 0;
859 /* Skipping signal. */
860 ++tmp;
862 /* This is an unary operation. Here is a list of allowed tokens
863 here:
865 - numeric literal;
866 - number (from register displacement)
867 - subexpression (beginning with `(')
869 We handle the register displacement here, and the other cases
870 recursively. */
871 if (p->inside_paren_p)
872 tmp = skip_spaces (tmp);
874 while (isdigit (*tmp))
876 /* We skip the digit here because we are only interested in
877 knowing what kind of unary operation this is. The digit
878 will be handled by one of the functions that will be
879 called below ('stap_parse_argument_conditionally' or
880 'stap_parse_register_operand'). */
881 ++tmp;
882 has_digit = 1;
885 if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp,
886 NULL))
888 /* If we are here, it means it is a displacement. The only
889 operations allowed here are `-' and `+'. */
890 if (c == '~')
891 error (_("Invalid operator `%c' for register displacement "
892 "on expression `%s'."), c, p->saved_arg);
894 stap_parse_register_operand (p);
896 else
898 /* This is not a displacement. We skip the operator, and
899 deal with it when the recursion returns. */
900 ++p->arg;
901 stap_parse_argument_conditionally (p);
902 if (c == '-')
903 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
904 else if (c == '~')
905 write_exp_elt_opcode (&p->pstate, UNOP_COMPLEMENT);
908 else if (isdigit (*p->arg))
910 /* A temporary variable, needed for lookahead. */
911 const char *tmp = p->arg;
912 char *endp;
913 long number;
915 /* We can be dealing with a numeric constant, or with a register
916 displacement. */
917 number = strtol (tmp, &endp, 10);
918 tmp = endp;
920 if (p->inside_paren_p)
921 tmp = skip_spaces (tmp);
923 /* If "stap_is_integer_prefix" returns true, it means we can
924 accept integers without a prefix here. But we also need to
925 check whether the next token (i.e., "tmp") is not a register
926 indirection prefix. */
927 if (stap_is_integer_prefix (gdbarch, p->arg, NULL)
928 && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
930 const char *int_suffix;
932 /* We are dealing with a numeric constant. */
933 write_exp_elt_opcode (&p->pstate, OP_LONG);
934 write_exp_elt_type (&p->pstate,
935 builtin_type (gdbarch)->builtin_long);
936 write_exp_elt_longcst (&p->pstate, number);
937 write_exp_elt_opcode (&p->pstate, OP_LONG);
939 p->arg = tmp;
941 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
942 p->arg += strlen (int_suffix);
943 else
944 error (_("Invalid constant suffix on expression `%s'."),
945 p->saved_arg);
947 else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
948 stap_parse_register_operand (p);
949 else
950 error (_("Unknown numeric token on expression `%s'."),
951 p->saved_arg);
953 else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
955 /* We are dealing with a numeric constant. */
956 long number;
957 char *endp;
958 const char *int_suffix;
960 p->arg += strlen (int_prefix);
961 number = strtol (p->arg, &endp, 10);
962 p->arg = endp;
964 write_exp_elt_opcode (&p->pstate, OP_LONG);
965 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
966 write_exp_elt_longcst (&p->pstate, number);
967 write_exp_elt_opcode (&p->pstate, OP_LONG);
969 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
970 p->arg += strlen (int_suffix);
971 else
972 error (_("Invalid constant suffix on expression `%s'."),
973 p->saved_arg);
975 else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
976 || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
977 stap_parse_register_operand (p);
978 else
979 error (_("Operator `%c' not recognized on expression `%s'."),
980 *p->arg, p->saved_arg);
983 /* This function parses an argument conditionally, based on single or
984 non-single operands. A non-single operand would be a parenthesized
985 expression (e.g., `(2 + 1)'), and a single operand is anything that
986 starts with `-', `~', `+' (i.e., unary operators), a digit, or
987 something recognized by `gdbarch_stap_is_single_operand'. */
989 static void
990 stap_parse_argument_conditionally (struct stap_parse_info *p)
992 gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));
994 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary. */
995 || isdigit (*p->arg)
996 || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
997 stap_parse_single_operand (p);
998 else if (*p->arg == '(')
1000 /* We are dealing with a parenthesized operand. It means we
1001 have to parse it as it was a separate expression, without
1002 left-side or precedence. */
1003 ++p->arg;
1004 p->arg = skip_spaces (p->arg);
1005 ++p->inside_paren_p;
1007 stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
1009 --p->inside_paren_p;
1010 if (*p->arg != ')')
1011 error (_("Missign close-paren on expression `%s'."),
1012 p->saved_arg);
1014 ++p->arg;
1015 if (p->inside_paren_p)
1016 p->arg = skip_spaces (p->arg);
1018 else
1019 error (_("Cannot parse expression `%s'."), p->saved_arg);
1022 /* Helper function for `stap_parse_argument'. Please, see its comments to
1023 better understand what this function does. */
1025 static void
1026 stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
1027 enum stap_operand_prec prec)
1029 /* This is an operator-precedence parser.
1031 We work with left- and right-sides of expressions, and
1032 parse them depending on the precedence of the operators
1033 we find. */
1035 gdb_assert (p->arg != NULL);
1037 if (p->inside_paren_p)
1038 p->arg = skip_spaces (p->arg);
1040 if (!has_lhs)
1042 /* We were called without a left-side, either because this is the
1043 first call, or because we were called to parse a parenthesized
1044 expression. It doesn't really matter; we have to parse the
1045 left-side in order to continue the process. */
1046 stap_parse_argument_conditionally (p);
1049 /* Start to parse the right-side, and to "join" left and right sides
1050 depending on the operation specified.
1052 This loop shall continue until we run out of characters in the input,
1053 or until we find a close-parenthesis, which means that we've reached
1054 the end of a sub-expression. */
1055 while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
1057 const char *tmp_exp_buf;
1058 enum exp_opcode opcode;
1059 enum stap_operand_prec cur_prec;
1061 if (!stap_is_operator (p->arg))
1062 error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
1063 p->saved_arg);
1065 /* We have to save the current value of the expression buffer because
1066 the `stap_get_opcode' modifies it in order to get the current
1067 operator. If this operator's precedence is lower than PREC, we
1068 should return and not advance the expression buffer pointer. */
1069 tmp_exp_buf = p->arg;
1070 opcode = stap_get_opcode (&tmp_exp_buf);
1072 cur_prec = stap_get_operator_prec (opcode);
1073 if (cur_prec < prec)
1075 /* If the precedence of the operator that we are seeing now is
1076 lower than the precedence of the first operator seen before
1077 this parsing process began, it means we should stop parsing
1078 and return. */
1079 break;
1082 p->arg = tmp_exp_buf;
1083 if (p->inside_paren_p)
1084 p->arg = skip_spaces (p->arg);
1086 /* Parse the right-side of the expression. */
1087 stap_parse_argument_conditionally (p);
1089 /* While we still have operators, try to parse another
1090 right-side, but using the current right-side as a left-side. */
1091 while (*p->arg != '\0' && stap_is_operator (p->arg))
1093 enum exp_opcode lookahead_opcode;
1094 enum stap_operand_prec lookahead_prec;
1096 /* Saving the current expression buffer position. The explanation
1097 is the same as above. */
1098 tmp_exp_buf = p->arg;
1099 lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
1100 lookahead_prec = stap_get_operator_prec (lookahead_opcode);
1102 if (lookahead_prec <= prec)
1104 /* If we are dealing with an operator whose precedence is lower
1105 than the first one, just abandon the attempt. */
1106 break;
1109 /* Parse the right-side of the expression, but since we already
1110 have a left-side at this point, set `has_lhs' to 1. */
1111 stap_parse_argument_1 (p, 1, lookahead_prec);
1114 write_exp_elt_opcode (&p->pstate, opcode);
1118 /* Parse a probe's argument.
1120 Assuming that:
1122 LP = literal integer prefix
1123 LS = literal integer suffix
1125 RP = register prefix
1126 RS = register suffix
1128 RIP = register indirection prefix
1129 RIS = register indirection suffix
1131 This routine assumes that arguments' tokens are of the form:
1133 - [LP] NUMBER [LS]
1134 - [RP] REGISTER [RS]
1135 - [RIP] [RP] REGISTER [RS] [RIS]
1136 - If we find a number without LP, we try to parse it as a literal integer
1137 constant (if LP == NULL), or as a register displacement.
1138 - We count parenthesis, and only skip whitespaces if we are inside them.
1139 - If we find an operator, we skip it.
1141 This function can also call a special function that will try to match
1142 unknown tokens. It will return the expression_up generated from
1143 parsing the argument. */
1145 static expression_up
1146 stap_parse_argument (const char **arg, struct type *atype,
1147 struct gdbarch *gdbarch)
1149 /* We need to initialize the expression buffer, in order to begin
1150 our parsing efforts. We use language_c here because we may need
1151 to do pointer arithmetics. */
1152 struct stap_parse_info p (*arg, atype, language_def (language_c),
1153 gdbarch);
1155 stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
1157 gdb_assert (p.inside_paren_p == 0);
1159 /* Casting the final expression to the appropriate type. */
1160 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
1161 write_exp_elt_type (&p.pstate, atype);
1162 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
1164 p.arg = skip_spaces (p.arg);
1165 *arg = p.arg;
1167 return p.pstate.release ();
1170 /* Implementation of 'parse_arguments' method. */
1172 void
1173 stap_probe::parse_arguments (struct gdbarch *gdbarch)
1175 const char *cur;
1177 gdb_assert (!m_have_parsed_args);
1178 cur = m_unparsed_args_text;
1179 m_have_parsed_args = true;
1181 if (cur == NULL || *cur == '\0' || *cur == ':')
1182 return;
1184 while (*cur != '\0')
1186 enum stap_arg_bitness bitness;
1187 bool got_minus = false;
1189 /* We expect to find something like:
1191 N@OP
1193 Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so
1194 we check it here. If we don't find it, go to the next
1195 state. */
1196 if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@')
1197 || (isdigit (cur[0]) && cur[1] == '@'))
1199 if (*cur == '-')
1201 /* Discard the `-'. */
1202 ++cur;
1203 got_minus = true;
1206 /* Defining the bitness. */
1207 switch (*cur)
1209 case '1':
1210 bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED
1211 : STAP_ARG_BITNESS_8BIT_UNSIGNED);
1212 break;
1214 case '2':
1215 bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED
1216 : STAP_ARG_BITNESS_16BIT_UNSIGNED);
1217 break;
1219 case '4':
1220 bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
1221 : STAP_ARG_BITNESS_32BIT_UNSIGNED);
1222 break;
1224 case '8':
1225 bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
1226 : STAP_ARG_BITNESS_64BIT_UNSIGNED);
1227 break;
1229 default:
1231 /* We have an error, because we don't expect anything
1232 except 1, 2, 4 and 8. */
1233 warning (_("unrecognized bitness %s%c' for probe `%s'"),
1234 got_minus ? "`-" : "`", *cur,
1235 this->get_name ().c_str ());
1236 return;
1239 /* Discard the number and the `@' sign. */
1240 cur += 2;
1242 else
1243 bitness = STAP_ARG_BITNESS_UNDEFINED;
1245 struct type *atype
1246 = stap_get_expected_argument_type (gdbarch, bitness,
1247 this->get_name ().c_str ());
1249 expression_up expr = stap_parse_argument (&cur, atype, gdbarch);
1251 if (stap_expression_debug)
1252 dump_raw_expression (expr.get (), gdb_stdlog,
1253 "before conversion to prefix form");
1255 prefixify_expression (expr.get ());
1257 if (stap_expression_debug)
1258 dump_prefix_expression (expr.get (), gdb_stdlog);
1260 m_parsed_args.emplace_back (bitness, atype, std::move (expr));
1262 /* Start it over again. */
1263 cur = skip_spaces (cur);
1267 /* Helper function to relocate an address. */
1269 static CORE_ADDR
1270 relocate_address (CORE_ADDR address, struct objfile *objfile)
1272 return address + ANOFFSET (objfile->section_offsets,
1273 SECT_OFF_DATA (objfile));
1276 /* Implementation of the get_relocated_address method. */
1278 CORE_ADDR
1279 stap_probe::get_relocated_address (struct objfile *objfile)
1281 return relocate_address (this->get_address (), objfile);
1284 /* Given PROBE, returns the number of arguments present in that probe's
1285 argument string. */
1287 unsigned
1288 stap_probe::get_argument_count (struct frame_info *frame)
1290 struct gdbarch *gdbarch = get_frame_arch (frame);
1292 if (!m_have_parsed_args)
1294 if (this->can_evaluate_arguments ())
1295 this->parse_arguments (gdbarch);
1296 else
1298 static int have_warned_stap_incomplete = 0;
1300 if (!have_warned_stap_incomplete)
1302 warning (_(
1303 "The SystemTap SDT probe support is not fully implemented on this target;\n"
1304 "you will not be able to inspect the arguments of the probes.\n"
1305 "Please report a bug against GDB requesting a port to this target."));
1306 have_warned_stap_incomplete = 1;
1309 /* Marking the arguments as "already parsed". */
1310 m_have_parsed_args = true;
1314 gdb_assert (m_have_parsed_args);
1315 return m_parsed_args.size ();
1318 /* Return 1 if OP is a valid operator inside a probe argument, or zero
1319 otherwise. */
1321 static int
1322 stap_is_operator (const char *op)
1324 int ret = 1;
1326 switch (*op)
1328 case '*':
1329 case '/':
1330 case '%':
1331 case '^':
1332 case '!':
1333 case '+':
1334 case '-':
1335 case '<':
1336 case '>':
1337 case '|':
1338 case '&':
1339 break;
1341 case '=':
1342 if (op[1] != '=')
1343 ret = 0;
1344 break;
1346 default:
1347 /* We didn't find any operator. */
1348 ret = 0;
1351 return ret;
1354 /* Implement the `can_evaluate_arguments' method. */
1356 bool
1357 stap_probe::can_evaluate_arguments () const
1359 struct gdbarch *gdbarch = this->get_gdbarch ();
1361 /* For SystemTap probes, we have to guarantee that the method
1362 stap_is_single_operand is defined on gdbarch. If it is not, then it
1363 means that argument evaluation is not implemented on this target. */
1364 return gdbarch_stap_is_single_operand_p (gdbarch);
1367 /* Evaluate the probe's argument N (indexed from 0), returning a value
1368 corresponding to it. Assertion is thrown if N does not exist. */
1370 struct value *
1371 stap_probe::evaluate_argument (unsigned n, struct frame_info *frame)
1373 struct stap_probe_arg *arg;
1374 int pos = 0;
1375 struct gdbarch *gdbarch = get_frame_arch (frame);
1377 arg = this->get_arg_by_number (n, gdbarch);
1378 return evaluate_subexp_standard (arg->atype, arg->aexpr.get (), &pos,
1379 EVAL_NORMAL);
1382 /* Compile the probe's argument N (indexed from 0) to agent expression.
1383 Assertion is thrown if N does not exist. */
1385 void
1386 stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value,
1387 unsigned n)
1389 struct stap_probe_arg *arg;
1390 union exp_element *pc;
1392 arg = this->get_arg_by_number (n, expr->gdbarch);
1394 pc = arg->aexpr->elts;
1395 gen_expr (arg->aexpr.get (), &pc, expr, value);
1397 require_rvalue (expr, value);
1398 value->type = arg->atype;
1402 /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
1403 address. SET is zero if the semaphore should be cleared, or one if
1404 it should be set. This is a helper function for
1405 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */
1407 static void
1408 stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1410 gdb_byte bytes[sizeof (LONGEST)];
1411 /* The ABI specifies "unsigned short". */
1412 struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1413 ULONGEST value;
1415 if (address == 0)
1416 return;
1418 /* Swallow errors. */
1419 if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1421 warning (_("Could not read the value of a SystemTap semaphore."));
1422 return;
1425 value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
1426 gdbarch_byte_order (gdbarch));
1427 /* Note that we explicitly don't worry about overflow or
1428 underflow. */
1429 if (set)
1430 ++value;
1431 else
1432 --value;
1434 store_unsigned_integer (bytes, TYPE_LENGTH (type),
1435 gdbarch_byte_order (gdbarch), value);
1437 if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1438 warning (_("Could not write the value of a SystemTap semaphore."));
1441 /* Implementation of the 'set_semaphore' method.
1443 SystemTap semaphores act as reference counters, so calls to this
1444 function must be paired with calls to 'clear_semaphore'.
1446 This function and 'clear_semaphore' race with another tool
1447 changing the probes, but that is too rare to care. */
1449 void
1450 stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1452 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
1455 /* Implementation of the 'clear_semaphore' method. */
1457 void
1458 stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1460 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
1463 /* Implementation of the 'get_static_ops' method. */
1465 const static_probe_ops *
1466 stap_probe::get_static_ops () const
1468 return &stap_static_probe_ops;
1471 /* Implementation of the 'gen_info_probes_table_values' method. */
1473 std::vector<const char *>
1474 stap_probe::gen_info_probes_table_values () const
1476 const char *val = NULL;
1478 if (m_sem_addr != 0)
1479 val = print_core_address (this->get_gdbarch (), m_sem_addr);
1481 return std::vector<const char *> { val };
1484 /* Helper function that parses the information contained in a
1485 SystemTap's probe. Basically, the information consists in:
1487 - Probe's PC address;
1488 - Link-time section address of `.stapsdt.base' section;
1489 - Link-time address of the semaphore variable, or ZERO if the
1490 probe doesn't have an associated semaphore;
1491 - Probe's provider name;
1492 - Probe's name;
1493 - Probe's argument format
1495 This function returns 1 if the handling was successful, and zero
1496 otherwise. */
1498 static void
1499 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
1500 std::vector<std::unique_ptr<probe>> *probesp,
1501 CORE_ADDR base)
1503 bfd *abfd = objfile->obfd;
1504 int size = bfd_get_arch_size (abfd) / 8;
1505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1506 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1508 /* Provider and the name of the probe. */
1509 const char *provider = (const char *) &el->data[3 * size];
1510 const char *name = ((const char *)
1511 memchr (provider, '\0',
1512 (char *) el->data + el->size - provider));
1513 /* Making sure there is a name. */
1514 if (name == NULL)
1516 complaint (_("corrupt probe name when "
1517 "reading `%s'"),
1518 objfile_name (objfile));
1520 /* There is no way to use a probe without a name or a provider, so
1521 returning zero here makes sense. */
1522 return;
1524 else
1525 ++name;
1527 /* Retrieving the probe's address. */
1528 CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type);
1530 /* Link-time sh_addr of `.stapsdt.base' section. */
1531 CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type);
1533 /* Semaphore address. */
1534 CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
1536 address += base - base_ref;
1537 if (sem_addr != 0)
1538 sem_addr += base - base_ref;
1540 /* Arguments. We can only extract the argument format if there is a valid
1541 name for this probe. */
1542 const char *probe_args = ((const char*)
1543 memchr (name, '\0',
1544 (char *) el->data + el->size - name));
1546 if (probe_args != NULL)
1547 ++probe_args;
1549 if (probe_args == NULL
1550 || (memchr (probe_args, '\0', (char *) el->data + el->size - name)
1551 != el->data + el->size - 1))
1553 complaint (_("corrupt probe argument when "
1554 "reading `%s'"),
1555 objfile_name (objfile));
1556 /* If the argument string is NULL, it means some problem happened with
1557 it. So we return 0. */
1558 return;
1561 stap_probe *ret = new stap_probe (std::string (name), std::string (provider),
1562 address, gdbarch, sem_addr, probe_args);
1564 /* Successfully created probe. */
1565 probesp->emplace_back (ret);
1568 /* Helper function which tries to find the base address of the SystemTap
1569 base section named STAP_BASE_SECTION_NAME. */
1571 static void
1572 get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
1574 asection **ret = (asection **) obj;
1576 if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1577 && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1578 *ret = sect;
1581 /* Helper function which iterates over every section in the BFD file,
1582 trying to find the base address of the SystemTap base section.
1583 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1585 static int
1586 get_stap_base_address (bfd *obfd, bfd_vma *base)
1588 asection *ret = NULL;
1590 bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);
1592 if (ret == NULL)
1594 complaint (_("could not obtain base address for "
1595 "SystemTap section on objfile `%s'."),
1596 obfd->filename);
1597 return 0;
1600 if (base != NULL)
1601 *base = ret->vma;
1603 return 1;
1606 /* Implementation of the 'is_linespec' method. */
1608 bool
1609 stap_static_probe_ops::is_linespec (const char **linespecp) const
1611 static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1613 return probe_is_linespec_by_keyword (linespecp, keywords);
1616 /* Implementation of the 'get_probes' method. */
1618 void
1619 stap_static_probe_ops::get_probes
1620 (std::vector<std::unique_ptr<probe>> *probesp,
1621 struct objfile *objfile) const
1623 /* If we are here, then this is the first time we are parsing the
1624 SystemTap probe's information. We basically have to count how many
1625 probes the objfile has, and then fill in the necessary information
1626 for each one. */
1627 bfd *obfd = objfile->obfd;
1628 bfd_vma base;
1629 struct sdt_note *iter;
1630 unsigned save_probesp_len = probesp->size ();
1632 if (objfile->separate_debug_objfile_backlink != NULL)
1634 /* This is a .debug file, not the objfile itself. */
1635 return;
1638 if (elf_tdata (obfd)->sdt_note_head == NULL)
1640 /* There isn't any probe here. */
1641 return;
1644 if (!get_stap_base_address (obfd, &base))
1646 /* There was an error finding the base address for the section.
1647 Just return NULL. */
1648 return;
1651 /* Parsing each probe's information. */
1652 for (iter = elf_tdata (obfd)->sdt_note_head;
1653 iter != NULL;
1654 iter = iter->next)
1656 /* We first have to handle all the information about the
1657 probe which is present in the section. */
1658 handle_stap_probe (objfile, iter, probesp, base);
1661 if (save_probesp_len == probesp->size ())
1663 /* If we are here, it means we have failed to parse every known
1664 probe. */
1665 complaint (_("could not parse SystemTap probe(s) "
1666 "from inferior"));
1667 return;
1671 /* Implementation of the type_name method. */
1673 const char *
1674 stap_static_probe_ops::type_name () const
1676 return "stap";
1679 /* Implementation of the 'gen_info_probes_table_header' method. */
1681 std::vector<struct info_probe_column>
1682 stap_static_probe_ops::gen_info_probes_table_header () const
1684 struct info_probe_column stap_probe_column;
1686 stap_probe_column.field_name = "semaphore";
1687 stap_probe_column.print_name = _("Semaphore");
1689 return std::vector<struct info_probe_column> { stap_probe_column };
1692 /* Implementation of the `info probes stap' command. */
1694 static void
1695 info_probes_stap_command (const char *arg, int from_tty)
1697 info_probes_for_spops (arg, from_tty, &stap_static_probe_ops);
1700 void
1701 _initialize_stap_probe (void)
1703 all_static_probe_ops.push_back (&stap_static_probe_ops);
1705 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
1706 &stap_expression_debug,
1707 _("Set SystemTap expression debugging."),
1708 _("Show SystemTap expression debugging."),
1709 _("When non-zero, the internal representation "
1710 "of SystemTap expressions will be printed."),
1711 NULL,
1712 show_stapexpressiondebug,
1713 &setdebuglist, &showdebuglist);
1715 add_cmd ("stap", class_info, info_probes_stap_command,
1716 _("\
1717 Show information about SystemTap static probes.\n\
1718 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1719 Each argument is a regular expression, used to select probes.\n\
1720 PROVIDER matches probe provider names.\n\
1721 NAME matches the probe names.\n\
1722 OBJECT matches the executable or shared library name."),
1723 info_probes_cmdlist_get ());