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