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