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>
64 #include "exc_request_S.h"
66 #include "process_reply_S.h"
67 #include "msg_reply_S.h"
69 #include "exc_request_U.h"
72 static process_t proc_server = MACH_PORT_NULL;
74 /* If we've sent a proc_wait_request to the proc server, the pid of the
75 process we asked about. We can only ever have one outstanding. */
76 int proc_wait_pid = 0;
78 /* The number of wait requests we've sent, and expect replies from. */
79 int proc_waits_pending = 0;
81 int gnu_debug_flag = 0;
85 extern struct target_ops gnu_ops;
87 struct inf *make_inf ();
88 void inf_clear_wait (struct inf *inf);
89 void inf_cleanup (struct inf *inf);
90 void inf_startup (struct inf *inf, int pid, task_t task);
91 int inf_update_suspends (struct inf *inf);
92 void inf_set_task (struct inf *inf, task_t port);
93 void inf_validate_procs (struct inf *inf);
94 void inf_steal_exc_ports (struct inf *inf);
95 void inf_restore_exc_ports (struct inf *inf);
96 int inf_update_procs (struct inf *inf);
97 struct proc *inf_tid_to_proc (struct inf *inf, int tid);
98 inline void inf_set_threads_resume_sc (struct inf *inf, struct proc
99 *run_thread, int run_others);
100 inline int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
101 inline void inf_suspend (struct inf *inf);
102 inline void inf_resume (struct inf *inf);
103 void inf_set_step_thread (struct inf *inf, struct proc *proc);
104 void inf_detach (struct inf *inf);
105 void inf_attach (struct inf *inf, int pid);
106 void inf_signal (struct inf *inf, enum target_signal sig);
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 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
113 struct proc *_proc_free (struct proc *proc);
114 int proc_update_sc (struct proc *proc);
115 void proc_abort (struct proc *proc, int force);
116 thread_state_t proc_get_state (struct proc *proc, int force);
117 error_t proc_get_exception_port (struct proc *proc, mach_port_t *port);
118 error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
119 static mach_port_t _proc_get_exc_port (struct proc *proc);
120 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
121 void proc_restore_exc_port (struct proc *proc);
122 int proc_trace (struct proc *proc, int set);
123 char *proc_string (struct proc *proc);
125 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
126 to INF's msg port and task port respectively. If it has no msg port,
127 EIEIO is returned. INF must refer to a running process! */
128 #define INF_MSGPORT_RPC(inf, rpc_expr) \
129 HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
130 (refport = inf->task->port, 0), 0, \
131 msgport ? (rpc_expr) : EIEIO)
133 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
134 there's someone around to deal with the RPC (and resuspend things
135 afterwards). This effects INF's threads' resume_sc count. */
136 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
137 (inf_set_threads_resume_sc_for_signal_thread (inf) \
140 __e = INF_MSGPORT_RPC (inf, rpc_expr); \
145 #define MIG_SERVER_DIED EMIG_SERVER_DIED /* XXX */
147 /* The state passed by an exception message. */
150 int exception; /* The exception code */
152 mach_port_t handler; /* The real exception port to handle this. */
153 mach_port_t reply; /* The reply port from the exception call. */
156 /* The results of the last wait an inf did. */
159 struct target_waitstatus status; /* The status returned to gdb. */
160 struct exc_state exc; /* The exception that caused us to return. */
161 struct proc *thread; /* The thread in question. */
162 int suppress; /* Something trivial happened. */
165 /* The state of an inferior. */
168 /* Fields describing the current inferior. */
170 struct proc *task; /* The mach task. */
171 struct proc *threads; /* A linked list of all threads in TASK. */
173 /* True if THREADS needn't be validated by querying the task. We assume that
174 we and the task in question are the only ones frobbing the thread list,
175 so as long as we don't let any code run, we don't have to worry about
177 int threads_up_to_date;
179 pid_t pid; /* The real system PID. */
181 struct inf_wait wait; /* What to return from target_wait. */
183 /* One thread proc in INF may be in `single-stepping mode'. This is it. */
184 struct proc *step_thread;
186 /* The thread we think is the signal thread. */
187 struct proc *signal_thread;
189 mach_port_t event_port; /* Where we receive various msgs. */
191 /* True if we think at least one thread in the inferior could currently be
195 /* True if the process has stopped (in the proc server sense). Note that
196 since a proc server `stop' leaves the signal thread running, the inf can
197 be RUNNING && STOPPED... */
200 /* True if the inferior is traced. */
203 /* True if we shouldn't try waiting for the inferior, usually because we
204 can't for some reason. */
207 /* When starting a new inferior, we don't try to validate threads until all
208 the proper execs have been done. This is a count of how many execs we
210 unsigned pending_execs;
212 /* Fields describing global state */
214 /* The task suspend count used when gdb has control. This is normally 1 to
215 make things easier for us, but sometimes (like when attaching to vital
216 system servers) it may be desirable to let the task continue to run
217 (pausing individual threads as necessary). */
220 /* The initial values used for the run_sc and pause_sc of newly discovered
221 threads -- see the definition of those fields in struct proc. */
222 int default_thread_run_sc;
223 int default_thread_pause_sc;
225 /* True if the process should be traced when started/attached. Newly
226 started processes *must* be traced at first to exec them properly, but
227 if this is false, tracing is turned off as soon it has done so. */
230 /* True if exceptions from the inferior process should be trapped. This
231 must be on to use breakpoints. */
235 int __proc_pid (struct proc *proc)
237 return proc->inf->pid;
240 /* Update PROC's real suspend count to match it's desired one. Returns true
241 if we think PROC is now in a runnable state. */
243 proc_update_sc (struct proc *proc)
247 int delta = proc->sc - proc->cur_sc;
250 proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
252 if (proc->sc == 0 && proc->state_changed)
253 /* Since PROC may start running, we must write back any state changes. */
255 assert (proc_is_thread (proc));
256 proc_debug (proc, "storing back changed thread state");
257 err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
258 &proc->state, THREAD_STATE_SIZE);
260 proc->state_changed = 0;
264 while (delta-- > 0 && !err)
265 if (proc_is_task (proc))
266 err = task_suspend (proc->port);
268 err = thread_suspend (proc->port);
270 while (delta++ < 0 && !err)
271 if (proc_is_task (proc))
272 err = task_resume (proc->port);
274 err = thread_resume (proc->port);
277 proc->cur_sc = proc->sc;
279 /* If we got an error, then the task/thread has disappeared. */
280 running = !err && proc->sc == 0;
282 proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
284 proc_debug (proc, "err = %s", strerror (err));
289 proc->state_valid = proc->state_changed = 0;
290 proc->fetched_regs = 0;
296 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
297 If PROC is deemed `precious', then nothing is done unless FORCE is true.
298 In particular, a thread is precious if it's running (in which case forcing
299 it includes suspending it first), or if it has an exception pending. */
301 proc_abort (struct proc *proc, int force)
303 assert (proc_is_thread (proc));
307 struct inf *inf = proc->inf;
308 int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
310 if (running && force)
313 inf_update_suspends (proc->inf);
315 warning ("Stopped %s.", proc_string (proc));
317 else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
318 /* An exception is pending on PROC, which don't mess with. */
322 /* We only abort the thread if it's not actually running. */
324 thread_abort (proc->port);
325 proc_debug (proc, "aborted");
329 proc_debug (proc, "not aborting");
333 /* Make sure that the state field in PROC is up to date, and return a pointer
334 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
335 that the thread is stopped and aborted first, and sets the state_changed
336 field in PROC to true. */
338 proc_get_state (struct proc *proc, int will_modify)
340 int was_aborted = proc->aborted;
342 proc_debug (proc, "updating state info%s",
343 will_modify ? " (with intention to modify)" : "");
345 proc_abort (proc, will_modify);
347 if (! was_aborted && proc->aborted)
348 /* PROC's state may have changed since we last fetched it. */
349 proc->state_valid = 0;
351 if (! proc->state_valid)
353 mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
355 thread_get_state (proc->port, THREAD_STATE_FLAVOR,
356 &proc->state, &state_size);
357 proc_debug (proc, "getting thread state");
358 proc->state_valid = !err;
361 if (proc->state_valid)
364 proc->state_changed = 1;
372 proc_get_exception_port (struct proc *proc, mach_port_t *port)
374 if (proc_is_task (proc))
375 return task_get_exception_port (proc->port, port);
377 return thread_get_exception_port (proc->port, port);
381 proc_set_exception_port (struct proc *proc, mach_port_t port)
383 proc_debug (proc, "setting exception port: %d", port);
384 if (proc_is_task (proc))
385 return task_set_exception_port (proc->port, port);
387 return thread_set_exception_port (proc->port, port);
390 /* Get PROC's exception port, cleaning up a bit if proc has died. */
392 _proc_get_exc_port (struct proc *proc)
394 mach_port_t exc_port;
395 error_t err = proc_get_exception_port (proc, &exc_port);
398 /* PROC must be dead. */
401 mach_port_deallocate (mach_task_self (), proc->exc_port);
402 proc->exc_port = MACH_PORT_NULL;
403 if (proc->saved_exc_port)
404 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
405 proc->saved_exc_port = MACH_PORT_NULL;
411 /* Replace PROC's exception port with EXC_PORT, unless it's already been
412 done. Stash away any existing exception port so we can restore it later. */
414 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
416 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
422 proc_debug (proc, "inserting exception port: %d", exc_port);
424 if (cur_exc_port != exc_port)
425 /* Put in our exception port. */
426 err = proc_set_exception_port (proc, exc_port);
428 if (err || cur_exc_port == proc->exc_port)
429 /* We previously set the exception port, and it's still set. So we
430 just keep the old saved port which is what the proc set. */
433 mach_port_deallocate (mach_task_self (), cur_exc_port);
436 /* Keep a copy of PROC's old exception port so it can be restored. */
438 if (proc->saved_exc_port)
439 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
440 proc->saved_exc_port = cur_exc_port;
443 proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
446 proc->exc_port = exc_port;
448 warning ("Error setting exception port for %s: %s",
449 proc_string (proc), strerror (err));
453 /* If we previously replaced PROC's exception port, put back what we found
454 there at the time, unless *our* exception port has since be overwritten,
455 in which case who knows what's going on. */
457 proc_restore_exc_port (struct proc *proc)
459 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
465 proc_debug (proc, "restoring real exception port");
467 if (proc->exc_port == cur_exc_port)
468 /* Our's is still there. */
469 err = proc_set_exception_port (proc, proc->saved_exc_port);
471 if (proc->saved_exc_port)
472 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
473 proc->saved_exc_port = MACH_PORT_NULL;
476 proc->exc_port = MACH_PORT_NULL;
478 warning ("Error setting exception port for %s: %s",
479 proc_string (proc), strerror (err));
483 /* Turns hardware tracing in PROC on or off when SET is true or fals,
484 respectively. Returns true on success. */
486 proc_trace (struct proc *proc, int set)
488 thread_state_t state = proc_get_state (proc, 1);
491 return 0; /* the thread must be dead. */
493 proc_debug (proc, "tracing %s", set ? "on" : "off");
497 /* XXX We don't get the exception unless the thread has its own
498 exception port???? */
499 if (proc->exc_port == MACH_PORT_NULL)
500 proc_steal_exc_port (proc, proc->inf->event_port);
501 THREAD_STATE_SET_TRACED (state);
504 THREAD_STATE_CLEAR_TRACED (state);
509 /* A variable from which to assign new TIDs. */
510 static int next_thread_id = 1;
512 /* Returns a new proc structure with the given fields. Also adds a
513 notification for PORT becoming dead to be sent to INF's notify port. */
515 make_proc (struct inf *inf, mach_port_t port, int tid)
518 mach_port_t prev_port = MACH_PORT_NULL;
519 struct proc *proc = malloc (sizeof (struct proc));
525 proc->saved_exc_port = MACH_PORT_NULL;
526 proc->exc_port = MACH_PORT_NULL;
529 proc->run_sc = inf->default_thread_run_sc;
530 proc->pause_sc = inf->default_thread_pause_sc;
531 proc->resume_sc = proc->run_sc;
533 proc->state_valid = 0;
534 proc->state_changed = 0;
536 proc_debug (proc, "is new");
538 /* Get notified when things die. */
540 mach_port_request_notification (mach_task_self(), port,
541 MACH_NOTIFY_DEAD_NAME, 1,
543 MACH_MSG_TYPE_MAKE_SEND_ONCE,
546 warning ("Couldn't request notification for port %d: %s",
547 port, strerror (err));
550 proc_debug (proc, "notifications to: %d", inf->event_port);
551 if (prev_port != MACH_PORT_NULL)
552 mach_port_deallocate (mach_task_self (), prev_port);
555 if (inf->want_exceptions)
556 if (proc_is_task (proc))
557 /* Make the task exception port point to us. */
558 proc_steal_exc_port (proc, inf->event_port);
560 /* Just clear thread exception ports -- they default to the task one. */
561 proc_steal_exc_port (proc, MACH_PORT_NULL);
566 /* Frees PROC and any resources it uses, and returns the value of PROC's next
569 _proc_free (struct proc *proc)
571 struct inf *inf = proc->inf;
572 struct proc *next = proc->next;
574 proc_debug (proc, "freeing...");
576 if (proc == inf->step_thread)
577 /* Turn off single stepping. */
578 inf_set_step_thread (inf, 0);
579 if (proc == inf->wait.thread)
580 inf_clear_wait (inf);
581 if (proc == inf->signal_thread)
582 inf->signal_thread = 0;
584 if (proc->port != MACH_PORT_NULL)
586 if (proc->exc_port != MACH_PORT_NULL)
587 /* Restore the original exception port. */
588 proc_restore_exc_port (proc);
589 if (proc->cur_sc != 0)
590 /* Resume the thread/task. */
593 proc_update_sc (proc);
595 mach_port_deallocate (mach_task_self (), proc->port);
602 struct inf *make_inf ()
604 struct inf *inf = malloc (sizeof (struct inf));
611 inf->threads_up_to_date = 0;
613 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
614 inf->wait.thread = 0;
615 inf->wait.exc.handler = MACH_PORT_NULL;
616 inf->wait.exc.reply = MACH_PORT_NULL;
617 inf->step_thread = 0;
618 inf->signal_thread = 0;
619 inf->event_port = MACH_PORT_NULL;
624 inf->pending_execs = 0;
626 inf->default_thread_run_sc = 0;
627 inf->default_thread_pause_sc = 0;
628 inf->want_signals = 1; /* By default */
629 inf->want_exceptions = 1; /* By default */
635 inf_clear_wait (struct inf *inf)
637 inf_debug (inf, "clearing wait");
638 inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
639 inf->wait.thread = 0;
640 inf->wait.suppress = 0;
641 if (inf->wait.exc.handler != MACH_PORT_NULL)
643 mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
644 inf->wait.exc.handler = MACH_PORT_NULL;
646 if (inf->wait.exc.reply != MACH_PORT_NULL)
648 mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
649 inf->wait.exc.reply = MACH_PORT_NULL;
654 inf_cleanup (struct inf *inf)
656 inf_debug (inf, "cleanup");
658 inf_clear_wait (inf);
660 inf_set_task (inf, MACH_PORT_NULL);
666 inf->pending_execs = 0;
670 mach_port_destroy (mach_task_self (), inf->event_port);
671 inf->event_port = MACH_PORT_NULL;
676 inf_startup (struct inf *inf, int pid, task_t task)
680 inf_debug (inf, "startup: pid = %d, task = %d", pid, task);
684 /* Make the port on which we receive all events. */
685 err = mach_port_allocate (mach_task_self (),
686 MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
688 error ("Error allocating event port: %s", strerror (err));
690 /* Make a send right for it, so we can easily copy it for other people. */
691 mach_port_insert_right (mach_task_self (), inf->event_port,
692 inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
697 inf_set_task (inf, task);
703 inf->task->sc = inf->task->cur_sc = 1; /* Reflect task_suspend above */
708 inf_set_task (struct inf *inf, mach_port_t port)
710 struct proc *task = inf->task;
712 inf_debug (inf, "setting task: %d", port);
714 if (task && task->port != port)
717 inf_validate_procs (inf); /* Trash all the threads. */
718 _proc_free (task); /* And the task. */
721 if (port != MACH_PORT_NULL)
723 inf->task = make_proc (inf, port, PROC_TID_TASK);
724 inf->threads_up_to_date = 0;
728 /* Validates INF's stopped field from the actual proc server state. */
730 inf_validate_stopped (struct inf *inf)
733 mach_msg_type_number_t noise_len = 0;
735 mach_msg_type_number_t pi_len = 0;
737 proc_getprocinfo (proc_server, inf->pid, 0,
738 (procinfo_t *)&pi, &pi_len, &noise, &noise_len);
742 inf->stopped = !!(pi->state & PI_STOPPED);
743 vm_deallocate (mach_task_self (), (vm_address_t)pi, pi_len);
745 vm_deallocate (mach_task_self (), (vm_address_t)noise, noise_len);
749 /* Validates INF's task suspend count. */
751 inf_validate_task_sc (struct inf *inf)
753 struct task_basic_info info;
754 mach_msg_type_number_t info_len = TASK_BASIC_INFO_COUNT;
755 error_t err = task_info (inf->task->port, TASK_BASIC_INFO, &info, &info_len);
758 if (inf->task->cur_sc < info.suspend_count)
759 warning ("Pid %d is suspended; continuing will clear existing suspend count.", inf->pid);
760 inf->task->cur_sc = info.suspend_count;
764 /* Turns tracing for INF on or off, depending on ON, unless it already is.
765 If INF is running, the resume_sc count of INF's threads will be modified,
766 and the signal thread will briefly be run to change the trace state. */
768 inf_set_traced (struct inf *inf, int on)
770 if (on != inf->traced)
772 /* Make it take effect immediately. */
774 error_t (*f)(mach_port_t, mach_port_t, int) =
775 on ? msg_set_some_exec_flags : msg_clear_some_exec_flags;
777 INF_RESUME_MSGPORT_RPC (inf, (*f)(msgport, refport, EXEC_TRACED));
779 warning ("Can't modify tracing state for pid %d: No signal thread",
782 warning ("Can't modify tracing state for pid %d: %s",
783 inf->pid, strerror (err));
791 /* Makes all the real suspend count deltas of all the procs in INF match the
792 desired values. Careful to always do thread/task suspend counts in the
793 safe order. Returns true if at least one thread is thought to be running.*/
795 inf_update_suspends (struct inf *inf)
797 struct proc *task = inf->task;
798 /* We don't have to update INF->threads even though we're iterating over it
799 because we'll change a thread only if it already has an existing proc
802 inf_debug (inf, "updating suspend counts");
807 int task_running = (task->sc == 0), thread_running = 0;
809 if (task->sc > task->cur_sc)
810 /* The task is becoming _more_ suspended; do before any threads. */
811 task_running = proc_update_sc (task);
813 if (inf->pending_execs)
814 /* When we're waiting for an exec, things may be happening behind our
815 back, so be conservative. */
818 /* Do all the thread suspend counts. */
819 for (thread = inf->threads; thread; thread = thread->next)
820 thread_running |= proc_update_sc (thread);
822 if (task->sc != task->cur_sc)
823 /* We didn't do the task first, because we wanted to wait for the
824 threads; do it now. */
825 task_running = proc_update_sc (task);
827 inf_debug (inf, "%srunning...",
828 (thread_running && task_running) ? "" : "not ");
830 inf->running = thread_running && task_running;
832 /* Once any thread has executed some code, we can't depend on the
833 threads list any more. */
835 inf->threads_up_to_date = 0;
843 /* Converts a GDB pid to a struct proc. */
845 inf_tid_to_thread (struct inf *inf, int tid)
847 struct proc *thread = inf->threads;
849 if (thread->tid == tid)
852 thread = thread->next;
856 /* Converts a thread port to a struct proc. */
858 inf_port_to_thread (struct inf *inf, mach_port_t port)
860 struct proc *thread = inf->threads;
862 if (thread->port == port)
865 thread = thread->next;
869 /* Make INF's list of threads be consistent with reality of TASK. */
871 inf_validate_procs (struct inf *inf)
874 thread_array_t threads;
875 unsigned num_threads;
876 struct proc *task = inf->task;
878 inf->threads_up_to_date = !inf->running;
882 error_t err = task_threads (task->port, &threads, &num_threads);
883 inf_debug (inf, "fetching threads");
885 /* TASK must be dead. */
887 task->port = MACH_PORT_NULL;
889 task = inf->task = 0;
896 inf_debug (inf, "no task");
900 unsigned search_start = 0; /* Make things normally linear. */
901 /* Which thread in PROCS corresponds to each task thread, & the task. */
902 struct proc *matched[num_threads + 1];
903 /* The last thread in INF->threads, so we can add to the end. */
904 struct proc *last = 0;
905 /* The current thread we're considering. */
906 struct proc *thread = inf->threads;
908 bzero (matched, sizeof (matched));
914 for (i = search_start, left = num_threads; left; i++, left--)
916 if (i >= num_threads)
917 i -= num_threads; /* I wrapped around. */
918 if (thread->port == threads[i])
919 /* We already know about this thread. */
923 thread = thread->next;
931 proc_debug (thread, "died!");
932 thread->port = MACH_PORT_NULL;
933 thread = _proc_free (thread); /* THREAD is dead. */
934 (last ? last->next : inf->threads) = thread;
938 for (i = 0; i < num_threads; i++)
940 /* Throw away the duplicate send right. */
941 mach_port_deallocate (mach_task_self (), threads[i]);
943 /* THREADS[I] is a thread we don't know about yet! */
945 thread = make_proc (inf, threads[i], next_thread_id++);
946 (last ? last->next : inf->threads) = thread;
948 proc_debug (thread, "new thread: %d", threads[i]);
949 add_thread (thread->tid); /* Tell GDB's generic thread code. */
952 vm_deallocate(mach_task_self(),
953 (vm_address_t)threads, (num_threads * sizeof(thread_t)));
957 /* Makes sure that INF's thread list is synced with the actual process. */
959 inf_update_procs (struct inf *inf)
963 if (! inf->threads_up_to_date)
964 inf_validate_procs (inf);
968 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
969 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
972 inf_set_threads_resume_sc (struct inf *inf,
973 struct proc *run_thread, int run_others)
976 inf_update_procs (inf);
977 for (thread = inf->threads; thread; thread = thread->next)
978 if (thread == run_thread)
979 thread->resume_sc = 0;
981 thread->resume_sc = thread->run_sc;
983 thread->resume_sc = thread->pause_sc;
986 /* Cause INF to continue execution immediately; individual threads may still
987 be suspended (but their suspend counts will be updated). */
989 inf_resume (struct inf *inf)
993 inf_update_procs (inf);
995 for (thread = inf->threads; thread; thread = thread->next)
996 thread->sc = thread->resume_sc;
1001 inf_update_suspends (inf);
1004 /* Cause INF to stop execution immediately; individual threads may still
1007 inf_suspend (struct inf *inf)
1009 struct proc *thread;
1011 inf_update_procs (inf);
1013 for (thread = inf->threads; thread; thread = thread->next)
1014 thread->sc = thread->pause_sc;
1017 inf->task->sc = inf->pause_sc;
1019 inf_update_suspends (inf);
1022 /* INF has one thread PROC that is in single-stepping mode. This functions
1023 changes it to be PROC, changing any old step_thread to be a normal one. A
1024 PROC of 0 clears an any existing value. */
1026 inf_set_step_thread (struct inf *inf, struct proc *thread)
1028 assert (!thread || proc_is_thread (thread));
1031 inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1033 inf_debug (inf, "clearing step thread");
1035 if (inf->step_thread != thread)
1037 if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1038 if (! proc_trace (inf->step_thread, 0))
1040 if (thread && proc_trace (thread, 1))
1041 inf->step_thread = thread;
1043 inf->step_thread = 0;
1047 /* Set up the thread resume_sc's so that only the signal thread is running
1048 (plus whatever other thread are set to always run). Returns true if we
1049 did so, or false if we can't find a signal thread. */
1051 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1053 if (inf->signal_thread)
1055 inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1063 inf_update_signal_thread (struct inf *inf)
1065 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1066 the signal thread. */
1067 inf->signal_thread = inf->threads ? inf->threads->next : 0;
1070 /* Detachs from INF's inferior task, letting it run once again... */
1072 inf_detach (struct inf *inf)
1074 struct proc *task = inf->task;
1076 inf_debug (inf, "detaching...");
1078 inf_clear_wait (inf);
1079 inf_set_step_thread (inf, 0);
1083 struct proc *thread;
1085 inf_set_traced (inf, 0);
1087 inf_signal (inf, TARGET_SIGNAL_0);
1089 proc_restore_exc_port (task);
1092 for (thread = inf->threads; thread; thread = thread->next)
1094 proc_restore_exc_port (thread);
1098 inf_update_suspends (inf);
1104 /* Attaches INF to the process with process id PID, returning it in a suspended
1105 state suitable for debugging. */
1107 inf_attach (struct inf *inf, int pid)
1112 inf_debug (inf, "attaching: %d", pid);
1114 err = proc_pid2task (proc_server, pid, &task);
1116 error ("Error getting task for pid %d: %s", pid, strerror (err));
1121 inf_startup (inf, pid, task);
1124 /* Makes sure that we've got our exception ports entrenched in the process. */
1125 void inf_steal_exc_ports (struct inf *inf)
1127 struct proc *thread;
1129 inf_debug (inf, "stealing exception ports");
1131 inf_set_step_thread (inf, 0); /* The step thread is special. */
1133 proc_steal_exc_port (inf->task, inf->event_port);
1134 for (thread = inf->threads; thread; thread = thread->next)
1135 proc_steal_exc_port (thread, MACH_PORT_NULL);
1138 /* Makes sure the process has its own exception ports. */
1139 void inf_restore_exc_ports (struct inf *inf)
1141 struct proc *thread;
1143 inf_debug (inf, "restoring exception ports");
1145 inf_set_step_thread (inf, 0); /* The step thread is special. */
1147 proc_restore_exc_port (inf->task);
1148 for (thread = inf->threads; thread; thread = thread->next)
1149 proc_restore_exc_port (thread);
1152 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1153 signal 0, will continue it. INF is assumed to be in a paused state, and
1154 the resume_sc's of INF's threads may be affected. */
1156 inf_signal (struct inf *inf, enum target_signal sig)
1159 int host_sig = target_signal_to_host (sig);
1161 #define NAME target_signal_to_name (sig)
1163 if (host_sig >= _NSIG)
1164 /* A mach exception. Exceptions are encoded in the signal space by
1165 putting them after _NSIG; this assumes they're positive (and not
1166 extremely large)! */
1168 struct inf_wait *w = &inf->wait;
1169 if (w->status.kind == TARGET_WAITKIND_STOPPED
1170 && w->status.value.sig == sig
1171 && w->thread && !w->thread->aborted)
1172 /* We're passing through the last exception we received. This is
1173 kind of bogus, because exceptions are per-thread whereas gdb
1174 treats signals as per-process. We just forward the exception to
1175 the correct handler, even it's not for the same thread as TID --
1176 i.e., we pretend it's global. */
1178 struct exc_state *e = &w->exc;
1179 inf_debug (inf, "passing through exception:"
1180 " task = %d, thread = %d, exc = %d"
1181 ", code = %d, subcode = %d",
1182 w->thread->port, inf->task->port,
1183 e->exception, e->code, e->subcode);
1185 exception_raise_request (e->handler,
1186 e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1187 w->thread->port, inf->task->port,
1188 e->exception, e->code, e->subcode);
1191 warning ("Can't forward spontaneous exception (%s).", NAME);
1194 /* A Unix signal. */
1196 /* The process is stopped an expecting a signal. Just send off a
1197 request and let it get handled when we resume everything. */
1199 inf_debug (inf, "sending %s to stopped process", NAME);
1201 INF_MSGPORT_RPC (inf,
1202 msg_sig_post_untraced_request (msgport,
1204 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1208 /* Posting an untraced signal automatically continues it.
1209 We clear this here rather than when we get the reply
1210 because we'd rather assume it's not stopped when it
1211 actually is, than the reverse. */
1215 /* It's not expecting it. We have to let just the signal thread
1216 run, and wait for it to get into a reasonable state before we
1217 can continue the rest of the process. When we finally resume the
1218 process the signal we request will be the very first thing that
1221 inf_debug (inf, "sending %s to unstopped process (so resuming signal thread)", NAME);
1223 INF_RESUME_MSGPORT_RPC (inf,
1224 msg_sig_post_untraced (msgport,
1225 host_sig, refport));
1229 /* Can't do too much... */
1230 warning ("Can't deliver signal %s: No signal thread.", NAME);
1232 warning ("Delivering signal %s: %s", NAME, strerror (err));
1237 /* The inferior used for all gdb target ops. */
1238 struct inf *current_inferior = 0;
1240 /* The inferior being waited for by gnu_wait. Since GDB is decidely not
1241 multi-threaded, we don't bother to lock this. */
1242 struct inf *waiting_inf;
1244 /* Wait for something to happen in the inferior, returning what in STATUS. */
1246 gnu_wait (int tid, struct target_waitstatus *status)
1249 mach_msg_header_t hdr;
1250 mach_msg_type_t type;
1254 struct proc *thread;
1255 struct inf *inf = current_inferior;
1259 inf_debug (inf, "waiting for: %d", tid);
1262 if (proc_wait_pid != inf->pid && !inf->no_wait)
1263 /* Always get information on events from the proc server. */
1265 inf_debug (inf, "requesting wait on pid %d", inf->pid);
1268 /* The proc server is single-threaded, and only allows a single
1269 outstanding wait request, so we have to cancel the previous one. */
1271 inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1272 interrupt_operation (proc_server);
1276 proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1278 warning ("wait request failed: %s", strerror (err));
1281 inf_debug (inf, "waits pending: %d", proc_waits_pending);
1282 proc_wait_pid = inf->pid;
1283 /* Even if proc_waits_pending was > 0 before, we still won't get
1284 any other replies, because it was either from a different INF,
1285 or a different process attached to INF -- and the event port,
1286 which is the wait reply port, changes when you switch processes.*/
1287 proc_waits_pending = 1;
1291 inf_clear_wait (inf);
1293 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1294 (3) wait reply from the proc server. */
1296 inf_debug (inf, "waiting for an event...");
1297 err = _hurd_intr_rpc_mach_msg (&msg.hdr, MACH_RCV_MSG, 0,
1298 sizeof (struct msg),
1299 inf->event_port, MACH_PORT_NULL);
1301 /* Re-suspend the task. */
1305 inf_debug (inf, "interrupted");
1307 error ("Couldn't wait for an event: %s", strerror (err));
1311 mach_msg_header_t hdr;
1312 mach_msg_type_t err_type;
1317 inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1319 /* Handle what we got. */
1320 if (! notify_server (&msg.hdr, &reply.hdr)
1321 && ! exc_server (&msg.hdr, &reply.hdr)
1322 && ! process_reply_server (&msg.hdr, &reply.hdr)
1323 && ! msg_reply_server (&msg.hdr, &reply.hdr))
1324 /* Whatever it is, it's something strange. */
1325 error ("Got a strange event, msg id = %d.", msg.hdr.msgh_id);
1328 error ("Handling event, msgid = %d: %s",
1329 msg.hdr.msgh_id, strerror (reply.err));
1332 if (inf->pending_execs)
1333 /* We're waiting for the inferior to finish execing. */
1335 struct inf_wait *w = &inf->wait;
1336 enum target_waitkind kind = w->status.kind;
1338 if (kind == TARGET_WAITKIND_SPURIOUS)
1339 /* Since gdb is actually counting the number of times the inferior
1340 stops, expecting one stop per exec, we only return major events
1343 else if (kind == TARGET_WAITKIND_STOPPED
1344 && w->status.value.sig == TARGET_SIGNAL_TRAP)
1345 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1346 means we've succesfully completed an exec! */
1347 if (--inf->pending_execs == 0)
1350 prune_threads (1); /* Get rid of the old shell threads */
1351 renumber_threads (0); /* Give our threads reasonable names. */
1355 if (inf->wait.suppress)
1356 /* Some totally spurious event happened that we don't consider
1357 worth returning to gdb. Just keep waiting. */
1359 inf_debug (inf, "suppressing return, rewaiting...");
1364 /* Pass back out our results. */
1365 bcopy (&inf->wait.status, status, sizeof (*status));
1367 thread = inf->wait.thread;
1371 thread = inf_tid_to_thread (inf, tid);
1373 if (!thread || thread->port == MACH_PORT_NULL)
1374 /* TID is dead; try and find a new thread. */
1375 if (inf_update_procs (inf) && inf->threads)
1376 tid = inf->threads->tid; /* The first available thread. */
1380 if (thread && tid >= 0 && status->kind != TARGET_WAITKIND_SPURIOUS
1381 && inf->pause_sc == 0 && thread->pause_sc == 0)
1382 /* If something actually happened to THREAD, make sure we suspend it. */
1385 inf_update_suspends (inf);
1388 inf_debug (inf, "returning tid = %d, status = %s (%d)", tid,
1389 status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
1390 : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
1391 : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
1392 : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
1393 : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
1395 status->value.integer);
1400 /* The rpc handler called by exc_server. */
1402 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1403 thread_t thread_port, task_t task_port,
1404 int exception, int code, int subcode)
1406 struct inf *inf = waiting_inf;
1407 struct proc *thread = inf_port_to_thread (inf, thread_port);
1409 inf_debug (waiting_inf,
1410 "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1411 thread_port, task_port, exception, code);
1414 /* We don't know about thread? */
1416 inf_update_procs (inf);
1417 thread = inf_port_to_thread (inf, thread_port);
1419 /* Give up, the generating thread is gone. */
1423 mach_port_deallocate (mach_task_self (), thread_port);
1424 mach_port_deallocate (mach_task_self (), task_port);
1426 if (! thread->aborted)
1427 /* THREAD hasn't been aborted since this exception happened (abortion
1428 clears any exception state), so it must be real. */
1430 /* Store away the details; this will destroy any previous info. */
1431 inf->wait.thread = thread;
1433 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1435 if (exception == EXC_BREAKPOINT)
1436 /* GDB likes to get SIGTRAP for breakpoints. */
1438 inf->wait.status.value.sig = TARGET_SIGNAL_TRAP;
1439 mach_port_deallocate (mach_task_self (), reply_port);
1442 /* Record the exception so that we can forward it later. */
1444 if (thread->exc_port == port)
1445 inf->wait.exc.handler = thread->saved_exc_port;
1448 inf->wait.exc.handler = inf->task->saved_exc_port;
1449 assert (inf->task->exc_port == port);
1451 if (inf->wait.exc.handler != MACH_PORT_NULL)
1452 /* Add a reference to the exception handler. */
1453 mach_port_mod_refs (mach_task_self (),
1454 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1457 inf->wait.exc.exception = exception;
1458 inf->wait.exc.code = code;
1459 inf->wait.exc.subcode = subcode;
1460 inf->wait.exc.reply = reply_port;
1462 /* Exceptions are encoded in the signal space by putting them after
1463 _NSIG; this assumes they're positive (and not extremely large)! */
1464 inf->wait.status.value.sig =
1465 target_signal_from_host (_NSIG + exception);
1469 /* A supppressed exception, which ignore. */
1471 inf->wait.suppress = 1;
1472 mach_port_deallocate (mach_task_self (), reply_port);
1478 /* Fill in INF's wait field after a task has died without giving us more
1479 detailed information. */
1481 inf_task_died_status (struct inf *inf)
1483 warning ("Pid %d died with unknown exit status, using SIGKILL.", inf->pid);
1484 inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
1485 inf->wait.status.value.sig = TARGET_SIGNAL_KILL;
1488 /* Notify server routines. The only real one is dead name notification. */
1491 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1493 struct inf *inf = waiting_inf;
1495 inf_debug (waiting_inf, "port = %d", dead_port);
1497 if (inf->task && inf->task->port == dead_port)
1499 proc_debug (inf->task, "is dead");
1500 inf->task->port = MACH_PORT_NULL;
1501 if (proc_wait_pid == inf->pid)
1502 /* We have a wait outstanding on the process, which will return more
1503 detailed information, so delay until we get that. */
1504 inf->wait.suppress = 1;
1506 /* We never waited for the process (maybe it wasn't a child), so just
1507 pretend it got a SIGKILL. */
1508 inf_task_died_status (inf);
1512 struct proc *thread = inf_port_to_thread (inf, dead_port);
1515 proc_debug (thread, "is dead");
1516 thread->port = MACH_PORT_NULL;
1520 mach_port_deallocate (mach_task_self (), dead_port);
1521 inf->threads_up_to_date = 0; /* Just in case */
1529 warning ("illegal rpc: %s", fun);
1534 do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
1536 return ill_rpc (__FUNCTION__);
1540 do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
1542 return ill_rpc (__FUNCTION__);
1546 do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
1548 return ill_rpc (__FUNCTION__);
1552 do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
1554 return ill_rpc (__FUNCTION__);
1558 do_mach_notify_send_once (mach_port_t notify)
1560 return ill_rpc (__FUNCTION__);
1563 /* Process_reply server routines. We only use process_wait_reply. */
1566 S_proc_wait_reply (mach_port_t reply, error_t err,
1567 int status, rusage_t rusage, pid_t pid)
1569 struct inf *inf = waiting_inf;
1571 inf_debug (inf, "err = %s, pid = %d, status = 0x%x",
1572 err ? strerror (err) : "0", pid, status);
1574 if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1575 /* Ack. The task has died, but the task-died notification code didn't
1576 tell anyone because it thought a more detailed reply from the
1577 procserver was forthcoming. However, we now learn that won't
1578 happen... So we have to act like the task just died, and this time,
1580 inf_task_died_status (inf);
1582 if (--proc_waits_pending == 0)
1583 /* PROC_WAIT_PID represents the most recent wait. We will always get
1584 replies in order because the proc server is single threaded. */
1587 inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1593 warning ("Can't wait for pid %d: %s", inf->pid, strerror (err));
1596 /* Since we can't see the inferior's signals, don't trap them. */
1597 inf_set_traced (inf, 0);
1600 else if (pid == inf->pid)
1602 store_waitstatus (&inf->wait.status, status);
1603 if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
1604 /* The process has sent us a signal, and stopped itself in a sane
1605 state pending our actions. */
1607 inf_debug (inf, "process has stopped itself");
1610 /* We recheck the task suspend count here because the crash server
1611 messes with it in an unfriendly way, right before `stopping'. */
1612 inf_validate_task_sc (inf);
1616 inf->wait.suppress = 1; /* Something odd happened. Ignore. */
1622 S_proc_setmsgport_reply (mach_port_t reply, error_t err,
1623 mach_port_t old_msg_port)
1625 return ill_rpc (__FUNCTION__);
1629 S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
1631 return ill_rpc (__FUNCTION__);
1634 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1637 S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
1639 struct inf *inf = waiting_inf;
1642 /* EBUSY is what we get when the crash server has grabbed control of the
1643 process and doesn't like what signal we tried to send it. Just act
1644 like the process stopped (using a signal of 0 should mean that the
1645 *next* time the user continues, it will pass signal 0, which the crash
1646 server should like). */
1648 inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
1649 inf->wait.status.value.sig = TARGET_SIGNAL_0;
1652 warning ("Signal delivery failed: %s", strerror (err));
1655 /* We only get this reply when we've posted a signal to a process which we
1656 thought was stopped, and which we expected to continue after the signal.
1657 Given that the signal has failed for some reason, it's reasonable to
1658 assume it's still stopped. */
1661 inf->wait.suppress = 1;
1667 S_msg_sig_post_reply (mach_port_t reply, error_t err)
1669 return ill_rpc (__FUNCTION__);
1672 /* Returns the number of messages queued for the receive right PORT. */
1673 static mach_port_msgcount_t
1674 port_msgs_queued (mach_port_t port)
1676 struct mach_port_status status;
1678 mach_port_get_receive_status (mach_task_self (), port, &status);
1683 return status.mps_msgcount;
1686 /* Resume execution of the inferior process.
1688 If STEP is nonzero, single-step it.
1689 If SIGNAL is nonzero, give it that signal.
1692 -1 true Single step the current thread allowing other threads to run.
1693 -1 false Continue the current thread allowing other threads to run.
1694 X true Single step the given thread, don't allow any others to run.
1695 X false Continue the given thread, do not allow any others to run.
1696 (Where X, of course, is anything except -1)
1698 Note that a resume may not `take' if there are pending exceptions/&c
1699 still unprocessed from the last resume we did (any given resume may result
1700 in multiple events returned by wait).
1703 gnu_resume (int tid, int step, enum target_signal sig)
1705 struct proc *step_thread = 0;
1706 struct inf *inf = current_inferior;
1708 inf_debug (inf, "tid = %d, step = %d, sig = %d", tid, step, sig);
1710 if (sig != TARGET_SIGNAL_0 || inf->stopped)
1711 inf_signal (inf, sig);
1712 else if (inf->wait.exc.reply != MACH_PORT_NULL)
1713 /* We received an exception to which we have chosen not to forward, so
1714 abort the faulting thread, which will perhaps retake it. */
1716 proc_abort (inf->wait.thread, 1);
1717 warning ("Aborting %s with unforwarded exception %s.",
1718 proc_string (inf->wait.thread),
1719 target_signal_to_name (inf->wait.status.value.sig));
1722 if (port_msgs_queued (inf->event_port))
1723 /* If there are still messages in our event queue, don't bother resuming
1724 the process, as we're just going to stop it right away anyway. */
1728 /* Allow all threads to run, except perhaps single-stepping one. */
1730 inf_debug (inf, "running all threads; tid = %d", inferior_pid);
1731 tid = inferior_pid; /* What to step. */
1732 inf_set_threads_resume_sc (inf, 0, 1);
1735 /* Just allow a single thread to run. */
1737 struct proc *thread = inf_tid_to_thread (inf, tid);
1740 inf_debug (inf, "running one thread: %d/%d", inf->pid, thread->tid);
1741 inf_set_threads_resume_sc (inf, thread, 0);
1746 step_thread = inf_tid_to_thread (inf, tid);
1747 assert (step_thread);
1748 inf_debug (inf, "stepping thread: %d/%d", inf->pid, step_thread->tid);
1750 if (step_thread != inf->step_thread)
1751 inf_set_step_thread (inf, step_thread);
1753 inf_debug (inf, "here we go...");
1758 gnu_kill_inferior ()
1760 struct proc *task = current_inferior->task;
1763 proc_debug (task, "terminating...");
1764 task_terminate (task->port);
1765 task->port = MACH_PORT_NULL;
1766 inf_validate_procs (current_inferior); /* Clear out the thread list &c */
1768 target_mourn_inferior ();
1771 /* Clean up after the inferior dies. */
1774 gnu_mourn_inferior ()
1776 inf_debug (current_inferior, "rip");
1777 inf_detach (current_inferior);
1778 unpush_target (&gnu_ops);
1779 generic_mourn_inferior ();
1782 /* Fork an inferior process, and start debugging it. */
1784 /* Set INFERIOR_PID to the first thread available in the child, if any. */
1786 pick_first_thread ()
1788 if (current_inferior->task && current_inferior->threads)
1789 /* The first thread. */
1790 inferior_pid = current_inferior->threads->tid;
1792 /* What may be the next thread. */
1793 inferior_pid = next_thread_id;
1799 if (! current_inferior)
1800 current_inferior = make_inf ();
1801 return current_inferior;
1805 gnu_create_inferior (exec_file, allargs, env)
1810 struct inf *inf = cur_inf ();
1814 /* We're in the child; make this process stop as soon as it execs. */
1815 inf_debug (inf, "tracing self");
1816 ptrace (PTRACE_TRACEME, 0, 0, 0);
1818 void attach_to_child (int pid)
1820 /* Attach to the now stopped child, which is actually a shell... */
1821 inf_debug (inf, "attaching to child: %d", pid);
1823 inf_attach (inf, pid);
1824 pick_first_thread ();
1827 push_target (&gnu_ops);
1829 inf->pending_execs = 2;
1832 /* Now let the child run again, knowing that it will stop immediately
1833 because of the ptrace. */
1836 startup_inferior (pid, inf->pending_execs);
1839 inf_debug (inf, "creating inferior");
1841 fork_inferior (exec_file, allargs, env, trace_me, attach_to_child, NULL);
1843 inf_update_signal_thread (inf);
1844 inf_set_traced (inf, inf->want_signals);
1846 /* Execing the process will have trashed our exception ports; steal them
1847 back (or make sure they're restored if the user wants that). */
1848 if (inf->want_exceptions)
1849 inf_steal_exc_ports (inf);
1851 inf_restore_exc_ports (inf);
1854 proceed ((CORE_ADDR) -1, 0, 0);
1857 /* Mark our target-struct as eligible for stray "run" and "attach"
1865 #ifdef ATTACH_DETACH
1867 /* Attach to process PID, then initialize for debugging it
1868 and wait for the trace-trap that results from attaching. */
1870 gnu_attach (args, from_tty)
1876 struct inf *inf = cur_inf ();
1879 error_no_arg ("PID to attach");
1883 if (pid == getpid()) /* Trying to masturbate? */
1884 error ("I refuse to debug myself!");
1888 exec_file = (char *) get_exec_file (0);
1891 printf_unfiltered ("Attaching to program `%s', pid %d\n",
1894 printf_unfiltered ("Attaching to pid %d\n", pid);
1896 gdb_flush (gdb_stdout);
1899 inf_debug (inf, "attaching to pid: %d", pid);
1901 inf_attach (inf, pid);
1902 inf_update_procs (inf);
1904 pick_first_thread ();
1907 push_target (&gnu_ops);
1909 inf_update_signal_thread (inf);
1910 inf_set_traced (inf, inf->want_signals);
1912 /* If the process was stopped before we attached, make it continue the next
1913 time the user does a continue. */
1914 inf_validate_stopped (inf);
1915 inf_validate_task_sc (inf);
1918 /* Take a program previously attached to and detaches it.
1919 The program resumes execution and will no longer stop
1920 on signals, etc. We'd better not have left any breakpoints
1921 in the program or it'll die when it hits one. For this
1922 to work, it may be necessary for the process to have been
1923 previously attached. It *might* work if the program was
1924 started via fork. */
1926 gnu_detach (args, from_tty)
1932 char *exec_file = get_exec_file (0);
1934 printf_unfiltered ("Detaching from program `%s' pid %d\n",
1935 exec_file, current_inferior->pid);
1937 printf_unfiltered ("Detaching from pid %d\n", current_inferior->pid);
1938 gdb_flush (gdb_stdout);
1941 inf_detach (current_inferior);
1945 unpush_target (&gnu_ops); /* Pop out of handling an inferior */
1947 #endif /* ATTACH_DETACH */
1950 gnu_terminal_init_inferior ()
1952 assert (current_inferior);
1953 terminal_init_inferior_with_pgrp (current_inferior->pid);
1956 /* Get ready to modify the registers array. On machines which store
1957 individual registers, this doesn't need to do anything. On machines
1958 which store all the registers in one fell swoop, this makes sure
1959 that registers contains all the registers from the program being
1963 gnu_prepare_to_store ()
1965 #ifdef CHILD_PREPARE_TO_STORE
1966 CHILD_PREPARE_TO_STORE ();
1971 gnu_open (arg, from_tty)
1975 error ("Use the \"run\" command to start a Unix child process.");
1981 error ("to_stop target function not implemented");
1985 gnu_thread_alive (int tid)
1987 inf_update_procs (current_inferior);
1988 return !!inf_tid_to_thread (current_inferior, tid);
1992 * Read inferior task's LEN bytes from ADDR and copy it to MYADDR
1993 * in gdb's address space.
1995 * Return 0 on failure; number of bytes read otherwise.
1998 gnu_read_inferior (task, addr, myaddr, length)
2005 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2006 vm_size_t aligned_length =
2007 (vm_size_t) round_page (addr+length) - low_address;
2011 /* Get memory from inferior with page aligned addresses */
2012 err = vm_read (task, low_address, aligned_length, &copied, ©_count);
2016 err = hurd_safe_copyin (myaddr, (void*)addr - low_address + copied, length);
2019 warning ("Read from inferior faulted: %s", strerror (err));
2023 err = vm_deallocate (mach_task_self (), copied, copy_count);
2025 warning ("gnu_read_inferior vm_deallocate failed: %s", strerror (err));
2030 #define CHK_GOTO_OUT(str,ret) \
2031 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2033 struct vm_region_list {
2034 struct vm_region_list *next;
2035 vm_prot_t protection;
2040 struct obstack region_obstack;
2043 * Write inferior task's LEN bytes from ADDR and copy it to MYADDR
2044 * in gdb's address space.
2047 gnu_write_inferior (task, addr, myaddr, length)
2054 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2055 vm_size_t aligned_length =
2056 (vm_size_t) round_page (addr+length) - low_address;
2061 char *errstr = "Bug in gnu_write_inferior";
2063 struct vm_region_list *region_element;
2064 struct vm_region_list *region_head = (struct vm_region_list *)NULL;
2066 /* Get memory from inferior with page aligned addresses */
2067 err = vm_read (task,
2072 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2076 err = hurd_safe_copyout ((void*)addr - low_address + copied, myaddr, length);
2077 CHK_GOTO_OUT ("Write to inferior faulted", err);
2079 obstack_init (®ion_obstack);
2081 /* Do writes atomically.
2082 * First check for holes and unwritable memory.
2085 vm_size_t remaining_length = aligned_length;
2086 vm_address_t region_address = low_address;
2088 struct vm_region_list *scan;
2090 while(region_address < low_address + aligned_length)
2092 vm_prot_t protection;
2093 vm_prot_t max_protection;
2094 vm_inherit_t inheritance;
2096 mach_port_t object_name;
2098 vm_size_t region_length = remaining_length;
2099 vm_address_t old_address = region_address;
2101 err = vm_region (task,
2110 CHK_GOTO_OUT ("vm_region failed", err);
2112 /* Check for holes in memory */
2113 if (old_address != region_address)
2115 warning ("No memory at 0x%x. Nothing written",
2122 if (!(max_protection & VM_PROT_WRITE))
2124 warning ("Memory at address 0x%x is unwritable. Nothing written",
2131 /* Chain the regions for later use */
2133 (struct vm_region_list *)
2134 obstack_alloc (®ion_obstack, sizeof (struct vm_region_list));
2136 region_element->protection = protection;
2137 region_element->start = region_address;
2138 region_element->length = region_length;
2140 /* Chain the regions along with protections */
2141 region_element->next = region_head;
2142 region_head = region_element;
2144 region_address += region_length;
2145 remaining_length = remaining_length - region_length;
2148 /* If things fail after this, we give up.
2149 * Somebody is messing up inferior_task's mappings.
2152 /* Enable writes to the chained vm regions */
2153 for (scan = region_head; scan; scan = scan->next)
2155 boolean_t protection_changed = FALSE;
2157 if (!(scan->protection & VM_PROT_WRITE))
2159 err = vm_protect (task,
2163 scan->protection | VM_PROT_WRITE);
2164 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2168 err = vm_write (task,
2172 CHK_GOTO_OUT ("vm_write failed", err);
2174 /* Set up the original region protections, if they were changed */
2175 for (scan = region_head; scan; scan = scan->next)
2177 boolean_t protection_changed = FALSE;
2179 if (!(scan->protection & VM_PROT_WRITE))
2181 err = vm_protect (task,
2186 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2194 obstack_free (®ion_obstack, 0);
2196 (void) vm_deallocate (mach_task_self (),
2201 if (err != KERN_SUCCESS)
2203 warning ("%s: %s", errstr, mach_error_string (err));
2210 /* Return 0 on failure, number of bytes handled otherwise. */
2212 gnu_xfer_memory (memaddr, myaddr, len, write, target)
2217 struct target_ops *target; /* IGNORED */
2222 ? (current_inferior->task ? current_inferior->task->port : 0)
2225 if (task == MACH_PORT_NULL)
2229 inf_debug (current_inferior, "%s %p[%d] %s %p",
2230 write ? "writing" : "reading", memaddr, len,
2231 write ? "<--" : "-->", myaddr);
2233 return gnu_write_inferior (task, memaddr, myaddr, len);
2235 return gnu_read_inferior (task, memaddr, myaddr, len);
2239 extern void gnu_store_registers (int regno);
2240 extern void gnu_fetch_registers (int regno);
2242 struct target_ops gnu_ops = {
2243 "GNU", /* to_shortname */
2244 "GNU Hurd process", /* to_longname */
2245 "GNU Hurd process", /* to_doc */
2246 gnu_open, /* to_open */
2248 gnu_attach, /* to_attach */
2249 gnu_detach, /* to_detach */
2250 gnu_resume, /* to_resume */
2251 gnu_wait, /* to_wait */
2252 gnu_fetch_registers, /* to_fetch_registers */
2253 gnu_store_registers, /* to_store_registers */
2254 gnu_prepare_to_store, /* to_prepare_to_store */
2255 gnu_xfer_memory, /* to_xfer_memory */
2256 0, /* to_files_info */
2257 memory_insert_breakpoint, /* to_insert_breakpoint */
2258 memory_remove_breakpoint, /* to_remove_breakpoint */
2259 gnu_terminal_init_inferior, /* to_terminal_init */
2260 terminal_inferior, /* to_terminal_inferior */
2261 terminal_ours_for_output, /* to_terminal_ours_for_output */
2262 terminal_ours, /* to_terminal_ours */
2263 child_terminal_info, /* to_terminal_info */
2264 gnu_kill_inferior, /* to_kill */
2266 0, /* to_lookup_symbol */
2268 gnu_create_inferior, /* to_create_inferior */
2269 gnu_mourn_inferior, /* to_mourn_inferior */
2270 gnu_can_run, /* to_can_run */
2271 0, /* to_notice_signals */
2272 gnu_thread_alive, /* to_thread_alive */
2273 gnu_stop, /* to_stop */
2274 process_stratum, /* to_stratum */
2276 1, /* to_has_all_memory */
2277 1, /* to_has_memory */
2278 1, /* to_has_stack */
2279 1, /* to_has_registers */
2280 1, /* to_has_execution */
2282 0, /* sections_end */
2283 OPS_MAGIC /* to_magic */
2286 char *proc_string (struct proc *proc)
2288 static char tid_str[80];
2289 if (proc_is_task (proc))
2290 sprintf (tid_str, "process %d", proc->inf->pid);
2292 sprintf (tid_str, "thread %d.%d",
2294 pid_to_thread_id (proc->tid));
2299 gnu_target_pid_to_str (int tid)
2301 struct inf *inf = current_inferior;
2302 struct proc *thread = inf_tid_to_thread (inf, tid);
2305 return proc_string (thread);
2308 static char tid_str[80];
2309 sprintf (tid_str, "bogus thread id %d", tid);
2314 /* User task commands. */
2316 struct cmd_list_element *set_task_cmd_list = 0;
2317 struct cmd_list_element *show_task_cmd_list = 0;
2319 extern struct cmd_list_element *set_thread_default_cmd_list;
2320 extern struct cmd_list_element *show_thread_default_cmd_list;
2323 _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
2325 if (!args || strcmp (args, t_val) == 0)
2327 else if (strcmp (args, f_val) == 0)
2330 error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2331 cmd_prefix, t_val, f_val);
2334 #define parse_bool_arg(args, cmd_prefix) \
2335 _parse_bool_arg (args, "on", "off", cmd_prefix)
2338 check_empty (char *args, char *cmd_prefix)
2341 error ("Garbage after \"%s\" command: `%s'", cmd_prefix, args);
2344 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2345 static struct proc *
2348 struct inf *inf = cur_inf ();
2349 struct proc *thread = inf_tid_to_thread (inf, inferior_pid);
2351 error ("No current thread.");
2356 set_task_pause_cmd (char *args, int from_tty)
2358 struct inf *inf = cur_inf ();
2359 int old_sc = inf->pause_sc;
2361 inf->pause_sc = parse_bool_arg (args, "set task pause");
2363 if (old_sc == 0 && inf->pause_sc != 0)
2364 /* If the task is currently unsuspended, immediately suspend it,
2365 otherwise wait until the next time it gets control. */
2370 show_task_pause_cmd (char *args, int from_tty)
2372 struct inf *inf = cur_inf ();
2373 check_empty (args, "show task pause");
2374 printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2376 ? (inf->pause_sc == 0 ? "isn't" : "is")
2377 : (inf->pause_sc == 0 ? "won't be" : "will be"));
2381 set_thread_default_pause_cmd (char *args, int from_tty)
2383 struct inf *inf = cur_inf ();
2384 inf->default_thread_pause_sc =
2385 parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2389 show_thread_default_pause_cmd (char *args, int from_tty)
2391 struct inf *inf = cur_inf ();
2392 int sc = inf->default_thread_pause_sc;
2393 check_empty (args, "show thread default pause");
2394 printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2395 sc ? "are" : "aren't",
2396 !sc && inf->pause_sc ? "(but the task is)" : "");
2400 set_thread_default_run_cmd (char *args, int from_tty)
2402 struct inf *inf = cur_inf ();
2403 inf->default_thread_run_sc =
2404 parse_bool_arg (args, "set thread default run") ? 0 : 1;
2408 show_thread_default_run_cmd (char *args, int from_tty)
2410 struct inf *inf = cur_inf ();
2411 check_empty (args, "show thread default run");
2412 printf_unfiltered ("New threads %s allowed to run.\n",
2413 inf->default_thread_run_sc == 0 ? "are" : "aren't");
2416 /* Steal a send right called NAME in the inferior task, and make it PROC's
2417 saved exception port. */
2419 steal_exc_port (struct proc *proc, mach_port_t name)
2423 mach_msg_type_name_t port_type;
2425 if (!proc || !proc->inf->task)
2426 error ("No inferior task.");
2428 err = mach_port_extract_right (proc->inf->task->port,
2429 name, MACH_MSG_TYPE_COPY_SEND,
2432 error ("Couldn't extract send right %d from inferior: %s",
2433 name, strerror (err));
2435 if (proc->saved_exc_port)
2436 /* Get rid of our reference to the old one. */
2437 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2439 proc->saved_exc_port = port;
2441 if (! proc->exc_port)
2442 /* If PROC is a thread, we may not have set its exception port before.
2443 We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2445 proc->exc_port = proc->inf->event_port;
2446 err = proc_set_exception_port (proc, proc->exc_port);
2447 error ("Can't set exception port for %s: %s",
2448 proc_string (proc), strerror (err));
2453 set_task_exc_port_cmd (char *args, int from_tty)
2455 struct inf *inf = cur_inf ();
2457 error ("No argument to \"set task exception-port\" command.");
2458 steal_exc_port (inf->task, parse_and_eval_address (args));
2462 set_signals_cmd (char *args, int from_tty)
2465 struct inf *inf = cur_inf ();
2467 inf->want_signals = parse_bool_arg (args, "set signals");
2469 if (inf->task && inf->want_signals != inf->traced)
2470 /* Make this take effect immediately in a running process. */
2471 inf_set_traced (inf, inf->want_signals);
2475 show_signals_cmd (char *args, int from_tty)
2477 struct inf *inf = cur_inf ();
2478 check_empty (args, "show signals");
2479 printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2481 ? (inf->traced ? "are" : "aren't")
2482 : (inf->want_signals ? "will be" : "won't be"));
2486 set_stopped_cmd (char *args, int from_tty)
2488 cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2492 show_stopped_cmd (char *args, int from_tty)
2494 struct inf *inf = cur_inf ();
2495 check_empty (args, "show stopped");
2497 error ("No current process.");
2498 printf_unfiltered ("The inferior process %s stopped.\n",
2499 inf->stopped ? "is" : "isn't");
2503 set_sig_thread_cmd (char *args, int from_tty)
2506 struct inf *inf = cur_inf ();
2508 if (!args || (! isdigit (*args) && strcmp (args, "none") != 0))
2509 error ("Illegal argument to \"set signal-thread\" command.\n"
2510 "Should be an integer thread ID, or `none'.");
2512 if (strcmp (args, "none") == 0)
2513 inf->signal_thread = 0;
2516 int tid = thread_id_to_pid (atoi (args));
2518 error ("Thread ID %s not known. Use the \"info threads\" command to\n"
2519 "see the IDs of currently known threads.", args);
2520 inf->signal_thread = inf_tid_to_thread (inf, tid);
2525 show_sig_thread_cmd (char *args, int from_tty)
2527 struct inf *inf = cur_inf ();
2528 check_empty (args, "show signal-thread");
2530 error ("No current process.");
2531 if (inf->signal_thread)
2532 printf_unfiltered ("The signal thread is %s.\n",
2533 proc_string (inf->signal_thread));
2535 printf_unfiltered ("There is no signal thread.\n");
2539 set_exceptions_cmd (char *args, int from_tty)
2541 struct inf *inf = cur_inf ();
2542 int val = parse_bool_arg (args, "set exceptions");
2544 if (inf->task && inf->want_exceptions != val)
2545 /* Make this take effect immediately in a running process. */
2548 inf->want_exceptions = val;
2552 show_exceptions_cmd (char *args, int from_tty)
2554 struct inf *inf = cur_inf ();
2555 check_empty (args, "show exceptions");
2556 printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2558 ? (inf->want_exceptions ? "are" : "aren't")
2559 : (inf->want_exceptions ? "will be" : "won't be"));
2563 set_task_cmd (char *args, int from_tty)
2565 printf_unfiltered ("\"set task\" must be followed by the name of a task property.\n");
2569 show_task_cmd (char *args, int from_tty)
2571 struct inf *inf = cur_inf ();
2573 check_empty (args, "show task");
2575 show_signals_cmd (0, from_tty);
2576 show_exceptions_cmd (0, from_tty);
2577 show_task_pause_cmd (0, from_tty);
2579 if (inf->pause_sc == 0)
2580 show_thread_default_pause_cmd (0, from_tty);
2581 show_thread_default_run_cmd (0, from_tty);
2585 show_stopped_cmd (0, from_tty);
2586 show_sig_thread_cmd (0, from_tty);
2590 static void add_task_commands ()
2592 add_cmd ("pause", class_run, set_thread_default_pause_cmd,
2593 "Set whether the new threads are suspended while gdb has control.\n"
2594 "This property normally has no effect because the whole task is\n"
2595 "suspended, however, that may be disabled with \"set task pause off\".\n"
2596 "The default value is \"off\".",
2597 &set_thread_default_cmd_list);
2598 add_cmd ("pause", no_class, show_thread_default_pause_cmd,
2599 "Show whether new threads are suspended while gdb has control.",
2600 &show_thread_default_cmd_list);
2601 add_cmd ("run", class_run, set_thread_default_run_cmd,
2602 "Set whether new threads are allowed to run (once gdb has noticed them).",
2603 &set_thread_default_cmd_list);
2604 add_cmd ("run", no_class, show_thread_default_run_cmd,
2605 "Show whether new threads are allowed to run (once gdb has noticed
2607 &show_thread_default_cmd_list);
2609 add_cmd ("signals", class_run, set_signals_cmd,
2610 "Set whether the inferior process's signals will be intercepted.\n"
2611 "Mach exceptions (such as breakpoint traps) are not affected.",
2613 add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
2614 add_cmd ("signals", no_class, show_signals_cmd,
2615 "Show whether the inferior process's signals will be intercepted.",
2617 add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
2619 add_cmd ("signal-thread", class_run, set_sig_thread_cmd,
2620 "Set the thread that gdb thinks is the libc signal thread.\n"
2621 "This thread is run when delivering a signal to a non-stopped process.",
2623 add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
2624 add_cmd ("signal-thread", no_class, show_sig_thread_cmd,
2625 "Set the thread that gdb thinks is the libc signal thread.",
2627 add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
2629 add_cmd ("stopped", class_run, set_stopped_cmd,
2630 "Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n"
2631 "Stopped process will be continued by sending them a signal.",
2633 add_cmd ("stopped", no_class, show_signals_cmd,
2634 "Show whether gdb thinks the inferior process is stopped as with SIGSTOP.",
2637 add_cmd ("exceptions", class_run, set_exceptions_cmd,
2638 "Set whether exceptions in the inferior process will be trapped.\n"
2639 "When exceptions are turned off, neither breakpoints nor single-stepping\n"
2642 /* Allow `set exc' despite conflict with `set exception-port'. */
2643 add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
2644 add_cmd ("exceptions", no_class, show_exceptions_cmd,
2645 "Show whether exceptions in the inferior process will be trapped.",
2650 add_prefix_cmd ("task", no_class, set_task_cmd,
2651 "Command prefix for setting task attributes.",
2652 &set_task_cmd_list, "set task ", 0, &setlist);
2653 add_prefix_cmd ("task", no_class, show_task_cmd,
2654 "Command prefix for showing task attributes.",
2655 &show_task_cmd_list, "show task ", 0, &showlist);
2657 add_cmd ("pause", class_run, set_task_pause_cmd,
2658 "Set whether the task is suspended while gdb has control.\n"
2659 "A value of \"on\" takes effect immediately, otherwise nothing\n"
2660 "happens until the next time the program is continued.\n"
2661 "When setting this to \"off\", \"set thread default pause on\"\n"
2662 "can be used to pause individual threads by default instead.",
2663 &set_task_cmd_list);
2664 add_cmd ("pause", no_class, show_task_pause_cmd,
2665 "Show whether the task is suspended while gdb has control.",
2666 &show_task_cmd_list);
2668 add_cmd ("exception-port", no_class, set_task_exc_port_cmd,
2669 "Set the task exception port to which we forward exceptions.\n"
2670 "The argument should be the value of the send right in the task.",
2671 &set_task_cmd_list);
2672 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
2673 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_task_cmd_list);
2676 /* User thread commands. */
2678 extern struct cmd_list_element *set_thread_cmd_list;
2679 extern struct cmd_list_element *show_thread_cmd_list;
2682 set_thread_pause_cmd (char *args, int from_tty)
2684 struct proc *thread = cur_thread ();
2685 int old_sc = thread->pause_sc;
2686 thread->pause_sc = parse_bool_arg (args, "set thread pause");
2687 if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
2688 /* If the task is currently unsuspended, immediately suspend it,
2689 otherwise wait until the next time it gets control. */
2690 inf_suspend (thread->inf);
2694 show_thread_pause_cmd (char *args, int from_tty)
2696 struct proc *thread = cur_thread ();
2697 int sc = thread->pause_sc;
2698 check_empty (args, "show task pause");
2699 printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
2700 proc_string (thread),
2701 sc ? "is" : "isn't",
2702 !sc && thread->inf->pause_sc ? "(but the task is)" : "");
2706 set_thread_run_cmd (char *args, int from_tty)
2708 struct proc *thread = cur_thread ();
2709 thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
2713 show_thread_run_cmd (char *args, int from_tty)
2715 struct proc *thread = cur_thread ();
2716 check_empty (args, "show thread run");
2717 printf_unfiltered ("Thread %s allowed to run.",
2718 proc_string (thread),
2719 thread->run_sc == 0 ? "is" : "isn't");
2723 set_thread_exc_port_cmd (char *args, int from_tty)
2725 struct proc *thread = cur_thread ();
2727 error ("No argument to \"set thread exception-port\" command.");
2728 steal_exc_port (thread, parse_and_eval_address (args));
2732 set_thread_cmd (char *args, int from_tty)
2734 printf_unfiltered ("\"set thread\" must be followed by the name of a thread property.\n");
2738 show_thread_cmd (char *args, int from_tty)
2740 check_empty (args, "show thread");
2741 show_thread_run_cmd (0, from_tty);
2742 show_thread_pause_cmd (0, from_tty);
2745 add_thread_commands ()
2747 add_cmd ("pause", class_run, set_thread_pause_cmd,
2748 "Set whether the current thread is suspended while gdb has control.\n"
2749 "A value of \"on\" takes effect immediately, otherwise nothing\n"
2750 "happens until the next time the program is continued. This\n"
2751 "property normally has no effect because the whole task is suspended,\n"
2752 "however, that may be disabled with \"set task pause off\".\n"
2753 "The default value is \"off\".",
2754 &set_thread_cmd_list);
2755 add_cmd ("pause", no_class, show_thread_pause_cmd,
2756 "Show whether the current thread is suspended while gdb has control.",
2757 &show_thread_cmd_list);
2759 add_cmd ("run", class_run, set_thread_run_cmd,
2760 "Set whether the current thread is allowed to run.",
2761 &set_thread_cmd_list);
2762 add_cmd ("run", no_class, show_thread_run_cmd,
2763 "Show whether the current thread is allowed to run.",
2764 &show_thread_cmd_list);
2766 add_cmd ("exception-port", no_class, set_thread_exc_port_cmd,
2767 "Set the exception port to which we forward exceptions for the\n"
2768 "current thread, overriding the task exception port.\n"
2769 "The argument should be the value of the send right in the task.",
2770 &set_thread_cmd_list);
2771 add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
2772 add_alias_cmd ("exc-port", "exception-port", no_class, 1, &set_thread_cmd_list);
2776 _initialize_gnu_nat ()
2778 proc_server = getproc ();
2780 add_target (&gnu_ops);
2782 add_task_commands ();
2783 add_thread_commands ();
2785 #if MAINTENANCE_CMDS
2786 add_set_cmd ("gnu-debug", class_maintenance,
2787 var_boolean, (char *)&gnu_debug_flag,
2788 "Set debugging output for the gnu backend.", &maintenancelist);
2792 #ifdef FLUSH_INFERIOR_CACHE
2794 /* When over-writing code on some machines the I-Cache must be flushed
2795 explicitly, because it is not kept coherent by the lazy hardware.
2796 This definitely includes breakpoints, for instance, or else we
2797 end up looping in mysterious Bpt traps */
2800 flush_inferior_icache(pc, amount)
2803 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
2806 ret = vm_machine_attribute (current_inferior->task->port,
2811 if (ret != KERN_SUCCESS)
2812 warning ("Error flushing inferior's cache : %s", strerror (ret));
2814 #endif FLUSH_INFERIOR_CACHE