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