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.
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/message.h>
37 #include <mach/notify.h>
38 #include <mach_error.h>
39 #include <mach/exception.h>
40 #include <mach/vm_attributes.h>
42 #include <hurd/process.h>
43 #include <hurd/process_request.h>
45 #include <hurd/msg_request.h>
46 #include <hurd/signal.h>
47 #include <hurd/interrupt.h>
48 #include <hurd/sigpreempt.h>
64 #include "exc_request_S.h"
66 #include "process_reply_S.h"
67 #include "msg_reply_S.h"
68 #include "exc_request_U.h"
71 static process_t proc_server = MACH_PORT_NULL;
73 /* If we've sent a proc_wait_request to the proc server, the pid of the
74 process we asked about. We can only ever have one outstanding. */
75 int proc_wait_pid = 0;
77 /* The number of wait requests we've sent, and expect replies from. */
78 int proc_waits_pending = 0;
80 int gnu_debug_flag = 0;
84 extern struct target_ops gnu_ops;
86 struct inf *make_inf ();
87 void inf_clear_wait (struct inf *inf);
88 void inf_cleanup (struct inf *inf);
89 void inf_startup (struct inf *inf, int pid);
90 int inf_update_suspends (struct inf *inf);
91 void inf_set_pid (struct inf *inf, pid_t pid);
92 void inf_validate_procs (struct inf *inf);
93 void inf_steal_exc_ports (struct inf *inf);
94 void inf_restore_exc_ports (struct inf *inf);
95 struct proc *inf_tid_to_proc (struct inf *inf, int tid);
96 inline void inf_set_threads_resume_sc (struct inf *inf,
97 struct proc *run_thread,
99 inline int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
100 inline void inf_suspend (struct inf *inf);
101 inline void inf_resume (struct inf *inf);
102 void inf_set_step_thread (struct inf *inf, struct proc *proc);
103 void inf_detach (struct inf *inf);
104 void inf_attach (struct inf *inf, int pid);
105 void inf_signal (struct inf *inf, enum target_signal sig);
106 void inf_continue (struct inf *inf);
108 #define inf_debug(_inf, msg, args...) \
109 do { struct inf *__inf = (_inf); \
110 debug ("{inf %d %p}: " msg, __inf->pid, __inf , ##args); } while (0)
112 void proc_abort (struct proc *proc, int force);
113 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
114 struct proc *_proc_free (struct proc *proc);
115 int proc_update_sc (struct proc *proc);
116 error_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
117 error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
118 static mach_port_t _proc_get_exc_port (struct proc *proc);
119 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
120 void proc_restore_exc_port (struct proc *proc);
121 int proc_trace (struct proc *proc, int set);
123 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
124 to INF's msg port and task port respectively. If it has no msg port,
125 EIEIO is returned. INF must refer to a running process! */
126 #define INF_MSGPORT_RPC(inf, rpc_expr) \
127 HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
128 (refport = inf->task->port, 0), 0, \
129 msgport ? (rpc_expr) : EIEIO)
131 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
132 there's someone around to deal with the RPC (and resuspend things
133 afterwards). This effects INF's threads' resume_sc count. */
134 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
135 (inf_set_threads_resume_sc_for_signal_thread (inf) \
138 __e = INF_MSGPORT_RPC (inf, rpc_expr); \
143 #define MIG_SERVER_DIED EMIG_SERVER_DIED /* XXX */
145 /* The state passed by an exception message. */
148 int exception; /* The exception code */
150 mach_port_t handler; /* The real exception port to handle this. */
151 mach_port_t reply; /* The reply port from the exception call. */
154 /* The results of the last wait an inf did. */
157 struct target_waitstatus status; /* The status returned to gdb. */
158 struct exc_state exc; /* The exception that caused us to return. */
159 struct proc *thread; /* The thread in question. */
160 int suppress; /* Something trivial happened. */
163 /* The state of an inferior. */
166 /* Fields describing the current inferior. */
168 struct proc *task; /* The mach task. */
169 struct proc *threads; /* A linked list of all threads in TASK. */
171 /* True if THREADS needn't be validated by querying the task. We assume that
172 we and the task in question are the only ones frobbing the thread list,
173 so as long as we don't let any code run, we don't have to worry about
175 int threads_up_to_date;
177 pid_t pid; /* The real system PID. */
179 struct inf_wait wait; /* What to return from target_wait. */
181 /* One thread proc in INF may be in `single-stepping mode'. This is it. */
182 struct proc *step_thread;
184 /* The thread we think is the signal thread. */
185 struct proc *signal_thread;
187 mach_port_t event_port; /* Where we receive various msgs. */
189 /* True if we think at least one thread in the inferior could currently be
191 unsigned int running:1;
193 /* True if the process has stopped (in the proc server sense). Note that
194 since a proc server `stop' leaves the signal thread running, the inf can
195 be RUNNING && STOPPED... */
196 unsigned int stopped:1;
198 /* True if the inferior has no message port. */
199 unsigned int nomsg:1;
201 /* True if the inferior is traced. */
202 unsigned int traced:1;
204 /* True if we shouldn't try waiting for the inferior, usually because we
205 can't for some reason. */
206 unsigned int no_wait:1;
208 /* When starting a new inferior, we don't try to validate threads until all
209 the proper execs have been done. This is a count of how many execs we
211 unsigned pending_execs;
213 /* Fields describing global state */
215 /* The task suspend count used when gdb has control. This is normally 1 to
216 make things easier for us, but sometimes (like when attaching to vital
217 system servers) it may be desirable to let the task continue to run
218 (pausing individual threads as necessary). */
221 /* The task suspend count left when detaching from a task. */
224 /* The initial values used for the run_sc and pause_sc of newly discovered
225 threads -- see the definition of those fields in struct proc. */
226 int default_thread_run_sc;
227 int default_thread_pause_sc;
228 int default_thread_detach_sc;
230 /* True if the process should be traced when started/attached. Newly
231 started processes *must* be traced at first to exec them properly, but
232 if this is false, tracing is turned off as soon it has done so. */
235 /* True if exceptions from the inferior process should be trapped. This
236 must be on to use breakpoints. */
242 __proc_pid (struct proc *proc)
244 return proc->inf->pid;
247 /* Update PROC's real suspend count to match it's desired one. Returns true
248 if we think PROC is now in a runnable state. */
250 proc_update_sc (struct proc *proc)
254 int delta = proc->sc - proc->cur_sc;
257 proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
259 if (proc->sc == 0 && proc->state_changed)
260 /* Since PROC may start running, we must write back any state changes. */
262 assert (proc_is_thread (proc));
263 proc_debug (proc, "storing back changed thread state");
264 err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
265 (thread_state_t) & proc->state, THREAD_STATE_SIZE);
267 proc->state_changed = 0;
271 while (delta-- > 0 && !err)
272 if (proc_is_task (proc))
273 err = task_suspend (proc->port);
275 err = thread_suspend (proc->port);
277 while (delta++ < 0 && !err)
278 if (proc_is_task (proc))
279 err = task_resume (proc->port);
281 err = thread_resume (proc->port);
284 proc->cur_sc = proc->sc;
286 /* If we got an error, then the task/thread has disappeared. */
287 running = !err && proc->sc == 0;
289 proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
291 proc_debug (proc, "err = %s", strerror (err));
296 proc->state_valid = proc->state_changed = 0;
297 proc->fetched_regs = 0;
303 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
304 If PROC is deemed `precious', then nothing is done unless FORCE is true.
305 In particular, a thread is precious if it's running (in which case forcing
306 it includes suspending it first), or if it has an exception pending. */
308 proc_abort (struct proc *proc, int force)
310 assert (proc_is_thread (proc));
314 struct inf *inf = proc->inf;
315 int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
317 if (running && force)
320 inf_update_suspends (proc->inf);
322 warning ("Stopped %s.", proc_string (proc));
324 else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
325 /* An exception is pending on PROC, which don't mess with. */
329 /* We only abort the thread if it's not actually running. */
331 thread_abort (proc->port);
332 proc_debug (proc, "aborted");
336 proc_debug (proc, "not aborting");
340 /* Make sure that the state field in PROC is up to date, and return a pointer
341 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
342 that the thread is stopped and aborted first, and sets the state_changed
343 field in PROC to true. */
345 proc_get_state (struct proc *proc, int will_modify)
347 int was_aborted = proc->aborted;
349 proc_debug (proc, "updating state info%s",
350 will_modify ? " (with intention to modify)" : "");
352 proc_abort (proc, will_modify);
354 if (!was_aborted && proc->aborted)
355 /* PROC's state may have changed since we last fetched it. */
356 proc->state_valid = 0;
358 if (!proc->state_valid)
360 mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
362 thread_get_state (proc->port, THREAD_STATE_FLAVOR,
363 (thread_state_t) & proc->state, &state_size);
364 proc_debug (proc, "getting thread state");
365 proc->state_valid = !err;
368 if (proc->state_valid)
371 proc->state_changed = 1;
372 return (thread_state_t) & proc->state;
378 /* Set PORT to PROC's exception port. */
380 proc_get_exception_port (struct proc * proc, mach_port_t * port)
382 if (proc_is_task (proc))
383 return task_get_exception_port (proc->port, port);
385 return thread_get_exception_port (proc->port, port);
388 /* Set PROC's exception port to PORT. */
390 proc_set_exception_port (struct proc * proc, mach_port_t port)
392 proc_debug (proc, "setting exception port: %d", port);
393 if (proc_is_task (proc))
394 return task_set_exception_port (proc->port, port);
396 return thread_set_exception_port (proc->port, port);
399 /* Get PROC's exception port, cleaning up a bit if proc has died. */
401 _proc_get_exc_port (struct proc *proc)
403 mach_port_t exc_port;
404 error_t err = proc_get_exception_port (proc, &exc_port);
407 /* PROC must be dead. */
410 mach_port_deallocate (mach_task_self (), proc->exc_port);
411 proc->exc_port = MACH_PORT_NULL;
412 if (proc->saved_exc_port)
413 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
414 proc->saved_exc_port = MACH_PORT_NULL;
420 /* Replace PROC's exception port with EXC_PORT, unless it's already been
421 done. Stash away any existing exception port so we can restore it later. */
423 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
425 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
431 proc_debug (proc, "inserting exception port: %d", exc_port);
433 if (cur_exc_port != exc_port)
434 /* Put in our exception port. */
435 err = proc_set_exception_port (proc, exc_port);
437 if (err || cur_exc_port == proc->exc_port)
438 /* We previously set the exception port, and it's still set. So we
439 just keep the old saved port which is what the proc set. */
442 mach_port_deallocate (mach_task_self (), cur_exc_port);
445 /* Keep a copy of PROC's old exception port so it can be restored. */
447 if (proc->saved_exc_port)
448 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
449 proc->saved_exc_port = cur_exc_port;
452 proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
455 proc->exc_port = exc_port;
457 warning ("Error setting exception port for %s: %s",
458 proc_string (proc), strerror (err));
462 /* If we previously replaced PROC's exception port, put back what we
463 found there at the time, unless *our* exception port has since been
464 overwritten, in which case who knows what's going on. */
466 proc_restore_exc_port (struct proc *proc)
468 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
474 proc_debug (proc, "restoring real exception port");
476 if (proc->exc_port == cur_exc_port)
477 /* Our's is still there. */
478 err = proc_set_exception_port (proc, proc->saved_exc_port);
480 if (proc->saved_exc_port)
481 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
482 proc->saved_exc_port = MACH_PORT_NULL;
485 proc->exc_port = MACH_PORT_NULL;
487 warning ("Error setting exception port for %s: %s",
488 proc_string (proc), strerror (err));
492 /* Turns hardware tracing in PROC on or off when SET is true or false,
493 respectively. Returns true on success. */
495 proc_trace (struct proc *proc, int set)
497 thread_state_t state = proc_get_state (proc, 1);
500 return 0; /* the thread must be dead. */
502 proc_debug (proc, "tracing %s", set ? "on" : "off");
506 /* XXX We don't get the exception unless the thread has its own
507 exception port???? */
508 if (proc->exc_port == MACH_PORT_NULL)
509 proc_steal_exc_port (proc, proc->inf->event_port);
510 THREAD_STATE_SET_TRACED (state);
513 THREAD_STATE_CLEAR_TRACED (state);
518 /* A variable from which to assign new TIDs. */
519 static int next_thread_id = 1;
521 /* Returns a new proc structure with the given fields. Also adds a
522 notification for PORT becoming dead to be sent to INF's notify port. */
524 make_proc (struct inf *inf, mach_port_t port, int tid)
527 mach_port_t prev_port = MACH_PORT_NULL;
528 struct proc *proc = malloc (sizeof (struct proc));
534 proc->saved_exc_port = MACH_PORT_NULL;
535 proc->exc_port = MACH_PORT_NULL;
540 /* Note that these are all the values for threads; the task simply uses the
541 corresponding field in INF directly. */
542 proc->run_sc = inf->default_thread_run_sc;
543 proc->pause_sc = inf->default_thread_pause_sc;
544 proc->detach_sc = inf->default_thread_detach_sc;
545 proc->resume_sc = proc->run_sc;
549 proc->state_valid = 0;
550 proc->state_changed = 0;
552 proc_debug (proc, "is new");
554 /* Get notified when things die. */
556 mach_port_request_notification (mach_task_self (), port,
557 MACH_NOTIFY_DEAD_NAME, 1,
559 MACH_MSG_TYPE_MAKE_SEND_ONCE,
562 warning ("Couldn't request notification for port %d: %s",
563 port, strerror (err));
566 proc_debug (proc, "notifications to: %d", inf->event_port);
567 if (prev_port != MACH_PORT_NULL)
568 mach_port_deallocate (mach_task_self (), prev_port);
571 if (inf->want_exceptions)
572 if (proc_is_task (proc))
573 /* Make the task exception port point to us. */
574 proc_steal_exc_port (proc, inf->event_port);
576 /* Just clear thread exception ports -- they default to the task one. */
577 proc_steal_exc_port (proc, MACH_PORT_NULL);
582 /* Frees PROC and any resources it uses, and returns the value of PROC's
585 _proc_free (struct proc *proc)
587 struct inf *inf = proc->inf;
588 struct proc *next = proc->next;
590 proc_debug (proc, "freeing...");
592 if (proc == inf->step_thread)
593 /* Turn off single stepping. */
594 inf_set_step_thread (inf, 0);
595 if (proc == inf->wait.thread)
596 inf_clear_wait (inf);
597 if (proc == inf->signal_thread)
598 inf->signal_thread = 0;
600 if (proc->port != MACH_PORT_NULL)
602 if (proc->exc_port != MACH_PORT_NULL)
603 /* Restore the original exception port. */
604 proc_restore_exc_port (proc);
605 if (proc->cur_sc != 0)
606 /* Resume the thread/task. */
609 proc_update_sc (proc);
611 mach_port_deallocate (mach_task_self (), proc->port);
621 struct inf *inf = malloc (sizeof (struct inf));
628 inf->threads_up_to_date = 0;
630 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
631 inf->wait.thread = 0;
632 inf->wait.exc.handler = MACH_PORT_NULL;
633 inf->wait.exc.reply = MACH_PORT_NULL;
634 inf->step_thread = 0;
635 inf->signal_thread = 0;
636 inf->event_port = MACH_PORT_NULL;
642 inf->pending_execs = 0;
645 inf->default_thread_run_sc = 0;
646 inf->default_thread_pause_sc = 0;
647 inf->default_thread_detach_sc = 0;
648 inf->want_signals = 1; /* By default */
649 inf->want_exceptions = 1; /* By default */
654 /* clear INF's target wait status. */
656 inf_clear_wait (struct inf *inf)
658 inf_debug (inf, "clearing wait");
659 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
660 inf->wait.thread = 0;
661 inf->wait.suppress = 0;
662 if (inf->wait.exc.handler != MACH_PORT_NULL)
664 mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
665 inf->wait.exc.handler = MACH_PORT_NULL;
667 if (inf->wait.exc.reply != MACH_PORT_NULL)
669 mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
670 inf->wait.exc.reply = MACH_PORT_NULL;
675 inf_cleanup (struct inf *inf)
677 inf_debug (inf, "cleanup");
679 inf_clear_wait (inf);
681 inf_set_pid (inf, -1);
688 inf->pending_execs = 0;
692 mach_port_destroy (mach_task_self (), inf->event_port);
693 inf->event_port = MACH_PORT_NULL;
698 inf_startup (struct inf *inf, int pid)
702 inf_debug (inf, "startup: pid = %d", pid);
706 /* Make the port on which we receive all events. */
707 err = mach_port_allocate (mach_task_self (),
708 MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
710 error ("Error allocating event port: %s", strerror (err));
712 /* Make a send right for it, so we can easily copy it for other people. */
713 mach_port_insert_right (mach_task_self (), inf->event_port,
714 inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
715 inf_set_pid (inf, pid);
718 /* close current process, if any, and attach INF to process PORT */
720 inf_set_pid (struct inf *inf, pid_t pid)
723 struct proc *task = inf->task;
725 inf_debug (inf, "setting pid: %d", pid);
728 task_port = MACH_PORT_NULL;
731 error_t err = proc_pid2task (proc_server, pid, &task_port);
733 error ("Error getting task for pid %d: %s", pid, strerror (err));
736 inf_debug (inf, "setting task: %d", task_port);
739 task_suspend (task_port);
741 if (task && task->port != task_port)
744 inf_validate_procs (inf); /* Trash all the threads. */
745 _proc_free (task); /* And the task. */
748 if (task_port != MACH_PORT_NULL)
750 inf->task = make_proc (inf, task_port, PROC_TID_TASK);
751 inf->threads_up_to_date = 0;
758 inf->task->sc = inf->task->cur_sc = 1; /* Reflect task_suspend above */
764 /* Validates INF's stopped, nomsg and traced field from the actual
765 proc server state. Note that the traced field is only updated from
766 the proc server state if we do not have a message port. If we do
767 have a message port we'd better look at the tracemask itself. */
769 inf_validate_procinfo (struct inf *inf)
772 mach_msg_type_number_t noise_len = 0;
774 mach_msg_type_number_t pi_len = 0;
777 proc_getprocinfo (proc_server, inf->pid, &info_flags,
778 (procinfo_t *) & pi, &pi_len, &noise, &noise_len);
782 inf->stopped = !!(pi->state & PI_STOPPED);
783 inf->nomsg = !!(pi->state & PI_NOMSG);
785 inf->traced = !!(pi->state & PI_TRACED);
786 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
788 vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
792 /* Validates INF's task suspend count. If it's higher than we expect,
793 verify with the user before `stealing' the extra count. */
795 inf_validate_task_sc (struct inf *inf)
798 mach_msg_type_number_t noise_len = 0;
800 mach_msg_type_number_t pi_len = 0;
801 int info_flags = PI_FETCH_TASKINFO;
802 int suspend_count = -1;
806 err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
807 &pi, &pi_len, &noise, &noise_len);
810 inf->task->dead = 1; /* oh well */
814 if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
816 /* The proc server might have suspended the task while stopping
817 it. This happens when the task is handling a traced signal.
818 Refetch the suspend count. The proc server should be
819 finished stopping the task by now. */
820 suspend_count = pi->taskinfo.suspend_count;
824 suspend_count = pi->taskinfo.suspend_count;
826 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
828 vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
830 if (inf->task->cur_sc < suspend_count)
834 target_terminal_ours (); /* Allow I/O. */
835 abort = !query ("Pid %d has an additional task suspend count of %d;"
836 " clear it? ", inf->pid,
837 suspend_count - inf->task->cur_sc);
838 target_terminal_inferior (); /* Give it back to the child. */
841 error ("Additional task suspend count left untouched.");
843 inf->task->cur_sc = suspend_count;
847 /* Turns tracing for INF on or off, depending on ON, unless it already is.
848 If INF is running, the resume_sc count of INF's threads will be modified,
849 and the signal thread will briefly be run to change the trace state. */
851 inf_set_traced (struct inf *inf, int on)
853 if (on != inf->traced)
854 if (inf->task && !inf->task->dead)
855 /* Make it take effect immediately. */
857 sigset_t mask = on ? ~(sigset_t) 0 : 0;
859 INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
860 INIT_TRACEMASK, mask));
864 warning ("Can't modify tracing state for pid %d: No signal thread",
869 warning ("Can't modify tracing state for pid %d: %s",
870 inf->pid, strerror (err));
878 /* Makes all the real suspend count deltas of all the procs in INF match the
879 desired values. Careful to always do thread/task suspend counts in the
880 safe order. Returns true if at least one thread is thought to be running. */
882 inf_update_suspends (struct inf *inf)
884 struct proc *task = inf->task;
885 /* We don't have to update INF->threads even though we're iterating over it
886 because we'll change a thread only if it already has an existing proc
889 inf_debug (inf, "updating suspend counts");
894 int task_running = (task->sc == 0), thread_running = 0;
896 if (task->sc > task->cur_sc)
897 /* The task is becoming _more_ suspended; do before any threads. */
898 task_running = proc_update_sc (task);
900 if (inf->pending_execs)
901 /* When we're waiting for an exec, things may be happening behind our
902 back, so be conservative. */
905 /* Do all the thread suspend counts. */
906 for (thread = inf->threads; thread; thread = thread->next)
907 thread_running |= proc_update_sc (thread);
909 if (task->sc != task->cur_sc)
910 /* We didn't do the task first, because we wanted to wait for the
911 threads; do it now. */
912 task_running = proc_update_sc (task);
914 inf_debug (inf, "%srunning...",
915 (thread_running && task_running) ? "" : "not ");
917 inf->running = thread_running && task_running;
919 /* Once any thread has executed some code, we can't depend on the
920 threads list any more. */
922 inf->threads_up_to_date = 0;
930 /* Converts a GDB pid to a struct proc. */
932 inf_tid_to_thread (struct inf *inf, int tid)
934 struct proc *thread = inf->threads;
937 if (thread->tid == tid)
940 thread = thread->next;
944 /* Converts a thread port to a struct proc. */
946 inf_port_to_thread (struct inf *inf, mach_port_t port)
948 struct proc *thread = inf->threads;
950 if (thread->port == port)
953 thread = thread->next;
957 /* Make INF's list of threads be consistent with reality of TASK. */
959 inf_validate_procs (struct inf *inf)
962 thread_array_t threads;
963 unsigned num_threads;
964 struct proc *task = inf->task;
966 /* If no threads are currently running, this function will guarantee that
967 things are up to date. The exception is if there are zero threads --
968 then it is almost certainly in an odd state, and probably some outside
969 agent will create threads. */
970 inf->threads_up_to_date = inf->threads ? !inf->running : 0;
974 error_t err = task_threads (task->port, &threads, &num_threads);
975 inf_debug (inf, "fetching threads");
977 /* TASK must be dead. */
987 inf_debug (inf, "no task");
991 unsigned search_start = 0; /* Make things normally linear. */
992 /* Which thread in PROCS corresponds to each task thread, & the task. */
993 struct proc *matched[num_threads + 1];
994 /* The last thread in INF->threads, so we can add to the end. */
995 struct proc *last = 0;
996 /* The current thread we're considering. */
997 struct proc *thread = inf->threads;
999 bzero (matched, sizeof (matched));
1005 for (i = search_start, left = num_threads; left; i++, left--)
1007 if (i >= num_threads)
1008 i -= num_threads; /* I wrapped around. */
1009 if (thread->port == threads[i])
1010 /* We already know about this thread. */
1012 matched[i] = thread;
1014 thread = thread->next;
1022 proc_debug (thread, "died!");
1023 thread->port = MACH_PORT_NULL;
1024 thread = _proc_free (thread); /* THREAD is dead. */
1025 (last ? last->next : inf->threads) = thread;
1029 for (i = 0; i < num_threads; i++)
1031 /* Throw away the duplicate send right. */
1032 mach_port_deallocate (mach_task_self (), threads[i]);
1034 /* THREADS[I] is a thread we don't know about yet! */
1036 thread = make_proc (inf, threads[i], next_thread_id++);
1037 (last ? last->next : inf->threads) = thread;
1039 proc_debug (thread, "new thread: %d", threads[i]);
1040 add_thread (thread->tid); /* Tell GDB's generic thread code. */
1043 vm_deallocate (mach_task_self (),
1044 (vm_address_t) threads, (num_threads * sizeof (thread_t)));
1048 /* Makes sure that INF's thread list is synced with the actual process. */
1050 inf_update_procs (struct inf *inf)
1054 if (!inf->threads_up_to_date)
1055 inf_validate_procs (inf);
1059 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
1060 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1063 inf_set_threads_resume_sc (struct inf *inf,
1064 struct proc *run_thread, int run_others)
1066 struct proc *thread;
1067 inf_update_procs (inf);
1068 for (thread = inf->threads; thread; thread = thread->next)
1069 if (thread == run_thread)
1070 thread->resume_sc = 0;
1071 else if (run_others)
1072 thread->resume_sc = thread->run_sc;
1074 thread->resume_sc = thread->pause_sc;
1077 /* Cause INF to continue execution immediately; individual threads may still
1078 be suspended (but their suspend counts will be updated). */
1080 inf_resume (struct inf *inf)
1082 struct proc *thread;
1084 inf_update_procs (inf);
1086 for (thread = inf->threads; thread; thread = thread->next)
1087 thread->sc = thread->resume_sc;
1091 if (!inf->pending_execs)
1092 /* Try to make sure our task count is correct -- in the case where
1093 we're waiting for an exec though, things are too volatile, so just
1094 assume things will be reasonable (which they usually will be). */
1095 inf_validate_task_sc (inf);
1099 inf_update_suspends (inf);
1102 /* Cause INF to stop execution immediately; individual threads may still
1105 inf_suspend (struct inf *inf)
1107 struct proc *thread;
1109 inf_update_procs (inf);
1111 for (thread = inf->threads; thread; thread = thread->next)
1112 thread->sc = thread->pause_sc;
1115 inf->task->sc = inf->pause_sc;
1117 inf_update_suspends (inf);
1120 /* INF has one thread PROC that is in single-stepping mode. This function
1121 changes it to be PROC, changing any old step_thread to be a normal one. A
1122 PROC of 0 clears any existing value. */
1124 inf_set_step_thread (struct inf *inf, struct proc *thread)
1126 assert (!thread || proc_is_thread (thread));
1129 inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1131 inf_debug (inf, "clearing step thread");
1133 if (inf->step_thread != thread)
1135 if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1136 if (!proc_trace (inf->step_thread, 0))
1138 if (thread && proc_trace (thread, 1))
1139 inf->step_thread = thread;
1141 inf->step_thread = 0;
1145 /* Set up the thread resume_sc's so that only the signal thread is running
1146 (plus whatever other thread are set to always run). Returns true if we
1147 did so, or false if we can't find a signal thread. */
1149 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1151 if (inf->signal_thread)
1153 inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1161 inf_update_signal_thread (struct inf *inf)
1163 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1164 the signal thread. */
1165 inf->signal_thread = inf->threads ? inf->threads->next : 0;
1168 /* Detachs from INF's inferior task, letting it run once again... */
1170 inf_detach (struct inf *inf)
1172 struct proc *task = inf->task;
1174 inf_debug (inf, "detaching...");
1176 inf_clear_wait (inf);
1177 inf_set_step_thread (inf, 0);
1181 struct proc *thread;
1183 inf_validate_procinfo (inf);
1185 inf_set_traced (inf, 0);
1191 inf_signal (inf, TARGET_SIGNAL_0);
1194 proc_restore_exc_port (task);
1195 task->sc = inf->detach_sc;
1197 for (thread = inf->threads; thread; thread = thread->next)
1199 proc_restore_exc_port (thread);
1200 thread->sc = thread->detach_sc;
1203 inf_update_suspends (inf);
1209 /* Attaches INF to the process with process id PID, returning it in a suspended
1210 state suitable for debugging. */
1212 inf_attach (struct inf *inf, int pid)
1214 inf_debug (inf, "attaching: %d", pid);
1219 inf_startup (inf, pid);
1222 /* Makes sure that we've got our exception ports entrenched in the process. */
1224 inf_steal_exc_ports (struct inf *inf)
1226 struct proc *thread;
1228 inf_debug (inf, "stealing exception ports");
1230 inf_set_step_thread (inf, 0); /* The step thread is special. */
1232 proc_steal_exc_port (inf->task, inf->event_port);
1233 for (thread = inf->threads; thread; thread = thread->next)
1234 proc_steal_exc_port (thread, MACH_PORT_NULL);
1237 /* Makes sure the process has its own exception ports. */
1239 inf_restore_exc_ports (struct inf *inf)
1241 struct proc *thread;
1243 inf_debug (inf, "restoring exception ports");
1245 inf_set_step_thread (inf, 0); /* The step thread is special. */
1247 proc_restore_exc_port (inf->task);
1248 for (thread = inf->threads; thread; thread = thread->next)
1249 proc_restore_exc_port (thread);
1252 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1253 signal 0, will continue it. INF is assumed to be in a paused state, and
1254 the resume_sc's of INF's threads may be affected. */
1256 inf_signal (struct inf *inf, enum target_signal sig)
1259 int host_sig = target_signal_to_host (sig);
1261 #define NAME target_signal_to_name (sig)
1263 if (host_sig >= _NSIG)
1264 /* A mach exception. Exceptions are encoded in the signal space by
1265 putting them after _NSIG; this assumes they're positive (and not
1266 extremely large)! */
1268 struct inf_wait *w = &inf->wait;
1269 if (w->status.kind == TARGET_WAITKIND_STOPPED
1270 && w->status.value.sig == sig
1271 && w->thread && !w->thread->aborted)
1272 /* We're passing through the last exception we received. This is
1273 kind of bogus, because exceptions are per-thread whereas gdb
1274 treats signals as per-process. We just forward the exception to
1275 the correct handler, even it's not for the same thread as TID --
1276 i.e., we pretend it's global. */
1278 struct exc_state *e = &w->exc;
1279 inf_debug (inf, "passing through exception:"
1280 " task = %d, thread = %d, exc = %d"
1281 ", code = %d, subcode = %d",
1282 w->thread->port, inf->task->port,
1283 e->exception, e->code, e->subcode);
1285 exception_raise_request (e->handler,
1286 e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1287 w->thread->port, inf->task->port,
1288 e->exception, e->code, e->subcode);
1291 error ("Can't forward spontaneous exception (%s).", NAME);
1294 /* A Unix signal. */
1296 /* The process is stopped and expecting a signal. Just send off a
1297 request and let it get handled when we resume everything. */
1299 inf_debug (inf, "sending %s to stopped process", NAME);
1301 INF_MSGPORT_RPC (inf,
1302 msg_sig_post_untraced_request (msgport,
1304 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1308 /* Posting an untraced signal automatically continues it.
1309 We clear this here rather than when we get the reply
1310 because we'd rather assume it's not stopped when it
1311 actually is, than the reverse. */
1315 /* It's not expecting it. We have to let just the signal thread
1316 run, and wait for it to get into a reasonable state before we
1317 can continue the rest of the process. When we finally resume the
1318 process the signal we request will be the very first thing that
1321 inf_debug (inf, "sending %s to unstopped process (so resuming signal thread)", NAME);
1323 INF_RESUME_MSGPORT_RPC (inf, msg_sig_post_untraced (msgport,
1324 host_sig, 0, refport));
1328 /* Can't do too much... */
1329 warning ("Can't deliver signal %s: No signal thread.", NAME);
1331 warning ("Delivering signal %s: %s", NAME, strerror (err));
1336 /* Continue INF without delivering a signal. This is meant to be used
1337 when INF does not have a message port. */
1339 inf_continue (struct inf *inf)
1342 error_t err = proc_pid2proc (proc_server, inf->pid, &proc);
1346 inf_debug (inf, "continuing process");
1348 err = proc_mark_cont (proc);
1351 struct proc *thread;
1353 for (thread = inf->threads; thread; thread = thread->next)
1354 thread_resume (thread->port);
1361 warning ("Can't continue process: %s", strerror (err));
1364 /* The inferior used for all gdb target ops. */
1365 struct inf *current_inferior = 0;
1367 /* The inferior being waited for by gnu_wait. Since GDB is decidely not
1368 multi-threaded, we don't bother to lock this. */
1369 struct inf *waiting_inf;
1371 /* Wait for something to happen in the inferior, returning what in STATUS. */
1373 gnu_wait (int tid, struct target_waitstatus *status)
1377 mach_msg_header_t hdr;
1378 mach_msg_type_t type;
1383 struct proc *thread;
1384 struct inf *inf = current_inferior;
1388 if (!inf->threads && !inf->pending_execs)
1389 /* No threads! Assume that maybe some outside agency is frobbing our
1390 task, and really look for new threads. If we can't find any, just tell
1391 the user to try again later. */
1393 inf_validate_procs (inf);
1394 if (!inf->threads && !inf->task->dead)
1395 error ("There are no threads; try again later.");
1400 inf_debug (inf, "waiting for: %d", tid);
1403 if (proc_wait_pid != inf->pid && !inf->no_wait)
1404 /* Always get information on events from the proc server. */
1406 inf_debug (inf, "requesting wait on pid %d", inf->pid);
1409 /* The proc server is single-threaded, and only allows a single
1410 outstanding wait request, so we have to cancel the previous one. */
1412 inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1413 interrupt_operation (proc_server, 0);
1417 proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1419 warning ("wait request failed: %s", strerror (err));
1422 inf_debug (inf, "waits pending: %d", proc_waits_pending);
1423 proc_wait_pid = inf->pid;
1424 /* Even if proc_waits_pending was > 0 before, we still won't get
1425 any other replies, because it was either from a different INF,
1426 or a different process attached to INF -- and the event port,
1427 which is the wait reply port, changes when you switch processes. */
1428 proc_waits_pending = 1;
1432 inf_clear_wait (inf);
1434 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1435 (3) wait reply from the proc server. */
1437 inf_debug (inf, "waiting for an event...");
1438 err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
1439 0, sizeof (struct msg), inf->event_port,
1440 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1442 /* Re-suspend the task. */
1445 if (!inf->task && inf->pending_execs)
1446 /* When doing an exec, it's possible that the old task wasn't reused
1447 (e.g., setuid execs). So if the task seems to have disappeared,
1448 attempt to refetch it, as the pid should still be the same. */
1449 inf_set_pid (inf, inf->pid);
1451 if (err == EMACH_RCV_INTERRUPTED)
1452 inf_debug (inf, "interrupted");
1454 error ("Couldn't wait for an event: %s", strerror (err));
1459 mach_msg_header_t hdr;
1460 mach_msg_type_t err_type;
1466 inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1468 /* Handle what we got. */
1469 if (!notify_server (&msg.hdr, &reply.hdr)
1470 && !exc_server (&msg.hdr, &reply.hdr)
1471 && !process_reply_server (&msg.hdr, &reply.hdr)
1472 && !msg_reply_server (&msg.hdr, &reply.hdr))
1473 /* Whatever it is, it's something strange. */
1474 error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id);
1477 error ("Handling event, msgid = %d: %s",
1478 msg.hdr.msgh_id, strerror (reply.err));
1481 if (inf->pending_execs)
1482 /* We're waiting for the inferior to finish execing. */
1484 struct inf_wait *w = &inf->wait;
1485 enum target_waitkind kind = w->status.kind;
1487 if (kind == TARGET_WAITKIND_SPURIOUS)
1488 /* Since gdb is actually counting the number of times the inferior
1489 stops, expecting one stop per exec, we only return major events
1493 inf_debug (inf, "pending_execs = %d, ignoring minor event",
1494 inf->pending_execs);
1496 else if (kind == TARGET_WAITKIND_STOPPED
1497 && w->status.value.sig == TARGET_SIGNAL_TRAP)
1498 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1499 means we've succesfully completed an exec! */
1501 if (--inf->pending_execs == 0)
1504 #if 0 /* do we need this? */
1505 prune_threads (1); /* Get rid of the old shell threads */
1506 renumber_threads (0); /* Give our threads reasonable names. */
1509 inf_debug (inf, "pending exec completed, pending_execs => %d",
1510 inf->pending_execs);
1512 else if (kind == TARGET_WAITKIND_STOPPED)
1513 /* It's possible that this signal is because of a crashed process
1514 being handled by the hurd crash server; in this case, the process
1515 will have an extra task suspend, which we need to know about.
1516 Since the code in inf_resume that normally checks for this is
1517 disabled while INF->pending_execs, we do the check here instead. */
1518 inf_validate_task_sc (inf);
1521 if (inf->wait.suppress)
1522 /* Some totally spurious event happened that we don't consider
1523 worth returning to gdb. Just keep waiting. */
1525 inf_debug (inf, "suppressing return, rewaiting...");
1530 /* Pass back out our results. */
1531 bcopy (&inf->wait.status, status, sizeof (*status));
1533 thread = inf->wait.thread;
1537 thread = inf_tid_to_thread (inf, tid);
1539 if (!thread || thread->port == MACH_PORT_NULL)
1540 /* TID is dead; try and find a new thread. */
1541 if (inf_update_procs (inf) && inf->threads)
1542 tid = inf->threads->tid; /* The first available thread. */
1544 tid = inferior_pid; /* let wait_for_inferior handle exit case */
1546 if (thread && tid >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS
1547 && inf->pause_sc == 0 && thread->pause_sc == 0)
1548 /* If something actually happened to THREAD, make sure we suspend it. */
1551 inf_update_suspends (inf);
1554 inf_debug (inf, "returning tid = %d, status = %s (%d)", tid,
1555 status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
1556 : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
1557 : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
1558 : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
1559 : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
1561 status->value.integer);
1566 /* The rpc handler called by exc_server. */
1568 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1569 thread_t thread_port, task_t task_port,
1570 int exception, int code, int subcode)
1572 struct inf *inf = waiting_inf;
1573 struct proc *thread = inf_port_to_thread (inf, thread_port);
1575 inf_debug (waiting_inf,
1576 "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1577 thread_port, task_port, exception, code);
1580 /* We don't know about thread? */
1582 inf_update_procs (inf);
1583 thread = inf_port_to_thread (inf, thread_port);
1585 /* Give up, the generating thread is gone. */
1589 mach_port_deallocate (mach_task_self (), thread_port);
1590 mach_port_deallocate (mach_task_self (), task_port);
1592 if (!thread->aborted)
1593 /* THREAD hasn't been aborted since this exception happened (abortion
1594 clears any exception state), so it must be real. */
1596 /* Store away the details; this will destroy any previous info. */
1597 inf->wait.thread = thread;
1599 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1601 if (exception == EXC_BREAKPOINT)
1602 /* GDB likes to get SIGTRAP for breakpoints. */
1604 inf->wait.status.value.sig = TARGET_SIGNAL_TRAP;
1605 mach_port_deallocate (mach_task_self (), reply_port);
1608 /* Record the exception so that we can forward it later. */
1610 if (thread->exc_port == port)
1612 inf_debug (waiting_inf, "Handler is thread exeption port <%d>",
1613 thread->saved_exc_port);
1614 inf->wait.exc.handler = thread->saved_exc_port;
1618 inf_debug (waiting_inf, "Handler is task exeption port <%d>",
1619 inf->task->saved_exc_port);
1620 inf->wait.exc.handler = inf->task->saved_exc_port;
1621 assert (inf->task->exc_port == port);
1623 if (inf->wait.exc.handler != MACH_PORT_NULL)
1624 /* Add a reference to the exception handler. */
1625 mach_port_mod_refs (mach_task_self (),
1626 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1629 inf->wait.exc.exception = exception;
1630 inf->wait.exc.code = code;
1631 inf->wait.exc.subcode = subcode;
1632 inf->wait.exc.reply = reply_port;
1634 /* Exceptions are encoded in the signal space by putting them after
1635 _NSIG; this assumes they're positive (and not extremely large)! */
1636 inf->wait.status.value.sig =
1637 target_signal_from_host (_NSIG + exception);
1641 /* A supppressed exception, which ignore. */
1643 inf->wait.suppress = 1;
1644 mach_port_deallocate (mach_task_self (), reply_port);
1650 /* Fill in INF's wait field after a task has died without giving us more
1651 detailed information. */
1653 inf_task_died_status (struct inf *inf)
1655 warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid);
1656 inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
1657 inf->wait.status.value.sig = TARGET_SIGNAL_KILL;
1660 /* Notify server routines. The only real one is dead name notification. */
1662 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1664 struct inf *inf = waiting_inf;
1666 inf_debug (waiting_inf, "port = %d", dead_port);
1668 if (inf->task && inf->task->port == dead_port)
1670 proc_debug (inf->task, "is dead");
1671 inf->task->port = MACH_PORT_NULL;
1672 if (proc_wait_pid == inf->pid)
1673 /* We have a wait outstanding on the process, which will return more
1674 detailed information, so delay until we get that. */
1675 inf->wait.suppress = 1;
1677 /* We never waited for the process (maybe it wasn't a child), so just
1678 pretend it got a SIGKILL. */
1679 inf_task_died_status (inf);
1683 struct proc *thread = inf_port_to_thread (inf, dead_port);
1686 proc_debug (thread, "is dead");
1687 thread->port = MACH_PORT_NULL;
1690 if (inf->task->dead)
1691 /* Since the task is dead, its threads are dying with it. */
1692 inf->wait.suppress = 1;
1695 mach_port_deallocate (mach_task_self (), dead_port);
1696 inf->threads_up_to_date = 0; /* Just in case */
1704 warning ("illegal rpc: %s", fun);
1709 do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
1711 return ill_rpc (__FUNCTION__);
1715 do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
1717 return ill_rpc (__FUNCTION__);
1721 do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
1723 return ill_rpc (__FUNCTION__);
1727 do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
1729 return ill_rpc (__FUNCTION__);
1733 do_mach_notify_send_once (mach_port_t notify)
1735 return ill_rpc (__FUNCTION__);
1738 /* Process_reply server routines. We only use process_wait_reply. */
1741 S_proc_wait_reply (mach_port_t reply, error_t err,
1742 int status, int sigcode, rusage_t rusage, pid_t pid)
1744 struct inf *inf = waiting_inf;
1746 inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1747 err ? strerror (err) : "0", pid, status, sigcode);
1749 if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1750 /* Ack. The task has died, but the task-died notification code didn't
1751 tell anyone because it thought a more detailed reply from the
1752 procserver was forthcoming. However, we now learn that won't
1753 happen... So we have to act like the task just died, and this time,
1755 inf_task_died_status (inf);
1757 if (--proc_waits_pending == 0)
1758 /* PROC_WAIT_PID represents the most recent wait. We will always get
1759 replies in order because the proc server is single threaded. */
1762 inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1768 warning ("Can't wait for pid %d: %s", inf->pid, strerror (err));
1771 /* Since we can't see the inferior's signals, don't trap them. */
1772 inf_set_traced (inf, 0);
1775 else if (pid == inf->pid)
1777 store_waitstatus (&inf->wait.status, status);
1778 if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
1779 /* The process has sent us a signal, and stopped itself in a sane
1780 state pending our actions. */
1782 inf_debug (inf, "process has stopped itself");
1787 inf->wait.suppress = 1; /* Something odd happened. Ignore. */
1793 S_proc_setmsgport_reply (mach_port_t reply, error_t err,
1794 mach_port_t old_msg_port)
1796 return ill_rpc (__FUNCTION__);
1800 S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
1802 return ill_rpc (__FUNCTION__);
1805 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1808 S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
1810 struct inf *inf = waiting_inf;
1813 /* EBUSY is what we get when the crash server has grabbed control of the
1814 process and doesn't like what signal we tried to send it. Just act
1815 like the process stopped (using a signal of 0 should mean that the
1816 *next* time the user continues, it will pass signal 0, which the crash
1817 server should like). */
1819 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1820 inf->wait.status.value.sig = TARGET_SIGNAL_0;
1823 warning ("Signal delivery failed: %s", strerror (err));
1826 /* We only get this reply when we've posted a signal to a process which we
1827 thought was stopped, and which we expected to continue after the signal.
1828 Given that the signal has failed for some reason, it's reasonable to
1829 assume it's still stopped. */
1832 inf->wait.suppress = 1;
1838 S_msg_sig_post_reply (mach_port_t reply, error_t err)
1840 return ill_rpc (__FUNCTION__);
1843 /* Returns the number of messages queued for the receive right PORT. */
1844 static mach_port_msgcount_t
1845 port_msgs_queued (mach_port_t port)
1847 struct mach_port_status status;
1849 mach_port_get_receive_status (mach_task_self (), port, &status);
1854 return status.mps_msgcount;
1857 /* Resume execution of the inferior process.
1859 If STEP is nonzero, single-step it.
1860 If SIGNAL is nonzero, give it that signal.
1863 -1 true Single step the current thread allowing other threads to run.
1864 -1 false Continue the current thread allowing other threads to run.
1865 X true Single step the given thread, don't allow any others to run.
1866 X false Continue the given thread, do not allow any others to run.
1867 (Where X, of course, is anything except -1)
1869 Note that a resume may not `take' if there are pending exceptions/&c
1870 still unprocessed from the last resume we did (any given resume may result
1871 in multiple events returned by wait).
1874 gnu_resume (int tid, int step, enum target_signal sig)
1876 struct proc *step_thread = 0;
1877 struct inf *inf = current_inferior;
1879 inf_debug (inf, "tid = %d, step = %d, sig = %d", tid, step, sig);
1881 inf_validate_procinfo (inf);
1883 if (sig != TARGET_SIGNAL_0 || inf->stopped)
1885 if (sig == TARGET_SIGNAL_0 && inf->nomsg)
1888 inf_signal (inf, sig);
1890 else if (inf->wait.exc.reply != MACH_PORT_NULL)
1891 /* We received an exception to which we have chosen not to forward, so
1892 abort the faulting thread, which will perhaps retake it. */
1894 proc_abort (inf->wait.thread, 1);
1895 warning ("Aborting %s with unforwarded exception %s.",
1896 proc_string (inf->wait.thread),
1897 target_signal_to_name (inf->wait.status.value.sig));
1900 if (port_msgs_queued (inf->event_port))
1901 /* If there are still messages in our event queue, don't bother resuming
1902 the process, as we're just going to stop it right away anyway. */
1905 inf_update_procs (inf);
1908 /* Allow all threads to run, except perhaps single-stepping one. */
1910 inf_debug (inf, "running all threads; tid = %d", inferior_pid);
1911 tid = inferior_pid; /* What to step. */
1912 inf_set_threads_resume_sc (inf, 0, 1);
1915 /* Just allow a single thread to run. */
1917 struct proc *thread = inf_tid_to_thread (inf, tid);
1919 error ("Can't run single thread id %d: no such thread!");
1920 inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid);
1921 inf_set_threads_resume_sc (inf, thread, 0);
1926 step_thread = inf_tid_to_thread (inf, tid);
1928 warning ("Can't step thread id %d: no such thread.", tid);
1930 inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid);
1932 if (step_thread != inf->step_thread)
1933 inf_set_step_thread (inf, step_thread);
1935 inf_debug (inf, "here we go...");
1940 gnu_kill_inferior ()
1942 struct proc *task = current_inferior->task;
1945 proc_debug (task, "terminating...");
1946 task_terminate (task->port);
1947 inf_set_pid (current_inferior, -1);
1949 target_mourn_inferior ();
1952 /* Clean up after the inferior dies. */
1955 gnu_mourn_inferior ()
1957 inf_debug (current_inferior, "rip");
1958 inf_detach (current_inferior);
1959 unpush_target (&gnu_ops);
1960 generic_mourn_inferior ();
1963 /* Fork an inferior process, and start debugging it. */
1965 /* Set INFERIOR_PID to the first thread available in the child, if any. */
1967 inf_pick_first_thread ()
1969 if (current_inferior->task && current_inferior->threads)
1970 /* The first thread. */
1971 return current_inferior->threads->tid;
1973 /* What may be the next thread. */
1974 return next_thread_id;
1980 if (!current_inferior)
1981 current_inferior = make_inf ();
1982 return current_inferior;
1986 gnu_create_inferior (exec_file, allargs, env)
1991 struct inf *inf = cur_inf ();
1995 /* We're in the child; make this process stop as soon as it execs. */
1996 inf_debug (inf, "tracing self");
1997 if (ptrace (PTRACE_TRACEME) != 0)
1998 error ("ptrace (PTRACE_TRACEME) failed!");
2000 void attach_to_child (int pid)
2002 /* Attach to the now stopped child, which is actually a shell... */
2003 inf_debug (inf, "attaching to child: %d", pid);
2005 inf_attach (inf, pid);
2008 push_target (&gnu_ops);
2010 inf->pending_execs = 2;
2014 /* Now let the child run again, knowing that it will stop immediately
2015 because of the ptrace. */
2017 inferior_pid = inf_pick_first_thread ();
2019 startup_inferior (inf->pending_execs);
2022 inf_debug (inf, "creating inferior");
2024 fork_inferior (exec_file, allargs, env, trace_me, attach_to_child,
2027 inf_validate_procinfo (inf);
2028 inf_update_signal_thread (inf);
2029 inf_set_traced (inf, inf->want_signals);
2031 /* Execing the process will have trashed our exception ports; steal them
2032 back (or make sure they're restored if the user wants that). */
2033 if (inf->want_exceptions)
2034 inf_steal_exc_ports (inf);
2036 inf_restore_exc_ports (inf);
2039 proceed ((CORE_ADDR) -1, 0, 0);
2042 /* Mark our target-struct as eligible for stray "run" and "attach"
2050 #ifdef ATTACH_DETACH
2052 /* Attach to process PID, then initialize for debugging it
2053 and wait for the trace-trap that results from attaching. */
2055 gnu_attach (args, from_tty)
2061 struct inf *inf = cur_inf ();
2064 error_no_arg ("PID to attach");
2068 if (pid == getpid ()) /* Trying to masturbate? */
2069 error ("I refuse to debug myself!");
2073 exec_file = (char *) get_exec_file (0);
2076 printf_unfiltered ("Attaching to program `%s', pid %d\n",
2079 printf_unfiltered ("Attaching to pid %d\n", pid);
2081 gdb_flush (gdb_stdout);
2084 inf_debug (inf, "attaching to pid: %d", pid);
2086 inf_attach (inf, pid);
2087 inf_update_procs (inf);
2089 inferior_pid = inf_pick_first_thread ();
2092 push_target (&gnu_ops);
2094 /* We have to initialize the terminal settings now, since the code
2095 below might try to restore them. */
2096 target_terminal_init ();
2098 /* If the process was stopped before we attached, make it continue the next
2099 time the user does a continue. */
2100 inf_validate_procinfo (inf);
2102 inf_update_signal_thread (inf);
2103 inf_set_traced (inf, inf->want_signals);
2105 #if 0 /* Do we need this? */
2106 renumber_threads (0); /* Give our threads reasonable names. */
2110 /* Take a program previously attached to and detaches it.
2111 The program resumes execution and will no longer stop
2112 on signals, etc. We'd better not have left any breakpoints
2113 in the program or it'll die when it hits one. For this
2114 to work, it may be necessary for the process to have been
2115 previously attached. It *might* work if the program was
2116 started via fork. */
2118 gnu_detach (args, from_tty)
2124 char *exec_file = get_exec_file (0);
2126 printf_unfiltered ("Detaching from program `%s' pid %d\n",
2127 exec_file, current_inferior->pid);
2129 printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid);
2130 gdb_flush (gdb_stdout);
2133 inf_detach (current_inferior);
2137 unpush_target (&gnu_ops); /* Pop out of handling an inferior */
2139 #endif /* ATTACH_DETACH */
2142 gnu_terminal_init_inferior ()
2144 assert (current_inferior);
2145 terminal_init_inferior_with_pgrp (current_inferior->pid);
2148 /* Get ready to modify the registers array. On machines which store
2149 individual registers, this doesn't need to do anything. On machines
2150 which store all the registers in one fell swoop, this makes sure
2151 that registers contains all the registers from the program being
2155 gnu_prepare_to_store ()
2157 #ifdef CHILD_PREPARE_TO_STORE
2158 CHILD_PREPARE_TO_STORE ();
2163 gnu_open (arg, from_tty)
2167 error ("Use the \"run\" command to start a Unix child process.");
2173 error ("to_stop target function not implemented");
2177 gnu_pid_to_exec_file ()
2179 error ("to_pid_to_exec_file target function not implemented");
2185 gnu_thread_alive (int tid)
2187 inf_update_procs (current_inferior);
2188 return !!inf_tid_to_thread (current_inferior, tid);
2192 * Read inferior task's LEN bytes from ADDR and copy it to MYADDR
2193 * in gdb's address space.
2195 * Return 0 on failure; number of bytes read otherwise.
2198 gnu_read_inferior (task, addr, myaddr, length)
2205 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2206 vm_size_t aligned_length =
2207 (vm_size_t) round_page (addr + length) - low_address;
2211 /* Get memory from inferior with page aligned addresses */
2212 err = vm_read (task, low_address, aligned_length, &copied, ©_count);
2216 err = hurd_safe_copyin (myaddr, (void *) addr - low_address + copied, length);
2219 warning ("Read from inferior faulted: %s", strerror (err));
2223 err = vm_deallocate (mach_task_self (), copied, copy_count);
2225 warning ("gnu_read_inferior vm_deallocate failed: %s", strerror (err));
2230 #define CHK_GOTO_OUT(str,ret) \
2231 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2233 struct vm_region_list
2235 struct vm_region_list *next;
2236 vm_prot_t protection;
2241 struct obstack region_obstack;
2244 * Write gdb's LEN bytes from MYADDR and copy it to ADDR
2245 * in inferior task's address space.
2248 gnu_write_inferior (task, addr, myaddr, length)
2255 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2256 vm_size_t aligned_length =
2257 (vm_size_t) round_page (addr + length) - low_address;
2262 char *errstr = "Bug in gnu_write_inferior";
2264 struct vm_region_list *region_element;
2265 struct vm_region_list *region_head = (struct vm_region_list *) NULL;
2267 /* Get memory from inferior with page aligned addresses */
2268 err = vm_read (task,
2273 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2277 err = hurd_safe_copyout ((void *) addr - low_address + copied, myaddr, length);
2278 CHK_GOTO_OUT ("Write to inferior faulted", err);
2280 obstack_init (®ion_obstack);
2282 /* Do writes atomically.
2283 * First check for holes and unwritable memory.
2286 vm_size_t remaining_length = aligned_length;
2287 vm_address_t region_address = low_address;
2289 struct vm_region_list *scan;
2291 while (region_address < low_address + aligned_length)
2293 vm_prot_t protection;
2294 vm_prot_t max_protection;
2295 vm_inherit_t inheritance;
2297 mach_port_t object_name;
2299 vm_size_t region_length = remaining_length;
2300 vm_address_t old_address = region_address;
2302 err = vm_region (task,
2311 CHK_GOTO_OUT ("vm_region failed", err);
2313 /* Check for holes in memory */
2314 if (old_address != region_address)
2316 warning ("No memory at 0x%x. Nothing written",
2323 if (!(max_protection & VM_PROT_WRITE))
2325 warning ("Memory at address 0x%x is unwritable. Nothing written",
2332 /* Chain the regions for later use */
2334 (struct vm_region_list *)
2335 obstack_alloc (®ion_obstack, sizeof (struct vm_region_list));
2337 region_element->protection = protection;
2338 region_element->start = region_address;
2339 region_element->length = region_length;
2341 /* Chain the regions along with protections */
2342 region_element->next = region_head;
2343 region_head = region_element;
2345 region_address += region_length;
2346 remaining_length = remaining_length - region_length;
2349 /* If things fail after this, we give up.
2350 * Somebody is messing up inferior_task's mappings.
2353 /* Enable writes to the chained vm regions */
2354 for (scan = region_head; scan; scan = scan->next)
2356 boolean_t protection_changed = FALSE;
2358 if (!(scan->protection & VM_PROT_WRITE))
2360 err = vm_protect (task,
2364 scan->protection | VM_PROT_WRITE);
2365 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2369 err = vm_write (task,
2373 CHK_GOTO_OUT ("vm_write failed", err);
2375 /* Set up the original region protections, if they were changed */
2376 for (scan = region_head; scan; scan = scan->next)
2378 boolean_t protection_changed = FALSE;
2380 if (!(scan->protection & VM_PROT_WRITE))
2382 err = vm_protect (task,
2387 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2395 obstack_free (®ion_obstack, 0);
2397 (void) vm_deallocate (mach_task_self (),
2402 if (err != KERN_SUCCESS)
2404 warning ("%s: %s", errstr, mach_error_string (err));
2411 /* Return 0 on failure, number of bytes handled otherwise. */
2413 gnu_xfer_memory (memaddr, myaddr, len, write, target)
2418 struct target_ops *target; /* IGNORED */
2423 ? (current_inferior->task ? current_inferior->task->port : 0)
2426 if (task == MACH_PORT_NULL)
2430 inf_debug (current_inferior, "%s %p[%d] %s %p",
2431 write ? "writing" : "reading", memaddr, len,
2432 write ? "<--" : "-->", myaddr);
2434 return gnu_write_inferior (task, memaddr, myaddr, len);
2436 return gnu_read_inferior (task, memaddr, myaddr, len);
2440 /* Return printable description of proc. */
2442 proc_string (struct proc *proc)
2444 static char tid_str[80];
2445 if (proc_is_task (proc))
2446 sprintf (tid_str, "process %d", proc->inf->pid);
2448 sprintf (tid_str, "thread %d.%d",
2449 proc->inf->pid, pid_to_thread_id (proc->tid));
2454 gnu_pid_to_str (int tid)
2456 struct inf *inf = current_inferior;
2457 struct proc *thread = inf_tid_to_thread (inf, tid);
2460 return proc_string (thread);
2463 static char tid_str[80];
2464 sprintf (tid_str, "bogus thread id %d", tid);
2469 extern void gnu_store_registers (int regno);
2470 extern void gnu_fetch_registers (int regno);
2472 struct target_ops gnu_ops;
2477 gnu_ops.to_shortname = "GNU"; /* to_shortname */
2478 gnu_ops.to_longname = "GNU Hurd process"; /* to_longname */
2479 gnu_ops.to_doc = "GNU Hurd process"; /* to_doc */
2480 gnu_ops.to_open = gnu_open; /* to_open */
2481 gnu_ops.to_close = 0; /* to_close */
2482 gnu_ops.to_attach = gnu_attach; /* to_attach */
2483 gnu_ops.to_post_attach = NULL;
2484 gnu_ops.to_require_attach = NULL; /* to_require_attach */
2485 gnu_ops.to_detach = gnu_detach; /* to_detach */
2486 gnu_ops.to_require_detach = NULL; /* to_require_detach */
2487 gnu_ops.to_resume = gnu_resume; /* to_resume */
2488 gnu_ops.to_wait = gnu_wait; /* to_wait */
2489 gnu_ops.to_post_wait = NULL; /* to_post_wait */
2490 gnu_ops.to_fetch_registers = gnu_fetch_registers; /* to_fetch_registers */
2491 gnu_ops.to_store_registers = gnu_store_registers; /* to_store_registers */
2492 gnu_ops.to_prepare_to_store = gnu_prepare_to_store; /* to_prepare_to_store */
2493 gnu_ops.to_xfer_memory = gnu_xfer_memory; /* to_xfer_memory */
2494 gnu_ops.to_files_info = 0; /* to_files_info */
2495 gnu_ops.to_insert_breakpoint = memory_insert_breakpoint;
2496 gnu_ops.to_remove_breakpoint = memory_remove_breakpoint;
2497 gnu_ops.to_terminal_init = gnu_terminal_init_inferior;
2498 gnu_ops.to_terminal_inferior = terminal_inferior;
2499 gnu_ops.to_terminal_ours_for_output = terminal_ours_for_output;
2500 gnu_ops.to_terminal_ours = terminal_ours;
2501 gnu_ops.to_terminal_info = child_terminal_info;
2502 gnu_ops.to_kill = gnu_kill_inferior; /* to_kill */
2503 gnu_ops.to_load = 0; /* to_load */
2504 gnu_ops.to_lookup_symbol = 0; /* to_lookup_symbol */
2505 gnu_ops.to_create_inferior = gnu_create_inferior; /* to_create_inferior */
2506 gnu_ops.to_post_startup_inferior = NULL; /* to_post_startup_inferior */
2507 gnu_ops.to_acknowledge_created_inferior = NULL; /* to_acknowledge_created_inferior */
2508 gnu_ops.to_clone_and_follow_inferior = NULL; /* to_clone_and_follow_inferior */
2509 gnu_ops.to_post_follow_inferior_by_clone = NULL; /* to_post_follow_inferior_by_clone */
2510 gnu_ops.to_insert_fork_catchpoint = NULL;
2511 gnu_ops.to_remove_fork_catchpoint = NULL;
2512 gnu_ops.to_insert_vfork_catchpoint = NULL;
2513 gnu_ops.to_remove_vfork_catchpoint = NULL;
2514 gnu_ops.to_has_forked = NULL; /* to_has_forked */
2515 gnu_ops.to_has_vforked = NULL; /* to_has_vforked */
2516 gnu_ops.to_can_follow_vfork_prior_to_exec = NULL;
2517 gnu_ops.to_post_follow_vfork = NULL; /* to_post_follow_vfork */
2518 gnu_ops.to_insert_exec_catchpoint = NULL;
2519 gnu_ops.to_remove_exec_catchpoint = NULL;
2520 gnu_ops.to_has_execd = NULL;
2521 gnu_ops.to_reported_exec_events_per_exec_call = NULL;
2522 gnu_ops.to_has_exited = NULL;
2523 gnu_ops.to_mourn_inferior = gnu_mourn_inferior; /* to_mourn_inferior */
2524 gnu_ops.to_can_run = gnu_can_run; /* to_can_run */
2525 gnu_ops.to_notice_signals = 0; /* to_notice_signals */
2526 gnu_ops.to_thread_alive = gnu_thread_alive; /* to_thread_alive */
2527 gnu_ops.to_pid_to_str = gnu_pid_to_str; /* to_pid_to_str */
2528 gnu_ops.to_stop = gnu_stop; /* to_stop */
2529 gnu_ops.to_pid_to_exec_file = gnu_pid_to_exec_file; /* to_pid_to_exec_file */
2530 gnu_ops.to_core_file_to_sym_file = NULL;
2531 gnu_ops.to_stratum = process_stratum; /* to_stratum */
2532 gnu_ops.DONT_USE = 0; /* to_next */
2533 gnu_ops.to_has_all_memory = 1; /* to_has_all_memory */
2534 gnu_ops.to_has_memory = 1; /* to_has_memory */
2535 gnu_ops.to_has_stack = 1; /* to_has_stack */
2536 gnu_ops.to_has_registers = 1; /* to_has_registers */
2537 gnu_ops.to_has_execution = 1; /* to_has_execution */
2538 gnu_ops.to_sections = 0; /* sections */
2539 gnu_ops.to_sections_end = 0; /* sections_end */
2540 gnu_ops.to_magic = OPS_MAGIC; /* to_magic */
2541 } /* init_gnu_ops */
2543 /* User task commands. */
2545 struct cmd_list_element *set_task_cmd_list = 0;
2546 struct cmd_list_element *show_task_cmd_list = 0;
2547 /* User thread commands. */
2549 /* Commands with a prefix of `set/show thread'. */
2550 extern struct cmd_list_element *thread_cmd_list;
2551 struct cmd_list_element *set_thread_cmd_list = NULL;
2552 struct cmd_list_element *show_thread_cmd_list = NULL;
2554 /* Commands with a prefix of `set/show thread default'. */
2555 struct cmd_list_element *set_thread_default_cmd_list = NULL;
2556 struct cmd_list_element *show_thread_default_cmd_list = NULL;
2559 set_thread_cmd (char *args, int from_tty)
2561 printf_unfiltered ("\"set thread\" must be followed by the name of a thread property, or \"default\".\n");
2565 show_thread_cmd (char *args, int from_tty)
2567 printf_unfiltered ("\"show thread\" must be followed by the name of a thread property, or \"default\".\n");
2571 set_thread_default_cmd (char *args, int from_tty)
2573 printf_unfiltered ("\"set thread default\" must be followed by the name of a thread property.\n");
2577 show_thread_default_cmd (char *args, int from_tty)
2579 printf_unfiltered ("\"show thread default\" must be followed by the name of a thread property.\n");
2583 parse_int_arg (char *args, char *cmd_prefix)
2588 int val = strtoul (args, &arg_end, 10);
2589 if (*args && *arg_end == '\0')
2592 error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix);
2596 _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
2598 if (!args || strcmp (args, t_val) == 0)
2600 else if (strcmp (args, f_val) == 0)
2603 error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2604 cmd_prefix, t_val, f_val);
2607 #define parse_bool_arg(args, cmd_prefix) \
2608 _parse_bool_arg (args, "on", "off", cmd_prefix)
2611 check_empty (char *args, char *cmd_prefix)
2614 error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args);
2617 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2618 static struct proc *
2621 struct inf *inf = cur_inf ();
2622 struct proc *thread = inf_tid_to_thread (inf, inferior_pid);
2624 error ("No current thread.");
2628 /* Returns the current inferior, but signals an error if it has no task. */
2632 struct inf *inf = cur_inf ();
2634 error ("No current process.");
2639 set_task_pause_cmd (char *args, int from_tty)
2641 struct inf *inf = cur_inf ();
2642 int old_sc = inf->pause_sc;
2644 inf->pause_sc = parse_bool_arg (args, "set task pause");
2646 if (old_sc == 0 && inf->pause_sc != 0)
2647 /* If the task is currently unsuspended, immediately suspend it,
2648 otherwise wait until the next time it gets control. */
2653 show_task_pause_cmd (char *args, int from_tty)
2655 struct inf *inf = cur_inf ();
2656 check_empty (args, "show task pause");
2657 printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2659 ? (inf->pause_sc == 0 ? "isn't" : "is")
2660 : (inf->pause_sc == 0 ? "won't be" : "will be"));
2664 set_task_detach_sc_cmd (char *args, int from_tty)
2666 cur_inf ()->detach_sc = parse_int_arg (args, "set task detach-suspend-count");
2670 show_task_detach_sc_cmd (char *args, int from_tty)
2672 check_empty (args, "show task detach-suspend-count");
2673 printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n",
2674 cur_inf ()->detach_sc);
2678 set_thread_default_pause_cmd (char *args, int from_tty)
2680 struct inf *inf = cur_inf ();
2681 inf->default_thread_pause_sc =
2682 parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2686 show_thread_default_pause_cmd (char *args, int from_tty)
2688 struct inf *inf = cur_inf ();
2689 int sc = inf->default_thread_pause_sc;
2690 check_empty (args, "show thread default pause");
2691 printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2692 sc ? "are" : "aren't",
2693 !sc && inf->pause_sc ? " (but the task is)" : "");
2697 set_thread_default_run_cmd (char *args, int from_tty)
2699 struct inf *inf = cur_inf ();
2700 inf->default_thread_run_sc =
2701 parse_bool_arg (args, "set thread default run") ? 0 : 1;
2705 show_thread_default_run_cmd (char *args, int from_tty)
2707 struct inf *inf = cur_inf ();
2708 check_empty (args, "show thread default run");
2709 printf_unfiltered ("New threads %s allowed to run.\n",
2710 inf->default_thread_run_sc == 0 ? "are" : "aren't");
2714 set_thread_default_detach_sc_cmd (char *args, int from_tty)
2716 cur_inf ()->default_thread_detach_sc =
2717 parse_int_arg (args, "set thread default detach-suspend-count");
2721 show_thread_default_detach_sc_cmd (char *args, int from_tty)
2723 check_empty (args, "show thread default detach-suspend-count");
2724 printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
2725 cur_inf ()->default_thread_detach_sc);
2728 /* Steal a send right called NAME in the inferior task, and make it PROC's
2729 saved exception port. */
2731 steal_exc_port (struct proc *proc, mach_port_t name)
2735 mach_msg_type_name_t port_type;
2737 if (!proc || !proc->inf->task)
2738 error ("No inferior task.");
2740 err = mach_port_extract_right (proc->inf->task->port,
2741 name, MACH_MSG_TYPE_COPY_SEND,
2744 error ("Couldn't extract send right %d from inferior: %s",
2745 name, strerror (err));
2747 if (proc->saved_exc_port)
2748 /* Get rid of our reference to the old one. */
2749 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2751 proc->saved_exc_port = port;
2753 if (!proc->exc_port)
2754 /* If PROC is a thread, we may not have set its exception port before.
2755 We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2757 proc->exc_port = proc->inf->event_port;
2758 err = proc_set_exception_port (proc, proc->exc_port);
2759 error ("Can't set exception port for %s: %s",
2760 proc_string (proc), strerror (err));
2765 set_task_exc_port_cmd (char *args, int from_tty)
2767 struct inf *inf = cur_inf ();
2769 error ("No argument to \"set task exception-port\" command.");
2770 steal_exc_port (inf->task, parse_and_eval_address (args));
2774 set_stopped_cmd (char *args, int from_tty)
2776 cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2780 show_stopped_cmd (char *args, int from_tty)
2782 struct inf *inf = active_inf ();
2783 check_empty (args, "show stopped");
2784 printf_unfiltered ("The inferior process %s stopped.\n",
2785 inf->stopped ? "is" : "isn't");
2789 set_sig_thread_cmd (char *args, int from_tty)
2792 struct inf *inf = cur_inf ();
2794 if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
2795 error ("Illegal argument to \"set signal-thread\" command.\n"
2796 "Should be an integer thread ID, or `none'.");
2798 if (strcmp (args, "none") == 0)
2799 inf->signal_thread = 0;
2802 int tid = thread_id_to_pid (atoi (args));
2804 error ("Thread ID %s not known. Use the \"info threads\" command to\n"
2805 "see the IDs of currently known threads.", args);
2806 inf->signal_thread = inf_tid_to_thread (inf, tid);
2811 show_sig_thread_cmd (char *args, int from_tty)
2813 struct inf *inf = active_inf ();
2814 check_empty (args, "show signal-thread");
2815 if (inf->signal_thread)
2816 printf_unfiltered ("The signal thread is %s.\n",
2817 proc_string (inf->signal_thread));
2819 printf_unfiltered ("There is no signal thread.\n");
2823 set_signals_cmd (char *args, int from_tty)
2826 struct inf *inf = cur_inf ();
2828 inf->want_signals = parse_bool_arg (args, "set signals");
2830 if (inf->task && inf->want_signals != inf->traced)
2831 /* Make this take effect immediately in a running process. */
2832 inf_set_traced (inf, inf->want_signals);
2836 show_signals_cmd (char *args, int from_tty)
2838 struct inf *inf = cur_inf ();
2839 check_empty (args, "show signals");
2840 printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2842 ? (inf->traced ? "are" : "aren't")
2843 : (inf->want_signals ? "will be" : "won't be"));
2847 set_exceptions_cmd (char *args, int from_tty)
2849 struct inf *inf = cur_inf ();
2850 int val = parse_bool_arg (args, "set exceptions");
2852 if (inf->task && inf->want_exceptions != val)
2853 /* Make this take effect immediately in a running process. */
2856 inf->want_exceptions = val;
2860 show_exceptions_cmd (char *args, int from_tty)
2862 struct inf *inf = cur_inf ();
2863 check_empty (args, "show exceptions");
2864 printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2866 ? (inf->want_exceptions ? "are" : "aren't")
2867 : (inf->want_exceptions ? "will be" : "won't be"));
2871 set_task_cmd (char *args, int from_tty)
2873 printf_unfiltered ("\"set task\" must be followed by the name of a task property.\n");
2877 show_task_cmd (char *args, int from_tty)
2879 struct inf *inf = cur_inf ();
2881 check_empty (args, "show task");
2883 show_signals_cmd (0, from_tty);
2884 show_exceptions_cmd (0, from_tty);
2885 show_task_pause_cmd (0, from_tty);
2887 if (inf->pause_sc == 0)
2888 show_thread_default_pause_cmd (0, from_tty);
2889 show_thread_default_run_cmd (0, from_tty);
2893 show_stopped_cmd (0, from_tty);
2894 show_sig_thread_cmd (0, from_tty);
2897 if (inf->detach_sc != 0)
2898 show_task_detach_sc_cmd (0, from_tty);
2899 if (inf->default_thread_detach_sc != 0)
2900 show_thread_default_detach_sc_cmd (0, from_tty);
2904 set_noninvasive_cmd (char *args, int from_tty)
2906 /* Invert the sense of the arg for each component. */
2907 char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on";
2909 set_task_pause_cmd (inv_args, from_tty);
2910 set_signals_cmd (inv_args, from_tty);
2911 set_exceptions_cmd (inv_args, from_tty);
2915 info_port_rights (char *args, mach_port_type_t only)
2917 struct inf *inf = active_inf ();
2918 value_ptr vmark = value_mark ();
2921 /* Explicit list of port rights. */
2925 value_ptr val = parse_to_comma_and_eval (&args);
2926 long right = value_as_long (val);
2928 print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
2931 error ("%ld: %s.", right, strerror (err));
2935 /* Print all of them. */
2938 print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
2941 error ("%s.", strerror (err));
2944 value_free_to_mark (vmark);
2948 info_send_rights_cmd (char *args, int from_tty)
2950 info_port_rights (args, MACH_PORT_TYPE_SEND);
2953 info_recv_rights_cmd (char *args, int from_tty)
2955 info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
2958 info_port_sets_cmd (char *args, int from_tty)
2960 info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
2963 info_dead_names_cmd (char *args, int from_tty)
2965 info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
2968 info_port_rights_cmd (char *args, int from_tty)
2970 info_port_rights (args, ~0);
2974 add_task_commands ()
2976 add_cmd ("pause", class_run, set_thread_default_pause_cmd,
2977 "Set whether the new threads are suspended while gdb has control.\n"
2978 "This property normally has no effect because the whole task is\n"
2979 "suspended, however, that may be disabled with \"set task pause off\".\n"
2980 "The default value is \"off\".",
2981 &set_thread_default_cmd_list);
2982 add_cmd ("pause", no_class, show_thread_default_pause_cmd,
2983 "Show whether new threads are suspended while gdb has control.",
2984 &show_thread_default_cmd_list);
2985 add_cmd ("run", class_run, set_thread_default_run_cmd,
2986 "Set whether new threads are allowed to run (once gdb has noticed them).",
2987 &set_thread_default_cmd_list);
2988 add_cmd ("run", no_class, show_thread_default_run_cmd,
2989 "Show whether new threads are allowed to run (once gdb has noticed them).",
2990 &show_thread_default_cmd_list);
2991 add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
2992 "Set the default detach-suspend-count value for new threads.",
2993 &set_thread_default_cmd_list);
2994 add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
2995 "Show the default detach-suspend-count value for new threads.",
2996 &show_thread_default_cmd_list);
2998 add_cmd ("signals", class_run, set_signals_cmd,
2999 "Set whether the inferior process's signals will be intercepted.\n"
3000 "Mach exceptions (such as breakpoint traps) are not affected.",
3002 add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
3003 add_cmd ("signals", no_class, show_signals_cmd,
3004 "Show whether the inferior process's signals will be intercepted.",
3006 add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
3008 add_cmd ("signal-thread", class_run, set_sig_thread_cmd,
3009 "Set the thread that gdb thinks is the libc signal thread.\n"
3010 "This thread is run when delivering a signal to a non-stopped process.",
3012 add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
3013 add_cmd ("signal-thread", no_class, show_sig_thread_cmd,
3014 "Set the thread that gdb thinks is the libc signal thread.",
3016 add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
3018 add_cmd ("stopped", class_run, set_stopped_cmd,
3019 "Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n"
3020 "Stopped process will be continued by sending them a signal.",
3022 add_cmd ("stopped", no_class, show_signals_cmd,
3023 "Show whether gdb thinks the inferior process is stopped as with SIGSTOP.",
3026 add_cmd ("exceptions", class_run, set_exceptions_cmd,
3027 "Set whether exceptions in the inferior process will be trapped.\n"
3028 "When exceptions are turned off, neither breakpoints nor single-stepping\n"
3031 /* Allow `set exc' despite conflict with `set exception-port'. */
3032 add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
3033 add_cmd ("exceptions", no_class, show_exceptions_cmd,
3034 "Show whether exceptions in the inferior process will be trapped.",
3037 add_prefix_cmd ("task", no_class, set_task_cmd,
3038 "Command prefix for setting task attributes.",
3039 &set_task_cmd_list, "set task ", 0, &setlist);
3040 add_prefix_cmd ("task", no_class, show_task_cmd,
3041 "Command prefix for showing task attributes.",
3042 &show_task_cmd_list, "show task ", 0, &showlist);
3044 add_cmd ("pause", class_run, set_task_pause_cmd,
3045 "Set whether the task is suspended while gdb has control.\n"
3046 "A value of \"on\" takes effect immediately, otherwise nothing\n"
3047 "happens until the next time the program is continued.\n"
3048 "When setting this to \"off\", \"set thread default pause on\"\n"
3049 "can be used to pause individual threads by default instead.",
3050 &set_task_cmd_list);
3051 add_cmd ("pause", no_class, show_task_pause_cmd,
3052 "Show whether the task is suspended while gdb has control.",
3053 &show_task_cmd_list);
3054 add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
3055 "Set the suspend count will leave on the thread when detaching.",
3056 &set_task_cmd_list);
3057 add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
3058 "Show the suspend count will leave on the thread when detaching.",
3059 &show_task_cmd_list);
3061 add_cmd ("exception-port", no_class, set_task_exc_port_cmd,
3062 "Set the task exception port to which we forward exceptions.\n"
3063 "The argument should be the value of the send right in the task.",
3064 &set_task_cmd_list);
3065 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
3066 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_task_cmd_list);
3068 /* A convenient way of turning on all options require to noninvasively
3069 debug running tasks. */
3070 add_cmd ("noninvasive", no_class, set_noninvasive_cmd,
3071 "Set task options so that we interfere as little as possible.\n"
3072 "This is the same as setting `task pause', `exceptions', and"
3073 "`signals' to the opposite value.",
3076 /* Commands to show information about the task's ports. */
3077 add_cmd ("send-rights", class_info, info_send_rights_cmd,
3078 "Show information about the task's send rights",
3080 add_cmd ("receive-rights", class_info, info_recv_rights_cmd,
3081 "Show information about the task's receive rights",
3083 add_cmd ("port-rights", class_info, info_send_rights_cmd,
3084 "Show information about the task's port rights",
3086 add_cmd ("port-sets", class_info, info_port_sets_cmd,
3087 "Show information about the task's port sets",
3089 add_cmd ("dead-names", class_info, info_dead_names_cmd,
3090 "Show information about the task's dead names",
3092 add_info_alias ("ports", "port-rights", 1);
3093 add_info_alias ("port", "port-rights", 1);
3094 add_info_alias ("psets", "port-sets", 1);
3099 set_thread_pause_cmd (char *args, int from_tty)
3101 struct proc *thread = cur_thread ();
3102 int old_sc = thread->pause_sc;
3103 thread->pause_sc = parse_bool_arg (args, "set thread pause");
3104 if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
3105 /* If the task is currently unsuspended, immediately suspend it,
3106 otherwise wait until the next time it gets control. */
3107 inf_suspend (thread->inf);
3111 show_thread_pause_cmd (char *args, int from_tty)
3113 struct proc *thread = cur_thread ();
3114 int sc = thread->pause_sc;
3115 check_empty (args, "show task pause");
3116 printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
3117 proc_string (thread),
3118 sc ? "is" : "isn't",
3119 !sc && thread->inf->pause_sc ? " (but the task is)" : "");
3123 set_thread_run_cmd (char *args, int from_tty)
3125 struct proc *thread = cur_thread ();
3126 thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
3130 show_thread_run_cmd (char *args, int from_tty)
3132 struct proc *thread = cur_thread ();
3133 check_empty (args, "show thread run");
3134 printf_unfiltered ("Thread %s %s allowed to run.",
3135 proc_string (thread),
3136 thread->run_sc == 0 ? "is" : "isn't");
3140 set_thread_detach_sc_cmd (char *args, int from_tty)
3142 cur_thread ()->detach_sc = parse_int_arg (args, "set thread detach-suspend-count");
3146 show_thread_detach_sc_cmd (char *args, int from_tty)
3148 struct proc *thread = cur_thread ();
3149 check_empty (args, "show thread detach-suspend-count");
3150 printf_unfiltered ("Thread %s will be left with a suspend count of %d when detaching.\n",
3151 proc_string (thread),
3156 set_thread_exc_port_cmd (char *args, int from_tty)
3158 struct proc *thread = cur_thread ();
3160 error ("No argument to \"set thread exception-port\" command.");
3161 steal_exc_port (thread, parse_and_eval_address (args));
3166 show_thread_cmd (char *args, int from_tty)
3168 struct proc *thread = cur_thread ();
3169 check_empty (args, "show thread");
3170 show_thread_run_cmd (0, from_tty);
3171 show_thread_pause_cmd (0, from_tty);
3172 if (thread->detach_sc != 0)
3173 show_thread_detach_sc_cmd (0, from_tty);
3178 thread_takeover_sc_cmd (char *args, int from_tty)
3180 struct proc *thread = cur_thread ();
3181 thread_basic_info_data_t _info;
3182 thread_basic_info_t info = &_info;
3183 mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
3185 thread_info (thread->port, THREAD_BASIC_INFO, (int *) &info, &info_len);
3187 error ("%s.", strerror (err));
3188 thread->sc = info->suspend_count;
3190 printf_unfiltered ("Suspend count was %d.\n", thread->sc);
3192 vm_deallocate (mach_task_self (), (vm_address_t) info, info_len * sizeof (int));
3195 add_thread_commands ()
3197 add_prefix_cmd ("thread", no_class, set_thread_cmd,
3198 "Command prefix for setting thread properties.",
3199 &set_thread_cmd_list, "set thread ", 0, &setlist);
3200 add_prefix_cmd ("default", no_class, show_thread_cmd,
3201 "Command prefix for setting default thread properties.",
3202 &set_thread_default_cmd_list, "set thread default ", 0,
3203 &set_thread_cmd_list);
3204 add_prefix_cmd ("thread", no_class, set_thread_default_cmd,
3205 "Command prefix for showing thread properties.",
3206 &show_thread_cmd_list, "show thread ", 0, &showlist);
3207 add_prefix_cmd ("default", no_class, show_thread_default_cmd,
3208 "Command prefix for showing default thread properties.",
3209 &show_thread_default_cmd_list, "show thread default ", 0,
3210 &show_thread_cmd_list);
3212 add_cmd ("pause", class_run, set_thread_pause_cmd,
3213 "Set whether the current thread is suspended while gdb has control.\n"
3214 "A value of \"on\" takes effect immediately, otherwise nothing\n"
3215 "happens until the next time the program is continued. This\n"
3216 "property normally has no effect because the whole task is suspended,\n"
3217 "however, that may be disabled with \"set task pause off\".\n"
3218 "The default value is \"off\".",
3219 &set_thread_cmd_list);
3220 add_cmd ("pause", no_class, show_thread_pause_cmd,
3221 "Show whether the current thread is suspended while gdb has control.",
3222 &show_thread_cmd_list);
3224 add_cmd ("run", class_run, set_thread_run_cmd,
3225 "Set whether the current thread is allowed to run.",
3226 &set_thread_cmd_list);
3227 add_cmd ("run", no_class, show_thread_run_cmd,
3228 "Show whether the current thread is allowed to run.",
3229 &show_thread_cmd_list);
3231 add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd,
3232 "Set the suspend count will leave on the thread when detaching.\n"
3233 "Note that this is relative to suspend count when gdb noticed the thread;\n"
3234 "use the `thread takeover-suspend-count' to force it to an absolute value.",
3235 &set_thread_cmd_list);
3236 add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd,
3237 "Show the suspend count will leave on the thread when detaching."
3238 "Note that this is relative to suspend count when gdb noticed the thread;\n"
3239 "use the `thread takeover-suspend-count' to force it to an absolute value.",
3240 &show_thread_cmd_list);
3242 add_cmd ("exception-port", no_class, set_thread_exc_port_cmd,
3243 "Set the exception port to which we forward exceptions for the\n"
3244 "current thread, overriding the task exception port.\n"
3245 "The argument should be the value of the send right in the task.",
3246 &set_thread_cmd_list);
3247 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
3248 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_thread_cmd_list);
3250 add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd,
3251 "Force the threads absolute suspend-count to be gdb's.\n"
3252 "Prior to giving this command, gdb's thread suspend-counts are relative to\n"
3253 "the thread's initial suspend-count when gdb notices the threads.",
3258 _initialize_gnu_nat ()
3260 proc_server = getproc ();
3262 add_target (&gnu_ops);
3263 add_task_commands ();
3264 add_thread_commands ();
3266 add_set_cmd ("gnu-debug", class_maintenance,
3267 var_boolean, (char *) &gnu_debug_flag,
3268 "Set debugging output for the gnu backend.", &maintenancelist);
3271 #ifdef FLUSH_INFERIOR_CACHE
3273 /* When over-writing code on some machines the I-Cache must be flushed
3274 explicitly, because it is not kept coherent by the lazy hardware.
3275 This definitely includes breakpoints, for instance, or else we
3276 end up looping in mysterious Bpt traps */
3279 flush_inferior_icache (pc, amount)
3282 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
3285 ret = vm_machine_attribute (current_inferior->task->port,
3290 if (ret != KERN_SUCCESS)
3291 warning ("Error flushing inferior's cache : %s", strerror (ret));
3293 #endif /* FLUSH_INFERIOR_CACHE */