gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / stap-probe.c
index 89cd780747ea0a7ba193c5761513a1c31e22daa6..73596446cce51d94e789b1ff3584b8fec807b5a1 100644 (file)
@@ -1,6 +1,6 @@
 /* SystemTap probe support for GDB.
 
-   Copyright (C) 2012-2019 Free Software Foundation, Inc.
+   Copyright (C) 2012-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -20,7 +20,6 @@
 #include "defs.h"
 #include "stap-probe.h"
 #include "probe.h"
-#include "common/vec.h"
 #include "ui-out.h"
 #include "objfiles.h"
 #include "arch-utils.h"
@@ -102,6 +101,12 @@ struct stap_probe_arg
 class stap_static_probe_ops : public static_probe_ops
 {
 public:
+  /* We need a user-provided constructor to placate some compilers.
+     See PR build/24937.  */
+  stap_static_probe_ops ()
+  {
+  }
+
   /* See probe.h.  */
   bool is_linespec (const char **linespecp) const override;
 
@@ -136,7 +141,7 @@ public:
   CORE_ADDR get_relocated_address (struct objfile *objfile) override;
 
   /* See probe.h.  */
-  unsigned get_argument_count (struct frame_info *frame) override;
+  unsigned get_argument_count (struct gdbarch *gdbarch) override;
 
   /* See probe.h.  */
   bool can_evaluate_arguments () const override;
@@ -316,8 +321,9 @@ stap_get_operator_prec (enum exp_opcode op)
     }
 }
 
-/* Given S, read the operator in it and fills the OP pointer with its code.
-   Return 1 on success, zero if the operator was not recognized.  */
+/* Given S, read the operator in it.  Return the EXP_OPCODE which
+   represents the operator detected, or throw an error if no operator
+   was found.  */
 
 static enum exp_opcode
 stap_get_opcode (const char **s)
@@ -422,7 +428,8 @@ stap_get_opcode (const char **s)
 }
 
 /* Given the bitness of the argument, represented by B, return the
-   corresponding `struct type *'.  */
+   corresponding `struct type *', or throw an error if B is
+   unknown.  */
 
 static struct type *
 stap_get_expected_argument_type (struct gdbarch *gdbarch,
@@ -685,12 +692,8 @@ stap_parse_register_operand (struct stap_parse_info *p)
   /* Variables used to extract the register name from the probe's
      argument.  */
   const char *start;
-  char *regname;
-  int len;
   const char *gdb_reg_prefix = gdbarch_stap_gdb_register_prefix (gdbarch);
-  int gdb_reg_prefix_len = gdb_reg_prefix ? strlen (gdb_reg_prefix) : 0;
   const char *gdb_reg_suffix = gdbarch_stap_gdb_register_suffix (gdbarch);
-  int gdb_reg_suffix_len = gdb_reg_suffix ? strlen (gdb_reg_suffix) : 0;
   const char *reg_prefix;
   const char *reg_ind_prefix;
   const char *reg_suffix;
@@ -703,8 +706,7 @@ stap_parse_register_operand (struct stap_parse_info *p)
         pointer.  */
       ++p->arg;
     }
-
-  if (*p->arg == '-')
+  else if (*p->arg == '-')
     {
       got_minus = true;
       ++p->arg;
@@ -752,37 +754,55 @@ stap_parse_register_operand (struct stap_parse_info *p)
   while (isalnum (*p->arg))
     ++p->arg;
 
-  len = p->arg - start;
-
-  regname = (char *) alloca (len + gdb_reg_prefix_len + gdb_reg_suffix_len + 1);
-  regname[0] = '\0';
+  std::string regname (start, p->arg - start);
 
   /* We only add the GDB's register prefix/suffix if we are dealing with
      a numeric register.  */
-  if (gdb_reg_prefix && isdigit (*start))
+  if (isdigit (*start))
     {
-      strncpy (regname, gdb_reg_prefix, gdb_reg_prefix_len);
-      strncpy (regname + gdb_reg_prefix_len, start, len);
-
-      if (gdb_reg_suffix)
-       strncpy (regname + gdb_reg_prefix_len + len,
-                gdb_reg_suffix, gdb_reg_suffix_len);
+      if (gdb_reg_prefix != NULL)
+       regname = gdb_reg_prefix + regname;
 
-      len += gdb_reg_prefix_len + gdb_reg_suffix_len;
+      if (gdb_reg_suffix != NULL)
+       regname += gdb_reg_suffix;
     }
-  else
-    strncpy (regname, start, len);
 
-  regname[len] = '\0';
+  int regnum = user_reg_map_name_to_regnum (gdbarch, regname.c_str (),
+                                           regname.size ());
 
   /* Is this a valid register name?  */
-  if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
+  if (regnum == -1)
     error (_("Invalid register name `%s' on expression `%s'."),
