GDB (API)
/home/stan/gdb/src/gdb/gnu-nat.c
Go to the documentation of this file.
00001 /* Interface GDB to the GNU Hurd.
00002    Copyright (C) 1992-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    Written by Miles Bader <miles@gnu.ai.mit.edu>
00007 
00008    Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 
00025 #include <ctype.h>
00026 #include <errno.h>
00027 #include <limits.h>
00028 #include <setjmp.h>
00029 #include <signal.h>
00030 #include <stdio.h>
00031 #include "gdb_string.h"
00032 #include <sys/ptrace.h>
00033 
00034 #include <mach.h>
00035 #include <mach_error.h>
00036 #include <mach/exception.h>
00037 #include <mach/message.h>
00038 #include <mach/notify.h>
00039 #include <mach/vm_attributes.h>
00040 
00041 #include <hurd.h>
00042 #include <hurd/interrupt.h>
00043 #include <hurd/msg.h>
00044 #include <hurd/msg_request.h>
00045 #include <hurd/process.h>
00046 /* Defined in <hurd/process.h>, but we need forward declarations from
00047    <hurd/process_request.h> as well.  */
00048 #undef _process_user_
00049 #include <hurd/process_request.h>
00050 #include <hurd/signal.h>
00051 #include <hurd/sigpreempt.h>
00052 
00053 #include <portinfo.h>
00054 
00055 #include "inferior.h"
00056 #include "symtab.h"
00057 #include "value.h"
00058 #include "language.h"
00059 #include "target.h"
00060 #include "gdb_wait.h"
00061 #include "gdbcmd.h"
00062 #include "gdbcore.h"
00063 #include "gdbthread.h"
00064 #include "gdb_assert.h"
00065 #include "gdb_obstack.h"
00066 
00067 #include "gnu-nat.h"
00068 #include "inf-child.h"
00069 
00070 #include "exc_request_S.h"
00071 #include "notify_S.h"
00072 #include "process_reply_S.h"
00073 #include "msg_reply_S.h"
00074 #include "exc_request_U.h"
00075 #include "msg_U.h"
00076 
00077 static process_t proc_server = MACH_PORT_NULL;
00078 
00079 /* If we've sent a proc_wait_request to the proc server, the pid of the
00080    process we asked about.  We can only ever have one outstanding.  */
00081 int proc_wait_pid = 0;
00082 
00083 /* The number of wait requests we've sent, and expect replies from.  */
00084 int proc_waits_pending = 0;
00085 
00086 int gnu_debug_flag = 0;
00087 
00088 /* Forward decls */
00089 
00090 struct inf *make_inf ();
00091 void inf_clear_wait (struct inf *inf);
00092 void inf_cleanup (struct inf *inf);
00093 void inf_startup (struct inf *inf, int pid);
00094 int inf_update_suspends (struct inf *inf);
00095 void inf_set_pid (struct inf *inf, pid_t pid);
00096 void inf_validate_procs (struct inf *inf);
00097 void inf_steal_exc_ports (struct inf *inf);
00098 void inf_restore_exc_ports (struct inf *inf);
00099 struct proc *inf_tid_to_proc (struct inf *inf, int tid);
00100 void inf_set_threads_resume_sc (struct inf *inf,
00101                                 struct proc *run_thread,
00102                                 int run_others);
00103 int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
00104 void inf_suspend (struct inf *inf);
00105 void inf_resume (struct inf *inf);
00106 void inf_set_step_thread (struct inf *inf, struct proc *proc);
00107 void inf_detach (struct inf *inf);
00108 void inf_attach (struct inf *inf, int pid);
00109 void inf_signal (struct inf *inf, enum gdb_signal sig);
00110 void inf_continue (struct inf *inf);
00111 
00112 #define inf_debug(_inf, msg, args...) \
00113   do { struct inf *__inf = (_inf); \
00114        debug ("{inf %d %s}: " msg, __inf->pid, \
00115        host_address_to_string (__inf) , ##args); } while (0)
00116 
00117 void proc_abort (struct proc *proc, int force);
00118 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
00119 struct proc *_proc_free (struct proc *proc);
00120 int proc_update_sc (struct proc *proc);
00121 error_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
00122 error_t proc_set_exception_port (struct proc *proc, mach_port_t port);
00123 static mach_port_t _proc_get_exc_port (struct proc *proc);
00124 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
00125 void proc_restore_exc_port (struct proc *proc);
00126 int proc_trace (struct proc *proc, int set);
00127 
00128 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
00129    to INF's msg port and task port respectively.  If it has no msg port,
00130    EIEIO is returned.  INF must refer to a running process!  */
00131 #define INF_MSGPORT_RPC(inf, rpc_expr) \
00132   HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
00133                     (refport = inf->task->port, 0), 0, \
00134                     msgport ? (rpc_expr) : EIEIO)
00135 
00136 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
00137    there's someone around to deal with the RPC (and resuspend things
00138    afterwards).  This effects INF's threads' resume_sc count.  */
00139 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
00140   (inf_set_threads_resume_sc_for_signal_thread (inf) \
00141    ? ({ error_t __e; \
00142         inf_resume (inf); \
00143         __e = INF_MSGPORT_RPC (inf, rpc_expr); \
00144         inf_suspend (inf); \
00145         __e; }) \
00146    : EIEIO)
00147 
00148 
00149 /* The state passed by an exception message.  */
00150 struct exc_state
00151   {
00152     int exception;              /* The exception code.  */
00153     int code, subcode;
00154     mach_port_t handler;        /* The real exception port to handle this.  */
00155     mach_port_t reply;          /* The reply port from the exception call.  */
00156   };
00157 
00158 /* The results of the last wait an inf did.  */
00159 struct inf_wait
00160   {
00161     struct target_waitstatus status;    /* The status returned to gdb.  */
00162     struct exc_state exc;       /* The exception that caused us to return.  */
00163     struct proc *thread;        /* The thread in question.  */
00164     int suppress;               /* Something trivial happened.  */
00165   };
00166 
00167 /* The state of an inferior.  */
00168 struct inf
00169   {
00170     /* Fields describing the current inferior.  */
00171 
00172     struct proc *task;          /* The mach task.   */
00173     struct proc *threads;       /* A linked list of all threads in TASK.  */
00174 
00175     /* True if THREADS needn't be validated by querying the task.  We
00176        assume that we and the task in question are the only ones
00177        frobbing the thread list, so as long as we don't let any code
00178        run, we don't have to worry about THREADS changing.  */
00179     int threads_up_to_date;
00180 
00181     pid_t pid;                  /* The real system PID.  */
00182 
00183     struct inf_wait wait;       /* What to return from target_wait.  */
00184 
00185     /* One thread proc in INF may be in `single-stepping mode'.  This
00186        is it.  */
00187     struct proc *step_thread;
00188 
00189     /* The thread we think is the signal thread.  */
00190     struct proc *signal_thread;
00191 
00192     mach_port_t event_port;     /* Where we receive various msgs.  */
00193 
00194     /* True if we think at least one thread in the inferior could currently be
00195        running.  */
00196     unsigned int running:1;
00197 
00198     /* True if the process has stopped (in the proc server sense).  Note that
00199        since a proc server `stop' leaves the signal thread running, the inf can
00200        be RUNNING && STOPPED...  */
00201     unsigned int stopped:1;
00202 
00203     /* True if the inferior has no message port.  */
00204     unsigned int nomsg:1;
00205 
00206     /* True if the inferior is traced.  */
00207     unsigned int traced:1;
00208 
00209     /* True if we shouldn't try waiting for the inferior, usually because we
00210        can't for some reason.  */
00211     unsigned int no_wait:1;
00212 
00213     /* When starting a new inferior, we don't try to validate threads until all
00214        the proper execs have been done.  This is a count of how many execs we
00215        expect to happen.  */
00216     unsigned pending_execs;
00217 
00218     /* Fields describing global state.  */
00219 
00220     /* The task suspend count used when gdb has control.  This is normally 1 to
00221        make things easier for us, but sometimes (like when attaching to vital
00222        system servers) it may be desirable to let the task continue to run
00223        (pausing individual threads as necessary).  */
00224     int pause_sc;
00225 
00226     /* The task suspend count left when detaching from a task.  */
00227     int detach_sc;
00228 
00229     /* The initial values used for the run_sc and pause_sc of newly discovered
00230        threads -- see the definition of those fields in struct proc.  */
00231     int default_thread_run_sc;
00232     int default_thread_pause_sc;
00233     int default_thread_detach_sc;
00234 
00235     /* True if the process should be traced when started/attached.  Newly
00236        started processes *must* be traced at first to exec them properly, but
00237        if this is false, tracing is turned off as soon it has done so.  */
00238     int want_signals;
00239 
00240     /* True if exceptions from the inferior process should be trapped.  This
00241        must be on to use breakpoints.  */
00242     int want_exceptions;
00243   };
00244 
00245 
00246 int
00247 __proc_pid (struct proc *proc)
00248 {
00249   return proc->inf->pid;
00250 }
00251 
00252 
00253 /* Update PROC's real suspend count to match it's desired one.  Returns true
00254    if we think PROC is now in a runnable state.  */
00255 int
00256 proc_update_sc (struct proc *proc)
00257 {
00258   int running;
00259   int err = 0;
00260   int delta = proc->sc - proc->cur_sc;
00261 
00262   if (delta)
00263     proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
00264 
00265   if (proc->sc == 0 && proc->state_changed)
00266     /* Since PROC may start running, we must write back any state changes.  */
00267     {
00268       gdb_assert (proc_is_thread (proc));
00269       proc_debug (proc, "storing back changed thread state");
00270       err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
00271                          (thread_state_t) &proc->state, THREAD_STATE_SIZE);
00272       if (!err)
00273         proc->state_changed = 0;
00274     }
00275 
00276   if (delta > 0)
00277     {
00278       while (delta-- > 0 && !err)
00279         {
00280           if (proc_is_task (proc))
00281             err = task_suspend (proc->port);
00282           else
00283             err = thread_suspend (proc->port);
00284         }
00285     }
00286   else
00287     {
00288       while (delta++ < 0 && !err)
00289         {
00290           if (proc_is_task (proc))
00291             err = task_resume (proc->port);
00292           else
00293             err = thread_resume (proc->port);
00294         }
00295     }
00296   if (!err)
00297     proc->cur_sc = proc->sc;
00298 
00299   /* If we got an error, then the task/thread has disappeared.  */
00300   running = !err && proc->sc == 0;
00301 
00302   proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
00303   if (err)
00304     proc_debug (proc, "err = %s", safe_strerror (err));
00305 
00306   if (running)
00307     {
00308       proc->aborted = 0;
00309       proc->state_valid = proc->state_changed = 0;
00310       proc->fetched_regs = 0;
00311     }
00312 
00313   return running;
00314 }
00315 
00316 
00317 /* Thread_abort is called on PROC if needed.  PROC must be a thread proc.
00318    If PROC is deemed `precious', then nothing is done unless FORCE is true.
00319    In particular, a thread is precious if it's running (in which case forcing
00320    it includes suspending it first), or if it has an exception pending.  */
00321 void
00322 proc_abort (struct proc *proc, int force)
00323 {
00324   gdb_assert (proc_is_thread (proc));
00325 
00326   if (!proc->aborted)
00327     {
00328       struct inf *inf = proc->inf;
00329       int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
00330 
00331       if (running && force)
00332         {
00333           proc->sc = 1;
00334           inf_update_suspends (proc->inf);
00335           running = 0;
00336           warning (_("Stopped %s."), proc_string (proc));
00337         }
00338       else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
00339         /* An exception is pending on PROC, which don't mess with.  */
00340         running = 1;
00341 
00342       if (!running)
00343         /* We only abort the thread if it's not actually running.  */
00344         {
00345           thread_abort (proc->port);
00346           proc_debug (proc, "aborted");
00347           proc->aborted = 1;
00348         }
00349       else
00350         proc_debug (proc, "not aborting");
00351     }
00352 }
00353 
00354 /* Make sure that the state field in PROC is up to date, and return a pointer
00355    to it, or 0 if something is wrong.  If WILL_MODIFY is true, makes sure
00356    that the thread is stopped and aborted first, and sets the state_changed
00357    field in PROC to true.  */
00358 thread_state_t
00359 proc_get_state (struct proc *proc, int will_modify)
00360 {
00361   int was_aborted = proc->aborted;
00362 
00363   proc_debug (proc, "updating state info%s",
00364               will_modify ? " (with intention to modify)" : "");
00365 
00366   proc_abort (proc, will_modify);
00367 
00368   if (!was_aborted && proc->aborted)
00369     /* PROC's state may have changed since we last fetched it.  */
00370     proc->state_valid = 0;
00371 
00372   if (!proc->state_valid)
00373     {
00374       mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
00375       error_t err =
00376         thread_get_state (proc->port, THREAD_STATE_FLAVOR,
00377                           (thread_state_t) &proc->state, &state_size);
00378 
00379       proc_debug (proc, "getting thread state");
00380       proc->state_valid = !err;
00381     }
00382 
00383   if (proc->state_valid)
00384     {
00385       if (will_modify)
00386         proc->state_changed = 1;
00387       return (thread_state_t) &proc->state;
00388     }
00389   else
00390     return 0;
00391 }
00392 
00393 
00394 /* Set PORT to PROC's exception port.  */
00395 error_t
00396 proc_get_exception_port (struct proc * proc, mach_port_t * port)
00397 {
00398   if (proc_is_task (proc))
00399     return task_get_exception_port (proc->port, port);
00400   else
00401     return thread_get_exception_port (proc->port, port);
00402 }
00403 
00404 /* Set PROC's exception port to PORT.  */
00405 error_t
00406 proc_set_exception_port (struct proc * proc, mach_port_t port)
00407 {
00408   proc_debug (proc, "setting exception port: %d", port);
00409   if (proc_is_task (proc))
00410     return task_set_exception_port (proc->port, port);
00411   else
00412     return thread_set_exception_port (proc->port, port);
00413 }
00414 
00415 /* Get PROC's exception port, cleaning up a bit if proc has died.  */
00416 static mach_port_t
00417 _proc_get_exc_port (struct proc *proc)
00418 {
00419   mach_port_t exc_port;
00420   error_t err = proc_get_exception_port (proc, &exc_port);
00421 
00422   if (err)
00423     /* PROC must be dead.  */
00424     {
00425       if (proc->exc_port)
00426         mach_port_deallocate (mach_task_self (), proc->exc_port);
00427       proc->exc_port = MACH_PORT_NULL;
00428       if (proc->saved_exc_port)
00429         mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
00430       proc->saved_exc_port = MACH_PORT_NULL;
00431     }
00432 
00433   return exc_port;
00434 }
00435 
00436 /* Replace PROC's exception port with EXC_PORT, unless it's already
00437    been done.  Stash away any existing exception port so we can
00438    restore it later.  */
00439 void
00440 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
00441 {
00442   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
00443 
00444   if (cur_exc_port)
00445     {
00446       error_t err = 0;
00447 
00448       proc_debug (proc, "inserting exception port: %d", exc_port);
00449 
00450       if (cur_exc_port != exc_port)
00451         /* Put in our exception port.  */
00452         err = proc_set_exception_port (proc, exc_port);
00453 
00454       if (err || cur_exc_port == proc->exc_port)
00455         /* We previously set the exception port, and it's still set.  So we
00456            just keep the old saved port which is what the proc set.  */
00457         {
00458           if (cur_exc_port)
00459             mach_port_deallocate (mach_task_self (), cur_exc_port);
00460         }
00461       else
00462         /* Keep a copy of PROC's old exception port so it can be restored.  */
00463         {
00464           if (proc->saved_exc_port)
00465             mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
00466           proc->saved_exc_port = cur_exc_port;
00467         }
00468 
00469       proc_debug (proc, "saved exception port: %d", proc->saved_exc_port);
00470 
00471       if (!err)
00472         proc->exc_port = exc_port;
00473       else
00474         warning (_("Error setting exception port for %s: %s"),
00475                  proc_string (proc), safe_strerror (err));
00476     }
00477 }
00478 
00479 /* If we previously replaced PROC's exception port, put back what we
00480    found there at the time, unless *our* exception port has since been
00481    overwritten, in which case who knows what's going on.  */
00482 void
00483 proc_restore_exc_port (struct proc *proc)
00484 {
00485   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
00486 
00487   if (cur_exc_port)
00488     {
00489       error_t err = 0;
00490 
00491       proc_debug (proc, "restoring real exception port");
00492 
00493       if (proc->exc_port == cur_exc_port)
00494         /* Our's is still there.  */
00495         err = proc_set_exception_port (proc, proc->saved_exc_port);
00496 
00497       if (proc->saved_exc_port)
00498         mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
00499       proc->saved_exc_port = MACH_PORT_NULL;
00500 
00501       if (!err)
00502         proc->exc_port = MACH_PORT_NULL;
00503       else
00504         warning (_("Error setting exception port for %s: %s"),
00505                  proc_string (proc), safe_strerror (err));
00506     }
00507 }
00508 
00509 
00510 /* Turns hardware tracing in PROC on or off when SET is true or false,
00511    respectively.  Returns true on success.  */
00512 int
00513 proc_trace (struct proc *proc, int set)
00514 {
00515   thread_state_t state = proc_get_state (proc, 1);
00516 
00517   if (!state)
00518     return 0;                   /* The thread must be dead.  */
00519 
00520   proc_debug (proc, "tracing %s", set ? "on" : "off");
00521 
00522   if (set)
00523     {
00524       /* XXX We don't get the exception unless the thread has its own
00525          exception port????  */
00526       if (proc->exc_port == MACH_PORT_NULL)
00527         proc_steal_exc_port (proc, proc->inf->event_port);
00528       THREAD_STATE_SET_TRACED (state);
00529     }
00530   else
00531     THREAD_STATE_CLEAR_TRACED (state);
00532 
00533   return 1;
00534 }
00535 
00536 
00537 /* A variable from which to assign new TIDs.  */
00538 static int next_thread_id = 1;
00539 
00540 /* Returns a new proc structure with the given fields.  Also adds a
00541    notification for PORT becoming dead to be sent to INF's notify port.  */
00542 struct proc *
00543 make_proc (struct inf *inf, mach_port_t port, int tid)
00544 {
00545   error_t err;
00546   mach_port_t prev_port = MACH_PORT_NULL;
00547   struct proc *proc = xmalloc (sizeof (struct proc));
00548 
00549   proc->port = port;
00550   proc->tid = tid;
00551   proc->inf = inf;
00552   proc->next = 0;
00553   proc->saved_exc_port = MACH_PORT_NULL;
00554   proc->exc_port = MACH_PORT_NULL;
00555 
00556   proc->sc = 0;
00557   proc->cur_sc = 0;
00558 
00559   /* Note that these are all the values for threads; the task simply uses the
00560      corresponding field in INF directly.  */
00561   proc->run_sc = inf->default_thread_run_sc;
00562   proc->pause_sc = inf->default_thread_pause_sc;
00563   proc->detach_sc = inf->default_thread_detach_sc;
00564   proc->resume_sc = proc->run_sc;
00565 
00566   proc->aborted = 0;
00567   proc->dead = 0;
00568   proc->state_valid = 0;
00569   proc->state_changed = 0;
00570 
00571   proc_debug (proc, "is new");
00572 
00573   /* Get notified when things die.  */
00574   err =
00575     mach_port_request_notification (mach_task_self (), port,
00576                                     MACH_NOTIFY_DEAD_NAME, 1,
00577                                     inf->event_port,
00578                                     MACH_MSG_TYPE_MAKE_SEND_ONCE,
00579                                     &prev_port);
00580   if (err)
00581     warning (_("Couldn't request notification for port %d: %s"),
00582              port, safe_strerror (err));
00583   else
00584     {
00585       proc_debug (proc, "notifications to: %d", inf->event_port);
00586       if (prev_port != MACH_PORT_NULL)
00587         mach_port_deallocate (mach_task_self (), prev_port);
00588     }
00589 
00590   if (inf->want_exceptions)
00591     {
00592       if (proc_is_task (proc))
00593         /* Make the task exception port point to us.  */
00594         proc_steal_exc_port (proc, inf->event_port);
00595       else
00596         /* Just clear thread exception ports -- they default to the
00597            task one.  */
00598         proc_steal_exc_port (proc, MACH_PORT_NULL);
00599     }
00600 
00601   return proc;
00602 }
00603 
00604 /* Frees PROC and any resources it uses, and returns the value of PROC's 
00605    next field.  */
00606 struct proc *
00607 _proc_free (struct proc *proc)
00608 {
00609   struct inf *inf = proc->inf;
00610   struct proc *next = proc->next;
00611 
00612   proc_debug (proc, "freeing...");
00613 
00614   if (proc == inf->step_thread)
00615     /* Turn off single stepping.  */
00616     inf_set_step_thread (inf, 0);
00617   if (proc == inf->wait.thread)
00618     inf_clear_wait (inf);
00619   if (proc == inf->signal_thread)
00620     inf->signal_thread = 0;
00621 
00622   if (proc->port != MACH_PORT_NULL)
00623     {
00624       if (proc->exc_port != MACH_PORT_NULL)
00625         /* Restore the original exception port.  */
00626         proc_restore_exc_port (proc);
00627       if (proc->cur_sc != 0)
00628         /* Resume the thread/task.  */
00629         {
00630           proc->sc = 0;
00631           proc_update_sc (proc);
00632         }
00633       mach_port_deallocate (mach_task_self (), proc->port);
00634     }
00635 
00636   xfree (proc);
00637   return next;
00638 }
00639 
00640 
00641 struct inf *
00642 make_inf (void)
00643 {
00644   struct inf *inf = xmalloc (sizeof (struct inf));
00645 
00646   inf->task = 0;
00647   inf->threads = 0;
00648   inf->threads_up_to_date = 0;
00649   inf->pid = 0;
00650   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
00651   inf->wait.thread = 0;
00652   inf->wait.exc.handler = MACH_PORT_NULL;
00653   inf->wait.exc.reply = MACH_PORT_NULL;
00654   inf->step_thread = 0;
00655   inf->signal_thread = 0;
00656   inf->event_port = MACH_PORT_NULL;
00657   inf->running = 0;
00658   inf->stopped = 0;
00659   inf->nomsg = 1;
00660   inf->traced = 0;
00661   inf->no_wait = 0;
00662   inf->pending_execs = 0;
00663   inf->pause_sc = 1;
00664   inf->detach_sc = 0;
00665   inf->default_thread_run_sc = 0;
00666   inf->default_thread_pause_sc = 0;
00667   inf->default_thread_detach_sc = 0;
00668   inf->want_signals = 1;        /* By default */
00669   inf->want_exceptions = 1;     /* By default */
00670 
00671   return inf;
00672 }
00673 
00674 /* Clear INF's target wait status.  */
00675 void
00676 inf_clear_wait (struct inf *inf)
00677 {
00678   inf_debug (inf, "clearing wait");
00679   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
00680   inf->wait.thread = 0;
00681   inf->wait.suppress = 0;
00682   if (inf->wait.exc.handler != MACH_PORT_NULL)
00683     {
00684       mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
00685       inf->wait.exc.handler = MACH_PORT_NULL;
00686     }
00687   if (inf->wait.exc.reply != MACH_PORT_NULL)
00688     {
00689       mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
00690       inf->wait.exc.reply = MACH_PORT_NULL;
00691     }
00692 }
00693 
00694 
00695 void
00696 inf_cleanup (struct inf *inf)
00697 {
00698   inf_debug (inf, "cleanup");
00699 
00700   inf_clear_wait (inf);
00701 
00702   inf_set_pid (inf, -1);
00703   inf->pid = 0;
00704   inf->running = 0;
00705   inf->stopped = 0;
00706   inf->nomsg = 1;
00707   inf->traced = 0;
00708   inf->no_wait = 0;
00709   inf->pending_execs = 0;
00710 
00711   if (inf->event_port)
00712     {
00713       mach_port_destroy (mach_task_self (), inf->event_port);
00714       inf->event_port = MACH_PORT_NULL;
00715     }
00716 }
00717 
00718 void
00719 inf_startup (struct inf *inf, int pid)
00720 {
00721   error_t err;
00722 
00723   inf_debug (inf, "startup: pid = %d", pid);
00724 
00725   inf_cleanup (inf);
00726 
00727   /* Make the port on which we receive all events.  */
00728   err = mach_port_allocate (mach_task_self (),
00729                             MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
00730   if (err)
00731     error (_("Error allocating event port: %s"), safe_strerror (err));
00732 
00733   /* Make a send right for it, so we can easily copy it for other people.  */
00734   mach_port_insert_right (mach_task_self (), inf->event_port,
00735                           inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
00736   inf_set_pid (inf, pid);
00737 }
00738 
00739 
00740 /* Close current process, if any, and attach INF to process PORT.  */
00741 void
00742 inf_set_pid (struct inf *inf, pid_t pid)
00743 {
00744   task_t task_port;
00745   struct proc *task = inf->task;
00746 
00747   inf_debug (inf, "setting pid: %d", pid);
00748 
00749   if (pid < 0)
00750     task_port = MACH_PORT_NULL;
00751   else
00752     {
00753       error_t err = proc_pid2task (proc_server, pid, &task_port);
00754 
00755       if (err)
00756         error (_("Error getting task for pid %d: %s"),
00757                pid, safe_strerror (err));
00758     }
00759 
00760   inf_debug (inf, "setting task: %d", task_port);
00761 
00762   if (inf->pause_sc)
00763     task_suspend (task_port);
00764 
00765   if (task && task->port != task_port)
00766     {
00767       inf->task = 0;
00768       inf_validate_procs (inf); /* Trash all the threads.  */
00769       _proc_free (task);        /* And the task.  */
00770     }
00771 
00772   if (task_port != MACH_PORT_NULL)
00773     {
00774       inf->task = make_proc (inf, task_port, PROC_TID_TASK);
00775       inf->threads_up_to_date = 0;
00776     }
00777 
00778   if (inf->task)
00779     {
00780       inf->pid = pid;
00781       if (inf->pause_sc)
00782         /* Reflect task_suspend above.  */
00783         inf->task->sc = inf->task->cur_sc = 1;
00784     }
00785   else
00786     inf->pid = -1;
00787 }
00788 
00789 
00790 /* Validates INF's stopped, nomsg and traced field from the actual
00791    proc server state.  Note that the traced field is only updated from
00792    the proc server state if we do not have a message port.  If we do
00793    have a message port we'd better look at the tracemask itself.  */
00794 static void
00795 inf_validate_procinfo (struct inf *inf)
00796 {
00797   char *noise;
00798   mach_msg_type_number_t noise_len = 0;
00799   struct procinfo *pi;
00800   mach_msg_type_number_t pi_len = 0;
00801   int info_flags = 0;
00802   error_t err =
00803     proc_getprocinfo (proc_server, inf->pid, &info_flags,
00804                       (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
00805 
00806   if (!err)
00807     {
00808       inf->stopped = !!(pi->state & PI_STOPPED);
00809       inf->nomsg = !!(pi->state & PI_NOMSG);
00810       if (inf->nomsg)
00811         inf->traced = !!(pi->state & PI_TRACED);
00812       vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
00813       if (noise_len > 0)
00814         vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
00815     }
00816 }
00817 
00818 /* Validates INF's task suspend count.  If it's higher than we expect,
00819    verify with the user before `stealing' the extra count.  */
00820 static void
00821 inf_validate_task_sc (struct inf *inf)
00822 {
00823   char *noise;
00824   mach_msg_type_number_t noise_len = 0;
00825   struct procinfo *pi;
00826   mach_msg_type_number_t pi_len = 0;
00827   int info_flags = PI_FETCH_TASKINFO;
00828   int suspend_count = -1;
00829   error_t err;
00830 
00831  retry:
00832   err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
00833                           (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
00834   if (err)
00835     {
00836       inf->task->dead = 1; /* oh well */
00837       return;
00838     }
00839 
00840   if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
00841     {
00842       /* The proc server might have suspended the task while stopping
00843          it.  This happens when the task is handling a traced signal.
00844          Refetch the suspend count.  The proc server should be
00845          finished stopping the task by now.  */
00846       suspend_count = pi->taskinfo.suspend_count;
00847       goto retry;
00848     }
00849 
00850   suspend_count = pi->taskinfo.suspend_count;
00851 
00852   vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
00853   if (noise_len > 0)
00854     vm_deallocate (mach_task_self (), (vm_address_t) pi, pi_len);
00855 
00856   if (inf->task->cur_sc < suspend_count)
00857     {
00858       int abort;
00859 
00860       target_terminal_ours ();  /* Allow I/O.  */
00861       abort = !query (_("Pid %d has an additional task suspend count of %d;"
00862                       " clear it? "), inf->pid,
00863                       suspend_count - inf->task->cur_sc);
00864       target_terminal_inferior ();      /* Give it back to the child.  */
00865 
00866       if (abort)
00867         error (_("Additional task suspend count left untouched."));
00868 
00869       inf->task->cur_sc = suspend_count;
00870     }
00871 }
00872 
00873 /* Turns tracing for INF on or off, depending on ON, unless it already
00874    is.  If INF is running, the resume_sc count of INF's threads will
00875    be modified, and the signal thread will briefly be run to change
00876    the trace state.  */
00877 void
00878 inf_set_traced (struct inf *inf, int on)
00879 {
00880   if (on == inf->traced)
00881     return;
00882   
00883   if (inf->task && !inf->task->dead)
00884     /* Make it take effect immediately.  */
00885     {
00886       sigset_t mask = on ? ~(sigset_t) 0 : 0;
00887       error_t err =
00888         INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
00889                                                        INIT_TRACEMASK, mask));
00890 
00891       if (err == EIEIO)
00892         {
00893           if (on)
00894             warning (_("Can't modify tracing state for pid %d: %s"),
00895                      inf->pid, "No signal thread");
00896           inf->traced = on;
00897         }
00898       else if (err)
00899         warning (_("Can't modify tracing state for pid %d: %s"),
00900                  inf->pid, safe_strerror (err));
00901       else
00902         inf->traced = on;
00903     }
00904   else
00905     inf->traced = on;
00906 }
00907 
00908 
00909 /* Makes all the real suspend count deltas of all the procs in INF
00910    match the desired values.  Careful to always do thread/task suspend
00911    counts in the safe order.  Returns true if at least one thread is
00912    thought to be running.  */
00913 int
00914 inf_update_suspends (struct inf *inf)
00915 {
00916   struct proc *task = inf->task;
00917 
00918   /* We don't have to update INF->threads even though we're iterating over it
00919      because we'll change a thread only if it already has an existing proc
00920      entry.  */
00921   inf_debug (inf, "updating suspend counts");
00922 
00923   if (task)
00924     {
00925       struct proc *thread;
00926       int task_running = (task->sc == 0), thread_running = 0;
00927 
00928       if (task->sc > task->cur_sc)
00929         /* The task is becoming _more_ suspended; do before any threads.  */
00930         task_running = proc_update_sc (task);
00931 
00932       if (inf->pending_execs)
00933         /* When we're waiting for an exec, things may be happening behind our
00934            back, so be conservative.  */
00935         thread_running = 1;
00936 
00937       /* Do all the thread suspend counts.  */
00938       for (thread = inf->threads; thread; thread = thread->next)
00939         thread_running |= proc_update_sc (thread);
00940 
00941       if (task->sc != task->cur_sc)
00942         /* We didn't do the task first, because we wanted to wait for the
00943            threads; do it now.  */
00944         task_running = proc_update_sc (task);
00945 
00946       inf_debug (inf, "%srunning...",
00947                  (thread_running && task_running) ? "" : "not ");
00948 
00949       inf->running = thread_running && task_running;
00950 
00951       /* Once any thread has executed some code, we can't depend on the
00952          threads list any more.  */
00953       if (inf->running)
00954         inf->threads_up_to_date = 0;
00955 
00956       return inf->running;
00957     }
00958 
00959   return 0;
00960 }
00961 
00962 
00963 /* Converts a GDB pid to a struct proc.  */
00964 struct proc *
00965 inf_tid_to_thread (struct inf *inf, int tid)
00966 {
00967   struct proc *thread = inf->threads;
00968 
00969   while (thread)
00970     if (thread->tid == tid)
00971       return thread;
00972     else
00973       thread = thread->next;
00974   return 0;
00975 }
00976 
00977 /* Converts a thread port to a struct proc.  */
00978 struct proc *
00979 inf_port_to_thread (struct inf *inf, mach_port_t port)
00980 {
00981   struct proc *thread = inf->threads;
00982 
00983   while (thread)
00984     if (thread->port == port)
00985       return thread;
00986     else
00987       thread = thread->next;
00988   return 0;
00989 }
00990 
00991 
00992 /* Make INF's list of threads be consistent with reality of TASK.  */
00993 void
00994 inf_validate_procs (struct inf *inf)
00995 {
00996   thread_array_t threads;
00997   mach_msg_type_number_t num_threads, i;
00998   struct proc *task = inf->task;
00999 
01000   /* If no threads are currently running, this function will guarantee that
01001      things are up to date.  The exception is if there are zero threads --
01002      then it is almost certainly in an odd state, and probably some outside
01003      agent will create threads.  */
01004   inf->threads_up_to_date = inf->threads ? !inf->running : 0;
01005 
01006   if (task)
01007     {
01008       error_t err = task_threads (task->port, &threads, &num_threads);
01009 
01010       inf_debug (inf, "fetching threads");
01011       if (err)
01012         /* TASK must be dead.  */
01013         {
01014           task->dead = 1;
01015           task = 0;
01016         }
01017     }
01018 
01019   if (!task)
01020     {
01021       num_threads = 0;
01022       inf_debug (inf, "no task");
01023     }
01024 
01025   {
01026     /* Make things normally linear.  */
01027     mach_msg_type_number_t search_start = 0;
01028     /* Which thread in PROCS corresponds to each task thread, & the task.  */
01029     struct proc *matched[num_threads + 1];
01030     /* The last thread in INF->threads, so we can add to the end.  */
01031     struct proc *last = 0;
01032     /* The current thread we're considering.  */
01033     struct proc *thread = inf->threads;
01034 
01035     memset (matched, 0, sizeof (matched));
01036 
01037     while (thread)
01038       {
01039         mach_msg_type_number_t left;
01040 
01041         for (i = search_start, left = num_threads; left; i++, left--)
01042           {
01043             if (i >= num_threads)
01044               i -= num_threads; /* I wrapped around.  */
01045             if (thread->port == threads[i])
01046               /* We already know about this thread.  */
01047               {
01048                 matched[i] = thread;
01049                 last = thread;
01050                 thread = thread->next;
01051                 search_start++;
01052                 break;
01053               }
01054           }
01055 
01056         if (!left)
01057           {
01058             proc_debug (thread, "died!");
01059             thread->port = MACH_PORT_NULL;
01060             thread = _proc_free (thread);       /* THREAD is dead.  */
01061             if (last)
01062               last->next = thread;
01063             else
01064               inf->threads = thread;
01065           }
01066       }
01067 
01068     for (i = 0; i < num_threads; i++)
01069       {
01070         if (matched[i])
01071           /* Throw away the duplicate send right.  */
01072           mach_port_deallocate (mach_task_self (), threads[i]);
01073         else
01074           /* THREADS[I] is a thread we don't know about yet!  */
01075           {
01076             ptid_t ptid;
01077 
01078             thread = make_proc (inf, threads[i], next_thread_id++);
01079             if (last)
01080               last->next = thread;
01081             else
01082               inf->threads = thread;
01083             last = thread;
01084             proc_debug (thread, "new thread: %d", threads[i]);
01085 
01086             ptid = ptid_build (inf->pid, thread->tid, 0);
01087 
01088             /* Tell GDB's generic thread code.  */
01089 
01090             if (ptid_equal (inferior_ptid, pid_to_ptid (inf->pid)))
01091               /* This is the first time we're hearing about thread
01092                  ids, after a fork-child.  */
01093               thread_change_ptid (inferior_ptid, ptid);
01094             else if (inf->pending_execs != 0)
01095               /* This is a shell thread.  */
01096               add_thread_silent (ptid);
01097             else
01098               add_thread (ptid);
01099           }
01100       }
01101 
01102     vm_deallocate (mach_task_self (),
01103                    (vm_address_t) threads, (num_threads * sizeof (thread_t)));
01104   }
01105 }
01106 
01107 
01108 /* Makes sure that INF's thread list is synced with the actual process.  */
01109 int
01110 inf_update_procs (struct inf *inf)
01111 {
01112   if (!inf->task)
01113     return 0;
01114   if (!inf->threads_up_to_date)
01115     inf_validate_procs (inf);
01116   return !!inf->task;
01117 }
01118 
01119 /* Sets the resume_sc of each thread in inf.  That of RUN_THREAD is set to 0,
01120    and others are set to their run_sc if RUN_OTHERS is true, and otherwise
01121    their pause_sc.  */
01122 void
01123 inf_set_threads_resume_sc (struct inf *inf,
01124                            struct proc *run_thread, int run_others)
01125 {
01126   struct proc *thread;
01127 
01128   inf_update_procs (inf);
01129   for (thread = inf->threads; thread; thread = thread->next)
01130     if (thread == run_thread)
01131       thread->resume_sc = 0;
01132     else if (run_others)
01133       thread->resume_sc = thread->run_sc;
01134     else
01135       thread->resume_sc = thread->pause_sc;
01136 }
01137 
01138 
01139 /* Cause INF to continue execution immediately; individual threads may still
01140    be suspended (but their suspend counts will be updated).  */
01141 void
01142 inf_resume (struct inf *inf)
01143 {
01144   struct proc *thread;
01145 
01146   inf_update_procs (inf);
01147 
01148   for (thread = inf->threads; thread; thread = thread->next)
01149     thread->sc = thread->resume_sc;
01150 
01151   if (inf->task)
01152     {
01153       if (!inf->pending_execs)
01154         /* Try to make sure our task count is correct -- in the case where
01155            we're waiting for an exec though, things are too volatile, so just
01156            assume things will be reasonable (which they usually will be).  */
01157         inf_validate_task_sc (inf);
01158       inf->task->sc = 0;
01159     }
01160 
01161   inf_update_suspends (inf);
01162 }
01163 
01164 /* Cause INF to stop execution immediately; individual threads may still
01165    be running.  */
01166 void
01167 inf_suspend (struct inf *inf)
01168 {
01169   struct proc *thread;
01170 
01171   inf_update_procs (inf);
01172 
01173   for (thread = inf->threads; thread; thread = thread->next)
01174     thread->sc = thread->pause_sc;
01175 
01176   if (inf->task)
01177     inf->task->sc = inf->pause_sc;
01178 
01179   inf_update_suspends (inf);
01180 }
01181 
01182 
01183 /* INF has one thread PROC that is in single-stepping mode.  This
01184    function changes it to be PROC, changing any old step_thread to be
01185    a normal one.  A PROC of 0 clears any existing value.  */
01186 void
01187 inf_set_step_thread (struct inf *inf, struct proc *thread)
01188 {
01189   gdb_assert (!thread || proc_is_thread (thread));
01190 
01191   if (thread)
01192     inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
01193   else
01194     inf_debug (inf, "clearing step thread");
01195 
01196   if (inf->step_thread != thread)
01197     {
01198       if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
01199         if (!proc_trace (inf->step_thread, 0))
01200           return;
01201       if (thread && proc_trace (thread, 1))
01202         inf->step_thread = thread;
01203       else
01204         inf->step_thread = 0;
01205     }
01206 }
01207 
01208 
01209 /* Set up the thread resume_sc's so that only the signal thread is running
01210    (plus whatever other thread are set to always run).  Returns true if we
01211    did so, or false if we can't find a signal thread.  */
01212 int
01213 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
01214 {
01215   if (inf->signal_thread)
01216     {
01217       inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
01218       return 1;
01219     }
01220   else
01221     return 0;
01222 }
01223 
01224 static void
01225 inf_update_signal_thread (struct inf *inf)
01226 {
01227   /* XXX for now we assume that if there's a msgport, the 2nd thread is
01228      the signal thread.  */
01229   inf->signal_thread = inf->threads ? inf->threads->next : 0;
01230 }
01231 
01232 
01233 /* Detachs from INF's inferior task, letting it run once again...  */
01234 void
01235 inf_detach (struct inf *inf)
01236 {
01237   struct proc *task = inf->task;
01238 
01239   inf_debug (inf, "detaching...");
01240 
01241   inf_clear_wait (inf);
01242   inf_set_step_thread (inf, 0);
01243 
01244   if (task)
01245     {
01246       struct proc *thread;
01247 
01248       inf_validate_procinfo (inf);
01249 
01250       inf_set_traced (inf, 0);
01251       if (inf->stopped)
01252         {
01253           if (inf->nomsg)
01254             inf_continue (inf);
01255           else
01256             inf_signal (inf, GDB_SIGNAL_0);
01257         }
01258 
01259       proc_restore_exc_port (task);
01260       task->sc = inf->detach_sc;
01261 
01262       for (thread = inf->threads; thread; thread = thread->next)
01263         {
01264           proc_restore_exc_port (thread);
01265           thread->sc = thread->detach_sc;
01266         }
01267 
01268       inf_update_suspends (inf);
01269     }
01270 
01271   inf_cleanup (inf);
01272 }
01273 
01274 /* Attaches INF to the process with process id PID, returning it in a
01275    suspended state suitable for debugging.  */
01276 void
01277 inf_attach (struct inf *inf, int pid)
01278 {
01279   inf_debug (inf, "attaching: %d", pid);
01280 
01281   if (inf->pid)
01282     inf_detach (inf);
01283 
01284   inf_startup (inf, pid);
01285 }
01286 
01287 
01288 /* Makes sure that we've got our exception ports entrenched in the process.  */
01289 void
01290 inf_steal_exc_ports (struct inf *inf)
01291 {
01292   struct proc *thread;
01293 
01294   inf_debug (inf, "stealing exception ports");
01295 
01296   inf_set_step_thread (inf, 0); /* The step thread is special.  */
01297 
01298   proc_steal_exc_port (inf->task, inf->event_port);
01299   for (thread = inf->threads; thread; thread = thread->next)
01300     proc_steal_exc_port (thread, MACH_PORT_NULL);
01301 }
01302 
01303 /* Makes sure the process has its own exception ports.  */
01304 void
01305 inf_restore_exc_ports (struct inf *inf)
01306 {
01307   struct proc *thread;
01308 
01309   inf_debug (inf, "restoring exception ports");
01310 
01311   inf_set_step_thread (inf, 0); /* The step thread is special.  */
01312 
01313   proc_restore_exc_port (inf->task);
01314   for (thread = inf->threads; thread; thread = thread->next)
01315     proc_restore_exc_port (thread);
01316 }
01317 
01318 
01319 /* Deliver signal SIG to INF.  If INF is stopped, delivering a signal, even
01320    signal 0, will continue it.  INF is assumed to be in a paused state, and
01321    the resume_sc's of INF's threads may be affected.  */
01322 void
01323 inf_signal (struct inf *inf, enum gdb_signal sig)
01324 {
01325   error_t err = 0;
01326   int host_sig = gdb_signal_to_host (sig);
01327 
01328 #define NAME gdb_signal_to_name (sig)
01329 
01330   if (host_sig >= _NSIG)
01331     /* A mach exception.  Exceptions are encoded in the signal space by
01332        putting them after _NSIG; this assumes they're positive (and not
01333        extremely large)!  */
01334     {
01335       struct inf_wait *w = &inf->wait;
01336 
01337       if (w->status.kind == TARGET_WAITKIND_STOPPED
01338           && w->status.value.sig == sig
01339           && w->thread && !w->thread->aborted)
01340         /* We're passing through the last exception we received.  This is
01341            kind of bogus, because exceptions are per-thread whereas gdb
01342            treats signals as per-process.  We just forward the exception to
01343            the correct handler, even it's not for the same thread as TID --
01344            i.e., we pretend it's global.  */
01345         {
01346           struct exc_state *e = &w->exc;
01347 
01348           inf_debug (inf, "passing through exception:"
01349                      " task = %d, thread = %d, exc = %d"
01350                      ", code = %d, subcode = %d",
01351                      w->thread->port, inf->task->port,
01352                      e->exception, e->code, e->subcode);
01353           err =
01354             exception_raise_request (e->handler,
01355                                      e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
01356                                      w->thread->port, inf->task->port,
01357                                      e->exception, e->code, e->subcode);
01358         }
01359       else
01360         error (_("Can't forward spontaneous exception (%s)."), NAME);
01361     }
01362   else
01363     /* A Unix signal.  */
01364   if (inf->stopped)
01365     /* The process is stopped and expecting a signal.  Just send off a
01366        request and let it get handled when we resume everything.  */
01367     {
01368       inf_debug (inf, "sending %s to stopped process", NAME);
01369       err =
01370         INF_MSGPORT_RPC (inf,
01371                          msg_sig_post_untraced_request (msgport,
01372                                                         inf->event_port,
01373                                                MACH_MSG_TYPE_MAKE_SEND_ONCE,
01374                                                         host_sig, 0,
01375                                                         refport));
01376       if (!err)
01377         /* Posting an untraced signal automatically continues it.
01378            We clear this here rather than when we get the reply
01379            because we'd rather assume it's not stopped when it
01380            actually is, than the reverse.  */
01381         inf->stopped = 0;
01382     }
01383   else
01384     /* It's not expecting it.  We have to let just the signal thread
01385        run, and wait for it to get into a reasonable state before we
01386        can continue the rest of the process.  When we finally resume the
01387        process the signal we request will be the very first thing that
01388        happens.  */
01389     {
01390       inf_debug (inf, "sending %s to unstopped process"
01391                  " (so resuming signal thread)", NAME);
01392       err =
01393         INF_RESUME_MSGPORT_RPC (inf,
01394                                 msg_sig_post_untraced (msgport, host_sig,
01395                                                        0, refport));
01396     }
01397 
01398   if (err == EIEIO)
01399     /* Can't do too much...  */
01400     warning (_("Can't deliver signal %s: No signal thread."), NAME);
01401   else if (err)
01402     warning (_("Delivering signal %s: %s"), NAME, safe_strerror (err));
01403 
01404 #undef NAME
01405 }
01406 
01407 
01408 /* Continue INF without delivering a signal.  This is meant to be used
01409    when INF does not have a message port.  */
01410 void
01411 inf_continue (struct inf *inf)
01412 {
01413   process_t proc;
01414   error_t err = proc_pid2proc (proc_server, inf->pid, &proc);
01415 
01416   if (!err)
01417     {
01418       inf_debug (inf, "continuing process");
01419 
01420       err = proc_mark_cont (proc);
01421       if (!err)
01422         {
01423           struct proc *thread;
01424 
01425           for (thread = inf->threads; thread; thread = thread->next)
01426             thread_resume (thread->port);
01427 
01428           inf->stopped = 0;
01429         }
01430     }
01431 
01432   if (err)
01433     warning (_("Can't continue process: %s"), safe_strerror (err));
01434 }
01435 
01436 
01437 /* The inferior used for all gdb target ops.  */
01438 struct inf *gnu_current_inf = 0;
01439 
01440 /* The inferior being waited for by gnu_wait.  Since GDB is decidely not
01441    multi-threaded, we don't bother to lock this.  */
01442 struct inf *waiting_inf;
01443 
01444 /* Wait for something to happen in the inferior, returning what in STATUS.  */
01445 static ptid_t
01446 gnu_wait (struct target_ops *ops,
01447           ptid_t ptid, struct target_waitstatus *status, int options)
01448 {
01449   struct msg
01450     {
01451       mach_msg_header_t hdr;
01452       mach_msg_type_t type;
01453       int data[8000];
01454     } msg;
01455   error_t err;
01456   struct proc *thread;
01457   struct inf *inf = gnu_current_inf;
01458 
01459   extern int exc_server (mach_msg_header_t *, mach_msg_header_t *);
01460   extern int msg_reply_server (mach_msg_header_t *, mach_msg_header_t *);
01461   extern int notify_server (mach_msg_header_t *, mach_msg_header_t *);
01462   extern int process_reply_server (mach_msg_header_t *, mach_msg_header_t *);
01463 
01464   gdb_assert (inf->task);
01465 
01466   if (!inf->threads && !inf->pending_execs)
01467     /* No threads!  Assume that maybe some outside agency is frobbing our
01468        task, and really look for new threads.  If we can't find any, just tell
01469        the user to try again later.  */
01470     {
01471       inf_validate_procs (inf);
01472       if (!inf->threads && !inf->task->dead)
01473         error (_("There are no threads; try again later."));
01474     }
01475 
01476   waiting_inf = inf;
01477 
01478   inf_debug (inf, "waiting for: %s", target_pid_to_str (ptid));
01479 
01480 rewait:
01481   if (proc_wait_pid != inf->pid && !inf->no_wait)
01482     /* Always get information on events from the proc server.  */
01483     {
01484       inf_debug (inf, "requesting wait on pid %d", inf->pid);
01485 
01486       if (proc_wait_pid)
01487         /* The proc server is single-threaded, and only allows a single
01488            outstanding wait request, so we have to cancel the previous one.  */
01489         {
01490           inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
01491           interrupt_operation (proc_server, 0);
01492         }
01493 
01494       err =
01495         proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
01496       if (err)
01497         warning (_("wait request failed: %s"), safe_strerror (err));
01498       else
01499         {
01500           inf_debug (inf, "waits pending: %d", proc_waits_pending);
01501           proc_wait_pid = inf->pid;
01502           /* Even if proc_waits_pending was > 0 before, we still won't
01503              get any other replies, because it was either from a
01504              different INF, or a different process attached to INF --
01505              and the event port, which is the wait reply port, changes
01506              when you switch processes.  */
01507           proc_waits_pending = 1;
01508         }
01509     }
01510 
01511   inf_clear_wait (inf);
01512 
01513   /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
01514      (3) wait reply from the proc server.  */
01515 
01516   inf_debug (inf, "waiting for an event...");
01517   err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
01518                   0, sizeof (struct msg), inf->event_port,
01519                   MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
01520 
01521   /* Re-suspend the task.  */
01522   inf_suspend (inf);
01523 
01524   if (!inf->task && inf->pending_execs)
01525     /* When doing an exec, it's possible that the old task wasn't reused
01526        (e.g., setuid execs).  So if the task seems to have disappeared,
01527        attempt to refetch it, as the pid should still be the same.  */
01528     inf_set_pid (inf, inf->pid);
01529 
01530   if (err == EMACH_RCV_INTERRUPTED)
01531     inf_debug (inf, "interrupted");
01532   else if (err)
01533     error (_("Couldn't wait for an event: %s"), safe_strerror (err));
01534   else
01535     {
01536       struct
01537         {
01538           mach_msg_header_t hdr;
01539           mach_msg_type_t err_type;
01540           kern_return_t err;
01541           char noise[200];
01542         }
01543       reply;
01544 
01545       inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
01546 
01547       /* Handle what we got.  */
01548       if (!notify_server (&msg.hdr, &reply.hdr)
01549           && !exc_server (&msg.hdr, &reply.hdr)
01550           && !process_reply_server (&msg.hdr, &reply.hdr)
01551           && !msg_reply_server (&msg.hdr, &reply.hdr))
01552         /* Whatever it is, it's something strange.  */
01553         error (_("Got a strange event, msg id = %d."), msg.hdr.msgh_id);
01554 
01555       if (reply.err)
01556         error (_("Handling event, msgid = %d: %s"),
01557                msg.hdr.msgh_id, safe_strerror (reply.err));
01558     }
01559 
01560   if (inf->pending_execs)
01561     /* We're waiting for the inferior to finish execing.  */
01562     {
01563       struct inf_wait *w = &inf->wait;
01564       enum target_waitkind kind = w->status.kind;
01565 
01566       if (kind == TARGET_WAITKIND_SPURIOUS)
01567         /* Since gdb is actually counting the number of times the inferior
01568            stops, expecting one stop per exec, we only return major events
01569            while execing.  */
01570         {
01571           w->suppress = 1;
01572           inf_debug (inf, "pending_execs = %d, ignoring minor event",
01573                      inf->pending_execs);
01574         }
01575       else if (kind == TARGET_WAITKIND_STOPPED
01576                && w->status.value.sig == GDB_SIGNAL_TRAP)
01577         /* Ah hah!  A SIGTRAP from the inferior while starting up probably
01578            means we've succesfully completed an exec!  */
01579         {
01580           if (--inf->pending_execs == 0)
01581             /* We're done!  */
01582             {
01583 #if 0                           /* do we need this?  */
01584               prune_threads (1);        /* Get rid of the old shell
01585                                            threads.  */
01586               renumber_threads (0);     /* Give our threads reasonable
01587                                            names.  */
01588 #endif
01589             }
01590           inf_debug (inf, "pending exec completed, pending_execs => %d",
01591                      inf->pending_execs);
01592         }
01593       else if (kind == TARGET_WAITKIND_STOPPED)
01594         /* It's possible that this signal is because of a crashed process
01595            being handled by the hurd crash server; in this case, the process
01596            will have an extra task suspend, which we need to know about.
01597            Since the code in inf_resume that normally checks for this is
01598            disabled while INF->pending_execs, we do the check here instead.  */
01599         inf_validate_task_sc (inf);
01600     }
01601 
01602   if (inf->wait.suppress)
01603     /* Some totally spurious event happened that we don't consider
01604        worth returning to gdb.  Just keep waiting.  */
01605     {
01606       inf_debug (inf, "suppressing return, rewaiting...");
01607       inf_resume (inf);
01608       goto rewait;
01609     }
01610 
01611   /* Pass back out our results.  */
01612   memcpy (status, &inf->wait.status, sizeof (*status));
01613 
01614   thread = inf->wait.thread;
01615   if (thread)
01616     ptid = ptid_build (inf->pid, thread->tid, 0);
01617   else if (ptid_equal (ptid, minus_one_ptid))
01618     thread = inf_tid_to_thread (inf, -1);
01619   else
01620     thread = inf_tid_to_thread (inf, ptid_get_lwp (ptid));
01621 
01622   if (!thread || thread->port == MACH_PORT_NULL)
01623     {
01624       /* TID is dead; try and find a new thread.  */
01625       if (inf_update_procs (inf) && inf->threads)
01626         ptid = ptid_build (inf->pid, inf->threads->tid, 0); /* The first
01627                                                                available
01628                                                                thread.  */
01629       else
01630         ptid = inferior_ptid;   /* let wait_for_inferior handle exit case */
01631     }
01632 
01633   if (thread
01634       && !ptid_equal (ptid, minus_one_ptid)
01635       && status->kind != TARGET_WAITKIND_SPURIOUS
01636       && inf->pause_sc == 0 && thread->pause_sc == 0)
01637     /* If something actually happened to THREAD, make sure we
01638        suspend it.  */
01639     {
01640       thread->sc = 1;
01641       inf_update_suspends (inf);
01642     }
01643 
01644   inf_debug (inf, "returning ptid = %s, status = %s (%d)",
01645              target_pid_to_str (ptid),
01646              status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
01647              : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
01648              : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
01649              : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
01650              : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
01651              : "?",
01652              status->value.integer);
01653 
01654   return ptid;
01655 }
01656 
01657 
01658 /* The rpc handler called by exc_server.  */
01659 error_t
01660 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
01661                            thread_t thread_port, task_t task_port,
01662                            int exception, int code, int subcode)
01663 {
01664   struct inf *inf = waiting_inf;
01665   struct proc *thread = inf_port_to_thread (inf, thread_port);
01666 
01667   inf_debug (waiting_inf,
01668              "thread = %d, task = %d, exc = %d, code = %d, subcode = %d",
01669              thread_port, task_port, exception, code, subcode);
01670 
01671   if (!thread)
01672     /* We don't know about thread?  */
01673     {
01674       inf_update_procs (inf);
01675       thread = inf_port_to_thread (inf, thread_port);
01676       if (!thread)
01677         /* Give up, the generating thread is gone.  */
01678         return 0;
01679     }
01680 
01681   mach_port_deallocate (mach_task_self (), thread_port);
01682   mach_port_deallocate (mach_task_self (), task_port);
01683 
01684   if (!thread->aborted)
01685     /* THREAD hasn't been aborted since this exception happened (abortion
01686        clears any exception state), so it must be real.  */
01687     {
01688       /* Store away the details; this will destroy any previous info.  */
01689       inf->wait.thread = thread;
01690 
01691       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
01692 
01693       if (exception == EXC_BREAKPOINT)
01694         /* GDB likes to get SIGTRAP for breakpoints.  */
01695         {
01696           inf->wait.status.value.sig = GDB_SIGNAL_TRAP;
01697           mach_port_deallocate (mach_task_self (), reply_port);
01698         }
01699       else
01700         /* Record the exception so that we can forward it later.  */
01701         {
01702           if (thread->exc_port == port)
01703             {
01704               inf_debug (waiting_inf, "Handler is thread exception port <%d>",
01705                          thread->saved_exc_port);
01706               inf->wait.exc.handler = thread->saved_exc_port;
01707             }
01708           else
01709             {
01710               inf_debug (waiting_inf, "Handler is task exception port <%d>",
01711                          inf->task->saved_exc_port);
01712               inf->wait.exc.handler = inf->task->saved_exc_port;
01713               gdb_assert (inf->task->exc_port == port);
01714             }
01715           if (inf->wait.exc.handler != MACH_PORT_NULL)
01716             /* Add a reference to the exception handler.  */
01717             mach_port_mod_refs (mach_task_self (),
01718                                 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
01719                                 1);
01720 
01721           inf->wait.exc.exception = exception;
01722           inf->wait.exc.code = code;
01723           inf->wait.exc.subcode = subcode;
01724           inf->wait.exc.reply = reply_port;
01725 
01726           /* Exceptions are encoded in the signal space by putting
01727              them after _NSIG; this assumes they're positive (and not
01728              extremely large)!  */
01729           inf->wait.status.value.sig =
01730             gdb_signal_from_host (_NSIG + exception);
01731         }
01732     }
01733   else
01734     /* A supppressed exception, which ignore.  */
01735     {
01736       inf->wait.suppress = 1;
01737       mach_port_deallocate (mach_task_self (), reply_port);
01738     }
01739 
01740   return 0;
01741 }
01742 
01743 
01744 /* Fill in INF's wait field after a task has died without giving us more
01745    detailed information.  */
01746 void
01747 inf_task_died_status (struct inf *inf)
01748 {
01749   warning (_("Pid %d died with unknown exit status, using SIGKILL."),
01750            inf->pid);
01751   inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
01752   inf->wait.status.value.sig = GDB_SIGNAL_KILL;
01753 }
01754 
01755 /* Notify server routines.  The only real one is dead name notification.  */
01756 error_t
01757 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
01758 {
01759   struct inf *inf = waiting_inf;
01760 
01761   inf_debug (waiting_inf, "port = %d", dead_port);
01762 
01763   if (inf->task && inf->task->port == dead_port)
01764     {
01765       proc_debug (inf->task, "is dead");
01766       inf->task->port = MACH_PORT_NULL;
01767       if (proc_wait_pid == inf->pid)
01768         /* We have a wait outstanding on the process, which will return more
01769            detailed information, so delay until we get that.  */
01770         inf->wait.suppress = 1;
01771       else
01772         /* We never waited for the process (maybe it wasn't a child), so just
01773            pretend it got a SIGKILL.  */
01774         inf_task_died_status (inf);
01775     }
01776   else
01777     {
01778       struct proc *thread = inf_port_to_thread (inf, dead_port);
01779 
01780       if (thread)
01781         {
01782           proc_debug (thread, "is dead");
01783           thread->port = MACH_PORT_NULL;
01784         }
01785 
01786       if (inf->task->dead)
01787         /* Since the task is dead, its threads are dying with it.  */
01788         inf->wait.suppress = 1;
01789     }
01790 
01791   mach_port_deallocate (mach_task_self (), dead_port);
01792   inf->threads_up_to_date = 0;  /* Just in case.  */
01793 
01794   return 0;
01795 }
01796 
01797 
01798 static error_t
01799 ill_rpc (char *fun)
01800 {
01801   warning (_("illegal rpc: %s"), fun);
01802   return 0;
01803 }
01804 
01805 error_t
01806 do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t count)
01807 {
01808   return ill_rpc ("do_mach_notify_no_senders");
01809 }
01810 
01811 error_t
01812 do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name)
01813 {
01814   return ill_rpc ("do_mach_notify_port_deleted");
01815 }
01816 
01817 error_t
01818 do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name)
01819 {
01820   return ill_rpc ("do_mach_notify_msg_accepted");
01821 }
01822 
01823 error_t
01824 do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t name)
01825 {
01826   return ill_rpc ("do_mach_notify_port_destroyed");
01827 }
01828 
01829 error_t
01830 do_mach_notify_send_once (mach_port_t notify)
01831 {
01832   return ill_rpc ("do_mach_notify_send_once");
01833 }
01834 
01835 
01836 /* Process_reply server routines.  We only use process_wait_reply.  */
01837 
01838 error_t
01839 S_proc_wait_reply (mach_port_t reply, error_t err,
01840                    int status, int sigcode, rusage_t rusage, pid_t pid)
01841 {
01842   struct inf *inf = waiting_inf;
01843 
01844   inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
01845              err ? safe_strerror (err) : "0", pid, status, sigcode);
01846 
01847   if (err && proc_wait_pid && (!inf->task || !inf->task->port))
01848     /* Ack.  The task has died, but the task-died notification code didn't
01849        tell anyone because it thought a more detailed reply from the
01850        procserver was forthcoming.  However, we now learn that won't
01851        happen...  So we have to act like the task just died, and this time,
01852        tell the world.  */
01853     inf_task_died_status (inf);
01854 
01855   if (--proc_waits_pending == 0)
01856     /* PROC_WAIT_PID represents the most recent wait.  We will always get
01857        replies in order because the proc server is single threaded.  */
01858     proc_wait_pid = 0;
01859 
01860   inf_debug (inf, "waits pending now: %d", proc_waits_pending);
01861 
01862   if (err)
01863     {
01864       if (err != EINTR)
01865         {
01866           warning (_("Can't wait for pid %d: %s"),
01867                    inf->pid, safe_strerror (err));
01868           inf->no_wait = 1;
01869 
01870           /* Since we can't see the inferior's signals, don't trap them.  */
01871           inf_set_traced (inf, 0);
01872         }
01873     }
01874   else if (pid == inf->pid)
01875     {
01876       store_waitstatus (&inf->wait.status, status);
01877       if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
01878         /* The process has sent us a signal, and stopped itself in a sane
01879            state pending our actions.  */
01880         {
01881           inf_debug (inf, "process has stopped itself");
01882           inf->stopped = 1;
01883         }
01884     }
01885   else
01886     inf->wait.suppress = 1;     /* Something odd happened.  Ignore.  */
01887 
01888   return 0;
01889 }
01890 
01891 error_t
01892 S_proc_setmsgport_reply (mach_port_t reply, error_t err,
01893                          mach_port_t old_msg_port)
01894 {
01895   return ill_rpc ("S_proc_setmsgport_reply");
01896 }
01897 
01898 error_t
01899 S_proc_getmsgport_reply (mach_port_t reply, error_t err, mach_port_t msg_port)
01900 {
01901   return ill_rpc ("S_proc_getmsgport_reply");
01902 }
01903 
01904 
01905 /* Msg_reply server routines.  We only use msg_sig_post_untraced_reply.  */
01906 
01907 error_t
01908 S_msg_sig_post_untraced_reply (mach_port_t reply, error_t err)
01909 {
01910   struct inf *inf = waiting_inf;
01911 
01912   if (err == EBUSY)
01913     /* EBUSY is what we get when the crash server has grabbed control of the
01914        process and doesn't like what signal we tried to send it.  Just act
01915        like the process stopped (using a signal of 0 should mean that the
01916        *next* time the user continues, it will pass signal 0, which the crash
01917        server should like).  */
01918     {
01919       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
01920       inf->wait.status.value.sig = GDB_SIGNAL_0;
01921     }
01922   else if (err)
01923     warning (_("Signal delivery failed: %s"), safe_strerror (err));
01924 
01925   if (err)
01926     /* We only get this reply when we've posted a signal to a process which we
01927        thought was stopped, and which we expected to continue after the signal.
01928        Given that the signal has failed for some reason, it's reasonable to
01929        assume it's still stopped.  */
01930     inf->stopped = 1;
01931   else
01932     inf->wait.suppress = 1;
01933 
01934   return 0;
01935 }
01936 
01937 error_t
01938 S_msg_sig_post_reply (mach_port_t reply, error_t err)
01939 {
01940   return ill_rpc ("S_msg_sig_post_reply");
01941 }
01942 
01943 
01944 /* Returns the number of messages queued for the receive right PORT.  */
01945 static mach_port_msgcount_t
01946 port_msgs_queued (mach_port_t port)
01947 {
01948   struct mach_port_status status;
01949   error_t err =
01950     mach_port_get_receive_status (mach_task_self (), port, &status);
01951 
01952   if (err)
01953     return 0;
01954   else
01955     return status.mps_msgcount;
01956 }
01957 
01958 
01959 /* Resume execution of the inferior process.
01960 
01961    If STEP is nonzero, single-step it.
01962    If SIGNAL is nonzero, give it that signal.
01963 
01964    TID  STEP:
01965    -1   true   Single step the current thread allowing other threads to run.
01966    -1   false  Continue the current thread allowing other threads to run.
01967    X    true   Single step the given thread, don't allow any others to run.
01968    X    false  Continue the given thread, do not allow any others to run.
01969    (Where X, of course, is anything except -1)
01970 
01971    Note that a resume may not `take' if there are pending exceptions/&c
01972    still unprocessed from the last resume we did (any given resume may result
01973    in multiple events returned by wait).  */
01974 
01975 static void
01976 gnu_resume (struct target_ops *ops,
01977             ptid_t ptid, int step, enum gdb_signal sig)
01978 {
01979   struct proc *step_thread = 0;
01980   int resume_all;
01981   struct inf *inf = gnu_current_inf;
01982 
01983   inf_debug (inf, "ptid = %s, step = %d, sig = %d",
01984              target_pid_to_str (ptid), step, sig);
01985 
01986   inf_validate_procinfo (inf);
01987 
01988   if (sig != GDB_SIGNAL_0 || inf->stopped)
01989     {
01990       if (sig == GDB_SIGNAL_0 && inf->nomsg)
01991         inf_continue (inf);
01992       else
01993         inf_signal (inf, sig);
01994     }
01995   else if (inf->wait.exc.reply != MACH_PORT_NULL)
01996     /* We received an exception to which we have chosen not to forward, so
01997        abort the faulting thread, which will perhaps retake it.  */
01998     {
01999       proc_abort (inf->wait.thread, 1);
02000       warning (_("Aborting %s with unforwarded exception %s."),
02001                proc_string (inf->wait.thread),
02002                gdb_signal_to_name (inf->wait.status.value.sig));
02003     }
02004 
02005   if (port_msgs_queued (inf->event_port))
02006     /* If there are still messages in our event queue, don't bother resuming
02007        the process, as we're just going to stop it right away anyway.  */
02008     return;
02009 
02010   inf_update_procs (inf);
02011 
02012   /* A specific PTID means `step only this process id'.  */
02013   resume_all = ptid_equal (ptid, minus_one_ptid);
02014 
02015   if (resume_all)
02016     /* Allow all threads to run, except perhaps single-stepping one.  */
02017     {
02018       inf_debug (inf, "running all threads; tid = %d",
02019                  ptid_get_pid (inferior_ptid));
02020       ptid = inferior_ptid;     /* What to step.  */
02021       inf_set_threads_resume_sc (inf, 0, 1);
02022     }
02023   else
02024     /* Just allow a single thread to run.  */
02025     {
02026       struct proc *thread = inf_tid_to_thread (inf, ptid_get_lwp (ptid));
02027 
02028       if (!thread)
02029         error (_("Can't run single thread id %s: no such thread!"),
02030                target_pid_to_str (ptid));
02031       inf_debug (inf, "running one thread: %s", target_pid_to_str (ptid));
02032       inf_set_threads_resume_sc (inf, thread, 0);
02033     }
02034 
02035   if (step)
02036     {
02037       step_thread = inf_tid_to_thread (inf, ptid_get_lwp (ptid));
02038       if (!step_thread)
02039         warning (_("Can't step thread id %s: no such thread."),
02040                  target_pid_to_str (ptid));
02041       else
02042         inf_debug (inf, "stepping thread: %s", target_pid_to_str (ptid));
02043     }
02044   if (step_thread != inf->step_thread)
02045     inf_set_step_thread (inf, step_thread);
02046 
02047   inf_debug (inf, "here we go...");
02048   inf_resume (inf);
02049 }
02050 
02051 
02052 static void
02053 gnu_kill_inferior (struct target_ops *ops)
02054 {
02055   struct proc *task = gnu_current_inf->task;
02056 
02057   if (task)
02058     {
02059       proc_debug (task, "terminating...");
02060       task_terminate (task->port);
02061       inf_set_pid (gnu_current_inf, -1);
02062     }
02063   target_mourn_inferior ();
02064 }
02065 
02066 /* Clean up after the inferior dies.  */
02067 static void
02068 gnu_mourn_inferior (struct target_ops *ops)
02069 {
02070   inf_debug (gnu_current_inf, "rip");
02071   inf_detach (gnu_current_inf);
02072   unpush_target (ops);
02073   generic_mourn_inferior ();
02074 }
02075 
02076 
02077 /* Fork an inferior process, and start debugging it.  */
02078 
02079 /* Set INFERIOR_PID to the first thread available in the child, if any.  */
02080 static int
02081 inf_pick_first_thread (void)
02082 {
02083   if (gnu_current_inf->task && gnu_current_inf->threads)
02084     /* The first thread.  */
02085     return gnu_current_inf->threads->tid;
02086   else
02087     /* What may be the next thread.  */
02088     return next_thread_id;
02089 }
02090 
02091 static struct inf *
02092 cur_inf (void)
02093 {
02094   if (!gnu_current_inf)
02095     gnu_current_inf = make_inf ();
02096   return gnu_current_inf;
02097 }
02098 
02099 static void
02100 gnu_create_inferior (struct target_ops *ops, 
02101                      char *exec_file, char *allargs, char **env,
02102                      int from_tty)
02103 {
02104   struct inf *inf = cur_inf ();
02105   int pid;
02106 
02107   void trace_me ()
02108   {
02109     /* We're in the child; make this process stop as soon as it execs.  */
02110     inf_debug (inf, "tracing self");
02111     if (ptrace (PTRACE_TRACEME) != 0)
02112       error (_("ptrace (PTRACE_TRACEME) failed!"));
02113   }
02114 
02115   inf_debug (inf, "creating inferior");
02116 
02117   pid = fork_inferior (exec_file, allargs, env, trace_me,
02118                        NULL, NULL, NULL, NULL);
02119 
02120   /* Attach to the now stopped child, which is actually a shell...  */
02121   inf_debug (inf, "attaching to child: %d", pid);
02122 
02123   inf_attach (inf, pid);
02124 
02125   push_target (ops);
02126 
02127   inf->pending_execs = 2;
02128   inf->nomsg = 1;
02129   inf->traced = 1;
02130 
02131   /* Now let the child run again, knowing that it will stop
02132      immediately because of the ptrace.  */
02133   inf_resume (inf);
02134 
02135   /* We now have thread info.  */
02136   thread_change_ptid (inferior_ptid,
02137                       ptid_build (inf->pid, inf_pick_first_thread (), 0));
02138 
02139   startup_inferior (inf->pending_execs);
02140 
02141   inf_validate_procinfo (inf);
02142   inf_update_signal_thread (inf);
02143   inf_set_traced (inf, inf->want_signals);
02144 
02145   /* Execing the process will have trashed our exception ports; steal them
02146      back (or make sure they're restored if the user wants that).  */
02147   if (inf->want_exceptions)
02148     inf_steal_exc_ports (inf);
02149   else
02150     inf_restore_exc_ports (inf);
02151 }
02152 
02153 
02154 /* Attach to process PID, then initialize for debugging it
02155    and wait for the trace-trap that results from attaching.  */
02156 static void
02157 gnu_attach (struct target_ops *ops, char *args, int from_tty)
02158 {
02159   int pid;
02160   char *exec_file;
02161   struct inf *inf = cur_inf ();
02162   struct inferior *inferior;
02163 
02164   pid = parse_pid_to_attach (args);
02165 
02166   if (pid == getpid ())         /* Trying to masturbate?  */
02167     error (_("I refuse to debug myself!"));
02168 
02169   if (from_tty)
02170     {
02171       exec_file = (char *) get_exec_file (0);
02172 
02173       if (exec_file)
02174         printf_unfiltered ("Attaching to program `%s', pid %d\n",
02175                            exec_file, pid);
02176       else
02177         printf_unfiltered ("Attaching to pid %d\n", pid);
02178 
02179       gdb_flush (gdb_stdout);
02180     }
02181 
02182   inf_debug (inf, "attaching to pid: %d", pid);
02183 
02184   inf_attach (inf, pid);
02185 
02186   push_target (ops);
02187 
02188   inferior = current_inferior ();
02189   inferior_appeared (inferior, pid);
02190   inferior->attach_flag = 1;
02191 
02192   inf_update_procs (inf);
02193 
02194   inferior_ptid = ptid_build (pid, inf_pick_first_thread (), 0);
02195 
02196   /* We have to initialize the terminal settings now, since the code
02197      below might try to restore them.  */
02198   target_terminal_init ();
02199 
02200   /* If the process was stopped before we attached, make it continue the next
02201      time the user does a continue.  */
02202   inf_validate_procinfo (inf);
02203 
02204   inf_update_signal_thread (inf);
02205   inf_set_traced (inf, inf->want_signals);
02206 
02207 #if 0                           /* Do we need this?  */
02208   renumber_threads (0);         /* Give our threads reasonable names.  */
02209 #endif
02210 }
02211 
02212 
02213 /* Take a program previously attached to and detaches it.
02214    The program resumes execution and will no longer stop
02215    on signals, etc.  We'd better not have left any breakpoints
02216    in the program or it'll die when it hits one.  For this
02217    to work, it may be necessary for the process to have been
02218    previously attached.  It *might* work if the program was
02219    started via fork.  */
02220 static void
02221 gnu_detach (struct target_ops *ops, char *args, int from_tty)
02222 {
02223   int pid;
02224 
02225   if (from_tty)
02226     {
02227       char *exec_file = get_exec_file (0);
02228 
02229       if (exec_file)
02230         printf_unfiltered ("Detaching from program `%s' pid %d\n",
02231                            exec_file, gnu_current_inf->pid);
02232       else
02233         printf_unfiltered ("Detaching from pid %d\n", gnu_current_inf->pid);
02234       gdb_flush (gdb_stdout);
02235     }
02236 
02237   pid = gnu_current_inf->pid;
02238 
02239   inf_detach (gnu_current_inf);
02240 
02241   inferior_ptid = null_ptid;
02242   detach_inferior (pid);
02243 
02244   unpush_target (ops);  /* Pop out of handling an inferior.  */
02245 }
02246 
02247 static void
02248 gnu_terminal_init_inferior (void)
02249 {
02250   gdb_assert (gnu_current_inf);
02251   terminal_init_inferior_with_pgrp (gnu_current_inf->pid);
02252 }
02253 
02254 static void
02255 gnu_stop (ptid_t ptid)
02256 {
02257   error (_("to_stop target function not implemented"));
02258 }
02259 
02260 static int
02261 gnu_thread_alive (struct target_ops *ops, ptid_t ptid)
02262 {
02263   inf_update_procs (gnu_current_inf);
02264   return !!inf_tid_to_thread (gnu_current_inf,
02265                               ptid_get_lwp (ptid));
02266 }
02267 
02268 
02269 /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
02270    gdb's address space.  Return 0 on failure; number of bytes read
02271    otherwise.  */
02272 static int
02273 gnu_read_inferior (task_t task, CORE_ADDR addr, gdb_byte *myaddr, int length)
02274 {
02275   error_t err;
02276   vm_address_t low_address = (vm_address_t) trunc_page (addr);
02277   vm_size_t aligned_length =
02278   (vm_size_t) round_page (addr + length) - low_address;
02279   pointer_t copied;
02280   int copy_count;
02281 
02282   /* Get memory from inferior with page aligned addresses.  */
02283   err = vm_read (task, low_address, aligned_length, &copied, &copy_count);
02284   if (err)
02285     return 0;
02286 
02287   err = hurd_safe_copyin (myaddr, (void *) (addr - low_address + copied),
02288                           length);
02289   if (err)
02290     {
02291       warning (_("Read from inferior faulted: %s"), safe_strerror (err));
02292       length = 0;
02293     }
02294 
02295   err = vm_deallocate (mach_task_self (), copied, copy_count);
02296   if (err)
02297     warning (_("gnu_read_inferior vm_deallocate failed: %s"),
02298              safe_strerror (err));
02299 
02300   return length;
02301 }
02302 
02303 #define CHK_GOTO_OUT(str,ret) \
02304   do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
02305 
02306 struct vm_region_list
02307 {
02308   struct vm_region_list *next;
02309   vm_prot_t protection;
02310   vm_address_t start;
02311   vm_size_t length;
02312 };
02313 
02314 struct obstack region_obstack;
02315 
02316 /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
02317    task's address space.  */
02318 static int
02319 gnu_write_inferior (task_t task, CORE_ADDR addr,
02320                     const gdb_byte *myaddr, int length)
02321 {
02322   error_t err = 0;
02323   vm_address_t low_address = (vm_address_t) trunc_page (addr);
02324   vm_size_t aligned_length =
02325   (vm_size_t) round_page (addr + length) - low_address;
02326   pointer_t copied;
02327   int copy_count;
02328   int deallocate = 0;
02329 
02330   char *errstr = "Bug in gnu_write_inferior";
02331 
02332   struct vm_region_list *region_element;
02333   struct vm_region_list *region_head = (struct vm_region_list *) NULL;
02334 
02335   /* Get memory from inferior with page aligned addresses.  */
02336   err = vm_read (task,
02337                  low_address,
02338                  aligned_length,
02339                  &copied,
02340                  &copy_count);
02341   CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
02342 
02343   deallocate++;
02344 
02345   err = hurd_safe_copyout ((void *) (addr - low_address + copied),
02346                            myaddr, length);
02347   CHK_GOTO_OUT ("Write to inferior faulted", err);
02348 
02349   obstack_init (&region_obstack);
02350 
02351   /* Do writes atomically.
02352      First check for holes and unwritable memory.  */
02353   {
02354     vm_size_t remaining_length = aligned_length;
02355     vm_address_t region_address = low_address;
02356 
02357     struct vm_region_list *scan;
02358 
02359     while (region_address < low_address + aligned_length)
02360       {
02361         vm_prot_t protection;
02362         vm_prot_t max_protection;
02363         vm_inherit_t inheritance;
02364         boolean_t shared;
02365         mach_port_t object_name;
02366         vm_offset_t offset;
02367         vm_size_t region_length = remaining_length;
02368         vm_address_t old_address = region_address;
02369 
02370         err = vm_region (task,
02371                          &region_address,
02372                          &region_length,
02373                          &protection,
02374                          &max_protection,
02375                          &inheritance,
02376                          &shared,
02377                          &object_name,
02378                          &offset);
02379         CHK_GOTO_OUT ("vm_region failed", err);
02380 
02381         /* Check for holes in memory.  */
02382         if (old_address != region_address)
02383           {
02384             warning (_("No memory at 0x%x. Nothing written"),
02385                      old_address);
02386             err = KERN_SUCCESS;
02387             length = 0;
02388             goto out;
02389           }
02390 
02391         if (!(max_protection & VM_PROT_WRITE))
02392           {
02393             warning (_("Memory at address 0x%x is unwritable. "
02394                        "Nothing written"),
02395                      old_address);
02396             err = KERN_SUCCESS;
02397             length = 0;
02398             goto out;
02399           }
02400 
02401         /* Chain the regions for later use.  */
02402         region_element =
02403           (struct vm_region_list *)
02404           obstack_alloc (&region_obstack, sizeof (struct vm_region_list));
02405 
02406         region_element->protection = protection;
02407         region_element->start = region_address;
02408         region_element->length = region_length;
02409 
02410         /* Chain the regions along with protections.  */
02411         region_element->next = region_head;
02412         region_head = region_element;
02413 
02414         region_address += region_length;
02415         remaining_length = remaining_length - region_length;
02416       }
02417 
02418     /* If things fail after this, we give up.
02419        Somebody is messing up inferior_task's mappings.  */
02420 
02421     /* Enable writes to the chained vm regions.  */
02422     for (scan = region_head; scan; scan = scan->next)
02423       {
02424         if (!(scan->protection & VM_PROT_WRITE))
02425           {
02426             err = vm_protect (task,
02427                               scan->start,
02428                               scan->length,
02429                               FALSE,
02430                               scan->protection | VM_PROT_WRITE);
02431             CHK_GOTO_OUT ("vm_protect: enable write failed", err);
02432           }
02433       }
02434 
02435     err = vm_write (task,
02436                     low_address,
02437                     copied,
02438                     aligned_length);
02439     CHK_GOTO_OUT ("vm_write failed", err);
02440 
02441     /* Set up the original region protections, if they were changed.  */
02442     for (scan = region_head; scan; scan = scan->next)
02443       {
02444         if (!(scan->protection & VM_PROT_WRITE))
02445           {
02446             err = vm_protect (task,
02447                               scan->start,
02448                               scan->length,
02449                               FALSE,
02450                               scan->protection);
02451             CHK_GOTO_OUT ("vm_protect: enable write failed", err);
02452           }
02453       }
02454   }
02455 
02456 out:
02457   if (deallocate)
02458     {
02459       obstack_free (&region_obstack, 0);
02460 
02461       (void) vm_deallocate (mach_task_self (),
02462                             copied,
02463                             copy_count);
02464     }
02465 
02466   if (err != KERN_SUCCESS)
02467     {
02468       warning (_("%s: %s"), errstr, mach_error_string (err));
02469       return 0;
02470     }
02471 
02472   return length;
02473 }
02474 
02475 
02476 
02477 /* Helper for gnu_xfer_partial that handles memory transfers.  */
02478 
02479 static LONGEST
02480 gnu_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
02481                  CORE_ADDR memaddr, LONGEST len)
02482 {
02483   task_t task = (gnu_current_inf
02484                  ? (gnu_current_inf->task
02485                     ? gnu_current_inf->task->port : 0)
02486                  : 0);
02487   int res;
02488 
02489   if (task == MACH_PORT_NULL)
02490     return TARGET_XFER_E_IO;
02491 
02492   if (writebuf != NULL)
02493     {
02494       inf_debug (gnu_current_inf, "writing %s[%s] <-- %s",
02495                  paddress (target_gdbarch (), memaddr), plongest (len),
02496                  host_address_to_string (writebuf));
02497       res = gnu_write_inferior (task, memaddr, writebuf, len);
02498     }
02499   else
02500     {
02501       inf_debug (gnu_current_inf, "reading %s[%s] --> %s",
02502                  paddress (target_gdbarch (), memaddr), plongest (len),
02503                  host_address_to_string (readbuf));
02504       res = gnu_read_inferior (task, memaddr, readbuf, len);
02505     }
02506   if (res == 0)
02507     return TARGET_XFER_E_IO;
02508   return res;
02509 }
02510 
02511 /* Target to_xfer_partial implementation.  */
02512 
02513 static LONGEST
02514 gnu_xfer_partial (struct target_ops *ops, enum target_object object,
02515                   const char *annex, gdb_byte *readbuf,
02516                   const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
02517 {
02518   switch (object)
02519     {
02520     case TARGET_OBJECT_MEMORY:
02521       return gnu_xfer_memory (readbuf, writebuf, offset, len);
02522 
02523     default:
02524       return -1;
02525     }
02526 }
02527 
02528 /* Call FUNC on each memory region in the task.  */
02529 static int
02530 gnu_find_memory_regions (find_memory_region_ftype func, void *data)
02531 {
02532   error_t err;
02533   task_t task;
02534   vm_address_t region_address, last_region_address, last_region_end;
02535   vm_prot_t last_protection;
02536 
02537   if (gnu_current_inf == 0 || gnu_current_inf->task == 0)
02538     return 0;
02539   task = gnu_current_inf->task->port;
02540   if (task == MACH_PORT_NULL)
02541     return 0;
02542 
02543   region_address = last_region_address = last_region_end = VM_MIN_ADDRESS;
02544   last_protection = VM_PROT_NONE;
02545   while (region_address < VM_MAX_ADDRESS)
02546     {
02547       vm_prot_t protection;
02548       vm_prot_t max_protection;
02549       vm_inherit_t inheritance;
02550       boolean_t shared;
02551       mach_port_t object_name;
02552       vm_offset_t offset;
02553       vm_size_t region_length = VM_MAX_ADDRESS - region_address;
02554       vm_address_t old_address = region_address;
02555 
02556       err = vm_region (task,
02557                        &region_address,
02558                        &region_length,
02559                        &protection,
02560                        &max_protection,
02561                        &inheritance,
02562                        &shared,
02563                        &object_name,
02564                        &offset);
02565       if (err == KERN_NO_SPACE)
02566         break;
02567       if (err != KERN_SUCCESS)
02568         {
02569           warning (_("vm_region failed: %s"), mach_error_string (err));
02570           return -1;
02571         }
02572 
02573       if (protection == last_protection && region_address == last_region_end)
02574         /* This region is contiguous with and indistinguishable from
02575            the previous one, so we just extend that one.  */
02576         last_region_end = region_address += region_length;
02577       else
02578         {
02579           /* This region is distinct from the last one we saw, so report
02580              that previous one.  */
02581           if (last_protection != VM_PROT_NONE)
02582             (*func) (last_region_address,
02583                      last_region_end - last_region_address,
02584                      last_protection & VM_PROT_READ,
02585                      last_protection & VM_PROT_WRITE,
02586                      last_protection & VM_PROT_EXECUTE,
02587                      1, /* MODIFIED is unknown, pass it as true.  */
02588                      data);
02589           last_region_address = region_address;
02590           last_region_end = region_address += region_length;
02591           last_protection = protection;
02592         }
02593     }
02594 
02595   /* Report the final region.  */
02596   if (last_region_end > last_region_address && last_protection != VM_PROT_NONE)
02597     (*func) (last_region_address, last_region_end - last_region_address,
02598              last_protection & VM_PROT_READ,
02599              last_protection & VM_PROT_WRITE,
02600              last_protection & VM_PROT_EXECUTE,
02601              1, /* MODIFIED is unknown, pass it as true.  */
02602              data);
02603 
02604   return 0;
02605 }
02606 
02607 
02608 /* Return printable description of proc.  */
02609 char *
02610 proc_string (struct proc *proc)
02611 {
02612   static char tid_str[80];
02613 
02614   if (proc_is_task (proc))
02615     xsnprintf (tid_str, sizeof (tid_str), "process %d", proc->inf->pid);
02616   else
02617     xsnprintf (tid_str, sizeof (tid_str), "Thread %d.%d",
02618                proc->inf->pid, proc->tid);
02619   return tid_str;
02620 }
02621 
02622 static char *
02623 gnu_pid_to_str (struct target_ops *ops, ptid_t ptid)
02624 {
02625   struct inf *inf = gnu_current_inf;
02626   int tid = ptid_get_lwp (ptid);
02627   struct proc *thread = inf_tid_to_thread (inf, tid);
02628 
02629   if (thread)
02630     return proc_string (thread);
02631   else
02632     {
02633       static char tid_str[80];
02634 
02635       xsnprintf (tid_str, sizeof (tid_str), "bogus thread id %d", tid);
02636       return tid_str;
02637     }
02638 }
02639 
02640 
02641 /* Create a prototype generic GNU/Hurd target.  The client can
02642    override it with local methods.  */
02643 
02644 struct target_ops *
02645 gnu_target (void)
02646 {
02647   struct target_ops *t = inf_child_target ();
02648 
02649   t->to_shortname = "GNU";
02650   t->to_longname = "GNU Hurd process";
02651   t->to_doc = "GNU Hurd process";
02652 
02653   t->to_attach = gnu_attach;
02654   t->to_attach_no_wait = 1;
02655   t->to_detach = gnu_detach;
02656   t->to_resume = gnu_resume;
02657   t->to_wait = gnu_wait;
02658   t->to_xfer_partial = gnu_xfer_partial;
02659   t->to_find_memory_regions = gnu_find_memory_regions;
02660   t->to_terminal_init = gnu_terminal_init_inferior;
02661   t->to_kill = gnu_kill_inferior;
02662   t->to_create_inferior = gnu_create_inferior;
02663   t->to_mourn_inferior = gnu_mourn_inferior;
02664   t->to_thread_alive = gnu_thread_alive;
02665   t->to_pid_to_str = gnu_pid_to_str;
02666   t->to_stop = gnu_stop;
02667 
02668   return t;
02669 }
02670 
02671 
02672 /* User task commands.  */
02673 
02674 static struct cmd_list_element *set_task_cmd_list = 0;
02675 static struct cmd_list_element *show_task_cmd_list = 0;
02676 /* User thread commands.  */
02677 
02678 /* Commands with a prefix of `set/show thread'.  */
02679 extern struct cmd_list_element *thread_cmd_list;
02680 struct cmd_list_element *set_thread_cmd_list = NULL;
02681 struct cmd_list_element *show_thread_cmd_list = NULL;
02682 
02683 /* Commands with a prefix of `set/show thread default'.  */
02684 struct cmd_list_element *set_thread_default_cmd_list = NULL;
02685 struct cmd_list_element *show_thread_default_cmd_list = NULL;
02686 
02687 static void
02688 set_thread_cmd (char *args, int from_tty)
02689 {
02690   printf_unfiltered ("\"set thread\" must be followed by the "
02691                      "name of a thread property, or \"default\".\n");
02692 }
02693 
02694 static void
02695 show_thread_cmd (char *args, int from_tty)
02696 {
02697   printf_unfiltered ("\"show thread\" must be followed by the "
02698                      "name of a thread property, or \"default\".\n");
02699 }
02700 
02701 static void
02702 set_thread_default_cmd (char *args, int from_tty)
02703 {
02704   printf_unfiltered ("\"set thread default\" must be followed "
02705                      "by the name of a thread property.\n");
02706 }
02707 
02708 static void
02709 show_thread_default_cmd (char *args, int from_tty)
02710 {
02711   printf_unfiltered ("\"show thread default\" must be followed "
02712                      "by the name of a thread property.\n");
02713 }
02714 
02715 static int
02716 parse_int_arg (char *args, char *cmd_prefix)
02717 {
02718   if (args)
02719     {
02720       char *arg_end;
02721       int val = strtoul (args, &arg_end, 10);
02722 
02723       if (*args && *arg_end == '\0')
02724         return val;
02725     }
02726   error (_("Illegal argument for \"%s\" command, should be an integer."),
02727          cmd_prefix);
02728 }
02729 
02730 static int
02731 _parse_bool_arg (char *args, char *t_val, char *f_val, char *cmd_prefix)
02732 {
02733   if (!args || strcmp (args, t_val) == 0)
02734     return 1;
02735   else if (strcmp (args, f_val) == 0)
02736     return 0;
02737   else
02738     error (_("Illegal argument for \"%s\" command, "
02739              "should be \"%s\" or \"%s\"."),
02740            cmd_prefix, t_val, f_val);
02741 }
02742 
02743 #define parse_bool_arg(args, cmd_prefix) \
02744   _parse_bool_arg (args, "on", "off", cmd_prefix)
02745 
02746 static void
02747 check_empty (char *args, char *cmd_prefix)
02748 {
02749   if (args)
02750     error (_("Garbage after \"%s\" command: `%s'"), cmd_prefix, args);
02751 }
02752 
02753 /* Returns the alive thread named by INFERIOR_PID, or signals an error.  */
02754 static struct proc *
02755 cur_thread (void)
02756 {
02757   struct inf *inf = cur_inf ();
02758   struct proc *thread = inf_tid_to_thread (inf,
02759                                            ptid_get_lwp (inferior_ptid));
02760   if (!thread)
02761     error (_("No current thread."));
02762   return thread;
02763 }
02764 
02765 /* Returns the current inferior, but signals an error if it has no task.  */
02766 static struct inf *
02767 active_inf (void)
02768 {
02769   struct inf *inf = cur_inf ();
02770 
02771   if (!inf->task)
02772     error (_("No current process."));
02773   return inf;
02774 }
02775 
02776 
02777 static void
02778 set_task_pause_cmd (char *args, int from_tty)
02779 {
02780   struct inf *inf = cur_inf ();
02781   int old_sc = inf->pause_sc;
02782 
02783   inf->pause_sc = parse_bool_arg (args, "set task pause");
02784 
02785   if (old_sc == 0 && inf->pause_sc != 0)
02786     /* If the task is currently unsuspended, immediately suspend it,
02787        otherwise wait until the next time it gets control.  */
02788     inf_suspend (inf);
02789 }
02790 
02791 static void
02792 show_task_pause_cmd (char *args, int from_tty)
02793 {
02794   struct inf *inf = cur_inf ();
02795 
02796   check_empty (args, "show task pause");
02797   printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
02798                      inf->task
02799                      ? (inf->pause_sc == 0 ? "isn't" : "is")
02800                      : (inf->pause_sc == 0 ? "won't be" : "will be"));
02801 }
02802 
02803 static void
02804 set_task_detach_sc_cmd (char *args, int from_tty)
02805 {
02806   cur_inf ()->detach_sc = parse_int_arg (args,
02807                                          "set task detach-suspend-count");
02808 }
02809 
02810 static void
02811 show_task_detach_sc_cmd (char *args, int from_tty)
02812 {
02813   check_empty (args, "show task detach-suspend-count");
02814   printf_unfiltered ("The inferior task will be left with a "
02815                      "suspend count of %d when detaching.\n",
02816                      cur_inf ()->detach_sc);
02817 }
02818 
02819 
02820 static void
02821 set_thread_default_pause_cmd (char *args, int from_tty)
02822 {
02823   struct inf *inf = cur_inf ();
02824 
02825   inf->default_thread_pause_sc =
02826     parse_bool_arg (args, "set thread default pause") ? 0 : 1;
02827 }
02828 
02829 static void
02830 show_thread_default_pause_cmd (char *args, int from_tty)
02831 {
02832   struct inf *inf = cur_inf ();
02833   int sc = inf->default_thread_pause_sc;
02834 
02835   check_empty (args, "show thread default pause");
02836   printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
02837                      sc ? "are" : "aren't",
02838                      !sc && inf->pause_sc ? " (but the task is)" : "");
02839 }
02840 
02841 static void
02842 set_thread_default_run_cmd (char *args, int from_tty)
02843 {
02844   struct inf *inf = cur_inf ();
02845 
02846   inf->default_thread_run_sc =
02847     parse_bool_arg (args, "set thread default run") ? 0 : 1;
02848 }
02849 
02850 static void
02851 show_thread_default_run_cmd (char *args, int from_tty)
02852 {
02853   struct inf *inf = cur_inf ();
02854 
02855   check_empty (args, "show thread default run");
02856   printf_unfiltered ("New threads %s allowed to run.\n",
02857                      inf->default_thread_run_sc == 0 ? "are" : "aren't");
02858 }
02859 
02860 static void
02861 set_thread_default_detach_sc_cmd (char *args, int from_tty)
02862 {
02863   cur_inf ()->default_thread_detach_sc =
02864     parse_int_arg (args, "set thread default detach-suspend-count");
02865 }
02866 
02867 static void
02868 show_thread_default_detach_sc_cmd (char *args, int from_tty)
02869 {
02870   check_empty (args, "show thread default detach-suspend-count");
02871   printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
02872                      cur_inf ()->default_thread_detach_sc);
02873 }
02874 
02875 
02876 /* Steal a send right called NAME in the inferior task, and make it PROC's
02877    saved exception port.  */
02878 static void
02879 steal_exc_port (struct proc *proc, mach_port_t name)
02880 {
02881   error_t err;
02882   mach_port_t port;
02883   mach_msg_type_name_t port_type;
02884 
02885   if (!proc || !proc->inf->task)
02886     error (_("No inferior task."));
02887 
02888   err = mach_port_extract_right (proc->inf->task->port,
02889                                  name, MACH_MSG_TYPE_COPY_SEND,
02890                                  &port, &port_type);
02891   if (err)
02892     error (_("Couldn't extract send right %d from inferior: %s"),
02893            name, safe_strerror (err));
02894 
02895   if (proc->saved_exc_port)
02896     /* Get rid of our reference to the old one.  */
02897     mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
02898 
02899   proc->saved_exc_port = port;
02900 
02901   if (!proc->exc_port)
02902     /* If PROC is a thread, we may not have set its exception port
02903        before.  We can't use proc_steal_exc_port because it also sets
02904        saved_exc_port.  */
02905     {
02906       proc->exc_port = proc->inf->event_port;
02907       err = proc_set_exception_port (proc, proc->exc_port);
02908       error (_("Can't set exception port for %s: %s"),
02909              proc_string (proc), safe_strerror (err));
02910     }
02911 }
02912 
02913 static void
02914 set_task_exc_port_cmd (char *args, int from_tty)
02915 {
02916   struct inf *inf = cur_inf ();
02917 
02918   if (!args)
02919     error (_("No argument to \"set task exception-port\" command."));
02920   steal_exc_port (inf->task, parse_and_eval_address (args));
02921 }
02922 
02923 static void
02924 set_stopped_cmd (char *args, int from_tty)
02925 {
02926   cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
02927 }
02928 
02929 static void
02930 show_stopped_cmd (char *args, int from_tty)
02931 {
02932   struct inf *inf = active_inf ();
02933 
02934   check_empty (args, "show stopped");
02935   printf_unfiltered ("The inferior process %s stopped.\n",
02936                      inf->stopped ? "is" : "isn't");
02937 }
02938 
02939 static void
02940 set_sig_thread_cmd (char *args, int from_tty)
02941 {
02942   struct inf *inf = cur_inf ();
02943 
02944   if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
02945     error (_("Illegal argument to \"set signal-thread\" command.\n"
02946            "Should be an integer thread ID, or `none'."));
02947 
02948   if (strcmp (args, "none") == 0)
02949     inf->signal_thread = 0;
02950   else
02951     {
02952       ptid_t ptid = thread_id_to_pid (atoi (args));
02953 
02954       if (ptid_equal (ptid, minus_one_ptid))
02955         error (_("Thread ID %s not known.  "
02956                  "Use the \"info threads\" command to\n"
02957                "see the IDs of currently known threads."), args);
02958       inf->signal_thread = inf_tid_to_thread (inf, ptid_get_lwp (ptid));
02959     }
02960 }
02961 
02962 static void
02963 show_sig_thread_cmd (char *args, int from_tty)
02964 {
02965   struct inf *inf = active_inf ();
02966 
02967   check_empty (args, "show signal-thread");
02968   if (inf->signal_thread)
02969     printf_unfiltered ("The signal thread is %s.\n",
02970                        proc_string (inf->signal_thread));
02971   else
02972     printf_unfiltered ("There is no signal thread.\n");
02973 }
02974 
02975 
02976 static void
02977 set_signals_cmd (char *args, int from_tty)
02978 {
02979   struct inf *inf = cur_inf ();
02980 
02981   inf->want_signals = parse_bool_arg (args, "set signals");
02982 
02983   if (inf->task && inf->want_signals != inf->traced)
02984     /* Make this take effect immediately in a running process.  */
02985     inf_set_traced (inf, inf->want_signals);
02986 }
02987 
02988 static void
02989 show_signals_cmd (char *args, int from_tty)
02990 {
02991   struct inf *inf = cur_inf ();
02992 
02993   check_empty (args, "show signals");
02994   printf_unfiltered ("The inferior process's signals %s intercepted.\n",
02995                      inf->task
02996                      ? (inf->traced ? "are" : "aren't")
02997                      : (inf->want_signals ? "will be" : "won't be"));
02998 }
02999 
03000 static void
03001 set_exceptions_cmd (char *args, int from_tty)
03002 {
03003   struct inf *inf = cur_inf ();
03004   int val = parse_bool_arg (args, "set exceptions");
03005 
03006   if (inf->task && inf->want_exceptions != val)
03007     /* Make this take effect immediately in a running process.  */
03008     /* XXX */ ;
03009 
03010   inf->want_exceptions = val;
03011 }
03012 
03013 static void
03014 show_exceptions_cmd (char *args, int from_tty)
03015 {
03016   struct inf *inf = cur_inf ();
03017 
03018   check_empty (args, "show exceptions");
03019   printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
03020                      inf->task
03021                      ? (inf->want_exceptions ? "are" : "aren't")
03022                      : (inf->want_exceptions ? "will be" : "won't be"));
03023 }
03024 
03025 
03026 static void
03027 set_task_cmd (char *args, int from_tty)
03028 {
03029   printf_unfiltered ("\"set task\" must be followed by the name"
03030                      " of a task property.\n");
03031 }
03032 
03033 static void
03034 show_task_cmd (char *args, int from_tty)
03035 {
03036   struct inf *inf = cur_inf ();
03037 
03038   check_empty (args, "show task");
03039 
03040   show_signals_cmd (0, from_tty);
03041   show_exceptions_cmd (0, from_tty);
03042   show_task_pause_cmd (0, from_tty);
03043 
03044   if (inf->pause_sc == 0)
03045     show_thread_default_pause_cmd (0, from_tty);
03046   show_thread_default_run_cmd (0, from_tty);
03047 
03048   if (inf->task)
03049     {
03050       show_stopped_cmd (0, from_tty);
03051       show_sig_thread_cmd (0, from_tty);
03052     }
03053 
03054   if (inf->detach_sc != 0)
03055     show_task_detach_sc_cmd (0, from_tty);
03056   if (inf->default_thread_detach_sc != 0)
03057     show_thread_default_detach_sc_cmd (0, from_tty);
03058 }
03059 
03060 
03061 static void
03062 set_noninvasive_cmd (char *args, int from_tty)
03063 {
03064   /* Invert the sense of the arg for each component.  */
03065   char *inv_args = parse_bool_arg (args, "set noninvasive") ? "off" : "on";
03066 
03067   set_task_pause_cmd (inv_args, from_tty);
03068   set_signals_cmd (inv_args, from_tty);
03069   set_exceptions_cmd (inv_args, from_tty);
03070 }
03071 
03072 
03073 static void
03074 info_port_rights (char *args, mach_port_type_t only)
03075 {
03076   struct inf *inf = active_inf ();
03077   struct value *vmark = value_mark ();
03078 
03079   if (args)
03080     /* Explicit list of port rights.  */
03081     {
03082       while (*args)
03083         {
03084           struct value *val = parse_to_comma_and_eval (&args);
03085           long right = value_as_long (val);
03086           error_t err =
03087             print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
03088                              stdout);
03089 
03090           if (err)
03091             error (_("%ld: %s."), right, safe_strerror (err));
03092         }
03093     }
03094   else
03095     /* Print all of them.  */
03096     {
03097       error_t err =
03098         print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
03099                                stdout);
03100       if (err)
03101         error (_("%s."), safe_strerror (err));
03102     }
03103 
03104   value_free_to_mark (vmark);
03105 }
03106 
03107 static void
03108 info_send_rights_cmd (char *args, int from_tty)
03109 {
03110   info_port_rights (args, MACH_PORT_TYPE_SEND);
03111 }
03112 
03113 static void
03114 info_recv_rights_cmd (char *args, int from_tty)
03115 {
03116   info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
03117 }
03118 
03119 static void
03120 info_port_sets_cmd (char *args, int from_tty)
03121 {
03122   info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
03123 }
03124 
03125 static void
03126 info_dead_names_cmd (char *args, int from_tty)
03127 {
03128   info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
03129 }
03130 
03131 static void
03132 info_port_rights_cmd (char *args, int from_tty)
03133 {
03134   info_port_rights (args, ~0);
03135 }
03136 
03137 
03138 static void
03139 add_task_commands (void)
03140 {
03141   add_cmd ("pause", class_run, set_thread_default_pause_cmd, _("\
03142 Set whether the new threads are suspended while gdb has control.\n\
03143 This property normally has no effect because the whole task is\n\
03144 suspended, however, that may be disabled with \"set task pause off\".\n\
03145 The default value is \"off\"."),
03146            &set_thread_default_cmd_list);
03147   add_cmd ("pause", no_class, show_thread_default_pause_cmd, _("\
03148 Show whether new threads are suspended while gdb has control."),
03149            &show_thread_default_cmd_list);
03150   
03151   add_cmd ("run", class_run, set_thread_default_run_cmd, _("\
03152 Set whether new threads are allowed to run (once gdb has noticed them)."),
03153            &set_thread_default_cmd_list);
03154   add_cmd ("run", no_class, show_thread_default_run_cmd, _("\
03155 Show whether new threads are allowed to run (once gdb has noticed them)."),
03156            &show_thread_default_cmd_list);
03157   
03158   add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
03159            _("Set the default detach-suspend-count value for new threads."),
03160            &set_thread_default_cmd_list);
03161   add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
03162            _("Show the default detach-suspend-count value for new threads."),
03163            &show_thread_default_cmd_list);
03164 
03165   add_cmd ("signals", class_run, set_signals_cmd, _("\
03166 Set whether the inferior process's signals will be intercepted.\n\
03167 Mach exceptions (such as breakpoint traps) are not affected."),
03168            &setlist);
03169   add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
03170   add_cmd ("signals", no_class, show_signals_cmd, _("\
03171 Show whether the inferior process's signals will be intercepted."),
03172            &showlist);
03173   add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
03174 
03175   add_cmd ("signal-thread", class_run, set_sig_thread_cmd, _("\
03176 Set the thread that gdb thinks is the libc signal thread.\n\
03177 This thread is run when delivering a signal to a non-stopped process."),
03178            &setlist);
03179   add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
03180   add_cmd ("signal-thread", no_class, show_sig_thread_cmd, _("\
03181 Set the thread that gdb thinks is the libc signal thread."),
03182            &showlist);
03183   add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
03184 
03185   add_cmd ("stopped", class_run, set_stopped_cmd, _("\
03186 Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n\
03187 Stopped process will be continued by sending them a signal."),
03188            &setlist);
03189   add_cmd ("stopped", no_class, show_stopped_cmd, _("\
03190 Show whether gdb thinks the inferior process is stopped as with SIGSTOP."),
03191            &showlist);
03192 
03193   add_cmd ("exceptions", class_run, set_exceptions_cmd, _("\
03194 Set whether exceptions in the inferior process will be trapped.\n\
03195 When exceptions are turned off, neither breakpoints nor single-stepping\n\
03196 will work."),
03197            &setlist);
03198   /* Allow `set exc' despite conflict with `set exception-port'.  */
03199   add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
03200   add_cmd ("exceptions", no_class, show_exceptions_cmd, _("\
03201 Show whether exceptions in the inferior process will be trapped."),
03202            &showlist);
03203 
03204   add_prefix_cmd ("task", no_class, set_task_cmd,
03205                   _("Command prefix for setting task attributes."),
03206                   &set_task_cmd_list, "set task ", 0, &setlist);
03207   add_prefix_cmd ("task", no_class, show_task_cmd,
03208                   _("Command prefix for showing task attributes."),
03209                   &show_task_cmd_list, "show task ", 0, &showlist);
03210 
03211   add_cmd ("pause", class_run, set_task_pause_cmd, _("\
03212 Set whether the task is suspended while gdb has control.\n\
03213 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
03214 until the next time the program is continued.\n\
03215 When setting this to \"off\", \"set thread default pause on\" can be\n\
03216 used to pause individual threads by default instead."),
03217            &set_task_cmd_list);
03218   add_cmd ("pause", no_class, show_task_pause_cmd,
03219            _("Show whether the task is suspended while gdb has control."),
03220            &show_task_cmd_list);
03221 
03222   add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
03223            _("Set the suspend count will leave on the thread when detaching."),
03224            &set_task_cmd_list);
03225   add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
03226            _("Show the suspend count will leave "
03227              "on the thread when detaching."),
03228            &show_task_cmd_list);
03229 
03230   add_cmd ("exception-port", no_class, set_task_exc_port_cmd, _("\
03231 Set the task exception port to which we forward exceptions.\n\
03232 The argument should be the value of the send right in the task."),
03233            &set_task_cmd_list);
03234   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
03235   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
03236                  &set_task_cmd_list);
03237 
03238   /* A convenient way of turning on all options require to noninvasively
03239      debug running tasks.  */
03240   add_cmd ("noninvasive", no_class, set_noninvasive_cmd, _("\
03241 Set task options so that we interfere as little as possible.\n\
03242 This is the same as setting `task pause', `exceptions', and\n\
03243 `signals' to the opposite value."),
03244            &setlist);
03245 
03246   /* Commands to show information about the task's ports.  */
03247   add_cmd ("send-rights", class_info, info_send_rights_cmd,
03248            _("Show information about the task's send rights"),
03249            &infolist);
03250   add_cmd ("receive-rights", class_info, info_recv_rights_cmd,
03251            _("Show information about the task's receive rights"),
03252            &infolist);
03253   add_cmd ("port-rights", class_info, info_port_rights_cmd,
03254            _("Show information about the task's port rights"),
03255            &infolist);
03256   add_cmd ("port-sets", class_info, info_port_sets_cmd,
03257            _("Show information about the task's port sets"),
03258            &infolist);
03259   add_cmd ("dead-names", class_info, info_dead_names_cmd,
03260            _("Show information about the task's dead names"),
03261            &infolist);
03262   add_info_alias ("ports", "port-rights", 1);
03263   add_info_alias ("port", "port-rights", 1);
03264   add_info_alias ("psets", "port-sets", 1);
03265 }
03266 
03267 
03268 static void
03269 set_thread_pause_cmd (char *args, int from_tty)
03270 {
03271   struct proc *thread = cur_thread ();
03272   int old_sc = thread->pause_sc;
03273 
03274   thread->pause_sc = parse_bool_arg (args, "set thread pause");
03275   if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
03276     /* If the task is currently unsuspended, immediately suspend it,
03277        otherwise wait until the next time it gets control.  */
03278     inf_suspend (thread->inf);
03279 }
03280 
03281 static void
03282 show_thread_pause_cmd (char *args, int from_tty)
03283 {
03284   struct proc *thread = cur_thread ();
03285   int sc = thread->pause_sc;
03286 
03287   check_empty (args, "show task pause");
03288   printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
03289                      proc_string (thread),
03290                      sc ? "is" : "isn't",
03291                      !sc && thread->inf->pause_sc ? " (but the task is)" : "");
03292 }
03293 
03294 static void
03295 set_thread_run_cmd (char *args, int from_tty)
03296 {
03297   struct proc *thread = cur_thread ();
03298 
03299   thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
03300 }
03301 
03302 static void
03303 show_thread_run_cmd (char *args, int from_tty)
03304 {
03305   struct proc *thread = cur_thread ();
03306 
03307   check_empty (args, "show thread run");
03308   printf_unfiltered ("Thread %s %s allowed to run.",
03309                      proc_string (thread),
03310                      thread->run_sc == 0 ? "is" : "isn't");
03311 }
03312 
03313 static void
03314 set_thread_detach_sc_cmd (char *args, int from_tty)
03315 {
03316   cur_thread ()->detach_sc = parse_int_arg (args,
03317                                             "set thread detach-suspend-count");
03318 }
03319 
03320 static void
03321 show_thread_detach_sc_cmd (char *args, int from_tty)
03322 {
03323   struct proc *thread = cur_thread ();
03324 
03325   check_empty (args, "show thread detach-suspend-count");
03326   printf_unfiltered ("Thread %s will be left with a suspend count"
03327                      " of %d when detaching.\n",
03328                      proc_string (thread),
03329                      thread->detach_sc);
03330 }
03331 
03332 static void
03333 set_thread_exc_port_cmd (char *args, int from_tty)
03334 {
03335   struct proc *thread = cur_thread ();
03336 
03337   if (!args)
03338     error (_("No argument to \"set thread exception-port\" command."));
03339   steal_exc_port (thread, parse_and_eval_address (args));
03340 }
03341 
03342 #if 0
03343 static void
03344 show_thread_cmd (char *args, int from_tty)
03345 {
03346   struct proc *thread = cur_thread ();
03347 
03348   check_empty (args, "show thread");
03349   show_thread_run_cmd (0, from_tty);
03350   show_thread_pause_cmd (0, from_tty);
03351   if (thread->detach_sc != 0)
03352     show_thread_detach_sc_cmd (0, from_tty);
03353 }
03354 #endif
03355 
03356 static void
03357 thread_takeover_sc_cmd (char *args, int from_tty)
03358 {
03359   struct proc *thread = cur_thread ();
03360 
03361   thread_basic_info_data_t _info;
03362   thread_basic_info_t info = &_info;
03363   mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
03364   error_t err =
03365   thread_info (thread->port, THREAD_BASIC_INFO, (int *) &info, &info_len);
03366   if (err)
03367     error (("%s."), safe_strerror (err));
03368   thread->sc = info->suspend_count;
03369   if (from_tty)
03370     printf_unfiltered ("Suspend count was %d.\n", thread->sc);
03371   if (info != &_info)
03372     vm_deallocate (mach_task_self (), (vm_address_t) info,
03373                    info_len * sizeof (int));
03374 }
03375 
03376 
03377 static void
03378 add_thread_commands (void)
03379 {
03380   add_prefix_cmd ("thread", no_class, set_thread_cmd,
03381                   _("Command prefix for setting thread properties."),
03382                   &set_thread_cmd_list, "set thread ", 0, &setlist);
03383   add_prefix_cmd ("default", no_class, show_thread_cmd,
03384                   _("Command prefix for setting default thread properties."),
03385                   &set_thread_default_cmd_list, "set thread default ", 0,
03386                   &set_thread_cmd_list);
03387   add_prefix_cmd ("thread", no_class, set_thread_default_cmd,
03388                   _("Command prefix for showing thread properties."),
03389                   &show_thread_cmd_list, "show thread ", 0, &showlist);
03390   add_prefix_cmd ("default", no_class, show_thread_default_cmd,
03391                   _("Command prefix for showing default thread properties."),
03392                   &show_thread_default_cmd_list, "show thread default ", 0,
03393                   &show_thread_cmd_list);
03394 
03395   add_cmd ("pause", class_run, set_thread_pause_cmd, _("\
03396 Set whether the current thread is suspended while gdb has control.\n\
03397 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
03398 until the next time the program is continued.  This property normally\n\
03399 has no effect because the whole task is suspended, however, that may\n\
03400 be disabled with \"set task pause off\".\n\
03401 The default value is \"off\"."),
03402            &set_thread_cmd_list);
03403   add_cmd ("pause", no_class, show_thread_pause_cmd, _("\
03404 Show whether the current thread is suspended while gdb has control."),
03405            &show_thread_cmd_list);
03406 
03407   add_cmd ("run", class_run, set_thread_run_cmd,
03408            _("Set whether the current thread is allowed to run."),
03409            &set_thread_cmd_list);
03410   add_cmd ("run", no_class, show_thread_run_cmd,
03411            _("Show whether the current thread is allowed to run."),
03412            &show_thread_cmd_list);
03413 
03414   add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd, _("\
03415 Set the suspend count will leave on the thread when detaching.\n\
03416 Note that this is relative to suspend count when gdb noticed the thread;\n\
03417 use the `thread takeover-suspend-count' to force it to an absolute value."),
03418            &set_thread_cmd_list);
03419   add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd, _("\
03420 Show the suspend count will leave on the thread when detaching.\n\
03421 Note that this is relative to suspend count when gdb noticed the thread;\n\
03422 use the `thread takeover-suspend-count' to force it to an absolute value."),
03423            &show_thread_cmd_list);
03424 
03425   add_cmd ("exception-port", no_class, set_thread_exc_port_cmd, _("\
03426 Set the thread exception port to which we forward exceptions.\n\
03427 This overrides the task exception port.\n\
03428 The argument should be the value of the send right in the task."),
03429            &set_thread_cmd_list);
03430   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
03431   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
03432                  &set_thread_cmd_list);
03433 
03434   add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd, _("\
03435 Force the threads absolute suspend-count to be gdb's.\n\
03436 Prior to giving this command, gdb's thread suspend-counts are relative\n\
03437 to the thread's initial suspend-count when gdb notices the threads."),
03438            &thread_cmd_list);
03439 }
03440 
03441 
03442 void
03443 _initialize_gnu_nat (void)
03444 {
03445   proc_server = getproc ();
03446 
03447   add_task_commands ();
03448   add_thread_commands ();
03449   add_setshow_boolean_cmd ("gnu-nat", class_maintenance,
03450                            &gnu_debug_flag,
03451                            _("Set debugging output for the gnu backend."),
03452                            _("Show debugging output for the gnu backend."),
03453                            NULL,
03454                            NULL,
03455                            NULL,
03456                            &setdebuglist,
03457                            &showdebuglist);
03458 }
03459 
03460 #ifdef  FLUSH_INFERIOR_CACHE
03461 
03462 /* When over-writing code on some machines the I-Cache must be flushed
03463    explicitly, because it is not kept coherent by the lazy hardware.
03464    This definitely includes breakpoints, for instance, or else we
03465    end up looping in mysterious Bpt traps.  */
03466 
03467 void
03468 flush_inferior_icache (CORE_ADDR pc, int amount)
03469 {
03470   vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
03471   error_t ret;
03472 
03473   ret = vm_machine_attribute (gnu_current_inf->task->port,
03474                               pc,
03475                               amount,
03476                               MATTR_CACHE,
03477                               &flush);
03478   if (ret != KERN_SUCCESS)
03479     warning (_("Error flushing inferior's cache : %s"), safe_strerror (ret));
03480 }
03481 #endif /* FLUSH_INFERIOR_CACHE */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines