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