[2/2] GDBserver: Pass process_info pointer to target_kill

Message ID 20180712222541.16229-3-palves@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves July 12, 2018, 10:25 p.m. UTC
  We start from a process_info pointer, pass down process->pid, and
then the target_kill implementations need to find the process from the
pid again.  Pass the process_info pointer down directly instead.

gdb/gdbserver/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* linux-low.c (linux_kill): Change parameter to process_info
	pointer instead of pid.  Adjust.
	* lynx-low.c (lynx_kill): Likewise.
	* nto-low.c (nto_kill): Likewise.
	* spu-low.c (spu_kill): Likewise.
	* win32-low.c (win32_kill): Likewise.
	* server.c (handle_v_kill, kill_inferior_callback)
	(detach_or_kill_for_exit): Adjust.
	* target.c (kill_inferior): Change parameter to process_info
	pointer instead of pid.  Adjust.
	* target.h (struct target_ops) <kill>: Change parameter to
	process_info pointer instead of pid.  Adjust all implementations
	and callers.
	(kill_inferior): Likewise.
---
 gdb/gdbserver/linux-low.c | 11 +++--------
 gdb/gdbserver/lynx-low.c  |  9 ++-------
 gdb/gdbserver/nto-low.c   |  4 +++-
 gdb/gdbserver/server.c    | 12 ++++++------
 gdb/gdbserver/spu-low.c   |  6 ++----
 gdb/gdbserver/target.c    |  6 +++---
 gdb/gdbserver/target.h    |  6 +++---
 gdb/gdbserver/win32-low.c | 11 +++--------
 8 files changed, 25 insertions(+), 40 deletions(-)
  

Comments

Simon Marchi July 13, 2018, 3:06 a.m. UTC | #1
On 2018-07-12 06:25 PM, Pedro Alves wrote:
> @@ -3481,10 +3484,7 @@ gdbserver_show_disableable (FILE *stream)
>  static void
>  kill_inferior_callback (process_info *process)
>  {
> -  int pid = process->pid;
> -
> -  kill_inferior (pid);
> -  discard_queued_stop_replies (ptid_t (pid));
> +  discard_queued_stop_replies (ptid_t (process->pid));
>  }

Hmm this doesn't call kill_inferior anymore, I guess that's not intended?

Otherwise, LGTM.

Simon
  
Pedro Alves July 13, 2018, 10:02 a.m. UTC | #2
On 07/13/2018 04:06 AM, Simon Marchi wrote:
> On 2018-07-12 06:25 PM, Pedro Alves wrote:
>> @@ -3481,10 +3484,7 @@ gdbserver_show_disableable (FILE *stream)
>>  static void
>>  kill_inferior_callback (process_info *process)
>>  {
>> -  int pid = process->pid;
>> -
>> -  kill_inferior (pid);
>> -  discard_queued_stop_replies (ptid_t (pid));
>> +  discard_queued_stop_replies (ptid_t (process->pid));
>>  }
> 
> Hmm this doesn't call kill_inferior anymore, I guess that's not intended?

Aw, certainly not intended.  Thanks for noticing.

> Otherwise, LGTM.

I've restored the call and pushed.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index dfa7fbaa49..984464fcc2 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -1385,14 +1385,9 @@  kill_one_lwp_callback (thread_info *thread, int pid)
 }
 
 static int
