Gate the displaying of non-debug sections in separate debuginfo files.
[deliverable/binutils-gdb.git] / gdb / stap-probe.c
CommitLineData
55aa24fb
SDJ
1/* SystemTap probe support for GDB.
2
3666a048 3 Copyright (C) 2012-2021 Free Software Foundation, Inc.
55aa24fb
SDJ
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "stap-probe.h"
22#include "probe.h"
55aa24fb
SDJ
23#include "ui-out.h"
24#include "objfiles.h"
25#include "arch-utils.h"
26#include "command.h"
27#include "gdbcmd.h"
28#include "filenames.h"
29#include "value.h"
55aa24fb
SDJ
30#include "ax.h"
31#include "ax-gdb.h"
32#include "complaints.h"
33#include "cli/cli-utils.h"
34#include "linespec.h"
35#include "user-regs.h"
36#include "parser-defs.h"
37#include "language.h"
38#include "elf-bfd.h"
39
40#include <ctype.h>
41
42/* The name of the SystemTap section where we will find information about
43 the probes. */
44
45#define STAP_BASE_SECTION_NAME ".stapsdt.base"
46
55aa24fb
SDJ
47/* Should we display debug information for the probe's argument expression
48 parsing? */
49
ccce17b0 50static unsigned int stap_expression_debug = 0;
55aa24fb
SDJ
51
52/* The various possibilities of bitness defined for a probe's argument.
53
54 The relationship is:
55
56 - STAP_ARG_BITNESS_UNDEFINED: The user hasn't specified the bitness.
30a1e6cc
SDJ
57 - STAP_ARG_BITNESS_8BIT_UNSIGNED: argument string starts with `1@'.
58 - STAP_ARG_BITNESS_8BIT_SIGNED: argument string starts with `-1@'.
59 - STAP_ARG_BITNESS_16BIT_UNSIGNED: argument string starts with `2@'.
60 - STAP_ARG_BITNESS_16BIT_SIGNED: argument string starts with `-2@'.
55aa24fb
SDJ
61 - STAP_ARG_BITNESS_32BIT_UNSIGNED: argument string starts with `4@'.
62 - STAP_ARG_BITNESS_32BIT_SIGNED: argument string starts with `-4@'.
63 - STAP_ARG_BITNESS_64BIT_UNSIGNED: argument string starts with `8@'.
64 - STAP_ARG_BITNESS_64BIT_SIGNED: argument string starts with `-8@'. */
65
66enum stap_arg_bitness
67{
68 STAP_ARG_BITNESS_UNDEFINED,
30a1e6cc
SDJ
69 STAP_ARG_BITNESS_8BIT_UNSIGNED,
70 STAP_ARG_BITNESS_8BIT_SIGNED,
71 STAP_ARG_BITNESS_16BIT_UNSIGNED,
72 STAP_ARG_BITNESS_16BIT_SIGNED,
55aa24fb
SDJ
73 STAP_ARG_BITNESS_32BIT_UNSIGNED,
74 STAP_ARG_BITNESS_32BIT_SIGNED,
75 STAP_ARG_BITNESS_64BIT_UNSIGNED,
76 STAP_ARG_BITNESS_64BIT_SIGNED,
77};
78
79/* The following structure represents a single argument for the probe. */
80
81struct stap_probe_arg
82{
0e9ae10f
SDJ
83 /* Constructor for stap_probe_arg. */
84 stap_probe_arg (enum stap_arg_bitness bitness_, struct type *atype_,
85 expression_up &&aexpr_)
86 : bitness (bitness_), atype (atype_), aexpr (std::move (aexpr_))
87 {}
88
55aa24fb
SDJ
89 /* The bitness of this argument. */
90 enum stap_arg_bitness bitness;
91
92 /* The corresponding `struct type *' to the bitness. */
93 struct type *atype;
94
95 /* The argument converted to an internal GDB expression. */
0e9ae10f 96 expression_up aexpr;
55aa24fb
SDJ
97};
98
0e9ae10f 99/* Class that implements the static probe methods for "stap" probes. */
55aa24fb 100
0e9ae10f 101class stap_static_probe_ops : public static_probe_ops
55aa24fb 102{
0e9ae10f 103public:
4212d509
TT
104 /* We need a user-provided constructor to placate some compilers.
105 See PR build/24937. */
106 stap_static_probe_ops ()
107 {
108 }
109
0e9ae10f
SDJ
110 /* See probe.h. */
111 bool is_linespec (const char **linespecp) const override;
55aa24fb 112
0e9ae10f 113 /* See probe.h. */
814cf43a 114 void get_probes (std::vector<std::unique_ptr<probe>> *probesp,
0e9ae10f
SDJ
115 struct objfile *objfile) const override;
116
117 /* See probe.h. */
118 const char *type_name () const override;
119
120 /* See probe.h. */
121 std::vector<struct info_probe_column> gen_info_probes_table_header
122 () const override;
123};
124
125/* SystemTap static_probe_ops. */
126
3dcfdc58 127const stap_static_probe_ops stap_static_probe_ops {};
0e9ae10f
SDJ
128
129class stap_probe : public probe
130{
131public:
132 /* Constructor for stap_probe. */
133 stap_probe (std::string &&name_, std::string &&provider_, CORE_ADDR address_,
134 struct gdbarch *arch_, CORE_ADDR sem_addr, const char *args_text)
135 : probe (std::move (name_), std::move (provider_), address_, arch_),
136 m_sem_addr (sem_addr),
137 m_have_parsed_args (false), m_unparsed_args_text (args_text)
138 {}
139
140 /* See probe.h. */
141 CORE_ADDR get_relocated_address (struct objfile *objfile) override;
142
143 /* See probe.h. */
fe01123e 144 unsigned get_argument_count (struct gdbarch *gdbarch) override;
0e9ae10f
SDJ
145
146 /* See probe.h. */
147 bool can_evaluate_arguments () const override;
148
149 /* See probe.h. */
150 struct value *evaluate_argument (unsigned n,
151 struct frame_info *frame) override;
152
153 /* See probe.h. */
154 void compile_to_ax (struct agent_expr *aexpr,
155 struct axs_value *axs_value,
156 unsigned n) override;
157
158 /* See probe.h. */
159 void set_semaphore (struct objfile *objfile,
160 struct gdbarch *gdbarch) override;
161
162 /* See probe.h. */
163 void clear_semaphore (struct objfile *objfile,
164 struct gdbarch *gdbarch) override;
165
166 /* See probe.h. */
167 const static_probe_ops *get_static_ops () const override;
168
169 /* See probe.h. */
170 std::vector<const char *> gen_info_probes_table_values () const override;
171
172 /* Return argument N of probe.
173
174 If the probe's arguments have not been parsed yet, parse them. If
175 there are no arguments, throw an exception (error). Otherwise,
176 return the requested argument. */
177 struct stap_probe_arg *get_arg_by_number (unsigned n,
178 struct gdbarch *gdbarch)
179 {
180 if (!m_have_parsed_args)
181 this->parse_arguments (gdbarch);
182
183 gdb_assert (m_have_parsed_args);
184 if (m_parsed_args.empty ())
185 internal_error (__FILE__, __LINE__,
186 _("Probe '%s' apparently does not have arguments, but \n"
187 "GDB is requesting its argument number %u anyway. "
188 "This should not happen. Please report this bug."),
189 this->get_name ().c_str (), n);
190
191 if (n > m_parsed_args.size ())
192 internal_error (__FILE__, __LINE__,
193 _("Probe '%s' has %d arguments, but GDB is requesting\n"
194 "argument %u. This should not happen. Please\n"
195 "report this bug."),
196 this->get_name ().c_str (),
197 (int) m_parsed_args.size (), n);
198
199 return &m_parsed_args[n];
200 }
201
202 /* Function which parses an argument string from the probe,
203 correctly splitting the arguments and storing their information
204 in properly ways.
205
206 Consider the following argument string (x86 syntax):
207
208 `4@%eax 4@$10'
209
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);
214
215private:
55aa24fb 216 /* If the probe has a semaphore associated, then this is the value of
729662a5 217 it, relative to SECT_OFF_DATA. */
0e9ae10f 218 CORE_ADDR m_sem_addr;
55aa24fb 219
0e9ae10f
SDJ
220 /* True if the arguments have been parsed. */
221 bool m_have_parsed_args;
97c2dca0 222
0e9ae10f
SDJ
223 /* The text version of the probe's arguments, unparsed. */
224 const char *m_unparsed_args_text;
55aa24fb 225
0e9ae10f
SDJ
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;
55aa24fb
SDJ
230};
231
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.
235
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'". */
239
240enum stap_operand_prec
241{
242 /* Lowest precedence, used for non-recognized operands or for the beginning
243 of the parsing process. */
244 STAP_OPERAND_PREC_NONE = 0,
245
246 /* Precedence of logical OR. */
247 STAP_OPERAND_PREC_LOGICAL_OR,
248
249 /* Precedence of logical AND. */
250 STAP_OPERAND_PREC_LOGICAL_AND,
251
252 /* Precedence of additive (plus, minus) and comparative (equal, less,
253 greater-than, etc) operands. */
254 STAP_OPERAND_PREC_ADD_CMP,
255
256 /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
257 logical NOT). */
258 STAP_OPERAND_PREC_BITWISE,
259
260 /* Precedence of multiplicative operands (multiplication, division,
261 remainder, left shift and right shift). */
262 STAP_OPERAND_PREC_MUL
263};
264
af2d9bee 265static void stap_parse_argument_1 (struct stap_parse_info *p, bool has_lhs,
55aa24fb
SDJ
266 enum stap_operand_prec prec);
267
268static void stap_parse_argument_conditionally (struct stap_parse_info *p);
269
af2d9bee 270/* Returns true if *S is an operator, false otherwise. */
55aa24fb 271
af2d9bee 272static bool stap_is_operator (const char *op);
55aa24fb
SDJ
273
274static void
275show_stapexpressiondebug (struct ui_file *file, int from_tty,
276 struct cmd_list_element *c, const char *value)
277{
278 fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"),
279 value);
280}
281
282/* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
283 if the operator code was not recognized. */
284
285static enum stap_operand_prec
286stap_get_operator_prec (enum exp_opcode op)
287{
288 switch (op)
289 {
290 case BINOP_LOGICAL_OR:
291 return STAP_OPERAND_PREC_LOGICAL_OR;
292
293 case BINOP_LOGICAL_AND:
294 return STAP_OPERAND_PREC_LOGICAL_AND;
295
296 case BINOP_ADD:
297 case BINOP_SUB:
298 case BINOP_EQUAL:
299 case BINOP_NOTEQUAL:
300 case BINOP_LESS:
301 case BINOP_LEQ:
302 case BINOP_GTR:
303 case BINOP_GEQ:
304 return STAP_OPERAND_PREC_ADD_CMP;
305
306 case BINOP_BITWISE_IOR:
307 case BINOP_BITWISE_AND:
308 case BINOP_BITWISE_XOR:
309 case UNOP_LOGICAL_NOT:
310 return STAP_OPERAND_PREC_BITWISE;
311
312 case BINOP_MUL:
313 case BINOP_DIV:
314 case BINOP_REM:
315 case BINOP_LSH:
316 case BINOP_RSH:
317 return STAP_OPERAND_PREC_MUL;
318
319 default:
320 return STAP_OPERAND_PREC_NONE;
321 }
322}
323
3ca58cde
SDJ
324/* Given S, read the operator in it. Return the EXP_OPCODE which
325 represents the operator detected, or throw an error if no operator
326 was found. */
55aa24fb 327
fcf57f19
SDJ
328static enum exp_opcode
329stap_get_opcode (const char **s)
55aa24fb
SDJ
330{
331 const char c = **s;
fcf57f19 332 enum exp_opcode op;
55aa24fb
SDJ
333
334 *s += 1;
335
336 switch (c)
337 {
338 case '*':
fcf57f19 339 op = BINOP_MUL;
55aa24fb
SDJ
340 break;
341
342 case '/':
fcf57f19 343 op = BINOP_DIV;
55aa24fb
SDJ
344 break;
345
346 case '%':
fcf57f19 347 op = BINOP_REM;
55aa24fb
SDJ
348 break;
349
350 case '<':
fcf57f19 351 op = BINOP_LESS;
55aa24fb
SDJ
352 if (**s == '<')
353 {
354 *s += 1;
fcf57f19 355 op = BINOP_LSH;
55aa24fb
SDJ
356 }
357 else if (**s == '=')
358 {
359 *s += 1;
fcf57f19 360 op = BINOP_LEQ;
55aa24fb
SDJ
361 }
362 else if (**s == '>')
363 {
364 *s += 1;
fcf57f19 365 op = BINOP_NOTEQUAL;
55aa24fb
SDJ
366 }
367 break;
368
369 case '>':
fcf57f19 370 op = BINOP_GTR;
55aa24fb
SDJ
371 if (**s == '>')
372 {
373 *s += 1;
fcf57f19 374 op = BINOP_RSH;
55aa24fb
SDJ
375 }
376 else if (**s == '=')
377 {
378 *s += 1;
fcf57f19 379 op = BINOP_GEQ;
55aa24fb
SDJ
380 }
381 break;
382
383 case '|':
fcf57f19 384 op = BINOP_BITWISE_IOR;
55aa24fb
SDJ
385 if (**s == '|')
386 {
387 *s += 1;
fcf57f19 388 op = BINOP_LOGICAL_OR;
55aa24fb
SDJ
389 }
390 break;
391
392 case '&':
fcf57f19 393 op = BINOP_BITWISE_AND;
55aa24fb
SDJ
394 if (**s == '&')
395 {
396 *s += 1;
fcf57f19 397 op = BINOP_LOGICAL_AND;
55aa24fb
SDJ
398 }
399 break;
400
401 case '^':
fcf57f19 402 op = BINOP_BITWISE_XOR;
55aa24fb
SDJ
403 break;
404
405 case '!':
fcf57f19 406 op = UNOP_LOGICAL_NOT;
55aa24fb
SDJ
407 break;
408
409 case '+':
fcf57f19 410 op = BINOP_ADD;
55aa24fb
SDJ
411 break;
412
413 case '-':
fcf57f19 414 op = BINOP_SUB;
55aa24fb
SDJ
415 break;
416
417 case '=':
fcf57f19
SDJ
418 gdb_assert (**s == '=');
419 op = BINOP_EQUAL;
55aa24fb
SDJ
420 break;
421
422 default:
f469e8ce
SDJ
423 error (_("Invalid opcode in expression `%s' for SystemTap"
424 "probe"), *s);
55aa24fb
SDJ
425 }
426
fcf57f19 427 return op;
55aa24fb
SDJ
428}
429
430/* Given the bitness of the argument, represented by B, return the
3ca58cde
SDJ
431 corresponding `struct type *', or throw an error if B is
432 unknown. */
55aa24fb
SDJ
433
434static struct type *
435stap_get_expected_argument_type (struct gdbarch *gdbarch,
f469e8ce 436 enum stap_arg_bitness b,
0e9ae10f 437 const char *probe_name)
55aa24fb
SDJ
438{
439 switch (b)
440 {
441 case STAP_ARG_BITNESS_UNDEFINED:
442 if (gdbarch_addr_bit (gdbarch) == 32)
443 return builtin_type (gdbarch)->builtin_uint32;
444 else
445 return builtin_type (gdbarch)->builtin_uint64;
446
30a1e6cc
SDJ
447 case STAP_ARG_BITNESS_8BIT_UNSIGNED:
448 return builtin_type (gdbarch)->builtin_uint8;
449
450 case STAP_ARG_BITNESS_8BIT_SIGNED:
451 return builtin_type (gdbarch)->builtin_int8;
452
453 case STAP_ARG_BITNESS_16BIT_UNSIGNED:
454 return builtin_type (gdbarch)->builtin_uint16;
455
456 case STAP_ARG_BITNESS_16BIT_SIGNED:
457 return builtin_type (gdbarch)->builtin_int16;
458
55aa24fb
SDJ
459 case STAP_ARG_BITNESS_32BIT_SIGNED:
460 return builtin_type (gdbarch)->builtin_int32;
461
462 case STAP_ARG_BITNESS_32BIT_UNSIGNED:
463 return builtin_type (gdbarch)->builtin_uint32;
464
465 case STAP_ARG_BITNESS_64BIT_SIGNED:
466 return builtin_type (gdbarch)->builtin_int64;
467
468 case STAP_ARG_BITNESS_64BIT_UNSIGNED:
469 return builtin_type (gdbarch)->builtin_uint64;
470
471 default:
0e9ae10f 472 error (_("Undefined bitness for probe '%s'."), probe_name);
55aa24fb
SDJ
473 break;
474 }
475}
476
05c0465e
SDJ
477/* Helper function to check for a generic list of prefixes. GDBARCH
478 is the current gdbarch being used. S is the expression being
479 analyzed. If R is not NULL, it will be used to return the found
480 prefix. PREFIXES is the list of expected prefixes.
481
482 This function does a case-insensitive match.
483
af2d9bee 484 Return true if any prefix has been found, false otherwise. */
05c0465e 485
af2d9bee 486static bool
05c0465e
SDJ
487stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s,
488 const char **r, const char *const *prefixes)
489{
490 const char *const *p;
491
492 if (prefixes == NULL)
493 {
494 if (r != NULL)
495 *r = "";
496
af2d9bee 497 return true;
05c0465e
SDJ
498 }
499
500 for (p = prefixes; *p != NULL; ++p)
97c2dca0
SDJ
501 if (strncasecmp (s, *p, strlen (*p)) == 0)
502 {
503 if (r != NULL)
504 *r = *p;
05c0465e 505
af2d9bee 506 return true;
97c2dca0 507 }
05c0465e 508
af2d9bee 509 return false;
05c0465e
SDJ
510}
511
af2d9bee
SDJ
512/* Return true if S points to a register prefix, false otherwise. For
513 a description of the arguments, look at stap_is_generic_prefix. */
05c0465e 514
af2d9bee 515static bool
05c0465e
SDJ
516stap_is_register_prefix (struct gdbarch *gdbarch, const char *s,
517 const char **r)
518{
519 const char *const *t = gdbarch_stap_register_prefixes (gdbarch);
520
521 return stap_is_generic_prefix (gdbarch, s, r, t);
522}
523
af2d9bee 524/* Return true if S points to a register indirection prefix, false
05c0465e
SDJ
525 otherwise. For a description of the arguments, look at
526 stap_is_generic_prefix. */
527
af2d9bee 528static bool
05c0465e
SDJ
529stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s,
530 const char **r)
531{
532 const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch);
533
534 return stap_is_generic_prefix (gdbarch, s, r, t);
535}
536
af2d9bee
SDJ
537/* Return true if S points to an integer prefix, false otherwise. For
538 a description of the arguments, look at stap_is_generic_prefix.
05c0465e
SDJ
539
540 This function takes care of analyzing whether we are dealing with
541 an expected integer prefix, or, if there is no integer prefix to be
542 expected, whether we are dealing with a digit. It does a
543 case-insensitive match. */
544
af2d9bee 545static bool
05c0465e
SDJ
546stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s,
547 const char **r)
548{
549 const char *const *t = gdbarch_stap_integer_prefixes (gdbarch);
550 const char *const *p;
551
552 if (t == NULL)
553 {
554 /* A NULL value here means that integers do not have a prefix.
555 We just check for a digit then. */
556 if (r != NULL)
557 *r = "";
558
af2d9bee 559 return isdigit (*s) > 0;
05c0465e
SDJ
560 }
561
562 for (p = t; *p != NULL; ++p)
563 {
564 size_t len = strlen (*p);
565
566 if ((len == 0 && isdigit (*s))
567 || (len > 0 && strncasecmp (s, *p, len) == 0))
568 {
569 /* Integers may or may not have a prefix. The "len == 0"
570 check covers the case when integers do not have a prefix
571 (therefore, we just check if we have a digit). The call
572 to "strncasecmp" covers the case when they have a
573 prefix. */
574 if (r != NULL)
575 *r = *p;
576
af2d9bee 577 return true;
05c0465e
SDJ
578 }
579 }
580
af2d9bee 581 return false;
05c0465e
SDJ
582}
583
584/* Helper function to check for a generic list of suffixes. If we are
585 not expecting any suffixes, then it just returns 1. If we are
af2d9bee
SDJ
586 expecting at least one suffix, then it returns true if a suffix has
587 been found, false otherwise. GDBARCH is the current gdbarch being
05c0465e
SDJ
588 used. S is the expression being analyzed. If R is not NULL, it
589 will be used to return the found suffix. SUFFIXES is the list of
590 expected suffixes. This function does a case-insensitive
591 match. */
592
af2d9bee 593static bool
05c0465e
SDJ
594stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s,
595 const char **r, const char *const *suffixes)
596{
597 const char *const *p;
af2d9bee 598 bool found = false;
05c0465e
SDJ
599
600 if (suffixes == NULL)
601 {
602 if (r != NULL)
603 *r = "";
604
af2d9bee 605 return true;
05c0465e
SDJ
606 }
607
608 for (p = suffixes; *p != NULL; ++p)
609 if (strncasecmp (s, *p, strlen (*p)) == 0)
610 {
611 if (r != NULL)
612 *r = *p;
613
af2d9bee 614 found = true;
05c0465e
SDJ
615 break;
616 }
617
618 return found;
619}
620
af2d9bee
SDJ
621/* Return true if S points to an integer suffix, false otherwise. For
622 a description of the arguments, look at
05c0465e
SDJ
623 stap_generic_check_suffix. */
624
af2d9bee 625static bool
05c0465e
SDJ
626stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s,
627 const char **r)
628{
629 const char *const *p = gdbarch_stap_integer_suffixes (gdbarch);
630
631 return stap_generic_check_suffix (gdbarch, s, r, p);
632}
633
af2d9bee
SDJ
634/* Return true if S points to a register suffix, false otherwise. For
635 a description of the arguments, look at
05c0465e
SDJ
636 stap_generic_check_suffix. */
637
af2d9bee 638static bool
05c0465e
SDJ
639stap_check_register_suffix (struct gdbarch *gdbarch, const char *s,
640 const char **r)
641{
642 const char *const *p = gdbarch_stap_register_suffixes (gdbarch);
643
644 return stap_generic_check_suffix (gdbarch, s, r, p);
645}
646
af2d9bee 647/* Return true if S points to a register indirection suffix, false
05c0465e
SDJ
648 otherwise. For a description of the arguments, look at
649 stap_generic_check_suffix. */
650
af2d9bee 651static bool
05c0465e
SDJ
652stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s,
653 const char **r)
654{
655 const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch);
656
657 return stap_generic_check_suffix (gdbarch, s, r, p);
658}
659
55aa24fb
SDJ
660/* Function responsible for parsing a register operand according to
661 SystemTap parlance. Assuming:
662
663 RP = register prefix
664 RS = register suffix
665 RIP = register indirection prefix
666 RIS = register indirection suffix
667
668 Then a register operand can be:
669
670 [RIP] [RP] REGISTER [RS] [RIS]
671
672 This function takes care of a register's indirection, displacement and
673 direct access. It also takes into consideration the fact that some
674 registers are named differently inside and outside GDB, e.g., PPC's
675 general-purpose registers are represented by integers in the assembly
676 language (e.g., `15' is the 15th general-purpose register), but inside
677 GDB they have a prefix (the letter `r') appended. */
678
679static void
680stap_parse_register_operand (struct stap_parse_info *p)
681{
682 /* Simple flag to indicate whether we have seen a minus signal before
683 certain number. */
af2d9bee 684 bool got_minus = false;
55aa24fb
SDJ
685 /* Flags to indicate whether this register access is being displaced and/or
686 indirected. */
af2d9bee
SDJ
687 bool disp_p = false;
688 bool indirect_p = false;
55aa24fb 689 struct gdbarch *gdbarch = p->gdbarch;
55aa24fb
SDJ
690 /* Needed to generate the register name as a part of an expression. */
691 struct stoken str;
55aa24fb
SDJ
692 /* Variables used to extract the register name from the probe's
693 argument. */
694 const char *start;
55aa24fb 695 const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
55aa24fb 696 const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
05c0465e
SDJ
697 const char *reg_prefix;
698 const char *reg_ind_prefix;
699 const char *reg_suffix;
700 const char *reg_ind_suffix;
55aa24fb
SDJ
701
702 /* Checking for a displacement argument. */
703 if (*p->arg == '+')
704 {
705 /* If it's a plus sign, we don't need to do anything, just advance the
706 pointer. */
707 ++p->arg;
708 }
f1bb75ab 709 else if (*p->arg == '-')
55aa24fb 710 {
af2d9bee 711 got_minus = true;
55aa24fb
SDJ
712 ++p->arg;
713 }
714
715 if (isdigit (*p->arg))
716 {
717 /* The value of the displacement. */
718 long displacement;
a0bcdaa7 719 char *endp;
55aa24fb 720
af2d9bee 721 disp_p = true;
a0bcdaa7
PA
722 displacement = strtol (p->arg, &endp, 10);
723 p->arg = endp;
55aa24fb
SDJ
724
725 /* Generating the expression for the displacement. */
410a0ff2
SDJ
726 write_exp_elt_opcode (&p->pstate, OP_LONG);
727 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
728 write_exp_elt_longcst (&p->pstate, displacement);
729 write_exp_elt_opcode (&p->pstate, OP_LONG);
55aa24fb 730 if (got_minus)
410a0ff2 731 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
55aa24fb
SDJ
732 }
733
734 /* Getting rid of register indirection prefix. */
05c0465e 735 if (stap_is_register_indirection_prefix (gdbarch, p->arg, &reg_ind_prefix))
55aa24fb 736 {
af2d9bee 737 indirect_p = true;
05c0465e 738 p->arg += strlen (reg_ind_prefix);
55aa24fb
SDJ
739 }
740
741 if (disp_p && !indirect_p)
742 error (_("Invalid register displacement syntax on expression `%s'."),
743 p->saved_arg);
744
745 /* Getting rid of register prefix. */
05c0465e
SDJ
746 if (stap_is_register_prefix (gdbarch, p->arg, &reg_prefix))
747 p->arg += strlen (reg_prefix);
55aa24fb
SDJ
748
749 /* Now we should have only the register name. Let's extract it and get
750 the associated number. */
751 start = p->arg;
752
753 /* We assume the register name is composed by letters and numbers. */
754 while (isalnum (*p->arg))
755 ++p->arg;
756
677052f2 757 std::string regname (start, p->arg - start);
55aa24fb
SDJ
758
759 /* We only add the GDB's register prefix/suffix if we are dealing with
760 a numeric register. */
677052f2 761 if (isdigit (*start))
55aa24fb 762 {
677052f2
SDJ
763 if (gdb_reg_prefix != NULL)
764 regname = gdb_reg_prefix + regname;
55aa24fb 765
677052f2
SDJ
766 if (gdb_reg_suffix != NULL)
767 regname += gdb_reg_suffix;
55aa24fb 768 }
55aa24fb 769
7d7571f0
SDJ
770 int regnum = user_reg_map_name_to_regnum (gdbarch, regname.c_str (),
771 regname.size ());
772
55aa24fb 773 /* Is this a valid register name? */
7d7571f0 774 if (regnum == -1)
55aa24fb 775 error (_("Invalid register name `%s' on expression `%s'."),
677052f2 776 regname.c_str (), p->saved_arg);
55aa24fb 777
7d7571f0
SDJ
778 /* Check if there's any special treatment that the arch-specific
779 code would like to perform on the register name. */
780 if (gdbarch_stap_adjust_register_p (gdbarch))
781 {
6b78c3f8
AB
782 std::string newregname
783 = gdbarch_stap_adjust_register (gdbarch, p, regname, regnum);
7d7571f0 784
6b78c3f8 785 if (regname != newregname)
7d7571f0
SDJ
786 {
787 /* This is just a check we perform to make sure that the
788 arch-dependent code has provided us with a valid
789 register name. */
6b78c3f8
AB
790 regnum = user_reg_map_name_to_regnum (gdbarch, newregname.c_str (),
791 newregname.size ());
7d7571f0
SDJ
792
793 if (regnum == -1)
794 internal_error (__FILE__, __LINE__,
795 _("Invalid register name '%s' after replacing it"
796 " (previous name was '%s')"),
6b78c3f8
AB
797 newregname.c_str (), regname.c_str ());
798
799 regname = newregname;
7d7571f0
SDJ
800 }
801 }
802
410a0ff2 803 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
677052f2
SDJ
804 str.ptr = regname.c_str ();
805 str.length = regname.size ();
410a0ff2
SDJ
806 write_exp_string (&p->pstate, str);
807 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
55aa24fb
SDJ
808
809 if (indirect_p)
810 {
811 if (disp_p)
410a0ff2 812 write_exp_elt_opcode (&p->pstate, BINOP_ADD);
55aa24fb
SDJ
813
814 /* Casting to the expected type. */
410a0ff2
SDJ
815 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
816 write_exp_elt_type (&p->pstate, lookup_pointer_type (p->arg_type));
817 write_exp_elt_opcode (&p->pstate, UNOP_CAST);
55aa24fb 818
410a0ff2 819 write_exp_elt_opcode (&p->pstate, UNOP_IND);
55aa24fb
SDJ
820 }
821
822 /* Getting rid of the register name suffix. */
05c0465e
SDJ
823 if (stap_check_register_suffix (gdbarch, p->arg, &reg_suffix))
824 p->arg += strlen (reg_suffix);
825 else
826 error (_("Missing register name suffix on expression `%s'."),
827 p->saved_arg);
55aa24fb
SDJ
828
829 /* Getting rid of the register indirection suffix. */
05c0465e 830 if (indirect_p)
55aa24fb 831 {
05c0465e
SDJ
832 if (stap_check_register_indirection_suffix (gdbarch, p->arg,
833 &reg_ind_suffix))
834 p->arg += strlen (reg_ind_suffix);
835 else
836 error (_("Missing indirection suffix on expression `%s'."),
837 p->saved_arg);
55aa24fb
SDJ
838 }
839}
840
841/* This function is responsible for parsing a single operand.
842
843 A single operand can be:
844
845 - an unary operation (e.g., `-5', `~2', or even with subexpressions
dda83cd7 846 like `-(2 + 1)')
55aa24fb 847 - a register displacement, which will be treated as a register
dda83cd7 848 operand (e.g., `-4(%eax)' on x86)
55aa24fb
SDJ
849 - a numeric constant, or
850 - a register operand (see function `stap_parse_register_operand')
851
852 The function also calls special-handling functions to deal with
853 unrecognized operands, allowing arch-specific parsers to be
854 created. */
855
856static void
857stap_parse_single_operand (struct stap_parse_info *p)
858{
859 struct gdbarch *gdbarch = p->gdbarch;
05c0465e 860 const char *int_prefix = NULL;
55aa24fb
SDJ
861
862 /* We first try to parse this token as a "special token". */
f1bb75ab
SDJ
863 if (gdbarch_stap_parse_special_token_p (gdbarch)
864 && (gdbarch_stap_parse_special_token (gdbarch, p) != 0))
865 {
866 /* If the return value of the above function is not zero,
867 it means it successfully parsed the special token.
55aa24fb 868
f1bb75ab
SDJ
869 If it is NULL, we try to parse it using our method. */
870 return;
871 }
55aa24fb 872
6f52fdf4 873 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!')
55aa24fb
SDJ
874 {
875 char c = *p->arg;
55aa24fb
SDJ
876 /* We use this variable to do a lookahead. */
877 const char *tmp = p->arg;
af2d9bee 878 bool has_digit = false;
55aa24fb 879
97c2dca0 880 /* Skipping signal. */
55aa24fb
SDJ
881 ++tmp;
882
883 /* This is an unary operation. Here is a list of allowed tokens
884 here:
885
886 - numeric literal;
887 - number (from register displacement)
888 - subexpression (beginning with `(')
889
890 We handle the register displacement here, and the other cases
891 recursively. */
892 if (p->inside_paren_p)
f1735a53 893 tmp = skip_spaces (tmp);
55aa24fb 894
474ca4f6 895 while (isdigit (*tmp))
a0bcdaa7 896 {
474ca4f6
SDJ
897 /* We skip the digit here because we are only interested in
898 knowing what kind of unary operation this is. The digit
899 will be handled by one of the functions that will be
900 called below ('stap_parse_argument_conditionally' or
901 'stap_parse_register_operand'). */
902 ++tmp;
af2d9bee 903 has_digit = true;
a0bcdaa7 904 }
55aa24fb 905
474ca4f6
SDJ
906 if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp,
907 NULL))
55aa24fb
SDJ
908 {
909 /* If we are here, it means it is a displacement. The only
910 operations allowed here are `-' and `+'. */
f1bb75ab 911 if (c != '-' && c != '+')
55aa24fb
SDJ
912 error (_("Invalid operator `%c' for register displacement "
913 "on expression `%s'."), c, p->saved_arg);
914
915 stap_parse_register_operand (p);
916 }
474ca4f6
SDJ
917 else
918 {
919 /* This is not a displacement. We skip the operator, and
920 deal with it when the recursion returns. */
921 ++p->arg;
922 stap_parse_argument_conditionally (p);
923 if (c == '-')
924 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
925 else if (c == '~')
926 write_exp_elt_opcode (&p->pstate, UNOP_COMPLEMENT);
6f52fdf4
SDJ
927 else if (c == '!')
928 write_exp_elt_opcode (&p->pstate, UNOP_LOGICAL_NOT);
474ca4f6 929 }
55aa24fb
SDJ
930 }
931 else if (isdigit (*p->arg))
932 {
933 /* A temporary variable, needed for lookahead. */
934 const char *tmp = p->arg;
a0bcdaa7 935 char *endp;
55aa24fb
SDJ
936 long number;
937
05c0465e
SDJ
938 /* We can be dealing with a numeric constant, or with a register
939 displacement. */
a0bcdaa7
PA
940 number = strtol (tmp, &endp, 10);
941 tmp = endp;
55aa24fb
SDJ
942
943 if (p->inside_paren_p)
f1735a53 944 tmp = skip_spaces (tmp);
05c0465e
SDJ
945
946 /* If "stap_is_integer_prefix" returns true, it means we can
947 accept integers without a prefix here. But we also need to
948 check whether the next token (i.e., "tmp") is not a register
949 indirection prefix. */
950 if (stap_is_integer_prefix (gdbarch, p->arg, NULL)
951 && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
55aa24fb 952 {
05c0465e
SDJ
953 const char *int_suffix;
954
55aa24fb 955 /* We are dealing with a numeric constant. */
410a0ff2
SDJ
956 write_exp_elt_opcode (&p->pstate, OP_LONG);
957 write_exp_elt_type (&p->pstate,
958 builtin_type (gdbarch)->builtin_long);
959 write_exp_elt_longcst (&p->pstate, number);
960 write_exp_elt_opcode (&p->pstate, OP_LONG);
55aa24fb
SDJ
961
962 p->arg = tmp;
963
05c0465e
SDJ
964 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
965 p->arg += strlen (int_suffix);
966 else
967 error (_("Invalid constant suffix on expression `%s'."),
968 p->saved_arg);
55aa24fb 969 }
05c0465e 970 else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
55aa24fb
SDJ
971 stap_parse_register_operand (p);
972 else
973 error (_("Unknown numeric token on expression `%s'."),
974 p->saved_arg);
975 }
05c0465e 976 else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
55aa24fb
SDJ
977 {
978 /* We are dealing with a numeric constant. */
979 long number;
a0bcdaa7 980 char *endp;
05c0465e 981 const char *int_suffix;
55aa24fb 982
05c0465e 983 p->arg += strlen (int_prefix);
a0bcdaa7
PA
984 number = strtol (p->arg, &endp, 10);
985 p->arg = endp;
55aa24fb 986
410a0ff2
SDJ
987 write_exp_elt_opcode (&p->pstate, OP_LONG);
988 write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
989 write_exp_elt_longcst (&p->pstate, number);
990 write_exp_elt_opcode (&p->pstate, OP_LONG);
55aa24fb 991
05c0465e
SDJ
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);
55aa24fb 997 }
05c0465e
SDJ
998 else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
999 || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
55aa24fb
SDJ
1000 stap_parse_register_operand (p);
1001 else
1002 error (_("Operator `%c' not recognized on expression `%s'."),
1003 *p->arg, p->saved_arg);
1004}
1005
1006/* This function parses an argument conditionally, based on single or
1007 non-single operands. A non-single operand would be a parenthesized
1008 expression (e.g., `(2 + 1)'), and a single operand is anything that
1009 starts with `-', `~', `+' (i.e., unary operators), a digit, or
1010 something recognized by `gdbarch_stap_is_single_operand'. */
1011
1012static void
1013stap_parse_argument_conditionally (struct stap_parse_info *p)
1014{
97c2dca0
SDJ
1015 gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));
1016
6f52fdf4 1017 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' || *p->arg == '!'
55aa24fb
SDJ
1018 || isdigit (*p->arg)
1019 || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
1020 stap_parse_single_operand (p);
1021 else if (*p->arg == '(')
1022 {
1023 /* We are dealing with a parenthesized operand. It means we
1024 have to parse it as it was a separate expression, without
1025 left-side or precedence. */
1026 ++p->arg;
f1735a53 1027 p->arg = skip_spaces (p->arg);
55aa24fb
SDJ
1028 ++p->inside_paren_p;
1029
1030 stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
1031
6f52fdf4 1032 p->arg = skip_spaces (p->arg);
55aa24fb 1033 if (*p->arg != ')')
9bb305b3 1034 error (_("Missing close-parenthesis on expression `%s'."),
55aa24fb
SDJ
1035 p->saved_arg);
1036
6f52fdf4 1037 --p->inside_paren_p;
55aa24fb
SDJ
1038 ++p->arg;
1039 if (p->inside_paren_p)
f1735a53 1040 p->arg = skip_spaces (p->arg);
55aa24fb
SDJ
1041 }
1042 else
1043 error (_("Cannot parse expression `%s'."), p->saved_arg);
1044}
1045
1046/* Helper function for `stap_parse_argument'. Please, see its comments to
1047 better understand what this function does. */
1048
1049static void
af2d9bee 1050stap_parse_argument_1 (struct stap_parse_info *p, bool has_lhs,
55aa24fb
SDJ
1051 enum stap_operand_prec prec)
1052{
1053 /* This is an operator-precedence parser.
1054
1055 We work with left- and right-sides of expressions, and
1056 parse them depending on the precedence of the operators
1057 we find. */
1058
97c2dca0
SDJ
1059 gdb_assert (p->arg != NULL);
1060
55aa24fb 1061 if (p->inside_paren_p)
f1735a53 1062 p->arg = skip_spaces (p->arg);
55aa24fb
SDJ
1063
1064 if (!has_lhs)
1065 {
1066 /* We were called without a left-side, either because this is the
1067 first call, or because we were called to parse a parenthesized
1068 expression. It doesn't really matter; we have to parse the
1069 left-side in order to continue the process. */
1070 stap_parse_argument_conditionally (p);
1071 }
1072
6f52fdf4
SDJ
1073 if (p->inside_paren_p)
1074 p->arg = skip_spaces (p->arg);
1075
55aa24fb
SDJ
1076 /* Start to parse the right-side, and to "join" left and right sides
1077 depending on the operation specified.
1078
1079 This loop shall continue until we run out of characters in the input,
1080 or until we find a close-parenthesis, which means that we've reached
1081 the end of a sub-expression. */
97c2dca0 1082 while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
55aa24fb
SDJ
1083 {
1084 const char *tmp_exp_buf;
1085 enum exp_opcode opcode;
1086 enum stap_operand_prec cur_prec;
1087
fcf57f19 1088 if (!stap_is_operator (p->arg))
55aa24fb
SDJ
1089 error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
1090 p->saved_arg);
1091
1092 /* We have to save the current value of the expression buffer because
1093 the `stap_get_opcode' modifies it in order to get the current
1094 operator. If this operator's precedence is lower than PREC, we
1095 should return and not advance the expression buffer pointer. */
1096 tmp_exp_buf = p->arg;
fcf57f19 1097 opcode = stap_get_opcode (&tmp_exp_buf);
55aa24fb
SDJ
1098
1099 cur_prec = stap_get_operator_prec (opcode);
1100 if (cur_prec < prec)
1101 {
1102 /* If the precedence of the operator that we are seeing now is
1103 lower than the precedence of the first operator seen before
1104 this parsing process began, it means we should stop parsing
1105 and return. */
1106 break;
1107 }
1108
1109 p->arg = tmp_exp_buf;
1110 if (p->inside_paren_p)
f1735a53 1111 p->arg = skip_spaces (p->arg);
55aa24fb 1112
6f52fdf4
SDJ
1113 /* Parse the right-side of the expression.
1114
1115 We save whether the right-side is a parenthesized
1116 subexpression because, if it is, we will have to finish
1117 processing this part of the expression before continuing. */
1118 bool paren_subexp = *p->arg == '(';
1119
55aa24fb 1120 stap_parse_argument_conditionally (p);
6f52fdf4
SDJ
1121 if (p->inside_paren_p)
1122 p->arg = skip_spaces (p->arg);
1123 if (paren_subexp)
1124 {
1125 write_exp_elt_opcode (&p->pstate, opcode);
1126 continue;
1127 }
55aa24fb
SDJ
1128
1129 /* While we still have operators, try to parse another
1130 right-side, but using the current right-side as a left-side. */
97c2dca0 1131 while (*p->arg != '\0' && stap_is_operator (p->arg))
55aa24fb
SDJ
1132 {
1133 enum exp_opcode lookahead_opcode;
1134 enum stap_operand_prec lookahead_prec;
1135
1136 /* Saving the current expression buffer position. The explanation
1137 is the same as above. */
1138 tmp_exp_buf = p->arg;
fcf57f19 1139 lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
55aa24fb
SDJ
1140 lookahead_prec = stap_get_operator_prec (lookahead_opcode);
1141
1142 if (lookahead_prec <= prec)
1143 {
1144 /* If we are dealing with an operator whose precedence is lower
1145 than the first one, just abandon the attempt. */
1146 break;
1147 }
1148
1149 /* Parse the right-side of the expression, but since we already
1150 have a left-side at this point, set `has_lhs' to 1. */
1151 stap_parse_argument_1 (p, 1, lookahead_prec);
6f52fdf4
SDJ
1152 if (p->inside_paren_p)
1153 p->arg = skip_spaces (p->arg);
55aa24fb
SDJ
1154 }
1155
410a0ff2 1156 write_exp_elt_opcode (&p->pstate, opcode);
55aa24fb
SDJ
1157 }
1158}
1159
1160/* Parse a probe's argument.
1161
1162 Assuming that:
1163
1164 LP = literal integer prefix
1165 LS = literal integer suffix
1166
1167 RP = register prefix
1168 RS = register suffix
1169
1170 RIP = register indirection prefix
1171 RIS = register indirection suffix
1172
1173 This routine assumes that arguments' tokens are of the form:
1174
1175 - [LP] NUMBER [LS]
1176 - [RP] REGISTER [RS]
1177 - [RIP] [RP] REGISTER [RS] [RIS]
1178 - If we find a number without LP, we try to parse it as a literal integer
1179 constant (if LP == NULL), or as a register displacement.
1180 - We count parenthesis, and only skip whitespaces if we are inside them.
1181 - If we find an operator, we skip it.
1182
1183 This function can also call a special function that will try to match
0e9ae10f
SDJ
1184 unknown tokens. It will return the expression_up generated from
1185 parsing the argument. */
55aa24fb 1186
0e9ae10f 1187static expression_up
55aa24fb
SDJ
1188stap_parse_argument (const char **arg, struct type *atype,
1189 struct gdbarch *gdbarch)
1190{
55aa24fb 1191 /* We need to initialize the expression buffer, in order to begin
f7088df3
SDJ
1192 our parsing efforts. We use language_c here because we may need
1193 to do pointer arithmetics. */
1201a264 1194 struct stap_parse_info p (*arg, atype, language_def (language_c),
e9d9f57e 1195 gdbarch);
55aa24fb
SDJ
1196
1197 stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
1198
55aa24fb
SDJ
1199 gdb_assert (p.inside_paren_p == 0);
1200
1201 /* Casting the final expression to the appropriate type. */
410a0ff2
SDJ
1202 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
1203 write_exp_elt_type (&p.pstate, atype);
1204 write_exp_elt_opcode (&p.pstate, UNOP_CAST);
55aa24fb 1205
f1735a53 1206 p.arg = skip_spaces (p.arg);
55aa24fb
SDJ
1207 *arg = p.arg;
1208
e9d9f57e 1209 return p.pstate.release ();
55aa24fb
SDJ
1210}
1211
0e9ae10f 1212/* Implementation of 'parse_arguments' method. */
55aa24fb 1213
0e9ae10f
SDJ
1214void
1215stap_probe::parse_arguments (struct gdbarch *gdbarch)
55aa24fb
SDJ
1216{
1217 const char *cur;
55aa24fb 1218
0e9ae10f
SDJ
1219 gdb_assert (!m_have_parsed_args);
1220 cur = m_unparsed_args_text;
1221 m_have_parsed_args = true;
55aa24fb 1222
97c2dca0 1223 if (cur == NULL || *cur == '\0' || *cur == ':')
55aa24fb
SDJ
1224 return;
1225
97c2dca0 1226 while (*cur != '\0')
55aa24fb 1227 {
0e9ae10f
SDJ
1228 enum stap_arg_bitness bitness;
1229 bool got_minus = false;
55aa24fb
SDJ
1230
1231 /* We expect to find something like:
1232
1233 N@OP
1234
30a1e6cc 1235 Where `N' can be [+,-][1,2,4,8]. This is not mandatory, so
55aa24fb
SDJ
1236 we check it here. If we don't find it, go to the next
1237 state. */
f33da99a
SDJ
1238 if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@')
1239 || (isdigit (cur[0]) && cur[1] == '@'))
55aa24fb
SDJ
1240 {
1241 if (*cur == '-')
1242 {
1243 /* Discard the `-'. */
1244 ++cur;
0e9ae10f 1245 got_minus = true;
55aa24fb
SDJ
1246 }
1247
30a1e6cc
SDJ
1248 /* Defining the bitness. */
1249 switch (*cur)
55aa24fb 1250 {
30a1e6cc 1251 case '1':
0e9ae10f
SDJ
1252 bitness = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED
1253 : STAP_ARG_BITNESS_8BIT_UNSIGNED);
30a1e6cc
SDJ
1254 break;
1255
1256 case '2':
0e9ae10f
SDJ
1257 bitness = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED
1258 : STAP_ARG_BITNESS_16BIT_UNSIGNED);
30a1e6cc
SDJ
1259 break;
1260
1261 case '4':
0e9ae10f
SDJ
1262 bitness = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
1263 : STAP_ARG_BITNESS_32BIT_UNSIGNED);
30a1e6cc
SDJ
1264 break;
1265
1266 case '8':
0e9ae10f
SDJ
1267 bitness = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
1268 : STAP_ARG_BITNESS_64BIT_UNSIGNED);
30a1e6cc
SDJ
1269 break;
1270
1271 default:
1272 {
1273 /* We have an error, because we don't expect anything
1274 except 1, 2, 4 and 8. */
1275 warning (_("unrecognized bitness %s%c' for probe `%s'"),
0e9ae10f
SDJ
1276 got_minus ? "`-" : "`", *cur,
1277 this->get_name ().c_str ());
30a1e6cc
SDJ
1278 return;
1279 }
55aa24fb 1280 }
55aa24fb
SDJ
1281 /* Discard the number and the `@' sign. */
1282 cur += 2;
1283 }
f33da99a 1284 else
0e9ae10f 1285 bitness = STAP_ARG_BITNESS_UNDEFINED;
f33da99a 1286
0e9ae10f
SDJ
1287 struct type *atype
1288 = stap_get_expected_argument_type (gdbarch, bitness,
1289 this->get_name ().c_str ());
55aa24fb 1290
0e9ae10f 1291 expression_up expr = stap_parse_argument (&cur, atype, gdbarch);
55aa24fb
SDJ
1292
1293 if (stap_expression_debug)
0e9ae10f 1294 dump_raw_expression (expr.get (), gdb_stdlog,
55aa24fb
SDJ
1295 "before conversion to prefix form");
1296
0e9ae10f 1297 prefixify_expression (expr.get ());
55aa24fb
SDJ
1298
1299 if (stap_expression_debug)
0e9ae10f 1300 dump_prefix_expression (expr.get (), gdb_stdlog);
55aa24fb 1301
0e9ae10f 1302 m_parsed_args.emplace_back (bitness, atype, std::move (expr));
55aa24fb
SDJ
1303
1304 /* Start it over again. */
f1735a53 1305 cur = skip_spaces (cur);
55aa24fb
SDJ
1306 }
1307}
1308
685de8c2
SDJ
1309/* Helper function to relocate an address. */
1310
1311static CORE_ADDR
1312relocate_address (CORE_ADDR address, struct objfile *objfile)
1313{
b3b3bada 1314 return address + objfile->data_section_offset ();
685de8c2
SDJ
1315}
1316
0e9ae10f 1317/* Implementation of the get_relocated_address method. */
729662a5 1318
0e9ae10f
SDJ
1319CORE_ADDR
1320stap_probe::get_relocated_address (struct objfile *objfile)
729662a5 1321{
685de8c2 1322 return relocate_address (this->get_address (), objfile);
729662a5
TT
1323}
1324
55aa24fb
SDJ
1325/* Given PROBE, returns the number of arguments present in that probe's
1326 argument string. */
1327
0e9ae10f 1328unsigned
fe01123e 1329stap_probe::get_argument_count (struct gdbarch *gdbarch)
55aa24fb 1330{
0e9ae10f 1331 if (!m_have_parsed_args)
25f9533e 1332 {
0e9ae10f
SDJ
1333 if (this->can_evaluate_arguments ())
1334 this->parse_arguments (gdbarch);
25f9533e
SDJ
1335 else
1336 {
af2d9bee 1337 static bool have_warned_stap_incomplete = false;
25f9533e
SDJ
1338
1339 if (!have_warned_stap_incomplete)
1340 {
1341 warning (_(
1342"The SystemTap SDT probe support is not fully implemented on this target;\n"
1343"you will not be able to inspect the arguments of the probes.\n"
1344"Please report a bug against GDB requesting a port to this target."));
af2d9bee 1345 have_warned_stap_incomplete = true;
25f9533e
SDJ
1346 }
1347
1348 /* Marking the arguments as "already parsed". */
0e9ae10f 1349 m_have_parsed_args = true;
25f9533e
SDJ
1350 }
1351 }
55aa24fb 1352
0e9ae10f
SDJ
1353 gdb_assert (m_have_parsed_args);
1354 return m_parsed_args.size ();
55aa24fb
SDJ
1355}
1356
af2d9bee
SDJ
1357/* Return true if OP is a valid operator inside a probe argument, or
1358 false otherwise. */
55aa24fb 1359
af2d9bee 1360static bool
fcf57f19 1361stap_is_operator (const char *op)
55aa24fb 1362{
af2d9bee 1363 bool ret = true;
fcf57f19
SDJ
1364
1365 switch (*op)
1366 {
1367 case '*':
1368 case '/':
1369 case '%':
1370 case '^':
1371 case '!':
1372 case '+':
1373 case '-':
1374 case '<':
1375 case '>':
1376 case '|':
1377 case '&':
1378 break;
1379
1380 case '=':
1381 if (op[1] != '=')
af2d9bee 1382 ret = false;
fcf57f19
SDJ
1383 break;
1384
1385 default:
1386 /* We didn't find any operator. */
af2d9bee 1387 ret = false;
fcf57f19
SDJ
1388 }
1389
1390 return ret;
55aa24fb
SDJ
1391}
1392
0e9ae10f 1393/* Implement the `can_evaluate_arguments' method. */
f469e8ce 1394
0e9ae10f
SDJ
1395bool
1396stap_probe::can_evaluate_arguments () const
25f9533e 1397{
0e9ae10f 1398 struct gdbarch *gdbarch = this->get_gdbarch ();
25f9533e
SDJ
1399
1400 /* For SystemTap probes, we have to guarantee that the method
1401 stap_is_single_operand is defined on gdbarch. If it is not, then it
1402 means that argument evaluation is not implemented on this target. */
1403 return gdbarch_stap_is_single_operand_p (gdbarch);
1404}
1405
55aa24fb
SDJ
1406/* Evaluate the probe's argument N (indexed from 0), returning a value
1407 corresponding to it. Assertion is thrown if N does not exist. */
1408
0e9ae10f
SDJ
1409struct value *
1410stap_probe::evaluate_argument (unsigned n, struct frame_info *frame)
55aa24fb 1411{
55aa24fb 1412 struct stap_probe_arg *arg;
0e9ae10f 1413 struct gdbarch *gdbarch = get_frame_arch (frame);
55aa24fb 1414
0e9ae10f 1415 arg = this->get_arg_by_number (n, gdbarch);
efd7ff14 1416 return evaluate_expression (arg->aexpr.get (), arg->atype);
55aa24fb
SDJ
1417}
1418
1419/* Compile the probe's argument N (indexed from 0) to agent expression.
1420 Assertion is thrown if N does not exist. */
1421
0e9ae10f
SDJ
1422void
1423stap_probe::compile_to_ax (struct agent_expr *expr, struct axs_value *value,
1424 unsigned n)
55aa24fb 1425{
55aa24fb
SDJ
1426 struct stap_probe_arg *arg;
1427 union exp_element *pc;
1428
0e9ae10f 1429 arg = this->get_arg_by_number (n, expr->gdbarch);
55aa24fb
SDJ
1430
1431 pc = arg->aexpr->elts;
0e9ae10f 1432 gen_expr (arg->aexpr.get (), &pc, expr, value);
55aa24fb
SDJ
1433
1434 require_rvalue (expr, value);
1435 value->type = arg->atype;
1436}
55aa24fb
SDJ
1437\f
1438
55aa24fb 1439/* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
0e9ae10f
SDJ
1440 address. SET is zero if the semaphore should be cleared, or one if
1441 it should be set. This is a helper function for
1442 'stap_probe::set_semaphore' and 'stap_probe::clear_semaphore'. */
55aa24fb
SDJ
1443
1444static void
1445stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1446{
1447 gdb_byte bytes[sizeof (LONGEST)];
1448 /* The ABI specifies "unsigned short". */
1449 struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1450 ULONGEST value;
1451
55aa24fb
SDJ
1452 /* Swallow errors. */
1453 if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1454 {
1455 warning (_("Could not read the value of a SystemTap semaphore."));
1456 return;
1457 }
1458
34877895
PJ
1459 enum bfd_endian byte_order = type_byte_order (type);
1460 value = extract_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order);
55aa24fb
SDJ
1461 /* Note that we explicitly don't worry about overflow or
1462 underflow. */
1463 if (set)
1464 ++value;
1465 else
1466 --value;
1467
34877895 1468 store_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order, value);
55aa24fb
SDJ
1469
1470 if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1471 warning (_("Could not write the value of a SystemTap semaphore."));
1472}
1473
0e9ae10f 1474/* Implementation of the 'set_semaphore' method.
55aa24fb 1475
0e9ae10f
SDJ
1476 SystemTap semaphores act as reference counters, so calls to this
1477 function must be paired with calls to 'clear_semaphore'.
55aa24fb 1478
0e9ae10f
SDJ
1479 This function and 'clear_semaphore' race with another tool
1480 changing the probes, but that is too rare to care. */
1481
1482void
1483stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
55aa24fb 1484{
7f0ae84c
GB
1485 if (m_sem_addr == 0)
1486 return;
685de8c2 1487 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
0e9ae10f 1488}
55aa24fb 1489
0e9ae10f 1490/* Implementation of the 'clear_semaphore' method. */
55aa24fb 1491
0e9ae10f
SDJ
1492void
1493stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
1494{
7f0ae84c
GB
1495 if (m_sem_addr == 0)
1496 return;
685de8c2 1497 stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
55aa24fb
SDJ
1498}
1499
0e9ae10f 1500/* Implementation of the 'get_static_ops' method. */
55aa24fb 1501
0e9ae10f
SDJ
1502const static_probe_ops *
1503stap_probe::get_static_ops () const
1504{
1505 return &stap_static_probe_ops;
1506}
1507
1508/* Implementation of the 'gen_info_probes_table_values' method. */
1509
1510std::vector<const char *>
1511stap_probe::gen_info_probes_table_values () const
55aa24fb 1512{
0e9ae10f 1513 const char *val = NULL;
55aa24fb 1514
0e9ae10f
SDJ
1515 if (m_sem_addr != 0)
1516 val = print_core_address (this->get_gdbarch (), m_sem_addr);
55aa24fb 1517
0e9ae10f 1518 return std::vector<const char *> { val };
55aa24fb
SDJ
1519}
1520
55aa24fb
SDJ
1521/* Helper function that parses the information contained in a
1522 SystemTap's probe. Basically, the information consists in:
1523
1524 - Probe's PC address;
1525 - Link-time section address of `.stapsdt.base' section;
1526 - Link-time address of the semaphore variable, or ZERO if the
1527 probe doesn't have an associated semaphore;
1528 - Probe's provider name;
1529 - Probe's name;
3ca58cde 1530 - Probe's argument format. */
55aa24fb
SDJ
1531
1532static void
1533handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
814cf43a
TT
1534 std::vector<std::unique_ptr<probe>> *probesp,
1535 CORE_ADDR base)
55aa24fb
SDJ
1536{
1537 bfd *abfd = objfile->obfd;
1538 int size = bfd_get_arch_size (abfd) / 8;
08feed99 1539 struct gdbarch *gdbarch = objfile->arch ();
55aa24fb 1540 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
55aa24fb
SDJ
1541
1542 /* Provider and the name of the probe. */
0e9ae10f
SDJ
1543 const char *provider = (const char *) &el->data[3 * size];
1544 const char *name = ((const char *)
1545 memchr (provider, '\0',
1546 (char *) el->data + el->size - provider));
55aa24fb 1547 /* Making sure there is a name. */
0e9ae10f 1548 if (name == NULL)
55aa24fb 1549 {
f3da9116 1550 complaint (_("corrupt probe name when reading `%s'"),
4262abfb 1551 objfile_name (objfile));
55aa24fb
SDJ
1552
1553 /* There is no way to use a probe without a name or a provider, so
f3da9116 1554 returning here makes sense. */
55aa24fb
SDJ
1555 return;
1556 }
1557 else
0e9ae10f 1558 ++name;
55aa24fb
SDJ
1559
1560 /* Retrieving the probe's address. */
0e9ae10f 1561 CORE_ADDR address = extract_typed_address (&el->data[0], ptr_type);
55aa24fb
SDJ
1562
1563 /* Link-time sh_addr of `.stapsdt.base' section. */
0e9ae10f 1564 CORE_ADDR base_ref = extract_typed_address (&el->data[size], ptr_type);
55aa24fb
SDJ
1565
1566 /* Semaphore address. */
0e9ae10f 1567 CORE_ADDR sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
55aa24fb 1568
0e9ae10f
SDJ
1569 address += base - base_ref;
1570 if (sem_addr != 0)
1571 sem_addr += base - base_ref;
55aa24fb
SDJ
1572
1573 /* Arguments. We can only extract the argument format if there is a valid
1574 name for this probe. */
0e9ae10f
SDJ
1575 const char *probe_args = ((const char*)
1576 memchr (name, '\0',
1577 (char *) el->data + el->size - name));
55aa24fb
SDJ
1578
1579 if (probe_args != NULL)
1580 ++probe_args;
1581
97c2dca0 1582 if (probe_args == NULL
0e9ae10f 1583 || (memchr (probe_args, '\0', (char *) el->data + el->size - name)
97c2dca0 1584 != el->data + el->size - 1))
55aa24fb 1585 {
f3da9116 1586 complaint (_("corrupt probe argument when reading `%s'"),
4262abfb 1587 objfile_name (objfile));
55aa24fb 1588 /* If the argument string is NULL, it means some problem happened with
f3da9116 1589 it. So we return. */
55aa24fb
SDJ
1590 return;
1591 }
1592
0e9ae10f
SDJ
1593 stap_probe *ret = new stap_probe (std::string (name), std::string (provider),
1594 address, gdbarch, sem_addr, probe_args);
55aa24fb
SDJ
1595
1596 /* Successfully created probe. */
814cf43a 1597 probesp->emplace_back (ret);
55aa24fb
SDJ
1598}
1599
55aa24fb
SDJ
1600/* Helper function which iterates over every section in the BFD file,
1601 trying to find the base address of the SystemTap base section.
1602 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1603
1604static int
1605get_stap_base_address (bfd *obfd, bfd_vma *base)
1606{
1607 asection *ret = NULL;
1608
3cabfd26
TT
1609 for (asection *sect : gdb_bfd_sections (obfd))
1610 if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1611 && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1612 ret = sect;
55aa24fb 1613
97c2dca0 1614 if (ret == NULL)
55aa24fb 1615 {
b98664d3 1616 complaint (_("could not obtain base address for "
55aa24fb 1617 "SystemTap section on objfile `%s'."),
c7e97679 1618 bfd_get_filename (obfd));
55aa24fb
SDJ
1619 return 0;
1620 }
1621
97c2dca0 1622 if (base != NULL)
55aa24fb
SDJ
1623 *base = ret->vma;
1624
1625 return 1;
1626}
1627
0e9ae10f 1628/* Implementation of the 'is_linespec' method. */
55aa24fb 1629
0e9ae10f
SDJ
1630bool
1631stap_static_probe_ops::is_linespec (const char **linespecp) const
1632{
1633 static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1634
1635 return probe_is_linespec_by_keyword (linespecp, keywords);
1636}
1637
1638/* Implementation of the 'get_probes' method. */
1639
1640void
814cf43a
TT
1641stap_static_probe_ops::get_probes
1642 (std::vector<std::unique_ptr<probe>> *probesp,
1643 struct objfile *objfile) const
55aa24fb
SDJ
1644{
1645 /* If we are here, then this is the first time we are parsing the
1646 SystemTap probe's information. We basically have to count how many
1647 probes the objfile has, and then fill in the necessary information
1648 for each one. */
1649 bfd *obfd = objfile->obfd;
1650 bfd_vma base;
1651 struct sdt_note *iter;
aaa63a31 1652 unsigned save_probesp_len = probesp->size ();
55aa24fb 1653
d7333987
SDJ
1654 if (objfile->separate_debug_objfile_backlink != NULL)
1655 {
1656 /* This is a .debug file, not the objfile itself. */
1657 return;
1658 }
1659
97c2dca0 1660 if (elf_tdata (obfd)->sdt_note_head == NULL)
55aa24fb
SDJ
1661 {
1662 /* There isn't any probe here. */
1663 return;
1664 }
1665
1666 if (!get_stap_base_address (obfd, &base))
1667 {
1668 /* There was an error finding the base address for the section.
1669 Just return NULL. */
1670 return;
1671 }
1672
1673 /* Parsing each probe's information. */
97c2dca0
SDJ
1674 for (iter = elf_tdata (obfd)->sdt_note_head;
1675 iter != NULL;
1676 iter = iter->next)
55aa24fb
SDJ
1677 {
1678 /* We first have to handle all the information about the
1679 probe which is present in the section. */
1680 handle_stap_probe (objfile, iter, probesp, base);
1681 }
1682
aaa63a31 1683 if (save_probesp_len == probesp->size ())
55aa24fb
SDJ
1684 {
1685 /* If we are here, it means we have failed to parse every known
1686 probe. */
f3da9116 1687 complaint (_("could not parse SystemTap probe(s) from inferior"));
55aa24fb
SDJ
1688 return;
1689 }
1690}
1691
6f9b8491
JM
1692/* Implementation of the type_name method. */
1693
0e9ae10f
SDJ
1694const char *
1695stap_static_probe_ops::type_name () const
6f9b8491 1696{
6f9b8491
JM
1697 return "stap";
1698}
1699
0e9ae10f 1700/* Implementation of the 'gen_info_probes_table_header' method. */
55aa24fb 1701
0e9ae10f
SDJ
1702std::vector<struct info_probe_column>
1703stap_static_probe_ops::gen_info_probes_table_header () const
55aa24fb 1704{
0e9ae10f 1705 struct info_probe_column stap_probe_column;
55aa24fb
SDJ
1706
1707 stap_probe_column.field_name = "semaphore";
1708 stap_probe_column.print_name = _("Semaphore");
1709
0e9ae10f 1710 return std::vector<struct info_probe_column> { stap_probe_column };
55aa24fb
SDJ
1711}
1712
55aa24fb
SDJ
1713/* Implementation of the `info probes stap' command. */
1714
1715static void
884beb0c 1716info_probes_stap_command (const char *arg, int from_tty)
55aa24fb 1717{
0e9ae10f 1718 info_probes_for_spops (arg, from_tty, &stap_static_probe_ops);
55aa24fb
SDJ
1719}
1720
6c265988 1721void _initialize_stap_probe ();
55aa24fb 1722void
6c265988 1723_initialize_stap_probe ()
55aa24fb 1724{
0e9ae10f 1725 all_static_probe_ops.push_back (&stap_static_probe_ops);
55aa24fb 1726
ccce17b0
YQ
1727 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
1728 &stap_expression_debug,
1729 _("Set SystemTap expression debugging."),
1730 _("Show SystemTap expression debugging."),
1731 _("When non-zero, the internal representation "
1732 "of SystemTap expressions will be printed."),
1733 NULL,
1734 show_stapexpressiondebug,
1735 &setdebuglist, &showdebuglist);
55aa24fb 1736
55aa24fb
SDJ
1737 add_cmd ("stap", class_info, info_probes_stap_command,
1738 _("\
1739Show information about SystemTap static probes.\n\
1740Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1741Each argument is a regular expression, used to select probes.\n\
1742PROVIDER matches probe provider names.\n\
1743NAME matches the probe names.\n\
1744OBJECT matches the executable or shared library name."),
1745 info_probes_cmdlist_get ());
1746
1747}
This page took 1.1477 seconds and 4 git commands to generate.