gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / gnu-nat.c
blob198fc42c2178c7785b43f2a99efdf2f5c2ef8990
1 /* Interface GDB to the GNU Hurd.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 Written by Miles Bader <miles@gnu.ai.mit.edu>
8 Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Include this first, to pick up the <mach.h> 'thread_info' diversion. */
24 #include "gnu-nat.h"
26 /* Mach/Hurd headers are not yet ready for C++ compilation. */
27 extern "C"
29 #include <mach.h>
30 #include <mach_error.h>
31 #include <mach/exception.h>
32 #include <mach/message.h>
33 #include <mach/notify.h>
34 #include <mach/vm_attributes.h>
35 #include <mach/vm_param.h>
37 #include <hurd.h>
38 #include <hurd/interrupt.h>
39 #include <hurd/msg.h>
40 #include <hurd/msg_request.h>
41 #include <hurd/process.h>
42 /* Defined in <hurd/process.h>, but we need forward declarations from
43 <hurd/process_request.h> as well. */
44 #undef _process_user_
45 #include <hurd/process_request.h>
46 #include <hurd/signal.h>
47 #include <hurd/sigpreempt.h>
49 #include <portinfo.h>
53 #include <ctype.h>
54 #include <setjmp.h>
55 #include <signal.h>
56 #include <sys/ptrace.h>
57 #include <elf.h>
58 #include <link.h>
60 #include "inferior.h"
61 #include "symtab.h"
62 #include "value.h"
63 #include "language.h"
64 #include "target.h"
65 #include "gdbsupport/gdb_wait.h"
66 #include "gdbarch.h"
67 #include "cli/cli-cmds.h"
68 #include "gdbcore.h"
69 #include "gdbthread.h"
70 #include "gdbsupport/gdb_obstack.h"
71 #include "tid-parse.h"
72 #include "nat/fork-inferior.h"
74 #include "inf-child.h"
76 /* MIG stubs are not yet ready for C++ compilation. */
77 extern "C"
79 #include "exc_request_S.h"
80 #include "notify_S.h"
81 #include "process_reply_S.h"
82 #include "msg_reply_S.h"
83 #include "exc_request_U.h"
84 #include "msg_U.h"
86 #include "gnu-nat-mig.h"
89 struct gnu_nat_target *gnu_target;
91 static process_t proc_server = MACH_PORT_NULL;
93 /* If we've sent a proc_wait_request to the proc server, the pid of the
94 process we asked about. We can only ever have one outstanding. */
95 int proc_wait_pid = 0;
97 /* The number of wait requests we've sent, and expect replies from. */
98 int proc_waits_pending = 0;
100 bool gnu_debug_flag = false;
102 /* Forward decls */
104 static struct inf *make_inf ();
106 #define inf_debug(_inf, msg, args...) \
107 do { struct inf *__inf = (_inf); \
108 debug ("{inf %d %s}: " msg, __inf->pid, \
109 host_address_to_string (__inf) , ##args); } while (0)
111 /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
112 to INF's msg port and task port respectively. If it has no msg port,
113 EIEIO is returned. INF must refer to a running process! */
114 #define INF_MSGPORT_RPC(inf, rpc_expr) \
115 HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
116 (refport = inf->task->port, 0), 0, \
117 msgport ? (rpc_expr) : EIEIO)
119 /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
120 there's someone around to deal with the RPC (and resuspend things
121 afterwards). This effects INF's threads' resume_sc count. */
122 #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
123 (inf_set_threads_resume_sc_for_signal_thread (inf) \
124 ? ({ kern_return_t __e; \
125 inf_resume (inf); \
126 __e = INF_MSGPORT_RPC (inf, rpc_expr); \
127 inf_suspend (inf); \
128 __e; }) \
129 : EIEIO)
132 /* The state passed by an exception message. */
133 struct exc_state
135 int exception; /* The exception code. */
136 int code, subcode;
137 mach_port_t handler; /* The real exception port to handle this. */
138 mach_port_t reply; /* The reply port from the exception call. */
141 /* The results of the last wait an inf did. */
142 struct inf_wait
144 struct target_waitstatus status; /* The status returned to gdb. */
145 struct exc_state exc; /* The exception that caused us to return. */
146 struct proc *thread; /* The thread in question. */
147 int suppress; /* Something trivial happened. */
150 /* Further Hurd-specific state of an inferior. */
151 struct inf
153 /* Fields describing the current inferior. */
155 struct proc *task; /* The mach task. */
156 struct proc *threads; /* A linked list of all threads in TASK. */
158 /* True if THREADS needn't be validated by querying the task. We
159 assume that we and the task in question are the only ones
160 frobbing the thread list, so as long as we don't let any code
161 run, we don't have to worry about THREADS changing. */
162 int threads_up_to_date;
164 pid_t pid; /* The real system PID. */
166 struct inf_wait wait; /* What to return from target_wait. */
168 /* One thread proc in INF may be in `single-stepping mode'. This
169 is it. */
170 struct proc *step_thread;
172 /* The thread we think is the signal thread. */
173 struct proc *signal_thread;
175 mach_port_t event_port; /* Where we receive various msgs. */
177 /* True if we think at least one thread in the inferior could currently be
178 running. */
179 unsigned int running:1;
181 /* True if the process has stopped (in the proc server sense). Note that
182 since a proc server `stop' leaves the signal thread running, the inf can
183 be RUNNING && STOPPED... */
184 unsigned int stopped:1;
186 /* True if the inferior has no message port. */
187 unsigned int nomsg:1;
189 /* True if the inferior is traced. */
190 unsigned int traced:1;
192 /* True if we shouldn't try waiting for the inferior, usually because we
193 can't for some reason. */
194 unsigned int no_wait:1;
196 /* When starting a new inferior, we don't try to validate threads until all
197 the proper execs have been done, which this flag states we still
198 expect to happen. */
199 unsigned int pending_execs:1;
201 /* Fields describing global state. */
203 /* The task suspend count used when gdb has control. This is normally 1 to
204 make things easier for us, but sometimes (like when attaching to vital
205 system servers) it may be desirable to let the task continue to run
206 (pausing individual threads as necessary). */
207 int pause_sc;
209 /* The task suspend count left when detaching from a task. */
210 int detach_sc;
212 /* The initial values used for the run_sc and pause_sc of newly discovered
213 threads -- see the definition of those fields in struct proc. */
214 int default_thread_run_sc;
215 int default_thread_pause_sc;
216 int default_thread_detach_sc;
218 /* True if the process should be traced when started/attached. Newly
219 started processes *must* be traced at first to exec them properly, but
220 if this is false, tracing is turned off as soon it has done so. */
221 int want_signals;
223 /* True if exceptions from the inferior process should be trapped. This
224 must be on to use breakpoints. */
225 int want_exceptions;
230 __proc_pid (struct proc *proc)
232 return proc->inf->pid;
236 /* Update PROC's real suspend count to match it's desired one. Returns true
237 if we think PROC is now in a runnable state. */
239 gnu_nat_target::proc_update_sc (struct proc *proc)
241 int running;
242 int err = 0;
243 int delta = proc->sc - proc->cur_sc;
245 if (delta)
246 proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
248 if (proc->sc == 0 && proc->state_changed)
249 /* Since PROC may start running, we must write back any state changes. */
251 gdb_assert (proc_is_thread (proc));
252 proc_debug (proc, "storing back changed thread state");
253 err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
254 (thread_state_t) &proc->state, THREAD_STATE_SIZE);
255 if (!err)
256 proc->state_changed = 0;
259 if (delta > 0)
261 while (delta-- > 0 && !err)
263 if (proc_is_task (proc))
264 err = task_suspend (proc->port);
265 else
266 err = thread_suspend (proc->port);
269 else
271 while (delta++ < 0 && !err)
273 if (proc_is_task (proc))
274 err = task_resume (proc->port);
275 else
276 err = thread_resume (proc->port);
279 if (!err)
280 proc->cur_sc = proc->sc;
282 /* If we got an error, then the task/thread has disappeared. */
283 running = !err && proc->sc == 0;
285 proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
286 if (err)
287 proc_debug (proc, "err = %s", safe_strerror (err));
289 if (running)
291 proc->aborted = 0;
292 proc->state_valid = proc->state_changed = 0;
293 proc->fetched_regs = 0;
296 return running;
300 /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
301 If PROC is deemed `precious', then nothing is done unless FORCE is true.
302 In particular, a thread is precious if it's running (in which case forcing
303 it includes suspending it first), or if it has an exception pending. */
304 void
305 gnu_nat_target::proc_abort (struct proc *proc, int force)
307 gdb_assert (proc_is_thread (proc));
309 if (!proc->aborted)
311 struct inf *inf = proc->inf;
312 int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
314 if (running && force)
316 proc->sc = 1;
317 inf_update_suspends (proc->inf);
318 running = 0;
319 warning (_("Stopped %s."), proc_string (proc));
321 else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
322 /* An exception is pending on PROC, which don't mess with. */
323 running = 1;
325 if (!running)
326 /* We only abort the thread if it's not actually running. */
328 thread_abort (proc->port);
329 proc_debug (proc, "aborted");
330 proc->aborted = 1;
332 else
333 proc_debug (proc, "not aborting");
337 /* Make sure that the state field in PROC is up to date, and return a pointer
338 to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
339 that the thread is stopped and aborted first, and sets the state_changed
340 field in PROC to true. */
341 thread_state_t
342 gnu_nat_target::proc_get_state (struct proc *proc, int will_modify)
344 int was_aborted = proc->aborted;
346 proc_debug (proc, "updating state info%s",
347 will_modify ? " (with intention to modify)" : "");
349 proc_abort (proc, will_modify);
351 if (!was_aborted && proc->aborted)
352 /* PROC's state may have changed since we last fetched it. */
353 proc->state_valid = 0;
355 if (!proc->state_valid)
357 mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
358 kern_return_t err =
359 thread_get_state (proc->port, THREAD_STATE_FLAVOR,
360 (thread_state_t) &proc->state, &state_size);
362 proc_debug (proc, "getting thread state");
363 proc->state_valid = !err;
366 if (proc->state_valid)
368 if (will_modify)
369 proc->state_changed = 1;
370 return (thread_state_t) &proc->state;
372 else
373 return 0;
377 /* Set PORT to PROC's exception port. */
378 kern_return_t
379 gnu_nat_target::proc_get_exception_port (struct proc * proc, mach_port_t * port)
381 if (proc_is_task (proc))
382 return task_get_exception_port (proc->port, port);
383 else
384 return thread_get_exception_port (proc->port, port);
387 /* Set PROC's exception port to PORT. */
388 kern_return_t
389 gnu_nat_target::proc_set_exception_port (struct proc * proc, mach_port_t port)
391 proc_debug (proc, "setting exception port: %lu", port);
392 if (proc_is_task (proc))
393 return task_set_exception_port (proc->port, port);
394 else
395 return thread_set_exception_port (proc->port, port);
398 /* Get PROC's exception port, cleaning up a bit if proc has died. */
399 mach_port_t
400 gnu_nat_target::_proc_get_exc_port (struct proc *proc)
402 mach_port_t exc_port;
403 kern_return_t err = proc_get_exception_port (proc, &exc_port);
405 if (err)
406 /* PROC must be dead. */
408 if (proc->exc_port)
409 mach_port_deallocate (mach_task_self (), proc->exc_port);
410 proc->exc_port = MACH_PORT_NULL;
411 if (proc->saved_exc_port)
412 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
413 proc->saved_exc_port = MACH_PORT_NULL;
416 return exc_port;
419 /* Replace PROC's exception port with EXC_PORT, unless it's already
420 been done. Stash away any existing exception port so we can
421 restore it later. */
422 void
423 gnu_nat_target::proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
425 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
427 if (cur_exc_port)
429 kern_return_t err = 0;
431 proc_debug (proc, "inserting exception port: %lu", exc_port);
433 if (cur_exc_port != exc_port)
434 /* Put in our exception port. */
435 err = proc_set_exception_port (proc, exc_port);
437 if (err || cur_exc_port == proc->exc_port)
438 /* We previously set the exception port, and it's still set. So we
439 just keep the old saved port which is what the proc set. */
441 if (cur_exc_port)
442 mach_port_deallocate (mach_task_self (), cur_exc_port);
444 else
445 /* Keep a copy of PROC's old exception port so it can be restored. */
447 if (proc->saved_exc_port)
448 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
449 proc->saved_exc_port = cur_exc_port;
452 proc_debug (proc, "saved exception port: %lu", proc->saved_exc_port);
454 if (!err)
455 proc->exc_port = exc_port;
456 else
457 warning (_("Error setting exception port for %s: %s"),
458 proc_string (proc), safe_strerror (err));
462 /* If we previously replaced PROC's exception port, put back what we
463 found there at the time, unless *our* exception port has since been
464 overwritten, in which case who knows what's going on. */
465 void
466 gnu_nat_target::proc_restore_exc_port (struct proc *proc)
468 mach_port_t cur_exc_port = _proc_get_exc_port (proc);
470 if (cur_exc_port)
472 kern_return_t err = 0;
474 proc_debug (proc, "restoring real exception port");
476 if (proc->exc_port == cur_exc_port)
477 /* Our's is still there. */
478 err = proc_set_exception_port (proc, proc->saved_exc_port);
480 if (proc->saved_exc_port)
481 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
482 proc->saved_exc_port = MACH_PORT_NULL;
484 if (!err)
485 proc->exc_port = MACH_PORT_NULL;
486 else
487 warning (_("Error setting exception port for %s: %s"),
488 proc_string (proc), safe_strerror (err));
493 /* Turns hardware tracing in PROC on or off when SET is true or false,
494 respectively. Returns true on success. */
496 gnu_nat_target::proc_trace (struct proc *proc, int set)
498 thread_state_t state = proc_get_state (proc, 1);
500 if (!state)
501 return 0; /* The thread must be dead. */
503 proc_debug (proc, "tracing %s", set ? "on" : "off");
505 if (set)
507 /* XXX We don't get the exception unless the thread has its own
508 exception port???? */
509 if (proc->exc_port == MACH_PORT_NULL)
510 proc_steal_exc_port (proc, proc->inf->event_port);
511 THREAD_STATE_SET_TRACED (state);
513 else
514 THREAD_STATE_CLEAR_TRACED (state);
516 return 1;
520 /* A variable from which to assign new TIDs. */
521 static int next_thread_id = 1;
523 /* Returns a new proc structure with the given fields. Also adds a
524 notification for PORT becoming dead to be sent to INF's notify port. */
525 struct proc *
526 gnu_nat_target::make_proc (struct inf *inf, mach_port_t port, int tid)
528 kern_return_t err;
529 mach_port_t prev_port = MACH_PORT_NULL;
530 struct proc *proc = XNEW (struct proc);
532 proc->port = port;
533 proc->tid = tid;
534 proc->inf = inf;
535 proc->next = 0;
536 proc->saved_exc_port = MACH_PORT_NULL;
537 proc->exc_port = MACH_PORT_NULL;
539 proc->sc = 0;
540 proc->cur_sc = 0;
542 /* Note that these are all the values for threads; the task simply uses the
543 corresponding field in INF directly. */
544 proc->run_sc = inf->default_thread_run_sc;
545 proc->pause_sc = inf->default_thread_pause_sc;
546 proc->detach_sc = inf->default_thread_detach_sc;
547 proc->resume_sc = proc->run_sc;
549 proc->aborted = 0;
550 proc->dead = 0;
551 proc->state_valid = 0;
552 proc->state_changed = 0;
554 proc_debug (proc, "is new");
556 /* Get notified when things die. */
557 err =
558 mach_port_request_notification (mach_task_self (), port,
559 MACH_NOTIFY_DEAD_NAME, 1,
560 inf->event_port,
561 MACH_MSG_TYPE_MAKE_SEND_ONCE,
562 &prev_port);
563 if (err)
564 warning (_("Couldn't request notification for port %lu: %s"),
565 port, safe_strerror (err));
566 else
568 proc_debug (proc, "notifications to: %lu", inf->event_port);
569 if (prev_port != MACH_PORT_NULL)
570 mach_port_deallocate (mach_task_self (), prev_port);
573 if (inf->want_exceptions)
575 if (proc_is_task (proc))
576 /* Make the task exception port point to us. */
577 proc_steal_exc_port (proc, inf->event_port);
578 else
579 /* Just clear thread exception ports -- they default to the
580 task one. */
581 proc_steal_exc_port (proc, MACH_PORT_NULL);
584 return proc;
587 /* Frees PROC and any resources it uses, and returns the value of PROC's
588 next field. */
589 struct proc *
590 gnu_nat_target::_proc_free (struct proc *proc)
592 struct inf *inf = proc->inf;
593 struct proc *next = proc->next;
595 proc_debug (proc, "freeing...");
597 if (proc == inf->step_thread)
598 /* Turn off single stepping. */
599 inf_set_step_thread (inf, 0);
600 if (proc == inf->wait.thread)
601 inf_clear_wait (inf);
602 if (proc == inf->signal_thread)
603 inf->signal_thread = 0;
605 if (proc->port != MACH_PORT_NULL)
607 if (proc->exc_port != MACH_PORT_NULL)
608 /* Restore the original exception port. */
609 proc_restore_exc_port (proc);
610 if (proc->cur_sc != 0)
611 /* Resume the thread/task. */
613 proc->sc = 0;
614 proc_update_sc (proc);
616 mach_port_deallocate (mach_task_self (), proc->port);
619 xfree (proc);
620 return next;
624 static struct inf *
625 make_inf (void)
627 struct inf *inf = new struct inf;
629 inf->task = 0;
630 inf->threads = 0;
631 inf->threads_up_to_date = 0;
632 inf->pid = 0;
633 inf->wait.status.set_spurious ();
634 inf->wait.thread = 0;
635 inf->wait.exc.handler = MACH_PORT_NULL;
636 inf->wait.exc.reply = MACH_PORT_NULL;
637 inf->step_thread = 0;
638 inf->signal_thread = 0;
639 inf->event_port = MACH_PORT_NULL;
640 inf->running = 0;
641 inf->stopped = 0;
642 inf->nomsg = 1;
643 inf->traced = 0;
644 inf->no_wait = 0;
645 inf->pending_execs = 0;
646 inf->pause_sc = 1;
647 inf->detach_sc = 0;
648 inf->default_thread_run_sc = 0;
649 inf->default_thread_pause_sc = 0;
650 inf->default_thread_detach_sc = 0;
651 inf->want_signals = 1; /* By default */
652 inf->want_exceptions = 1; /* By default */
654 return inf;
657 /* Clear INF's target wait status. */
658 void
659 gnu_nat_target::inf_clear_wait (struct inf *inf)
661 inf_debug (inf, "clearing wait");
662 inf->wait.status.set_spurious ();
663 inf->wait.thread = 0;
664 inf->wait.suppress = 0;
665 if (inf->wait.exc.handler != MACH_PORT_NULL)
667 mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
668 inf->wait.exc.handler = MACH_PORT_NULL;
670 if (inf->wait.exc.reply != MACH_PORT_NULL)
672 mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
673 inf->wait.exc.reply = MACH_PORT_NULL;
678 void
679 gnu_nat_target::inf_cleanup (struct inf *inf)
681 inf_debug (inf, "cleanup");
683 inf_clear_wait (inf);
685 inf_set_pid (inf, -1);
686 inf->pid = 0;
687 inf->running = 0;
688 inf->stopped = 0;
689 inf->nomsg = 1;
690 inf->traced = 0;
691 inf->no_wait = 0;
692 inf->pending_execs = 0;
694 if (inf->event_port)
696 mach_port_destroy (mach_task_self (), inf->event_port);
697 inf->event_port = MACH_PORT_NULL;
701 void
702 gnu_nat_target::inf_startup (struct inf *inf, int pid)
704 kern_return_t err;
706 inf_debug (inf, "startup: pid = %d", pid);
708 inf_cleanup (inf);
710 /* Make the port on which we receive all events. */
711 err = mach_port_allocate (mach_task_self (),
712 MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
713 if (err)
714 error (_("Error allocating event port: %s"), safe_strerror (err));
716 /* Make a send right for it, so we can easily copy it for other people. */
717 mach_port_insert_right (mach_task_self (), inf->event_port,
718 inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
719 inf_set_pid (inf, pid);
723 /* Close current process, if any, and attach INF to process PORT. */
724 void
725 gnu_nat_target::inf_set_pid (struct inf *inf, pid_t pid)
727 task_t task_port;
728 struct proc *task = inf->task;
730 inf_debug (inf, "setting pid: %d", pid);
732 if (pid < 0)
733 task_port = MACH_PORT_NULL;
734 else
736 kern_return_t err = proc_pid2task (proc_server, pid, &task_port);
738 if (err)
739 error (_("Error getting task for pid %d: %s"),
740 pid, safe_strerror (err));
743 inf_debug (inf, "setting task: %lu", task_port);
745 if (inf->pause_sc)
746 task_suspend (task_port);
748 if (task && task->port != task_port)
750 inf->task = 0;
751 inf_validate_procs (inf); /* Trash all the threads. */
752 _proc_free (task); /* And the task. */
755 if (task_port != MACH_PORT_NULL)
757 inf->task = make_proc (inf, task_port, PROC_TID_TASK);
758 inf->threads_up_to_date = 0;
761 if (inf->task)
763 inf->pid = pid;
764 if (inf->pause_sc)
765 /* Reflect task_suspend above. */
766 inf->task->sc = inf->task->cur_sc = 1;
768 else
769 inf->pid = -1;
773 /* Validates INF's stopped, nomsg and traced field from the actual
774 proc server state. Note that the traced field is only updated from
775 the proc server state if we do not have a message port. If we do
776 have a message port we'd better look at the tracemask itself. */
777 void
778 gnu_nat_target::inf_validate_procinfo (struct inf *inf)
780 char *noise;
781 mach_msg_type_number_t noise_len = 0;
782 struct procinfo *pi;
783 mach_msg_type_number_t pi_len = 0;
784 int info_flags = 0;
785 kern_return_t err =
786 proc_getprocinfo (proc_server, inf->pid, &info_flags,
787 (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
789 if (!err)
791 inf->stopped = !!(pi->state & PI_STOPPED);
792 inf->nomsg = !!(pi->state & PI_NOMSG);
793 if (inf->nomsg)
794 inf->traced = !!(pi->state & PI_TRACED);
795 vm_deallocate (mach_task_self (), (vm_address_t) pi,
796 pi_len * sizeof (*(procinfo_t) 0));
797 if (noise_len > 0)
798 vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
802 /* Validates INF's task suspend count. If it's higher than we expect,
803 verify with the user before `stealing' the extra count. */
804 void
805 gnu_nat_target::inf_validate_task_sc (struct inf *inf)
807 char *noise;
808 mach_msg_type_number_t noise_len = 0;
809 struct procinfo *pi;
810 mach_msg_type_number_t pi_len = 0;
811 int info_flags = PI_FETCH_TASKINFO;
812 int suspend_count = -1;
813 kern_return_t err;
815 retry:
816 err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
817 (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
818 if (err)
820 inf->task->dead = 1; /* oh well */
821 return;
824 if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
826 /* The proc server might have suspended the task while stopping
827 it. This happens when the task is handling a traced signal.
828 Refetch the suspend count. The proc server should be
829 finished stopping the task by now. */
830 suspend_count = pi->taskinfo.suspend_count;
831 goto retry;
834 suspend_count = pi->taskinfo.suspend_count;
836 vm_deallocate (mach_task_self (), (vm_address_t) pi,
837 pi_len * sizeof (*(procinfo_t) 0));
838 if (noise_len > 0)
839 vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
841 if (inf->task->cur_sc < suspend_count)
843 if (!query (_("Pid %d has an additional task suspend count of %d;"
844 " clear it? "), inf->pid,
845 suspend_count - inf->task->cur_sc))
846 error (_("Additional task suspend count left untouched."));
848 inf->task->cur_sc = suspend_count;
852 /* Turns tracing for INF on or off, depending on ON, unless it already
853 is. If INF is running, the resume_sc count of INF's threads will
854 be modified, and the signal thread will briefly be run to change
855 the trace state. */
856 void
857 gnu_nat_target::inf_set_traced (struct inf *inf, int on)
859 if (on == inf->traced)
860 return;
862 if (inf->task && !inf->task->dead)
863 /* Make it take effect immediately. */
865 sigset_t mask = on ? ~(sigset_t) 0 : 0;
866 kern_return_t err =
867 INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
868 INIT_TRACEMASK, mask));
870 if (err == EIEIO)
872 if (on)
873 warning (_("Can't modify tracing state for pid %d: %s"),
874 inf->pid, "No signal thread");
875 inf->traced = on;
877 else if (err)
878 warning (_("Can't modify tracing state for pid %d: %s"),
879 inf->pid, safe_strerror (err));
880 else
881 inf->traced = on;
883 else
884 inf->traced = on;
888 /* Makes all the real suspend count deltas of all the procs in INF
889 match the desired values. Careful to always do thread/task suspend
890 counts in the safe order. Returns true if at least one thread is
891 thought to be running. */
893 gnu_nat_target::inf_update_suspends (struct inf *inf)
895 struct proc *task = inf->task;
897 /* We don't have to update INF->threads even though we're iterating over it
898 because we'll change a thread only if it already has an existing proc
899 entry. */
900 inf_debug (inf, "updating suspend counts");
902 if (task)
904 struct proc *thread;
905 int task_running = (task->sc == 0), thread_running = 0;
907 if (task->sc > task->cur_sc)
908 /* The task is becoming _more_ suspended; do before any threads. */
909 task_running = proc_update_sc (task);
911 if (inf->pending_execs)
912 /* When we're waiting for an exec, things may be happening behind our
913 back, so be conservative. */
914 thread_running = 1;
916 /* Do all the thread suspend counts. */
917 for (thread = inf->threads; thread; thread = thread->next)
918 thread_running |= proc_update_sc (thread);
920 if (task->sc != task->cur_sc)
921 /* We didn't do the task first, because we wanted to wait for the
922 threads; do it now. */
923 task_running = proc_update_sc (task);
925 inf_debug (inf, "%srunning...",
926 (thread_running && task_running) ? "" : "not ");
928 inf->running = thread_running && task_running;
930 /* Once any thread has executed some code, we can't depend on the
931 threads list any more. */
932 if (inf->running)
933 inf->threads_up_to_date = 0;
935 return inf->running;
938 return 0;
942 /* Converts a GDB pid to a struct proc. */
943 struct proc *
944 inf_tid_to_thread (struct inf *inf, int tid)
946 struct proc *thread = inf->threads;
948 while (thread)
949 if (thread->tid == tid)
950 return thread;
951 else
952 thread = thread->next;
953 return 0;
956 /* Converts a thread port to a struct proc. */
957 static struct proc *
958 inf_port_to_thread (struct inf *inf, mach_port_t port)
960 struct proc *thread = inf->threads;
962 while (thread)
963 if (thread->port == port)
964 return thread;
965 else
966 thread = thread->next;
967 return 0;
970 /* See gnu-nat.h. */
972 void
973 inf_threads (struct inf *inf, inf_threads_ftype *f, void *arg)
975 struct proc *thread;
977 for (thread = inf->threads; thread; thread = thread->next)
978 f (thread, arg);
982 /* Make INF's list of threads be consistent with reality of TASK. */
983 void
984 gnu_nat_target::inf_validate_procs (struct inf *inf)
986 thread_array_t threads;
987 mach_msg_type_number_t num_threads, i;
988 struct proc *task = inf->task;
990 /* If no threads are currently running, this function will guarantee that
991 things are up to date. The exception is if there are zero threads --
992 then it is almost certainly in an odd state, and probably some outside
993 agent will create threads. */
994 inf->threads_up_to_date = inf->threads ? !inf->running : 0;
996 if (task)
998 kern_return_t err = task_threads (task->port, &threads, &num_threads);
1000 inf_debug (inf, "fetching threads");
1001 if (err)
1002 /* TASK must be dead. */
1004 task->dead = 1;
1005 task = 0;
1009 if (!task)
1011 num_threads = 0;
1012 inf_debug (inf, "no task");
1016 /* Make things normally linear. */
1017 mach_msg_type_number_t search_start = 0;
1018 /* Which thread in PROCS corresponds to each task thread, & the task. */
1019 struct proc *matched[num_threads + 1];
1020 /* The last thread in INF->threads, so we can add to the end. */
1021 struct proc *last = 0;
1022 /* The current thread we're considering. */
1023 struct proc *thread = inf->threads;
1025 memset (matched, 0, sizeof (matched));
1027 while (thread)
1029 mach_msg_type_number_t left;
1031 for (i = search_start, left = num_threads; left; i++, left--)
1033 if (i >= num_threads)
1034 i -= num_threads; /* I wrapped around. */
1035 if (thread->port == threads[i])
1036 /* We already know about this thread. */
1038 matched[i] = thread;
1039 last = thread;
1040 thread = thread->next;
1041 search_start++;
1042 break;
1046 if (!left)
1048 proc_debug (thread, "died!");
1049 thread->port = MACH_PORT_NULL;
1050 thread = _proc_free (thread); /* THREAD is dead. */
1051 if (last)
1052 last->next = thread;
1053 else
1054 inf->threads = thread;
1058 for (i = 0; i < num_threads; i++)
1060 if (matched[i])
1061 /* Throw away the duplicate send right. */
1062 mach_port_deallocate (mach_task_self (), threads[i]);
1063 else
1064 /* THREADS[I] is a thread we don't know about yet! */
1066 ptid_t ptid;
1068 thread = make_proc (inf, threads[i], next_thread_id++);
1069 if (last)
1070 last->next = thread;
1071 else
1072 inf->threads = thread;
1073 last = thread;
1074 proc_debug (thread, "new thread: %lu", threads[i]);
1076 ptid = ptid_t (inf->pid, thread->tid, 0);
1078 /* Tell GDB's generic thread code. */
1080 if (inferior_ptid == ptid_t (inf->pid))
1081 /* This is the first time we're hearing about thread
1082 ids, after a fork-child. */
1083 thread_change_ptid (this, inferior_ptid, ptid);
1084 else if (inf->pending_execs != 0)
1085 /* This is a shell thread. */
1086 add_thread_silent (this, ptid);
1087 else
1088 add_thread (this, ptid);
1092 vm_deallocate (mach_task_self (),
1093 (vm_address_t) threads, (num_threads * sizeof (thread_t)));
1098 /* Makes sure that INF's thread list is synced with the actual process. */
1100 inf_update_procs (struct inf *inf)
1102 if (!inf->task)
1103 return 0;
1104 if (!inf->threads_up_to_date)
1105 gnu_target->inf_validate_procs (inf);
1106 return !!inf->task;
1109 /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
1110 and others are set to their run_sc if RUN_OTHERS is true, and otherwise
1111 their pause_sc. */
1112 void
1113 gnu_nat_target::inf_set_threads_resume_sc (struct inf *inf,
1114 struct proc *run_thread, int run_others)
1116 struct proc *thread;
1118 inf_update_procs (inf);
1119 for (thread = inf->threads; thread; thread = thread->next)
1120 if (thread == run_thread)
1121 thread->resume_sc = 0;
1122 else if (run_others)
1123 thread->resume_sc = thread->run_sc;
1124 else
1125 thread->resume_sc = thread->pause_sc;
1129 /* Cause INF to continue execution immediately; individual threads may still
1130 be suspended (but their suspend counts will be updated). */
1131 void
1132 gnu_nat_target::inf_resume (struct inf *inf)
1134 struct proc *thread;
1136 inf_update_procs (inf);
1138 for (thread = inf->threads; thread; thread = thread->next)
1139 thread->sc = thread->resume_sc;
1141 if (inf->task)
1143 if (!inf->pending_execs)
1144 /* Try to make sure our task count is correct -- in the case where
1145 we're waiting for an exec though, things are too volatile, so just
1146 assume things will be reasonable (which they usually will be). */
1147 inf_validate_task_sc (inf);
1148 inf->task->sc = 0;
1151 inf_update_suspends (inf);
1154 /* Cause INF to stop execution immediately; individual threads may still
1155 be running. */
1156 void
1157 gnu_nat_target::inf_suspend (struct inf *inf)
1159 struct proc *thread;
1161 inf_update_procs (inf);
1163 for (thread = inf->threads; thread; thread = thread->next)
1164 thread->sc = thread->pause_sc;
1166 if (inf->task)
1167 inf->task->sc = inf->pause_sc;
1169 inf_update_suspends (inf);
1173 /* INF has one thread PROC that is in single-stepping mode. This
1174 function changes it to be PROC, changing any old step_thread to be
1175 a normal one. A PROC of 0 clears any existing value. */
1176 void
1177 gnu_nat_target::inf_set_step_thread (struct inf *inf, struct proc *thread)
1179 gdb_assert (!thread || proc_is_thread (thread));
1181 if (thread)
1182 inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
1183 else
1184 inf_debug (inf, "clearing step thread");
1186 if (inf->step_thread != thread)
1188 if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
1189 if (!proc_trace (inf->step_thread, 0))
1190 return;
1191 if (thread && proc_trace (thread, 1))
1192 inf->step_thread = thread;
1193 else
1194 inf->step_thread = 0;
1199 /* Set up the thread resume_sc's so that only the signal thread is running
1200 (plus whatever other thread are set to always run). Returns true if we
1201 did so, or false if we can't find a signal thread. */
1203 gnu_nat_target::inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
1205 if (inf->signal_thread)
1207 inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
1208 return 1;
1210 else
1211 return 0;
1214 static void
1215 inf_update_signal_thread (struct inf *inf)
1217 /* XXX for now we assume that if there's a msgport, the 2nd thread is
1218 the signal thread. */
1219 inf->signal_thread = inf->threads ? inf->threads->next : 0;
1223 /* Detachs from INF's inferior task, letting it run once again... */
1224 void
1225 gnu_nat_target::inf_detach (struct inf *inf)
1227 struct proc *task = inf->task;
1229 inf_debug (inf, "detaching...");
1231 inf_clear_wait (inf);
1232 inf_set_step_thread (inf, 0);
1234 if (task)
1236 struct proc *thread;
1238 inf_validate_procinfo (inf);
1240 inf_set_traced (inf, 0);
1241 if (inf->stopped)
1243 if (inf->nomsg)
1244 inf_continue (inf);
1245 else
1246 inf_signal (inf, GDB_SIGNAL_0);
1249 proc_restore_exc_port (task);
1250 task->sc = inf->detach_sc;
1252 for (thread = inf->threads; thread; thread = thread->next)
1254 proc_restore_exc_port (thread);
1255 thread->sc = thread->detach_sc;
1258 inf_update_suspends (inf);
1261 inf_cleanup (inf);
1264 /* Attaches INF to the process with process id PID, returning it in a
1265 suspended state suitable for debugging. */
1266 void
1267 gnu_nat_target::inf_attach (struct inf *inf, int pid)
1269 inf_debug (inf, "attaching: %d", pid);
1271 if (inf->pid)
1272 inf_detach (inf);
1274 inf_startup (inf, pid);
1278 /* Makes sure that we've got our exception ports entrenched in the process. */
1279 void
1280 gnu_nat_target::inf_steal_exc_ports (struct inf *inf)
1282 struct proc *thread;
1284 inf_debug (inf, "stealing exception ports");
1286 inf_set_step_thread (inf, 0); /* The step thread is special. */
1288 proc_steal_exc_port (inf->task, inf->event_port);
1289 for (thread = inf->threads; thread; thread = thread->next)
1290 proc_steal_exc_port (thread, MACH_PORT_NULL);
1293 /* Makes sure the process has its own exception ports. */
1294 void
1295 gnu_nat_target::inf_restore_exc_ports (struct inf *inf)
1297 struct proc *thread;
1299 inf_debug (inf, "restoring exception ports");
1301 inf_set_step_thread (inf, 0); /* The step thread is special. */
1303 proc_restore_exc_port (inf->task);
1304 for (thread = inf->threads; thread; thread = thread->next)
1305 proc_restore_exc_port (thread);
1309 /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
1310 signal 0, will continue it. INF is assumed to be in a paused state, and
1311 the resume_sc's of INF's threads may be affected. */
1312 void
1313 gnu_nat_target::inf_signal (struct inf *inf, enum gdb_signal sig)
1315 kern_return_t err = 0;
1316 int host_sig = gdb_signal_to_host (sig);
1318 #define NAME gdb_signal_to_name (sig)
1320 if (host_sig >= _NSIG)
1321 /* A mach exception. Exceptions are encoded in the signal space by
1322 putting them after _NSIG; this assumes they're positive (and not
1323 extremely large)! */
1325 struct inf_wait *w = &inf->wait;
1327 if (w->status.kind () == TARGET_WAITKIND_STOPPED
1328 && w->status.sig () == sig
1329 && w->thread && !w->thread->aborted)
1330 /* We're passing through the last exception we received. This is
1331 kind of bogus, because exceptions are per-thread whereas gdb
1332 treats signals as per-process. We just forward the exception to
1333 the correct handler, even it's not for the same thread as TID --
1334 i.e., we pretend it's global. */
1336 struct exc_state *e = &w->exc;
1338 inf_debug (inf, "passing through exception:"
1339 " task = %lu, thread = %lu, exc = %d"
1340 ", code = %d, subcode = %d",
1341 w->thread->port, inf->task->port,
1342 e->exception, e->code, e->subcode);
1343 err =
1344 exception_raise_request (e->handler,
1345 e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
1346 w->thread->port, inf->task->port,
1347 e->exception, e->code, e->subcode);
1349 else
1350 error (_("Can't forward spontaneous exception (%s)."), NAME);
1352 else
1353 /* A Unix signal. */
1354 if (inf->stopped)
1355 /* The process is stopped and expecting a signal. Just send off a
1356 request and let it get handled when we resume everything. */
1358 inf_debug (inf, "sending %s to stopped process", NAME);
1359 err =
1360 INF_MSGPORT_RPC (inf,
1361 msg_sig_post_untraced_request (msgport,
1362 inf->event_port,
1363 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1364 host_sig, 0,
1365 refport));
1366 if (!err)
1367 /* Posting an untraced signal automatically continues it.
1368 We clear this here rather than when we get the reply
1369 because we'd rather assume it's not stopped when it
1370 actually is, than the reverse. */
1371 inf->stopped = 0;
1373 else
1374 /* It's not expecting it. We have to let just the signal thread
1375 run, and wait for it to get into a reasonable state before we
1376 can continue the rest of the process. When we finally resume the
1377 process the signal we request will be the very first thing that
1378 happens. */
1380 inf_debug (inf, "sending %s to unstopped process"
1381 " (so resuming signal thread)", NAME);
1382 err =
1383 INF_RESUME_MSGPORT_RPC (inf,
1384 msg_sig_post_untraced (msgport, host_sig,
1385 0, refport));
1388 if (err == EIEIO)
1389 /* Can't do too much... */
1390 warning (_("Can't deliver signal %s: No signal thread."), NAME);
1391 else if (err)
1392 warning (_("Delivering signal %s: %s"), NAME, safe_strerror (err));
1394 #undef NAME
1398 /* Continue INF without delivering a signal. This is meant to be used
1399 when INF does not have a message port. */
1400 void
1401 gnu_nat_target::inf_continue (struct inf *inf)
1403 process_t proc;
1404 kern_return_t err = proc_pid2proc (proc_server, inf->pid, &proc);
1406 if (!err)
1408 inf_debug (inf, "continuing process");
1410 err = proc_mark_cont (proc);
1411 if (!err)
1413 struct proc *thread;
1415 for (thread = inf->threads; thread; thread = thread->next)
1416 thread_resume (thread->port);
1418 inf->stopped = 0;
1422 if (err)
1423 warning (_("Can't continue process: %s"), safe_strerror (err));
1427 /* The inferior used for all gdb target ops. */
1428 struct inf *gnu_current_inf = 0;
1430 /* The inferior being waited for by gnu_wait. Since GDB is decidedly not
1431 multi-threaded, we don't bother to lock this. */
1432 static struct inf *waiting_inf;
1434 /* Wait for something to happen in the inferior, returning what in STATUS. */
1436 ptid_t
1437 gnu_nat_target::wait (ptid_t ptid, struct target_waitstatus *status,
1438 target_wait_flags options)
1440 struct msg
1442 mach_msg_header_t hdr;
1443 mach_msg_type_t type;
1444 int data[8000];
1445 } msg;
1446 kern_return_t err;
1447 struct proc *thread;
1448 struct inf *inf = gnu_current_inf;
1450 gdb_assert (inf->task);
1452 if (!inf->threads && !inf->pending_execs)
1453 /* No threads! Assume that maybe some outside agency is frobbing our
1454 task, and really look for new threads. If we can't find any, just tell
1455 the user to try again later. */
1457 inf_validate_procs (inf);
1458 if (!inf->threads && !inf->task->dead)
1459 error (_("There are no threads; try again later."));
1462 waiting_inf = inf;
1464 inf_debug (inf, "waiting for: %s", ptid.to_string ().c_str ());
1466 rewait:
1467 if (proc_wait_pid != inf->pid && !inf->no_wait)
1468 /* Always get information on events from the proc server. */
1470 inf_debug (inf, "requesting wait on pid %d", inf->pid);
1472 if (proc_wait_pid)
1473 /* The proc server is single-threaded, and only allows a single
1474 outstanding wait request, so we have to cancel the previous one. */
1476 inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
1477 interrupt_operation (proc_server, 0);
1480 err =
1481 proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
1482 if (err)
1483 warning (_("wait request failed: %s"), safe_strerror (err));
1484 else
1486 inf_debug (inf, "waits pending: %d", proc_waits_pending);
1487 proc_wait_pid = inf->pid;
1488 /* Even if proc_waits_pending was > 0 before, we still won't
1489 get any other replies, because it was either from a
1490 different INF, or a different process attached to INF --
1491 and the event port, which is the wait reply port, changes
1492 when you switch processes. */
1493 proc_waits_pending = 1;
1497 inf_clear_wait (inf);
1499 /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
1500 (3) wait reply from the proc server. */
1502 inf_debug (inf, "waiting for an event...");
1503 err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
1504 0, sizeof (struct msg), inf->event_port,
1505 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
1507 /* Re-suspend the task. */
1508 inf_suspend (inf);
1510 if (!inf->task && inf->pending_execs)
1511 /* When doing an exec, it's possible that the old task wasn't reused
1512 (e.g., setuid execs). So if the task seems to have disappeared,
1513 attempt to refetch it, as the pid should still be the same. */
1514 inf_set_pid (inf, inf->pid);
1516 if (err == EMACH_RCV_INTERRUPTED)
1517 inf_debug (inf, "interrupted");
1518 else if (err)
1519 error (_("Couldn't wait for an event: %s"), safe_strerror (err));
1520 else
1522 struct
1524 mach_msg_header_t hdr;
1525 mach_msg_type_t err_type;
1526 kern_return_t err;
1527 char noise[200];
1529 reply;
1531 inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
1533 /* Handle what we got. */
1534 if (!notify_server (&msg.hdr, &reply.hdr)
1535 && !exc_server (&msg.hdr, &reply.hdr)
1536 && !process_reply_server (&msg.hdr, &reply.hdr)
1537 && !msg_reply_server (&msg.hdr, &reply.hdr))
1538 /* Whatever it is, it's something strange. */
1539 error (_("Got a strange event, msg id = %d."), msg.hdr.msgh_id);
1541 if (reply.err)
1542 error (_("Handling event, msgid = %d: %s"),
1543 msg.hdr.msgh_id, safe_strerror (reply.err));
1546 if (inf->pending_execs)
1547 /* We're waiting for the inferior to finish execing. */
1549 struct inf_wait *w = &inf->wait;
1550 enum target_waitkind kind = w->status.kind ();
1552 if (kind == TARGET_WAITKIND_SPURIOUS)
1553 /* Since gdb is actually counting the number of times the inferior
1554 stops, expecting one stop per exec, we only return major events
1555 while execing. */
1557 w->suppress = 1;
1558 inf_debug (inf, "pending_execs, ignoring minor event");
1560 else if (kind == TARGET_WAITKIND_STOPPED
1561 && w->status.sig () == GDB_SIGNAL_TRAP)
1562 /* Ah hah! A SIGTRAP from the inferior while starting up probably
1563 means we've successfully completed an exec! */
1565 inf_debug (inf, "one pending exec completed");
1567 else if (kind == TARGET_WAITKIND_STOPPED)
1568 /* It's possible that this signal is because of a crashed process
1569 being handled by the hurd crash server; in this case, the process
1570 will have an extra task suspend, which we need to know about.
1571 Since the code in inf_resume that normally checks for this is
1572 disabled while INF->pending_execs, we do the check here instead. */
1573 inf_validate_task_sc (inf);
1576 if (inf->wait.suppress)
1577 /* Some totally spurious event happened that we don't consider
1578 worth returning to gdb. Just keep waiting. */
1580 inf_debug (inf, "suppressing return, rewaiting...");
1581 inf_resume (inf);
1582 goto rewait;
1585 /* Pass back out our results. */
1586 *status = inf->wait.status;
1588 thread = inf->wait.thread;
1589 if (thread)
1590 ptid = ptid_t (inf->pid, thread->tid, 0);
1591 else if (ptid == minus_one_ptid)
1592 thread = inf_tid_to_thread (inf, -1);
1593 else
1594 thread = inf_tid_to_thread (inf, ptid.lwp ());
1596 if (!thread || thread->port == MACH_PORT_NULL)
1598 /* TID is dead; try and find a new thread. */
1599 if (inf_update_procs (inf) && inf->threads)
1600 ptid = ptid_t (inf->pid, inf->threads->tid, 0); /* The first
1601 available
1602 thread. */
1603 else
1605 /* The process exited. */
1606 ptid = ptid_t (inf->pid);
1610 if (thread
1611 && ptid != minus_one_ptid
1612 && status->kind () != TARGET_WAITKIND_SPURIOUS
1613 && inf->pause_sc == 0 && thread->pause_sc == 0)
1614 /* If something actually happened to THREAD, make sure we
1615 suspend it. */
1617 thread->sc = 1;
1618 inf_update_suspends (inf);
1621 inf_debug (inf, "returning ptid = %s, %s",
1622 ptid.to_string ().c_str (),
1623 status->to_string ().c_str ());
1625 return ptid;
1629 /* The rpc handler called by exc_server. */
1630 kern_return_t
1631 S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
1632 thread_t thread_port, task_t task_port,
1633 int exception, int code, int subcode)
1635 struct inf *inf = waiting_inf;
1636 struct proc *thread = inf_port_to_thread (inf, thread_port);
1638 inf_debug (waiting_inf,
1639 "thread = %lu, task = %lu, exc = %d, code = %d, subcode = %d",
1640 thread_port, task_port, exception, code, subcode);
1642 if (!thread)
1643 /* We don't know about thread? */
1645 inf_update_procs (inf);
1646 thread = inf_port_to_thread (inf, thread_port);
1647 if (!thread)
1648 /* Give up, the generating thread is gone. */
1649 return 0;
1652 mach_port_deallocate (mach_task_self (), thread_port);
1653 mach_port_deallocate (mach_task_self (), task_port);
1655 if (!thread->aborted)
1656 /* THREAD hasn't been aborted since this exception happened (abortion
1657 clears any exception state), so it must be real. */
1659 /* Store away the details; this will destroy any previous info. */
1660 inf->wait.thread = thread;
1662 if (exception == EXC_BREAKPOINT)
1663 /* GDB likes to get SIGTRAP for breakpoints. */
1665 inf->wait.status.set_stopped (GDB_SIGNAL_TRAP);
1666 mach_port_deallocate (mach_task_self (), reply_port);
1668 else
1669 /* Record the exception so that we can forward it later. */
1671 if (thread->exc_port == port)
1673 inf_debug (waiting_inf, "Handler is thread exception port <%lu>",
1674 thread->saved_exc_port);
1675 inf->wait.exc.handler = thread->saved_exc_port;
1677 else
1679 inf_debug (waiting_inf, "Handler is task exception port <%lu>",
1680 inf->task->saved_exc_port);
1681 inf->wait.exc.handler = inf->task->saved_exc_port;
1682 gdb_assert (inf->task->exc_port == port);
1684 if (inf->wait.exc.handler != MACH_PORT_NULL)
1685 /* Add a reference to the exception handler. */
1686 mach_port_mod_refs (mach_task_self (),
1687 inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
1690 inf->wait.exc.exception = exception;
1691 inf->wait.exc.code = code;
1692 inf->wait.exc.subcode = subcode;
1693 inf->wait.exc.reply = reply_port;
1695 /* Exceptions are encoded in the signal space by putting
1696 them after _NSIG; this assumes they're positive (and not
1697 extremely large)! */
1698 inf->wait.status.set_stopped
1699 (gdb_signal_from_host (_NSIG + exception));
1702 else
1703 /* A suppressed exception, which ignore. */
1705 inf->wait.suppress = 1;
1706 mach_port_deallocate (mach_task_self (), reply_port);
1709 return 0;
1713 /* Fill in INF's wait field after a task has died without giving us more
1714 detailed information. */
1715 static void
1716 inf_task_died_status (struct inf *inf)
1718 warning (_("Pid %d died with unknown exit status, using SIGKILL."),
1719 inf->pid);
1720 inf->wait.status.set_signalled (GDB_SIGNAL_KILL);
1723 /* Notify server routines. The only real one is dead name notification. */
1724 kern_return_t
1725 do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
1727 struct inf *inf = waiting_inf;
1729 inf_debug (waiting_inf, "port = %lu", dead_port);
1731 if (inf->task && inf->task->port == dead_port)
1733 proc_debug (inf->task, "is dead");
1734 inf->task->port = MACH_PORT_NULL;
1735 if (proc_wait_pid == inf->pid)
1736 /* We have a wait outstanding on the process, which will return more
1737 detailed information, so delay until we get that. */
1738 inf->wait.suppress = 1;
1739 else
1740 /* We never waited for the process (maybe it wasn't a child), so just
1741 pretend it got a SIGKILL. */
1742 inf_task_died_status (inf);
1744 else
1746 struct proc *thread = inf_port_to_thread (inf, dead_port);
1748 if (thread)
1750 proc_debug (thread, "is dead");
1751 thread->port = MACH_PORT_NULL;
1754 if (inf->task->dead)
1755 /* Since the task is dead, its threads are dying with it. */
1756 inf->wait.suppress = 1;
1759 mach_port_deallocate (mach_task_self (), dead_port);
1760 inf->threads_up_to_date = 0; /* Just in case. */
1762 return 0;
1766 #define ILL_RPC(fun, ...) \
1767 extern "C" kern_return_t fun (__VA_ARGS__); \
1768 kern_return_t fun (__VA_ARGS__) \
1770 warning (_("illegal rpc: %s"), #fun); \
1771 return 0; \
1774 ILL_RPC (do_mach_notify_no_senders,
1775 mach_port_t notify, mach_port_mscount_t count)
1776 ILL_RPC (do_mach_notify_port_deleted,
1777 mach_port_t notify, mach_port_t name)
1778 ILL_RPC (do_mach_notify_msg_accepted,
1779 mach_port_t notify, mach_port_t name)
1780 ILL_RPC (do_mach_notify_port_destroyed,
1781 mach_port_t notify, mach_port_t name)
1782 ILL_RPC (do_mach_notify_send_once,
1783 mach_port_t notify)
1785 /* Process_reply server routines. We only use process_wait_reply. */
1787 kern_return_t
1788 S_proc_wait_reply (mach_port_t reply, kern_return_t err,
1789 int status, int sigcode, rusage_t rusage, pid_t pid)
1791 struct inf *inf = waiting_inf;
1793 inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
1794 err ? safe_strerror (err) : "0", pid, status, sigcode);
1796 if (err && proc_wait_pid && (!inf->task || !inf->task->port))
1797 /* Ack. The task has died, but the task-died notification code didn't
1798 tell anyone because it thought a more detailed reply from the
1799 procserver was forthcoming. However, we now learn that won't
1800 happen... So we have to act like the task just died, and this time,
1801 tell the world. */
1802 inf_task_died_status (inf);
1804 if (--proc_waits_pending == 0)
1805 /* PROC_WAIT_PID represents the most recent wait. We will always get
1806 replies in order because the proc server is single threaded. */
1807 proc_wait_pid = 0;
1809 inf_debug (inf, "waits pending now: %d", proc_waits_pending);
1811 if (err)
1813 if (err != EINTR)
1815 warning (_("Can't wait for pid %d: %s"),
1816 inf->pid, safe_strerror (err));
1817 inf->no_wait = 1;
1819 /* Since we can't see the inferior's signals, don't trap them. */
1820 gnu_target->inf_set_traced (inf, 0);
1823 else if (pid == inf->pid)
1825 inf->wait.status = host_status_to_waitstatus (status);
1826 if (inf->wait.status.kind () == TARGET_WAITKIND_STOPPED)
1827 /* The process has sent us a signal, and stopped itself in a sane
1828 state pending our actions. */
1830 inf_debug (inf, "process has stopped itself");
1831 inf->stopped = 1;
1834 else
1835 inf->wait.suppress = 1; /* Something odd happened. Ignore. */
1837 return 0;
1840 ILL_RPC (S_proc_setmsgport_reply,
1841 mach_port_t reply_port, kern_return_t return_code,
1842 mach_port_t oldmsgport)
1843 ILL_RPC (S_proc_getmsgport_reply,
1844 mach_port_t reply_port, kern_return_t return_code,
1845 mach_port_t msgports, mach_msg_type_name_t msgportsPoly)
1846 ILL_RPC (S_proc_pid2task_reply,
1847 mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
1848 ILL_RPC (S_proc_task2pid_reply,
1849 mach_port_t reply_port, kern_return_t return_code, pid_t pid)
1850 ILL_RPC (S_proc_task2proc_reply,
1851 mach_port_t reply_port, kern_return_t return_code,
1852 mach_port_t proc, mach_msg_type_name_t procPoly)
1853 ILL_RPC (S_proc_proc2task_reply,
1854 mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
1855 ILL_RPC (S_proc_pid2proc_reply,
1856 mach_port_t reply_port, kern_return_t return_code,
1857 mach_port_t proc, mach_msg_type_name_t procPoly)
1858 ILL_RPC (S_proc_getprocinfo_reply,
1859 mach_port_t reply_port, kern_return_t return_code,
1860 int flags, const_procinfo_t procinfo, mach_msg_type_number_t procinfoCnt,
1861 const_data_t threadwaits, mach_msg_type_number_t threadwaitsCnt)
1862 ILL_RPC (S_proc_getprocargs_reply,
1863 mach_port_t reply_port, kern_return_t return_code,
1864 const_data_t procargs, mach_msg_type_number_t procargsCnt)
1865 ILL_RPC (S_proc_getprocenv_reply,
1866 mach_port_t reply_port, kern_return_t return_code,
1867 const_data_t procenv, mach_msg_type_number_t procenvCnt)
1868 ILL_RPC (S_proc_getloginid_reply,
1869 mach_port_t reply_port, kern_return_t return_code, pid_t login_id)
1870 ILL_RPC (S_proc_getloginpids_reply,
1871 mach_port_t reply_port, kern_return_t return_code,
1872 const_pidarray_t pids, mach_msg_type_number_t pidsCnt)
1873 ILL_RPC (S_proc_getlogin_reply,
1874 mach_port_t reply_port, kern_return_t return_code, const_string_t logname)
1875 ILL_RPC (S_proc_getsid_reply,
1876 mach_port_t reply_port, kern_return_t return_code, pid_t sid)
1877 ILL_RPC (S_proc_getsessionpgids_reply,
1878 mach_port_t reply_port, kern_return_t return_code,
1879 const_pidarray_t pgidset, mach_msg_type_number_t pgidsetCnt)
1880 ILL_RPC (S_proc_getsessionpids_reply,
1881 mach_port_t reply_port, kern_return_t return_code,
1882 const_pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
1883 ILL_RPC (S_proc_getsidport_reply,
1884 mach_port_t reply_port, kern_return_t return_code,
1885 mach_port_t sessport)
1886 ILL_RPC (S_proc_getpgrp_reply,
1887 mach_port_t reply_port, kern_return_t return_code, pid_t pgrp)
1888 ILL_RPC (S_proc_getpgrppids_reply,
1889 mach_port_t reply_port, kern_return_t return_code,
1890 const_pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
1891 ILL_RPC (S_proc_get_tty_reply,
1892 mach_port_t reply_port, kern_return_t return_code, mach_port_t tty)
1893 ILL_RPC (S_proc_getnports_reply,
1894 mach_port_t reply_port, kern_return_t return_code,
1895 mach_msg_type_number_t nports)
1896 ILL_RPC (S_proc_is_important_reply,
1897 mach_port_t reply_port, kern_return_t return_code,
1898 boolean_t essential)
1899 ILL_RPC (S_proc_get_code_reply,
1900 mach_port_t reply_port, kern_return_t return_code,
1901 vm_address_t start_code, vm_address_t end_code)
1903 /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
1905 kern_return_t
1906 S_msg_sig_post_untraced_reply (mach_port_t reply, kern_return_t err)
1908 struct inf *inf = waiting_inf;
1910 if (err == EBUSY)
1911 /* EBUSY is what we get when the crash server has grabbed control of the
1912 process and doesn't like what signal we tried to send it. Just act
1913 like the process stopped (using a signal of 0 should mean that the
1914 *next* time the user continues, it will pass signal 0, which the crash
1915 server should like). */
1916 inf->wait.status.set_stopped (GDB_SIGNAL_0);
1917 else if (err)
1918 warning (_("Signal delivery failed: %s"), safe_strerror (err));
1920 if (err)
1921 /* We only get this reply when we've posted a signal to a process which we
1922 thought was stopped, and which we expected to continue after the signal.
1923 Given that the signal has failed for some reason, it's reasonable to
1924 assume it's still stopped. */
1925 inf->stopped = 1;
1926 else
1927 inf->wait.suppress = 1;
1929 return 0;
1932 ILL_RPC (S_msg_sig_post_reply,
1933 mach_port_t reply, kern_return_t err)
1935 /* Returns the number of messages queued for the receive right PORT. */
1936 static mach_port_msgcount_t
1937 port_msgs_queued (mach_port_t port)
1939 struct mach_port_status status;
1940 kern_return_t err =
1941 mach_port_get_receive_status (mach_task_self (), port, &status);
1943 if (err)
1944 return 0;
1945 else
1946 return status.mps_msgcount;
1950 /* Resume execution of the inferior process.
1952 If STEP is nonzero, single-step it.
1953 If SIGNAL is nonzero, give it that signal.
1955 TID STEP:
1956 -1 true Single step the current thread allowing other threads to run.
1957 -1 false Continue the current thread allowing other threads to run.
1958 X true Single step the given thread, don't allow any others to run.
1959 X false Continue the given thread, do not allow any others to run.
1960 (Where X, of course, is anything except -1)
1962 Note that a resume may not `take' if there are pending exceptions/&c
1963 still unprocessed from the last resume we did (any given resume may result
1964 in multiple events returned by wait). */
1966 void
1967 gnu_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1969 struct proc *step_thread = 0;
1970 int resume_all;
1971 struct inf *inf = gnu_current_inf;
1973 inf_debug (inf, "ptid = %s, step = %d, sig = %d",
1974 ptid.to_string ().c_str (), step, sig);
1976 inf_validate_procinfo (inf);
1978 if (sig != GDB_SIGNAL_0 || inf->stopped)
1980 if (sig == GDB_SIGNAL_0 && inf->nomsg)
1981 inf_continue (inf);
1982 else
1983 inf_signal (inf, sig);
1985 else if (inf->wait.exc.reply != MACH_PORT_NULL)
1986 /* We received an exception to which we have chosen not to forward, so
1987 abort the faulting thread, which will perhaps retake it. */
1989 proc_abort (inf->wait.thread, 1);
1990 warning (_("Aborting %s with unforwarded exception %s."),
1991 proc_string (inf->wait.thread),
1992 gdb_signal_to_name (inf->wait.status.sig ()));
1995 if (port_msgs_queued (inf->event_port))
1996 /* If there are still messages in our event queue, don't bother resuming
1997 the process, as we're just going to stop it right away anyway. */
1998 return;
2000 inf_update_procs (inf);
2002 /* A specific PTID means `step only this process id'. */
2003 resume_all = ptid == minus_one_ptid;
2005 if (resume_all)
2006 /* Allow all threads to run, except perhaps single-stepping one. */
2008 inf_debug (inf, "running all threads; tid = %d",
2009 inferior_ptid.pid ());
2010 ptid = inferior_ptid; /* What to step. */
2011 inf_set_threads_resume_sc (inf, 0, 1);
2013 else
2014 /* Just allow a single thread to run. */
2016 struct proc *thread = inf_tid_to_thread (inf, ptid.lwp ());
2018 if (!thread)
2019 error (_("Can't run single thread id %s: no such thread!"),
2020 target_pid_to_str (ptid).c_str ());
2021 inf_debug (inf, "running one thread: %s",
2022 ptid.to_string ().c_str ());
2023 inf_set_threads_resume_sc (inf, thread, 0);
2026 if (step)
2028 step_thread = inf_tid_to_thread (inf, ptid.lwp ());
2029 if (!step_thread)
2030 warning (_("Can't step thread id %s: no such thread."),
2031 target_pid_to_str (ptid).c_str ());
2032 else
2033 inf_debug (inf, "stepping thread: %s",
2034 ptid.to_string ().c_str ());
2036 if (step_thread != inf->step_thread)
2037 inf_set_step_thread (inf, step_thread);
2039 inf_debug (inf, "here we go...");
2040 inf_resume (inf);
2044 void
2045 gnu_nat_target::kill ()
2047 struct proc *task = gnu_current_inf->task;
2049 if (task)
2051 proc_debug (task, "terminating...");
2052 task_terminate (task->port);
2053 inf_set_pid (gnu_current_inf, -1);
2055 target_mourn_inferior (inferior_ptid);
2058 /* Clean up after the inferior dies. */
2059 void
2060 gnu_nat_target::mourn_inferior ()
2062 inf_debug (gnu_current_inf, "rip");
2063 inf_detach (gnu_current_inf);
2064 inf_child_target::mourn_inferior ();
2068 /* Fork an inferior process, and start debugging it. */
2070 /* Set INFERIOR_PID to the first thread available in the child, if any. */
2071 static int
2072 inf_pick_first_thread (void)
2074 if (gnu_current_inf->task && gnu_current_inf->threads)
2075 /* The first thread. */
2076 return gnu_current_inf->threads->tid;
2077 else
2078 /* What may be the next thread. */
2079 return next_thread_id;
2082 static struct inf *
2083 cur_inf (void)
2085 if (!gnu_current_inf)
2086 gnu_current_inf = make_inf ();
2087 return gnu_current_inf;
2090 static void
2091 gnu_ptrace_me (void)
2093 /* We're in the child; make this process stop as soon as it execs. */
2094 struct inf *inf = cur_inf ();
2095 inf_debug (inf, "tracing self");
2096 if (ptrace (PTRACE_TRACEME) != 0)
2097 trace_start_error_with_name ("ptrace");
2100 void
2101 gnu_nat_target::create_inferior (const char *exec_file,
2102 const std::string &allargs,
2103 char **env,
2104 int from_tty)
2106 struct inf *inf = cur_inf ();
2107 inferior *inferior = current_inferior ();
2108 int pid;
2110 inf_debug (inf, "creating inferior");
2112 if (!inferior->target_is_pushed (this))
2113 inferior->push_target (this);
2115 pid = fork_inferior (exec_file, allargs, env, gnu_ptrace_me,
2116 NULL, NULL, NULL, NULL);
2118 /* We have something that executes now. We'll be running through
2119 the shell at this point (if startup-with-shell is true), but the
2120 pid shouldn't change. */
2121 thread_info *thr = add_thread_silent (this, ptid_t (pid));
2122 switch_to_thread (thr);
2124 /* Attach to the now stopped child, which is actually a shell... */
2125 inf_debug (inf, "attaching to child: %d", pid);
2127 inf_attach (inf, pid);
2129 inf->pending_execs = 1;
2130 inf->nomsg = 1;
2131 inf->traced = 1;
2133 /* Now let the child run again, knowing that it will stop
2134 immediately because of the ptrace. */
2135 inf_resume (inf);
2137 /* We now have thread info. */
2138 thread_change_ptid (this, inferior_ptid,
2139 ptid_t (inf->pid, inf_pick_first_thread (), 0));
2141 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
2143 inf->pending_execs = 0;
2144 /* Get rid of the old shell threads. */
2145 prune_threads ();
2147 inf_validate_procinfo (inf);
2148 inf_update_signal_thread (inf);
2149 inf_set_traced (inf, inf->want_signals);
2151 /* Execing the process will have trashed our exception ports; steal them
2152 back (or make sure they're restored if the user wants that). */
2153 if (inf->want_exceptions)
2154 inf_steal_exc_ports (inf);
2155 else
2156 inf_restore_exc_ports (inf);
2160 /* Attach to process PID, then initialize for debugging it
2161 and wait for the trace-trap that results from attaching. */
2162 void
2163 gnu_nat_target::attach (const char *args, int from_tty)
2165 int pid;
2166 struct inf *inf = cur_inf ();
2167 struct inferior *inferior;
2169 pid = parse_pid_to_attach (args);
2171 if (pid == getpid ()) /* Trying to masturbate? */
2172 error (_("I refuse to debug myself!"));
2174 target_announce_attach (from_tty, pid);
2176 inf_debug (inf, "attaching to pid: %d", pid);
2178 inf_attach (inf, pid);
2180 inferior = current_inferior ();
2181 inferior->push_target (this);
2183 inferior_appeared (inferior, pid);
2184 inferior->attach_flag = true;
2186 inf_update_procs (inf);
2188 thread_info *thr = this->find_thread (ptid_t (pid, inf_pick_first_thread ()));
2189 switch_to_thread (thr);
2191 /* We have to initialize the terminal settings now, since the code
2192 below might try to restore them. */
2193 target_terminal::init ();
2195 /* If the process was stopped before we attached, make it continue the next
2196 time the user does a continue. */
2197 inf_validate_procinfo (inf);
2199 inf_update_signal_thread (inf);
2200 inf_set_traced (inf, inf->want_signals);
2202 #if 0 /* Do we need this? */
2203 renumber_threads (0); /* Give our threads reasonable names. */
2204 #endif
2208 /* Take a program previously attached to and detaches it.
2209 The program resumes execution and will no longer stop
2210 on signals, etc. We'd better not have left any breakpoints
2211 in the program or it'll die when it hits one. For this
2212 to work, it may be necessary for the process to have been
2213 previously attached. It *might* work if the program was
2214 started via fork. */
2215 void
2216 gnu_nat_target::detach (inferior *inf, int from_tty)
2218 target_announce_detach (from_tty);
2220 inf_detach (gnu_current_inf);
2222 switch_to_no_thread ();
2223 detach_inferior (inf);
2225 maybe_unpush_target ();
2229 void
2230 gnu_nat_target::stop (ptid_t ptid)
2232 error (_("stop target function not implemented"));
2235 bool
2236 gnu_nat_target::thread_alive (ptid_t ptid)
2238 inf_update_procs (gnu_current_inf);
2239 return !!inf_tid_to_thread (gnu_current_inf,
2240 ptid.lwp ());
2244 /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
2245 gdb's address space. Return 0 on failure; number of bytes read
2246 otherwise. */
2247 static int
2248 gnu_read_inferior (task_t task, CORE_ADDR addr, gdb_byte *myaddr, int length)
2250 kern_return_t err;
2251 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2252 vm_size_t aligned_length =
2253 (vm_size_t) round_page (addr + length) - low_address;
2254 pointer_t copied;
2255 mach_msg_type_number_t copy_count;
2257 /* Get memory from inferior with page aligned addresses. */
2258 err = vm_read (task, low_address, aligned_length, &copied, &copy_count);
2259 if (err)
2260 return 0;
2262 err = hurd_safe_copyin (myaddr, (void *) (addr - low_address + copied),
2263 length);
2264 if (err)
2266 warning (_("Read from inferior faulted: %s"), safe_strerror (err));
2267 length = 0;
2270 err = vm_deallocate (mach_task_self (), copied, copy_count);
2271 if (err)
2272 warning (_("gnu_read_inferior vm_deallocate failed: %s"),
2273 safe_strerror (err));
2275 return length;
2278 #define CHK_GOTO_OUT(str,ret) \
2279 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
2281 struct vm_region_list
2283 struct vm_region_list *next;
2284 vm_prot_t protection;
2285 vm_address_t start;
2286 vm_size_t length;
2289 struct obstack region_obstack;
2291 /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
2292 task's address space. */
2293 static int
2294 gnu_write_inferior (task_t task, CORE_ADDR addr,
2295 const gdb_byte *myaddr, int length)
2297 kern_return_t err;
2298 vm_address_t low_address = (vm_address_t) trunc_page (addr);
2299 vm_size_t aligned_length =
2300 (vm_size_t) round_page (addr + length) - low_address;
2301 pointer_t copied;
2302 mach_msg_type_number_t copy_count;
2303 int deallocate = 0;
2305 const char *errstr = "Bug in gnu_write_inferior";
2307 struct vm_region_list *region_element;
2308 struct vm_region_list *region_head = NULL;
2310 /* Get memory from inferior with page aligned addresses. */
2311 err = vm_read (task,
2312 low_address,
2313 aligned_length,
2314 &copied,
2315 &copy_count);
2316 CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
2318 deallocate++;
2320 err = hurd_safe_copyout ((void *) (addr - low_address + copied),
2321 myaddr, length);
2322 CHK_GOTO_OUT ("Write to inferior faulted", err);
2324 obstack_init (&region_obstack);
2326 /* Do writes atomically.
2327 First check for holes and unwritable memory. */
2329 vm_size_t remaining_length = aligned_length;
2330 vm_address_t region_address = low_address;
2332 struct vm_region_list *scan;
2334 while (region_address < low_address + aligned_length)
2336 vm_prot_t protection;
2337 vm_prot_t max_protection;
2338 vm_inherit_t inheritance;
2339 boolean_t shared;
2340 mach_port_t object_name;
2341 vm_offset_t offset;
2342 vm_size_t region_length = remaining_length;
2343 vm_address_t old_address = region_address;
2345 err = vm_region (task,
2346 &region_address,
2347 &region_length,
2348 &protection,
2349 &max_protection,
2350 &inheritance,
2351 &shared,
2352 &object_name,
2353 &offset);
2354 CHK_GOTO_OUT ("vm_region failed", err);
2356 /* Check for holes in memory. */
2357 if (old_address != region_address)
2359 warning (_("No memory at 0x%lx. Nothing written"),
2360 old_address);
2361 err = KERN_SUCCESS;
2362 length = 0;
2363 goto out;
2366 if (!(max_protection & VM_PROT_WRITE))
2368 warning (_("Memory at address 0x%lx is unwritable. "
2369 "Nothing written"),
2370 old_address);
2371 err = KERN_SUCCESS;
2372 length = 0;
2373 goto out;
2376 /* Chain the regions for later use. */
2377 region_element = XOBNEW (&region_obstack, struct vm_region_list);
2379 region_element->protection = protection;
2380 region_element->start = region_address;
2381 region_element->length = region_length;
2383 /* Chain the regions along with protections. */
2384 region_element->next = region_head;
2385 region_head = region_element;
2387 region_address += region_length;
2388 remaining_length = remaining_length - region_length;
2391 /* If things fail after this, we give up.
2392 Somebody is messing up inferior_task's mappings. */
2394 /* Enable writes to the chained vm regions. */
2395 for (scan = region_head; scan; scan = scan->next)
2397 if (!(scan->protection & VM_PROT_WRITE))
2399 err = vm_protect (task,
2400 scan->start,
2401 scan->length,
2402 FALSE,
2403 scan->protection | VM_PROT_WRITE);
2404 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2408 err = vm_write (task,
2409 low_address,
2410 copied,
2411 aligned_length);
2412 CHK_GOTO_OUT ("vm_write failed", err);
2414 /* Set up the original region protections, if they were changed. */
2415 for (scan = region_head; scan; scan = scan->next)
2417 if (!(scan->protection & VM_PROT_WRITE))
2419 err = vm_protect (task,
2420 scan->start,
2421 scan->length,
2422 FALSE,
2423 scan->protection);
2424 CHK_GOTO_OUT ("vm_protect: enable write failed", err);
2429 out:
2430 if (deallocate)
2432 obstack_free (&region_obstack, 0);
2434 (void) vm_deallocate (mach_task_self (),
2435 copied,
2436 copy_count);
2439 if (err != KERN_SUCCESS)
2441 warning (_("%s: %s"), errstr, mach_error_string (err));
2442 return 0;
2445 return length;
2450 /* Implement the to_xfer_partial target_ops method for
2451 TARGET_OBJECT_MEMORY. */
2453 static enum target_xfer_status
2454 gnu_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
2455 CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
2457 task_t task = (gnu_current_inf
2458 ? (gnu_current_inf->task
2459 ? gnu_current_inf->task->port : 0)
2460 : 0);
2461 int res;
2463 if (task == MACH_PORT_NULL)
2464 return TARGET_XFER_E_IO;
2466 if (writebuf != NULL)
2468 inf_debug (gnu_current_inf, "writing %s[%s] <-- %s",
2469 paddress (current_inferior ()->arch (), memaddr), pulongest (len),
2470 host_address_to_string (writebuf));
2471 res = gnu_write_inferior (task, memaddr, writebuf, len);
2473 else
2475 inf_debug (gnu_current_inf, "reading %s[%s] --> %s",
2476 paddress (current_inferior ()->arch (), memaddr), pulongest (len),
2477 host_address_to_string (readbuf));
2478 res = gnu_read_inferior (task, memaddr, readbuf, len);
2480 gdb_assert (res >= 0);
2481 if (res == 0)
2482 return TARGET_XFER_E_IO;
2483 else
2485 *xfered_len = (ULONGEST) res;
2486 return TARGET_XFER_OK;
2490 /* GNU does not have auxv, but we can at least fake the AT_ENTRY entry for PIE
2491 binaries. */
2492 static enum target_xfer_status
2493 gnu_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf,
2494 CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
2496 task_t task = (gnu_current_inf
2497 ? (gnu_current_inf->task
2498 ? gnu_current_inf->task->port : 0)
2499 : 0);
2500 process_t proc;
2501 kern_return_t err;
2502 vm_address_t entry;
2503 ElfW(auxv_t) auxv[2];
2505 if (task == MACH_PORT_NULL)
2506 return TARGET_XFER_E_IO;
2507 if (writebuf != NULL)
2508 return TARGET_XFER_E_IO;
2510 if (memaddr == sizeof (auxv))
2511 return TARGET_XFER_EOF;
2512 if (memaddr > sizeof (auxv))
2513 return TARGET_XFER_E_IO;
2515 err = proc_task2proc (proc_server, task, &proc);
2516 if (err != 0)
2517 return TARGET_XFER_E_IO;
2519 /* Get entry from proc server. */
2520 err = proc_get_entry (proc, &entry);
2521 if (err != 0)
2522 return TARGET_XFER_E_IO;
2524 /* Fake auxv entry. */
2525 auxv[0].a_type = AT_ENTRY;
2526 auxv[0].a_un.a_val = entry;
2527 auxv[1].a_type = AT_NULL;
2528 auxv[1].a_un.a_val = 0;
2530 inf_debug (gnu_current_inf, "reading auxv %s[%s] --> %s",
2531 paddress (current_inferior ()->arch (), memaddr), pulongest (len),
2532 host_address_to_string (readbuf));
2534 if (memaddr + len > sizeof (auxv))
2535 len = sizeof (auxv) - memaddr;
2537 memcpy (readbuf, (gdb_byte *) &auxv + memaddr, len);
2538 *xfered_len = len;
2540 return TARGET_XFER_OK;
2543 /* Target to_xfer_partial implementation. */
2545 enum target_xfer_status
2546 gnu_nat_target::xfer_partial (enum target_object object,
2547 const char *annex, gdb_byte *readbuf,
2548 const gdb_byte *writebuf, ULONGEST offset,
2549 ULONGEST len, ULONGEST *xfered_len)
2551 switch (object)
2553 case TARGET_OBJECT_MEMORY:
2554 return gnu_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
2555 case TARGET_OBJECT_AUXV:
2556 return gnu_xfer_auxv (readbuf, writebuf, offset, len, xfered_len);
2557 default:
2558 return TARGET_XFER_E_IO;
2562 /* Call FUNC on each memory region in the task. */
2565 gnu_nat_target::find_memory_regions (find_memory_region_ftype func,
2566 void *data)
2568 kern_return_t err;
2569 task_t task;
2570 vm_address_t region_address, last_region_address, last_region_end;
2571 vm_prot_t last_protection;
2573 if (gnu_current_inf == 0 || gnu_current_inf->task == 0)
2574 return 0;
2575 task = gnu_current_inf->task->port;
2576 if (task == MACH_PORT_NULL)
2577 return 0;
2579 region_address = last_region_address = last_region_end = VM_MIN_ADDRESS;
2580 last_protection = VM_PROT_NONE;
2581 while (region_address < VM_MAX_ADDRESS)
2583 vm_prot_t protection;
2584 vm_prot_t max_protection;
2585 vm_inherit_t inheritance;
2586 boolean_t shared;
2587 mach_port_t object_name;
2588 vm_offset_t offset;
2589 vm_size_t region_length = VM_MAX_ADDRESS - region_address;
2591 err = vm_region (task,
2592 &region_address,
2593 &region_length,
2594 &protection,
2595 &max_protection,
2596 &inheritance,
2597 &shared,
2598 &object_name,
2599 &offset);
2600 if (err == KERN_NO_SPACE)
2601 break;
2602 if (err != KERN_SUCCESS)
2604 warning (_("vm_region failed: %s"), mach_error_string (err));
2605 return -1;
2608 if (protection == last_protection && region_address == last_region_end)
2609 /* This region is contiguous with and indistinguishable from
2610 the previous one, so we just extend that one. */
2611 last_region_end = region_address += region_length;
2612 else
2614 /* This region is distinct from the last one we saw, so report
2615 that previous one. */
2616 if (last_protection != VM_PROT_NONE)
2617 (*func) (last_region_address,
2618 last_region_end - last_region_address,
2619 last_protection & VM_PROT_READ,
2620 last_protection & VM_PROT_WRITE,
2621 last_protection & VM_PROT_EXECUTE,
2622 1, /* MODIFIED is unknown, pass it as true. */
2623 false, /* No memory tags in the object file. */
2624 data);
2625 last_region_address = region_address;
2626 last_region_end = region_address += region_length;
2627 last_protection = protection;
2631 /* Report the final region. */
2632 if (last_region_end > last_region_address && last_protection != VM_PROT_NONE)
2633 (*func) (last_region_address, last_region_end - last_region_address,
2634 last_protection & VM_PROT_READ,
2635 last_protection & VM_PROT_WRITE,
2636 last_protection & VM_PROT_EXECUTE,
2637 1, /* MODIFIED is unknown, pass it as true. */
2638 false, /* No memory tags in the object file. */
2639 data);
2641 return 0;
2645 /* Return printable description of proc. */
2646 char *
2647 proc_string (struct proc *proc)
2649 static char tid_str[80];
2651 if (proc_is_task (proc))
2652 xsnprintf (tid_str, sizeof (tid_str), "process %d", proc->inf->pid);
2653 else
2654 xsnprintf (tid_str, sizeof (tid_str), "Thread %d.%d",
2655 proc->inf->pid, proc->tid);
2656 return tid_str;
2659 std::string
2660 gnu_nat_target::pid_to_str (ptid_t ptid)
2662 struct inf *inf = gnu_current_inf;
2663 int tid = ptid.lwp ();
2664 struct proc *thread = inf_tid_to_thread (inf, tid);
2666 if (thread)
2667 return proc_string (thread);
2668 else
2669 return string_printf ("bogus thread id %d", tid);
2673 /* User task commands. */
2675 static struct cmd_list_element *set_task_cmd_list = 0;
2676 static struct cmd_list_element *show_task_cmd_list = 0;
2677 /* User thread commands. */
2679 /* Commands with a prefix of `set/show thread'. */
2680 extern struct cmd_list_element *thread_cmd_list;
2681 struct cmd_list_element *set_thread_cmd_list = NULL;
2682 struct cmd_list_element *show_thread_cmd_list = NULL;
2684 /* Commands with a prefix of `set/show thread default'. */
2685 struct cmd_list_element *set_thread_default_cmd_list = NULL;
2686 struct cmd_list_element *show_thread_default_cmd_list = NULL;
2688 static int
2689 parse_int_arg (const char *args, const char *cmd_prefix)
2691 if (args)
2693 char *arg_end;
2694 int val = strtoul (args, &arg_end, 10);
2696 if (*args && *arg_end == '\0')
2697 return val;
2699 error (_("Illegal argument for \"%s\" command, should be an integer."),
2700 cmd_prefix);
2703 static int
2704 _parse_bool_arg (const char *args, const char *t_val, const char *f_val,
2705 const char *cmd_prefix)
2707 if (!args || strcmp (args, t_val) == 0)
2708 return 1;
2709 else if (strcmp (args, f_val) == 0)
2710 return 0;
2711 else
2712 error (_("Illegal argument for \"%s\" command, "
2713 "should be \"%s\" or \"%s\"."),
2714 cmd_prefix, t_val, f_val);
2717 #define parse_bool_arg(args, cmd_prefix) \
2718 _parse_bool_arg (args, "on", "off", cmd_prefix)
2720 static void
2721 check_empty (const char *args, const char *cmd_prefix)
2723 if (args)
2724 error (_("Garbage after \"%s\" command: `%s'"), cmd_prefix, args);
2727 /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
2728 static struct proc *
2729 cur_thread (void)
2731 struct inf *inf = cur_inf ();
2732 struct proc *thread = inf_tid_to_thread (inf,
2733 inferior_ptid.lwp ());
2734 if (!thread)
2735 error (_("No current thread."));
2736 return thread;
2739 /* Returns the current inferior, but signals an error if it has no task. */
2740 static struct inf *
2741 active_inf (void)
2743 struct inf *inf = cur_inf ();
2745 if (!inf->task)
2746 error (_("No current process."));
2747 return inf;
2751 static void
2752 set_task_pause_cmd (int arg, int from_tty)
2754 struct inf *inf = cur_inf ();
2755 int old_sc = inf->pause_sc;
2757 inf->pause_sc = arg;
2759 if (old_sc == 0 && inf->pause_sc != 0)
2760 /* If the task is currently unsuspended, immediately suspend it,
2761 otherwise wait until the next time it gets control. */
2762 gnu_target->inf_suspend (inf);
2765 static void
2766 set_task_pause_cmd (const char *args, int from_tty)
2768 set_task_pause_cmd (parse_bool_arg (args, "set task pause"), from_tty);
2771 static void
2772 show_task_pause_cmd (const char *args, int from_tty)
2774 struct inf *inf = cur_inf ();
2776 check_empty (args, "show task pause");
2777 gdb_printf ("The inferior task %s suspended while gdb has control.\n",
2778 inf->task
2779 ? (inf->pause_sc == 0 ? "isn't" : "is")
2780 : (inf->pause_sc == 0 ? "won't be" : "will be"));
2783 static void
2784 set_task_detach_sc_cmd (const char *args, int from_tty)
2786 cur_inf ()->detach_sc = parse_int_arg (args,
2787 "set task detach-suspend-count");
2790 static void
2791 show_task_detach_sc_cmd (const char *args, int from_tty)
2793 check_empty (args, "show task detach-suspend-count");
2794 gdb_printf ("The inferior task will be left with a "
2795 "suspend count of %d when detaching.\n",
2796 cur_inf ()->detach_sc);
2800 static void
2801 set_thread_default_pause_cmd (const char *args, int from_tty)
2803 struct inf *inf = cur_inf ();
2805 inf->default_thread_pause_sc =
2806 parse_bool_arg (args, "set thread default pause") ? 0 : 1;
2809 static void
2810 show_thread_default_pause_cmd (const char *args, int from_tty)
2812 struct inf *inf = cur_inf ();
2813 int sc = inf->default_thread_pause_sc;
2815 check_empty (args, "show thread default pause");
2816 gdb_printf ("New threads %s suspended while gdb has control%s.\n",
2817 sc ? "are" : "aren't",
2818 !sc && inf->pause_sc ? " (but the task is)" : "");
2821 static void
2822 set_thread_default_run_cmd (const char *args, int from_tty)
2824 struct inf *inf = cur_inf ();
2826 inf->default_thread_run_sc =
2827 parse_bool_arg (args, "set thread default run") ? 0 : 1;
2830 static void
2831 show_thread_default_run_cmd (const char *args, int from_tty)
2833 struct inf *inf = cur_inf ();
2835 check_empty (args, "show thread default run");
2836 gdb_printf ("New threads %s allowed to run.\n",
2837 inf->default_thread_run_sc == 0 ? "are" : "aren't");
2840 static void
2841 set_thread_default_detach_sc_cmd (const char *args, int from_tty)
2843 cur_inf ()->default_thread_detach_sc =
2844 parse_int_arg (args, "set thread default detach-suspend-count");
2847 static void
2848 show_thread_default_detach_sc_cmd (const char *args, int from_tty)
2850 check_empty (args, "show thread default detach-suspend-count");
2851 gdb_printf ("New threads will get a detach-suspend-count of %d.\n",
2852 cur_inf ()->default_thread_detach_sc);
2856 /* Steal a send right called NAME in the inferior task, and make it PROC's
2857 saved exception port. */
2858 void
2859 gnu_nat_target::steal_exc_port (struct proc *proc, mach_port_t name)
2861 kern_return_t err;
2862 mach_port_t port;
2863 mach_msg_type_name_t port_type;
2865 if (!proc || !proc->inf->task)
2866 error (_("No inferior task."));
2868 err = mach_port_extract_right (proc->inf->task->port,
2869 name, MACH_MSG_TYPE_COPY_SEND,
2870 &port, &port_type);
2871 if (err)
2872 error (_("Couldn't extract send right %lu from inferior: %s"),
2873 name, safe_strerror (err));
2875 if (proc->saved_exc_port)
2876 /* Get rid of our reference to the old one. */
2877 mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
2879 proc->saved_exc_port = port;
2881 if (!proc->exc_port)
2882 /* If PROC is a thread, we may not have set its exception port
2883 before. We can't use proc_steal_exc_port because it also sets
2884 saved_exc_port. */
2886 proc->exc_port = proc->inf->event_port;
2887 err = proc_set_exception_port (proc, proc->exc_port);
2888 error (_("Can't set exception port for %s: %s"),
2889 proc_string (proc), safe_strerror (err));
2893 static void
2894 set_task_exc_port_cmd (const char *args, int from_tty)
2896 struct inf *inf = cur_inf ();
2898 if (!args)
2899 error (_("No argument to \"set task exception-port\" command."));
2900 gnu_target->steal_exc_port (inf->task, parse_and_eval_address (args));
2903 static void
2904 set_stopped_cmd (const char *args, int from_tty)
2906 cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
2909 static void
2910 show_stopped_cmd (const char *args, int from_tty)
2912 struct inf *inf = active_inf ();
2914 check_empty (args, "show stopped");
2915 gdb_printf ("The inferior process %s stopped.\n",
2916 inf->stopped ? "is" : "isn't");
2919 static void
2920 set_sig_thread_cmd (const char *args, int from_tty)
2922 struct inf *inf = cur_inf ();
2924 if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
2925 error (_("Illegal argument to \"set signal-thread\" command.\n"
2926 "Should be a thread ID, or \"none\"."));
2928 if (strcmp (args, "none") == 0)
2929 inf->signal_thread = 0;
2930 else
2932 struct thread_info *tp = parse_thread_id (args, NULL);
2933 inf->signal_thread = inf_tid_to_thread (inf, tp->ptid.lwp ());
2937 static void
2938 show_sig_thread_cmd (const char *args, int from_tty)
2940 struct inf *inf = active_inf ();
2942 check_empty (args, "show signal-thread");
2943 if (inf->signal_thread)
2944 gdb_printf ("The signal thread is %s.\n",
2945 proc_string (inf->signal_thread));
2946 else
2947 gdb_printf ("There is no signal thread.\n");
2951 static void
2952 set_signals_cmd (int arg, int from_tty)
2954 struct inf *inf = cur_inf ();
2956 inf->want_signals = arg;
2958 if (inf->task && inf->want_signals != inf->traced)
2959 /* Make this take effect immediately in a running process. */
2960 gnu_target->inf_set_traced (inf, inf->want_signals);
2963 static void
2964 set_signals_cmd (const char *args, int from_tty)
2966 set_signals_cmd(parse_bool_arg (args, "set signals"), from_tty);
2969 static void
2970 show_signals_cmd (const char *args, int from_tty)
2972 struct inf *inf = cur_inf ();
2974 check_empty (args, "show signals");
2975 gdb_printf ("The inferior process's signals %s intercepted.\n",
2976 inf->task
2977 ? (inf->traced ? "are" : "aren't")
2978 : (inf->want_signals ? "will be" : "won't be"));
2981 static void
2982 set_exceptions_cmd (int arg, int from_tty)
2984 struct inf *inf = cur_inf ();
2986 /* Make this take effect immediately in a running process. */
2987 /* XXX */ ;
2989 inf->want_exceptions = arg;
2992 static void
2993 set_exceptions_cmd (const char *args, int from_tty)
2995 set_exceptions_cmd (parse_bool_arg (args, "set exceptions"), from_tty);
2998 static void
2999 show_exceptions_cmd (const char *args, int from_tty)
3001 struct inf *inf = cur_inf ();
3003 check_empty (args, "show exceptions");
3004 gdb_printf ("Exceptions in the inferior %s trapped.\n",
3005 inf->task
3006 ? (inf->want_exceptions ? "are" : "aren't")
3007 : (inf->want_exceptions ? "will be" : "won't be"));
3011 static void
3012 set_task_cmd (const char *args, int from_tty)
3014 gdb_printf ("\"set task\" must be followed by the name"
3015 " of a task property.\n");
3018 static void
3019 show_task_cmd (const char *args, int from_tty)
3021 struct inf *inf = cur_inf ();
3023 check_empty (args, "show task");
3025 show_signals_cmd (0, from_tty);
3026 show_exceptions_cmd (0, from_tty);
3027 show_task_pause_cmd (0, from_tty);
3029 if (inf->pause_sc == 0)
3030 show_thread_default_pause_cmd (0, from_tty);
3031 show_thread_default_run_cmd (0, from_tty);
3033 if (inf->task)
3035 show_stopped_cmd (0, from_tty);
3036 show_sig_thread_cmd (0, from_tty);
3039 if (inf->detach_sc != 0)
3040 show_task_detach_sc_cmd (0, from_tty);
3041 if (inf->default_thread_detach_sc != 0)
3042 show_thread_default_detach_sc_cmd (0, from_tty);
3046 static void
3047 set_noninvasive_cmd (const char *args, int from_tty)
3049 /* Invert the sense of the arg for each component. */
3050 int inv_arg = parse_bool_arg (args, "set noninvasive") ? 0 : 1;
3052 set_task_pause_cmd (inv_arg, from_tty);
3053 set_signals_cmd (inv_arg, from_tty);
3054 set_exceptions_cmd (inv_arg, from_tty);
3058 static void
3059 info_port_rights (const char *args, mach_port_type_t only)
3061 struct inf *inf = active_inf ();
3062 scoped_value_mark vmark;
3064 if (args)
3065 /* Explicit list of port rights. */
3067 while (*args)
3069 struct value *val = parse_to_comma_and_eval (&args);
3070 long right = value_as_long (val);
3071 error_t err =
3072 print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
3073 stdout);
3075 if (err)
3076 error (_("%ld: %s."), right, safe_strerror (err));
3079 else
3080 /* Print all of them. */
3082 error_t err =
3083 print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
3084 stdout);
3085 if (err)
3086 error (_("%s."), safe_strerror (err));
3090 static void
3091 info_send_rights_cmd (const char *args, int from_tty)
3093 info_port_rights (args, MACH_PORT_TYPE_SEND);
3096 static void
3097 info_recv_rights_cmd (const char *args, int from_tty)
3099 info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
3102 static void
3103 info_port_sets_cmd (const char *args, int from_tty)
3105 info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
3108 static void
3109 info_dead_names_cmd (const char *args, int from_tty)
3111 info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
3114 static void
3115 info_port_rights_cmd (const char *args, int from_tty)
3117 info_port_rights (args, ~0);
3121 static void
3122 add_task_commands (void)
3124 add_cmd ("pause", class_run, set_thread_default_pause_cmd, _("\
3125 Set whether the new threads are suspended while gdb has control.\n\
3126 This property normally has no effect because the whole task is\n\
3127 suspended, however, that may be disabled with \"set task pause off\".\n\
3128 The default value is \"off\"."),
3129 &set_thread_default_cmd_list);
3130 add_cmd ("pause", no_class, show_thread_default_pause_cmd, _("\
3131 Show whether new threads are suspended while gdb has control."),
3132 &show_thread_default_cmd_list);
3134 add_cmd ("run", class_run, set_thread_default_run_cmd, _("\
3135 Set whether new threads are allowed to run (once gdb has noticed them)."),
3136 &set_thread_default_cmd_list);
3137 add_cmd ("run", no_class, show_thread_default_run_cmd, _("\
3138 Show whether new threads are allowed to run (once gdb has noticed them)."),
3139 &show_thread_default_cmd_list);
3141 add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
3142 _("Set the default detach-suspend-count value for new threads."),
3143 &set_thread_default_cmd_list);
3144 add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
3145 _("Show the default detach-suspend-count value for new threads."),
3146 &show_thread_default_cmd_list);
3148 cmd_list_element *set_signals_cmd_
3149 = add_cmd ("signals", class_run, set_signals_cmd, _("\
3150 Set whether the inferior process's signals will be intercepted.\n\
3151 Mach exceptions (such as breakpoint traps) are not affected."),
3152 &setlist);
3153 add_alias_cmd ("sigs", set_signals_cmd_, class_run, 1, &setlist);
3155 cmd_list_element *show_signals_cmd_
3156 = add_cmd ("signals", no_class, show_signals_cmd, _("\
3157 Show whether the inferior process's signals will be intercepted."),
3158 &showlist);
3159 add_alias_cmd ("sigs", show_signals_cmd_, no_class, 1, &showlist);
3161 cmd_list_element *set_signal_thread_cmd_
3162 = add_cmd ("signal-thread", class_run, set_sig_thread_cmd, _("\
3163 Set the thread that gdb thinks is the libc signal thread.\n\
3164 This thread is run when delivering a signal to a non-stopped process."),
3165 &setlist);
3166 add_alias_cmd ("sigthread", set_signal_thread_cmd_, class_run, 1, &setlist);
3168 cmd_list_element *show_signal_thread_cmd_
3169 = add_cmd ("signal-thread", no_class, show_sig_thread_cmd, _("\
3170 Set the thread that gdb thinks is the libc signal thread."),
3171 &showlist);
3172 add_alias_cmd ("sigthread", show_signal_thread_cmd_, no_class, 1, &showlist);
3174 add_cmd ("stopped", class_run, set_stopped_cmd, _("\
3175 Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n\
3176 Stopped process will be continued by sending them a signal."),
3177 &setlist);
3178 add_cmd ("stopped", no_class, show_stopped_cmd, _("\
3179 Show whether gdb thinks the inferior process is stopped as with SIGSTOP."),
3180 &showlist);
3182 cmd_list_element *set_exceptions_cmd_
3183 = add_cmd ("exceptions", class_run, set_exceptions_cmd, _("\
3184 Set whether exceptions in the inferior process will be trapped.\n\
3185 When exceptions are turned off, neither breakpoints nor single-stepping\n\
3186 will work."), &setlist);
3187 /* Allow `set exc' despite conflict with `set exception-port'. */
3188 add_alias_cmd ("exc", set_exceptions_cmd_, class_run, 1, &setlist);
3190 add_cmd ("exceptions", no_class, show_exceptions_cmd, _("\
3191 Show whether exceptions in the inferior process will be trapped."),
3192 &showlist);
3194 add_prefix_cmd ("task", no_class, set_task_cmd,
3195 _("Command prefix for setting task attributes."),
3196 &set_task_cmd_list, 0, &setlist);
3197 add_prefix_cmd ("task", no_class, show_task_cmd,
3198 _("Command prefix for showing task attributes."),
3199 &show_task_cmd_list, 0, &showlist);
3201 add_cmd ("pause", class_run, set_task_pause_cmd, _("\
3202 Set whether the task is suspended while gdb has control.\n\
3203 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3204 until the next time the program is continued.\n\
3205 When setting this to \"off\", \"set thread default pause on\" can be\n\
3206 used to pause individual threads by default instead."),
3207 &set_task_cmd_list);
3208 add_cmd ("pause", no_class, show_task_pause_cmd,
3209 _("Show whether the task is suspended while gdb has control."),
3210 &show_task_cmd_list);
3212 add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
3213 _("Set the suspend count will leave on the thread when detaching."),
3214 &set_task_cmd_list);
3215 add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
3216 _("Show the suspend count will leave "
3217 "on the thread when detaching."),
3218 &show_task_cmd_list);
3220 cmd_list_element *set_task_exception_port_cmd_
3221 = add_cmd ("exception-port", no_class, set_task_exc_port_cmd, _("\
3222 Set the task exception port to which we forward exceptions.\n\
3223 The argument should be the value of the send right in the task."),
3224 &set_task_cmd_list);
3225 add_alias_cmd ("excp", set_task_exception_port_cmd_, no_class, 1,
3226 &set_task_cmd_list);
3227 add_alias_cmd ("exc-port", set_task_exception_port_cmd_, no_class, 1,
3228 &set_task_cmd_list);
3230 /* A convenient way of turning on all options require to noninvasively
3231 debug running tasks. */
3232 add_cmd ("noninvasive", no_class, set_noninvasive_cmd, _("\
3233 Set task options so that we interfere as little as possible.\n\
3234 This is the same as setting `task pause', `exceptions', and\n\
3235 `signals' to the opposite value."),
3236 &setlist);
3238 /* Commands to show information about the task's ports. */
3239 add_info ("send-rights", info_send_rights_cmd,
3240 _("Show information about the task's send rights."));
3241 add_info ("receive-rights", info_recv_rights_cmd,
3242 _("Show information about the task's receive rights."));
3243 cmd_list_element *port_rights_cmd
3244 = add_info ("port-rights", info_port_rights_cmd,
3245 _("Show information about the task's port rights."));
3246 cmd_list_element *port_sets_cmd
3247 = add_info ("port-sets", info_port_sets_cmd,
3248 _("Show information about the task's port sets."));
3249 add_info ("dead-names", info_dead_names_cmd,
3250 _("Show information about the task's dead names."));
3251 add_info_alias ("ports", port_rights_cmd, 1);
3252 add_info_alias ("port", port_rights_cmd, 1);
3253 add_info_alias ("psets", port_sets_cmd, 1);
3257 static void
3258 set_thread_pause_cmd (const char *args, int from_tty)
3260 struct proc *thread = cur_thread ();
3261 int old_sc = thread->pause_sc;
3263 thread->pause_sc = parse_bool_arg (args, "set thread pause");
3264 if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
3265 /* If the task is currently unsuspended, immediately suspend it,
3266 otherwise wait until the next time it gets control. */
3267 gnu_target->inf_suspend (thread->inf);
3270 static void
3271 show_thread_pause_cmd (const char *args, int from_tty)
3273 struct proc *thread = cur_thread ();
3274 int sc = thread->pause_sc;
3276 check_empty (args, "show task pause");
3277 gdb_printf ("Thread %s %s suspended while gdb has control%s.\n",
3278 proc_string (thread),
3279 sc ? "is" : "isn't",
3280 !sc && thread->inf->pause_sc ? " (but the task is)" : "");
3283 static void
3284 set_thread_run_cmd (const char *args, int from_tty)
3286 struct proc *thread = cur_thread ();
3288 thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
3291 static void
3292 show_thread_run_cmd (const char *args, int from_tty)
3294 struct proc *thread = cur_thread ();
3296 check_empty (args, "show thread run");
3297 gdb_printf ("Thread %s %s allowed to run.",
3298 proc_string (thread),
3299 thread->run_sc == 0 ? "is" : "isn't");
3302 static void
3303 set_thread_detach_sc_cmd (const char *args, int from_tty)
3305 cur_thread ()->detach_sc = parse_int_arg (args,
3306 "set thread detach-suspend-count");
3309 static void
3310 show_thread_detach_sc_cmd (const char *args, int from_tty)
3312 struct proc *thread = cur_thread ();
3314 check_empty (args, "show thread detach-suspend-count");
3315 gdb_printf ("Thread %s will be left with a suspend count"
3316 " of %d when detaching.\n",
3317 proc_string (thread),
3318 thread->detach_sc);
3321 static void
3322 set_thread_exc_port_cmd (const char *args, int from_tty)
3324 struct proc *thread = cur_thread ();
3326 if (!args)
3327 error (_("No argument to \"set thread exception-port\" command."));
3328 gnu_target->steal_exc_port (thread, parse_and_eval_address (args));
3331 #if 0
3332 static void
3333 show_thread_cmd (char *args, int from_tty)
3335 struct proc *thread = cur_thread ();
3337 check_empty (args, "show thread");
3338 show_thread_run_cmd (0, from_tty);
3339 show_thread_pause_cmd (0, from_tty);
3340 if (thread->detach_sc != 0)
3341 show_thread_detach_sc_cmd (0, from_tty);
3343 #endif
3345 static void
3346 thread_takeover_sc_cmd (const char *args, int from_tty)
3348 struct proc *thread = cur_thread ();
3350 thread_basic_info_data_t _info;
3351 thread_basic_info_t info = &_info;
3352 mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
3353 kern_return_t err
3354 = mach_thread_info (thread->port, THREAD_BASIC_INFO,
3355 (int *) &info, &info_len);
3356 if (err)
3357 error (("%s."), safe_strerror (err));
3358 thread->sc = info->suspend_count;
3359 if (from_tty)
3360 gdb_printf ("Suspend count was %d.\n", thread->sc);
3361 if (info != &_info)
3362 vm_deallocate (mach_task_self (), (vm_address_t) info,
3363 info_len * sizeof (int));
3367 static void
3368 add_thread_commands (void)
3370 add_setshow_prefix_cmd ("thread", no_class,
3371 _("Command prefix for setting thread properties."),
3372 _("Command prefix for showing thread properties."),
3373 &set_thread_cmd_list,
3374 &show_thread_cmd_list,
3375 &setlist, &showlist);
3377 add_setshow_prefix_cmd ("default", no_class,
3378 _("Command prefix for setting default thread properties."),
3379 _("Command prefix for showing default thread properties."),
3380 &set_thread_default_cmd_list,
3381 &show_thread_default_cmd_list,
3382 &set_thread_cmd_list, &show_thread_cmd_list);
3384 add_cmd ("pause", class_run, set_thread_pause_cmd, _("\
3385 Set whether the current thread is suspended while gdb has control.\n\
3386 A value of \"on\" takes effect immediately, otherwise nothing happens\n\
3387 until the next time the program is continued. This property normally\n\
3388 has no effect because the whole task is suspended, however, that may\n\
3389 be disabled with \"set task pause off\".\n\
3390 The default value is \"off\"."),
3391 &set_thread_cmd_list);
3392 add_cmd ("pause", no_class, show_thread_pause_cmd, _("\
3393 Show whether the current thread is suspended while gdb has control."),
3394 &show_thread_cmd_list);
3396 add_cmd ("run", class_run, set_thread_run_cmd,
3397 _("Set whether the current thread is allowed to run."),
3398 &set_thread_cmd_list);
3399 add_cmd ("run", no_class, show_thread_run_cmd,
3400 _("Show whether the current thread is allowed to run."),
3401 &show_thread_cmd_list);
3403 add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd, _("\
3404 Set the suspend count will leave on the thread when detaching.\n\
3405 Note that this is relative to suspend count when gdb noticed the thread;\n\
3406 use the `thread takeover-suspend-count' to force it to an absolute value."),
3407 &set_thread_cmd_list);
3408 add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd, _("\
3409 Show the suspend count will leave on the thread when detaching.\n\
3410 Note that this is relative to suspend count when gdb noticed the thread;\n\
3411 use the `thread takeover-suspend-count' to force it to an absolute value."),
3412 &show_thread_cmd_list);
3414 cmd_list_element *set_thread_exception_port_cmd_
3415 = add_cmd ("exception-port", no_class, set_thread_exc_port_cmd, _("\
3416 Set the thread exception port to which we forward exceptions.\n\
3417 This overrides the task exception port.\n\
3418 The argument should be the value of the send right in the task."),
3419 &set_thread_cmd_list);
3420 add_alias_cmd ("excp", set_thread_exception_port_cmd_, no_class, 1,
3421 &set_thread_cmd_list);
3422 add_alias_cmd ("exc-port", set_thread_exception_port_cmd_, no_class, 1,
3423 &set_thread_cmd_list);
3425 add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd, _("\
3426 Force the threads absolute suspend-count to be gdb's.\n\
3427 Prior to giving this command, gdb's thread suspend-counts are relative\n\
3428 to the thread's initial suspend-count when gdb notices the threads."),
3429 &thread_cmd_list);
3432 void _initialize_gnu_nat ();
3433 void
3434 _initialize_gnu_nat ()
3436 proc_server = getproc ();
3438 add_task_commands ();
3439 add_thread_commands ();
3440 add_setshow_boolean_cmd ("gnu-nat", class_maintenance,
3441 &gnu_debug_flag,
3442 _("Set debugging output for the gnu backend."),
3443 _("Show debugging output for the gnu backend."),
3444 NULL,
3445 NULL,
3446 NULL,
3447 &setdebuglist,
3448 &showdebuglist);
3451 #ifdef FLUSH_INFERIOR_CACHE
3453 /* When over-writing code on some machines the I-Cache must be flushed
3454 explicitly, because it is not kept coherent by the lazy hardware.
3455 This definitely includes breakpoints, for instance, or else we
3456 end up looping in mysterious Bpt traps. */
3458 void
3459 flush_inferior_icache (CORE_ADDR pc, int amount)
3461 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
3462 kern_return_t ret;
3464 ret = vm_machine_attribute (gnu_current_inf->task->port,
3466 amount,
3467 MATTR_CACHE,
3468 &flush);
3469 if (ret != KERN_SUCCESS)
3470 warning (_("Error flushing inferior's cache : %s"), safe_strerror (ret));
3472 #endif /* FLUSH_INFERIOR_CACHE */