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