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