[gdb/testsuite] Add target board cc-with-gnu-debuglink.exp
[deliverable/binutils-gdb.git] / gdb / procfs.c
index 31f33fe9a2b40fd247fa7b23198cb1f780042aef..23c0aa22a7a60727cfb3973b064cb76b11c565c8 100644 (file)
@@ -1,6 +1,6 @@
 /* Machine independent support for Solaris /proc (process file system) for GDB.
 
 /* Machine independent support for Solaris /proc (process file system) for GDB.
 
-   Copyright (C) 1999-2020 Free Software Foundation, Inc.
+   Copyright (C) 1999-2021 Free Software Foundation, Inc.
 
    Written by Michael Snyder at Cygnus Solutions.
    Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
 
    Written by Michael Snyder at Cygnus Solutions.
    Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
@@ -136,7 +136,7 @@ public:
   int find_memory_regions (find_memory_region_ftype func, void *data)
     override;
 
   int find_memory_regions (find_memory_region_ftype func, void *data)
     override;
 
-  char *make_corefile_notes (bfd *, int *) override;
+  gdb::unique_xmalloc_ptr<char> make_corefile_notes (bfd *, int *) override;
 
   bool info_proc (const char *, enum info_proc_what) override;
 
 
   bool info_proc (const char *, enum info_proc_what) override;
 
@@ -1767,6 +1767,15 @@ procfs_target::attach (const char *args, int from_tty)
   if (pid == getpid ())
     error (_("Attaching GDB to itself is not a good idea..."));
 
   if (pid == getpid ())
     error (_("Attaching GDB to itself is not a good idea..."));
 
+  /* Push the target if needed, ensure it gets un-pushed it if attach fails.  */
+  inferior *inf = current_inferior ();
+  target_unpush_up unpusher;
+  if (!inf->target_is_pushed (this))
+    {
+      inf->push_target (this);
+      unpusher.reset (this);
+    }
+
   if (from_tty)
     {
       const char *exec_file = get_exec_file (0);
   if (from_tty)
     {
       const char *exec_file = get_exec_file (0);
@@ -1780,9 +1789,11 @@ procfs_target::attach (const char *args, int from_tty)
 
       fflush (stdout);
     }
 
       fflush (stdout);
     }
+
   do_attach (ptid_t (pid));
   do_attach (ptid_t (pid));
-  if (!target_is_pushed (this))
-    push_target (this);
+
+  /* Everything went fine, keep the target pushed.  */
+  unpusher.release ();
 }
 
 void
 }
 
 void
@@ -2852,8 +2863,9 @@ procfs_target::create_inferior (const char *exec_file,
       shell_file = tryname;
     }
 
       shell_file = tryname;
     }
 
-  if (!target_is_pushed (this))
-    push_target (this);
+  inferior *inf = current_inferior ();
+  if (!inf->target_is_pushed (this))
+    inf->push_target (this);
 
   pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
                       NULL, procfs_pre_trace, shell_file, NULL);
 
   pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
                       NULL, procfs_pre_trace, shell_file, NULL);
@@ -3495,10 +3507,10 @@ procfs_first_available (void)
 
 /* ===================  GCORE .NOTE "MODULE" =================== */
 
 
 /* ===================  GCORE .NOTE "MODULE" =================== */
 
-static char *
+static void
 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
-                           char *note_data, int *note_size,
-                           enum gdb_signal stop_signal)
+                           gdb::unique_xmalloc_ptr<char> &note_data,
+                           int *note_size, enum gdb_signal stop_signal)
 {
   struct regcache *regcache = get_thread_regcache (&the_procfs_target, ptid);
   gdb_gregset_t gregs;
 {
   struct regcache *regcache = get_thread_regcache (&the_procfs_target, ptid);
   gdb_gregset_t gregs;
@@ -3515,25 +3527,31 @@ procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
   target_fetch_registers (regcache, -1);
 
   fill_gregset (regcache, &gregs, -1);
   target_fetch_registers (regcache, -1);
 
   fill_gregset (regcache, &gregs, -1);
-  note_data = (char *) elfcore_write_lwpstatus (obfd,
-                                               note_data,
-                                               note_size,
-                                               merged_pid,
-                                               stop_signal,
-                                               &gregs);
+  note_data.reset (elfcore_write_lwpstatus (obfd,
+                                           note_data.release (),
+                                           note_size,
+                                           merged_pid,
+                                           stop_signal,
+                                           &gregs));
   fill_fpregset (regcache, &fpregs, -1);
   fill_fpregset (regcache, &fpregs, -1);
-  note_data = (char *) elfcore_write_prfpreg (obfd,
-                                             note_data,
-                                             note_size,
-                                             &fpregs,
-                                             sizeof (fpregs));
-
-  return note_data;
+  note_data.reset (elfcore_write_prfpreg (obfd,
+                                         note_data.release (),
+                                         note_size,
+                                         &fpregs,
+                                         sizeof (fpregs)));
 }
 
 }
 
