GDB (API)
/home/stan/gdb/src/gdb/stap-probe.c
Go to the documentation of this file.
00001 /* SystemTap probe support for GDB.
00002 
00003    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "stap-probe.h"
00022 #include "probe.h"
00023 #include "vec.h"
00024 #include "ui-out.h"
00025 #include "objfiles.h"
00026 #include "arch-utils.h"
00027 #include "command.h"
00028 #include "gdbcmd.h"
00029 #include "filenames.h"
00030 #include "value.h"
00031 #include "exceptions.h"
00032 #include "ax.h"
00033 #include "ax-gdb.h"
00034 #include "complaints.h"
00035 #include "cli/cli-utils.h"
00036 #include "linespec.h"
00037 #include "user-regs.h"
00038 #include "parser-defs.h"
00039 #include "language.h"
00040 #include "elf-bfd.h"
00041 
00042 #include <ctype.h>
00043 
00044 /* The name of the SystemTap section where we will find information about
00045    the probes.  */
00046 
00047 #define STAP_BASE_SECTION_NAME ".stapsdt.base"
00048 
00049 /* Forward declaration. */
00050 
00051 static const struct probe_ops stap_probe_ops;
00052 
00053 /* Should we display debug information for the probe's argument expression
00054    parsing?  */
00055 
00056 static unsigned int stap_expression_debug = 0;
00057 
00058 /* The various possibilities of bitness defined for a probe's argument.
00059 
00060    The relationship is:
00061 
00062    - STAP_ARG_BITNESS_UNDEFINED:  The user hasn't specified the bitness.
00063    - STAP_ARG_BITNESS_32BIT_UNSIGNED:  argument string starts with `4@'.
00064    - STAP_ARG_BITNESS_32BIT_SIGNED:  argument string starts with `-4@'.
00065    - STAP_ARG_BITNESS_64BIT_UNSIGNED:  argument string starts with `8@'.
00066    - STAP_ARG_BITNESS_64BIT_SIGNED:  argument string starts with `-8@'.  */
00067 
00068 enum stap_arg_bitness
00069 {
00070   STAP_ARG_BITNESS_UNDEFINED,
00071   STAP_ARG_BITNESS_32BIT_UNSIGNED,
00072   STAP_ARG_BITNESS_32BIT_SIGNED,
00073   STAP_ARG_BITNESS_64BIT_UNSIGNED,
00074   STAP_ARG_BITNESS_64BIT_SIGNED,
00075 };
00076 
00077 /* The following structure represents a single argument for the probe.  */
00078 
00079 struct stap_probe_arg
00080 {
00081   /* The bitness of this argument.  */
00082   enum stap_arg_bitness bitness;
00083 
00084   /* The corresponding `struct type *' to the bitness.  */
00085   struct type *atype;
00086 
00087   /* The argument converted to an internal GDB expression.  */
00088   struct expression *aexpr;
00089 };
00090 
00091 typedef struct stap_probe_arg stap_probe_arg_s;
00092 DEF_VEC_O (stap_probe_arg_s);
00093 
00094 struct stap_probe
00095 {
00096   /* Generic information about the probe.  This shall be the first element
00097      of this struct, in order to maintain binary compatibility with the
00098      `struct probe' and be able to fully abstract it.  */
00099   struct probe p;
00100 
00101   /* If the probe has a semaphore associated, then this is the value of
00102      it.  */
00103   CORE_ADDR sem_addr;
00104 
00105   unsigned int args_parsed : 1;
00106   union
00107     {
00108       const char *text;
00109 
00110       /* Information about each argument.  This is an array of `stap_probe_arg',
00111          with each entry representing one argument.  */
00112       VEC (stap_probe_arg_s) *vec;
00113     }
00114   args_u;
00115 };
00116 
00117 /* When parsing the arguments, we have to establish different precedences
00118    for the various kinds of asm operators.  This enumeration represents those
00119    precedences.
00120 
00121    This logic behind this is available at
00122    <http://sourceware.org/binutils/docs/as/Infix-Ops.html#Infix-Ops>, or using
00123    the command "info '(as)Infix Ops'".  */
00124 
00125 enum stap_operand_prec
00126 {
00127   /* Lowest precedence, used for non-recognized operands or for the beginning
00128      of the parsing process.  */
00129   STAP_OPERAND_PREC_NONE = 0,
00130 
00131   /* Precedence of logical OR.  */
00132   STAP_OPERAND_PREC_LOGICAL_OR,
00133 
00134   /* Precedence of logical AND.  */
00135   STAP_OPERAND_PREC_LOGICAL_AND,
00136 
00137   /* Precedence of additive (plus, minus) and comparative (equal, less,
00138      greater-than, etc) operands.  */
00139   STAP_OPERAND_PREC_ADD_CMP,
00140 
00141   /* Precedence of bitwise operands (bitwise OR, XOR, bitwise AND,
00142      logical NOT).  */
00143   STAP_OPERAND_PREC_BITWISE,
00144 
00145   /* Precedence of multiplicative operands (multiplication, division,
00146      remainder, left shift and right shift).  */
00147   STAP_OPERAND_PREC_MUL
00148 };
00149 
00150 static void stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
00151                                    enum stap_operand_prec prec);
00152 
00153 static void stap_parse_argument_conditionally (struct stap_parse_info *p);
00154 
00155 /* Returns 1 if *S is an operator, zero otherwise.  */
00156 
00157 static int stap_is_operator (const char *op);
00158 
00159 static void
00160 show_stapexpressiondebug (struct ui_file *file, int from_tty,
00161                           struct cmd_list_element *c, const char *value)
00162 {
00163   fprintf_filtered (file, _("SystemTap Probe expression debugging is %s.\n"),
00164                     value);
00165 }
00166 
00167 /* Returns the operator precedence level of OP, or STAP_OPERAND_PREC_NONE
00168    if the operator code was not recognized.  */
00169 
00170 static enum stap_operand_prec
00171 stap_get_operator_prec (enum exp_opcode op)
00172 {
00173   switch (op)
00174     {
00175     case BINOP_LOGICAL_OR:
00176       return STAP_OPERAND_PREC_LOGICAL_OR;
00177 
00178     case BINOP_LOGICAL_AND:
00179       return STAP_OPERAND_PREC_LOGICAL_AND;
00180 
00181     case BINOP_ADD:
00182     case BINOP_SUB:
00183     case BINOP_EQUAL:
00184     case BINOP_NOTEQUAL:
00185     case BINOP_LESS:
00186     case BINOP_LEQ:
00187     case BINOP_GTR:
00188     case BINOP_GEQ:
00189       return STAP_OPERAND_PREC_ADD_CMP;
00190 
00191     case BINOP_BITWISE_IOR:
00192     case BINOP_BITWISE_AND:
00193     case BINOP_BITWISE_XOR:
00194     case UNOP_LOGICAL_NOT:
00195       return STAP_OPERAND_PREC_BITWISE;
00196 
00197     case BINOP_MUL:
00198     case BINOP_DIV:
00199     case BINOP_REM:
00200     case BINOP_LSH:
00201     case BINOP_RSH:
00202       return STAP_OPERAND_PREC_MUL;
00203 
00204     default:
00205       return STAP_OPERAND_PREC_NONE;
00206     }
00207 }
00208 
00209 /* Given S, read the operator in it and fills the OP pointer with its code.
00210    Return 1 on success, zero if the operator was not recognized.  */
00211 
00212 static enum exp_opcode
00213 stap_get_opcode (const char **s)
00214 {
00215   const char c = **s;
00216   enum exp_opcode op;
00217 
00218   *s += 1;
00219 
00220   switch (c)
00221     {
00222     case '*':
00223       op = BINOP_MUL;
00224       break;
00225 
00226     case '/':
00227       op = BINOP_DIV;
00228       break;
00229 
00230     case '%':
00231       op = BINOP_REM;
00232     break;
00233 
00234     case '<':
00235       op = BINOP_LESS;
00236       if (**s == '<')
00237         {
00238           *s += 1;
00239           op = BINOP_LSH;
00240         }
00241       else if (**s == '=')
00242         {
00243           *s += 1;
00244           op = BINOP_LEQ;
00245         }
00246       else if (**s == '>')
00247         {
00248           *s += 1;
00249           op = BINOP_NOTEQUAL;
00250         }
00251     break;
00252 
00253     case '>':
00254       op = BINOP_GTR;
00255       if (**s == '>')
00256         {
00257           *s += 1;
00258           op = BINOP_RSH;
00259         }
00260       else if (**s == '=')
00261         {
00262           *s += 1;
00263           op = BINOP_GEQ;
00264         }
00265     break;
00266 
00267     case '|':
00268       op = BINOP_BITWISE_IOR;
00269       if (**s == '|')
00270         {
00271           *s += 1;
00272           op = BINOP_LOGICAL_OR;
00273         }
00274     break;
00275 
00276     case '&':
00277       op = BINOP_BITWISE_AND;
00278       if (**s == '&')
00279         {
00280           *s += 1;
00281           op = BINOP_LOGICAL_AND;
00282         }
00283     break;
00284 
00285     case '^':
00286       op = BINOP_BITWISE_XOR;
00287       break;
00288 
00289     case '!':
00290       op = UNOP_LOGICAL_NOT;
00291       break;
00292 
00293     case '+':
00294       op = BINOP_ADD;
00295       break;
00296 
00297     case '-':
00298       op = BINOP_SUB;
00299       break;
00300 
00301     case '=':
00302       gdb_assert (**s == '=');
00303       op = BINOP_EQUAL;
00304       break;
00305 
00306     default:
00307       internal_error (__FILE__, __LINE__,
00308                       _("Invalid opcode in expression `%s' for SystemTap"
00309                         "probe"), *s);
00310     }
00311 
00312   return op;
00313 }
00314 
00315 /* Given the bitness of the argument, represented by B, return the
00316    corresponding `struct type *'.  */
00317 
00318 static struct type *
00319 stap_get_expected_argument_type (struct gdbarch *gdbarch,
00320                                  enum stap_arg_bitness b)
00321 {
00322   switch (b)
00323     {
00324     case STAP_ARG_BITNESS_UNDEFINED:
00325       if (gdbarch_addr_bit (gdbarch) == 32)
00326         return builtin_type (gdbarch)->builtin_uint32;
00327       else
00328         return builtin_type (gdbarch)->builtin_uint64;
00329 
00330     case STAP_ARG_BITNESS_32BIT_SIGNED:
00331       return builtin_type (gdbarch)->builtin_int32;
00332 
00333     case STAP_ARG_BITNESS_32BIT_UNSIGNED:
00334       return builtin_type (gdbarch)->builtin_uint32;
00335 
00336     case STAP_ARG_BITNESS_64BIT_SIGNED:
00337       return builtin_type (gdbarch)->builtin_int64;
00338 
00339     case STAP_ARG_BITNESS_64BIT_UNSIGNED:
00340       return builtin_type (gdbarch)->builtin_uint64;
00341 
00342     default:
00343       internal_error (__FILE__, __LINE__,
00344                       _("Undefined bitness for probe."));
00345       break;
00346     }
00347 }
00348 
00349 /* Function responsible for parsing a register operand according to
00350    SystemTap parlance.  Assuming:
00351 
00352    RP  = register prefix
00353    RS  = register suffix
00354    RIP = register indirection prefix
00355    RIS = register indirection suffix
00356    
00357    Then a register operand can be:
00358    
00359    [RIP] [RP] REGISTER [RS] [RIS]
00360 
00361    This function takes care of a register's indirection, displacement and
00362    direct access.  It also takes into consideration the fact that some
00363    registers are named differently inside and outside GDB, e.g., PPC's
00364    general-purpose registers are represented by integers in the assembly
00365    language (e.g., `15' is the 15th general-purpose register), but inside
00366    GDB they have a prefix (the letter `r') appended.  */
00367 
00368 static void
00369 stap_parse_register_operand (struct stap_parse_info *p)
00370 {
00371   /* Simple flag to indicate whether we have seen a minus signal before
00372      certain number.  */
00373   int got_minus = 0;
00374 
00375   /* Flags to indicate whether this register access is being displaced and/or
00376      indirected.  */
00377   int disp_p = 0, indirect_p = 0;
00378   struct gdbarch *gdbarch = p->gdbarch;
00379 
00380   /* Needed to generate the register name as a part of an expression.  */
00381   struct stoken str;
00382 
00383   /* Variables used to extract the register name from the probe's
00384      argument.  */
00385   const char *start;
00386   char *regname;
00387   int len;
00388 
00389   /* Prefixes for the parser.  */
00390   const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch);
00391   const char *reg_ind_prefix
00392     = gdbarch_stap_register_indirection_prefix (gdbarch);
00393   const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
00394   int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0;
00395   int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0;
00396   int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0;
00397 
00398   /* Suffixes for the parser.  */
00399   const char *reg_suffix = gdbarch_stap_register_suffix (gdbarch);
00400   const char *reg_ind_suffix
00401     = gdbarch_stap_register_indirection_suffix (gdbarch);
00402   const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
00403   int reg_suffix_len = reg_suffix ? strlen (reg_suffix) : 0;
00404   int reg_ind_suffix_len = reg_ind_suffix ? strlen (reg_ind_suffix) : 0;
00405   int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0;
00406 
00407   /* Checking for a displacement argument.  */
00408   if (*p->arg == '+')
00409     {
00410       /* If it's a plus sign, we don't need to do anything, just advance the
00411          pointer.  */
00412       ++p->arg;
00413     }
00414 
00415   if (*p->arg == '-')
00416     {
00417       got_minus = 1;
00418       ++p->arg;
00419     }
00420 
00421   if (isdigit (*p->arg))
00422     {
00423       /* The value of the displacement.  */
00424       long displacement;
00425       char *endp;
00426 
00427       disp_p = 1;
00428       displacement = strtol (p->arg, &endp, 10);
00429       p->arg = endp;
00430 
00431       /* Generating the expression for the displacement.  */
00432       write_exp_elt_opcode (OP_LONG);
00433       write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
00434       write_exp_elt_longcst (displacement);
00435       write_exp_elt_opcode (OP_LONG);
00436       if (got_minus)
00437         write_exp_elt_opcode (UNOP_NEG);
00438     }
00439 
00440   /* Getting rid of register indirection prefix.  */
00441   if (reg_ind_prefix
00442       && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0)
00443     {
00444       indirect_p = 1;
00445       p->arg += reg_ind_prefix_len;
00446     }
00447 
00448   if (disp_p && !indirect_p)
00449     error (_("Invalid register displacement syntax on expression `%s'."),
00450            p->saved_arg);
00451 
00452   /* Getting rid of register prefix.  */
00453   if (reg_prefix && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0)
00454     p->arg += reg_prefix_len;
00455 
00456   /* Now we should have only the register name.  Let's extract it and get
00457      the associated number.  */
00458   start = p->arg;
00459 
00460   /* We assume the register name is composed by letters and numbers.  */
00461   while (isalnum (*p->arg))
00462     ++p->arg;
00463 
00464   len = p->arg - start;
00465 
00466   regname = alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1);
00467   regname[0] = '\0';
00468 
00469   /* We only add the GDB's register prefix/suffix if we are dealing with
00470      a numeric register.  */
00471   if (gdb_reg_prefix && isdigit (*start))
00472     {
00473       strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len);
00474       strncpy (regname + gdb_reg_prefix_len, start, len);
00475 
00476       if (gdb_reg_suffix)
00477         strncpy (regname + gdb_reg_prefix_len + len,
00478                  gdb_reg_suffix, gdb_reg_suffix_len);
00479 
00480       len += gdb_reg_prefix_len + gdb_reg_suffix_len;
00481     }
00482   else
00483     strncpy (regname, start, len);
00484 
00485   regname[len] = '\0';
00486 
00487   /* Is this a valid register name?  */
00488   if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
00489     error (_("Invalid register name `%s' on expression `%s'."),
00490            regname, p->saved_arg);
00491 
00492   write_exp_elt_opcode (OP_REGISTER);
00493   str.ptr = regname;
00494   str.length = len;
00495   write_exp_string (str);
00496   write_exp_elt_opcode (OP_REGISTER);
00497 
00498   if (indirect_p)
00499     {
00500       if (disp_p)
00501         write_exp_elt_opcode (BINOP_ADD);
00502 
00503       /* Casting to the expected type.  */
00504       write_exp_elt_opcode (UNOP_CAST);
00505       write_exp_elt_type (lookup_pointer_type (p->arg_type));
00506       write_exp_elt_opcode (UNOP_CAST);
00507 
00508       write_exp_elt_opcode (UNOP_IND);
00509     }
00510 
00511   /* Getting rid of the register name suffix.  */
00512   if (reg_suffix)
00513     {
00514       if (strncmp (p->arg, reg_suffix, reg_suffix_len) != 0)
00515         error (_("Missing register name suffix `%s' on expression `%s'."),
00516                reg_suffix, p->saved_arg);
00517 
00518       p->arg += reg_suffix_len;
00519     }
00520 
00521   /* Getting rid of the register indirection suffix.  */
00522   if (indirect_p && reg_ind_suffix)
00523     {
00524       if (strncmp (p->arg, reg_ind_suffix, reg_ind_suffix_len) != 0)
00525         error (_("Missing indirection suffix `%s' on expression `%s'."),
00526                reg_ind_suffix, p->saved_arg);
00527 
00528       p->arg += reg_ind_suffix_len;
00529     }
00530 }
00531 
00532 /* This function is responsible for parsing a single operand.
00533 
00534    A single operand can be:
00535 
00536       - an unary operation (e.g., `-5', `~2', or even with subexpressions
00537         like `-(2 + 1)')
00538       - a register displacement, which will be treated as a register
00539         operand (e.g., `-4(%eax)' on x86)
00540       - a numeric constant, or
00541       - a register operand (see function `stap_parse_register_operand')
00542 
00543    The function also calls special-handling functions to deal with
00544    unrecognized operands, allowing arch-specific parsers to be
00545    created.  */
00546 
00547 static void
00548 stap_parse_single_operand (struct stap_parse_info *p)
00549 {
00550   struct gdbarch *gdbarch = p->gdbarch;
00551 
00552   /* Prefixes for the parser.  */
00553   const char *const_prefix = gdbarch_stap_integer_prefix (gdbarch);
00554   const char *reg_prefix = gdbarch_stap_register_prefix (gdbarch);
00555   const char *reg_ind_prefix
00556     = gdbarch_stap_register_indirection_prefix (gdbarch);
00557   int const_prefix_len = const_prefix ? strlen (const_prefix) : 0;
00558   int reg_prefix_len = reg_prefix ? strlen (reg_prefix) : 0;
00559   int reg_ind_prefix_len = reg_ind_prefix ? strlen (reg_ind_prefix) : 0;
00560 
00561   /* Suffixes for the parser.  */
00562   const char *const_suffix = gdbarch_stap_integer_suffix (gdbarch);
00563   int const_suffix_len = const_suffix ? strlen (const_suffix) : 0;
00564 
00565   /* We first try to parse this token as a "special token".  */
00566   if (gdbarch_stap_parse_special_token_p (gdbarch))
00567     {
00568       int ret = gdbarch_stap_parse_special_token (gdbarch, p);
00569 
00570       if (ret)
00571         {
00572           /* If the return value of the above function is not zero,
00573              it means it successfully parsed the special token.
00574 
00575              If it is NULL, we try to parse it using our method.  */
00576           return;
00577         }
00578     }
00579 
00580   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
00581     {
00582       char c = *p->arg;
00583       int number;
00584 
00585       /* We use this variable to do a lookahead.  */
00586       const char *tmp = p->arg;
00587 
00588       ++tmp;
00589 
00590       /* This is an unary operation.  Here is a list of allowed tokens
00591          here:
00592 
00593          - numeric literal;
00594          - number (from register displacement)
00595          - subexpression (beginning with `(')
00596 
00597          We handle the register displacement here, and the other cases
00598          recursively.  */
00599       if (p->inside_paren_p)
00600         tmp = skip_spaces_const (tmp);
00601 
00602       if (isdigit (*tmp))
00603         {
00604           char *endp;
00605 
00606           number = strtol (tmp, &endp, 10);
00607           tmp = endp;
00608         }
00609 
00610       if (!reg_ind_prefix
00611           || strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0)
00612         {
00613           /* This is not a displacement.  We skip the operator, and deal
00614              with it later.  */
00615           ++p->arg;
00616           stap_parse_argument_conditionally (p);
00617           if (c == '-')
00618             write_exp_elt_opcode (UNOP_NEG);
00619           else if (c == '~')
00620             write_exp_elt_opcode (UNOP_COMPLEMENT);
00621         }
00622       else
00623         {
00624           /* If we are here, it means it is a displacement.  The only
00625              operations allowed here are `-' and `+'.  */
00626           if (c == '~')
00627             error (_("Invalid operator `%c' for register displacement "
00628                      "on expression `%s'."), c, p->saved_arg);
00629 
00630           stap_parse_register_operand (p);
00631         }
00632     }
00633   else if (isdigit (*p->arg))
00634     {
00635       /* A temporary variable, needed for lookahead.  */
00636       const char *tmp = p->arg;
00637       char *endp;
00638       long number;
00639 
00640       /* We can be dealing with a numeric constant (if `const_prefix' is
00641          NULL), or with a register displacement.  */
00642       number = strtol (tmp, &endp, 10);
00643       tmp = endp;
00644 
00645       if (p->inside_paren_p)
00646         tmp = skip_spaces_const (tmp);
00647       if (!const_prefix && reg_ind_prefix
00648           && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) != 0)
00649         {
00650           /* We are dealing with a numeric constant.  */
00651           write_exp_elt_opcode (OP_LONG);
00652           write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
00653           write_exp_elt_longcst (number);
00654           write_exp_elt_opcode (OP_LONG);
00655 
00656           p->arg = tmp;
00657 
00658           if (const_suffix)
00659             {
00660               if (strncmp (p->arg, const_suffix, const_suffix_len) == 0)
00661                 p->arg += const_suffix_len;
00662               else
00663                 error (_("Invalid constant suffix on expression `%s'."),
00664                        p->saved_arg);
00665             }
00666         }
00667       else if (reg_ind_prefix
00668                && strncmp (tmp, reg_ind_prefix, reg_ind_prefix_len) == 0)
00669         stap_parse_register_operand (p);
00670       else
00671         error (_("Unknown numeric token on expression `%s'."),
00672                p->saved_arg);
00673     }
00674   else if (const_prefix
00675            && strncmp (p->arg, const_prefix, const_prefix_len) == 0)
00676     {
00677       /* We are dealing with a numeric constant.  */
00678       long number;
00679       char *endp;
00680 
00681       p->arg += const_prefix_len;
00682       number = strtol (p->arg, &endp, 10);
00683       p->arg = endp;
00684 
00685       write_exp_elt_opcode (OP_LONG);
00686       write_exp_elt_type (builtin_type (gdbarch)->builtin_long);
00687       write_exp_elt_longcst (number);
00688       write_exp_elt_opcode (OP_LONG);
00689 
00690       if (const_suffix)
00691         {
00692           if (strncmp (p->arg, const_suffix, const_suffix_len) == 0)
00693             p->arg += const_suffix_len;
00694           else
00695             error (_("Invalid constant suffix on expression `%s'."),
00696                    p->saved_arg);
00697         }
00698     }
00699   else if ((reg_prefix
00700             && strncmp (p->arg, reg_prefix, reg_prefix_len) == 0)
00701            || (reg_ind_prefix
00702                && strncmp (p->arg, reg_ind_prefix, reg_ind_prefix_len) == 0))
00703     stap_parse_register_operand (p);
00704   else
00705     error (_("Operator `%c' not recognized on expression `%s'."),
00706            *p->arg, p->saved_arg);
00707 }
00708 
00709 /* This function parses an argument conditionally, based on single or
00710    non-single operands.  A non-single operand would be a parenthesized
00711    expression (e.g., `(2 + 1)'), and a single operand is anything that
00712    starts with `-', `~', `+' (i.e., unary operators), a digit, or
00713    something recognized by `gdbarch_stap_is_single_operand'.  */
00714 
00715 static void
00716 stap_parse_argument_conditionally (struct stap_parse_info *p)
00717 {
00718   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+' /* Unary.  */
00719       || isdigit (*p->arg)
00720       || gdbarch_stap_is_single_operand (p->gdbarch, p->arg))
00721     stap_parse_single_operand (p);
00722   else if (*p->arg == '(')
00723     {
00724       /* We are dealing with a parenthesized operand.  It means we
00725          have to parse it as it was a separate expression, without
00726          left-side or precedence.  */
00727       ++p->arg;
00728       p->arg = skip_spaces_const (p->arg);
00729       ++p->inside_paren_p;
00730 
00731       stap_parse_argument_1 (p, 0, STAP_OPERAND_PREC_NONE);
00732 
00733       --p->inside_paren_p;
00734       if (*p->arg != ')')
00735         error (_("Missign close-paren on expression `%s'."),
00736                p->saved_arg);
00737 
00738       ++p->arg;
00739       if (p->inside_paren_p)
00740         p->arg = skip_spaces_const (p->arg);
00741     }
00742   else
00743     error (_("Cannot parse expression `%s'."), p->saved_arg);
00744 }
00745 
00746 /* Helper function for `stap_parse_argument'.  Please, see its comments to
00747    better understand what this function does.  */
00748 
00749 static void
00750 stap_parse_argument_1 (struct stap_parse_info *p, int has_lhs,
00751                        enum stap_operand_prec prec)
00752 {
00753   /* This is an operator-precedence parser.
00754 
00755      We work with left- and right-sides of expressions, and
00756      parse them depending on the precedence of the operators
00757      we find.  */
00758 
00759   if (p->inside_paren_p)
00760     p->arg = skip_spaces_const (p->arg);
00761 
00762   if (!has_lhs)
00763     {
00764       /* We were called without a left-side, either because this is the
00765          first call, or because we were called to parse a parenthesized
00766          expression.  It doesn't really matter; we have to parse the
00767          left-side in order to continue the process.  */
00768       stap_parse_argument_conditionally (p);
00769     }
00770 
00771   /* Start to parse the right-side, and to "join" left and right sides
00772      depending on the operation specified.
00773 
00774      This loop shall continue until we run out of characters in the input,
00775      or until we find a close-parenthesis, which means that we've reached
00776      the end of a sub-expression.  */
00777   while (p->arg && *p->arg && *p->arg != ')' && !isspace (*p->arg))
00778     {
00779       const char *tmp_exp_buf;
00780       enum exp_opcode opcode;
00781       enum stap_operand_prec cur_prec;
00782 
00783       if (!stap_is_operator (p->arg))
00784         error (_("Invalid operator `%c' on expression `%s'."), *p->arg,
00785                p->saved_arg);
00786 
00787       /* We have to save the current value of the expression buffer because
00788          the `stap_get_opcode' modifies it in order to get the current
00789          operator.  If this operator's precedence is lower than PREC, we
00790          should return and not advance the expression buffer pointer.  */
00791       tmp_exp_buf = p->arg;
00792       opcode = stap_get_opcode (&tmp_exp_buf);
00793 
00794       cur_prec = stap_get_operator_prec (opcode);
00795       if (cur_prec < prec)
00796         {
00797           /* If the precedence of the operator that we are seeing now is
00798              lower than the precedence of the first operator seen before
00799              this parsing process began, it means we should stop parsing
00800              and return.  */
00801           break;
00802         }
00803 
00804       p->arg = tmp_exp_buf;
00805       if (p->inside_paren_p)
00806         p->arg = skip_spaces_const (p->arg);
00807 
00808       /* Parse the right-side of the expression.  */
00809       stap_parse_argument_conditionally (p);
00810 
00811       /* While we still have operators, try to parse another
00812          right-side, but using the current right-side as a left-side.  */
00813       while (*p->arg && stap_is_operator (p->arg))
00814         {
00815           enum exp_opcode lookahead_opcode;
00816           enum stap_operand_prec lookahead_prec;
00817 
00818           /* Saving the current expression buffer position.  The explanation
00819              is the same as above.  */
00820           tmp_exp_buf = p->arg;
00821           lookahead_opcode = stap_get_opcode (&tmp_exp_buf);
00822           lookahead_prec = stap_get_operator_prec (lookahead_opcode);
00823 
00824           if (lookahead_prec <= prec)
00825             {
00826               /* If we are dealing with an operator whose precedence is lower
00827                  than the first one, just abandon the attempt.  */
00828               break;
00829             }
00830 
00831           /* Parse the right-side of the expression, but since we already
00832              have a left-side at this point, set `has_lhs' to 1.  */
00833           stap_parse_argument_1 (p, 1, lookahead_prec);
00834         }
00835 
00836       write_exp_elt_opcode (opcode);
00837     }
00838 }
00839 
00840 /* Parse a probe's argument.
00841 
00842    Assuming that:
00843 
00844    LP = literal integer prefix
00845    LS = literal integer suffix
00846 
00847    RP = register prefix
00848    RS = register suffix
00849 
00850    RIP = register indirection prefix
00851    RIS = register indirection suffix
00852 
00853    This routine assumes that arguments' tokens are of the form:
00854 
00855    - [LP] NUMBER [LS]
00856    - [RP] REGISTER [RS]
00857    - [RIP] [RP] REGISTER [RS] [RIS]
00858    - If we find a number without LP, we try to parse it as a literal integer
00859    constant (if LP == NULL), or as a register displacement.
00860    - We count parenthesis, and only skip whitespaces if we are inside them.
00861    - If we find an operator, we skip it.
00862 
00863    This function can also call a special function that will try to match
00864    unknown tokens.  It will return 1 if the argument has been parsed
00865    successfully, or zero otherwise.  */
00866 
00867 static struct expression *
00868 stap_parse_argument (const char **arg, struct type *atype,
00869                      struct gdbarch *gdbarch)
00870 {
00871   struct stap_parse_info p;
00872   struct cleanup *back_to;
00873 
00874   /* We need to initialize the expression buffer, in order to begin
00875      our parsing efforts.  The language here does not matter, since we
00876      are using our own parser.  */
00877   initialize_expout (10, current_language, gdbarch);
00878   back_to = make_cleanup (free_current_contents, &expout);
00879 
00880   p.saved_arg = *arg;
00881   p.arg = *arg;
00882   p.arg_type = atype;
00883   p.gdbarch = gdbarch;
00884   p.inside_paren_p = 0;
00885 
00886   stap_parse_argument_1 (&p, 0, STAP_OPERAND_PREC_NONE);
00887 
00888   discard_cleanups (back_to);
00889 
00890   gdb_assert (p.inside_paren_p == 0);
00891 
00892   /* Casting the final expression to the appropriate type.  */
00893   write_exp_elt_opcode (UNOP_CAST);
00894   write_exp_elt_type (atype);
00895   write_exp_elt_opcode (UNOP_CAST);
00896 
00897   reallocate_expout ();
00898 
00899   p.arg = skip_spaces_const (p.arg);
00900   *arg = p.arg;
00901 
00902   return expout;
00903 }
00904 
00905 /* Function which parses an argument string from PROBE, correctly splitting
00906    the arguments and storing their information in properly ways.
00907 
00908    Consider the following argument string (x86 syntax):
00909 
00910    `4@%eax 4@$10'
00911 
00912    We have two arguments, `%eax' and `$10', both with 32-bit unsigned bitness.
00913    This function basically handles them, properly filling some structures with
00914    this information.  */
00915 
00916 static void
00917 stap_parse_probe_arguments (struct stap_probe *probe)
00918 {
00919   const char *cur;
00920   struct gdbarch *gdbarch = get_objfile_arch (probe->p.objfile);
00921 
00922   gdb_assert (!probe->args_parsed);
00923   cur = probe->args_u.text;
00924   probe->args_parsed = 1;
00925   probe->args_u.vec = NULL;
00926 
00927   if (!cur || !*cur || *cur == ':')
00928     return;
00929 
00930   while (*cur)
00931     {
00932       struct stap_probe_arg arg;
00933       enum stap_arg_bitness b;
00934       int got_minus = 0;
00935       struct expression *expr;
00936 
00937       memset (&arg, 0, sizeof (arg));
00938 
00939       /* We expect to find something like:
00940 
00941          N@OP
00942 
00943          Where `N' can be [+,-][4,8].  This is not mandatory, so
00944          we check it here.  If we don't find it, go to the next
00945          state.  */
00946       if ((*cur == '-' && cur[1] && cur[2] != '@')
00947           && cur[1] != '@')
00948         arg.bitness = STAP_ARG_BITNESS_UNDEFINED;
00949       else
00950         {
00951           if (*cur == '-')
00952             {
00953               /* Discard the `-'.  */
00954               ++cur;
00955               got_minus = 1;
00956             }
00957 
00958           if (*cur == '4')
00959             b = (got_minus ? STAP_ARG_BITNESS_32BIT_SIGNED
00960                  : STAP_ARG_BITNESS_32BIT_UNSIGNED);
00961           else if (*cur == '8')
00962             b = (got_minus ? STAP_ARG_BITNESS_64BIT_SIGNED
00963                  : STAP_ARG_BITNESS_64BIT_UNSIGNED);
00964           else
00965             {
00966               /* We have an error, because we don't expect anything
00967                  except 4 and 8.  */
00968               complaint (&symfile_complaints,
00969                          _("unrecognized bitness `%c' for probe `%s'"),
00970                          *cur, probe->p.name);
00971               return;
00972             }
00973 
00974           arg.bitness = b;
00975           arg.atype = stap_get_expected_argument_type (gdbarch, b);
00976 
00977           /* Discard the number and the `@' sign.  */
00978           cur += 2;
00979         }
00980 
00981       expr = stap_parse_argument (&cur, arg.atype, gdbarch);
00982 
00983       if (stap_expression_debug)
00984         dump_raw_expression (expr, gdb_stdlog,
00985                              "before conversion to prefix form");
00986 
00987       prefixify_expression (expr);
00988 
00989       if (stap_expression_debug)
00990         dump_prefix_expression (expr, gdb_stdlog);
00991 
00992       arg.aexpr = expr;
00993 
00994       /* Start it over again.  */
00995       cur = skip_spaces_const (cur);
00996 
00997       VEC_safe_push (stap_probe_arg_s, probe->args_u.vec, &arg);
00998     }
00999 }
01000 
01001 /* Given PROBE, returns the number of arguments present in that probe's
01002    argument string.  */
01003 
01004 static unsigned
01005 stap_get_probe_argument_count (struct probe *probe_generic)
01006 {
01007   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01008 
01009   gdb_assert (probe_generic->pops == &stap_probe_ops);
01010 
01011   if (!probe->args_parsed)
01012     {
01013       if (probe_generic->pops->can_evaluate_probe_arguments (probe_generic))
01014         stap_parse_probe_arguments (probe);
01015       else
01016         {
01017           static int have_warned_stap_incomplete = 0;
01018 
01019           if (!have_warned_stap_incomplete)
01020             {
01021               warning (_(
01022 "The SystemTap SDT probe support is not fully implemented on this target;\n"
01023 "you will not be able to inspect the arguments of the probes.\n"
01024 "Please report a bug against GDB requesting a port to this target."));
01025               have_warned_stap_incomplete = 1;
01026             }
01027 
01028           /* Marking the arguments as "already parsed".  */
01029           probe->args_u.vec = NULL;
01030           probe->args_parsed = 1;
01031         }
01032     }
01033 
01034   gdb_assert (probe->args_parsed);
01035   return VEC_length (stap_probe_arg_s, probe->args_u.vec);
01036 }
01037 
01038 /* Return 1 if OP is a valid operator inside a probe argument, or zero
01039    otherwise.  */
01040 
01041 static int
01042 stap_is_operator (const char *op)
01043 {
01044   int ret = 1;
01045 
01046   switch (*op)
01047     {
01048     case '*':
01049     case '/':
01050     case '%':
01051     case '^':
01052     case '!':
01053     case '+':
01054     case '-':
01055     case '<':
01056     case '>':
01057     case '|':
01058     case '&':
01059       break;
01060 
01061     case '=':
01062       if (op[1] != '=')
01063         ret = 0;
01064       break;
01065 
01066     default:
01067       /* We didn't find any operator.  */
01068       ret = 0;
01069     }
01070 
01071   return ret;
01072 }
01073 
01074 static struct stap_probe_arg *
01075 stap_get_arg (struct stap_probe *probe, unsigned n)
01076 {
01077   if (!probe->args_parsed)
01078     stap_parse_probe_arguments (probe);
01079 
01080   return VEC_index (stap_probe_arg_s, probe->args_u.vec, n);
01081 }
01082 
01083 /* Implement the `can_evaluate_probe_arguments' method of probe_ops.  */
01084 
01085 static int
01086 stap_can_evaluate_probe_arguments (struct probe *probe_generic)
01087 {
01088   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
01089   struct gdbarch *gdbarch = get_objfile_arch (stap_probe->p.objfile);
01090 
01091   /* For SystemTap probes, we have to guarantee that the method
01092      stap_is_single_operand is defined on gdbarch.  If it is not, then it
01093      means that argument evaluation is not implemented on this target.  */
01094   return gdbarch_stap_is_single_operand_p (gdbarch);
01095 }
01096 
01097 /* Evaluate the probe's argument N (indexed from 0), returning a value
01098    corresponding to it.  Assertion is thrown if N does not exist.  */
01099 
01100 static struct value *
01101 stap_evaluate_probe_argument (struct probe *probe_generic, unsigned n)
01102 {
01103   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
01104   struct stap_probe_arg *arg;
01105   int pos = 0;
01106 
01107   gdb_assert (probe_generic->pops == &stap_probe_ops);
01108 
01109   arg = stap_get_arg (stap_probe, n);
01110   return evaluate_subexp_standard (arg->atype, arg->aexpr, &pos, EVAL_NORMAL);
01111 }
01112 
01113 /* Compile the probe's argument N (indexed from 0) to agent expression.
01114    Assertion is thrown if N does not exist.  */
01115 
01116 static void
01117 stap_compile_to_ax (struct probe *probe_generic, struct agent_expr *expr,
01118                     struct axs_value *value, unsigned n)
01119 {
01120   struct stap_probe *stap_probe = (struct stap_probe *) probe_generic;
01121   struct stap_probe_arg *arg;
01122   union exp_element *pc;
01123 
01124   gdb_assert (probe_generic->pops == &stap_probe_ops);
01125 
01126   arg = stap_get_arg (stap_probe, n);
01127 
01128   pc = arg->aexpr->elts;
01129   gen_expr (arg->aexpr, &pc, expr, value);
01130 
01131   require_rvalue (expr, value);
01132   value->type = arg->atype;
01133 }
01134 
01135 /* Destroy (free) the data related to PROBE.  PROBE memory itself is not feed
01136    as it is allocated from OBJFILE_OBSTACK.  */
01137 
01138 static void
01139 stap_probe_destroy (struct probe *probe_generic)
01140 {
01141   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01142 
01143   gdb_assert (probe_generic->pops == &stap_probe_ops);
01144 
01145   if (probe->args_parsed)
01146     {
01147       struct stap_probe_arg *arg;
01148       int ix;
01149 
01150       for (ix = 0; VEC_iterate (stap_probe_arg_s, probe->args_u.vec, ix, arg);
01151            ++ix)
01152         xfree (arg->aexpr);
01153       VEC_free (stap_probe_arg_s, probe->args_u.vec);
01154     }
01155 }
01156 
01157 
01158 
01159 /* This is called to compute the value of one of the $_probe_arg*
01160    convenience variables.  */
01161 
01162 static struct value *
01163 compute_probe_arg (struct gdbarch *arch, struct internalvar *ivar,
01164                    void *data)
01165 {
01166   struct frame_info *frame = get_selected_frame (_("No frame selected"));
01167   CORE_ADDR pc = get_frame_pc (frame);
01168   int sel = (int) (uintptr_t) data;
01169   struct probe *pc_probe;
01170   const struct sym_probe_fns *pc_probe_fns;
01171   unsigned n_args;
01172 
01173   /* SEL == -1 means "_probe_argc".  */
01174   gdb_assert (sel >= -1);
01175 
01176   pc_probe = find_probe_by_pc (pc);
01177   if (pc_probe == NULL)
01178     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
01179 
01180   gdb_assert (pc_probe->objfile != NULL);
01181   gdb_assert (pc_probe->objfile->sf != NULL);
01182   gdb_assert (pc_probe->objfile->sf->sym_probe_fns != NULL);
01183 
01184   pc_probe_fns = pc_probe->objfile->sf->sym_probe_fns;
01185 
01186   n_args = pc_probe_fns->sym_get_probe_argument_count (pc_probe);
01187   if (sel == -1)
01188     return value_from_longest (builtin_type (arch)->builtin_int, n_args);
01189 
01190   if (sel >= n_args)
01191     error (_("Invalid probe argument %d -- probe has %u arguments available"),
01192            sel, n_args);
01193 
01194   return pc_probe_fns->sym_evaluate_probe_argument (pc_probe, sel);
01195 }
01196 
01197 /* This is called to compile one of the $_probe_arg* convenience
01198    variables into an agent expression.  */
01199 
01200 static void
01201 compile_probe_arg (struct internalvar *ivar, struct agent_expr *expr,
01202                    struct axs_value *value, void *data)
01203 {
01204   CORE_ADDR pc = expr->scope;
01205   int sel = (int) (uintptr_t) data;
01206   struct probe *pc_probe;
01207   const struct sym_probe_fns *pc_probe_fns;
01208   int n_args;
01209 
01210   /* SEL == -1 means "_probe_argc".  */
01211   gdb_assert (sel >= -1);
01212 
01213   pc_probe = find_probe_by_pc (pc);
01214   if (pc_probe == NULL)
01215     error (_("No SystemTap probe at PC %s"), core_addr_to_string (pc));
01216 
01217   gdb_assert (pc_probe->objfile != NULL);
01218   gdb_assert (pc_probe->objfile->sf != NULL);
01219   gdb_assert (pc_probe->objfile->sf->sym_probe_fns != NULL);
01220 
01221   pc_probe_fns = pc_probe->objfile->sf->sym_probe_fns;
01222 
01223   n_args = pc_probe_fns->sym_get_probe_argument_count (pc_probe);
01224 
01225   if (sel == -1)
01226     {
01227       value->kind = axs_rvalue;
01228       value->type = builtin_type (expr->gdbarch)->builtin_int;
01229       ax_const_l (expr, n_args);
01230       return;
01231     }
01232 
01233   gdb_assert (sel >= 0);
01234   if (sel >= n_args)
01235     error (_("Invalid probe argument %d -- probe has %d arguments available"),
01236            sel, n_args);
01237 
01238   pc_probe_fns->sym_compile_to_ax (pc_probe, expr, value, sel);
01239 }
01240 
01241 
01242 
01243 /* Set or clear a SystemTap semaphore.  ADDRESS is the semaphore's
01244    address.  SET is zero if the semaphore should be cleared, or one
01245    if it should be set.  This is a helper function for `stap_semaphore_down'
01246    and `stap_semaphore_up'.  */
01247 
01248 static void
01249 stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
01250 {
01251   gdb_byte bytes[sizeof (LONGEST)];
01252   /* The ABI specifies "unsigned short".  */
01253   struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
01254   ULONGEST value;
01255 
01256   if (address == 0)
01257     return;
01258 
01259   /* Swallow errors.  */
01260   if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
01261     {
01262       warning (_("Could not read the value of a SystemTap semaphore."));
01263       return;
01264     }
01265 
01266   value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
01267                                     gdbarch_byte_order (gdbarch));
01268   /* Note that we explicitly don't worry about overflow or
01269      underflow.  */
01270   if (set)
01271     ++value;
01272   else
01273     --value;
01274 
01275   store_unsigned_integer (bytes, TYPE_LENGTH (type),
01276                           gdbarch_byte_order (gdbarch), value);
01277 
01278   if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
01279     warning (_("Could not write the value of a SystemTap semaphore."));
01280 }
01281 
01282 /* Set a SystemTap semaphore.  SEM is the semaphore's address.  Semaphores
01283    act as reference counters, so calls to this function must be paired with
01284    calls to `stap_semaphore_down'.
01285 
01286    This function and `stap_semaphore_down' race with another tool changing
01287    the probes, but that is too rare to care.  */
01288 
01289 static void
01290 stap_set_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
01291 {
01292   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01293 
01294   gdb_assert (probe_generic->pops == &stap_probe_ops);
01295 
01296   stap_modify_semaphore (probe->sem_addr, 1, gdbarch);
01297 }
01298 
01299 /* Clear a SystemTap semaphore.  SEM is the semaphore's address.  */
01300 
01301 static void
01302 stap_clear_semaphore (struct probe *probe_generic, struct gdbarch *gdbarch)
01303 {
01304   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01305 
01306   gdb_assert (probe_generic->pops == &stap_probe_ops);
01307 
01308   stap_modify_semaphore (probe->sem_addr, 0, gdbarch);
01309 }
01310 
01311 /* Implementation of `$_probe_arg*' set of variables.  */
01312 
01313 static const struct internalvar_funcs probe_funcs =
01314 {
01315   compute_probe_arg,
01316   compile_probe_arg,
01317   NULL
01318 };
01319 
01320 /* Helper function that parses the information contained in a
01321    SystemTap's probe.  Basically, the information consists in:
01322 
01323    - Probe's PC address;
01324    - Link-time section address of `.stapsdt.base' section;
01325    - Link-time address of the semaphore variable, or ZERO if the
01326      probe doesn't have an associated semaphore;
01327    - Probe's provider name;
01328    - Probe's name;
01329    - Probe's argument format
01330    
01331    This function returns 1 if the handling was successful, and zero
01332    otherwise.  */
01333 
01334 static void
01335 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
01336                    VEC (probe_p) **probesp, CORE_ADDR base)
01337 {
01338   bfd *abfd = objfile->obfd;
01339   int size = bfd_get_arch_size (abfd) / 8;
01340   struct gdbarch *gdbarch = get_objfile_arch (objfile);
01341   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
01342   CORE_ADDR base_ref;
01343   const char *probe_args = NULL;
01344   struct stap_probe *ret;
01345 
01346   ret = obstack_alloc (&objfile->objfile_obstack, sizeof (*ret));
01347   ret->p.pops = &stap_probe_ops;
01348   ret->p.objfile = objfile;
01349 
01350   /* Provider and the name of the probe.  */
01351   ret->p.provider = (char *) &el->data[3 * size];
01352   ret->p.name = memchr (ret->p.provider, '\0',
01353                         (char *) el->data + el->size - ret->p.provider);
01354   /* Making sure there is a name.  */
01355   if (!ret->p.name)
01356     {
01357       complaint (&symfile_complaints, _("corrupt probe name when "
01358                                         "reading `%s'"),
01359                  objfile_name (objfile));
01360 
01361       /* There is no way to use a probe without a name or a provider, so
01362          returning zero here makes sense.  */
01363       return;
01364     }
01365   else
01366     ++ret->p.name;
01367 
01368   /* Retrieving the probe's address.  */
01369   ret->p.address = extract_typed_address (&el->data[0], ptr_type);
01370 
01371   /* Link-time sh_addr of `.stapsdt.base' section.  */
01372   base_ref = extract_typed_address (&el->data[size], ptr_type);
01373 
01374   /* Semaphore address.  */
01375   ret->sem_addr = extract_typed_address (&el->data[2 * size], ptr_type);
01376 
01377   ret->p.address += (ANOFFSET (objfile->section_offsets,
01378                                SECT_OFF_TEXT (objfile))
01379                      + base - base_ref);
01380   if (ret->sem_addr)
01381     ret->sem_addr += (ANOFFSET (objfile->section_offsets,
01382                                 SECT_OFF_DATA (objfile))
01383                       + base - base_ref);
01384 
01385   /* Arguments.  We can only extract the argument format if there is a valid
01386      name for this probe.  */
01387   probe_args = memchr (ret->p.name, '\0',
01388                        (char *) el->data + el->size - ret->p.name);
01389 
01390   if (probe_args != NULL)
01391     ++probe_args;
01392 
01393   if (probe_args == NULL || (memchr (probe_args, '\0',
01394                                      (char *) el->data + el->size - ret->p.name)
01395                              != el->data + el->size - 1))
01396     {
01397       complaint (&symfile_complaints, _("corrupt probe argument when "
01398                                         "reading `%s'"),
01399                  objfile_name (objfile));
01400       /* If the argument string is NULL, it means some problem happened with
01401          it.  So we return 0.  */
01402       return;
01403     }
01404 
01405   ret->args_parsed = 0;
01406   ret->args_u.text = (void *) probe_args;
01407 
01408   /* Successfully created probe.  */
01409   VEC_safe_push (probe_p, *probesp, (struct probe *) ret);
01410 }
01411 
01412 /* Helper function which tries to find the base address of the SystemTap
01413    base section named STAP_BASE_SECTION_NAME.  */
01414 
01415 static void
01416 get_stap_base_address_1 (bfd *abfd, asection *sect, void *obj)
01417 {
01418   asection **ret = obj;
01419 
01420   if ((sect->flags & (SEC_DATA | SEC_ALLOC | SEC_HAS_CONTENTS))
01421       && sect->name && !strcmp (sect->name, STAP_BASE_SECTION_NAME))
01422     *ret = sect;
01423 }
01424 
01425 /* Helper function which iterates over every section in the BFD file,
01426    trying to find the base address of the SystemTap base section.
01427    Returns 1 if found (setting BASE to the proper value), zero otherwise.  */
01428 
01429 static int
01430 get_stap_base_address (bfd *obfd, bfd_vma *base)
01431 {
01432   asection *ret = NULL;
01433 
01434   bfd_map_over_sections (obfd, get_stap_base_address_1, (void *) &ret);
01435 
01436   if (!ret)
01437     {
01438       complaint (&symfile_complaints, _("could not obtain base address for "
01439                                         "SystemTap section on objfile `%s'."),
01440                  obfd->filename);
01441       return 0;
01442     }
01443 
01444   if (base)
01445     *base = ret->vma;
01446 
01447   return 1;
01448 }
01449 
01450 /* Helper function for `elf_get_probes', which gathers information about all
01451    SystemTap probes from OBJFILE.  */
01452 
01453 static void
01454 stap_get_probes (VEC (probe_p) **probesp, struct objfile *objfile)
01455 {
01456   /* If we are here, then this is the first time we are parsing the
01457      SystemTap probe's information.  We basically have to count how many
01458      probes the objfile has, and then fill in the necessary information
01459      for each one.  */
01460   bfd *obfd = objfile->obfd;
01461   bfd_vma base;
01462   struct sdt_note *iter;
01463   unsigned save_probesp_len = VEC_length (probe_p, *probesp);
01464 
01465   if (objfile->separate_debug_objfile_backlink != NULL)
01466     {
01467       /* This is a .debug file, not the objfile itself.  */
01468       return;
01469     }
01470 
01471   if (!elf_tdata (obfd)->sdt_note_head)
01472     {
01473       /* There isn't any probe here.  */
01474       return;
01475     }
01476 
01477   if (!get_stap_base_address (obfd, &base))
01478     {
01479       /* There was an error finding the base address for the section.
01480          Just return NULL.  */
01481       return;
01482     }
01483 
01484   /* Parsing each probe's information.  */
01485   for (iter = elf_tdata (obfd)->sdt_note_head; iter; iter = iter->next)
01486     {
01487       /* We first have to handle all the information about the
01488          probe which is present in the section.  */
01489       handle_stap_probe (objfile, iter, probesp, base);
01490     }
01491 
01492   if (save_probesp_len == VEC_length (probe_p, *probesp))
01493     {
01494       /* If we are here, it means we have failed to parse every known
01495          probe.  */
01496       complaint (&symfile_complaints, _("could not parse SystemTap probe(s) "
01497                                         "from inferior"));
01498       return;
01499     }
01500 }
01501 
01502 static void
01503 stap_relocate (struct probe *probe_generic, CORE_ADDR delta)
01504 {
01505   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01506 
01507   gdb_assert (probe_generic->pops == &stap_probe_ops);
01508 
01509   probe->p.address += delta;
01510   if (probe->sem_addr)
01511     probe->sem_addr += delta;
01512 }
01513 
01514 static int
01515 stap_probe_is_linespec (const char **linespecp)
01516 {
01517   static const char *const keywords[] = { "-pstap", "-probe-stap", NULL };
01518 
01519   return probe_is_linespec_by_keyword (linespecp, keywords);
01520 }
01521 
01522 static void
01523 stap_gen_info_probes_table_header (VEC (info_probe_column_s) **heads)
01524 {
01525   info_probe_column_s stap_probe_column;
01526 
01527   stap_probe_column.field_name = "semaphore";
01528   stap_probe_column.print_name = _("Semaphore");
01529 
01530   VEC_safe_push (info_probe_column_s, *heads, &stap_probe_column);
01531 }
01532 
01533 static void
01534 stap_gen_info_probes_table_values (struct probe *probe_generic,
01535                                    VEC (const_char_ptr) **ret)
01536 {
01537   struct stap_probe *probe = (struct stap_probe *) probe_generic;
01538   struct gdbarch *gdbarch;
01539   const char *val = NULL;
01540 
01541   gdb_assert (probe_generic->pops == &stap_probe_ops);
01542 
01543   gdbarch = get_objfile_arch (probe->p.objfile);
01544 
01545   if (probe->sem_addr)
01546     val = print_core_address (gdbarch, probe->sem_addr);
01547 
01548   VEC_safe_push (const_char_ptr, *ret, val);
01549 }
01550 
01551 /* SystemTap probe_ops.  */
01552 
01553 static const struct probe_ops stap_probe_ops =
01554 {
01555   stap_probe_is_linespec,
01556   stap_get_probes,
01557   stap_relocate,
01558   stap_get_probe_argument_count,
01559   stap_can_evaluate_probe_arguments,
01560   stap_evaluate_probe_argument,
01561   stap_compile_to_ax,
01562   stap_set_semaphore,
01563   stap_clear_semaphore,
01564   stap_probe_destroy,
01565   stap_gen_info_probes_table_header,
01566   stap_gen_info_probes_table_values,
01567 };
01568 
01569 /* Implementation of the `info probes stap' command.  */
01570 
01571 static void
01572 info_probes_stap_command (char *arg, int from_tty)
01573 {
01574   info_probes_for_ops (arg, from_tty, &stap_probe_ops);
01575 }
01576 
01577 void _initialize_stap_probe (void);
01578 
01579 void
01580 _initialize_stap_probe (void)
01581 {
01582   VEC_safe_push (probe_ops_cp, all_probe_ops, &stap_probe_ops);
01583 
01584   add_setshow_zuinteger_cmd ("stap-expression", class_maintenance,
01585                              &stap_expression_debug,
01586                              _("Set SystemTap expression debugging."),
01587                              _("Show SystemTap expression debugging."),
01588                              _("When non-zero, the internal representation "
01589                                "of SystemTap expressions will be printed."),
01590                              NULL,
01591                              show_stapexpressiondebug,
01592                              &setdebuglist, &showdebuglist);
01593 
01594   create_internalvar_type_lazy ("_probe_argc", &probe_funcs,
01595                                 (void *) (uintptr_t) -1);
01596   create_internalvar_type_lazy ("_probe_arg0", &probe_funcs,
01597                                 (void *) (uintptr_t) 0);
01598   create_internalvar_type_lazy ("_probe_arg1", &probe_funcs,
01599                                 (void *) (uintptr_t) 1);
01600   create_internalvar_type_lazy ("_probe_arg2", &probe_funcs,
01601                                 (void *) (uintptr_t) 2);
01602   create_internalvar_type_lazy ("_probe_arg3", &probe_funcs,
01603                                 (void *) (uintptr_t) 3);
01604   create_internalvar_type_lazy ("_probe_arg4", &probe_funcs,
01605                                 (void *) (uintptr_t) 4);
01606   create_internalvar_type_lazy ("_probe_arg5", &probe_funcs,
01607                                 (void *) (uintptr_t) 5);
01608   create_internalvar_type_lazy ("_probe_arg6", &probe_funcs,
01609                                 (void *) (uintptr_t) 6);
01610   create_internalvar_type_lazy ("_probe_arg7", &probe_funcs,
01611                                 (void *) (uintptr_t) 7);
01612   create_internalvar_type_lazy ("_probe_arg8", &probe_funcs,
01613                                 (void *) (uintptr_t) 8);
01614   create_internalvar_type_lazy ("_probe_arg9", &probe_funcs,
01615                                 (void *) (uintptr_t) 9);
01616   create_internalvar_type_lazy ("_probe_arg10", &probe_funcs,
01617                                 (void *) (uintptr_t) 10);
01618   create_internalvar_type_lazy ("_probe_arg11", &probe_funcs,
01619                                 (void *) (uintptr_t) 11);
01620 
01621   add_cmd ("stap", class_info, info_probes_stap_command,
01622            _("\
01623 Show information about SystemTap static probes.\n\
01624 Usage: info probes stap [PROVIDER [NAME [OBJECT]]]\n\
01625 Each argument is a regular expression, used to select probes.\n\
01626 PROVIDER matches probe provider names.\n\
01627 NAME matches the probe names.\n\
01628 OBJECT matches the executable or shared library name."),
01629            info_probes_cmdlist_get ());
01630 
01631 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines