OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [gdb/] [gnu-nat.c] - Blame information for rev 1181

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.