]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/stap-probe.c
comment fixes
[thirdparty/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
102 it. */
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. */
604 write_exp_elt_opcode (OP_LONG);
605 write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
606 write_exp_elt_longcst (displacement);
607 write_exp_elt_opcode (OP_LONG);
608 if (got_minus)
609 write_exp_elt_opcode (UNOP_NEG);
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
663 write_exp_elt_opcode (OP_REGISTER);
664 str.ptr = regname;
665 str.length = len;
666 write_exp_string (str);
667 write_exp_elt_opcode (OP_REGISTER);
668
669 if (indirect_p)
670 {
671 if (disp_p)
672 write_exp_elt_opcode (BINOP_ADD);
673
674 /* Casting to the expected type. */
675 write_exp_elt_opcode (UNOP_CAST);
676 write_exp_elt_type (lookup_pointer_type (p->arg_type));
677 write_exp_elt_opcode (UNOP_CAST);
678
679 write_exp_elt_opcode (UNOP_IND);
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 == '-')
770 write_exp_elt_opcode (UNOP_NEG);
771 else if (c == '~')
772 write_exp_elt_opcode (UNOP_COMPLEMENT);
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
SDJ
809 /* We are dealing with a numeric constant. */
810 write_exp_elt_opcode (OP_LONG);
811 write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
812 write_exp_elt_longcst (number);
813 write_exp_elt_opcode (OP_LONG);
814
815 p->arg = tmp;
816
05c0465e
SDJ
817 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
818 p->arg += strlen (int_suffix);
819 else
820 error (_("Invalid constant suffix on expression `%s'."),
821 p->saved_arg);
55aa24fb 822 }
05c0465e 823 else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
55aa24fb
SDJ
824 stap_parse_register_operand (p);
825 else
826 error (_("Unknown numeric token on expression `%s'."),
827 p->saved_arg);
828 }
05c0465e 829 else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
55aa24fb
SDJ
830 {
831 /* We are dealing with a numeric constant. */
832 long number;
a0bcdaa7 833 char *endp;
05c0465e 834 const char *int_suffix;
55aa24fb 835
05c0465e 836 p->arg += strlen (int_prefix);
a0bcdaa7
PA
837 number = strtol (p->arg, &endp, 10);
838 p->arg = endp;
55aa24fb
SDJ
839
840 write_exp_elt_opcode (OP_LONG);
841 write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
842 write_exp_elt_longcst (number);
843 write_exp_elt_opcode (OP_LONG);
844
05c0465e
SDJ
845 if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
846 p->arg += strlen (int_suffix);
847 else
848 error (_("Invalid constant suffix on expression `%s'."),
849 p->saved_arg);
55aa24fb 850 }
05c0465e
SDJ
851 else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
852 || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
55aa24fb
SDJ
853 stap_parse_register_operand (p);
854 else
855 error (_("Operator `%c' not recognized on expression `%s'."),
856 *p->arg, p->saved_arg);
857}
858
859/* This function parses an argument conditionally, based on single or
860 non-single operands. A non-single operand would be a parenthesized
861 expression (e.g., `(2 + 1)'), and a single operand is anything that
862 starts with `-', `~', `+' (i.e., unary operators), a digit, or
863 something recognized by `gdbarch_stap_is_single_operand'. */
864
865static void
866stap_parse_argument_conditionally (struct stap_parse_info *p)
867{
97c2dca0
SDJ
868 gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));
869
55aa24fb
SDJ
870 if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary. */
871 || isdigit (*p->arg)
872 || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
873 stap_parse_single_operand (p);
874 else if (*p->arg == '(')
875 {
876 /* We are dealing with a parenthesized operand. It means we
877 have to parse it as it was a separate expression, without
878 left-side or precedence. */
879 ++p->arg;
880 p->arg = skip_spaces_const (p->arg);
881 ++p->inside_paren_p;
882
883 stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
884
885 --p->inside_paren_p;
886 if (*p->arg != ')')
887 error (_("Missign close-paren on expression `%s'."),
888 p->saved_arg);
889
890 ++p->arg;
891 if (p->inside_paren_p)
892 p->arg = skip_spaces_const (p->arg);
893 }
894 else
895 error (_("Cannot parse expression `%s'."), p->saved_arg);
896}
897
898/* Helper function for `stap_parse_argument'. Please, see its comments to
899 better understand what this function does. */
900
901static void
902stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
903 enum stap_operand_prec prec)
904{
905 /* This is an operator-precedence parser.
906
907 We work with left- and right-sides of expressions, and
908 parse them depending on the precedence of the operators
909 we find. */
910
97c2dca0
SDJ
911 gdb_assert (p->arg != NULL);
912
55aa24fb
SDJ
913 if (p->inside_paren_p)
914 p->arg = skip_spaces_const (p->arg);
915
916 if (!has_lhs)
917 {
918 /* We were called without a left-side, either because this is the
919 first call, or because we were called to parse a parenthesized
920 expression. It doesn't really matter; we have to parse the
921 left-side in order to continue the process. */
922 stap_parse_argument_conditionally (p);
923 }
924
925 /* Start to parse the right-side, and to "join" left and right sides
926 depending on the operation specified.
927
928 This loop shall continue until we run out of characters in the input,
929 or until we find a close-parenthesis, which means that we've reached
930 the end of a sub-expression. */
97c2dca0 931 while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
55aa24fb
SDJ
932 {
933 const char *tmp_exp_buf;
934 enum exp_opcode opcode;
935 enum stap_operand_prec cur_prec;
936
fcf57f19 937 if (!stap_is_operator (p->arg))
55aa24fb
SDJ
938 error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
939 p->saved_arg);
940
941 /* We have to save the current value of the expression buffer because
942 the `stap_get_opcode' modifies it in order to get the current
943 operator. If this operator's precedence is lower than PREC, we
944 should return and not advance the expression buffer pointer. */
945 tmp_exp_buf = p->arg;
fcf57f19 946 opcode = stap_get_opcode (&tmp_exp_buf);
55aa24fb
SDJ
947
948 cur_prec = stap_get_operator_prec (opcode);
949 if (cur_prec < prec)
950 {
951 /* If the precedence of the operator that we are seeing now is
952 lower than the precedence of the first operator seen before
953 this parsing process began, it means we should stop parsing
954 and return. */
955 break;
956 }
957
958 p->arg = tmp_exp_buf;
959 if (p->inside_paren_p)
960 p->arg = skip_spaces_const (p->arg);
961
962 /* Parse the right-side of the expression. */
963 stap_parse_argument_conditionally (p);
964
965 /* While we still have operators, try to parse another
966 right-side, but using the current right-side as a left-side. */
97c2dca0 967 while (*p->arg != '\0' && stap_is_operator (p->arg))
55aa24fb
SDJ
968 {
969 enum exp_opcode lookahead_opcode;
970 enum stap_operand_prec lookahead_prec;
971
972 /* Saving the current expression buffer position. The explanation
973 is the same as above. */
974 tmp_exp_buf = p->arg;
fcf57f19 975 lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
55aa24fb
SDJ
976 lookahead_prec = stap_get_operator_prec (lookahead_opcode);
977
978 if (lookahead_prec <= prec)
979 {
980 /* If we are dealing with an operator whose precedence is lower
981 than the first one, just abandon the attempt. */
982 break;
983 }
984
985 /* Parse the right-side of the expression, but since we already
986 have a left-side at this point, set `has_lhs' to 1. */
987 stap_parse_argument_1 (p, 1, lookahead_prec);
988 }
989
990 write_exp_elt_opcode (opcode);
991 }
992}
993
994/* Parse a probe's argument.
995
996 Assuming that:
997
998 LP = literal integer prefix
999 LS = literal integer suffix
1000
1001 RP = register prefix
1002 RS = register suffix
1003
1004 RIP = register indirection prefix
1005 RIS = register indirection suffix
1006
1007 This routine assumes that arguments' tokens are of the form:
1008
1009 - [LP] NUMBER [LS]
1010 - [RP] REGISTER [RS]
1011 - [RIP] [RP] REGISTER [RS] [RIS]
1012 - If we find a number without LP, we try to parse it as a literal integer
1013 constant (if LP == NULL), or as a register displacement.
1014 - We count parenthesis, and only skip whitespaces if we are inside them.
1015 - If we find an operator, we skip it.
1016
1017 This function can also call a special function that will try to match
1018 unknown tokens. It will return 1 if the argument has been parsed
1019 successfully, or zero otherwise. */
1020
1021static struct expression *
1022stap_parse_argument (const char **arg, struct type *atype,
1023 struct gdbarch *gdbarch)
1024{
1025 struct stap_parse_info p;
55aa24fb
SDJ
1026 struct cleanup *back_to;
1027
1028 /* We need to initialize the expression buffer, in order to begin
1029 our parsing efforts. The language here does not matter, since we
1030 are using our own parser. */
1031 initialize_expout (10, current_language, gdbarch);
1032 back_to = make_cleanup (free_current_contents, &expout);
1033
1034 p.saved_arg = *arg;
1035 p.arg = *arg;
1036 p.arg_type = atype;
1037 p.gdbarch = gdbarch;
1038 p.inside_paren_p = 0;
1039
1040 stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
1041
1042 discard_cleanups (back_to);
1043
1044 gdb_assert (p.inside_paren_p == 0);
1045
1046 /* Casting the final expression to the appropriate type. */
1047 write_exp_elt_opcode (UNOP_CAST);
1048 write_exp_elt_type (atype);
1049 write_exp_elt_opcode (UNOP_CAST);
1050
1051 reallocate_expout ();
1052
1053 p.arg = skip_spaces_const (p.arg);
1054 *arg = p.arg;
1055
1056 return expout;
1057}
1058
1059/* Function which parses an argument string from PROBE, correctly splitting
1060 the arguments and storing their information in properly ways.
1061
1062 Consider the following argument string (x86 syntax):
1063
1064 `4@%eax 4@$10'
1065
1066 We have two arguments, `%eax' and `$10', both with 32-bit unsigned bitness.
1067 This function basically handles them, properly filling some structures with
1068 this information. */
1069
1070static void
08a6411c 1071stap_parse_probe_arguments (struct stap_probe *probe, struct gdbarch *gdbarch)
55aa24fb
SDJ
1072{
1073 const char *cur;
55aa24fb
SDJ
1074
1075 gdb_assert (!probe->args_parsed);
1076 cur = probe->args_u.text;
1077 probe->args_parsed = 1;
1078 probe->args_u.vec = NULL;
1079
97c2dca0 1080 if (cur == NULL || *cur == '\0' || *cur == ':')
55aa24fb
SDJ
1081 return;
1082
97c2dca0 1083 while (*cur != '\0')
55aa24fb
SDJ
1084 {
1085 struct stap_probe_arg arg;
1086 enum stap_arg_bitness b;
1087 int got_minus = 0;
1088 struct expression *expr;
1089
1090 memset (&arg, 0, sizeof (arg));
1091
1092 /* We expect to find something like:
1093
1094 N@OP
1095
1096 Where `N' can be [+,-][4,8]. This is not mandatory, so
1097 we check it here. If we don't find it, go to the next
1098 state. */
97c2dca0 1099 if ((*cur == '-' && cur[1] != '\0' && cur[2] != '@')
55aa24fb
SDJ
1100 && cur[1] != '@')
1101 arg.bitness = STAP_ARG_BITNESS_UNDEFINED;
1102 else
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;
1128 arg.atype = stap_get_expected_argument_type (gdbarch, b);
1129
1130 /* Discard the number and the `@' sign. */
1131 cur += 2;
1132 }
1133
1134 expr = stap_parse_argument (&cur, arg.atype, gdbarch);
1135
1136 if (stap_expression_debug)
1137 dump_raw_expression (expr, gdb_stdlog,
1138 "before conversion to prefix form");
1139
1140 prefixify_expression (expr);
1141
1142 if (stap_expression_debug)
1143 dump_prefix_expression (expr, gdb_stdlog);
1144
1145 arg.aexpr = expr;
1146
1147 /* Start it over again. */
1148 cur = skip_spaces_const (cur);
1149
1150 VEC_safe_push (stap_probe_arg_s, probe->args_u.vec, &arg);
1151 }
1152}
1153
1154/* Given PROBE, returns the number of arguments present in that probe's
1155 argument string. */
1156
1157static unsigned
08a6411c
SDJ
1158stap_get_probe_argument_count (struct probe *probe_generic,
1159 struct frame_info *frame)
55aa24fb
SDJ
1160{
1161 struct stap_probe *probe = (struct stap_probe *) probe_generic;
08a6411c 1162 struct gdbarch *gdbarch = get_frame_arch (frame);
55aa24fb
SDJ
1163
1164 gdb_assert (probe_generic->pops == &stap_probe_ops);
1165
1166 if (!probe->args_parsed)
25f9533e 1167 {
08a6411c
SDJ
1168 if (can_evaluate_probe_arguments (probe_generic))
1169 stap_parse_probe_arguments (probe, gdbarch);
25f9533e
SDJ
1170 else
1171 {
1172 static int have_warned_stap_incomplete = 0;
1173
1174 if (!have_warned_stap_incomplete)
1175 {
1176 warning (_(
1177"The SystemTap SDT probe support is not fully implemented on this target;\n"
1178"you will not be able to inspect the arguments of the probes.\n"
1179"Please report a bug against GDB requesting a port to this target."));
1180 have_warned_stap_incomplete = 1;
1181 }
1182
1183 /* Marking the arguments as "already parsed". */
1184 probe->args_u.vec = NULL;
1185 probe->args_parsed = 1;
1186 }
1187 }
55aa24fb
SDJ
1188
1189 gdb_assert (probe->args_parsed);
1190 return VEC_length (stap_probe_arg_s, probe->args_u.vec);
1191}
1192
1193/* Return 1 if OP is a valid operator inside a probe argument, or zero
1194 otherwise. */
1195
1196static int
fcf57f19 1197stap_is_operator (const char *op)
55aa24fb 1198{
fcf57f19
SDJ
1199 int ret = 1;
1200
1201 switch (*op)
1202 {
1203 case '*':
1204 case '/':
1205 case '%':
1206 case '^':
1207 case '!':
1208 case '+':
1209 case '-':
1210 case '<':
1211 case '>':
1212 case '|':
1213 case '&':
1214 break;
1215
1216 case '=':
1217 if (op[1] != '=')
1218 ret = 0;
1219 break;
1220
1221 default:
1222 /* We didn't find any operator. */
1223 ret = 0;
1224 }
1225
1226 return ret;
55aa24fb
SDJ
1227}
1228
1229static struct stap_probe_arg *
08a6411c 1230stap_get_arg (struct stap_probe *probe, unsigned n, struct gdbarch *gdbarch)
55aa24fb
SDJ
1231{
1232 if (!probe->args_parsed)
08a6411c 1233 stap_parse_probe_arguments (probe, gdbarch);
55aa24fb
SDJ
1234
1235 return VEC_index (stap_probe_arg_s, probe->args_u.vec, n);
1236}
1237
25f9533e
SDJ
1238/* Implement the `can_evaluate_probe_arguments' method of probe_ops. */
1239
1240static int
1241stap_can_evaluate_probe_arguments (struct probe *probe_generic)
1242{
1243 struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
df6d5441 1244 struct gdbarch *gdbarch = get_objfile_arch (stap_probe->p.objfile);
25f9533e
SDJ
1245
1246 /* For SystemTap probes, we have to guarantee that the method
1247 stap_is_single_operand is defined on gdbarch. If it is not, then it
1248 means that argument evaluation is not implemented on this target. */
1249 return gdbarch_stap_is_single_operand_p (gdbarch);
1250}
1251
55aa24fb
SDJ
1252/* Evaluate the probe's argument N (indexed from 0), returning a value
1253 corresponding to it. Assertion is thrown if N does not exist. */
1254
1255static struct value *
08a6411c
SDJ
1256stap_evaluate_probe_argument (struct probe *probe_generic, unsigned n,
1257 struct frame_info *frame)
55aa24fb
SDJ
1258{
1259 struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
08a6411c 1260 struct gdbarch *gdbarch = get_frame_arch (frame);
55aa24fb
SDJ
1261 struct stap_probe_arg *arg;
1262 int pos = 0;
1263
1264 gdb_assert (probe_generic->pops == &stap_probe_ops);
1265
08a6411c 1266 arg = stap_get_arg (stap_probe, n, gdbarch);
55aa24fb
SDJ
1267 return evaluate_subexp_standard (arg->atype, arg->aexpr, &pos, EVAL_NORMAL);
1268}
1269
1270/* Compile the probe's argument N (indexed from 0) to agent expression.
1271 Assertion is thrown if N does not exist. */
1272
1273static void
6bac7473
SDJ
1274stap_compile_to_ax (struct probe *probe_generic, struct agent_expr *expr,
1275 struct axs_value *value, unsigned n)
55aa24fb
SDJ
1276{
1277 struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
1278 struct stap_probe_arg *arg;
1279 union exp_element *pc;
1280
1281 gdb_assert (probe_generic->pops == &stap_probe_ops);
1282
08a6411c 1283 arg = stap_get_arg (stap_probe, n, expr->gdbarch);
55aa24fb
SDJ
1284
1285 pc = arg->aexpr->elts;
1286 gen_expr (arg->aexpr, &pc, expr, value);
1287
1288 require_rvalue (expr, value);
1289 value->type = arg->atype;
1290}
1291
1292/* Destroy (free) the data related to PROBE. PROBE memory itself is not feed
1293 as it is allocated from OBJFILE_OBSTACK. */
1294
1295static void
1296stap_probe_destroy (struct probe *probe_generic)
1297{
1298 struct stap_probe *probe = (struct stap_probe *) probe_generic;
1299
1300 gdb_assert (probe_generic->pops == &stap_probe_ops);
1301
1302 if (probe->args_parsed)
1303 {
1304 struct stap_probe_arg *arg;
1305 int ix;
1306
1307 for (ix = 0; VEC_iterate (stap_probe_arg_s, probe->args_u.vec, ix, arg);
1308 ++ix)
1309 xfree (arg->aexpr);
1310 VEC_free (stap_probe_arg_s, probe->args_u.vec);
1311 }
1312}
1313
1314\f
1315
1316/* This is called to compute the value of one of the $_probe_arg*
1317 convenience variables. */
1318
1319static struct value *
1320compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
1321 void *data)
1322{
1323 struct frame_info *frame = get_selected_frame (_("No frame selected"));
1324 CORE_ADDR pc = get_frame_pc (frame);
1325 int sel = (int) (uintptr_t) data;
55aa24fb 1326 struct probe *pc_probe;
6bac7473 1327 const struct sym_probe_fns *pc_probe_fns;
55aa24fb
SDJ
1328 unsigned n_args;
1329
1330 /* SEL == -1 means "_probe_argc". */
1331 gdb_assert (sel >= -1);
1332
6bac7473 1333 pc_probe = find_probe_by_pc (pc);
55aa24fb
SDJ
1334 if (pc_probe == NULL)
1335 error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
1336
08a6411c 1337 n_args = get_probe_argument_count (pc_probe, frame);
55aa24fb
SDJ
1338 if (sel == -1)
1339 return value_from_longest (builtin_type (arch)->builtin_int, n_args);
1340
1341 if (sel >= n_args)
1342 error (_("Invalid probe argument %d -- probe has %u arguments available"),
1343 sel, n_args);
1344
08a6411c 1345 return evaluate_probe_argument (pc_probe, sel, frame);
55aa24fb
SDJ
1346}
1347
1348/* This is called to compile one of the $_probe_arg* convenience
1349 variables into an agent expression. */
1350
1351static void
1352compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
1353 struct axs_value *value, void *data)
1354{
1355 CORE_ADDR pc = expr->scope;
1356 int sel = (int) (uintptr_t) data;
55aa24fb 1357 struct probe *pc_probe;
6bac7473 1358 const struct sym_probe_fns *pc_probe_fns;
2b963b68 1359 int n_args;
08a6411c 1360 struct frame_info *frame = get_selected_frame (NULL);
55aa24fb
SDJ
1361
1362 /* SEL == -1 means "_probe_argc". */
1363 gdb_assert (sel >= -1);
1364
6bac7473 1365 pc_probe = find_probe_by_pc (pc);
55aa24fb
SDJ
1366 if (pc_probe == NULL)
1367 error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
1368
08a6411c 1369 n_args = get_probe_argument_count (pc_probe, frame);
6bac7473 1370
55aa24fb
SDJ
1371 if (sel == -1)
1372 {
1373 value->kind = axs_rvalue;
1374 value->type = builtin_type (expr->gdbarch)->builtin_int;
2b963b68 1375 ax_const_l (expr, n_args);
55aa24fb
SDJ
1376 return;
1377 }
1378
1379 gdb_assert (sel >= 0);
2b963b68 1380 if (sel >= n_args)
55aa24fb 1381 error (_("Invalid probe argument %d -- probe has %d arguments available"),
2b963b68 1382 sel, n_args);
55aa24fb 1383
37fbcad0 1384 pc_probe->pops->compile_to_ax (pc_probe, expr, value, sel);
55aa24fb
SDJ
1385}
1386
1387\f
1388
1389/* Set or clear a SystemTap semaphore. ADDRESS is the semaphore's
1390 address. SET is zero if the semaphore should be cleared, or one
1391 if it should be set. This is a helper function for `stap_semaphore_down'
1392 and `stap_semaphore_up'. */
1393
1394static void
1395stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1396{
1397 gdb_byte bytes[sizeof (LONGEST)];
1398 /* The ABI specifies "unsigned short". */
1399 struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1400 ULONGEST value;
1401
1402 if (address == 0)
1403 return;
1404
1405 /* Swallow errors. */
1406 if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1407 {
1408 warning (_("Could not read the value of a SystemTap semaphore."));
1409 return;
1410 }
1411
1412 value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
1413 gdbarch_byte_order (gdbarch));
1414 /* Note that we explicitly don't worry about overflow or
1415 underflow. */
1416 if (set)
1417 ++value;
1418 else
1419 --value;
1420
1421 store_unsigned_integer (bytes, TYPE_LENGTH (type),
1422 gdbarch_byte_order (gdbarch), value);
1423
1424 if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1425 warning (_("Could not write the value of a SystemTap semaphore."));
1426}
1427
1428/* Set a SystemTap semaphore. SEM is the semaphore's address. Semaphores
1429 act as reference counters, so calls to this function must be paired with
1430 calls to `stap_semaphore_down'.
1431
1432 This function and `stap_semaphore_down' race with another tool changing
1433 the probes, but that is too rare to care. */
1434
1435static void
1436stap_set_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
1437{
1438 struct stap_probe *probe = (struct stap_probe *) probe_generic;
1439
1440 gdb_assert (probe_generic->pops == &stap_probe_ops);
1441
1442 stap_modify_semaphore (probe->sem_addr, 1, gdbarch);
1443}
1444
1445/* Clear a SystemTap semaphore. SEM is the semaphore's address. */
1446
1447static void
1448stap_clear_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
1449{
1450 struct stap_probe *probe = (struct stap_probe *) probe_generic;
1451
1452 gdb_assert (probe_generic->pops == &stap_probe_ops);
1453
1454 stap_modify_semaphore (probe->sem_addr, 0, gdbarch);
1455}
1456
1457/* Implementation of `$_probe_arg*' set of variables. */
1458
1459static const struct internalvar_funcs probe_funcs =
1460{
1461 compute_probe_arg,
1462 compile_probe_arg,
1463 NULL
1464};
1465
1466/* Helper function that parses the information contained in a
1467 SystemTap's probe. Basically, the information consists in:
1468
1469 - Probe's PC address;
1470 - Link-time section address of `.stapsdt.base' section;
1471 - Link-time address of the semaphore variable, or ZERO if the
1472 probe doesn't have an associated semaphore;
1473 - Probe's provider name;
1474 - Probe's name;
1475 - Probe's argument format
1476
1477 This function returns 1 if the handling was successful, and zero
1478 otherwise. */
1479
1480static void
1481handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
1482 VEC (probe_p) **probesp, CORE_ADDR base)
1483{
1484 bfd *abfd = objfile->obfd;
1485 int size = bfd_get_arch_size (abfd) / 8;
1486 struct gdbarch *gdbarch = get_objfile_arch (objfile);
55aa24fb
SDJ
1487 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1488 CORE_ADDR base_ref;
1489 const char *probe_args = NULL;
1490 struct stap_probe *ret;
1491
1492 ret = obstack_alloc (&objfile->objfile_obstack, sizeof (*ret));
1493 ret->p.pops = &stap_probe_ops;
6bac7473 1494 ret->p.objfile = objfile;
55aa24fb
SDJ
1495
1496 /* Provider and the name of the probe. */
fe106009 1497 ret->p.provider = (char *) &el->data[3 * size];
55aa24fb
SDJ
1498 ret->p.name = memchr (ret->p.provider, '\0',
1499 (char *) el->data + el->size - ret->p.provider);
1500 /* Making sure there is a name. */
97c2dca0 1501 if (ret->p.name == NULL)
55aa24fb
SDJ
1502 {
1503 complaint (&symfile_complaints, _("corrupt probe name when "
4262abfb
JK
1504 "reading `%s'"),
1505 objfile_name (objfile));
55aa24fb
SDJ
1506
1507 /* There is no way to use a probe without a name or a provider, so
1508 returning zero here makes sense. */
1509 return;
1510 }
1511 else
1512 ++ret->p.name;
1513
1514 /* Retrieving the probe's address. */
1515 ret->p.address = extract_typed_address (&el->data[0], ptr_type);
1516
1517 /* Link-time sh_addr of `.stapsdt.base' section. */
1518 base_ref = extract_typed_address (&el->data[size], ptr_type);
1519
1520 /* Semaphore address. */
1521 ret->sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
1522
1523 ret->p.address += (ANOFFSET (objfile->section_offsets,
1524 SECT_OFF_TEXT (objfile))
1525 + base - base_ref);
97c2dca0 1526 if (ret->sem_addr != 0)
55aa24fb
SDJ
1527 ret->sem_addr += (ANOFFSET (objfile->section_offsets,
1528 SECT_OFF_DATA (objfile))
1529 + base - base_ref);
1530
1531 /* Arguments. We can only extract the argument format if there is a valid
1532 name for this probe. */
1533 probe_args = memchr (ret->p.name, '\0',
1534 (char *) el->data + el->size - ret->p.name);
1535
1536 if (probe_args != NULL)
1537 ++probe_args;
1538
97c2dca0
SDJ
1539 if (probe_args == NULL
1540 || (memchr (probe_args, '\0', (char *) el->data + el->size - ret->p.name)
1541 != el->data + el->size - 1))
55aa24fb
SDJ
1542 {
1543 complaint (&symfile_complaints, _("corrupt probe argument when "
4262abfb
JK
1544 "reading `%s'"),
1545 objfile_name (objfile));
55aa24fb
SDJ
1546 /* If the argument string is NULL, it means some problem happened with
1547 it. So we return 0. */
1548 return;
1549 }
1550
1551 ret->args_parsed = 0;
1552 ret->args_u.text = (void *) probe_args;
1553
1554 /* Successfully created probe. */
1555 VEC_safe_push (probe_p, *probesp, (struct probe *) ret);
1556}
1557
1558/* Helper function which tries to find the base address of the SystemTap
1559 base section named STAP_BASE_SECTION_NAME. */
1560
1561static void
1562get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
1563{
1564 asection **ret = obj;
1565
1566 if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1567 && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1568 *ret = sect;
1569}
1570
1571/* Helper function which iterates over every section in the BFD file,
1572 trying to find the base address of the SystemTap base section.
1573 Returns 1 if found (setting BASE to the proper value), zero otherwise. */
1574
1575static int
1576get_stap_base_address (bfd *obfd, bfd_vma *base)
1577{
1578 asection *ret = NULL;
1579
1580 bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);
1581
97c2dca0 1582 if (ret == NULL)
55aa24fb
SDJ
1583 {
1584 complaint (&symfile_complaints, _("could not obtain base address for "
1585 "SystemTap section on objfile `%s'."),
1586 obfd->filename);
1587 return 0;
1588 }
1589
97c2dca0 1590 if (base != NULL)
55aa24fb
SDJ
1591 *base = ret->vma;
1592
1593 return 1;
1594}
1595
1596/* Helper function for `elf_get_probes', which gathers information about all
1597 SystemTap probes from OBJFILE. */
1598
1599static void
1600stap_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
1601{
1602 /* If we are here, then this is the first time we are parsing the
1603 SystemTap probe's information. We basically have to count how many
1604 probes the objfile has, and then fill in the necessary information
1605 for each one. */
1606 bfd *obfd = objfile->obfd;
1607 bfd_vma base;
1608 struct sdt_note *iter;
1609 unsigned save_probesp_len = VEC_length (probe_p, *probesp);
1610
d7333987
SDJ
1611 if (objfile->separate_debug_objfile_backlink != NULL)
1612 {
1613 /* This is a .debug file, not the objfile itself. */
1614 return;
1615 }
1616
97c2dca0 1617 if (elf_tdata (obfd)->sdt_note_head == NULL)
55aa24fb
SDJ
1618 {
1619 /* There isn't any probe here. */
1620 return;
1621 }
1622
1623 if (!get_stap_base_address (obfd, &base))
1624 {
1625 /* There was an error finding the base address for the section.
1626 Just return NULL. */
1627 return;
1628 }
1629
1630 /* Parsing each probe's information. */
97c2dca0
SDJ
1631 for (iter = elf_tdata (obfd)->sdt_note_head;
1632 iter != NULL;
1633 iter = iter->next)
55aa24fb
SDJ
1634 {
1635 /* We first have to handle all the information about the
1636 probe which is present in the section. */
1637 handle_stap_probe (objfile, iter, probesp, base);
1638 }
1639
1640 if (save_probesp_len == VEC_length (probe_p, *probesp))
1641 {
1642 /* If we are here, it means we have failed to parse every known
1643 probe. */
1644 complaint (&symfile_complaints, _("could not parse SystemTap probe(s) "
1645 "from inferior"));
1646 return;
1647 }
1648}
1649
1650static void
1651stap_relocate (struct probe *probe_generic, CORE_ADDR delta)
1652{
1653 struct stap_probe *probe = (struct stap_probe *) probe_generic;
1654
1655 gdb_assert (probe_generic->pops == &stap_probe_ops);
1656
1657 probe->p.address += delta;
97c2dca0 1658 if (probe->sem_addr != 0)
55aa24fb
SDJ
1659 probe->sem_addr += delta;
1660}
1661
1662static int
1663stap_probe_is_linespec (const char **linespecp)
1664{
1665 static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1666
1667 return probe_is_linespec_by_keyword (linespecp, keywords);
1668}
1669
1670static void
1671stap_gen_info_probes_table_header (VEC (info_probe_column_s) **heads)
1672{
1673 info_probe_column_s stap_probe_column;
1674
1675 stap_probe_column.field_name = "semaphore";
1676 stap_probe_column.print_name = _("Semaphore");
1677
1678 VEC_safe_push (info_probe_column_s, *heads, &stap_probe_column);
1679}
1680
1681static void
1682stap_gen_info_probes_table_values (struct probe *probe_generic,
55aa24fb
SDJ
1683 VEC (const_char_ptr) **ret)
1684{
1685 struct stap_probe *probe = (struct stap_probe *) probe_generic;
6bac7473 1686 struct gdbarch *gdbarch;
55aa24fb
SDJ
1687 const char *val = NULL;
1688
1689 gdb_assert (probe_generic->pops == &stap_probe_ops);
1690
6bac7473
SDJ
1691 gdbarch = get_objfile_arch (probe->p.objfile);
1692
97c2dca0 1693 if (probe->sem_addr != 0)
55aa24fb
SDJ
1694 val = print_core_address (gdbarch, probe->sem_addr);
1695
1696 VEC_safe_push (const_char_ptr, *ret, val);
1697}
1698
1699/* SystemTap probe_ops. */
1700
1701static const struct probe_ops stap_probe_ops =
1702{
1703 stap_probe_is_linespec,
1704 stap_get_probes,
1705 stap_relocate,
1706 stap_get_probe_argument_count,
25f9533e 1707 stap_can_evaluate_probe_arguments,
55aa24fb
SDJ
1708 stap_evaluate_probe_argument,
1709 stap_compile_to_ax,
1710 stap_set_semaphore,
1711 stap_clear_semaphore,
1712 stap_probe_destroy,
1713 stap_gen_info_probes_table_header,
1714 stap_gen_info_probes_table_values,
1715};
1716
1717/* Implementation of the `info probes stap' command. */
1718
1719static void
1720info_probes_stap_command (char *arg, int from_tty)
1721{
1722 info_probes_for_ops (arg, from_tty, &stap_probe_ops);
1723}
1724
1725void _initialize_stap_probe (void);
1726
1727void
1728_initialize_stap_probe (void)
1729{
1730 VEC_safe_push (probe_ops_cp, all_probe_ops, &stap_probe_ops);
1731
ccce17b0
YQ
1732 add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
1733 &stap_expression_debug,
1734 _("Set SystemTap expression debugging."),
1735 _("Show SystemTap expression debugging."),
1736 _("When non-zero, the internal representation "
1737 "of SystemTap expressions will be printed."),
1738 NULL,
1739 show_stapexpressiondebug,
1740 &setdebuglist, &showdebuglist);
55aa24fb
SDJ
1741
1742 create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1743 (void *) (uintptr_t) -1);
1744 create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1745 (void *) (uintptr_t) 0);
1746 create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1747 (void *) (uintptr_t) 1);
1748 create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1749 (void *) (uintptr_t) 2);
1750 create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1751 (void *) (uintptr_t) 3);
1752 create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1753 (void *) (uintptr_t) 4);
1754 create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1755 (void *) (uintptr_t) 5);
1756 create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1757 (void *) (uintptr_t) 6);
1758 create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1759 (void *) (uintptr_t) 7);
1760 create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1761 (void *) (uintptr_t) 8);
1762 create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1763 (void *) (uintptr_t) 9);
1764 create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1765 (void *) (uintptr_t) 10);
1766 create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1767 (void *) (uintptr_t) 11);
1768
1769 add_cmd ("stap", class_info, info_probes_stap_command,
1770 _("\
1771Show information about SystemTap static probes.\n\
1772Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1773Each argument is a regular expression, used to select probes.\n\
1774PROVIDER matches probe provider names.\n\
1775NAME matches the probe names.\n\
1776OBJECT matches the executable or shared library name."),
1777 info_probes_cmdlist_get ());
1778
1779}