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