1157571Sjmg/* Low level interface to ptrace, for the remote server for GDB.
2157571Sjmg   Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3157571Sjmg   Free Software Foundation, Inc.
4157571Sjmg
5157571Sjmg   This file is part of GDB.
6157571Sjmg
7157571Sjmg   This program is free software; you can redistribute it and/or modify
8157571Sjmg   it under the terms of the GNU General Public License as published by
9157571Sjmg   the Free Software Foundation; either version 2 of the License, or
10157571Sjmg   (at your option) any later version.
11157571Sjmg
12157571Sjmg   This program is distributed in the hope that it will be useful,
13157571Sjmg   but WITHOUT ANY WARRANTY; without even the implied warranty of
14157571Sjmg   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15157571Sjmg   GNU General Public License for more details.
16157571Sjmg
17157571Sjmg   You should have received a copy of the GNU General Public License
18157571Sjmg   along with this program; if not, write to the Free Software
19157571Sjmg   Foundation, Inc., 59 Temple Place - Suite 330,
20157571Sjmg   Boston, MA 02111-1307, USA.  */
21157571Sjmg
22157574Sjmg#include <sys/cdefs.h>
23157574Sjmg__FBSDID("$FreeBSD$");
24157574Sjmg
25157571Sjmg#include "server.h"
26157574Sjmg#include "fbsd-low.h"
27157571Sjmg
28157571Sjmg#include <sys/wait.h>
29157571Sjmg#include <sys/param.h>
30157571Sjmg#include <sys/ptrace.h>
31157571Sjmg#include <sys/user.h>
32157571Sjmg#include <sys/ioctl.h>
33157574Sjmg#include <dirent.h>
34157574Sjmg#include <errno.h>
35157571Sjmg#include <fcntl.h>
36157574Sjmg#include <signal.h>
37157574Sjmg#include <stdio.h>
38157574Sjmg#include <stdlib.h>
39157571Sjmg#include <string.h>
40157571Sjmg#include <unistd.h>
41157571Sjmg
42157571Sjmg/* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
43157571Sjmg   however.  This requires changing the ID in place when we go from !using_threads
44157571Sjmg   to using_threads, immediately.
45157571Sjmg
46157571Sjmg   ``all_processes'' is keyed by the process ID - which on Linux is (presently)
47157571Sjmg   the same as the LWP ID.  */
48157571Sjmg
49157571Sjmgstruct inferior_list all_processes;
50157571Sjmg
51157571Sjmg/* FIXME this is a bit of a hack, and could be removed.  */
52157571Sjmgint stopping_threads;
53157571Sjmg
54157571Sjmg/* FIXME make into a target method?  */
55157571Sjmgint using_threads;
56157571Sjmg
57157574Sjmgstatic void fbsd_resume_one_process (struct inferior_list_entry *entry,
58157571Sjmg				      int step, int signal);
59157574Sjmgstatic void fbsd_resume (struct thread_resume *resume_info);
60157571Sjmgstatic void stop_all_processes (void);
61157574Sjmgstatic int fbsd_wait_for_event (struct thread_info *child);
62157571Sjmg
63157571Sjmgstruct pending_signals
64157571Sjmg{
65157571Sjmg  int signal;
66157571Sjmg  struct pending_signals *prev;
67157571Sjmg};
68157571Sjmg
69157574Sjmg#define PTRACE_ARG3_TYPE caddr_t
70157574Sjmg#define PTRACE_XFER_TYPE int
71157571Sjmg
72157571Sjmgint debug_threads = 0;
73157571Sjmg
74157571Sjmg#define pid_of(proc) ((proc)->head.id)
75157571Sjmg
76157571Sjmg/* FIXME: Delete eventually.  */
77157571Sjmg#define inferior_pid (pid_of (get_thread_process (current_inferior)))
78157571Sjmg
79157571Sjmg/* This function should only be called if the process got a SIGTRAP.
80157571Sjmg   The SIGTRAP could mean several things.
81157571Sjmg
82157571Sjmg   On i386, where decr_pc_after_break is non-zero:
83157574Sjmg   If we were single-stepping this process using PT_STEP,
84157571Sjmg   we will get only the one SIGTRAP (even if the instruction we
85157571Sjmg   stepped over was a breakpoint).  The value of $eip will be the
86157571Sjmg   next instruction.
87157571Sjmg   If we continue the process using PTRACE_CONT, we will get a
88157571Sjmg   SIGTRAP when we hit a breakpoint.  The value of $eip will be
89157571Sjmg   the instruction after the breakpoint (i.e. needs to be
90157571Sjmg   decremented).  If we report the SIGTRAP to GDB, we must also
91157571Sjmg   report the undecremented PC.  If we cancel the SIGTRAP, we
92157571Sjmg   must resume at the decremented PC.
93157571Sjmg
94157571Sjmg   (Presumably, not yet tested) On a non-decr_pc_after_break machine
95157571Sjmg   with hardware or kernel single-step:
96157571Sjmg   If we single-step over a breakpoint instruction, our PC will
97157571Sjmg   point at the following instruction.  If we continue and hit a
98157571Sjmg   breakpoint instruction, our PC will point at the breakpoint
99157571Sjmg   instruction.  */
100157571Sjmg
101157571Sjmgstatic CORE_ADDR
102157571Sjmgget_stop_pc (void)
103157571Sjmg{
104157571Sjmg  CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
105157571Sjmg
106157571Sjmg  if (get_thread_process (current_inferior)->stepping)
107157571Sjmg    return stop_pc;
108157571Sjmg  else
109157571Sjmg    return stop_pc - the_low_target.decr_pc_after_break;
110157571Sjmg}
111157571Sjmg
112157571Sjmgstatic void *
113157571Sjmgadd_process (int pid)
114157571Sjmg{
115157571Sjmg  struct process_info *process;
116157571Sjmg
117157571Sjmg  process = (struct process_info *) malloc (sizeof (*process));
118157571Sjmg  memset (process, 0, sizeof (*process));
119157571Sjmg
120157571Sjmg  process->head.id = pid;
121157571Sjmg
122157571Sjmg  /* Default to tid == lwpid == pid.  */
123157571Sjmg  process->tid = pid;
124157571Sjmg  process->lwpid = pid;
125157571Sjmg
126157571Sjmg  add_inferior_to_list (&all_processes, &process->head);
127157571Sjmg
128157571Sjmg  return process;
129157571Sjmg}
130157571Sjmg
131157571Sjmg/* Start an inferior process and returns its pid.
132157571Sjmg   ALLARGS is a vector of program-name and args. */
133157571Sjmg
134157571Sjmgstatic int
135157574Sjmgfbsd_create_inferior (char *program, char **allargs)
136157571Sjmg{
137157571Sjmg  void *new_process;
138157571Sjmg  int pid;
139157571Sjmg
140157574Sjmg  pid = vfork ();
141157571Sjmg  if (pid < 0)
142157574Sjmg    perror_with_name ("vfork");
143157571Sjmg
144157571Sjmg  if (pid == 0)
145157571Sjmg    {
146157574Sjmg      ptrace (PT_TRACE_ME, 0, 0, 0);
147157571Sjmg
148157571Sjmg      setpgid (0, 0);
149157571Sjmg
150157571Sjmg      execv (program, allargs);
151157571Sjmg
152157571Sjmg      fprintf (stderr, "Cannot exec %s: %s.\n", program,
153157571Sjmg	       strerror (errno));
154157571Sjmg      fflush (stderr);
155157571Sjmg      _exit (0177);
156157571Sjmg    }
157157571Sjmg
158157571Sjmg  new_process = add_process (pid);
159157571Sjmg  add_thread (pid, new_process);
160157571Sjmg
161157571Sjmg  return pid;
162157571Sjmg}
163157571Sjmg
164157571Sjmg/* Attach to an inferior process.  */
165157571Sjmg
166157571Sjmgvoid
167157574Sjmgfbsd_attach_lwp (int pid, int tid)
168157571Sjmg{
169157571Sjmg  struct process_info *new_process;
170157571Sjmg
171157574Sjmg  if (ptrace (PT_ATTACH, pid, 0, 0) != 0)
172157571Sjmg    {
173157571Sjmg      fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
174157571Sjmg	       strerror (errno), errno);
175157571Sjmg      fflush (stderr);
176157571Sjmg
177157571Sjmg      /* If we fail to attach to an LWP, just return.  */
178157571Sjmg      if (!using_threads)
179157571Sjmg	_exit (0177);
180157571Sjmg      return;
181157571Sjmg    }
182157571Sjmg
183157571Sjmg  new_process = (struct process_info *) add_process (pid);
184157571Sjmg  add_thread (tid, new_process);
185157571Sjmg
186157571Sjmg  /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
187157571Sjmg     brings it to a halt.  We should ignore that SIGSTOP and resume the process
188157571Sjmg     (unless this is the first process, in which case the flag will be cleared
189157574Sjmg     in fbsd_attach).
190157571Sjmg
191157571Sjmg     On the other hand, if we are currently trying to stop all threads, we
192157571Sjmg     should treat the new thread as if we had sent it a SIGSTOP.  This works
193157571Sjmg     because we are guaranteed that add_process added us to the end of the
194157571Sjmg     list, and so the new thread has not yet reached wait_for_sigstop (but
195157571Sjmg     will).  */
196157571Sjmg  if (! stopping_threads)
197157571Sjmg    new_process->stop_expected = 1;
198157571Sjmg}
199157571Sjmg
200157571Sjmgint
201157574Sjmgfbsd_attach (int pid)
202157571Sjmg{
203157571Sjmg  struct process_info *process;
204157571Sjmg
205157574Sjmg  fbsd_attach_lwp (pid, pid);
206157571Sjmg
207157571Sjmg  /* Don't ignore the initial SIGSTOP if we just attached to this process.  */
208157571Sjmg  process = (struct process_info *) find_inferior_id (&all_processes, pid);
209157571Sjmg  process->stop_expected = 0;
210157571Sjmg
211157571Sjmg  return 0;
212157571Sjmg}
213157571Sjmg
214157571Sjmg/* Kill the inferior process.  Make us have no inferior.  */
215157571Sjmg
216157571Sjmgstatic void
217157574Sjmgfbsd_kill_one_process (struct inferior_list_entry *entry)
218157571Sjmg{
219157571Sjmg  struct thread_info *thread = (struct thread_info *) entry;
220157571Sjmg  struct process_info *process = get_thread_process (thread);
221157571Sjmg  int wstat;
222157571Sjmg
223157571Sjmg  do
224157571Sjmg    {
225157574Sjmg      ptrace (PT_KILL, pid_of (process), 0, 0);
226157571Sjmg
227157571Sjmg      /* Make sure it died.  The loop is most likely unnecessary.  */
228157574Sjmg      wstat = fbsd_wait_for_event (thread);
229157571Sjmg    } while (WIFSTOPPED (wstat));
230157571Sjmg}
231157571Sjmg
232157571Sjmgstatic void
233157574Sjmgfbsd_kill (void)
234157571Sjmg{
235157574Sjmg  for_each_inferior (&all_threads, fbsd_kill_one_process);
236157571Sjmg}
237157571Sjmg
238157571Sjmgstatic void
239157574Sjmgfbsd_detach_one_process (struct inferior_list_entry *entry)
240157571Sjmg{
241157571Sjmg  struct thread_info *thread = (struct thread_info *) entry;
242157571Sjmg  struct process_info *process = get_thread_process (thread);
243157571Sjmg
244157574Sjmg  ptrace (PT_DETACH, pid_of (process), 0, 0);
245157571Sjmg}
246157571Sjmg
247157571Sjmgstatic void
248157574Sjmgfbsd_detach (void)
249157571Sjmg{
250157574Sjmg  for_each_inferior (&all_threads, fbsd_detach_one_process);
251157571Sjmg}
252157571Sjmg
253157571Sjmg/* Return nonzero if the given thread is still alive.  */
254157571Sjmgstatic int
255157574Sjmgfbsd_thread_alive (int tid)
256157571Sjmg{
257157571Sjmg  if (find_inferior_id (&all_threads, tid) != NULL)
258157571Sjmg    return 1;
259157571Sjmg  else
260157571Sjmg    return 0;
261157571Sjmg}
262157571Sjmg
263157571Sjmg/* Return nonzero if this process stopped at a breakpoint which
264157571Sjmg   no longer appears to be inserted.  Also adjust the PC
265157571Sjmg   appropriately to resume where the breakpoint used to be.  */
266157571Sjmgstatic int
267157571Sjmgcheck_removed_breakpoint (struct process_info *event_child)
268157571Sjmg{
269157571Sjmg  CORE_ADDR stop_pc;
270157571Sjmg  struct thread_info *saved_inferior;
271157571Sjmg
272157571Sjmg  if (event_child->pending_is_breakpoint == 0)
273157571Sjmg    return 0;
274157571Sjmg
275157571Sjmg  if (debug_threads)
276157571Sjmg    fprintf (stderr, "Checking for breakpoint.\n");
277157571Sjmg
278157571Sjmg  saved_inferior = current_inferior;
279157571Sjmg  current_inferior = get_process_thread (event_child);
280157571Sjmg
281157571Sjmg  stop_pc = get_stop_pc ();
282157571Sjmg
283157571Sjmg  /* If the PC has changed since we stopped, then we shouldn't do
284157571Sjmg     anything.  This happens if, for instance, GDB handled the
285157571Sjmg     decr_pc_after_break subtraction itself.  */
286157571Sjmg  if (stop_pc != event_child->pending_stop_pc)
287157571Sjmg    {
288157571Sjmg      if (debug_threads)
289157571Sjmg	fprintf (stderr, "Ignoring, PC was changed.\n");
290157571Sjmg
291157571Sjmg      event_child->pending_is_breakpoint = 0;
292157571Sjmg      current_inferior = saved_inferior;
293157571Sjmg      return 0;
294157571Sjmg    }
295157571Sjmg
296157571Sjmg  /* If the breakpoint is still there, we will report hitting it.  */
297157571Sjmg  if ((*the_low_target.breakpoint_at) (stop_pc))
298157571Sjmg    {
299157571Sjmg      if (debug_threads)
300157571Sjmg	fprintf (stderr, "Ignoring, breakpoint is still present.\n");
301157571Sjmg      current_inferior = saved_inferior;
302157571Sjmg      return 0;
303157571Sjmg    }
304157571Sjmg
305157571Sjmg  if (debug_threads)
306157571Sjmg    fprintf (stderr, "Removed breakpoint.\n");
307157571Sjmg
308157571Sjmg  /* For decr_pc_after_break targets, here is where we perform the
309157571Sjmg     decrement.  We go immediately from this function to resuming,
310157571Sjmg     and can not safely call get_stop_pc () again.  */
311157571Sjmg  if (the_low_target.set_pc != NULL)
312157571Sjmg    (*the_low_target.set_pc) (stop_pc);
313157571Sjmg
314157571Sjmg  /* We consumed the pending SIGTRAP.  */
315157571Sjmg  event_child->pending_is_breakpoint = 0;
316157571Sjmg  event_child->status_pending_p = 0;
317157571Sjmg  event_child->status_pending = 0;
318157571Sjmg
319157571Sjmg  current_inferior = saved_inferior;
320157571Sjmg  return 1;
321157571Sjmg}
322157571Sjmg
323157571Sjmg/* Return 1 if this process has an interesting status pending.  This function
324157571Sjmg   may silently resume an inferior process.  */
325157571Sjmgstatic int
326157571Sjmgstatus_pending_p (struct inferior_list_entry *entry, void *dummy)
327157571Sjmg{
328157571Sjmg  struct process_info *process = (struct process_info *) entry;
329157571Sjmg
330157571Sjmg  if (process->status_pending_p)
331157571Sjmg    if (check_removed_breakpoint (process))
332157571Sjmg      {
333157571Sjmg	/* This thread was stopped at a breakpoint, and the breakpoint
334157571Sjmg	   is now gone.  We were told to continue (or step...) all threads,
335157571Sjmg	   so GDB isn't trying to single-step past this breakpoint.
336157571Sjmg	   So instead of reporting the old SIGTRAP, pretend we got to
337157571Sjmg	   the breakpoint just after it was removed instead of just
338157571Sjmg	   before; resume the process.  */
339157574Sjmg	fbsd_resume_one_process (&process->head, 0, 0);
340157571Sjmg	return 0;
341157571Sjmg      }
342157571Sjmg
343157571Sjmg  return process->status_pending_p;
344157571Sjmg}
345157571Sjmg
346157571Sjmgstatic void
347157574Sjmgfbsd_wait_for_process (struct process_info **childp, int *wstatp)
348157571Sjmg{
349157571Sjmg  int ret;
350157571Sjmg  int to_wait_for = -1;
351157571Sjmg
352157571Sjmg  if (*childp != NULL)
353157571Sjmg    to_wait_for = (*childp)->lwpid;
354157571Sjmg
355157571Sjmg  while (1)
356157571Sjmg    {
357157571Sjmg      ret = waitpid (to_wait_for, wstatp, WNOHANG);
358157571Sjmg
359157571Sjmg      if (ret == -1)
360157571Sjmg	{
361157571Sjmg	  if (errno != ECHILD)
362157571Sjmg	    perror_with_name ("waitpid");
363157571Sjmg	}
364157571Sjmg      else if (ret > 0)
365157571Sjmg	break;
366157571Sjmg
367157571Sjmg      usleep (1000);
368157571Sjmg    }
369157571Sjmg
370157571Sjmg  if (debug_threads
371157571Sjmg      && (!WIFSTOPPED (*wstatp)
372157571Sjmg	  || (WSTOPSIG (*wstatp) != 32
373157571Sjmg	      && WSTOPSIG (*wstatp) != 33)))
374157571Sjmg    fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
375157571Sjmg
376157571Sjmg  if (to_wait_for == -1)
377157571Sjmg    *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
378157571Sjmg
379157571Sjmg  (*childp)->stopped = 1;
380157571Sjmg  (*childp)->pending_is_breakpoint = 0;
381157571Sjmg
382157571Sjmg  if (debug_threads
383157571Sjmg      && WIFSTOPPED (*wstatp))
384157571Sjmg    {
385157571Sjmg      current_inferior = (struct thread_info *)
386157571Sjmg	find_inferior_id (&all_threads, (*childp)->tid);
387157571Sjmg      /* For testing only; i386_stop_pc prints out a diagnostic.  */
388157571Sjmg      if (the_low_target.get_pc != NULL)
389157571Sjmg	get_stop_pc ();
390157571Sjmg    }
391157571Sjmg}
392157571Sjmg
393157571Sjmgstatic int
394157574Sjmgfbsd_wait_for_event (struct thread_info *child)
395157571Sjmg{
396157571Sjmg  CORE_ADDR stop_pc;
397157571Sjmg  struct process_info *event_child;
398157571Sjmg  int wstat;
399157571Sjmg
400157571Sjmg  /* Check for a process with a pending status.  */
401157571Sjmg  /* It is possible that the user changed the pending task's registers since
402157571Sjmg     it stopped.  We correctly handle the change of PC if we hit a breakpoint
403157571Sjmg     (in check_removed_breakpoint); signals should be reported anyway.  */
404157571Sjmg  if (child == NULL)
405157571Sjmg    {
406157571Sjmg      event_child = (struct process_info *)
407157571Sjmg	find_inferior (&all_processes, status_pending_p, NULL);
408157571Sjmg      if (debug_threads && event_child)
409157571Sjmg	fprintf (stderr, "Got a pending child %d\n", event_child->lwpid);
410157571Sjmg    }
411157571Sjmg  else
412157571Sjmg    {
413157571Sjmg      event_child = get_thread_process (child);
414157571Sjmg      if (event_child->status_pending_p
415157571Sjmg	  && check_removed_breakpoint (event_child))
416157571Sjmg	event_child = NULL;
417157571Sjmg    }
418157571Sjmg
419157571Sjmg  if (event_child != NULL)
420157571Sjmg    {
421157571Sjmg      if (event_child->status_pending_p)
422157571Sjmg	{
423157571Sjmg	  if (debug_threads)
424157571Sjmg	    fprintf (stderr, "Got an event from pending child %d (%04x)\n",
425157571Sjmg		     event_child->lwpid, event_child->status_pending);
426157571Sjmg	  wstat = event_child->status_pending;
427157571Sjmg	  event_child->status_pending_p = 0;
428157571Sjmg	  event_child->status_pending = 0;
429157571Sjmg	  current_inferior = get_process_thread (event_child);
430157571Sjmg	  return wstat;
431157571Sjmg	}
432157571Sjmg    }
433157571Sjmg
434157571Sjmg  /* We only enter this loop if no process has a pending wait status.  Thus
435157571Sjmg     any action taken in response to a wait status inside this loop is
436157571Sjmg     responding as soon as we detect the status, not after any pending
437157571Sjmg     events.  */
438157571Sjmg  while (1)
439157571Sjmg    {
440157571Sjmg      if (child == NULL)
441157571Sjmg	event_child = NULL;
442157571Sjmg      else
443157571Sjmg	event_child = get_thread_process (child);
444157571Sjmg
445157574Sjmg      fbsd_wait_for_process (&event_child, &wstat);
446157571Sjmg
447157571Sjmg      if (event_child == NULL)
448157571Sjmg	error ("event from unknown child");
449157571Sjmg
450157571Sjmg      current_inferior = (struct thread_info *)
451157571Sjmg	find_inferior_id (&all_threads, event_child->tid);
452157571Sjmg
453157571Sjmg      if (using_threads)
454157571Sjmg	{
455157571Sjmg	  /* Check for thread exit.  */
456157571Sjmg	  if (! WIFSTOPPED (wstat))
457157571Sjmg	    {
458157571Sjmg	      if (debug_threads)
459157571Sjmg		fprintf (stderr, "Thread %d (LWP %d) exiting\n",
460157571Sjmg			 event_child->tid, event_child->head.id);
461157571Sjmg
462157571Sjmg	      /* If the last thread is exiting, just return.  */
463157571Sjmg	      if (all_threads.head == all_threads.tail)
464157571Sjmg		return wstat;
465157571Sjmg
466157571Sjmg	      dead_thread_notify (event_child->tid);
467157571Sjmg
468157571Sjmg	      remove_inferior (&all_processes, &event_child->head);
469157571Sjmg	      free (event_child);
470157571Sjmg	      remove_thread (current_inferior);
471157571Sjmg	      current_inferior = (struct thread_info *) all_threads.head;
472157571Sjmg
473157571Sjmg	      /* If we were waiting for this particular child to do something...
474157571Sjmg		 well, it did something.  */
475157571Sjmg	      if (child != NULL)
476157571Sjmg		return wstat;
477157571Sjmg
478157571Sjmg	      /* Wait for a more interesting event.  */
479157571Sjmg	      continue;
480157571Sjmg	    }
481157571Sjmg
482157571Sjmg	  if (WIFSTOPPED (wstat)
483157571Sjmg	      && WSTOPSIG (wstat) == SIGSTOP
484157571Sjmg	      && event_child->stop_expected)
485157571Sjmg	    {
486157571Sjmg	      if (debug_threads)
487157571Sjmg		fprintf (stderr, "Expected stop.\n");
488157571Sjmg	      event_child->stop_expected = 0;
489157574Sjmg	      fbsd_resume_one_process (&event_child->head,
490157571Sjmg					event_child->stepping, 0);
491157571Sjmg	      continue;
492157571Sjmg	    }
493157571Sjmg
494157571Sjmg	  /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
495157571Sjmg	     thread library?  */
496157574Sjmg	  if (WIFSTOPPED (wstat))
497157571Sjmg	    {
498157571Sjmg	      if (debug_threads)
499157571Sjmg		fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n",
500157571Sjmg			 WSTOPSIG (wstat), event_child->tid,
501157571Sjmg			 event_child->head.id);
502157574Sjmg	      fbsd_resume_one_process (&event_child->head,
503157571Sjmg					event_child->stepping,
504157571Sjmg					WSTOPSIG (wstat));
505157571Sjmg	      continue;
506157571Sjmg	    }
507157571Sjmg	}
508157571Sjmg
509157571Sjmg      /* If this event was not handled above, and is not a SIGTRAP, report
510157571Sjmg	 it.  */
511157571Sjmg      if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
512157571Sjmg	return wstat;
513157571Sjmg
514157571Sjmg      /* If this target does not support breakpoints, we simply report the
515157571Sjmg	 SIGTRAP; it's of no concern to us.  */
516157571Sjmg      if (the_low_target.get_pc == NULL)
517157571Sjmg	return wstat;
518157571Sjmg
519157571Sjmg      stop_pc = get_stop_pc ();
520157571Sjmg
521157571Sjmg      /* bp_reinsert will only be set if we were single-stepping.
522157571Sjmg	 Notice that we will resume the process after hitting
523157571Sjmg	 a gdbserver breakpoint; single-stepping to/over one
524157571Sjmg	 is not supported (yet).  */
525157571Sjmg      if (event_child->bp_reinsert != 0)
526157571Sjmg	{
527157571Sjmg	  if (debug_threads)
528157571Sjmg	    fprintf (stderr, "Reinserted breakpoint.\n");
529157571Sjmg	  reinsert_breakpoint (event_child->bp_reinsert);
530157571Sjmg	  event_child->bp_reinsert = 0;
531157571Sjmg
532157571Sjmg	  /* Clear the single-stepping flag and SIGTRAP as we resume.  */
533157574Sjmg	  fbsd_resume_one_process (&event_child->head, 0, 0);
534157571Sjmg	  continue;
535157571Sjmg	}
536157571Sjmg
537157571Sjmg      if (debug_threads)
538157571Sjmg	fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
539157571Sjmg
540157571Sjmg      if (check_breakpoints (stop_pc) != 0)
541157571Sjmg	{
542157571Sjmg	  /* We hit one of our own breakpoints.  We mark it as a pending
543157571Sjmg	     breakpoint, so that check_removed_breakpoint () will do the PC
544157571Sjmg	     adjustment for us at the appropriate time.  */
545157571Sjmg	  event_child->pending_is_breakpoint = 1;
546157571Sjmg	  event_child->pending_stop_pc = stop_pc;
547157571Sjmg
548157571Sjmg	  /* Now we need to put the breakpoint back.  We continue in the event
549157571Sjmg	     loop instead of simply replacing the breakpoint right away,
550157571Sjmg	     in order to not lose signals sent to the thread that hit the
551157571Sjmg	     breakpoint.  Unfortunately this increases the window where another
552157571Sjmg	     thread could sneak past the removed breakpoint.  For the current
553157571Sjmg	     use of server-side breakpoints (thread creation) this is
554157571Sjmg	     acceptable; but it needs to be considered before this breakpoint
555157571Sjmg	     mechanism can be used in more general ways.  For some breakpoints
556157571Sjmg	     it may be necessary to stop all other threads, but that should
557157571Sjmg	     be avoided where possible.
558157571Sjmg
559157571Sjmg	     If breakpoint_reinsert_addr is NULL, that means that we can
560157574Sjmg	     use PT_STEP on this platform.  Uninsert the breakpoint,
561157571Sjmg	     mark it for reinsertion, and single-step.
562157571Sjmg
563157571Sjmg	     Otherwise, call the target function to figure out where we need
564157571Sjmg	     our temporary breakpoint, create it, and continue executing this
565157571Sjmg	     process.  */
566157571Sjmg	  if (the_low_target.breakpoint_reinsert_addr == NULL)
567157571Sjmg	    {
568157571Sjmg	      event_child->bp_reinsert = stop_pc;
569157571Sjmg	      uninsert_breakpoint (stop_pc);
570157574Sjmg	      fbsd_resume_one_process (&event_child->head, 1, 0);
571157571Sjmg	    }
572157571Sjmg	  else
573157571Sjmg	    {
574157571Sjmg	      reinsert_breakpoint_by_bp
575157571Sjmg		(stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
576157574Sjmg	      fbsd_resume_one_process (&event_child->head, 0, 0);
577157571Sjmg	    }
578157571Sjmg
579157571Sjmg	  continue;
580157571Sjmg	}
581157571Sjmg
582157571Sjmg      /* If we were single-stepping, we definitely want to report the
583157571Sjmg	 SIGTRAP.  The single-step operation has completed, so also
584157571Sjmg         clear the stepping flag; in general this does not matter,
585157571Sjmg	 because the SIGTRAP will be reported to the client, which
586157571Sjmg	 will give us a new action for this thread, but clear it for
587157571Sjmg	 consistency anyway.  It's safe to clear the stepping flag
588157571Sjmg         because the only consumer of get_stop_pc () after this point
589157571Sjmg	 is check_removed_breakpoint, and pending_is_breakpoint is not
590157571Sjmg	 set.  It might be wiser to use a step_completed flag instead.  */
591157571Sjmg      if (event_child->stepping)
592157571Sjmg	{
593157571Sjmg	  event_child->stepping = 0;
594157571Sjmg	  return wstat;
595157571Sjmg	}
596157571Sjmg
597157571Sjmg      /* A SIGTRAP that we can't explain.  It may have been a breakpoint.
598157571Sjmg	 Check if it is a breakpoint, and if so mark the process information
599157571Sjmg	 accordingly.  This will handle both the necessary fiddling with the
600157571Sjmg	 PC on decr_pc_after_break targets and suppressing extra threads
601157571Sjmg	 hitting a breakpoint if two hit it at once and then GDB removes it
602157571Sjmg	 after the first is reported.  Arguably it would be better to report
603157571Sjmg	 multiple threads hitting breakpoints simultaneously, but the current
604157571Sjmg	 remote protocol does not allow this.  */
605157571Sjmg      if ((*the_low_target.breakpoint_at) (stop_pc))
606157571Sjmg	{
607157571Sjmg	  event_child->pending_is_breakpoint = 1;
608157571Sjmg	  event_child->pending_stop_pc = stop_pc;
609157571Sjmg	}
610157571Sjmg
611157571Sjmg      return wstat;
612157571Sjmg    }
613157571Sjmg
614157571Sjmg  /* NOTREACHED */
615157571Sjmg  return 0;
616157571Sjmg}
617157571Sjmg
618157571Sjmg/* Wait for process, returns status.  */
619157571Sjmg
620157571Sjmgstatic unsigned char
621157574Sjmgfbsd_wait (char *status)
622157571Sjmg{
623157571Sjmg  int w;
624157571Sjmg  struct thread_info *child = NULL;
625157571Sjmg
626157571Sjmgretry:
627157571Sjmg  /* If we were only supposed to resume one thread, only wait for
628157571Sjmg     that thread - if it's still alive.  If it died, however - which
629157571Sjmg     can happen if we're coming from the thread death case below -
630157571Sjmg     then we need to make sure we restart the other threads.  We could
631157571Sjmg     pick a thread at random or restart all; restarting all is less
632157571Sjmg     arbitrary.  */
633157571Sjmg  if (cont_thread > 0)
634157571Sjmg    {
635157571Sjmg      child = (struct thread_info *) find_inferior_id (&all_threads,
636157571Sjmg						       cont_thread);
637157571Sjmg
638157571Sjmg      /* No stepping, no signal - unless one is pending already, of course.  */
639157571Sjmg      if (child == NULL)
640157571Sjmg	{
641157571Sjmg	  struct thread_resume resume_info;
642157571Sjmg	  resume_info.thread = -1;
643157571Sjmg	  resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
644157574Sjmg	  fbsd_resume (&resume_info);
645157571Sjmg	}
646157571Sjmg    }
647157571Sjmg
648157571Sjmg  enable_async_io ();
649157571Sjmg  unblock_async_io ();
650157574Sjmg  w = fbsd_wait_for_event (child);
651157571Sjmg  stop_all_processes ();
652157571Sjmg  disable_async_io ();
653157571Sjmg
654157571Sjmg  /* If we are waiting for a particular child, and it exited,
655157574Sjmg     fbsd_wait_for_event will return its exit status.  Similarly if
656157571Sjmg     the last child exited.  If this is not the last child, however,
657157571Sjmg     do not report it as exited until there is a 'thread exited' response
658157571Sjmg     available in the remote protocol.  Instead, just wait for another event.
659157571Sjmg     This should be safe, because if the thread crashed we will already
660157571Sjmg     have reported the termination signal to GDB; that should stop any
661157571Sjmg     in-progress stepping operations, etc.
662157571Sjmg
663157571Sjmg     Report the exit status of the last thread to exit.  This matches
664157571Sjmg     LinuxThreads' behavior.  */
665157571Sjmg
666157571Sjmg  if (all_threads.head == all_threads.tail)
667157571Sjmg    {
668157571Sjmg      if (WIFEXITED (w))
669157571Sjmg	{
670157571Sjmg	  fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
671157571Sjmg	  *status = 'W';
672157571Sjmg	  clear_inferiors ();
673157571Sjmg	  free (all_processes.head);
674157571Sjmg	  all_processes.head = all_processes.tail = NULL;
675157571Sjmg	  return ((unsigned char) WEXITSTATUS (w));
676157571Sjmg	}
677157571Sjmg      else if (!WIFSTOPPED (w))
678157571Sjmg	{
679157571Sjmg	  fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
680157571Sjmg	  *status = 'X';
681157571Sjmg	  clear_inferiors ();
682157571Sjmg	  free (all_processes.head);
683157571Sjmg	  all_processes.head = all_processes.tail = NULL;
684157571Sjmg	  return ((unsigned char) WTERMSIG (w));
685157571Sjmg	}
686157571Sjmg    }
687157571Sjmg  else
688157571Sjmg    {
689157571Sjmg      if (!WIFSTOPPED (w))
690157571Sjmg	goto retry;
691157571Sjmg    }
692157571Sjmg
693157571Sjmg  *status = 'T';
694157571Sjmg  return ((unsigned char) WSTOPSIG (w));
695157571Sjmg}
696157571Sjmg
697157571Sjmgstatic void
698157571Sjmgsend_sigstop (struct inferior_list_entry *entry)
699157571Sjmg{
700157571Sjmg  struct process_info *process = (struct process_info *) entry;
701157571Sjmg
702157571Sjmg  if (process->stopped)
703157571Sjmg    return;
704157571Sjmg
705157571Sjmg  /* If we already have a pending stop signal for this process, don't
706157571Sjmg     send another.  */
707157571Sjmg  if (process->stop_expected)
708157571Sjmg    {
709157571Sjmg      process->stop_expected = 0;
710157571Sjmg      return;
711157571Sjmg    }
712157571Sjmg
713157571Sjmg  if (debug_threads)
714157571Sjmg    fprintf (stderr, "Sending sigstop to process %d\n", process->head.id);
715157571Sjmg
716157571Sjmg  kill (process->head.id, SIGSTOP);
717157571Sjmg  process->sigstop_sent = 1;
718157571Sjmg}
719157571Sjmg
720157571Sjmgstatic void
721157571Sjmgwait_for_sigstop (struct inferior_list_entry *entry)
722157571Sjmg{
723157571Sjmg  struct process_info *process = (struct process_info *) entry;
724157571Sjmg  struct thread_info *saved_inferior, *thread;
725157571Sjmg  int wstat, saved_tid;
726157571Sjmg
727157571Sjmg  if (process->stopped)
728157571Sjmg    return;
729157571Sjmg
730157571Sjmg  saved_inferior = current_inferior;
731157571Sjmg  saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
732157571Sjmg  thread = (struct thread_info *) find_inferior_id (&all_threads,
733157571Sjmg						    process->tid);
734157574Sjmg  wstat = fbsd_wait_for_event (thread);
735157571Sjmg
736157571Sjmg  /* If we stopped with a non-SIGSTOP signal, save it for later
737157571Sjmg     and record the pending SIGSTOP.  If the process exited, just
738157571Sjmg     return.  */
739157571Sjmg  if (WIFSTOPPED (wstat)
740157571Sjmg      && WSTOPSIG (wstat) != SIGSTOP)
741157571Sjmg    {
742157571Sjmg      if (debug_threads)
743157571Sjmg	fprintf (stderr, "Stopped with non-sigstop signal\n");
744157571Sjmg      process->status_pending_p = 1;
745157571Sjmg      process->status_pending = wstat;
746157571Sjmg      process->stop_expected = 1;
747157571Sjmg    }
748157571Sjmg
749157574Sjmg  if (fbsd_thread_alive (saved_tid))
750157571Sjmg    current_inferior = saved_inferior;
751157571Sjmg  else
752157571Sjmg    {
753157571Sjmg      if (debug_threads)
754157571Sjmg	fprintf (stderr, "Previously current thread died.\n");
755157571Sjmg
756157571Sjmg      /* Set a valid thread as current.  */
757157571Sjmg      set_desired_inferior (0);
758157571Sjmg    }
759157571Sjmg}
760157571Sjmg
761157571Sjmgstatic void
762157571Sjmgstop_all_processes (void)
763157571Sjmg{
764157571Sjmg  stopping_threads = 1;
765157571Sjmg  for_each_inferior (&all_processes, send_sigstop);
766157571Sjmg  for_each_inferior (&all_processes, wait_for_sigstop);
767157571Sjmg  stopping_threads = 0;
768157571Sjmg}
769157571Sjmg
770157571Sjmg/* Resume execution of the inferior process.
771157571Sjmg   If STEP is nonzero, single-step it.
772157571Sjmg   If SIGNAL is nonzero, give it that signal.  */
773157571Sjmg
774157571Sjmgstatic void
775157574Sjmgfbsd_resume_one_process (struct inferior_list_entry *entry,
776157571Sjmg			  int step, int signal)
777157571Sjmg{
778157571Sjmg  struct process_info *process = (struct process_info *) entry;
779157571Sjmg  struct thread_info *saved_inferior;
780157571Sjmg
781157571Sjmg  if (process->stopped == 0)
782157571Sjmg    return;
783157571Sjmg
784157571Sjmg  /* If we have pending signals or status, and a new signal, enqueue the
785157571Sjmg     signal.  Also enqueue the signal if we are waiting to reinsert a
786157571Sjmg     breakpoint; it will be picked up again below.  */
787157571Sjmg  if (signal != 0
788157571Sjmg      && (process->status_pending_p || process->pending_signals != NULL
789157571Sjmg	  || process->bp_reinsert != 0))
790157571Sjmg    {
791157571Sjmg      struct pending_signals *p_sig;
792157571Sjmg      p_sig = malloc (sizeof (*p_sig));
793157571Sjmg      p_sig->prev = process->pending_signals;
794157571Sjmg      p_sig->signal = signal;
795157571Sjmg      process->pending_signals = p_sig;
796157571Sjmg    }
797157571Sjmg
798157571Sjmg  if (process->status_pending_p && !check_removed_breakpoint (process))
799157571Sjmg    return;
800157571Sjmg
801157571Sjmg  saved_inferior = current_inferior;
802157571Sjmg  current_inferior = get_process_thread (process);
803157571Sjmg
804157571Sjmg  if (debug_threads)
805157571Sjmg    fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid,
806157571Sjmg	     step ? "step" : "continue", signal,
807157571Sjmg	     process->stop_expected ? "expected" : "not expected");
808157571Sjmg
809157571Sjmg  /* This bit needs some thinking about.  If we get a signal that
810157571Sjmg     we must report while a single-step reinsert is still pending,
811157571Sjmg     we often end up resuming the thread.  It might be better to
812157571Sjmg     (ew) allow a stack of pending events; then we could be sure that
813157571Sjmg     the reinsert happened right away and not lose any signals.
814157571Sjmg
815157571Sjmg     Making this stack would also shrink the window in which breakpoints are
816157574Sjmg     uninserted (see comment in fbsd_wait_for_process) but not enough for
817157571Sjmg     complete correctness, so it won't solve that problem.  It may be
818157571Sjmg     worthwhile just to solve this one, however.  */
819157571Sjmg  if (process->bp_reinsert != 0)
820157571Sjmg    {
821157571Sjmg      if (debug_threads)
822157571Sjmg	fprintf (stderr, "  pending reinsert at %08lx", (long)process->bp_reinsert);
823157571Sjmg      if (step == 0)
824157571Sjmg	fprintf (stderr, "BAD - reinserting but not stepping.\n");
825157571Sjmg      step = 1;
826157571Sjmg
827157571Sjmg      /* Postpone any pending signal.  It was enqueued above.  */
828157571Sjmg      signal = 0;
829157571Sjmg    }
830157571Sjmg
831157571Sjmg  check_removed_breakpoint (process);
832157571Sjmg
833157571Sjmg  if (debug_threads && the_low_target.get_pc != NULL)
834157571Sjmg    {
835157571Sjmg      fprintf (stderr, "  ");
836157571Sjmg      (long) (*the_low_target.get_pc) ();
837157571Sjmg    }
838157571Sjmg
839157571Sjmg  /* If we have pending signals, consume one unless we are trying to reinsert
840157571Sjmg     a breakpoint.  */
841157571Sjmg  if (process->pending_signals != NULL && process->bp_reinsert == 0)
842157571Sjmg    {
843157571Sjmg      struct pending_signals **p_sig;
844157571Sjmg
845157571Sjmg      p_sig = &process->pending_signals;
846157571Sjmg      while ((*p_sig)->prev != NULL)
847157571Sjmg	p_sig = &(*p_sig)->prev;
848157571Sjmg
849157571Sjmg      signal = (*p_sig)->signal;
850157571Sjmg      free (*p_sig);
851157571Sjmg      *p_sig = NULL;
852157571Sjmg    }
853157571Sjmg
854157571Sjmg  regcache_invalidate_one ((struct inferior_list_entry *)
855157571Sjmg			   get_process_thread (process));
856157571Sjmg  errno = 0;
857157571Sjmg  process->stopped = 0;
858157571Sjmg  process->stepping = step;
859157574Sjmg  ptrace (step ? PT_STEP : PT_CONTINUE, process->lwpid, (PTRACE_ARG3_TYPE) 1, signal);
860157571Sjmg
861157571Sjmg  current_inferior = saved_inferior;
862157571Sjmg  if (errno)
863157571Sjmg    perror_with_name ("ptrace");
864157571Sjmg}
865157571Sjmg
866157571Sjmgstatic struct thread_resume *resume_ptr;
867157571Sjmg
868157571Sjmg/* This function is called once per thread.  We look up the thread
869157571Sjmg   in RESUME_PTR, and mark the thread with a pointer to the appropriate
870157571Sjmg   resume request.
871157571Sjmg
872157571Sjmg   This algorithm is O(threads * resume elements), but resume elements
873157571Sjmg   is small (and will remain small at least until GDB supports thread
874157571Sjmg   suspension).  */
875157571Sjmgstatic void
876157574Sjmgfbsd_set_resume_request (struct inferior_list_entry *entry)
877157571Sjmg{
878157571Sjmg  struct process_info *process;
879157571Sjmg  struct thread_info *thread;
880157571Sjmg  int ndx;
881157571Sjmg
882157571Sjmg  thread = (struct thread_info *) entry;
883157571Sjmg  process = get_thread_process (thread);
884157571Sjmg
885157571Sjmg  ndx = 0;
886157571Sjmg  while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
887157571Sjmg    ndx++;
888157571Sjmg
889157571Sjmg  process->resume = &resume_ptr[ndx];
890157571Sjmg}
891157571Sjmg
892157571Sjmg/* This function is called once per thread.  We check the thread's resume
893157571Sjmg   request, which will tell us whether to resume, step, or leave the thread
894157571Sjmg   stopped; and what signal, if any, it should be sent.  For threads which
895157571Sjmg   we aren't explicitly told otherwise, we preserve the stepping flag; this
896157571Sjmg   is used for stepping over gdbserver-placed breakpoints.  */
897157571Sjmg
898157571Sjmgstatic void
899157574Sjmgfbsd_continue_one_thread (struct inferior_list_entry *entry)
900157571Sjmg{
901157571Sjmg  struct process_info *process;
902157571Sjmg  struct thread_info *thread;
903157571Sjmg  int step;
904157571Sjmg
905157571Sjmg  thread = (struct thread_info *) entry;
906157571Sjmg  process = get_thread_process (thread);
907157571Sjmg
908157571Sjmg  if (process->resume->leave_stopped)
909157571Sjmg    return;
910157571Sjmg
911157571Sjmg  if (process->resume->thread == -1)
912157571Sjmg    step = process->stepping || process->resume->step;
913157571Sjmg  else
914157571Sjmg    step = process->resume->step;
915157571Sjmg
916157574Sjmg  fbsd_resume_one_process (&process->head, step, process->resume->sig);
917157571Sjmg
918157571Sjmg  process->resume = NULL;
919157571Sjmg}
920157571Sjmg
921157571Sjmg/* This function is called once per thread.  We check the thread's resume
922157571Sjmg   request, which will tell us whether to resume, step, or leave the thread
923157571Sjmg   stopped; and what signal, if any, it should be sent.  We queue any needed
924157571Sjmg   signals, since we won't actually resume.  We already have a pending event
925157571Sjmg   to report, so we don't need to preserve any step requests; they should
926157571Sjmg   be re-issued if necessary.  */
927157571Sjmg
928157571Sjmgstatic void
929157574Sjmgfbsd_queue_one_thread (struct inferior_list_entry *entry)
930157571Sjmg{
931157571Sjmg  struct process_info *process;
932157571Sjmg  struct thread_info *thread;
933157571Sjmg
934157571Sjmg  thread = (struct thread_info *) entry;
935157571Sjmg  process = get_thread_process (thread);
936157571Sjmg
937157571Sjmg  if (process->resume->leave_stopped)
938157571Sjmg    return;
939157571Sjmg
940157571Sjmg  /* If we have a new signal, enqueue the signal.  */
941157571Sjmg  if (process->resume->sig != 0)
942157571Sjmg    {
943157571Sjmg      struct pending_signals *p_sig;
944157571Sjmg      p_sig = malloc (sizeof (*p_sig));
945157571Sjmg      p_sig->prev = process->pending_signals;
946157571Sjmg      p_sig->signal = process->resume->sig;
947157571Sjmg      process->pending_signals = p_sig;
948157571Sjmg    }
949157571Sjmg
950157571Sjmg  process->resume = NULL;
951157571Sjmg}
952157571Sjmg
953157571Sjmg/* Set DUMMY if this process has an interesting status pending.  */
954157571Sjmgstatic int
955157571Sjmgresume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
956157571Sjmg{
957157571Sjmg  struct process_info *process = (struct process_info *) entry;
958157571Sjmg
959157571Sjmg  /* Processes which will not be resumed are not interesting, because
960157574Sjmg     we might not wait for them next time through fbsd_wait.  */
961157571Sjmg  if (process->resume->leave_stopped)
962157571Sjmg    return 0;
963157571Sjmg
964157571Sjmg  /* If this thread has a removed breakpoint, we won't have any
965157571Sjmg     events to report later, so check now.  check_removed_breakpoint
966157571Sjmg     may clear status_pending_p.  We avoid calling check_removed_breakpoint
967157571Sjmg     for any thread that we are not otherwise going to resume - this
968157571Sjmg     lets us preserve stopped status when two threads hit a breakpoint.
969157571Sjmg     GDB removes the breakpoint to single-step a particular thread
970157571Sjmg     past it, then re-inserts it and resumes all threads.  We want
971157571Sjmg     to report the second thread without resuming it in the interim.  */
972157571Sjmg  if (process->status_pending_p)
973157571Sjmg    check_removed_breakpoint (process);
974157571Sjmg
975157571Sjmg  if (process->status_pending_p)
976157571Sjmg    * (int *) flag_p = 1;
977157571Sjmg
978157571Sjmg  return 0;
979157571Sjmg}
980157571Sjmg
981157571Sjmgstatic void
982157574Sjmgfbsd_resume (struct thread_resume *resume_info)
983157571Sjmg{
984157571Sjmg  int pending_flag;
985157571Sjmg
986157571Sjmg  /* Yes, the use of a global here is rather ugly.  */
987157571Sjmg  resume_ptr = resume_info;
988157571Sjmg
989157574Sjmg  for_each_inferior (&all_threads, fbsd_set_resume_request);
990157571Sjmg
991157571Sjmg  /* If there is a thread which would otherwise be resumed, which
992157571Sjmg     has a pending status, then don't resume any threads - we can just
993157571Sjmg     report the pending status.  Make sure to queue any signals
994157571Sjmg     that would otherwise be sent.  */
995157571Sjmg  pending_flag = 0;
996157571Sjmg  find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
997157571Sjmg
998157571Sjmg  if (debug_threads)
999157571Sjmg    {
1000157571Sjmg      if (pending_flag)
1001157571Sjmg	fprintf (stderr, "Not resuming, pending status\n");
1002157571Sjmg      else
1003157571Sjmg	fprintf (stderr, "Resuming, no pending status\n");
1004157571Sjmg    }
1005157571Sjmg
1006157571Sjmg  if (pending_flag)
1007157574Sjmg    for_each_inferior (&all_threads, fbsd_queue_one_thread);
1008157571Sjmg  else
1009157571Sjmg    {
1010157571Sjmg      block_async_io ();
1011157571Sjmg      enable_async_io ();
1012157574Sjmg      for_each_inferior (&all_threads, fbsd_continue_one_thread);
1013157571Sjmg    }
1014157571Sjmg}
1015157571Sjmg
1016157571Sjmg
1017157571Sjmgstatic int
1018157571Sjmgregsets_fetch_inferior_registers ()
1019157571Sjmg{
1020157571Sjmg  struct regset_info *regset;
1021157571Sjmg
1022157571Sjmg  regset = target_regsets;
1023157571Sjmg
1024157571Sjmg  while (regset->size >= 0)
1025157571Sjmg    {
1026157571Sjmg      void *buf;
1027157571Sjmg      int res;
1028157571Sjmg
1029157571Sjmg      if (regset->size == 0)
1030157571Sjmg	{
1031157571Sjmg	  regset ++;
1032157571Sjmg	  continue;
1033157571Sjmg	}
1034157571Sjmg
1035157571Sjmg      buf = malloc (regset->size);
1036157574Sjmg      res = ptrace (regset->get_request, inferior_pid, (PTRACE_ARG3_TYPE) buf, 0);
1037157571Sjmg      if (res < 0)
1038157571Sjmg	{
1039157574Sjmg	  char s[256];
1040157574Sjmg	  sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1041157574Sjmg		   inferior_pid);
1042157574Sjmg	  perror (s);
1043157571Sjmg	}
1044157571Sjmg      regset->store_function (buf);
1045157571Sjmg      regset ++;
1046157571Sjmg    }
1047157571Sjmg  return 0;
1048157571Sjmg}
1049157571Sjmg
1050157571Sjmgstatic int
1051157571Sjmgregsets_store_inferior_registers ()
1052157571Sjmg{
1053157571Sjmg  struct regset_info *regset;
1054157571Sjmg
1055157571Sjmg  regset = target_regsets;
1056157571Sjmg
1057157571Sjmg  while (regset->size >= 0)
1058157571Sjmg    {
1059157571Sjmg      void *buf;
1060157571Sjmg      int res;
1061157571Sjmg
1062157571Sjmg      if (regset->size == 0)
1063157571Sjmg	{
1064157571Sjmg	  regset ++;
1065157571Sjmg	  continue;
1066157571Sjmg	}
1067157571Sjmg
1068157571Sjmg      buf = malloc (regset->size);
1069157571Sjmg      regset->fill_function (buf);
1070157574Sjmg      res = ptrace (regset->set_request, inferior_pid, (PTRACE_ARG3_TYPE) buf, 0);
1071157571Sjmg      if (res < 0)
1072157571Sjmg	{
1073157574Sjmg	  perror ("Warning: ptrace(regsets_store_inferior_registers)");
1074157571Sjmg	}
1075157571Sjmg      regset ++;
1076157571Sjmg      free (buf);
1077157571Sjmg    }
1078157571Sjmg  return 0;
1079157571Sjmg}
1080157571Sjmg
1081157571Sjmgvoid
1082157574Sjmgfbsd_fetch_registers (int regno)
1083157571Sjmg{
1084157574Sjmg      regsets_fetch_inferior_registers ();
1085157571Sjmg}
1086157571Sjmg
1087157571Sjmgvoid
1088157574Sjmgfbsd_store_registers (int regno)
1089157571Sjmg{
1090157574Sjmg      regsets_store_inferior_registers ();
1091157571Sjmg}
1092157571Sjmg
1093157571Sjmg
1094157571Sjmg/* Copy LEN bytes from inferior's memory starting at MEMADDR
1095157571Sjmg   to debugger memory starting at MYADDR.  */
1096157571Sjmg
1097157571Sjmgstatic int
1098157574Sjmgfbsd_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
1099157571Sjmg{
1100157571Sjmg  register int i;
1101157571Sjmg  /* Round starting address down to longword boundary.  */
1102157571Sjmg  register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1103157571Sjmg  /* Round ending address up; get number of longwords that makes.  */
1104157571Sjmg  register int count
1105157571Sjmg    = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
1106157571Sjmg      / sizeof (PTRACE_XFER_TYPE);
1107157571Sjmg  /* Allocate buffer of that many longwords.  */
1108157571Sjmg  register PTRACE_XFER_TYPE *buffer
1109157571Sjmg    = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1110157571Sjmg
1111157571Sjmg  /* Read all the longwords */
1112157571Sjmg  for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1113157571Sjmg    {
1114157571Sjmg      errno = 0;
1115157574Sjmg      buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) (intptr_t)addr, 0);
1116157571Sjmg      if (errno)
1117157571Sjmg	return errno;
1118157571Sjmg    }
1119157571Sjmg
1120157571Sjmg  /* Copy appropriate bytes out of the buffer.  */
1121157571Sjmg  memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
1122157571Sjmg
1123157571Sjmg  return 0;
1124157571Sjmg}
1125157571Sjmg
1126157571Sjmg/* Copy LEN bytes of data from debugger memory at MYADDR
1127157571Sjmg   to inferior's memory at MEMADDR.
1128157571Sjmg   On failure (cannot write the inferior)
1129157571Sjmg   returns the value of errno.  */
1130157571Sjmg
1131157571Sjmgstatic int
1132157574Sjmgfbsd_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
1133157571Sjmg{
1134157571Sjmg  register int i;
1135157571Sjmg  /* Round starting address down to longword boundary.  */
1136157571Sjmg  register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1137157571Sjmg  /* Round ending address up; get number of longwords that makes.  */
1138157571Sjmg  register int count
1139157571Sjmg  = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1140157571Sjmg  /* Allocate buffer of that many longwords.  */
1141157571Sjmg  register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1142157571Sjmg  extern int errno;
1143157571Sjmg
1144157571Sjmg  if (debug_threads)
1145157571Sjmg    {
1146157571Sjmg      fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1147157571Sjmg    }
1148157571Sjmg
1149157571Sjmg  /* Fill start and end extra bytes of buffer with existing memory data.  */
1150157571Sjmg
1151157574Sjmg  buffer[0] = ptrace (PT_READ_D, inferior_pid,
1152157574Sjmg		      (PTRACE_ARG3_TYPE) (intptr_t)addr, 0);
1153157571Sjmg
1154157571Sjmg  if (count > 1)
1155157571Sjmg    {
1156157571Sjmg      buffer[count - 1]
1157157574Sjmg	= ptrace (PT_READ_D, inferior_pid,
1158157574Sjmg		  (PTRACE_ARG3_TYPE) (intptr_t) (addr + (count - 1)
1159157571Sjmg				      * sizeof (PTRACE_XFER_TYPE)),
1160157571Sjmg		  0);
1161157571Sjmg    }
1162157571Sjmg
1163157571Sjmg  /* Copy data to be written over corresponding part of buffer */
1164157571Sjmg
1165157571Sjmg  memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1166157571Sjmg
1167157571Sjmg  /* Write the entire buffer.  */
1168157571Sjmg
1169157571Sjmg  for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1170157571Sjmg    {
1171157571Sjmg      errno = 0;
1172157574Sjmg      ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) (intptr_t)addr, buffer[i]);
1173157571Sjmg      if (errno)
1174157571Sjmg	return errno;
1175157571Sjmg    }
1176157571Sjmg
1177157571Sjmg  return 0;
1178157571Sjmg}
1179157571Sjmg
1180157571Sjmgstatic void
1181157574Sjmgfbsd_look_up_symbols (void)
1182157571Sjmg{
1183157571Sjmg#ifdef USE_THREAD_DB
1184157571Sjmg  if (using_threads)
1185157571Sjmg    return;
1186157571Sjmg
1187157571Sjmg  using_threads = thread_db_init ();
1188157571Sjmg#endif
1189157571Sjmg}
1190157571Sjmg
1191157571Sjmgstatic void
1192157574Sjmgfbsd_send_signal (int signum)
1193157571Sjmg{
1194157571Sjmg  extern int signal_pid;
1195157571Sjmg
1196157571Sjmg  if (cont_thread > 0)
1197157571Sjmg    {
1198157571Sjmg      struct process_info *process;
1199157571Sjmg
1200157571Sjmg      process = get_thread_process (current_inferior);
1201157571Sjmg      kill (process->lwpid, signum);
1202157571Sjmg    }
1203157571Sjmg  else
1204157571Sjmg    kill (signal_pid, signum);
1205157571Sjmg}
1206157571Sjmg
1207157571Sjmg/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1208157571Sjmg   to debugger memory starting at MYADDR.  */
1209157571Sjmg
1210157571Sjmgstatic int
1211157574Sjmgfbsd_read_auxv (CORE_ADDR offset, char *myaddr, unsigned int len)
1212157571Sjmg{
1213157571Sjmg  char filename[PATH_MAX];
1214157571Sjmg  int fd, n;
1215157571Sjmg
1216157571Sjmg  snprintf (filename, sizeof filename, "/proc/%d/auxv", inferior_pid);
1217157571Sjmg
1218157571Sjmg  fd = open (filename, O_RDONLY);
1219157571Sjmg  if (fd < 0)
1220157571Sjmg    return -1;
1221157571Sjmg
1222157571Sjmg  if (offset != (CORE_ADDR) 0
1223157571Sjmg      && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
1224157571Sjmg    n = -1;
1225157571Sjmg  else
1226157571Sjmg    n = read (fd, myaddr, len);
1227157571Sjmg
1228157571Sjmg  close (fd);
1229157571Sjmg
1230157571Sjmg  return n;
1231157571Sjmg}
1232157571Sjmg
1233157571Sjmg
1234157574Sjmgstatic struct target_ops fbsd_target_ops = {
1235157574Sjmg  fbsd_create_inferior,
1236157574Sjmg  fbsd_attach,
1237157574Sjmg  fbsd_kill,
1238157574Sjmg  fbsd_detach,
1239157574Sjmg  fbsd_thread_alive,
1240157574Sjmg  fbsd_resume,
1241157574Sjmg  fbsd_wait,
1242157574Sjmg  fbsd_fetch_registers,
1243157574Sjmg  fbsd_store_registers,
1244157574Sjmg  fbsd_read_memory,
1245157574Sjmg  fbsd_write_memory,
1246157574Sjmg  fbsd_look_up_symbols,
1247157574Sjmg  fbsd_send_signal,
1248157574Sjmg  fbsd_read_auxv,
1249157571Sjmg};
1250157571Sjmg
1251157571Sjmgstatic void
1252157574Sjmgfbsd_init_signals ()
1253157571Sjmg{
1254157571Sjmg}
1255157571Sjmg
1256157571Sjmgvoid
1257157571Sjmginitialize_low (void)
1258157571Sjmg{
1259157571Sjmg  using_threads = 0;
1260157574Sjmg  set_target_ops (&fbsd_target_ops);
1261157571Sjmg  set_breakpoint_data (the_low_target.breakpoint,
1262157571Sjmg		       the_low_target.breakpoint_len);
1263157571Sjmg  init_registers ();
1264157574Sjmg  fbsd_init_signals ();
1265157571Sjmg}
1266