-          regname, p->saved_arg);
+          regname.c_str (), p->saved_arg);
+
+  /* Check if there's any special treatment that the arch-specific
+     code would like to perform on the register name.  */
+  if (gdbarch_stap_adjust_register_p (gdbarch))
+    {
+      std::string newregname
+       = gdbarch_stap_adjust_register (gdbarch, p, regname, regnum);
+
+      if (regname != newregname)
+       {
+         /* This is just a check we perform to make sure that the
+            arch-dependent code has provided us with a valid
+            register name.  */
+         regnum = user_reg_map_name_to_regnum (gdbarch, newregname.c_str (),
+                                               newregname.size ());
+
+         if (regnum == -1)
+           internal_error (__FILE__, __LINE__,
+                           _("Invalid register name '%s' after replacing it"
+                             " (previous name was '%s')"),
+                           newregname.c_str (), regname.c_str ());
+
+         regname = newregname;
+       }
+    }
 
   write_exp_elt_opcode (&p->pstate, OP_REGISTER);
-  str.ptr = regname;
-  str.length = len;
+  str.ptr = regname.c_str ();
+  str.length = regname.size ();
   write_exp_string (&p->pstate, str);
   write_exp_elt_opcode (&p->pstate, OP_REGISTER);
 
@@ -840,15 +860,15 @@ stap_parse_single_operand (struct stap_parse_info *p)
   const char *int_prefix = NULL;
 
   /* We first try to parse this token as a "special token".  */
