gdb/stap-probe.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* SystemTap probe support for GDB.

  2.    Copyright (C) 2012-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "stap-probe.h"
  16. #include "probe.h"
  17. #include "vec.h"
  18. #include "ui-out.h"
  19. #include "objfiles.h"
  20. #include "arch-utils.h"
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "filenames.h"
  24. #include "value.h"
  25. #include "ax.h"
  26. #include "ax-gdb.h"
  27. #include "complaints.h"
  28. #include "cli/cli-utils.h"
  29. #include "linespec.h"
  30. #include "user-regs.h"
  31. #include "parser-defs.h"
  32. #include "language.h"
  33. #include "elf-bfd.h"

  34. #include <ctype.h>

  35. /* The name of the SystemTap section where we will find information about
  36.    the probes.  */

  37. #define STAP_BASE_SECTION_NAME ".stapsdt.base"

  38. /* Forward declaration. */

  39. static const struct probe_ops stap_probe_ops;

  40. /* Should we display debug information for the probe's argument expression
  41.    parsing?  */

  42. static unsigned int stap_expression_debug = 0;

  43. /* The various possibilities of bitness defined for a probe's argument.

  44.    The relationship is:

  45.    - STAP_ARG_BITNESS_UNDEFINED:  The user hasn't specified the bitness.
  46.    - STAP_ARG_BITNESS_8BIT_UNSIGNED:  argument string starts with `1@'.
  47.    - STAP_ARG_BITNESS_8BIT_SIGNED:  argument string starts with `-1@'.
  48.    - STAP_ARG_BITNESS_16BIT_UNSIGNED:  argument string starts with `2@'.
  49.    - STAP_ARG_BITNESS_16BIT_SIGNED:  argument string starts with `-2@'.
  50.    - STAP_ARG_BITNESS_32BIT_UNSIGNED:  argument string starts with `4@'.
  51.    - STAP_ARG_BITNESS_32BIT_SIGNED:  argument string starts with `-4@'.
  52.    - STAP_ARG_BITNESS_64BIT_UNSIGNED:  argument string starts with `8@'.
  53.    - STAP_ARG_BITNESS_64BIT_SIGNED:  argument string starts with `-8@'.  */

  54. enum stap_arg_bitness
  55. {
  56.   STAP_ARG_BITNESS_UNDEFINED,
  57.   STAP_ARG_BITNESS_8BIT_UNSIGNED,
  58.   STAP_ARG_BITNESS_8BIT_SIGNED,
  59.   STAP_ARG_BITNESS_16BIT_UNSIGNED,
  60.   STAP_ARG_BITNESS_16BIT_SIGNED,
  61.   STAP_ARG_BITNESS_32BIT_UNSIGNED,
  62.   STAP_ARG_BITNESS_32BIT_SIGNED,
  63.   STAP_ARG_BITNESS_64BIT_UNSIGNED,
  64.   STAP_ARG_BITNESS_64BIT_SIGNED,
  65. };

  66. /* The following structure represents a single argument for the probe.  */

  67. struct stap_probe_arg
  68. {
  69.   /* The bitness of this argument.  */
  70.   enum stap_arg_bitness bitness;

  71.   /* The corresponding `struct type *' to the bitness.  */
  72.   struct type *atype;

  73.   /* The argument converted to an internal GDB expression.  */
  74.   struct expression *aexpr;
  75. };

  76. typedef struct stap_probe_arg stap_probe_arg_s;
  77. DEF_VEC_O (stap_probe_arg_s);

  78. struct stap_probe
  79. {
  80.   /* Generic information about the probe.  This shall be the first element
  81.      of this struct, in order to maintain binary compatibility with the
  82.      `struct probe' and be able to fully abstract it.  */
  83.   struct probe p;

  84.   /* If the probe has a semaphore associated, then this is the value of
  85.      it, relative to SECT_OFF_DATA.  */
  86.   CORE_ADDR sem_addr;

  87.   /* One if the arguments have been parsed.  */
  88.   unsigned int args_parsed : 1;

  89.   union
  90.     {
  91.       const char *text;

  92.       /* Information about each argument.  This is an array of `stap_probe_arg',
  93.          with each entry representing one argument.  */
  94.       VEC (stap_probe_arg_s) *vec;
  95.     }
  96.   args_u;
  97. };

  98. /* When parsing the arguments, we have to establish different precedences
  99.    for the various kinds of asm operators.  This enumeration represents those
  100.    precedences.

  101.    This logic behind this is available at
  102.    <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using
  103.    the command "info '(as)Infix Ops'".  */

  104. enum stap_operand_prec
  105. {
  106.   /* Lowest precedence, used for non-recognized operands or for the beginning
  107.      of the parsing process.  */
  108.   STAP_OPERAND_PREC_NONE = 0,

  109.   /* Precedence of logical OR.  */
  110.   STAP_OPERAND_PREC_LOGICAL_OR,

  111.   /* Precedence of logical AND.  */
  112.   STAP_OPERAND_PREC_LOGICAL_AND,

  113.   /* Precedence of additive (plus, minus) and comparative (equal, less,
  114.      greater-than, etc) operands.  */
  115.   STAP_OPERAND_PREC_ADD_CMP,

  116.   /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
  117.      logical NOT).  */
  118.   STAP_OPERAND_PREC_BITWISE,

  119.   /* Precedence of multiplicative operands (multiplication, division,
  120.      remainder, left shift and right shift).  */
  121.   STAP_OPERAND_PREC_MUL
  122. };

  123. static void stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
  124.                                    enum stap_operand_prec prec);

  125. static void stap_parse_argument_conditionally (struct stap_parse_info *p);

  126. /* Returns 1 if *S is an operator, zero otherwise.  */

  127. static int stap_is_operator (const char *op);

  128. static void
  129. show_stapexpressiondebug (struct ui_file *file, int from_tty,
  130.                           struct cmd_list_element *c, const char *value)
  131. {
  132.   fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"),
  133.                     value);
  134. }

  135. /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
  136.    if the operator code was not recognized.  */

  137. static enum stap_operand_prec
  138. stap_get_operator_prec (enum exp_opcode op)
  139. {
  140.   switch (op)
  141.     {
  142.     case BINOP_LOGICAL_OR:
  143.       return STAP_OPERAND_PREC_LOGICAL_OR;

  144.     case BINOP_LOGICAL_AND:
  145.       return STAP_OPERAND_PREC_LOGICAL_AND;

  146.     case BINOP_ADD:
  147.     case BINOP_SUB:
  148.     case BINOP_EQUAL:
  149.     case BINOP_NOTEQUAL:
  150.     case BINOP_LESS:
  151.     case BINOP_LEQ:
  152.     case BINOP_GTR:
  153.     case BINOP_GEQ:
  154.       return STAP_OPERAND_PREC_ADD_CMP;

  155.     case BINOP_BITWISE_IOR:
  156.     case BINOP_BITWISE_AND:
  157.     case BINOP_BITWISE_XOR:
  158.     case UNOP_LOGICAL_NOT:
  159.       return STAP_OPERAND_PREC_BITWISE;

  160.     case BINOP_MUL:
  161.     case BINOP_DIV:
  162.     case BINOP_REM:
  163.     case BINOP_LSH:
  164.     case BINOP_RSH:
  165.       return STAP_OPERAND_PREC_MUL;

  166.     default:
  167.       return STAP_OPERAND_PREC_NONE;
  168.     }
  169. }

  170. /* Given S, read the operator in it and fills the OP pointer with its code.
  171.    Return 1 on success, zero if the operator was not recognized.  */

  172. static enum exp_opcode
  173. stap_get_opcode (const char **s)
  174. {
  175.   const char c = **s;
  176.   enum exp_opcode op;

  177.   *s += 1;

  178.   switch (c)
  179.     {
  180.     case '*':
  181.       op = BINOP_MUL;
  182.       break;

  183.     case '/':
  184.       op = BINOP_DIV;
  185.       break;

  186.     case '%':
  187.       op = BINOP_REM;
  188.     break;

  189.     case '<':
  190.       op = BINOP_LESS;
  191.       if (**s == '<')
  192.         {
  193.           *s += 1;
  194.           op = BINOP_LSH;
  195.         }
  196.       else if (**s == '=')
  197.         {
  198.           *s += 1;
  199.           op = BINOP_LEQ;
  200.         }
  201.       else if (**s == '>')
  202.         {
  203.           *s += 1;
  204.           op = BINOP_NOTEQUAL;
  205.         }
  206.     break;

  207.     case '>':
  208.       op = BINOP_GTR;
  209.       if (**s == '>')
  210.         {
  211.           *s += 1;
  212.           op = BINOP_RSH;
  213.         }
  214.       else if (**s == '=')
  215.         {
  216.           *s += 1;
  217.           op = BINOP_GEQ;
  218.         }
  219.     break;

  220.     case '|':
  221.       op = BINOP_BITWISE_IOR;
  222.       if (**s == '|')
  223.         {
  224.           *s += 1;
  225.           op = BINOP_LOGICAL_OR;
  226.         }
  227.     break;

  228.     case '&':
  229.       op = BINOP_BITWISE_AND;
  230.       if (**s == '&')
  231.         {
  232.           *s += 1;
  233.           op = BINOP_LOGICAL_AND;
  234.         }
  235.     break;

  236.     case '^':
  237.       op = BINOP_BITWISE_XOR;
  238.       break;

  239.     case '!':
  240.       op = UNOP_LOGICAL_NOT;
  241.       break;

  242.     case '+':
  243.       op = BINOP_ADD;
  244.       break;

  245.     case '-':
  246.       op = BINOP_SUB;
  247.       break;

  248.     case '=':
  249.       gdb_assert (**s == '=');
  250.       op = BINOP_EQUAL;
  251.       break;

  252.     default:
  253.       internal_error (__FILE__, __LINE__,
  254.                       _("Invalid opcode in expression `%s' for SystemTap"
  255.                         "probe"), *s);
  256.     }

  257.   return op;
  258. }

  259. /* Given the bitness of the argument, represented by B, return the
  260.    corresponding `struct type *'.  */

  261. static struct type *
  262. stap_get_expected_argument_type (struct gdbarch *gdbarch,
  263.                                  enum stap_arg_bitness b)
  264. {
  265.   switch (b)
  266.     {
  267.     case STAP_ARG_BITNESS_UNDEFINED:
  268.       if (gdbarch_addr_bit (gdbarch) == 32)
  269.         return builtin_type (gdbarch)->builtin_uint32;
  270.       else
  271.         return builtin_type (gdbarch)->builtin_uint64;

  272.     case STAP_ARG_BITNESS_8BIT_UNSIGNED:
  273.       return builtin_type (gdbarch)->builtin_uint8;

  274.     case STAP_ARG_BITNESS_8BIT_SIGNED:
  275.       return builtin_type (gdbarch)->builtin_int8;

  276.     case STAP_ARG_BITNESS_16BIT_UNSIGNED:
  277.       return builtin_type (gdbarch)->builtin_uint16;

  278.     case STAP_ARG_BITNESS_16BIT_SIGNED:
  279.       return builtin_type (gdbarch)->builtin_int16;

  280.     case STAP_ARG_BITNESS_32BIT_SIGNED:
  281.       return builtin_type (gdbarch)->builtin_int32;

  282.     case STAP_ARG_BITNESS_32BIT_UNSIGNED:
  283.       return builtin_type (gdbarch)->builtin_uint32;

  284.     case STAP_ARG_BITNESS_64BIT_SIGNED:
  285.       return builtin_type (gdbarch)->builtin_int64;

  286.     case STAP_ARG_BITNESS_64BIT_UNSIGNED:
  287.       return builtin_type (gdbarch)->builtin_uint64;

  288.     default:
  289.       internal_error (__FILE__, __LINE__,
  290.                       _("Undefined bitness for probe."));
  291.       break;
  292.     }
  293. }

  294. /* Helper function to check for a generic list of prefixes.  GDBARCH
  295.    is the current gdbarch being used.  S is the expression being
  296.    analyzed.  If R is not NULL, it will be used to return the found
  297.    prefix.  PREFIXES is the list of expected prefixes.

  298.    This function does a case-insensitive match.

  299.    Return 1 if any prefix has been found, zero otherwise.  */

  300. static int
  301. stap_is_generic_prefix (struct gdbarch *gdbarch, const char *s,
  302.                         const char **r, const char *const *prefixes)
  303. {
  304.   const char *const *p;

  305.   if (prefixes == NULL)
  306.     {
  307.       if (r != NULL)
  308.         *r = "";

  309.       return 1;
  310.     }

  311.   for (p = prefixes; *p != NULL; ++p)
  312.     if (strncasecmp (s, *p, strlen (*p)) == 0)
  313.       {
  314.         if (r != NULL)
  315.           *r = *p;

  316.         return 1;
  317.       }

  318.   return 0;
  319. }

  320. /* Return 1 if S points to a register prefix, zero otherwise.  For a
  321.    description of the arguments, look at stap_is_generic_prefix.  */

  322. static int
  323. stap_is_register_prefix (struct gdbarch *gdbarch, const char *s,
  324.                          const char **r)
  325. {
  326.   const char *const *t = gdbarch_stap_register_prefixes (gdbarch);

  327.   return stap_is_generic_prefix (gdbarch, s, r, t);
  328. }

  329. /* Return 1 if S points to a register indirection prefix, zero
  330.    otherwise.  For a description of the arguments, look at
  331.    stap_is_generic_prefix.  */

  332. static int
  333. stap_is_register_indirection_prefix (struct gdbarch *gdbarch, const char *s,
  334.                                      const char **r)
  335. {
  336.   const char *const *t = gdbarch_stap_register_indirection_prefixes (gdbarch);

  337.   return stap_is_generic_prefix (gdbarch, s, r, t);
  338. }

  339. /* Return 1 if S points to an integer prefix, zero otherwise.  For a
  340.    description of the arguments, look at stap_is_generic_prefix.

  341.    This function takes care of analyzing whether we are dealing with
  342.    an expected integer prefix, or, if there is no integer prefix to be
  343.    expected, whether we are dealing with a digit.  It does a
  344.    case-insensitive match.  */

  345. static int
  346. stap_is_integer_prefix (struct gdbarch *gdbarch, const char *s,
  347.                         const char **r)
  348. {
  349.   const char *const *t = gdbarch_stap_integer_prefixes (gdbarch);
  350.   const char *const *p;

  351.   if (t == NULL)
  352.     {
  353.       /* A NULL value here means that integers do not have a prefix.
  354.          We just check for a digit then.  */
  355.       if (r != NULL)
  356.         *r = "";

  357.       return isdigit (*s);
  358.     }

  359.   for (p = t; *p != NULL; ++p)
  360.     {
  361.       size_t len = strlen (*p);

  362.       if ((len == 0 && isdigit (*s))
  363.           || (len > 0 && strncasecmp (s, *p, len) == 0))
  364.         {
  365.           /* Integers may or may not have a prefix.  The "len == 0"
  366.              check covers the case when integers do not have a prefix
  367.              (therefore, we just check if we have a digit).  The call
  368.              to "strncasecmp" covers the case when they have a
  369.              prefix.  */
  370.           if (r != NULL)
  371.             *r = *p;

  372.           return 1;
  373.         }
  374.     }

  375.   return 0;
  376. }

  377. /* Helper function to check for a generic list of suffixes.  If we are
  378.    not expecting any suffixes, then it just returns 1.  If we are
  379.    expecting at least one suffix, then it returns 1 if a suffix has
  380.    been found, zero otherwise.  GDBARCH is the current gdbarch being
  381.    used.  S is the expression being analyzed.  If R is not NULL, it
  382.    will be used to return the found suffix.  SUFFIXES is the list of
  383.    expected suffixes.  This function does a case-insensitive
  384.    match.  */

  385. static int
  386. stap_generic_check_suffix (struct gdbarch *gdbarch, const char *s,
  387.                            const char **r, const char *const *suffixes)
  388. {
  389.   const char *const *p;
  390.   int found = 0;

  391.   if (suffixes == NULL)
  392.     {
  393.       if (r != NULL)
  394.         *r = "";

  395.       return 1;
  396.     }

  397.   for (p = suffixes; *p != NULL; ++p)
  398.     if (strncasecmp (s, *p, strlen (*p)) == 0)
  399.       {
  400.         if (r != NULL)
  401.           *r = *p;

  402.         found = 1;
  403.         break;
  404.       }

  405.   return found;
  406. }

  407. /* Return 1 if S points to an integer suffix, zero otherwise.  For a
  408.    description of the arguments, look at
  409.    stap_generic_check_suffix.  */

  410. static int
  411. stap_check_integer_suffix (struct gdbarch *gdbarch, const char *s,
  412.                            const char **r)
  413. {
  414.   const char *const *p = gdbarch_stap_integer_suffixes (gdbarch);

  415.   return stap_generic_check_suffix (gdbarch, s, r, p);
  416. }

  417. /* Return 1 if S points to a register suffix, zero otherwise.  For a
  418.    description of the arguments, look at
  419.    stap_generic_check_suffix.  */

  420. static int
  421. stap_check_register_suffix (struct gdbarch *gdbarch, const char *s,
  422.                             const char **r)
  423. {
  424.   const char *const *p = gdbarch_stap_register_suffixes (gdbarch);

  425.   return stap_generic_check_suffix (gdbarch, s, r, p);
  426. }

  427. /* Return 1 if S points to a register indirection suffix, zero
  428.    otherwise.  For a description of the arguments, look at
  429.    stap_generic_check_suffix.  */

  430. static int
  431. stap_check_register_indirection_suffix (struct gdbarch *gdbarch, const char *s,
  432.                                         const char **r)
  433. {
  434.   const char *const *p = gdbarch_stap_register_indirection_suffixes (gdbarch);

  435.   return stap_generic_check_suffix (gdbarch, s, r, p);
  436. }

  437. /* Function responsible for parsing a register operand according to
  438.    SystemTap parlance.  Assuming:

  439.    RP  = register prefix
  440.    RS  = register suffix
  441.    RIP = register indirection prefix
  442.    RIS = register indirection suffix

  443.    Then a register operand can be:

  444.    [RIP] [RP] REGISTER [RS] [RIS]

  445.    This function takes care of a register's indirection, displacement and
  446.    direct access.  It also takes into consideration the fact that some
  447.    registers are named differently inside and outside GDB, e.g., PPC's
  448.    general-purpose registers are represented by integers in the assembly
  449.    language (e.g., `15' is the 15th general-purpose register), but inside
  450.    GDB they have a prefix (the letter `r') appended.  */

  451. static void
  452. stap_parse_register_operand (struct stap_parse_info *p)
  453. {
  454.   /* Simple flag to indicate whether we have seen a minus signal before
  455.      certain number.  */
  456.   int got_minus = 0;
  457.   /* Flags to indicate whether this register access is being displaced and/or
  458.      indirected.  */
  459.   int disp_p = 0, indirect_p = 0;
  460.   struct gdbarch *gdbarch = p->gdbarch;
  461.   /* Needed to generate the register name as a part of an expression.  */
  462.   struct stoken str;
  463.   /* Variables used to extract the register name from the probe's
  464.      argument.  */
  465.   const char *start;
  466.   char *regname;
  467.   int len;
  468.   const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
  469.   int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0;
  470.   const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
  471.   int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0;
  472.   const char *reg_prefix;
  473.   const char *reg_ind_prefix;
  474.   const char *reg_suffix;
  475.   const char *reg_ind_suffix;

  476.   /* Checking for a displacement argument.  */
  477.   if (*p->arg == '+')
  478.     {
  479.       /* If it's a plus sign, we don't need to do anything, just advance the
  480.          pointer.  */
  481.       ++p->arg;
  482.     }

  483.   if (*p->arg == '-')
  484.     {
  485.       got_minus = 1;
  486.       ++p->arg;
  487.     }

  488.   if (isdigit (*p->arg))
  489.     {
  490.       /* The value of the displacement.  */
  491.       long displacement;
  492.       char *endp;

  493.       disp_p = 1;
  494.       displacement = strtol (p->arg, &endp, 10);
  495.       p->arg = endp;

  496.       /* Generating the expression for the displacement.  */
  497.       write_exp_elt_opcode (&p->pstate, OP_LONG);
  498.       write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
  499.       write_exp_elt_longcst (&p->pstate, displacement);
  500.       write_exp_elt_opcode (&p->pstate, OP_LONG);
  501.       if (got_minus)
  502.         write_exp_elt_opcode (&p->pstate, UNOP_NEG);
  503.     }

  504.   /* Getting rid of register indirection prefix.  */
  505.   if (stap_is_register_indirection_prefix (gdbarch, p->arg, &reg_ind_prefix))
  506.     {
  507.       indirect_p = 1;
  508.       p->arg += strlen (reg_ind_prefix);
  509.     }

  510.   if (disp_p && !indirect_p)
  511.     error (_("Invalid register displacement syntax on expression `%s'."),
  512.            p->saved_arg);

  513.   /* Getting rid of register prefix.  */
  514.   if (stap_is_register_prefix (gdbarch, p->arg, &reg_prefix))
  515.     p->arg += strlen (reg_prefix);

  516.   /* Now we should have only the register name.  Let's extract it and get
  517.      the associated number.  */
  518.   start = p->arg;

  519.   /* We assume the register name is composed by letters and numbers.  */
  520.   while (isalnum (*p->arg))
  521.     ++p->arg;

  522.   len = p->arg - start;

  523.   regname = alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1);
  524.   regname[0] = '\0';

  525.   /* We only add the GDB's register prefix/suffix if we are dealing with
  526.      a numeric register.  */
  527.   if (gdb_reg_prefix && isdigit (*start))
  528.     {
  529.       strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len);
  530.       strncpy (regname + gdb_reg_prefix_len, start, len);

  531.       if (gdb_reg_suffix)
  532.         strncpy (regname + gdb_reg_prefix_len + len,
  533.                  gdb_reg_suffix, gdb_reg_suffix_len);

  534.       len += gdb_reg_prefix_len + gdb_reg_suffix_len;
  535.     }
  536.   else
  537.     strncpy (regname, start, len);

  538.   regname[len] = '\0';

  539.   /* Is this a valid register name?  */
  540.   if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
  541.     error (_("Invalid register name `%s' on expression `%s'."),
  542.            regname, p->saved_arg);

  543.   write_exp_elt_opcode (&p->pstate, OP_REGISTER);
  544.   str.ptr = regname;
  545.   str.length = len;
  546.   write_exp_string (&p->pstate, str);
  547.   write_exp_elt_opcode (&p->pstate, OP_REGISTER);

  548.   if (indirect_p)
  549.     {
  550.       if (disp_p)
  551.         write_exp_elt_opcode (&p->pstate, BINOP_ADD);

  552.       /* Casting to the expected type.  */
  553.       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
  554.       write_exp_elt_type (&p->pstate, lookup_pointer_type (p->arg_type));
  555.       write_exp_elt_opcode (&p->pstate, UNOP_CAST);

  556.       write_exp_elt_opcode (&p->pstate, UNOP_IND);
  557.     }

  558.   /* Getting rid of the register name suffix.  */
  559.   if (stap_check_register_suffix (gdbarch, p->arg, &reg_suffix))
  560.     p->arg += strlen (reg_suffix);
  561.   else
  562.     error (_("Missing register name suffix on expression `%s'."),
  563.            p->saved_arg);

  564.   /* Getting rid of the register indirection suffix.  */
  565.   if (indirect_p)
  566.     {
  567.       if (stap_check_register_indirection_suffix (gdbarch, p->arg,
  568.                                                   &reg_ind_suffix))
  569.         p->arg += strlen (reg_ind_suffix);
  570.       else
  571.         error (_("Missing indirection suffix on expression `%s'."),
  572.                p->saved_arg);
  573.     }
  574. }

  575. /* This function is responsible for parsing a single operand.

  576.    A single operand can be:

  577.       - an unary operation (e.g., `-5', `~2', or even with subexpressions
  578.         like `-(2 + 1)')
  579.       - a register displacement, which will be treated as a register
  580.         operand (e.g., `-4(%eax)' on x86)
  581.       - a numeric constant, or
  582.       - a register operand (see function `stap_parse_register_operand')

  583.    The function also calls special-handling functions to deal with
  584.    unrecognized operands, allowing arch-specific parsers to be
  585.    created.  */

  586. static void
  587. stap_parse_single_operand (struct stap_parse_info *p)
  588. {
  589.   struct gdbarch *gdbarch = p->gdbarch;
  590.   const char *int_prefix = NULL;

  591.   /* We first try to parse this token as a "special token".  */
  592.   if (gdbarch_stap_parse_special_token_p (gdbarch))
  593.     if (gdbarch_stap_parse_special_token (gdbarch, p) != 0)
  594.       {
  595.         /* If the return value of the above function is not zero,
  596.            it means it successfully parsed the special token.

  597.            If it is NULL, we try to parse it using our method.  */
  598.         return;
  599.       }

  600.   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
  601.     {
  602.       char c = *p->arg;
  603.       /* We use this variable to do a lookahead.  */
  604.       const char *tmp = p->arg;
  605.       int has_digit = 0;

  606.       /* Skipping signal.  */
  607.       ++tmp;

  608.       /* This is an unary operation.  Here is a list of allowed tokens
  609.          here:

  610.          - numeric literal;
  611.          - number (from register displacement)
  612.          - subexpression (beginning with `(')

  613.          We handle the register displacement here, and the other cases
  614.          recursively.  */
  615.       if (p->inside_paren_p)
  616.         tmp = skip_spaces_const (tmp);

  617.       while (isdigit (*tmp))
  618.         {
  619.           /* We skip the digit here because we are only interested in
  620.              knowing what kind of unary operation this is.  The digit
  621.              will be handled by one of the functions that will be
  622.              called below ('stap_parse_argument_conditionally' or
  623.              'stap_parse_register_operand').  */
  624.           ++tmp;
  625.           has_digit = 1;
  626.         }

  627.       if (has_digit && stap_is_register_indirection_prefix (gdbarch, tmp,
  628.                                                             NULL))
  629.         {
  630.           /* If we are here, it means it is a displacement.  The only
  631.              operations allowed here are `-' and `+'.  */
  632.           if (c == '~')
  633.             error (_("Invalid operator `%c' for register displacement "
  634.                      "on expression `%s'."), c, p->saved_arg);

  635.           stap_parse_register_operand (p);
  636.         }
  637.       else
  638.         {
  639.           /* This is not a displacement.  We skip the operator, and
  640.              deal with it when the recursion returns.  */
  641.           ++p->arg;
  642.           stap_parse_argument_conditionally (p);
  643.           if (c == '-')
  644.             write_exp_elt_opcode (&p->pstate, UNOP_NEG);
  645.           else if (c == '~')
  646.             write_exp_elt_opcode (&p->pstate, UNOP_COMPLEMENT);
  647.         }
  648.     }
  649.   else if (isdigit (*p->arg))
  650.     {
  651.       /* A temporary variable, needed for lookahead.  */
  652.       const char *tmp = p->arg;
  653.       char *endp;
  654.       long number;

  655.       /* We can be dealing with a numeric constant, or with a register
  656.          displacement.  */
  657.       number = strtol (tmp, &endp, 10);
  658.       tmp = endp;

  659.       if (p->inside_paren_p)
  660.         tmp = skip_spaces_const (tmp);

  661.       /* If "stap_is_integer_prefix" returns true, it means we can
  662.          accept integers without a prefix here.  But we also need to
  663.          check whether the next token (i.e., "tmp") is not a register
  664.          indirection prefix.  */
  665.       if (stap_is_integer_prefix (gdbarch, p->arg, NULL)
  666.           && !stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
  667.         {
  668.           const char *int_suffix;

  669.           /* We are dealing with a numeric constant.  */
  670.           write_exp_elt_opcode (&p->pstate, OP_LONG);
  671.           write_exp_elt_type (&p->pstate,
  672.                               builtin_type (gdbarch)->builtin_long);
  673.           write_exp_elt_longcst (&p->pstate, number);
  674.           write_exp_elt_opcode (&p->pstate, OP_LONG);

  675.           p->arg = tmp;

  676.           if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
  677.             p->arg += strlen (int_suffix);
  678.           else
  679.             error (_("Invalid constant suffix on expression `%s'."),
  680.                    p->saved_arg);
  681.         }
  682.       else if (stap_is_register_indirection_prefix (gdbarch, tmp, NULL))
  683.         stap_parse_register_operand (p);
  684.       else
  685.         error (_("Unknown numeric token on expression `%s'."),
  686.                p->saved_arg);
  687.     }
  688.   else if (stap_is_integer_prefix (gdbarch, p->arg, &int_prefix))
  689.     {
  690.       /* We are dealing with a numeric constant.  */
  691.       long number;
  692.       char *endp;
  693.       const char *int_suffix;

  694.       p->arg += strlen (int_prefix);
  695.       number = strtol (p->arg, &endp, 10);
  696.       p->arg = endp;

  697.       write_exp_elt_opcode (&p->pstate, OP_LONG);
  698.       write_exp_elt_type (&p->pstate, builtin_type (gdbarch)->builtin_long);
  699.       write_exp_elt_longcst (&p->pstate, number);
  700.       write_exp_elt_opcode (&p->pstate, OP_LONG);

  701.       if (stap_check_integer_suffix (gdbarch, p->arg, &int_suffix))
  702.         p->arg += strlen (int_suffix);
  703.       else
  704.         error (_("Invalid constant suffix on expression `%s'."),
  705.                p->saved_arg);
  706.     }
  707.   else if (stap_is_register_prefix (gdbarch, p->arg, NULL)
  708.            || stap_is_register_indirection_prefix (gdbarch, p->arg, NULL))
  709.     stap_parse_register_operand (p);
  710.   else
  711.     error (_("Operator `%c' not recognized on expression `%s'."),
  712.            *p->arg, p->saved_arg);
  713. }

  714. /* This function parses an argument conditionally, based on single or
  715.    non-single operands.  A non-single operand would be a parenthesized
  716.    expression (e.g., `(2 + 1)'), and a single operand is anything that
  717.    starts with `-', `~', `+' (i.e., unary operators), a digit, or
  718.    something recognized by `gdbarch_stap_is_single_operand'.  */

  719. static void
  720. stap_parse_argument_conditionally (struct stap_parse_info *p)
  721. {
  722.   gdb_assert (gdbarch_stap_is_single_operand_p (p->gdbarch));

  723.   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary.  */
  724.       || isdigit (*p->arg)
  725.       || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
  726.     stap_parse_single_operand (p);
  727.   else if (*p->arg == '(')
  728.     {
  729.       /* We are dealing with a parenthesized operand.  It means we
  730.          have to parse it as it was a separate expression, without
  731.          left-side or precedence.  */
  732.       ++p->arg;
  733.       p->arg = skip_spaces_const (p->arg);
  734.       ++p->inside_paren_p;

  735.       stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);

  736.       --p->inside_paren_p;
  737.       if (*p->arg != ')')
  738.         error (_("Missign close-paren on expression `%s'."),
  739.                p->saved_arg);

  740.       ++p->arg;
  741.       if (p->inside_paren_p)
  742.         p->arg = skip_spaces_const (p->arg);
  743.     }
  744.   else
  745.     error (_("Cannot parse expression `%s'."), p->saved_arg);
  746. }

  747. /* Helper function for `stap_parse_argument'.  Please, see its comments to
  748.    better understand what this function does.  */

  749. static void
  750. stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
  751.                        enum stap_operand_prec prec)
  752. {
  753.   /* This is an operator-precedence parser.

  754.      We work with left- and right-sides of expressions, and
  755.      parse them depending on the precedence of the operators
  756.      we find.  */

  757.   gdb_assert (p->arg != NULL);

  758.   if (p->inside_paren_p)
  759.     p->arg = skip_spaces_const (p->arg);

  760.   if (!has_lhs)
  761.     {
  762.       /* We were called without a left-side, either because this is the
  763.          first call, or because we were called to parse a parenthesized
  764.          expression.  It doesn't really matter; we have to parse the
  765.          left-side in order to continue the process.  */
  766.       stap_parse_argument_conditionally (p);
  767.     }

  768.   /* Start to parse the right-side, and to "join" left and right sides
  769.      depending on the operation specified.

  770.      This loop shall continue until we run out of characters in the input,
  771.      or until we find a close-parenthesis, which means that we've reached
  772.      the end of a sub-expression.  */
  773.   while (*p->arg != '\0' && *p->arg != ')' && !isspace (*p->arg))
  774.     {
  775.       const char *tmp_exp_buf;
  776.       enum exp_opcode opcode;
  777.       enum stap_operand_prec cur_prec;

  778.       if (!stap_is_operator (p->arg))
  779.         error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
  780.                p->saved_arg);

  781.       /* We have to save the current value of the expression buffer because
  782.          the `stap_get_opcode' modifies it in order to get the current
  783.          operator.  If this operator's precedence is lower than PREC, we
  784.          should return and not advance the expression buffer pointer.  */
  785.       tmp_exp_buf = p->arg;
  786.       opcode = stap_get_opcode (&tmp_exp_buf);

  787.       cur_prec = stap_get_operator_prec (opcode);
  788.       if (cur_prec < prec)
  789.         {
  790.           /* If the precedence of the operator that we are seeing now is
  791.              lower than the precedence of the first operator seen before
  792.              this parsing process began, it means we should stop parsing
  793.              and return.  */
  794.           break;
  795.         }

  796.       p->arg = tmp_exp_buf;
  797.       if (p->inside_paren_p)
  798.         p->arg = skip_spaces_const (p->arg);

  799.       /* Parse the right-side of the expression.  */
  800.       stap_parse_argument_conditionally (p);

  801.       /* While we still have operators, try to parse another
  802.          right-side, but using the current right-side as a left-side.  */
  803.       while (*p->arg != '\0' && stap_is_operator (p->arg))
  804.         {
  805.           enum exp_opcode lookahead_opcode;
  806.           enum stap_operand_prec lookahead_prec;

  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.           if (lookahead_prec <= prec)
  813.             {
  814.               /* If we are dealing with an operator whose precedence is lower
  815.                  than the first one, just abandon the attempt.  */
  816.               break;
  817.             }

  818.           /* Parse the right-side of the expression, but since we already
  819.              have a left-side at this point, set `has_lhs' to 1.  */
  820.           stap_parse_argument_1 (p, 1, lookahead_prec);
  821.         }

  822.       write_exp_elt_opcode (&p->pstate, opcode);
  823.     }
  824. }

  825. /* Parse a probe's argument.

  826.    Assuming that:

  827.    LP = literal integer prefix
  828.    LS = literal integer suffix

  829.    RP = register prefix
  830.    RS = register suffix

  831.    RIP = register indirection prefix
  832.    RIS = register indirection suffix

  833.    This routine assumes that arguments' tokens are of the form:

  834.    - [LP] NUMBER [LS]
  835.    - [RP] REGISTER [RS]
  836.    - [RIP] [RP] REGISTER [RS] [RIS]
  837.    - If we find a number without LP, we try to parse it as a literal integer
  838.    constant (if LP == NULL), or as a register displacement.
  839.    - We count parenthesis, and only skip whitespaces if we are inside them.
  840.    - If we find an operator, we skip it.

  841.    This function can also call a special function that will try to match
  842.    unknown tokens.  It will return 1 if the argument has been parsed
  843.    successfully, or zero otherwise.  */

  844. static struct expression *
  845. stap_parse_argument (const char **arg, struct type *atype,
  846.                      struct gdbarch *gdbarch)
  847. {
  848.   struct stap_parse_info p;
  849.   struct cleanup *back_to;

  850.   /* We need to initialize the expression buffer, in order to begin
  851.      our parsing efforts.  We use language_c here because we may need
  852.      to do pointer arithmetics.  */
  853.   initialize_expout (&p.pstate, 10, language_def (language_c), gdbarch);
  854.   back_to = make_cleanup (free_current_contents, &p.pstate.expout);

  855.   p.saved_arg = *arg;
  856.   p.arg = *arg;
  857.   p.arg_type = atype;
  858.   p.gdbarch = gdbarch;
  859.   p.inside_paren_p = 0;

  860.   stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);

  861.   discard_cleanups (back_to);

  862.   gdb_assert (p.inside_paren_p == 0);

  863.   /* Casting the final expression to the appropriate type.  */
  864.   write_exp_elt_opcode (&p.pstate, UNOP_CAST);
  865.   write_exp_elt_type (&p.pstate, atype);
  866.   write_exp_elt_opcode (&p.pstate, UNOP_CAST);

  867.   reallocate_expout (&p.pstate);

  868.   p.arg = skip_spaces_const (p.arg);
  869.   *arg = p.arg;

  870.   /* We can safely return EXPOUT here.  */
  871.   return p.pstate.expout;
  872. }

  873. /* Function which parses an argument string from PROBE, correctly splitting
  874.    the arguments and storing their information in properly ways.

  875.    Consider the following argument string (x86 syntax):

  876.    `4@%eax 4@$10'

  877.    We have two arguments, `%eax' and `$10', both with 32-bit unsigned bitness.
  878.    This function basically handles them, properly filling some structures with
  879.    this information.  */

  880. static void
  881. stap_parse_probe_arguments (struct stap_probe *probe, struct gdbarch *gdbarch)
  882. {
  883.   const char *cur;

  884.   gdb_assert (!probe->args_parsed);
  885.   cur = probe->args_u.text;
  886.   probe->args_parsed = 1;
  887.   probe->args_u.vec = NULL;

  888.   if (cur == NULL || *cur == '\0' || *cur == ':')
  889.     return;

  890.   while (*cur != '\0')
  891.     {
  892.       struct stap_probe_arg arg;
  893.       enum stap_arg_bitness b;
  894.       int got_minus = 0;
  895.       struct expression *expr;

  896.       memset (&arg, 0, sizeof (arg));

  897.       /* We expect to find something like:

  898.          N@OP

  899.          Where `N' can be [+,-][1,2,4,8].  This is not mandatory, so
  900.          we check it here.  If we don't find it, go to the next
  901.          state.  */
  902.       if ((cur[0] == '-' && isdigit (cur[1]) && cur[2] == '@')
  903.           || (isdigit (cur[0]) && cur[1] == '@'))
  904.         {
  905.           if (*cur == '-')
  906.             {
  907.               /* Discard the `-'.  */
  908.               ++cur;
  909.               got_minus = 1;
  910.             }

  911.           /* Defining the bitness.  */
  912.           switch (*cur)
  913.             {
  914.             case '1':
  915.               b = (got_minus ? STAP_ARG_BITNESS_8BIT_SIGNED
  916.                    : STAP_ARG_BITNESS_8BIT_UNSIGNED);
  917.               break;

  918.             case '2':
  919.               b = (got_minus ? STAP_ARG_BITNESS_16BIT_SIGNED
  920.                    : STAP_ARG_BITNESS_16BIT_UNSIGNED);
  921.               break;

  922.             case '4':
  923.               b = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
  924.                    : STAP_ARG_BITNESS_32BIT_UNSIGNED);
  925.               break;

  926.             case '8':
  927.               b = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
  928.                    : STAP_ARG_BITNESS_64BIT_UNSIGNED);
  929.               break;

  930.             default:
  931.               {
  932.                 /* We have an error, because we don't expect anything
  933.                    except 1, 2, 4 and 8.  */
  934.                 warning (_("unrecognized bitness %s%c' for probe `%s'"),
  935.                          got_minus ? "`-" : "`", *cur, probe->p.name);
  936.                 return;
  937.               }
  938.             }

  939.           arg.bitness = b;

  940.           /* Discard the number and the `@' sign.  */
  941.           cur += 2;
  942.         }
  943.       else
  944.         arg.bitness = STAP_ARG_BITNESS_UNDEFINED;

  945.       arg.atype = stap_get_expected_argument_type (gdbarch, arg.bitness);

  946.       expr = stap_parse_argument (&cur, arg.atype, gdbarch);

  947.       if (stap_expression_debug)
  948.         dump_raw_expression (expr, gdb_stdlog,
  949.                              "before conversion to prefix form");

  950.       prefixify_expression (expr);

  951.       if (stap_expression_debug)
  952.         dump_prefix_expression (expr, gdb_stdlog);

  953.       arg.aexpr = expr;

  954.       /* Start it over again.  */
  955.       cur = skip_spaces_const (cur);

  956.       VEC_safe_push (stap_probe_arg_s, probe->args_u.vec, &arg);
  957.     }
  958. }

  959. /* Implementation of the get_probe_address method.  */

  960. static CORE_ADDR
  961. stap_get_probe_address (struct probe *probe, struct objfile *objfile)
  962. {
  963.   return probe->address + ANOFFSET (objfile->section_offsets,
  964.                                     SECT_OFF_DATA (objfile));
  965. }

  966. /* Given PROBE, returns the number of arguments present in that probe's
  967.    argument string.  */

  968. static unsigned
  969. stap_get_probe_argument_count (struct probe *probe_generic,
  970.                                struct frame_info *frame)
  971. {
  972.   struct stap_probe *probe = (struct stap_probe *) probe_generic;
  973.   struct gdbarch *gdbarch = get_frame_arch (frame);

  974.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  975.   if (!probe->args_parsed)
  976.     {
  977.       if (can_evaluate_probe_arguments (probe_generic))
  978.         stap_parse_probe_arguments (probe, gdbarch);
  979.       else
  980.         {
  981.           static int have_warned_stap_incomplete = 0;

  982.           if (!have_warned_stap_incomplete)
  983.             {
  984.               warning (_(
  985. "The SystemTap SDT probe support is not fully implemented on this target;\n"
  986. "you will not be able to inspect the arguments of the probes.\n"
  987. "Please report a bug against GDB requesting a port to this target."));
  988.               have_warned_stap_incomplete = 1;
  989.             }

  990.           /* Marking the arguments as "already parsed".  */
  991.           probe->args_u.vec = NULL;
  992.           probe->args_parsed = 1;
  993.         }
  994.     }

  995.   gdb_assert (probe->args_parsed);
  996.   return VEC_length (stap_probe_arg_s, probe->args_u.vec);
  997. }

  998. /* Return 1 if OP is a valid operator inside a probe argument, or zero
  999.    otherwise.  */

  1000. static int
  1001. stap_is_operator (const char *op)
  1002. {
  1003.   int ret = 1;

  1004.   switch (*op)
  1005.     {
  1006.     case '*':
  1007.     case '/':
  1008.     case '%':
  1009.     case '^':
  1010.     case '!':
  1011.     case '+':
  1012.     case '-':
  1013.     case '<':
  1014.     case '>':
  1015.     case '|':
  1016.     case '&':
  1017.       break;

  1018.     case '=':
  1019.       if (op[1] != '=')
  1020.         ret = 0;
  1021.       break;

  1022.     default:
  1023.       /* We didn't find any operator.  */
  1024.       ret = 0;
  1025.     }

  1026.   return ret;
  1027. }

  1028. static struct stap_probe_arg *
  1029. stap_get_arg (struct stap_probe *probe, unsigned n, struct gdbarch *gdbarch)
  1030. {
  1031.   if (!probe->args_parsed)
  1032.     stap_parse_probe_arguments (probe, gdbarch);

  1033.   return VEC_index (stap_probe_arg_s, probe->args_u.vec, n);
  1034. }

  1035. /* Implement the `can_evaluate_probe_arguments' method of probe_ops.  */

  1036. static int
  1037. stap_can_evaluate_probe_arguments (struct probe *probe_generic)
  1038. {
  1039.   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
  1040.   struct gdbarch *gdbarch = stap_probe->p.arch;

  1041.   /* For SystemTap probes, we have to guarantee that the method
  1042.      stap_is_single_operand is defined on gdbarch.  If it is not, then it
  1043.      means that argument evaluation is not implemented on this target.  */
  1044.   return gdbarch_stap_is_single_operand_p (gdbarch);
  1045. }

  1046. /* Evaluate the probe's argument N (indexed from 0), returning a value
  1047.    corresponding to it.  Assertion is thrown if N does not exist.  */

  1048. static struct value *
  1049. stap_evaluate_probe_argument (struct probe *probe_generic, unsigned n,
  1050.                               struct frame_info *frame)
  1051. {
  1052.   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
  1053.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1054.   struct stap_probe_arg *arg;
  1055.   int pos = 0;

  1056.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1057.   arg = stap_get_arg (stap_probe, n, gdbarch);
  1058.   return evaluate_subexp_standard (arg->atype, arg->aexpr, &pos, EVAL_NORMAL);
  1059. }

  1060. /* Compile the probe's argument N (indexed from 0) to agent expression.
  1061.    Assertion is thrown if N does not exist.  */

  1062. static void
  1063. stap_compile_to_ax (struct probe *probe_generic, struct agent_expr *expr,
  1064.                     struct axs_value *value, unsigned n)
  1065. {
  1066.   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
  1067.   struct stap_probe_arg *arg;
  1068.   union exp_element *pc;

  1069.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1070.   arg = stap_get_arg (stap_probe, n, expr->gdbarch);

  1071.   pc = arg->aexpr->elts;
  1072.   gen_expr (arg->aexpr, &pc, expr, value);

  1073.   require_rvalue (expr, value);
  1074.   value->type = arg->atype;
  1075. }

  1076. /* Destroy (free) the data related to PROBE.  PROBE memory itself is not feed
  1077.    as it is allocated on an obstack.  */

  1078. static void
  1079. stap_probe_destroy (struct probe *probe_generic)
  1080. {
  1081.   struct stap_probe *probe = (struct stap_probe *) probe_generic;

  1082.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1083.   if (probe->args_parsed)
  1084.     {
  1085.       struct stap_probe_arg *arg;
  1086.       int ix;

  1087.       for (ix = 0; VEC_iterate (stap_probe_arg_s, probe->args_u.vec, ix, arg);
  1088.            ++ix)
  1089.         xfree (arg->aexpr);
  1090.       VEC_free (stap_probe_arg_s, probe->args_u.vec);
  1091.     }
  1092. }



  1093. /* This is called to compute the value of one of the $_probe_arg*
  1094.    convenience variables.  */

  1095. static struct value *
  1096. compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
  1097.                    void *data)
  1098. {
  1099.   struct frame_info *frame = get_selected_frame (_("No frame selected"));
  1100.   CORE_ADDR pc = get_frame_pc (frame);
  1101.   int sel = (int) (uintptr_t) data;
  1102.   struct bound_probe pc_probe;
  1103.   const struct sym_probe_fns *pc_probe_fns;
  1104.   unsigned n_args;

  1105.   /* SEL == -1 means "_probe_argc".  */
  1106.   gdb_assert (sel >= -1);

  1107.   pc_probe = find_probe_by_pc (pc);
  1108.   if (pc_probe.probe == NULL)
  1109.     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));

  1110.   n_args = get_probe_argument_count (pc_probe.probe, frame);
  1111.   if (sel == -1)
  1112.     return value_from_longest (builtin_type (arch)->builtin_int, n_args);

  1113.   if (sel >= n_args)
  1114.     error (_("Invalid probe argument %d -- probe has %u arguments available"),
  1115.            sel, n_args);

  1116.   return evaluate_probe_argument (pc_probe.probe, sel, frame);
  1117. }

  1118. /* This is called to compile one of the $_probe_arg* convenience
  1119.    variables into an agent expression.  */

  1120. static void
  1121. compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
  1122.                    struct axs_value *value, void *data)
  1123. {
  1124.   CORE_ADDR pc = expr->scope;
  1125.   int sel = (int) (uintptr_t) data;
  1126.   struct bound_probe pc_probe;
  1127.   const struct sym_probe_fns *pc_probe_fns;
  1128.   int n_args;
  1129.   struct frame_info *frame = get_selected_frame (NULL);

  1130.   /* SEL == -1 means "_probe_argc".  */
  1131.   gdb_assert (sel >= -1);

  1132.   pc_probe = find_probe_by_pc (pc);
  1133.   if (pc_probe.probe == NULL)
  1134.     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));

  1135.   n_args = get_probe_argument_count (pc_probe.probe, frame);

  1136.   if (sel == -1)
  1137.     {
  1138.       value->kind = axs_rvalue;
  1139.       value->type = builtin_type (expr->gdbarch)->builtin_int;
  1140.       ax_const_l (expr, n_args);
  1141.       return;
  1142.     }

  1143.   gdb_assert (sel >= 0);
  1144.   if (sel >= n_args)
  1145.     error (_("Invalid probe argument %d -- probe has %d arguments available"),
  1146.            sel, n_args);

  1147.   pc_probe.probe->pops->compile_to_ax (pc_probe.probe, expr, value, sel);
  1148. }



  1149. /* Set or clear a SystemTap semaphore.  ADDRESS is the semaphore's
  1150.    addressSET is zero if the semaphore should be cleared, or one
  1151.    if it should be set.  This is a helper function for `stap_semaphore_down'
  1152.    and `stap_semaphore_up'.  */

  1153. static void
  1154. stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
  1155. {
  1156.   gdb_byte bytes[sizeof (LONGEST)];
  1157.   /* The ABI specifies "unsigned short".  */
  1158.   struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
  1159.   ULONGEST value;

  1160.   if (address == 0)
  1161.     return;

  1162.   /* Swallow errors.  */
  1163.   if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
  1164.     {
  1165.       warning (_("Could not read the value of a SystemTap semaphore."));
  1166.       return;
  1167.     }

  1168.   value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
  1169.                                     gdbarch_byte_order (gdbarch));
  1170.   /* Note that we explicitly don't worry about overflow or
  1171.      underflow.  */
  1172.   if (set)
  1173.     ++value;
  1174.   else
  1175.     --value;

  1176.   store_unsigned_integer (bytes, TYPE_LENGTH (type),
  1177.                           gdbarch_byte_order (gdbarch), value);

  1178.   if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
  1179.     warning (_("Could not write the value of a SystemTap semaphore."));
  1180. }

  1181. /* Set a SystemTap semaphore.  SEM is the semaphore's address.  Semaphores
  1182.    act as reference counters, so calls to this function must be paired with
  1183.    calls to `stap_semaphore_down'.

  1184.    This function and `stap_semaphore_down' race with another tool changing
  1185.    the probes, but that is too rare to care.  */

  1186. static void
  1187. stap_set_semaphore (struct probe *probe_generic, struct objfile *objfile,
  1188.                     struct gdbarch *gdbarch)
  1189. {
  1190.   struct stap_probe *probe = (struct stap_probe *) probe_generic;
  1191.   CORE_ADDR addr;

  1192.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1193.   addr = (probe->sem_addr
  1194.           + ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile)));
  1195.   stap_modify_semaphore (addr, 1, gdbarch);
  1196. }

  1197. /* Clear a SystemTap semaphore.  SEM is the semaphore's address.  */

  1198. static void
  1199. stap_clear_semaphore (struct probe *probe_generic, struct objfile *objfile,
  1200.                       struct gdbarch *gdbarch)
  1201. {
  1202.   struct stap_probe *probe = (struct stap_probe *) probe_generic;
  1203.   CORE_ADDR addr;

  1204.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1205.   addr = (probe->sem_addr
  1206.           + ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile)));
  1207.   stap_modify_semaphore (addr, 0, gdbarch);
  1208. }

  1209. /* Implementation of `$_probe_arg*' set of variables.  */

  1210. static const struct internalvar_funcs probe_funcs =
  1211. {
  1212.   compute_probe_arg,
  1213.   compile_probe_arg,
  1214.   NULL
  1215. };

  1216. /* Helper function that parses the information contained in a
  1217.    SystemTap's probe.  Basically, the information consists in:

  1218.    - Probe's PC address;
  1219.    - Link-time section address of `.stapsdt.base' section;
  1220.    - Link-time address of the semaphore variable, or ZERO if the
  1221.      probe doesn't have an associated semaphore;
  1222.    - Probe's provider name;
  1223.    - Probe's name;
  1224.    - Probe's argument format

  1225.    This function returns 1 if the handling was successful, and zero
  1226.    otherwise.  */

  1227. static void
  1228. handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
  1229.                    VEC (probe_p) **probesp, CORE_ADDR base)
  1230. {
  1231.   bfd *abfd = objfile->obfd;
  1232.   int size = bfd_get_arch_size (abfd) / 8;
  1233.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1234.   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  1235.   CORE_ADDR base_ref;
  1236.   const char *probe_args = NULL;
  1237.   struct stap_probe *ret;

  1238.   ret = obstack_alloc (&objfile->per_bfd->storage_obstack, sizeof (*ret));
  1239.   ret->p.pops = &stap_probe_ops;
  1240.   ret->p.arch = gdbarch;

  1241.   /* Provider and the name of the probe.  */
  1242.   ret->p.provider = (char *) &el->data[3 * size];
  1243.   ret->p.name = memchr (ret->p.provider, '\0',
  1244.                         (char *) el->data + el->size - ret->p.provider);
  1245.   /* Making sure there is a name.  */
  1246.   if (ret->p.name == NULL)
  1247.     {
  1248.       complaint (&symfile_complaints, _("corrupt probe name when "
  1249.                                         "reading `%s'"),
  1250.                  objfile_name (objfile));

  1251.       /* There is no way to use a probe without a name or a provider, so
  1252.          returning zero here makes sense.  */
  1253.       return;
  1254.     }
  1255.   else
  1256.     ++ret->p.name;

  1257.   /* Retrieving the probe's address.  */
  1258.   ret->p.address = extract_typed_address (&el->data[0], ptr_type);

  1259.   /* Link-time sh_addr of `.stapsdt.base' section.  */
  1260.   base_ref = extract_typed_address (&el->data[size], ptr_type);

  1261.   /* Semaphore address.  */
  1262.   ret->sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);

  1263.   ret->p.address += base - base_ref;
  1264.   if (ret->sem_addr != 0)
  1265.     ret->sem_addr += base - base_ref;

  1266.   /* Arguments.  We can only extract the argument format if there is a valid
  1267.      name for this probe.  */
  1268.   probe_args = memchr (ret->p.name, '\0',
  1269.                        (char *) el->data + el->size - ret->p.name);

  1270.   if (probe_args != NULL)
  1271.     ++probe_args;

  1272.   if (probe_args == NULL
  1273.       || (memchr (probe_args, '\0', (char *) el->data + el->size - ret->p.name)
  1274.           != el->data + el->size - 1))
  1275.     {
  1276.       complaint (&symfile_complaints, _("corrupt probe argument when "
  1277.                                         "reading `%s'"),
  1278.                  objfile_name (objfile));
  1279.       /* If the argument string is NULL, it means some problem happened with
  1280.          it.  So we return 0.  */
  1281.       return;
  1282.     }

  1283.   ret->args_parsed = 0;
  1284.   ret->args_u.text = (void *) probe_args;

  1285.   /* Successfully created probe.  */
  1286.   VEC_safe_push (probe_p, *probesp, (struct probe *) ret);
  1287. }

  1288. /* Helper function which tries to find the base address of the SystemTap
  1289.    base section named STAP_BASE_SECTION_NAME.  */

  1290. static void
  1291. get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
  1292. {
  1293.   asection **ret = obj;

  1294.   if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
  1295.       && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
  1296.     *ret = sect;
  1297. }

  1298. /* Helper function which iterates over every section in the BFD file,
  1299.    trying to find the base address of the SystemTap base section.
  1300.    Returns 1 if found (setting BASE to the proper value), zero otherwise.  */

  1301. static int
  1302. get_stap_base_address (bfd *obfd, bfd_vma *base)
  1303. {
  1304.   asection *ret = NULL;

  1305.   bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);

  1306.   if (ret == NULL)
  1307.     {
  1308.       complaint (&symfile_complaints, _("could not obtain base address for "
  1309.                                         "SystemTap section on objfile `%s'."),
  1310.                  obfd->filename);
  1311.       return 0;
  1312.     }

  1313.   if (base != NULL)
  1314.     *base = ret->vma;

  1315.   return 1;
  1316. }

  1317. /* Helper function for `elf_get_probes', which gathers information about all
  1318.    SystemTap probes from OBJFILE.  */

  1319. static void
  1320. stap_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
  1321. {
  1322.   /* If we are here, then this is the first time we are parsing the
  1323.      SystemTap probe's information.  We basically have to count how many
  1324.      probes the objfile has, and then fill in the necessary information
  1325.      for each one.  */
  1326.   bfd *obfd = objfile->obfd;
  1327.   bfd_vma base;
  1328.   struct sdt_note *iter;
  1329.   unsigned save_probesp_len = VEC_length (probe_p, *probesp);

  1330.   if (objfile->separate_debug_objfile_backlink != NULL)
  1331.     {
  1332.       /* This is a .debug file, not the objfile itself.  */
  1333.       return;
  1334.     }

  1335.   if (elf_tdata (obfd)->sdt_note_head == NULL)
  1336.     {
  1337.       /* There isn't any probe here.  */
  1338.       return;
  1339.     }

  1340.   if (!get_stap_base_address (obfd, &base))
  1341.     {
  1342.       /* There was an error finding the base address for the section.
  1343.          Just return NULL.  */
  1344.       return;
  1345.     }

  1346.   /* Parsing each probe's information.  */
  1347.   for (iter = elf_tdata (obfd)->sdt_note_head;
  1348.        iter != NULL;
  1349.        iter = iter->next)
  1350.     {
  1351.       /* We first have to handle all the information about the
  1352.          probe which is present in the section.  */
  1353.       handle_stap_probe (objfile, iter, probesp, base);
  1354.     }

  1355.   if (save_probesp_len == VEC_length (probe_p, *probesp))
  1356.     {
  1357.       /* If we are here, it means we have failed to parse every known
  1358.          probe.  */
  1359.       complaint (&symfile_complaints, _("could not parse SystemTap probe(s) "
  1360.                                         "from inferior"));
  1361.       return;
  1362.     }
  1363. }

  1364. static int
  1365. stap_probe_is_linespec (const char **linespecp)
  1366. {
  1367.   static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };

  1368.   return probe_is_linespec_by_keyword (linespecp, keywords);
  1369. }

  1370. static void
  1371. stap_gen_info_probes_table_header (VEC (info_probe_column_s) **heads)
  1372. {
  1373.   info_probe_column_s stap_probe_column;

  1374.   stap_probe_column.field_name = "semaphore";
  1375.   stap_probe_column.print_name = _("Semaphore");

  1376.   VEC_safe_push (info_probe_column_s, *heads, &stap_probe_column);
  1377. }

  1378. static void
  1379. stap_gen_info_probes_table_values (struct probe *probe_generic,
  1380.                                    VEC (const_char_ptr) **ret)
  1381. {
  1382.   struct stap_probe *probe = (struct stap_probe *) probe_generic;
  1383.   struct gdbarch *gdbarch;
  1384.   const char *val = NULL;

  1385.   gdb_assert (probe_generic->pops == &stap_probe_ops);

  1386.   gdbarch = probe->p.arch;

  1387.   if (probe->sem_addr != 0)
  1388.     val = print_core_address (gdbarch, probe->sem_addr);

  1389.   VEC_safe_push (const_char_ptr, *ret, val);
  1390. }

  1391. /* SystemTap probe_ops.  */

  1392. static const struct probe_ops stap_probe_ops =
  1393. {
  1394.   stap_probe_is_linespec,
  1395.   stap_get_probes,
  1396.   stap_get_probe_address,
  1397.   stap_get_probe_argument_count,
  1398.   stap_can_evaluate_probe_arguments,
  1399.   stap_evaluate_probe_argument,
  1400.   stap_compile_to_ax,
  1401.   stap_set_semaphore,
  1402.   stap_clear_semaphore,
  1403.   stap_probe_destroy,
  1404.   stap_gen_info_probes_table_header,
  1405.   stap_gen_info_probes_table_values,
  1406. };

  1407. /* Implementation of the `info probes stap' command.  */

  1408. static void
  1409. info_probes_stap_command (char *arg, int from_tty)
  1410. {
  1411.   info_probes_for_ops (arg, from_tty, &stap_probe_ops);
  1412. }

  1413. void _initialize_stap_probe (void);

  1414. void
  1415. _initialize_stap_probe (void)
  1416. {
  1417.   VEC_safe_push (probe_ops_cp, all_probe_ops, &stap_probe_ops);

  1418.   add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
  1419.                              &stap_expression_debug,
  1420.                              _("Set SystemTap expression debugging."),
  1421.                              _("Show SystemTap expression debugging."),
  1422.                              _("When non-zero, the internal representation "
  1423.                                "of SystemTap expressions will be printed."),
  1424.                              NULL,
  1425.                              show_stapexpressiondebug,
  1426.                              &setdebuglist, &showdebuglist);

  1427.   create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
  1428.                                 (void *) (uintptr_t) -1);
  1429.   create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
  1430.                                 (void *) (uintptr_t) 0);
  1431.   create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
  1432.                                 (void *) (uintptr_t) 1);
  1433.   create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
  1434.                                 (void *) (uintptr_t) 2);
  1435.   create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
  1436.                                 (void *) (uintptr_t) 3);
  1437.   create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
  1438.                                 (void *) (uintptr_t) 4);
  1439.   create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
  1440.                                 (void *) (uintptr_t) 5);
  1441.   create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
  1442.                                 (void *) (uintptr_t) 6);
  1443.   create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
  1444.                                 (void *) (uintptr_t) 7);
  1445.   create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
  1446.                                 (void *) (uintptr_t) 8);
  1447.   create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
  1448.                                 (void *) (uintptr_t) 9);
  1449.   create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
  1450.                                 (void *) (uintptr_t) 10);
  1451.   create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
  1452.                                 (void *) (uintptr_t) 11);

  1453.   add_cmd ("stap", class_info, info_probes_stap_command,
  1454.            _("\
  1455. Show information about SystemTap static probes.\n\
  1456. Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
  1457. Each argument is a regular expression, used to select probes.\n\
  1458. PROVIDER matches probe provider names.\n\
  1459. NAME matches the probe names.\n\
  1460. OBJECT matches the executable or shared library name."),
  1461.            info_probes_cmdlist_get ());

  1462. }