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