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