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