xref: /openbsd-src/gnu/usr.bin/binutils/gdb/gnu-nat.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1b725ae77Skettenis /* Interface GDB to the GNU Hurd.
2b725ae77Skettenis    Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3b725ae77Skettenis    Free Software Foundation, Inc.
4e93f7393Sniklas 
5e93f7393Sniklas    This file is part of GDB.
6e93f7393Sniklas 
7e93f7393Sniklas    Written by Miles Bader <miles@gnu.ai.mit.edu>
8e93f7393Sniklas 
9e93f7393Sniklas    Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
10e93f7393Sniklas 
11e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
12e93f7393Sniklas    it under the terms of the GNU General Public License as published by
13e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
14e93f7393Sniklas    (at your option) any later version.
15e93f7393Sniklas 
16e93f7393Sniklas    This program is distributed in the hope that it will be useful,
17e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
18e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19e93f7393Sniklas    GNU General Public License for more details.
20e93f7393Sniklas 
21e93f7393Sniklas    You should have received a copy of the GNU General Public License
22e93f7393Sniklas    along with this program; if not, write to the Free Software
23b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
24b725ae77Skettenis    Boston, MA 02111-1307, USA.
25e93f7393Sniklas  */
26e93f7393Sniklas 
27b725ae77Skettenis #include <ctype.h>
28e93f7393Sniklas #include <errno.h>
29e93f7393Sniklas #include <limits.h>
30b725ae77Skettenis #include <setjmp.h>
31b725ae77Skettenis #include <signal.h>
32b725ae77Skettenis #include <stdio.h>
33b725ae77Skettenis #include "gdb_string.h"
34e93f7393Sniklas #include <sys/ptrace.h>
35e93f7393Sniklas 
36e93f7393Sniklas #include <mach.h>
37e93f7393Sniklas #include <mach_error.h>
38e93f7393Sniklas #include <mach/exception.h>
39b725ae77Skettenis #include <mach/message.h>
40b725ae77Skettenis #include <mach/notify.h>
41e93f7393Sniklas #include <mach/vm_attributes.h>
42e93f7393Sniklas 
43b725ae77Skettenis #include <hurd.h>
44b725ae77Skettenis #include <hurd/interrupt.h>
45e93f7393Sniklas #include <hurd/msg.h>
46e93f7393Sniklas #include <hurd/msg_request.h>
47b725ae77Skettenis #include <hurd/process.h>
48b725ae77Skettenis #include <hurd/process_request.h>
49e93f7393Sniklas #include <hurd/signal.h>
50e93f7393Sniklas #include <hurd/sigpreempt.h>
51e93f7393Sniklas 
52e93f7393Sniklas #include <portinfo.h>
53e93f7393Sniklas 
54e93f7393Sniklas #include "defs.h"
55e93f7393Sniklas #include "inferior.h"
56e93f7393Sniklas #include "symtab.h"
57e93f7393Sniklas #include "value.h"
58e93f7393Sniklas #include "language.h"
59e93f7393Sniklas #include "target.h"
60b725ae77Skettenis #include "gdb_wait.h"
61e93f7393Sniklas #include "gdbcmd.h"
62e93f7393Sniklas #include "gdbcore.h"
63b725ae77Skettenis #include "gdbthread.h"
64b725ae77Skettenis #include "gdb_assert.h"
65b725ae77Skettenis #include "gdb_obstack.h"
66e93f7393Sniklas 
67e93f7393Sniklas #include "gnu-nat.h"
68e93f7393Sniklas 
69e93f7393Sniklas #include "exc_request_S.h"
70e93f7393Sniklas #include "notify_S.h"
71e93f7393Sniklas #include "process_reply_S.h"
72e93f7393Sniklas #include "msg_reply_S.h"
73e93f7393Sniklas #include "exc_request_U.h"
74e93f7393Sniklas #include "msg_U.h"
75e93f7393Sniklas 
76e93f7393Sniklas static process_t proc_server = MACH_PORT_NULL;
77e93f7393Sniklas 
78e93f7393Sniklas /* If we've sent a proc_wait_request to the proc server, the pid of the
79e93f7393Sniklas    process we asked about.  We can only ever have one outstanding.  */
80e93f7393Sniklas int proc_wait_pid = 0;
81e93f7393Sniklas 
82e93f7393Sniklas /* The number of wait requests we've sent, and expect replies from.  */
83e93f7393Sniklas int proc_waits_pending = 0;
84e93f7393Sniklas 
85e93f7393Sniklas int gnu_debug_flag = 0;
86e93f7393Sniklas 
87e93f7393Sniklas /* Forward decls */
88e93f7393Sniklas 
89e93f7393Sniklas extern struct target_ops gnu_ops;
90e93f7393Sniklas 
91e93f7393Sniklas struct inf *make_inf ();
92e93f7393Sniklas void inf_clear_wait (struct inf *inf);
93e93f7393Sniklas void inf_cleanup (struct inf *inf);
94b725ae77Skettenis void inf_startup (struct inf *inf, int pid);
95e93f7393Sniklas int inf_update_suspends (struct inf *inf);
96b725ae77Skettenis void inf_set_pid (struct inf *inf, pid_t pid);
97e93f7393Sniklas void inf_validate_procs (struct inf *inf);
98e93f7393Sniklas void inf_steal_exc_ports (struct inf *inf);
99e93f7393Sniklas void inf_restore_exc_ports (struct inf *inf);
100e93f7393Sniklas struct proc *inf_tid_to_proc (struct inf *inf, int tid);
101b725ae77Skettenis void inf_set_threads_resume_sc (struct inf *inf,
102b725ae77Skettenis 				struct proc *run_thread,
103b725ae77Skettenis 				int run_others);
104b725ae77Skettenis int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
105b725ae77Skettenis void inf_suspend (struct inf *inf);
106b725ae77Skettenis void inf_resume (struct inf *inf);
107e93f7393Sniklas void inf_set_step_thread (struct inf *inf, struct proc *proc);
108e93f7393Sniklas void inf_detach (struct inf *inf);
109e93f7393Sniklas void inf_attach (struct inf *inf, int pid);
110e93f7393Sniklas void inf_signal (struct inf *inf, enum target_signal sig);
111b725ae77Skettenis void inf_continue (struct inf *inf);
112e93f7393Sniklas 
113e93f7393Sniklas #define inf_debug(_inf, msg, args...) \
114e93f7393Sniklas   do { struct inf *__inf = (_inf); \
115e93f7393Sniklas        debug ("{inf %d %p}: " msg, __inf->pid, __inf , ##args); } while (0)
116e93f7393Sniklas 
117b725ae77Skettenis void proc_abort (struct proc *proc, int force);
118e93f7393Sniklas struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
119e93f7393Sniklas struct proc *_proc_free (struct proc *proc);
120e93f7393Sniklas int proc_update_sc (struct proc *proc);
121e93f7393Sniklas error_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
122e93f7393Sniklas error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
123e93f7393Sniklas static mach_port_t _proc_get_exc_port (struct proc *proc);
124e93f7393Sniklas void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
125e93f7393Sniklas void proc_restore_exc_port (struct proc *proc);
126e93f7393Sniklas int proc_trace (struct proc *proc, int set);
127e93f7393Sniklas 
128e93f7393Sniklas /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
129e93f7393Sniklas    to INF's msg port and task port respectively.  If it has no msg port,
130e93f7393Sniklas    EIEIO is returned.  INF must refer to a running process!  */
131e93f7393Sniklas #define INF_MSGPORT_RPC(inf, rpc_expr) \
132e93f7393Sniklas   HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
133e93f7393Sniklas 		    (refport = inf->task->port, 0), 0, \
134e93f7393Sniklas 		    msgport ? (rpc_expr) : EIEIO)
135e93f7393Sniklas 
136e93f7393Sniklas /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
137e93f7393Sniklas    there's someone around to deal with the RPC (and resuspend things
138e93f7393Sniklas    afterwards).  This effects INF's threads' resume_sc count.  */
139e93f7393Sniklas #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
140e93f7393Sniklas   (inf_set_threads_resume_sc_for_signal_thread (inf) \
141e93f7393Sniklas    ? ({ error_t __e; \
142e93f7393Sniklas 	inf_resume (inf); \
143e93f7393Sniklas 	__e = INF_MSGPORT_RPC (inf, rpc_expr); \
144e93f7393Sniklas 	inf_suspend (inf); \
145e93f7393Sniklas 	__e; }) \
146e93f7393Sniklas    : EIEIO)
147e93f7393Sniklas 
148e93f7393Sniklas 
149e93f7393Sniklas /* The state passed by an exception message.  */
150e93f7393Sniklas struct exc_state
151e93f7393Sniklas   {
152e93f7393Sniklas     int exception;		/* The exception code */
153e93f7393Sniklas     int code, subcode;
154e93f7393Sniklas     mach_port_t handler;	/* The real exception port to handle this. */
155e93f7393Sniklas     mach_port_t reply;		/* The reply port from the exception call. */
156e93f7393Sniklas   };
157e93f7393Sniklas 
158e93f7393Sniklas /* The results of the last wait an inf did. */
159e93f7393Sniklas struct inf_wait
160e93f7393Sniklas   {
161e93f7393Sniklas     struct target_waitstatus status;	/* The status returned to gdb.  */
162e93f7393Sniklas     struct exc_state exc;	/* The exception that caused us to return. */
163e93f7393Sniklas     struct proc *thread;	/* The thread in question.  */
164e93f7393Sniklas     int suppress;		/* Something trivial happened.  */
165e93f7393Sniklas   };
166e93f7393Sniklas 
167e93f7393Sniklas /* The state of an inferior.  */
168e93f7393Sniklas struct inf
169e93f7393Sniklas   {
170e93f7393Sniklas     /* Fields describing the current inferior.  */
171e93f7393Sniklas 
172e93f7393Sniklas     struct proc *task;		/* The mach task.   */
173e93f7393Sniklas     struct proc *threads;	/* A linked list of all threads in TASK.  */
174e93f7393Sniklas 
175e93f7393Sniklas     /* True if THREADS needn't be validated by querying the task.  We assume that
176e93f7393Sniklas        we and the task in question are the only ones frobbing the thread list,
177e93f7393Sniklas        so as long as we don't let any code run, we don't have to worry about
178e93f7393Sniklas        THREADS changing.  */
179e93f7393Sniklas     int threads_up_to_date;
180e93f7393Sniklas 
181e93f7393Sniklas     pid_t pid;			/* The real system PID. */
182e93f7393Sniklas 
183e93f7393Sniklas     struct inf_wait wait;	/* What to return from target_wait.  */
184e93f7393Sniklas 
185e93f7393Sniklas     /* One thread proc in INF may be in `single-stepping mode'.  This is it.  */
186e93f7393Sniklas     struct proc *step_thread;
187e93f7393Sniklas 
188e93f7393Sniklas     /* The thread we think is the signal thread.  */
189e93f7393Sniklas     struct proc *signal_thread;
190e93f7393Sniklas 
191e93f7393Sniklas     mach_port_t event_port;	/* Where we receive various msgs.  */
192e93f7393Sniklas 
193e93f7393Sniklas     /* True if we think at least one thread in the inferior could currently be
194e93f7393Sniklas        running.  */
195b725ae77Skettenis     unsigned int running:1;
196e93f7393Sniklas 
197e93f7393Sniklas     /* True if the process has stopped (in the proc server sense).  Note that
198e93f7393Sniklas        since a proc server `stop' leaves the signal thread running, the inf can
199e93f7393Sniklas        be RUNNING && STOPPED...  */
200b725ae77Skettenis     unsigned int stopped:1;
201b725ae77Skettenis 
202b725ae77Skettenis     /* True if the inferior has no message port.  */
203b725ae77Skettenis     unsigned int nomsg:1;
204e93f7393Sniklas 
205e93f7393Sniklas     /* True if the inferior is traced.  */
206b725ae77Skettenis     unsigned int traced:1;
207e93f7393Sniklas 
208e93f7393Sniklas     /* True if we shouldn't try waiting for the inferior, usually because we
209e93f7393Sniklas        can't for some reason.  */
210b725ae77Skettenis     unsigned int no_wait:1;
211e93f7393Sniklas 
212e93f7393Sniklas     /* When starting a new inferior, we don't try to validate threads until all
213e93f7393Sniklas        the proper execs have been done.  This is a count of how many execs we
214e93f7393Sniklas        expect to happen.  */
215e93f7393Sniklas     unsigned pending_execs;
216e93f7393Sniklas 
217e93f7393Sniklas     /* Fields describing global state */
218e93f7393Sniklas 
219e93f7393Sniklas     /* The task suspend count used when gdb has control.  This is normally 1 to
220e93f7393Sniklas        make things easier for us, but sometimes (like when attaching to vital
221e93f7393Sniklas        system servers) it may be desirable to let the task continue to run
222e93f7393Sniklas        (pausing individual threads as necessary).  */
223e93f7393Sniklas     int pause_sc;
224e93f7393Sniklas 
225e93f7393Sniklas     /* The task suspend count left when detaching from a task.  */
226e93f7393Sniklas     int detach_sc;
227e93f7393Sniklas 
228e93f7393Sniklas     /* The initial values used for the run_sc and pause_sc of newly discovered
229e93f7393Sniklas        threads -- see the definition of those fields in struct proc.  */
230e93f7393Sniklas     int default_thread_run_sc;
231e93f7393Sniklas     int default_thread_pause_sc;
232e93f7393Sniklas     int default_thread_detach_sc;
233e93f7393Sniklas 
234e93f7393Sniklas     /* True if the process should be traced when started/attached.  Newly
235e93f7393Sniklas        started processes *must* be traced at first to exec them properly, but
236e93f7393Sniklas        if this is false, tracing is turned off as soon it has done so.  */
237e93f7393Sniklas     int want_signals;
238e93f7393Sniklas 
239e93f7393Sniklas     /* True if exceptions from the inferior process should be trapped.  This
240e93f7393Sniklas        must be on to use breakpoints.  */
241e93f7393Sniklas     int want_exceptions;
242e93f7393Sniklas   };
243e93f7393Sniklas 
244b725ae77Skettenis 
245b725ae77Skettenis int
__proc_pid(struct proc * proc)246b725ae77Skettenis __proc_pid (struct proc *proc)
247e93f7393Sniklas {
248e93f7393Sniklas   return proc->inf->pid;
249e93f7393Sniklas }
250b725ae77Skettenis 
251e93f7393Sniklas 
252e93f7393Sniklas /* Update PROC's real suspend count to match it's desired one.  Returns true
253e93f7393Sniklas    if we think PROC is now in a runnable state.  */
254e93f7393Sniklas int
proc_update_sc(struct proc * proc)255e93f7393Sniklas proc_update_sc (struct proc *proc)
256e93f7393Sniklas {
257e93f7393Sniklas   int running;
258e93f7393Sniklas   int err = 0;
259e93f7393Sniklas   int delta = proc->sc - proc->cur_sc;
260e93f7393Sniklas 
261e93f7393Sniklas   if (delta)
262e93f7393Sniklas     proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
263e93f7393Sniklas 
264e93f7393Sniklas   if (proc->sc == 0 && proc->state_changed)
265e93f7393Sniklas     /* Since PROC may start running, we must write back any state changes. */
266e93f7393Sniklas     {
267b725ae77Skettenis       gdb_assert (proc_is_thread (proc));
268e93f7393Sniklas       proc_debug (proc, "storing back changed thread state");
269e93f7393Sniklas       err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
270e93f7393Sniklas 			 (thread_state_t) &proc->state, THREAD_STATE_SIZE);
271e93f7393Sniklas       if (!err)
272e93f7393Sniklas 	proc->state_changed = 0;
273e93f7393Sniklas     }
274e93f7393Sniklas 
275e93f7393Sniklas   if (delta > 0)
276b725ae77Skettenis     {
277e93f7393Sniklas       while (delta-- > 0 && !err)
278b725ae77Skettenis 	{
279e93f7393Sniklas 	  if (proc_is_task (proc))
280e93f7393Sniklas 	    err = task_suspend (proc->port);
281e93f7393Sniklas 	  else
282e93f7393Sniklas 	    err = thread_suspend (proc->port);
283b725ae77Skettenis 	}
284b725ae77Skettenis     }
285e93f7393Sniklas   else
286b725ae77Skettenis     {
287e93f7393Sniklas       while (delta++ < 0 && !err)
288b725ae77Skettenis 	{
289e93f7393Sniklas 	  if (proc_is_task (proc))
290e93f7393Sniklas 	    err = task_resume (proc->port);
291e93f7393Sniklas 	  else
292e93f7393Sniklas 	    err = thread_resume (proc->port);
293b725ae77Skettenis 	}
294b725ae77Skettenis     }
295e93f7393Sniklas   if (!err)
296e93f7393Sniklas     proc->cur_sc = proc->sc;
297e93f7393Sniklas 
298e93f7393Sniklas   /* If we got an error, then the task/thread has disappeared.  */
299e93f7393Sniklas   running = !err && proc->sc == 0;
300e93f7393Sniklas 
301e93f7393Sniklas   proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
302e93f7393Sniklas   if (err)
303b725ae77Skettenis     proc_debug (proc, "err = %s", safe_strerror (err));
304e93f7393Sniklas 
305e93f7393Sniklas   if (running)
306e93f7393Sniklas     {
307e93f7393Sniklas       proc->aborted = 0;
308e93f7393Sniklas       proc->state_valid = proc->state_changed = 0;
309e93f7393Sniklas       proc->fetched_regs = 0;
310e93f7393Sniklas     }
311e93f7393Sniklas 
312e93f7393Sniklas   return running;
313e93f7393Sniklas }
314b725ae77Skettenis 
315e93f7393Sniklas 
316e93f7393Sniklas /* Thread_abort is called on PROC if needed.  PROC must be a thread proc.
317e93f7393Sniklas    If PROC is deemed `precious', then nothing is done unless FORCE is true.
318e93f7393Sniklas    In particular, a thread is precious if it's running (in which case forcing
319e93f7393Sniklas    it includes suspending it first), or if it has an exception pending.  */
320e93f7393Sniklas void
proc_abort(struct proc * proc,int force)321e93f7393Sniklas proc_abort (struct proc *proc, int force)
322e93f7393Sniklas {
323b725ae77Skettenis   gdb_assert (proc_is_thread (proc));
324e93f7393Sniklas 
325e93f7393Sniklas   if (!proc->aborted)
326e93f7393Sniklas     {
327e93f7393Sniklas       struct inf *inf = proc->inf;
328e93f7393Sniklas       int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
329e93f7393Sniklas 
330e93f7393Sniklas       if (running && force)
331e93f7393Sniklas 	{
332e93f7393Sniklas 	  proc->sc = 1;
333e93f7393Sniklas 	  inf_update_suspends (proc->inf);
334e93f7393Sniklas 	  running = 0;
335e93f7393Sniklas 	  warning ("Stopped %s.", proc_string (proc));
336e93f7393Sniklas 	}
337e93f7393Sniklas       else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
338e93f7393Sniklas 	/* An exception is pending on PROC, which don't mess with.  */
339e93f7393Sniklas 	running = 1;
340e93f7393Sniklas 
341e93f7393Sniklas       if (!running)
342e93f7393Sniklas 	/* We only abort the thread if it's not actually running.  */
343e93f7393Sniklas 	{
344e93f7393Sniklas 	  thread_abort (proc->port);
345e93f7393Sniklas 	  proc_debug (proc, "aborted");
346e93f7393Sniklas 	  proc->aborted = 1;
347e93f7393Sniklas 	}
348e93f7393Sniklas       else
349e93f7393Sniklas 	proc_debug (proc, "not aborting");
350e93f7393Sniklas     }
351e93f7393Sniklas }
352e93f7393Sniklas 
353e93f7393Sniklas /* Make sure that the state field in PROC is up to date, and return a pointer
354e93f7393Sniklas    to it, or 0 if something is wrong.  If WILL_MODIFY is true, makes sure
355e93f7393Sniklas    that the thread is stopped and aborted first, and sets the state_changed
356e93f7393Sniklas    field in PROC to true.  */
357e93f7393Sniklas thread_state_t
proc_get_state(struct proc * proc,int will_modify)358e93f7393Sniklas proc_get_state (struct proc *proc, int will_modify)
359e93f7393Sniklas {
360e93f7393Sniklas   int was_aborted = proc->aborted;
361e93f7393Sniklas 
362e93f7393Sniklas   proc_debug (proc, "updating state info%s",
363e93f7393Sniklas 	      will_modify ? " (with intention to modify)" : "");
364e93f7393Sniklas 
365e93f7393Sniklas   proc_abort (proc, will_modify);
366e93f7393Sniklas 
367e93f7393Sniklas   if (!was_aborted && proc->aborted)
368e93f7393Sniklas     /* PROC's state may have changed since we last fetched it.  */
369e93f7393Sniklas     proc->state_valid = 0;
370e93f7393Sniklas 
371e93f7393Sniklas   if (!proc->state_valid)
372e93f7393Sniklas     {
373e93f7393Sniklas       mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
374e93f7393Sniklas       error_t err =
375e93f7393Sniklas       thread_get_state (proc->port, THREAD_STATE_FLAVOR,
376e93f7393Sniklas 			(thread_state_t) &proc->state, &state_size);
377e93f7393Sniklas       proc_debug (proc, "getting thread state");
378e93f7393Sniklas       proc->state_valid = !err;
379e93f7393Sniklas     }
380e93f7393Sniklas 
381e93f7393Sniklas   if (proc->state_valid)
382e93f7393Sniklas     {
383e93f7393Sniklas       if (will_modify)
384e93f7393Sniklas 	proc->state_changed = 1;
385e93f7393Sniklas       return (thread_state_t) &proc->state;
386e93f7393Sniklas     }
387e93f7393Sniklas   else
388e93f7393Sniklas     return 0;
389e93f7393Sniklas }
390b725ae77Skettenis 
391e93f7393Sniklas 
392b725ae77Skettenis /* Set PORT to PROC's exception port.  */
393e93f7393Sniklas error_t
proc_get_exception_port(struct proc * proc,mach_port_t * port)394e93f7393Sniklas proc_get_exception_port (struct proc * proc, mach_port_t * port)
395e93f7393Sniklas {
396e93f7393Sniklas   if (proc_is_task (proc))
397e93f7393Sniklas     return task_get_exception_port (proc->port, port);
398e93f7393Sniklas   else
399e93f7393Sniklas     return thread_get_exception_port (proc->port, port);
400e93f7393Sniklas }
401e93f7393Sniklas 
402b725ae77Skettenis /* Set PROC's exception port to PORT.  */
403e93f7393Sniklas error_t
proc_set_exception_port(struct proc * proc,mach_port_t port)404e93f7393Sniklas proc_set_exception_port (struct proc * proc, mach_port_t port)
405e93f7393Sniklas {
406e93f7393Sniklas   proc_debug (proc, "setting exception port: %d", port);
407e93f7393Sniklas   if (proc_is_task (proc))
408e93f7393Sniklas     return task_set_exception_port (proc->port, port);
409e93f7393Sniklas   else
410e93f7393Sniklas     return thread_set_exception_port (proc->port, port);
411e93f7393Sniklas }
412e93f7393Sniklas 
413e93f7393Sniklas /* Get PROC's exception port, cleaning up a bit if proc has died.  */
414e93f7393Sniklas static mach_port_t
_proc_get_exc_port(struct proc * proc)415e93f7393Sniklas _proc_get_exc_port (struct proc *proc)
416e93f7393Sniklas {
417e93f7393Sniklas   mach_port_t exc_port;
418e93f7393Sniklas   error_t err = proc_get_exception_port (proc, &exc_port);
419e93f7393Sniklas 
420e93f7393Sniklas   if (err)
421e93f7393Sniklas     /* PROC must be dead.  */
422e93f7393Sniklas     {
423e93f7393Sniklas       if (proc->exc_port)
424e93f7393Sniklas 	mach_port_deallocate (mach_task_self (), proc->exc_port);
425e93f7393Sniklas       proc->exc_port = MACH_PORT_NULL;
426e93f7393Sniklas       if (proc->saved_exc_port)
427e93f7393Sniklas 	mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
428e93f7393Sniklas       proc->saved_exc_port = MACH_PORT_NULL;
429e93f7393Sniklas     }
430e93f7393Sniklas 
431e93f7393Sniklas   return exc_port;
432e93f7393Sniklas }
433e93f7393Sniklas 
434e93f7393Sniklas /* Replace PROC's exception port with EXC_PORT, unless it's already been
435e93f7393Sniklas    done.  Stash away any existing exception port so we can restore it later. */
436e93f7393Sniklas void
proc_steal_exc_port(struct proc * proc,mach_port_t exc_port)437e93f7393Sniklas proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
438e93f7393Sniklas {
439e93f7393Sniklas   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
440e93f7393Sniklas 
441e93f7393Sniklas   if (cur_exc_port)
442e93f7393Sniklas     {
443b725ae77Skettenis       error_t err = 0;
444e93f7393Sniklas 
445e93f7393Sniklas       proc_debug (proc, "inserting exception port: %d", exc_port);
446e93f7393Sniklas 
447e93f7393Sniklas       if (cur_exc_port != exc_port)
448e93f7393Sniklas 	/* Put in our exception port.  */
449e93f7393Sniklas 	err = proc_set_exception_port (proc, exc_port);
450e93f7393Sniklas 
451e93f7393Sniklas       if (err || cur_exc_port == proc->exc_port)
452e93f7393Sniklas 	/* We previously set the exception port, and it's still set.  So we
453e93f7393Sniklas 	   just keep the old saved port which is what the proc set.  */
454e93f7393Sniklas 	{
455e93f7393Sniklas 	  if (cur_exc_port)
456e93f7393Sniklas 	    mach_port_deallocate (mach_task_self (), cur_exc_port);
457e93f7393Sniklas 	}
458e93f7393Sniklas       else
459e93f7393Sniklas 	/* Keep a copy of PROC's old exception port so it can be restored. */
460e93f7393Sniklas 	{
461e93f7393Sniklas 	  if (proc->saved_exc_port)
462e93f7393Sniklas 	    mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
463e93f7393Sniklas 	  proc->saved_exc_port = cur_exc_port;
464e93f7393Sniklas 	}
465e93f7393Sniklas 
466e93f7393Sniklas       proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
467e93f7393Sniklas 
468e93f7393Sniklas       if (!err)
469e93f7393Sniklas 	proc->exc_port = exc_port;
470e93f7393Sniklas       else
471e93f7393Sniklas 	warning ("Error setting exception port for %s: %s",
472b725ae77Skettenis 		 proc_string (proc), safe_strerror (err));
473e93f7393Sniklas     }
474e93f7393Sniklas }
475e93f7393Sniklas 
476b725ae77Skettenis /* If we previously replaced PROC's exception port, put back what we
477b725ae77Skettenis    found there at the time, unless *our* exception port has since been
478b725ae77Skettenis    overwritten, in which case who knows what's going on.  */
479e93f7393Sniklas void
proc_restore_exc_port(struct proc * proc)480e93f7393Sniklas proc_restore_exc_port (struct proc *proc)
481e93f7393Sniklas {
482e93f7393Sniklas   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
483e93f7393Sniklas 
484e93f7393Sniklas   if (cur_exc_port)
485e93f7393Sniklas     {
486e93f7393Sniklas       error_t err = 0;
487e93f7393Sniklas 
488e93f7393Sniklas       proc_debug (proc, "restoring real exception port");
489e93f7393Sniklas 
490e93f7393Sniklas       if (proc->exc_port == cur_exc_port)
491e93f7393Sniklas 	/* Our's is still there.  */
492e93f7393Sniklas 	err = proc_set_exception_port (proc, proc->saved_exc_port);
493e93f7393Sniklas 
494e93f7393Sniklas       if (proc->saved_exc_port)
495e93f7393Sniklas 	mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
496e93f7393Sniklas       proc->saved_exc_port = MACH_PORT_NULL;
497e93f7393Sniklas 
498e93f7393Sniklas       if (!err)
499e93f7393Sniklas 	proc->exc_port = MACH_PORT_NULL;
500e93f7393Sniklas       else
501e93f7393Sniklas 	warning ("Error setting exception port for %s: %s",
502b725ae77Skettenis 		 proc_string (proc), safe_strerror (err));
503e93f7393Sniklas     }
504e93f7393Sniklas }
505b725ae77Skettenis 
506e93f7393Sniklas 
507b725ae77Skettenis /* Turns hardware tracing in PROC on or off when SET is true or false,
508e93f7393Sniklas    respectively.  Returns true on success.  */
509e93f7393Sniklas int
proc_trace(struct proc * proc,int set)510e93f7393Sniklas proc_trace (struct proc *proc, int set)
511e93f7393Sniklas {
512e93f7393Sniklas   thread_state_t state = proc_get_state (proc, 1);
513e93f7393Sniklas 
514e93f7393Sniklas   if (!state)
515e93f7393Sniklas     return 0;			/* the thread must be dead.  */
516e93f7393Sniklas 
517e93f7393Sniklas   proc_debug (proc, "tracing %s", set ? "on" : "off");
518e93f7393Sniklas 
519e93f7393Sniklas   if (set)
520e93f7393Sniklas     {
521e93f7393Sniklas       /* XXX We don't get the exception unless the thread has its own
522e93f7393Sniklas          exception port???? */
523e93f7393Sniklas       if (proc->exc_port == MACH_PORT_NULL)
524e93f7393Sniklas 	proc_steal_exc_port (proc, proc->inf->event_port);
525e93f7393Sniklas       THREAD_STATE_SET_TRACED (state);
526e93f7393Sniklas     }
527e93f7393Sniklas   else
528e93f7393Sniklas     THREAD_STATE_CLEAR_TRACED (state);
529e93f7393Sniklas 
530e93f7393Sniklas   return 1;
531e93f7393Sniklas }
532b725ae77Skettenis 
533e93f7393Sniklas 
534e93f7393Sniklas /* A variable from which to assign new TIDs.  */
535e93f7393Sniklas static int next_thread_id = 1;
536e93f7393Sniklas 
537e93f7393Sniklas /* Returns a new proc structure with the given fields.  Also adds a
538e93f7393Sniklas    notification for PORT becoming dead to be sent to INF's notify port.  */
539e93f7393Sniklas struct proc *
make_proc(struct inf * inf,mach_port_t port,int tid)540e93f7393Sniklas make_proc (struct inf *inf, mach_port_t port, int tid)
541e93f7393Sniklas {
542e93f7393Sniklas   error_t err;
543e93f7393Sniklas   mach_port_t prev_port = MACH_PORT_NULL;
544b725ae77Skettenis   struct proc *proc = xmalloc (sizeof (struct proc));
545e93f7393Sniklas 
546e93f7393Sniklas   proc->port = port;
547e93f7393Sniklas   proc->tid = tid;
548e93f7393Sniklas   proc->inf = inf;
549e93f7393Sniklas   proc->next = 0;
550e93f7393Sniklas   proc->saved_exc_port = MACH_PORT_NULL;
551e93f7393Sniklas   proc->exc_port = MACH_PORT_NULL;
552e93f7393Sniklas 
553e93f7393Sniklas   proc->sc = 0;
554e93f7393Sniklas   proc->cur_sc = 0;
555e93f7393Sniklas 
556e93f7393Sniklas   /* Note that these are all the values for threads; the task simply uses the
557e93f7393Sniklas      corresponding field in INF directly.  */
558e93f7393Sniklas   proc->run_sc = inf->default_thread_run_sc;
559e93f7393Sniklas   proc->pause_sc = inf->default_thread_pause_sc;
560e93f7393Sniklas   proc->detach_sc = inf->default_thread_detach_sc;
561e93f7393Sniklas   proc->resume_sc = proc->run_sc;
562e93f7393Sniklas 
563e93f7393Sniklas   proc->aborted = 0;
564e93f7393Sniklas   proc->dead = 0;
565e93f7393Sniklas   proc->state_valid = 0;
566e93f7393Sniklas   proc->state_changed = 0;
567e93f7393Sniklas 
568e93f7393Sniklas   proc_debug (proc, "is new");
569e93f7393Sniklas 
570e93f7393Sniklas   /* Get notified when things die.  */
571e93f7393Sniklas   err =
572e93f7393Sniklas     mach_port_request_notification (mach_task_self (), port,
573e93f7393Sniklas 				    MACH_NOTIFY_DEAD_NAME, 1,
574e93f7393Sniklas 				    inf->event_port,
575e93f7393Sniklas 				    MACH_MSG_TYPE_MAKE_SEND_ONCE,
576e93f7393Sniklas 				    &prev_port);
577e93f7393Sniklas   if (err)
578e93f7393Sniklas     warning ("Couldn't request notification for port %d: %s",
579b725ae77Skettenis 	     port, safe_strerror (err));
580e93f7393Sniklas   else
581e93f7393Sniklas     {
582e93f7393Sniklas       proc_debug (proc, "notifications to: %d", inf->event_port);
583e93f7393Sniklas       if (prev_port != MACH_PORT_NULL)
584e93f7393Sniklas 	mach_port_deallocate (mach_task_self (), prev_port);
585e93f7393Sniklas     }
586e93f7393Sniklas 
587e93f7393Sniklas   if (inf->want_exceptions)
588b725ae77Skettenis     {
589e93f7393Sniklas       if (proc_is_task (proc))
590e93f7393Sniklas 	/* Make the task exception port point to us.  */
591e93f7393Sniklas 	proc_steal_exc_port (proc, inf->event_port);
592e93f7393Sniklas       else
593b725ae77Skettenis 	/* Just clear thread exception ports -- they default to the
594b725ae77Skettenis            task one.  */
595e93f7393Sniklas 	proc_steal_exc_port (proc, MACH_PORT_NULL);
596b725ae77Skettenis     }
597e93f7393Sniklas 
598e93f7393Sniklas   return proc;
599e93f7393Sniklas }
600e93f7393Sniklas 
601b725ae77Skettenis /* Frees PROC and any resources it uses, and returns the value of PROC's
602b725ae77Skettenis    next field.  */
603e93f7393Sniklas struct proc *
_proc_free(struct proc * proc)604e93f7393Sniklas _proc_free (struct proc *proc)
605e93f7393Sniklas {
606e93f7393Sniklas   struct inf *inf = proc->inf;
607e93f7393Sniklas   struct proc *next = proc->next;
608e93f7393Sniklas 
609e93f7393Sniklas   proc_debug (proc, "freeing...");
610e93f7393Sniklas 
611e93f7393Sniklas   if (proc == inf->step_thread)
612e93f7393Sniklas     /* Turn off single stepping.  */
613e93f7393Sniklas     inf_set_step_thread (inf, 0);
614e93f7393Sniklas   if (proc == inf->wait.thread)
615e93f7393Sniklas     inf_clear_wait (inf);
616e93f7393Sniklas   if (proc == inf->signal_thread)
617e93f7393Sniklas     inf->signal_thread = 0;
618e93f7393Sniklas 
619e93f7393Sniklas   if (proc->port != MACH_PORT_NULL)
620e93f7393Sniklas     {
621e93f7393Sniklas       if (proc->exc_port != MACH_PORT_NULL)
622e93f7393Sniklas 	/* Restore the original exception port.  */
623e93f7393Sniklas 	proc_restore_exc_port (proc);
624e93f7393Sniklas       if (proc->cur_sc != 0)
625e93f7393Sniklas 	/* Resume the thread/task.  */
626e93f7393Sniklas 	{
627e93f7393Sniklas 	  proc->sc = 0;
628e93f7393Sniklas 	  proc_update_sc (proc);
629e93f7393Sniklas 	}
630e93f7393Sniklas       mach_port_deallocate (mach_task_self (), proc->port);
631e93f7393Sniklas     }
632e93f7393Sniklas 
633b725ae77Skettenis   xfree (proc);
634e93f7393Sniklas   return next;
635e93f7393Sniklas }
636e93f7393Sniklas 
637b725ae77Skettenis 
638b725ae77Skettenis struct inf *
make_inf(void)639b725ae77Skettenis make_inf (void)
640b725ae77Skettenis {
641b725ae77Skettenis   struct inf *inf = xmalloc (sizeof (struct inf));
642e93f7393Sniklas 
643e93f7393Sniklas   inf->task = 0;
644e93f7393Sniklas   inf->threads = 0;
645e93f7393Sniklas   inf->threads_up_to_date = 0;
646e93f7393Sniklas   inf->pid = 0;
647e93f7393Sniklas   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
648e93f7393Sniklas   inf->wait.thread = 0;
649e93f7393Sniklas   inf->wait.exc.handler = MACH_PORT_NULL;
650e93f7393Sniklas   inf->wait.exc.reply = MACH_PORT_NULL;
651e93f7393Sniklas   inf->step_thread = 0;
652e93f7393Sniklas   inf->signal_thread = 0;
653e93f7393Sniklas   inf->event_port = MACH_PORT_NULL;
654e93f7393Sniklas   inf->running = 0;
655b725ae77Skettenis   inf->stopped = 0;
656b725ae77Skettenis   inf->nomsg = 1;
657e93f7393Sniklas   inf->traced = 0;
658e93f7393Sniklas   inf->no_wait = 0;
659e93f7393Sniklas   inf->pending_execs = 0;
660e93f7393Sniklas   inf->pause_sc = 1;
661e93f7393Sniklas   inf->detach_sc = 0;
662e93f7393Sniklas   inf->default_thread_run_sc = 0;
663e93f7393Sniklas   inf->default_thread_pause_sc = 0;
664e93f7393Sniklas   inf->default_thread_detach_sc = 0;
665e93f7393Sniklas   inf->want_signals = 1;	/* By default */
666e93f7393Sniklas   inf->want_exceptions = 1;	/* By default */
667e93f7393Sniklas 
668e93f7393Sniklas   return inf;
669e93f7393Sniklas }
670e93f7393Sniklas 
671b725ae77Skettenis /* Clear INF's target wait status.  */
672e93f7393Sniklas void
inf_clear_wait(struct inf * inf)673e93f7393Sniklas inf_clear_wait (struct inf *inf)
674e93f7393Sniklas {
675e93f7393Sniklas   inf_debug (inf, "clearing wait");
676e93f7393Sniklas   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
677e93f7393Sniklas   inf->wait.thread = 0;
678e93f7393Sniklas   inf->wait.suppress = 0;
679e93f7393Sniklas   if (inf->wait.exc.handler != MACH_PORT_NULL)
680e93f7393Sniklas     {
681e93f7393Sniklas       mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
682e93f7393Sniklas       inf->wait.exc.handler = MACH_PORT_NULL;
683e93f7393Sniklas     }
684e93f7393Sniklas   if (inf->wait.exc.reply != MACH_PORT_NULL)
685e93f7393Sniklas     {
686e93f7393Sniklas       mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
687e93f7393Sniklas       inf->wait.exc.reply = MACH_PORT_NULL;
688e93f7393Sniklas     }
689e93f7393Sniklas }
690b725ae77Skettenis 
691e93f7393Sniklas 
692e93f7393Sniklas void
inf_cleanup(struct inf * inf)693e93f7393Sniklas inf_cleanup (struct inf *inf)
694e93f7393Sniklas {
695e93f7393Sniklas   inf_debug (inf, "cleanup");
696e93f7393Sniklas 
697e93f7393Sniklas   inf_clear_wait (inf);
698e93f7393Sniklas 
699b725ae77Skettenis   inf_set_pid (inf, -1);
700e93f7393Sniklas   inf->pid = 0;
701b725ae77Skettenis   inf->running = 0;
702b725ae77Skettenis   inf->stopped = 0;
703b725ae77Skettenis   inf->nomsg = 1;
704e93f7393Sniklas   inf->traced = 0;
705e93f7393Sniklas   inf->no_wait = 0;
706e93f7393Sniklas   inf->pending_execs = 0;
707e93f7393Sniklas 
708e93f7393Sniklas   if (inf->event_port)
709e93f7393Sniklas     {
710e93f7393Sniklas       mach_port_destroy (mach_task_self (), inf->event_port);
711e93f7393Sniklas       inf->event_port = MACH_PORT_NULL;
712e93f7393Sniklas     }
713e93f7393Sniklas }
714e93f7393Sniklas 
715e93f7393Sniklas void
inf_startup(struct inf * inf,int pid)716b725ae77Skettenis inf_startup (struct inf *inf, int pid)
717e93f7393Sniklas {
718e93f7393Sniklas   error_t err;
719e93f7393Sniklas 
720b725ae77Skettenis   inf_debug (inf, "startup: pid = %d", pid);
721e93f7393Sniklas 
722e93f7393Sniklas   inf_cleanup (inf);
723e93f7393Sniklas 
724e93f7393Sniklas   /* Make the port on which we receive all events.  */
725e93f7393Sniklas   err = mach_port_allocate (mach_task_self (),
726e93f7393Sniklas 			    MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
727e93f7393Sniklas   if (err)
728b725ae77Skettenis     error ("Error allocating event port: %s", safe_strerror (err));
729e93f7393Sniklas 
730e93f7393Sniklas   /* Make a send right for it, so we can easily copy it for other people.  */
731e93f7393Sniklas   mach_port_insert_right (mach_task_self (), inf->event_port,
732e93f7393Sniklas 			  inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
733b725ae77Skettenis   inf_set_pid (inf, pid);
734e93f7393Sniklas }
735b725ae77Skettenis 
736e93f7393Sniklas 
737b725ae77Skettenis /* Close current process, if any, and attach INF to process PORT.  */
738e93f7393Sniklas void
inf_set_pid(struct inf * inf,pid_t pid)739b725ae77Skettenis inf_set_pid (struct inf *inf, pid_t pid)
740e93f7393Sniklas {
741b725ae77Skettenis   task_t task_port;
742e93f7393Sniklas   struct proc *task = inf->task;
743e93f7393Sniklas 
744b725ae77Skettenis   inf_debug (inf, "setting pid: %d", pid);
745e93f7393Sniklas 
746b725ae77Skettenis   if (pid < 0)
747b725ae77Skettenis     task_port = MACH_PORT_NULL;
748b725ae77Skettenis   else
749b725ae77Skettenis     {
750b725ae77Skettenis       error_t err = proc_pid2task (proc_server, pid, &task_port);
751b725ae77Skettenis       if (err)
752b725ae77Skettenis 	error ("Error getting task for pid %d: %s", pid, safe_strerror (err));
753b725ae77Skettenis     }
754b725ae77Skettenis 
755b725ae77Skettenis   inf_debug (inf, "setting task: %d", task_port);
756b725ae77Skettenis 
757b725ae77Skettenis   if (inf->pause_sc)
758b725ae77Skettenis     task_suspend (task_port);
759b725ae77Skettenis 
760b725ae77Skettenis   if (task && task->port != task_port)
761e93f7393Sniklas     {
762e93f7393Sniklas       inf->task = 0;
763e93f7393Sniklas       inf_validate_procs (inf);	/* Trash all the threads. */
764e93f7393Sniklas       _proc_free (task);	/* And the task. */
765e93f7393Sniklas     }
766e93f7393Sniklas 
767b725ae77Skettenis   if (task_port != MACH_PORT_NULL)
768e93f7393Sniklas     {
769b725ae77Skettenis       inf->task = make_proc (inf, task_port, PROC_TID_TASK);
770e93f7393Sniklas       inf->threads_up_to_date = 0;
771e93f7393Sniklas     }
772b725ae77Skettenis 
773b725ae77Skettenis   if (inf->task)
774b725ae77Skettenis     {
775b725ae77Skettenis       inf->pid = pid;
776b725ae77Skettenis       if (inf->pause_sc)
777b725ae77Skettenis 	/* Reflect task_suspend above.  */
778b725ae77Skettenis 	inf->task->sc = inf->task->cur_sc = 1;
779e93f7393Sniklas     }
780b725ae77Skettenis   else
781b725ae77Skettenis     inf->pid = -1;
782b725ae77Skettenis }
783b725ae77Skettenis 
784e93f7393Sniklas 
785b725ae77Skettenis /* Validates INF's stopped, nomsg and traced field from the actual
786b725ae77Skettenis    proc server state.  Note that the traced field is only updated from
787b725ae77Skettenis    the proc server state if we do not have a message port.  If we do
788b725ae77Skettenis    have a message port we'd better look at the tracemask itself.  */
789e93f7393Sniklas static void
inf_validate_procinfo(struct inf * inf)790b725ae77Skettenis inf_validate_procinfo (struct inf *inf)
791e93f7393Sniklas {
792e93f7393Sniklas   char *noise;
793e93f7393Sniklas   mach_msg_type_number_t noise_len = 0;
794e93f7393Sniklas   struct procinfo *pi;
795e93f7393Sniklas   mach_msg_type_number_t pi_len = 0;
796e93f7393Sniklas   int info_flags = 0;
797e93f7393Sniklas   error_t err =
798e93f7393Sniklas   proc_getprocinfo (proc_server, inf->pid, &info_flags,
799e93f7393Sniklas 		    (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
800e93f7393Sniklas 
801e93f7393Sniklas   if (!err)
802e93f7393Sniklas     {
803e93f7393Sniklas       inf->stopped = !!(pi->state & PI_STOPPED);
804b725ae77Skettenis       inf->nomsg = !!(pi->state & PI_NOMSG);
805b725ae77Skettenis       if (inf->nomsg)
806b725ae77Skettenis 	inf->traced = !!(pi->state & PI_TRACED);
807e93f7393Sniklas       vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
808e93f7393Sniklas       if (noise_len > 0)
809e93f7393Sniklas 	vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
810e93f7393Sniklas     }
811e93f7393Sniklas }
812e93f7393Sniklas 
813b725ae77Skettenis /* Validates INF's task suspend count.  If it's higher than we expect,
814b725ae77Skettenis    verify with the user before `stealing' the extra count.  */
815e93f7393Sniklas static void
inf_validate_task_sc(struct inf * inf)816e93f7393Sniklas inf_validate_task_sc (struct inf *inf)
817e93f7393Sniklas {
818b725ae77Skettenis   char *noise;
819b725ae77Skettenis   mach_msg_type_number_t noise_len = 0;
820b725ae77Skettenis   struct procinfo *pi;
821b725ae77Skettenis   mach_msg_type_number_t pi_len = 0;
822b725ae77Skettenis   int info_flags = PI_FETCH_TASKINFO;
823b725ae77Skettenis   int suspend_count = -1;
824b725ae77Skettenis   error_t err;
825e93f7393Sniklas 
826b725ae77Skettenis  retry:
827b725ae77Skettenis   err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
828b725ae77Skettenis 			  (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
829e93f7393Sniklas   if (err)
830b725ae77Skettenis     {
831e93f7393Sniklas       inf->task->dead = 1; /* oh well */
832b725ae77Skettenis       return;
833b725ae77Skettenis     }
834b725ae77Skettenis 
835b725ae77Skettenis   if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
836b725ae77Skettenis     {
837b725ae77Skettenis       /* The proc server might have suspended the task while stopping
838b725ae77Skettenis          it.  This happens when the task is handling a traced signal.
839b725ae77Skettenis          Refetch the suspend count.  The proc server should be
840b725ae77Skettenis          finished stopping the task by now.  */
841b725ae77Skettenis       suspend_count = pi->taskinfo.suspend_count;
842b725ae77Skettenis       goto retry;
843b725ae77Skettenis     }
844b725ae77Skettenis 
845b725ae77Skettenis   suspend_count = pi->taskinfo.suspend_count;
846b725ae77Skettenis 
847b725ae77Skettenis   vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
848b725ae77Skettenis   if (noise_len > 0)
849b725ae77Skettenis     vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
850b725ae77Skettenis 
851b725ae77Skettenis   if (inf->task->cur_sc < suspend_count)
852e93f7393Sniklas     {
853e93f7393Sniklas       int abort;
854e93f7393Sniklas 
855e93f7393Sniklas       target_terminal_ours ();	/* Allow I/O.  */
856b725ae77Skettenis       abort = !query ("Pid %d has an additional task suspend count of %d;"
857b725ae77Skettenis 		      " clear it? ", inf->pid,
858b725ae77Skettenis 		      suspend_count - inf->task->cur_sc);
859e93f7393Sniklas       target_terminal_inferior ();	/* Give it back to the child.  */
860e93f7393Sniklas 
861e93f7393Sniklas       if (abort)
862e93f7393Sniklas 	error ("Additional task suspend count left untouched.");
863e93f7393Sniklas 
864b725ae77Skettenis       inf->task->cur_sc = suspend_count;
865e93f7393Sniklas     }
866e93f7393Sniklas }
867e93f7393Sniklas 
868b725ae77Skettenis /* Turns tracing for INF on or off, depending on ON, unless it already
869b725ae77Skettenis    is.  If INF is running, the resume_sc count of INF's threads will
870b725ae77Skettenis    be modified, and the signal thread will briefly be run to change
871b725ae77Skettenis    the trace state.  */
872e93f7393Sniklas void
inf_set_traced(struct inf * inf,int on)873e93f7393Sniklas inf_set_traced (struct inf *inf, int on)
874e93f7393Sniklas {
875b725ae77Skettenis   if (on == inf->traced)
876b725ae77Skettenis     return;
877b725ae77Skettenis 
878e93f7393Sniklas   if (inf->task && !inf->task->dead)
879e93f7393Sniklas     /* Make it take effect immediately.  */
880e93f7393Sniklas     {
881e93f7393Sniklas       sigset_t mask = on ? ~(sigset_t) 0 : 0;
882e93f7393Sniklas       error_t err =
883e93f7393Sniklas 	INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
884e93f7393Sniklas 						       INIT_TRACEMASK, mask));
885e93f7393Sniklas       if (err == EIEIO)
886e93f7393Sniklas 	{
887e93f7393Sniklas 	  if (on)
888b725ae77Skettenis 	    warning ("Can't modify tracing state for pid %d: %s",
889b725ae77Skettenis 		     inf->pid, "No signal thread");
890e93f7393Sniklas 	  inf->traced = on;
891e93f7393Sniklas 	}
892e93f7393Sniklas       else if (err)
893e93f7393Sniklas 	warning ("Can't modify tracing state for pid %d: %s",
894b725ae77Skettenis 		 inf->pid, safe_strerror (err));
895e93f7393Sniklas       else
896e93f7393Sniklas 	inf->traced = on;
897e93f7393Sniklas     }
898e93f7393Sniklas   else
899e93f7393Sniklas     inf->traced = on;
900e93f7393Sniklas }
901b725ae77Skettenis 
902e93f7393Sniklas 
903b725ae77Skettenis /* Makes all the real suspend count deltas of all the procs in INF
904b725ae77Skettenis    match the desired values.  Careful to always do thread/task suspend
905b725ae77Skettenis    counts in the safe order.  Returns true if at least one thread is
906b725ae77Skettenis    thought to be running. */
907e93f7393Sniklas int
inf_update_suspends(struct inf * inf)908e93f7393Sniklas inf_update_suspends (struct inf *inf)
909e93f7393Sniklas {
910e93f7393Sniklas   struct proc *task = inf->task;
911e93f7393Sniklas   /* We don't have to update INF->threads even though we're iterating over it
912e93f7393Sniklas      because we'll change a thread only if it already has an existing proc
913e93f7393Sniklas      entry.  */
914e93f7393Sniklas 
915e93f7393Sniklas   inf_debug (inf, "updating suspend counts");
916e93f7393Sniklas 
917e93f7393Sniklas   if (task)
918e93f7393Sniklas     {
919e93f7393Sniklas       struct proc *thread;
920e93f7393Sniklas       int task_running = (task->sc == 0), thread_running = 0;
921e93f7393Sniklas 
922e93f7393Sniklas       if (task->sc > task->cur_sc)
923e93f7393Sniklas 	/* The task is becoming _more_ suspended; do before any threads.  */
924e93f7393Sniklas 	task_running = proc_update_sc (task);
925e93f7393Sniklas 
926e93f7393Sniklas       if (inf->pending_execs)
927e93f7393Sniklas 	/* When we're waiting for an exec, things may be happening behind our
928e93f7393Sniklas 	   back, so be conservative.  */
929e93f7393Sniklas 	thread_running = 1;
930e93f7393Sniklas 
931e93f7393Sniklas       /* Do all the thread suspend counts.  */
932e93f7393Sniklas       for (thread = inf->threads; thread; thread = thread->next)
933e93f7393Sniklas 	thread_running |= proc_update_sc (thread);
934e93f7393Sniklas 
935e93f7393Sniklas       if (task->sc != task->cur_sc)
936e93f7393Sniklas 	/* We didn't do the task first, because we wanted to wait for the
937e93f7393Sniklas 	   threads; do it now.  */
938e93f7393Sniklas 	task_running = proc_update_sc (task);
939e93f7393Sniklas 
940e93f7393Sniklas       inf_debug (inf, "%srunning...",
941e93f7393Sniklas 		 (thread_running && task_running) ? "" : "not ");
942e93f7393Sniklas 
943e93f7393Sniklas       inf->running = thread_running && task_running;
944e93f7393Sniklas 
945e93f7393Sniklas       /* Once any thread has executed some code, we can't depend on the
946e93f7393Sniklas          threads list any more.  */
947e93f7393Sniklas       if (inf->running)
948e93f7393Sniklas 	inf->threads_up_to_date = 0;
949e93f7393Sniklas 
950e93f7393Sniklas       return inf->running;
951e93f7393Sniklas     }
952e93f7393Sniklas 
953e93f7393Sniklas   return 0;
954e93f7393Sniklas }
955b725ae77Skettenis 
956e93f7393Sniklas 
957e93f7393Sniklas /* Converts a GDB pid to a struct proc.  */
958e93f7393Sniklas struct proc *
inf_tid_to_thread(struct inf * inf,int tid)959e93f7393Sniklas inf_tid_to_thread (struct inf *inf, int tid)
960e93f7393Sniklas {
961e93f7393Sniklas   struct proc *thread = inf->threads;
962b725ae77Skettenis 
963e93f7393Sniklas   while (thread)
964e93f7393Sniklas     if (thread->tid == tid)
965e93f7393Sniklas       return thread;
966e93f7393Sniklas     else
967e93f7393Sniklas       thread = thread->next;
968e93f7393Sniklas   return 0;
969e93f7393Sniklas }
970e93f7393Sniklas 
971e93f7393Sniklas /* Converts a thread port to a struct proc.  */
972e93f7393Sniklas struct proc *
inf_port_to_thread(struct inf * inf,mach_port_t port)973e93f7393Sniklas inf_port_to_thread (struct inf *inf, mach_port_t port)
974e93f7393Sniklas {
975e93f7393Sniklas   struct proc *thread = inf->threads;
976e93f7393Sniklas   while (thread)
977e93f7393Sniklas     if (thread->port == port)
978e93f7393Sniklas       return thread;
979e93f7393Sniklas     else
980e93f7393Sniklas       thread = thread->next;
981e93f7393Sniklas   return 0;
982e93f7393Sniklas }
983b725ae77Skettenis 
984e93f7393Sniklas 
985e93f7393Sniklas /* Make INF's list of threads be consistent with reality of TASK.  */
986e93f7393Sniklas void
inf_validate_procs(struct inf * inf)987e93f7393Sniklas inf_validate_procs (struct inf *inf)
988e93f7393Sniklas {
989e93f7393Sniklas   thread_array_t threads;
990b725ae77Skettenis   mach_msg_type_number_t num_threads, i;
991e93f7393Sniklas   struct proc *task = inf->task;
992e93f7393Sniklas 
993e93f7393Sniklas   /* If no threads are currently running, this function will guarantee that
994e93f7393Sniklas      things are up to date.  The exception is if there are zero threads --
995e93f7393Sniklas      then it is almost certainly in an odd state, and probably some outside
996e93f7393Sniklas      agent will create threads.  */
997e93f7393Sniklas   inf->threads_up_to_date = inf->threads ? !inf->running : 0;
998e93f7393Sniklas 
999e93f7393Sniklas   if (task)
1000e93f7393Sniklas     {
1001e93f7393Sniklas       error_t err = task_threads (task->port, &threads, &num_threads);
1002e93f7393Sniklas       inf_debug (inf, "fetching threads");
1003e93f7393Sniklas       if (err)
1004e93f7393Sniklas 	/* TASK must be dead.  */
1005e93f7393Sniklas 	{
1006e93f7393Sniklas 	  task->dead = 1;
1007e93f7393Sniklas 	  task = 0;
1008e93f7393Sniklas 	}
1009e93f7393Sniklas     }
1010e93f7393Sniklas 
1011e93f7393Sniklas   if (!task)
1012e93f7393Sniklas     {
1013e93f7393Sniklas       num_threads = 0;
1014e93f7393Sniklas       inf_debug (inf, "no task");
1015e93f7393Sniklas     }
1016e93f7393Sniklas 
1017e93f7393Sniklas   {
1018b725ae77Skettenis     /* Make things normally linear.  */
1019b725ae77Skettenis     mach_msg_type_number_t search_start = 0;
1020e93f7393Sniklas     /* Which thread in PROCS corresponds to each task thread, & the task.  */
1021e93f7393Sniklas     struct proc *matched[num_threads + 1];
1022e93f7393Sniklas     /* The last thread in INF->threads, so we can add to the end.  */
1023e93f7393Sniklas     struct proc *last = 0;
1024e93f7393Sniklas     /* The current thread we're considering. */
1025e93f7393Sniklas     struct proc *thread = inf->threads;
1026e93f7393Sniklas 
1027b725ae77Skettenis     memset (matched, 0, sizeof (matched));
1028e93f7393Sniklas 
1029e93f7393Sniklas     while (thread)
1030e93f7393Sniklas       {
1031b725ae77Skettenis 	mach_msg_type_number_t left;
1032e93f7393Sniklas 
1033e93f7393Sniklas 	for (i = search_start, left = num_threads; left; i++, left--)
1034e93f7393Sniklas 	  {
1035e93f7393Sniklas 	    if (i >= num_threads)
1036e93f7393Sniklas 	      i -= num_threads;	/* I wrapped around.  */
1037e93f7393Sniklas 	    if (thread->port == threads[i])
1038e93f7393Sniklas 	      /* We already know about this thread.  */
1039e93f7393Sniklas 	      {
1040e93f7393Sniklas 		matched[i] = thread;
1041e93f7393Sniklas 		last = thread;
1042e93f7393Sniklas 		thread = thread->next;
1043e93f7393Sniklas 		search_start++;
1044e93f7393Sniklas 		break;
1045e93f7393Sniklas 	      }
1046e93f7393Sniklas 	  }
1047e93f7393Sniklas 
1048e93f7393Sniklas 	if (!left)
1049e93f7393Sniklas 	  {
1050e93f7393Sniklas 	    proc_debug (thread, "died!");
1051e93f7393Sniklas 	    thread->port = MACH_PORT_NULL;
1052e93f7393Sniklas 	    thread = _proc_free (thread);	/* THREAD is dead.  */
1053e93f7393Sniklas 	    (last ? last->next : inf->threads) = thread;
1054e93f7393Sniklas 	  }
1055e93f7393Sniklas       }
1056e93f7393Sniklas 
1057e93f7393Sniklas     for (i = 0; i < num_threads; i++)
1058b725ae77Skettenis       {
1059e93f7393Sniklas 	if (matched[i])
1060e93f7393Sniklas 	  /* Throw away the duplicate send right.  */
1061e93f7393Sniklas 	  mach_port_deallocate (mach_task_self (), threads[i]);
1062e93f7393Sniklas 	else
1063e93f7393Sniklas 	  /* THREADS[I] is a thread we don't know about yet!  */
1064e93f7393Sniklas 	  {
1065e93f7393Sniklas 	    thread = make_proc (inf, threads[i], next_thread_id++);
1066e93f7393Sniklas 	    (last ? last->next : inf->threads) = thread;
1067e93f7393Sniklas 	    last = thread;
1068e93f7393Sniklas 	    proc_debug (thread, "new thread: %d", threads[i]);
1069b725ae77Skettenis 	    add_thread (pid_to_ptid (thread->tid));	/* Tell GDB's generic thread code.  */
1070b725ae77Skettenis 	  }
1071e93f7393Sniklas       }
1072e93f7393Sniklas 
1073e93f7393Sniklas     vm_deallocate (mach_task_self (),
1074e93f7393Sniklas 		   (vm_address_t) threads, (num_threads * sizeof (thread_t)));
1075e93f7393Sniklas   }
1076e93f7393Sniklas }
1077b725ae77Skettenis 
1078e93f7393Sniklas 
1079e93f7393Sniklas /* Makes sure that INF's thread list is synced with the actual process.  */
1080b725ae77Skettenis int
inf_update_procs(struct inf * inf)1081e93f7393Sniklas inf_update_procs (struct inf *inf)
1082e93f7393Sniklas {
1083e93f7393Sniklas   if (!inf->task)
1084e93f7393Sniklas     return 0;
1085e93f7393Sniklas   if (!inf->threads_up_to_date)
1086e93f7393Sniklas     inf_validate_procs (inf);
1087e93f7393Sniklas   return !!inf->task;
1088e93f7393Sniklas }
1089e93f7393Sniklas 
1090e93f7393Sniklas /* Sets the resume_sc of each thread in inf.  That of RUN_THREAD is set to 0,
1091e93f7393Sniklas    and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1092e93f7393Sniklas    their pause_sc.  */
1093b725ae77Skettenis void
inf_set_threads_resume_sc(struct inf * inf,struct proc * run_thread,int run_others)1094e93f7393Sniklas inf_set_threads_resume_sc (struct inf *inf,
1095e93f7393Sniklas 			   struct proc *run_thread, int run_others)
1096e93f7393Sniklas {
1097e93f7393Sniklas   struct proc *thread;
1098e93f7393Sniklas   inf_update_procs (inf);
1099e93f7393Sniklas   for (thread = inf->threads; thread; thread = thread->next)
1100e93f7393Sniklas     if (thread == run_thread)
1101e93f7393Sniklas       thread->resume_sc = 0;
1102e93f7393Sniklas     else if (run_others)
1103e93f7393Sniklas       thread->resume_sc = thread->run_sc;
1104e93f7393Sniklas     else
1105e93f7393Sniklas       thread->resume_sc = thread->pause_sc;
1106e93f7393Sniklas }
1107b725ae77Skettenis 
1108e93f7393Sniklas 
1109e93f7393Sniklas /* Cause INF to continue execution immediately; individual threads may still
1110e93f7393Sniklas    be suspended (but their suspend counts will be updated).  */
1111b725ae77Skettenis void
inf_resume(struct inf * inf)1112e93f7393Sniklas inf_resume (struct inf *inf)
1113e93f7393Sniklas {
1114e93f7393Sniklas   struct proc *thread;
1115e93f7393Sniklas 
1116e93f7393Sniklas   inf_update_procs (inf);
1117e93f7393Sniklas 
1118e93f7393Sniklas   for (thread = inf->threads; thread; thread = thread->next)
1119e93f7393Sniklas     thread->sc = thread->resume_sc;
1120e93f7393Sniklas 
1121e93f7393Sniklas   if (inf->task)
1122e93f7393Sniklas     {
1123e93f7393Sniklas       if (!inf->pending_execs)
1124e93f7393Sniklas 	/* Try to make sure our task count is correct -- in the case where
1125e93f7393Sniklas 	   we're waiting for an exec though, things are too volatile, so just
1126e93f7393Sniklas 	   assume things will be reasonable (which they usually will be).  */
1127e93f7393Sniklas 	inf_validate_task_sc (inf);
1128e93f7393Sniklas       inf->task->sc = 0;
1129e93f7393Sniklas     }
1130e93f7393Sniklas 
1131e93f7393Sniklas   inf_update_suspends (inf);
1132e93f7393Sniklas }
1133e93f7393Sniklas 
1134e93f7393Sniklas /* Cause INF to stop execution immediately; individual threads may still
1135e93f7393Sniklas    be running.  */
1136b725ae77Skettenis void
inf_suspend(struct inf * inf)1137e93f7393Sniklas inf_suspend (struct inf *inf)
1138e93f7393Sniklas {
1139e93f7393Sniklas   struct proc *thread;
1140e93f7393Sniklas 
1141e93f7393Sniklas   inf_update_procs (inf);
1142e93f7393Sniklas 
1143e93f7393Sniklas   for (thread = inf->threads; thread; thread = thread->next)
1144e93f7393Sniklas     thread->sc = thread->pause_sc;
1145e93f7393Sniklas 
1146e93f7393Sniklas   if (inf->task)
1147e93f7393Sniklas     inf->task->sc = inf->pause_sc;
1148e93f7393Sniklas 
1149e93f7393Sniklas   inf_update_suspends (inf);
1150e93f7393Sniklas }
1151b725ae77Skettenis 
1152e93f7393Sniklas 
1153b725ae77Skettenis /* INF has one thread PROC that is in single-stepping mode.  This
1154b725ae77Skettenis    function changes it to be PROC, changing any old step_thread to be
1155b725ae77Skettenis    a normal one.  A PROC of 0 clears any existing value.  */
1156e93f7393Sniklas void
inf_set_step_thread(struct inf * inf,struct proc * thread)1157e93f7393Sniklas inf_set_step_thread (struct inf *inf, struct proc *thread)
1158e93f7393Sniklas {
1159b725ae77Skettenis   gdb_assert (!thread || proc_is_thread (thread));
1160e93f7393Sniklas 
1161e93f7393Sniklas   if (thread)
1162e93f7393Sniklas     inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1163e93f7393Sniklas   else
1164e93f7393Sniklas     inf_debug (inf, "clearing step thread");
1165e93f7393Sniklas 
1166e93f7393Sniklas   if (inf->step_thread != thread)
1167e93f7393Sniklas     {
1168e93f7393Sniklas       if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1169e93f7393Sniklas 	if (!proc_trace (inf->step_thread, 0))
1170e93f7393Sniklas 	  return;
1171e93f7393Sniklas       if (thread && proc_trace (thread, 1))
1172e93f7393Sniklas 	inf->step_thread = thread;
1173e93f7393Sniklas       else
1174e93f7393Sniklas 	inf->step_thread = 0;
1175e93f7393Sniklas     }
1176e93f7393Sniklas }
1177b725ae77Skettenis 
1178e93f7393Sniklas 
1179e93f7393Sniklas /* Set up the thread resume_sc's so that only the signal thread is running
1180e93f7393Sniklas    (plus whatever other thread are set to always run).  Returns true if we
1181e93f7393Sniklas    did so, or false if we can't find a signal thread.  */
1182b725ae77Skettenis int
inf_set_threads_resume_sc_for_signal_thread(struct inf * inf)1183e93f7393Sniklas inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1184e93f7393Sniklas {
1185e93f7393Sniklas   if (inf->signal_thread)
1186e93f7393Sniklas     {
1187e93f7393Sniklas       inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1188e93f7393Sniklas       return 1;
1189e93f7393Sniklas     }
1190e93f7393Sniklas   else
1191e93f7393Sniklas     return 0;
1192e93f7393Sniklas }
1193e93f7393Sniklas 
1194e93f7393Sniklas static void
inf_update_signal_thread(struct inf * inf)1195e93f7393Sniklas inf_update_signal_thread (struct inf *inf)
1196e93f7393Sniklas {
1197e93f7393Sniklas   /* XXX for now we assume that if there's a msgport, the 2nd thread is
1198e93f7393Sniklas      the signal thread.  */
1199e93f7393Sniklas   inf->signal_thread = inf->threads ? inf->threads->next : 0;
1200e93f7393Sniklas }
1201b725ae77Skettenis 
1202e93f7393Sniklas 
1203e93f7393Sniklas /* Detachs from INF's inferior task, letting it run once again...  */
1204e93f7393Sniklas void
inf_detach(struct inf * inf)1205e93f7393Sniklas inf_detach (struct inf *inf)
1206e93f7393Sniklas {
1207e93f7393Sniklas   struct proc *task = inf->task;
1208e93f7393Sniklas 
1209e93f7393Sniklas   inf_debug (inf, "detaching...");
1210e93f7393Sniklas 
1211e93f7393Sniklas   inf_clear_wait (inf);
1212e93f7393Sniklas   inf_set_step_thread (inf, 0);
1213e93f7393Sniklas 
1214e93f7393Sniklas   if (task)
1215e93f7393Sniklas     {
1216e93f7393Sniklas       struct proc *thread;
1217e93f7393Sniklas 
1218b725ae77Skettenis       inf_validate_procinfo (inf);
1219b725ae77Skettenis 
1220e93f7393Sniklas       inf_set_traced (inf, 0);
1221e93f7393Sniklas       if (inf->stopped)
1222b725ae77Skettenis 	{
1223b725ae77Skettenis 	  if (inf->nomsg)
1224b725ae77Skettenis 	    inf_continue (inf);
1225b725ae77Skettenis 	  else
1226e93f7393Sniklas 	    inf_signal (inf, TARGET_SIGNAL_0);
1227b725ae77Skettenis 	}
1228e93f7393Sniklas 
1229e93f7393Sniklas       proc_restore_exc_port (task);
1230e93f7393Sniklas       task->sc = inf->detach_sc;
1231e93f7393Sniklas 
1232e93f7393Sniklas       for (thread = inf->threads; thread; thread = thread->next)
1233e93f7393Sniklas 	{
1234e93f7393Sniklas 	  proc_restore_exc_port (thread);
1235e93f7393Sniklas 	  thread->sc = thread->detach_sc;
1236e93f7393Sniklas 	}
1237e93f7393Sniklas 
1238e93f7393Sniklas       inf_update_suspends (inf);
1239e93f7393Sniklas     }
1240e93f7393Sniklas 
1241e93f7393Sniklas   inf_cleanup (inf);
1242e93f7393Sniklas }
1243e93f7393Sniklas 
1244b725ae77Skettenis /* Attaches INF to the process with process id PID, returning it in a
1245b725ae77Skettenis    suspended state suitable for debugging.  */
1246e93f7393Sniklas void
inf_attach(struct inf * inf,int pid)1247e93f7393Sniklas inf_attach (struct inf *inf, int pid)
1248e93f7393Sniklas {
1249e93f7393Sniklas   inf_debug (inf, "attaching: %d", pid);
1250e93f7393Sniklas 
1251e93f7393Sniklas   if (inf->pid)
1252e93f7393Sniklas     inf_detach (inf);
1253e93f7393Sniklas 
1254b725ae77Skettenis   inf_startup (inf, pid);
1255e93f7393Sniklas }
1256b725ae77Skettenis 
1257e93f7393Sniklas 
1258e93f7393Sniklas /* Makes sure that we've got our exception ports entrenched in the process. */
1259b725ae77Skettenis void
inf_steal_exc_ports(struct inf * inf)1260b725ae77Skettenis inf_steal_exc_ports (struct inf *inf)
1261e93f7393Sniklas {
1262e93f7393Sniklas   struct proc *thread;
1263e93f7393Sniklas 
1264e93f7393Sniklas   inf_debug (inf, "stealing exception ports");
1265e93f7393Sniklas 
1266e93f7393Sniklas   inf_set_step_thread (inf, 0);	/* The step thread is special. */
1267e93f7393Sniklas 
1268e93f7393Sniklas   proc_steal_exc_port (inf->task, inf->event_port);
1269e93f7393Sniklas   for (thread = inf->threads; thread; thread = thread->next)
1270e93f7393Sniklas     proc_steal_exc_port (thread, MACH_PORT_NULL);
1271e93f7393Sniklas }
1272e93f7393Sniklas 
1273e93f7393Sniklas /* Makes sure the process has its own exception ports.  */
1274b725ae77Skettenis void
inf_restore_exc_ports(struct inf * inf)1275b725ae77Skettenis inf_restore_exc_ports (struct inf *inf)
1276e93f7393Sniklas {
1277e93f7393Sniklas   struct proc *thread;
1278e93f7393Sniklas 
1279e93f7393Sniklas   inf_debug (inf, "restoring exception ports");
1280e93f7393Sniklas 
1281e93f7393Sniklas   inf_set_step_thread (inf, 0);	/* The step thread is special. */
1282e93f7393Sniklas 
1283e93f7393Sniklas   proc_restore_exc_port (inf->task);
1284e93f7393Sniklas   for (thread = inf->threads; thread; thread = thread->next)
1285e93f7393Sniklas     proc_restore_exc_port (thread);
1286e93f7393Sniklas }
1287b725ae77Skettenis 
1288e93f7393Sniklas 
1289e93f7393Sniklas /* Deliver signal SIG to INF.  If INF is stopped, delivering a signal, even
1290e93f7393Sniklas    signal 0, will continue it.  INF is assumed to be in a paused state, and
1291e93f7393Sniklas    the resume_sc's of INF's threads may be affected.  */
1292e93f7393Sniklas void
inf_signal(struct inf * inf,enum target_signal sig)1293e93f7393Sniklas inf_signal (struct inf *inf, enum target_signal sig)
1294e93f7393Sniklas {
1295e93f7393Sniklas   error_t err = 0;
1296e93f7393Sniklas   int host_sig = target_signal_to_host (sig);
1297e93f7393Sniklas 
1298e93f7393Sniklas #define NAME target_signal_to_name (sig)
1299e93f7393Sniklas 
1300e93f7393Sniklas   if (host_sig >= _NSIG)
1301e93f7393Sniklas     /* A mach exception.  Exceptions are encoded in the signal space by
1302e93f7393Sniklas        putting them after _NSIG; this assumes they're positive (and not
1303e93f7393Sniklas        extremely large)!  */
1304e93f7393Sniklas     {
1305e93f7393Sniklas       struct inf_wait *w = &inf->wait;
1306e93f7393Sniklas       if (w->status.kind == TARGET_WAITKIND_STOPPED
1307e93f7393Sniklas 	  && w->status.value.sig == sig
1308e93f7393Sniklas 	  && w->thread && !w->thread->aborted)
1309e93f7393Sniklas 	/* We're passing through the last exception we received.  This is
1310e93f7393Sniklas 	   kind of bogus, because exceptions are per-thread whereas gdb
1311e93f7393Sniklas 	   treats signals as per-process.  We just forward the exception to
1312e93f7393Sniklas 	   the correct handler, even it's not for the same thread as TID --
1313e93f7393Sniklas 	   i.e., we pretend it's global.  */
1314e93f7393Sniklas 	{
1315e93f7393Sniklas 	  struct exc_state *e = &w->exc;
1316e93f7393Sniklas 	  inf_debug (inf, "passing through exception:"
1317e93f7393Sniklas 		     " task = %d, thread = %d, exc = %d"
1318e93f7393Sniklas 		     ", code = %d, subcode = %d",
1319e93f7393Sniklas 		     w->thread->port, inf->task->port,
1320e93f7393Sniklas 		     e->exception, e->code, e->subcode);
1321e93f7393Sniklas 	  err =
1322e93f7393Sniklas 	    exception_raise_request (e->handler,
1323e93f7393Sniklas 				     e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1324e93f7393Sniklas 				     w->thread->port, inf->task->port,
1325e93f7393Sniklas 				     e->exception, e->code, e->subcode);
1326e93f7393Sniklas 	}
1327e93f7393Sniklas       else
1328e93f7393Sniklas 	error ("Can't forward spontaneous exception (%s).", NAME);
1329e93f7393Sniklas     }
1330e93f7393Sniklas   else
1331e93f7393Sniklas     /* A Unix signal.  */
1332e93f7393Sniklas   if (inf->stopped)
1333b725ae77Skettenis     /* The process is stopped and expecting a signal.  Just send off a
1334e93f7393Sniklas        request and let it get handled when we resume everything.  */
1335e93f7393Sniklas     {
1336e93f7393Sniklas       inf_debug (inf, "sending %s to stopped process", NAME);
1337e93f7393Sniklas       err =
1338e93f7393Sniklas 	INF_MSGPORT_RPC (inf,
1339e93f7393Sniklas 			 msg_sig_post_untraced_request (msgport,
1340e93f7393Sniklas 							inf->event_port,
1341e93f7393Sniklas 					       MACH_MSG_TYPE_MAKE_SEND_ONCE,
1342e93f7393Sniklas 							host_sig, 0,
1343e93f7393Sniklas 							refport));
1344e93f7393Sniklas       if (!err)
1345e93f7393Sniklas 	/* Posting an untraced signal automatically continues it.
1346e93f7393Sniklas 	   We clear this here rather than when we get the reply
1347e93f7393Sniklas 	   because we'd rather assume it's not stopped when it
1348e93f7393Sniklas 	   actually is, than the reverse.  */
1349e93f7393Sniklas 	inf->stopped = 0;
1350e93f7393Sniklas     }
1351e93f7393Sniklas   else
1352e93f7393Sniklas     /* It's not expecting it.  We have to let just the signal thread
1353e93f7393Sniklas        run, and wait for it to get into a reasonable state before we
1354e93f7393Sniklas        can continue the rest of the process.  When we finally resume the
1355e93f7393Sniklas        process the signal we request will be the very first thing that
1356e93f7393Sniklas        happens. */
1357e93f7393Sniklas     {
1358b725ae77Skettenis       inf_debug (inf, "sending %s to unstopped process"
1359b725ae77Skettenis 		 " (so resuming signal thread)", NAME);
1360e93f7393Sniklas       err =
1361b725ae77Skettenis 	INF_RESUME_MSGPORT_RPC (inf,
1362b725ae77Skettenis 				msg_sig_post_untraced (msgport, host_sig,
1363b725ae77Skettenis 						       0, refport));
1364e93f7393Sniklas     }
1365e93f7393Sniklas 
1366e93f7393Sniklas   if (err == EIEIO)
1367e93f7393Sniklas     /* Can't do too much... */
1368e93f7393Sniklas     warning ("Can't deliver signal %s: No signal thread.", NAME);
1369e93f7393Sniklas   else if (err)
1370b725ae77Skettenis     warning ("Delivering signal %s: %s", NAME, safe_strerror (err));
1371e93f7393Sniklas 
1372e93f7393Sniklas #undef NAME
1373e93f7393Sniklas }
1374b725ae77Skettenis 
1375b725ae77Skettenis 
1376b725ae77Skettenis /* Continue INF without delivering a signal.  This is meant to be used
1377b725ae77Skettenis    when INF does not have a message port.  */
1378b725ae77Skettenis void
inf_continue(struct inf * inf)1379b725ae77Skettenis inf_continue (struct inf *inf)
1380b725ae77Skettenis {
1381b725ae77Skettenis   process_t proc;
1382b725ae77Skettenis   error_t err = proc_pid2proc (proc_server, inf->pid, &proc);
1383b725ae77Skettenis 
1384b725ae77Skettenis   if (!err)
1385b725ae77Skettenis     {
1386b725ae77Skettenis       inf_debug (inf, "continuing process");
1387b725ae77Skettenis 
1388b725ae77Skettenis       err = proc_mark_cont (proc);
1389b725ae77Skettenis       if (!err)
1390b725ae77Skettenis 	{
1391b725ae77Skettenis 	  struct proc *thread;
1392b725ae77Skettenis 
1393b725ae77Skettenis 	  for (thread = inf->threads; thread; thread = thread->next)
1394b725ae77Skettenis 	    thread_resume (thread->port);
1395b725ae77Skettenis 
1396b725ae77Skettenis 	  inf->stopped = 0;
1397b725ae77Skettenis 	}
1398b725ae77Skettenis     }
1399b725ae77Skettenis 
1400b725ae77Skettenis   if (err)
1401b725ae77Skettenis     warning ("Can't continue process: %s", safe_strerror (err));
1402b725ae77Skettenis }
1403b725ae77Skettenis 
1404e93f7393Sniklas 
1405e93f7393Sniklas /* The inferior used for all gdb target ops.  */
1406e93f7393Sniklas struct inf *current_inferior = 0;
1407e93f7393Sniklas 
1408e93f7393Sniklas /* The inferior being waited for by gnu_wait.  Since GDB is decidely not
1409e93f7393Sniklas    multi-threaded, we don't bother to lock this.  */
1410e93f7393Sniklas struct inf *waiting_inf;
1411e93f7393Sniklas 
1412e93f7393Sniklas /* Wait for something to happen in the inferior, returning what in STATUS. */
1413b725ae77Skettenis static ptid_t
gnu_wait(ptid_t tid,struct target_waitstatus * status)1414b725ae77Skettenis gnu_wait (ptid_t tid, struct target_waitstatus *status)
1415e93f7393Sniklas {
1416b725ae77Skettenis   struct msg
1417b725ae77Skettenis     {
1418e93f7393Sniklas       mach_msg_header_t hdr;
1419e93f7393Sniklas       mach_msg_type_t type;
1420e93f7393Sniklas       int data[8000];
1421e93f7393Sniklas     } msg;
1422e93f7393Sniklas   error_t err;
1423e93f7393Sniklas   struct proc *thread;
1424e93f7393Sniklas   struct inf *inf = current_inferior;
1425e93f7393Sniklas 
1426b725ae77Skettenis   extern int exc_server (mach_msg_header_t *, mach_msg_header_t *);
1427b725ae77Skettenis   extern int msg_reply_server (mach_msg_header_t *, mach_msg_header_t *);
1428b725ae77Skettenis   extern int notify_server (mach_msg_header_t *, mach_msg_header_t *);
1429b725ae77Skettenis   extern int process_reply_server (mach_msg_header_t *, mach_msg_header_t *);
1430b725ae77Skettenis 
1431b725ae77Skettenis   gdb_assert (inf->task);
1432e93f7393Sniklas 
1433e93f7393Sniklas   if (!inf->threads && !inf->pending_execs)
1434e93f7393Sniklas     /* No threads!  Assume that maybe some outside agency is frobbing our
1435e93f7393Sniklas        task, and really look for new threads.  If we can't find any, just tell
1436e93f7393Sniklas        the user to try again later.  */
1437e93f7393Sniklas     {
1438e93f7393Sniklas       inf_validate_procs (inf);
1439e93f7393Sniklas       if (!inf->threads && !inf->task->dead)
1440e93f7393Sniklas 	error ("There are no threads; try again later.");
1441e93f7393Sniklas     }
1442e93f7393Sniklas 
1443e93f7393Sniklas   waiting_inf = inf;
1444e93f7393Sniklas 
1445b725ae77Skettenis   inf_debug (inf, "waiting for: %d", PIDGET (tid));
1446e93f7393Sniklas 
1447e93f7393Sniklas rewait:
1448e93f7393Sniklas   if (proc_wait_pid != inf->pid && !inf->no_wait)
1449e93f7393Sniklas     /* Always get information on events from the proc server.  */
1450e93f7393Sniklas     {
1451e93f7393Sniklas       inf_debug (inf, "requesting wait on pid %d", inf->pid);
1452e93f7393Sniklas 
1453e93f7393Sniklas       if (proc_wait_pid)
1454e93f7393Sniklas 	/* The proc server is single-threaded, and only allows a single
1455e93f7393Sniklas 	   outstanding wait request, so we have to cancel the previous one. */
1456e93f7393Sniklas 	{
1457e93f7393Sniklas 	  inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1458e93f7393Sniklas 	  interrupt_operation (proc_server, 0);
1459e93f7393Sniklas 	}
1460e93f7393Sniklas 
1461e93f7393Sniklas       err =
1462e93f7393Sniklas 	proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1463e93f7393Sniklas       if (err)
1464b725ae77Skettenis 	warning ("wait request failed: %s", safe_strerror (err));
1465e93f7393Sniklas       else
1466e93f7393Sniklas 	{
1467e93f7393Sniklas 	  inf_debug (inf, "waits pending: %d", proc_waits_pending);
1468e93f7393Sniklas 	  proc_wait_pid = inf->pid;
1469b725ae77Skettenis 	  /* Even if proc_waits_pending was > 0 before, we still won't
1470b725ae77Skettenis 	     get any other replies, because it was either from a
1471b725ae77Skettenis 	     different INF, or a different process attached to INF --
1472b725ae77Skettenis 	     and the event port, which is the wait reply port, changes
1473b725ae77Skettenis 	     when you switch processes. */
1474e93f7393Sniklas 	  proc_waits_pending = 1;
1475e93f7393Sniklas 	}
1476e93f7393Sniklas     }
1477e93f7393Sniklas 
1478e93f7393Sniklas   inf_clear_wait (inf);
1479e93f7393Sniklas 
1480e93f7393Sniklas   /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1481e93f7393Sniklas      (3) wait reply from the proc server.  */
1482e93f7393Sniklas 
1483e93f7393Sniklas   inf_debug (inf, "waiting for an event...");
1484e93f7393Sniklas   err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
1485e93f7393Sniklas 		  0, sizeof (struct msg), inf->event_port,
1486e93f7393Sniklas 		  MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1487e93f7393Sniklas 
1488e93f7393Sniklas   /* Re-suspend the task.  */
1489e93f7393Sniklas   inf_suspend (inf);
1490e93f7393Sniklas 
1491b725ae77Skettenis   if (!inf->task && inf->pending_execs)
1492b725ae77Skettenis     /* When doing an exec, it's possible that the old task wasn't reused
1493b725ae77Skettenis        (e.g., setuid execs).  So if the task seems to have disappeared,
1494b725ae77Skettenis        attempt to refetch it, as the pid should still be the same.  */
1495b725ae77Skettenis     inf_set_pid (inf, inf->pid);
1496b725ae77Skettenis 
1497e93f7393Sniklas   if (err == EMACH_RCV_INTERRUPTED)
1498e93f7393Sniklas     inf_debug (inf, "interrupted");
1499e93f7393Sniklas   else if (err)
1500b725ae77Skettenis     error ("Couldn't wait for an event: %s", safe_strerror (err));
1501e93f7393Sniklas   else
1502e93f7393Sniklas     {
1503b725ae77Skettenis       struct
1504b725ae77Skettenis 	{
1505e93f7393Sniklas 	  mach_msg_header_t hdr;
1506e93f7393Sniklas 	  mach_msg_type_t err_type;
1507e93f7393Sniklas 	  kern_return_t err;
1508e93f7393Sniklas 	  char noise[200];
1509b725ae77Skettenis 	}
1510b725ae77Skettenis       reply;
1511e93f7393Sniklas 
1512e93f7393Sniklas       inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1513e93f7393Sniklas 
1514e93f7393Sniklas       /* Handle what we got.  */
1515e93f7393Sniklas       if (!notify_server (&msg.hdr, &reply.hdr)
1516e93f7393Sniklas 	  && !exc_server (&msg.hdr, &reply.hdr)
1517e93f7393Sniklas 	  && !process_reply_server (&msg.hdr, &reply.hdr)
1518e93f7393Sniklas 	  && !msg_reply_server (&msg.hdr, &reply.hdr))
1519e93f7393Sniklas 	/* Whatever it is, it's something strange.  */
1520e93f7393Sniklas 	error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id);
1521e93f7393Sniklas 
1522e93f7393Sniklas       if (reply.err)
1523e93f7393Sniklas 	error ("Handling event, msgid = %d: %s",
1524b725ae77Skettenis 	       msg.hdr.msgh_id, safe_strerror (reply.err));
1525e93f7393Sniklas     }
1526e93f7393Sniklas 
1527e93f7393Sniklas   if (inf->pending_execs)
1528e93f7393Sniklas     /* We're waiting for the inferior to finish execing.  */
1529e93f7393Sniklas     {
1530e93f7393Sniklas       struct inf_wait *w = &inf->wait;
1531e93f7393Sniklas       enum target_waitkind kind = w->status.kind;
1532e93f7393Sniklas 
1533e93f7393Sniklas       if (kind == TARGET_WAITKIND_SPURIOUS)
1534e93f7393Sniklas 	/* Since gdb is actually counting the number of times the inferior
1535e93f7393Sniklas 	   stops, expecting one stop per exec, we only return major events
1536e93f7393Sniklas 	   while execing.  */
1537e93f7393Sniklas 	{
1538e93f7393Sniklas 	  w->suppress = 1;
1539e93f7393Sniklas 	  inf_debug (inf, "pending_execs = %d, ignoring minor event",
1540e93f7393Sniklas 		     inf->pending_execs);
1541e93f7393Sniklas 	}
1542e93f7393Sniklas       else if (kind == TARGET_WAITKIND_STOPPED
1543e93f7393Sniklas 	       && w->status.value.sig == TARGET_SIGNAL_TRAP)
1544e93f7393Sniklas 	/* Ah hah!  A SIGTRAP from the inferior while starting up probably
1545e93f7393Sniklas 	   means we've succesfully completed an exec!  */
1546e93f7393Sniklas 	{
1547e93f7393Sniklas 	  if (--inf->pending_execs == 0)
1548e93f7393Sniklas 	    /* We're done!  */
1549e93f7393Sniklas 	    {
1550b725ae77Skettenis #if 0				/* do we need this? */
1551e93f7393Sniklas 	      prune_threads (1);	/* Get rid of the old shell threads */
1552e93f7393Sniklas 	      renumber_threads (0);	/* Give our threads reasonable names. */
1553b725ae77Skettenis #endif
1554e93f7393Sniklas 	    }
1555e93f7393Sniklas 	  inf_debug (inf, "pending exec completed, pending_execs => %d",
1556e93f7393Sniklas 		     inf->pending_execs);
1557e93f7393Sniklas 	}
1558b725ae77Skettenis       else if (kind == TARGET_WAITKIND_STOPPED)
1559b725ae77Skettenis 	/* It's possible that this signal is because of a crashed process
1560b725ae77Skettenis 	   being handled by the hurd crash server; in this case, the process
1561b725ae77Skettenis 	   will have an extra task suspend, which we need to know about.
1562b725ae77Skettenis 	   Since the code in inf_resume that normally checks for this is
1563b725ae77Skettenis 	   disabled while INF->pending_execs, we do the check here instead.  */
1564b725ae77Skettenis 	inf_validate_task_sc (inf);
1565e93f7393Sniklas     }
1566e93f7393Sniklas 
1567e93f7393Sniklas   if (inf->wait.suppress)
1568e93f7393Sniklas     /* Some totally spurious event happened that we don't consider
1569e93f7393Sniklas        worth returning to gdb.  Just keep waiting.  */
1570e93f7393Sniklas     {
1571e93f7393Sniklas       inf_debug (inf, "suppressing return, rewaiting...");
1572e93f7393Sniklas       inf_resume (inf);
1573e93f7393Sniklas       goto rewait;
1574e93f7393Sniklas     }
1575e93f7393Sniklas 
1576e93f7393Sniklas   /* Pass back out our results.  */
1577*63addd46Skettenis   memcpy (status, &inf->wait.status, sizeof (*status));
1578e93f7393Sniklas 
1579e93f7393Sniklas   thread = inf->wait.thread;
1580e93f7393Sniklas   if (thread)
1581b725ae77Skettenis     tid = pid_to_ptid (thread->tid);
1582e93f7393Sniklas   else
1583b725ae77Skettenis     thread = inf_tid_to_thread (inf, PIDGET (tid));
1584e93f7393Sniklas 
1585e93f7393Sniklas   if (!thread || thread->port == MACH_PORT_NULL)
1586b725ae77Skettenis     {
1587e93f7393Sniklas       /* TID is dead; try and find a new thread.  */
1588e93f7393Sniklas       if (inf_update_procs (inf) && inf->threads)
1589b725ae77Skettenis 	tid = pid_to_ptid (inf->threads->tid); /* The first available thread.  */
1590e93f7393Sniklas       else
1591b725ae77Skettenis 	tid = inferior_ptid;	/* let wait_for_inferior handle exit case */
1592b725ae77Skettenis     }
1593e93f7393Sniklas 
1594b725ae77Skettenis   if (thread && PIDGET (tid) >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS
1595e93f7393Sniklas       && inf->pause_sc == 0 && thread->pause_sc == 0)
1596b725ae77Skettenis     /* If something actually happened to THREAD, make sure we
1597b725ae77Skettenis        suspend it.  */
1598e93f7393Sniklas     {
1599e93f7393Sniklas       thread->sc = 1;
1600e93f7393Sniklas       inf_update_suspends (inf);
1601e93f7393Sniklas     }
1602e93f7393Sniklas 
1603b725ae77Skettenis   inf_debug (inf, "returning tid = %d, status = %s (%d)", PIDGET (tid),
1604e93f7393Sniklas 	     status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
1605e93f7393Sniklas 	     : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
1606e93f7393Sniklas 	     : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
1607e93f7393Sniklas 	     : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
1608e93f7393Sniklas 	     : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
1609e93f7393Sniklas 	     : "?",
1610e93f7393Sniklas 	     status->value.integer);
1611e93f7393Sniklas 
1612e93f7393Sniklas   return tid;
1613e93f7393Sniklas }
1614b725ae77Skettenis 
1615e93f7393Sniklas 
1616e93f7393Sniklas /* The rpc handler called by exc_server.  */
1617e93f7393Sniklas error_t
S_exception_raise_request(mach_port_t port,mach_port_t reply_port,thread_t thread_port,task_t task_port,int exception,int code,int subcode)1618e93f7393Sniklas S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1619e93f7393Sniklas 			   thread_t thread_port, task_t task_port,
1620e93f7393Sniklas 			   int exception, int code, int subcode)
1621e93f7393Sniklas {
1622e93f7393Sniklas   struct inf *inf = waiting_inf;
1623e93f7393Sniklas   struct proc *thread = inf_port_to_thread (inf, thread_port);
1624e93f7393Sniklas 
1625e93f7393Sniklas   inf_debug (waiting_inf,
1626e93f7393Sniklas 	     "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1627b725ae77Skettenis 	     thread_port, task_port, exception, code, subcode);
1628e93f7393Sniklas 
1629e93f7393Sniklas   if (!thread)
1630e93f7393Sniklas     /* We don't know about thread?  */
1631e93f7393Sniklas     {
1632e93f7393Sniklas       inf_update_procs (inf);
1633e93f7393Sniklas       thread = inf_port_to_thread (inf, thread_port);
1634e93f7393Sniklas       if (!thread)
1635e93f7393Sniklas 	/* Give up, the generating thread is gone.  */
1636e93f7393Sniklas 	return 0;
1637e93f7393Sniklas     }
1638e93f7393Sniklas 
1639e93f7393Sniklas   mach_port_deallocate (mach_task_self (), thread_port);
1640e93f7393Sniklas   mach_port_deallocate (mach_task_self (), task_port);
1641e93f7393Sniklas 
1642e93f7393Sniklas   if (!thread->aborted)
1643e93f7393Sniklas     /* THREAD hasn't been aborted since this exception happened (abortion
1644e93f7393Sniklas        clears any exception state), so it must be real.  */
1645e93f7393Sniklas     {
1646e93f7393Sniklas       /* Store away the details; this will destroy any previous info.  */
1647e93f7393Sniklas       inf->wait.thread = thread;
1648e93f7393Sniklas 
1649e93f7393Sniklas       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1650e93f7393Sniklas 
1651e93f7393Sniklas       if (exception == EXC_BREAKPOINT)
1652e93f7393Sniklas 	/* GDB likes to get SIGTRAP for breakpoints.  */
1653e93f7393Sniklas 	{
1654e93f7393Sniklas 	  inf->wait.status.value.sig = TARGET_SIGNAL_TRAP;
1655e93f7393Sniklas 	  mach_port_deallocate (mach_task_self (), reply_port);
1656e93f7393Sniklas 	}
1657e93f7393Sniklas       else
1658e93f7393Sniklas 	/* Record the exception so that we can forward it later.  */
1659e93f7393Sniklas 	{
1660e93f7393Sniklas 	  if (thread->exc_port == port)
1661e93f7393Sniklas 	    {
1662b725ae77Skettenis 	      inf_debug (waiting_inf, "Handler is thread exception port <%d>",
1663e93f7393Sniklas 			 thread->saved_exc_port);
1664e93f7393Sniklas 	      inf->wait.exc.handler = thread->saved_exc_port;
1665e93f7393Sniklas 	    }
1666e93f7393Sniklas 	  else
1667e93f7393Sniklas 	    {
1668b725ae77Skettenis 	      inf_debug (waiting_inf, "Handler is task exception port <%d>",
1669e93f7393Sniklas 			 inf->task->saved_exc_port);
1670e93f7393Sniklas 	      inf->wait.exc.handler = inf->task->saved_exc_port;
1671b725ae77Skettenis 	      gdb_assert (inf->task->exc_port == port);
1672e93f7393Sniklas 	    }
1673e93f7393Sniklas 	  if (inf->wait.exc.handler != MACH_PORT_NULL)
1674e93f7393Sniklas 	    /* Add a reference to the exception handler. */
1675e93f7393Sniklas 	    mach_port_mod_refs (mach_task_self (),
1676e93f7393Sniklas 				inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1677e93f7393Sniklas 				1);
1678e93f7393Sniklas 
1679e93f7393Sniklas 	  inf->wait.exc.exception = exception;
1680e93f7393Sniklas 	  inf->wait.exc.code = code;
1681e93f7393Sniklas 	  inf->wait.exc.subcode = subcode;
1682e93f7393Sniklas 	  inf->wait.exc.reply = reply_port;
1683e93f7393Sniklas 
1684e93f7393Sniklas 	  /* Exceptions are encoded in the signal space by putting them after
1685e93f7393Sniklas 	     _NSIG; this assumes they're positive (and not extremely large)! */
1686e93f7393Sniklas 	  inf->wait.status.value.sig =
1687e93f7393Sniklas 	    target_signal_from_host (_NSIG + exception);
1688e93f7393Sniklas 	}
1689e93f7393Sniklas     }
1690e93f7393Sniklas   else
1691e93f7393Sniklas     /* A supppressed exception, which ignore.  */
1692e93f7393Sniklas     {
1693e93f7393Sniklas       inf->wait.suppress = 1;
1694e93f7393Sniklas       mach_port_deallocate (mach_task_self (), reply_port);
1695e93f7393Sniklas     }
1696e93f7393Sniklas 
1697e93f7393Sniklas   return 0;
1698e93f7393Sniklas }
1699b725ae77Skettenis 
1700e93f7393Sniklas 
1701e93f7393Sniklas /* Fill in INF's wait field after a task has died without giving us more
1702e93f7393Sniklas    detailed information.  */
1703e93f7393Sniklas void
inf_task_died_status(struct inf * inf)1704e93f7393Sniklas inf_task_died_status (struct inf *inf)
1705e93f7393Sniklas {
1706e93f7393Sniklas   warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid);
1707e93f7393Sniklas   inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
1708e93f7393Sniklas   inf->wait.status.value.sig = TARGET_SIGNAL_KILL;
1709e93f7393Sniklas }
1710e93f7393Sniklas 
1711e93f7393Sniklas /* Notify server routines.  The only real one is dead name notification.  */
1712e93f7393Sniklas error_t
do_mach_notify_dead_name(mach_port_t notify,mach_port_t dead_port)1713e93f7393Sniklas do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1714e93f7393Sniklas {
1715e93f7393Sniklas   struct inf *inf = waiting_inf;
1716e93f7393Sniklas 
1717e93f7393Sniklas   inf_debug (waiting_inf, "port = %d", dead_port);
1718e93f7393Sniklas 
1719e93f7393Sniklas   if (inf->task && inf->task->port == dead_port)
1720e93f7393Sniklas     {
1721e93f7393Sniklas       proc_debug (inf->task, "is dead");
1722e93f7393Sniklas       inf->task->port = MACH_PORT_NULL;
1723e93f7393Sniklas       if (proc_wait_pid == inf->pid)
1724e93f7393Sniklas 	/* We have a wait outstanding on the process, which will return more
1725e93f7393Sniklas 	   detailed information, so delay until we get that.  */
1726e93f7393Sniklas 	inf->wait.suppress = 1;
1727e93f7393Sniklas       else
1728e93f7393Sniklas 	/* We never waited for the process (maybe it wasn't a child), so just
1729e93f7393Sniklas 	   pretend it got a SIGKILL.  */
1730e93f7393Sniklas 	inf_task_died_status (inf);
1731e93f7393Sniklas     }
1732e93f7393Sniklas   else
1733e93f7393Sniklas     {
1734e93f7393Sniklas       struct proc *thread = inf_port_to_thread (inf, dead_port);
1735e93f7393Sniklas       if (thread)
1736e93f7393Sniklas 	{
1737e93f7393Sniklas 	  proc_debug (thread, "is dead");
1738e93f7393Sniklas 	  thread->port = MACH_PORT_NULL;
1739e93f7393Sniklas 	}
1740b725ae77Skettenis 
1741b725ae77Skettenis       if (inf->task->dead)
1742b725ae77Skettenis 	/* Since the task is dead, its threads are dying with it.  */
1743b725ae77Skettenis 	inf->wait.suppress = 1;
1744e93f7393Sniklas     }
1745e93f7393Sniklas 
1746e93f7393Sniklas   mach_port_deallocate (mach_task_self (), dead_port);
1747e93f7393Sniklas   inf->threads_up_to_date = 0;	/* Just in case */
1748e93f7393Sniklas 
1749e93f7393Sniklas   return 0;
1750e93f7393Sniklas }
1751b725ae77Skettenis 
1752e93f7393Sniklas 
1753e93f7393Sniklas static error_t
ill_rpc(char * fun)1754e93f7393Sniklas ill_rpc (char *fun)
1755e93f7393Sniklas {
1756e93f7393Sniklas   warning ("illegal rpc: %s", fun);
1757e93f7393Sniklas   return 0;
1758e93f7393Sniklas }
1759e93f7393Sniklas 
1760e93f7393Sniklas error_t
do_mach_notify_no_senders(mach_port_t notify,mach_port_mscount_t count)1761e93f7393Sniklas do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
1762e93f7393Sniklas {
1763b725ae77Skettenis   return ill_rpc ("do_mach_notify_no_senders");
1764e93f7393Sniklas }
1765e93f7393Sniklas 
1766e93f7393Sniklas error_t
do_mach_notify_port_deleted(mach_port_t notify,mach_port_t name)1767e93f7393Sniklas do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
1768e93f7393Sniklas {
1769b725ae77Skettenis   return ill_rpc ("do_mach_notify_port_deleted");
1770e93f7393Sniklas }
1771e93f7393Sniklas 
1772e93f7393Sniklas error_t
do_mach_notify_msg_accepted(mach_port_t notify,mach_port_t name)1773e93f7393Sniklas do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
1774e93f7393Sniklas {
1775b725ae77Skettenis   return ill_rpc ("do_mach_notify_msg_accepted");
1776e93f7393Sniklas }
1777e93f7393Sniklas 
1778e93f7393Sniklas error_t
do_mach_notify_port_destroyed(mach_port_t notify,mach_port_t name)1779e93f7393Sniklas do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
1780e93f7393Sniklas {
1781b725ae77Skettenis   return ill_rpc ("do_mach_notify_port_destroyed");
1782e93f7393Sniklas }
1783e93f7393Sniklas 
1784e93f7393Sniklas error_t
do_mach_notify_send_once(mach_port_t notify)1785e93f7393Sniklas do_mach_notify_send_once (mach_port_t notify)
1786e93f7393Sniklas {
1787b725ae77Skettenis   return ill_rpc ("do_mach_notify_send_once");
1788e93f7393Sniklas }
1789b725ae77Skettenis 
1790e93f7393Sniklas 
1791e93f7393Sniklas /* Process_reply server routines.  We only use process_wait_reply.  */
1792e93f7393Sniklas 
1793e93f7393Sniklas error_t
S_proc_wait_reply(mach_port_t reply,error_t err,int status,int sigcode,rusage_t rusage,pid_t pid)1794e93f7393Sniklas S_proc_wait_reply (mach_port_t reply, error_t err,
1795e93f7393Sniklas 		   int status, int sigcode, rusage_t rusage, pid_t pid)
1796e93f7393Sniklas {
1797e93f7393Sniklas   struct inf *inf = waiting_inf;
1798e93f7393Sniklas 
1799e93f7393Sniklas   inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1800b725ae77Skettenis 	     err ? safe_strerror (err) : "0", pid, status, sigcode);
1801e93f7393Sniklas 
1802e93f7393Sniklas   if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1803e93f7393Sniklas     /* Ack.  The task has died, but the task-died notification code didn't
1804e93f7393Sniklas        tell anyone because it thought a more detailed reply from the
1805e93f7393Sniklas        procserver was forthcoming.  However, we now learn that won't
1806e93f7393Sniklas        happen...  So we have to act like the task just died, and this time,
1807e93f7393Sniklas        tell the world.  */
1808e93f7393Sniklas     inf_task_died_status (inf);
1809e93f7393Sniklas 
1810e93f7393Sniklas   if (--proc_waits_pending == 0)
1811e93f7393Sniklas     /* PROC_WAIT_PID represents the most recent wait.  We will always get
1812e93f7393Sniklas        replies in order because the proc server is single threaded.  */
1813e93f7393Sniklas     proc_wait_pid = 0;
1814e93f7393Sniklas 
1815e93f7393Sniklas   inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1816e93f7393Sniklas 
1817e93f7393Sniklas   if (err)
1818e93f7393Sniklas     {
1819e93f7393Sniklas       if (err != EINTR)
1820e93f7393Sniklas 	{
1821b725ae77Skettenis 	  warning ("Can't wait for pid %d: %s", inf->pid, safe_strerror (err));
1822e93f7393Sniklas 	  inf->no_wait = 1;
1823e93f7393Sniklas 
1824e93f7393Sniklas 	  /* Since we can't see the inferior's signals, don't trap them.  */
1825e93f7393Sniklas 	  inf_set_traced (inf, 0);
1826e93f7393Sniklas 	}
1827e93f7393Sniklas     }
1828e93f7393Sniklas   else if (pid == inf->pid)
1829e93f7393Sniklas     {
1830e93f7393Sniklas       store_waitstatus (&inf->wait.status, status);
1831e93f7393Sniklas       if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
1832e93f7393Sniklas 	/* The process has sent us a signal, and stopped itself in a sane
1833e93f7393Sniklas 	   state pending our actions.  */
1834e93f7393Sniklas 	{
1835e93f7393Sniklas 	  inf_debug (inf, "process has stopped itself");
1836e93f7393Sniklas 	  inf->stopped = 1;
1837e93f7393Sniklas 	}
1838e93f7393Sniklas     }
1839e93f7393Sniklas   else
1840e93f7393Sniklas     inf->wait.suppress = 1;	/* Something odd happened.  Ignore.  */
1841e93f7393Sniklas 
1842e93f7393Sniklas   return 0;
1843e93f7393Sniklas }
1844e93f7393Sniklas 
1845e93f7393Sniklas error_t
S_proc_setmsgport_reply(mach_port_t reply,error_t err,mach_port_t old_msg_port)1846e93f7393Sniklas S_proc_setmsgport_reply (mach_port_t reply, error_t err,
1847e93f7393Sniklas 			 mach_port_t old_msg_port)
1848e93f7393Sniklas {
1849b725ae77Skettenis   return ill_rpc ("S_proc_setmsgport_reply");
1850e93f7393Sniklas }
1851e93f7393Sniklas 
1852e93f7393Sniklas error_t
S_proc_getmsgport_reply(mach_port_t reply,error_t err,mach_port_t msg_port)1853e93f7393Sniklas S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
1854e93f7393Sniklas {
1855b725ae77Skettenis   return ill_rpc ("S_proc_getmsgport_reply");
1856e93f7393Sniklas }
1857b725ae77Skettenis 
1858e93f7393Sniklas 
1859e93f7393Sniklas /* Msg_reply server routines.  We only use msg_sig_post_untraced_reply.  */
1860e93f7393Sniklas 
1861e93f7393Sniklas error_t
S_msg_sig_post_untraced_reply(mach_port_t reply,error_t err)1862e93f7393Sniklas S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
1863e93f7393Sniklas {
1864e93f7393Sniklas   struct inf *inf = waiting_inf;
1865e93f7393Sniklas 
1866e93f7393Sniklas   if (err == EBUSY)
1867e93f7393Sniklas     /* EBUSY is what we get when the crash server has grabbed control of the
1868e93f7393Sniklas        process and doesn't like what signal we tried to send it.  Just act
1869e93f7393Sniklas        like the process stopped (using a signal of 0 should mean that the
1870e93f7393Sniklas        *next* time the user continues, it will pass signal 0, which the crash
1871e93f7393Sniklas        server should like).  */
1872e93f7393Sniklas     {
1873e93f7393Sniklas       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1874e93f7393Sniklas       inf->wait.status.value.sig = TARGET_SIGNAL_0;
1875e93f7393Sniklas     }
1876e93f7393Sniklas   else if (err)
1877b725ae77Skettenis     warning ("Signal delivery failed: %s", safe_strerror (err));
1878e93f7393Sniklas 
1879e93f7393Sniklas   if (err)
1880e93f7393Sniklas     /* We only get this reply when we've posted a signal to a process which we
1881e93f7393Sniklas        thought was stopped, and which we expected to continue after the signal.
1882e93f7393Sniklas        Given that the signal has failed for some reason, it's reasonable to
1883e93f7393Sniklas        assume it's still stopped.  */
1884e93f7393Sniklas     inf->stopped = 1;
1885e93f7393Sniklas   else
1886e93f7393Sniklas     inf->wait.suppress = 1;
1887e93f7393Sniklas 
1888e93f7393Sniklas   return 0;
1889e93f7393Sniklas }
1890e93f7393Sniklas 
1891e93f7393Sniklas error_t
S_msg_sig_post_reply(mach_port_t reply,error_t err)1892e93f7393Sniklas S_msg_sig_post_reply (mach_port_t reply, error_t err)
1893e93f7393Sniklas {
1894b725ae77Skettenis   return ill_rpc ("S_msg_sig_post_reply");
1895e93f7393Sniklas }
1896b725ae77Skettenis 
1897e93f7393Sniklas 
1898e93f7393Sniklas /* Returns the number of messages queued for the receive right PORT.  */
1899e93f7393Sniklas static mach_port_msgcount_t
port_msgs_queued(mach_port_t port)1900e93f7393Sniklas port_msgs_queued (mach_port_t port)
1901e93f7393Sniklas {
1902e93f7393Sniklas   struct mach_port_status status;
1903e93f7393Sniklas   error_t err =
1904e93f7393Sniklas   mach_port_get_receive_status (mach_task_self (), port, &status);
1905e93f7393Sniklas 
1906e93f7393Sniklas   if (err)
1907e93f7393Sniklas     return 0;
1908e93f7393Sniklas   else
1909e93f7393Sniklas     return status.mps_msgcount;
1910e93f7393Sniklas }
1911b725ae77Skettenis 
1912e93f7393Sniklas 
1913e93f7393Sniklas /* Resume execution of the inferior process.
1914e93f7393Sniklas 
1915e93f7393Sniklas    If STEP is nonzero, single-step it.
1916e93f7393Sniklas    If SIGNAL is nonzero, give it that signal.
1917e93f7393Sniklas 
1918e93f7393Sniklas    TID  STEP:
1919e93f7393Sniklas    -1   true   Single step the current thread allowing other threads to run.
1920e93f7393Sniklas    -1   false  Continue the current thread allowing other threads to run.
1921e93f7393Sniklas    X    true   Single step the given thread, don't allow any others to run.
1922e93f7393Sniklas    X    false  Continue the given thread, do not allow any others to run.
1923e93f7393Sniklas    (Where X, of course, is anything except -1)
1924e93f7393Sniklas 
1925e93f7393Sniklas    Note that a resume may not `take' if there are pending exceptions/&c
1926e93f7393Sniklas    still unprocessed from the last resume we did (any given resume may result
1927e93f7393Sniklas    in multiple events returned by wait).
1928e93f7393Sniklas  */
1929e93f7393Sniklas static void
gnu_resume(ptid_t tid,int step,enum target_signal sig)1930b725ae77Skettenis gnu_resume (ptid_t tid, int step, enum target_signal sig)
1931e93f7393Sniklas {
1932e93f7393Sniklas   struct proc *step_thread = 0;
1933e93f7393Sniklas   struct inf *inf = current_inferior;
1934e93f7393Sniklas 
1935b725ae77Skettenis   inf_debug (inf, "tid = %d, step = %d, sig = %d", PIDGET (tid), step, sig);
1936b725ae77Skettenis 
1937b725ae77Skettenis   inf_validate_procinfo (inf);
1938e93f7393Sniklas 
1939e93f7393Sniklas   if (sig != TARGET_SIGNAL_0 || inf->stopped)
1940b725ae77Skettenis     {
1941b725ae77Skettenis       if (sig == TARGET_SIGNAL_0 && inf->nomsg)
1942b725ae77Skettenis 	inf_continue (inf);
1943b725ae77Skettenis       else
1944e93f7393Sniklas 	inf_signal (inf, sig);
1945b725ae77Skettenis     }
1946e93f7393Sniklas   else if (inf->wait.exc.reply != MACH_PORT_NULL)
1947e93f7393Sniklas     /* We received an exception to which we have chosen not to forward, so
1948e93f7393Sniklas        abort the faulting thread, which will perhaps retake it.  */
1949e93f7393Sniklas     {
1950e93f7393Sniklas       proc_abort (inf->wait.thread, 1);
1951e93f7393Sniklas       warning ("Aborting %s with unforwarded exception %s.",
1952e93f7393Sniklas 	       proc_string (inf->wait.thread),
1953e93f7393Sniklas 	       target_signal_to_name (inf->wait.status.value.sig));
1954e93f7393Sniklas     }
1955e93f7393Sniklas 
1956e93f7393Sniklas   if (port_msgs_queued (inf->event_port))
1957e93f7393Sniklas     /* If there are still messages in our event queue, don't bother resuming
1958e93f7393Sniklas        the process, as we're just going to stop it right away anyway. */
1959e93f7393Sniklas     return;
1960e93f7393Sniklas 
1961e93f7393Sniklas   inf_update_procs (inf);
1962e93f7393Sniklas 
1963b725ae77Skettenis   if (PIDGET (tid) < 0)
1964e93f7393Sniklas     /* Allow all threads to run, except perhaps single-stepping one.  */
1965e93f7393Sniklas     {
1966b725ae77Skettenis       inf_debug (inf, "running all threads; tid = %d", PIDGET (inferior_ptid));
1967b725ae77Skettenis       tid = inferior_ptid;	/* What to step. */
1968e93f7393Sniklas       inf_set_threads_resume_sc (inf, 0, 1);
1969e93f7393Sniklas     }
1970e93f7393Sniklas   else
1971e93f7393Sniklas     /* Just allow a single thread to run.  */
1972e93f7393Sniklas     {
1973b725ae77Skettenis       struct proc *thread = inf_tid_to_thread (inf, PIDGET (tid));
1974e93f7393Sniklas       if (!thread)
1975e93f7393Sniklas 	error ("Can't run single thread id %d: no such thread!");
1976e93f7393Sniklas       inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid);
1977e93f7393Sniklas       inf_set_threads_resume_sc (inf, thread, 0);
1978e93f7393Sniklas     }
1979e93f7393Sniklas 
1980e93f7393Sniklas   if (step)
1981e93f7393Sniklas     {
1982b725ae77Skettenis       step_thread = inf_tid_to_thread (inf, PIDGET (tid));
1983e93f7393Sniklas       if (!step_thread)
1984b725ae77Skettenis 	warning ("Can't step thread id %d: no such thread.", PIDGET (tid));
1985e93f7393Sniklas       else
1986e93f7393Sniklas 	inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid);
1987e93f7393Sniklas     }
1988e93f7393Sniklas   if (step_thread != inf->step_thread)
1989e93f7393Sniklas     inf_set_step_thread (inf, step_thread);
1990e93f7393Sniklas 
1991e93f7393Sniklas   inf_debug (inf, "here we go...");
1992e93f7393Sniklas   inf_resume (inf);
1993e93f7393Sniklas }
1994b725ae77Skettenis 
1995e93f7393Sniklas 
1996e93f7393Sniklas static void
gnu_kill_inferior(void)1997b725ae77Skettenis gnu_kill_inferior (void)
1998e93f7393Sniklas {
1999e93f7393Sniklas   struct proc *task = current_inferior->task;
2000e93f7393Sniklas   if (task)
2001e93f7393Sniklas     {
2002e93f7393Sniklas       proc_debug (task, "terminating...");
2003e93f7393Sniklas       task_terminate (task->port);
2004b725ae77Skettenis       inf_set_pid (current_inferior, -1);
2005e93f7393Sniklas     }
2006e93f7393Sniklas   target_mourn_inferior ();
2007e93f7393Sniklas }
2008e93f7393Sniklas 
2009e93f7393Sniklas /* Clean up after the inferior dies.  */
2010e93f7393Sniklas static void
gnu_mourn_inferior(void)2011b725ae77Skettenis gnu_mourn_inferior (void)
2012e93f7393Sniklas {
2013e93f7393Sniklas   inf_debug (current_inferior, "rip");
2014e93f7393Sniklas   inf_detach (current_inferior);
2015e93f7393Sniklas   unpush_target (&gnu_ops);
2016e93f7393Sniklas   generic_mourn_inferior ();
2017e93f7393Sniklas }
2018b725ae77Skettenis 
2019e93f7393Sniklas 
2020e93f7393Sniklas /* Fork an inferior process, and start debugging it.  */
2021e93f7393Sniklas 
2022e93f7393Sniklas /* Set INFERIOR_PID to the first thread available in the child, if any.  */
2023b725ae77Skettenis static int
inf_pick_first_thread(void)2024b725ae77Skettenis inf_pick_first_thread (void)
2025e93f7393Sniklas {
2026e93f7393Sniklas   if (current_inferior->task && current_inferior->threads)
2027e93f7393Sniklas     /* The first thread.  */
2028b725ae77Skettenis     return current_inferior->threads->tid;
2029e93f7393Sniklas   else
2030e93f7393Sniklas     /* What may be the next thread.  */
2031b725ae77Skettenis     return next_thread_id;
2032e93f7393Sniklas }
2033e93f7393Sniklas 
2034e93f7393Sniklas static struct inf *
cur_inf(void)2035b725ae77Skettenis cur_inf (void)
2036e93f7393Sniklas {
2037e93f7393Sniklas   if (!current_inferior)
2038e93f7393Sniklas     current_inferior = make_inf ();
2039e93f7393Sniklas   return current_inferior;
2040e93f7393Sniklas }
2041e93f7393Sniklas 
2042e93f7393Sniklas static void
gnu_create_inferior(char * exec_file,char * allargs,char ** env,int from_tty)2043*63addd46Skettenis gnu_create_inferior (char *exec_file, char *allargs, char **env,
2044*63addd46Skettenis 		     int from_tty)
2045e93f7393Sniklas {
2046e93f7393Sniklas   struct inf *inf = cur_inf ();
2047e93f7393Sniklas 
2048e93f7393Sniklas   void trace_me ()
2049e93f7393Sniklas   {
2050e93f7393Sniklas     /* We're in the child; make this process stop as soon as it execs.  */
2051e93f7393Sniklas     inf_debug (inf, "tracing self");
2052e93f7393Sniklas     if (ptrace (PTRACE_TRACEME) != 0)
2053e93f7393Sniklas       error ("ptrace (PTRACE_TRACEME) failed!");
2054e93f7393Sniklas   }
2055b725ae77Skettenis   void attach_to_child (int pid)
2056e93f7393Sniklas   {
2057e93f7393Sniklas     /* Attach to the now stopped child, which is actually a shell...  */
2058e93f7393Sniklas     inf_debug (inf, "attaching to child: %d", pid);
2059e93f7393Sniklas 
2060e93f7393Sniklas     inf_attach (inf, pid);
2061e93f7393Sniklas 
2062e93f7393Sniklas     attach_flag = 0;
2063e93f7393Sniklas     push_target (&gnu_ops);
2064e93f7393Sniklas 
2065e93f7393Sniklas     inf->pending_execs = 2;
2066b725ae77Skettenis     inf->nomsg = 1;
2067e93f7393Sniklas     inf->traced = 1;
2068e93f7393Sniklas 
2069e93f7393Sniklas     /* Now let the child run again, knowing that it will stop immediately
2070e93f7393Sniklas        because of the ptrace. */
2071e93f7393Sniklas     inf_resume (inf);
2072b725ae77Skettenis     inferior_ptid = pid_to_ptid (inf_pick_first_thread ());
2073e93f7393Sniklas 
2074b725ae77Skettenis     startup_inferior (inf->pending_execs);
2075e93f7393Sniklas   }
2076e93f7393Sniklas 
2077e93f7393Sniklas   inf_debug (inf, "creating inferior");
2078e93f7393Sniklas 
2079b725ae77Skettenis   fork_inferior (exec_file, allargs, env, trace_me, attach_to_child,
2080b725ae77Skettenis 		 NULL, NULL);
2081e93f7393Sniklas 
2082b725ae77Skettenis   inf_validate_procinfo (inf);
2083e93f7393Sniklas   inf_update_signal_thread (inf);
2084e93f7393Sniklas   inf_set_traced (inf, inf->want_signals);
2085e93f7393Sniklas 
2086e93f7393Sniklas   /* Execing the process will have trashed our exception ports; steal them
2087e93f7393Sniklas      back (or make sure they're restored if the user wants that).  */
2088e93f7393Sniklas   if (inf->want_exceptions)
2089e93f7393Sniklas     inf_steal_exc_ports (inf);
2090e93f7393Sniklas   else
2091e93f7393Sniklas     inf_restore_exc_ports (inf);
2092e93f7393Sniklas 
2093e93f7393Sniklas   /* Here we go!  */
2094e93f7393Sniklas   proceed ((CORE_ADDR) -1, 0, 0);
2095e93f7393Sniklas }
2096e93f7393Sniklas 
2097e93f7393Sniklas /* Mark our target-struct as eligible for stray "run" and "attach"
2098e93f7393Sniklas    commands.  */
2099e93f7393Sniklas static int
gnu_can_run(void)2100b725ae77Skettenis gnu_can_run (void)
2101e93f7393Sniklas {
2102e93f7393Sniklas   return 1;
2103e93f7393Sniklas }
2104b725ae77Skettenis 
2105e93f7393Sniklas 
2106e93f7393Sniklas /* Attach to process PID, then initialize for debugging it
2107e93f7393Sniklas    and wait for the trace-trap that results from attaching.  */
2108e93f7393Sniklas static void
gnu_attach(char * args,int from_tty)2109b725ae77Skettenis gnu_attach (char *args, int from_tty)
2110e93f7393Sniklas {
2111e93f7393Sniklas   int pid;
2112e93f7393Sniklas   char *exec_file;
2113e93f7393Sniklas   struct inf *inf = cur_inf ();
2114e93f7393Sniklas 
2115e93f7393Sniklas   if (!args)
2116b725ae77Skettenis     error_no_arg ("process-id to attach");
2117e93f7393Sniklas 
2118e93f7393Sniklas   pid = atoi (args);
2119e93f7393Sniklas 
2120e93f7393Sniklas   if (pid == getpid ())		/* Trying to masturbate? */
2121e93f7393Sniklas     error ("I refuse to debug myself!");
2122e93f7393Sniklas 
2123e93f7393Sniklas   if (from_tty)
2124e93f7393Sniklas     {
2125e93f7393Sniklas       exec_file = (char *) get_exec_file (0);
2126e93f7393Sniklas 
2127e93f7393Sniklas       if (exec_file)
2128e93f7393Sniklas 	printf_unfiltered ("Attaching to program `%s', pid %d\n",
2129e93f7393Sniklas 			   exec_file, pid);
2130e93f7393Sniklas       else
2131e93f7393Sniklas 	printf_unfiltered ("Attaching to pid %d\n", pid);
2132e93f7393Sniklas 
2133e93f7393Sniklas       gdb_flush (gdb_stdout);
2134e93f7393Sniklas     }
2135e93f7393Sniklas 
2136e93f7393Sniklas   inf_debug (inf, "attaching to pid: %d", pid);
2137e93f7393Sniklas 
2138e93f7393Sniklas   inf_attach (inf, pid);
2139e93f7393Sniklas   inf_update_procs (inf);
2140e93f7393Sniklas 
2141b725ae77Skettenis   inferior_ptid = pid_to_ptid (inf_pick_first_thread ());
2142e93f7393Sniklas 
2143e93f7393Sniklas   attach_flag = 1;
2144e93f7393Sniklas   push_target (&gnu_ops);
2145e93f7393Sniklas 
2146b725ae77Skettenis   /* We have to initialize the terminal settings now, since the code
2147b725ae77Skettenis      below might try to restore them.  */
2148b725ae77Skettenis   target_terminal_init ();
2149e93f7393Sniklas 
2150e93f7393Sniklas   /* If the process was stopped before we attached, make it continue the next
2151e93f7393Sniklas      time the user does a continue.  */
2152b725ae77Skettenis   inf_validate_procinfo (inf);
2153e93f7393Sniklas 
2154b725ae77Skettenis   inf_update_signal_thread (inf);
2155b725ae77Skettenis   inf_set_traced (inf, inf->want_signals);
2156b725ae77Skettenis 
2157b725ae77Skettenis #if 0				/* Do we need this? */
2158e93f7393Sniklas   renumber_threads (0);		/* Give our threads reasonable names. */
2159b725ae77Skettenis #endif
2160e93f7393Sniklas }
2161b725ae77Skettenis 
2162e93f7393Sniklas 
2163e93f7393Sniklas /* Take a program previously attached to and detaches it.
2164e93f7393Sniklas    The program resumes execution and will no longer stop
2165e93f7393Sniklas    on signals, etc.  We'd better not have left any breakpoints
2166e93f7393Sniklas    in the program or it'll die when it hits one.  For this
2167e93f7393Sniklas    to work, it may be necessary for the process to have been
2168e93f7393Sniklas    previously attached.  It *might* work if the program was
2169e93f7393Sniklas    started via fork.  */
2170e93f7393Sniklas static void
gnu_detach(char * args,int from_tty)2171b725ae77Skettenis gnu_detach (char *args, int from_tty)
2172e93f7393Sniklas {
2173e93f7393Sniklas   if (from_tty)
2174e93f7393Sniklas     {
2175e93f7393Sniklas       char *exec_file = get_exec_file (0);
2176e93f7393Sniklas       if (exec_file)
2177e93f7393Sniklas 	printf_unfiltered ("Detaching from program `%s' pid %d\n",
2178e93f7393Sniklas 			   exec_file, current_inferior->pid);
2179e93f7393Sniklas       else
2180e93f7393Sniklas 	printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid);
2181e93f7393Sniklas       gdb_flush (gdb_stdout);
2182e93f7393Sniklas     }
2183e93f7393Sniklas 
2184e93f7393Sniklas   inf_detach (current_inferior);
2185e93f7393Sniklas 
2186b725ae77Skettenis   inferior_ptid = null_ptid;
2187e93f7393Sniklas 
2188e93f7393Sniklas   unpush_target (&gnu_ops);	/* Pop out of handling an inferior */
2189e93f7393Sniklas }
2190b725ae77Skettenis 
2191e93f7393Sniklas static void
gnu_terminal_init_inferior(void)2192b725ae77Skettenis gnu_terminal_init_inferior (void)
2193e93f7393Sniklas {
2194b725ae77Skettenis   gdb_assert (current_inferior);
2195e93f7393Sniklas   terminal_init_inferior_with_pgrp (current_inferior->pid);
2196e93f7393Sniklas }
2197e93f7393Sniklas 
2198e93f7393Sniklas /* Get ready to modify the registers array.  On machines which store
2199e93f7393Sniklas    individual registers, this doesn't need to do anything.  On machines
2200e93f7393Sniklas    which store all the registers in one fell swoop, this makes sure
2201e93f7393Sniklas    that registers contains all the registers from the program being
2202e93f7393Sniklas    debugged.  */
2203e93f7393Sniklas static void
gnu_prepare_to_store(void)2204b725ae77Skettenis gnu_prepare_to_store (void)
2205e93f7393Sniklas {
2206e93f7393Sniklas #ifdef CHILD_PREPARE_TO_STORE
2207e93f7393Sniklas   CHILD_PREPARE_TO_STORE ();
2208e93f7393Sniklas #endif
2209e93f7393Sniklas }
2210e93f7393Sniklas 
2211e93f7393Sniklas static void
gnu_open(char * arg,int from_tty)2212b725ae77Skettenis gnu_open (char *arg, int from_tty)
2213e93f7393Sniklas {
2214e93f7393Sniklas   error ("Use the \"run\" command to start a Unix child process.");
2215e93f7393Sniklas }
2216e93f7393Sniklas 
2217e93f7393Sniklas static void
gnu_stop(void)2218b725ae77Skettenis gnu_stop (void)
2219e93f7393Sniklas {
2220e93f7393Sniklas   error ("to_stop target function not implemented");
2221e93f7393Sniklas }
2222e93f7393Sniklas 
2223b725ae77Skettenis static char *
gnu_pid_to_exec_file(int pid)2224b725ae77Skettenis gnu_pid_to_exec_file (int pid)
2225b725ae77Skettenis {
2226b725ae77Skettenis   error ("to_pid_to_exec_file target function not implemented");
2227b725ae77Skettenis   return NULL;
2228b725ae77Skettenis }
2229b725ae77Skettenis 
2230b725ae77Skettenis 
2231e93f7393Sniklas static int
gnu_thread_alive(ptid_t tid)2232b725ae77Skettenis gnu_thread_alive (ptid_t tid)
2233e93f7393Sniklas {
2234e93f7393Sniklas   inf_update_procs (current_inferior);
2235b725ae77Skettenis   return !!inf_tid_to_thread (current_inferior, PIDGET (tid));
2236e93f7393Sniklas }
2237b725ae77Skettenis 
2238e93f7393Sniklas 
2239b725ae77Skettenis /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
2240b725ae77Skettenis    gdb's address space.  Return 0 on failure; number of bytes read
2241b725ae77Skettenis    otherwise.  */
2242e93f7393Sniklas int
gnu_read_inferior(task_t task,CORE_ADDR addr,char * myaddr,int length)2243b725ae77Skettenis gnu_read_inferior (task_t task, CORE_ADDR addr, char *myaddr, int length)
2244e93f7393Sniklas {
2245e93f7393Sniklas   error_t err;
2246e93f7393Sniklas   vm_address_t low_address = (vm_address_t) trunc_page (addr);
2247e93f7393Sniklas   vm_size_t aligned_length =
2248e93f7393Sniklas   (vm_size_t) round_page (addr + length) - low_address;
2249e93f7393Sniklas   pointer_t copied;
2250e93f7393Sniklas   int copy_count;
2251e93f7393Sniklas 
2252e93f7393Sniklas   /* Get memory from inferior with page aligned addresses */
2253e93f7393Sniklas   err = vm_read (task, low_address, aligned_length, &copied, &copy_count);
2254e93f7393Sniklas   if (err)
2255e93f7393Sniklas     return 0;
2256e93f7393Sniklas 
2257e93f7393Sniklas   err = hurd_safe_copyin (myaddr, (void *) addr - low_address + copied, length);
2258e93f7393Sniklas   if (err)
2259e93f7393Sniklas     {
2260b725ae77Skettenis       warning ("Read from inferior faulted: %s", safe_strerror (err));
2261e93f7393Sniklas       length = 0;
2262e93f7393Sniklas     }
2263e93f7393Sniklas 
2264e93f7393Sniklas   err = vm_deallocate (mach_task_self (), copied, copy_count);
2265e93f7393Sniklas   if (err)
2266b725ae77Skettenis     warning ("gnu_read_inferior vm_deallocate failed: %s", safe_strerror (err));
2267e93f7393Sniklas 
2268e93f7393Sniklas   return length;
2269e93f7393Sniklas }
2270e93f7393Sniklas 
2271e93f7393Sniklas #define CHK_GOTO_OUT(str,ret) \
2272e93f7393Sniklas   do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2273e93f7393Sniklas 
2274b725ae77Skettenis struct vm_region_list
2275b725ae77Skettenis {
2276e93f7393Sniklas   struct vm_region_list *next;
2277e93f7393Sniklas   vm_prot_t protection;
2278e93f7393Sniklas   vm_address_t start;
2279e93f7393Sniklas   vm_size_t length;
2280e93f7393Sniklas };
2281e93f7393Sniklas 
2282e93f7393Sniklas struct obstack region_obstack;
2283e93f7393Sniklas 
2284b725ae77Skettenis /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
2285b725ae77Skettenis    task's address space.  */
2286e93f7393Sniklas int
gnu_write_inferior(task_t task,CORE_ADDR addr,char * myaddr,int length)2287b725ae77Skettenis gnu_write_inferior (task_t task, CORE_ADDR addr, char *myaddr, int length)
2288e93f7393Sniklas {
2289e93f7393Sniklas   error_t err = 0;
2290e93f7393Sniklas   vm_address_t low_address = (vm_address_t) trunc_page (addr);
2291e93f7393Sniklas   vm_size_t aligned_length =
2292e93f7393Sniklas   (vm_size_t) round_page (addr + length) - low_address;
2293e93f7393Sniklas   pointer_t copied;
2294e93f7393Sniklas   int copy_count;
2295e93f7393Sniklas   int deallocate = 0;
2296e93f7393Sniklas 
2297e93f7393Sniklas   char *errstr = "Bug in gnu_write_inferior";
2298e93f7393Sniklas 
2299e93f7393Sniklas   struct vm_region_list *region_element;
2300e93f7393Sniklas   struct vm_region_list *region_head = (struct vm_region_list *) NULL;
2301e93f7393Sniklas 
2302e93f7393Sniklas   /* Get memory from inferior with page aligned addresses */
2303e93f7393Sniklas   err = vm_read (task,
2304e93f7393Sniklas 		 low_address,
2305e93f7393Sniklas 		 aligned_length,
2306e93f7393Sniklas 		 &copied,
2307e93f7393Sniklas 		 &copy_count);
2308e93f7393Sniklas   CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2309e93f7393Sniklas 
2310e93f7393Sniklas   deallocate++;
2311e93f7393Sniklas 
2312b725ae77Skettenis   err = hurd_safe_copyout ((void *) addr - low_address + copied,
2313b725ae77Skettenis 			   myaddr, length);
2314e93f7393Sniklas   CHK_GOTO_OUT ("Write to inferior faulted", err);
2315e93f7393Sniklas 
2316e93f7393Sniklas   obstack_init (&region_obstack);
2317e93f7393Sniklas 
2318e93f7393Sniklas   /* Do writes atomically.
2319b725ae77Skettenis      First check for holes and unwritable memory.  */
2320e93f7393Sniklas   {
2321e93f7393Sniklas     vm_size_t remaining_length = aligned_length;
2322e93f7393Sniklas     vm_address_t region_address = low_address;
2323e93f7393Sniklas 
2324e93f7393Sniklas     struct vm_region_list *scan;
2325e93f7393Sniklas 
2326e93f7393Sniklas     while (region_address < low_address + aligned_length)
2327e93f7393Sniklas       {
2328e93f7393Sniklas 	vm_prot_t protection;
2329e93f7393Sniklas 	vm_prot_t max_protection;
2330e93f7393Sniklas 	vm_inherit_t inheritance;
2331e93f7393Sniklas 	boolean_t shared;
2332e93f7393Sniklas 	mach_port_t object_name;
2333e93f7393Sniklas 	vm_offset_t offset;
2334e93f7393Sniklas 	vm_size_t region_length = remaining_length;
2335e93f7393Sniklas 	vm_address_t old_address = region_address;
2336e93f7393Sniklas 
2337e93f7393Sniklas 	err = vm_region (task,
2338e93f7393Sniklas 			 &region_address,
2339e93f7393Sniklas 			 &region_length,
2340e93f7393Sniklas 			 &protection,
2341e93f7393Sniklas 			 &max_protection,
2342e93f7393Sniklas 			 &inheritance,
2343e93f7393Sniklas 			 &shared,
2344e93f7393Sniklas 			 &object_name,
2345e93f7393Sniklas 			 &offset);
2346e93f7393Sniklas 	CHK_GOTO_OUT ("vm_region failed", err);
2347e93f7393Sniklas 
2348e93f7393Sniklas 	/* Check for holes in memory */
2349e93f7393Sniklas 	if (old_address != region_address)
2350e93f7393Sniklas 	  {
2351e93f7393Sniklas 	    warning ("No memory at 0x%x. Nothing written",
2352e93f7393Sniklas 		     old_address);
2353e93f7393Sniklas 	    err = KERN_SUCCESS;
2354e93f7393Sniklas 	    length = 0;
2355e93f7393Sniklas 	    goto out;
2356e93f7393Sniklas 	  }
2357e93f7393Sniklas 
2358e93f7393Sniklas 	if (!(max_protection & VM_PROT_WRITE))
2359e93f7393Sniklas 	  {
2360e93f7393Sniklas 	    warning ("Memory at address 0x%x is unwritable. Nothing written",
2361e93f7393Sniklas 		     old_address);
2362e93f7393Sniklas 	    err = KERN_SUCCESS;
2363e93f7393Sniklas 	    length = 0;
2364e93f7393Sniklas 	    goto out;
2365e93f7393Sniklas 	  }
2366e93f7393Sniklas 
2367e93f7393Sniklas 	/* Chain the regions for later use */
2368e93f7393Sniklas 	region_element =
2369e93f7393Sniklas 	  (struct vm_region_list *)
2370e93f7393Sniklas 	  obstack_alloc (&region_obstack, sizeof (struct vm_region_list));
2371e93f7393Sniklas 
2372e93f7393Sniklas 	region_element->protection = protection;
2373e93f7393Sniklas 	region_element->start = region_address;
2374e93f7393Sniklas 	region_element->length = region_length;
2375e93f7393Sniklas 
2376e93f7393Sniklas 	/* Chain the regions along with protections */
2377e93f7393Sniklas 	region_element->next = region_head;
2378e93f7393Sniklas 	region_head = region_element;
2379e93f7393Sniklas 
2380e93f7393Sniklas 	region_address += region_length;
2381e93f7393Sniklas 	remaining_length = remaining_length - region_length;
2382e93f7393Sniklas       }
2383e93f7393Sniklas 
2384e93f7393Sniklas     /* If things fail after this, we give up.
2385b725ae77Skettenis        Somebody is messing up inferior_task's mappings.  */
2386e93f7393Sniklas 
2387e93f7393Sniklas     /* Enable writes to the chained vm regions */
2388e93f7393Sniklas     for (scan = region_head; scan; scan = scan->next)
2389e93f7393Sniklas       {
2390e93f7393Sniklas 	if (!(scan->protection & VM_PROT_WRITE))
2391e93f7393Sniklas 	  {
2392e93f7393Sniklas 	    err = vm_protect (task,
2393e93f7393Sniklas 			      scan->start,
2394e93f7393Sniklas 			      scan->length,
2395e93f7393Sniklas 			      FALSE,
2396e93f7393Sniklas 			      scan->protection | VM_PROT_WRITE);
2397e93f7393Sniklas 	    CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2398e93f7393Sniklas 	  }
2399e93f7393Sniklas       }
2400e93f7393Sniklas 
2401e93f7393Sniklas     err = vm_write (task,
2402e93f7393Sniklas 		    low_address,
2403e93f7393Sniklas 		    copied,
2404e93f7393Sniklas 		    aligned_length);
2405e93f7393Sniklas     CHK_GOTO_OUT ("vm_write failed", err);
2406e93f7393Sniklas 
2407e93f7393Sniklas     /* Set up the original region protections, if they were changed */
2408e93f7393Sniklas     for (scan = region_head; scan; scan = scan->next)
2409e93f7393Sniklas       {
2410e93f7393Sniklas 	if (!(scan->protection & VM_PROT_WRITE))
2411e93f7393Sniklas 	  {
2412e93f7393Sniklas 	    err = vm_protect (task,
2413e93f7393Sniklas 			      scan->start,
2414e93f7393Sniklas 			      scan->length,
2415e93f7393Sniklas 			      FALSE,
2416e93f7393Sniklas 			      scan->protection);
2417e93f7393Sniklas 	    CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2418e93f7393Sniklas 	  }
2419e93f7393Sniklas       }
2420e93f7393Sniklas   }
2421e93f7393Sniklas 
2422e93f7393Sniklas out:
2423e93f7393Sniklas   if (deallocate)
2424e93f7393Sniklas     {
2425e93f7393Sniklas       obstack_free (&region_obstack, 0);
2426e93f7393Sniklas 
2427e93f7393Sniklas       (void) vm_deallocate (mach_task_self (),
2428e93f7393Sniklas 			    copied,
2429e93f7393Sniklas 			    copy_count);
2430e93f7393Sniklas     }
2431e93f7393Sniklas 
2432e93f7393Sniklas   if (err != KERN_SUCCESS)
2433e93f7393Sniklas     {
2434e93f7393Sniklas       warning ("%s: %s", errstr, mach_error_string (err));
2435e93f7393Sniklas       return 0;
2436e93f7393Sniklas     }
2437e93f7393Sniklas 
2438e93f7393Sniklas   return length;
2439e93f7393Sniklas }
2440b725ae77Skettenis 
2441e93f7393Sniklas 
2442b725ae77Skettenis /* Return 0 on failure, number of bytes handled otherwise.  TARGET
2443b725ae77Skettenis    is ignored. */
2444e93f7393Sniklas static int
gnu_xfer_memory(CORE_ADDR memaddr,char * myaddr,int len,int write,struct mem_attrib * attrib,struct target_ops * target)2445b725ae77Skettenis gnu_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2446b725ae77Skettenis 		 struct mem_attrib *attrib,
2447b725ae77Skettenis 		 struct target_ops *target)
2448e93f7393Sniklas {
2449b725ae77Skettenis   task_t task = (current_inferior
2450b725ae77Skettenis 		 ? (current_inferior->task
2451b725ae77Skettenis 		    ? current_inferior->task->port : 0)
2452b725ae77Skettenis 		 : 0);
2453e93f7393Sniklas 
2454e93f7393Sniklas   if (task == MACH_PORT_NULL)
2455e93f7393Sniklas     return 0;
2456e93f7393Sniklas   else
2457e93f7393Sniklas     {
2458e93f7393Sniklas       inf_debug (current_inferior, "%s %p[%d] %s %p",
2459b725ae77Skettenis 		 write ? "writing" : "reading", (void *) memaddr, len,
2460e93f7393Sniklas 		 write ? "<--" : "-->", myaddr);
2461e93f7393Sniklas       if (write)
2462e93f7393Sniklas 	return gnu_write_inferior (task, memaddr, myaddr, len);
2463e93f7393Sniklas       else
2464e93f7393Sniklas 	return gnu_read_inferior (task, memaddr, myaddr, len);
2465e93f7393Sniklas     }
2466e93f7393Sniklas }
2467e93f7393Sniklas 
2468b725ae77Skettenis /* Call FUNC on each memory region in the task.  */
2469b725ae77Skettenis static int
gnu_find_memory_regions(int (* func)(CORE_ADDR,unsigned long,int,int,int,void *),void * data)2470b725ae77Skettenis gnu_find_memory_regions (int (*func) (CORE_ADDR,
2471b725ae77Skettenis 				      unsigned long,
2472b725ae77Skettenis 				      int, int, int,
2473b725ae77Skettenis 				      void *),
2474b725ae77Skettenis 			 void *data)
2475b725ae77Skettenis {
2476b725ae77Skettenis   error_t err;
2477b725ae77Skettenis   task_t task;
2478b725ae77Skettenis   vm_address_t region_address, last_region_address, last_region_end;
2479b725ae77Skettenis   vm_prot_t last_protection;
2480e93f7393Sniklas 
2481b725ae77Skettenis   if (current_inferior == 0 || current_inferior->task == 0)
2482b725ae77Skettenis     return 0;
2483b725ae77Skettenis   task = current_inferior->task->port;
2484b725ae77Skettenis   if (task == MACH_PORT_NULL)
2485b725ae77Skettenis     return 0;
2486b725ae77Skettenis 
2487b725ae77Skettenis   region_address = last_region_address = last_region_end = VM_MIN_ADDRESS;
2488b725ae77Skettenis   last_protection = VM_PROT_NONE;
2489b725ae77Skettenis   while (region_address < VM_MAX_ADDRESS)
2490b725ae77Skettenis     {
2491b725ae77Skettenis       vm_prot_t protection;
2492b725ae77Skettenis       vm_prot_t max_protection;
2493b725ae77Skettenis       vm_inherit_t inheritance;
2494b725ae77Skettenis       boolean_t shared;
2495b725ae77Skettenis       mach_port_t object_name;
2496b725ae77Skettenis       vm_offset_t offset;
2497b725ae77Skettenis       vm_size_t region_length = VM_MAX_ADDRESS - region_address;
2498b725ae77Skettenis       vm_address_t old_address = region_address;
2499b725ae77Skettenis 
2500b725ae77Skettenis       err = vm_region (task,
2501b725ae77Skettenis 		       &region_address,
2502b725ae77Skettenis 		       &region_length,
2503b725ae77Skettenis 		       &protection,
2504b725ae77Skettenis 		       &max_protection,
2505b725ae77Skettenis 		       &inheritance,
2506b725ae77Skettenis 		       &shared,
2507b725ae77Skettenis 		       &object_name,
2508b725ae77Skettenis 		       &offset);
2509b725ae77Skettenis       if (err == KERN_NO_SPACE)
2510b725ae77Skettenis 	break;
2511b725ae77Skettenis       if (err != KERN_SUCCESS)
2512b725ae77Skettenis 	{
2513b725ae77Skettenis 	  warning ("vm_region failed: %s", mach_error_string (err));
2514b725ae77Skettenis 	  return -1;
2515b725ae77Skettenis 	}
2516b725ae77Skettenis 
2517b725ae77Skettenis       if (protection == last_protection && region_address == last_region_end)
2518b725ae77Skettenis 	/* This region is contiguous with and indistinguishable from
2519b725ae77Skettenis 	   the previous one, so we just extend that one.  */
2520b725ae77Skettenis 	last_region_end = region_address += region_length;
2521b725ae77Skettenis       else
2522b725ae77Skettenis 	{
2523b725ae77Skettenis 	  /* This region is distinct from the last one we saw, so report
2524b725ae77Skettenis 	     that previous one.  */
2525b725ae77Skettenis 	  if (last_protection != VM_PROT_NONE)
2526b725ae77Skettenis 	    (*func) (last_region_address,
2527b725ae77Skettenis 		     last_region_end - last_region_address,
2528b725ae77Skettenis 		     last_protection & VM_PROT_READ,
2529b725ae77Skettenis 		     last_protection & VM_PROT_WRITE,
2530b725ae77Skettenis 		     last_protection & VM_PROT_EXECUTE,
2531b725ae77Skettenis 		     data);
2532b725ae77Skettenis 	  last_region_address = region_address;
2533b725ae77Skettenis 	  last_region_end = region_address += region_length;
2534b725ae77Skettenis 	  last_protection = protection;
2535b725ae77Skettenis 	}
2536b725ae77Skettenis     }
2537b725ae77Skettenis 
2538b725ae77Skettenis   /* Report the final region.  */
2539b725ae77Skettenis   if (last_region_end > last_region_address && last_protection != VM_PROT_NONE)
2540b725ae77Skettenis     (*func) (last_region_address, last_region_end - last_region_address,
2541b725ae77Skettenis 	     last_protection & VM_PROT_READ,
2542b725ae77Skettenis 	     last_protection & VM_PROT_WRITE,
2543b725ae77Skettenis 	     last_protection & VM_PROT_EXECUTE,
2544b725ae77Skettenis 	     data);
2545b725ae77Skettenis 
2546b725ae77Skettenis   return 0;
2547b725ae77Skettenis }
2548b725ae77Skettenis 
2549e93f7393Sniklas 
2550b725ae77Skettenis /* Return printable description of proc.  */
2551b725ae77Skettenis char *
proc_string(struct proc * proc)2552b725ae77Skettenis proc_string (struct proc *proc)
2553e93f7393Sniklas {
2554e93f7393Sniklas   static char tid_str[80];
2555e93f7393Sniklas   if (proc_is_task (proc))
2556e93f7393Sniklas     sprintf (tid_str, "process %d", proc->inf->pid);
2557e93f7393Sniklas   else
2558e93f7393Sniklas     sprintf (tid_str, "thread %d.%d",
2559b725ae77Skettenis 	     proc->inf->pid, pid_to_thread_id (MERGEPID (proc->tid, 0)));
2560e93f7393Sniklas   return tid_str;
2561e93f7393Sniklas }
2562e93f7393Sniklas 
2563b725ae77Skettenis static char *
gnu_pid_to_str(ptid_t ptid)2564b725ae77Skettenis gnu_pid_to_str (ptid_t ptid)
2565e93f7393Sniklas {
2566e93f7393Sniklas   struct inf *inf = current_inferior;
2567b725ae77Skettenis   int tid = PIDGET (ptid);
2568e93f7393Sniklas   struct proc *thread = inf_tid_to_thread (inf, tid);
2569e93f7393Sniklas 
2570e93f7393Sniklas   if (thread)
2571e93f7393Sniklas     return proc_string (thread);
2572e93f7393Sniklas   else
2573e93f7393Sniklas     {
2574e93f7393Sniklas       static char tid_str[80];
2575e93f7393Sniklas       sprintf (tid_str, "bogus thread id %d", tid);
2576e93f7393Sniklas       return tid_str;
2577e93f7393Sniklas     }
2578e93f7393Sniklas }
2579b725ae77Skettenis 
2580b725ae77Skettenis 
2581b725ae77Skettenis extern void gnu_store_registers (int regno);
2582b725ae77Skettenis extern void gnu_fetch_registers (int regno);
2583b725ae77Skettenis 
2584b725ae77Skettenis struct target_ops gnu_ops;
2585b725ae77Skettenis 
2586b725ae77Skettenis static void
init_gnu_ops(void)2587b725ae77Skettenis init_gnu_ops (void)
2588b725ae77Skettenis {
2589b725ae77Skettenis   gnu_ops.to_shortname = "GNU";		/* to_shortname */
2590b725ae77Skettenis   gnu_ops.to_longname = "GNU Hurd process"; /* to_longname */
2591b725ae77Skettenis   gnu_ops.to_doc = "GNU Hurd process";	/* to_doc */
2592b725ae77Skettenis   gnu_ops.to_open = gnu_open;		/* to_open */
2593b725ae77Skettenis   gnu_ops.to_attach = gnu_attach;	/* to_attach */
2594b725ae77Skettenis   gnu_ops.to_detach = gnu_detach;	/* to_detach */
2595b725ae77Skettenis   gnu_ops.to_resume = gnu_resume;	/* to_resume */
2596b725ae77Skettenis   gnu_ops.to_wait = gnu_wait;		/* to_wait */
2597b725ae77Skettenis   gnu_ops.to_fetch_registers = gnu_fetch_registers;    /* to_fetch_registers */
2598b725ae77Skettenis   gnu_ops.to_store_registers = gnu_store_registers;    /* to_store_registers */
2599b725ae77Skettenis   gnu_ops.to_prepare_to_store = gnu_prepare_to_store; /* to_prepare_to_store */
2600*63addd46Skettenis   gnu_ops.deprecated_xfer_memory = gnu_xfer_memory;
2601b725ae77Skettenis   gnu_ops.to_find_memory_regions = gnu_find_memory_regions;
2602b725ae77Skettenis   gnu_ops.to_insert_breakpoint = memory_insert_breakpoint;
2603b725ae77Skettenis   gnu_ops.to_remove_breakpoint = memory_remove_breakpoint;
2604b725ae77Skettenis   gnu_ops.to_terminal_init = gnu_terminal_init_inferior;
2605b725ae77Skettenis   gnu_ops.to_terminal_inferior = terminal_inferior;
2606b725ae77Skettenis   gnu_ops.to_terminal_ours_for_output = terminal_ours_for_output;
2607b725ae77Skettenis   gnu_ops.to_terminal_save_ours = terminal_save_ours;
2608b725ae77Skettenis   gnu_ops.to_terminal_ours = terminal_ours;
2609b725ae77Skettenis   gnu_ops.to_terminal_info = child_terminal_info;
2610b725ae77Skettenis   gnu_ops.to_kill = gnu_kill_inferior;	/* to_kill */
2611b725ae77Skettenis   gnu_ops.to_create_inferior = gnu_create_inferior; /* to_create_inferior */
2612b725ae77Skettenis   gnu_ops.to_mourn_inferior = gnu_mourn_inferior;	/* to_mourn_inferior */
2613b725ae77Skettenis   gnu_ops.to_can_run = gnu_can_run;	/* to_can_run */
2614b725ae77Skettenis   gnu_ops.to_thread_alive = gnu_thread_alive;	/* to_thread_alive */
2615b725ae77Skettenis   gnu_ops.to_pid_to_str = gnu_pid_to_str;   /* to_pid_to_str */
2616b725ae77Skettenis   gnu_ops.to_stop = gnu_stop;	/* to_stop */
2617b725ae77Skettenis   gnu_ops.to_pid_to_exec_file = gnu_pid_to_exec_file; /* to_pid_to_exec_file */
2618b725ae77Skettenis   gnu_ops.to_stratum = process_stratum;		/* to_stratum */
2619b725ae77Skettenis   gnu_ops.to_has_all_memory = 1;	/* to_has_all_memory */
2620b725ae77Skettenis   gnu_ops.to_has_memory = 1;		/* to_has_memory */
2621b725ae77Skettenis   gnu_ops.to_has_stack = 1;		/* to_has_stack */
2622b725ae77Skettenis   gnu_ops.to_has_registers = 1;		/* to_has_registers */
2623b725ae77Skettenis   gnu_ops.to_has_execution = 1;		/* to_has_execution */
2624b725ae77Skettenis   gnu_ops.to_magic = OPS_MAGIC;		/* to_magic */
2625b725ae77Skettenis }				/* init_gnu_ops */
2626b725ae77Skettenis 
2627e93f7393Sniklas 
2628e93f7393Sniklas /* User task commands.  */
2629e93f7393Sniklas 
2630e93f7393Sniklas struct cmd_list_element *set_task_cmd_list = 0;
2631e93f7393Sniklas struct cmd_list_element *show_task_cmd_list = 0;
2632b725ae77Skettenis /* User thread commands.  */
2633e93f7393Sniklas 
2634b725ae77Skettenis /* Commands with a prefix of `set/show thread'.  */
2635b725ae77Skettenis extern struct cmd_list_element *thread_cmd_list;
2636b725ae77Skettenis struct cmd_list_element *set_thread_cmd_list = NULL;
2637b725ae77Skettenis struct cmd_list_element *show_thread_cmd_list = NULL;
2638b725ae77Skettenis 
2639b725ae77Skettenis /* Commands with a prefix of `set/show thread default'.  */
2640b725ae77Skettenis struct cmd_list_element *set_thread_default_cmd_list = NULL;
2641b725ae77Skettenis struct cmd_list_element *show_thread_default_cmd_list = NULL;
2642b725ae77Skettenis 
2643b725ae77Skettenis static void
set_thread_cmd(char * args,int from_tty)2644b725ae77Skettenis set_thread_cmd (char *args, int from_tty)
2645b725ae77Skettenis {
2646b725ae77Skettenis   printf_unfiltered ("\"set thread\" must be followed by the name of a thread property, or \"default\".\n");
2647b725ae77Skettenis }
2648b725ae77Skettenis 
2649b725ae77Skettenis static void
show_thread_cmd(char * args,int from_tty)2650b725ae77Skettenis show_thread_cmd (char *args, int from_tty)
2651b725ae77Skettenis {
2652b725ae77Skettenis   printf_unfiltered ("\"show thread\" must be followed by the name of a thread property, or \"default\".\n");
2653b725ae77Skettenis }
2654b725ae77Skettenis 
2655b725ae77Skettenis static void
set_thread_default_cmd(char * args,int from_tty)2656b725ae77Skettenis set_thread_default_cmd (char *args, int from_tty)
2657b725ae77Skettenis {
2658b725ae77Skettenis   printf_unfiltered ("\"set thread default\" must be followed by the name of a thread property.\n");
2659b725ae77Skettenis }
2660b725ae77Skettenis 
2661b725ae77Skettenis static void
show_thread_default_cmd(char * args,int from_tty)2662b725ae77Skettenis show_thread_default_cmd (char *args, int from_tty)
2663b725ae77Skettenis {
2664b725ae77Skettenis   printf_unfiltered ("\"show thread default\" must be followed by the name of a thread property.\n");
2665b725ae77Skettenis }
2666e93f7393Sniklas 
2667e93f7393Sniklas static int
parse_int_arg(char * args,char * cmd_prefix)2668e93f7393Sniklas parse_int_arg (char *args, char *cmd_prefix)
2669e93f7393Sniklas {
2670e93f7393Sniklas   if (args)
2671e93f7393Sniklas     {
2672e93f7393Sniklas       char *arg_end;
2673e93f7393Sniklas       int val = strtoul (args, &arg_end, 10);
2674e93f7393Sniklas       if (*args && *arg_end == '\0')
2675e93f7393Sniklas 	return val;
2676e93f7393Sniklas     }
2677e93f7393Sniklas   error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix);
2678e93f7393Sniklas }
2679e93f7393Sniklas 
2680e93f7393Sniklas static int
_parse_bool_arg(char * args,char * t_val,char * f_val,char * cmd_prefix)2681e93f7393Sniklas _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
2682e93f7393Sniklas {
2683e93f7393Sniklas   if (!args || strcmp (args, t_val) == 0)
2684e93f7393Sniklas     return 1;
2685e93f7393Sniklas   else if (strcmp (args, f_val) == 0)
2686e93f7393Sniklas     return 0;
2687e93f7393Sniklas   else
2688e93f7393Sniklas     error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2689e93f7393Sniklas 	   cmd_prefix, t_val, f_val);
2690e93f7393Sniklas }
2691e93f7393Sniklas 
2692e93f7393Sniklas #define parse_bool_arg(args, cmd_prefix) \
2693e93f7393Sniklas   _parse_bool_arg (args, "on", "off", cmd_prefix)
2694e93f7393Sniklas 
2695e93f7393Sniklas static void
check_empty(char * args,char * cmd_prefix)2696e93f7393Sniklas check_empty (char *args, char *cmd_prefix)
2697e93f7393Sniklas {
2698e93f7393Sniklas   if (args)
2699e93f7393Sniklas     error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args);
2700e93f7393Sniklas }
2701e93f7393Sniklas 
2702e93f7393Sniklas /* Returns the alive thread named by INFERIOR_PID, or signals an error.  */
2703e93f7393Sniklas static struct proc *
cur_thread(void)2704b725ae77Skettenis cur_thread (void)
2705e93f7393Sniklas {
2706e93f7393Sniklas   struct inf *inf = cur_inf ();
2707b725ae77Skettenis   struct proc *thread = inf_tid_to_thread (inf, PIDGET (inferior_ptid));
2708e93f7393Sniklas   if (!thread)
2709e93f7393Sniklas     error ("No current thread.");
2710e93f7393Sniklas   return thread;
2711e93f7393Sniklas }
2712e93f7393Sniklas 
2713e93f7393Sniklas /* Returns the current inferior, but signals an error if it has no task.  */
2714e93f7393Sniklas static struct inf *
active_inf(void)2715b725ae77Skettenis active_inf (void)
2716e93f7393Sniklas {
2717e93f7393Sniklas   struct inf *inf = cur_inf ();
2718e93f7393Sniklas   if (!inf->task)
2719e93f7393Sniklas     error ("No current process.");
2720e93f7393Sniklas   return inf;
2721e93f7393Sniklas }
2722b725ae77Skettenis 
2723e93f7393Sniklas 
2724e93f7393Sniklas static void
set_task_pause_cmd(char * args,int from_tty)2725e93f7393Sniklas set_task_pause_cmd (char *args, int from_tty)
2726e93f7393Sniklas {
2727e93f7393Sniklas   struct inf *inf = cur_inf ();
2728e93f7393Sniklas   int old_sc = inf->pause_sc;
2729e93f7393Sniklas 
2730e93f7393Sniklas   inf->pause_sc = parse_bool_arg (args, "set task pause");
2731e93f7393Sniklas 
2732e93f7393Sniklas   if (old_sc == 0 && inf->pause_sc != 0)
2733e93f7393Sniklas     /* If the task is currently unsuspended, immediately suspend it,
2734e93f7393Sniklas        otherwise wait until the next time it gets control.  */
2735e93f7393Sniklas     inf_suspend (inf);
2736e93f7393Sniklas }
2737e93f7393Sniklas 
2738e93f7393Sniklas static void
show_task_pause_cmd(char * args,int from_tty)2739e93f7393Sniklas show_task_pause_cmd (char *args, int from_tty)
2740e93f7393Sniklas {
2741e93f7393Sniklas   struct inf *inf = cur_inf ();
2742e93f7393Sniklas   check_empty (args, "show task pause");
2743e93f7393Sniklas   printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2744e93f7393Sniklas 		     inf->task
2745e93f7393Sniklas 		     ? (inf->pause_sc == 0 ? "isn't" : "is")
2746e93f7393Sniklas 		     : (inf->pause_sc == 0 ? "won't be" : "will be"));
2747e93f7393Sniklas }
2748e93f7393Sniklas 
2749e93f7393Sniklas static void
set_task_detach_sc_cmd(char * args,int from_tty)2750e93f7393Sniklas set_task_detach_sc_cmd (char *args, int from_tty)
2751e93f7393Sniklas {
2752e93f7393Sniklas   cur_inf ()->detach_sc = parse_int_arg (args, "set task detach-suspend-count");
2753e93f7393Sniklas }
2754e93f7393Sniklas 
2755e93f7393Sniklas static void
show_task_detach_sc_cmd(char * args,int from_tty)2756e93f7393Sniklas show_task_detach_sc_cmd (char *args, int from_tty)
2757e93f7393Sniklas {
2758e93f7393Sniklas   check_empty (args, "show task detach-suspend-count");
2759e93f7393Sniklas   printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n",
2760e93f7393Sniklas 		     cur_inf ()->detach_sc);
2761e93f7393Sniklas }
2762b725ae77Skettenis 
2763e93f7393Sniklas 
2764e93f7393Sniklas static void
set_thread_default_pause_cmd(char * args,int from_tty)2765e93f7393Sniklas set_thread_default_pause_cmd (char *args, int from_tty)
2766e93f7393Sniklas {
2767e93f7393Sniklas   struct inf *inf = cur_inf ();
2768e93f7393Sniklas   inf->default_thread_pause_sc =
2769e93f7393Sniklas     parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2770e93f7393Sniklas }
2771e93f7393Sniklas 
2772e93f7393Sniklas static void
show_thread_default_pause_cmd(char * args,int from_tty)2773e93f7393Sniklas show_thread_default_pause_cmd (char *args, int from_tty)
2774e93f7393Sniklas {
2775e93f7393Sniklas   struct inf *inf = cur_inf ();
2776e93f7393Sniklas   int sc = inf->default_thread_pause_sc;
2777e93f7393Sniklas   check_empty (args, "show thread default pause");
2778e93f7393Sniklas   printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2779e93f7393Sniklas 		     sc ? "are" : "aren't",
2780e93f7393Sniklas 		     !sc && inf->pause_sc ? " (but the task is)" : "");
2781e93f7393Sniklas }
2782e93f7393Sniklas 
2783e93f7393Sniklas static void
set_thread_default_run_cmd(char * args,int from_tty)2784e93f7393Sniklas set_thread_default_run_cmd (char *args, int from_tty)
2785e93f7393Sniklas {
2786e93f7393Sniklas   struct inf *inf = cur_inf ();
2787e93f7393Sniklas   inf->default_thread_run_sc =
2788e93f7393Sniklas     parse_bool_arg (args, "set thread default run") ? 0 : 1;
2789e93f7393Sniklas }
2790e93f7393Sniklas 
2791e93f7393Sniklas static void
show_thread_default_run_cmd(char * args,int from_tty)2792e93f7393Sniklas show_thread_default_run_cmd (char *args, int from_tty)
2793e93f7393Sniklas {
2794e93f7393Sniklas   struct inf *inf = cur_inf ();
2795e93f7393Sniklas   check_empty (args, "show thread default run");
2796e93f7393Sniklas   printf_unfiltered ("New threads %s allowed to run.\n",
2797e93f7393Sniklas 		     inf->default_thread_run_sc == 0 ? "are" : "aren't");
2798e93f7393Sniklas }
2799e93f7393Sniklas 
2800e93f7393Sniklas static void
set_thread_default_detach_sc_cmd(char * args,int from_tty)2801e93f7393Sniklas set_thread_default_detach_sc_cmd (char *args, int from_tty)
2802e93f7393Sniklas {
2803e93f7393Sniklas   cur_inf ()->default_thread_detach_sc =
2804e93f7393Sniklas     parse_int_arg (args, "set thread default detach-suspend-count");
2805e93f7393Sniklas }
2806e93f7393Sniklas 
2807e93f7393Sniklas static void
show_thread_default_detach_sc_cmd(char * args,int from_tty)2808e93f7393Sniklas show_thread_default_detach_sc_cmd (char *args, int from_tty)
2809e93f7393Sniklas {
2810e93f7393Sniklas   check_empty (args, "show thread default detach-suspend-count");
2811e93f7393Sniklas   printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
2812e93f7393Sniklas 		     cur_inf ()->default_thread_detach_sc);
2813e93f7393Sniklas }
2814b725ae77Skettenis 
2815e93f7393Sniklas 
2816e93f7393Sniklas /* Steal a send right called NAME in the inferior task, and make it PROC's
2817e93f7393Sniklas    saved exception port.  */
2818e93f7393Sniklas static void
steal_exc_port(struct proc * proc,mach_port_t name)2819e93f7393Sniklas steal_exc_port (struct proc *proc, mach_port_t name)
2820e93f7393Sniklas {
2821e93f7393Sniklas   error_t err;
2822e93f7393Sniklas   mach_port_t port;
2823e93f7393Sniklas   mach_msg_type_name_t port_type;
2824e93f7393Sniklas 
2825e93f7393Sniklas   if (!proc || !proc->inf->task)
2826e93f7393Sniklas     error ("No inferior task.");
2827e93f7393Sniklas 
2828e93f7393Sniklas   err = mach_port_extract_right (proc->inf->task->port,
2829e93f7393Sniklas 				 name, MACH_MSG_TYPE_COPY_SEND,
2830e93f7393Sniklas 				 &port, &port_type);
2831e93f7393Sniklas   if (err)
2832e93f7393Sniklas     error ("Couldn't extract send right %d from inferior: %s",
2833b725ae77Skettenis 	   name, safe_strerror (err));
2834e93f7393Sniklas 
2835e93f7393Sniklas   if (proc->saved_exc_port)
2836e93f7393Sniklas     /* Get rid of our reference to the old one.  */
2837e93f7393Sniklas     mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2838e93f7393Sniklas 
2839e93f7393Sniklas   proc->saved_exc_port = port;
2840e93f7393Sniklas 
2841e93f7393Sniklas   if (!proc->exc_port)
2842e93f7393Sniklas     /* If PROC is a thread, we may not have set its exception port before.
2843e93f7393Sniklas        We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2844e93f7393Sniklas     {
2845e93f7393Sniklas       proc->exc_port = proc->inf->event_port;
2846e93f7393Sniklas       err = proc_set_exception_port (proc, proc->exc_port);
2847e93f7393Sniklas       error ("Can't set exception port for %s: %s",
2848b725ae77Skettenis 	     proc_string (proc), safe_strerror (err));
2849e93f7393Sniklas     }
2850e93f7393Sniklas }
2851b725ae77Skettenis 
2852e93f7393Sniklas static void
set_task_exc_port_cmd(char * args,int from_tty)2853e93f7393Sniklas set_task_exc_port_cmd (char *args, int from_tty)
2854e93f7393Sniklas {
2855e93f7393Sniklas   struct inf *inf = cur_inf ();
2856e93f7393Sniklas   if (!args)
2857e93f7393Sniklas     error ("No argument to \"set task exception-port\" command.");
2858e93f7393Sniklas   steal_exc_port (inf->task, parse_and_eval_address (args));
2859e93f7393Sniklas }
2860e93f7393Sniklas 
2861e93f7393Sniklas static void
set_stopped_cmd(char * args,int from_tty)2862e93f7393Sniklas set_stopped_cmd (char *args, int from_tty)
2863e93f7393Sniklas {
2864e93f7393Sniklas   cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2865e93f7393Sniklas }
2866e93f7393Sniklas 
2867e93f7393Sniklas static void
show_stopped_cmd(char * args,int from_tty)2868e93f7393Sniklas show_stopped_cmd (char *args, int from_tty)
2869e93f7393Sniklas {
2870e93f7393Sniklas   struct inf *inf = active_inf ();
2871e93f7393Sniklas   check_empty (args, "show stopped");
2872e93f7393Sniklas   printf_unfiltered ("The inferior process %s stopped.\n",
2873e93f7393Sniklas 		     inf->stopped ? "is" : "isn't");
2874e93f7393Sniklas }
2875e93f7393Sniklas 
2876e93f7393Sniklas static void
set_sig_thread_cmd(char * args,int from_tty)2877e93f7393Sniklas set_sig_thread_cmd (char *args, int from_tty)
2878e93f7393Sniklas {
2879e93f7393Sniklas   struct inf *inf = cur_inf ();
2880e93f7393Sniklas 
2881e93f7393Sniklas   if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
2882e93f7393Sniklas     error ("Illegal argument to \"set signal-thread\" command.\n"
2883e93f7393Sniklas 	   "Should be an integer thread ID, or `none'.");
2884e93f7393Sniklas 
2885e93f7393Sniklas   if (strcmp (args, "none") == 0)
2886e93f7393Sniklas     inf->signal_thread = 0;
2887e93f7393Sniklas   else
2888e93f7393Sniklas     {
2889b725ae77Skettenis       int tid = PIDGET (thread_id_to_pid (atoi (args)));
2890e93f7393Sniklas       if (tid < 0)
2891e93f7393Sniklas 	error ("Thread ID %s not known.  Use the \"info threads\" command to\n"
2892e93f7393Sniklas 	       "see the IDs of currently known threads.", args);
2893e93f7393Sniklas       inf->signal_thread = inf_tid_to_thread (inf, tid);
2894e93f7393Sniklas     }
2895e93f7393Sniklas }
2896e93f7393Sniklas 
2897e93f7393Sniklas static void
show_sig_thread_cmd(char * args,int from_tty)2898e93f7393Sniklas show_sig_thread_cmd (char *args, int from_tty)
2899e93f7393Sniklas {
2900e93f7393Sniklas   struct inf *inf = active_inf ();
2901e93f7393Sniklas   check_empty (args, "show signal-thread");
2902e93f7393Sniklas   if (inf->signal_thread)
2903e93f7393Sniklas     printf_unfiltered ("The signal thread is %s.\n",
2904e93f7393Sniklas 		       proc_string (inf->signal_thread));
2905e93f7393Sniklas   else
2906e93f7393Sniklas     printf_unfiltered ("There is no signal thread.\n");
2907e93f7393Sniklas }
2908b725ae77Skettenis 
2909e93f7393Sniklas 
2910e93f7393Sniklas static void
set_signals_cmd(char * args,int from_tty)2911e93f7393Sniklas set_signals_cmd (char *args, int from_tty)
2912e93f7393Sniklas {
2913e93f7393Sniklas   struct inf *inf = cur_inf ();
2914e93f7393Sniklas 
2915e93f7393Sniklas   inf->want_signals = parse_bool_arg (args, "set signals");
2916e93f7393Sniklas 
2917e93f7393Sniklas   if (inf->task && inf->want_signals != inf->traced)
2918e93f7393Sniklas     /* Make this take effect immediately in a running process.  */
2919e93f7393Sniklas     inf_set_traced (inf, inf->want_signals);
2920e93f7393Sniklas }
2921e93f7393Sniklas 
2922e93f7393Sniklas static void
show_signals_cmd(char * args,int from_tty)2923e93f7393Sniklas show_signals_cmd (char *args, int from_tty)
2924e93f7393Sniklas {
2925e93f7393Sniklas   struct inf *inf = cur_inf ();
2926e93f7393Sniklas   check_empty (args, "show signals");
2927e93f7393Sniklas   printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2928e93f7393Sniklas 		     inf->task
2929e93f7393Sniklas 		     ? (inf->traced ? "are" : "aren't")
2930e93f7393Sniklas 		     : (inf->want_signals ? "will be" : "won't be"));
2931e93f7393Sniklas }
2932e93f7393Sniklas 
2933e93f7393Sniklas static void
set_exceptions_cmd(char * args,int from_tty)2934e93f7393Sniklas set_exceptions_cmd (char *args, int from_tty)
2935e93f7393Sniklas {
2936e93f7393Sniklas   struct inf *inf = cur_inf ();
2937e93f7393Sniklas   int val = parse_bool_arg (args, "set exceptions");
2938e93f7393Sniklas 
2939e93f7393Sniklas   if (inf->task && inf->want_exceptions != val)
2940e93f7393Sniklas     /* Make this take effect immediately in a running process.  */
2941e93f7393Sniklas     /* XXX */ ;
2942e93f7393Sniklas 
2943e93f7393Sniklas   inf->want_exceptions = val;
2944e93f7393Sniklas }
2945e93f7393Sniklas 
2946e93f7393Sniklas static void
show_exceptions_cmd(char * args,int from_tty)2947e93f7393Sniklas show_exceptions_cmd (char *args, int from_tty)
2948e93f7393Sniklas {
2949e93f7393Sniklas   struct inf *inf = cur_inf ();
2950e93f7393Sniklas   check_empty (args, "show exceptions");
2951e93f7393Sniklas   printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2952e93f7393Sniklas 		     inf->task
2953e93f7393Sniklas 		     ? (inf->want_exceptions ? "are" : "aren't")
2954e93f7393Sniklas 		     : (inf->want_exceptions ? "will be" : "won't be"));
2955e93f7393Sniklas }
2956b725ae77Skettenis 
2957e93f7393Sniklas 
2958e93f7393Sniklas static void
set_task_cmd(char * args,int from_tty)2959e93f7393Sniklas set_task_cmd (char *args, int from_tty)
2960e93f7393Sniklas {
2961b725ae77Skettenis   printf_unfiltered ("\"set task\" must be followed by the name"
2962b725ae77Skettenis 		     " of a task property.\n");
2963e93f7393Sniklas }
2964e93f7393Sniklas 
2965e93f7393Sniklas static void
show_task_cmd(char * args,int from_tty)2966e93f7393Sniklas show_task_cmd (char *args, int from_tty)
2967e93f7393Sniklas {
2968e93f7393Sniklas   struct inf *inf = cur_inf ();
2969e93f7393Sniklas 
2970e93f7393Sniklas   check_empty (args, "show task");
2971e93f7393Sniklas 
2972e93f7393Sniklas   show_signals_cmd (0, from_tty);
2973e93f7393Sniklas   show_exceptions_cmd (0, from_tty);
2974e93f7393Sniklas   show_task_pause_cmd (0, from_tty);
2975e93f7393Sniklas 
2976e93f7393Sniklas   if (inf->pause_sc == 0)
2977e93f7393Sniklas     show_thread_default_pause_cmd (0, from_tty);
2978e93f7393Sniklas   show_thread_default_run_cmd (0, from_tty);
2979e93f7393Sniklas 
2980e93f7393Sniklas   if (inf->task)
2981e93f7393Sniklas     {
2982e93f7393Sniklas       show_stopped_cmd (0, from_tty);
2983e93f7393Sniklas       show_sig_thread_cmd (0, from_tty);
2984e93f7393Sniklas     }
2985e93f7393Sniklas 
2986e93f7393Sniklas   if (inf->detach_sc != 0)
2987e93f7393Sniklas     show_task_detach_sc_cmd (0, from_tty);
2988e93f7393Sniklas   if (inf->default_thread_detach_sc != 0)
2989e93f7393Sniklas     show_thread_default_detach_sc_cmd (0, from_tty);
2990e93f7393Sniklas }
2991b725ae77Skettenis 
2992e93f7393Sniklas 
2993e93f7393Sniklas static void
set_noninvasive_cmd(char * args,int from_tty)2994e93f7393Sniklas set_noninvasive_cmd (char *args, int from_tty)
2995e93f7393Sniklas {
2996e93f7393Sniklas   /* Invert the sense of the arg for each component.  */
2997e93f7393Sniklas   char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on";
2998e93f7393Sniklas 
2999e93f7393Sniklas   set_task_pause_cmd (inv_args, from_tty);
3000e93f7393Sniklas   set_signals_cmd (inv_args, from_tty);
3001e93f7393Sniklas   set_exceptions_cmd (inv_args, from_tty);
3002e93f7393Sniklas }
3003b725ae77Skettenis 
3004e93f7393Sniklas 
3005e93f7393Sniklas static void
info_port_rights(char * args,mach_port_type_t only)3006e93f7393Sniklas info_port_rights (char *args, mach_port_type_t only)
3007e93f7393Sniklas {
3008e93f7393Sniklas   struct inf *inf = active_inf ();
3009b725ae77Skettenis   struct value *vmark = value_mark ();
3010e93f7393Sniklas 
3011e93f7393Sniklas   if (args)
3012e93f7393Sniklas     /* Explicit list of port rights.  */
3013e93f7393Sniklas     {
3014e93f7393Sniklas       while (*args)
3015e93f7393Sniklas 	{
3016b725ae77Skettenis 	  struct value *val = parse_to_comma_and_eval (&args);
3017e93f7393Sniklas 	  long right = value_as_long (val);
3018e93f7393Sniklas 	  error_t err =
3019e93f7393Sniklas 	  print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
3020e93f7393Sniklas 			   stdout);
3021e93f7393Sniklas 	  if (err)
3022b725ae77Skettenis 	    error ("%ld: %s.", right, safe_strerror (err));
3023e93f7393Sniklas 	}
3024e93f7393Sniklas     }
3025e93f7393Sniklas   else
3026e93f7393Sniklas     /* Print all of them.  */
3027e93f7393Sniklas     {
3028e93f7393Sniklas       error_t err =
3029e93f7393Sniklas       print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
3030e93f7393Sniklas 			     stdout);
3031e93f7393Sniklas       if (err)
3032b725ae77Skettenis 	error ("%s.", safe_strerror (err));
3033e93f7393Sniklas     }
3034e93f7393Sniklas 
3035e93f7393Sniklas   value_free_to_mark (vmark);
3036e93f7393Sniklas }
3037e93f7393Sniklas 
3038e93f7393Sniklas static void
info_send_rights_cmd(char * args,int from_tty)3039e93f7393Sniklas info_send_rights_cmd (char *args, int from_tty)
3040e93f7393Sniklas {
3041e93f7393Sniklas   info_port_rights (args, MACH_PORT_TYPE_SEND);
3042e93f7393Sniklas }
3043b725ae77Skettenis 
3044e93f7393Sniklas static void
info_recv_rights_cmd(char * args,int from_tty)3045e93f7393Sniklas info_recv_rights_cmd (char *args, int from_tty)
3046e93f7393Sniklas {
3047e93f7393Sniklas   info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
3048e93f7393Sniklas }
3049b725ae77Skettenis 
3050e93f7393Sniklas static void
info_port_sets_cmd(char * args,int from_tty)3051e93f7393Sniklas info_port_sets_cmd (char *args, int from_tty)
3052e93f7393Sniklas {
3053e93f7393Sniklas   info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
3054e93f7393Sniklas }
3055b725ae77Skettenis 
3056e93f7393Sniklas static void
info_dead_names_cmd(char * args,int from_tty)3057e93f7393Sniklas info_dead_names_cmd (char *args, int from_tty)
3058e93f7393Sniklas {
3059e93f7393Sniklas   info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
3060e93f7393Sniklas }
3061b725ae77Skettenis 
3062e93f7393Sniklas static void
info_port_rights_cmd(char * args,int from_tty)3063e93f7393Sniklas info_port_rights_cmd (char *args, int from_tty)
3064e93f7393Sniklas {
3065e93f7393Sniklas   info_port_rights (args, ~0);
3066e93f7393Sniklas }
3067b725ae77Skettenis 
3068e93f7393Sniklas 
3069b725ae77Skettenis static void
add_task_commands(void)3070b725ae77Skettenis add_task_commands (void)
3071e93f7393Sniklas {
3072e93f7393Sniklas   add_cmd ("pause", class_run, set_thread_default_pause_cmd,
3073b725ae77Skettenis 	   "Set whether the new threads are suspended while gdb has control.\n\
3074b725ae77Skettenis This property normally has no effect because the whole task is\n\
3075b725ae77Skettenis suspended, however, that may be disabled with \"set task pause off\".\n\
3076b725ae77Skettenis The default value is \"off\".",
3077e93f7393Sniklas 	   &set_thread_default_cmd_list);
3078e93f7393Sniklas   add_cmd ("pause", no_class, show_thread_default_pause_cmd,
3079e93f7393Sniklas 	   "Show whether new threads are suspended while gdb has control.",
3080e93f7393Sniklas 	   &show_thread_default_cmd_list);
3081b725ae77Skettenis 
3082e93f7393Sniklas   add_cmd ("run", class_run, set_thread_default_run_cmd,
3083b725ae77Skettenis 	   "Set whether new threads are allowed to run \
3084b725ae77Skettenis (once gdb has noticed them).",
3085e93f7393Sniklas 	   &set_thread_default_cmd_list);
3086e93f7393Sniklas   add_cmd ("run", no_class, show_thread_default_run_cmd,
3087b725ae77Skettenis 	   "Show whether new threads are allowed to run \
3088b725ae77Skettenis (once gdb has noticed them).",
3089e93f7393Sniklas 	   &show_thread_default_cmd_list);
3090b725ae77Skettenis 
3091e93f7393Sniklas   add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
3092e93f7393Sniklas 	   "Set the default detach-suspend-count value for new threads.",
3093e93f7393Sniklas 	   &set_thread_default_cmd_list);
3094e93f7393Sniklas   add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
3095e93f7393Sniklas 	   "Show the default detach-suspend-count value for new threads.",
3096e93f7393Sniklas 	   &show_thread_default_cmd_list);
3097e93f7393Sniklas 
3098e93f7393Sniklas   add_cmd ("signals", class_run, set_signals_cmd,
3099b725ae77Skettenis 	   "Set whether the inferior process's signals will be intercepted.\n\
3100b725ae77Skettenis Mach exceptions (such as breakpoint traps) are not affected.",
3101e93f7393Sniklas 	   &setlist);
3102e93f7393Sniklas   add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
3103e93f7393Sniklas   add_cmd ("signals", no_class, show_signals_cmd,
3104e93f7393Sniklas 	   "Show whether the inferior process's signals will be intercepted.",
3105e93f7393Sniklas 	   &showlist);
3106e93f7393Sniklas   add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
3107e93f7393Sniklas 
3108e93f7393Sniklas   add_cmd ("signal-thread", class_run, set_sig_thread_cmd,
3109b725ae77Skettenis 	   "Set the thread that gdb thinks is the libc signal thread.\n\
3110b725ae77Skettenis This thread is run when delivering a signal to a non-stopped process.",
3111e93f7393Sniklas 	   &setlist);
3112e93f7393Sniklas   add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
3113e93f7393Sniklas   add_cmd ("signal-thread", no_class, show_sig_thread_cmd,
3114e93f7393Sniklas 	   "Set the thread that gdb thinks is the libc signal thread.",
3115e93f7393Sniklas 	   &showlist);
3116e93f7393Sniklas   add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
3117e93f7393Sniklas 
3118e93f7393Sniklas   add_cmd ("stopped", class_run, set_stopped_cmd,
3119b725ae77Skettenis 	   "Set whether gdb thinks the inferior process is stopped \
3120b725ae77Skettenis as with SIGSTOP.\n\
3121b725ae77Skettenis Stopped process will be continued by sending them a signal.",
3122e93f7393Sniklas 	   &setlist);
3123e93f7393Sniklas   add_cmd ("stopped", no_class, show_signals_cmd,
3124b725ae77Skettenis 	   "Show whether gdb thinks the inferior process is stopped \
3125b725ae77Skettenis as with SIGSTOP.",
3126e93f7393Sniklas 	   &showlist);
3127e93f7393Sniklas 
3128e93f7393Sniklas   add_cmd ("exceptions", class_run, set_exceptions_cmd,
3129b725ae77Skettenis 	   "Set whether exceptions in the inferior process will be trapped.\n\
3130b725ae77Skettenis When exceptions are turned off, neither breakpoints nor single-stepping\n\
3131b725ae77Skettenis will work.",
3132e93f7393Sniklas 	   &setlist);
3133e93f7393Sniklas   /* Allow `set exc' despite conflict with `set exception-port'.  */
3134e93f7393Sniklas   add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
3135e93f7393Sniklas   add_cmd ("exceptions", no_class, show_exceptions_cmd,
3136e93f7393Sniklas 	   "Show whether exceptions in the inferior process will be trapped.",
3137e93f7393Sniklas 	   &showlist);
3138e93f7393Sniklas 
3139e93f7393Sniklas   add_prefix_cmd ("task", no_class, set_task_cmd,
3140e93f7393Sniklas 		  "Command prefix for setting task attributes.",
3141e93f7393Sniklas 		  &set_task_cmd_list, "set task ", 0, &setlist);
3142e93f7393Sniklas   add_prefix_cmd ("task", no_class, show_task_cmd,
3143e93f7393Sniklas 		  "Command prefix for showing task attributes.",
3144e93f7393Sniklas 		  &show_task_cmd_list, "show task ", 0, &showlist);
3145e93f7393Sniklas 
3146e93f7393Sniklas   add_cmd ("pause", class_run, set_task_pause_cmd,
3147b725ae77Skettenis 	   "Set whether the task is suspended while gdb has control.\n\
3148b725ae77Skettenis A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3149b725ae77Skettenis until the next time the program is continued.\n\
3150b725ae77Skettenis When setting this to \"off\", \"set thread default pause on\" can be\n\
3151b725ae77Skettenis used to pause individual threads by default instead.",
3152e93f7393Sniklas 	   &set_task_cmd_list);
3153e93f7393Sniklas   add_cmd ("pause", no_class, show_task_pause_cmd,
3154e93f7393Sniklas 	   "Show whether the task is suspended while gdb has control.",
3155e93f7393Sniklas 	   &show_task_cmd_list);
3156b725ae77Skettenis 
3157e93f7393Sniklas   add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
3158e93f7393Sniklas 	   "Set the suspend count will leave on the thread when detaching.",
3159e93f7393Sniklas 	   &set_task_cmd_list);
3160e93f7393Sniklas   add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
3161e93f7393Sniklas 	   "Show the suspend count will leave on the thread when detaching.",
3162e93f7393Sniklas 	   &show_task_cmd_list);
3163e93f7393Sniklas 
3164e93f7393Sniklas   add_cmd ("exception-port", no_class, set_task_exc_port_cmd,
3165b725ae77Skettenis 	   "Set the task exception port to which we forward exceptions.\n\
3166b725ae77Skettenis The argument should be the value of the send right in the task.",
3167e93f7393Sniklas 	   &set_task_cmd_list);
3168e93f7393Sniklas   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
3169b725ae77Skettenis   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
3170b725ae77Skettenis 		 &set_task_cmd_list);
3171e93f7393Sniklas 
3172e93f7393Sniklas   /* A convenient way of turning on all options require to noninvasively
3173e93f7393Sniklas      debug running tasks.  */
3174e93f7393Sniklas   add_cmd ("noninvasive", no_class, set_noninvasive_cmd,
3175b725ae77Skettenis 	   "Set task options so that we interfere as little as possible.\n\
3176b725ae77Skettenis This is the same as setting `task pause', `exceptions', and\n\
3177b725ae77Skettenis `signals' to the opposite value.",
3178e93f7393Sniklas 	   &setlist);
3179e93f7393Sniklas 
3180e93f7393Sniklas   /* Commands to show information about the task's ports.  */
3181e93f7393Sniklas   add_cmd ("send-rights", class_info, info_send_rights_cmd,
3182e93f7393Sniklas 	   "Show information about the task's send rights",
3183e93f7393Sniklas 	   &infolist);
3184e93f7393Sniklas   add_cmd ("receive-rights", class_info, info_recv_rights_cmd,
3185e93f7393Sniklas 	   "Show information about the task's receive rights",
3186e93f7393Sniklas 	   &infolist);
3187b725ae77Skettenis   add_cmd ("port-rights", class_info, info_port_rights_cmd,
3188e93f7393Sniklas 	   "Show information about the task's port rights",
3189e93f7393Sniklas 	   &infolist);
3190e93f7393Sniklas   add_cmd ("port-sets", class_info, info_port_sets_cmd,
3191e93f7393Sniklas 	   "Show information about the task's port sets",
3192e93f7393Sniklas 	   &infolist);
3193e93f7393Sniklas   add_cmd ("dead-names", class_info, info_dead_names_cmd,
3194e93f7393Sniklas 	   "Show information about the task's dead names",
3195e93f7393Sniklas 	   &infolist);
3196e93f7393Sniklas   add_info_alias ("ports", "port-rights", 1);
3197e93f7393Sniklas   add_info_alias ("port", "port-rights", 1);
3198e93f7393Sniklas   add_info_alias ("psets", "port-sets", 1);
3199e93f7393Sniklas }
3200b725ae77Skettenis 
3201e93f7393Sniklas 
3202e93f7393Sniklas static void
set_thread_pause_cmd(char * args,int from_tty)3203e93f7393Sniklas set_thread_pause_cmd (char *args, int from_tty)
3204e93f7393Sniklas {
3205e93f7393Sniklas   struct proc *thread = cur_thread ();
3206e93f7393Sniklas   int old_sc = thread->pause_sc;
3207e93f7393Sniklas   thread->pause_sc = parse_bool_arg (args, "set thread pause");
3208e93f7393Sniklas   if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
3209e93f7393Sniklas     /* If the task is currently unsuspended, immediately suspend it,
3210e93f7393Sniklas        otherwise wait until the next time it gets control.  */
3211e93f7393Sniklas     inf_suspend (thread->inf);
3212e93f7393Sniklas }
3213e93f7393Sniklas 
3214e93f7393Sniklas static void
show_thread_pause_cmd(char * args,int from_tty)3215e93f7393Sniklas show_thread_pause_cmd (char *args, int from_tty)
3216e93f7393Sniklas {
3217e93f7393Sniklas   struct proc *thread = cur_thread ();
3218e93f7393Sniklas   int sc = thread->pause_sc;
3219e93f7393Sniklas   check_empty (args, "show task pause");
3220e93f7393Sniklas   printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
3221e93f7393Sniklas 		     proc_string (thread),
3222e93f7393Sniklas 		     sc ? "is" : "isn't",
3223e93f7393Sniklas 		     !sc && thread->inf->pause_sc ? " (but the task is)" : "");
3224e93f7393Sniklas }
3225e93f7393Sniklas 
3226e93f7393Sniklas static void
set_thread_run_cmd(char * args,int from_tty)3227e93f7393Sniklas set_thread_run_cmd (char *args, int from_tty)
3228e93f7393Sniklas {
3229e93f7393Sniklas   struct proc *thread = cur_thread ();
3230e93f7393Sniklas   thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
3231e93f7393Sniklas }
3232e93f7393Sniklas 
3233e93f7393Sniklas static void
show_thread_run_cmd(char * args,int from_tty)3234e93f7393Sniklas show_thread_run_cmd (char *args, int from_tty)
3235e93f7393Sniklas {
3236e93f7393Sniklas   struct proc *thread = cur_thread ();
3237e93f7393Sniklas   check_empty (args, "show thread run");
3238e93f7393Sniklas   printf_unfiltered ("Thread %s %s allowed to run.",
3239e93f7393Sniklas 		     proc_string (thread),
3240e93f7393Sniklas 		     thread->run_sc == 0 ? "is" : "isn't");
3241e93f7393Sniklas }
3242e93f7393Sniklas 
3243e93f7393Sniklas static void
set_thread_detach_sc_cmd(char * args,int from_tty)3244e93f7393Sniklas set_thread_detach_sc_cmd (char *args, int from_tty)
3245e93f7393Sniklas {
3246b725ae77Skettenis   cur_thread ()->detach_sc = parse_int_arg (args,
3247b725ae77Skettenis 					    "set thread detach-suspend-count");
3248e93f7393Sniklas }
3249e93f7393Sniklas 
3250e93f7393Sniklas static void
show_thread_detach_sc_cmd(char * args,int from_tty)3251e93f7393Sniklas show_thread_detach_sc_cmd (char *args, int from_tty)
3252e93f7393Sniklas {
3253e93f7393Sniklas   struct proc *thread = cur_thread ();
3254e93f7393Sniklas   check_empty (args, "show thread detach-suspend-count");
3255b725ae77Skettenis   printf_unfiltered ("Thread %s will be left with a suspend count"
3256b725ae77Skettenis 		     " of %d when detaching.\n",
3257e93f7393Sniklas 		     proc_string (thread),
3258e93f7393Sniklas 		     thread->detach_sc);
3259e93f7393Sniklas }
3260e93f7393Sniklas 
3261e93f7393Sniklas static void
set_thread_exc_port_cmd(char * args,int from_tty)3262e93f7393Sniklas set_thread_exc_port_cmd (char *args, int from_tty)
3263e93f7393Sniklas {
3264e93f7393Sniklas   struct proc *thread = cur_thread ();
3265e93f7393Sniklas   if (!args)
3266e93f7393Sniklas     error ("No argument to \"set thread exception-port\" command.");
3267e93f7393Sniklas   steal_exc_port (thread, parse_and_eval_address (args));
3268e93f7393Sniklas }
3269e93f7393Sniklas 
3270b725ae77Skettenis #if 0
3271e93f7393Sniklas static void
3272e93f7393Sniklas show_thread_cmd (char *args, int from_tty)
3273e93f7393Sniklas {
3274e93f7393Sniklas   struct proc *thread = cur_thread ();
3275e93f7393Sniklas   check_empty (args, "show thread");
3276e93f7393Sniklas   show_thread_run_cmd (0, from_tty);
3277e93f7393Sniklas   show_thread_pause_cmd (0, from_tty);
3278e93f7393Sniklas   if (thread->detach_sc != 0)
3279e93f7393Sniklas     show_thread_detach_sc_cmd (0, from_tty);
3280e93f7393Sniklas }
3281b725ae77Skettenis #endif
3282e93f7393Sniklas 
3283e93f7393Sniklas static void
thread_takeover_sc_cmd(char * args,int from_tty)3284e93f7393Sniklas thread_takeover_sc_cmd (char *args, int from_tty)
3285e93f7393Sniklas {
3286e93f7393Sniklas   struct proc *thread = cur_thread ();
3287e93f7393Sniklas   thread_basic_info_data_t _info;
3288e93f7393Sniklas   thread_basic_info_t info = &_info;
3289e93f7393Sniklas   mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
3290e93f7393Sniklas   error_t err =
3291e93f7393Sniklas   thread_info (thread->port, THREAD_BASIC_INFO, (int *) &info, &info_len);
3292e93f7393Sniklas   if (err)
3293b725ae77Skettenis     error ("%s.", safe_strerror (err));
3294e93f7393Sniklas   thread->sc = info->suspend_count;
3295e93f7393Sniklas   if (from_tty)
3296e93f7393Sniklas     printf_unfiltered ("Suspend count was %d.\n", thread->sc);
3297e93f7393Sniklas   if (info != &_info)
3298b725ae77Skettenis     vm_deallocate (mach_task_self (), (vm_address_t) info,
3299b725ae77Skettenis 		   info_len * sizeof (int));
3300e93f7393Sniklas }
3301e93f7393Sniklas 
3302b725ae77Skettenis 
3303b725ae77Skettenis static void
add_thread_commands(void)3304b725ae77Skettenis add_thread_commands (void)
3305e93f7393Sniklas {
3306b725ae77Skettenis   add_prefix_cmd ("thread", no_class, set_thread_cmd,
3307b725ae77Skettenis 		  "Command prefix for setting thread properties.",
3308b725ae77Skettenis 		  &set_thread_cmd_list, "set thread ", 0, &setlist);
3309b725ae77Skettenis   add_prefix_cmd ("default", no_class, show_thread_cmd,
3310b725ae77Skettenis 		  "Command prefix for setting default thread properties.",
3311b725ae77Skettenis 		  &set_thread_default_cmd_list, "set thread default ", 0,
3312b725ae77Skettenis 		  &set_thread_cmd_list);
3313b725ae77Skettenis   add_prefix_cmd ("thread", no_class, set_thread_default_cmd,
3314b725ae77Skettenis 		  "Command prefix for showing thread properties.",
3315b725ae77Skettenis 		  &show_thread_cmd_list, "show thread ", 0, &showlist);
3316b725ae77Skettenis   add_prefix_cmd ("default", no_class, show_thread_default_cmd,
3317b725ae77Skettenis 		  "Command prefix for showing default thread properties.",
3318b725ae77Skettenis 		  &show_thread_default_cmd_list, "show thread default ", 0,
3319b725ae77Skettenis 		  &show_thread_cmd_list);
3320b725ae77Skettenis 
3321e93f7393Sniklas   add_cmd ("pause", class_run, set_thread_pause_cmd,
3322b725ae77Skettenis 	   "Set whether the current thread is suspended \
3323b725ae77Skettenis while gdb has control.\n\
3324b725ae77Skettenis A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3325b725ae77Skettenis until the next time the program is continued.  This property normally\n\
3326b725ae77Skettenis has no effect because the whole task is suspended, however, that may\n\
3327b725ae77Skettenis be disabled with \"set task pause off\".\n\
3328b725ae77Skettenis The default value is \"off\".",
3329e93f7393Sniklas 	   &set_thread_cmd_list);
3330e93f7393Sniklas   add_cmd ("pause", no_class, show_thread_pause_cmd,
3331b725ae77Skettenis 	   "Show whether the current thread is suspended \
3332b725ae77Skettenis while gdb has control.",
3333e93f7393Sniklas 	   &show_thread_cmd_list);
3334e93f7393Sniklas 
3335e93f7393Sniklas   add_cmd ("run", class_run, set_thread_run_cmd,
3336e93f7393Sniklas 	   "Set whether the current thread is allowed to run.",
3337e93f7393Sniklas 	   &set_thread_cmd_list);
3338e93f7393Sniklas   add_cmd ("run", no_class, show_thread_run_cmd,
3339e93f7393Sniklas 	   "Show whether the current thread is allowed to run.",
3340e93f7393Sniklas 	   &show_thread_cmd_list);
3341e93f7393Sniklas 
3342e93f7393Sniklas   add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd,
3343b725ae77Skettenis 	   "Set the suspend count will leave on the thread when detaching.\n\
3344b725ae77Skettenis Note that this is relative to suspend count when gdb noticed the thread;\n\
3345b725ae77Skettenis use the `thread takeover-suspend-count' to force it to an absolute value.",
3346e93f7393Sniklas 	   &set_thread_cmd_list);
3347e93f7393Sniklas   add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd,
3348b725ae77Skettenis 	   "Show the suspend count will leave on the thread when detaching.\n\
3349b725ae77Skettenis Note that this is relative to suspend count when gdb noticed the thread;\n\
3350b725ae77Skettenis use the `thread takeover-suspend-count' to force it to an absolute value.",
3351e93f7393Sniklas 	   &show_thread_cmd_list);
3352e93f7393Sniklas 
3353e93f7393Sniklas   add_cmd ("exception-port", no_class, set_thread_exc_port_cmd,
3354b725ae77Skettenis 	   "Set the thread exception port to which we forward exceptions.\n\
3355b725ae77Skettenis This overrides the task exception port.\n\
3356b725ae77Skettenis The argument should be the value of the send right in the task.",
3357e93f7393Sniklas 	   &set_thread_cmd_list);
3358e93f7393Sniklas   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
3359b725ae77Skettenis   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
3360b725ae77Skettenis 		 &set_thread_cmd_list);
3361e93f7393Sniklas 
3362e93f7393Sniklas   add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd,
3363b725ae77Skettenis 	   "Force the threads absolute suspend-count to be gdb's.\n\
3364b725ae77Skettenis Prior to giving this command, gdb's thread suspend-counts are relative\n\
3365b725ae77Skettenis to the thread's initial suspend-count when gdb notices the threads.",
3366e93f7393Sniklas 	   &thread_cmd_list);
3367e93f7393Sniklas }
3368b725ae77Skettenis 
3369e93f7393Sniklas 
3370e93f7393Sniklas void
_initialize_gnu_nat(void)3371b725ae77Skettenis _initialize_gnu_nat (void)
3372e93f7393Sniklas {
3373e93f7393Sniklas   proc_server = getproc ();
3374e93f7393Sniklas 
3375b725ae77Skettenis   init_gnu_ops ();
3376e93f7393Sniklas   add_target (&gnu_ops);
3377e93f7393Sniklas 
3378e93f7393Sniklas   add_task_commands ();
3379e93f7393Sniklas   add_thread_commands ();
3380e93f7393Sniklas   add_set_cmd ("gnu-debug", class_maintenance,
3381e93f7393Sniklas 	       var_boolean, (char *) &gnu_debug_flag,
3382e93f7393Sniklas 	       "Set debugging output for the gnu backend.", &maintenancelist);
3383e93f7393Sniklas }
3384e93f7393Sniklas 
3385e93f7393Sniklas #ifdef	FLUSH_INFERIOR_CACHE
3386e93f7393Sniklas 
3387e93f7393Sniklas /* When over-writing code on some machines the I-Cache must be flushed
3388e93f7393Sniklas    explicitly, because it is not kept coherent by the lazy hardware.
3389e93f7393Sniklas    This definitely includes breakpoints, for instance, or else we
3390e93f7393Sniklas    end up looping in mysterious Bpt traps */
3391e93f7393Sniklas 
3392e93f7393Sniklas void
flush_inferior_icache(CORE_ADDR pc,int amount)3393b725ae77Skettenis flush_inferior_icache (CORE_ADDR pc, int amount)
3394e93f7393Sniklas {
3395e93f7393Sniklas   vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
3396e93f7393Sniklas   error_t ret;
3397e93f7393Sniklas 
3398e93f7393Sniklas   ret = vm_machine_attribute (current_inferior->task->port,
3399e93f7393Sniklas 			      pc,
3400e93f7393Sniklas 			      amount,
3401e93f7393Sniklas 			      MATTR_CACHE,
3402e93f7393Sniklas 			      &flush);
3403e93f7393Sniklas   if (ret != KERN_SUCCESS)
3404b725ae77Skettenis     warning ("Error flushing inferior's cache : %s", safe_strerror (ret));
3405e93f7393Sniklas }
3406b725ae77Skettenis #endif /* FLUSH_INFERIOR_CACHE */
3407