1 /* Interface GDB to the GNU Hurd.
2 Copyright (C) 1992, 95, 96, 97, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 Written by Miles Bader <miles@gnu.ai.mit.edu>
8 Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
33 #include <sys/ptrace.h>
36 #include <mach_error.h>
37 #include <mach/exception.h>
38 #include <mach/message.h>
39 #include <mach/notify.h>
40 #include <mach/vm_attributes.h>
43 #include <hurd/interrupt.h>
45 #include <hurd/msg_request.h>
46 #include <hurd/process.h>
47 #include <hurd/process_request.h>
48 #include <hurd/signal.h>
49 #include <hurd/sigpreempt.h>
62 #include "gdbthread.h"
66 #include "exc_request_S.h"
68 #include "process_reply_S.h"
69 #include "msg_reply_S.h"
70 #include "exc_request_U.h"
73 static process_t proc_server = MACH_PORT_NULL;
75 /* If we've sent a proc_wait_request to the proc server, the pid of the
76 process we asked about. We can only ever have one outstanding. */
77 int proc_wait_pid = 0;
79 /* The number of wait requests we've sent, and expect replies from. */
80 int proc_waits_pending = 0;
82 int gnu_debug_flag = 0;
86 extern struct target_ops gnu_ops;
88 struct inf *make_inf ();
89 void inf_clear_wait (struct inf *inf);
90 void inf_cleanup (struct inf *inf);
91 void inf_startup (struct inf *inf, int pid);
92 int inf_update_suspends (struct inf *inf);
93 void inf_set_pid (struct inf *inf, pid_t pid);
94 void inf_validate_procs (struct inf *inf);
95 void inf_steal_exc_ports (struct inf *inf);
96 void inf_restore_exc_ports (struct inf *inf);
97 struct proc *inf_tid_to_proc (struct inf *inf, int tid);
98 inline void inf_set_threads_resume_sc (struct inf *inf,
99 struct proc *run_thread,
101 inline int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
102 inline void inf_suspend (struct inf *inf);
103 inline void inf_resume (struct inf *inf);
104 void inf_set_step_thread (struct inf *inf, struct proc *proc);
105 void inf_detach (struct inf *inf);
106 void inf_attach (struct inf *inf, int pid);
107 void inf_signal (struct inf *inf, enum target_signal sig);
108 void inf_continue (struct inf *inf);
110 #define inf_debug(_inf, msg, args...) \
111 do { struct inf *__inf = (_inf); \
112 debug ("{inf %d %p}: " msg, __inf->pid, __inf , ##args); } while (0)
114 void proc_abort (struct proc *proc, int force);
115 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
116 struct proc *_proc_free (struct proc *proc);
117 int proc_update_sc (struct proc *proc);
118 error_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
119 error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
120 static mach_port_t _proc_get_exc_port (struct proc *proc);
121 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
122 void proc_restore_exc_port (struct proc *proc);
123 int proc_trace (struct proc *proc, int set);
125 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
126 to INF's msg port and task port respectively. If it has no msg port,
127 EIEIO is returned. INF must refer to a running process! */
128 #define INF_MSGPORT_RPC(inf, rpc_expr) \
129 HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
130 (refport = inf->task->port, 0), 0, \
131 msgport ? (rpc_expr) : EIEIO)
133 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
134 there's someone around to deal with the RPC (and resuspend things
135 afterwards). This effects INF's threads' resume_sc count. */
136 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
137 (inf_set_threads_resume_sc_for_signal_thread (inf) \
140 __e = INF_MSGPORT_RPC (inf, rpc_expr); \
146 /* The state passed by an exception message. */
149 int exception; /* The exception code */
151 mach_port_t handler; /* The real exception port to handle this. */
152 mach_port_t reply; /* The reply port from the exception call. */
155 /* The results of the last wait an inf did. */
158 struct target_waitstatus status; /* The status returned to gdb. */
159 struct exc_state exc; /* The exception that caused us to return. */
160 struct proc *thread; /* The thread in question. */
161 int suppress; /* Something trivial happened. */
164 /* The state of an inferior. */
167 /* Fields describing the current inferior. */
169 struct proc *task; /* The mach task. */
170 struct proc *threads; /* A linked list of all threads in TASK. */
172 /* True if THREADS needn't be validated by querying the task. We assume that
173 we and the task in question are the only ones frobbing the thread list,
174 so as long as we don't let any code run, we don't have to worry about
176 int threads_up_to_date;
178 pid_t pid; /* The real system PID. */
180 struct inf_wait wait; /* What to return from target_wait. */
182 /* One thread proc in INF may be in `single-stepping mode'. This is it. */
183 struct proc *step_thread;
185 /* The thread we think is the signal thread. */
186 struct proc *signal_thread;
188 mach_port_t event_port; /* Where we receive various msgs. */
190 /* True if we think at least one thread in the inferior could currently be
192 unsigned int running:1;
194 /* True if the process has stopped (in the proc server sense). Note that
195 since a proc server `stop' leaves the signal thread running, the inf can
196 be RUNNING && STOPPED... */
197 unsigned int stopped:1;
199 /* True if the inferior has no message port. */
200 unsigned int nomsg:1;
202 /* True if the inferior is traced. */
203 unsigned int traced:1;
205 /* True if we shouldn't try waiting for the inferior, usually because we
206 can't for some reason. */
207 unsigned int no_wait:1;
209 /* When starting a new inferior, we don't try to validate threads until all
210 the proper execs have been done. This is a count of how many execs we
212 unsigned pending_execs;
214 /* Fields describing global state */
216 /* The task suspend count used when gdb has control. This is normally 1 to
217 make things easier for us, but sometimes (like when attaching to vital
218 system servers) it may be desirable to let the task continue to run
219 (pausing individual threads as necessary). */
222 /* The task suspend count left when detaching from a task. */
225 /* The initial values used for the run_sc and pause_sc of newly discovered
226 threads -- see the definition of those fields in struct proc. */
227 int default_thread_run_sc;
228 int default_thread_pause_sc;
229 int default_thread_detach_sc;
231 /* True if the process should be traced when started/attached. Newly
232 started processes *must* be traced at first to exec them properly, but
233 if this is false, tracing is turned off as soon it has done so. */
236 /* True if exceptions from the inferior process should be trapped. This
237 must be on to use breakpoints. */
243 __proc_pid (struct proc *proc)
245 return proc->inf->pid;
249 /* Update PROC's real suspend count to match it's desired one. Returns true
250 if we think PROC is now in a runnable state. */
252 proc_update_sc (struct proc *proc)
256 int delta = proc->sc - proc->cur_sc;
259 proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
261 if (proc->sc == 0 && proc->state_changed)
262 /* Since PROC may start running, we must write back any state changes. */
264 assert (proc_is_thread (proc));
265 proc_debug (proc, "storing back changed thread state");
266 err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
267 (thread_state_t) &proc->state, THREAD_STATE_SIZE);
269 proc->state_changed = 0;
274 while (delta-- > 0 && !err)
276 if (proc_is_task (proc))
277 err = task_suspend (proc->port);
279 err = thread_suspend (proc->port);
284 while (delta++ < 0 && !err)
286 if (proc_is_task (proc))
287 err = task_resume (proc->port);
289 err = thread_resume (proc->port);
293 proc->cur_sc = proc->sc;
295 /* If we got an error, then the task/thread has disappeared. */
296 running = !err && proc->sc == 0;
298 proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
300 proc_debug (proc, "err = %s", strerror (err));
305 proc->state_valid = proc->state_changed = 0;
306 proc->fetched_regs = 0;
313 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
314 If PROC is deemed `precious', then nothing is done unless FORCE is true.
315 In particular, a thread is precious if it's running (in which case forcing
316 it includes suspending it first), or if it has an exception pending. */
318 proc_abort (struct proc *proc, int force)
320 assert (proc_is_thread (proc));
324 struct inf *inf = proc->inf;
325 int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
327 if (running && force)
330 inf_update_suspends (proc->inf);
332 warning ("Stopped %s.", proc_string (proc));
334 else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
335 /* An exception is pending on PROC, which don't mess with. */
339 /* We only abort the thread if it's not actually running. */
341 thread_abort (proc->port);
342 proc_debug (proc, "aborted");
346 proc_debug (proc, "not aborting");
350 /* Make sure that the state field in PROC is up to date, and return a pointer
351 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
352 that the thread is stopped and aborted first, and sets the state_changed
353 field in PROC to true. */
355 proc_get_state (struct proc *proc, int will_modify)
357 int was_aborted = proc->aborted;
359 proc_debug (proc, "updating state info%s",
360 will_modify ? " (with intention to modify)" : "");
362 proc_abort (proc, will_modify);
364 if (!was_aborted && proc->aborted)
365 /* PROC's state may have changed since we last fetched it. */
366 proc->state_valid = 0;
368 if (!proc->state_valid)
370 mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
372 thread_get_state (proc->port, THREAD_STATE_FLAVOR,
373 (thread_state_t) &proc->state, &state_size);
374 proc_debug (proc, "getting thread state");
375 proc->state_valid = !err;
378 if (proc->state_valid)
381 proc->state_changed = 1;
382 return (thread_state_t) &proc->state;
389 /* Set PORT to PROC's exception port. */
391 proc_get_exception_port (struct proc * proc, mach_port_t * port)
393 if (proc_is_task (proc))
394 return task_get_exception_port (proc->port, port);
396 return thread_get_exception_port (proc->port, port);
399 /* Set PROC's exception port to PORT. */
401 proc_set_exception_port (struct proc * proc, mach_port_t port)
403 proc_debug (proc, "setting exception port: %d", port);
404 if (proc_is_task (proc))
405 return task_set_exception_port (proc->port, port);
407 return thread_set_exception_port (proc->port, port);
410 /* Get PROC's exception port, cleaning up a bit if proc has died. */
412 _proc_get_exc_port (struct proc *proc)
414 mach_port_t exc_port;
415 error_t err = proc_get_exception_port (proc, &exc_port);
418 /* PROC must be dead. */
421 mach_port_deallocate (mach_task_self (), proc->exc_port);
422 proc->exc_port = MACH_PORT_NULL;
423 if (proc->saved_exc_port)
424 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
425 proc->saved_exc_port = MACH_PORT_NULL;
431 /* Replace PROC's exception port with EXC_PORT, unless it's already been
432 done. Stash away any existing exception port so we can restore it later. */
434 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
436 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
442 proc_debug (proc, "inserting exception port: %d", exc_port);
444 if (cur_exc_port != exc_port)
445 /* Put in our exception port. */
446 err = proc_set_exception_port (proc, exc_port);
448 if (err || cur_exc_port == proc->exc_port)
449 /* We previously set the exception port, and it's still set. So we
450 just keep the old saved port which is what the proc set. */
453 mach_port_deallocate (mach_task_self (), cur_exc_port);
456 /* Keep a copy of PROC's old exception port so it can be restored. */
458 if (proc->saved_exc_port)
459 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
460 proc->saved_exc_port = cur_exc_port;
463 proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
466 proc->exc_port = exc_port;
468 warning ("Error setting exception port for %s: %s",
469 proc_string (proc), strerror (err));
473 /* If we previously replaced PROC's exception port, put back what we
474 found there at the time, unless *our* exception port has since been
475 overwritten, in which case who knows what's going on. */
477 proc_restore_exc_port (struct proc *proc)
479 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
485 proc_debug (proc, "restoring real exception port");
487 if (proc->exc_port == cur_exc_port)
488 /* Our's is still there. */
489 err = proc_set_exception_port (proc, proc->saved_exc_port);
491 if (proc->saved_exc_port)
492 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
493 proc->saved_exc_port = MACH_PORT_NULL;
496 proc->exc_port = MACH_PORT_NULL;
498 warning ("Error setting exception port for %s: %s",
499 proc_string (proc), strerror (err));
504 /* Turns hardware tracing in PROC on or off when SET is true or false,
505 respectively. Returns true on success. */
507 proc_trace (struct proc *proc, int set)
509 thread_state_t state = proc_get_state (proc, 1);
512 return 0; /* the thread must be dead. */
514 proc_debug (proc, "tracing %s", set ? "on" : "off");
518 /* XXX We don't get the exception unless the thread has its own
519 exception port???? */
520 if (proc->exc_port == MACH_PORT_NULL)
521 proc_steal_exc_port (proc, proc->inf->event_port);
522 THREAD_STATE_SET_TRACED (state);
525 THREAD_STATE_CLEAR_TRACED (state);
531 /* A variable from which to assign new TIDs. */
532 static int next_thread_id = 1;
534 /* Returns a new proc structure with the given fields. Also adds a
535 notification for PORT becoming dead to be sent to INF's notify port. */
537 make_proc (struct inf *inf, mach_port_t port, int tid)
540 mach_port_t prev_port = MACH_PORT_NULL;
541 struct proc *proc = malloc (sizeof (struct proc));
547 proc->saved_exc_port = MACH_PORT_NULL;
548 proc->exc_port = MACH_PORT_NULL;
553 /* Note that these are all the values for threads; the task simply uses the
554 corresponding field in INF directly. */
555 proc->run_sc = inf->default_thread_run_sc;
556 proc->pause_sc = inf->default_thread_pause_sc;
557 proc->detach_sc = inf->default_thread_detach_sc;
558 proc->resume_sc = proc->run_sc;
562 proc->state_valid = 0;
563 proc->state_changed = 0;
565 proc_debug (proc, "is new");
567 /* Get notified when things die. */
569 mach_port_request_notification (mach_task_self (), port,
570 MACH_NOTIFY_DEAD_NAME, 1,
572 MACH_MSG_TYPE_MAKE_SEND_ONCE,
575 warning ("Couldn't request notification for port %d: %s",
576 port, strerror (err));
579 proc_debug (proc, "notifications to: %d", inf->event_port);
580 if (prev_port != MACH_PORT_NULL)
581 mach_port_deallocate (mach_task_self (), prev_port);
584 if (inf->want_exceptions)
586 if (proc_is_task (proc))
587 /* Make the task exception port point to us. */
588 proc_steal_exc_port (proc, inf->event_port);
590 /* Just clear thread exception ports -- they default to the
592 proc_steal_exc_port (proc, MACH_PORT_NULL);
598 /* Frees PROC and any resources it uses, and returns the value of PROC's
601 _proc_free (struct proc *proc)
603 struct inf *inf = proc->inf;
604 struct proc *next = proc->next;
606 proc_debug (proc, "freeing...");
608 if (proc == inf->step_thread)
609 /* Turn off single stepping. */
610 inf_set_step_thread (inf, 0);
611 if (proc == inf->wait.thread)
612 inf_clear_wait (inf);
613 if (proc == inf->signal_thread)
614 inf->signal_thread = 0;
616 if (proc->port != MACH_PORT_NULL)
618 if (proc->exc_port != MACH_PORT_NULL)
619 /* Restore the original exception port. */
620 proc_restore_exc_port (proc);
621 if (proc->cur_sc != 0)
622 /* Resume the thread/task. */
625 proc_update_sc (proc);
627 mach_port_deallocate (mach_task_self (), proc->port);
638 struct inf *inf = malloc (sizeof (struct inf));
645 inf->threads_up_to_date = 0;
647 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
648 inf->wait.thread = 0;
649 inf->wait.exc.handler = MACH_PORT_NULL;
650 inf->wait.exc.reply = MACH_PORT_NULL;
651 inf->step_thread = 0;
652 inf->signal_thread = 0;
653 inf->event_port = MACH_PORT_NULL;
659 inf->pending_execs = 0;
662 inf->default_thread_run_sc = 0;
663 inf->default_thread_pause_sc = 0;
664 inf->default_thread_detach_sc = 0;
665 inf->want_signals = 1; /* By default */
666 inf->want_exceptions = 1; /* By default */
671 /* Clear INF's target wait status. */
673 inf_clear_wait (struct inf *inf)
675 inf_debug (inf, "clearing wait");
676 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
677 inf->wait.thread = 0;
678 inf->wait.suppress = 0;
679 if (inf->wait.exc.handler != MACH_PORT_NULL)
681 mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
682 inf->wait.exc.handler = MACH_PORT_NULL;
684 if (inf->wait.exc.reply != MACH_PORT_NULL)
686 mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
687 inf->wait.exc.reply = MACH_PORT_NULL;
693 inf_cleanup (struct inf *inf)
695 inf_debug (inf, "cleanup");
697 inf_clear_wait (inf);
699 inf_set_pid (inf, -1);
706 inf->pending_execs = 0;
710 mach_port_destroy (mach_task_self (), inf->event_port);
711 inf->event_port = MACH_PORT_NULL;
716 inf_startup (struct inf *inf, int pid)
720 inf_debug (inf, "startup: pid = %d", pid);
724 /* Make the port on which we receive all events. */
725 err = mach_port_allocate (mach_task_self (),
726 MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
728 error ("Error allocating event port: %s", strerror (err));
730 /* Make a send right for it, so we can easily copy it for other people. */
731 mach_port_insert_right (mach_task_self (), inf->event_port,
732 inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
733 inf_set_pid (inf, pid);
737 /* Close current process, if any, and attach INF to process PORT. */
739 inf_set_pid (struct inf *inf, pid_t pid)
742 struct proc *task = inf->task;
744 inf_debug (inf, "setting pid: %d", pid);
747 task_port = MACH_PORT_NULL;
750 error_t err = proc_pid2task (proc_server, pid, &task_port);
752 error ("Error getting task for pid %d: %s", pid, strerror (err));
755 inf_debug (inf, "setting task: %d", task_port);
758 task_suspend (task_port);
760 if (task && task->port != task_port)
763 inf_validate_procs (inf); /* Trash all the threads. */
764 _proc_free (task); /* And the task. */
767 if (task_port != MACH_PORT_NULL)
769 inf->task = make_proc (inf, task_port, PROC_TID_TASK);
770 inf->threads_up_to_date = 0;
777 /* Reflect task_suspend above. */
778 inf->task->sc = inf->task->cur_sc = 1;
785 /* Validates INF's stopped, nomsg and traced field from the actual
786 proc server state. Note that the traced field is only updated from
787 the proc server state if we do not have a message port. If we do
788 have a message port we'd better look at the tracemask itself. */
790 inf_validate_procinfo (struct inf *inf)
793 mach_msg_type_number_t noise_len = 0;
795 mach_msg_type_number_t pi_len = 0;
798 proc_getprocinfo (proc_server, inf->pid, &info_flags,
799 (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
803 inf->stopped = !!(pi->state & PI_STOPPED);
804 inf->nomsg = !!(pi->state & PI_NOMSG);
806 inf->traced = !!(pi->state & PI_TRACED);
807 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
809 vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
813 /* Validates INF's task suspend count. If it's higher than we expect,
814 verify with the user before `stealing' the extra count. */
816 inf_validate_task_sc (struct inf *inf)
819 mach_msg_type_number_t noise_len = 0;
821 mach_msg_type_number_t pi_len = 0;
822 int info_flags = PI_FETCH_TASKINFO;
823 int suspend_count = -1;
827 err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
828 (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
831 inf->task->dead = 1; /* oh well */
835 if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
837 /* The proc server might have suspended the task while stopping
838 it. This happens when the task is handling a traced signal.
839 Refetch the suspend count. The proc server should be
840 finished stopping the task by now. */
841 suspend_count = pi->taskinfo.suspend_count;
845 suspend_count = pi->taskinfo.suspend_count;
847 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
849 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
851 if (inf->task->cur_sc < suspend_count)
855 target_terminal_ours (); /* Allow I/O. */
856 abort = !query ("Pid %d has an additional task suspend count of %d;"
857 " clear it? ", inf->pid,
858 suspend_count - inf->task->cur_sc);
859 target_terminal_inferior (); /* Give it back to the child. */
862 error ("Additional task suspend count left untouched.");
864 inf->task->cur_sc = suspend_count;
868 /* Turns tracing for INF on or off, depending on ON, unless it already
869 is. If INF is running, the resume_sc count of INF's threads will
870 be modified, and the signal thread will briefly be run to change
873 inf_set_traced (struct inf *inf, int on)
875 if (on == inf->traced)
878 if (inf->task && !inf->task->dead)
879 /* Make it take effect immediately. */
881 sigset_t mask = on ? ~(sigset_t) 0 : 0;
883 INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
884 INIT_TRACEMASK, mask));
888 warning ("Can't modify tracing state for pid %d: %s",
889 inf->pid, "No signal thread");
893 warning ("Can't modify tracing state for pid %d: %s",
894 inf->pid, strerror (err));
903 /* Makes all the real suspend count deltas of all the procs in INF
904 match the desired values. Careful to always do thread/task suspend
905 counts in the safe order. Returns true if at least one thread is
906 thought to be running. */
908 inf_update_suspends (struct inf *inf)
910 struct proc *task = inf->task;
911 /* We don't have to update INF->threads even though we're iterating over it
912 because we'll change a thread only if it already has an existing proc
915 inf_debug (inf, "updating suspend counts");
920 int task_running = (task->sc == 0), thread_running = 0;
922 if (task->sc > task->cur_sc)
923 /* The task is becoming _more_ suspended; do before any threads. */
924 task_running = proc_update_sc (task);
926 if (inf->pending_execs)
927 /* When we're waiting for an exec, things may be happening behind our
928 back, so be conservative. */
931 /* Do all the thread suspend counts. */
932 for (thread = inf->threads; thread; thread = thread->next)
933 thread_running |= proc_update_sc (thread);
935 if (task->sc != task->cur_sc)
936 /* We didn't do the task first, because we wanted to wait for the
937 threads; do it now. */
938 task_running = proc_update_sc (task);
940 inf_debug (inf, "%srunning...",
941 (thread_running && task_running) ? "" : "not ");
943 inf->running = thread_running && task_running;
945 /* Once any thread has executed some code, we can't depend on the
946 threads list any more. */
948 inf->threads_up_to_date = 0;
957 /* Converts a GDB pid to a struct proc. */
959 inf_tid_to_thread (struct inf *inf, int tid)
961 struct proc *thread = inf->threads;
964 if (thread->tid == tid)
967 thread = thread->next;
971 /* Converts a thread port to a struct proc. */
973 inf_port_to_thread (struct inf *inf, mach_port_t port)
975 struct proc *thread = inf->threads;
977 if (thread->port == port)
980 thread = thread->next;
985 /* Make INF's list of threads be consistent with reality of TASK. */
987 inf_validate_procs (struct inf *inf)
989 thread_array_t threads;
990 mach_msg_type_number_t num_threads, i;
991 struct proc *task = inf->task;
993 /* If no threads are currently running, this function will guarantee that
994 things are up to date. The exception is if there are zero threads --
995 then it is almost certainly in an odd state, and probably some outside
996 agent will create threads. */
997 inf->threads_up_to_date = inf->threads ? !inf->running : 0;
1001 error_t err = task_threads (task->port, &threads, &num_threads);
1002 inf_debug (inf, "fetching threads");
1004 /* TASK must be dead. */
1014 inf_debug (inf, "no task");
1018 /* Make things normally linear. */
1019 mach_msg_type_number_t search_start = 0;
1020 /* Which thread in PROCS corresponds to each task thread, & the task. */
1021 struct proc *matched[num_threads + 1];
1022 /* The last thread in INF->threads, so we can add to the end. */
1023 struct proc *last = 0;
1024 /* The current thread we're considering. */
1025 struct proc *thread = inf->threads;
1027 bzero (matched, sizeof (matched));
1031 mach_msg_type_number_t left;
1033 for (i = search_start, left = num_threads; left; i++, left--)
1035 if (i >= num_threads)
1036 i -= num_threads; /* I wrapped around. */
1037 if (thread->port == threads[i])
1038 /* We already know about this thread. */
1040 matched[i] = thread;
1042 thread = thread->next;
1050 proc_debug (thread, "died!");
1051 thread->port = MACH_PORT_NULL;
1052 thread = _proc_free (thread); /* THREAD is dead. */
1053 (last ? last->next : inf->threads) = thread;
1057 for (i = 0; i < num_threads; i++)
1060 /* Throw away the duplicate send right. */
1061 mach_port_deallocate (mach_task_self (), threads[i]);
1063 /* THREADS[I] is a thread we don't know about yet! */
1065 thread = make_proc (inf, threads[i], next_thread_id++);
1066 (last ? last->next : inf->threads) = thread;
1068 proc_debug (thread, "new thread: %d", threads[i]);
1069 add_thread (thread->tid); /* Tell GDB's generic thread code. */
1073 vm_deallocate (mach_task_self (),
1074 (vm_address_t) threads, (num_threads * sizeof (thread_t)));
1079 /* Makes sure that INF's thread list is synced with the actual process. */
1081 inf_update_procs (struct inf *inf)
1085 if (!inf->threads_up_to_date)
1086 inf_validate_procs (inf);
1090 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
1091 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1094 inf_set_threads_resume_sc (struct inf *inf,
1095 struct proc *run_thread, int run_others)
1097 struct proc *thread;
1098 inf_update_procs (inf);
1099 for (thread = inf->threads; thread; thread = thread->next)
1100 if (thread == run_thread)
1101 thread->resume_sc = 0;
1102 else if (run_others)
1103 thread->resume_sc = thread->run_sc;
1105 thread->resume_sc = thread->pause_sc;
1109 /* Cause INF to continue execution immediately; individual threads may still
1110 be suspended (but their suspend counts will be updated). */
1112 inf_resume (struct inf *inf)
1114 struct proc *thread;
1116 inf_update_procs (inf);
1118 for (thread = inf->threads; thread; thread = thread->next)
1119 thread->sc = thread->resume_sc;
1123 if (!inf->pending_execs)
1124 /* Try to make sure our task count is correct -- in the case where
1125 we're waiting for an exec though, things are too volatile, so just
1126 assume things will be reasonable (which they usually will be). */
1127 inf_validate_task_sc (inf);
1131 inf_update_suspends (inf);
1134 /* Cause INF to stop execution immediately; individual threads may still
1137 inf_suspend (struct inf *inf)
1139 struct proc *thread;
1141 inf_update_procs (inf);
1143 for (thread = inf->threads; thread; thread = thread->next)
1144 thread->sc = thread->pause_sc;
1147 inf->task->sc = inf->pause_sc;
1149 inf_update_suspends (inf);
1153 /* INF has one thread PROC that is in single-stepping mode. This
1154 function changes it to be PROC, changing any old step_thread to be
1155 a normal one. A PROC of 0 clears any existing value. */
1157 inf_set_step_thread (struct inf *inf, struct proc *thread)
1159 assert (!thread || proc_is_thread (thread));
1162 inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1164 inf_debug (inf, "clearing step thread");
1166 if (inf->step_thread != thread)
1168 if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1169 if (!proc_trace (inf->step_thread, 0))
1171 if (thread && proc_trace (thread, 1))
1172 inf->step_thread = thread;
1174 inf->step_thread = 0;
1179 /* Set up the thread resume_sc's so that only the signal thread is running
1180 (plus whatever other thread are set to always run). Returns true if we
1181 did so, or false if we can't find a signal thread. */
1183 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1185 if (inf->signal_thread)
1187 inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1195 inf_update_signal_thread (struct inf *inf)
1197 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1198 the signal thread. */
1199 inf->signal_thread = inf->threads ? inf->threads->next : 0;
1203 /* Detachs from INF's inferior task, letting it run once again... */
1205 inf_detach (struct inf *inf)
1207 struct proc *task = inf->task;
1209 inf_debug (inf, "detaching...");
1211 inf_clear_wait (inf);
1212 inf_set_step_thread (inf, 0);
1216 struct proc *thread;
1218 inf_validate_procinfo (inf);
1220 inf_set_traced (inf, 0);
1226 inf_signal (inf, TARGET_SIGNAL_0);
1229 proc_restore_exc_port (task);
1230 task->sc = inf->detach_sc;
1232 for (thread = inf->threads; thread; thread = thread->next)
1234 proc_restore_exc_port (thread);
1235 thread->sc = thread->detach_sc;
1238 inf_update_suspends (inf);
1244 /* Attaches INF to the process with process id PID, returning it in a
1245 suspended state suitable for debugging. */
1247 inf_attach (struct inf *inf, int pid)
1249 inf_debug (inf, "attaching: %d", pid);
1254 inf_startup (inf, pid);
1258 /* Makes sure that we've got our exception ports entrenched in the process. */
1260 inf_steal_exc_ports (struct inf *inf)
1262 struct proc *thread;
1264 inf_debug (inf, "stealing exception ports");
1266 inf_set_step_thread (inf, 0); /* The step thread is special. */
1268 proc_steal_exc_port (inf->task, inf->event_port);
1269 for (thread = inf->threads; thread; thread = thread->next)
1270 proc_steal_exc_port (thread, MACH_PORT_NULL);
1273 /* Makes sure the process has its own exception ports. */
1275 inf_restore_exc_ports (struct inf *inf)
1277 struct proc *thread;
1279 inf_debug (inf, "restoring exception ports");
1281 inf_set_step_thread (inf, 0); /* The step thread is special. */
1283 proc_restore_exc_port (inf->task);
1284 for (thread = inf->threads; thread; thread = thread->next)
1285 proc_restore_exc_port (thread);
1289 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1290 signal 0, will continue it. INF is assumed to be in a paused state, and
1291 the resume_sc's of INF's threads may be affected. */
1293 inf_signal (struct inf *inf, enum target_signal sig)
1296 int host_sig = target_signal_to_host (sig);
1298 #define NAME target_signal_to_name (sig)
1300 if (host_sig >= _NSIG)
1301 /* A mach exception. Exceptions are encoded in the signal space by
1302 putting them after _NSIG; this assumes they're positive (and not
1303 extremely large)! */
1305 struct inf_wait *w = &inf->wait;
1306 if (w->status.kind == TARGET_WAITKIND_STOPPED
1307 && w->status.value.sig == sig
1308 && w->thread && !w->thread->aborted)
1309 /* We're passing through the last exception we received. This is
1310 kind of bogus, because exceptions are per-thread whereas gdb
1311 treats signals as per-process. We just forward the exception to
1312 the correct handler, even it's not for the same thread as TID --
1313 i.e., we pretend it's global. */
1315 struct exc_state *e = &w->exc;
1316 inf_debug (inf, "passing through exception:"
1317 " task = %d, thread = %d, exc = %d"
1318 ", code = %d, subcode = %d",
1319 w->thread->port, inf->task->port,
1320 e->exception, e->code, e->subcode);
1322 exception_raise_request (e->handler,
1323 e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1324 w->thread->port, inf->task->port,
1325 e->exception, e->code, e->subcode);
1328 error ("Can't forward spontaneous exception (%s).", NAME);
1331 /* A Unix signal. */
1333 /* The process is stopped and expecting a signal. Just send off a
1334 request and let it get handled when we resume everything. */
1336 inf_debug (inf, "sending %s to stopped process", NAME);
1338 INF_MSGPORT_RPC (inf,
1339 msg_sig_post_untraced_request (msgport,
1341 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1345 /* Posting an untraced signal automatically continues it.
1346 We clear this here rather than when we get the reply
1347 because we'd rather assume it's not stopped when it
1348 actually is, than the reverse. */
1352 /* It's not expecting it. We have to let just the signal thread
1353 run, and wait for it to get into a reasonable state before we
1354 can continue the rest of the process. When we finally resume the
1355 process the signal we request will be the very first thing that
1358 inf_debug (inf, "sending %s to unstopped process"
1359 " (so resuming signal thread)", NAME);
1361 INF_RESUME_MSGPORT_RPC (inf,
1362 msg_sig_post_untraced (msgport, host_sig,
1367 /* Can't do too much... */
1368 warning ("Can't deliver signal %s: No signal thread.", NAME);
1370 warning ("Delivering signal %s: %s", NAME, strerror (err));
1376 /* Continue INF without delivering a signal. This is meant to be used
1377 when INF does not have a message port. */
1379 inf_continue (struct inf *inf)
1382 error_t err = proc_pid2proc (proc_server, inf->pid, &proc);
1386 inf_debug (inf, "continuing process");
1388 err = proc_mark_cont (proc);
1391 struct proc *thread;
1393 for (thread = inf->threads; thread; thread = thread->next)
1394 thread_resume (thread->port);
1401 warning ("Can't continue process: %s", strerror (err));
1405 /* The inferior used for all gdb target ops. */
1406 struct inf *current_inferior = 0;
1408 /* The inferior being waited for by gnu_wait. Since GDB is decidely not
1409 multi-threaded, we don't bother to lock this. */
1410 struct inf *waiting_inf;
1412 /* Wait for something to happen in the inferior, returning what in STATUS. */
1414 gnu_wait (int tid, struct target_waitstatus *status)
1418 mach_msg_header_t hdr;
1419 mach_msg_type_t type;
1423 struct proc *thread;
1424 struct inf *inf = current_inferior;
1426 extern int exc_server (mach_msg_header_t *, mach_msg_header_t *);
1427 extern int msg_reply_server (mach_msg_header_t *, mach_msg_header_t *);
1428 extern int notify_server (mach_msg_header_t *, mach_msg_header_t *);
1429 extern int process_reply_server (mach_msg_header_t *, mach_msg_header_t *);
1433 if (!inf->threads && !inf->pending_execs)
1434 /* No threads! Assume that maybe some outside agency is frobbing our
1435 task, and really look for new threads. If we can't find any, just tell
1436 the user to try again later. */
1438 inf_validate_procs (inf);
1439 if (!inf->threads && !inf->task->dead)
1440 error ("There are no threads; try again later.");
1445 inf_debug (inf, "waiting for: %d", tid);
1448 if (proc_wait_pid != inf->pid && !inf->no_wait)
1449 /* Always get information on events from the proc server. */
1451 inf_debug (inf, "requesting wait on pid %d", inf->pid);
1454 /* The proc server is single-threaded, and only allows a single
1455 outstanding wait request, so we have to cancel the previous one. */
1457 inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1458 interrupt_operation (proc_server, 0);
1462 proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1464 warning ("wait request failed: %s", strerror (err));
1467 inf_debug (inf, "waits pending: %d", proc_waits_pending);
1468 proc_wait_pid = inf->pid;
1469 /* Even if proc_waits_pending was > 0 before, we still won't
1470 get any other replies, because it was either from a
1471 different INF, or a different process attached to INF --
1472 and the event port, which is the wait reply port, changes
1473 when you switch processes. */
1474 proc_waits_pending = 1;
1478 inf_clear_wait (inf);
1480 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1481 (3) wait reply from the proc server. */
1483 inf_debug (inf, "waiting for an event...");
1484 err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
1485 0, sizeof (struct msg), inf->event_port,
1486 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1488 /* Re-suspend the task. */
1491 if (!inf->task && inf->pending_execs)
1492 /* When doing an exec, it's possible that the old task wasn't reused
1493 (e.g., setuid execs). So if the task seems to have disappeared,
1494 attempt to refetch it, as the pid should still be the same. */
1495 inf_set_pid (inf, inf->pid);
1497 if (err == EMACH_RCV_INTERRUPTED)
1498 inf_debug (inf, "interrupted");
1500 error ("Couldn't wait for an event: %s", strerror (err));
1505 mach_msg_header_t hdr;
1506 mach_msg_type_t err_type;
1512 inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1514 /* Handle what we got. */
1515 if (!notify_server (&msg.hdr, &reply.hdr)
1516 && !exc_server (&msg.hdr, &reply.hdr)
1517 && !process_reply_server (&msg.hdr, &reply.hdr)
1518 && !msg_reply_server (&msg.hdr, &reply.hdr))
1519 /* Whatever it is, it's something strange. */
1520 error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id);
1523 error ("Handling event, msgid = %d: %s",
1524 msg.hdr.msgh_id, strerror (reply.err));
1527 if (inf->pending_execs)
1528 /* We're waiting for the inferior to finish execing. */
1530 struct inf_wait *w = &inf->wait;
1531 enum target_waitkind kind = w->status.kind;
1533 if (kind == TARGET_WAITKIND_SPURIOUS)
1534 /* Since gdb is actually counting the number of times the inferior
1535 stops, expecting one stop per exec, we only return major events
1539 inf_debug (inf, "pending_execs = %d, ignoring minor event",
1540 inf->pending_execs);
1542 else if (kind == TARGET_WAITKIND_STOPPED
1543 && w->status.value.sig == TARGET_SIGNAL_TRAP)
1544 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1545 means we've succesfully completed an exec! */
1547 if (--inf->pending_execs == 0)
1550 #if 0 /* do we need this? */
1551 prune_threads (1); /* Get rid of the old shell threads */
1552 renumber_threads (0); /* Give our threads reasonable names. */
1555 inf_debug (inf, "pending exec completed, pending_execs => %d",
1556 inf->pending_execs);
1558 else if (kind == TARGET_WAITKIND_STOPPED)
1559 /* It's possible that this signal is because of a crashed process
1560 being handled by the hurd crash server; in this case, the process
1561 will have an extra task suspend, which we need to know about.
1562 Since the code in inf_resume that normally checks for this is
1563 disabled while INF->pending_execs, we do the check here instead. */
1564 inf_validate_task_sc (inf);
1567 if (inf->wait.suppress)
1568 /* Some totally spurious event happened that we don't consider
1569 worth returning to gdb. Just keep waiting. */
1571 inf_debug (inf, "suppressing return, rewaiting...");
1576 /* Pass back out our results. */
1577 bcopy (&inf->wait.status, status, sizeof (*status));
1579 thread = inf->wait.thread;
1583 thread = inf_tid_to_thread (inf, tid);
1585 if (!thread || thread->port == MACH_PORT_NULL)
1587 /* TID is dead; try and find a new thread. */
1588 if (inf_update_procs (inf) && inf->threads)
1589 tid = inf->threads->tid; /* The first available thread. */
1591 tid = inferior_pid; /* let wait_for_inferior handle exit case */
1594 if (thread && tid >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS
1595 && inf->pause_sc == 0 && thread->pause_sc == 0)
1596 /* If something actually happened to THREAD, make sure we
1600 inf_update_suspends (inf);
1603 inf_debug (inf, "returning tid = %d, status = %s (%d)", tid,
1604 status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
1605 : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
1606 : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
1607 : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
1608 : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
1610 status->value.integer);
1616 /* The rpc handler called by exc_server. */
1618 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1619 thread_t thread_port, task_t task_port,
1620 int exception, int code, int subcode)
1622 struct inf *inf = waiting_inf;
1623 struct proc *thread = inf_port_to_thread (inf, thread_port);
1625 inf_debug (waiting_inf,
1626 "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1627 thread_port, task_port, exception, code, subcode);
1630 /* We don't know about thread? */
1632 inf_update_procs (inf);
1633 thread = inf_port_to_thread (inf, thread_port);
1635 /* Give up, the generating thread is gone. */
1639 mach_port_deallocate (mach_task_self (), thread_port);
1640 mach_port_deallocate (mach_task_self (), task_port);
1642 if (!thread->aborted)
1643 /* THREAD hasn't been aborted since this exception happened (abortion
1644 clears any exception state), so it must be real. */
1646 /* Store away the details; this will destroy any previous info. */
1647 inf->wait.thread = thread;
1649 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1651 if (exception == EXC_BREAKPOINT)
1652 /* GDB likes to get SIGTRAP for breakpoints. */
1654 inf->wait.status.value.sig = TARGET_SIGNAL_TRAP;
1655 mach_port_deallocate (mach_task_self (), reply_port);
1658 /* Record the exception so that we can forward it later. */
1660 if (thread->exc_port == port)
1662 inf_debug (waiting_inf, "Handler is thread exeption port <%d>",
1663 thread->saved_exc_port);
1664 inf->wait.exc.handler = thread->saved_exc_port;
1668 inf_debug (waiting_inf, "Handler is task exeption port <%d>",
1669 inf->task->saved_exc_port);
1670 inf->wait.exc.handler = inf->task->saved_exc_port;
1671 assert (inf->task->exc_port == port);
1673 if (inf->wait.exc.handler != MACH_PORT_NULL)
1674 /* Add a reference to the exception handler. */
1675 mach_port_mod_refs (mach_task_self (),
1676 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1679 inf->wait.exc.exception = exception;
1680 inf->wait.exc.code = code;
1681 inf->wait.exc.subcode = subcode;
1682 inf->wait.exc.reply = reply_port;
1684 /* Exceptions are encoded in the signal space by putting them after
1685 _NSIG; this assumes they're positive (and not extremely large)! */
1686 inf->wait.status.value.sig =
1687 target_signal_from_host (_NSIG + exception);
1691 /* A supppressed exception, which ignore. */
1693 inf->wait.suppress = 1;
1694 mach_port_deallocate (mach_task_self (), reply_port);
1701 /* Fill in INF's wait field after a task has died without giving us more
1702 detailed information. */
1704 inf_task_died_status (struct inf *inf)
1706 warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid);
1707 inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
1708 inf->wait.status.value.sig = TARGET_SIGNAL_KILL;
1711 /* Notify server routines. The only real one is dead name notification. */
1713 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1715 struct inf *inf = waiting_inf;
1717 inf_debug (waiting_inf, "port = %d", dead_port);
1719 if (inf->task && inf->task->port == dead_port)
1721 proc_debug (inf->task, "is dead");
1722 inf->task->port = MACH_PORT_NULL;
1723 if (proc_wait_pid == inf->pid)
1724 /* We have a wait outstanding on the process, which will return more
1725 detailed information, so delay until we get that. */
1726 inf->wait.suppress = 1;
1728 /* We never waited for the process (maybe it wasn't a child), so just
1729 pretend it got a SIGKILL. */
1730 inf_task_died_status (inf);
1734 struct proc *thread = inf_port_to_thread (inf, dead_port);
1737 proc_debug (thread, "is dead");
1738 thread->port = MACH_PORT_NULL;
1741 if (inf->task->dead)
1742 /* Since the task is dead, its threads are dying with it. */
1743 inf->wait.suppress = 1;
1746 mach_port_deallocate (mach_task_self (), dead_port);
1747 inf->threads_up_to_date = 0; /* Just in case */
1756 warning ("illegal rpc: %s", fun);
1761 do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
1763 return ill_rpc (__FUNCTION__);
1767 do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
1769 return ill_rpc (__FUNCTION__);
1773 do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
1775 return ill_rpc (__FUNCTION__);
1779 do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
1781 return ill_rpc (__FUNCTION__);
1785 do_mach_notify_send_once (mach_port_t notify)
1787 return ill_rpc (__FUNCTION__);
1791 /* Process_reply server routines. We only use process_wait_reply. */
1794 S_proc_wait_reply (mach_port_t reply, error_t err,
1795 int status, int sigcode, rusage_t rusage, pid_t pid)
1797 struct inf *inf = waiting_inf;
1799 inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1800 err ? strerror (err) : "0", pid, status, sigcode);
1802 if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1803 /* Ack. The task has died, but the task-died notification code didn't
1804 tell anyone because it thought a more detailed reply from the
1805 procserver was forthcoming. However, we now learn that won't
1806 happen... So we have to act like the task just died, and this time,
1808 inf_task_died_status (inf);
1810 if (--proc_waits_pending == 0)
1811 /* PROC_WAIT_PID represents the most recent wait. We will always get
1812 replies in order because the proc server is single threaded. */
1815 inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1821 warning ("Can't wait for pid %d: %s", inf->pid, strerror (err));
1824 /* Since we can't see the inferior's signals, don't trap them. */
1825 inf_set_traced (inf, 0);
1828 else if (pid == inf->pid)
1830 store_waitstatus (&inf->wait.status, status);
1831 if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
1832 /* The process has sent us a signal, and stopped itself in a sane
1833 state pending our actions. */
1835 inf_debug (inf, "process has stopped itself");
1840 inf->wait.suppress = 1; /* Something odd happened. Ignore. */
1846 S_proc_setmsgport_reply (mach_port_t reply, error_t err,
1847 mach_port_t old_msg_port)
1849 return ill_rpc (__FUNCTION__);
1853 S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
1855 return ill_rpc (__FUNCTION__);
1859 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1862 S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
1864 struct inf *inf = waiting_inf;
1867 /* EBUSY is what we get when the crash server has grabbed control of the
1868 process and doesn't like what signal we tried to send it. Just act
1869 like the process stopped (using a signal of 0 should mean that the
1870 *next* time the user continues, it will pass signal 0, which the crash
1871 server should like). */
1873 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1874 inf->wait.status.value.sig = TARGET_SIGNAL_0;
1877 warning ("Signal delivery failed: %s", strerror (err));
1880 /* We only get this reply when we've posted a signal to a process which we
1881 thought was stopped, and which we expected to continue after the signal.
1882 Given that the signal has failed for some reason, it's reasonable to
1883 assume it's still stopped. */
1886 inf->wait.suppress = 1;
1892 S_msg_sig_post_reply (mach_port_t reply, error_t err)
1894 return ill_rpc (__FUNCTION__);
1898 /* Returns the number of messages queued for the receive right PORT. */
1899 static mach_port_msgcount_t
1900 port_msgs_queued (mach_port_t port)
1902 struct mach_port_status status;
1904 mach_port_get_receive_status (mach_task_self (), port, &status);
1909 return status.mps_msgcount;
1913 /* Resume execution of the inferior process.
1915 If STEP is nonzero, single-step it.
1916 If SIGNAL is nonzero, give it that signal.
1919 -1 true Single step the current thread allowing other threads to run.
1920 -1 false Continue the current thread allowing other threads to run.
1921 X true Single step the given thread, don't allow any others to run.
1922 X false Continue the given thread, do not allow any others to run.
1923 (Where X, of course, is anything except -1)
1925 Note that a resume may not `take' if there are pending exceptions/&c
1926 still unprocessed from the last resume we did (any given resume may result
1927 in multiple events returned by wait).
1930 gnu_resume (int tid, int step, enum target_signal sig)
1932 struct proc *step_thread = 0;
1933 struct inf *inf = current_inferior;
1935 inf_debug (inf, "tid = %d, step = %d, sig = %d", tid, step, sig);
1937 inf_validate_procinfo (inf);
1939 if (sig != TARGET_SIGNAL_0 || inf->stopped)
1941 if (sig == TARGET_SIGNAL_0 && inf->nomsg)
1944 inf_signal (inf, sig);
1946 else if (inf->wait.exc.reply != MACH_PORT_NULL)
1947 /* We received an exception to which we have chosen not to forward, so
1948 abort the faulting thread, which will perhaps retake it. */
1950 proc_abort (inf->wait.thread, 1);
1951 warning ("Aborting %s with unforwarded exception %s.",
1952 proc_string (inf->wait.thread),
1953 target_signal_to_name (inf->wait.status.value.sig));
1956 if (port_msgs_queued (inf->event_port))
1957 /* If there are still messages in our event queue, don't bother resuming
1958 the process, as we're just going to stop it right away anyway. */
1961 inf_update_procs (inf);
1964 /* Allow all threads to run, except perhaps single-stepping one. */
1966 inf_debug (inf, "running all threads; tid = %d", inferior_pid);
1967 tid = inferior_pid; /* What to step. */
1968 inf_set_threads_resume_sc (inf, 0, 1);
1971 /* Just allow a single thread to run. */
1973 struct proc *thread = inf_tid_to_thread (inf, tid);
1975 error ("Can't run single thread id %d: no such thread!");
1976 inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid);
1977 inf_set_threads_resume_sc (inf, thread, 0);
1982 step_thread = inf_tid_to_thread (inf, tid);
1984 warning ("Can't step thread id %d: no such thread.", tid);
1986 inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid);
1988 if (step_thread != inf->step_thread)
1989 inf_set_step_thread (inf, step_thread);
1991 inf_debug (inf, "here we go...");
1997 gnu_kill_inferior ()
1999 struct proc *task = current_inferior->task;
2002 proc_debug (task, "terminating...");
2003 task_terminate (task->port);
2004 inf_set_pid (current_inferior, -1);
2006 target_mourn_inferior ();
2009 /* Clean up after the inferior dies. */
2011 gnu_mourn_inferior ()
2013 inf_debug (current_inferior, "rip");
2014 inf_detach (current_inferior);
2015 unpush_target (&gnu_ops);
2016 generic_mourn_inferior ();
2020 /* Fork an inferior process, and start debugging it. */
2022 /* Set INFERIOR_PID to the first thread available in the child, if any. */
2024 inf_pick_first_thread ()
2026 if (current_inferior->task && current_inferior->threads)
2027 /* The first thread. */
2028 return current_inferior->threads->tid;
2030 /* What may be the next thread. */
2031 return next_thread_id;
2037 if (!current_inferior)
2038 current_inferior = make_inf ();
2039 return current_inferior;
2043 gnu_create_inferior (exec_file, allargs, env)
2048 struct inf *inf = cur_inf ();
2052 /* We're in the child; make this process stop as soon as it execs. */
2053 inf_debug (inf, "tracing self");
2054 if (ptrace (PTRACE_TRACEME) != 0)
2055 error ("ptrace (PTRACE_TRACEME) failed!");
2057 void attach_to_child (int pid)
2059 /* Attach to the now stopped child, which is actually a shell... */
2060 inf_debug (inf, "attaching to child: %d", pid);
2062 inf_attach (inf, pid);
2065 push_target (&gnu_ops);
2067 inf->pending_execs = 2;
2071 /* Now let the child run again, knowing that it will stop immediately
2072 because of the ptrace. */
2074 inferior_pid = inf_pick_first_thread ();
2076 startup_inferior (inf->pending_execs);
2079 inf_debug (inf, "creating inferior");
2081 fork_inferior (exec_file, allargs, env, trace_me, attach_to_child,
2084 inf_validate_procinfo (inf);
2085 inf_update_signal_thread (inf);
2086 inf_set_traced (inf, inf->want_signals);
2088 /* Execing the process will have trashed our exception ports; steal them
2089 back (or make sure they're restored if the user wants that). */
2090 if (inf->want_exceptions)
2091 inf_steal_exc_ports (inf);
2093 inf_restore_exc_ports (inf);
2096 proceed ((CORE_ADDR) -1, 0, 0);
2099 /* Mark our target-struct as eligible for stray "run" and "attach"
2108 #ifdef ATTACH_DETACH
2110 /* Attach to process PID, then initialize for debugging it
2111 and wait for the trace-trap that results from attaching. */
2113 gnu_attach (args, from_tty)
2119 struct inf *inf = cur_inf ();
2122 error_no_arg ("process-id to attach");
2126 if (pid == getpid ()) /* Trying to masturbate? */
2127 error ("I refuse to debug myself!");
2131 exec_file = (char *) get_exec_file (0);
2134 printf_unfiltered ("Attaching to program `%s', pid %d\n",
2137 printf_unfiltered ("Attaching to pid %d\n", pid);
2139 gdb_flush (gdb_stdout);
2142 inf_debug (inf, "attaching to pid: %d", pid);
2144 inf_attach (inf, pid);
2145 inf_update_procs (inf);
2147 inferior_pid = inf_pick_first_thread ();
2150 push_target (&gnu_ops);
2152 /* We have to initialize the terminal settings now, since the code
2153 below might try to restore them. */
2154 target_terminal_init ();
2156 /* If the process was stopped before we attached, make it continue the next
2157 time the user does a continue. */
2158 inf_validate_procinfo (inf);
2160 inf_update_signal_thread (inf);
2161 inf_set_traced (inf, inf->want_signals);
2163 #if 0 /* Do we need this? */
2164 renumber_threads (0); /* Give our threads reasonable names. */
2169 /* Take a program previously attached to and detaches it.
2170 The program resumes execution and will no longer stop
2171 on signals, etc. We'd better not have left any breakpoints
2172 in the program or it'll die when it hits one. For this
2173 to work, it may be necessary for the process to have been
2174 previously attached. It *might* work if the program was
2175 started via fork. */
2177 gnu_detach (args, from_tty)
2183 char *exec_file = get_exec_file (0);
2185 printf_unfiltered ("Detaching from program `%s' pid %d\n",
2186 exec_file, current_inferior->pid);
2188 printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid);
2189 gdb_flush (gdb_stdout);
2192 inf_detach (current_inferior);
2196 unpush_target (&gnu_ops); /* Pop out of handling an inferior */
2198 #endif /* ATTACH_DETACH */
2202 gnu_terminal_init_inferior ()
2204 assert (current_inferior);
2205 terminal_init_inferior_with_pgrp (current_inferior->pid);
2208 /* Get ready to modify the registers array. On machines which store
2209 individual registers, this doesn't need to do anything. On machines
2210 which store all the registers in one fell swoop, this makes sure
2211 that registers contains all the registers from the program being
2214 gnu_prepare_to_store ()
2216 #ifdef CHILD_PREPARE_TO_STORE
2217 CHILD_PREPARE_TO_STORE ();
2222 gnu_open (arg, from_tty)
2226 error ("Use the \"run\" command to start a Unix child process.");
2232 error ("to_stop target function not implemented");
2236 gnu_pid_to_exec_file ()
2238 error ("to_pid_to_exec_file target function not implemented");
2244 gnu_thread_alive (int tid)
2246 inf_update_procs (current_inferior);
2247 return !!inf_tid_to_thread (current_inferior, tid);
2251 /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
2252 gdb's address space. Return 0 on failure; number of bytes read
2255 gnu_read_inferior (task, addr, myaddr, length)
2262 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2263 vm_size_t aligned_length =
2264 (vm_size_t) round_page (addr + length) - low_address;
2268 /* Get memory from inferior with page aligned addresses */
2269 err = vm_read (task, low_address, aligned_length, &copied, ©_count);
2273 err = hurd_safe_copyin (myaddr, (void *) addr - low_address + copied, length);
2276 warning ("Read from inferior faulted: %s", strerror (err));
2280 err = vm_deallocate (mach_task_self (), copied, copy_count);
2282 warning ("gnu_read_inferior vm_deallocate failed: %s", strerror (err));
2287 #define CHK_GOTO_OUT(str,ret) \
2288 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2290 struct vm_region_list
2292 struct vm_region_list *next;
2293 vm_prot_t protection;
2298 struct obstack region_obstack;
2300 /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
2301 task's address space. */
2303 gnu_write_inferior (task, addr, myaddr, length)
2310 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2311 vm_size_t aligned_length =
2312 (vm_size_t) round_page (addr + length) - low_address;
2317 char *errstr = "Bug in gnu_write_inferior";
2319 struct vm_region_list *region_element;
2320 struct vm_region_list *region_head = (struct vm_region_list *) NULL;
2322 /* Get memory from inferior with page aligned addresses */
2323 err = vm_read (task,
2328 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2332 err = hurd_safe_copyout ((void *) addr - low_address + copied,
2334 CHK_GOTO_OUT ("Write to inferior faulted", err);
2336 obstack_init (®ion_obstack);
2338 /* Do writes atomically.
2339 First check for holes and unwritable memory. */
2341 vm_size_t remaining_length = aligned_length;
2342 vm_address_t region_address = low_address;
2344 struct vm_region_list *scan;
2346 while (region_address < low_address + aligned_length)
2348 vm_prot_t protection;
2349 vm_prot_t max_protection;
2350 vm_inherit_t inheritance;
2352 mach_port_t object_name;
2354 vm_size_t region_length = remaining_length;
2355 vm_address_t old_address = region_address;
2357 err = vm_region (task,
2366 CHK_GOTO_OUT ("vm_region failed", err);
2368 /* Check for holes in memory */
2369 if (old_address != region_address)
2371 warning ("No memory at 0x%x. Nothing written",
2378 if (!(max_protection & VM_PROT_WRITE))
2380 warning ("Memory at address 0x%x is unwritable. Nothing written",
2387 /* Chain the regions for later use */
2389 (struct vm_region_list *)
2390 obstack_alloc (®ion_obstack, sizeof (struct vm_region_list));
2392 region_element->protection = protection;
2393 region_element->start = region_address;
2394 region_element->length = region_length;
2396 /* Chain the regions along with protections */
2397 region_element->next = region_head;
2398 region_head = region_element;
2400 region_address += region_length;
2401 remaining_length = remaining_length - region_length;
2404 /* If things fail after this, we give up.
2405 Somebody is messing up inferior_task's mappings. */
2407 /* Enable writes to the chained vm regions */
2408 for (scan = region_head; scan; scan = scan->next)
2410 if (!(scan->protection & VM_PROT_WRITE))
2412 err = vm_protect (task,
2416 scan->protection | VM_PROT_WRITE);
2417 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2421 err = vm_write (task,
2425 CHK_GOTO_OUT ("vm_write failed", err);
2427 /* Set up the original region protections, if they were changed */
2428 for (scan = region_head; scan; scan = scan->next)
2430 if (!(scan->protection & VM_PROT_WRITE))
2432 err = vm_protect (task,
2437 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2445 obstack_free (®ion_obstack, 0);
2447 (void) vm_deallocate (mach_task_self (),
2452 if (err != KERN_SUCCESS)
2454 warning ("%s: %s", errstr, mach_error_string (err));
2462 /* Return 0 on failure, number of bytes handled otherwise. */
2464 gnu_xfer_memory (memaddr, myaddr, len, write, target)
2469 struct target_ops *target; /* IGNORED */
2471 task_t task = (current_inferior
2472 ? (current_inferior->task
2473 ? current_inferior->task->port : 0)
2476 if (task == MACH_PORT_NULL)
2480 inf_debug (current_inferior, "%s %p[%d] %s %p",
2481 write ? "writing" : "reading", memaddr, len,
2482 write ? "<--" : "-->", myaddr);
2484 return gnu_write_inferior (task, memaddr, myaddr, len);
2486 return gnu_read_inferior (task, memaddr, myaddr, len);
2491 /* Return printable description of proc. */
2493 proc_string (struct proc *proc)
2495 static char tid_str[80];
2496 if (proc_is_task (proc))
2497 sprintf (tid_str, "process %d", proc->inf->pid);
2499 sprintf (tid_str, "thread %d.%d",
2500 proc->inf->pid, pid_to_thread_id (proc->tid));
2505 gnu_pid_to_str (int tid)
2507 struct inf *inf = current_inferior;
2508 struct proc *thread = inf_tid_to_thread (inf, tid);
2511 return proc_string (thread);
2514 static char tid_str[80];
2515 sprintf (tid_str, "bogus thread id %d", tid);
2521 extern void gnu_store_registers (int regno);
2522 extern void gnu_fetch_registers (int regno);
2524 struct target_ops gnu_ops;
2529 gnu_ops.to_shortname = "GNU"; /* to_shortname */
2530 gnu_ops.to_longname = "GNU Hurd process"; /* to_longname */
2531 gnu_ops.to_doc = "GNU Hurd process"; /* to_doc */
2532 gnu_ops.to_open = gnu_open; /* to_open */
2533 gnu_ops.to_close = 0; /* to_close */
2534 gnu_ops.to_attach = gnu_attach; /* to_attach */
2535 gnu_ops.to_post_attach = NULL;
2536 gnu_ops.to_require_attach = NULL; /* to_require_attach */
2537 gnu_ops.to_detach = gnu_detach; /* to_detach */
2538 gnu_ops.to_require_detach = NULL; /* to_require_detach */
2539 gnu_ops.to_resume = gnu_resume; /* to_resume */
2540 gnu_ops.to_wait = gnu_wait; /* to_wait */
2541 gnu_ops.to_post_wait = NULL; /* to_post_wait */
2542 gnu_ops.to_fetch_registers = gnu_fetch_registers; /* to_fetch_registers */
2543 gnu_ops.to_store_registers = gnu_store_registers; /* to_store_registers */
2544 gnu_ops.to_prepare_to_store = gnu_prepare_to_store; /* to_prepare_to_store */
2545 gnu_ops.to_xfer_memory = gnu_xfer_memory; /* to_xfer_memory */
2546 gnu_ops.to_files_info = 0; /* to_files_info */
2547 gnu_ops.to_insert_breakpoint = memory_insert_breakpoint;
2548 gnu_ops.to_remove_breakpoint = memory_remove_breakpoint;
2549 gnu_ops.to_terminal_init = gnu_terminal_init_inferior;
2550 gnu_ops.to_terminal_inferior = terminal_inferior;
2551 gnu_ops.to_terminal_ours_for_output = terminal_ours_for_output;
2552 gnu_ops.to_terminal_ours = terminal_ours;
2553 gnu_ops.to_terminal_info = child_terminal_info;
2554 gnu_ops.to_kill = gnu_kill_inferior; /* to_kill */
2555 gnu_ops.to_load = 0; /* to_load */
2556 gnu_ops.to_lookup_symbol = 0; /* to_lookup_symbol */
2557 gnu_ops.to_create_inferior = gnu_create_inferior; /* to_create_inferior */
2558 gnu_ops.to_post_startup_inferior = NULL; /* to_post_startup_inferior */
2559 /* to_acknowledge_created_inferior */
2560 gnu_ops.to_acknowledge_created_inferior = NULL;
2561 /* to_clone_and_follow_inferior */
2562 gnu_ops.to_clone_and_follow_inferior = NULL;
2563 /* to_post_follow_inferior_by_clone */
2564 gnu_ops.to_post_follow_inferior_by_clone = NULL;
2565 gnu_ops.to_insert_fork_catchpoint = NULL;
2566 gnu_ops.to_remove_fork_catchpoint = NULL;
2567 gnu_ops.to_insert_vfork_catchpoint = NULL;
2568 gnu_ops.to_remove_vfork_catchpoint = NULL;
2569 gnu_ops.to_has_forked = NULL; /* to_has_forked */
2570 gnu_ops.to_has_vforked = NULL; /* to_has_vforked */
2571 gnu_ops.to_can_follow_vfork_prior_to_exec = NULL;
2572 gnu_ops.to_post_follow_vfork = NULL; /* to_post_follow_vfork */
2573 gnu_ops.to_insert_exec_catchpoint = NULL;
2574 gnu_ops.to_remove_exec_catchpoint = NULL;
2575 gnu_ops.to_has_execd = NULL;
2576 gnu_ops.to_reported_exec_events_per_exec_call = NULL;
2577 gnu_ops.to_has_exited = NULL;
2578 gnu_ops.to_mourn_inferior = gnu_mourn_inferior; /* to_mourn_inferior */
2579 gnu_ops.to_can_run = gnu_can_run; /* to_can_run */
2580 gnu_ops.to_notice_signals = 0; /* to_notice_signals */
2581 gnu_ops.to_thread_alive = gnu_thread_alive; /* to_thread_alive */
2582 gnu_ops.to_pid_to_str = gnu_pid_to_str; /* to_pid_to_str */
2583 gnu_ops.to_stop = gnu_stop; /* to_stop */
2584 gnu_ops.to_pid_to_exec_file = gnu_pid_to_exec_file; /* to_pid_to_exec_file */
2585 gnu_ops.to_core_file_to_sym_file = NULL;
2586 gnu_ops.to_stratum = process_stratum; /* to_stratum */
2587 gnu_ops.DONT_USE = 0; /* to_next */
2588 gnu_ops.to_has_all_memory = 1; /* to_has_all_memory */
2589 gnu_ops.to_has_memory = 1; /* to_has_memory */
2590 gnu_ops.to_has_stack = 1; /* to_has_stack */
2591 gnu_ops.to_has_registers = 1; /* to_has_registers */
2592 gnu_ops.to_has_execution = 1; /* to_has_execution */
2593 gnu_ops.to_sections = 0; /* sections */
2594 gnu_ops.to_sections_end = 0; /* sections_end */
2595 gnu_ops.to_magic = OPS_MAGIC; /* to_magic */
2596 } /* init_gnu_ops */
2599 /* User task commands. */
2601 struct cmd_list_element *set_task_cmd_list = 0;
2602 struct cmd_list_element *show_task_cmd_list = 0;
2603 /* User thread commands. */
2605 /* Commands with a prefix of `set/show thread'. */
2606 extern struct cmd_list_element *thread_cmd_list;
2607 struct cmd_list_element *set_thread_cmd_list = NULL;
2608 struct cmd_list_element *show_thread_cmd_list = NULL;
2610 /* Commands with a prefix of `set/show thread default'. */
2611 struct cmd_list_element *set_thread_default_cmd_list = NULL;
2612 struct cmd_list_element *show_thread_default_cmd_list = NULL;
2615 set_thread_cmd (char *args, int from_tty)
2617 printf_unfiltered ("\"set thread\" must be followed by the name of a thread property, or \"default\".\n");
2621 show_thread_cmd (char *args, int from_tty)
2623 printf_unfiltered ("\"show thread\" must be followed by the name of a thread property, or \"default\".\n");
2627 set_thread_default_cmd (char *args, int from_tty)
2629 printf_unfiltered ("\"set thread default\" must be followed by the name of a thread property.\n");
2633 show_thread_default_cmd (char *args, int from_tty)
2635 printf_unfiltered ("\"show thread default\" must be followed by the name of a thread property.\n");
2639 parse_int_arg (char *args, char *cmd_prefix)
2644 int val = strtoul (args, &arg_end, 10);
2645 if (*args && *arg_end == '\0')
2648 error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix);
2652 _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
2654 if (!args || strcmp (args, t_val) == 0)
2656 else if (strcmp (args, f_val) == 0)
2659 error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2660 cmd_prefix, t_val, f_val);
2663 #define parse_bool_arg(args, cmd_prefix) \
2664 _parse_bool_arg (args, "on", "off", cmd_prefix)
2667 check_empty (char *args, char *cmd_prefix)
2670 error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args);
2673 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2674 static struct proc *
2677 struct inf *inf = cur_inf ();
2678 struct proc *thread = inf_tid_to_thread (inf, inferior_pid);
2680 error ("No current thread.");
2684 /* Returns the current inferior, but signals an error if it has no task. */
2688 struct inf *inf = cur_inf ();
2690 error ("No current process.");
2696 set_task_pause_cmd (char *args, int from_tty)
2698 struct inf *inf = cur_inf ();
2699 int old_sc = inf->pause_sc;
2701 inf->pause_sc = parse_bool_arg (args, "set task pause");
2703 if (old_sc == 0 && inf->pause_sc != 0)
2704 /* If the task is currently unsuspended, immediately suspend it,
2705 otherwise wait until the next time it gets control. */
2710 show_task_pause_cmd (char *args, int from_tty)
2712 struct inf *inf = cur_inf ();
2713 check_empty (args, "show task pause");
2714 printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2716 ? (inf->pause_sc == 0 ? "isn't" : "is")
2717 : (inf->pause_sc == 0 ? "won't be" : "will be"));
2721 set_task_detach_sc_cmd (char *args, int from_tty)
2723 cur_inf ()->detach_sc = parse_int_arg (args, "set task detach-suspend-count");
2727 show_task_detach_sc_cmd (char *args, int from_tty)
2729 check_empty (args, "show task detach-suspend-count");
2730 printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n",
2731 cur_inf ()->detach_sc);
2736 set_thread_default_pause_cmd (char *args, int from_tty)
2738 struct inf *inf = cur_inf ();
2739 inf->default_thread_pause_sc =
2740 parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2744 show_thread_default_pause_cmd (char *args, int from_tty)
2746 struct inf *inf = cur_inf ();
2747 int sc = inf->default_thread_pause_sc;
2748 check_empty (args, "show thread default pause");
2749 printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2750 sc ? "are" : "aren't",
2751 !sc && inf->pause_sc ? " (but the task is)" : "");
2755 set_thread_default_run_cmd (char *args, int from_tty)
2757 struct inf *inf = cur_inf ();
2758 inf->default_thread_run_sc =
2759 parse_bool_arg (args, "set thread default run") ? 0 : 1;
2763 show_thread_default_run_cmd (char *args, int from_tty)
2765 struct inf *inf = cur_inf ();
2766 check_empty (args, "show thread default run");
2767 printf_unfiltered ("New threads %s allowed to run.\n",
2768 inf->default_thread_run_sc == 0 ? "are" : "aren't");
2772 set_thread_default_detach_sc_cmd (char *args, int from_tty)
2774 cur_inf ()->default_thread_detach_sc =
2775 parse_int_arg (args, "set thread default detach-suspend-count");
2779 show_thread_default_detach_sc_cmd (char *args, int from_tty)
2781 check_empty (args, "show thread default detach-suspend-count");
2782 printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
2783 cur_inf ()->default_thread_detach_sc);
2787 /* Steal a send right called NAME in the inferior task, and make it PROC's
2788 saved exception port. */
2790 steal_exc_port (struct proc *proc, mach_port_t name)
2794 mach_msg_type_name_t port_type;
2796 if (!proc || !proc->inf->task)
2797 error ("No inferior task.");
2799 err = mach_port_extract_right (proc->inf->task->port,
2800 name, MACH_MSG_TYPE_COPY_SEND,
2803 error ("Couldn't extract send right %d from inferior: %s",
2804 name, strerror (err));
2806 if (proc->saved_exc_port)
2807 /* Get rid of our reference to the old one. */
2808 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2810 proc->saved_exc_port = port;
2812 if (!proc->exc_port)
2813 /* If PROC is a thread, we may not have set its exception port before.
2814 We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2816 proc->exc_port = proc->inf->event_port;
2817 err = proc_set_exception_port (proc, proc->exc_port);
2818 error ("Can't set exception port for %s: %s",
2819 proc_string (proc), strerror (err));
2824 set_task_exc_port_cmd (char *args, int from_tty)
2826 struct inf *inf = cur_inf ();
2828 error ("No argument to \"set task exception-port\" command.");
2829 steal_exc_port (inf->task, parse_and_eval_address (args));
2833 set_stopped_cmd (char *args, int from_tty)
2835 cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2839 show_stopped_cmd (char *args, int from_tty)
2841 struct inf *inf = active_inf ();
2842 check_empty (args, "show stopped");
2843 printf_unfiltered ("The inferior process %s stopped.\n",
2844 inf->stopped ? "is" : "isn't");
2848 set_sig_thread_cmd (char *args, int from_tty)
2850 struct inf *inf = cur_inf ();
2852 if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
2853 error ("Illegal argument to \"set signal-thread\" command.\n"
2854 "Should be an integer thread ID, or `none'.");
2856 if (strcmp (args, "none") == 0)
2857 inf->signal_thread = 0;
2860 int tid = thread_id_to_pid (atoi (args));
2862 error ("Thread ID %s not known. Use the \"info threads\" command to\n"
2863 "see the IDs of currently known threads.", args);
2864 inf->signal_thread = inf_tid_to_thread (inf, tid);
2869 show_sig_thread_cmd (char *args, int from_tty)
2871 struct inf *inf = active_inf ();
2872 check_empty (args, "show signal-thread");
2873 if (inf->signal_thread)
2874 printf_unfiltered ("The signal thread is %s.\n",
2875 proc_string (inf->signal_thread));
2877 printf_unfiltered ("There is no signal thread.\n");
2882 set_signals_cmd (char *args, int from_tty)
2884 struct inf *inf = cur_inf ();
2886 inf->want_signals = parse_bool_arg (args, "set signals");
2888 if (inf->task && inf->want_signals != inf->traced)
2889 /* Make this take effect immediately in a running process. */
2890 inf_set_traced (inf, inf->want_signals);
2894 show_signals_cmd (char *args, int from_tty)
2896 struct inf *inf = cur_inf ();
2897 check_empty (args, "show signals");
2898 printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2900 ? (inf->traced ? "are" : "aren't")
2901 : (inf->want_signals ? "will be" : "won't be"));
2905 set_exceptions_cmd (char *args, int from_tty)
2907 struct inf *inf = cur_inf ();
2908 int val = parse_bool_arg (args, "set exceptions");
2910 if (inf->task && inf->want_exceptions != val)
2911 /* Make this take effect immediately in a running process. */
2914 inf->want_exceptions = val;
2918 show_exceptions_cmd (char *args, int from_tty)
2920 struct inf *inf = cur_inf ();
2921 check_empty (args, "show exceptions");
2922 printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2924 ? (inf->want_exceptions ? "are" : "aren't")
2925 : (inf->want_exceptions ? "will be" : "won't be"));
2930 set_task_cmd (char *args, int from_tty)
2932 printf_unfiltered ("\"set task\" must be followed by the name"
2933 " of a task property.\n");
2937 show_task_cmd (char *args, int from_tty)
2939 struct inf *inf = cur_inf ();
2941 check_empty (args, "show task");
2943 show_signals_cmd (0, from_tty);
2944 show_exceptions_cmd (0, from_tty);
2945 show_task_pause_cmd (0, from_tty);
2947 if (inf->pause_sc == 0)
2948 show_thread_default_pause_cmd (0, from_tty);
2949 show_thread_default_run_cmd (0, from_tty);
2953 show_stopped_cmd (0, from_tty);
2954 show_sig_thread_cmd (0, from_tty);
2957 if (inf->detach_sc != 0)
2958 show_task_detach_sc_cmd (0, from_tty);
2959 if (inf->default_thread_detach_sc != 0)
2960 show_thread_default_detach_sc_cmd (0, from_tty);
2965 set_noninvasive_cmd (char *args, int from_tty)
2967 /* Invert the sense of the arg for each component. */
2968 char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on";
2970 set_task_pause_cmd (inv_args, from_tty);
2971 set_signals_cmd (inv_args, from_tty);
2972 set_exceptions_cmd (inv_args, from_tty);
2977 info_port_rights (char *args, mach_port_type_t only)
2979 struct inf *inf = active_inf ();
2980 value_ptr vmark = value_mark ();
2983 /* Explicit list of port rights. */
2987 value_ptr val = parse_to_comma_and_eval (&args);
2988 long right = value_as_long (val);
2990 print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
2993 error ("%ld: %s.", right, strerror (err));
2997 /* Print all of them. */
3000 print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
3003 error ("%s.", strerror (err));
3006 value_free_to_mark (vmark);
3010 info_send_rights_cmd (char *args, int from_tty)
3012 info_port_rights (args, MACH_PORT_TYPE_SEND);
3016 info_recv_rights_cmd (char *args, int from_tty)
3018 info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
3022 info_port_sets_cmd (char *args, int from_tty)
3024 info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
3028 info_dead_names_cmd (char *args, int from_tty)
3030 info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
3034 info_port_rights_cmd (char *args, int from_tty)
3036 info_port_rights (args, ~0);
3041 add_task_commands (void)
3043 add_cmd ("pause", class_run, set_thread_default_pause_cmd,
3044 "Set whether the new threads are suspended while gdb has control.\n\
3045 This property normally has no effect because the whole task is\n\
3046 suspended, however, that may be disabled with \"set task pause off\".\n\
3047 The default value is \"off\".",
3048 &set_thread_default_cmd_list);
3049 add_cmd ("pause", no_class, show_thread_default_pause_cmd,
3050 "Show whether new threads are suspended while gdb has control.",
3051 &show_thread_default_cmd_list);
3053 add_cmd ("run", class_run, set_thread_default_run_cmd,
3054 "Set whether new threads are allowed to run \
3055 (once gdb has noticed them).",
3056 &set_thread_default_cmd_list);
3057 add_cmd ("run", no_class, show_thread_default_run_cmd,
3058 "Show whether new threads are allowed to run \
3059 (once gdb has noticed them).",
3060 &show_thread_default_cmd_list);
3062 add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
3063 "Set the default detach-suspend-count value for new threads.",
3064 &set_thread_default_cmd_list);
3065 add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
3066 "Show the default detach-suspend-count value for new threads.",
3067 &show_thread_default_cmd_list);
3069 add_cmd ("signals", class_run, set_signals_cmd,
3070 "Set whether the inferior process's signals will be intercepted.\n\
3071 Mach exceptions (such as breakpoint traps) are not affected.",
3073 add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
3074 add_cmd ("signals", no_class, show_signals_cmd,
3075 "Show whether the inferior process's signals will be intercepted.",
3077 add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
3079 add_cmd ("signal-thread", class_run, set_sig_thread_cmd,
3080 "Set the thread that gdb thinks is the libc signal thread.\n\
3081 This thread is run when delivering a signal to a non-stopped process.",
3083 add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
3084 add_cmd ("signal-thread", no_class, show_sig_thread_cmd,
3085 "Set the thread that gdb thinks is the libc signal thread.",
3087 add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
3089 add_cmd ("stopped", class_run, set_stopped_cmd,
3090 "Set whether gdb thinks the inferior process is stopped \
3092 Stopped process will be continued by sending them a signal.",
3094 add_cmd ("stopped", no_class, show_signals_cmd,
3095 "Show whether gdb thinks the inferior process is stopped \
3099 add_cmd ("exceptions", class_run, set_exceptions_cmd,
3100 "Set whether exceptions in the inferior process will be trapped.\n\
3101 When exceptions are turned off, neither breakpoints nor single-stepping\n\
3104 /* Allow `set exc' despite conflict with `set exception-port'. */
3105 add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
3106 add_cmd ("exceptions", no_class, show_exceptions_cmd,
3107 "Show whether exceptions in the inferior process will be trapped.",
3110 add_prefix_cmd ("task", no_class, set_task_cmd,
3111 "Command prefix for setting task attributes.",
3112 &set_task_cmd_list, "set task ", 0, &setlist);
3113 add_prefix_cmd ("task", no_class, show_task_cmd,
3114 "Command prefix for showing task attributes.",
3115 &show_task_cmd_list, "show task ", 0, &showlist);
3117 add_cmd ("pause", class_run, set_task_pause_cmd,
3118 "Set whether the task is suspended while gdb has control.\n\
3119 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3120 until the next time the program is continued.\n\
3121 When setting this to \"off\", \"set thread default pause on\" can be\n\
3122 used to pause individual threads by default instead.",
3123 &set_task_cmd_list);
3124 add_cmd ("pause", no_class, show_task_pause_cmd,
3125 "Show whether the task is suspended while gdb has control.",
3126 &show_task_cmd_list);
3128 add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
3129 "Set the suspend count will leave on the thread when detaching.",
3130 &set_task_cmd_list);
3131 add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
3132 "Show the suspend count will leave on the thread when detaching.",
3133 &show_task_cmd_list);
3135 add_cmd ("exception-port", no_class, set_task_exc_port_cmd,
3136 "Set the task exception port to which we forward exceptions.\n\
3137 The argument should be the value of the send right in the task.",
3138 &set_task_cmd_list);
3139 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
3140 add_alias_cmd ("exc-port", "exception-port", no_class, 1,
3141 &set_task_cmd_list);
3143 /* A convenient way of turning on all options require to noninvasively
3144 debug running tasks. */
3145 add_cmd ("noninvasive", no_class, set_noninvasive_cmd,
3146 "Set task options so that we interfere as little as possible.\n\
3147 This is the same as setting `task pause', `exceptions', and\n\
3148 `signals' to the opposite value.",
3151 /* Commands to show information about the task's ports. */
3152 add_cmd ("send-rights", class_info, info_send_rights_cmd,
3153 "Show information about the task's send rights",
3155 add_cmd ("receive-rights", class_info, info_recv_rights_cmd,
3156 "Show information about the task's receive rights",
3158 add_cmd ("port-rights", class_info, info_port_rights_cmd,
3159 "Show information about the task's port rights",
3161 add_cmd ("port-sets", class_info, info_port_sets_cmd,
3162 "Show information about the task's port sets",
3164 add_cmd ("dead-names", class_info, info_dead_names_cmd,
3165 "Show information about the task's dead names",
3167 add_info_alias ("ports", "port-rights", 1);
3168 add_info_alias ("port", "port-rights", 1);
3169 add_info_alias ("psets", "port-sets", 1);
3174 set_thread_pause_cmd (char *args, int from_tty)
3176 struct proc *thread = cur_thread ();
3177 int old_sc = thread->pause_sc;
3178 thread->pause_sc = parse_bool_arg (args, "set thread pause");
3179 if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
3180 /* If the task is currently unsuspended, immediately suspend it,
3181 otherwise wait until the next time it gets control. */
3182 inf_suspend (thread->inf);
3186 show_thread_pause_cmd (char *args, int from_tty)
3188 struct proc *thread = cur_thread ();
3189 int sc = thread->pause_sc;
3190 check_empty (args, "show task pause");
3191 printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
3192 proc_string (thread),
3193 sc ? "is" : "isn't",
3194 !sc && thread->inf->pause_sc ? " (but the task is)" : "");
3198 set_thread_run_cmd (char *args, int from_tty)
3200 struct proc *thread = cur_thread ();
3201 thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
3205 show_thread_run_cmd (char *args, int from_tty)
3207 struct proc *thread = cur_thread ();
3208 check_empty (args, "show thread run");
3209 printf_unfiltered ("Thread %s %s allowed to run.",
3210 proc_string (thread),
3211 thread->run_sc == 0 ? "is" : "isn't");
3215 set_thread_detach_sc_cmd (char *args, int from_tty)
3217 cur_thread ()->detach_sc = parse_int_arg (args,
3218 "set thread detach-suspend-count");
3222 show_thread_detach_sc_cmd (char *args, int from_tty)
3224 struct proc *thread = cur_thread ();
3225 check_empty (args, "show thread detach-suspend-count");
3226 printf_unfiltered ("Thread %s will be left with a suspend count"
3227 " of %d when detaching.\n",
3228 proc_string (thread),
3233 set_thread_exc_port_cmd (char *args, int from_tty)
3235 struct proc *thread = cur_thread ();
3237 error ("No argument to \"set thread exception-port\" command.");
3238 steal_exc_port (thread, parse_and_eval_address (args));
3243 show_thread_cmd (char *args, int from_tty)
3245 struct proc *thread = cur_thread ();
3246 check_empty (args, "show thread");
3247 show_thread_run_cmd (0, from_tty);
3248 show_thread_pause_cmd (0, from_tty);
3249 if (thread->detach_sc != 0)
3250 show_thread_detach_sc_cmd (0, from_tty);
3255 thread_takeover_sc_cmd (char *args, int from_tty)
3257 struct proc *thread = cur_thread ();
3258 thread_basic_info_data_t _info;
3259 thread_basic_info_t info = &_info;
3260 mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
3262 thread_info (thread->port, THREAD_BASIC_INFO, (int *) &info, &info_len);
3264 error ("%s.", strerror (err));
3265 thread->sc = info->suspend_count;
3267 printf_unfiltered ("Suspend count was %d.\n", thread->sc);
3269 vm_deallocate (mach_task_self (), (vm_address_t) info,
3270 info_len * sizeof (int));
3275 add_thread_commands (void)
3277 add_prefix_cmd ("thread", no_class, set_thread_cmd,
3278 "Command prefix for setting thread properties.",
3279 &set_thread_cmd_list, "set thread ", 0, &setlist);
3280 add_prefix_cmd ("default", no_class, show_thread_cmd,
3281 "Command prefix for setting default thread properties.",
3282 &set_thread_default_cmd_list, "set thread default ", 0,
3283 &set_thread_cmd_list);
3284 add_prefix_cmd ("thread", no_class, set_thread_default_cmd,
3285 "Command prefix for showing thread properties.",
3286 &show_thread_cmd_list, "show thread ", 0, &showlist);
3287 add_prefix_cmd ("default", no_class, show_thread_default_cmd,
3288 "Command prefix for showing default thread properties.",
3289 &show_thread_default_cmd_list, "show thread default ", 0,
3290 &show_thread_cmd_list);
3292 add_cmd ("pause", class_run, set_thread_pause_cmd,
3293 "Set whether the current thread is suspended \
3294 while gdb has control.\n\
3295 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3296 until the next time the program is continued. This property normally\n\
3297 has no effect because the whole task is suspended, however, that may\n\
3298 be disabled with \"set task pause off\".\n\
3299 The default value is \"off\".",
3300 &set_thread_cmd_list);
3301 add_cmd ("pause", no_class, show_thread_pause_cmd,
3302 "Show whether the current thread is suspended \
3303 while gdb has control.",
3304 &show_thread_cmd_list);
3306 add_cmd ("run", class_run, set_thread_run_cmd,
3307 "Set whether the current thread is allowed to run.",
3308 &set_thread_cmd_list);
3309 add_cmd ("run", no_class, show_thread_run_cmd,
3310 "Show whether the current thread is allowed to run.",
3311 &show_thread_cmd_list);
3313 add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd,
3314 "Set the suspend count will leave on the thread when detaching.\n\
3315 Note that this is relative to suspend count when gdb noticed the thread;\n\
3316 use the `thread takeover-suspend-count' to force it to an absolute value.",
3317 &set_thread_cmd_list);
3318 add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd,
3319 "Show the suspend count will leave on the thread when detaching.\n\
3320 Note that this is relative to suspend count when gdb noticed the thread;\n\
3321 use the `thread takeover-suspend-count' to force it to an absolute value.",
3322 &show_thread_cmd_list);
3324 add_cmd ("exception-port", no_class, set_thread_exc_port_cmd,
3325 "Set the thread exception port to which we forward exceptions.\n\
3326 This overrides the task exception port.\n\
3327 The argument should be the value of the send right in the task.",
3328 &set_thread_cmd_list);
3329 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
3330 add_alias_cmd ("exc-port", "exception-port", no_class, 1,
3331 &set_thread_cmd_list);
3333 add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd,
3334 "Force the threads absolute suspend-count to be gdb's.\n\
3335 Prior to giving this command, gdb's thread suspend-counts are relative\n\
3336 to the thread's initial suspend-count when gdb notices the threads.",
3342 _initialize_gnu_nat (void)
3344 proc_server = getproc ();
3347 add_target (&gnu_ops);
3349 add_task_commands ();
3350 add_thread_commands ();
3351 add_set_cmd ("gnu-debug", class_maintenance,
3352 var_boolean, (char *) &gnu_debug_flag,
3353 "Set debugging output for the gnu backend.", &maintenancelist);
3356 #ifdef FLUSH_INFERIOR_CACHE
3358 /* When over-writing code on some machines the I-Cache must be flushed
3359 explicitly, because it is not kept coherent by the lazy hardware.
3360 This definitely includes breakpoints, for instance, or else we
3361 end up looping in mysterious Bpt traps */
3364 flush_inferior_icache (pc, amount)
3367 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
3370 ret = vm_machine_attribute (current_inferior->task->port,
3375 if (ret != KERN_SUCCESS)
3376 warning ("Error flushing inferior's cache : %s", strerror (ret));
3378 #endif /* FLUSH_INFERIOR_CACHE */