1 /* Interface GDB to the GNU Hurd
2 Copyright (C) 1992, 1995, 1996 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, Boston, MA 02111-1307, USA.
31 #include <sys/ptrace.h>
33 /* We include this because we don't need the access macros and they conflict
34 with gdb's definitions (ick). This is very non standard! */
35 #include <waitflags.h>
38 #include <mach/message.h>
39 #include <mach/notify.h>
40 #include <mach_error.h>
41 #include <mach/exception.h>
42 #include <mach/vm_attributes.h>
44 #include <hurd/process.h>
45 #include <hurd/process_request.h>
47 #include <hurd/msg_request.h>
48 #include <hurd/signal.h>
49 #include <hurd/interrupt.h>
50 #include <hurd/sigpreempt.h>
66 #include "exc_request_S.h"
68 #include "process_reply_S.h"
69 #include "msg_reply_S.h"
71 #include "exc_request_U.h"
74 static process_t proc_server = MACH_PORT_NULL;
76 /* If we've sent a proc_wait_request to the proc server, the pid of the
77 process we asked about. We can only ever have one outstanding. */
78 int proc_wait_pid = 0;
80 /* The number of wait requests we've sent, and expect replies from. */
81 int proc_waits_pending = 0;
83 int gnu_debug_flag = 0;
87 extern struct target_ops gnu_ops;
89 struct inf *make_inf ();
90 void inf_clear_wait (struct inf *inf);
91 void inf_cleanup (struct inf *inf);
92 void inf_startup (struct inf *inf, int pid, task_t task);
93 int inf_update_suspends (struct inf *inf);
94 void inf_set_task (struct inf *inf, task_t port);
95 void inf_validate_procs (struct inf *inf);
96 void inf_steal_exc_ports (struct inf *inf);
97 void inf_restore_exc_ports (struct inf *inf);
98 int inf_update_procs (struct inf *inf);
99 struct proc *inf_tid_to_proc (struct inf *inf, int tid);
100 inline void inf_set_threads_resume_sc (struct inf *inf, struct proc
101 *run_thread, int run_others);
102 inline int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
103 inline void inf_suspend (struct inf *inf);
104 inline void inf_resume (struct inf *inf);
105 void inf_set_step_thread (struct inf *inf, struct proc *proc);
106 void inf_detach (struct inf *inf);
107 void inf_attach (struct inf *inf, int pid);
108 void inf_signal (struct inf *inf, enum target_signal sig);
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 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
115 struct proc *_proc_free (struct proc *proc);
116 int proc_update_sc (struct proc *proc);
117 void proc_abort (struct proc *proc, int force);
118 thread_state_t proc_get_state (struct proc *proc, int force);
119 error_t proc_get_exception_port (struct proc *proc, mach_port_t *port);
120 error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
121 static mach_port_t _proc_get_exc_port (struct proc *proc);
122 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
123 void proc_restore_exc_port (struct proc *proc);
124 int proc_trace (struct proc *proc, int set);
125 char *proc_string (struct proc *proc);
127 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
128 to INF's msg port and task port respectively. If it has no msg port,
129 EIEIO is returned. INF must refer to a running process! */
130 #define INF_MSGPORT_RPC(inf, rpc_expr) \
131 HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
132 (refport = inf->task->port, 0), 0, \
133 msgport ? (rpc_expr) : EIEIO)
135 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
136 there's someone around to deal with the RPC (and resuspend things
137 afterwards). This effects INF's threads' resume_sc count. */
138 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
139 (inf_set_threads_resume_sc_for_signal_thread (inf) \
142 __e = INF_MSGPORT_RPC (inf, rpc_expr); \
147 #define MIG_SERVER_DIED EMIG_SERVER_DIED /* XXX */
149 /* The state passed by an exception message. */
152 int exception; /* The exception code */
154 mach_port_t handler; /* The real exception port to handle this. */
155 mach_port_t reply; /* The reply port from the exception call. */
158 /* The results of the last wait an inf did. */
161 struct target_waitstatus status; /* The status returned to gdb. */
162 struct exc_state exc; /* The exception that caused us to return. */
163 struct proc *thread; /* The thread in question. */
164 int suppress; /* Something trivial happened. */
167 /* The state of an inferior. */
170 /* Fields describing the current inferior. */
172 struct proc *task; /* The mach task. */
173 struct proc *threads; /* A linked list of all threads in TASK. */
175 /* True if THREADS needn't be validated by querying the task. We assume that
176 we and the task in question are the only ones frobbing the thread list,
177 so as long as we don't let any code run, we don't have to worry about
179 int threads_up_to_date;
181 pid_t pid; /* The real system PID. */
183 struct inf_wait wait; /* What to return from target_wait. */
185 /* One thread proc in INF may be in `single-stepping mode'. This is it. */
186 struct proc *step_thread;
188 /* The thread we think is the signal thread. */
189 struct proc *signal_thread;
191 mach_port_t event_port; /* Where we receive various msgs. */
193 /* True if we think at least one thread in the inferior could currently be
197 /* True if the process has stopped (in the proc server sense). Note that
198 since a proc server `stop' leaves the signal thread running, the inf can
199 be RUNNING && STOPPED... */
202 /* True if the inferior is traced. */
205 /* True if we shouldn't try waiting for the inferior, usually because we
206 can't for some reason. */
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. */
241 int __proc_pid (struct proc *proc)
243 return proc->inf->pid;
246 /* Update PROC's real suspend count to match it's desired one. Returns true
247 if we think PROC is now in a runnable state. */
249 proc_update_sc (struct proc *proc)
253 int delta = proc->sc - proc->cur_sc;
256 proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
258 if (proc->sc == 0 && proc->state_changed)
259 /* Since PROC may start running, we must write back any state changes. */
261 assert (proc_is_thread (proc));
262 proc_debug (proc, "storing back changed thread state");
263 err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
264 (thread_state_t)&proc->state, THREAD_STATE_SIZE);
266 proc->state_changed = 0;
270 while (delta-- > 0 && !err)
271 if (proc_is_task (proc))
272 err = task_suspend (proc->port);
274 err = thread_suspend (proc->port);
276 while (delta++ < 0 && !err)
277 if (proc_is_task (proc))
278 err = task_resume (proc->port);
280 err = thread_resume (proc->port);
283 proc->cur_sc = proc->sc;
285 /* If we got an error, then the task/thread has disappeared. */
286 running = !err && proc->sc == 0;
288 proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
290 proc_debug (proc, "err = %s", strerror (err));
295 proc->state_valid = proc->state_changed = 0;
296 proc->fetched_regs = 0;
302 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
303 If PROC is deemed `precious', then nothing is done unless FORCE is true.
304 In particular, a thread is precious if it's running (in which case forcing
305 it includes suspending it first), or if it has an exception pending. */
307 proc_abort (struct proc *proc, int force)
309 assert (proc_is_thread (proc));
313 struct inf *inf = proc->inf;
314 int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
316 if (running && force)
319 inf_update_suspends (proc->inf);
321 warning ("Stopped %s.", proc_string (proc));
323 else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
324 /* An exception is pending on PROC, which don't mess with. */
328 /* We only abort the thread if it's not actually running. */
330 thread_abort (proc->port);
331 proc_debug (proc, "aborted");
335 proc_debug (proc, "not aborting");
339 /* Make sure that the state field in PROC is up to date, and return a pointer
340 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
341 that the thread is stopped and aborted first, and sets the state_changed
342 field in PROC to true. */
344 proc_get_state (struct proc *proc, int will_modify)
346 int was_aborted = proc->aborted;
348 proc_debug (proc, "updating state info%s",
349 will_modify ? " (with intention to modify)" : "");
351 proc_abort (proc, will_modify);
353 if (! was_aborted && proc->aborted)
354 /* PROC's state may have changed since we last fetched it. */
355 proc->state_valid = 0;
357 if (! proc->state_valid)
359 mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
361 thread_get_state (proc->port, THREAD_STATE_FLAVOR,
362 (thread_state_t)&proc->state, &state_size);
363 proc_debug (proc, "getting thread state");
364 proc->state_valid = !err;
367 if (proc->state_valid)
370 proc->state_changed = 1;
371 return (thread_state_t)&proc->state;
378 proc_get_exception_port (struct proc *proc, mach_port_t *port)
380 if (proc_is_task (proc))
381 return task_get_exception_port (proc->port, port);
383 return thread_get_exception_port (proc->port, port);
387 proc_set_exception_port (struct proc *proc, mach_port_t port)
389 proc_debug (proc, "setting exception port: %d", port);
390 if (proc_is_task (proc))
391 return task_set_exception_port (proc->port, port);
393 return thread_set_exception_port (proc->port, port);
396 /* Get PROC's exception port, cleaning up a bit if proc has died. */
398 _proc_get_exc_port (struct proc *proc)
400 mach_port_t exc_port;
401 error_t err = proc_get_exception_port (proc, &exc_port);
404 /* PROC must be dead. */
407 mach_port_deallocate (mach_task_self (), proc->exc_port);
408 proc->exc_port = MACH_PORT_NULL;
409 if (proc->saved_exc_port)
410 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
411 proc->saved_exc_port = MACH_PORT_NULL;
417 /* Replace PROC's exception port with EXC_PORT, unless it's already been
418 done. Stash away any existing exception port so we can restore it later. */
420 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
422 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
428 proc_debug (proc, "inserting exception port: %d", exc_port);
430 if (cur_exc_port != exc_port)
431 /* Put in our exception port. */
432 err = proc_set_exception_port (proc, exc_port);
434 if (err || cur_exc_port == proc->exc_port)
435 /* We previously set the exception port, and it's still set. So we
436 just keep the old saved port which is what the proc set. */
439 mach_port_deallocate (mach_task_self (), cur_exc_port);
442 /* Keep a copy of PROC's old exception port so it can be restored. */
444 if (proc->saved_exc_port)
445 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
446 proc->saved_exc_port = cur_exc_port;
449 proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
452 proc->exc_port = exc_port;
454 warning ("Error setting exception port for %s: %s",
455 proc_string (proc), strerror (err));
459 /* If we previously replaced PROC's exception port, put back what we found
460 there at the time, unless *our* exception port has since be overwritten,
461 in which case who knows what's going on. */
463 proc_restore_exc_port (struct proc *proc)
465 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
471 proc_debug (proc, "restoring real exception port");
473 if (proc->exc_port == cur_exc_port)
474 /* Our's is still there. */
475 err = proc_set_exception_port (proc, proc->saved_exc_port);
477 if (proc->saved_exc_port)
478 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
479 proc->saved_exc_port = MACH_PORT_NULL;
482 proc->exc_port = MACH_PORT_NULL;
484 warning ("Error setting exception port for %s: %s",
485 proc_string (proc), strerror (err));
489 /* Turns hardware tracing in PROC on or off when SET is true or fals,
490 respectively. Returns true on success. */
492 proc_trace (struct proc *proc, int set)
494 thread_state_t state = proc_get_state (proc, 1);
497 return 0; /* the thread must be dead. */
499 proc_debug (proc, "tracing %s", set ? "on" : "off");
503 /* XXX We don't get the exception unless the thread has its own
504 exception port???? */
505 if (proc->exc_port == MACH_PORT_NULL)
506 proc_steal_exc_port (proc, proc->inf->event_port);
507 THREAD_STATE_SET_TRACED (state);
510 THREAD_STATE_CLEAR_TRACED (state);
515 /* A variable from which to assign new TIDs. */
516 static int next_thread_id = 1;
518 /* Returns a new proc structure with the given fields. Also adds a
519 notification for PORT becoming dead to be sent to INF's notify port. */
521 make_proc (struct inf *inf, mach_port_t port, int tid)
524 mach_port_t prev_port = MACH_PORT_NULL;
525 struct proc *proc = malloc (sizeof (struct proc));
531 proc->saved_exc_port = MACH_PORT_NULL;
532 proc->exc_port = MACH_PORT_NULL;
537 /* Note that these are all the values for threads; the task simply uses the
538 corresponding field in INF directly. */
539 proc->run_sc = inf->default_thread_run_sc;
540 proc->pause_sc = inf->default_thread_pause_sc;
541 proc->detach_sc = inf->default_thread_detach_sc;
542 proc->resume_sc = proc->run_sc;
546 proc->state_valid = 0;
547 proc->state_changed = 0;
549 proc_debug (proc, "is new");
551 /* Get notified when things die. */
553 mach_port_request_notification (mach_task_self(), port,
554 MACH_NOTIFY_DEAD_NAME, 1,
556 MACH_MSG_TYPE_MAKE_SEND_ONCE,
559 warning ("Couldn't request notification for port %d: %s",
560 port, strerror (err));
563 proc_debug (proc, "notifications to: %d", inf->event_port);
564 if (prev_port != MACH_PORT_NULL)
565 mach_port_deallocate (mach_task_self (), prev_port);
568 if (inf->want_exceptions)
569 if (proc_is_task (proc))
570 /* Make the task exception port point to us. */
571 proc_steal_exc_port (proc, inf->event_port);
573 /* Just clear thread exception ports -- they default to the task one. */
574 proc_steal_exc_port (proc, MACH_PORT_NULL);
579 /* Frees PROC and any resources it uses, and returns the value of PROC's next
582 _proc_free (struct proc *proc)
584 struct inf *inf = proc->inf;
585 struct proc *next = proc->next;
587 proc_debug (proc, "freeing...");
589 if (proc == inf->step_thread)
590 /* Turn off single stepping. */
591 inf_set_step_thread (inf, 0);
592 if (proc == inf->wait.thread)
593 inf_clear_wait (inf);
594 if (proc == inf->signal_thread)
595 inf->signal_thread = 0;
597 if (proc->port != MACH_PORT_NULL)
599 if (proc->exc_port != MACH_PORT_NULL)
600 /* Restore the original exception port. */
601 proc_restore_exc_port (proc);
602 if (proc->cur_sc != 0)
603 /* Resume the thread/task. */
606 proc_update_sc (proc);
608 mach_port_deallocate (mach_task_self (), proc->port);
615 struct inf *make_inf ()
617 struct inf *inf = malloc (sizeof (struct inf));
624 inf->threads_up_to_date = 0;
626 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
627 inf->wait.thread = 0;
628 inf->wait.exc.handler = MACH_PORT_NULL;
629 inf->wait.exc.reply = MACH_PORT_NULL;
630 inf->step_thread = 0;
631 inf->signal_thread = 0;
632 inf->event_port = MACH_PORT_NULL;
637 inf->pending_execs = 0;
640 inf->default_thread_run_sc = 0;
641 inf->default_thread_pause_sc = 0;
642 inf->default_thread_detach_sc = 0;
643 inf->want_signals = 1; /* By default */
644 inf->want_exceptions = 1; /* By default */
650 inf_clear_wait (struct inf *inf)
652 inf_debug (inf, "clearing wait");
653 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
654 inf->wait.thread = 0;
655 inf->wait.suppress = 0;
656 if (inf->wait.exc.handler != MACH_PORT_NULL)
658 mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
659 inf->wait.exc.handler = MACH_PORT_NULL;
661 if (inf->wait.exc.reply != MACH_PORT_NULL)
663 mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
664 inf->wait.exc.reply = MACH_PORT_NULL;
669 inf_cleanup (struct inf *inf)
671 inf_debug (inf, "cleanup");
673 inf_clear_wait (inf);
675 inf_set_task (inf, MACH_PORT_NULL);
681 inf->pending_execs = 0;
685 mach_port_destroy (mach_task_self (), inf->event_port);
686 inf->event_port = MACH_PORT_NULL;
691 inf_startup (struct inf *inf, int pid, task_t task)
695 inf_debug (inf, "startup: pid = %d, task = %d", pid, task);
699 /* Make the port on which we receive all events. */
700 err = mach_port_allocate (mach_task_self (),
701 MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
703 error ("Error allocating event port: %s", strerror (err));
705 /* Make a send right for it, so we can easily copy it for other people. */
706 mach_port_insert_right (mach_task_self (), inf->event_port,
707 inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
712 inf_set_task (inf, task);
718 inf->task->sc = inf->task->cur_sc = 1; /* Reflect task_suspend above */
723 inf_set_task (struct inf *inf, mach_port_t port)
725 struct proc *task = inf->task;
727 inf_debug (inf, "setting task: %d", port);
729 if (task && task->port != port)
732 inf_validate_procs (inf); /* Trash all the threads. */
733 _proc_free (task); /* And the task. */
736 if (port != MACH_PORT_NULL)
738 inf->task = make_proc (inf, port, PROC_TID_TASK);
739 inf->threads_up_to_date = 0;
743 /* Validates INF's stopped field from the actual proc server state. */
745 inf_validate_stopped (struct inf *inf)
748 mach_msg_type_number_t noise_len = 0;
750 mach_msg_type_number_t pi_len = 0;
753 proc_getprocinfo (proc_server, inf->pid, &info_flags,
754 (procinfo_t *)&pi, &pi_len, &noise, &noise_len);
758 inf->stopped = !!(pi->state & PI_STOPPED);
759 vm_deallocate (mach_task_self (), (vm_address_t)pi, pi_len);
761 vm_deallocate (mach_task_self (), (vm_address_t)noise, noise_len);
765 /* Validates INF's task suspend count. If it's higher than we expect, verify
766 with the user before `stealing' the extra count. */
768 inf_validate_task_sc (struct inf *inf)
770 struct task_basic_info info;
771 mach_msg_type_number_t info_len = TASK_BASIC_INFO_COUNT;
773 task_info (inf->task->port, TASK_BASIC_INFO, (task_info_t)&info, &info_len);
776 inf->task->dead = 1; /* oh well */
777 else if (inf->task->cur_sc < info.suspend_count)
781 target_terminal_ours (); /* Allow I/O. */
783 !query ("Pid %d has an additional task suspend count of %d; clear it? ",
784 inf->pid, info.suspend_count - inf->task->cur_sc);
785 target_terminal_inferior (); /* Give it back to the child. */
788 error ("Additional task suspend count left untouched.");
790 inf->task->cur_sc = info.suspend_count;
794 /* Turns tracing for INF on or off, depending on ON, unless it already is.
795 If INF is running, the resume_sc count of INF's threads will be modified,
796 and the signal thread will briefly be run to change the trace state. */
798 inf_set_traced (struct inf *inf, int on)
800 if (on != inf->traced)
801 if (inf->task && !inf->task->dead)
802 /* Make it take effect immediately. */
804 sigset_t mask = on ? ~(sigset_t)0 : 0;
806 INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
807 INIT_TRACEMASK, mask));
811 warning ("Can't modify tracing state for pid %d: No signal thread",
816 warning ("Can't modify tracing state for pid %d: %s",
817 inf->pid, strerror (err));
825 /* Makes all the real suspend count deltas of all the procs in INF match the
826 desired values. Careful to always do thread/task suspend counts in the
827 safe order. Returns true if at least one thread is thought to be running.*/
829 inf_update_suspends (struct inf *inf)
831 struct proc *task = inf->task;
832 /* We don't have to update INF->threads even though we're iterating over it
833 because we'll change a thread only if it already has an existing proc
836 inf_debug (inf, "updating suspend counts");
841 int task_running = (task->sc == 0), thread_running = 0;
843 if (task->sc > task->cur_sc)
844 /* The task is becoming _more_ suspended; do before any threads. */
845 task_running = proc_update_sc (task);
847 if (inf->pending_execs)
848 /* When we're waiting for an exec, things may be happening behind our
849 back, so be conservative. */
852 /* Do all the thread suspend counts. */
853 for (thread = inf->threads; thread; thread = thread->next)
854 thread_running |= proc_update_sc (thread);
856 if (task->sc != task->cur_sc)
857 /* We didn't do the task first, because we wanted to wait for the
858 threads; do it now. */
859 task_running = proc_update_sc (task);
861 inf_debug (inf, "%srunning...",
862 (thread_running && task_running) ? "" : "not ");
864 inf->running = thread_running && task_running;
866 /* Once any thread has executed some code, we can't depend on the
867 threads list any more. */
869 inf->threads_up_to_date = 0;
877 /* Converts a GDB pid to a struct proc. */
879 inf_tid_to_thread (struct inf *inf, int tid)
881 struct proc *thread = inf->threads;
883 if (thread->tid == tid)
886 thread = thread->next;
890 /* Converts a thread port to a struct proc. */
892 inf_port_to_thread (struct inf *inf, mach_port_t port)
894 struct proc *thread = inf->threads;
896 if (thread->port == port)
899 thread = thread->next;
903 /* Make INF's list of threads be consistent with reality of TASK. */
905 inf_validate_procs (struct inf *inf)
908 thread_array_t threads;
909 unsigned num_threads;
910 struct proc *task = inf->task;
912 /* If no threads are currently running, this function will guarantee that
913 things are up to date. The exception is if there are zero threads --
914 then it is almost certainly in an odd state, and probably some outside
915 agent will create threads. */
916 inf->threads_up_to_date = inf->threads ? !inf->running : 0;
920 error_t err = task_threads (task->port, &threads, &num_threads);
921 inf_debug (inf, "fetching threads");
923 /* TASK must be dead. */
933 inf_debug (inf, "no task");
937 unsigned search_start = 0; /* Make things normally linear. */
938 /* Which thread in PROCS corresponds to each task thread, & the task. */
939 struct proc *matched[num_threads + 1];
940 /* The last thread in INF->threads, so we can add to the end. */
941 struct proc *last = 0;
942 /* The current thread we're considering. */
943 struct proc *thread = inf->threads;
945 bzero (matched, sizeof (matched));
951 for (i = search_start, left = num_threads; left; i++, left--)
953 if (i >= num_threads)
954 i -= num_threads; /* I wrapped around. */
955 if (thread->port == threads[i])
956 /* We already know about this thread. */
960 thread = thread->next;
968 proc_debug (thread, "died!");
969 thread->port = MACH_PORT_NULL;
970 thread = _proc_free (thread); /* THREAD is dead. */
971 (last ? last->next : inf->threads) = thread;
975 for (i = 0; i < num_threads; i++)
977 /* Throw away the duplicate send right. */
978 mach_port_deallocate (mach_task_self (), threads[i]);
980 /* THREADS[I] is a thread we don't know about yet! */
982 thread = make_proc (inf, threads[i], next_thread_id++);
983 (last ? last->next : inf->threads) = thread;
985 proc_debug (thread, "new thread: %d", threads[i]);
986 add_thread (thread->tid); /* Tell GDB's generic thread code. */
989 vm_deallocate(mach_task_self(),
990 (vm_address_t)threads, (num_threads * sizeof(thread_t)));
994 /* Makes sure that INF's thread list is synced with the actual process. */
996 inf_update_procs (struct inf *inf)
1000 if (! inf->threads_up_to_date)
1001 inf_validate_procs (inf);
1005 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
1006 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1009 inf_set_threads_resume_sc (struct inf *inf,
1010 struct proc *run_thread, int run_others)
1012 struct proc *thread;
1013 inf_update_procs (inf);
1014 for (thread = inf->threads; thread; thread = thread->next)
1015 if (thread == run_thread)
1016 thread->resume_sc = 0;
1017 else if (run_others)
1018 thread->resume_sc = thread->run_sc;
1020 thread->resume_sc = thread->pause_sc;
1023 /* Cause INF to continue execution immediately; individual threads may still
1024 be suspended (but their suspend counts will be updated). */
1026 inf_resume (struct inf *inf)
1028 struct proc *thread;
1030 inf_update_procs (inf);
1032 for (thread = inf->threads; thread; thread = thread->next)
1033 thread->sc = thread->resume_sc;
1037 if (! inf->pending_execs)
1038 /* Try to make sure our task count is correct -- in the case where
1039 we're waiting for an exec though, things are too volatile, so just
1040 assume things will be reasonable (which they usually will be). */
1041 inf_validate_task_sc (inf);
1045 inf_update_suspends (inf);
1048 /* Cause INF to stop execution immediately; individual threads may still
1051 inf_suspend (struct inf *inf)
1053 struct proc *thread;
1055 inf_update_procs (inf);
1057 for (thread = inf->threads; thread; thread = thread->next)
1058 thread->sc = thread->pause_sc;
1061 inf->task->sc = inf->pause_sc;
1063 inf_update_suspends (inf);
1066 /* INF has one thread PROC that is in single-stepping mode. This functions
1067 changes it to be PROC, changing any old step_thread to be a normal one. A
1068 PROC of 0 clears an any existing value. */
1070 inf_set_step_thread (struct inf *inf, struct proc *thread)
1072 assert (!thread || proc_is_thread (thread));
1075 inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1077 inf_debug (inf, "clearing step thread");
1079 if (inf->step_thread != thread)
1081 if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1082 if (! proc_trace (inf->step_thread, 0))
1084 if (thread && proc_trace (thread, 1))
1085 inf->step_thread = thread;
1087 inf->step_thread = 0;
1091 /* Set up the thread resume_sc's so that only the signal thread is running
1092 (plus whatever other thread are set to always run). Returns true if we
1093 did so, or false if we can't find a signal thread. */
1095 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1097 if (inf->signal_thread)
1099 inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1107 inf_update_signal_thread (struct inf *inf)
1109 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1110 the signal thread. */
1111 inf->signal_thread = inf->threads ? inf->threads->next : 0;
1114 /* Detachs from INF's inferior task, letting it run once again... */
1116 inf_detach (struct inf *inf)
1118 struct proc *task = inf->task;
1120 inf_debug (inf, "detaching...");
1122 inf_clear_wait (inf);
1123 inf_set_step_thread (inf, 0);
1127 struct proc *thread;
1129 inf_set_traced (inf, 0);
1131 inf_signal (inf, TARGET_SIGNAL_0);
1133 proc_restore_exc_port (task);
1134 task->sc = inf->detach_sc;
1136 for (thread = inf->threads; thread; thread = thread->next)
1138 proc_restore_exc_port (thread);
1139 thread->sc = thread->detach_sc;
1142 inf_update_suspends (inf);
1148 /* Attaches INF to the process with process id PID, returning it in a suspended
1149 state suitable for debugging. */
1151 inf_attach (struct inf *inf, int pid)
1156 inf_debug (inf, "attaching: %d", pid);
1158 err = proc_pid2task (proc_server, pid, &task);
1160 error ("Error getting task for pid %d: %s", pid, strerror (err));
1165 inf_startup (inf, pid, task);
1168 /* Makes sure that we've got our exception ports entrenched in the process. */
1169 void inf_steal_exc_ports (struct inf *inf)
1171 struct proc *thread;
1173 inf_debug (inf, "stealing exception ports");
1175 inf_set_step_thread (inf, 0); /* The step thread is special. */
1177 proc_steal_exc_port (inf->task, inf->event_port);
1178 for (thread = inf->threads; thread; thread = thread->next)
1179 proc_steal_exc_port (thread, MACH_PORT_NULL);
1182 /* Makes sure the process has its own exception ports. */
1183 void inf_restore_exc_ports (struct inf *inf)
1185 struct proc *thread;
1187 inf_debug (inf, "restoring exception ports");
1189 inf_set_step_thread (inf, 0); /* The step thread is special. */
1191 proc_restore_exc_port (inf->task);
1192 for (thread = inf->threads; thread; thread = thread->next)
1193 proc_restore_exc_port (thread);
1196 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1197 signal 0, will continue it. INF is assumed to be in a paused state, and
1198 the resume_sc's of INF's threads may be affected. */
1200 inf_signal (struct inf *inf, enum target_signal sig)
1203 int host_sig = target_signal_to_host (sig);
1205 #define NAME target_signal_to_name (sig)
1207 if (host_sig >= _NSIG)
1208 /* A mach exception. Exceptions are encoded in the signal space by
1209 putting them after _NSIG; this assumes they're positive (and not
1210 extremely large)! */
1212 struct inf_wait *w = &inf->wait;
1213 if (w->status.kind == TARGET_WAITKIND_STOPPED
1214 && w->status.value.sig == sig
1215 && w->thread && !w->thread->aborted)
1216 /* We're passing through the last exception we received. This is
1217 kind of bogus, because exceptions are per-thread whereas gdb
1218 treats signals as per-process. We just forward the exception to
1219 the correct handler, even it's not for the same thread as TID --
1220 i.e., we pretend it's global. */
1222 struct exc_state *e = &w->exc;
1223 inf_debug (inf, "passing through exception:"
1224 " task = %d, thread = %d, exc = %d"
1225 ", code = %d, subcode = %d",
1226 w->thread->port, inf->task->port,
1227 e->exception, e->code, e->subcode);
1229 exception_raise_request (e->handler,
1230 e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1231 w->thread->port, inf->task->port,
1232 e->exception, e->code, e->subcode);
1235 error ("Can't forward spontaneous exception (%s).", NAME);
1238 /* A Unix signal. */
1240 /* The process is stopped an expecting a signal. Just send off a
1241 request and let it get handled when we resume everything. */
1243 inf_debug (inf, "sending %s to stopped process", NAME);
1245 INF_MSGPORT_RPC (inf,
1246 msg_sig_post_untraced_request (msgport,
1248 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1252 /* Posting an untraced signal automatically continues it.
1253 We clear this here rather than when we get the reply
1254 because we'd rather assume it's not stopped when it
1255 actually is, than the reverse. */
1259 /* It's not expecting it. We have to let just the signal thread
1260 run, and wait for it to get into a reasonable state before we
1261 can continue the rest of the process. When we finally resume the
1262 process the signal we request will be the very first thing that
1265 inf_debug (inf, "sending %s to unstopped process (so resuming signal thread)", NAME);
1267 INF_RESUME_MSGPORT_RPC (inf, msg_sig_post_untraced (msgport,
1268 host_sig, 0, refport));
1272 /* Can't do too much... */
1273 warning ("Can't deliver signal %s: No signal thread.", NAME);
1275 warning ("Delivering signal %s: %s", NAME, strerror (err));
1280 /* The inferior used for all gdb target ops. */
1281 struct inf *current_inferior = 0;
1283 /* The inferior being waited for by gnu_wait. Since GDB is decidely not
1284 multi-threaded, we don't bother to lock this. */
1285 struct inf *waiting_inf;
1287 /* Wait for something to happen in the inferior, returning what in STATUS. */
1289 gnu_wait (int tid, struct target_waitstatus *status)
1292 mach_msg_header_t hdr;
1293 mach_msg_type_t type;
1297 struct proc *thread;
1298 struct inf *inf = current_inferior;
1302 if (!inf->threads && !inf->pending_execs)
1303 /* No threads! Assume that maybe some outside agency is frobbing our
1304 task, and really look for new threads. If we can't find any, just tell
1305 the user to try again later. */
1307 inf_validate_procs (inf);
1308 if (!inf->threads && !inf->task->dead)
1309 error ("There are no threads; try again later.");
1314 inf_debug (inf, "waiting for: %d", tid);
1317 if (proc_wait_pid != inf->pid && !inf->no_wait)
1318 /* Always get information on events from the proc server. */
1320 inf_debug (inf, "requesting wait on pid %d", inf->pid);
1323 /* The proc server is single-threaded, and only allows a single
1324 outstanding wait request, so we have to cancel the previous one. */
1326 inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1327 interrupt_operation (proc_server, 0);
1331 proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1333 warning ("wait request failed: %s", strerror (err));
1336 inf_debug (inf, "waits pending: %d", proc_waits_pending);
1337 proc_wait_pid = inf->pid;
1338 /* Even if proc_waits_pending was > 0 before, we still won't get
1339 any other replies, because it was either from a different INF,
1340 or a different process attached to INF -- and the event port,
1341 which is the wait reply port, changes when you switch processes.*/
1342 proc_waits_pending = 1;
1346 inf_clear_wait (inf);
1348 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1349 (3) wait reply from the proc server. */
1351 inf_debug (inf, "waiting for an event...");
1352 err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
1353 0, sizeof (struct msg), inf->event_port,
1354 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1356 /* Re-suspend the task. */
1359 if (err == EMACH_RCV_INTERRUPTED)
1360 inf_debug (inf, "interrupted");
1362 error ("Couldn't wait for an event: %s", strerror (err));
1366 mach_msg_header_t hdr;
1367 mach_msg_type_t err_type;
1372 inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1374 /* Handle what we got. */
1375 if (! notify_server (&msg.hdr, &reply.hdr)
1376 && ! exc_server (&msg.hdr, &reply.hdr)
1377 && ! process_reply_server (&msg.hdr, &reply.hdr)
1378 && ! msg_reply_server (&msg.hdr, &reply.hdr))
1379 /* Whatever it is, it's something strange. */
1380 error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id);
1383 error ("Handling event, msgid = %d: %s",
1384 msg.hdr.msgh_id, strerror (reply.err));
1387 if (inf->pending_execs)
1388 /* We're waiting for the inferior to finish execing. */
1390 struct inf_wait *w = &inf->wait;
1391 enum target_waitkind kind = w->status.kind;
1393 if (kind == TARGET_WAITKIND_SPURIOUS)
1394 /* Since gdb is actually counting the number of times the inferior
1395 stops, expecting one stop per exec, we only return major events
1399 inf_debug (inf, "pending_execs = %d, ignoring minor event",
1400 inf->pending_execs);
1402 else if (kind == TARGET_WAITKIND_STOPPED
1403 && w->status.value.sig == TARGET_SIGNAL_TRAP)
1404 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1405 means we've succesfully completed an exec! */
1407 if (--inf->pending_execs == 0)
1410 prune_threads (1); /* Get rid of the old shell threads */
1411 renumber_threads (0); /* Give our threads reasonable names. */
1413 inf_debug (inf, "pending exec completed, pending_execs => %d",
1414 inf->pending_execs);
1418 if (inf->wait.suppress)
1419 /* Some totally spurious event happened that we don't consider
1420 worth returning to gdb. Just keep waiting. */
1422 inf_debug (inf, "suppressing return, rewaiting...");
1427 /* Pass back out our results. */
1428 bcopy (&inf->wait.status, status, sizeof (*status));
1430 thread = inf->wait.thread;
1434 thread = inf_tid_to_thread (inf, tid);
1436 if (!thread || thread->port == MACH_PORT_NULL)
1437 /* TID is dead; try and find a new thread. */
1438 if (inf_update_procs (inf) && inf->threads)
1439 tid = inf->threads->tid; /* The first available thread. */
1443 if (thread && tid >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS
1444 && inf->pause_sc == 0 && thread->pause_sc == 0)
1445 /* If something actually happened to THREAD, make sure we suspend it. */
1448 inf_update_suspends (inf);
1451 inf_debug (inf, "returning tid = %d, status = %s (%d)", tid,
1452 status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
1453 : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
1454 : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
1455 : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
1456 : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
1458 status->value.integer);
1463 /* The rpc handler called by exc_server. */
1465 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1466 thread_t thread_port, task_t task_port,
1467 int exception, int code, int subcode)
1469 struct inf *inf = waiting_inf;
1470 struct proc *thread = inf_port_to_thread (inf, thread_port);
1472 inf_debug (waiting_inf,
1473 "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1474 thread_port, task_port, exception, code);
1477 /* We don't know about thread? */
1479 inf_update_procs (inf);
1480 thread = inf_port_to_thread (inf, thread_port);
1482 /* Give up, the generating thread is gone. */
1486 mach_port_deallocate (mach_task_self (), thread_port);
1487 mach_port_deallocate (mach_task_self (), task_port);
1489 if (! thread->aborted)
1490 /* THREAD hasn't been aborted since this exception happened (abortion
1491 clears any exception state), so it must be real. */
1493 /* Store away the details; this will destroy any previous info. */
1494 inf->wait.thread = thread;
1496 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1498 if (exception == EXC_BREAKPOINT)
1499 /* GDB likes to get SIGTRAP for breakpoints. */
1501 inf->wait.status.value.sig = TARGET_SIGNAL_TRAP;
1502 mach_port_deallocate (mach_task_self (), reply_port);
1505 /* Record the exception so that we can forward it later. */
1507 if (thread->exc_port == port)
1509 inf_debug (waiting_inf, "Handler is thread exeption port <%d>",
1510 thread->saved_exc_port);
1511 inf->wait.exc.handler = thread->saved_exc_port;
1515 inf_debug (waiting_inf, "Handler is task exeption port <%d>",
1516 inf->task->saved_exc_port);
1517 inf->wait.exc.handler = inf->task->saved_exc_port;
1518 assert (inf->task->exc_port == port);
1520 if (inf->wait.exc.handler != MACH_PORT_NULL)
1521 /* Add a reference to the exception handler. */
1522 mach_port_mod_refs (mach_task_self (),
1523 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1526 inf->wait.exc.exception = exception;
1527 inf->wait.exc.code = code;
1528 inf->wait.exc.subcode = subcode;
1529 inf->wait.exc.reply = reply_port;
1531 /* Exceptions are encoded in the signal space by putting them after
1532 _NSIG; this assumes they're positive (and not extremely large)! */
1533 inf->wait.status.value.sig =
1534 target_signal_from_host (_NSIG + exception);
1538 /* A supppressed exception, which ignore. */
1540 inf->wait.suppress = 1;
1541 mach_port_deallocate (mach_task_self (), reply_port);
1547 /* Fill in INF's wait field after a task has died without giving us more
1548 detailed information. */
1550 inf_task_died_status (struct inf *inf)
1552 warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid);
1553 inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
1554 inf->wait.status.value.sig = TARGET_SIGNAL_KILL;
1557 /* Notify server routines. The only real one is dead name notification. */
1559 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1561 struct inf *inf = waiting_inf;
1563 inf_debug (waiting_inf, "port = %d", dead_port);
1565 if (inf->task && inf->task->port == dead_port)
1567 proc_debug (inf->task, "is dead");
1568 inf->task->port = MACH_PORT_NULL;
1569 if (proc_wait_pid == inf->pid)
1570 /* We have a wait outstanding on the process, which will return more
1571 detailed information, so delay until we get that. */
1572 inf->wait.suppress = 1;
1574 /* We never waited for the process (maybe it wasn't a child), so just
1575 pretend it got a SIGKILL. */
1576 inf_task_died_status (inf);
1580 struct proc *thread = inf_port_to_thread (inf, dead_port);
1583 proc_debug (thread, "is dead");
1584 thread->port = MACH_PORT_NULL;
1588 mach_port_deallocate (mach_task_self (), dead_port);
1589 inf->threads_up_to_date = 0; /* Just in case */
1597 warning ("illegal rpc: %s", fun);
1602 do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
1604 return ill_rpc (__FUNCTION__);
1608 do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
1610 return ill_rpc (__FUNCTION__);
1614 do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
1616 return ill_rpc (__FUNCTION__);
1620 do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
1622 return ill_rpc (__FUNCTION__);
1626 do_mach_notify_send_once (mach_port_t notify)
1628 return ill_rpc (__FUNCTION__);
1631 /* Process_reply server routines. We only use process_wait_reply. */
1634 S_proc_wait_reply (mach_port_t reply, error_t err,
1635 int status, int sigcode, rusage_t rusage, pid_t pid)
1637 struct inf *inf = waiting_inf;
1639 inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1640 err ? strerror (err) : "0", pid, status, sigcode);
1642 if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1643 /* Ack. The task has died, but the task-died notification code didn't
1644 tell anyone because it thought a more detailed reply from the
1645 procserver was forthcoming. However, we now learn that won't
1646 happen... So we have to act like the task just died, and this time,
1648 inf_task_died_status (inf);
1650 if (--proc_waits_pending == 0)
1651 /* PROC_WAIT_PID represents the most recent wait. We will always get
1652 replies in order because the proc server is single threaded. */
1655 inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1661 warning ("Can't wait for pid %d: %s", inf->pid, strerror (err));
1664 /* Since we can't see the inferior's signals, don't trap them. */
1665 inf_set_traced (inf, 0);
1668 else if (pid == inf->pid)
1670 store_waitstatus (&inf->wait.status, status);
1671 if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
1672 /* The process has sent us a signal, and stopped itself in a sane
1673 state pending our actions. */
1675 inf_debug (inf, "process has stopped itself");
1680 inf->wait.suppress = 1; /* Something odd happened. Ignore. */
1686 S_proc_setmsgport_reply (mach_port_t reply, error_t err,
1687 mach_port_t old_msg_port)
1689 return ill_rpc (__FUNCTION__);
1693 S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
1695 return ill_rpc (__FUNCTION__);
1698 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1701 S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
1703 struct inf *inf = waiting_inf;
1706 /* EBUSY is what we get when the crash server has grabbed control of the
1707 process and doesn't like what signal we tried to send it. Just act
1708 like the process stopped (using a signal of 0 should mean that the
1709 *next* time the user continues, it will pass signal 0, which the crash
1710 server should like). */
1712 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1713 inf->wait.status.value.sig = TARGET_SIGNAL_0;
1716 warning ("Signal delivery failed: %s", strerror (err));
1719 /* We only get this reply when we've posted a signal to a process which we
1720 thought was stopped, and which we expected to continue after the signal.
1721 Given that the signal has failed for some reason, it's reasonable to
1722 assume it's still stopped. */
1725 inf->wait.suppress = 1;
1731 S_msg_sig_post_reply (mach_port_t reply, error_t err)
1733 return ill_rpc (__FUNCTION__);
1736 /* Returns the number of messages queued for the receive right PORT. */
1737 static mach_port_msgcount_t
1738 port_msgs_queued (mach_port_t port)
1740 struct mach_port_status status;
1742 mach_port_get_receive_status (mach_task_self (), port, &status);
1747 return status.mps_msgcount;
1750 /* Resume execution of the inferior process.
1752 If STEP is nonzero, single-step it.
1753 If SIGNAL is nonzero, give it that signal.
1756 -1 true Single step the current thread allowing other threads to run.
1757 -1 false Continue the current thread allowing other threads to run.
1758 X true Single step the given thread, don't allow any others to run.
1759 X false Continue the given thread, do not allow any others to run.
1760 (Where X, of course, is anything except -1)
1762 Note that a resume may not `take' if there are pending exceptions/&c
1763 still unprocessed from the last resume we did (any given resume may result
1764 in multiple events returned by wait).
1767 gnu_resume (int tid, int step, enum target_signal sig)
1769 struct proc *step_thread = 0;
1770 struct inf *inf = current_inferior;
1772 inf_debug (inf, "tid = %d, step = %d, sig = %d", tid, step, sig);
1774 if (sig != TARGET_SIGNAL_0 || inf->stopped)
1775 inf_signal (inf, sig);
1776 else if (inf->wait.exc.reply != MACH_PORT_NULL)
1777 /* We received an exception to which we have chosen not to forward, so
1778 abort the faulting thread, which will perhaps retake it. */
1780 proc_abort (inf->wait.thread, 1);
1781 warning ("Aborting %s with unforwarded exception %s.",
1782 proc_string (inf->wait.thread),
1783 target_signal_to_name (inf->wait.status.value.sig));
1786 if (port_msgs_queued (inf->event_port))
1787 /* If there are still messages in our event queue, don't bother resuming
1788 the process, as we're just going to stop it right away anyway. */
1791 inf_update_procs (inf);
1794 /* Allow all threads to run, except perhaps single-stepping one. */
1796 inf_debug (inf, "running all threads; tid = %d", inferior_pid);
1797 tid = inferior_pid; /* What to step. */
1798 inf_set_threads_resume_sc (inf, 0, 1);
1801 /* Just allow a single thread to run. */
1803 struct proc *thread = inf_tid_to_thread (inf, tid);
1805 error ("Can't run single thread id %d: no such thread!");
1806 inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid);
1807 inf_set_threads_resume_sc (inf, thread, 0);
1812 step_thread = inf_tid_to_thread (inf, tid);
1814 warning ("Can't step thread id %d: no such thread.", tid);
1816 inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid);
1818 if (step_thread != inf->step_thread)
1819 inf_set_step_thread (inf, step_thread);
1821 inf_debug (inf, "here we go...");
1826 gnu_kill_inferior ()
1828 struct proc *task = current_inferior->task;
1831 proc_debug (task, "terminating...");
1832 task_terminate (task->port);
1833 inf_set_task (current_inferior, MACH_PORT_NULL);
1835 target_mourn_inferior ();
1838 /* Clean up after the inferior dies. */
1841 gnu_mourn_inferior ()
1843 inf_debug (current_inferior, "rip");
1844 inf_detach (current_inferior);
1845 unpush_target (&gnu_ops);
1846 generic_mourn_inferior ();
1849 /* Fork an inferior process, and start debugging it. */
1851 /* Set INFERIOR_PID to the first thread available in the child, if any. */
1853 pick_first_thread ()
1855 if (current_inferior->task && current_inferior->threads)
1856 /* The first thread. */
1857 inferior_pid = current_inferior->threads->tid;
1859 /* What may be the next thread. */
1860 inferior_pid = next_thread_id;
1866 if (! current_inferior)
1867 current_inferior = make_inf ();
1868 return current_inferior;
1872 gnu_create_inferior (exec_file, allargs, env)
1877 struct inf *inf = cur_inf ();
1881 /* We're in the child; make this process stop as soon as it execs. */
1882 inf_debug (inf, "tracing self");
1883 if (ptrace (PTRACE_TRACEME) != 0)
1884 error ("ptrace (PTRACE_TRACEME) failed!");
1886 int attach_to_child (int pid)
1888 /* Attach to the now stopped child, which is actually a shell... */
1889 inf_debug (inf, "attaching to child: %d", pid);
1891 inf_attach (inf, pid);
1892 pick_first_thread ();
1895 push_target (&gnu_ops);
1897 inf->pending_execs = 2;
1900 /* Now let the child run again, knowing that it will stop immediately
1901 because of the ptrace. */
1904 startup_inferior (pid, inf->pending_execs);
1909 inf_debug (inf, "creating inferior");
1911 fork_inferior (exec_file, allargs, env, trace_me, attach_to_child, NULL);
1913 inf_update_signal_thread (inf);
1914 inf_set_traced (inf, inf->want_signals);
1916 /* Execing the process will have trashed our exception ports; steal them
1917 back (or make sure they're restored if the user wants that). */
1918 if (inf->want_exceptions)
1919 inf_steal_exc_ports (inf);
1921 inf_restore_exc_ports (inf);
1924 proceed ((CORE_ADDR) -1, 0, 0);
1927 /* Mark our target-struct as eligible for stray "run" and "attach"
1935 #ifdef ATTACH_DETACH
1937 /* Attach to process PID, then initialize for debugging it
1938 and wait for the trace-trap that results from attaching. */
1940 gnu_attach (args, from_tty)
1946 struct inf *inf = cur_inf ();
1949 error_no_arg ("PID to attach");
1953 if (pid == getpid()) /* Trying to masturbate? */
1954 error ("I refuse to debug myself!");
1958 exec_file = (char *) get_exec_file (0);
1961 printf_unfiltered ("Attaching to program `%s', pid %d\n",
1964 printf_unfiltered ("Attaching to pid %d\n", pid);
1966 gdb_flush (gdb_stdout);
1969 inf_debug (inf, "attaching to pid: %d", pid);
1971 inf_attach (inf, pid);
1972 inf_update_procs (inf);
1974 pick_first_thread ();
1977 push_target (&gnu_ops);
1979 inf_update_signal_thread (inf);
1980 inf_set_traced (inf, inf->want_signals);
1982 /* If the process was stopped before we attached, make it continue the next
1983 time the user does a continue. */
1984 inf_validate_stopped (inf);
1986 renumber_threads (0); /* Give our threads reasonable names. */
1989 /* Take a program previously attached to and detaches it.
1990 The program resumes execution and will no longer stop
1991 on signals, etc. We'd better not have left any breakpoints
1992 in the program or it'll die when it hits one. For this
1993 to work, it may be necessary for the process to have been
1994 previously attached. It *might* work if the program was
1995 started via fork. */
1997 gnu_detach (args, from_tty)
2003 char *exec_file = get_exec_file (0);
2005 printf_unfiltered ("Detaching from program `%s' pid %d\n",
2006 exec_file, current_inferior->pid);
2008 printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid);
2009 gdb_flush (gdb_stdout);
2012 inf_detach (current_inferior);
2016 unpush_target (&gnu_ops); /* Pop out of handling an inferior */
2018 #endif /* ATTACH_DETACH */
2021 gnu_terminal_init_inferior ()
2023 assert (current_inferior);
2024 terminal_init_inferior_with_pgrp (current_inferior->pid);
2027 /* Get ready to modify the registers array. On machines which store
2028 individual registers, this doesn't need to do anything. On machines
2029 which store all the registers in one fell swoop, this makes sure
2030 that registers contains all the registers from the program being
2034 gnu_prepare_to_store ()
2036 #ifdef CHILD_PREPARE_TO_STORE
2037 CHILD_PREPARE_TO_STORE ();
2042 gnu_open (arg, from_tty)
2046 error ("Use the \"run\" command to start a Unix child process.");
2052 error ("to_stop target function not implemented");
2056 gnu_thread_alive (int tid)
2058 inf_update_procs (current_inferior);
2059 return !!inf_tid_to_thread (current_inferior, tid);
2063 * Read inferior task's LEN bytes from ADDR and copy it to MYADDR
2064 * in gdb's address space.
2066 * Return 0 on failure; number of bytes read otherwise.
2069 gnu_read_inferior (task, addr, myaddr, length)
2076 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2077 vm_size_t aligned_length =
2078 (vm_size_t) round_page (addr+length) - low_address;
2082 /* Get memory from inferior with page aligned addresses */
2083 err = vm_read (task, low_address, aligned_length, &copied, ©_count);
2087 err = hurd_safe_copyin (myaddr, (void*)addr - low_address + copied, length);
2090 warning ("Read from inferior faulted: %s", strerror (err));
2094 err = vm_deallocate (mach_task_self (), copied, copy_count);
2096 warning ("gnu_read_inferior vm_deallocate failed: %s", strerror (err));
2101 #define CHK_GOTO_OUT(str,ret) \
2102 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2104 struct vm_region_list {
2105 struct vm_region_list *next;
2106 vm_prot_t protection;
2111 struct obstack region_obstack;
2114 * Write inferior task's LEN bytes from ADDR and copy it to MYADDR
2115 * in gdb's address space.
2118 gnu_write_inferior (task, addr, myaddr, length)
2125 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2126 vm_size_t aligned_length =
2127 (vm_size_t) round_page (addr+length) - low_address;
2132 char *errstr = "Bug in gnu_write_inferior";
2134 struct vm_region_list *region_element;
2135 struct vm_region_list *region_head = (struct vm_region_list *)NULL;
2137 /* Get memory from inferior with page aligned addresses */
2138 err = vm_read (task,
2143 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2147 err = hurd_safe_copyout ((void*)addr - low_address + copied, myaddr, length);
2148 CHK_GOTO_OUT ("Write to inferior faulted", err);
2150 obstack_init (®ion_obstack);
2152 /* Do writes atomically.
2153 * First check for holes and unwritable memory.
2156 vm_size_t remaining_length = aligned_length;
2157 vm_address_t region_address = low_address;
2159 struct vm_region_list *scan;
2161 while(region_address < low_address + aligned_length)
2163 vm_prot_t protection;
2164 vm_prot_t max_protection;
2165 vm_inherit_t inheritance;
2167 mach_port_t object_name;
2169 vm_size_t region_length = remaining_length;
2170 vm_address_t old_address = region_address;
2172 err = vm_region (task,
2181 CHK_GOTO_OUT ("vm_region failed", err);
2183 /* Check for holes in memory */
2184 if (old_address != region_address)
2186 warning ("No memory at 0x%x. Nothing written",
2193 if (!(max_protection & VM_PROT_WRITE))
2195 warning ("Memory at address 0x%x is unwritable. Nothing written",
2202 /* Chain the regions for later use */
2204 (struct vm_region_list *)
2205 obstack_alloc (®ion_obstack, sizeof (struct vm_region_list));
2207 region_element->protection = protection;
2208 region_element->start = region_address;
2209 region_element->length = region_length;
2211 /* Chain the regions along with protections */
2212 region_element->next = region_head;
2213 region_head = region_element;
2215 region_address += region_length;
2216 remaining_length = remaining_length - region_length;
2219 /* If things fail after this, we give up.
2220 * Somebody is messing up inferior_task's mappings.
2223 /* Enable writes to the chained vm regions */
2224 for (scan = region_head; scan; scan = scan->next)
2226 boolean_t protection_changed = FALSE;
2228 if (!(scan->protection & VM_PROT_WRITE))
2230 err = vm_protect (task,
2234 scan->protection | VM_PROT_WRITE);
2235 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2239 err = vm_write (task,
2243 CHK_GOTO_OUT ("vm_write failed", err);
2245 /* Set up the original region protections, if they were changed */
2246 for (scan = region_head; scan; scan = scan->next)
2248 boolean_t protection_changed = FALSE;
2250 if (!(scan->protection & VM_PROT_WRITE))
2252 err = vm_protect (task,
2257 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2265 obstack_free (®ion_obstack, 0);
2267 (void) vm_deallocate (mach_task_self (),
2272 if (err != KERN_SUCCESS)
2274 warning ("%s: %s", errstr, mach_error_string (err));
2281 /* Return 0 on failure, number of bytes handled otherwise. */
2283 gnu_xfer_memory (memaddr, myaddr, len, write, target)
2288 struct target_ops *target; /* IGNORED */
2293 ? (current_inferior->task ? current_inferior->task->port : 0)
2296 if (task == MACH_PORT_NULL)
2300 inf_debug (current_inferior, "%s %p[%d] %s %p",
2301 write ? "writing" : "reading", memaddr, len,
2302 write ? "<--" : "-->", myaddr);
2304 return gnu_write_inferior (task, memaddr, myaddr, len);
2306 return gnu_read_inferior (task, memaddr, myaddr, len);
2310 extern void gnu_store_registers (int regno);
2311 extern void gnu_fetch_registers (int regno);
2313 struct target_ops gnu_ops = {
2314 "GNU", /* to_shortname */
2315 "GNU Hurd process", /* to_longname */
2316 "GNU Hurd process", /* to_doc */
2317 gnu_open, /* to_open */
2319 gnu_attach, /* to_attach */
2320 gnu_detach, /* to_detach */
2321 gnu_resume, /* to_resume */
2322 gnu_wait, /* to_wait */
2323 gnu_fetch_registers, /* to_fetch_registers */
2324 gnu_store_registers, /* to_store_registers */
2325 gnu_prepare_to_store, /* to_prepare_to_store */
2326 gnu_xfer_memory, /* to_xfer_memory */
2327 0, /* to_files_info */
2328 memory_insert_breakpoint, /* to_insert_breakpoint */
2329 memory_remove_breakpoint, /* to_remove_breakpoint */
2330 gnu_terminal_init_inferior, /* to_terminal_init */
2331 terminal_inferior, /* to_terminal_inferior */
2332 terminal_ours_for_output, /* to_terminal_ours_for_output */
2333 terminal_ours, /* to_terminal_ours */
2334 child_terminal_info, /* to_terminal_info */
2335 gnu_kill_inferior, /* to_kill */
2337 0, /* to_lookup_symbol */
2339 gnu_create_inferior, /* to_create_inferior */
2340 gnu_mourn_inferior, /* to_mourn_inferior */
2341 gnu_can_run, /* to_can_run */
2342 0, /* to_notice_signals */
2343 gnu_thread_alive, /* to_thread_alive */
2344 gnu_stop, /* to_stop */
2345 process_stratum, /* to_stratum */
2347 1, /* to_has_all_memory */
2348 1, /* to_has_memory */
2349 1, /* to_has_stack */
2350 1, /* to_has_registers */
2351 1, /* to_has_execution */
2353 0, /* sections_end */
2354 OPS_MAGIC /* to_magic */
2357 char *proc_string (struct proc *proc)
2359 static char tid_str[80];
2360 if (proc_is_task (proc))
2361 sprintf (tid_str, "process %d", proc->inf->pid);
2363 sprintf (tid_str, "thread %d.%d",
2365 pid_to_thread_id (proc->tid));
2370 gnu_target_pid_to_str (int tid)
2372 struct inf *inf = current_inferior;
2373 struct proc *thread = inf_tid_to_thread (inf, tid);
2376 return proc_string (thread);
2379 static char tid_str[80];
2380 sprintf (tid_str, "bogus thread id %d", tid);
2385 /* User task commands. */
2387 struct cmd_list_element *set_task_cmd_list = 0;
2388 struct cmd_list_element *show_task_cmd_list = 0;
2390 extern struct cmd_list_element *set_thread_default_cmd_list;
2391 extern struct cmd_list_element *show_thread_default_cmd_list;
2394 parse_int_arg (char *args, char *cmd_prefix)
2399 int val = strtoul (args, &arg_end, 10);
2400 if (*args && *arg_end == '\0')
2403 error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix);
2407 _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
2409 if (!args || strcmp (args, t_val) == 0)
2411 else if (strcmp (args, f_val) == 0)
2414 error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2415 cmd_prefix, t_val, f_val);
2418 #define parse_bool_arg(args, cmd_prefix) \
2419 _parse_bool_arg (args, "on", "off", cmd_prefix)
2422 check_empty (char *args, char *cmd_prefix)
2425 error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args);
2428 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2429 static struct proc *
2432 struct inf *inf = cur_inf ();
2433 struct proc *thread = inf_tid_to_thread (inf, inferior_pid);
2435 error ("No current thread.");
2439 /* Returns the current inferior, but signals an error if it has no task. */
2443 struct inf *inf = cur_inf ();
2445 error ("No current process.");
2450 set_task_pause_cmd (char *args, int from_tty)
2452 struct inf *inf = cur_inf ();
2453 int old_sc = inf->pause_sc;
2455 inf->pause_sc = parse_bool_arg (args, "set task pause");
2457 if (old_sc == 0 && inf->pause_sc != 0)
2458 /* If the task is currently unsuspended, immediately suspend it,
2459 otherwise wait until the next time it gets control. */
2464 show_task_pause_cmd (char *args, int from_tty)
2466 struct inf *inf = cur_inf ();
2467 check_empty (args, "show task pause");
2468 printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2470 ? (inf->pause_sc == 0 ? "isn't" : "is")
2471 : (inf->pause_sc == 0 ? "won't be" : "will be"));
2475 set_task_detach_sc_cmd (char *args, int from_tty)
2477 cur_inf ()->detach_sc = parse_int_arg (args, "set task detach-suspend-count");
2481 show_task_detach_sc_cmd (char *args, int from_tty)
2483 check_empty (args, "show task detach-suspend-count");
2484 printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n",
2485 cur_inf ()->detach_sc);
2489 set_thread_default_pause_cmd (char *args, int from_tty)
2491 struct inf *inf = cur_inf ();
2492 inf->default_thread_pause_sc =
2493 parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2497 show_thread_default_pause_cmd (char *args, int from_tty)
2499 struct inf *inf = cur_inf ();
2500 int sc = inf->default_thread_pause_sc;
2501 check_empty (args, "show thread default pause");
2502 printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2503 sc ? "are" : "aren't",
2504 !sc && inf->pause_sc ? " (but the task is)" : "");
2508 set_thread_default_run_cmd (char *args, int from_tty)
2510 struct inf *inf = cur_inf ();
2511 inf->default_thread_run_sc =
2512 parse_bool_arg (args, "set thread default run") ? 0 : 1;
2516 show_thread_default_run_cmd (char *args, int from_tty)
2518 struct inf *inf = cur_inf ();
2519 check_empty (args, "show thread default run");
2520 printf_unfiltered ("New threads %s allowed to run.\n",
2521 inf->default_thread_run_sc == 0 ? "are" : "aren't");
2525 set_thread_default_detach_sc_cmd (char *args, int from_tty)
2527 cur_inf ()->default_thread_detach_sc =
2528 parse_int_arg (args, "set thread default detach-suspend-count");
2532 show_thread_default_detach_sc_cmd (char *args, int from_tty)
2534 check_empty (args, "show thread default detach-suspend-count");
2535 printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
2536 cur_inf ()->default_thread_detach_sc);
2539 /* Steal a send right called NAME in the inferior task, and make it PROC's
2540 saved exception port. */
2542 steal_exc_port (struct proc *proc, mach_port_t name)
2546 mach_msg_type_name_t port_type;
2548 if (!proc || !proc->inf->task)
2549 error ("No inferior task.");
2551 err = mach_port_extract_right (proc->inf->task->port,
2552 name, MACH_MSG_TYPE_COPY_SEND,
2555 error ("Couldn't extract send right %d from inferior: %s",
2556 name, strerror (err));
2558 if (proc->saved_exc_port)
2559 /* Get rid of our reference to the old one. */
2560 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2562 proc->saved_exc_port = port;
2564 if (! proc->exc_port)
2565 /* If PROC is a thread, we may not have set its exception port before.
2566 We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2568 proc->exc_port = proc->inf->event_port;
2569 err = proc_set_exception_port (proc, proc->exc_port);
2570 error ("Can't set exception port for %s: %s",
2571 proc_string (proc), strerror (err));
2576 set_task_exc_port_cmd (char *args, int from_tty)
2578 struct inf *inf = cur_inf ();
2580 error ("No argument to \"set task exception-port\" command.");
2581 steal_exc_port (inf->task, parse_and_eval_address (args));
2585 set_stopped_cmd (char *args, int from_tty)
2587 cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2591 show_stopped_cmd (char *args, int from_tty)
2593 struct inf *inf = active_inf ();
2594 check_empty (args, "show stopped");
2595 printf_unfiltered ("The inferior process %s stopped.\n",
2596 inf->stopped ? "is" : "isn't");
2600 set_sig_thread_cmd (char *args, int from_tty)
2603 struct inf *inf = cur_inf ();
2605 if (!args || (! isdigit (*args) && strcmp (args, "none") != 0))
2606 error ("Illegal argument to \"set signal-thread\" command.\n"
2607 "Should be an integer thread ID, or `none'.");
2609 if (strcmp (args, "none") == 0)
2610 inf->signal_thread = 0;
2613 int tid = thread_id_to_pid (atoi (args));
2615 error ("Thread ID %s not known. Use the \"info threads\" command to\n"
2616 "see the IDs of currently known threads.", args);
2617 inf->signal_thread = inf_tid_to_thread (inf, tid);
2622 show_sig_thread_cmd (char *args, int from_tty)
2624 struct inf *inf = active_inf ();
2625 check_empty (args, "show signal-thread");
2626 if (inf->signal_thread)
2627 printf_unfiltered ("The signal thread is %s.\n",
2628 proc_string (inf->signal_thread));
2630 printf_unfiltered ("There is no signal thread.\n");
2634 set_signals_cmd (char *args, int from_tty)
2637 struct inf *inf = cur_inf ();
2639 inf->want_signals = parse_bool_arg (args, "set signals");
2641 if (inf->task && inf->want_signals != inf->traced)
2642 /* Make this take effect immediately in a running process. */
2643 inf_set_traced (inf, inf->want_signals);
2647 show_signals_cmd (char *args, int from_tty)
2649 struct inf *inf = cur_inf ();
2650 check_empty (args, "show signals");
2651 printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2653 ? (inf->traced ? "are" : "aren't")
2654 : (inf->want_signals ? "will be" : "won't be"));
2658 set_exceptions_cmd (char *args, int from_tty)
2660 struct inf *inf = cur_inf ();
2661 int val = parse_bool_arg (args, "set exceptions");
2663 if (inf->task && inf->want_exceptions != val)
2664 /* Make this take effect immediately in a running process. */
2667 inf->want_exceptions = val;
2671 show_exceptions_cmd (char *args, int from_tty)
2673 struct inf *inf = cur_inf ();
2674 check_empty (args, "show exceptions");
2675 printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2677 ? (inf->want_exceptions ? "are" : "aren't")
2678 : (inf->want_exceptions ? "will be" : "won't be"));
2682 set_task_cmd (char *args, int from_tty)
2684 printf_unfiltered ("\"set task\" must be followed by the name of a task property.\n");
2688 show_task_cmd (char *args, int from_tty)
2690 struct inf *inf = cur_inf ();
2692 check_empty (args, "show task");
2694 show_signals_cmd (0, from_tty);
2695 show_exceptions_cmd (0, from_tty);
2696 show_task_pause_cmd (0, from_tty);
2698 if (inf->pause_sc == 0)
2699 show_thread_default_pause_cmd (0, from_tty);
2700 show_thread_default_run_cmd (0, from_tty);
2704 show_stopped_cmd (0, from_tty);
2705 show_sig_thread_cmd (0, from_tty);
2708 if (inf->detach_sc != 0)
2709 show_task_detach_sc_cmd (0, from_tty);
2710 if (inf->default_thread_detach_sc != 0)
2711 show_thread_default_detach_sc_cmd (0, from_tty);
2715 set_noninvasive_cmd (char *args, int from_tty)
2717 /* Invert the sense of the arg for each component. */
2718 char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on";
2720 set_task_pause_cmd (inv_args, from_tty);
2721 set_signals_cmd (inv_args, from_tty);
2722 set_exceptions_cmd (inv_args, from_tty);
2726 info_port_rights (char *args, mach_port_type_t only)
2728 struct inf *inf = active_inf ();
2729 value_ptr vmark = value_mark ();
2732 /* Explicit list of port rights. */
2736 value_ptr val = parse_to_comma_and_eval (&args);
2737 long right = value_as_long (val);
2739 print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
2742 error ("%ld: %s.", right, strerror (err));
2746 /* Print all of them. */
2749 print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
2752 error ("%s.", strerror (err));
2755 value_free_to_mark (vmark);
2759 info_send_rights_cmd (char *args, int from_tty)
2761 info_port_rights (args, MACH_PORT_TYPE_SEND);
2764 info_recv_rights_cmd (char *args, int from_tty)
2766 info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
2769 info_port_sets_cmd (char *args, int from_tty)
2771 info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
2774 info_dead_names_cmd (char *args, int from_tty)
2776 info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
2779 info_port_rights_cmd (char *args, int from_tty)
2781 info_port_rights (args, ~0);
2784 static void add_task_commands ()
2786 add_cmd ("pause", class_run, set_thread_default_pause_cmd,
2787 "Set whether the new threads are suspended while gdb has control.\n"
2788 "This property normally has no effect because the whole task is\n"
2789 "suspended, however, that may be disabled with \"set task pause off\".\n"
2790 "The default value is \"off\".",
2791 &set_thread_default_cmd_list);
2792 add_cmd ("pause", no_class, show_thread_default_pause_cmd,
2793 "Show whether new threads are suspended while gdb has control.",
2794 &show_thread_default_cmd_list);
2795 add_cmd ("run", class_run, set_thread_default_run_cmd,
2796 "Set whether new threads are allowed to run (once gdb has noticed them).",
2797 &set_thread_default_cmd_list);
2798 add_cmd ("run", no_class, show_thread_default_run_cmd,
2799 "Show whether new threads are allowed to run (once gdb has noticed
2801 &show_thread_default_cmd_list);
2802 add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
2803 "Set the default detach-suspend-count value for new threads.",
2804 &set_thread_default_cmd_list);
2805 add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
2806 "Show the default detach-suspend-count value for new threads.",
2807 &show_thread_default_cmd_list);
2809 add_cmd ("signals", class_run, set_signals_cmd,
2810 "Set whether the inferior process's signals will be intercepted.\n"
2811 "Mach exceptions (such as breakpoint traps) are not affected.",
2813 add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
2814 add_cmd ("signals", no_class, show_signals_cmd,
2815 "Show whether the inferior process's signals will be intercepted.",
2817 add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
2819 add_cmd ("signal-thread", class_run, set_sig_thread_cmd,
2820 "Set the thread that gdb thinks is the libc signal thread.\n"
2821 "This thread is run when delivering a signal to a non-stopped process.",
2823 add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
2824 add_cmd ("signal-thread", no_class, show_sig_thread_cmd,
2825 "Set the thread that gdb thinks is the libc signal thread.",
2827 add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
2829 add_cmd ("stopped", class_run, set_stopped_cmd,
2830 "Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n"
2831 "Stopped process will be continued by sending them a signal.",
2833 add_cmd ("stopped", no_class, show_signals_cmd,
2834 "Show whether gdb thinks the inferior process is stopped as with SIGSTOP.",
2837 add_cmd ("exceptions", class_run, set_exceptions_cmd,
2838 "Set whether exceptions in the inferior process will be trapped.\n"
2839 "When exceptions are turned off, neither breakpoints nor single-stepping\n"
2842 /* Allow `set exc' despite conflict with `set exception-port'. */
2843 add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
2844 add_cmd ("exceptions", no_class, show_exceptions_cmd,
2845 "Show whether exceptions in the inferior process will be trapped.",
2848 add_prefix_cmd ("task", no_class, set_task_cmd,
2849 "Command prefix for setting task attributes.",
2850 &set_task_cmd_list, "set task ", 0, &setlist);
2851 add_prefix_cmd ("task", no_class, show_task_cmd,
2852 "Command prefix for showing task attributes.",
2853 &show_task_cmd_list, "show task ", 0, &showlist);
2855 add_cmd ("pause", class_run, set_task_pause_cmd,
2856 "Set whether the task is suspended while gdb has control.\n"
2857 "A value of \"on\" takes effect immediately, otherwise nothing\n"
2858 "happens until the next time the program is continued.\n"
2859 "When setting this to \"off\", \"set thread default pause on\"\n"
2860 "can be used to pause individual threads by default instead.",
2861 &set_task_cmd_list);
2862 add_cmd ("pause", no_class, show_task_pause_cmd,
2863 "Show whether the task is suspended while gdb has control.",
2864 &show_task_cmd_list);
2865 add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
2866 "Set the suspend count will leave on the thread when detaching.",
2867 &set_task_cmd_list);
2868 add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
2869 "Show the suspend count will leave on the thread when detaching.",
2870 &show_task_cmd_list);
2872 add_cmd ("exception-port", no_class, set_task_exc_port_cmd,
2873 "Set the task exception port to which we forward exceptions.\n"
2874 "The argument should be the value of the send right in the task.",
2875 &set_task_cmd_list);
2876 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
2877 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_task_cmd_list);
2879 /* A convenient way of turning on all options require to noninvasively
2880 debug running tasks. */
2881 add_cmd ("noninvasive", no_class, set_noninvasive_cmd,
2882 "Set task options so that we interfere as little as possible.\n"
2883 "This is the same as setting `task pause', `exceptions', and"
2884 "`signals' to the opposite value.",
2887 /* Commands to show information about the task's ports. */
2888 add_cmd ("send-rights", class_info, info_send_rights_cmd,
2889 "Show information about the task's send rights",
2891 add_cmd ("receive-rights", class_info, info_recv_rights_cmd,
2892 "Show information about the task's receive rights",
2894 add_cmd ("port-rights", class_info, info_send_rights_cmd,
2895 "Show information about the task's port rights",
2897 add_cmd ("port-sets", class_info, info_port_sets_cmd,
2898 "Show information about the task's port sets",
2900 add_cmd ("dead-names", class_info, info_dead_names_cmd,
2901 "Show information about the task's dead names",
2903 add_info_alias ("ports", "port-rights", 1);
2904 add_info_alias ("port", "port-rights", 1);
2905 add_info_alias ("psets", "port-sets", 1);
2908 /* User thread commands. */
2910 extern struct cmd_list_element *set_thread_cmd_list;
2911 extern struct cmd_list_element *show_thread_cmd_list;
2912 extern struct cmd_list_element *thread_cmd_list;
2915 set_thread_pause_cmd (char *args, int from_tty)
2917 struct proc *thread = cur_thread ();
2918 int old_sc = thread->pause_sc;
2919 thread->pause_sc = parse_bool_arg (args, "set thread pause");
2920 if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
2921 /* If the task is currently unsuspended, immediately suspend it,
2922 otherwise wait until the next time it gets control. */
2923 inf_suspend (thread->inf);
2927 show_thread_pause_cmd (char *args, int from_tty)
2929 struct proc *thread = cur_thread ();
2930 int sc = thread->pause_sc;
2931 check_empty (args, "show task pause");
2932 printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
2933 proc_string (thread),
2934 sc ? "is" : "isn't",
2935 !sc && thread->inf->pause_sc ? " (but the task is)" : "");
2939 set_thread_run_cmd (char *args, int from_tty)
2941 struct proc *thread = cur_thread ();
2942 thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
2946 show_thread_run_cmd (char *args, int from_tty)
2948 struct proc *thread = cur_thread ();
2949 check_empty (args, "show thread run");
2950 printf_unfiltered ("Thread %s %s allowed to run.",
2951 proc_string (thread),
2952 thread->run_sc == 0 ? "is" : "isn't");
2956 set_thread_detach_sc_cmd (char *args, int from_tty)
2958 cur_thread ()->detach_sc = parse_int_arg (args, "set thread detach-suspend-count");
2962 show_thread_detach_sc_cmd (char *args, int from_tty)
2964 struct proc *thread = cur_thread ();
2965 check_empty (args, "show thread detach-suspend-count");
2966 printf_unfiltered ("Thread %s will be left with a suspend count of %d when detaching.\n",
2967 proc_string (thread),
2972 set_thread_exc_port_cmd (char *args, int from_tty)
2974 struct proc *thread = cur_thread ();
2976 error ("No argument to \"set thread exception-port\" command.");
2977 steal_exc_port (thread, parse_and_eval_address (args));
2981 set_thread_cmd (char *args, int from_tty)
2983 printf_unfiltered ("\"set thread\" must be followed by the name of a thread property.\n");
2987 show_thread_cmd (char *args, int from_tty)
2989 struct proc *thread = cur_thread ();
2990 check_empty (args, "show thread");
2991 show_thread_run_cmd (0, from_tty);
2992 show_thread_pause_cmd (0, from_tty);
2993 if (thread->detach_sc != 0)
2994 show_thread_detach_sc_cmd (0, from_tty);
2998 thread_takeover_sc_cmd (char *args, int from_tty)
3000 struct proc *thread = cur_thread ();
3001 thread_basic_info_data_t _info;
3002 thread_basic_info_t info = &_info;
3003 mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
3005 thread_info (thread->port, THREAD_BASIC_INFO, (int *)&info, &info_len);
3007 error ("%s.", strerror (err));
3008 thread->sc = info->suspend_count;
3010 printf_unfiltered ("Suspend count was %d.\n", thread->sc);
3012 vm_deallocate (mach_task_self (), (vm_address_t)info, info_len * sizeof (int));
3015 add_thread_commands ()
3017 add_cmd ("pause", class_run, set_thread_pause_cmd,
3018 "Set whether the current thread is suspended while gdb has control.\n"
3019 "A value of \"on\" takes effect immediately, otherwise nothing\n"
3020 "happens until the next time the program is continued. This\n"
3021 "property normally has no effect because the whole task is suspended,\n"
3022 "however, that may be disabled with \"set task pause off\".\n"
3023 "The default value is \"off\".",
3024 &set_thread_cmd_list);
3025 add_cmd ("pause", no_class, show_thread_pause_cmd,
3026 "Show whether the current thread is suspended while gdb has control.",
3027 &show_thread_cmd_list);
3029 add_cmd ("run", class_run, set_thread_run_cmd,
3030 "Set whether the current thread is allowed to run.",
3031 &set_thread_cmd_list);
3032 add_cmd ("run", no_class, show_thread_run_cmd,
3033 "Show whether the current thread is allowed to run.",
3034 &show_thread_cmd_list);
3036 add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd,
3037 "Set the suspend count will leave on the thread when detaching.\n"
3038 "Note that this is relative to suspend count when gdb noticed the thread;\n"
3039 "use the `thread takeover-suspend-count' to force it to an absolute value.",
3040 &set_thread_cmd_list);
3041 add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd,
3042 "Show the suspend count will leave on the thread when detaching."
3043 "Note that this is relative to suspend count when gdb noticed the thread;\n"
3044 "use the `thread takeover-suspend-count' to force it to an absolute value.",
3045 &show_thread_cmd_list);
3047 add_cmd ("exception-port", no_class, set_thread_exc_port_cmd,
3048 "Set the exception port to which we forward exceptions for the\n"
3049 "current thread, overriding the task exception port.\n"
3050 "The argument should be the value of the send right in the task.",
3051 &set_thread_cmd_list);
3052 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
3053 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_thread_cmd_list);
3055 add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd,
3056 "Force the threads absolute suspend-count to be gdb's.\n"
3057 "Prior to giving this command, gdb's thread suspend-counts are relative to\n"
3058 "the thread's initial suspend-count when gdb notices the threads.",
3063 _initialize_gnu_nat ()
3065 proc_server = getproc ();
3067 add_target (&gnu_ops);
3069 add_task_commands ();
3070 add_thread_commands ();
3072 #if MAINTENANCE_CMDS
3073 add_set_cmd ("gnu-debug", class_maintenance,
3074 var_boolean, (char *)&gnu_debug_flag,
3075 "Set debugging output for the gnu backend.", &maintenancelist);
3079 #ifdef FLUSH_INFERIOR_CACHE
3081 /* When over-writing code on some machines the I-Cache must be flushed
3082 explicitly, because it is not kept coherent by the lazy hardware.
3083 This definitely includes breakpoints, for instance, or else we
3084 end up looping in mysterious Bpt traps */
3087 flush_inferior_icache(pc, amount)
3090 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
3093 ret = vm_machine_attribute (current_inferior->task->port,
3098 if (ret != KERN_SUCCESS)
3099 warning ("Error flushing inferior's cache : %s", strerror (ret));
3101 #endif FLUSH_INFERIOR_CACHE