Make target_read_alloc & al return vectors
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
index f99e2e2bf6d45406fb65d6755f0f4919e297197e..3b0af668bda3aad5590d8e1569f356e36c6d80c5 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
 
-   Copyright (C) 1990-2017 Free Software Foundation, Inc.
+   Copyright (C) 1990-2018 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -33,7 +33,7 @@
 #include "infrun.h"
 #include "regcache.h"
 #include "gdbthread.h"
-#include "observer.h"
+#include "observable.h"
 
 #include "solist.h"
 #include "solib.h"
@@ -168,28 +168,22 @@ svr4_same (struct so_list *gdb, struct so_list *inferior)
   return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
 }
 
-static lm_info_svr4 *
+static std::unique_ptr<lm_info_svr4>
 lm_info_read (CORE_ADDR lm_addr)
 {
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  gdb_byte *lm;
-  lm_info_svr4 *lm_info;
-  struct cleanup *back_to;
+  std::unique_ptr<lm_info_svr4> lm_info;
 
-  lm = (gdb_byte *) xmalloc (lmo->link_map_size);
-  back_to = make_cleanup (xfree, lm);
+  gdb::byte_vector lm (lmo->link_map_size);
 
-  if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)
-    {
-      warning (_("Error reading shared library list entry at %s"),
-              paddress (target_gdbarch (), lm_addr)),
-      lm_info = NULL;
-    }
+  if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
+    warning (_("Error reading shared library list entry at %s"),
+            paddress (target_gdbarch (), lm_addr));
   else
     {
       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
 
-      lm_info = new lm_info_svr4;
+      lm_info.reset (new lm_info_svr4);
       lm_info->lm_addr = lm_addr;
 
       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
@@ -203,8 +197,6 @@ lm_info_read (CORE_ADDR lm_addr)
                                               ptr_type);
     }
 
-  do_cleanups (back_to);
-
   return lm_info;
 }
 
@@ -351,7 +343,7 @@ struct svr4_info
   /* Table of struct probe_and_action instances, used by the
      probes-based interface to map breakpoint addresses to probes
      and their associated actions.  Lookup is performed using
-     probe_and_action->probe->address.  */
+     probe_and_action->prob->address.  */
   htab_t probes_table;
 
   /* List of objects loaded into the inferior, used by the probes-
@@ -958,8 +950,6 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
 {
   struct svr4_info *info;
   CORE_ADDR ldsomap;
-  struct so_list *newobj;
-  struct cleanup *old_chain;
   CORE_ADDR name_lm;
 
   info = get_svr4_info ();
@@ -973,36 +963,24 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
   if (!ldsomap)
     return 0;
 
-  newobj = XCNEW (struct so_list);
-  old_chain = make_cleanup (xfree, newobj);
-  lm_info_svr4 *li = lm_info_read (ldsomap);
-  newobj->lm_info = li;
-  make_cleanup (xfree, newobj->lm_info);
+  std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
   name_lm = li != NULL ? li->l_name : 0;
-  do_cleanups (old_chain);
 
   return (name_lm >= vaddr && name_lm < vaddr + size);
 }
 
-/* Implement the "open_symbol_file_object" target_so_ops method.
-
-   If no open symbol file, attempt to locate and open the main symbol
-   file.  On SVR4 systems, this is the first link map entry.  If its
-   name is here, we can open it.  Useful when attaching to a process
-   without first loading its symbol file.  */
+/* See solist.h.  */
 
 static int
