1 /* Interface GDB to the GNU Hurd.
2 Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 Written by Miles Bader <miles@gnu.ai.mit.edu>
9 Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA.
33 #include "gdb_string.h"
34 #include <sys/ptrace.h>
37 #include <mach_error.h>
38 #include <mach/exception.h>
39 #include <mach/message.h>
40 #include <mach/notify.h>
41 #include <mach/vm_attributes.h>
44 #include <hurd/interrupt.h>
46 #include <hurd/msg_request.h>
47 #include <hurd/process.h>
48 #include <hurd/process_request.h>
49 #include <hurd/signal.h>
50 #include <hurd/sigpreempt.h>
63 #include "gdbthread.h"
64 #include "gdb_assert.h"
68 #include "exc_request_S.h"
70 #include "process_reply_S.h"
71 #include "msg_reply_S.h"
72 #include "exc_request_U.h"
75 static process_t proc_server
= MACH_PORT_NULL
;
77 /* If we've sent a proc_wait_request to the proc server, the pid of the
78 process we asked about. We can only ever have one outstanding. */
79 int proc_wait_pid
= 0;
81 /* The number of wait requests we've sent, and expect replies from. */
82 int proc_waits_pending
= 0;
84 int gnu_debug_flag
= 0;
88 extern struct target_ops gnu_ops
;
90 struct inf
*make_inf ();
91 void inf_clear_wait (struct inf
*inf
);
92 void inf_cleanup (struct inf
*inf
);
93 void inf_startup (struct inf
*inf
, int pid
);
94 int inf_update_suspends (struct inf
*inf
);
95 void inf_set_pid (struct inf
*inf
, pid_t pid
);
96 void inf_validate_procs (struct inf
*inf
);
97 void inf_steal_exc_ports (struct inf
*inf
);
98 void inf_restore_exc_ports (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
,
101 struct proc
*run_thread
,
103 inline int inf_set_threads_resume_sc_for_signal_thread (struct inf
*inf
);
104 inline void inf_suspend (struct inf
*inf
);
105 inline void inf_resume (struct inf
*inf
);
106 void inf_set_step_thread (struct inf
*inf
, struct proc
*proc
);
107 void inf_detach (struct inf
*inf
);
108 void inf_attach (struct inf
*inf
, int pid
);
109 void inf_signal (struct inf
*inf
, enum target_signal sig
);
110 void inf_continue (struct inf
*inf
);
112 #define inf_debug(_inf, msg, args...) \
113 do { struct inf *__inf = (_inf); \
114 debug ("{inf %d %p}: " msg, __inf->pid, __inf , ##args); } while (0)
116 void proc_abort (struct proc
*proc
, int force
);
117 struct proc
*make_proc (struct inf
*inf
, mach_port_t port
, int tid
);
118 struct proc
*_proc_free (struct proc
*proc
);
119 int proc_update_sc (struct proc
*proc
);
120 error_t
proc_get_exception_port (struct proc
*proc
, mach_port_t
* port
);
121 error_t
proc_set_exception_port (struct proc
*proc
, mach_port_t port
);
122 static mach_port_t
_proc_get_exc_port (struct proc
*proc
);
123 void proc_steal_exc_port (struct proc
*proc
, mach_port_t exc_port
);
124 void proc_restore_exc_port (struct proc
*proc
);
125 int proc_trace (struct proc
*proc
, int set
);
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); \
148 /* The state passed by an exception message. */
151 int exception
; /* The exception code */
153 mach_port_t handler
; /* The real exception port to handle this. */
154 mach_port_t reply
; /* The reply port from the exception call. */
157 /* The results of the last wait an inf did. */
160 struct target_waitstatus status
; /* The status returned to gdb. */
161 struct exc_state exc
; /* The exception that caused us to return. */
162 struct proc
*thread
; /* The thread in question. */
163 int suppress
; /* Something trivial happened. */
166 /* The state of an inferior. */
169 /* Fields describing the current inferior. */
171 struct proc
*task
; /* The mach task. */
172 struct proc
*threads
; /* A linked list of all threads in TASK. */
174 /* True if THREADS needn't be validated by querying the task. We assume that
175 we and the task in question are the only ones frobbing the thread list,
176 so as long as we don't let any code run, we don't have to worry about
178 int threads_up_to_date
;
180 pid_t pid
; /* The real system PID. */
182 struct inf_wait wait
; /* What to return from target_wait. */
184 /* One thread proc in INF may be in `single-stepping mode'. This is it. */
185 struct proc
*step_thread
;
187 /* The thread we think is the signal thread. */
188 struct proc
*signal_thread
;
190 mach_port_t event_port
; /* Where we receive various msgs. */
192 /* True if we think at least one thread in the inferior could currently be
194 unsigned int running
:1;
196 /* True if the process has stopped (in the proc server sense). Note that
197 since a proc server `stop' leaves the signal thread running, the inf can
198 be RUNNING && STOPPED... */
199 unsigned int stopped
:1;
201 /* True if the inferior has no message port. */
202 unsigned int nomsg
:1;
204 /* True if the inferior is traced. */
205 unsigned int traced
:1;
207 /* True if we shouldn't try waiting for the inferior, usually because we
208 can't for some reason. */
209 unsigned int no_wait
:1;
211 /* When starting a new inferior, we don't try to validate threads until all
212 the proper execs have been done. This is a count of how many execs we
214 unsigned pending_execs
;
216 /* Fields describing global state */
218 /* The task suspend count used when gdb has control. This is normally 1 to
219 make things easier for us, but sometimes (like when attaching to vital
220 system servers) it may be desirable to let the task continue to run
221 (pausing individual threads as necessary). */
224 /* The task suspend count left when detaching from a task. */
227 /* The initial values used for the run_sc and pause_sc of newly discovered
228 threads -- see the definition of those fields in struct proc. */
229 int default_thread_run_sc
;
230 int default_thread_pause_sc
;
231 int default_thread_detach_sc
;
233 /* True if the process should be traced when started/attached. Newly
234 started processes *must* be traced at first to exec them properly, but
235 if this is false, tracing is turned off as soon it has done so. */
238 /* True if exceptions from the inferior process should be trapped. This
239 must be on to use breakpoints. */
245 __proc_pid (struct proc
*proc
)
247 return proc
->inf
->pid
;
251 /* Update PROC's real suspend count to match it's desired one. Returns true
252 if we think PROC is now in a runnable state. */
254 proc_update_sc (struct proc
*proc
)
258 int delta
= proc
->sc
- proc
->cur_sc
;
261 proc_debug (proc
, "sc: %d --> %d", proc
->cur_sc
, proc
->sc
);
263 if (proc
->sc
== 0 && proc
->state_changed
)
264 /* Since PROC may start running, we must write back any state changes. */
266 gdb_assert (proc_is_thread (proc
));
267 proc_debug (proc
, "storing back changed thread state");
268 err
= thread_set_state (proc
->port
, THREAD_STATE_FLAVOR
,
269 (thread_state_t
) &proc
->state
, THREAD_STATE_SIZE
);
271 proc
->state_changed
= 0;
276 while (delta
-- > 0 && !err
)
278 if (proc_is_task (proc
))
279 err
= task_suspend (proc
->port
);
281 err
= thread_suspend (proc
->port
);
286 while (delta
++ < 0 && !err
)
288 if (proc_is_task (proc
))
289 err
= task_resume (proc
->port
);
291 err
= thread_resume (proc
->port
);
295 proc
->cur_sc
= proc
->sc
;
297 /* If we got an error, then the task/thread has disappeared. */
298 running
= !err
&& proc
->sc
== 0;
300 proc_debug (proc
, "is %s", err
? "dead" : running
? "running" : "suspended");
302 proc_debug (proc
, "err = %s", safe_strerror (err
));
307 proc
->state_valid
= proc
->state_changed
= 0;
308 proc
->fetched_regs
= 0;
315 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
316 If PROC is deemed `precious', then nothing is done unless FORCE is true.
317 In particular, a thread is precious if it's running (in which case forcing
318 it includes suspending it first), or if it has an exception pending. */
320 proc_abort (struct proc
*proc
, int force
)
322 gdb_assert (proc_is_thread (proc
));
326 struct inf
*inf
= proc
->inf
;
327 int running
= (proc
->cur_sc
== 0 && inf
->task
->cur_sc
== 0);
329 if (running
&& force
)
332 inf_update_suspends (proc
->inf
);
334 warning ("Stopped %s.", proc_string (proc
));
336 else if (proc
== inf
->wait
.thread
&& inf
->wait
.exc
.reply
&& !force
)
337 /* An exception is pending on PROC, which don't mess with. */
341 /* We only abort the thread if it's not actually running. */
343 thread_abort (proc
->port
);
344 proc_debug (proc
, "aborted");
348 proc_debug (proc
, "not aborting");
352 /* Make sure that the state field in PROC is up to date, and return a pointer
353 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
354 that the thread is stopped and aborted first, and sets the state_changed
355 field in PROC to true. */
357 proc_get_state (struct proc
*proc
, int will_modify
)
359 int was_aborted
= proc
->aborted
;
361 proc_debug (proc
, "updating state info%s",
362 will_modify
? " (with intention to modify)" : "");
364 proc_abort (proc
, will_modify
);
366 if (!was_aborted
&& proc
->aborted
)
367 /* PROC's state may have changed since we last fetched it. */
368 proc
->state_valid
= 0;
370 if (!proc
->state_valid
)
372 mach_msg_type_number_t state_size
= THREAD_STATE_SIZE
;
374 thread_get_state (proc
->port
, THREAD_STATE_FLAVOR
,
375 (thread_state_t
) &proc
->state
, &state_size
);
376 proc_debug (proc
, "getting thread state");
377 proc
->state_valid
= !err
;
380 if (proc
->state_valid
)
383 proc
->state_changed
= 1;
384 return (thread_state_t
) &proc
->state
;
391 /* Set PORT to PROC's exception port. */
393 proc_get_exception_port (struct proc
* proc
, mach_port_t
* port
)
395 if (proc_is_task (proc
))
396 return task_get_exception_port (proc
->port
, port
);
398 return thread_get_exception_port (proc
->port
, port
);
401 /* Set PROC's exception port to PORT. */
403 proc_set_exception_port (struct proc
* proc
, mach_port_t port
)
405 proc_debug (proc
, "setting exception port: %d", port
);
406 if (proc_is_task (proc
))
407 return task_set_exception_port (proc
->port
, port
);
409 return thread_set_exception_port (proc
->port
, port
);
412 /* Get PROC's exception port, cleaning up a bit if proc has died. */
414 _proc_get_exc_port (struct proc
*proc
)
416 mach_port_t exc_port
;
417 error_t err
= proc_get_exception_port (proc
, &exc_port
);
420 /* PROC must be dead. */
423 mach_port_deallocate (mach_task_self (), proc
->exc_port
);
424 proc
->exc_port
= MACH_PORT_NULL
;
425 if (proc
->saved_exc_port
)
426 mach_port_deallocate (mach_task_self (), proc
->saved_exc_port
);
427 proc
->saved_exc_port
= MACH_PORT_NULL
;
433 /* Replace PROC's exception port with EXC_PORT, unless it's already been
434 done. Stash away any existing exception port so we can restore it later. */
436 proc_steal_exc_port (struct proc
*proc
, mach_port_t exc_port
)
438 mach_port_t cur_exc_port
= _proc_get_exc_port (proc
);
444 proc_debug (proc
, "inserting exception port: %d", exc_port
);
446 if (cur_exc_port
!= exc_port
)
447 /* Put in our exception port. */
448 err
= proc_set_exception_port (proc
, exc_port
);
450 if (err
|| cur_exc_port
== proc
->exc_port
)
451 /* We previously set the exception port, and it's still set. So we
452 just keep the old saved port which is what the proc set. */
455 mach_port_deallocate (mach_task_self (), cur_exc_port
);
458 /* Keep a copy of PROC's old exception port so it can be restored. */
460 if (proc
->saved_exc_port
)
461 mach_port_deallocate (mach_task_self (), proc
->saved_exc_port
);
462 proc
->saved_exc_port
= cur_exc_port
;
465 proc_debug (proc
, "saved exception port: %d", proc
->saved_exc_port
);
468 proc
->exc_port
= exc_port
;
470 warning ("Error setting exception port for %s: %s",
471 proc_string (proc
), safe_strerror (err
));
475 /* If we previously replaced PROC's exception port, put back what we
476 found there at the time, unless *our* exception port has since been
477 overwritten, in which case who knows what's going on. */
479 proc_restore_exc_port (struct proc
*proc
)
481 mach_port_t cur_exc_port
= _proc_get_exc_port (proc
);
487 proc_debug (proc
, "restoring real exception port");
489 if (proc
->exc_port
== cur_exc_port
)
490 /* Our's is still there. */
491 err
= proc_set_exception_port (proc
, proc
->saved_exc_port
);
493 if (proc
->saved_exc_port
)
494 mach_port_deallocate (mach_task_self (), proc
->saved_exc_port
);
495 proc
->saved_exc_port
= MACH_PORT_NULL
;
498 proc
->exc_port
= MACH_PORT_NULL
;
500 warning ("Error setting exception port for %s: %s",
501 proc_string (proc
), safe_strerror (err
));
506 /* Turns hardware tracing in PROC on or off when SET is true or false,
507 respectively. Returns true on success. */
509 proc_trace (struct proc
*proc
, int set
)
511 thread_state_t state
= proc_get_state (proc
, 1);
514 return 0; /* the thread must be dead. */
516 proc_debug (proc
, "tracing %s", set
? "on" : "off");
520 /* XXX We don't get the exception unless the thread has its own
521 exception port???? */
522 if (proc
->exc_port
== MACH_PORT_NULL
)
523 proc_steal_exc_port (proc
, proc
->inf
->event_port
);
524 THREAD_STATE_SET_TRACED (state
);
527 THREAD_STATE_CLEAR_TRACED (state
);
533 /* A variable from which to assign new TIDs. */
534 static int next_thread_id
= 1;
536 /* Returns a new proc structure with the given fields. Also adds a
537 notification for PORT becoming dead to be sent to INF's notify port. */
539 make_proc (struct inf
*inf
, mach_port_t port
, int tid
)
542 mach_port_t prev_port
= MACH_PORT_NULL
;
543 struct proc
*proc
= xmalloc (sizeof (struct proc
));
549 proc
->saved_exc_port
= MACH_PORT_NULL
;
550 proc
->exc_port
= MACH_PORT_NULL
;
555 /* Note that these are all the values for threads; the task simply uses the
556 corresponding field in INF directly. */
557 proc
->run_sc
= inf
->default_thread_run_sc
;
558 proc
->pause_sc
= inf
->default_thread_pause_sc
;
559 proc
->detach_sc
= inf
->default_thread_detach_sc
;
560 proc
->resume_sc
= proc
->run_sc
;
564 proc
->state_valid
= 0;
565 proc
->state_changed
= 0;
567 proc_debug (proc
, "is new");
569 /* Get notified when things die. */
571 mach_port_request_notification (mach_task_self (), port
,
572 MACH_NOTIFY_DEAD_NAME
, 1,
574 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
577 warning ("Couldn't request notification for port %d: %s",
578 port
, safe_strerror (err
));
581 proc_debug (proc
, "notifications to: %d", inf
->event_port
);
582 if (prev_port
!= MACH_PORT_NULL
)
583 mach_port_deallocate (mach_task_self (), prev_port
);
586 if (inf
->want_exceptions
)
588 if (proc_is_task (proc
))
589 /* Make the task exception port point to us. */
590 proc_steal_exc_port (proc
, inf
->event_port
);
592 /* Just clear thread exception ports -- they default to the
594 proc_steal_exc_port (proc
, MACH_PORT_NULL
);
600 /* Frees PROC and any resources it uses, and returns the value of PROC's
603 _proc_free (struct proc
*proc
)
605 struct inf
*inf
= proc
->inf
;
606 struct proc
*next
= proc
->next
;
608 proc_debug (proc
, "freeing...");
610 if (proc
== inf
->step_thread
)
611 /* Turn off single stepping. */
612 inf_set_step_thread (inf
, 0);
613 if (proc
== inf
->wait
.thread
)
614 inf_clear_wait (inf
);
615 if (proc
== inf
->signal_thread
)
616 inf
->signal_thread
= 0;
618 if (proc
->port
!= MACH_PORT_NULL
)
620 if (proc
->exc_port
!= MACH_PORT_NULL
)
621 /* Restore the original exception port. */
622 proc_restore_exc_port (proc
);
623 if (proc
->cur_sc
!= 0)
624 /* Resume the thread/task. */
627 proc_update_sc (proc
);
629 mach_port_deallocate (mach_task_self (), proc
->port
);
640 struct inf
*inf
= xmalloc (sizeof (struct inf
));
644 inf
->threads_up_to_date
= 0;
646 inf
->wait
.status
.kind
= TARGET_WAITKIND_SPURIOUS
;
647 inf
->wait
.thread
= 0;
648 inf
->wait
.exc
.handler
= MACH_PORT_NULL
;
649 inf
->wait
.exc
.reply
= MACH_PORT_NULL
;
650 inf
->step_thread
= 0;
651 inf
->signal_thread
= 0;
652 inf
->event_port
= MACH_PORT_NULL
;
658 inf
->pending_execs
= 0;
661 inf
->default_thread_run_sc
= 0;
662 inf
->default_thread_pause_sc
= 0;
663 inf
->default_thread_detach_sc
= 0;
664 inf
->want_signals
= 1; /* By default */
665 inf
->want_exceptions
= 1; /* By default */
670 /* Clear INF's target wait status. */
672 inf_clear_wait (struct inf
*inf
)
674 inf_debug (inf
, "clearing wait");
675 inf
->wait
.status
.kind
= TARGET_WAITKIND_SPURIOUS
;
676 inf
->wait
.thread
= 0;
677 inf
->wait
.suppress
= 0;
678 if (inf
->wait
.exc
.handler
!= MACH_PORT_NULL
)
680 mach_port_deallocate (mach_task_self (), inf
->wait
.exc
.handler
);
681 inf
->wait
.exc
.handler
= MACH_PORT_NULL
;
683 if (inf
->wait
.exc
.reply
!= MACH_PORT_NULL
)
685 mach_port_deallocate (mach_task_self (), inf
->wait
.exc
.reply
);
686 inf
->wait
.exc
.reply
= MACH_PORT_NULL
;
692 inf_cleanup (struct inf
*inf
)
694 inf_debug (inf
, "cleanup");
696 inf_clear_wait (inf
);
698 inf_set_pid (inf
, -1);
705 inf
->pending_execs
= 0;
709 mach_port_destroy (mach_task_self (), inf
->event_port
);
710 inf
->event_port
= MACH_PORT_NULL
;
715 inf_startup (struct inf
*inf
, int pid
)
719 inf_debug (inf
, "startup: pid = %d", pid
);
723 /* Make the port on which we receive all events. */
724 err
= mach_port_allocate (mach_task_self (),
725 MACH_PORT_RIGHT_RECEIVE
, &inf
->event_port
);
727 error ("Error allocating event port: %s", safe_strerror (err
));
729 /* Make a send right for it, so we can easily copy it for other people. */
730 mach_port_insert_right (mach_task_self (), inf
->event_port
,
731 inf
->event_port
, MACH_MSG_TYPE_MAKE_SEND
);
732 inf_set_pid (inf
, pid
);
736 /* Close current process, if any, and attach INF to process PORT. */
738 inf_set_pid (struct inf
*inf
, pid_t pid
)
741 struct proc
*task
= inf
->task
;
743 inf_debug (inf
, "setting pid: %d", pid
);
746 task_port
= MACH_PORT_NULL
;
749 error_t err
= proc_pid2task (proc_server
, pid
, &task_port
);
751 error ("Error getting task for pid %d: %s", pid
, safe_strerror (err
));
754 inf_debug (inf
, "setting task: %d", task_port
);
757 task_suspend (task_port
);
759 if (task
&& task
->port
!= task_port
)
762 inf_validate_procs (inf
); /* Trash all the threads. */
763 _proc_free (task
); /* And the task. */
766 if (task_port
!= MACH_PORT_NULL
)
768 inf
->task
= make_proc (inf
, task_port
, PROC_TID_TASK
);
769 inf
->threads_up_to_date
= 0;
776 /* Reflect task_suspend above. */
777 inf
->task
->sc
= inf
->task
->cur_sc
= 1;
784 /* Validates INF's stopped, nomsg and traced field from the actual
785 proc server state. Note that the traced field is only updated from
786 the proc server state if we do not have a message port. If we do
787 have a message port we'd better look at the tracemask itself. */
789 inf_validate_procinfo (struct inf
*inf
)
792 mach_msg_type_number_t noise_len
= 0;
794 mach_msg_type_number_t pi_len
= 0;
797 proc_getprocinfo (proc_server
, inf
->pid
, &info_flags
,
798 (procinfo_t
*) &pi
, &pi_len
, &noise
, &noise_len
);
802 inf
->stopped
= !!(pi
->state
& PI_STOPPED
);
803 inf
->nomsg
= !!(pi
->state
& PI_NOMSG
);
805 inf
->traced
= !!(pi
->state
& PI_TRACED
);
806 vm_deallocate (mach_task_self (), (vm_address_t
) pi
, pi_len
);
808 vm_deallocate (mach_task_self (), (vm_address_t
) noise
, noise_len
);
812 /* Validates INF's task suspend count. If it's higher than we expect,
813 verify with the user before `stealing' the extra count. */
815 inf_validate_task_sc (struct inf
*inf
)
818 mach_msg_type_number_t noise_len
= 0;
820 mach_msg_type_number_t pi_len
= 0;
821 int info_flags
= PI_FETCH_TASKINFO
;
822 int suspend_count
= -1;
826 err
= proc_getprocinfo (proc_server
, inf
->pid
, &info_flags
,
827 (procinfo_t
*) &pi
, &pi_len
, &noise
, &noise_len
);
830 inf
->task
->dead
= 1; /* oh well */
834 if (inf
->task
->cur_sc
< pi
->taskinfo
.suspend_count
&& suspend_count
== -1)
836 /* The proc server might have suspended the task while stopping
837 it. This happens when the task is handling a traced signal.
838 Refetch the suspend count. The proc server should be
839 finished stopping the task by now. */
840 suspend_count
= pi
->taskinfo
.suspend_count
;
844 suspend_count
= pi
->taskinfo
.suspend_count
;
846 vm_deallocate (mach_task_self (), (vm_address_t
) pi
, pi_len
);
848 vm_deallocate (mach_task_self (), (vm_address_t
) pi
, pi_len
);
850 if (inf
->task
->cur_sc
< suspend_count
)
854 target_terminal_ours (); /* Allow I/O. */
855 abort
= !query ("Pid %d has an additional task suspend count of %d;"
856 " clear it? ", inf
->pid
,
857 suspend_count
- inf
->task
->cur_sc
);
858 target_terminal_inferior (); /* Give it back to the child. */
861 error ("Additional task suspend count left untouched.");
863 inf
->task
->cur_sc
= suspend_count
;
867 /* Turns tracing for INF on or off, depending on ON, unless it already
868 is. If INF is running, the resume_sc count of INF's threads will
869 be modified, and the signal thread will briefly be run to change
872 inf_set_traced (struct inf
*inf
, int on
)
874 if (on
== inf
->traced
)
877 if (inf
->task
&& !inf
->task
->dead
)
878 /* Make it take effect immediately. */
880 sigset_t mask
= on
? ~(sigset_t
) 0 : 0;
882 INF_RESUME_MSGPORT_RPC (inf
, msg_set_init_int (msgport
, refport
,
883 INIT_TRACEMASK
, mask
));
887 warning ("Can't modify tracing state for pid %d: %s",
888 inf
->pid
, "No signal thread");
892 warning ("Can't modify tracing state for pid %d: %s",
893 inf
->pid
, safe_strerror (err
));
902 /* Makes all the real suspend count deltas of all the procs in INF
903 match the desired values. Careful to always do thread/task suspend
904 counts in the safe order. Returns true if at least one thread is
905 thought to be running. */
907 inf_update_suspends (struct inf
*inf
)
909 struct proc
*task
= inf
->task
;
910 /* We don't have to update INF->threads even though we're iterating over it
911 because we'll change a thread only if it already has an existing proc
914 inf_debug (inf
, "updating suspend counts");
919 int task_running
= (task
->sc
== 0), thread_running
= 0;
921 if (task
->sc
> task
->cur_sc
)
922 /* The task is becoming _more_ suspended; do before any threads. */
923 task_running
= proc_update_sc (task
);
925 if (inf
->pending_execs
)
926 /* When we're waiting for an exec, things may be happening behind our
927 back, so be conservative. */
930 /* Do all the thread suspend counts. */
931 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
932 thread_running
|= proc_update_sc (thread
);
934 if (task
->sc
!= task
->cur_sc
)
935 /* We didn't do the task first, because we wanted to wait for the
936 threads; do it now. */
937 task_running
= proc_update_sc (task
);
939 inf_debug (inf
, "%srunning...",
940 (thread_running
&& task_running
) ? "" : "not ");
942 inf
->running
= thread_running
&& task_running
;
944 /* Once any thread has executed some code, we can't depend on the
945 threads list any more. */
947 inf
->threads_up_to_date
= 0;
956 /* Converts a GDB pid to a struct proc. */
958 inf_tid_to_thread (struct inf
*inf
, int tid
)
960 struct proc
*thread
= inf
->threads
;
963 if (thread
->tid
== tid
)
966 thread
= thread
->next
;
970 /* Converts a thread port to a struct proc. */
972 inf_port_to_thread (struct inf
*inf
, mach_port_t port
)
974 struct proc
*thread
= inf
->threads
;
976 if (thread
->port
== port
)
979 thread
= thread
->next
;
984 /* Make INF's list of threads be consistent with reality of TASK. */
986 inf_validate_procs (struct inf
*inf
)
988 thread_array_t threads
;
989 mach_msg_type_number_t num_threads
, i
;
990 struct proc
*task
= inf
->task
;
992 /* If no threads are currently running, this function will guarantee that
993 things are up to date. The exception is if there are zero threads --
994 then it is almost certainly in an odd state, and probably some outside
995 agent will create threads. */
996 inf
->threads_up_to_date
= inf
->threads
? !inf
->running
: 0;
1000 error_t err
= task_threads (task
->port
, &threads
, &num_threads
);
1001 inf_debug (inf
, "fetching threads");
1003 /* TASK must be dead. */
1013 inf_debug (inf
, "no task");
1017 /* Make things normally linear. */
1018 mach_msg_type_number_t search_start
= 0;
1019 /* Which thread in PROCS corresponds to each task thread, & the task. */
1020 struct proc
*matched
[num_threads
+ 1];
1021 /* The last thread in INF->threads, so we can add to the end. */
1022 struct proc
*last
= 0;
1023 /* The current thread we're considering. */
1024 struct proc
*thread
= inf
->threads
;
1026 bzero (matched
, sizeof (matched
));
1030 mach_msg_type_number_t left
;
1032 for (i
= search_start
, left
= num_threads
; left
; i
++, left
--)
1034 if (i
>= num_threads
)
1035 i
-= num_threads
; /* I wrapped around. */
1036 if (thread
->port
== threads
[i
])
1037 /* We already know about this thread. */
1039 matched
[i
] = thread
;
1041 thread
= thread
->next
;
1049 proc_debug (thread
, "died!");
1050 thread
->port
= MACH_PORT_NULL
;
1051 thread
= _proc_free (thread
); /* THREAD is dead. */
1052 (last
? last
->next
: inf
->threads
) = thread
;
1056 for (i
= 0; i
< num_threads
; i
++)
1059 /* Throw away the duplicate send right. */
1060 mach_port_deallocate (mach_task_self (), threads
[i
]);
1062 /* THREADS[I] is a thread we don't know about yet! */
1064 thread
= make_proc (inf
, threads
[i
], next_thread_id
++);
1065 (last
? last
->next
: inf
->threads
) = thread
;
1067 proc_debug (thread
, "new thread: %d", threads
[i
]);
1068 add_thread (pid_to_ptid (thread
->tid
)); /* Tell GDB's generic thread code. */
1072 vm_deallocate (mach_task_self (),
1073 (vm_address_t
) threads
, (num_threads
* sizeof (thread_t
)));
1078 /* Makes sure that INF's thread list is synced with the actual process. */
1080 inf_update_procs (struct inf
*inf
)
1084 if (!inf
->threads_up_to_date
)
1085 inf_validate_procs (inf
);
1089 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
1090 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1093 inf_set_threads_resume_sc (struct inf
*inf
,
1094 struct proc
*run_thread
, int run_others
)
1096 struct proc
*thread
;
1097 inf_update_procs (inf
);
1098 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1099 if (thread
== run_thread
)
1100 thread
->resume_sc
= 0;
1101 else if (run_others
)
1102 thread
->resume_sc
= thread
->run_sc
;
1104 thread
->resume_sc
= thread
->pause_sc
;
1108 /* Cause INF to continue execution immediately; individual threads may still
1109 be suspended (but their suspend counts will be updated). */
1111 inf_resume (struct inf
*inf
)
1113 struct proc
*thread
;
1115 inf_update_procs (inf
);
1117 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1118 thread
->sc
= thread
->resume_sc
;
1122 if (!inf
->pending_execs
)
1123 /* Try to make sure our task count is correct -- in the case where
1124 we're waiting for an exec though, things are too volatile, so just
1125 assume things will be reasonable (which they usually will be). */
1126 inf_validate_task_sc (inf
);
1130 inf_update_suspends (inf
);
1133 /* Cause INF to stop execution immediately; individual threads may still
1136 inf_suspend (struct inf
*inf
)
1138 struct proc
*thread
;
1140 inf_update_procs (inf
);
1142 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1143 thread
->sc
= thread
->pause_sc
;
1146 inf
->task
->sc
= inf
->pause_sc
;
1148 inf_update_suspends (inf
);
1152 /* INF has one thread PROC that is in single-stepping mode. This
1153 function changes it to be PROC, changing any old step_thread to be
1154 a normal one. A PROC of 0 clears any existing value. */
1156 inf_set_step_thread (struct inf
*inf
, struct proc
*thread
)
1158 gdb_assert (!thread
|| proc_is_thread (thread
));
1161 inf_debug (inf
, "setting step thread: %d/%d", inf
->pid
, thread
->tid
);
1163 inf_debug (inf
, "clearing step thread");
1165 if (inf
->step_thread
!= thread
)
1167 if (inf
->step_thread
&& inf
->step_thread
->port
!= MACH_PORT_NULL
)
1168 if (!proc_trace (inf
->step_thread
, 0))
1170 if (thread
&& proc_trace (thread
, 1))
1171 inf
->step_thread
= thread
;
1173 inf
->step_thread
= 0;
1178 /* Set up the thread resume_sc's so that only the signal thread is running
1179 (plus whatever other thread are set to always run). Returns true if we
1180 did so, or false if we can't find a signal thread. */
1182 inf_set_threads_resume_sc_for_signal_thread (struct inf
*inf
)
1184 if (inf
->signal_thread
)
1186 inf_set_threads_resume_sc (inf
, inf
->signal_thread
, 0);
1194 inf_update_signal_thread (struct inf
*inf
)
1196 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1197 the signal thread. */
1198 inf
->signal_thread
= inf
->threads
? inf
->threads
->next
: 0;
1202 /* Detachs from INF's inferior task, letting it run once again... */
1204 inf_detach (struct inf
*inf
)
1206 struct proc
*task
= inf
->task
;
1208 inf_debug (inf
, "detaching...");
1210 inf_clear_wait (inf
);
1211 inf_set_step_thread (inf
, 0);
1215 struct proc
*thread
;
1217 inf_validate_procinfo (inf
);
1219 inf_set_traced (inf
, 0);
1225 inf_signal (inf
, TARGET_SIGNAL_0
);
1228 proc_restore_exc_port (task
);
1229 task
->sc
= inf
->detach_sc
;
1231 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1233 proc_restore_exc_port (thread
);
1234 thread
->sc
= thread
->detach_sc
;
1237 inf_update_suspends (inf
);
1243 /* Attaches INF to the process with process id PID, returning it in a
1244 suspended state suitable for debugging. */
1246 inf_attach (struct inf
*inf
, int pid
)
1248 inf_debug (inf
, "attaching: %d", pid
);
1253 inf_startup (inf
, pid
);
1257 /* Makes sure that we've got our exception ports entrenched in the process. */
1259 inf_steal_exc_ports (struct inf
*inf
)
1261 struct proc
*thread
;
1263 inf_debug (inf
, "stealing exception ports");
1265 inf_set_step_thread (inf
, 0); /* The step thread is special. */
1267 proc_steal_exc_port (inf
->task
, inf
->event_port
);
1268 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1269 proc_steal_exc_port (thread
, MACH_PORT_NULL
);
1272 /* Makes sure the process has its own exception ports. */
1274 inf_restore_exc_ports (struct inf
*inf
)
1276 struct proc
*thread
;
1278 inf_debug (inf
, "restoring exception ports");
1280 inf_set_step_thread (inf
, 0); /* The step thread is special. */
1282 proc_restore_exc_port (inf
->task
);
1283 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1284 proc_restore_exc_port (thread
);
1288 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1289 signal 0, will continue it. INF is assumed to be in a paused state, and
1290 the resume_sc's of INF's threads may be affected. */
1292 inf_signal (struct inf
*inf
, enum target_signal sig
)
1295 int host_sig
= target_signal_to_host (sig
);
1297 #define NAME target_signal_to_name (sig)
1299 if (host_sig
>= _NSIG
)
1300 /* A mach exception. Exceptions are encoded in the signal space by
1301 putting them after _NSIG; this assumes they're positive (and not
1302 extremely large)! */
1304 struct inf_wait
*w
= &inf
->wait
;
1305 if (w
->status
.kind
== TARGET_WAITKIND_STOPPED
1306 && w
->status
.value
.sig
== sig
1307 && w
->thread
&& !w
->thread
->aborted
)
1308 /* We're passing through the last exception we received. This is
1309 kind of bogus, because exceptions are per-thread whereas gdb
1310 treats signals as per-process. We just forward the exception to
1311 the correct handler, even it's not for the same thread as TID --
1312 i.e., we pretend it's global. */
1314 struct exc_state
*e
= &w
->exc
;
1315 inf_debug (inf
, "passing through exception:"
1316 " task = %d, thread = %d, exc = %d"
1317 ", code = %d, subcode = %d",
1318 w
->thread
->port
, inf
->task
->port
,
1319 e
->exception
, e
->code
, e
->subcode
);
1321 exception_raise_request (e
->handler
,
1322 e
->reply
, MACH_MSG_TYPE_MOVE_SEND_ONCE
,
1323 w
->thread
->port
, inf
->task
->port
,
1324 e
->exception
, e
->code
, e
->subcode
);
1327 error ("Can't forward spontaneous exception (%s).", NAME
);
1330 /* A Unix signal. */
1332 /* The process is stopped and expecting a signal. Just send off a
1333 request and let it get handled when we resume everything. */
1335 inf_debug (inf
, "sending %s to stopped process", NAME
);
1337 INF_MSGPORT_RPC (inf
,
1338 msg_sig_post_untraced_request (msgport
,
1340 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1344 /* Posting an untraced signal automatically continues it.
1345 We clear this here rather than when we get the reply
1346 because we'd rather assume it's not stopped when it
1347 actually is, than the reverse. */
1351 /* It's not expecting it. We have to let just the signal thread
1352 run, and wait for it to get into a reasonable state before we
1353 can continue the rest of the process. When we finally resume the
1354 process the signal we request will be the very first thing that
1357 inf_debug (inf
, "sending %s to unstopped process"
1358 " (so resuming signal thread)", NAME
);
1360 INF_RESUME_MSGPORT_RPC (inf
,
1361 msg_sig_post_untraced (msgport
, host_sig
,
1366 /* Can't do too much... */
1367 warning ("Can't deliver signal %s: No signal thread.", NAME
);
1369 warning ("Delivering signal %s: %s", NAME
, safe_strerror (err
));
1375 /* Continue INF without delivering a signal. This is meant to be used
1376 when INF does not have a message port. */
1378 inf_continue (struct inf
*inf
)
1381 error_t err
= proc_pid2proc (proc_server
, inf
->pid
, &proc
);
1385 inf_debug (inf
, "continuing process");
1387 err
= proc_mark_cont (proc
);
1390 struct proc
*thread
;
1392 for (thread
= inf
->threads
; thread
; thread
= thread
->next
)
1393 thread_resume (thread
->port
);
1400 warning ("Can't continue process: %s", safe_strerror (err
));
1404 /* The inferior used for all gdb target ops. */
1405 struct inf
*current_inferior
= 0;
1407 /* The inferior being waited for by gnu_wait. Since GDB is decidely not
1408 multi-threaded, we don't bother to lock this. */
1409 struct inf
*waiting_inf
;
1411 /* Wait for something to happen in the inferior, returning what in STATUS. */
1413 gnu_wait (ptid_t tid
, struct target_waitstatus
*status
)
1417 mach_msg_header_t hdr
;
1418 mach_msg_type_t type
;
1422 struct proc
*thread
;
1423 struct inf
*inf
= current_inferior
;
1425 extern int exc_server (mach_msg_header_t
*, mach_msg_header_t
*);
1426 extern int msg_reply_server (mach_msg_header_t
*, mach_msg_header_t
*);
1427 extern int notify_server (mach_msg_header_t
*, mach_msg_header_t
*);
1428 extern int process_reply_server (mach_msg_header_t
*, mach_msg_header_t
*);
1430 gdb_assert (inf
->task
);
1432 if (!inf
->threads
&& !inf
->pending_execs
)
1433 /* No threads! Assume that maybe some outside agency is frobbing our
1434 task, and really look for new threads. If we can't find any, just tell
1435 the user to try again later. */
1437 inf_validate_procs (inf
);
1438 if (!inf
->threads
&& !inf
->task
->dead
)
1439 error ("There are no threads; try again later.");
1444 inf_debug (inf
, "waiting for: %d", PIDGET (tid
));
1447 if (proc_wait_pid
!= inf
->pid
&& !inf
->no_wait
)
1448 /* Always get information on events from the proc server. */
1450 inf_debug (inf
, "requesting wait on pid %d", inf
->pid
);
1453 /* The proc server is single-threaded, and only allows a single
1454 outstanding wait request, so we have to cancel the previous one. */
1456 inf_debug (inf
, "cancelling previous wait on pid %d", proc_wait_pid
);
1457 interrupt_operation (proc_server
, 0);
1461 proc_wait_request (proc_server
, inf
->event_port
, inf
->pid
, WUNTRACED
);
1463 warning ("wait request failed: %s", safe_strerror (err
));
1466 inf_debug (inf
, "waits pending: %d", proc_waits_pending
);
1467 proc_wait_pid
= inf
->pid
;
1468 /* Even if proc_waits_pending was > 0 before, we still won't
1469 get any other replies, because it was either from a
1470 different INF, or a different process attached to INF --
1471 and the event port, which is the wait reply port, changes
1472 when you switch processes. */
1473 proc_waits_pending
= 1;
1477 inf_clear_wait (inf
);
1479 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1480 (3) wait reply from the proc server. */
1482 inf_debug (inf
, "waiting for an event...");
1483 err
= mach_msg (&msg
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
,
1484 0, sizeof (struct msg
), inf
->event_port
,
1485 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1487 /* Re-suspend the task. */
1490 if (!inf
->task
&& inf
->pending_execs
)
1491 /* When doing an exec, it's possible that the old task wasn't reused
1492 (e.g., setuid execs). So if the task seems to have disappeared,
1493 attempt to refetch it, as the pid should still be the same. */
1494 inf_set_pid (inf
, inf
->pid
);
1496 if (err
== EMACH_RCV_INTERRUPTED
)
1497 inf_debug (inf
, "interrupted");
1499 error ("Couldn't wait for an event: %s", safe_strerror (err
));
1504 mach_msg_header_t hdr
;
1505 mach_msg_type_t err_type
;
1511 inf_debug (inf
, "event: msgid = %d", msg
.hdr
.msgh_id
);
1513 /* Handle what we got. */
1514 if (!notify_server (&msg
.hdr
, &reply
.hdr
)
1515 && !exc_server (&msg
.hdr
, &reply
.hdr
)
1516 && !process_reply_server (&msg
.hdr
, &reply
.hdr
)
1517 && !msg_reply_server (&msg
.hdr
, &reply
.hdr
))
1518 /* Whatever it is, it's something strange. */
1519 error ("Got a strange event, msg id = %d.", msg
.hdr
.msgh_id
);
1522 error ("Handling event, msgid = %d: %s",
1523 msg
.hdr
.msgh_id
, safe_strerror (reply
.err
));
1526 if (inf
->pending_execs
)
1527 /* We're waiting for the inferior to finish execing. */
1529 struct inf_wait
*w
= &inf
->wait
;
1530 enum target_waitkind kind
= w
->status
.kind
;
1532 if (kind
== TARGET_WAITKIND_SPURIOUS
)
1533 /* Since gdb is actually counting the number of times the inferior
1534 stops, expecting one stop per exec, we only return major events
1538 inf_debug (inf
, "pending_execs = %d, ignoring minor event",
1539 inf
->pending_execs
);
1541 else if (kind
== TARGET_WAITKIND_STOPPED
1542 && w
->status
.value
.sig
== TARGET_SIGNAL_TRAP
)
1543 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1544 means we've succesfully completed an exec! */
1546 if (--inf
->pending_execs
== 0)
1549 #if 0 /* do we need this? */
1550 prune_threads (1); /* Get rid of the old shell threads */
1551 renumber_threads (0); /* Give our threads reasonable names. */
1554 inf_debug (inf
, "pending exec completed, pending_execs => %d",
1555 inf
->pending_execs
);
1557 else if (kind
== TARGET_WAITKIND_STOPPED
)
1558 /* It's possible that this signal is because of a crashed process
1559 being handled by the hurd crash server; in this case, the process
1560 will have an extra task suspend, which we need to know about.
1561 Since the code in inf_resume that normally checks for this is
1562 disabled while INF->pending_execs, we do the check here instead. */
1563 inf_validate_task_sc (inf
);
1566 if (inf
->wait
.suppress
)
1567 /* Some totally spurious event happened that we don't consider
1568 worth returning to gdb. Just keep waiting. */
1570 inf_debug (inf
, "suppressing return, rewaiting...");
1575 /* Pass back out our results. */
1576 bcopy (&inf
->wait
.status
, status
, sizeof (*status
));
1578 thread
= inf
->wait
.thread
;
1580 tid
= pid_to_ptid (thread
->tid
);
1582 thread
= inf_tid_to_thread (inf
, PIDGET (tid
));
1584 if (!thread
|| thread
->port
== MACH_PORT_NULL
)
1586 /* TID is dead; try and find a new thread. */
1587 if (inf_update_procs (inf
) && inf
->threads
)
1588 tid
= pid_to_ptid (inf
->threads
->tid
); /* The first available thread. */
1590 tid
= inferior_ptid
; /* let wait_for_inferior handle exit case */
1593 if (thread
&& PIDGET (tid
) >= 0 && status
->kind
!= TARGET_WAITKIND_SPURIOUS
1594 && inf
->pause_sc
== 0 && thread
->pause_sc
== 0)
1595 /* If something actually happened to THREAD, make sure we
1599 inf_update_suspends (inf
);
1602 inf_debug (inf
, "returning tid = %d, status = %s (%d)", PIDGET (tid
),
1603 status
->kind
== TARGET_WAITKIND_EXITED
? "EXITED"
1604 : status
->kind
== TARGET_WAITKIND_STOPPED
? "STOPPED"
1605 : status
->kind
== TARGET_WAITKIND_SIGNALLED
? "SIGNALLED"
1606 : status
->kind
== TARGET_WAITKIND_LOADED
? "LOADED"
1607 : status
->kind
== TARGET_WAITKIND_SPURIOUS
? "SPURIOUS"
1609 status
->value
.integer
);
1615 /* The rpc handler called by exc_server. */
1617 S_exception_raise_request (mach_port_t port
, mach_port_t reply_port
,
1618 thread_t thread_port
, task_t task_port
,
1619 int exception
, int code
, int subcode
)
1621 struct inf
*inf
= waiting_inf
;
1622 struct proc
*thread
= inf_port_to_thread (inf
, thread_port
);
1624 inf_debug (waiting_inf
,
1625 "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
1626 thread_port
, task_port
, exception
, code
, subcode
);
1629 /* We don't know about thread? */
1631 inf_update_procs (inf
);
1632 thread
= inf_port_to_thread (inf
, thread_port
);
1634 /* Give up, the generating thread is gone. */
1638 mach_port_deallocate (mach_task_self (), thread_port
);
1639 mach_port_deallocate (mach_task_self (), task_port
);
1641 if (!thread
->aborted
)
1642 /* THREAD hasn't been aborted since this exception happened (abortion
1643 clears any exception state), so it must be real. */
1645 /* Store away the details; this will destroy any previous info. */
1646 inf
->wait
.thread
= thread
;
1648 inf
->wait
.status
.kind
= TARGET_WAITKIND_STOPPED
;
1650 if (exception
== EXC_BREAKPOINT
)
1651 /* GDB likes to get SIGTRAP for breakpoints. */
1653 inf
->wait
.status
.value
.sig
= TARGET_SIGNAL_TRAP
;
1654 mach_port_deallocate (mach_task_self (), reply_port
);
1657 /* Record the exception so that we can forward it later. */
1659 if (thread
->exc_port
== port
)
1661 inf_debug (waiting_inf
, "Handler is thread exception port <%d>",
1662 thread
->saved_exc_port
);
1663 inf
->wait
.exc
.handler
= thread
->saved_exc_port
;
1667 inf_debug (waiting_inf
, "Handler is task exception port <%d>",
1668 inf
->task
->saved_exc_port
);
1669 inf
->wait
.exc
.handler
= inf
->task
->saved_exc_port
;
1670 gdb_assert (inf
->task
->exc_port
== port
);
1672 if (inf
->wait
.exc
.handler
!= MACH_PORT_NULL
)
1673 /* Add a reference to the exception handler. */
1674 mach_port_mod_refs (mach_task_self (),
1675 inf
->wait
.exc
.handler
, MACH_PORT_RIGHT_SEND
,
1678 inf
->wait
.exc
.exception
= exception
;
1679 inf
->wait
.exc
.code
= code
;
1680 inf
->wait
.exc
.subcode
= subcode
;
1681 inf
->wait
.exc
.reply
= reply_port
;
1683 /* Exceptions are encoded in the signal space by putting them after
1684 _NSIG; this assumes they're positive (and not extremely large)! */
1685 inf
->wait
.status
.value
.sig
=
1686 target_signal_from_host (_NSIG
+ exception
);
1690 /* A supppressed exception, which ignore. */
1692 inf
->wait
.suppress
= 1;
1693 mach_port_deallocate (mach_task_self (), reply_port
);
1700 /* Fill in INF's wait field after a task has died without giving us more
1701 detailed information. */
1703 inf_task_died_status (struct inf
*inf
)
1705 warning ("Pid %d died with unknown exit status, using SIGKILL.", inf
->pid
);
1706 inf
->wait
.status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1707 inf
->wait
.status
.value
.sig
= TARGET_SIGNAL_KILL
;
1710 /* Notify server routines. The only real one is dead name notification. */
1712 do_mach_notify_dead_name (mach_port_t notify
, mach_port_t dead_port
)
1714 struct inf
*inf
= waiting_inf
;
1716 inf_debug (waiting_inf
, "port = %d", dead_port
);
1718 if (inf
->task
&& inf
->task
->port
== dead_port
)
1720 proc_debug (inf
->task
, "is dead");
1721 inf
->task
->port
= MACH_PORT_NULL
;
1722 if (proc_wait_pid
== inf
->pid
)
1723 /* We have a wait outstanding on the process, which will return more
1724 detailed information, so delay until we get that. */
1725 inf
->wait
.suppress
= 1;
1727 /* We never waited for the process (maybe it wasn't a child), so just
1728 pretend it got a SIGKILL. */
1729 inf_task_died_status (inf
);
1733 struct proc
*thread
= inf_port_to_thread (inf
, dead_port
);
1736 proc_debug (thread
, "is dead");
1737 thread
->port
= MACH_PORT_NULL
;
1740 if (inf
->task
->dead
)
1741 /* Since the task is dead, its threads are dying with it. */
1742 inf
->wait
.suppress
= 1;
1745 mach_port_deallocate (mach_task_self (), dead_port
);
1746 inf
->threads_up_to_date
= 0; /* Just in case */
1755 warning ("illegal rpc: %s", fun
);
1760 do_mach_notify_no_senders (mach_port_t notify
, mach_port_mscount_t count
)
1762 return ill_rpc (__FUNCTION__
);
1766 do_mach_notify_port_deleted (mach_port_t notify
, mach_port_t name
)
1768 return ill_rpc (__FUNCTION__
);
1772 do_mach_notify_msg_accepted (mach_port_t notify
, mach_port_t name
)
1774 return ill_rpc (__FUNCTION__
);
1778 do_mach_notify_port_destroyed (mach_port_t notify
, mach_port_t name
)
1780 return ill_rpc (__FUNCTION__
);
1784 do_mach_notify_send_once (mach_port_t notify
)
1786 return ill_rpc (__FUNCTION__
);
1790 /* Process_reply server routines. We only use process_wait_reply. */
1793 S_proc_wait_reply (mach_port_t reply
, error_t err
,
1794 int status
, int sigcode
, rusage_t rusage
, pid_t pid
)
1796 struct inf
*inf
= waiting_inf
;
1798 inf_debug (inf
, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1799 err
? safe_strerror (err
) : "0", pid
, status
, sigcode
);
1801 if (err
&& proc_wait_pid
&& (!inf
->task
|| !inf
->task
->port
))
1802 /* Ack. The task has died, but the task-died notification code didn't
1803 tell anyone because it thought a more detailed reply from the
1804 procserver was forthcoming. However, we now learn that won't
1805 happen... So we have to act like the task just died, and this time,
1807 inf_task_died_status (inf
);
1809 if (--proc_waits_pending
== 0)
1810 /* PROC_WAIT_PID represents the most recent wait. We will always get
1811 replies in order because the proc server is single threaded. */
1814 inf_debug (inf
, "waits pending now: %d", proc_waits_pending
);
1820 warning ("Can't wait for pid %d: %s", inf
->pid
, safe_strerror (err
));
1823 /* Since we can't see the inferior's signals, don't trap them. */
1824 inf_set_traced (inf
, 0);
1827 else if (pid
== inf
->pid
)
1829 store_waitstatus (&inf
->wait
.status
, status
);
1830 if (inf
->wait
.status
.kind
== TARGET_WAITKIND_STOPPED
)
1831 /* The process has sent us a signal, and stopped itself in a sane
1832 state pending our actions. */
1834 inf_debug (inf
, "process has stopped itself");
1839 inf
->wait
.suppress
= 1; /* Something odd happened. Ignore. */
1845 S_proc_setmsgport_reply (mach_port_t reply
, error_t err
,
1846 mach_port_t old_msg_port
)
1848 return ill_rpc (__FUNCTION__
);
1852 S_proc_getmsgport_reply (mach_port_t reply
, error_t err
, mach_port_t msg_port
)
1854 return ill_rpc (__FUNCTION__
);
1858 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1861 S_msg_sig_post_untraced_reply (mach_port_t reply
, error_t err
)
1863 struct inf
*inf
= waiting_inf
;
1866 /* EBUSY is what we get when the crash server has grabbed control of the
1867 process and doesn't like what signal we tried to send it. Just act
1868 like the process stopped (using a signal of 0 should mean that the
1869 *next* time the user continues, it will pass signal 0, which the crash
1870 server should like). */
1872 inf
->wait
.status
.kind
= TARGET_WAITKIND_STOPPED
;
1873 inf
->wait
.status
.value
.sig
= TARGET_SIGNAL_0
;
1876 warning ("Signal delivery failed: %s", safe_strerror (err
));
1879 /* We only get this reply when we've posted a signal to a process which we
1880 thought was stopped, and which we expected to continue after the signal.
1881 Given that the signal has failed for some reason, it's reasonable to
1882 assume it's still stopped. */
1885 inf
->wait
.suppress
= 1;
1891 S_msg_sig_post_reply (mach_port_t reply
, error_t err
)
1893 return ill_rpc (__FUNCTION__
);
1897 /* Returns the number of messages queued for the receive right PORT. */
1898 static mach_port_msgcount_t
1899 port_msgs_queued (mach_port_t port
)
1901 struct mach_port_status status
;
1903 mach_port_get_receive_status (mach_task_self (), port
, &status
);
1908 return status
.mps_msgcount
;
1912 /* Resume execution of the inferior process.
1914 If STEP is nonzero, single-step it.
1915 If SIGNAL is nonzero, give it that signal.
1918 -1 true Single step the current thread allowing other threads to run.
1919 -1 false Continue the current thread allowing other threads to run.
1920 X true Single step the given thread, don't allow any others to run.
1921 X false Continue the given thread, do not allow any others to run.
1922 (Where X, of course, is anything except -1)
1924 Note that a resume may not `take' if there are pending exceptions/&c
1925 still unprocessed from the last resume we did (any given resume may result
1926 in multiple events returned by wait).
1929 gnu_resume (ptid_t tid
, int step
, enum target_signal sig
)
1931 struct proc
*step_thread
= 0;
1932 struct inf
*inf
= current_inferior
;
1934 inf_debug (inf
, "tid = %d, step = %d, sig = %d", PIDGET (tid
), step
, sig
);
1936 inf_validate_procinfo (inf
);
1938 if (sig
!= TARGET_SIGNAL_0
|| inf
->stopped
)
1940 if (sig
== TARGET_SIGNAL_0
&& inf
->nomsg
)
1943 inf_signal (inf
, sig
);
1945 else if (inf
->wait
.exc
.reply
!= MACH_PORT_NULL
)
1946 /* We received an exception to which we have chosen not to forward, so
1947 abort the faulting thread, which will perhaps retake it. */
1949 proc_abort (inf
->wait
.thread
, 1);
1950 warning ("Aborting %s with unforwarded exception %s.",
1951 proc_string (inf
->wait
.thread
),
1952 target_signal_to_name (inf
->wait
.status
.value
.sig
));
1955 if (port_msgs_queued (inf
->event_port
))
1956 /* If there are still messages in our event queue, don't bother resuming
1957 the process, as we're just going to stop it right away anyway. */
1960 inf_update_procs (inf
);
1962 if (PIDGET (tid
) < 0)
1963 /* Allow all threads to run, except perhaps single-stepping one. */
1965 inf_debug (inf
, "running all threads; tid = %d", PIDGET (inferior_ptid
));
1966 tid
= inferior_ptid
; /* What to step. */
1967 inf_set_threads_resume_sc (inf
, 0, 1);
1970 /* Just allow a single thread to run. */
1972 struct proc
*thread
= inf_tid_to_thread (inf
, PIDGET (tid
));
1974 error ("Can't run single thread id %d: no such thread!");
1975 inf_debug (inf
, "running one thread: %d/%d", inf
->pid
, thread
->tid
);
1976 inf_set_threads_resume_sc (inf
, thread
, 0);
1981 step_thread
= inf_tid_to_thread (inf
, PIDGET (tid
));
1983 warning ("Can't step thread id %d: no such thread.", PIDGET (tid
));
1985 inf_debug (inf
, "stepping thread: %d/%d", inf
->pid
, step_thread
->tid
);
1987 if (step_thread
!= inf
->step_thread
)
1988 inf_set_step_thread (inf
, step_thread
);
1990 inf_debug (inf
, "here we go...");
1996 gnu_kill_inferior (void)
1998 struct proc
*task
= current_inferior
->task
;
2001 proc_debug (task
, "terminating...");
2002 task_terminate (task
->port
);
2003 inf_set_pid (current_inferior
, -1);
2005 target_mourn_inferior ();
2008 /* Clean up after the inferior dies. */
2010 gnu_mourn_inferior (void)
2012 inf_debug (current_inferior
, "rip");
2013 inf_detach (current_inferior
);
2014 unpush_target (&gnu_ops
);
2015 generic_mourn_inferior ();
2019 /* Fork an inferior process, and start debugging it. */
2021 /* Set INFERIOR_PID to the first thread available in the child, if any. */
2023 inf_pick_first_thread (void)
2025 if (current_inferior
->task
&& current_inferior
->threads
)
2026 /* The first thread. */
2027 return current_inferior
->threads
->tid
;
2029 /* What may be the next thread. */
2030 return next_thread_id
;
2036 if (!current_inferior
)
2037 current_inferior
= make_inf ();
2038 return current_inferior
;
2042 gnu_create_inferior (char *exec_file
, char *allargs
, char **env
)
2044 struct inf
*inf
= cur_inf ();
2048 /* We're in the child; make this process stop as soon as it execs. */
2049 inf_debug (inf
, "tracing self");
2050 if (ptrace (PTRACE_TRACEME
) != 0)
2051 error ("ptrace (PTRACE_TRACEME) failed!");
2053 void attach_to_child (int pid
)
2055 /* Attach to the now stopped child, which is actually a shell... */
2056 inf_debug (inf
, "attaching to child: %d", pid
);
2058 inf_attach (inf
, pid
);
2061 push_target (&gnu_ops
);
2063 inf
->pending_execs
= 2;
2067 /* Now let the child run again, knowing that it will stop immediately
2068 because of the ptrace. */
2070 inferior_ptid
= pid_to_ptid (inf_pick_first_thread ());
2072 startup_inferior (inf
->pending_execs
);
2075 inf_debug (inf
, "creating inferior");
2077 fork_inferior (exec_file
, allargs
, env
, trace_me
, attach_to_child
,
2080 inf_validate_procinfo (inf
);
2081 inf_update_signal_thread (inf
);
2082 inf_set_traced (inf
, inf
->want_signals
);
2084 /* Execing the process will have trashed our exception ports; steal them
2085 back (or make sure they're restored if the user wants that). */
2086 if (inf
->want_exceptions
)
2087 inf_steal_exc_ports (inf
);
2089 inf_restore_exc_ports (inf
);
2092 proceed ((CORE_ADDR
) -1, 0, 0);
2095 /* Mark our target-struct as eligible for stray "run" and "attach"
2104 #ifdef ATTACH_DETACH
2106 /* Attach to process PID, then initialize for debugging it
2107 and wait for the trace-trap that results from attaching. */
2109 gnu_attach (char *args
, int from_tty
)
2113 struct inf
*inf
= cur_inf ();
2116 error_no_arg ("process-id to attach");
2120 if (pid
== getpid ()) /* Trying to masturbate? */
2121 error ("I refuse to debug myself!");
2125 exec_file
= (char *) get_exec_file (0);
2128 printf_unfiltered ("Attaching to program `%s', pid %d\n",
2131 printf_unfiltered ("Attaching to pid %d\n", pid
);
2133 gdb_flush (gdb_stdout
);
2136 inf_debug (inf
, "attaching to pid: %d", pid
);
2138 inf_attach (inf
, pid
);
2139 inf_update_procs (inf
);
2141 inferior_ptid
= pid_to_ptid (inf_pick_first_thread ());
2144 push_target (&gnu_ops
);
2146 /* We have to initialize the terminal settings now, since the code
2147 below might try to restore them. */
2148 target_terminal_init ();
2150 /* If the process was stopped before we attached, make it continue the next
2151 time the user does a continue. */
2152 inf_validate_procinfo (inf
);
2154 inf_update_signal_thread (inf
);
2155 inf_set_traced (inf
, inf
->want_signals
);
2157 #if 0 /* Do we need this? */
2158 renumber_threads (0); /* Give our threads reasonable names. */
2163 /* Take a program previously attached to and detaches it.
2164 The program resumes execution and will no longer stop
2165 on signals, etc. We'd better not have left any breakpoints
2166 in the program or it'll die when it hits one. For this
2167 to work, it may be necessary for the process to have been
2168 previously attached. It *might* work if the program was
2169 started via fork. */
2171 gnu_detach (char *args
, int from_tty
)
2175 char *exec_file
= get_exec_file (0);
2177 printf_unfiltered ("Detaching from program `%s' pid %d\n",
2178 exec_file
, current_inferior
->pid
);
2180 printf_unfiltered ("Detaching from pid %d\n", current_inferior
->pid
);
2181 gdb_flush (gdb_stdout
);
2184 inf_detach (current_inferior
);
2186 inferior_ptid
= null_ptid
;
2188 unpush_target (&gnu_ops
); /* Pop out of handling an inferior */
2190 #endif /* ATTACH_DETACH */
2194 gnu_terminal_init_inferior (void)
2196 gdb_assert (current_inferior
);
2197 terminal_init_inferior_with_pgrp (current_inferior
->pid
);
2200 /* Get ready to modify the registers array. On machines which store
2201 individual registers, this doesn't need to do anything. On machines
2202 which store all the registers in one fell swoop, this makes sure
2203 that registers contains all the registers from the program being
2206 gnu_prepare_to_store (void)
2208 #ifdef CHILD_PREPARE_TO_STORE
2209 CHILD_PREPARE_TO_STORE ();
2214 gnu_open (char *arg
, int from_tty
)
2216 error ("Use the \"run\" command to start a Unix child process.");
2222 error ("to_stop target function not implemented");
2226 gnu_pid_to_exec_file (int pid
)
2228 error ("to_pid_to_exec_file target function not implemented");
2234 gnu_thread_alive (ptid_t tid
)
2236 inf_update_procs (current_inferior
);
2237 return !!inf_tid_to_thread (current_inferior
, PIDGET (tid
));
2241 /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
2242 gdb's address space. Return 0 on failure; number of bytes read
2245 gnu_read_inferior (task_t task
, CORE_ADDR addr
, char *myaddr
, int length
)
2248 vm_address_t low_address
= (vm_address_t
) trunc_page (addr
);
2249 vm_size_t aligned_length
=
2250 (vm_size_t
) round_page (addr
+ length
) - low_address
;
2254 /* Get memory from inferior with page aligned addresses */
2255 err
= vm_read (task
, low_address
, aligned_length
, &copied
, ©_count
);
2259 err
= hurd_safe_copyin (myaddr
, (void *) addr
- low_address
+ copied
, length
);
2262 warning ("Read from inferior faulted: %s", safe_strerror (err
));
2266 err
= vm_deallocate (mach_task_self (), copied
, copy_count
);
2268 warning ("gnu_read_inferior vm_deallocate failed: %s", safe_strerror (err
));
2273 #define CHK_GOTO_OUT(str,ret) \
2274 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2276 struct vm_region_list
2278 struct vm_region_list
*next
;
2279 vm_prot_t protection
;
2284 struct obstack region_obstack
;
2286 /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
2287 task's address space. */
2289 gnu_write_inferior (task_t task
, CORE_ADDR addr
, char *myaddr
, int length
)
2292 vm_address_t low_address
= (vm_address_t
) trunc_page (addr
);
2293 vm_size_t aligned_length
=
2294 (vm_size_t
) round_page (addr
+ length
) - low_address
;
2299 char *errstr
= "Bug in gnu_write_inferior";
2301 struct vm_region_list
*region_element
;
2302 struct vm_region_list
*region_head
= (struct vm_region_list
*) NULL
;
2304 /* Get memory from inferior with page aligned addresses */
2305 err
= vm_read (task
,
2310 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err
);
2314 err
= hurd_safe_copyout ((void *) addr
- low_address
+ copied
,
2316 CHK_GOTO_OUT ("Write to inferior faulted", err
);
2318 obstack_init (®ion_obstack
);
2320 /* Do writes atomically.
2321 First check for holes and unwritable memory. */
2323 vm_size_t remaining_length
= aligned_length
;
2324 vm_address_t region_address
= low_address
;
2326 struct vm_region_list
*scan
;
2328 while (region_address
< low_address
+ aligned_length
)
2330 vm_prot_t protection
;
2331 vm_prot_t max_protection
;
2332 vm_inherit_t inheritance
;
2334 mach_port_t object_name
;
2336 vm_size_t region_length
= remaining_length
;
2337 vm_address_t old_address
= region_address
;
2339 err
= vm_region (task
,
2348 CHK_GOTO_OUT ("vm_region failed", err
);
2350 /* Check for holes in memory */
2351 if (old_address
!= region_address
)
2353 warning ("No memory at 0x%x. Nothing written",
2360 if (!(max_protection
& VM_PROT_WRITE
))
2362 warning ("Memory at address 0x%x is unwritable. Nothing written",
2369 /* Chain the regions for later use */
2371 (struct vm_region_list
*)
2372 obstack_alloc (®ion_obstack
, sizeof (struct vm_region_list
));
2374 region_element
->protection
= protection
;
2375 region_element
->start
= region_address
;
2376 region_element
->length
= region_length
;
2378 /* Chain the regions along with protections */
2379 region_element
->next
= region_head
;
2380 region_head
= region_element
;
2382 region_address
+= region_length
;
2383 remaining_length
= remaining_length
- region_length
;
2386 /* If things fail after this, we give up.
2387 Somebody is messing up inferior_task's mappings. */
2389 /* Enable writes to the chained vm regions */
2390 for (scan
= region_head
; scan
; scan
= scan
->next
)
2392 if (!(scan
->protection
& VM_PROT_WRITE
))
2394 err
= vm_protect (task
,
2398 scan
->protection
| VM_PROT_WRITE
);
2399 CHK_GOTO_OUT ("vm_protect: enable write failed", err
);
2403 err
= vm_write (task
,
2407 CHK_GOTO_OUT ("vm_write failed", err
);
2409 /* Set up the original region protections, if they were changed */
2410 for (scan
= region_head
; scan
; scan
= scan
->next
)
2412 if (!(scan
->protection
& VM_PROT_WRITE
))
2414 err
= vm_protect (task
,
2419 CHK_GOTO_OUT ("vm_protect: enable write failed", err
);
2427 obstack_free (®ion_obstack
, 0);
2429 (void) vm_deallocate (mach_task_self (),
2434 if (err
!= KERN_SUCCESS
)
2436 warning ("%s: %s", errstr
, mach_error_string (err
));
2444 /* Return 0 on failure, number of bytes handled otherwise. TARGET
2447 gnu_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
2448 struct mem_attrib
*attrib
,
2449 struct target_ops
*target
)
2451 task_t task
= (current_inferior
2452 ? (current_inferior
->task
2453 ? current_inferior
->task
->port
: 0)
2456 if (task
== MACH_PORT_NULL
)
2460 inf_debug (current_inferior
, "%s %p[%d] %s %p",
2461 write
? "writing" : "reading", (void *) memaddr
, len
,
2462 write
? "<--" : "-->", myaddr
);
2464 return gnu_write_inferior (task
, memaddr
, myaddr
, len
);
2466 return gnu_read_inferior (task
, memaddr
, myaddr
, len
);
2470 /* Call FUNC on each memory region in the task. */
2472 gnu_find_memory_regions (int (*func
) (CORE_ADDR
,
2480 vm_address_t region_address
, last_region_address
, last_region_end
;
2481 vm_prot_t last_protection
;
2483 if (current_inferior
== 0 || current_inferior
->task
== 0)
2485 task
= current_inferior
->task
->port
;
2486 if (task
== MACH_PORT_NULL
)
2489 region_address
= last_region_address
= last_region_end
= VM_MIN_ADDRESS
;
2490 last_protection
= VM_PROT_NONE
;
2491 while (region_address
< VM_MAX_ADDRESS
)
2493 vm_prot_t protection
;
2494 vm_prot_t max_protection
;
2495 vm_inherit_t inheritance
;
2497 mach_port_t object_name
;
2499 vm_size_t region_length
= VM_MAX_ADDRESS
- region_address
;
2500 vm_address_t old_address
= region_address
;
2502 err
= vm_region (task
,
2511 if (err
== KERN_NO_SPACE
)
2513 if (err
!= KERN_SUCCESS
)
2515 warning ("vm_region failed: %s", mach_error_string (err
));
2519 if (protection
== last_protection
&& region_address
== last_region_end
)
2520 /* This region is contiguous with and indistinguishable from
2521 the previous one, so we just extend that one. */
2522 last_region_end
= region_address
+= region_length
;
2525 /* This region is distinct from the last one we saw, so report
2526 that previous one. */
2527 if (last_protection
!= VM_PROT_NONE
)
2528 (*func
) (last_region_address
,
2529 last_region_end
- last_region_address
,
2530 last_protection
& VM_PROT_READ
,
2531 last_protection
& VM_PROT_WRITE
,
2532 last_protection
& VM_PROT_EXECUTE
,
2534 last_region_address
= region_address
;
2535 last_region_end
= region_address
+= region_length
;
2536 last_protection
= protection
;
2539 /* Report the final region. */
2540 if (last_region_end
> last_region_address
&& last_protection
!= VM_PROT_NONE
)
2541 (*func
) (last_region_address
, last_region_end
- last_region_address
,
2542 last_protection
& VM_PROT_READ
,
2543 last_protection
& VM_PROT_WRITE
,
2544 last_protection
& VM_PROT_EXECUTE
,
2549 /* Return printable description of proc. */
2551 proc_string (struct proc
*proc
)
2553 static char tid_str
[80];
2554 if (proc_is_task (proc
))
2555 sprintf (tid_str
, "process %d", proc
->inf
->pid
);
2557 sprintf (tid_str
, "thread %d.%d",
2558 proc
->inf
->pid
, pid_to_thread_id (MERGEPID (proc
->tid
, 0)));
2563 gnu_pid_to_str (ptid_t ptid
)
2565 struct inf
*inf
= current_inferior
;
2566 int tid
= PIDGET (ptid
);
2567 struct proc
*thread
= inf_tid_to_thread (inf
, tid
);
2570 return proc_string (thread
);
2573 static char tid_str
[80];
2574 sprintf (tid_str
, "bogus thread id %d", tid
);
2580 extern void gnu_store_registers (int regno
);
2581 extern void gnu_fetch_registers (int regno
);
2583 struct target_ops gnu_ops
;
2588 gnu_ops
.to_shortname
= "GNU"; /* to_shortname */
2589 gnu_ops
.to_longname
= "GNU Hurd process"; /* to_longname */
2590 gnu_ops
.to_doc
= "GNU Hurd process"; /* to_doc */
2591 gnu_ops
.to_open
= gnu_open
; /* to_open */
2592 gnu_ops
.to_close
= 0; /* to_close */
2593 gnu_ops
.to_attach
= gnu_attach
; /* to_attach */
2594 gnu_ops
.to_post_attach
= NULL
;
2595 gnu_ops
.to_require_attach
= NULL
; /* to_require_attach */
2596 gnu_ops
.to_detach
= gnu_detach
; /* to_detach */
2597 gnu_ops
.to_require_detach
= NULL
; /* to_require_detach */
2598 gnu_ops
.to_resume
= gnu_resume
; /* to_resume */
2599 gnu_ops
.to_wait
= gnu_wait
; /* to_wait */
2600 gnu_ops
.to_post_wait
= NULL
; /* to_post_wait */
2601 gnu_ops
.to_fetch_registers
= gnu_fetch_registers
; /* to_fetch_registers */
2602 gnu_ops
.to_store_registers
= gnu_store_registers
; /* to_store_registers */
2603 gnu_ops
.to_prepare_to_store
= gnu_prepare_to_store
; /* to_prepare_to_store */
2604 gnu_ops
.to_xfer_memory
= gnu_xfer_memory
; /* to_xfer_memory */
2605 gnu_ops
.to_find_memory_regions
= gnu_find_memory_regions
;
2606 gnu_ops
.to_files_info
= 0; /* to_files_info */
2607 gnu_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
2608 gnu_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
2609 gnu_ops
.to_terminal_init
= gnu_terminal_init_inferior
;
2610 gnu_ops
.to_terminal_inferior
= terminal_inferior
;
2611 gnu_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
2612 gnu_ops
.to_terminal_ours
= terminal_ours
;
2613 gnu_ops
.to_terminal_info
= child_terminal_info
;
2614 gnu_ops
.to_kill
= gnu_kill_inferior
; /* to_kill */
2615 gnu_ops
.to_load
= 0; /* to_load */
2616 gnu_ops
.to_lookup_symbol
= 0; /* to_lookup_symbol */
2617 gnu_ops
.to_create_inferior
= gnu_create_inferior
; /* to_create_inferior */
2618 gnu_ops
.to_post_startup_inferior
= NULL
; /* to_post_startup_inferior */
2619 /* to_acknowledge_created_inferior */
2620 gnu_ops
.to_acknowledge_created_inferior
= NULL
;
2621 /* to_clone_and_follow_inferior */
2622 gnu_ops
.to_clone_and_follow_inferior
= NULL
;
2623 /* to_post_follow_inferior_by_clone */
2624 gnu_ops
.to_post_follow_inferior_by_clone
= NULL
;
2625 gnu_ops
.to_insert_fork_catchpoint
= NULL
;
2626 gnu_ops
.to_remove_fork_catchpoint
= NULL
;
2627 gnu_ops
.to_insert_vfork_catchpoint
= NULL
;
2628 gnu_ops
.to_remove_vfork_catchpoint
= NULL
;
2629 gnu_ops
.to_has_forked
= NULL
; /* to_has_forked */
2630 gnu_ops
.to_has_vforked
= NULL
; /* to_has_vforked */
2631 gnu_ops
.to_can_follow_vfork_prior_to_exec
= NULL
;
2632 gnu_ops
.to_post_follow_vfork
= NULL
; /* to_post_follow_vfork */
2633 gnu_ops
.to_insert_exec_catchpoint
= NULL
;
2634 gnu_ops
.to_remove_exec_catchpoint
= NULL
;
2635 gnu_ops
.to_has_execd
= NULL
;
2636 gnu_ops
.to_reported_exec_events_per_exec_call
= NULL
;
2637 gnu_ops
.to_has_exited
= NULL
;
2638 gnu_ops
.to_mourn_inferior
= gnu_mourn_inferior
; /* to_mourn_inferior */
2639 gnu_ops
.to_can_run
= gnu_can_run
; /* to_can_run */
2640 gnu_ops
.to_notice_signals
= 0; /* to_notice_signals */
2641 gnu_ops
.to_thread_alive
= gnu_thread_alive
; /* to_thread_alive */
2642 gnu_ops
.to_pid_to_str
= gnu_pid_to_str
; /* to_pid_to_str */
2643 gnu_ops
.to_stop
= gnu_stop
; /* to_stop */
2644 gnu_ops
.to_pid_to_exec_file
= gnu_pid_to_exec_file
; /* to_pid_to_exec_file */
2645 gnu_ops
.to_stratum
= process_stratum
; /* to_stratum */
2646 gnu_ops
.DONT_USE
= 0; /* to_next */
2647 gnu_ops
.to_has_all_memory
= 1; /* to_has_all_memory */
2648 gnu_ops
.to_has_memory
= 1; /* to_has_memory */
2649 gnu_ops
.to_has_stack
= 1; /* to_has_stack */
2650 gnu_ops
.to_has_registers
= 1; /* to_has_registers */
2651 gnu_ops
.to_has_execution
= 1; /* to_has_execution */
2652 gnu_ops
.to_sections
= 0; /* sections */
2653 gnu_ops
.to_sections_end
= 0; /* sections_end */
2654 gnu_ops
.to_magic
= OPS_MAGIC
; /* to_magic */
2655 } /* init_gnu_ops */
2658 /* User task commands. */
2660 struct cmd_list_element
*set_task_cmd_list
= 0;
2661 struct cmd_list_element
*show_task_cmd_list
= 0;
2662 /* User thread commands. */
2664 /* Commands with a prefix of `set/show thread'. */
2665 extern struct cmd_list_element
*thread_cmd_list
;
2666 struct cmd_list_element
*set_thread_cmd_list
= NULL
;
2667 struct cmd_list_element
*show_thread_cmd_list
= NULL
;
2669 /* Commands with a prefix of `set/show thread default'. */
2670 struct cmd_list_element
*set_thread_default_cmd_list
= NULL
;
2671 struct cmd_list_element
*show_thread_default_cmd_list
= NULL
;
2674 set_thread_cmd (char *args
, int from_tty
)
2676 printf_unfiltered ("\"set thread\" must be followed by the name of a thread property, or \"default\".\n");
2680 show_thread_cmd (char *args
, int from_tty
)
2682 printf_unfiltered ("\"show thread\" must be followed by the name of a thread property, or \"default\".\n");
2686 set_thread_default_cmd (char *args
, int from_tty
)
2688 printf_unfiltered ("\"set thread default\" must be followed by the name of a thread property.\n");
2692 show_thread_default_cmd (char *args
, int from_tty
)
2694 printf_unfiltered ("\"show thread default\" must be followed by the name of a thread property.\n");
2698 parse_int_arg (char *args
, char *cmd_prefix
)
2703 int val
= strtoul (args
, &arg_end
, 10);
2704 if (*args
&& *arg_end
== '\0')
2707 error ("Illegal argument for \"%s\" command, should be an integer.", cmd_prefix
);
2711 _parse_bool_arg (char *args
, char *t_val
, char *f_val
, char *cmd_prefix
)
2713 if (!args
|| strcmp (args
, t_val
) == 0)
2715 else if (strcmp (args
, f_val
) == 0)
2718 error ("Illegal argument for \"%s\" command, should be \"%s\" or \"%s\".",
2719 cmd_prefix
, t_val
, f_val
);
2722 #define parse_bool_arg(args, cmd_prefix) \
2723 _parse_bool_arg (args, "on", "off", cmd_prefix)
2726 check_empty (char *args
, char *cmd_prefix
)
2729 error ("Garbage after \"%s\" command: `%s'", cmd_prefix
, args
);
2732 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2733 static struct proc
*
2736 struct inf
*inf
= cur_inf ();
2737 struct proc
*thread
= inf_tid_to_thread (inf
, PIDGET (inferior_ptid
));
2739 error ("No current thread.");
2743 /* Returns the current inferior, but signals an error if it has no task. */
2747 struct inf
*inf
= cur_inf ();
2749 error ("No current process.");
2755 set_task_pause_cmd (char *args
, int from_tty
)
2757 struct inf
*inf
= cur_inf ();
2758 int old_sc
= inf
->pause_sc
;
2760 inf
->pause_sc
= parse_bool_arg (args
, "set task pause");
2762 if (old_sc
== 0 && inf
->pause_sc
!= 0)
2763 /* If the task is currently unsuspended, immediately suspend it,
2764 otherwise wait until the next time it gets control. */
2769 show_task_pause_cmd (char *args
, int from_tty
)
2771 struct inf
*inf
= cur_inf ();
2772 check_empty (args
, "show task pause");
2773 printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
2775 ? (inf
->pause_sc
== 0 ? "isn't" : "is")
2776 : (inf
->pause_sc
== 0 ? "won't be" : "will be"));
2780 set_task_detach_sc_cmd (char *args
, int from_tty
)
2782 cur_inf ()->detach_sc
= parse_int_arg (args
, "set task detach-suspend-count");
2786 show_task_detach_sc_cmd (char *args
, int from_tty
)
2788 check_empty (args
, "show task detach-suspend-count");
2789 printf_unfiltered ("The inferior task will be left with a suspend count of %d when detaching.\n",
2790 cur_inf ()->detach_sc
);
2795 set_thread_default_pause_cmd (char *args
, int from_tty
)
2797 struct inf
*inf
= cur_inf ();
2798 inf
->default_thread_pause_sc
=
2799 parse_bool_arg (args
, "set thread default pause") ? 0 : 1;
2803 show_thread_default_pause_cmd (char *args
, int from_tty
)
2805 struct inf
*inf
= cur_inf ();
2806 int sc
= inf
->default_thread_pause_sc
;
2807 check_empty (args
, "show thread default pause");
2808 printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
2809 sc
? "are" : "aren't",
2810 !sc
&& inf
->pause_sc
? " (but the task is)" : "");
2814 set_thread_default_run_cmd (char *args
, int from_tty
)
2816 struct inf
*inf
= cur_inf ();
2817 inf
->default_thread_run_sc
=
2818 parse_bool_arg (args
, "set thread default run") ? 0 : 1;
2822 show_thread_default_run_cmd (char *args
, int from_tty
)
2824 struct inf
*inf
= cur_inf ();
2825 check_empty (args
, "show thread default run");
2826 printf_unfiltered ("New threads %s allowed to run.\n",
2827 inf
->default_thread_run_sc
== 0 ? "are" : "aren't");
2831 set_thread_default_detach_sc_cmd (char *args
, int from_tty
)
2833 cur_inf ()->default_thread_detach_sc
=
2834 parse_int_arg (args
, "set thread default detach-suspend-count");
2838 show_thread_default_detach_sc_cmd (char *args
, int from_tty
)
2840 check_empty (args
, "show thread default detach-suspend-count");
2841 printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
2842 cur_inf ()->default_thread_detach_sc
);
2846 /* Steal a send right called NAME in the inferior task, and make it PROC's
2847 saved exception port. */
2849 steal_exc_port (struct proc
*proc
, mach_port_t name
)
2853 mach_msg_type_name_t port_type
;
2855 if (!proc
|| !proc
->inf
->task
)
2856 error ("No inferior task.");
2858 err
= mach_port_extract_right (proc
->inf
->task
->port
,
2859 name
, MACH_MSG_TYPE_COPY_SEND
,
2862 error ("Couldn't extract send right %d from inferior: %s",
2863 name
, safe_strerror (err
));
2865 if (proc
->saved_exc_port
)
2866 /* Get rid of our reference to the old one. */
2867 mach_port_deallocate (mach_task_self (), proc
->saved_exc_port
);
2869 proc
->saved_exc_port
= port
;
2871 if (!proc
->exc_port
)
2872 /* If PROC is a thread, we may not have set its exception port before.
2873 We can't use proc_steal_exc_port because it also sets saved_exc_port. */
2875 proc
->exc_port
= proc
->inf
->event_port
;
2876 err
= proc_set_exception_port (proc
, proc
->exc_port
);
2877 error ("Can't set exception port for %s: %s",
2878 proc_string (proc
), safe_strerror (err
));
2883 set_task_exc_port_cmd (char *args
, int from_tty
)
2885 struct inf
*inf
= cur_inf ();
2887 error ("No argument to \"set task exception-port\" command.");
2888 steal_exc_port (inf
->task
, parse_and_eval_address (args
));
2892 set_stopped_cmd (char *args
, int from_tty
)
2894 cur_inf ()->stopped
= _parse_bool_arg (args
, "yes", "no", "set stopped");
2898 show_stopped_cmd (char *args
, int from_tty
)
2900 struct inf
*inf
= active_inf ();
2901 check_empty (args
, "show stopped");
2902 printf_unfiltered ("The inferior process %s stopped.\n",
2903 inf
->stopped
? "is" : "isn't");
2907 set_sig_thread_cmd (char *args
, int from_tty
)
2909 struct inf
*inf
= cur_inf ();
2911 if (!args
|| (!isdigit (*args
) && strcmp (args
, "none") != 0))
2912 error ("Illegal argument to \"set signal-thread\" command.\n"
2913 "Should be an integer thread ID, or `none'.");
2915 if (strcmp (args
, "none") == 0)
2916 inf
->signal_thread
= 0;
2919 int tid
= PIDGET (thread_id_to_pid (atoi (args
)));
2921 error ("Thread ID %s not known. Use the \"info threads\" command to\n"
2922 "see the IDs of currently known threads.", args
);
2923 inf
->signal_thread
= inf_tid_to_thread (inf
, tid
);
2928 show_sig_thread_cmd (char *args
, int from_tty
)
2930 struct inf
*inf
= active_inf ();
2931 check_empty (args
, "show signal-thread");
2932 if (inf
->signal_thread
)
2933 printf_unfiltered ("The signal thread is %s.\n",
2934 proc_string (inf
->signal_thread
));
2936 printf_unfiltered ("There is no signal thread.\n");
2941 set_signals_cmd (char *args
, int from_tty
)
2943 struct inf
*inf
= cur_inf ();
2945 inf
->want_signals
= parse_bool_arg (args
, "set signals");
2947 if (inf
->task
&& inf
->want_signals
!= inf
->traced
)
2948 /* Make this take effect immediately in a running process. */
2949 inf_set_traced (inf
, inf
->want_signals
);
2953 show_signals_cmd (char *args
, int from_tty
)
2955 struct inf
*inf
= cur_inf ();
2956 check_empty (args
, "show signals");
2957 printf_unfiltered ("The inferior process's signals %s intercepted.\n",
2959 ? (inf
->traced
? "are" : "aren't")
2960 : (inf
->want_signals
? "will be" : "won't be"));
2964 set_exceptions_cmd (char *args
, int from_tty
)
2966 struct inf
*inf
= cur_inf ();
2967 int val
= parse_bool_arg (args
, "set exceptions");
2969 if (inf
->task
&& inf
->want_exceptions
!= val
)
2970 /* Make this take effect immediately in a running process. */
2973 inf
->want_exceptions
= val
;
2977 show_exceptions_cmd (char *args
, int from_tty
)
2979 struct inf
*inf
= cur_inf ();
2980 check_empty (args
, "show exceptions");
2981 printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
2983 ? (inf
->want_exceptions
? "are" : "aren't")
2984 : (inf
->want_exceptions
? "will be" : "won't be"));
2989 set_task_cmd (char *args
, int from_tty
)
2991 printf_unfiltered ("\"set task\" must be followed by the name"
2992 " of a task property.\n");
2996 show_task_cmd (char *args
, int from_tty
)
2998 struct inf
*inf
= cur_inf ();
3000 check_empty (args
, "show task");
3002 show_signals_cmd (0, from_tty
);
3003 show_exceptions_cmd (0, from_tty
);
3004 show_task_pause_cmd (0, from_tty
);
3006 if (inf
->pause_sc
== 0)
3007 show_thread_default_pause_cmd (0, from_tty
);
3008 show_thread_default_run_cmd (0, from_tty
);
3012 show_stopped_cmd (0, from_tty
);
3013 show_sig_thread_cmd (0, from_tty
);
3016 if (inf
->detach_sc
!= 0)
3017 show_task_detach_sc_cmd (0, from_tty
);
3018 if (inf
->default_thread_detach_sc
!= 0)
3019 show_thread_default_detach_sc_cmd (0, from_tty
);
3024 set_noninvasive_cmd (char *args
, int from_tty
)
3026 /* Invert the sense of the arg for each component. */
3027 char *inv_args
= parse_bool_arg (args
, "set noninvasive") ? "off" : "on";
3029 set_task_pause_cmd (inv_args
, from_tty
);
3030 set_signals_cmd (inv_args
, from_tty
);
3031 set_exceptions_cmd (inv_args
, from_tty
);
3036 info_port_rights (char *args
, mach_port_type_t only
)
3038 struct inf
*inf
= active_inf ();
3039 struct value
*vmark
= value_mark ();
3042 /* Explicit list of port rights. */
3046 struct value
*val
= parse_to_comma_and_eval (&args
);
3047 long right
= value_as_long (val
);
3049 print_port_info (right
, 0, inf
->task
->port
, PORTINFO_DETAILS
,
3052 error ("%ld: %s.", right
, safe_strerror (err
));
3056 /* Print all of them. */
3059 print_task_ports_info (inf
->task
->port
, only
, PORTINFO_DETAILS
,
3062 error ("%s.", safe_strerror (err
));
3065 value_free_to_mark (vmark
);
3069 info_send_rights_cmd (char *args
, int from_tty
)
3071 info_port_rights (args
, MACH_PORT_TYPE_SEND
);
3075 info_recv_rights_cmd (char *args
, int from_tty
)
3077 info_port_rights (args
, MACH_PORT_TYPE_RECEIVE
);
3081 info_port_sets_cmd (char *args
, int from_tty
)
3083 info_port_rights (args
, MACH_PORT_TYPE_PORT_SET
);
3087 info_dead_names_cmd (char *args
, int from_tty
)
3089 info_port_rights (args
, MACH_PORT_TYPE_DEAD_NAME
);
3093 info_port_rights_cmd (char *args
, int from_tty
)
3095 info_port_rights (args
, ~0);
3100 add_task_commands (void)
3102 add_cmd ("pause", class_run
, set_thread_default_pause_cmd
,
3103 "Set whether the new threads are suspended while gdb has control.\n\
3104 This property normally has no effect because the whole task is\n\
3105 suspended, however, that may be disabled with \"set task pause off\".\n\
3106 The default value is \"off\".",
3107 &set_thread_default_cmd_list
);
3108 add_cmd ("pause", no_class
, show_thread_default_pause_cmd
,
3109 "Show whether new threads are suspended while gdb has control.",
3110 &show_thread_default_cmd_list
);
3112 add_cmd ("run", class_run
, set_thread_default_run_cmd
,
3113 "Set whether new threads are allowed to run \
3114 (once gdb has noticed them).",
3115 &set_thread_default_cmd_list
);
3116 add_cmd ("run", no_class
, show_thread_default_run_cmd
,
3117 "Show whether new threads are allowed to run \
3118 (once gdb has noticed them).",
3119 &show_thread_default_cmd_list
);
3121 add_cmd ("detach-suspend-count", class_run
, set_thread_default_detach_sc_cmd
,
3122 "Set the default detach-suspend-count value for new threads.",
3123 &set_thread_default_cmd_list
);
3124 add_cmd ("detach-suspend-count", no_class
, show_thread_default_detach_sc_cmd
,
3125 "Show the default detach-suspend-count value for new threads.",
3126 &show_thread_default_cmd_list
);
3128 add_cmd ("signals", class_run
, set_signals_cmd
,
3129 "Set whether the inferior process's signals will be intercepted.\n\
3130 Mach exceptions (such as breakpoint traps) are not affected.",
3132 add_alias_cmd ("sigs", "signals", class_run
, 1, &setlist
);
3133 add_cmd ("signals", no_class
, show_signals_cmd
,
3134 "Show whether the inferior process's signals will be intercepted.",
3136 add_alias_cmd ("sigs", "signals", no_class
, 1, &showlist
);
3138 add_cmd ("signal-thread", class_run
, set_sig_thread_cmd
,
3139 "Set the thread that gdb thinks is the libc signal thread.\n\
3140 This thread is run when delivering a signal to a non-stopped process.",
3142 add_alias_cmd ("sigthread", "signal-thread", class_run
, 1, &setlist
);
3143 add_cmd ("signal-thread", no_class
, show_sig_thread_cmd
,
3144 "Set the thread that gdb thinks is the libc signal thread.",
3146 add_alias_cmd ("sigthread", "signal-thread", no_class
, 1, &showlist
);
3148 add_cmd ("stopped", class_run
, set_stopped_cmd
,
3149 "Set whether gdb thinks the inferior process is stopped \
3151 Stopped process will be continued by sending them a signal.",
3153 add_cmd ("stopped", no_class
, show_signals_cmd
,
3154 "Show whether gdb thinks the inferior process is stopped \
3158 add_cmd ("exceptions", class_run
, set_exceptions_cmd
,
3159 "Set whether exceptions in the inferior process will be trapped.\n\
3160 When exceptions are turned off, neither breakpoints nor single-stepping\n\
3163 /* Allow `set exc' despite conflict with `set exception-port'. */
3164 add_alias_cmd ("exc", "exceptions", class_run
, 1, &setlist
);
3165 add_cmd ("exceptions", no_class
, show_exceptions_cmd
,
3166 "Show whether exceptions in the inferior process will be trapped.",
3169 add_prefix_cmd ("task", no_class
, set_task_cmd
,
3170 "Command prefix for setting task attributes.",
3171 &set_task_cmd_list
, "set task ", 0, &setlist
);
3172 add_prefix_cmd ("task", no_class
, show_task_cmd
,
3173 "Command prefix for showing task attributes.",
3174 &show_task_cmd_list
, "show task ", 0, &showlist
);
3176 add_cmd ("pause", class_run
, set_task_pause_cmd
,
3177 "Set whether the task is suspended while gdb has control.\n\
3178 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3179 until the next time the program is continued.\n\
3180 When setting this to \"off\", \"set thread default pause on\" can be\n\
3181 used to pause individual threads by default instead.",
3182 &set_task_cmd_list
);
3183 add_cmd ("pause", no_class
, show_task_pause_cmd
,
3184 "Show whether the task is suspended while gdb has control.",
3185 &show_task_cmd_list
);
3187 add_cmd ("detach-suspend-count", class_run
, set_task_detach_sc_cmd
,
3188 "Set the suspend count will leave on the thread when detaching.",
3189 &set_task_cmd_list
);
3190 add_cmd ("detach-suspend-count", no_class
, show_task_detach_sc_cmd
,
3191 "Show the suspend count will leave on the thread when detaching.",
3192 &show_task_cmd_list
);
3194 add_cmd ("exception-port", no_class
, set_task_exc_port_cmd
,
3195 "Set the task exception port to which we forward exceptions.\n\
3196 The argument should be the value of the send right in the task.",
3197 &set_task_cmd_list
);
3198 add_alias_cmd ("excp", "exception-port", no_class
, 1, &set_task_cmd_list
);
3199 add_alias_cmd ("exc-port", "exception-port", no_class
, 1,
3200 &set_task_cmd_list
);
3202 /* A convenient way of turning on all options require to noninvasively
3203 debug running tasks. */
3204 add_cmd ("noninvasive", no_class
, set_noninvasive_cmd
,
3205 "Set task options so that we interfere as little as possible.\n\
3206 This is the same as setting `task pause', `exceptions', and\n\
3207 `signals' to the opposite value.",
3210 /* Commands to show information about the task's ports. */
3211 add_cmd ("send-rights", class_info
, info_send_rights_cmd
,
3212 "Show information about the task's send rights",
3214 add_cmd ("receive-rights", class_info
, info_recv_rights_cmd
,
3215 "Show information about the task's receive rights",
3217 add_cmd ("port-rights", class_info
, info_port_rights_cmd
,
3218 "Show information about the task's port rights",
3220 add_cmd ("port-sets", class_info
, info_port_sets_cmd
,
3221 "Show information about the task's port sets",
3223 add_cmd ("dead-names", class_info
, info_dead_names_cmd
,
3224 "Show information about the task's dead names",
3226 add_info_alias ("ports", "port-rights", 1);
3227 add_info_alias ("port", "port-rights", 1);
3228 add_info_alias ("psets", "port-sets", 1);
3233 set_thread_pause_cmd (char *args
, int from_tty
)
3235 struct proc
*thread
= cur_thread ();
3236 int old_sc
= thread
->pause_sc
;
3237 thread
->pause_sc
= parse_bool_arg (args
, "set thread pause");
3238 if (old_sc
== 0 && thread
->pause_sc
!= 0 && thread
->inf
->pause_sc
== 0)
3239 /* If the task is currently unsuspended, immediately suspend it,
3240 otherwise wait until the next time it gets control. */
3241 inf_suspend (thread
->inf
);
3245 show_thread_pause_cmd (char *args
, int from_tty
)
3247 struct proc
*thread
= cur_thread ();
3248 int sc
= thread
->pause_sc
;
3249 check_empty (args
, "show task pause");
3250 printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
3251 proc_string (thread
),
3252 sc
? "is" : "isn't",
3253 !sc
&& thread
->inf
->pause_sc
? " (but the task is)" : "");
3257 set_thread_run_cmd (char *args
, int from_tty
)
3259 struct proc
*thread
= cur_thread ();
3260 thread
->run_sc
= parse_bool_arg (args
, "set thread run") ? 0 : 1;
3264 show_thread_run_cmd (char *args
, int from_tty
)
3266 struct proc
*thread
= cur_thread ();
3267 check_empty (args
, "show thread run");
3268 printf_unfiltered ("Thread %s %s allowed to run.",
3269 proc_string (thread
),
3270 thread
->run_sc
== 0 ? "is" : "isn't");
3274 set_thread_detach_sc_cmd (char *args
, int from_tty
)
3276 cur_thread ()->detach_sc
= parse_int_arg (args
,
3277 "set thread detach-suspend-count");
3281 show_thread_detach_sc_cmd (char *args
, int from_tty
)
3283 struct proc
*thread
= cur_thread ();
3284 check_empty (args
, "show thread detach-suspend-count");
3285 printf_unfiltered ("Thread %s will be left with a suspend count"
3286 " of %d when detaching.\n",
3287 proc_string (thread
),
3292 set_thread_exc_port_cmd (char *args
, int from_tty
)
3294 struct proc
*thread
= cur_thread ();
3296 error ("No argument to \"set thread exception-port\" command.");
3297 steal_exc_port (thread
, parse_and_eval_address (args
));
3302 show_thread_cmd (char *args
, int from_tty
)
3304 struct proc
*thread
= cur_thread ();
3305 check_empty (args
, "show thread");
3306 show_thread_run_cmd (0, from_tty
);
3307 show_thread_pause_cmd (0, from_tty
);
3308 if (thread
->detach_sc
!= 0)
3309 show_thread_detach_sc_cmd (0, from_tty
);
3314 thread_takeover_sc_cmd (char *args
, int from_tty
)
3316 struct proc
*thread
= cur_thread ();
3317 thread_basic_info_data_t _info
;
3318 thread_basic_info_t info
= &_info
;
3319 mach_msg_type_number_t info_len
= THREAD_BASIC_INFO_COUNT
;
3321 thread_info (thread
->port
, THREAD_BASIC_INFO
, (int *) &info
, &info_len
);
3323 error ("%s.", safe_strerror (err
));
3324 thread
->sc
= info
->suspend_count
;
3326 printf_unfiltered ("Suspend count was %d.\n", thread
->sc
);
3328 vm_deallocate (mach_task_self (), (vm_address_t
) info
,
3329 info_len
* sizeof (int));
3334 add_thread_commands (void)
3336 add_prefix_cmd ("thread", no_class
, set_thread_cmd
,
3337 "Command prefix for setting thread properties.",
3338 &set_thread_cmd_list
, "set thread ", 0, &setlist
);
3339 add_prefix_cmd ("default", no_class
, show_thread_cmd
,
3340 "Command prefix for setting default thread properties.",
3341 &set_thread_default_cmd_list
, "set thread default ", 0,
3342 &set_thread_cmd_list
);
3343 add_prefix_cmd ("thread", no_class
, set_thread_default_cmd
,
3344 "Command prefix for showing thread properties.",
3345 &show_thread_cmd_list
, "show thread ", 0, &showlist
);
3346 add_prefix_cmd ("default", no_class
, show_thread_default_cmd
,
3347 "Command prefix for showing default thread properties.",
3348 &show_thread_default_cmd_list
, "show thread default ", 0,
3349 &show_thread_cmd_list
);
3351 add_cmd ("pause", class_run
, set_thread_pause_cmd
,
3352 "Set whether the current thread is suspended \
3353 while gdb has control.\n\
3354 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3355 until the next time the program is continued. This property normally\n\
3356 has no effect because the whole task is suspended, however, that may\n\
3357 be disabled with \"set task pause off\".\n\
3358 The default value is \"off\".",
3359 &set_thread_cmd_list
);
3360 add_cmd ("pause", no_class
, show_thread_pause_cmd
,
3361 "Show whether the current thread is suspended \
3362 while gdb has control.",
3363 &show_thread_cmd_list
);
3365 add_cmd ("run", class_run
, set_thread_run_cmd
,
3366 "Set whether the current thread is allowed to run.",
3367 &set_thread_cmd_list
);
3368 add_cmd ("run", no_class
, show_thread_run_cmd
,
3369 "Show whether the current thread is allowed to run.",
3370 &show_thread_cmd_list
);
3372 add_cmd ("detach-suspend-count", class_run
, set_thread_detach_sc_cmd
,
3373 "Set the suspend count will leave on the thread when detaching.\n\
3374 Note that this is relative to suspend count when gdb noticed the thread;\n\
3375 use the `thread takeover-suspend-count' to force it to an absolute value.",
3376 &set_thread_cmd_list
);
3377 add_cmd ("detach-suspend-count", no_class
, show_thread_detach_sc_cmd
,
3378 "Show the suspend count will leave on the thread when detaching.\n\
3379 Note that this is relative to suspend count when gdb noticed the thread;\n\
3380 use the `thread takeover-suspend-count' to force it to an absolute value.",
3381 &show_thread_cmd_list
);
3383 add_cmd ("exception-port", no_class
, set_thread_exc_port_cmd
,
3384 "Set the thread exception port to which we forward exceptions.\n\
3385 This overrides the task exception port.\n\
3386 The argument should be the value of the send right in the task.",
3387 &set_thread_cmd_list
);
3388 add_alias_cmd ("excp", "exception-port", no_class
, 1, &set_thread_cmd_list
);
3389 add_alias_cmd ("exc-port", "exception-port", no_class
, 1,
3390 &set_thread_cmd_list
);
3392 add_cmd ("takeover-suspend-count", no_class
, thread_takeover_sc_cmd
,
3393 "Force the threads absolute suspend-count to be gdb's.\n\
3394 Prior to giving this command, gdb's thread suspend-counts are relative\n\
3395 to the thread's initial suspend-count when gdb notices the threads.",
3401 _initialize_gnu_nat (void)
3403 proc_server
= getproc ();
3406 add_target (&gnu_ops
);
3408 add_task_commands ();
3409 add_thread_commands ();
3410 add_set_cmd ("gnu-debug", class_maintenance
,
3411 var_boolean
, (char *) &gnu_debug_flag
,
3412 "Set debugging output for the gnu backend.", &maintenancelist
);
3415 #ifdef FLUSH_INFERIOR_CACHE
3417 /* When over-writing code on some machines the I-Cache must be flushed
3418 explicitly, because it is not kept coherent by the lazy hardware.
3419 This definitely includes breakpoints, for instance, or else we
3420 end up looping in mysterious Bpt traps */
3423 flush_inferior_icache (CORE_ADDR pc
, int amount
)
3425 vm_machine_attribute_val_t flush
= MATTR_VAL_ICACHE_FLUSH
;
3428 ret
= vm_machine_attribute (current_inferior
->task
->port
,
3433 if (ret
!= KERN_SUCCESS
)
3434 warning ("Error flushing inferior's cache : %s", safe_strerror (ret
));
3436 #endif /* FLUSH_INFERIOR_CACHE */