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