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