-open_symbol_file_object (void *from_ttyp)
+open_symbol_file_object (int from_tty)
 {
   CORE_ADDR lm, l_name;
-  char *filename;
+  gdb::unique_xmalloc_ptr<char> filename;
   int errcode;
-  int from_tty = *(int *)from_ttyp;
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
   int l_name_size = TYPE_LENGTH (ptr_type);
-  gdb_byte *l_name_buf = (gdb_byte *) xmalloc (l_name_size);
-  struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
+  gdb::byte_vector l_name_buf (l_name_size);
   struct svr4_info *info = get_svr4_info ();
   symfile_add_flags add_flags = 0;
 
@@ -1011,55 +989,40 @@ open_symbol_file_object (void *from_ttyp)
 
   if (symfile_objfile)
     if (!query (_("Attempt to reload symbols from process? ")))
-      {
-       do_cleanups (cleanups);
-       return 0;
-      }
+      return 0;
 
   /* Always locate the debug struct, in case it has moved.  */
   info->debug_base = 0;
   if (locate_base (info) == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;        /* failed somehow...  */
-    }
+    return 0;  /* failed somehow...  */
 
   /* First link map member should be the executable.  */
   lm = solib_svr4_r_map (info);
   if (lm == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;        /* failed somehow...  */
-    }
+    return 0;  /* failed somehow...  */
 
   /* Read address of name from target memory to GDB.  */
-  read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);
+  read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
 
   /* Convert the address to host format.  */
-  l_name = extract_typed_address (l_name_buf, ptr_type);
+  l_name = extract_typed_address (l_name_buf.data (), ptr_type);
 
   if (l_name == 0)
-    {
-      do_cleanups (cleanups);
-      return 0;                /* No filename.  */
-    }
+    return 0;          /* No filename.  */
 
   /* Now fetch the filename from target memory.  */
   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
-  make_cleanup (xfree, filename);
 
   if (errcode)
     {
       warning (_("failed to read exec filename from attached file: %s"),
               safe_strerror (errcode));
-      do_cleanups (cleanups);
       return 0;
     }
 
   /* Have a pathname: read the symbol file.  */
-  symbol_file_add_main (filename, add_flags);
+  symbol_file_add_main (filename.get (), add_flags);
 
-  do_cleanups (cleanups);
   return 1;
 }
 
@@ -1150,17 +1113,18 @@ svr4_copy_library_list (struct so_list *src)
 static void
 library_list_start_library (struct gdb_xml_parser *parser,
                            const struct gdb_xml_element *element,
-                           void *user_data, VEC(gdb_xml_value_s) *attributes)
+                           void *user_data,
+                           std::vector<gdb_xml_value> &attributes)
 {
   struct svr4_library_list *list = (struct svr4_library_list *) user_data;
   const char *name
-    = (const char *) xml_find_attribute (attributes, "name")->value;
+    = (const char *) xml_find_attribute (attributes, "name")->value.get ();
   ULONGEST *lmp
-    = (ULONGEST *) xml_find_attribute (attributes, "lm")->value;
+    = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
   ULONGEST *l_addrp
-    = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value;
+    = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
   ULONGEST *l_ldp
-    = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value;
+    = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
   struct so_list *new_elem;
 
   new_elem = XCNEW (struct so_list);
@@ -1183,11 +1147,12 @@ library_list_start_library (struct gdb_xml_parser *parser,
 static void
 svr4_library_list_start_list (struct gdb_xml_parser *parser,
                              const struct gdb_xml_element *element,
-                             void *user_data, VEC(gdb_xml_value_s) *attributes)
+                             void *user_data,
+                             std::vector<gdb_xml_value> &attributes)
 {
   struct svr4_library_list *list = (struct svr4_library_list *) user_data;
   const char *version
-    = (const char *) xml_find_attribute (attributes, "version")->value;
+    = (const char *) xml_find_attribute (attributes, "version")->value.get ();
   struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
 
   if (strcmp (version, "1.0") != 0)
@@ -1196,7 +1161,7 @@ svr4_library_list_start_list (struct gdb_xml_parser *parser,
                   version);
 
   if (main_lm)
-    list->main_lm = *(ULONGEST *) main_lm->value;
+    list->main_lm = *(ULONGEST *) main_lm->value.get ();
 }
 
 /* The allowed elements and attributes for an XML library list.
@@ -1275,24 +1240,16 @@ static int
 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
                                     const char *annex)
 {
-  char *svr4_library_document;
-  int result;
-  struct cleanup *back_to;
-
   gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
 
   /* Fetch the list of shared libraries.  */
-  svr4_library_document = target_read_stralloc (&current_target,
-                                               TARGET_OBJECT_LIBRARIES_SVR4,
-                                               annex);
-  if (svr4_library_document == NULL)
+  gdb::optional<gdb::char_vector> svr4_library_document
+    = target_read_stralloc (&current_target, TARGET_OBJECT_LIBRARIES_SVR4,
+                           annex);
+  if (!svr4_library_document)
     return 0;
 
-  back_to = make_cleanup (xfree, svr4_library_document);
-  result = svr4_parse_libraries (svr4_library_document, list);
-  do_cleanups (back_to);
-
-  return result;
+  return svr4_parse_libraries (svr4_library_document->data (), list);
 }
 
 #else