-linux_kill (int pid)
+linux_kill (process_info *process)
 {
-  struct process_info *process;
-  struct lwp_info *lwp;
-
-  process = find_process_pid (pid);
-  if (process == NULL)
-    return -1;
+  int pid = process->pid;
 
   /* If we're killing a running inferior, make sure it is stopped
      first, as PTRACE_KILL will not work otherwise.  */
@@ -1405,7 +1400,7 @@  linux_kill (int pid)
 
   /* See the comment in linux_kill_one_lwp.  We did not kill the first
      thread in the list, so do so now.  */
-  lwp = find_lwp_pid (ptid_t (pid));
+  lwp_info *lwp = find_lwp_pid (ptid_t (pid));
 
   if (lwp == NULL)
     {
diff --git a/gdb/gdbserver/lynx-low.c b/gdb/gdbserver/lynx-low.c
index 902a70386b..6c5933bc47 100644
--- a/gdb/gdbserver/lynx-low.c
+++ b/gdb/gdbserver/lynx-low.c
@@ -522,15 +522,10 @@  lynx_wait (ptid_t ptid, struct target_waitstatus *status, int options)
 /* Implement the kill target_ops method.  */
 
 static int
-lynx_kill (int pid)
+lynx_kill (process_info *process)
 {
-  ptid_t ptid = lynx_ptid_t (pid, 0);
+  ptid_t ptid = lynx_ptid_t (process->pid, 0);
   struct target_waitstatus status;
-  struct process_info *process;
-
-  process = find_process_pid (pid);
-  if (process == NULL)
-    return -1;
 
   lynx_ptrace (PTRACE_KILL, ptid, 0, 0, 0);
   lynx_wait (ptid, &status, 0);
diff --git a/gdb/gdbserver/nto-low.c b/gdb/gdbserver/nto-low.c
index 46e96449a1..098678e70b 100644
--- a/gdb/gdbserver/nto-low.c
+++ b/gdb/gdbserver/nto-low.c
@@ -397,8 +397,10 @@  nto_attach (unsigned long pid)
 /* Send signal to process PID.  */
 
 static int
-nto_kill (int pid)
+nto_kill (process_info *proc)
 {
+  int pid = proc->pid;
+
   TRACE ("%s %d\n", __func__, pid);
   kill (pid, SIGKILL);
   do_detach ();
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index b35015b7bf..540f643607 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -3080,7 +3080,10 @@  handle_v_kill (char *own_buf)
     pid = strtol (p, NULL, 16);
   else
     pid = signal_pid;
-  if (pid != 0 && kill_inferior (pid) == 0)
+
+  process_info *proc = find_process_pid (pid);
+
+  if (proc != nullptr && kill_inferior (proc) == 0)
     {
       cs.last_status.kind = TARGET_WAITKIND_SIGNALLED;
       cs.last_status.value.sig = GDB_SIGNAL_KILL;
@@ -3481,10 +3484,7 @@  gdbserver_show_disableable (FILE *stream)
 static void
 kill_inferior_callback (process_info *process)
 {
-  int pid = process->pid;
-
-  kill_inferior (pid);
-  discard_queued_stop_replies (ptid_t (pid));
+  discard_queued_stop_replies (ptid_t (process->pid));
 }
 
 /* Call this when exiting gdbserver with possible inferiors that need
@@ -3528,7 +3528,7 @@  detach_or_kill_for_exit (void)
     if (process->attached)
       detach_inferior (process);
     else
-      kill_inferior (pid);
+      kill_inferior (process);
 
     discard_queued_stop_replies (ptid_t (pid));
   });
diff --git a/gdb/gdbserver/spu-low.c b/gdb/gdbserver/spu-low.c
index c2a8734f68..83a31a203d 100644
--- a/gdb/gdbserver/spu-low.c
+++ b/gdb/gdbserver/spu-low.c
@@ -326,12 +326,10 @@  spu_attach (unsigned long  pid)
 
 /* Kill the inferior process.  */
 static int
-spu_kill (int pid)
+spu_kill (process_info *process)
 {
   int status, ret;
-  struct process_info *process = find_process_pid (pid);
-  if (process == NULL)
-    return -1;
+  int pid = process->pid;
 
   ptrace (PTRACE_KILL, pid, 0, 0);
 
diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c
index 00379bfdc0..886e6cfb5c 100644
--- a/gdb/gdbserver/target.c
+++ b/gdb/gdbserver/target.c
@@ -332,11 +332,11 @@  target_pid_to_str (ptid_t ptid)
 }
 
 int
-kill_inferior (int pid)
+kill_inferior (process_info *proc)
 {
-  gdb_agent_about_to_close (pid);
+  gdb_agent_about_to_close (proc->pid);
 
-  return (*the_target->kill) (pid);
+  return (*the_target->kill) (proc);
 }
 
 /* Target can do hardware single step.  */
diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h
index 7f59485580..fce54e05ad 100644
--- a/gdb/gdbserver/target.h
+++ b/gdb/gdbserver/target.h
@@ -90,9 +90,9 @@  struct target_ops
 
   int (*attach) (unsigned long pid);
 
-  /* Kill inferior PID.  Return -1 on failure, and 0 on success.  */
+  /* Kill process PROC.  Return -1 on failure, and 0 on success.  */
 
-  int (*kill) (int pid);
+  int (*kill) (process_info *proc);
 
   /* Detach from process PROC.  Return -1 on failure, and 0 on
      success.  */
@@ -497,7 +497,7 @@  void set_target_ops (struct target_ops *);
 #define myattach(pid) \
   (*the_target->attach) (pid)
 
-int kill_inferior (int);
+int kill_inferior (process_info *proc);
 
 #define target_supports_fork_events() \
   (the_target->supports_fork_events ? \
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 17729a83cc..765391dd47 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -805,15 +805,11 @@  win32_clear_inferiors (void)
   clear_inferiors ();
 }
 
-/* Kill all inferiors.  */
+/* Implementation of target_ops::kill.  */
+
 static int
-win32_kill (int pid)
+win32_kill (process_info *process)
 {
-  struct process_info *process;
-
-  if (current_process_handle == NULL)
-    return -1;
-
   TerminateProcess (current_process_handle, 0);
   for (;;)
     {
@@ -829,7 +825,6 @@  win32_kill (int pid)
 
   win32_clear_inferiors ();
 
-  process = find_process_pid (pid);
   remove_process (process);
   return 0;
 }