-struct procfs_corefile_thread_data {
+struct procfs_corefile_thread_data
+{
+  procfs_corefile_thread_data (bfd *obfd,
+                              gdb::unique_xmalloc_ptr<char> &note_data,
+                              int *note_size, gdb_signal stop_signal)
+    : obfd (obfd), note_data (note_data), note_size (note_size),
+      stop_signal (stop_signal)
+  {}
+
   bfd *obfd;
   bfd *obfd;
-  char *note_data;
+  gdb::unique_xmalloc_ptr<char> &note_data;
   int *note_size;
   enum gdb_signal stop_signal;
 };
   int *note_size;
   enum gdb_signal stop_signal;
 };
@@ -3548,10 +3566,10 @@ procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
     {
       ptid_t ptid = ptid_t (pi->pid, thread->tid, 0);
 
     {
       ptid_t ptid = ptid_t (pi->pid, thread->tid, 0);
 
-      args->note_data = procfs_do_thread_registers (args->obfd, ptid,
-                                                   args->note_data,
-                                                   args->note_size,
-                                                   args->stop_signal);
+      procfs_do_thread_registers (args->obfd, ptid,
+                                 args->note_data,
+                                 args->note_size,
+                                 args->stop_signal);
     }
   return 0;
 }
     }
   return 0;
 }
@@ -3578,16 +3596,15 @@ find_stop_signal (void)
     return GDB_SIGNAL_0;
 }
 
     return GDB_SIGNAL_0;
 }
 
-char *
+gdb::unique_xmalloc_ptr<char>
 procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
 {
   gdb_gregset_t gregs;
   char fname[16] = {'\0'};
   char psargs[80] = {'\0'};
   procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
 procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
 {
   gdb_gregset_t gregs;
   char fname[16] = {'\0'};
   char psargs[80] = {'\0'};
   procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
-  char *note_data = NULL;
+  gdb::unique_xmalloc_ptr<char> note_data;
   const char *inf_args;
   const char *inf_args;
-  struct procfs_corefile_thread_data thread_args;
   enum gdb_signal stop_signal;
 
   if (get_exec_file (0))
   enum gdb_signal stop_signal;
 
   if (get_exec_file (0))
@@ -3609,33 +3626,31 @@ procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
        }
     }
 
        }
     }
 
-  note_data = (char *) elfcore_write_prpsinfo (obfd,
-                                              note_data,
-                                              note_size,
-                                              fname,
-                                              psargs);
+  note_data.reset (elfcore_write_prpsinfo (obfd,
+                                          note_data.release (),
+                                          note_size,
+                                          fname,
+                                          psargs));
 
   stop_signal = find_stop_signal ();
 
   fill_gregset (get_current_regcache (), &gregs, -1);
 
   stop_signal = find_stop_signal ();
 
   fill_gregset (get_current_regcache (), &gregs, -1);
-  note_data = elfcore_write_pstatus (obfd, note_data, note_size,
-                                    inferior_ptid.pid (),
-                                    stop_signal, &gregs);
-
-  thread_args.obfd = obfd;
-  thread_args.note_data = note_data;
-  thread_args.note_size = note_size;
-  thread_args.stop_signal = stop_signal;
+  note_data.reset (elfcore_write_pstatus (obfd, note_data.release (), note_size,
+                                         inferior_ptid.pid (),
+                                         stop_signal, &gregs));
+
+  procfs_corefile_thread_data thread_args (obfd, note_data, note_size,
+                                          stop_signal);
   proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
                             &thread_args);
   proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
                             &thread_args);
-  note_data = thread_args.note_data;
 
   gdb::optional<gdb::byte_vector> auxv =
 
   gdb::optional<gdb::byte_vector> auxv =
-    target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV, NULL);
+    target_read_alloc (current_inferior ()->top_target (),
+                      TARGET_OBJECT_AUXV, NULL);
   if (auxv && !auxv->empty ())
   if (auxv && !auxv->empty ())
-    note_data = elfcore_write_note (obfd, note_data, note_size,
-                                   "CORE", NT_AUXV, auxv->data (),
-                                   auxv->size ());
+    note_data.reset (elfcore_write_note (obfd, note_data.release (), note_size,
+                                        "CORE", NT_AUXV, auxv->data (),
+                                        auxv->size ()));
 
   return note_data;
 }
 
   return note_data;
 }
This page took 0.028245 seconds and 4 git commands to generate.