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