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