2012-05-02 Sergio Durigan Junior <sergiodj@gmail.com>
[external/binutils.git] / gdb / stap-probe.c
1 /* SystemTap probe support for GDB.
2
3    Copyright (C) 2012 Free Software Foundation, Inc.
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
51 static const struct probe_ops stap_probe_ops;
52
53 /* Should we display debug information for the probe's argument expression
54    parsing?  */
55
56 static int stap_expression_debug = 0;
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
68 enum 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
79 struct 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
91 typedef struct stap_probe_arg stap_probe_arg_s;
92 DEF_VEC_O (stap_probe_arg_s);
93
94 struct 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
125 enum 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
150 static void stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
151                                    enum stap_operand_prec prec);
152
153 static void stap_parse_argument_conditionally (struct stap_parse_info *p);
154
155 /* Returns 1 if *S is an operator, zero otherwise.  */
156
157 static int stap_is_operator (char op);
158
159 static void
160 show_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
170 static enum stap_operand_prec
171 stap_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
212 static int
213 stap_get_opcode (const char **s, enum exp_opcode *op)
214 {
215   const char c = **s;
216   int ret = 1;
217
218   *s += 1;
219
220   switch (c)
221     {
222     case '*':
223       *op = BINOP_MUL;
224       break;
225
226     case '/':
227       *op = BINOP_DIV;
228       break;
229
230     case '%':
231       *op = BINOP_REM;
232     break;
233
234     case '<':
235       *op = BINOP_LESS;
236       if (**s == '<')
237         {
238           *s += 1;
239           *op = BINOP_LSH;
240         }
241       else if (**s == '=')
242         {
243           *s += 1;
244           *op = BINOP_LEQ;
245         }
246       else if (**s == '>')
247         {
248           *s += 1;
249           *op = BINOP_NOTEQUAL;
250         }
251     break;
252
253     case '>':
254       *op = BINOP_GTR;
255       if (**s == '>')
256         {
257           *s += 1;
258           *op = BINOP_RSH;
259         }
260       else if (**s == '=')
261         {
262           *s += 1;
263           *op = BINOP_GEQ;
264         }
265     break;
266
267     case '|':
268       *op = BINOP_BITWISE_IOR;
269       if (**s == '|')
270         {
271           *s += 1;
272           *op = BINOP_LOGICAL_OR;
273         }
274     break;
275
276     case '&':
277       *op = BINOP_BITWISE_AND;
278       if (**s == '&')
279         {
280           *s += 1;
281           *op = BINOP_LOGICAL_AND;
282         }
283     break;
284
285     case '^':
286       *op = BINOP_BITWISE_XOR;
287       break;
288
289     case '!':
290       *op = UNOP_LOGICAL_NOT;
291       break;
292
293     case '+':
294       *op = BINOP_ADD;
295       break;
296
297     case '-':
298       *op = BINOP_SUB;
299       break;
300
301     case '=':
302       if (**s != '=')
303         {
304           ret = 0;
305           break;
306         }
307       *op = BINOP_EQUAL;
308       break;
309
310     default:
311       /* We didn't find any operator.  */
312       *s -= 1;
313       return 0;
314     }
315
316   return ret;
317 }
318
319 /* Given the bitness of the argument, represented by B, return the
320    corresponding `struct type *'.  */
321
322 static struct type *
323 stap_get_expected_argument_type (struct gdbarch *gdbarch,
324                                  enum stap_arg_bitness b)
325 {
326   switch (b)
327     {
328     case STAP_ARG_BITNESS_UNDEFINED:
329       if (gdbarch_addr_bit (gdbarch) == 32)
330         return builtin_type (gdbarch)->builtin_uint32;
331       else
332         return builtin_type (gdbarch)->builtin_uint64;
333
334     case STAP_ARG_BITNESS_32BIT_SIGNED:
335       return builtin_type (gdbarch)->builtin_int32;
336
337     case STAP_ARG_BITNESS_32BIT_UNSIGNED:
338       return builtin_type (gdbarch)->builtin_uint32;
339
340     case STAP_ARG_BITNESS_64BIT_SIGNED:
341       return builtin_type (gdbarch)->builtin_int64;
342
343     case STAP_ARG_BITNESS_64BIT_UNSIGNED:
344       return builtin_type (gdbarch)->builtin_uint64;
345
346     default:
347       internal_error (__FILE__, __LINE__,
348                       _("Undefined bitness for probe."));
349       break;
350     }
351 }
352
353 /* Function responsible for parsing a register operand according to
354    SystemTap parlance.  Assuming:
355
356    RP  = register prefix
357    RS  = register suffix
358    RIP = register indirection prefix
359    RIS = register indirection suffix
360    
361    Then a register operand can be:
362    
363    [RIP] [RP] REGISTER [RS] [RIS]
364
365    This function takes care of a register's indirection, displacement and
366    direct access.  It also takes into consideration the fact that some
367    registers are named differently inside and outside GDB, e.g., PPC's
368    general-purpose registers are represented by integers in the assembly
369    language (e.g., `15' is the 15th general-purpose register), but inside
370    GDB they have a prefix (the letter `r') appended.  */
371
372 static void
373 stap_parse_register_operand (struct stap_parse_info *p)
374 {
375   /* Simple flag to indicate whether we have seen a minus signal before
376      certain number.  */
377   int got_minus = 0;
378
379   /* Flags to indicate whether this register access is being displaced and/or
380      indirected.  */
381   int disp_p = 0, indirect_p = 0;
382   struct gdbarch *gdbarch = p->gdbarch;
383
384   /* Needed to generate the register name as a part of an expression.  */
385   struct stoken str;
386
387   /* Variables used to extract the register name from the probe's
388      argument.  */
389   const char *start;
390   char *regname;
391   int len;
392
393   /* Prefixes for the parser.  */
394   const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch);
395   const char *reg_ind_prefix
396     = gdbarch_stap_register_indirection_prefix (gdbarch);
397   const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
398   int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0;
399   int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0;
400   int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0;
401
402   /* Suffixes for the parser.  */
403   const char *reg_suffix = gdbarch_stap_register_suffix (gdbarch);
404   const char *reg_ind_suffix
405     = gdbarch_stap_register_indirection_suffix (gdbarch);
406   const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
407   int reg_suffix_len = reg_suffix ? strlen (reg_suffix) : 0;
408   int reg_ind_suffix_len = reg_ind_suffix ? strlen (reg_ind_suffix) : 0;
409   int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0;
410
411   /* Checking for a displacement argument.  */
412   if (*p->arg == '+')
413     {
414       /* If it's a plus sign, we don't need to do anything, just advance the
415          pointer.  */
416       ++p->arg;
417     }
418
419   if (*p->arg == '-')
420     {
421       got_minus = 1;
422       ++p->arg;
423     }
424
425   if (isdigit (*p->arg))
426     {
427       /* The value of the displacement.  */
428       long displacement;
429
430       disp_p = 1;
431       displacement = strtol (p->arg, (char **) &p->arg, 10);
432
433       /* Generating the expression for the displacement.  */
434       write_exp_elt_opcode (OP_LONG);
435       write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
436       write_exp_elt_longcst (displacement);
437       write_exp_elt_opcode (OP_LONG);
438       if (got_minus)
439         write_exp_elt_opcode (UNOP_NEG);
440     }
441
442   /* Getting rid of register indirection prefix.  */
443   if (reg_ind_prefix
444       && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0)
445     {
446       indirect_p = 1;
447       p->arg += reg_ind_prefix_len;
448     }
449
450   if (disp_p && !indirect_p)
451     error (_("Invalid register displacement syntax on expression `%s'."),
452            p->saved_arg);
453
454   /* Getting rid of register prefix.  */
455   if (reg_prefix && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0)
456     p->arg += reg_prefix_len;
457
458   /* Now we should have only the register name.  Let's extract it and get
459      the associated number.  */
460   start = p->arg;
461
462   /* We assume the register name is composed by letters and numbers.  */
463   while (isalnum (*p->arg))
464     ++p->arg;
465
466   len = p->arg - start;
467
468   regname = alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1);
469   regname[0] = '\0';
470
471   /* We only add the GDB's register prefix/suffix if we are dealing with
472      a numeric register.  */
473   if (gdb_reg_prefix && isdigit (*start))
474     {
475       strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len);
476       strncpy (regname + gdb_reg_prefix_len, start, len);
477
478       if (gdb_reg_suffix)
479         strncpy (regname + gdb_reg_prefix_len + len,
480                  gdb_reg_suffix, gdb_reg_suffix_len);
481
482       len += gdb_reg_prefix_len + gdb_reg_suffix_len;
483     }
484   else
485     strncpy (regname, start, len);
486
487   regname[len] = '\0';
488
489   /* Is this a valid register name?  */
490   if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
491     error (_("Invalid register name `%s' on expression `%s'."),
492            regname, p->saved_arg);
493
494   write_exp_elt_opcode (OP_REGISTER);
495   str.ptr = regname;
496   str.length = len;
497   write_exp_string (str);
498   write_exp_elt_opcode (OP_REGISTER);
499
500   if (indirect_p)
501     {
502       if (disp_p)
503         write_exp_elt_opcode (BINOP_ADD);
504
505       /* Casting to the expected type.  */
506       write_exp_elt_opcode (UNOP_CAST);
507       write_exp_elt_type (lookup_pointer_type (p->arg_type));
508       write_exp_elt_opcode (UNOP_CAST);
509
510       write_exp_elt_opcode (UNOP_IND);
511     }
512
513   /* Getting rid of the register name suffix.  */
514   if (reg_suffix)
515     {
516       if (strncmp (p->arg, reg_suffix, reg_suffix_len) != 0)
517         error (_("Missing register name suffix `%s' on expression `%s'."),
518                reg_suffix, p->saved_arg);
519
520       p->arg += reg_suffix_len;
521     }
522
523   /* Getting rid of the register indirection suffix.  */
524   if (indirect_p && reg_ind_suffix)
525     {
526       if (strncmp (p->arg, reg_ind_suffix, reg_ind_suffix_len) != 0)
527         error (_("Missing indirection suffix `%s' on expression `%s'."),
528                reg_ind_suffix, p->saved_arg);
529
530       p->arg += reg_ind_suffix_len;
531     }
532 }
533
534 /* This function is responsible for parsing a single operand.
535
536    A single operand can be:
537
538       - an unary operation (e.g., `-5', `~2', or even with subexpressions
539         like `-(2 + 1)')
540       - a register displacement, which will be treated as a register
541         operand (e.g., `-4(%eax)' on x86)
542       - a numeric constant, or
543       - a register operand (see function `stap_parse_register_operand')
544
545    The function also calls special-handling functions to deal with
546    unrecognized operands, allowing arch-specific parsers to be
547    created.  */
548
549 static void
550 stap_parse_single_operand (struct stap_parse_info *p)
551 {
552   struct gdbarch *gdbarch = p->gdbarch;
553
554   /* Prefixes for the parser.  */
555   const char *const_prefix = gdbarch_stap_integer_prefix (gdbarch);
556   const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch);
557   const char *reg_ind_prefix
558     = gdbarch_stap_register_indirection_prefix (gdbarch);
559   int const_prefix_len = const_prefix ? strlen (const_prefix) : 0;
560   int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0;
561   int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0;
562
563   /* Suffixes for the parser.  */
564   const char *const_suffix = gdbarch_stap_integer_suffix (gdbarch);
565   int const_suffix_len = const_suffix ? strlen (const_suffix) : 0;
566
567   /* We first try to parse this token as a "special token".  */
568   if (gdbarch_stap_parse_special_token_p (gdbarch))
569     {
570       int ret = gdbarch_stap_parse_special_token (gdbarch, p);
571
572       if (ret)
573         {
574           /* If the return value of the above function is not zero,
575              it means it successfully parsed the special token.
576
577              If it is NULL, we try to parse it using our method.  */
578           return;
579         }
580     }
581
582   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
583     {
584       char c = *p->arg;
585       int number;
586
587       /* We use this variable to do a lookahead.  */
588       const char *tmp = p->arg;
589
590       ++tmp;
591
592       /* This is an unary operation.  Here is a list of allowed tokens
593          here:
594
595          - numeric literal;
596          - number (from register displacement)
597          - subexpression (beginning with `(')
598
599          We handle the register displacement here, and the other cases
600          recursively.  */
601       if (p->inside_paren_p)
602         tmp = skip_spaces_const (tmp);
603
604       if (isdigit (*tmp))
605         number = strtol (tmp, (char **) &tmp, 10);
606
607       if (!reg_ind_prefix
608           || strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0)
609         {
610           /* This is not a displacement.  We skip the operator, and deal
611              with it later.  */
612           ++p->arg;
613           stap_parse_argument_conditionally (p);
614           if (c == '-')
615             write_exp_elt_opcode (UNOP_NEG);
616           else if (c == '~')
617             write_exp_elt_opcode (UNOP_COMPLEMENT);
618         }
619       else
620         {
621           /* If we are here, it means it is a displacement.  The only
622              operations allowed here are `-' and `+'.  */
623           if (c == '~')
624             error (_("Invalid operator `%c' for register displacement "
625                      "on expression `%s'."), c, p->saved_arg);
626
627           stap_parse_register_operand (p);
628         }
629     }
630   else if (isdigit (*p->arg))
631     {
632       /* A temporary variable, needed for lookahead.  */
633       const char *tmp = p->arg;
634       long number;
635
636       /* We can be dealing with a numeric constant (if `const_prefix' is
637          NULL), or with a register displacement.  */
638       number = strtol (tmp, (char **) &tmp, 10);
639
640       if (p->inside_paren_p)
641         tmp = skip_spaces_const (tmp);
642       if (!const_prefix && reg_ind_prefix
643           && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0)
644         {
645           /* We are dealing with a numeric constant.  */
646           write_exp_elt_opcode (OP_LONG);
647           write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
648           write_exp_elt_longcst (number);
649           write_exp_elt_opcode (OP_LONG);
650
651           p->arg = tmp;
652
653           if (const_suffix)
654             {
655               if (strncmp (p->arg, const_suffix, const_suffix_len) == 0)
656                 p->arg += const_suffix_len;
657               else
658                 error (_("Invalid constant suffix on expression `%s'."),
659                        p->saved_arg);
660             }
661         }
662       else if (reg_ind_prefix
663                && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) == 0)
664         stap_parse_register_operand (p);
665       else
666         error (_("Unknown numeric token on expression `%s'."),
667                p->saved_arg);
668     }
669   else if (const_prefix
670            && strncmp (p->arg, const_prefix, const_prefix_len) == 0)
671     {
672       /* We are dealing with a numeric constant.  */
673       long number;
674
675       p->arg += const_prefix_len;
676       number = strtol (p->arg, (char **) &p->arg, 10);
677
678       write_exp_elt_opcode (OP_LONG);
679       write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
680       write_exp_elt_longcst (number);
681       write_exp_elt_opcode (OP_LONG);
682
683       if (const_suffix)
684         {
685           if (strncmp (p->arg, const_suffix, const_suffix_len) == 0)
686             p->arg += const_suffix_len;
687           else
688             error (_("Invalid constant suffix on expression `%s'."),
689                    p->saved_arg);
690         }
691     }
692   else if ((reg_prefix
693             && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0)
694            || (reg_ind_prefix
695                && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0))
696     stap_parse_register_operand (p);
697   else
698     error (_("Operator `%c' not recognized on expression `%s'."),
699            *p->arg, p->saved_arg);
700 }
701
702 /* This function parses an argument conditionally, based on single or
703    non-single operands.  A non-single operand would be a parenthesized
704    expression (e.g., `(2 + 1)'), and a single operand is anything that
705    starts with `-', `~', `+' (i.e., unary operators), a digit, or
706    something recognized by `gdbarch_stap_is_single_operand'.  */
707
708 static void
709 stap_parse_argument_conditionally (struct stap_parse_info *p)
710 {
711   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary.  */
712       || isdigit (*p->arg)
713       || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
714     stap_parse_single_operand (p);
715   else if (*p->arg == '(')
716     {
717       /* We are dealing with a parenthesized operand.  It means we
718          have to parse it as it was a separate expression, without
719          left-side or precedence.  */
720       ++p->arg;
721       p->arg = skip_spaces_const (p->arg);
722       ++p->inside_paren_p;
723
724       stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
725
726       --p->inside_paren_p;
727       if (*p->arg != ')')
728         error (_("Missign close-paren on expression `%s'."),
729                p->saved_arg);
730
731       ++p->arg;
732       if (p->inside_paren_p)
733         p->arg = skip_spaces_const (p->arg);
734     }
735   else
736     error (_("Cannot parse expression `%s'."), p->saved_arg);
737 }
738
739 /* Helper function for `stap_parse_argument'.  Please, see its comments to
740    better understand what this function does.  */
741
742 static void
743 stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
744                        enum stap_operand_prec prec)
745 {
746   /* This is an operator-precedence parser.
747
748      We work with left- and right-sides of expressions, and
749      parse them depending on the precedence of the operators
750      we find.  */
751
752   if (p->inside_paren_p)
753     p->arg = skip_spaces_const (p->arg);
754
755   if (!has_lhs)
756     {
757       /* We were called without a left-side, either because this is the
758          first call, or because we were called to parse a parenthesized
759          expression.  It doesn't really matter; we have to parse the
760          left-side in order to continue the process.  */
761       stap_parse_argument_conditionally (p);
762     }
763
764   /* Start to parse the right-side, and to "join" left and right sides
765      depending on the operation specified.
766
767      This loop shall continue until we run out of characters in the input,
768      or until we find a close-parenthesis, which means that we've reached
769      the end of a sub-expression.  */
770   while (p->arg && *p->arg && *p->arg != ')' && !isspace (*p->arg))
771     {
772       const char *tmp_exp_buf;
773       enum exp_opcode opcode;
774       enum stap_operand_prec cur_prec;
775
776       if (!stap_is_operator (*p->arg))
777         error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
778                p->saved_arg);
779
780       /* We have to save the current value of the expression buffer because
781          the `stap_get_opcode' modifies it in order to get the current
782          operator.  If this operator's precedence is lower than PREC, we
783          should return and not advance the expression buffer pointer.  */
784       tmp_exp_buf = p->arg;
785       stap_get_opcode (&tmp_exp_buf, &opcode);
786
787       cur_prec = stap_get_operator_prec (opcode);
788       if (cur_prec < prec)
789         {
790           /* If the precedence of the operator that we are seeing now is
791              lower than the precedence of the first operator seen before
792              this parsing process began, it means we should stop parsing
793              and return.  */
794           break;
795         }
796
797       p->arg = tmp_exp_buf;
798       if (p->inside_paren_p)
799         p->arg = skip_spaces_const (p->arg);
800
801       /* Parse the right-side of the expression.  */
802       stap_parse_argument_conditionally (p);
803
804       /* While we still have operators, try to parse another
805          right-side, but using the current right-side as a left-side.  */
806       while (*p->arg && stap_is_operator (*p->arg))
807         {
808           enum exp_opcode lookahead_opcode;
809           enum stap_operand_prec lookahead_prec;
810
811           /* Saving the current expression buffer position.  The explanation
812              is the same as above.  */
813           tmp_exp_buf = p->arg;
814           stap_get_opcode (&tmp_exp_buf, &lookahead_opcode);
815           lookahead_prec = stap_get_operator_prec (lookahead_opcode);
816
817           if (lookahead_prec <= prec)
818             {
819               /* If we are dealing with an operator whose precedence is lower
820                  than the first one, just abandon the attempt.  */
821               break;
822             }
823
824           /* Parse the right-side of the expression, but since we already
825              have a left-side at this point, set `has_lhs' to 1.  */
826           stap_parse_argument_1 (p, 1, lookahead_prec);
827         }
828
829       write_exp_elt_opcode (opcode);
830     }
831 }
832
833 /* Parse a probe's argument.
834
835    Assuming that:
836
837    LP = literal integer prefix
838    LS = literal integer suffix
839
840    RP = register prefix
841    RS = register suffix
842
843    RIP = register indirection prefix
844    RIS = register indirection suffix
845
846    This routine assumes that arguments' tokens are of the form:
847
848    - [LP] NUMBER [LS]
849    - [RP] REGISTER [RS]
850    - [RIP] [RP] REGISTER [RS] [RIS]
851    - If we find a number without LP, we try to parse it as a literal integer
852    constant (if LP == NULL), or as a register displacement.
853    - We count parenthesis, and only skip whitespaces if we are inside them.
854    - If we find an operator, we skip it.
855
856    This function can also call a special function that will try to match
857    unknown tokens.  It will return 1 if the argument has been parsed
858    successfully, or zero otherwise.  */
859
860 static struct expression *
861 stap_parse_argument (const char **arg, struct type *atype,
862                      struct gdbarch *gdbarch)
863 {
864   struct stap_parse_info p;
865   struct cleanup *back_to;
866
867   /* We need to initialize the expression buffer, in order to begin
868      our parsing efforts.  The language here does not matter, since we
869      are using our own parser.  */
870   initialize_expout (10, current_language, gdbarch);
871   back_to = make_cleanup (free_current_contents, &expout);
872
873   p.saved_arg = *arg;
874   p.arg = *arg;
875   p.arg_type = atype;
876   p.gdbarch = gdbarch;
877   p.inside_paren_p = 0;
878
879   stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
880
881   discard_cleanups (back_to);
882
883   gdb_assert (p.inside_paren_p == 0);
884
885   /* Casting the final expression to the appropriate type.  */
886   write_exp_elt_opcode (UNOP_CAST);
887   write_exp_elt_type (atype);
888   write_exp_elt_opcode (UNOP_CAST);
889
890   reallocate_expout ();
891
892   p.arg = skip_spaces_const (p.arg);
893   *arg = p.arg;
894
895   return expout;
896 }
897
898 /* Function which parses an argument string from PROBE, correctly splitting
899    the arguments and storing their information in properly ways.
900
901    Consider the following argument string (x86 syntax):
902
903    `4@%eax 4@$10'
904
905    We have two arguments, `%eax' and `$10', both with 32-bit unsigned bitness.
906    This function basically handles them, properly filling some structures with
907    this information.  */
908
909 static void
910 stap_parse_probe_arguments (struct stap_probe *probe, struct objfile *objfile)
911 {
912   const char *cur;
913   struct gdbarch *gdbarch = get_objfile_arch (objfile);
914
915   gdb_assert (!probe->args_parsed);
916   cur = probe->args_u.text;
917   probe->args_parsed = 1;
918   probe->args_u.vec = NULL;
919
920   if (!cur || !*cur || *cur == ':')
921     return;
922
923   while (*cur)
924     {
925       struct stap_probe_arg arg;
926       enum stap_arg_bitness b;
927       int got_minus = 0;
928       struct expression *expr;
929
930       memset (&arg, 0, sizeof (arg));
931
932       /* We expect to find something like:
933
934          N@OP
935
936          Where `N' can be [+,-][4,8].  This is not mandatory, so
937          we check it here.  If we don't find it, go to the next
938          state.  */
939       if ((*cur == '-' && cur[1] && cur[2] != '@')
940           && cur[1] != '@')
941         arg.bitness = STAP_ARG_BITNESS_UNDEFINED;
942       else
943         {
944           if (*cur == '-')
945             {
946               /* Discard the `-'.  */
947               ++cur;
948               got_minus = 1;
949             }
950
951           if (*cur == '4')
952             b = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
953                  : STAP_ARG_BITNESS_32BIT_UNSIGNED);
954           else if (*cur == '8')
955             b = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
956                  : STAP_ARG_BITNESS_64BIT_UNSIGNED);
957           else
958             {
959               /* We have an error, because we don't expect anything
960                  except 4 and 8.  */
961               complaint (&symfile_complaints,
962                          _("unrecognized bitness `%c' for probe `%s'"),
963                          *cur, probe->p.name);
964               return;
965             }
966
967           arg.bitness = b;
968           arg.atype = stap_get_expected_argument_type (gdbarch, b);
969
970           /* Discard the number and the `@' sign.  */
971           cur += 2;
972         }
973
974       expr = stap_parse_argument (&cur, arg.atype, gdbarch);
975
976       if (stap_expression_debug)
977         dump_raw_expression (expr, gdb_stdlog,
978                              "before conversion to prefix form");
979
980       prefixify_expression (expr);
981
982       if (stap_expression_debug)
983         dump_prefix_expression (expr, gdb_stdlog);
984
985       arg.aexpr = expr;
986
987       /* Start it over again.  */
988       cur = skip_spaces_const (cur);
989
990       VEC_safe_push (stap_probe_arg_s, probe->args_u.vec, &arg);
991     }
992 }
993
994 /* Given PROBE, returns the number of arguments present in that probe's
995    argument string.  */
996
997 static unsigned
998 stap_get_probe_argument_count (struct probe *probe_generic,
999                                struct objfile *objfile)
1000 {
1001   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1002
1003   gdb_assert (probe_generic->pops == &stap_probe_ops);
1004
1005   if (!probe->args_parsed)
1006     stap_parse_probe_arguments (probe, objfile);
1007
1008   gdb_assert (probe->args_parsed);
1009   return VEC_length (stap_probe_arg_s, probe->args_u.vec);
1010 }
1011
1012 /* Return 1 if OP is a valid operator inside a probe argument, or zero
1013    otherwise.  */
1014
1015 static int
1016 stap_is_operator (char op)
1017 {
1018   return (op == '+' || op == '-' || op == '*' || op == '/'
1019           || op == '>' || op == '<' || op == '!' || op == '^'
1020           || op == '|' || op == '&' || op == '%' || op == '=');
1021 }
1022
1023 static struct stap_probe_arg *
1024 stap_get_arg (struct stap_probe *probe, struct objfile *objfile, unsigned n)
1025 {
1026   if (!probe->args_parsed)
1027     stap_parse_probe_arguments (probe, objfile);
1028
1029   return VEC_index (stap_probe_arg_s, probe->args_u.vec, n);
1030 }
1031
1032 /* Evaluate the probe's argument N (indexed from 0), returning a value
1033    corresponding to it.  Assertion is thrown if N does not exist.  */
1034
1035 static struct value *
1036 stap_evaluate_probe_argument (struct probe *probe_generic,
1037                               struct objfile *objfile, unsigned n)
1038 {
1039   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
1040   struct stap_probe_arg *arg;
1041   int pos = 0;
1042
1043   gdb_assert (probe_generic->pops == &stap_probe_ops);
1044
1045   arg = stap_get_arg (stap_probe, objfile, n);
1046   return evaluate_subexp_standard (arg->atype, arg->aexpr, &pos, EVAL_NORMAL);
1047 }
1048
1049 /* Compile the probe's argument N (indexed from 0) to agent expression.
1050    Assertion is thrown if N does not exist.  */
1051
1052 static void
1053 stap_compile_to_ax (struct probe *probe_generic, struct objfile *objfile,
1054                     struct agent_expr *expr, struct axs_value *value,
1055                     unsigned n)
1056 {
1057   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
1058   struct stap_probe_arg *arg;
1059   union exp_element *pc;
1060
1061   gdb_assert (probe_generic->pops == &stap_probe_ops);
1062
1063   arg = stap_get_arg (stap_probe, objfile, n);
1064
1065   pc = arg->aexpr->elts;
1066   gen_expr (arg->aexpr, &pc, expr, value);
1067
1068   require_rvalue (expr, value);
1069   value->type = arg->atype;
1070 }
1071
1072 /* Destroy (free) the data related to PROBE.  PROBE memory itself is not feed
1073    as it is allocated from OBJFILE_OBSTACK.  */
1074
1075 static void
1076 stap_probe_destroy (struct probe *probe_generic)
1077 {
1078   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1079
1080   gdb_assert (probe_generic->pops == &stap_probe_ops);
1081
1082   if (probe->args_parsed)
1083     {
1084       struct stap_probe_arg *arg;
1085       int ix;
1086
1087       for (ix = 0; VEC_iterate (stap_probe_arg_s, probe->args_u.vec, ix, arg);
1088            ++ix)
1089         xfree (arg->aexpr);
1090       VEC_free (stap_probe_arg_s, probe->args_u.vec);
1091     }
1092 }
1093
1094 \f
1095
1096 /* This is called to compute the value of one of the $_probe_arg*
1097    convenience variables.  */
1098
1099 static struct value *
1100 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
1101                    void *data)
1102 {
1103   struct frame_info *frame = get_selected_frame (_("No frame selected"));
1104   CORE_ADDR pc = get_frame_pc (frame);
1105   int sel = (int) (uintptr_t) data;
1106   struct objfile *objfile;
1107   struct probe *pc_probe;
1108   unsigned n_args;
1109
1110   /* SEL == -1 means "_probe_argc".  */
1111   gdb_assert (sel >= -1);
1112
1113   pc_probe = find_probe_by_pc (pc, &objfile);
1114   if (pc_probe == NULL)
1115     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
1116
1117   n_args
1118     = objfile->sf->sym_probe_fns->sym_get_probe_argument_count (objfile,
1119                                                                 pc_probe);
1120   if (sel == -1)
1121     return value_from_longest (builtin_type (arch)->builtin_int, n_args);
1122
1123   if (sel >= n_args)
1124     error (_("Invalid probe argument %d -- probe has %u arguments available"),
1125            sel, n_args);
1126
1127   return objfile->sf->sym_probe_fns->sym_evaluate_probe_argument (objfile,
1128                                                                   pc_probe,
1129                                                                   sel);
1130 }
1131
1132 /* This is called to compile one of the $_probe_arg* convenience
1133    variables into an agent expression.  */
1134
1135 static void
1136 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
1137                    struct axs_value *value, void *data)
1138 {
1139   CORE_ADDR pc = expr->scope;
1140   int sel = (int) (uintptr_t) data;
1141   struct objfile *objfile;
1142   struct probe *pc_probe;
1143   int n_probes;
1144
1145   /* SEL == -1 means "_probe_argc".  */
1146   gdb_assert (sel >= -1);
1147
1148   pc_probe = find_probe_by_pc (pc, &objfile);
1149   if (pc_probe == NULL)
1150     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
1151
1152   n_probes
1153     = objfile->sf->sym_probe_fns->sym_get_probe_argument_count (objfile,
1154                                                                 pc_probe);
1155   if (sel == -1)
1156     {
1157       value->kind = axs_rvalue;
1158       value->type = builtin_type (expr->gdbarch)->builtin_int;
1159       ax_const_l (expr, n_probes);
1160       return;
1161     }
1162
1163   gdb_assert (sel >= 0);
1164   if (sel >= n_probes)
1165     error (_("Invalid probe argument %d -- probe has %d arguments available"),
1166            sel, n_probes);
1167
1168   objfile->sf->sym_probe_fns->sym_compile_to_ax (objfile, pc_probe,
1169                                                  expr, value, sel);
1170 }
1171
1172 \f
1173
1174 /* Set or clear a SystemTap semaphore.  ADDRESS is the semaphore's
1175    address.  SET is zero if the semaphore should be cleared, or one
1176    if it should be set.  This is a helper function for `stap_semaphore_down'
1177    and `stap_semaphore_up'.  */
1178
1179 static void
1180 stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
1181 {
1182   gdb_byte bytes[sizeof (LONGEST)];
1183   /* The ABI specifies "unsigned short".  */
1184   struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
1185   ULONGEST value;
1186
1187   if (address == 0)
1188     return;
1189
1190   /* Swallow errors.  */
1191   if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1192     {
1193       warning (_("Could not read the value of a SystemTap semaphore."));
1194       return;
1195     }
1196
1197   value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
1198                                     gdbarch_byte_order (gdbarch));
1199   /* Note that we explicitly don't worry about overflow or
1200      underflow.  */
1201   if (set)
1202     ++value;
1203   else
1204     --value;
1205
1206   store_unsigned_integer (bytes, TYPE_LENGTH (type),
1207                           gdbarch_byte_order (gdbarch), value);
1208
1209   if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
1210     warning (_("Could not write the value of a SystemTap semaphore."));
1211 }
1212
1213 /* Set a SystemTap semaphore.  SEM is the semaphore's address.  Semaphores
1214    act as reference counters, so calls to this function must be paired with
1215    calls to `stap_semaphore_down'.
1216
1217    This function and `stap_semaphore_down' race with another tool changing
1218    the probes, but that is too rare to care.  */
1219
1220 static void
1221 stap_set_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
1222 {
1223   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1224
1225   gdb_assert (probe_generic->pops == &stap_probe_ops);
1226
1227   stap_modify_semaphore (probe->sem_addr, 1, gdbarch);
1228 }
1229
1230 /* Clear a SystemTap semaphore.  SEM is the semaphore's address.  */
1231
1232 static void
1233 stap_clear_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
1234 {
1235   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1236
1237   gdb_assert (probe_generic->pops == &stap_probe_ops);
1238
1239   stap_modify_semaphore (probe->sem_addr, 0, gdbarch);
1240 }
1241
1242 /* Implementation of `$_probe_arg*' set of variables.  */
1243
1244 static const struct internalvar_funcs probe_funcs =
1245 {
1246   compute_probe_arg,
1247   compile_probe_arg,
1248   NULL
1249 };
1250
1251 /* Helper function that parses the information contained in a
1252    SystemTap's probe.  Basically, the information consists in:
1253
1254    - Probe's PC address;
1255    - Link-time section address of `.stapsdt.base' section;
1256    - Link-time address of the semaphore variable, or ZERO if the
1257      probe doesn't have an associated semaphore;
1258    - Probe's provider name;
1259    - Probe's name;
1260    - Probe's argument format
1261    
1262    This function returns 1 if the handling was successful, and zero
1263    otherwise.  */
1264
1265 static void
1266 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
1267                    VEC (probe_p) **probesp, CORE_ADDR base)
1268 {
1269   bfd *abfd = objfile->obfd;
1270   int size = bfd_get_arch_size (abfd) / 8;
1271   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1272   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
1273   CORE_ADDR base_ref;
1274   const char *probe_args = NULL;
1275   struct stap_probe *ret;
1276
1277   ret = obstack_alloc (&objfile->objfile_obstack, sizeof (*ret));
1278   ret->p.pops = &stap_probe_ops;
1279
1280   /* Provider and the name of the probe.  */
1281   ret->p.provider = &el->data[3 * size];
1282   ret->p.name = memchr (ret->p.provider, '\0',
1283                         (char *) el->data + el->size - ret->p.provider);
1284   /* Making sure there is a name.  */
1285   if (!ret->p.name)
1286     {
1287       complaint (&symfile_complaints, _("corrupt probe name when "
1288                                         "reading `%s'"), objfile->name);
1289
1290       /* There is no way to use a probe without a name or a provider, so
1291          returning zero here makes sense.  */
1292       return;
1293     }
1294   else
1295     ++ret->p.name;
1296
1297   /* Retrieving the probe's address.  */
1298   ret->p.address = extract_typed_address (&el->data[0], ptr_type);
1299
1300   /* Link-time sh_addr of `.stapsdt.base' section.  */
1301   base_ref = extract_typed_address (&el->data[size], ptr_type);
1302
1303   /* Semaphore address.  */
1304   ret->sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
1305
1306   ret->p.address += (ANOFFSET (objfile->section_offsets,
1307                                SECT_OFF_TEXT (objfile))
1308                      + base - base_ref);
1309   if (ret->sem_addr)
1310     ret->sem_addr += (ANOFFSET (objfile->section_offsets,
1311                                 SECT_OFF_DATA (objfile))
1312                       + base - base_ref);
1313
1314   /* Arguments.  We can only extract the argument format if there is a valid
1315      name for this probe.  */
1316   probe_args = memchr (ret->p.name, '\0',
1317                        (char *) el->data + el->size - ret->p.name);
1318
1319   if (probe_args != NULL)
1320     ++probe_args;
1321
1322   if (probe_args == NULL || (memchr (probe_args, '\0',
1323                                      (char *) el->data + el->size - ret->p.name)
1324                              != el->data + el->size - 1))
1325     {
1326       complaint (&symfile_complaints, _("corrupt probe argument when "
1327                                         "reading `%s'"), objfile->name);
1328       /* If the argument string is NULL, it means some problem happened with
1329          it.  So we return 0.  */
1330       return;
1331     }
1332
1333   ret->args_parsed = 0;
1334   ret->args_u.text = (void *) probe_args;
1335
1336   /* Successfully created probe.  */
1337   VEC_safe_push (probe_p, *probesp, (struct probe *) ret);
1338 }
1339
1340 /* Helper function which tries to find the base address of the SystemTap
1341    base section named STAP_BASE_SECTION_NAME.  */
1342
1343 static void
1344 get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
1345 {
1346   asection **ret = obj;
1347
1348   if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
1349       && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
1350     *ret = sect;
1351 }
1352
1353 /* Helper function which iterates over every section in the BFD file,
1354    trying to find the base address of the SystemTap base section.
1355    Returns 1 if found (setting BASE to the proper value), zero otherwise.  */
1356
1357 static int
1358 get_stap_base_address (bfd *obfd, bfd_vma *base)
1359 {
1360   asection *ret = NULL;
1361
1362   bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);
1363
1364   if (!ret)
1365     {
1366       complaint (&symfile_complaints, _("could not obtain base address for "
1367                                         "SystemTap section on objfile `%s'."),
1368                  obfd->filename);
1369       return 0;
1370     }
1371
1372   if (base)
1373     *base = ret->vma;
1374
1375   return 1;
1376 }
1377
1378 /* Helper function for `elf_get_probes', which gathers information about all
1379    SystemTap probes from OBJFILE.  */
1380
1381 static void
1382 stap_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
1383 {
1384   /* If we are here, then this is the first time we are parsing the
1385      SystemTap probe's information.  We basically have to count how many
1386      probes the objfile has, and then fill in the necessary information
1387      for each one.  */
1388   bfd *obfd = objfile->obfd;
1389   bfd_vma base;
1390   struct sdt_note *iter;
1391   unsigned save_probesp_len = VEC_length (probe_p, *probesp);
1392
1393   if (!elf_tdata (obfd)->sdt_note_head)
1394     {
1395       /* There isn't any probe here.  */
1396       return;
1397     }
1398
1399   if (!get_stap_base_address (obfd, &base))
1400     {
1401       /* There was an error finding the base address for the section.
1402          Just return NULL.  */
1403       return;
1404     }
1405
1406   /* Parsing each probe's information.  */
1407   for (iter = elf_tdata (obfd)->sdt_note_head; iter; iter = iter->next)
1408     {
1409       /* We first have to handle all the information about the
1410          probe which is present in the section.  */
1411       handle_stap_probe (objfile, iter, probesp, base);
1412     }
1413
1414   if (save_probesp_len == VEC_length (probe_p, *probesp))
1415     {
1416       /* If we are here, it means we have failed to parse every known
1417          probe.  */
1418       complaint (&symfile_complaints, _("could not parse SystemTap probe(s) "
1419                                         "from inferior"));
1420       return;
1421     }
1422 }
1423
1424 static void
1425 stap_relocate (struct probe *probe_generic, CORE_ADDR delta)
1426 {
1427   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1428
1429   gdb_assert (probe_generic->pops == &stap_probe_ops);
1430
1431   probe->p.address += delta;
1432   if (probe->sem_addr)
1433     probe->sem_addr += delta;
1434 }
1435
1436 static int
1437 stap_probe_is_linespec (const char **linespecp)
1438 {
1439   static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
1440
1441   return probe_is_linespec_by_keyword (linespecp, keywords);
1442 }
1443
1444 static void
1445 stap_gen_info_probes_table_header (VEC (info_probe_column_s) **heads)
1446 {
1447   info_probe_column_s stap_probe_column;
1448
1449   stap_probe_column.field_name = "semaphore";
1450   stap_probe_column.print_name = _("Semaphore");
1451
1452   VEC_safe_push (info_probe_column_s, *heads, &stap_probe_column);
1453 }
1454
1455 static void
1456 stap_gen_info_probes_table_values (struct probe *probe_generic,
1457                                    struct objfile *objfile,
1458                                    VEC (const_char_ptr) **ret)
1459 {
1460   struct stap_probe *probe = (struct stap_probe *) probe_generic;
1461   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1462   const char *val = NULL;
1463
1464   gdb_assert (probe_generic->pops == &stap_probe_ops);
1465
1466   if (probe->sem_addr)
1467     val = print_core_address (gdbarch, probe->sem_addr);
1468
1469   VEC_safe_push (const_char_ptr, *ret, val);
1470 }
1471
1472 /* SystemTap probe_ops.  */
1473
1474 static const struct probe_ops stap_probe_ops =
1475 {
1476   stap_probe_is_linespec,
1477   stap_get_probes,
1478   stap_relocate,
1479   stap_get_probe_argument_count,
1480   stap_evaluate_probe_argument,
1481   stap_compile_to_ax,
1482   stap_set_semaphore,
1483   stap_clear_semaphore,
1484   stap_probe_destroy,
1485   stap_gen_info_probes_table_header,
1486   stap_gen_info_probes_table_values,
1487 };
1488
1489 /* Implementation of the `info probes stap' command.  */
1490
1491 static void
1492 info_probes_stap_command (char *arg, int from_tty)
1493 {
1494   info_probes_for_ops (arg, from_tty, &stap_probe_ops);
1495 }
1496
1497 void _initialize_stap_probe (void);
1498
1499 void
1500 _initialize_stap_probe (void)
1501 {
1502   VEC_safe_push (probe_ops_cp, all_probe_ops, &stap_probe_ops);
1503
1504   add_setshow_zinteger_cmd ("stap-expression", class_maintenance,
1505                             &stap_expression_debug,
1506                             _("Set SystemTap expression debugging."),
1507                             _("Show SystemTap expression debugging."),
1508                             _("When non-zero, the internal representation "
1509                               "of SystemTap expressions will be printed."),
1510                             NULL,
1511                             show_stapexpressiondebug,
1512                             &setdebuglist, &showdebuglist);
1513
1514   create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
1515                                 (void *) (uintptr_t) -1);
1516   create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
1517                                 (void *) (uintptr_t) 0);
1518   create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
1519                                 (void *) (uintptr_t) 1);
1520   create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
1521                                 (void *) (uintptr_t) 2);
1522   create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
1523                                 (void *) (uintptr_t) 3);
1524   create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
1525                                 (void *) (uintptr_t) 4);
1526   create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
1527                                 (void *) (uintptr_t) 5);
1528   create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
1529                                 (void *) (uintptr_t) 6);
1530   create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
1531                                 (void *) (uintptr_t) 7);
1532   create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
1533                                 (void *) (uintptr_t) 8);
1534   create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
1535                                 (void *) (uintptr_t) 9);
1536   create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
1537                                 (void *) (uintptr_t) 10);
1538   create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
1539                                 (void *) (uintptr_t) 11);
1540
1541   add_cmd ("stap", class_info, info_probes_stap_command,
1542            _("\
1543 Show information about SystemTap static probes.\n\
1544 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
1545 Each argument is a regular expression, used to select probes.\n\
1546 PROVIDER matches probe provider names.\n\
1547 NAME matches the probe names.\n\
1548 OBJECT matches the executable or shared library name."),
1549            info_probes_cmdlist_get ());
1550
1551 }