@@ -1351,11 +1308,11 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
   for (; lm != 0; prev_lm = lm, lm = next_lm)
     {
       int errcode;
-      char *buffer;
+      gdb::unique_xmalloc_ptr<char> buffer;
 
       so_list_up newobj (XCNEW (struct so_list));
 
-      lm_info_svr4 *li = lm_info_read (lm);
+      lm_info_svr4 *li = lm_info_read (lm).release ();
       newobj->lm_info = li;
       if (li == NULL)
        return 0;
@@ -1399,10 +1356,9 @@ svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
          continue;
        }
 
-      strncpy (newobj->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
+      strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
       newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
       strcpy (newobj->so_original_name, newobj->so_name);
-      xfree (buffer);
 
       /* If this entry has no name, or its name matches the name
         for the main executable, don't include it in the list.  */
@@ -1678,7 +1634,7 @@ exec_entry_point (struct bfd *abfd, struct target_ops *targ)
 struct probe_and_action
 {
   /* The probe.  */
-  struct probe *probe;
+  probe *prob;
 
   /* The relocated address of the probe.  */
   CORE_ADDR address;
@@ -1713,7 +1669,7 @@ equal_probe_and_action (const void *p1, const void *p2)
    probes table.  */
 
 static void
-register_solib_event_probe (struct probe *probe, CORE_ADDR address,
+register_solib_event_probe (probe *prob, CORE_ADDR address,
                            enum probe_action action)
 {
   struct svr4_info *info = get_svr4_info ();
@@ -1726,13 +1682,13 @@ register_solib_event_probe (struct probe *probe, CORE_ADDR address,
                                            equal_probe_and_action,
                                            xfree, xcalloc, xfree);
 
-  lookup.probe = probe;
+  lookup.prob = prob;
   lookup.address = address;
   slot = htab_find_slot (info->probes_table, &lookup, INSERT);
   gdb_assert (*slot == HTAB_EMPTY_ENTRY);
 
   pa = XCNEW (struct probe_and_action);
-  pa->probe = probe;
+  pa->prob = prob;
   pa->address = address;
   pa->action = action;
 
@@ -1781,7 +1737,7 @@ solib_event_probe_action (struct probe_and_action *pa)
        arg2: struct link_map *new (optional, for incremental updates)  */
   TRY
     {
-      probe_argc = get_probe_argument_count (pa->probe, frame);
+      probe_argc = pa->prob->get_argument_count (frame);
     }
   CATCH (ex, RETURN_MASK_ERROR)
     {
@@ -1790,11 +1746,11 @@ solib_event_probe_action (struct probe_and_action *pa)
     }
   END_CATCH
 
-  /* If get_probe_argument_count throws an exception, probe_argc will
-     be set to zero.  However, if pa->probe does not have arguments,
-     then get_probe_argument_count will succeed but probe_argc will
-     also be zero.  Both cases happen because of different things, but
-     they are treated equally here: action will be set to
+  /* If get_argument_count throws an exception, probe_argc will be set
+     to zero.  However, if pa->prob does not have arguments, then
+     get_argument_count will succeed but probe_argc will also be zero.
+     Both cases happen because of different things, but they are
+     treated equally here: action will be set to
      PROBES_INTERFACE_FAILED.  */
   if (probe_argc == 2)
     action = FULL_RELOAD;
@@ -1936,7 +1892,7 @@ svr4_handle_solib_event (void)
       return;
     }
 
-  /* evaluate_probe_argument looks up symbols in the dynamic linker
+  /* evaluate_argument looks up symbols in the dynamic linker
      using find_pc_section.  find_pc_section is accelerated by a cache
      called the section map.  The section map is invalidated every
      time a shared library is loaded or unloaded, and if the inferior
@@ -1945,14 +1901,14 @@ svr4_handle_solib_event (void)
      We called find_pc_section in svr4_create_solib_event_breakpoints,
      so we can guarantee that the dynamic linker's sections are in the
      section map.  We can therefore inhibit section map updates across
-     these calls to evaluate_probe_argument and save a lot of time.  */
+     these calls to evaluate_argument and save a lot of time.  */
   inhibit_section_map_updates (current_program_space);
   usm_chain = make_cleanup (resume_section_map_updates_cleanup,
                            current_program_space);
 
   TRY
     {
-      val = evaluate_probe_argument (pa->probe, 1, frame);
+      val = pa->prob->evaluate_argument (1, frame);
     }
   CATCH (ex, RETURN_MASK_ERROR)
     {
@@ -1993,7 +1949,7 @@ svr4_handle_solib_event (void)
     {
       TRY
        {
-         val = evaluate_probe_argument (pa->probe, 2, frame);
+         val = pa->prob->evaluate_argument (2, frame);
        }
       CATCH (ex, RETURN_MASK_ERROR)
        {
@@ -2089,25 +2045,19 @@ svr4_update_solib_event_breakpoints (void)
 
 static void
 svr4_create_probe_breakpoints (struct gdbarch *gdbarch,
-                              VEC (probe_p) **probes,
+                              const std::vector<probe *> *probes,
                               struct objfile *objfile)
 {
-  int i;
-
-  for (i = 0; i < NUM_PROBES; i++)
+  for (int i = 0; i < NUM_PROBES; i++)
     {
       enum probe_action action = probe_info[i].action;
-      struct probe *probe;
-      int ix;
 
-      for (ix = 0;
-          VEC_iterate (probe_p, probes[i], ix, probe);
-          ++ix)
+      for (probe *p : probes[i])
        {
-         CORE_ADDR address = get_probe_address (probe, objfile);
+         CORE_ADDR address = p->get_relocated_address (objfile);
 
          create_solib_event_breakpoint (gdbarch, address);
-         register_solib_event_probe (probe, address, action);
+         register_solib_event_probe (p, address, action);
        }
     }
 
@@ -2139,16 +2089,14 @@ svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
 
       for (with_prefix = 0; with_prefix <= 1; with_prefix++)
        {
-         VEC (probe_p) *probes[NUM_PROBES];
+         std::vector<probe *> probes[NUM_PROBES];
          int all_probes_found = 1;
          int checked_can_use_probe_arguments = 0;
-         int i;
 
-         memset (probes, 0, sizeof (probes));
-         for (i = 0; i < NUM_PROBES; i++)
+         for (int i = 0; i < NUM_PROBES; i++)
            {
              const char *name = probe_info[i].name;
-             struct probe *p;
+             probe *p;
              char buf[32];
 
              /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4
@@ -2172,7 +2120,7 @@ svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
              if (strcmp (name, "rtld_map_failed") == 0)
                continue;
 
-             if (VEC_empty (probe_p, probes[i]))
+             if (probes[i].empty ())
                {
                  all_probes_found = 0;
                  break;
@@ -2181,8 +2129,8 @@ svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
              /* Ensure probe arguments can be evaluated.  */
              if (!checked_can_use_probe_arguments)
                {
-                 p = VEC_index (probe_p, probes[i], 0);
-                 if (!can_evaluate_probe_arguments (p))
+                 p = probes[i][0];
+                 if (!p->can_evaluate_arguments ())
                    {
                      all_probes_found = 0;
                      break;
@@ -2194,9 +2142,6 @@ svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
          if (all_probes_found)
            svr4_create_probe_breakpoints (gdbarch, probes, os->objfile);
 
-         for (i = 0; i < NUM_PROBES; i++)
-           VEC_free (probe_p, probes[i]);
-
          if (all_probes_found)
            return;
        }
@@ -3293,8 +3238,6 @@ elf_lookup_lib_symbol (struct objfile *objfile,
   return lookup_global_symbol_from_objfile (objfile, name, domain);
 }
 
-extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */
-
 void
 _initialize_svr4_solib (void)
 {
This page took 0.039076 seconds and 4 git commands to generate.