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