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