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