Home | History | Annotate | Line # | Download | only in gdb
gnu-nat.c revision 1.8
      1 /* Interface GDB to the GNU Hurd.
      2    Copyright (C) 1992-2019 Free Software Foundation, Inc.
      3 
      4    This file is part of GDB.
      5 
      6    Written by Miles Bader <miles (at) gnu.ai.mit.edu>
      7 
      8    Some code and ideas from m3-nat.c by Jukka Virtanen <jtv (at) hut.fi>
      9 
     10    This program is free software; you can redistribute it and/or modify
     11    it under the terms of the GNU General Public License as published by
     12    the Free Software Foundation; either version 3 of the License, or
     13    (at your option) any later version.
     14 
     15    This program is distributed in the hope that it will be useful,
     16    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18    GNU General Public License for more details.
     19 
     20    You should have received a copy of the GNU General Public License
     21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     22 
     23 /* Include this first, to pick up the <mach.h> 'thread_info' diversion.  */
     24 #include "gnu-nat.h"
     25 
     26 /* Mach/Hurd headers are not yet ready for C++ compilation.  */
     27 extern "C"
     28 {
     29 #include <mach.h>
     30 #include <mach_error.h>
     31 #include <mach/exception.h>
     32 #include <mach/message.h>
     33 #include <mach/notify.h>
     34 #include <mach/vm_attributes.h>
     35 
     36 #include <hurd.h>
     37 #include <hurd/interrupt.h>
     38 #include <hurd/msg.h>
     39 #include <hurd/msg_request.h>
     40 #include <hurd/process.h>
     41 /* Defined in <hurd/process.h>, but we need forward declarations from
     42    <hurd/process_request.h> as well.  */
     43 #undef _process_user_
     44 #include <hurd/process_request.h>
     45 #include <hurd/signal.h>
     46 #include <hurd/sigpreempt.h>
     47 
     48 #include <portinfo.h>
     49 }
     50 
     51 #include "defs.h"
     52 
     53 #include <ctype.h>
     54 #include <limits.h>
     55 #include <setjmp.h>
     56 #include <signal.h>
     57 #include <sys/ptrace.h>
     58 #include <elf.h>
     59 #include <link.h>
     60 
     61 #include "inferior.h"
     62 #include "symtab.h"
     63 #include "value.h"
     64 #include "language.h"
     65 #include "target.h"
     66 #include "common/gdb_wait.h"
     67 #include "gdbcmd.h"
     68 #include "gdbcore.h"
     69 #include "gdbthread.h"
     70 #include "gdb_obstack.h"
     71 #include "tid-parse.h"
     72 #include "nat/fork-inferior.h"
     73 
     74 #include "inf-child.h"
     75 
     76 /* MIG stubs are not yet ready for C++ compilation.  */
     77 extern "C"
     78 {
     79 #include "exc_request_S.h"
     80 #include "notify_S.h"
     81 #include "process_reply_S.h"
     82 #include "msg_reply_S.h"
     83 #include "exc_request_U.h"
     84 #include "msg_U.h"
     85 }
     86 
     87 static process_t proc_server = MACH_PORT_NULL;
     88 
     89 /* If we've sent a proc_wait_request to the proc server, the pid of the
     90    process we asked about.  We can only ever have one outstanding.  */
     91 int proc_wait_pid = 0;
     92 
     93 /* The number of wait requests we've sent, and expect replies from.  */
     94 int proc_waits_pending = 0;
     95 
     96 int gnu_debug_flag = 0;
     97 
     98 /* Forward decls */
     99 
    100 static struct inf *make_inf ();
    101 void inf_clear_wait (struct inf *inf);
    102 void inf_cleanup (struct inf *inf);
    103 void inf_startup (struct inf *inf, int pid);
    104 int inf_update_suspends (struct inf *inf);
    105 void inf_set_pid (struct inf *inf, pid_t pid);
    106 void inf_validate_procs (struct inf *inf);
    107 void inf_steal_exc_ports (struct inf *inf);
    108 void inf_restore_exc_ports (struct inf *inf);
    109 void inf_set_threads_resume_sc (struct inf *inf,
    110 				struct proc *run_thread,
    111 				int run_others);
    112 int inf_set_threads_resume_sc_for_signal_thread (struct inf *inf);
    113 void inf_suspend (struct inf *inf);
    114 void inf_resume (struct inf *inf);
    115 void inf_set_step_thread (struct inf *inf, struct proc *proc);
    116 void inf_detach (struct inf *inf);
    117 void inf_attach (struct inf *inf, int pid);
    118 void inf_signal (struct inf *inf, enum gdb_signal sig);
    119 void inf_continue (struct inf *inf);
    120 
    121 #define inf_debug(_inf, msg, args...) \
    122   do { struct inf *__inf = (_inf); \
    123        debug ("{inf %d %s}: " msg, __inf->pid, \
    124        host_address_to_string (__inf) , ##args); } while (0)
    125 
    126 void proc_abort (struct proc *proc, int force);
    127 struct proc *make_proc (struct inf *inf, mach_port_t port, int tid);
    128 struct proc *_proc_free (struct proc *proc);
    129 int proc_update_sc (struct proc *proc);
    130 kern_return_t proc_get_exception_port (struct proc *proc, mach_port_t * port);
    131 kern_return_t proc_set_exception_port (struct proc *proc, mach_port_t port);
    132 static mach_port_t _proc_get_exc_port (struct proc *proc);
    133 void proc_steal_exc_port (struct proc *proc, mach_port_t exc_port);
    134 void proc_restore_exc_port (struct proc *proc);
    135 int proc_trace (struct proc *proc, int set);
    136 
    137 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
    138    to INF's msg port and task port respectively.  If it has no msg port,
    139    EIEIO is returned.  INF must refer to a running process!  */
    140 #define INF_MSGPORT_RPC(inf, rpc_expr) \
    141   HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
    142 		    (refport = inf->task->port, 0), 0, \
    143 		    msgport ? (rpc_expr) : EIEIO)
    144 
    145 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
    146    there's someone around to deal with the RPC (and resuspend things
    147    afterwards).  This effects INF's threads' resume_sc count.  */
    148 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
    149   (inf_set_threads_resume_sc_for_signal_thread (inf) \
    150    ? ({ kern_return_t __e; \
    151 	inf_resume (inf); \
    152 	__e = INF_MSGPORT_RPC (inf, rpc_expr); \
    153 	inf_suspend (inf); \
    154 	__e; }) \
    155    : EIEIO)
    156 
    157 
    158 /* The state passed by an exception message.  */
    160 struct exc_state
    161   {
    162     int exception;		/* The exception code.  */
    163     int code, subcode;
    164     mach_port_t handler;	/* The real exception port to handle this.  */
    165     mach_port_t reply;		/* The reply port from the exception call.  */
    166   };
    167 
    168 /* The results of the last wait an inf did.  */
    169 struct inf_wait
    170   {
    171     struct target_waitstatus status;	/* The status returned to gdb.  */
    172     struct exc_state exc;	/* The exception that caused us to return.  */
    173     struct proc *thread;	/* The thread in question.  */
    174     int suppress;		/* Something trivial happened.  */
    175   };
    176 
    177 /* The state of an inferior.  */
    178 struct inf
    179   {
    180     /* Fields describing the current inferior.  */
    181 
    182     struct proc *task;		/* The mach task.   */
    183     struct proc *threads;	/* A linked list of all threads in TASK.  */
    184 
    185     /* True if THREADS needn't be validated by querying the task.  We
    186        assume that we and the task in question are the only ones
    187        frobbing the thread list, so as long as we don't let any code
    188        run, we don't have to worry about THREADS changing.  */
    189     int threads_up_to_date;
    190 
    191     pid_t pid;			/* The real system PID.  */
    192 
    193     struct inf_wait wait;	/* What to return from target_wait.  */
    194 
    195     /* One thread proc in INF may be in `single-stepping mode'.  This
    196        is it.  */
    197     struct proc *step_thread;
    198 
    199     /* The thread we think is the signal thread.  */
    200     struct proc *signal_thread;
    201 
    202     mach_port_t event_port;	/* Where we receive various msgs.  */
    203 
    204     /* True if we think at least one thread in the inferior could currently be
    205        running.  */
    206     unsigned int running:1;
    207 
    208     /* True if the process has stopped (in the proc server sense).  Note that
    209        since a proc server `stop' leaves the signal thread running, the inf can
    210        be RUNNING && STOPPED...  */
    211     unsigned int stopped:1;
    212 
    213     /* True if the inferior has no message port.  */
    214     unsigned int nomsg:1;
    215 
    216     /* True if the inferior is traced.  */
    217     unsigned int traced:1;
    218 
    219     /* True if we shouldn't try waiting for the inferior, usually because we
    220        can't for some reason.  */
    221     unsigned int no_wait:1;
    222 
    223     /* When starting a new inferior, we don't try to validate threads until all
    224        the proper execs have been done, which this flag states we still
    225        expect to happen.  */
    226     unsigned int pending_execs:1;
    227 
    228     /* Fields describing global state.  */
    229 
    230     /* The task suspend count used when gdb has control.  This is normally 1 to
    231        make things easier for us, but sometimes (like when attaching to vital
    232        system servers) it may be desirable to let the task continue to run
    233        (pausing individual threads as necessary).  */
    234     int pause_sc;
    235 
    236     /* The task suspend count left when detaching from a task.  */
    237     int detach_sc;
    238 
    239     /* The initial values used for the run_sc and pause_sc of newly discovered
    240        threads -- see the definition of those fields in struct proc.  */
    241     int default_thread_run_sc;
    242     int default_thread_pause_sc;
    243     int default_thread_detach_sc;
    244 
    245     /* True if the process should be traced when started/attached.  Newly
    246        started processes *must* be traced at first to exec them properly, but
    247        if this is false, tracing is turned off as soon it has done so.  */
    248     int want_signals;
    249 
    250     /* True if exceptions from the inferior process should be trapped.  This
    251        must be on to use breakpoints.  */
    252     int want_exceptions;
    253   };
    254 
    255 
    256 int
    257 __proc_pid (struct proc *proc)
    258 {
    259   return proc->inf->pid;
    260 }
    261 
    262 
    263 /* Update PROC's real suspend count to match it's desired one.  Returns true
    265    if we think PROC is now in a runnable state.  */
    266 int
    267 proc_update_sc (struct proc *proc)
    268 {
    269   int running;
    270   int err = 0;
    271   int delta = proc->sc - proc->cur_sc;
    272 
    273   if (delta)
    274     proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
    275 
    276   if (proc->sc == 0 && proc->state_changed)
    277     /* Since PROC may start running, we must write back any state changes.  */
    278     {
    279       gdb_assert (proc_is_thread (proc));
    280       proc_debug (proc, "storing back changed thread state");
    281       err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
    282 			 (thread_state_t) &proc->state, THREAD_STATE_SIZE);
    283       if (!err)
    284 	proc->state_changed = 0;
    285     }
    286 
    287   if (delta > 0)
    288     {
    289       while (delta-- > 0 && !err)
    290 	{
    291 	  if (proc_is_task (proc))
    292 	    err = task_suspend (proc->port);
    293 	  else
    294 	    err = thread_suspend (proc->port);
    295 	}
    296     }
    297   else
    298     {
    299       while (delta++ < 0 && !err)
    300 	{
    301 	  if (proc_is_task (proc))
    302 	    err = task_resume (proc->port);
    303 	  else
    304 	    err = thread_resume (proc->port);
    305 	}
    306     }
    307   if (!err)
    308     proc->cur_sc = proc->sc;
    309 
    310   /* If we got an error, then the task/thread has disappeared.  */
    311   running = !err && proc->sc == 0;
    312 
    313   proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
    314   if (err)
    315     proc_debug (proc, "err = %s", safe_strerror (err));
    316 
    317   if (running)
    318     {
    319       proc->aborted = 0;
    320       proc->state_valid = proc->state_changed = 0;
    321       proc->fetched_regs = 0;
    322     }
    323 
    324   return running;
    325 }
    326 
    327 
    328 /* Thread_abort is called on PROC if needed.  PROC must be a thread proc.
    330    If PROC is deemed `precious', then nothing is done unless FORCE is true.
    331    In particular, a thread is precious if it's running (in which case forcing
    332    it includes suspending it first), or if it has an exception pending.  */
    333 void
    334 proc_abort (struct proc *proc, int force)
    335 {
    336   gdb_assert (proc_is_thread (proc));
    337 
    338   if (!proc->aborted)
    339     {
    340       struct inf *inf = proc->inf;
    341       int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
    342 
    343       if (running && force)
    344 	{
    345 	  proc->sc = 1;
    346 	  inf_update_suspends (proc->inf);
    347 	  running = 0;
    348 	  warning (_("Stopped %s."), proc_string (proc));
    349 	}
    350       else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
    351 	/* An exception is pending on PROC, which don't mess with.  */
    352 	running = 1;
    353 
    354       if (!running)
    355 	/* We only abort the thread if it's not actually running.  */
    356 	{
    357 	  thread_abort (proc->port);
    358 	  proc_debug (proc, "aborted");
    359 	  proc->aborted = 1;
    360 	}
    361       else
    362 	proc_debug (proc, "not aborting");
    363     }
    364 }
    365 
    366 /* Make sure that the state field in PROC is up to date, and return a pointer
    367    to it, or 0 if something is wrong.  If WILL_MODIFY is true, makes sure
    368    that the thread is stopped and aborted first, and sets the state_changed
    369    field in PROC to true.  */
    370 thread_state_t
    371 proc_get_state (struct proc *proc, int will_modify)
    372 {
    373   int was_aborted = proc->aborted;
    374 
    375   proc_debug (proc, "updating state info%s",
    376 	      will_modify ? " (with intention to modify)" : "");
    377 
    378   proc_abort (proc, will_modify);
    379 
    380   if (!was_aborted && proc->aborted)
    381     /* PROC's state may have changed since we last fetched it.  */
    382     proc->state_valid = 0;
    383 
    384   if (!proc->state_valid)
    385     {
    386       mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
    387       kern_return_t err =
    388 	thread_get_state (proc->port, THREAD_STATE_FLAVOR,
    389 			  (thread_state_t) &proc->state, &state_size);
    390 
    391       proc_debug (proc, "getting thread state");
    392       proc->state_valid = !err;
    393     }
    394 
    395   if (proc->state_valid)
    396     {
    397       if (will_modify)
    398 	proc->state_changed = 1;
    399       return (thread_state_t) &proc->state;
    400     }
    401   else
    402     return 0;
    403 }
    404 
    405 
    406 /* Set PORT to PROC's exception port.  */
    408 kern_return_t
    409 proc_get_exception_port (struct proc * proc, mach_port_t * port)
    410 {
    411   if (proc_is_task (proc))
    412     return task_get_exception_port (proc->port, port);
    413   else
    414     return thread_get_exception_port (proc->port, port);
    415 }
    416 
    417 /* Set PROC's exception port to PORT.  */
    418 kern_return_t
    419 proc_set_exception_port (struct proc * proc, mach_port_t port)
    420 {
    421   proc_debug (proc, "setting exception port: %lu", port);
    422   if (proc_is_task (proc))
    423     return task_set_exception_port (proc->port, port);
    424   else
    425     return thread_set_exception_port (proc->port, port);
    426 }
    427 
    428 /* Get PROC's exception port, cleaning up a bit if proc has died.  */
    429 static mach_port_t
    430 _proc_get_exc_port (struct proc *proc)
    431 {
    432   mach_port_t exc_port;
    433   kern_return_t err = proc_get_exception_port (proc, &exc_port);
    434 
    435   if (err)
    436     /* PROC must be dead.  */
    437     {
    438       if (proc->exc_port)
    439 	mach_port_deallocate (mach_task_self (), proc->exc_port);
    440       proc->exc_port = MACH_PORT_NULL;
    441       if (proc->saved_exc_port)
    442 	mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
    443       proc->saved_exc_port = MACH_PORT_NULL;
    444     }
    445 
    446   return exc_port;
    447 }
    448 
    449 /* Replace PROC's exception port with EXC_PORT, unless it's already
    450    been done.  Stash away any existing exception port so we can
    451    restore it later.  */
    452 void
    453 proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
    454 {
    455   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
    456 
    457   if (cur_exc_port)
    458     {
    459       kern_return_t err = 0;
    460 
    461       proc_debug (proc, "inserting exception port: %lu", exc_port);
    462 
    463       if (cur_exc_port != exc_port)
    464 	/* Put in our exception port.  */
    465 	err = proc_set_exception_port (proc, exc_port);
    466 
    467       if (err || cur_exc_port == proc->exc_port)
    468 	/* We previously set the exception port, and it's still set.  So we
    469 	   just keep the old saved port which is what the proc set.  */
    470 	{
    471 	  if (cur_exc_port)
    472 	    mach_port_deallocate (mach_task_self (), cur_exc_port);
    473 	}
    474       else
    475 	/* Keep a copy of PROC's old exception port so it can be restored.  */
    476 	{
    477 	  if (proc->saved_exc_port)
    478 	    mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
    479 	  proc->saved_exc_port = cur_exc_port;
    480 	}
    481 
    482       proc_debug (proc, "saved exception port: %lu", proc->saved_exc_port);
    483 
    484       if (!err)
    485 	proc->exc_port = exc_port;
    486       else
    487 	warning (_("Error setting exception port for %s: %s"),
    488 		 proc_string (proc), safe_strerror (err));
    489     }
    490 }
    491 
    492 /* If we previously replaced PROC's exception port, put back what we
    493    found there at the time, unless *our* exception port has since been
    494    overwritten, in which case who knows what's going on.  */
    495 void
    496 proc_restore_exc_port (struct proc *proc)
    497 {
    498   mach_port_t cur_exc_port = _proc_get_exc_port (proc);
    499 
    500   if (cur_exc_port)
    501     {
    502       kern_return_t err = 0;
    503 
    504       proc_debug (proc, "restoring real exception port");
    505 
    506       if (proc->exc_port == cur_exc_port)
    507 	/* Our's is still there.  */
    508 	err = proc_set_exception_port (proc, proc->saved_exc_port);
    509 
    510       if (proc->saved_exc_port)
    511 	mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
    512       proc->saved_exc_port = MACH_PORT_NULL;
    513 
    514       if (!err)
    515 	proc->exc_port = MACH_PORT_NULL;
    516       else
    517 	warning (_("Error setting exception port for %s: %s"),
    518 		 proc_string (proc), safe_strerror (err));
    519     }
    520 }
    521 
    522 
    523 /* Turns hardware tracing in PROC on or off when SET is true or false,
    525    respectively.  Returns true on success.  */
    526 int
    527 proc_trace (struct proc *proc, int set)
    528 {
    529   thread_state_t state = proc_get_state (proc, 1);
    530 
    531   if (!state)
    532     return 0;			/* The thread must be dead.  */
    533 
    534   proc_debug (proc, "tracing %s", set ? "on" : "off");
    535 
    536   if (set)
    537     {
    538       /* XXX We don't get the exception unless the thread has its own
    539          exception port????  */
    540       if (proc->exc_port == MACH_PORT_NULL)
    541 	proc_steal_exc_port (proc, proc->inf->event_port);
    542       THREAD_STATE_SET_TRACED (state);
    543     }
    544   else
    545     THREAD_STATE_CLEAR_TRACED (state);
    546 
    547   return 1;
    548 }
    549 
    550 
    551 /* A variable from which to assign new TIDs.  */
    553 static int next_thread_id = 1;
    554 
    555 /* Returns a new proc structure with the given fields.  Also adds a
    556    notification for PORT becoming dead to be sent to INF's notify port.  */
    557 struct proc *
    558 make_proc (struct inf *inf, mach_port_t port, int tid)
    559 {
    560   kern_return_t err;
    561   mach_port_t prev_port = MACH_PORT_NULL;
    562   struct proc *proc = XNEW (struct proc);
    563 
    564   proc->port = port;
    565   proc->tid = tid;
    566   proc->inf = inf;
    567   proc->next = 0;
    568   proc->saved_exc_port = MACH_PORT_NULL;
    569   proc->exc_port = MACH_PORT_NULL;
    570 
    571   proc->sc = 0;
    572   proc->cur_sc = 0;
    573 
    574   /* Note that these are all the values for threads; the task simply uses the
    575      corresponding field in INF directly.  */
    576   proc->run_sc = inf->default_thread_run_sc;
    577   proc->pause_sc = inf->default_thread_pause_sc;
    578   proc->detach_sc = inf->default_thread_detach_sc;
    579   proc->resume_sc = proc->run_sc;
    580 
    581   proc->aborted = 0;
    582   proc->dead = 0;
    583   proc->state_valid = 0;
    584   proc->state_changed = 0;
    585 
    586   proc_debug (proc, "is new");
    587 
    588   /* Get notified when things die.  */
    589   err =
    590     mach_port_request_notification (mach_task_self (), port,
    591 				    MACH_NOTIFY_DEAD_NAME, 1,
    592 				    inf->event_port,
    593 				    MACH_MSG_TYPE_MAKE_SEND_ONCE,
    594 				    &prev_port);
    595   if (err)
    596     warning (_("Couldn't request notification for port %lu: %s"),
    597 	     port, safe_strerror (err));
    598   else
    599     {
    600       proc_debug (proc, "notifications to: %lu", inf->event_port);
    601       if (prev_port != MACH_PORT_NULL)
    602 	mach_port_deallocate (mach_task_self (), prev_port);
    603     }
    604 
    605   if (inf->want_exceptions)
    606     {
    607       if (proc_is_task (proc))
    608 	/* Make the task exception port point to us.  */
    609 	proc_steal_exc_port (proc, inf->event_port);
    610       else
    611 	/* Just clear thread exception ports -- they default to the
    612            task one.  */
    613 	proc_steal_exc_port (proc, MACH_PORT_NULL);
    614     }
    615 
    616   return proc;
    617 }
    618 
    619 /* Frees PROC and any resources it uses, and returns the value of PROC's
    620    next field.  */
    621 struct proc *
    622 _proc_free (struct proc *proc)
    623 {
    624   struct inf *inf = proc->inf;
    625   struct proc *next = proc->next;
    626 
    627   proc_debug (proc, "freeing...");
    628 
    629   if (proc == inf->step_thread)
    630     /* Turn off single stepping.  */
    631     inf_set_step_thread (inf, 0);
    632   if (proc == inf->wait.thread)
    633     inf_clear_wait (inf);
    634   if (proc == inf->signal_thread)
    635     inf->signal_thread = 0;
    636 
    637   if (proc->port != MACH_PORT_NULL)
    638     {
    639       if (proc->exc_port != MACH_PORT_NULL)
    640 	/* Restore the original exception port.  */
    641 	proc_restore_exc_port (proc);
    642       if (proc->cur_sc != 0)
    643 	/* Resume the thread/task.  */
    644 	{
    645 	  proc->sc = 0;
    646 	  proc_update_sc (proc);
    647 	}
    648       mach_port_deallocate (mach_task_self (), proc->port);
    649     }
    650 
    651   xfree (proc);
    652   return next;
    653 }
    654 
    655 
    656 static struct inf *
    658 make_inf (void)
    659 {
    660   struct inf *inf = XNEW (struct inf);
    661 
    662   inf->task = 0;
    663   inf->threads = 0;
    664   inf->threads_up_to_date = 0;
    665   inf->pid = 0;
    666   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
    667   inf->wait.thread = 0;
    668   inf->wait.exc.handler = MACH_PORT_NULL;
    669   inf->wait.exc.reply = MACH_PORT_NULL;
    670   inf->step_thread = 0;
    671   inf->signal_thread = 0;
    672   inf->event_port = MACH_PORT_NULL;
    673   inf->running = 0;
    674   inf->stopped = 0;
    675   inf->nomsg = 1;
    676   inf->traced = 0;
    677   inf->no_wait = 0;
    678   inf->pending_execs = 0;
    679   inf->pause_sc = 1;
    680   inf->detach_sc = 0;
    681   inf->default_thread_run_sc = 0;
    682   inf->default_thread_pause_sc = 0;
    683   inf->default_thread_detach_sc = 0;
    684   inf->want_signals = 1;	/* By default */
    685   inf->want_exceptions = 1;	/* By default */
    686 
    687   return inf;
    688 }
    689 
    690 /* Clear INF's target wait status.  */
    691 void
    692 inf_clear_wait (struct inf *inf)
    693 {
    694   inf_debug (inf, "clearing wait");
    695   inf->wait.status.kind = TARGET_WAITKIND_SPURIOUS;
    696   inf->wait.thread = 0;
    697   inf->wait.suppress = 0;
    698   if (inf->wait.exc.handler != MACH_PORT_NULL)
    699     {
    700       mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
    701       inf->wait.exc.handler = MACH_PORT_NULL;
    702     }
    703   if (inf->wait.exc.reply != MACH_PORT_NULL)
    704     {
    705       mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
    706       inf->wait.exc.reply = MACH_PORT_NULL;
    707     }
    708 }
    709 
    710 
    711 void
    713 inf_cleanup (struct inf *inf)
    714 {
    715   inf_debug (inf, "cleanup");
    716 
    717   inf_clear_wait (inf);
    718 
    719   inf_set_pid (inf, -1);
    720   inf->pid = 0;
    721   inf->running = 0;
    722   inf->stopped = 0;
    723   inf->nomsg = 1;
    724   inf->traced = 0;
    725   inf->no_wait = 0;
    726   inf->pending_execs = 0;
    727 
    728   if (inf->event_port)
    729     {
    730       mach_port_destroy (mach_task_self (), inf->event_port);
    731       inf->event_port = MACH_PORT_NULL;
    732     }
    733 }
    734 
    735 void
    736 inf_startup (struct inf *inf, int pid)
    737 {
    738   kern_return_t err;
    739 
    740   inf_debug (inf, "startup: pid = %d", pid);
    741 
    742   inf_cleanup (inf);
    743 
    744   /* Make the port on which we receive all events.  */
    745   err = mach_port_allocate (mach_task_self (),
    746 			    MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
    747   if (err)
    748     error (_("Error allocating event port: %s"), safe_strerror (err));
    749 
    750   /* Make a send right for it, so we can easily copy it for other people.  */
    751   mach_port_insert_right (mach_task_self (), inf->event_port,
    752 			  inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
    753   inf_set_pid (inf, pid);
    754 }
    755 
    756 
    757 /* Close current process, if any, and attach INF to process PORT.  */
    759 void
    760 inf_set_pid (struct inf *inf, pid_t pid)
    761 {
    762   task_t task_port;
    763   struct proc *task = inf->task;
    764 
    765   inf_debug (inf, "setting pid: %d", pid);
    766 
    767   if (pid < 0)
    768     task_port = MACH_PORT_NULL;
    769   else
    770     {
    771       kern_return_t err = proc_pid2task (proc_server, pid, &task_port);
    772 
    773       if (err)
    774 	error (_("Error getting task for pid %d: %s"),
    775 	       pid, safe_strerror (err));
    776     }
    777 
    778   inf_debug (inf, "setting task: %lu", task_port);
    779 
    780   if (inf->pause_sc)
    781     task_suspend (task_port);
    782 
    783   if (task && task->port != task_port)
    784     {
    785       inf->task = 0;
    786       inf_validate_procs (inf);	/* Trash all the threads.  */
    787       _proc_free (task);	/* And the task.  */
    788     }
    789 
    790   if (task_port != MACH_PORT_NULL)
    791     {
    792       inf->task = make_proc (inf, task_port, PROC_TID_TASK);
    793       inf->threads_up_to_date = 0;
    794     }
    795 
    796   if (inf->task)
    797     {
    798       inf->pid = pid;
    799       if (inf->pause_sc)
    800 	/* Reflect task_suspend above.  */
    801 	inf->task->sc = inf->task->cur_sc = 1;
    802     }
    803   else
    804     inf->pid = -1;
    805 }
    806 
    807 
    808 /* Validates INF's stopped, nomsg and traced field from the actual
    810    proc server state.  Note that the traced field is only updated from
    811    the proc server state if we do not have a message port.  If we do
    812    have a message port we'd better look at the tracemask itself.  */
    813 static void
    814 inf_validate_procinfo (struct inf *inf)
    815 {
    816   char *noise;
    817   mach_msg_type_number_t noise_len = 0;
    818   struct procinfo *pi;
    819   mach_msg_type_number_t pi_len = 0;
    820   int info_flags = 0;
    821   kern_return_t err =
    822     proc_getprocinfo (proc_server, inf->pid, &info_flags,
    823 		      (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
    824 
    825   if (!err)
    826     {
    827       inf->stopped = !!(pi->state & PI_STOPPED);
    828       inf->nomsg = !!(pi->state & PI_NOMSG);
    829       if (inf->nomsg)
    830 	inf->traced = !!(pi->state & PI_TRACED);
    831       vm_deallocate (mach_task_self (), (vm_address_t) pi,
    832 		     pi_len * sizeof (*(procinfo_t) 0));
    833       if (noise_len > 0)
    834 	vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
    835     }
    836 }
    837 
    838 /* Validates INF's task suspend count.  If it's higher than we expect,
    839    verify with the user before `stealing' the extra count.  */
    840 static void
    841 inf_validate_task_sc (struct inf *inf)
    842 {
    843   char *noise;
    844   mach_msg_type_number_t noise_len = 0;
    845   struct procinfo *pi;
    846   mach_msg_type_number_t pi_len = 0;
    847   int info_flags = PI_FETCH_TASKINFO;
    848   int suspend_count = -1;
    849   kern_return_t err;
    850 
    851  retry:
    852   err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
    853 			  (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
    854   if (err)
    855     {
    856       inf->task->dead = 1; /* oh well */
    857       return;
    858     }
    859 
    860   if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
    861     {
    862       /* The proc server might have suspended the task while stopping
    863          it.  This happens when the task is handling a traced signal.
    864          Refetch the suspend count.  The proc server should be
    865          finished stopping the task by now.  */
    866       suspend_count = pi->taskinfo.suspend_count;
    867       goto retry;
    868     }
    869 
    870   suspend_count = pi->taskinfo.suspend_count;
    871 
    872   vm_deallocate (mach_task_self (), (vm_address_t) pi,
    873 		 pi_len * sizeof (*(procinfo_t) 0));
    874   if (noise_len > 0)
    875     vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
    876 
    877   if (inf->task->cur_sc < suspend_count)
    878     {
    879       if (!query (_("Pid %d has an additional task suspend count of %d;"
    880 		    " clear it? "), inf->pid,
    881 		  suspend_count - inf->task->cur_sc))
    882 	error (_("Additional task suspend count left untouched."));
    883 
    884       inf->task->cur_sc = suspend_count;
    885     }
    886 }
    887 
    888 /* Turns tracing for INF on or off, depending on ON, unless it already
    889    is.  If INF is running, the resume_sc count of INF's threads will
    890    be modified, and the signal thread will briefly be run to change
    891    the trace state.  */
    892 static void
    893 inf_set_traced (struct inf *inf, int on)
    894 {
    895   if (on == inf->traced)
    896     return;
    897 
    898   if (inf->task && !inf->task->dead)
    899     /* Make it take effect immediately.  */
    900     {
    901       sigset_t mask = on ? ~(sigset_t) 0 : 0;
    902       kern_return_t err =
    903 	INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
    904 						       INIT_TRACEMASK, mask));
    905 
    906       if (err == EIEIO)
    907 	{
    908 	  if (on)
    909 	    warning (_("Can't modify tracing state for pid %d: %s"),
    910 		     inf->pid, "No signal thread");
    911 	  inf->traced = on;
    912 	}
    913       else if (err)
    914 	warning (_("Can't modify tracing state for pid %d: %s"),
    915 		 inf->pid, safe_strerror (err));
    916       else
    917 	inf->traced = on;
    918     }
    919   else
    920     inf->traced = on;
    921 }
    922 
    923 
    924 /* Makes all the real suspend count deltas of all the procs in INF
    926    match the desired values.  Careful to always do thread/task suspend
    927    counts in the safe order.  Returns true if at least one thread is
    928    thought to be running.  */
    929 int
    930 inf_update_suspends (struct inf *inf)
    931 {
    932   struct proc *task = inf->task;
    933 
    934   /* We don't have to update INF->threads even though we're iterating over it
    935      because we'll change a thread only if it already has an existing proc
    936      entry.  */
    937   inf_debug (inf, "updating suspend counts");
    938 
    939   if (task)
    940     {
    941       struct proc *thread;
    942       int task_running = (task->sc == 0), thread_running = 0;
    943 
    944       if (task->sc > task->cur_sc)
    945 	/* The task is becoming _more_ suspended; do before any threads.  */
    946 	task_running = proc_update_sc (task);
    947 
    948       if (inf->pending_execs)
    949 	/* When we're waiting for an exec, things may be happening behind our
    950 	   back, so be conservative.  */
    951 	thread_running = 1;
    952 
    953       /* Do all the thread suspend counts.  */
    954       for (thread = inf->threads; thread; thread = thread->next)
    955 	thread_running |= proc_update_sc (thread);
    956 
    957       if (task->sc != task->cur_sc)
    958 	/* We didn't do the task first, because we wanted to wait for the
    959 	   threads; do it now.  */
    960 	task_running = proc_update_sc (task);
    961 
    962       inf_debug (inf, "%srunning...",
    963 		 (thread_running && task_running) ? "" : "not ");
    964 
    965       inf->running = thread_running && task_running;
    966 
    967       /* Once any thread has executed some code, we can't depend on the
    968          threads list any more.  */
    969       if (inf->running)
    970 	inf->threads_up_to_date = 0;
    971 
    972       return inf->running;
    973     }
    974 
    975   return 0;
    976 }
    977 
    978 
    979 /* Converts a GDB pid to a struct proc.  */
    981 struct proc *
    982 inf_tid_to_thread (struct inf *inf, int tid)
    983 {
    984   struct proc *thread = inf->threads;
    985 
    986   while (thread)
    987     if (thread->tid == tid)
    988       return thread;
    989     else
    990       thread = thread->next;
    991   return 0;
    992 }
    993 
    994 /* Converts a thread port to a struct proc.  */
    995 static struct proc *
    996 inf_port_to_thread (struct inf *inf, mach_port_t port)
    997 {
    998   struct proc *thread = inf->threads;
    999 
   1000   while (thread)
   1001     if (thread->port == port)
   1002       return thread;
   1003     else
   1004       thread = thread->next;
   1005   return 0;
   1006 }
   1007 
   1008 /* See gnu-nat.h.  */
   1009 
   1010 void
   1011 inf_threads (struct inf *inf, inf_threads_ftype *f, void *arg)
   1012 {
   1013   struct proc *thread;
   1014 
   1015   for (thread = inf->threads; thread; thread = thread->next)
   1016     f (thread, arg);
   1017 }
   1018 
   1019 
   1020 /* Make INF's list of threads be consistent with reality of TASK.  */
   1022 void
   1023 inf_validate_procs (struct inf *inf)
   1024 {
   1025   thread_array_t threads;
   1026   mach_msg_type_number_t num_threads, i;
   1027   struct proc *task = inf->task;
   1028 
   1029   /* If no threads are currently running, this function will guarantee that
   1030      things are up to date.  The exception is if there are zero threads --
   1031      then it is almost certainly in an odd state, and probably some outside
   1032      agent will create threads.  */
   1033   inf->threads_up_to_date = inf->threads ? !inf->running : 0;
   1034 
   1035   if (task)
   1036     {
   1037       kern_return_t err = task_threads (task->port, &threads, &num_threads);
   1038 
   1039       inf_debug (inf, "fetching threads");
   1040       if (err)
   1041 	/* TASK must be dead.  */
   1042 	{
   1043 	  task->dead = 1;
   1044 	  task = 0;
   1045 	}
   1046     }
   1047 
   1048   if (!task)
   1049     {
   1050       num_threads = 0;
   1051       inf_debug (inf, "no task");
   1052     }
   1053 
   1054   {
   1055     /* Make things normally linear.  */
   1056     mach_msg_type_number_t search_start = 0;
   1057     /* Which thread in PROCS corresponds to each task thread, & the task.  */
   1058     struct proc *matched[num_threads + 1];
   1059     /* The last thread in INF->threads, so we can add to the end.  */
   1060     struct proc *last = 0;
   1061     /* The current thread we're considering.  */
   1062     struct proc *thread = inf->threads;
   1063 
   1064     memset (matched, 0, sizeof (matched));
   1065 
   1066     while (thread)
   1067       {
   1068 	mach_msg_type_number_t left;
   1069 
   1070 	for (i = search_start, left = num_threads; left; i++, left--)
   1071 	  {
   1072 	    if (i >= num_threads)
   1073 	      i -= num_threads;	/* I wrapped around.  */
   1074 	    if (thread->port == threads[i])
   1075 	      /* We already know about this thread.  */
   1076 	      {
   1077 		matched[i] = thread;
   1078 		last = thread;
   1079 		thread = thread->next;
   1080 		search_start++;
   1081 		break;
   1082 	      }
   1083 	  }
   1084 
   1085 	if (!left)
   1086 	  {
   1087 	    proc_debug (thread, "died!");
   1088 	    thread->port = MACH_PORT_NULL;
   1089 	    thread = _proc_free (thread);	/* THREAD is dead.  */
   1090 	    if (last)
   1091 	      last->next = thread;
   1092 	    else
   1093 	      inf->threads = thread;
   1094 	  }
   1095       }
   1096 
   1097     for (i = 0; i < num_threads; i++)
   1098       {
   1099 	if (matched[i])
   1100 	  /* Throw away the duplicate send right.  */
   1101 	  mach_port_deallocate (mach_task_self (), threads[i]);
   1102 	else
   1103 	  /* THREADS[I] is a thread we don't know about yet!  */
   1104 	  {
   1105 	    ptid_t ptid;
   1106 
   1107 	    thread = make_proc (inf, threads[i], next_thread_id++);
   1108 	    if (last)
   1109 	      last->next = thread;
   1110 	    else
   1111 	      inf->threads = thread;
   1112 	    last = thread;
   1113 	    proc_debug (thread, "new thread: %lu", threads[i]);
   1114 
   1115 	    ptid = ptid_t (inf->pid, thread->tid, 0);
   1116 
   1117 	    /* Tell GDB's generic thread code.  */
   1118 
   1119 	    if (inferior_ptid == ptid_t (inf->pid))
   1120 	      /* This is the first time we're hearing about thread
   1121 		 ids, after a fork-child.  */
   1122 	      thread_change_ptid (inferior_ptid, ptid);
   1123 	    else if (inf->pending_execs != 0)
   1124 	      /* This is a shell thread.  */
   1125 	      add_thread_silent (ptid);
   1126 	    else
   1127 	      add_thread (ptid);
   1128 	  }
   1129       }
   1130 
   1131     vm_deallocate (mach_task_self (),
   1132 		   (vm_address_t) threads, (num_threads * sizeof (thread_t)));
   1133   }
   1134 }
   1135 
   1136 
   1137 /* Makes sure that INF's thread list is synced with the actual process.  */
   1139 int
   1140 inf_update_procs (struct inf *inf)
   1141 {
   1142   if (!inf->task)
   1143     return 0;
   1144   if (!inf->threads_up_to_date)
   1145     inf_validate_procs (inf);
   1146   return !!inf->task;
   1147 }
   1148 
   1149 /* Sets the resume_sc of each thread in inf.  That of RUN_THREAD is set to 0,
   1150    and others are set to their run_sc if RUN_OTHERS is true, and otherwise
   1151    their pause_sc.  */
   1152 void
   1153 inf_set_threads_resume_sc (struct inf *inf,
   1154 			   struct proc *run_thread, int run_others)
   1155 {
   1156   struct proc *thread;
   1157 
   1158   inf_update_procs (inf);
   1159   for (thread = inf->threads; thread; thread = thread->next)
   1160     if (thread == run_thread)
   1161       thread->resume_sc = 0;
   1162     else if (run_others)
   1163       thread->resume_sc = thread->run_sc;
   1164     else
   1165       thread->resume_sc = thread->pause_sc;
   1166 }
   1167 
   1168 
   1169 /* Cause INF to continue execution immediately; individual threads may still
   1171    be suspended (but their suspend counts will be updated).  */
   1172 void
   1173 inf_resume (struct inf *inf)
   1174 {
   1175   struct proc *thread;
   1176 
   1177   inf_update_procs (inf);
   1178 
   1179   for (thread = inf->threads; thread; thread = thread->next)
   1180     thread->sc = thread->resume_sc;
   1181 
   1182   if (inf->task)
   1183     {
   1184       if (!inf->pending_execs)
   1185 	/* Try to make sure our task count is correct -- in the case where
   1186 	   we're waiting for an exec though, things are too volatile, so just
   1187 	   assume things will be reasonable (which they usually will be).  */
   1188 	inf_validate_task_sc (inf);
   1189       inf->task->sc = 0;
   1190     }
   1191 
   1192   inf_update_suspends (inf);
   1193 }
   1194 
   1195 /* Cause INF to stop execution immediately; individual threads may still
   1196    be running.  */
   1197 void
   1198 inf_suspend (struct inf *inf)
   1199 {
   1200   struct proc *thread;
   1201 
   1202   inf_update_procs (inf);
   1203 
   1204   for (thread = inf->threads; thread; thread = thread->next)
   1205     thread->sc = thread->pause_sc;
   1206 
   1207   if (inf->task)
   1208     inf->task->sc = inf->pause_sc;
   1209 
   1210   inf_update_suspends (inf);
   1211 }
   1212 
   1213 
   1214 /* INF has one thread PROC that is in single-stepping mode.  This
   1216    function changes it to be PROC, changing any old step_thread to be
   1217    a normal one.  A PROC of 0 clears any existing value.  */
   1218 void
   1219 inf_set_step_thread (struct inf *inf, struct proc *thread)
   1220 {
   1221   gdb_assert (!thread || proc_is_thread (thread));
   1222 
   1223   if (thread)
   1224     inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
   1225   else
   1226     inf_debug (inf, "clearing step thread");
   1227 
   1228   if (inf->step_thread != thread)
   1229     {
   1230       if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
   1231 	if (!proc_trace (inf->step_thread, 0))
   1232 	  return;
   1233       if (thread && proc_trace (thread, 1))
   1234 	inf->step_thread = thread;
   1235       else
   1236 	inf->step_thread = 0;
   1237     }
   1238 }
   1239 
   1240 
   1241 /* Set up the thread resume_sc's so that only the signal thread is running
   1243    (plus whatever other thread are set to always run).  Returns true if we
   1244    did so, or false if we can't find a signal thread.  */
   1245 int
   1246 inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
   1247 {
   1248   if (inf->signal_thread)
   1249     {
   1250       inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
   1251       return 1;
   1252     }
   1253   else
   1254     return 0;
   1255 }
   1256 
   1257 static void
   1258 inf_update_signal_thread (struct inf *inf)
   1259 {
   1260   /* XXX for now we assume that if there's a msgport, the 2nd thread is
   1261      the signal thread.  */
   1262   inf->signal_thread = inf->threads ? inf->threads->next : 0;
   1263 }
   1264 
   1265 
   1266 /* Detachs from INF's inferior task, letting it run once again...  */
   1268 void
   1269 inf_detach (struct inf *inf)
   1270 {
   1271   struct proc *task = inf->task;
   1272 
   1273   inf_debug (inf, "detaching...");
   1274 
   1275   inf_clear_wait (inf);
   1276   inf_set_step_thread (inf, 0);
   1277 
   1278   if (task)
   1279     {
   1280       struct proc *thread;
   1281 
   1282       inf_validate_procinfo (inf);
   1283 
   1284       inf_set_traced (inf, 0);
   1285       if (inf->stopped)
   1286 	{
   1287 	  if (inf->nomsg)
   1288 	    inf_continue (inf);
   1289 	  else
   1290 	    inf_signal (inf, GDB_SIGNAL_0);
   1291 	}
   1292 
   1293       proc_restore_exc_port (task);
   1294       task->sc = inf->detach_sc;
   1295 
   1296       for (thread = inf->threads; thread; thread = thread->next)
   1297 	{
   1298 	  proc_restore_exc_port (thread);
   1299 	  thread->sc = thread->detach_sc;
   1300 	}
   1301 
   1302       inf_update_suspends (inf);
   1303     }
   1304 
   1305   inf_cleanup (inf);
   1306 }
   1307 
   1308 /* Attaches INF to the process with process id PID, returning it in a
   1309    suspended state suitable for debugging.  */
   1310 void
   1311 inf_attach (struct inf *inf, int pid)
   1312 {
   1313   inf_debug (inf, "attaching: %d", pid);
   1314 
   1315   if (inf->pid)
   1316     inf_detach (inf);
   1317 
   1318   inf_startup (inf, pid);
   1319 }
   1320 
   1321 
   1322 /* Makes sure that we've got our exception ports entrenched in the process.  */
   1324 void
   1325 inf_steal_exc_ports (struct inf *inf)
   1326 {
   1327   struct proc *thread;
   1328 
   1329   inf_debug (inf, "stealing exception ports");
   1330 
   1331   inf_set_step_thread (inf, 0);	/* The step thread is special.  */
   1332 
   1333   proc_steal_exc_port (inf->task, inf->event_port);
   1334   for (thread = inf->threads; thread; thread = thread->next)
   1335     proc_steal_exc_port (thread, MACH_PORT_NULL);
   1336 }
   1337 
   1338 /* Makes sure the process has its own exception ports.  */
   1339 void
   1340 inf_restore_exc_ports (struct inf *inf)
   1341 {
   1342   struct proc *thread;
   1343 
   1344   inf_debug (inf, "restoring exception ports");
   1345 
   1346   inf_set_step_thread (inf, 0);	/* The step thread is special.  */
   1347 
   1348   proc_restore_exc_port (inf->task);
   1349   for (thread = inf->threads; thread; thread = thread->next)
   1350     proc_restore_exc_port (thread);
   1351 }
   1352 
   1353 
   1354 /* Deliver signal SIG to INF.  If INF is stopped, delivering a signal, even
   1356    signal 0, will continue it.  INF is assumed to be in a paused state, and
   1357    the resume_sc's of INF's threads may be affected.  */
   1358 void
   1359 inf_signal (struct inf *inf, enum gdb_signal sig)
   1360 {
   1361   kern_return_t err = 0;
   1362   int host_sig = gdb_signal_to_host (sig);
   1363 
   1364 #define NAME gdb_signal_to_name (sig)
   1365 
   1366   if (host_sig >= _NSIG)
   1367     /* A mach exception.  Exceptions are encoded in the signal space by
   1368        putting them after _NSIG; this assumes they're positive (and not
   1369        extremely large)!  */
   1370     {
   1371       struct inf_wait *w = &inf->wait;
   1372 
   1373       if (w->status.kind == TARGET_WAITKIND_STOPPED
   1374 	  && w->status.value.sig == sig
   1375 	  && w->thread && !w->thread->aborted)
   1376 	/* We're passing through the last exception we received.  This is
   1377 	   kind of bogus, because exceptions are per-thread whereas gdb
   1378 	   treats signals as per-process.  We just forward the exception to
   1379 	   the correct handler, even it's not for the same thread as TID --
   1380 	   i.e., we pretend it's global.  */
   1381 	{
   1382 	  struct exc_state *e = &w->exc;
   1383 
   1384 	  inf_debug (inf, "passing through exception:"
   1385 		     " task = %lu, thread = %lu, exc = %d"
   1386 		     ", code = %d, subcode = %d",
   1387 		     w->thread->port, inf->task->port,
   1388 		     e->exception, e->code, e->subcode);
   1389 	  err =
   1390 	    exception_raise_request (e->handler,
   1391 				     e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
   1392 				     w->thread->port, inf->task->port,
   1393 				     e->exception, e->code, e->subcode);
   1394 	}
   1395       else
   1396 	error (_("Can't forward spontaneous exception (%s)."), NAME);
   1397     }
   1398   else
   1399     /* A Unix signal.  */
   1400   if (inf->stopped)
   1401     /* The process is stopped and expecting a signal.  Just send off a
   1402        request and let it get handled when we resume everything.  */
   1403     {
   1404       inf_debug (inf, "sending %s to stopped process", NAME);
   1405       err =
   1406 	INF_MSGPORT_RPC (inf,
   1407 			 msg_sig_post_untraced_request (msgport,
   1408 							inf->event_port,
   1409 					       MACH_MSG_TYPE_MAKE_SEND_ONCE,
   1410 							host_sig, 0,
   1411 							refport));
   1412       if (!err)
   1413 	/* Posting an untraced signal automatically continues it.
   1414 	   We clear this here rather than when we get the reply
   1415 	   because we'd rather assume it's not stopped when it
   1416 	   actually is, than the reverse.  */
   1417 	inf->stopped = 0;
   1418     }
   1419   else
   1420     /* It's not expecting it.  We have to let just the signal thread
   1421        run, and wait for it to get into a reasonable state before we
   1422        can continue the rest of the process.  When we finally resume the
   1423        process the signal we request will be the very first thing that
   1424        happens.  */
   1425     {
   1426       inf_debug (inf, "sending %s to unstopped process"
   1427 		 " (so resuming signal thread)", NAME);
   1428       err =
   1429 	INF_RESUME_MSGPORT_RPC (inf,
   1430 				msg_sig_post_untraced (msgport, host_sig,
   1431 						       0, refport));
   1432     }
   1433 
   1434   if (err == EIEIO)
   1435     /* Can't do too much...  */
   1436     warning (_("Can't deliver signal %s: No signal thread."), NAME);
   1437   else if (err)
   1438     warning (_("Delivering signal %s: %s"), NAME, safe_strerror (err));
   1439 
   1440 #undef NAME
   1441 }
   1442 
   1443 
   1444 /* Continue INF without delivering a signal.  This is meant to be used
   1446    when INF does not have a message port.  */
   1447 void
   1448 inf_continue (struct inf *inf)
   1449 {
   1450   process_t proc;
   1451   kern_return_t err = proc_pid2proc (proc_server, inf->pid, &proc);
   1452 
   1453   if (!err)
   1454     {
   1455       inf_debug (inf, "continuing process");
   1456 
   1457       err = proc_mark_cont (proc);
   1458       if (!err)
   1459 	{
   1460 	  struct proc *thread;
   1461 
   1462 	  for (thread = inf->threads; thread; thread = thread->next)
   1463 	    thread_resume (thread->port);
   1464 
   1465 	  inf->stopped = 0;
   1466 	}
   1467     }
   1468 
   1469   if (err)
   1470     warning (_("Can't continue process: %s"), safe_strerror (err));
   1471 }
   1472 
   1473 
   1474 /* The inferior used for all gdb target ops.  */
   1476 struct inf *gnu_current_inf = 0;
   1477 
   1478 /* The inferior being waited for by gnu_wait.  Since GDB is decidely not
   1479    multi-threaded, we don't bother to lock this.  */
   1480 struct inf *waiting_inf;
   1481 
   1482 /* MIG stubs are not yet ready for C++ compilation.  */
   1483 extern "C" int exc_server (mach_msg_header_t *, mach_msg_header_t *);
   1484 extern "C" int msg_reply_server (mach_msg_header_t *, mach_msg_header_t *);
   1485 extern "C" int notify_server (mach_msg_header_t *, mach_msg_header_t *);
   1486 extern "C" int process_reply_server (mach_msg_header_t *, mach_msg_header_t *);
   1487 
   1488 /* Wait for something to happen in the inferior, returning what in STATUS.  */
   1489 
   1490 ptid_t
   1491 gnu_nat_target::wait (ptid_t ptid, struct target_waitstatus *status,
   1492 		      int options)
   1493 {
   1494   struct msg
   1495     {
   1496       mach_msg_header_t hdr;
   1497       mach_msg_type_t type;
   1498       int data[8000];
   1499     } msg;
   1500   kern_return_t err;
   1501   struct proc *thread;
   1502   struct inf *inf = gnu_current_inf;
   1503 
   1504   gdb_assert (inf->task);
   1505 
   1506   if (!inf->threads && !inf->pending_execs)
   1507     /* No threads!  Assume that maybe some outside agency is frobbing our
   1508        task, and really look for new threads.  If we can't find any, just tell
   1509        the user to try again later.  */
   1510     {
   1511       inf_validate_procs (inf);
   1512       if (!inf->threads && !inf->task->dead)
   1513 	error (_("There are no threads; try again later."));
   1514     }
   1515 
   1516   waiting_inf = inf;
   1517 
   1518   inf_debug (inf, "waiting for: %s", target_pid_to_str (ptid));
   1519 
   1520 rewait:
   1521   if (proc_wait_pid != inf->pid && !inf->no_wait)
   1522     /* Always get information on events from the proc server.  */
   1523     {
   1524       inf_debug (inf, "requesting wait on pid %d", inf->pid);
   1525 
   1526       if (proc_wait_pid)
   1527 	/* The proc server is single-threaded, and only allows a single
   1528 	   outstanding wait request, so we have to cancel the previous one.  */
   1529 	{
   1530 	  inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
   1531 	  interrupt_operation (proc_server, 0);
   1532 	}
   1533 
   1534       err =
   1535 	proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
   1536       if (err)
   1537 	warning (_("wait request failed: %s"), safe_strerror (err));
   1538       else
   1539 	{
   1540 	  inf_debug (inf, "waits pending: %d", proc_waits_pending);
   1541 	  proc_wait_pid = inf->pid;
   1542 	  /* Even if proc_waits_pending was > 0 before, we still won't
   1543 	     get any other replies, because it was either from a
   1544 	     different INF, or a different process attached to INF --
   1545 	     and the event port, which is the wait reply port, changes
   1546 	     when you switch processes.  */
   1547 	  proc_waits_pending = 1;
   1548 	}
   1549     }
   1550 
   1551   inf_clear_wait (inf);
   1552 
   1553   /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
   1554      (3) wait reply from the proc server.  */
   1555 
   1556   inf_debug (inf, "waiting for an event...");
   1557   err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
   1558 		  0, sizeof (struct msg), inf->event_port,
   1559 		  MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
   1560 
   1561   /* Re-suspend the task.  */
   1562   inf_suspend (inf);
   1563 
   1564   if (!inf->task && inf->pending_execs)
   1565     /* When doing an exec, it's possible that the old task wasn't reused
   1566        (e.g., setuid execs).  So if the task seems to have disappeared,
   1567        attempt to refetch it, as the pid should still be the same.  */
   1568     inf_set_pid (inf, inf->pid);
   1569 
   1570   if (err == EMACH_RCV_INTERRUPTED)
   1571     inf_debug (inf, "interrupted");
   1572   else if (err)
   1573     error (_("Couldn't wait for an event: %s"), safe_strerror (err));
   1574   else
   1575     {
   1576       struct
   1577 	{
   1578 	  mach_msg_header_t hdr;
   1579 	  mach_msg_type_t err_type;
   1580 	  kern_return_t err;
   1581 	  char noise[200];
   1582 	}
   1583       reply;
   1584 
   1585       inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
   1586 
   1587       /* Handle what we got.  */
   1588       if (!notify_server (&msg.hdr, &reply.hdr)
   1589 	  && !exc_server (&msg.hdr, &reply.hdr)
   1590 	  && !process_reply_server (&msg.hdr, &reply.hdr)
   1591 	  && !msg_reply_server (&msg.hdr, &reply.hdr))
   1592 	/* Whatever it is, it's something strange.  */
   1593 	error (_("Got a strange event, msg id = %d."), msg.hdr.msgh_id);
   1594 
   1595       if (reply.err)
   1596 	error (_("Handling event, msgid = %d: %s"),
   1597 	       msg.hdr.msgh_id, safe_strerror (reply.err));
   1598     }
   1599 
   1600   if (inf->pending_execs)
   1601     /* We're waiting for the inferior to finish execing.  */
   1602     {
   1603       struct inf_wait *w = &inf->wait;
   1604       enum target_waitkind kind = w->status.kind;
   1605 
   1606       if (kind == TARGET_WAITKIND_SPURIOUS)
   1607 	/* Since gdb is actually counting the number of times the inferior
   1608 	   stops, expecting one stop per exec, we only return major events
   1609 	   while execing.  */
   1610 	{
   1611 	  w->suppress = 1;
   1612 	  inf_debug (inf, "pending_execs, ignoring minor event");
   1613 	}
   1614       else if (kind == TARGET_WAITKIND_STOPPED
   1615 	       && w->status.value.sig == GDB_SIGNAL_TRAP)
   1616 	/* Ah hah!  A SIGTRAP from the inferior while starting up probably
   1617 	   means we've succesfully completed an exec!  */
   1618 	{
   1619 	  inf_debug (inf, "one pending exec completed");
   1620 	}
   1621       else if (kind == TARGET_WAITKIND_STOPPED)
   1622 	/* It's possible that this signal is because of a crashed process
   1623 	   being handled by the hurd crash server; in this case, the process
   1624 	   will have an extra task suspend, which we need to know about.
   1625 	   Since the code in inf_resume that normally checks for this is
   1626 	   disabled while INF->pending_execs, we do the check here instead.  */
   1627 	inf_validate_task_sc (inf);
   1628     }
   1629 
   1630   if (inf->wait.suppress)
   1631     /* Some totally spurious event happened that we don't consider
   1632        worth returning to gdb.  Just keep waiting.  */
   1633     {
   1634       inf_debug (inf, "suppressing return, rewaiting...");
   1635       inf_resume (inf);
   1636       goto rewait;
   1637     }
   1638 
   1639   /* Pass back out our results.  */
   1640   memcpy (status, &inf->wait.status, sizeof (*status));
   1641 
   1642   thread = inf->wait.thread;
   1643   if (thread)
   1644     ptid = ptid_t (inf->pid, thread->tid, 0);
   1645   else if (ptid == minus_one_ptid)
   1646     thread = inf_tid_to_thread (inf, -1);
   1647   else
   1648     thread = inf_tid_to_thread (inf, ptid.lwp ());
   1649 
   1650   if (!thread || thread->port == MACH_PORT_NULL)
   1651     {
   1652       /* TID is dead; try and find a new thread.  */
   1653       if (inf_update_procs (inf) && inf->threads)
   1654 	ptid = ptid_t (inf->pid, inf->threads->tid, 0); /* The first
   1655 							       available
   1656 							       thread.  */
   1657       else
   1658 	ptid = inferior_ptid;	/* let wait_for_inferior handle exit case */
   1659     }
   1660 
   1661   if (thread
   1662       && ptid != minus_one_ptid
   1663       && status->kind != TARGET_WAITKIND_SPURIOUS
   1664       && inf->pause_sc == 0 && thread->pause_sc == 0)
   1665     /* If something actually happened to THREAD, make sure we
   1666        suspend it.  */
   1667     {
   1668       thread->sc = 1;
   1669       inf_update_suspends (inf);
   1670     }
   1671 
   1672   inf_debug (inf, "returning ptid = %s, status = %s (%d)",
   1673 	     target_pid_to_str (ptid),
   1674 	     status->kind == TARGET_WAITKIND_EXITED ? "EXITED"
   1675 	     : status->kind == TARGET_WAITKIND_STOPPED ? "STOPPED"
   1676 	     : status->kind == TARGET_WAITKIND_SIGNALLED ? "SIGNALLED"
   1677 	     : status->kind == TARGET_WAITKIND_LOADED ? "LOADED"
   1678 	     : status->kind == TARGET_WAITKIND_SPURIOUS ? "SPURIOUS"
   1679 	     : "?",
   1680 	     status->value.integer);
   1681 
   1682   return ptid;
   1683 }
   1684 
   1685 
   1686 /* The rpc handler called by exc_server.  */
   1688 kern_return_t
   1689 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
   1690 			   thread_t thread_port, task_t task_port,
   1691 			   int exception, int code, int subcode)
   1692 {
   1693   struct inf *inf = waiting_inf;
   1694   struct proc *thread = inf_port_to_thread (inf, thread_port);
   1695 
   1696   inf_debug (waiting_inf,
   1697 	     "thread = %lu, task = %lu, exc = %d, code = %d, subcode = %d",
   1698 	     thread_port, task_port, exception, code, subcode);
   1699 
   1700   if (!thread)
   1701     /* We don't know about thread?  */
   1702     {
   1703       inf_update_procs (inf);
   1704       thread = inf_port_to_thread (inf, thread_port);
   1705       if (!thread)
   1706 	/* Give up, the generating thread is gone.  */
   1707 	return 0;
   1708     }
   1709 
   1710   mach_port_deallocate (mach_task_self (), thread_port);
   1711   mach_port_deallocate (mach_task_self (), task_port);
   1712 
   1713   if (!thread->aborted)
   1714     /* THREAD hasn't been aborted since this exception happened (abortion
   1715        clears any exception state), so it must be real.  */
   1716     {
   1717       /* Store away the details; this will destroy any previous info.  */
   1718       inf->wait.thread = thread;
   1719 
   1720       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
   1721 
   1722       if (exception == EXC_BREAKPOINT)
   1723 	/* GDB likes to get SIGTRAP for breakpoints.  */
   1724 	{
   1725 	  inf->wait.status.value.sig = GDB_SIGNAL_TRAP;
   1726 	  mach_port_deallocate (mach_task_self (), reply_port);
   1727 	}
   1728       else
   1729 	/* Record the exception so that we can forward it later.  */
   1730 	{
   1731 	  if (thread->exc_port == port)
   1732 	    {
   1733 	      inf_debug (waiting_inf, "Handler is thread exception port <%lu>",
   1734 			 thread->saved_exc_port);
   1735 	      inf->wait.exc.handler = thread->saved_exc_port;
   1736 	    }
   1737 	  else
   1738 	    {
   1739 	      inf_debug (waiting_inf, "Handler is task exception port <%lu>",
   1740 			 inf->task->saved_exc_port);
   1741 	      inf->wait.exc.handler = inf->task->saved_exc_port;
   1742 	      gdb_assert (inf->task->exc_port == port);
   1743 	    }
   1744 	  if (inf->wait.exc.handler != MACH_PORT_NULL)
   1745 	    /* Add a reference to the exception handler.  */
   1746 	    mach_port_mod_refs (mach_task_self (),
   1747 				inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
   1748 				1);
   1749 
   1750 	  inf->wait.exc.exception = exception;
   1751 	  inf->wait.exc.code = code;
   1752 	  inf->wait.exc.subcode = subcode;
   1753 	  inf->wait.exc.reply = reply_port;
   1754 
   1755 	  /* Exceptions are encoded in the signal space by putting
   1756 	     them after _NSIG; this assumes they're positive (and not
   1757 	     extremely large)!  */
   1758 	  inf->wait.status.value.sig =
   1759 	    gdb_signal_from_host (_NSIG + exception);
   1760 	}
   1761     }
   1762   else
   1763     /* A supppressed exception, which ignore.  */
   1764     {
   1765       inf->wait.suppress = 1;
   1766       mach_port_deallocate (mach_task_self (), reply_port);
   1767     }
   1768 
   1769   return 0;
   1770 }
   1771 
   1772 
   1773 /* Fill in INF's wait field after a task has died without giving us more
   1775    detailed information.  */
   1776 static void
   1777 inf_task_died_status (struct inf *inf)
   1778 {
   1779   warning (_("Pid %d died with unknown exit status, using SIGKILL."),
   1780 	   inf->pid);
   1781   inf->wait.status.kind = TARGET_WAITKIND_SIGNALLED;
   1782   inf->wait.status.value.sig = GDB_SIGNAL_KILL;
   1783 }
   1784 
   1785 /* Notify server routines.  The only real one is dead name notification.  */
   1786 kern_return_t
   1787 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
   1788 {
   1789   struct inf *inf = waiting_inf;
   1790 
   1791   inf_debug (waiting_inf, "port = %lu", dead_port);
   1792 
   1793   if (inf->task && inf->task->port == dead_port)
   1794     {
   1795       proc_debug (inf->task, "is dead");
   1796       inf->task->port = MACH_PORT_NULL;
   1797       if (proc_wait_pid == inf->pid)
   1798 	/* We have a wait outstanding on the process, which will return more
   1799 	   detailed information, so delay until we get that.  */
   1800 	inf->wait.suppress = 1;
   1801       else
   1802 	/* We never waited for the process (maybe it wasn't a child), so just
   1803 	   pretend it got a SIGKILL.  */
   1804 	inf_task_died_status (inf);
   1805     }
   1806   else
   1807     {
   1808       struct proc *thread = inf_port_to_thread (inf, dead_port);
   1809 
   1810       if (thread)
   1811 	{
   1812 	  proc_debug (thread, "is dead");
   1813 	  thread->port = MACH_PORT_NULL;
   1814 	}
   1815 
   1816       if (inf->task->dead)
   1817 	/* Since the task is dead, its threads are dying with it.  */
   1818 	inf->wait.suppress = 1;
   1819     }
   1820 
   1821   mach_port_deallocate (mach_task_self (), dead_port);
   1822   inf->threads_up_to_date = 0;	/* Just in case.  */
   1823 
   1824   return 0;
   1825 }
   1826 
   1827 
   1828 #define ILL_RPC(fun, ...) \
   1830   extern kern_return_t fun (__VA_ARGS__); \
   1831   kern_return_t fun (__VA_ARGS__) \
   1832   { \
   1833     warning (_("illegal rpc: %s"), #fun); \
   1834     return 0; \
   1835   }
   1836 
   1837 ILL_RPC (do_mach_notify_no_senders,
   1838 	 mach_port_t notify, mach_port_mscount_t count)
   1839 ILL_RPC (do_mach_notify_port_deleted,
   1840 	 mach_port_t notify, mach_port_t name)
   1841 ILL_RPC (do_mach_notify_msg_accepted,
   1842 	 mach_port_t notify, mach_port_t name)
   1843 ILL_RPC (do_mach_notify_port_destroyed,
   1844 	 mach_port_t notify, mach_port_t name)
   1845 ILL_RPC (do_mach_notify_send_once,
   1846 	 mach_port_t notify)
   1847 
   1848 /* Process_reply server routines.  We only use process_wait_reply.  */
   1850 
   1851 kern_return_t
   1852 S_proc_wait_reply (mach_port_t reply, kern_return_t err,
   1853 		   int status, int sigcode, rusage_t rusage, pid_t pid)
   1854 {
   1855   struct inf *inf = waiting_inf;
   1856 
   1857   inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
   1858 	     err ? safe_strerror (err) : "0", pid, status, sigcode);
   1859 
   1860   if (err && proc_wait_pid && (!inf->task || !inf->task->port))
   1861     /* Ack.  The task has died, but the task-died notification code didn't
   1862        tell anyone because it thought a more detailed reply from the
   1863        procserver was forthcoming.  However, we now learn that won't
   1864        happen...  So we have to act like the task just died, and this time,
   1865        tell the world.  */
   1866     inf_task_died_status (inf);
   1867 
   1868   if (--proc_waits_pending == 0)
   1869     /* PROC_WAIT_PID represents the most recent wait.  We will always get
   1870        replies in order because the proc server is single threaded.  */
   1871     proc_wait_pid = 0;
   1872 
   1873   inf_debug (inf, "waits pending now: %d", proc_waits_pending);
   1874 
   1875   if (err)
   1876     {
   1877       if (err != EINTR)
   1878 	{
   1879 	  warning (_("Can't wait for pid %d: %s"),
   1880 		   inf->pid, safe_strerror (err));
   1881 	  inf->no_wait = 1;
   1882 
   1883 	  /* Since we can't see the inferior's signals, don't trap them.  */
   1884 	  inf_set_traced (inf, 0);
   1885 	}
   1886     }
   1887   else if (pid == inf->pid)
   1888     {
   1889       store_waitstatus (&inf->wait.status, status);
   1890       if (inf->wait.status.kind == TARGET_WAITKIND_STOPPED)
   1891 	/* The process has sent us a signal, and stopped itself in a sane
   1892 	   state pending our actions.  */
   1893 	{
   1894 	  inf_debug (inf, "process has stopped itself");
   1895 	  inf->stopped = 1;
   1896 	}
   1897     }
   1898   else
   1899     inf->wait.suppress = 1;	/* Something odd happened.  Ignore.  */
   1900 
   1901   return 0;
   1902 }
   1903 
   1904 ILL_RPC (S_proc_setmsgport_reply,
   1905 	 mach_port_t reply_port, kern_return_t return_code,
   1906 	 mach_port_t oldmsgport)
   1907 ILL_RPC (S_proc_getmsgport_reply,
   1908 	 mach_port_t reply_port, kern_return_t return_code,
   1909 	 mach_port_t msgports, mach_msg_type_name_t msgportsPoly)
   1910 ILL_RPC (S_proc_pid2task_reply,
   1911 	 mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
   1912 ILL_RPC (S_proc_task2pid_reply,
   1913 	 mach_port_t reply_port, kern_return_t return_code, pid_t pid)
   1914 ILL_RPC (S_proc_task2proc_reply,
   1915 	 mach_port_t reply_port, kern_return_t return_code,
   1916 	 mach_port_t proc, mach_msg_type_name_t procPoly)
   1917 ILL_RPC (S_proc_proc2task_reply,
   1918 	 mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
   1919 ILL_RPC (S_proc_pid2proc_reply,
   1920 	 mach_port_t reply_port, kern_return_t return_code,
   1921 	 mach_port_t proc, mach_msg_type_name_t procPoly)
   1922 ILL_RPC (S_proc_getprocinfo_reply,
   1923 	 mach_port_t reply_port, kern_return_t return_code,
   1924 	 int flags, procinfo_t procinfo, mach_msg_type_number_t procinfoCnt,
   1925 	 data_t threadwaits, mach_msg_type_number_t threadwaitsCnt)
   1926 ILL_RPC (S_proc_getprocargs_reply,
   1927 	 mach_port_t reply_port, kern_return_t return_code,
   1928 	 data_t procargs, mach_msg_type_number_t procargsCnt)
   1929 ILL_RPC (S_proc_getprocenv_reply,
   1930 	 mach_port_t reply_port, kern_return_t return_code,
   1931 	 data_t procenv, mach_msg_type_number_t procenvCnt)
   1932 ILL_RPC (S_proc_getloginid_reply,
   1933 	 mach_port_t reply_port, kern_return_t return_code, pid_t login_id)
   1934 ILL_RPC (S_proc_getloginpids_reply,
   1935 	 mach_port_t reply_port, kern_return_t return_code,
   1936 	 pidarray_t pids, mach_msg_type_number_t pidsCnt)
   1937 ILL_RPC (S_proc_getlogin_reply,
   1938 	 mach_port_t reply_port, kern_return_t return_code, string_t logname)
   1939 ILL_RPC (S_proc_getsid_reply,
   1940 	 mach_port_t reply_port, kern_return_t return_code, pid_t sid)
   1941 ILL_RPC (S_proc_getsessionpgids_reply,
   1942 	 mach_port_t reply_port, kern_return_t return_code,
   1943 	 pidarray_t pgidset, mach_msg_type_number_t pgidsetCnt)
   1944 ILL_RPC (S_proc_getsessionpids_reply,
   1945 	 mach_port_t reply_port, kern_return_t return_code,
   1946 	 pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
   1947 ILL_RPC (S_proc_getsidport_reply,
   1948 	 mach_port_t reply_port, kern_return_t return_code,
   1949 	 mach_port_t sessport)
   1950 ILL_RPC (S_proc_getpgrp_reply,
   1951 	 mach_port_t reply_port, kern_return_t return_code, pid_t pgrp)
   1952 ILL_RPC (S_proc_getpgrppids_reply,
   1953 	 mach_port_t reply_port, kern_return_t return_code,
   1954 	 pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
   1955 ILL_RPC (S_proc_get_tty_reply,
   1956 	 mach_port_t reply_port, kern_return_t return_code, mach_port_t tty)
   1957 ILL_RPC (S_proc_getnports_reply,
   1958 	 mach_port_t reply_port, kern_return_t return_code,
   1959 	 mach_msg_type_number_t nports)
   1960 ILL_RPC (S_proc_is_important_reply,
   1961 	 mach_port_t reply_port, kern_return_t return_code,
   1962 	 boolean_t essential)
   1963 ILL_RPC (S_proc_get_code_reply,
   1964 	 mach_port_t reply_port, kern_return_t return_code,
   1965 	 vm_address_t start_code, vm_address_t end_code)
   1966 
   1967 /* Msg_reply server routines.  We only use msg_sig_post_untraced_reply.  */
   1969 
   1970 kern_return_t
   1971 S_msg_sig_post_untraced_reply (mach_port_t reply, kern_return_t err)
   1972 {
   1973   struct inf *inf = waiting_inf;
   1974 
   1975   if (err == EBUSY)
   1976     /* EBUSY is what we get when the crash server has grabbed control of the
   1977        process and doesn't like what signal we tried to send it.  Just act
   1978        like the process stopped (using a signal of 0 should mean that the
   1979        *next* time the user continues, it will pass signal 0, which the crash
   1980        server should like).  */
   1981     {
   1982       inf->wait.status.kind = TARGET_WAITKIND_STOPPED;
   1983       inf->wait.status.value.sig = GDB_SIGNAL_0;
   1984     }
   1985   else if (err)
   1986     warning (_("Signal delivery failed: %s"), safe_strerror (err));
   1987 
   1988   if (err)
   1989     /* We only get this reply when we've posted a signal to a process which we
   1990        thought was stopped, and which we expected to continue after the signal.
   1991        Given that the signal has failed for some reason, it's reasonable to
   1992        assume it's still stopped.  */
   1993     inf->stopped = 1;
   1994   else
   1995     inf->wait.suppress = 1;
   1996 
   1997   return 0;
   1998 }
   1999 
   2000 ILL_RPC (S_msg_sig_post_reply,
   2001 	 mach_port_t reply, kern_return_t err)
   2002 
   2003 /* Returns the number of messages queued for the receive right PORT.  */
   2005 static mach_port_msgcount_t
   2006 port_msgs_queued (mach_port_t port)
   2007 {
   2008   struct mach_port_status status;
   2009   kern_return_t err =
   2010     mach_port_get_receive_status (mach_task_self (), port, &status);
   2011 
   2012   if (err)
   2013     return 0;
   2014   else
   2015     return status.mps_msgcount;
   2016 }
   2017 
   2018 
   2019 /* Resume execution of the inferior process.
   2021 
   2022    If STEP is nonzero, single-step it.
   2023    If SIGNAL is nonzero, give it that signal.
   2024 
   2025    TID  STEP:
   2026    -1   true   Single step the current thread allowing other threads to run.
   2027    -1   false  Continue the current thread allowing other threads to run.
   2028    X    true   Single step the given thread, don't allow any others to run.
   2029    X    false  Continue the given thread, do not allow any others to run.
   2030    (Where X, of course, is anything except -1)
   2031 
   2032    Note that a resume may not `take' if there are pending exceptions/&c
   2033    still unprocessed from the last resume we did (any given resume may result
   2034    in multiple events returned by wait).  */
   2035 
   2036 void
   2037 gnu_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
   2038 {
   2039   struct proc *step_thread = 0;
   2040   int resume_all;
   2041   struct inf *inf = gnu_current_inf;
   2042 
   2043   inf_debug (inf, "ptid = %s, step = %d, sig = %d",
   2044 	     target_pid_to_str (ptid), step, sig);
   2045 
   2046   inf_validate_procinfo (inf);
   2047 
   2048   if (sig != GDB_SIGNAL_0 || inf->stopped)
   2049     {
   2050       if (sig == GDB_SIGNAL_0 && inf->nomsg)
   2051 	inf_continue (inf);
   2052       else
   2053 	inf_signal (inf, sig);
   2054     }
   2055   else if (inf->wait.exc.reply != MACH_PORT_NULL)
   2056     /* We received an exception to which we have chosen not to forward, so
   2057        abort the faulting thread, which will perhaps retake it.  */
   2058     {
   2059       proc_abort (inf->wait.thread, 1);
   2060       warning (_("Aborting %s with unforwarded exception %s."),
   2061 	       proc_string (inf->wait.thread),
   2062 	       gdb_signal_to_name (inf->wait.status.value.sig));
   2063     }
   2064 
   2065   if (port_msgs_queued (inf->event_port))
   2066     /* If there are still messages in our event queue, don't bother resuming
   2067        the process, as we're just going to stop it right away anyway.  */
   2068     return;
   2069 
   2070   inf_update_procs (inf);
   2071 
   2072   /* A specific PTID means `step only this process id'.  */
   2073   resume_all = ptid == minus_one_ptid;
   2074 
   2075   if (resume_all)
   2076     /* Allow all threads to run, except perhaps single-stepping one.  */
   2077     {
   2078       inf_debug (inf, "running all threads; tid = %d",
   2079 		 inferior_ptid.pid ());
   2080       ptid = inferior_ptid;	/* What to step.  */
   2081       inf_set_threads_resume_sc (inf, 0, 1);
   2082     }
   2083   else
   2084     /* Just allow a single thread to run.  */
   2085     {
   2086       struct proc *thread = inf_tid_to_thread (inf, ptid.lwp ());
   2087 
   2088       if (!thread)
   2089 	error (_("Can't run single thread id %s: no such thread!"),
   2090 	       target_pid_to_str (ptid));
   2091       inf_debug (inf, "running one thread: %s", target_pid_to_str (ptid));
   2092       inf_set_threads_resume_sc (inf, thread, 0);
   2093     }
   2094 
   2095   if (step)
   2096     {
   2097       step_thread = inf_tid_to_thread (inf, ptid.lwp ());
   2098       if (!step_thread)
   2099 	warning (_("Can't step thread id %s: no such thread."),
   2100 		 target_pid_to_str (ptid));
   2101       else
   2102 	inf_debug (inf, "stepping thread: %s", target_pid_to_str (ptid));
   2103     }
   2104   if (step_thread != inf->step_thread)
   2105     inf_set_step_thread (inf, step_thread);
   2106 
   2107   inf_debug (inf, "here we go...");
   2108   inf_resume (inf);
   2109 }
   2110 
   2111 
   2112 void
   2114 gnu_nat_target::kill ()
   2115 {
   2116   struct proc *task = gnu_current_inf->task;
   2117 
   2118   if (task)
   2119     {
   2120       proc_debug (task, "terminating...");
   2121       task_terminate (task->port);
   2122       inf_set_pid (gnu_current_inf, -1);
   2123     }
   2124   target_mourn_inferior (inferior_ptid);
   2125 }
   2126 
   2127 /* Clean up after the inferior dies.  */
   2128 void
   2129 gnu_nat_target::mourn_inferior ()
   2130 {
   2131   inf_debug (gnu_current_inf, "rip");
   2132   inf_detach (gnu_current_inf);
   2133   inf_child_target::mourn_inferior ();
   2134 }
   2135 
   2136 
   2137 /* Fork an inferior process, and start debugging it.  */
   2139 
   2140 /* Set INFERIOR_PID to the first thread available in the child, if any.  */
   2141 static int
   2142 inf_pick_first_thread (void)
   2143 {
   2144   if (gnu_current_inf->task && gnu_current_inf->threads)
   2145     /* The first thread.  */
   2146     return gnu_current_inf->threads->tid;
   2147   else
   2148     /* What may be the next thread.  */
   2149     return next_thread_id;
   2150 }
   2151 
   2152 static struct inf *
   2153 cur_inf (void)
   2154 {
   2155   if (!gnu_current_inf)
   2156     gnu_current_inf = make_inf ();
   2157   return gnu_current_inf;
   2158 }
   2159 
   2160 static void
   2161 gnu_ptrace_me (void)
   2162 {
   2163   /* We're in the child; make this process stop as soon as it execs.  */
   2164   struct inf *inf = cur_inf ();
   2165   inf_debug (inf, "tracing self");
   2166   if (ptrace (PTRACE_TRACEME) != 0)
   2167     trace_start_error_with_name ("ptrace");
   2168 }
   2169 
   2170 void
   2171 gnu_nat_target::create_inferior (const char *exec_file,
   2172 				 const std::string &allargs,
   2173 				 char **env,
   2174 				 int from_tty)
   2175 {
   2176   struct inf *inf = cur_inf ();
   2177   int pid;
   2178 
   2179   inf_debug (inf, "creating inferior");
   2180 
   2181   pid = fork_inferior (exec_file, allargs, env, gnu_ptrace_me,
   2182                        NULL, NULL, NULL, NULL);
   2183 
   2184   /* We have something that executes now.  We'll be running through
   2185      the shell at this point (if startup-with-shell is true), but the
   2186      pid shouldn't change.  */
   2187   add_thread_silent (ptid_t (pid));
   2188 
   2189   /* Attach to the now stopped child, which is actually a shell...  */
   2190   inf_debug (inf, "attaching to child: %d", pid);
   2191 
   2192   inf_attach (inf, pid);
   2193 
   2194   push_target (this);
   2195 
   2196   inf->pending_execs = 1;
   2197   inf->nomsg = 1;
   2198   inf->traced = 1;
   2199 
   2200   /* Now let the child run again, knowing that it will stop
   2201      immediately because of the ptrace.  */
   2202   inf_resume (inf);
   2203 
   2204   /* We now have thread info.  */
   2205   thread_change_ptid (inferior_ptid,
   2206 		      ptid_t (inf->pid, inf_pick_first_thread (), 0));
   2207 
   2208   gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
   2209 
   2210   inf->pending_execs = 0;
   2211   /* Get rid of the old shell threads.  */
   2212   prune_threads ();
   2213 
   2214   inf_validate_procinfo (inf);
   2215   inf_update_signal_thread (inf);
   2216   inf_set_traced (inf, inf->want_signals);
   2217 
   2218   /* Execing the process will have trashed our exception ports; steal them
   2219      back (or make sure they're restored if the user wants that).  */
   2220   if (inf->want_exceptions)
   2221     inf_steal_exc_ports (inf);
   2222   else
   2223     inf_restore_exc_ports (inf);
   2224 }
   2225 
   2226 
   2227 /* Attach to process PID, then initialize for debugging it
   2229    and wait for the trace-trap that results from attaching.  */
   2230 void
   2231 gnu_nat_target::attach (const char *args, int from_tty)
   2232 {
   2233   int pid;
   2234   char *exec_file;
   2235   struct inf *inf = cur_inf ();
   2236   struct inferior *inferior;
   2237 
   2238   pid = parse_pid_to_attach (args);
   2239 
   2240   if (pid == getpid ())		/* Trying to masturbate?  */
   2241     error (_("I refuse to debug myself!"));
   2242 
   2243   if (from_tty)
   2244     {
   2245       exec_file = (char *) get_exec_file (0);
   2246 
   2247       if (exec_file)
   2248 	printf_unfiltered ("Attaching to program `%s', pid %d\n",
   2249 			   exec_file, pid);
   2250       else
   2251 	printf_unfiltered ("Attaching to pid %d\n", pid);
   2252 
   2253       gdb_flush (gdb_stdout);
   2254     }
   2255 
   2256   inf_debug (inf, "attaching to pid: %d", pid);
   2257 
   2258   inf_attach (inf, pid);
   2259 
   2260   push_target (this);
   2261 
   2262   inferior = current_inferior ();
   2263   inferior_appeared (inferior, pid);
   2264   inferior->attach_flag = 1;
   2265 
   2266   inf_update_procs (inf);
   2267 
   2268   inferior_ptid = ptid_t (pid, inf_pick_first_thread (), 0);
   2269 
   2270   /* We have to initialize the terminal settings now, since the code
   2271      below might try to restore them.  */
   2272   target_terminal::init ();
   2273 
   2274   /* If the process was stopped before we attached, make it continue the next
   2275      time the user does a continue.  */
   2276   inf_validate_procinfo (inf);
   2277 
   2278   inf_update_signal_thread (inf);
   2279   inf_set_traced (inf, inf->want_signals);
   2280 
   2281 #if 0				/* Do we need this?  */
   2282   renumber_threads (0);		/* Give our threads reasonable names.  */
   2283 #endif
   2284 }
   2285 
   2286 
   2287 /* Take a program previously attached to and detaches it.
   2289    The program resumes execution and will no longer stop
   2290    on signals, etc.  We'd better not have left any breakpoints
   2291    in the program or it'll die when it hits one.  For this
   2292    to work, it may be necessary for the process to have been
   2293    previously attached.  It *might* work if the program was
   2294    started via fork.  */
   2295 void
   2296 gnu_nat_target::detach (inferior *inf, int from_tty)
   2297 {
   2298   int pid;
   2299 
   2300   if (from_tty)
   2301     {
   2302       char *exec_file = get_exec_file (0);
   2303 
   2304       if (exec_file)
   2305 	printf_unfiltered ("Detaching from program `%s' pid %d\n",
   2306 			   exec_file, gnu_current_inf->pid);
   2307       else
   2308 	printf_unfiltered ("Detaching from pid %d\n", gnu_current_inf->pid);
   2309       gdb_flush (gdb_stdout);
   2310     }
   2311 
   2312   pid = gnu_current_inf->pid;
   2313 
   2314   inf_detach (gnu_current_inf);
   2315 
   2316   inferior_ptid = null_ptid;
   2317   detach_inferior (find_inferior_pid (pid));
   2318 
   2319   maybe_unpush_target ();
   2320 }
   2321 
   2322 
   2324 void
   2325 gnu_nat_target::stop (ptid_t ptid)
   2326 {
   2327   error (_("stop target function not implemented"));
   2328 }
   2329 
   2330 bool
   2331 gnu_nat_target::thread_alive (ptid_t ptid)
   2332 {
   2333   inf_update_procs (gnu_current_inf);
   2334   return !!inf_tid_to_thread (gnu_current_inf,
   2335 			      ptid.lwp ());
   2336 }
   2337 
   2338 
   2339 /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
   2341    gdb's address space.  Return 0 on failure; number of bytes read
   2342    otherwise.  */
   2343 static int
   2344 gnu_read_inferior (task_t task, CORE_ADDR addr, gdb_byte *myaddr, int length)
   2345 {
   2346   kern_return_t err;
   2347   vm_address_t low_address = (vm_address_t) trunc_page (addr);
   2348   vm_size_t aligned_length =
   2349   (vm_size_t) round_page (addr + length) - low_address;
   2350   pointer_t copied;
   2351   mach_msg_type_number_t copy_count;
   2352 
   2353   /* Get memory from inferior with page aligned addresses.  */
   2354   err = vm_read (task, low_address, aligned_length, &copied, &copy_count);
   2355   if (err)
   2356     return 0;
   2357 
   2358   err = hurd_safe_copyin (myaddr, (void *) (addr - low_address + copied),
   2359 			  length);
   2360   if (err)
   2361     {
   2362       warning (_("Read from inferior faulted: %s"), safe_strerror (err));
   2363       length = 0;
   2364     }
   2365 
   2366   err = vm_deallocate (mach_task_self (), copied, copy_count);
   2367   if (err)
   2368     warning (_("gnu_read_inferior vm_deallocate failed: %s"),
   2369 	     safe_strerror (err));
   2370 
   2371   return length;
   2372 }
   2373 
   2374 #define CHK_GOTO_OUT(str,ret) \
   2375   do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
   2376 
   2377 struct vm_region_list
   2378 {
   2379   struct vm_region_list *next;
   2380   vm_prot_t protection;
   2381   vm_address_t start;
   2382   vm_size_t length;
   2383 };
   2384 
   2385 struct obstack region_obstack;
   2386 
   2387 /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
   2388    task's address space.  */
   2389 static int
   2390 gnu_write_inferior (task_t task, CORE_ADDR addr,
   2391 		    const gdb_byte *myaddr, int length)
   2392 {
   2393   kern_return_t err;
   2394   vm_address_t low_address = (vm_address_t) trunc_page (addr);
   2395   vm_size_t aligned_length =
   2396   (vm_size_t) round_page (addr + length) - low_address;
   2397   pointer_t copied;
   2398   mach_msg_type_number_t copy_count;
   2399   int deallocate = 0;
   2400 
   2401   const char *errstr = "Bug in gnu_write_inferior";
   2402 
   2403   struct vm_region_list *region_element;
   2404   struct vm_region_list *region_head = NULL;
   2405 
   2406   /* Get memory from inferior with page aligned addresses.  */
   2407   err = vm_read (task,
   2408 		 low_address,
   2409 		 aligned_length,
   2410 		 &copied,
   2411 		 &copy_count);
   2412   CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
   2413 
   2414   deallocate++;
   2415 
   2416   err = hurd_safe_copyout ((void *) (addr - low_address + copied),
   2417 			   myaddr, length);
   2418   CHK_GOTO_OUT ("Write to inferior faulted", err);
   2419 
   2420   obstack_init (&region_obstack);
   2421 
   2422   /* Do writes atomically.
   2423      First check for holes and unwritable memory.  */
   2424   {
   2425     vm_size_t remaining_length = aligned_length;
   2426     vm_address_t region_address = low_address;
   2427 
   2428     struct vm_region_list *scan;
   2429 
   2430     while (region_address < low_address + aligned_length)
   2431       {
   2432 	vm_prot_t protection;
   2433 	vm_prot_t max_protection;
   2434 	vm_inherit_t inheritance;
   2435 	boolean_t shared;
   2436 	mach_port_t object_name;
   2437 	vm_offset_t offset;
   2438 	vm_size_t region_length = remaining_length;
   2439 	vm_address_t old_address = region_address;
   2440 
   2441 	err = vm_region (task,
   2442 			 &region_address,
   2443 			 &region_length,
   2444 			 &protection,
   2445 			 &max_protection,
   2446 			 &inheritance,
   2447 			 &shared,
   2448 			 &object_name,
   2449 			 &offset);
   2450 	CHK_GOTO_OUT ("vm_region failed", err);
   2451 
   2452 	/* Check for holes in memory.  */
   2453 	if (old_address != region_address)
   2454 	  {
   2455 	    warning (_("No memory at 0x%lx. Nothing written"),
   2456 		     old_address);
   2457 	    err = KERN_SUCCESS;
   2458 	    length = 0;
   2459 	    goto out;
   2460 	  }
   2461 
   2462 	if (!(max_protection & VM_PROT_WRITE))
   2463 	  {
   2464 	    warning (_("Memory at address 0x%lx is unwritable. "
   2465 		       "Nothing written"),
   2466 		     old_address);
   2467 	    err = KERN_SUCCESS;
   2468 	    length = 0;
   2469 	    goto out;
   2470 	  }
   2471 
   2472 	/* Chain the regions for later use.  */
   2473 	region_element = XOBNEW (&region_obstack, struct vm_region_list);
   2474 
   2475 	region_element->protection = protection;
   2476 	region_element->start = region_address;
   2477 	region_element->length = region_length;
   2478 
   2479 	/* Chain the regions along with protections.  */
   2480 	region_element->next = region_head;
   2481 	region_head = region_element;
   2482 
   2483 	region_address += region_length;
   2484 	remaining_length = remaining_length - region_length;
   2485       }
   2486 
   2487     /* If things fail after this, we give up.
   2488        Somebody is messing up inferior_task's mappings.  */
   2489 
   2490     /* Enable writes to the chained vm regions.  */
   2491     for (scan = region_head; scan; scan = scan->next)
   2492       {
   2493 	if (!(scan->protection & VM_PROT_WRITE))
   2494 	  {
   2495 	    err = vm_protect (task,
   2496 			      scan->start,
   2497 			      scan->length,
   2498 			      FALSE,
   2499 			      scan->protection | VM_PROT_WRITE);
   2500 	    CHK_GOTO_OUT ("vm_protect: enable write failed", err);
   2501 	  }
   2502       }
   2503 
   2504     err = vm_write (task,
   2505 		    low_address,
   2506 		    copied,
   2507 		    aligned_length);
   2508     CHK_GOTO_OUT ("vm_write failed", err);
   2509 
   2510     /* Set up the original region protections, if they were changed.  */
   2511     for (scan = region_head; scan; scan = scan->next)
   2512       {
   2513 	if (!(scan->protection & VM_PROT_WRITE))
   2514 	  {
   2515 	    err = vm_protect (task,
   2516 			      scan->start,
   2517 			      scan->length,
   2518 			      FALSE,
   2519 			      scan->protection);
   2520 	    CHK_GOTO_OUT ("vm_protect: enable write failed", err);
   2521 	  }
   2522       }
   2523   }
   2524 
   2525 out:
   2526   if (deallocate)
   2527     {
   2528       obstack_free (&region_obstack, 0);
   2529 
   2530       (void) vm_deallocate (mach_task_self (),
   2531 			    copied,
   2532 			    copy_count);
   2533     }
   2534 
   2535   if (err != KERN_SUCCESS)
   2536     {
   2537       warning (_("%s: %s"), errstr, mach_error_string (err));
   2538       return 0;
   2539     }
   2540 
   2541   return length;
   2542 }
   2543 
   2544 
   2545 
   2547 /* Implement the to_xfer_partial target_ops method for
   2548    TARGET_OBJECT_MEMORY.  */
   2549 
   2550 static enum target_xfer_status
   2551 gnu_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
   2552 		 CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
   2553 {
   2554   task_t task = (gnu_current_inf
   2555 		 ? (gnu_current_inf->task
   2556 		    ? gnu_current_inf->task->port : 0)
   2557 		 : 0);
   2558   int res;
   2559 
   2560   if (task == MACH_PORT_NULL)
   2561     return TARGET_XFER_E_IO;
   2562 
   2563   if (writebuf != NULL)
   2564     {
   2565       inf_debug (gnu_current_inf, "writing %s[%s] <-- %s",
   2566 		 paddress (target_gdbarch (), memaddr), pulongest (len),
   2567 		 host_address_to_string (writebuf));
   2568       res = gnu_write_inferior (task, memaddr, writebuf, len);
   2569     }
   2570   else
   2571     {
   2572       inf_debug (gnu_current_inf, "reading %s[%s] --> %s",
   2573 		 paddress (target_gdbarch (), memaddr), pulongest (len),
   2574 		 host_address_to_string (readbuf));
   2575       res = gnu_read_inferior (task, memaddr, readbuf, len);
   2576     }
   2577   gdb_assert (res >= 0);
   2578   if (res == 0)
   2579     return TARGET_XFER_E_IO;
   2580   else
   2581     {
   2582       *xfered_len = (ULONGEST) res;
   2583       return TARGET_XFER_OK;
   2584     }
   2585 }
   2586 
   2587 /* GNU does not have auxv, but we can at least fake the AT_ENTRY entry for PIE
   2588    binaries.  */
   2589 static enum target_xfer_status
   2590 gnu_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf,
   2591 	       CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
   2592 {
   2593   task_t task = (gnu_current_inf
   2594 		 ? (gnu_current_inf->task
   2595 		    ? gnu_current_inf->task->port : 0)
   2596 		 : 0);
   2597   process_t proc;
   2598   int res;
   2599   kern_return_t err;
   2600   vm_address_t entry;
   2601   ElfW(auxv_t) auxv[2];
   2602 
   2603   if (task == MACH_PORT_NULL)
   2604     return TARGET_XFER_E_IO;
   2605   if (writebuf != NULL)
   2606     return TARGET_XFER_E_IO;
   2607 
   2608   if (memaddr == sizeof (auxv))
   2609     return TARGET_XFER_EOF;
   2610   if (memaddr > sizeof (auxv))
   2611     return TARGET_XFER_E_IO;
   2612 
   2613   err = proc_task2proc (proc_server, task, &proc);
   2614   if (err != 0)
   2615     return TARGET_XFER_E_IO;
   2616 
   2617   /* Get entry from proc server.  */
   2618   err = proc_get_entry (proc, &entry);
   2619   if (err != 0)
   2620     return TARGET_XFER_E_IO;
   2621 
   2622   /* Fake auxv entry.  */
   2623   auxv[0].a_type = AT_ENTRY;
   2624   auxv[0].a_un.a_val = entry;
   2625   auxv[1].a_type = AT_NULL;
   2626   auxv[1].a_un.a_val = 0;
   2627 
   2628   inf_debug (gnu_current_inf, "reading auxv %s[%s] --> %s",
   2629 	     paddress (target_gdbarch (), memaddr), pulongest (len),
   2630 	     host_address_to_string (readbuf));
   2631 
   2632   if (memaddr + len > sizeof (auxv))
   2633     len = sizeof (auxv) - memaddr;
   2634 
   2635   memcpy (readbuf, (gdb_byte *) &auxv + memaddr, len);
   2636   *xfered_len = len;
   2637 
   2638   return TARGET_XFER_OK;
   2639 }
   2640 
   2641 /* Target to_xfer_partial implementation.  */
   2642 
   2643 enum target_xfer_status
   2644 gnu_nat_target::xfer_partial (enum target_object object,
   2645 			      const char *annex, gdb_byte *readbuf,
   2646 			      const gdb_byte *writebuf, ULONGEST offset,
   2647 			      ULONGEST len, ULONGEST *xfered_len)
   2648 {
   2649   switch (object)
   2650     {
   2651     case TARGET_OBJECT_MEMORY:
   2652       return gnu_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
   2653     case TARGET_OBJECT_AUXV:
   2654       return gnu_xfer_auxv (readbuf, writebuf, offset, len, xfered_len);
   2655     default:
   2656       return TARGET_XFER_E_IO;
   2657     }
   2658 }
   2659 
   2660 /* Call FUNC on each memory region in the task.  */
   2661 
   2662 int
   2663 gnu_nat_target::find_memory_regions (find_memory_region_ftype func,
   2664 				     void *data)
   2665 {
   2666   kern_return_t err;
   2667   task_t task;
   2668   vm_address_t region_address, last_region_address, last_region_end;
   2669   vm_prot_t last_protection;
   2670 
   2671   if (gnu_current_inf == 0 || gnu_current_inf->task == 0)
   2672     return 0;
   2673   task = gnu_current_inf->task->port;
   2674   if (task == MACH_PORT_NULL)
   2675     return 0;
   2676 
   2677   region_address = last_region_address = last_region_end = VM_MIN_ADDRESS;
   2678   last_protection = VM_PROT_NONE;
   2679   while (region_address < VM_MAX_ADDRESS)
   2680     {
   2681       vm_prot_t protection;
   2682       vm_prot_t max_protection;
   2683       vm_inherit_t inheritance;
   2684       boolean_t shared;
   2685       mach_port_t object_name;
   2686       vm_offset_t offset;
   2687       vm_size_t region_length = VM_MAX_ADDRESS - region_address;
   2688       vm_address_t old_address = region_address;
   2689 
   2690       err = vm_region (task,
   2691 		       &region_address,
   2692 		       &region_length,
   2693 		       &protection,
   2694 		       &max_protection,
   2695 		       &inheritance,
   2696 		       &shared,
   2697 		       &object_name,
   2698 		       &offset);
   2699       if (err == KERN_NO_SPACE)
   2700 	break;
   2701       if (err != KERN_SUCCESS)
   2702 	{
   2703 	  warning (_("vm_region failed: %s"), mach_error_string (err));
   2704 	  return -1;
   2705 	}
   2706 
   2707       if (protection == last_protection && region_address == last_region_end)
   2708 	/* This region is contiguous with and indistinguishable from
   2709 	   the previous one, so we just extend that one.  */
   2710 	last_region_end = region_address += region_length;
   2711       else
   2712 	{
   2713 	  /* This region is distinct from the last one we saw, so report
   2714 	     that previous one.  */
   2715 	  if (last_protection != VM_PROT_NONE)
   2716 	    (*func) (last_region_address,
   2717 		     last_region_end - last_region_address,
   2718 		     last_protection & VM_PROT_READ,
   2719 		     last_protection & VM_PROT_WRITE,
   2720 		     last_protection & VM_PROT_EXECUTE,
   2721 		     1, /* MODIFIED is unknown, pass it as true.  */
   2722 		     data);
   2723 	  last_region_address = region_address;
   2724 	  last_region_end = region_address += region_length;
   2725 	  last_protection = protection;
   2726 	}
   2727     }
   2728 
   2729   /* Report the final region.  */
   2730   if (last_region_end > last_region_address && last_protection != VM_PROT_NONE)
   2731     (*func) (last_region_address, last_region_end - last_region_address,
   2732 	     last_protection & VM_PROT_READ,
   2733 	     last_protection & VM_PROT_WRITE,
   2734 	     last_protection & VM_PROT_EXECUTE,
   2735 	     1, /* MODIFIED is unknown, pass it as true.  */
   2736 	     data);
   2737 
   2738   return 0;
   2739 }
   2740 
   2741 
   2742 /* Return printable description of proc.  */
   2744 char *
   2745 proc_string (struct proc *proc)
   2746 {
   2747   static char tid_str[80];
   2748 
   2749   if (proc_is_task (proc))
   2750     xsnprintf (tid_str, sizeof (tid_str), "process %d", proc->inf->pid);
   2751   else
   2752     xsnprintf (tid_str, sizeof (tid_str), "Thread %d.%d",
   2753 	       proc->inf->pid, proc->tid);
   2754   return tid_str;
   2755 }
   2756 
   2757 const char *
   2758 gnu_nat_target::pid_to_str (ptid_t ptid)
   2759 {
   2760   struct inf *inf = gnu_current_inf;
   2761   int tid = ptid.lwp ();
   2762   struct proc *thread = inf_tid_to_thread (inf, tid);
   2763 
   2764   if (thread)
   2765     return proc_string (thread);
   2766   else
   2767     {
   2768       static char tid_str[80];
   2769 
   2770       xsnprintf (tid_str, sizeof (tid_str), "bogus thread id %d", tid);
   2771       return tid_str;
   2772     }
   2773 }
   2774 
   2775 
   2776 /* User task commands.  */
   2778 
   2779 static struct cmd_list_element *set_task_cmd_list = 0;
   2780 static struct cmd_list_element *show_task_cmd_list = 0;
   2781 /* User thread commands.  */
   2782 
   2783 /* Commands with a prefix of `set/show thread'.  */
   2784 extern struct cmd_list_element *thread_cmd_list;
   2785 struct cmd_list_element *set_thread_cmd_list = NULL;
   2786 struct cmd_list_element *show_thread_cmd_list = NULL;
   2787 
   2788 /* Commands with a prefix of `set/show thread default'.  */
   2789 struct cmd_list_element *set_thread_default_cmd_list = NULL;
   2790 struct cmd_list_element *show_thread_default_cmd_list = NULL;
   2791 
   2792 static void
   2793 set_thread_cmd (const char *args, int from_tty)
   2794 {
   2795   printf_unfiltered ("\"set thread\" must be followed by the "
   2796 		     "name of a thread property, or \"default\".\n");
   2797 }
   2798 
   2799 static void
   2800 show_thread_cmd (const char *args, int from_tty)
   2801 {
   2802   printf_unfiltered ("\"show thread\" must be followed by the "
   2803 		     "name of a thread property, or \"default\".\n");
   2804 }
   2805 
   2806 static void
   2807 set_thread_default_cmd (const char *args, int from_tty)
   2808 {
   2809   printf_unfiltered ("\"set thread default\" must be followed "
   2810 		     "by the name of a thread property.\n");
   2811 }
   2812 
   2813 static void
   2814 show_thread_default_cmd (const char *args, int from_tty)
   2815 {
   2816   printf_unfiltered ("\"show thread default\" must be followed "
   2817 		     "by the name of a thread property.\n");
   2818 }
   2819 
   2820 static int
   2821 parse_int_arg (const char *args, const char *cmd_prefix)
   2822 {
   2823   if (args)
   2824     {
   2825       char *arg_end;
   2826       int val = strtoul (args, &arg_end, 10);
   2827 
   2828       if (*args && *arg_end == '\0')
   2829 	return val;
   2830     }
   2831   error (_("Illegal argument for \"%s\" command, should be an integer."),
   2832 	 cmd_prefix);
   2833 }
   2834 
   2835 static int
   2836 _parse_bool_arg (const char *args, const char *t_val, const char *f_val,
   2837 		 const char *cmd_prefix)
   2838 {
   2839   if (!args || strcmp (args, t_val) == 0)
   2840     return 1;
   2841   else if (strcmp (args, f_val) == 0)
   2842     return 0;
   2843   else
   2844     error (_("Illegal argument for \"%s\" command, "
   2845 	     "should be \"%s\" or \"%s\"."),
   2846 	   cmd_prefix, t_val, f_val);
   2847 }
   2848 
   2849 #define parse_bool_arg(args, cmd_prefix) \
   2850   _parse_bool_arg (args, "on", "off", cmd_prefix)
   2851 
   2852 static void
   2853 check_empty (const char *args, const char *cmd_prefix)
   2854 {
   2855   if (args)
   2856     error (_("Garbage after \"%s\" command: `%s'"), cmd_prefix, args);
   2857 }
   2858 
   2859 /* Returns the alive thread named by INFERIOR_PID, or signals an error.  */
   2860 static struct proc *
   2861 cur_thread (void)
   2862 {
   2863   struct inf *inf = cur_inf ();
   2864   struct proc *thread = inf_tid_to_thread (inf,
   2865 					   inferior_ptid.lwp ());
   2866   if (!thread)
   2867     error (_("No current thread."));
   2868   return thread;
   2869 }
   2870 
   2871 /* Returns the current inferior, but signals an error if it has no task.  */
   2872 static struct inf *
   2873 active_inf (void)
   2874 {
   2875   struct inf *inf = cur_inf ();
   2876 
   2877   if (!inf->task)
   2878     error (_("No current process."));
   2879   return inf;
   2880 }
   2881 
   2882 
   2883 static void
   2885 set_task_pause_cmd (int arg, int from_tty)
   2886 {
   2887   struct inf *inf = cur_inf ();
   2888   int old_sc = inf->pause_sc;
   2889 
   2890   inf->pause_sc = arg;
   2891 
   2892   if (old_sc == 0 && inf->pause_sc != 0)
   2893     /* If the task is currently unsuspended, immediately suspend it,
   2894        otherwise wait until the next time it gets control.  */
   2895     inf_suspend (inf);
   2896 }
   2897 
   2898 static void
   2899 set_task_pause_cmd (const char *args, int from_tty)
   2900 {
   2901   set_task_pause_cmd (parse_bool_arg (args, "set task pause"), from_tty);
   2902 }
   2903 
   2904 static void
   2905 show_task_pause_cmd (const char *args, int from_tty)
   2906 {
   2907   struct inf *inf = cur_inf ();
   2908 
   2909   check_empty (args, "show task pause");
   2910   printf_unfiltered ("The inferior task %s suspended while gdb has control.\n",
   2911 		     inf->task
   2912 		     ? (inf->pause_sc == 0 ? "isn't" : "is")
   2913 		     : (inf->pause_sc == 0 ? "won't be" : "will be"));
   2914 }
   2915 
   2916 static void
   2917 set_task_detach_sc_cmd (const char *args, int from_tty)
   2918 {
   2919   cur_inf ()->detach_sc = parse_int_arg (args,
   2920 					 "set task detach-suspend-count");
   2921 }
   2922 
   2923 static void
   2924 show_task_detach_sc_cmd (const char *args, int from_tty)
   2925 {
   2926   check_empty (args, "show task detach-suspend-count");
   2927   printf_unfiltered ("The inferior task will be left with a "
   2928 		     "suspend count of %d when detaching.\n",
   2929 		     cur_inf ()->detach_sc);
   2930 }
   2931 
   2932 
   2933 static void
   2935 set_thread_default_pause_cmd (const char *args, int from_tty)
   2936 {
   2937   struct inf *inf = cur_inf ();
   2938 
   2939   inf->default_thread_pause_sc =
   2940     parse_bool_arg (args, "set thread default pause") ? 0 : 1;
   2941 }
   2942 
   2943 static void
   2944 show_thread_default_pause_cmd (const char *args, int from_tty)
   2945 {
   2946   struct inf *inf = cur_inf ();
   2947   int sc = inf->default_thread_pause_sc;
   2948 
   2949   check_empty (args, "show thread default pause");
   2950   printf_unfiltered ("New threads %s suspended while gdb has control%s.\n",
   2951 		     sc ? "are" : "aren't",
   2952 		     !sc && inf->pause_sc ? " (but the task is)" : "");
   2953 }
   2954 
   2955 static void
   2956 set_thread_default_run_cmd (const char *args, int from_tty)
   2957 {
   2958   struct inf *inf = cur_inf ();
   2959 
   2960   inf->default_thread_run_sc =
   2961     parse_bool_arg (args, "set thread default run") ? 0 : 1;
   2962 }
   2963 
   2964 static void
   2965 show_thread_default_run_cmd (const char *args, int from_tty)
   2966 {
   2967   struct inf *inf = cur_inf ();
   2968 
   2969   check_empty (args, "show thread default run");
   2970   printf_unfiltered ("New threads %s allowed to run.\n",
   2971 		     inf->default_thread_run_sc == 0 ? "are" : "aren't");
   2972 }
   2973 
   2974 static void
   2975 set_thread_default_detach_sc_cmd (const char *args, int from_tty)
   2976 {
   2977   cur_inf ()->default_thread_detach_sc =
   2978     parse_int_arg (args, "set thread default detach-suspend-count");
   2979 }
   2980 
   2981 static void
   2982 show_thread_default_detach_sc_cmd (const char *args, int from_tty)
   2983 {
   2984   check_empty (args, "show thread default detach-suspend-count");
   2985   printf_unfiltered ("New threads will get a detach-suspend-count of %d.\n",
   2986 		     cur_inf ()->default_thread_detach_sc);
   2987 }
   2988 
   2989 
   2990 /* Steal a send right called NAME in the inferior task, and make it PROC's
   2992    saved exception port.  */
   2993 static void
   2994 steal_exc_port (struct proc *proc, mach_port_t name)
   2995 {
   2996   kern_return_t err;
   2997   mach_port_t port;
   2998   mach_msg_type_name_t port_type;
   2999 
   3000   if (!proc || !proc->inf->task)
   3001     error (_("No inferior task."));
   3002 
   3003   err = mach_port_extract_right (proc->inf->task->port,
   3004 				 name, MACH_MSG_TYPE_COPY_SEND,
   3005 				 &port, &port_type);
   3006   if (err)
   3007     error (_("Couldn't extract send right %lu from inferior: %s"),
   3008 	   name, safe_strerror (err));
   3009 
   3010   if (proc->saved_exc_port)
   3011     /* Get rid of our reference to the old one.  */
   3012     mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
   3013 
   3014   proc->saved_exc_port = port;
   3015 
   3016   if (!proc->exc_port)
   3017     /* If PROC is a thread, we may not have set its exception port
   3018        before.  We can't use proc_steal_exc_port because it also sets
   3019        saved_exc_port.  */
   3020     {
   3021       proc->exc_port = proc->inf->event_port;
   3022       err = proc_set_exception_port (proc, proc->exc_port);
   3023       error (_("Can't set exception port for %s: %s"),
   3024 	     proc_string (proc), safe_strerror (err));
   3025     }
   3026 }
   3027 
   3028 static void
   3029 set_task_exc_port_cmd (const char *args, int from_tty)
   3030 {
   3031   struct inf *inf = cur_inf ();
   3032 
   3033   if (!args)
   3034     error (_("No argument to \"set task exception-port\" command."));
   3035   steal_exc_port (inf->task, parse_and_eval_address (args));
   3036 }
   3037 
   3038 static void
   3039 set_stopped_cmd (const char *args, int from_tty)
   3040 {
   3041   cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
   3042 }
   3043 
   3044 static void
   3045 show_stopped_cmd (const char *args, int from_tty)
   3046 {
   3047   struct inf *inf = active_inf ();
   3048 
   3049   check_empty (args, "show stopped");
   3050   printf_unfiltered ("The inferior process %s stopped.\n",
   3051 		     inf->stopped ? "is" : "isn't");
   3052 }
   3053 
   3054 static void
   3055 set_sig_thread_cmd (const char *args, int from_tty)
   3056 {
   3057   struct inf *inf = cur_inf ();
   3058 
   3059   if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
   3060     error (_("Illegal argument to \"set signal-thread\" command.\n"
   3061 	     "Should be a thread ID, or \"none\"."));
   3062 
   3063   if (strcmp (args, "none") == 0)
   3064     inf->signal_thread = 0;
   3065   else
   3066     {
   3067       struct thread_info *tp = parse_thread_id (args, NULL);
   3068       inf->signal_thread = inf_tid_to_thread (inf, tp->ptid.lwp ());
   3069     }
   3070 }
   3071 
   3072 static void
   3073 show_sig_thread_cmd (const char *args, int from_tty)
   3074 {
   3075   struct inf *inf = active_inf ();
   3076 
   3077   check_empty (args, "show signal-thread");
   3078   if (inf->signal_thread)
   3079     printf_unfiltered ("The signal thread is %s.\n",
   3080 		       proc_string (inf->signal_thread));
   3081   else
   3082     printf_unfiltered ("There is no signal thread.\n");
   3083 }
   3084 
   3085 
   3086 static void
   3088 set_signals_cmd (int arg, int from_tty)
   3089 {
   3090   struct inf *inf = cur_inf ();
   3091 
   3092   inf->want_signals = arg;
   3093 
   3094   if (inf->task && inf->want_signals != inf->traced)
   3095     /* Make this take effect immediately in a running process.  */
   3096     inf_set_traced (inf, inf->want_signals);
   3097 }
   3098 
   3099 static void
   3100 set_signals_cmd (const char *args, int from_tty)
   3101 {
   3102   set_signals_cmd(parse_bool_arg (args, "set signals"), from_tty);
   3103 }
   3104 
   3105 static void
   3106 show_signals_cmd (const char *args, int from_tty)
   3107 {
   3108   struct inf *inf = cur_inf ();
   3109 
   3110   check_empty (args, "show signals");
   3111   printf_unfiltered ("The inferior process's signals %s intercepted.\n",
   3112 		     inf->task
   3113 		     ? (inf->traced ? "are" : "aren't")
   3114 		     : (inf->want_signals ? "will be" : "won't be"));
   3115 }
   3116 
   3117 static void
   3118 set_exceptions_cmd (int arg, int from_tty)
   3119 {
   3120   struct inf *inf = cur_inf ();
   3121 
   3122   /* Make this take effect immediately in a running process.  */
   3123   /* XXX */ ;
   3124 
   3125   inf->want_exceptions = arg;
   3126 }
   3127 
   3128 static void
   3129 set_exceptions_cmd (const char *args, int from_tty)
   3130 {
   3131   set_exceptions_cmd (parse_bool_arg (args, "set exceptions"), from_tty);
   3132 }
   3133 
   3134 static void
   3135 show_exceptions_cmd (const char *args, int from_tty)
   3136 {
   3137   struct inf *inf = cur_inf ();
   3138 
   3139   check_empty (args, "show exceptions");
   3140   printf_unfiltered ("Exceptions in the inferior %s trapped.\n",
   3141 		     inf->task
   3142 		     ? (inf->want_exceptions ? "are" : "aren't")
   3143 		     : (inf->want_exceptions ? "will be" : "won't be"));
   3144 }
   3145 
   3146 
   3147 static void
   3149 set_task_cmd (const char *args, int from_tty)
   3150 {
   3151   printf_unfiltered ("\"set task\" must be followed by the name"
   3152 		     " of a task property.\n");
   3153 }
   3154 
   3155 static void
   3156 show_task_cmd (const char *args, int from_tty)
   3157 {
   3158   struct inf *inf = cur_inf ();
   3159 
   3160   check_empty (args, "show task");
   3161 
   3162   show_signals_cmd (0, from_tty);
   3163   show_exceptions_cmd (0, from_tty);
   3164   show_task_pause_cmd (0, from_tty);
   3165 
   3166   if (inf->pause_sc == 0)
   3167     show_thread_default_pause_cmd (0, from_tty);
   3168   show_thread_default_run_cmd (0, from_tty);
   3169 
   3170   if (inf->task)
   3171     {
   3172       show_stopped_cmd (0, from_tty);
   3173       show_sig_thread_cmd (0, from_tty);
   3174     }
   3175 
   3176   if (inf->detach_sc != 0)
   3177     show_task_detach_sc_cmd (0, from_tty);
   3178   if (inf->default_thread_detach_sc != 0)
   3179     show_thread_default_detach_sc_cmd (0, from_tty);
   3180 }
   3181 
   3182 
   3183 static void
   3185 set_noninvasive_cmd (const char *args, int from_tty)
   3186 {
   3187   /* Invert the sense of the arg for each component.  */
   3188   int inv_arg = parse_bool_arg (args, "set noninvasive") ? 0 : 1;
   3189 
   3190   set_task_pause_cmd (inv_arg, from_tty);
   3191   set_signals_cmd (inv_arg, from_tty);
   3192   set_exceptions_cmd (inv_arg, from_tty);
   3193 }
   3194 
   3195 
   3196 static void
   3198 info_port_rights (const char *args, mach_port_type_t only)
   3199 {
   3200   struct inf *inf = active_inf ();
   3201   struct value *vmark = value_mark ();
   3202 
   3203   if (args)
   3204     /* Explicit list of port rights.  */
   3205     {
   3206       while (*args)
   3207 	{
   3208 	  struct value *val = parse_to_comma_and_eval (&args);
   3209 	  long right = value_as_long (val);
   3210 	  error_t err =
   3211 	    print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
   3212 			     stdout);
   3213 
   3214 	  if (err)
   3215 	    error (_("%ld: %s."), right, safe_strerror (err));
   3216 	}
   3217     }
   3218   else
   3219     /* Print all of them.  */
   3220     {
   3221       error_t err =
   3222 	print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
   3223 			       stdout);
   3224       if (err)
   3225 	error (_("%s."), safe_strerror (err));
   3226     }
   3227 
   3228   value_free_to_mark (vmark);
   3229 }
   3230 
   3231 static void
   3232 info_send_rights_cmd (const char *args, int from_tty)
   3233 {
   3234   info_port_rights (args, MACH_PORT_TYPE_SEND);
   3235 }
   3236 
   3237 static void
   3238 info_recv_rights_cmd (const char *args, int from_tty)
   3239 {
   3240   info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
   3241 }
   3242 
   3243 static void
   3244 info_port_sets_cmd (const char *args, int from_tty)
   3245 {
   3246   info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
   3247 }
   3248 
   3249 static void
   3250 info_dead_names_cmd (const char *args, int from_tty)
   3251 {
   3252   info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
   3253 }
   3254 
   3255 static void
   3256 info_port_rights_cmd (const char *args, int from_tty)
   3257 {
   3258   info_port_rights (args, ~0);
   3259 }
   3260 
   3261 
   3262 static void
   3264 add_task_commands (void)
   3265 {
   3266   add_cmd ("pause", class_run, set_thread_default_pause_cmd, _("\
   3267 Set whether the new threads are suspended while gdb has control.\n\
   3268 This property normally has no effect because the whole task is\n\
   3269 suspended, however, that may be disabled with \"set task pause off\".\n\
   3270 The default value is \"off\"."),
   3271 	   &set_thread_default_cmd_list);
   3272   add_cmd ("pause", no_class, show_thread_default_pause_cmd, _("\
   3273 Show whether new threads are suspended while gdb has control."),
   3274 	   &show_thread_default_cmd_list);
   3275 
   3276   add_cmd ("run", class_run, set_thread_default_run_cmd, _("\
   3277 Set whether new threads are allowed to run (once gdb has noticed them)."),
   3278 	   &set_thread_default_cmd_list);
   3279   add_cmd ("run", no_class, show_thread_default_run_cmd, _("\
   3280 Show whether new threads are allowed to run (once gdb has noticed them)."),
   3281 	   &show_thread_default_cmd_list);
   3282 
   3283   add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
   3284 	   _("Set the default detach-suspend-count value for new threads."),
   3285 	   &set_thread_default_cmd_list);
   3286   add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
   3287 	   _("Show the default detach-suspend-count value for new threads."),
   3288 	   &show_thread_default_cmd_list);
   3289 
   3290   add_cmd ("signals", class_run, set_signals_cmd, _("\
   3291 Set whether the inferior process's signals will be intercepted.\n\
   3292 Mach exceptions (such as breakpoint traps) are not affected."),
   3293 	   &setlist);
   3294   add_alias_cmd ("sigs", "signals", class_run, 1, &setlist);
   3295   add_cmd ("signals", no_class, show_signals_cmd, _("\
   3296 Show whether the inferior process's signals will be intercepted."),
   3297 	   &showlist);
   3298   add_alias_cmd ("sigs", "signals", no_class, 1, &showlist);
   3299 
   3300   add_cmd ("signal-thread", class_run, set_sig_thread_cmd, _("\
   3301 Set the thread that gdb thinks is the libc signal thread.\n\
   3302 This thread is run when delivering a signal to a non-stopped process."),
   3303 	   &setlist);
   3304   add_alias_cmd ("sigthread", "signal-thread", class_run, 1, &setlist);
   3305   add_cmd ("signal-thread", no_class, show_sig_thread_cmd, _("\
   3306 Set the thread that gdb thinks is the libc signal thread."),
   3307 	   &showlist);
   3308   add_alias_cmd ("sigthread", "signal-thread", no_class, 1, &showlist);
   3309 
   3310   add_cmd ("stopped", class_run, set_stopped_cmd, _("\
   3311 Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n\
   3312 Stopped process will be continued by sending them a signal."),
   3313 	   &setlist);
   3314   add_cmd ("stopped", no_class, show_stopped_cmd, _("\
   3315 Show whether gdb thinks the inferior process is stopped as with SIGSTOP."),
   3316 	   &showlist);
   3317 
   3318   add_cmd ("exceptions", class_run, set_exceptions_cmd, _("\
   3319 Set whether exceptions in the inferior process will be trapped.\n\
   3320 When exceptions are turned off, neither breakpoints nor single-stepping\n\
   3321 will work."),
   3322 	   &setlist);
   3323   /* Allow `set exc' despite conflict with `set exception-port'.  */
   3324   add_alias_cmd ("exc", "exceptions", class_run, 1, &setlist);
   3325   add_cmd ("exceptions", no_class, show_exceptions_cmd, _("\
   3326 Show whether exceptions in the inferior process will be trapped."),
   3327 	   &showlist);
   3328 
   3329   add_prefix_cmd ("task", no_class, set_task_cmd,
   3330 		  _("Command prefix for setting task attributes."),
   3331 		  &set_task_cmd_list, "set task ", 0, &setlist);
   3332   add_prefix_cmd ("task", no_class, show_task_cmd,
   3333 		  _("Command prefix for showing task attributes."),
   3334 		  &show_task_cmd_list, "show task ", 0, &showlist);
   3335 
   3336   add_cmd ("pause", class_run, set_task_pause_cmd, _("\
   3337 Set whether the task is suspended while gdb has control.\n\
   3338 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
   3339 until the next time the program is continued.\n\
   3340 When setting this to \"off\", \"set thread default pause on\" can be\n\
   3341 used to pause individual threads by default instead."),
   3342 	   &set_task_cmd_list);
   3343   add_cmd ("pause", no_class, show_task_pause_cmd,
   3344 	   _("Show whether the task is suspended while gdb has control."),
   3345 	   &show_task_cmd_list);
   3346 
   3347   add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
   3348 	   _("Set the suspend count will leave on the thread when detaching."),
   3349 	   &set_task_cmd_list);
   3350   add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
   3351 	   _("Show the suspend count will leave "
   3352 	     "on the thread when detaching."),
   3353 	   &show_task_cmd_list);
   3354 
   3355   add_cmd ("exception-port", no_class, set_task_exc_port_cmd, _("\
   3356 Set the task exception port to which we forward exceptions.\n\
   3357 The argument should be the value of the send right in the task."),
   3358 	   &set_task_cmd_list);
   3359   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_task_cmd_list);
   3360   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
   3361 		 &set_task_cmd_list);
   3362 
   3363   /* A convenient way of turning on all options require to noninvasively
   3364      debug running tasks.  */
   3365   add_cmd ("noninvasive", no_class, set_noninvasive_cmd, _("\
   3366 Set task options so that we interfere as little as possible.\n\
   3367 This is the same as setting `task pause', `exceptions', and\n\
   3368 `signals' to the opposite value."),
   3369 	   &setlist);
   3370 
   3371   /* Commands to show information about the task's ports.  */
   3372   add_info ("send-rights", info_send_rights_cmd,
   3373 	    _("Show information about the task's send rights"));
   3374   add_info ("receive-rights", info_recv_rights_cmd,
   3375 	    _("Show information about the task's receive rights"));
   3376   add_info ("port-rights", info_port_rights_cmd,
   3377 	    _("Show information about the task's port rights"));
   3378   add_info ("port-sets", info_port_sets_cmd,
   3379 	    _("Show information about the task's port sets"));
   3380   add_info ("dead-names", info_dead_names_cmd,
   3381 	    _("Show information about the task's dead names"));
   3382   add_info_alias ("ports", "port-rights", 1);
   3383   add_info_alias ("port", "port-rights", 1);
   3384   add_info_alias ("psets", "port-sets", 1);
   3385 }
   3386 
   3387 
   3388 static void
   3390 set_thread_pause_cmd (const char *args, int from_tty)
   3391 {
   3392   struct proc *thread = cur_thread ();
   3393   int old_sc = thread->pause_sc;
   3394 
   3395   thread->pause_sc = parse_bool_arg (args, "set thread pause");
   3396   if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
   3397     /* If the task is currently unsuspended, immediately suspend it,
   3398        otherwise wait until the next time it gets control.  */
   3399     inf_suspend (thread->inf);
   3400 }
   3401 
   3402 static void
   3403 show_thread_pause_cmd (const char *args, int from_tty)
   3404 {
   3405   struct proc *thread = cur_thread ();
   3406   int sc = thread->pause_sc;
   3407 
   3408   check_empty (args, "show task pause");
   3409   printf_unfiltered ("Thread %s %s suspended while gdb has control%s.\n",
   3410 		     proc_string (thread),
   3411 		     sc ? "is" : "isn't",
   3412 		     !sc && thread->inf->pause_sc ? " (but the task is)" : "");
   3413 }
   3414 
   3415 static void
   3416 set_thread_run_cmd (const char *args, int from_tty)
   3417 {
   3418   struct proc *thread = cur_thread ();
   3419 
   3420   thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
   3421 }
   3422 
   3423 static void
   3424 show_thread_run_cmd (const char *args, int from_tty)
   3425 {
   3426   struct proc *thread = cur_thread ();
   3427 
   3428   check_empty (args, "show thread run");
   3429   printf_unfiltered ("Thread %s %s allowed to run.",
   3430 		     proc_string (thread),
   3431 		     thread->run_sc == 0 ? "is" : "isn't");
   3432 }
   3433 
   3434 static void
   3435 set_thread_detach_sc_cmd (const char *args, int from_tty)
   3436 {
   3437   cur_thread ()->detach_sc = parse_int_arg (args,
   3438 					    "set thread detach-suspend-count");
   3439 }
   3440 
   3441 static void
   3442 show_thread_detach_sc_cmd (const char *args, int from_tty)
   3443 {
   3444   struct proc *thread = cur_thread ();
   3445 
   3446   check_empty (args, "show thread detach-suspend-count");
   3447   printf_unfiltered ("Thread %s will be left with a suspend count"
   3448 		     " of %d when detaching.\n",
   3449 		     proc_string (thread),
   3450 		     thread->detach_sc);
   3451 }
   3452 
   3453 static void
   3454 set_thread_exc_port_cmd (const char *args, int from_tty)
   3455 {
   3456   struct proc *thread = cur_thread ();
   3457 
   3458   if (!args)
   3459     error (_("No argument to \"set thread exception-port\" command."));
   3460   steal_exc_port (thread, parse_and_eval_address (args));
   3461 }
   3462 
   3463 #if 0
   3464 static void
   3465 show_thread_cmd (char *args, int from_tty)
   3466 {
   3467   struct proc *thread = cur_thread ();
   3468 
   3469   check_empty (args, "show thread");
   3470   show_thread_run_cmd (0, from_tty);
   3471   show_thread_pause_cmd (0, from_tty);
   3472   if (thread->detach_sc != 0)
   3473     show_thread_detach_sc_cmd (0, from_tty);
   3474 }
   3475 #endif
   3476 
   3477 static void
   3478 thread_takeover_sc_cmd (const char *args, int from_tty)
   3479 {
   3480   struct proc *thread = cur_thread ();
   3481 
   3482   thread_basic_info_data_t _info;
   3483   thread_basic_info_t info = &_info;
   3484   mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
   3485   kern_return_t err
   3486     = mach_thread_info (thread->port, THREAD_BASIC_INFO,
   3487 			(int *) &info, &info_len);
   3488   if (err)
   3489     error (("%s."), safe_strerror (err));
   3490   thread->sc = info->suspend_count;
   3491   if (from_tty)
   3492     printf_unfiltered ("Suspend count was %d.\n", thread->sc);
   3493   if (info != &_info)
   3494     vm_deallocate (mach_task_self (), (vm_address_t) info,
   3495 		   info_len * sizeof (int));
   3496 }
   3497 
   3498 
   3499 static void
   3501 add_thread_commands (void)
   3502 {
   3503   add_prefix_cmd ("thread", no_class, set_thread_cmd,
   3504 		  _("Command prefix for setting thread properties."),
   3505 		  &set_thread_cmd_list, "set thread ", 0, &setlist);
   3506   add_prefix_cmd ("default", no_class, show_thread_cmd,
   3507 		  _("Command prefix for setting default thread properties."),
   3508 		  &set_thread_default_cmd_list, "set thread default ", 0,
   3509 		  &set_thread_cmd_list);
   3510   add_prefix_cmd ("thread", no_class, set_thread_default_cmd,
   3511 		  _("Command prefix for showing thread properties."),
   3512 		  &show_thread_cmd_list, "show thread ", 0, &showlist);
   3513   add_prefix_cmd ("default", no_class, show_thread_default_cmd,
   3514 		  _("Command prefix for showing default thread properties."),
   3515 		  &show_thread_default_cmd_list, "show thread default ", 0,
   3516 		  &show_thread_cmd_list);
   3517 
   3518   add_cmd ("pause", class_run, set_thread_pause_cmd, _("\
   3519 Set whether the current thread is suspended while gdb has control.\n\
   3520 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
   3521 until the next time the program is continued.  This property normally\n\
   3522 has no effect because the whole task is suspended, however, that may\n\
   3523 be disabled with \"set task pause off\".\n\
   3524 The default value is \"off\"."),
   3525 	   &set_thread_cmd_list);
   3526   add_cmd ("pause", no_class, show_thread_pause_cmd, _("\
   3527 Show whether the current thread is suspended while gdb has control."),
   3528 	   &show_thread_cmd_list);
   3529 
   3530   add_cmd ("run", class_run, set_thread_run_cmd,
   3531 	   _("Set whether the current thread is allowed to run."),
   3532 	   &set_thread_cmd_list);
   3533   add_cmd ("run", no_class, show_thread_run_cmd,
   3534 	   _("Show whether the current thread is allowed to run."),
   3535 	   &show_thread_cmd_list);
   3536 
   3537   add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd, _("\
   3538 Set the suspend count will leave on the thread when detaching.\n\
   3539 Note that this is relative to suspend count when gdb noticed the thread;\n\
   3540 use the `thread takeover-suspend-count' to force it to an absolute value."),
   3541 	   &set_thread_cmd_list);
   3542   add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd, _("\
   3543 Show the suspend count will leave on the thread when detaching.\n\
   3544 Note that this is relative to suspend count when gdb noticed the thread;\n\
   3545 use the `thread takeover-suspend-count' to force it to an absolute value."),
   3546 	   &show_thread_cmd_list);
   3547 
   3548   add_cmd ("exception-port", no_class, set_thread_exc_port_cmd, _("\
   3549 Set the thread exception port to which we forward exceptions.\n\
   3550 This overrides the task exception port.\n\
   3551 The argument should be the value of the send right in the task."),
   3552 	   &set_thread_cmd_list);
   3553   add_alias_cmd ("excp", "exception-port", no_class, 1, &set_thread_cmd_list);
   3554   add_alias_cmd ("exc-port", "exception-port", no_class, 1,
   3555 		 &set_thread_cmd_list);
   3556 
   3557   add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd, _("\
   3558 Force the threads absolute suspend-count to be gdb's.\n\
   3559 Prior to giving this command, gdb's thread suspend-counts are relative\n\
   3560 to the thread's initial suspend-count when gdb notices the threads."),
   3561 	   &thread_cmd_list);
   3562 }
   3563 
   3564 void
   3565 _initialize_gnu_nat (void)
   3566 {
   3567   proc_server = getproc ();
   3568 
   3569   add_task_commands ();
   3570   add_thread_commands ();
   3571   add_setshow_boolean_cmd ("gnu-nat", class_maintenance,
   3572 			   &gnu_debug_flag,
   3573 			   _("Set debugging output for the gnu backend."),
   3574 			   _("Show debugging output for the gnu backend."),
   3575 			   NULL,
   3576 			   NULL,
   3577 			   NULL,
   3578 			   &setdebuglist,
   3579 			   &showdebuglist);
   3580 }
   3581 
   3582 #ifdef	FLUSH_INFERIOR_CACHE
   3584 
   3585 /* When over-writing code on some machines the I-Cache must be flushed
   3586    explicitly, because it is not kept coherent by the lazy hardware.
   3587    This definitely includes breakpoints, for instance, or else we
   3588    end up looping in mysterious Bpt traps.  */
   3589 
   3590 void
   3591 flush_inferior_icache (CORE_ADDR pc, int amount)
   3592 {
   3593   vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
   3594   kern_return_t ret;
   3595 
   3596   ret = vm_machine_attribute (gnu_current_inf->task->port,
   3597 			      pc,
   3598 			      amount,
   3599 			      MATTR_CACHE,
   3600 			      &flush);
   3601   if (ret != KERN_SUCCESS)
   3602     warning (_("Error flushing inferior's cache : %s"), safe_strerror (ret));
   3603 }
   3604 #endif /* FLUSH_INFERIOR_CACHE */
   3605