-  if (gdbarch_stap_parse_special_token_p (gdbarch))
-    if (gdbarch_stap_parse_special_token (gdbarch, p) != 0)
-      {
-       /* If the return value of the above function is not zero,
-          it means it successfully parsed the special token.
+  if (gdbarch_stap_parse_special_token_p (gdbarch)
+      && (gdbarch_stap_parse_special_token (gdbarch, p) != 0))
+    {
+      /* If the return value of the above function is not zero,
+        it means it successfully parsed the special token.
 
-          If it is NULL, we try to parse it using our method.  */
-       return;
-      }
+        If it is NULL, we try to parse it using our method.  */
+      return;
+    }
 
   if (*p->arg == '-' || *p->arg == '~' || *p->arg == '+')
     {
@@ -888,7 +908,7 @@ stap_parse_single_operand (struct stap_parse_info *p)
        {
          /* If we are here, it means it is a displacement.  The only
             operations allowed here are `-' and `+'.  */
-         if (c == '~')
+         if (c != '-' && c != '+')
            error (_("Invalid operator `%c' for register displacement "
                     "on expression `%s'."), c, p->saved_arg);
 
@@ -1270,8 +1290,7 @@ stap_probe::parse_arguments (struct gdbarch *gdbarch)
 static CORE_ADDR
 relocate_address (CORE_ADDR address, struct objfile *objfile)
 {
-  return address + ANOFFSET (objfile->section_offsets,
-                            SECT_OFF_DATA (objfile));
+  return address + objfile->data_section_offset ();
 }
 
 /* Implementation of the get_relocated_address method.  */
@@ -1286,10 +1305,8 @@ stap_probe::get_relocated_address (struct objfile *objfile)
    argument string.  */
 
 unsigned
-stap_probe::get_argument_count (struct frame_info *frame)
+stap_probe::get_argument_count (struct gdbarch *gdbarch)
 {
-  struct gdbarch *gdbarch = get_frame_arch (frame);
-
   if (!m_have_parsed_args)
     {
       if (this->can_evaluate_arguments ())
@@ -1413,9 +1430,6 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
   struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
   ULONGEST value;
 
-  if (address == 0)
-    return;
-
   /* Swallow errors.  */
   if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
     {
@@ -1423,8 +1437,8 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
       return;
     }
 
-  value = extract_unsigned_integer (bytes, TYPE_LENGTH (type),
-                                   gdbarch_byte_order (gdbarch));
+  enum bfd_endian byte_order = type_byte_order (type);
+  value = extract_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order);
   /* Note that we explicitly don't worry about overflow or
      underflow.  */
   if (set)
@@ -1432,8 +1446,7 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
   else
     --value;
 
-  store_unsigned_integer (bytes, TYPE_LENGTH (type),
-                         gdbarch_byte_order (gdbarch), value);
+  store_unsigned_integer (bytes, TYPE_LENGTH (type), byte_order, value);
 
   if (target_write_memory (address, bytes, TYPE_LENGTH (type)) != 0)
     warning (_("Could not write the value of a SystemTap semaphore."));
@@ -1450,6 +1463,8 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
 void
 stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 {
+  if (m_sem_addr == 0)
+    return;
   stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
 }
 
@@ -1458,6 +1473,8 @@ stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 void
 stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 {
+  if (m_sem_addr == 0)
+    return;
   stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
 }
 
@@ -1491,10 +1508,7 @@ stap_probe::gen_info_probes_table_values () const
      probe doesn't have an associated semaphore;
    - Probe's provider name;
    - Probe's name;
-   - Probe's argument format
-   
-   This function returns 1 if the handling was successful, and zero
-   otherwise.  */
+   - Probe's argument format.  */
 
 static void
 handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
@@ -1503,7 +1517,7 @@ handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
 {
   bfd *abfd = objfile->obfd;
   int size = bfd_get_arch_size (abfd) / 8;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  struct gdbarch *gdbarch = objfile->arch ();
   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
 
   /* Provider and the name of the probe.  */
@@ -1514,12 +1528,11 @@ handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
   /* Making sure there is a name.  */
   if (name == NULL)
     {
-      complaint (_("corrupt probe name when "
-                                       "reading `%s'"),
+      complaint (_("corrupt probe name when reading `%s'"),
                 objfile_name (objfile));
 
       /* There is no way to use a probe without a name or a provider, so
-        returning zero here makes sense.  */
+        returning here makes sense.  */
       return;
     }
   else
@@ -1551,11 +1564,10 @@ handle_stap_probe (struct objfile *objfile, struct sdt_note *el,
       || (memchr (probe_args, '\0', (char *) el->data + el->size - name)
          != el->data + el->size - 1))
     {
-      complaint (_("corrupt probe argument when "
-                                       "reading `%s'"),
+      complaint (_("corrupt probe argument when reading `%s'"),
                 objfile_name (objfile));
       /* If the argument string is NULL, it means some problem happened with
-        it.  So we return 0.  */
+        it.  So we return.  */
       return;
     }
 
@@ -1594,7 +1606,7 @@ get_stap_base_address (bfd *obfd, bfd_vma *base)
     {
       complaint (_("could not obtain base address for "
                                        "SystemTap section on objfile `%s'."),
-                obfd->filename);
+                bfd_get_filename (obfd));
       return 0;
     }
 
@@ -1663,8 +1675,7 @@ stap_static_probe_ops::get_probes
     {
       /* If we are here, it means we have failed to parse every known
         probe.  */
-      complaint (_("could not parse SystemTap probe(s) "
-                                       "from inferior"));
+      complaint (_("could not parse SystemTap probe(s) from inferior"));
       return;
     }
 }
@@ -1698,8 +1709,9 @@ info_probes_stap_command (const char *arg, int from_tty)
   info_probes_for_spops (arg, from_tty, &stap_static_probe_ops);
 }
 
+void _initialize_stap_probe ();
 void
-_initialize_stap_probe (void)
+_initialize_stap_probe ()
 {
   all_static_probe_ops.push_back (&stap_static_probe_ops);
 
This page took 0.028282 seconds and 4 git commands to generate.