Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / stap-probe.c
blob0b66554efae1acd9e68344584a0e9681084fb7c1
1 /* SystemTap probe support for GDB.
3 Copyright (C) 2012-2024 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 "stap-probe.h"
21 #include "probe.h"
22 #include "ui-out.h"
23 #include "objfiles.h"
24 #include "arch-utils.h"
25 #include "command.h"
26 #include "gdbcmd.h"
27 #include "filenames.h"
28 #include "value.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "complaints.h"
32 #include "cli/cli-utils.h"
33 #include "linespec.h"
34 #include "user-regs.h"
35 #include "parser-defs.h"
36 #include "language.h"
37 #include "elf-bfd.h"
38 #include "expop.h"
39 #include <unordered_map>
40 #include "gdbsupport/hash_enum.h"
42 #include <ctype.h>
44 /* The name of the SystemTap section where we will find information about
45 the probes. */
47 #define STAP_BASE_SECTION_NAME ".stapsdt.base"
49 /* Should we display debug information for the probe's argument expression
50 parsing? */
52 static unsigned int stap_expression_debug = 0;
54 /* The various possibilities of bitness defined for a probe's argument.
56 The relationship is:
58 - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness.
59 - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'.
60 - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'.
61 - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'.
62 - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'.
63 - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'.
64 - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'.
65 - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'.
66 - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */
68 enum stap_arg_bitness
70 STAP_ARG_BITNESS_UNDEFINED,
71 STAP_ARG_BITNESS_8BIT_UNSIGNED,
72 STAP_ARG_BITNESS_8BIT_SIGNED,
73 STAP_ARG_BITNESS_16BIT_UNSIGNED,
74 STAP_ARG_BITNESS_16BIT_SIGNED,
75 STAP_ARG_BITNESS_32BIT_UNSIGNED,
76 STAP_ARG_BITNESS_32BIT_SIGNED,
77 STAP_ARG_BITNESS_64BIT_UNSIGNED,
78 STAP_ARG_BITNESS_64BIT_SIGNED,
81 /* The following structure represents a single argument for the probe. */
83 struct stap_probe_arg
85 /* Constructor for stap_probe_arg. */
86 stap_probe_arg (enum stap_arg_bitness bitness_, struct type *atype_,
87 expression_up &&aexpr_)
88 : bitness (bitness_), atype (atype_), aexpr (std::move (aexpr_))
91 /* The bitness of this argument. */
92 enum stap_arg_bitness bitness;
94 /* The corresponding `struct type *' to the bitness. */
95 struct type *atype;
97 /* The argument converted to an internal GDB expression. */
98 expression_up aexpr;
101 /* Class that implements the static probe methods for "stap" probes. */
103 class stap_static_probe_ops : public static_probe_ops
105 public:
106 /* We need a user-provided constructor to placate some compilers.
107 See PR build/24937. */
108 stap_static_probe_ops ()
112 /* See probe.h. */
113 bool is_linespec (const char **linespecp) const override;
115 /* See probe.h. */
116 void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
117 struct objfile *objfile) const override;
119 /* See probe.h. */
120 const char *type_name () const override;
122 /* See probe.h. */
123 std::vector<struct info_probe_column> gen_info_probes_table_header
124 () const override;
127 /* SystemTap static_probe_ops. */
129 const stap_static_probe_ops stap_static_probe_ops {};
131 class stap_probe : public probe
133 public:
134 /* Constructor for stap_probe. */
135 stap_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_,
136 struct gdbarch *arch_, CORE_ADDR sem_addr, const char *args_text)
137 : probe (std::move (name_), std::move (provider_), address_, arch_),
138 m_sem_addr (sem_addr),
139 m_have_parsed_args (false), m_unparsed_args_text (args_text)
142 /* See probe.h. */
143 CORE_ADDR get_relocated_address (struct objfile *objfile) override;
145 /* See probe.h. */
146 unsigned get_argument_count (struct gdbarch *gdbarch) override;
148 /* See probe.h. */
149 bool can_evaluate_arguments () const override;
151 /* See probe.h. */
152 struct value *evaluate_argument (unsigned n,
153 const frame_info_ptr &frame) override;
155 /* See probe.h. */
156 void compile_to_ax (struct agent_expr *aexpr,
157 struct axs_value *axs_value,
158 unsigned n) override;
160 /* See probe.h. */
161 void set_semaphore (struct objfile *objfile,
162 struct gdbarch *gdbarch) override;
164 /* See probe.h. */
165 void clear_semaphore (struct objfile *objfile,
166 struct gdbarch *gdbarch) override;
168 /* See probe.h. */
169 const static_probe_ops *get_static_ops () const override;
171 /* See probe.h. */
172 std::vector<const char *> gen_info_probes_table_values () const override;
174 /* Return argument N of probe.
176 If the probe's arguments have not been parsed yet, parse them. If
177 there are no arguments, throw an exception (error). Otherwise,
178 return the requested argument. */
179 struct stap_probe_arg *get_arg_by_number (unsigned n,
180 struct gdbarch *gdbarch)
182 if (!m_have_parsed_args)
183 this->parse_arguments (gdbarch);
185 gdb_assert (m_have_parsed_args);
186 if (m_parsed_args.empty ())
187 internal_error (_("Probe '%s' apparently does not have arguments, but \n"
188 "GDB is requesting its argument number %u anyway. "
189 "This should not happen. Please report this bug."),
190 this->get_name ().c_str (), n);
192 if (n > m_parsed_args.size ())
193 internal_error (_("Probe '%s' has %d arguments, but GDB is requesting\n"
194 "argument %u. This should not happen. Please\n"
195 "report this bug."),
196 this->get_name ().c_str (),
197 (int) m_parsed_args.size (), n);
199 return &m_parsed_args[n];
202 /* Function which parses an argument string from the probe,
203 correctly splitting the arguments and storing their information
204 in properly ways.
206 Consider the following argument string (x86 syntax):
208 `4@%eax 4@$10'
210 We have two arguments, `%eax' and `$10', both with 32-bit
211 unsigned bitness. This function basically handles them, properly
212 filling some structures with this information. */
213 void parse_arguments (struct gdbarch *gdbarch);
215 private:
216 /* If the probe has a semaphore associated, then this is the value of
217 it, relative to SECT_OFF_DATA. */
218 CORE_ADDR m_sem_addr;
220 /* True if the arguments have been parsed. */
221 bool m_have_parsed_args;
223 /* The text version of the probe's arguments, unparsed. */
224 const char *m_unparsed_args_text;
226 /* Information about each argument. This is an array of `stap_probe_arg',
227 with each entry representing one argument. This is only valid if
228 M_ARGS_PARSED is true. */
229 std::vector<struct stap_probe_arg> m_parsed_args;
232 /* When parsing the arguments, we have to establish different precedences
233 for the various kinds of asm operators. This enumeration represents those
234 precedences.
236 This logic behind this is available at
237 <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using
238 the command "info '(as)Infix Ops'". */
240 enum stap_operand_prec
242 /* Lowest precedence, used for non-recognized operands or for the beginning
243 of the parsing process. */
244 STAP_OPERAND_PREC_NONE = 0,
246 /* Precedence of logical OR. */
247 STAP_OPERAND_PREC_LOGICAL_OR,
249 /* Precedence of logical AND. */
250 STAP_OPERAND_PREC_LOGICAL_AND,
252 /* Precedence of additive (plus, minus) and comparative (equal, less,
253 greater-than, etc) operands. */
254 STAP_OPERAND_PREC_ADD_CMP,
256 /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
257 logical NOT). */
258 STAP_OPERAND_PREC_BITWISE,
260 /* Precedence of multiplicative operands (multiplication, division,
261 remainder, left shift and right shift). */
262 STAP_OPERAND_PREC_MUL
265 static expr::operation_up stap_parse_argument_1 (struct stap_parse_info *p,
266 expr::operation_up &&lhs,
267 enum stap_operand_prec prec)
268 ATTRIBUTE_UNUSED_RESULT;
270 static expr::operation_up stap_parse_argument_conditionally
271 (struct stap_parse_info *p) ATTRIBUTE_UNUSED_RESULT;
273 /* Returns true if *S is an operator, false otherwise. */
275 static bool stap_is_operator (const char *op);
277 static void
278 show_stapexpressiondebug (struct ui_file *file, int from_tty,
279 struct cmd_list_element *c, const char *value)
281 gdb_printf (file, _("SystemTap Probe expression debugging is %s.\n"),
282 value);
285 /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
286 if the operator code was not recognized. */
288 static enum stap_operand_prec
289 stap_get_operator_prec (enum exp_opcode op)
291 switch (op)
293 case BINOP_LOGICAL_OR:
294 return STAP_OPERAND_PREC_LOGICAL_OR;
296 case BINOP_LOGICAL_AND:
297 return STAP_OPERAND_PREC_LOGICAL_AND;
299 case BINOP_ADD:
300 case BINOP_SUB:
301 case BINOP_EQUAL:
302 case BINOP_NOTEQUAL:
303 case BINOP_LESS:
304 case BINOP_LEQ:
305 case BINOP_GTR:
306 case BINOP_GEQ:
307 return STAP_OPERAND_PREC_ADD_CMP;
309 case BINOP_BITWISE_IOR:
310 case BINOP_BITWISE_AND:
311 case BINOP_BITWISE_XOR:
312 case UNOP_LOGICAL_NOT:
313 return STAP_OPERAND_PREC_BITWISE;
315 case BINOP_MUL:
316 case BINOP_DIV:
317 case BINOP_REM:
318 case BINOP_LSH:
319 case BINOP_RSH:
320 return STAP_OPERAND_PREC_MUL;
322 default:
323 return STAP_OPERAND_PREC_NONE;
327 /* Given S, read the operator in it. Return the EXP_OPCODE which
328 represents the operator detected, or throw an error if no operator
329 was found. */
331 static enum exp_opcode
332 stap_get_opcode (const char **s)
334 const char c = **s;
335 enum exp_opcode op;
337 *s += 1;
339 switch (c)
341 case '*':
342 op = BINOP_MUL;
343 break;
345 case '/':
346 op = BINOP_DIV;
347 break;
349 case '%':
350 op = BINOP_REM;
351 break;
353 case '<':
354 op = BINOP_LESS;
355 if (**s == '<')
357 *s += 1;
358 op = BINOP_LSH;
360 else if (**s == '=')
362 *s += 1;
363 op = BINOP_LEQ;
365 else if (**s == '>')
367 *s += 1;
368 op = BINOP_NOTEQUAL;
370 break;
372 case '>':
373 op = BINOP_GTR;
374 if (**s == '>')
376 *s += 1;
377 op = BINOP_RSH;
379 else if (**s == '=')
381 *s += 1;
382 op = BINOP_GEQ;
384 break;
386 case '|':
387 op = BINOP_BITWISE_IOR;
388 if (**s == '|')
390 *s += 1;
391 op = BINOP_LOGICAL_OR;
393 break;
395 case '&':
396 op = BINOP_BITWISE_AND;
397 if (**s == '&')
399 *s += 1;
400 op = BINOP_LOGICAL_AND;
402 break;
404 case '^':
405 op = BINOP_BITWISE_XOR;
406 break;
408 case '!':
409 op = UNOP_LOGICAL_NOT;
410 break;
412 case '+':
413 op = BINOP_ADD;
414 break;
416 case '-':
417 op = BINOP_SUB;
418 break;
420 case '=':
421 gdb_assert (**s == '=');
422 op = BINOP_EQUAL;
423 break;
425 default:
426 error (_("Invalid opcode in expression `%s' for SystemTap"
427 "probe"), *s);
430 return op;
433 typedef expr::operation_up binop_maker_ftype (expr::operation_up &&,
434 expr::operation_up &&);
435 /* Map from an expression opcode to a function that can create a
436 binary operation of that type. */
437 static std::unordered_map<exp_opcode, binop_maker_ftype *,
438 gdb::hash_enum<exp_opcode>> stap_maker_map;
440 /* Helper function to create a binary operation. */
441 static expr::operation_up
442 stap_make_binop (enum exp_opcode opcode, expr::operation_up &&lhs,
443 expr::operation_up &&rhs)
445 auto iter = stap_maker_map.find (opcode);
446 gdb_assert (iter != stap_maker_map.end ());
447 return iter->second (std::move (lhs), std::move (rhs));
450 /* Given the bitness of the argument, represented by B, return the
451 corresponding `struct type *', or throw an error if B is
452 unknown. */
454 static struct type *
455 stap_get_expected_argument_type (struct gdbarch *gdbarch,
456 enum stap_arg_bitness b,
457 const char *probe_name)
459 switch (b)
461 case STAP_ARG_BITNESS_UNDEFINED:
462 if (gdbarch_addr_bit (gdbarch) == 32)
463 return builtin_type (gdbarch)->builtin_uint32;
464 else
465 return builtin_type (gdbarch)->builtin_uint64;
467 case STAP_ARG_BITNESS_8BIT_UNSIGNED:
468 return builtin_type (gdbarch)->builtin_uint8;
470 case STAP_ARG_BITNESS_8BIT_SIGNED:
471 return builtin_type (gdbarch)->builtin_int8;
473 case STAP_ARG_BITNESS_16BIT_UNSIGNED:
474 return builtin_type (gdbarch)->builtin_uint16;
476 case STAP_ARG_BITNESS_16BIT_SIGNED:
477 return builtin_type (gdbarch)->builtin_int16;
479 case STAP_ARG_BITNESS_32BIT_SIGNED:
480 return builtin_type (gdbarch)->builtin_int32;
482 case STAP_ARG_BITNESS_32BIT_UNSIGNED:
483 return builtin_type (gdbarch)->builtin_uint32;
485 case STAP_ARG_BITNESS_64BIT_SIGNED:
486 return builtin_type (gdbarch)->builtin_int64;
488 case STAP_ARG_BITNESS_64BIT_UNSIGNED:
489 return builtin_type (gdbarch)->builtin_uint64;
491 default:
492 error (_("Undefined bitness for probe '%s'."), probe_name);
493 break;
497 /* Helper function to check for a generic list of prefixes. GDBARCH
498 is the current gdbarch being used. S is the expression being
499 analyzed. If R is not NULL, it will be used to return the found
500 prefix. PREFIXES is the list of expected prefixes.
502 This function does a case-insensitive match.
504 Return true if any prefix has been found, false otherwise. */
506 static bool
507 stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s,
508 const char **r, const char *const *prefixes)
510 const char *const *p;
512 if (prefixes == NULL)
514 if (r != NULL)
515 *r = "";
517 return true;
520 for (p = prefixes; *p != NULL; ++p)
521 if (strncasecmp (s, *p, strlen (*p)) == 0)
523 if (r != NULL)
524 *r = *p;
526 return true;
529 return false;
532 /* Return true if S points to a register prefix, false otherwise. For
533 a description of the arguments, look at stap_is_generic_prefix. */
535 static bool
536 stap_is_register_prefix (struct gdbarch *gdbarch, const char *s,
537 const char **r)
539 const char *const *t = gdbarch_stap_register_prefixes (gdbarch);
541 return stap_is_generic_prefix (gdbarch, s, r, t);
544 /* Return true if S points to a register indirection prefix, false
545 otherwise. For a description of the arguments, look at
546 stap_is_generic_prefix. */
548 static bool
549 stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s,
550 const char **r)
552 const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch);
554 return stap_is_generic_prefix (gdbarch, s, r, t);
557 /* Return true if S points to an integer prefix, false otherwise. For
558 a description of the arguments, look at stap_is_generic_prefix.
560 This function takes care of analyzing whether we are dealing with
561 an expected integer prefix, or, if there is no integer prefix to be
562 expected, whether we are dealing with a digit. It does a
563 case-insensitive match. */
565 static bool
566 stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s,
567 const char **r)
569 const char *const *t = gdbarch_stap_integer_prefixes (gdbarch);
570 const char *const *p;
572 if (t == NULL)
574 /* A NULL value here means that integers do not have a prefix.
575 We just check for a digit then. */
576 if (r != NULL)
577 *r = "";
579 return isdigit (*s) > 0;
582 for (p = t; *p != NULL; ++p)
584 size_t len = strlen (*p);
586 if ((len == 0 && isdigit (*s))
587 || (len > 0 && strncasecmp (s, *p, len) == 0))
589 /* Integers may or may not have a prefix. The "len == 0"
590 check covers the case when integers do not have a prefix
591 (therefore, we just check if we have a digit). The call
592 to "strncasecmp" covers the case when they have a
593 prefix. */
594 if (r != NULL)
595 *r = *p;
597 return true;
601 return false;
604 /* Helper function to check for a generic list of suffixes. If we are
605 not expecting any suffixes, then it just returns 1. If we are
606 expecting at least one suffix, then it returns true if a suffix has
607 been found, false otherwise. GDBARCH is the current gdbarch being
608 used. S is the expression being analyzed. If R is not NULL, it
609 will be used to return the found suffix. SUFFIXES is the list of
610 expected suffixes. This function does a case-insensitive
611 match. */
613 static bool
614 stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s,
615 const char **r, const char *const *suffixes)
617 const char *const *p;
618 bool found = false;
620 if (suffixes == NULL)
622 if (r != NULL)
623 *r = "";
625 return true;
628 for (p = suffixes; *p != NULL; ++p)
629 if (strncasecmp (s, *p, strlen (*p)) == 0)
631 if (r != NULL)
632 *r = *p;
634 found = true;
635 break;
638 return found;
641 /* Return true if S points to an integer suffix, false otherwise. For
642 a description of the arguments, look at
643 stap_generic_check_suffix. */
645 static bool
646 stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s,
647 const char **r)
649 const char *const *p = gdbarch_stap_integer_suffixes (gdbarch);
651 return stap_generic_check_suffix (gdbarch, s, r, p);
654 /* Return true if S points to a register suffix, false otherwise. For
655 a description of the arguments, look at
656 stap_generic_check_suffix. */
658 static bool
659 stap_check_register_suffix (struct gdbarch *gdbarch, const char *s,
660 const char **r)
662 const char *const *p = gdbarch_stap_register_suffixes (gdbarch);
664 return stap_generic_check_suffix (gdbarch, s, r, p);
667 /* Return true if S points to a register indirection suffix, false
668 otherwise. For a description of the arguments, look at
669 stap_generic_check_suffix. */
671 static bool
672 stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s,
673 const char **r)
675 const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch);
677 return stap_generic_check_suffix (gdbarch, s, r, p);
680 /* Function responsible for parsing a register operand according to
681 SystemTap parlance. Assuming:
683 RP = register prefix
684 RS = register suffix
685 RIP = register indirection prefix
686 RIS = register indirection suffix
688 Then a register operand can be:
690 [RIP] [RP] REGISTER [RS] [RIS]
692 This function takes care of a register's indirection, displacement and
693 direct access. It also takes into consideration the fact that some
694 registers are named differently inside and outside GDB, e.g., PPC's
695 general-purpose registers are represented by integers in the assembly
696 language (e.g., `15' is the 15th general-purpose register), but inside
697 GDB they have a prefix (the letter `r') appended. */
699 static expr::operation_up
700 stap_parse_register_operand (struct stap_parse_info *p)
702 /* Simple flag to indicate whether we have seen a minus signal before
703 certain number. */
704 bool got_minus = false;
705 /* Flag to indicate whether this register access is being
706 indirected. */
707 bool indirect_p = false;
708 struct gdbarch *gdbarch = p->gdbarch;
709 /* Variables used to extract the register name from the probe's
710 argument. */
711 const char *start;
712 const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
713 const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
714 const char *reg_prefix;
715 const char *reg_ind_prefix;
716 const char *reg_suffix;
717 const char *reg_ind_suffix;
719 using namespace expr;
721 /* Checking for a displacement argument. */
722 if (*p->arg == '+')
724 /* If it's a plus sign, we don't need to do anything, just advance the
725 pointer. */
726 ++p->arg;
728 else if (*p->arg == '-')
730 got_minus = true;
731 ++p->arg;
734 struct type *long_type = builtin_type (gdbarch)->builtin_long;
735 operation_up disp_op;
736 if (isdigit (*p->arg))
738 /* The value of the displacement. */
739 long displacement;
740 char *endp;
742 displacement = strtol (p->arg, &endp, 10);
743 p->arg = endp;
745 /* Generating the expression for the displacement. */
746 if (got_minus)
747 displacement = -displacement;
748 disp_op = make_operation<long_const_operation> (long_type, displacement);
751 /* Getting rid of register indirection prefix. */
752 if (stap_is_register_indirection_prefix (gdbarch, p->arg, &reg_ind_prefix))
754 indirect_p = true;
755 p->arg += strlen (reg_ind_prefix);
758 if (disp_op != nullptr && !indirect_p)
759 error (_("Invalid register displacement syntax on expression `%s'."),
760 p->saved_arg);
762 /* Getting rid of register prefix. */
763 if (stap_is_register_prefix (gdbarch, p->arg, &reg_prefix))
764 p->arg += strlen (reg_prefix);
766 /* Now we should have only the register name. Let's extract it and get
767 the associated number. */
768 start = p->arg;
770 /* We assume the register name is composed by letters and numbers. */
771 while (isalnum (*p->arg))
772 ++p->arg;
774 std::string regname (start, p->arg - start);
776 /* We only add the GDB's register prefix/suffix if we are dealing with
777 a numeric register. */
778 if (isdigit (*start))
780 if (gdb_reg_prefix != NULL)
781 regname = gdb_reg_prefix + regname;
783 if (gdb_reg_suffix != NULL)
784 regname += gdb_reg_suffix;
787 int regnum = user_reg_map_name_to_regnum (gdbarch, regname.c_str (),
788 regname.size ());
790 /* Is this a valid register name? */
791 if (regnum == -1)
792 error (_("Invalid register name `%s' on expression `%s'."),
793 regname.c_str (), p->saved_arg);
795 /* Check if there's any special treatment that the arch-specific
796 code would like to perform on the register name. */
797 if (gdbarch_stap_adjust_register_p (gdbarch))
799 std::string newregname
800 = gdbarch_stap_adjust_register (gdbarch, p, regname, regnum);
802 if (regname != newregname)
804 /* This is just a check we perform to make sure that the
805 arch-dependent code has provided us with a valid
806 register name. */
807 regnum = user_reg_map_name_to_regnum (gdbarch, newregname.c_str (),
808 newregname.size ());
810 if (regnum == -1)
811 internal_error (_("Invalid register name '%s' after replacing it"
812 " (previous name was '%s')"),
813 newregname.c_str (), regname.c_str ());
815 regname = std::move (newregname);
819 operation_up reg = make_operation<register_operation> (std::move (regname));
821 /* If the argument has been placed into a vector register then (for most
822 architectures), the type of this register will be a union of arrays.
823 As a result, attempting to cast from the register type to the scalar
824 argument type will not be possible (GDB will throw an error during
825 expression evaluation).
827 The solution is to extract the scalar type from the value contents of
828 the entire register value. */
829 if (!is_scalar_type (gdbarch_register_type (gdbarch, regnum)))
831 gdb_assert (is_scalar_type (p->arg_type));
832 reg = make_operation<unop_extract_operation> (std::move (reg),
833 p->arg_type);
836 if (indirect_p)
838 if (disp_op != nullptr)
839 reg = make_operation<add_operation> (std::move (disp_op),
840 std::move (reg));
842 /* Casting to the expected type. */
843 struct type *arg_ptr_type = lookup_pointer_type (p->arg_type);
844 reg = make_operation<unop_cast_operation> (std::move (reg),
845 arg_ptr_type);
846 reg = make_operation<unop_ind_operation> (std::move (reg));
849 /* Getting rid of the register name suffix. */
850 if (stap_check_register_suffix (gdbarch, p->arg, &reg_suffix))
851 p->arg += strlen (reg_suffix);
852 else
853 error (_("Missing register name suffix on expression `%s'."),
854 p->saved_arg);
856 /* Getting rid of the register indirection suffix. */
857 if (indirect_p)
859 if (stap_check_register_indirection_suffix (gdbarch, p->arg,
860 &reg_ind_suffix))
861 p->arg += strlen (reg_ind_suffix);
862 else
863 error (_("Missing indirection suffix on expression `%s'."),
864 p->saved_arg);
867 return reg;
870 /* This function is responsible for parsing a single operand.
872 A single operand can be:
874 - an unary operation (e.g., `-5', `~2', or even with subexpressions
875 like `-(2 + 1)')
876 - a register displacement, which will be treated as a register
877 operand (e.g., `-4(%eax)' on x86)
878 - a numeric constant, or
879 - a register operand (see function `stap_parse_register_operand')
881 The function also calls special-handling functions to deal with
882 unrecognized operands, allowing arch-specific parsers to be
883 created. */
885 static expr::operation_up
886 stap_parse_single_operand (struct stap_parse_info *p)
888 struct gdbarch *gdbarch = p->gdbarch;
889 const char *int_prefix = NULL;
891 using namespace expr;
893 /* We first try to parse this token as a "special token". */
894 if (gdbarch_stap_parse_special_token_p (gdbarch))
896 operation_up token = gdbarch_stap_parse_special_token (gdbarch, p);
897 if (token != nullptr)
898 return token;
901 struct type *long_type = builtin_type (gdbarch)->builtin_long;
902 operation_up result;
903 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!')
905 char c = *p->arg;
906 /* We use this variable to do a lookahead. */
907 const char *tmp = p->arg;
908 bool has_digit = false;
910 /* Skipping signal. */
911 ++tmp;
913 /* This is an unary operation. Here is a list of allowed tokens
914 here:
916 - numeric literal;
917 - number (from register displacement)
918 - subexpression (beginning with `(')
920 We handle the register displacement here, and the other cases
921 recursively. */
922 if (p->inside_paren_p)
923 tmp = skip_spaces (tmp);
925 while (isdigit (*tmp))
927 /* We skip the digit here because we are only interested in
928 knowing what kind of unary operation this is. The digit
929 will be handled by one of the functions that will be
930 called below ('stap_parse_argument_conditionally' or
931 'stap_parse_register_operand'). */
932 ++tmp;
933 has_digit = true;
936 if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp,
937 NULL))
939 /* If we are here, it means it is a displacement. The only
940 operations allowed here are `-' and `+'. */
941 if (c != '-' && c != '+')
942 error (_("Invalid operator `%c' for register displacement "
943 "on expression `%s'."), c, p->saved_arg);
945 result = stap_parse_register_operand (p);
947 else
949 /* This is not a displacement. We skip the operator, and
950 deal with it when the recursion returns. */
951 ++p->arg;
952 result = stap_parse_argument_conditionally (p);
953 if (c == '-')
954 result = make_operation<unary_neg_operation> (std::move (result));
955 else if (c == '~')
956 result = (make_operation<unary_complement_operation>
957 (std::move (result)));
958 else if (c == '!')
959 result = (make_operation<unary_logical_not_operation>
960 (std::move (result)));
963 else if (isdigit (*p->arg))
965 /* A temporary variable, needed for lookahead. */
966 const char *tmp = p->arg;
967 char *endp;
968 long number;
970 /* We can be dealing with a numeric constant, or with a register
971 displacement. */
972 number = strtol (tmp, &endp, 10);
973 tmp = endp;
975 if (p->inside_paren_p)
976 tmp = skip_spaces (tmp);
978 /* If "stap_is_integer_prefix" returns true, it means we can
979 accept integers without a prefix here. But we also need to
980 check whether the next token (i.e., "tmp") is not a register
981 indirection prefix. */
982 if (stap_is_integer_prefix (gdbarch, p->arg, NULL)
983 && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
985 const char *int_suffix;
987 /* We are dealing with a numeric constant. */
988 result = make_operation<long_const_operation> (long_type, number);
990 p->arg = tmp;
992 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
993 p->arg += strlen (int_suffix);
994 else
995 error (_("Invalid constant suffix on expression `%s'."),
996 p->saved_arg);
998 else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
999 result = stap_parse_register_operand (p);
1000 else
1001 error (_("Unknown numeric token on expression `%s'."),
1002 p->saved_arg);
1004 else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
1006 /* We are dealing with a numeric constant. */
1007 long number;
1008 char *endp;
1009 const char *int_suffix;
1011 p->arg += strlen (int_prefix);
1012 number = strtol (p->arg, &endp, 10);
1013 p->arg = endp;
1015 result = make_operation<long_const_operation> (long_type, number);
1017 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
1018 p->arg += strlen (int_suffix);
1019 else
1020 error (_("Invalid constant suffix on expression `%s'."),
1021 p->saved_arg);
1023 else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
1024 || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
1025 result = stap_parse_register_operand (p);
1026 else
1027 error (_("Operator `%c' not recognized on expression `%s'."),
1028 *p->arg, p->saved_arg);
1030 return result;
1033 /* This function parses an argument conditionally, based on single or
1034 non-single operands. A non-single operand would be a parenthesized
1035 expression (e.g., `(2 + 1)'), and a single operand is anything that
1036 starts with `-', `~', `+' (i.e., unary operators), a digit, or
1037 something recognized by `gdbarch_stap_is_single_operand'. */
1039 static expr::operation_up
1040 stap_parse_argument_conditionally (struct stap_parse_info *p)
1042 gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));
1044 expr::operation_up result;
1045 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!'
1046 || isdigit (*p->arg)
1047 || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
1048 result = stap_parse_single_operand (p);
1049 else if (*p->arg == '(')
1051 /* We are dealing with a parenthesized operand. It means we
1052 have to parse it as it was a separate expression, without
1053 left-side or precedence. */
1054 ++p->arg;
1055 p->arg = skip_spaces (p->arg);
1056 ++p->inside_paren_p;
1058 result = stap_parse_argument_1 (p, {}, STAP_OPERAND_PREC_NONE);
1060 p->arg = skip_spaces (p->arg);
1061 if (*p->arg != ')')
1062 error (_("Missing close-parenthesis on expression `%s'."),
1063 p->saved_arg);
1065 --p->inside_paren_p;
1066 ++p->arg;
1067 if (p->inside_paren_p)
1068 p->arg = skip_spaces (p->arg);
1070 else
1071 error (_("Cannot parse expression `%s'."), p->saved_arg);
1073 return result;
1076 /* Helper function for `stap_parse_argument'. Please, see its comments to
1077 better understand what this function does. */
1079 static expr::operation_up ATTRIBUTE_UNUSED_RESULT
1080 stap_parse_argument_1 (struct stap_parse_info *p,
1081 expr::operation_up &&lhs_in,
1082 enum stap_operand_prec prec)
1084 /* This is an operator-precedence parser.
1086 We work with left- and right-sides of expressions, and
1087 parse them depending on the precedence of the operators
1088 we find. */
1090 gdb_assert (p->arg != NULL);
1092 if (p->inside_paren_p)
1093 p->arg = skip_spaces (p->arg);
1095 using namespace expr;
1096 operation_up lhs = std::move (lhs_in);
1097 if (lhs == nullptr)
1099 /* We were called without a left-side, either because this is the
1100 first call, or because we were called to parse a parenthesized
1101 expression. It doesn't really matter; we have to parse the
1102 left-side in order to continue the process. */
1103 lhs = stap_parse_argument_conditionally (p);
1106 if (p->inside_paren_p)
1107 p->arg = skip_spaces (p->arg);
1109 /* Start to parse the right-side, and to "join" left and right sides
1110 depending on the operation specified.
1112 This loop shall continue until we run out of characters in the input,
1113 or until we find a close-parenthesis, which means that we've reached
1114 the end of a sub-expression. */
1115 while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
1117 const char *tmp_exp_buf;
1118 enum exp_opcode opcode;
1119 enum stap_operand_prec cur_prec;
1121 if (!stap_is_operator (p->arg))
1122 error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
1123 p->saved_arg);
1125 /* We have to save the current value of the expression buffer because
1126 the `stap_get_opcode' modifies it in order to get the current
1127 operator. If this operator's precedence is lower than PREC, we
1128 should return and not advance the expression buffer pointer. */
1129 tmp_exp_buf = p->arg;
1130 opcode = stap_get_opcode (&tmp_exp_buf);
1132 cur_prec = stap_get_operator_prec (opcode);
1133 if (cur_prec < prec)
1135 /* If the precedence of the operator that we are seeing now is
1136 lower than the precedence of the first operator seen before
1137 this parsing process began, it means we should stop parsing
1138 and return. */
1139 break;
1142 p->arg = tmp_exp_buf;
1143 if (p->inside_paren_p)
1144 p->arg = skip_spaces (p->arg);
1146 /* Parse the right-side of the expression.
1148 We save whether the right-side is a parenthesized
1149 subexpression because, if it is, we will have to finish
1150 processing this part of the expression before continuing. */
1151 bool paren_subexp = *p->arg == '(';
1153 operation_up rhs = stap_parse_argument_conditionally (p);
1154 if (p->inside_paren_p)
1155 p->arg = skip_spaces (p->arg);
1156 if (paren_subexp)
1158 lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs));
1159 continue;
1162 /* While we still have operators, try to parse another
1163 right-side, but using the current right-side as a left-side. */
1164 while (*p->arg != '\0' && stap_is_operator (p->arg))
1166 enum exp_opcode lookahead_opcode;
1167 enum stap_operand_prec lookahead_prec;
1169 /* Saving the current expression buffer position. The explanation
1170 is the same as above. */
1171 tmp_exp_buf = p->arg;
1172 lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
1173 lookahead_prec = stap_get_operator_prec (lookahead_opcode);
1175 if (lookahead_prec <= prec)
1177 /* If we are dealing with an operator whose precedence is lower
1178 than the first one, just abandon the attempt. */
1179 break;
1182 /* Parse the right-side of the expression, using the current
1183 right-hand-side as the left-hand-side of the new
1184 subexpression. */
1185 rhs = stap_parse_argument_1 (p, std::move (rhs), lookahead_prec);
1186 if (p->inside_paren_p)
1187 p->arg = skip_spaces (p->arg);
1190 lhs = stap_make_binop (opcode, std::move (lhs), std::move (rhs));
1193 return lhs;
1196 /* Parse a probe's argument.
1198 Assuming that:
1200 LP = literal integer prefix
1201 LS = literal integer suffix
1203 RP = register prefix
1204 RS = register suffix
1206 RIP = register indirection prefix
1207 RIS = register indirection suffix
1209 This routine assumes that arguments' tokens are of the form:
1211 - [LP] NUMBER [LS]
1212 - [RP] REGISTER [RS]
1213 - [RIP] [RP] REGISTER [RS] [RIS]
1214 - If we find a number without LP, we try to parse it as a literal integer
1215 constant (if LP == NULL), or as a register displacement.
1216 - We count parenthesis, and only skip whitespaces if we are inside them.
1217 - If we find an operator, we skip it.
1219 This function can also call a special function that will try to match
1220 unknown tokens. It will return the expression_up generated from
1221 parsing the argument. */
1223 static expression_up
1224 stap_parse_argument (const char **arg, struct type *atype,
1225 struct gdbarch *gdbarch)
1227 /* We need to initialize the expression buffer, in order to begin
1228 our parsing efforts. We use language_c here because we may need
1229 to do pointer arithmetics. */
1230 struct stap_parse_info p (*arg, atype, language_def (language_c),
1231 gdbarch);
1233 using namespace expr;
1234 operation_up result = stap_parse_argument_1 (&p, {}, STAP_OPERAND_PREC_NONE);
1236 gdb_assert (p.inside_paren_p == 0);
1238 /* Casting the final expression to the appropriate type. */
1239 result = make_operation<unop_cast_operation> (std::move (result), atype);
1240 p.pstate.set_operation (std::move (result));
1242 p.arg = skip_spaces (p.arg);
1243 *arg = p.arg;
1245 return p.pstate.release ();
1248 /* Implementation of 'parse_arguments' method. */
1250 void
1251 stap_probe::parse_arguments (struct gdbarch *gdbarch)
1253 const char *cur;
1255 gdb_assert (!m_have_parsed_args);
1256 cur = m_unparsed_args_text;
1257 m_have_parsed_args = true;
1259 if (cur == NULL || *cur == '\0' || *cur == ':')
1260 return;
1262 while (*cur != '\0')
1264 enum stap_arg_bitness bitness;
1265 bool got_minus = false;
1267 /* We expect to find something like:
1269 N@OP
1271 Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so
1272 we check it here. If we don't find it, go to the next
1273 state. */
1274 if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@')
1275 || (isdigit (cur[0]) && cur[1] == '@'))
1277 if (*cur == '-')
1279 /* Discard the `-'. */
1280 ++cur;
1281 got_minus = true;
1284 /* Defining the bitness. */
1285 switch (*cur)
1287 case '1':
1288 bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED
1289 : STAP_ARG_BITNESS_8BIT_UNSIGNED);
1290 break;
1292 case '2':
1293 bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED
1294 : STAP_ARG_BITNESS_16BIT_UNSIGNED);
1295 break;
1297 case '4':
1298 bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
1299 : STAP_ARG_BITNESS_32BIT_UNSIGNED);
1300 break;
1302 case '8':
1303 bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
1304 : STAP_ARG_BITNESS_64BIT_UNSIGNED);
1305 break;
1307 default:
1309 /* We have an error, because we don't expect anything
1310 except 1, 2, 4 and 8. */
1311 warning (_("unrecognized bitness %s%c' for probe `%s'"),
1312 got_minus ? "`-" : "`", *cur,
1313 this->get_name ().c_str ());
1314 return;
1317 /* Discard the number and the `@' sign. */
1318 cur += 2;
1320 else
1321 bitness = STAP_ARG_BITNESS_UNDEFINED;
1323 struct type *atype
1324 = stap_get_expected_argument_type (gdbarch, bitness,
1325 this->get_name ().c_str ());
1327 expression_up expr = stap_parse_argument (&cur, atype, gdbarch);
1329 if (stap_expression_debug)
1330 expr->dump (gdb_stdlog);
1332 m_parsed_args.emplace_back (bitness, atype, std::move (expr));
1334 /* Start it over again. */
1335 cur = skip_spaces (cur);
1339 /* Helper function to relocate an address. */
1341 static CORE_ADDR
1342 relocate_address (CORE_ADDR address, struct objfile *objfile)
1344 return address + objfile->text_section_offset ();
1347 /* Implementation of the get_relocated_address method. */
1349 CORE_ADDR
1350 stap_probe::get_relocated_address (struct objfile *objfile)
1352 return relocate_address (this->get_address (), objfile);
1355 /* Given PROBE, returns the number of arguments present in that probe's
1356 argument string. */
1358 unsigned
1359 stap_probe::get_argument_count (struct gdbarch *gdbarch)
1361 if (!m_have_parsed_args)
1363 if (this->can_evaluate_arguments ())
1364 this->parse_arguments (gdbarch);
1365 else
1367 static bool have_warned_stap_incomplete = false;
1369 if (!have_warned_stap_incomplete)
1371 warning (_(
1372 "The SystemTap SDT probe support is not fully implemented on this target;\n"
1373 "you will not be able to inspect the arguments of the probes.\n"
1374 "Please report a bug against GDB requesting a port to this target."));
1375 have_warned_stap_incomplete = true;
1378 /* Marking the arguments as "already parsed". */
1379 m_have_parsed_args = true;
1383 gdb_assert (m_have_parsed_args);
1384 return m_parsed_args.size ();
1387 /* Return true if OP is a valid operator inside a probe argument, or
1388 false otherwise. */
1390 static bool
1391 stap_is_operator (const char *op)
1393 bool ret = true;
1395 switch (*op)
1397 case '*':
1398 case '/':
1399 case '%':
1400 case '^':
1401 case '!':
1402 case '+':
1403 case '-':
1404 case '<':
1405 case '>':
1406 case '|':
1407 case '&':
1408 break;
1410 case '=':
1411 if (op[1] != '=')
1412 ret = false;
1413 break;
1415 default:
1416 /* We didn't find any operator. */
1417 ret = false;
1420 return ret;
1423 /* Implement the `can_evaluate_arguments' method. */
1425 bool
1426 stap_probe::can_evaluate_arguments () const
1428 struct gdbarch *gdbarch = this->get_gdbarch ();
1430 /* For SystemTap probes, we have to guarantee that the method
1431 stap_is_single_operand is defined on gdbarch. If it is not, then it
1432 means that argument evaluation is not implemented on this target. */
1433 return gdbarch_stap_is_single_operand_p (gdbarch);
1436 /* Evaluate the probe's argument N (indexed from 0), returning a value
1437 corresponding to it. Assertion is thrown if N does not exist. */
1439 struct value *
1440 stap_probe::evaluate_argument (unsigned n, const frame_info_ptr &frame)
1442 struct stap_probe_arg *arg;
1443 struct gdbarch *gdbarch = get_frame_arch (frame);
1445 arg = this->get_arg_by_number (n, gdbarch);
1446 return arg->aexpr->evaluate (arg->atype);
1449 /* Compile the probe's argument N (indexed from 0) to agent expression.
1450 Assertion is thrown if N does not exist. */
1452 void
1453 stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value,
1454 unsigned n)
1456 struct stap_probe_arg *arg;
1458 arg = this->get_arg_by_number (n, expr->gdbarch);
1460 arg->aexpr->op->generate_ax (arg->aexpr.get (), expr, value);
1462 require_rvalue (expr, value);
1463 value->type = arg->atype;
1467 /* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
1468 address. SET is zero if the semaphore should be cleared, or one if
1469 it should be set. This is a helper function for
1470 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */
1472 static void
1473 stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1475 gdb_byte bytes[sizeof (LONGEST)];
1476 /* The ABI specifies "unsigned short". */
1477 struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1478 ULONGEST value;
1480 /* Swallow errors. */
1481 if (target_read_memory (address, bytes, type->length ()) != 0)
1483 warning (_("Could not read the value of a SystemTap semaphore."));
1484 return;
1487 enum bfd_endian byte_order = type_byte_order (type);
1488 value = extract_unsigned_integer (bytes, type->length (), byte_order);
1489 /* Note that we explicitly don't worry about overflow or
1490 underflow. */
1491 if (set)
1492 ++value;
1493 else
1494 --value;
1496 store_unsigned_integer (bytes, type->length (), byte_order, value);
1498 if (target_write_memory (address, bytes, type->length ()) != 0)
1499 warning (_("Could not write the value of a SystemTap semaphore."));
1502 /* Implementation of the 'set_semaphore' method.
1504 SystemTap semaphores act as reference counters, so calls to this
1505 function must be paired with calls to 'clear_semaphore'.
1507 This function and 'clear_semaphore' race with another tool
1508 changing the probes, but that is too rare to care. */
1510 void
1511 stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1513 if (m_sem_addr == 0)
1514 return;
1515 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
1518 /* Implementation of the 'clear_semaphore' method. */
1520 void
1521 stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1523 if (m_sem_addr == 0)
1524 return;
1525 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
1528 /* Implementation of the 'get_static_ops' method. */
1530 const static_probe_ops *
1531 stap_probe::get_static_ops () const
1533 return &stap_static_probe_ops;
1536 /* Implementation of the 'gen_info_probes_table_values' method. */
1538 std::vector<const char *>
1539 stap_probe::gen_info_probes_table_values () const
1541 const char *val = NULL;
1543 if (m_sem_addr != 0)
1544 val = print_core_address (this->get_gdbarch (), m_sem_addr);
1546 return std::vector<const char *> { val };
1549 /* Helper function that parses the information contained in a
1550 SystemTap's probe. Basically, the information consists in:
1552 - Probe's PC address;
1553 - Link-time section address of `.stapsdt.base' section;
1554 - Link-time address of the semaphore variable, or ZERO if the
1555 probe doesn't have an associated semaphore;
1556 - Probe's provider name;
1557 - Probe's name;
1558 - Probe's argument format. */
1560 static void
1561 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
1562 std::vector<std::unique_ptr<probe>> *probesp,
1563 CORE_ADDR base)
1565 bfd *abfd = objfile->obfd.get ();
1566 int size = bfd_get_arch_size (abfd) / 8;
1567 struct gdbarch *gdbarch = objfile->arch ();
1568 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1570 /* Provider and the name of the probe. */
1571 const char *provider = (const char *) &el->data[3 * size];
1572 const char *name = ((const char *)
1573 memchr (provider, '\0',
1574 (char *) el->data + el->size - provider));
1575 /* Making sure there is a name. */
1576 if (name == NULL)
1578 complaint (_("corrupt probe name when reading `%s'"),
1579 objfile_name (objfile));
1581 /* There is no way to use a probe without a name or a provider, so
1582 returning here makes sense. */
1583 return;
1585 else
1586 ++name;
1588 /* Retrieving the probe's address. */
1589 CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type);
1591 /* Link-time sh_addr of `.stapsdt.base' section. */
1592 CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type);
1594 /* Semaphore address. */
1595 CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
1597 address += base - base_ref;
1598 if (sem_addr != 0)
1599 sem_addr += base - base_ref;
1601 /* Arguments. We can only extract the argument format if there is a valid
1602 name for this probe. */
1603 const char *probe_args = ((const char*)
1604 memchr (name, '\0',
1605 (char *) el->data + el->size - name));
1607 if (probe_args != NULL)
1608 ++probe_args;
1610 if (probe_args == NULL
1611 || (memchr (probe_args, '\0', (char *) el->data + el->size - name)
1612 != el->data + el->size - 1))
1614 complaint (_("corrupt probe argument when reading `%s'"),
1615 objfile_name (objfile));
1616 /* If the argument string is NULL, it means some problem happened with
1617 it. So we return. */
1618 return;
1621 if (ignore_probe_p (provider, name, objfile_name (objfile), "SystemTap"))
1622 return;
1624 stap_probe *ret = new stap_probe (std::string (name), std::string (provider),
1625 address, gdbarch, sem_addr, probe_args);
1627 /* Successfully created probe. */
1628 probesp->emplace_back (ret);
1631 /* Helper function which iterates over every section in the BFD file,
1632 trying to find the base address of the SystemTap base section.
1633 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1635 static int
1636 get_stap_base_address (bfd *obfd, bfd_vma *base)
1638 asection *ret = NULL;
1640 for (asection *sect : gdb_bfd_sections (obfd))
1641 if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1642 && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1643 ret = sect;
1645 if (ret == NULL)
1647 complaint (_("could not obtain base address for "
1648 "SystemTap section on objfile `%s'."),
1649 bfd_get_filename (obfd));
1650 return 0;
1653 if (base != NULL)
1654 *base = ret->vma;
1656 return 1;
1659 /* Implementation of the 'is_linespec' method. */
1661 bool
1662 stap_static_probe_ops::is_linespec (const char **linespecp) const
1664 static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1666 return probe_is_linespec_by_keyword (linespecp, keywords);
1669 /* Implementation of the 'get_probes' method. */
1671 void
1672 stap_static_probe_ops::get_probes
1673 (std::vector<std::unique_ptr<probe>> *probesp,
1674 struct objfile *objfile) const
1676 /* If we are here, then this is the first time we are parsing the
1677 SystemTap probe's information. We basically have to count how many
1678 probes the objfile has, and then fill in the necessary information
1679 for each one. */
1680 bfd *obfd = objfile->obfd.get ();
1681 bfd_vma base;
1682 struct sdt_note *iter;
1683 unsigned save_probesp_len = probesp->size ();
1685 if (objfile->separate_debug_objfile_backlink != NULL)
1687 /* This is a .debug file, not the objfile itself. */
1688 return;
1691 if (elf_tdata (obfd)->sdt_note_head == NULL)
1693 /* There isn't any probe here. */
1694 return;
1697 if (!get_stap_base_address (obfd, &base))
1699 /* There was an error finding the base address for the section.
1700 Just return NULL. */
1701 return;
1704 /* Parsing each probe's information. */
1705 for (iter = elf_tdata (obfd)->sdt_note_head;
1706 iter != NULL;
1707 iter = iter->next)
1709 /* We first have to handle all the information about the
1710 probe which is present in the section. */
1711 handle_stap_probe (objfile, iter, probesp, base);
1714 if (save_probesp_len == probesp->size ())
1716 /* If we are here, it means we have failed to parse every known
1717 probe. */
1718 complaint (_("could not parse SystemTap probe(s) from inferior"));
1719 return;
1723 /* Implementation of the type_name method. */
1725 const char *
1726 stap_static_probe_ops::type_name () const
1728 return "stap";
1731 /* Implementation of the 'gen_info_probes_table_header' method. */
1733 std::vector<struct info_probe_column>
1734 stap_static_probe_ops::gen_info_probes_table_header () const
1736 struct info_probe_column stap_probe_column;
1738 stap_probe_column.field_name = "semaphore";
1739 stap_probe_column.print_name = _("Semaphore");
1741 return std::vector<struct info_probe_column> { stap_probe_column };
1744 /* Implementation of the `info probes stap' command. */
1746 static void
1747 info_probes_stap_command (const char *arg, int from_tty)
1749 info_probes_for_spops (arg, from_tty, &stap_static_probe_ops);
1752 void _initialize_stap_probe ();
1753 void
1754 _initialize_stap_probe ()
1756 all_static_probe_ops.push_back (&stap_static_probe_ops);
1758 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
1759 &stap_expression_debug,
1760 _("Set SystemTap expression debugging."),
1761 _("Show SystemTap expression debugging."),
1762 _("When non-zero, the internal representation "
1763 "of SystemTap expressions will be printed."),
1764 NULL,
1765 show_stapexpressiondebug,
1766 &setdebuglist, &showdebuglist);
1768 add_cmd ("stap", class_info, info_probes_stap_command,
1769 _("\
1770 Show information about SystemTap static probes.\n\
1771 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1772 Each argument is a regular expression, used to select probes.\n\
1773 PROVIDER matches probe provider names.\n\
1774 NAME matches the probe names.\n\
1775 OBJECT matches the executable or shared library name."),
1776 info_probes_cmdlist_get ());
1779 using namespace expr;
1780 stap_maker_map[BINOP_ADD] = make_operation<add_operation>;
1781 stap_maker_map[BINOP_BITWISE_AND] = make_operation<bitwise_and_operation>;
1782 stap_maker_map[BINOP_BITWISE_IOR] = make_operation<bitwise_ior_operation>;
1783 stap_maker_map[BINOP_BITWISE_XOR] = make_operation<bitwise_xor_operation>;
1784 stap_maker_map[BINOP_DIV] = make_operation<div_operation>;
1785 stap_maker_map[BINOP_EQUAL] = make_operation<equal_operation>;
1786 stap_maker_map[BINOP_GEQ] = make_operation<geq_operation>;
1787 stap_maker_map[BINOP_GTR] = make_operation<gtr_operation>;
1788 stap_maker_map[BINOP_LEQ] = make_operation<leq_operation>;
1789 stap_maker_map[BINOP_LESS] = make_operation<less_operation>;
1790 stap_maker_map[BINOP_LOGICAL_AND] = make_operation<logical_and_operation>;
1791 stap_maker_map[BINOP_LOGICAL_OR] = make_operation<logical_or_operation>;
1792 stap_maker_map[BINOP_LSH] = make_operation<lsh_operation>;
1793 stap_maker_map[BINOP_MUL] = make_operation<mul_operation>;
1794 stap_maker_map[BINOP_NOTEQUAL] = make_operation<notequal_operation>;
1795 stap_maker_map[BINOP_REM] = make_operation<rem_operation>;
1796 stap_maker_map[BINOP_RSH] = make_operation<rsh_operation>;
1797 stap_maker_map[BINOP_SUB] = make_operation<sub_operation>;