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