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