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