1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 Contributed by AdaCore.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "gdbthread.h"
34 #include "event-top.h"
37 #include "exceptions.h"
38 #include "inf-child.h"
40 #include "arch-utils.h"
43 #include <sys/ptrace.h>
44 #include <sys/signal.h>
45 #include <machine/setjmp.h>
46 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/sysctl.h>
55 #include <sys/syscall.h>
57 #include <mach/mach_error.h>
58 #include <mach/mach_vm.h>
59 #include <mach/mach_init.h>
60 #include <mach/vm_map.h>
61 #include <mach/task.h>
62 #include <mach/mach_port.h>
63 #include <mach/thread_act.h>
64 #include <mach/port.h>
66 #include "darwin-nat.h"
69 Darwin kernel is Mach + BSD derived kernel. Note that they share the
70 same memory space and are linked together (ie there is no micro-kernel).
72 Although ptrace(2) is available on Darwin, it is not complete. We have
73 to use Mach calls to read and write memory and to modify registers. We
74 also use Mach to get inferior faults. As we cannot use select(2) or
75 signals with Mach port (the Mach communication channel), signals are
76 reported to gdb as an exception. Furthermore we detect death of the
77 inferior through a Mach notification message. This way we only wait
80 Some Mach documentation is available for Apple xnu source package or
84 #define PTRACE(CMD, PID, ADDR, SIG) \
85 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
87 extern boolean_t exc_server (mach_msg_header_t *in, mach_msg_header_t *out);
89 static void darwin_stop (ptid_t);
91 static void darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step,
92 enum target_signal signal);
93 static void darwin_resume (ptid_t ptid, int step,
94 enum target_signal signal);
96 static ptid_t darwin_wait_to (struct target_ops *ops, ptid_t ptid,
97 struct target_waitstatus *status, int options);
98 static ptid_t darwin_wait (ptid_t ptid, struct target_waitstatus *status);
100 static void darwin_mourn_inferior (struct target_ops *ops);
102 static int darwin_lookup_task (char *args, task_t * ptask, int *ppid);
104 static void darwin_kill_inferior (struct target_ops *ops);
106 static void darwin_ptrace_me (void);
108 static void darwin_ptrace_him (int pid);
110 static void darwin_create_inferior (struct target_ops *ops, char *exec_file,
111 char *allargs, char **env, int from_tty);
113 static void darwin_files_info (struct target_ops *ops);
115 static char *darwin_pid_to_str (struct target_ops *ops, ptid_t tpid);
117 static int darwin_thread_alive (struct target_ops *ops, ptid_t tpid);
119 /* Target operations for Darwin. */
120 static struct target_ops *darwin_ops;
122 /* Task identifier of gdb. */
123 static task_t gdb_task;
125 /* A copy of mach_host_self (). */
126 mach_port_t darwin_host_self;
128 /* Exception port. */
129 mach_port_t darwin_ex_port;
132 mach_port_t darwin_port_set;
135 static vm_size_t mach_page_size;
137 /* If Set, catch all mach exceptions (before they are converted to signals
139 static int enable_mach_exceptions;
141 /* Inferior that should report a fake stop event. */
142 static struct inferior *darwin_inf_fake_stop;
144 #define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1))
145 #define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1)
147 /* This controls output of inferior debugging. */
148 static int darwin_debug_flag = 0;
151 inferior_debug (int level, const char *fmt, ...)
155 if (darwin_debug_flag < level)
159 printf_unfiltered (_("[%d inferior]: "), getpid ());
160 vprintf_unfiltered (fmt, ap);
165 mach_check_error (kern_return_t ret, const char *file,
166 unsigned int line, const char *func)
168 if (ret == KERN_SUCCESS)
171 func = _("[UNKNOWN]");
173 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)\n"),
174 file, line, func, mach_error_string (ret), (unsigned long) ret);
178 unparse_exception_type (unsigned int i)
180 static char unknown_exception_buf[32];
185 return "EXC_BAD_ACCESS";
186 case EXC_BAD_INSTRUCTION:
187 return "EXC_BAD_INSTRUCTION";
189 return "EXC_ARITHMETIC";
191 return "EXC_EMULATION";
193 return "EXC_SOFTWARE";
195 return "EXC_BREAKPOINT";
197 return "EXC_SYSCALL";
198 case EXC_MACH_SYSCALL:
199 return "EXC_MACH_SYSCALL";
201 return "EXC_RPC_ALERT";
205 snprintf (unknown_exception_buf, 32, _("unknown (%d)"), i);
206 return unknown_exception_buf;
211 darwin_ptrace (const char *name,
212 int request, int pid, PTRACE_TYPE_ARG3 arg3, int arg4)
216 ret = ptrace (request, pid, (caddr_t) arg3, arg4);
218 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
219 name, pid, arg3, arg4, ret,
220 (ret != 0) ? safe_strerror (errno) : _("no error"));
225 cmp_thread_t (const void *l, const void *r)
227 thread_t tl = *(const thread_t *)l;
228 thread_t tr = *(const thread_t *)r;
229 return (int)(tl - tr);
233 darwin_check_new_threads (struct inferior *inf)
237 thread_array_t thread_list;
238 unsigned int new_nbr;
239 unsigned int old_nbr;
240 unsigned int new_ix, old_ix;
241 darwin_inferior *darwin_inf = inf->private;
242 VEC (darwin_thread_t) *thread_vec;
244 /* Get list of threads. */
245 kret = task_threads (darwin_inf->task, &thread_list, &new_nbr);
246 MACH_CHECK_ERROR (kret);
247 if (kret != KERN_SUCCESS)
252 qsort (thread_list, new_nbr, sizeof (thread_t), cmp_thread_t);
254 if (darwin_inf->threads)
255 old_nbr = VEC_length (darwin_thread_t, darwin_inf->threads);
259 /* Quick check for no changes. */
260 if (old_nbr == new_nbr)
262 for (i = 0; i < new_nbr; i++)
264 != VEC_index (darwin_thread_t, darwin_inf->threads, i)->gdb_port)
268 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
269 new_nbr * sizeof (int));
270 MACH_CHECK_ERROR (kret);
275 thread_vec = VEC_alloc (darwin_thread_t, new_nbr);
277 for (new_ix = 0, old_ix = 0; new_ix < new_nbr || old_ix < old_nbr;)
279 thread_t new_id = (new_ix < new_nbr) ?
280 thread_list[new_ix] : THREAD_NULL;
281 darwin_thread_t *old = (old_ix < old_nbr) ?
282 VEC_index (darwin_thread_t, darwin_inf->threads, old_ix) : NULL;
283 thread_t old_id = old ? old->gdb_port : THREAD_NULL;
286 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:%x old_id:%x\n"),
287 new_ix, new_nbr, old_ix, old_nbr, new_id, old_id);
289 if (old_id == new_id)
291 /* Thread still exist. */
292 VEC_safe_push (darwin_thread_t, thread_vec, old);
296 kret = mach_port_deallocate (gdb_task, old_id);
297 MACH_CHECK_ERROR (kret);
300 if (new_ix < new_nbr && new_id == MACH_PORT_DEAD)
302 /* Ignore dead ports.
303 In some weird cases, we might get dead ports. They should
304 correspond to dead thread so they could safely be ignored. */
308 if (new_ix < new_nbr && (old_ix == old_nbr || new_id < old_id))
310 /* A thread was created. */
311 struct thread_info *tp;
312 struct private_thread_info *pti;
314 pti = XZALLOC (struct private_thread_info);
315 pti->gdb_port = new_id;
316 pti->msg_state = DARWIN_RUNNING;
318 /* Add a new thread unless this is the first one ever met. */
319 if (!(old_nbr == 0 && new_ix == 0))
320 tp = add_thread_with_info (ptid_build (inf->pid, 0, new_id), pti);
323 tp = find_thread_ptid (ptid_build (inf->pid, 0, 0));
327 VEC_safe_push (darwin_thread_t, thread_vec, pti);
331 if (old_ix < old_nbr && (new_ix == new_nbr || new_id > old_id))
333 /* A thread was removed. */
334 delete_thread (ptid_build (inf->pid, 0, old_id));
335 kret = mach_port_deallocate (gdb_task, old_id);
336 MACH_CHECK_ERROR (kret);
343 if (darwin_inf->threads)
344 VEC_free (darwin_thread_t, darwin_inf->threads);
345 darwin_inf->threads = thread_vec;
347 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
348 new_nbr * sizeof (int));
349 MACH_CHECK_ERROR (kret);
353 find_inferior_task_it (struct inferior *inf, void *port_ptr)
355 return inf->private->task == *(task_t*)port_ptr;
359 find_inferior_notify_it (struct inferior *inf, void *port_ptr)
361 return inf->private->notify_port == *(task_t*)port_ptr;
364 /* Return an inferior by task port. */
365 static struct inferior *
366 darwin_find_inferior_by_task (task_t port)
368 return iterate_over_inferiors (&find_inferior_task_it, &port);
371 /* Return an inferior by notification port. */
372 static struct inferior *
373 darwin_find_inferior_by_notify (mach_port_t port)
375 return iterate_over_inferiors (&find_inferior_notify_it, &port);
378 /* Return a thread by port. */
379 static darwin_thread_t *
380 darwin_find_thread (struct inferior *inf, thread_t thread)
386 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
388 if (t->gdb_port == thread)
393 /* Suspend (ie stop) an inferior at Mach level. */
396 darwin_suspend_inferior (struct inferior *inf)
398 if (!inf->private->suspended)
402 kret = task_suspend (inf->private->task);
403 MACH_CHECK_ERROR (kret);
405 inf->private->suspended = 1;
409 /* Resume an inferior at Mach level. */
412 darwin_resume_inferior (struct inferior *inf)
414 if (inf->private->suspended)
418 kret = task_resume (inf->private->task);
419 MACH_CHECK_ERROR (kret);
421 inf->private->suspended = 0;
425 /* Iterator functions. */
428 darwin_suspend_inferior_it (struct inferior *inf, void *arg)
430 darwin_suspend_inferior (inf);
431 darwin_check_new_threads (inf);
436 darwin_resume_inferior_it (struct inferior *inf, void *arg)
438 darwin_resume_inferior (inf);
443 darwin_dump_message (mach_msg_header_t *hdr, int disp_body)
445 printf_unfiltered (_("message header:\n"));
446 printf_unfiltered (_(" bits: 0x%x\n"), hdr->msgh_bits);
447 printf_unfiltered (_(" size: 0x%x\n"), hdr->msgh_size);
448 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr->msgh_remote_port);
449 printf_unfiltered (_(" local-port: 0x%x\n"), hdr->msgh_local_port);
450 printf_unfiltered (_(" reserved: 0x%x\n"), hdr->msgh_reserved);
451 printf_unfiltered (_(" id: 0x%x\n"), hdr->msgh_id);
455 const unsigned char *data;
456 const unsigned long *ldata;
460 data = (unsigned char *)(hdr + 1);
461 size = hdr->msgh_size - sizeof (mach_msg_header_t);
463 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
465 mach_msg_body_t *bod = (mach_msg_body_t*)data;
466 mach_msg_port_descriptor_t *desc =
467 (mach_msg_port_descriptor_t *)(bod + 1);
470 printf_unfiltered (_("body: descriptor_count=%u\n"),
471 bod->msgh_descriptor_count);
472 data += sizeof (mach_msg_body_t);
473 size -= sizeof (mach_msg_body_t);
474 for (k = 0; k < bod->msgh_descriptor_count; k++)
475 switch (desc[k].type)
477 case MACH_MSG_PORT_DESCRIPTOR:
479 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
480 k, desc[k].type, desc[k].name, desc[k].disposition);
483 printf_unfiltered (_(" descr %d: type=%u\n"),
487 data += bod->msgh_descriptor_count
488 * sizeof (mach_msg_port_descriptor_t);
489 size -= bod->msgh_descriptor_count
490 * sizeof (mach_msg_port_descriptor_t);
491 ndr = (NDR_record_t *)(desc + bod->msgh_descriptor_count);
493 (_("NDR: mig=%02x if=%02x encod=%02x "
494 "int=%02x char=%02x float=%02x\n"),
495 ndr->mig_vers, ndr->if_vers, ndr->mig_encoding,
496 ndr->int_rep, ndr->char_rep, ndr->float_rep);
497 data += sizeof (NDR_record_t);
498 size -= sizeof (NDR_record_t);
501 printf_unfiltered (_(" data:"));
502 ldata = (const unsigned long *)data;
503 for (i = 0; i < size / sizeof (unsigned long); i++)
504 printf_unfiltered (" %08lx", ldata[i]);
505 printf_unfiltered (_("\n"));
510 darwin_decode_exception_message (mach_msg_header_t *hdr,
511 struct inferior **pinf,
512 darwin_thread_t **pthread)
514 mach_msg_body_t *bod = (mach_msg_body_t*)(hdr + 1);
515 mach_msg_port_descriptor_t *desc = (mach_msg_port_descriptor_t *)(bod + 1);
518 struct inferior *inf;
519 darwin_thread_t *thread;
521 thread_t thread_port;
525 /* Check message identifier. 2401 is exc. */
526 if (hdr->msgh_id != 2401)
529 /* Check message header. */
530 if (!(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX))
533 /* Check descriptors. */
534 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
535 + sizeof (*ndr) + 2 * sizeof (integer_t))
536 || bod->msgh_descriptor_count != 2
537 || desc[0].type != MACH_MSG_PORT_DESCRIPTOR
538 || desc[0].disposition != MACH_MSG_TYPE_MOVE_SEND
539 || desc[1].type != MACH_MSG_PORT_DESCRIPTOR
540 || desc[1].disposition != MACH_MSG_TYPE_MOVE_SEND)
543 /* Check data representation. */
544 ndr = (NDR_record_t *)(desc + 2);
545 if (ndr->mig_vers != NDR_PROTOCOL_2_0
546 || ndr->if_vers != NDR_PROTOCOL_2_0
547 || ndr->mig_encoding != NDR_record.mig_encoding
548 || ndr->int_rep != NDR_record.int_rep
549 || ndr->char_rep != NDR_record.char_rep
550 || ndr->float_rep != NDR_record.float_rep)
553 /* Ok, the hard work. */
554 data = (integer_t *)(ndr + 1);
556 /* Find process by port. */
557 task_port = desc[1].name;
558 thread_port = desc[0].name;
559 inf = darwin_find_inferior_by_task (task_port);
564 /* Find thread by port. */
565 /* Check for new threads. Do it early so that the port in the exception
566 message can be deallocated. */
567 darwin_check_new_threads (inf);
569 /* We got new rights to the task and the thread. Get rid of them. */
570 kret = mach_port_deallocate (mach_task_self (), task_port);
571 MACH_CHECK_ERROR (kret);
572 kret = mach_port_deallocate (mach_task_self (), thread_port);
573 MACH_CHECK_ERROR (kret);
575 thread = darwin_find_thread (inf, thread_port);
580 /* Finish decoding. */
581 gdb_assert (thread->msg_state == DARWIN_RUNNING);
582 thread->event.header = *hdr;
583 thread->event.thread_port = thread_port;
584 thread->event.task_port = task_port;
585 thread->event.ex_type = data[0];
586 thread->event.data_count = data[1];
588 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
589 + sizeof (*ndr) + 2 * sizeof (integer_t)
590 + data[1] * sizeof (integer_t)))
592 for (i = 0; i < data[1]; i++)
593 thread->event.ex_data[i] = data[2 + i];
595 thread->msg_state = DARWIN_MESSAGE;
601 darwin_encode_reply (mig_reply_error_t *reply, mach_msg_header_t *hdr,
604 mach_msg_header_t *rh = &reply->Head;
605 rh->msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(hdr->msgh_bits), 0);
606 rh->msgh_remote_port = hdr->msgh_remote_port;
607 rh->msgh_size = (mach_msg_size_t)sizeof(mig_reply_error_t);
608 rh->msgh_local_port = MACH_PORT_NULL;
609 rh->msgh_id = hdr->msgh_id + 100;
611 reply->NDR = NDR_record;
612 reply->RetCode = code;
616 darwin_send_reply (struct inferior *inf, darwin_thread_t *thread)
619 mig_reply_error_t reply;
621 darwin_encode_reply (&reply, &thread->event.header, KERN_SUCCESS);
623 kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
624 reply.Head.msgh_size, 0,
625 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
627 MACH_CHECK_ERROR (kret);
629 inf->private->pending_messages--;
633 darwin_resume_thread (struct inferior *inf, darwin_thread_t *thread,
634 int step, int nsignal)
640 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
641 thread->msg_state, thread->gdb_port, step, nsignal);
643 switch (thread->msg_state)
646 if (thread->event.ex_type == EXC_SOFTWARE
647 && thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
649 /* Either deliver a new signal or cancel the signal received. */
650 res = PTRACE (PT_THUPDATE, inf->pid,
651 (void *)(uintptr_t)thread->gdb_port, nsignal);
653 inferior_debug (1, _("ptrace THUP: res=%d\n"), res);
657 /* Note: ptrace is allowed only if the process is stopped.
658 Directly send the signal to the thread. */
659 res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal);
660 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
661 thread->gdb_port, nsignal, res);
662 thread->signaled = 1;
665 /* Set single step. */
666 inferior_debug (4, _("darwin_set_sstep (thread=%x, enable=%d)\n"),
667 thread->gdb_port, step);
668 darwin_set_sstep (thread->gdb_port, step);
669 thread->single_step = step;
671 darwin_send_reply (inf, thread);
672 thread->msg_state = DARWIN_RUNNING;
679 kret = thread_resume (thread->gdb_port);
680 MACH_CHECK_ERROR (kret);
682 thread->msg_state = DARWIN_RUNNING;
687 /* Resume all threads of the inferior. */
690 darwin_resume_inferior_threads (struct inferior *inf, int step, int nsignal)
692 darwin_thread_t *thread;
696 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread);
698 darwin_resume_thread (inf, thread, step, nsignal);
701 struct resume_inferior_threads_param
708 darwin_resume_inferior_threads_it (struct inferior *inf, void *param)
710 int step = ((struct resume_inferior_threads_param *)param)->step;
711 int nsignal = ((struct resume_inferior_threads_param *)param)->nsignal;
713 darwin_resume_inferior_threads (inf, step, nsignal);
718 /* Suspend all threads of INF. */
721 darwin_suspend_inferior_threads (struct inferior *inf)
723 darwin_thread_t *thread;
728 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread);
730 switch (thread->msg_state)
736 kret = thread_suspend (thread->gdb_port);
737 MACH_CHECK_ERROR (kret);
738 thread->msg_state = DARWIN_STOPPED;
744 darwin_resume (ptid_t ptid, int step, enum target_signal signal)
746 struct target_waitstatus status;
752 struct inferior *inf;
755 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
756 ptid_get_pid (ptid), ptid_get_tid (ptid), step, signal);
758 if (signal == TARGET_SIGNAL_0)
761 nsignal = target_signal_to_host (signal);
763 /* Don't try to single step all threads. */
765 ptid = inferior_ptid;
767 /* minus_one_ptid is RESUME_ALL. */
768 if (ptid_equal (ptid, minus_one_ptid))
770 struct resume_inferior_threads_param param;
772 param.nsignal = nsignal;
775 /* Resume threads. */
776 iterate_over_inferiors (darwin_resume_inferior_threads_it, ¶m);
778 iterate_over_inferiors (darwin_resume_inferior_it, NULL);
782 struct inferior *inf = find_inferior_pid (ptid_get_pid (ptid));
783 long tid = ptid_get_tid (ptid);
785 /* Stop the inferior (should be useless). */
786 darwin_suspend_inferior (inf);
789 darwin_resume_inferior_threads (inf, step, nsignal);
792 darwin_thread_t *thread;
794 /* Suspend threads of the task. */
795 darwin_suspend_inferior_threads (inf);
797 /* Resume the selected thread. */
798 thread = darwin_find_thread (inf, tid);
800 darwin_resume_thread (inf, thread, step, nsignal);
803 /* Resume the task. */
804 darwin_resume_inferior (inf);
809 darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step,
810 enum target_signal signal)
812 return darwin_resume (ptid, step, signal);
816 darwin_decode_message (mach_msg_header_t *hdr,
817 darwin_thread_t **pthread,
818 struct inferior **pinf,
819 struct target_waitstatus *status)
821 darwin_thread_t *thread;
822 struct inferior *inf;
824 /* Exception message. */
825 if (hdr->msgh_local_port == darwin_ex_port)
829 /* Decode message. */
830 res = darwin_decode_exception_message (hdr, &inf, &thread);
834 /* Should not happen... */
835 printf_unfiltered (_("darwin_wait: ill-formatted message (id=%x)\n"),
837 /* FIXME: send a failure reply? */
838 status->kind = TARGET_WAITKIND_SPURIOUS;
839 return minus_one_ptid;
843 inf->private->pending_messages++;
845 status->kind = TARGET_WAITKIND_STOPPED;
846 thread->msg_state = DARWIN_MESSAGE;
848 inferior_debug (4, _("darwin_wait: thread=%x, got %s\n"),
850 unparse_exception_type (thread->event.ex_type));
852 switch (thread->event.ex_type)
855 status->value.sig = TARGET_EXC_BAD_ACCESS;
857 case EXC_BAD_INSTRUCTION:
858 status->value.sig = TARGET_EXC_BAD_INSTRUCTION;
861 status->value.sig = TARGET_EXC_ARITHMETIC;
864 status->value.sig = TARGET_EXC_EMULATION;
867 if (thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
870 target_signal_from_host (thread->event.ex_data[1]);
871 inferior_debug (5, _(" (signal %d: %s)\n"),
872 thread->event.ex_data[1],
873 target_signal_to_name (status->value.sig));
875 /* If the thread is stopped because it has received a signal
876 that gdb has just sent, continue. */
877 if (thread->signaled)
879 thread->signaled = 0;
880 darwin_send_reply (inf, thread);
881 thread->msg_state = DARWIN_RUNNING;
882 status->kind = TARGET_WAITKIND_IGNORE;
886 status->value.sig = TARGET_EXC_SOFTWARE;
889 /* Many internal GDB routines expect breakpoints to be reported
890 as TARGET_SIGNAL_TRAP, and will report TARGET_EXC_BREAKPOINT
891 as a spurious signal. */
892 status->value.sig = TARGET_SIGNAL_TRAP;
895 status->value.sig = TARGET_SIGNAL_UNKNOWN;
899 return ptid_build (inf->pid, 0, thread->gdb_port);
905 inf = darwin_find_inferior_by_notify (hdr->msgh_local_port);
908 if (!inf->private->no_ptrace)
913 res = wait4 (inf->pid, &wstatus, 0, NULL);
914 if (res < 0 || res != inf->pid)
916 printf_unfiltered (_("wait4: res=%d: %s\n"),
917 res, safe_strerror (errno));
918 status->kind = TARGET_WAITKIND_SPURIOUS;
919 return minus_one_ptid;
921 if (WIFEXITED (wstatus))
923 status->kind = TARGET_WAITKIND_EXITED;
924 status->value.integer = WEXITSTATUS (wstatus);
928 status->kind = TARGET_WAITKIND_SIGNALLED;
929 status->value.sig = WTERMSIG (wstatus);
932 inferior_debug (4, _("darwin_wait: pid=%d exit, status=%x\n"),
935 /* Looks necessary on Leopard and harmless... */
936 wait4 (inf->pid, &wstatus, 0, NULL);
938 return ptid_build (inf->pid, 0, 0);
942 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf->pid);
943 status->kind = TARGET_WAITKIND_EXITED;
944 status->value.integer = 0; /* Don't know. */
945 return ptid_build (inf->pid, 0, 0);
949 printf_unfiltered (_("Bad local-port: %x\n"), hdr->msgh_local_port);
950 status->kind = TARGET_WAITKIND_SPURIOUS;
951 return minus_one_ptid;
955 cancel_breakpoint (ptid_t ptid)
957 /* Arrange for a breakpoint to be hit again later. We will handle
958 the current event, eventually we will resume this thread, and this
959 breakpoint will trap again.
961 If we do not do this, then we run the risk that the user will
962 delete or disable the breakpoint, but the thread will have already
965 struct regcache *regcache = get_thread_regcache (ptid);
966 struct gdbarch *gdbarch = get_regcache_arch (regcache);
969 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
970 if (breakpoint_inserted_here_p (pc))
972 inferior_debug (4, "cancel_breakpoint for thread %x\n",
973 ptid_get_tid (ptid));
975 /* Back up the PC if necessary. */
976 if (gdbarch_decr_pc_after_break (gdbarch))
977 regcache_write_pc (regcache, pc);
985 darwin_wait (ptid_t ptid, struct target_waitstatus *status)
990 mach_msg_header_t hdr;
993 mach_msg_header_t *hdr = &msgin.hdr;
995 darwin_thread_t *thread;
996 struct inferior *inf;
999 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1000 ptid_get_pid (ptid), ptid_get_tid (ptid));
1002 /* Handle fake stop events at first. */
1003 if (darwin_inf_fake_stop != NULL)
1005 inf = darwin_inf_fake_stop;
1006 darwin_inf_fake_stop = NULL;
1008 status->kind = TARGET_WAITKIND_STOPPED;
1009 status->value.sig = TARGET_SIGNAL_TRAP;
1010 thread = VEC_index (darwin_thread_t, inf->private->threads, 0);
1011 thread->msg_state = DARWIN_STOPPED;
1012 return ptid_build (inf->pid, 0, thread->gdb_port);
1017 /* set_sigint_trap (); */
1019 /* Wait for a message. */
1020 kret = mach_msg (&msgin.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0,
1021 sizeof (msgin.data), darwin_port_set, 0, MACH_PORT_NULL);
1023 /* clear_sigint_trap (); */
1025 if (kret == MACH_RCV_INTERRUPTED)
1027 status->kind = TARGET_WAITKIND_IGNORE;
1028 return minus_one_ptid;
1031 if (kret != MACH_MSG_SUCCESS)
1033 inferior_debug (5, _("mach_msg: ret=%x\n"), kret);
1034 status->kind = TARGET_WAITKIND_SPURIOUS;
1035 return minus_one_ptid;
1038 /* Debug: display message. */
1039 if (darwin_debug_flag > 10)
1040 darwin_dump_message (hdr, darwin_debug_flag > 11);
1042 res = darwin_decode_message (hdr, &thread, &inf, status);
1047 while (status->kind == TARGET_WAITKIND_IGNORE);
1049 /* Stop all tasks. */
1050 iterate_over_inferiors (darwin_suspend_inferior_it, NULL);
1052 /* Read pending messages. */
1055 struct target_waitstatus status2;
1058 kret = mach_msg (&msgin.hdr,
1059 MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
1060 sizeof (msgin.data), darwin_port_set, 1, MACH_PORT_NULL);
1062 if (kret == MACH_RCV_TIMED_OUT)
1064 if (kret != MACH_MSG_SUCCESS)
1067 (5, _("darwin_wait: mach_msg(pending) ret=%x\n"), kret);
1071 ptid2 = darwin_decode_message (hdr, &thread, &inf, &status2);
1073 if (inf != NULL && thread != NULL
1074 && thread->event.ex_type == EXC_BREAKPOINT)
1076 if (thread->single_step
1077 || cancel_breakpoint (ptid_build (inf->pid, 0, thread->gdb_port)))
1079 gdb_assert (thread->msg_state == DARWIN_MESSAGE);
1080 darwin_send_reply (inf, thread);
1081 thread->msg_state = DARWIN_RUNNING;
1085 (3, _("darwin_wait: thread %x hit a non-gdb breakpoint\n"),
1089 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1095 darwin_wait_to (struct target_ops *ops,
1096 ptid_t ptid, struct target_waitstatus *status, int options)
1098 return darwin_wait (ptid, status);
1102 darwin_stop (ptid_t t)
1104 struct inferior *inf = current_inferior ();
1106 /* FIXME: handle in no_ptrace mode. */
1107 gdb_assert (!inf->private->no_ptrace);
1108 kill (inf->pid, SIGINT);
1112 darwin_mourn_inferior (struct target_ops *ops)
1114 struct inferior *inf = current_inferior ();
1119 unpush_target (darwin_ops);
1121 /* Deallocate threads. */
1122 if (inf->private->threads)
1127 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1130 kret = mach_port_deallocate (gdb_task, t->gdb_port);
1131 MACH_CHECK_ERROR (kret);
1133 VEC_free (darwin_thread_t, inf->private->threads);
1134 inf->private->threads = NULL;
1137 kret = mach_port_move_member (gdb_task,
1138 inf->private->notify_port, MACH_PORT_NULL);
1139 gdb_assert (kret == KERN_SUCCESS);
1141 kret = mach_port_request_notification (gdb_task, inf->private->task,
1142 MACH_NOTIFY_DEAD_NAME, 0,
1144 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1146 /* This can fail if the task is dead. */
1147 inferior_debug (4, "task=%x, prev=%x, notify_port=%x\n",
1148 inf->private->task, prev, inf->private->notify_port);
1150 if (kret == KERN_SUCCESS)
1152 kret = mach_port_deallocate (gdb_task, prev);
1153 MACH_CHECK_ERROR (kret);
1156 kret = mach_port_destroy (gdb_task, inf->private->notify_port);
1157 MACH_CHECK_ERROR (kret);
1160 /* Deallocate saved exception ports. */
1161 for (i = 0; i < inf->private->exception_info.count; i++)
1163 kret = mach_port_deallocate
1164 (gdb_task, inf->private->exception_info.ports[i]);
1165 MACH_CHECK_ERROR (kret);
1167 inf->private->exception_info.count = 0;
1169 kret = mach_port_deallocate (gdb_task, inf->private->task);
1170 MACH_CHECK_ERROR (kret);
1172 xfree (inf->private);
1173 inf->private = NULL;
1175 generic_mourn_inferior ();
1179 darwin_reply_to_all_pending_messages (struct inferior *inf)
1185 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1188 if (t->msg_state == DARWIN_MESSAGE)
1189 darwin_resume_thread (inf, t, 0, 0);
1194 darwin_stop_inferior (struct inferior *inf)
1196 struct target_waitstatus wstatus;
1202 gdb_assert (inf != NULL);
1204 darwin_suspend_inferior (inf);
1206 darwin_reply_to_all_pending_messages (inf);
1208 if (inf->private->no_ptrace)
1211 res = kill (inf->pid, SIGSTOP);
1213 warning (_("cannot kill: %s\n"), safe_strerror (errno));
1215 /* Wait until the process is really stopped. */
1218 ptid = darwin_wait (inferior_ptid, &wstatus);
1219 if (wstatus.kind == TARGET_WAITKIND_STOPPED
1220 && wstatus.value.sig == TARGET_SIGNAL_STOP)
1225 static kern_return_t
1226 darwin_save_exception_ports (darwin_inferior *inf)
1230 inf->exception_info.count =
1231 sizeof (inf->exception_info.ports) / sizeof (inf->exception_info.ports[0]);
1233 kret = task_get_exception_ports
1234 (inf->task, EXC_MASK_ALL, inf->exception_info.masks,
1235 &inf->exception_info.count, inf->exception_info.ports,
1236 inf->exception_info.behaviors, inf->exception_info.flavors);
1240 static kern_return_t
1241 darwin_restore_exception_ports (darwin_inferior *inf)
1246 for (i = 0; i < inf->exception_info.count; i++)
1248 kret = task_set_exception_ports
1249 (inf->task, inf->exception_info.masks[i], inf->exception_info.ports[i],
1250 inf->exception_info.behaviors[i], inf->exception_info.flavors[i]);
1251 if (kret != KERN_SUCCESS)
1255 return KERN_SUCCESS;
1259 darwin_kill_inferior (struct target_ops *ops)
1261 struct inferior *inf = current_inferior ();
1262 struct target_waitstatus wstatus;
1268 if (ptid_equal (inferior_ptid, null_ptid))
1271 gdb_assert (inf != NULL);
1273 if (!inf->private->no_ptrace)
1275 darwin_stop_inferior (inf);
1277 res = PTRACE (PT_KILL, inf->pid, 0, 0);
1278 gdb_assert (res == 0);
1280 darwin_reply_to_all_pending_messages (inf);
1282 darwin_resume_inferior (inf);
1284 ptid = darwin_wait (inferior_ptid, &wstatus);
1288 kret = darwin_restore_exception_ports (inf->private);
1289 MACH_CHECK_ERROR (kret);
1291 darwin_reply_to_all_pending_messages (inf);
1293 darwin_resume_inferior (inf);
1295 res = kill (inf->pid, 9);
1297 ptid = darwin_wait (inferior_ptid, &wstatus);
1300 target_mourn_inferior ();
1304 darwin_attach_pid (struct inferior *inf)
1307 mach_port_t prev_port;
1309 mach_port_t prev_not;
1310 exception_mask_t mask;
1312 inf->private = XZALLOC (darwin_inferior);
1314 kret = task_for_pid (gdb_task, inf->pid, &inf->private->task);
1315 if (kret != KERN_SUCCESS)
1319 if (!inf->attach_flag)
1322 waitpid (inf->pid, &status, 0);
1325 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1326 " (please check gdb is setgid procmod)"),
1327 inf->pid, mach_error_string (kret), (unsigned long) kret);
1330 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1331 inf->private->task, inf->pid);
1333 if (darwin_ex_port == MACH_PORT_NULL)
1335 /* Create a port to get exceptions. */
1336 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
1338 gdb_assert (kret == KERN_SUCCESS);
1340 kret = mach_port_insert_right (gdb_task, darwin_ex_port, darwin_ex_port,
1341 MACH_MSG_TYPE_MAKE_SEND);
1342 gdb_assert (kret == KERN_SUCCESS);
1344 /* Create a port set and put ex_port in it. */
1345 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_PORT_SET,
1347 gdb_assert (kret == KERN_SUCCESS);
1349 kret = mach_port_move_member (gdb_task, darwin_ex_port, darwin_port_set);
1350 gdb_assert (kret == KERN_SUCCESS);
1353 /* Create a port to be notified when the child task terminates. */
1354 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
1355 &inf->private->notify_port);
1356 gdb_assert (kret == KERN_SUCCESS);
1358 kret = mach_port_move_member (gdb_task,
1359 inf->private->notify_port, darwin_port_set);
1360 gdb_assert (kret == KERN_SUCCESS);
1362 kret = mach_port_request_notification (gdb_task, inf->private->task,
1363 MACH_NOTIFY_DEAD_NAME, 0,
1364 inf->private->notify_port,
1365 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1367 gdb_assert (kret == KERN_SUCCESS);
1368 gdb_assert (prev_not == MACH_PORT_NULL);
1370 kret = darwin_save_exception_ports (inf->private);
1371 gdb_assert (kret == KERN_SUCCESS);
1373 /* Set exception port. */
1374 if (enable_mach_exceptions)
1375 mask = EXC_MASK_ALL;
1377 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
1378 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port,
1379 EXCEPTION_DEFAULT, THREAD_STATE_NONE);
1380 gdb_assert (kret == KERN_SUCCESS);
1382 push_target (darwin_ops);
1386 darwin_init_thread_list (struct inferior *inf)
1388 darwin_thread_t *thread;
1391 darwin_check_new_threads (inf);
1393 gdb_assert (inf->private->threads
1394 && VEC_length (darwin_thread_t, inf->private->threads) > 0);
1395 thread = VEC_index (darwin_thread_t, inf->private->threads, 0);
1397 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1399 new_ptid = ptid_build (inf->pid, 0, thread->gdb_port);
1400 thread_change_ptid (inferior_ptid, new_ptid);
1401 inferior_ptid = new_ptid;
1404 /* The child must synchronize with gdb: gdb must set the exception port
1405 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1406 FIXME: is there a lighter way ? */
1407 static int ptrace_fds[2];
1410 darwin_ptrace_me (void)
1415 /* Close write end point. */
1416 close (ptrace_fds[1]);
1418 /* Wait until gdb is ready. */
1419 res = read (ptrace_fds[0], &c, 1);
1420 gdb_assert (res == 0);
1421 close (ptrace_fds[0]);
1423 /* Get rid of privileges. */
1424 setegid (getgid ());
1427 PTRACE (PT_TRACE_ME, 0, 0, 0);
1429 /* Redirect signals to exception port. */
1430 PTRACE (PT_SIGEXC, 0, 0, 0);
1433 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1435 darwin_pre_ptrace (void)
1437 if (pipe (ptrace_fds) != 0)
1441 error (_("unable to create a pipe: %s"), safe_strerror (errno));
1446 darwin_ptrace_him (int pid)
1450 mach_port_t prev_port;
1452 struct inferior *inf = current_inferior ();
1454 darwin_attach_pid (inf);
1456 /* Let's the child run. */
1457 close (ptrace_fds[0]);
1458 close (ptrace_fds[1]);
1460 darwin_init_thread_list (inf);
1462 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
1466 darwin_create_inferior (struct target_ops *ops, char *exec_file,
1467 char *allargs, char **env, int from_tty)
1469 /* Do the hard work. */
1470 fork_inferior (exec_file, allargs, env, darwin_ptrace_me, darwin_ptrace_him,
1471 darwin_pre_ptrace, NULL);
1473 /* Return now in case of error. */
1474 if (ptid_equal (inferior_ptid, null_ptid))
1479 /* Attach to process PID, then initialize for debugging it
1480 and wait for the trace-trap that results from attaching. */
1482 darwin_attach (struct target_ops *ops, char *args, int from_tty)
1488 struct inferior *inf;
1492 error_no_arg (_("process-id to attach"));
1496 if (pid == getpid ()) /* Trying to masturbate? */
1497 error (_("I refuse to debug myself!"));
1501 char *exec_file = get_exec_file (0);
1504 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
1505 target_pid_to_str (pid_to_ptid (pid)));
1507 printf_unfiltered (_("Attaching to %s\n"),
1508 target_pid_to_str (pid_to_ptid (pid)));
1510 gdb_flush (gdb_stdout);
1513 if (pid == 0 || kill (pid, 0) < 0)
1514 error (_("Can't attach to process %d: %s (%d)"),
1515 pid, safe_strerror (errno), errno);
1517 inferior_ptid = pid_to_ptid (pid);
1518 inf = add_inferior (pid);
1519 inf->attach_flag = 1;
1520 /* Always add a main thread. */
1521 add_thread_silent (inferior_ptid);
1523 darwin_attach_pid (inf);
1525 darwin_suspend_inferior (inf);
1527 darwin_init_thread_list (inf);
1529 darwin_check_osabi (inf->private, ptid_get_tid (inferior_ptid));
1531 gdb_assert (darwin_inf_fake_stop == NULL);
1532 darwin_inf_fake_stop = inf;
1533 inf->private->no_ptrace = 1;
1536 /* Take a program previously attached to and detaches it.
1537 The program resumes execution and will no longer stop
1538 on signals, etc. We'd better not have left any breakpoints
1539 in the program or it'll die when it hits one. For this
1540 to work, it may be necessary for the process to have been
1541 previously attached. It *might* work if the program was
1542 started via fork. */
1544 darwin_detach (struct target_ops *ops, char *args, int from_tty)
1546 pid_t pid = ptid_get_pid (inferior_ptid);
1547 struct inferior *inf = current_inferior ();
1551 /* Display message. */
1554 char *exec_file = get_exec_file (0);
1557 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
1558 target_pid_to_str (pid_to_ptid (pid)));
1559 gdb_flush (gdb_stdout);
1562 /* If ptrace() is in use, stop the process. */
1563 if (!inf->private->no_ptrace)
1564 darwin_stop_inferior (inf);
1566 kret = darwin_restore_exception_ports (inf->private);
1567 MACH_CHECK_ERROR (kret);
1569 if (!inf->private->no_ptrace)
1571 res = PTRACE (PT_DETACH, inf->pid, 0, 0);
1573 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1574 inf->pid, safe_strerror (errno), errno);
1577 darwin_reply_to_all_pending_messages (inf);
1579 darwin_resume_inferior (inf);
1581 darwin_mourn_inferior (ops);
1585 darwin_files_info (struct target_ops *ops)
1590 darwin_pid_to_str (struct target_ops *ops, ptid_t ptid)
1592 static char buf[80];
1593 long tid = ptid_get_tid (ptid);
1597 snprintf (buf, sizeof (buf), _("Thread 0x%lx of process %u"),
1598 tid, ptid_get_pid (ptid));
1602 return normal_pid_to_str (ptid);
1606 darwin_thread_alive (struct target_ops *ops, ptid_t ptid)
1611 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1612 copy it to RDADDR in gdb's address space.
1613 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1614 to ADDR in inferior task's address space.
1615 Return 0 on failure; number of bytes read / writen otherwise. */
1617 darwin_read_write_inferior (task_t task, CORE_ADDR addr,
1618 char *rdaddr, const char *wraddr, int length)
1621 mach_vm_address_t offset = addr & (mach_page_size - 1);
1622 mach_vm_address_t low_address = (mach_vm_address_t) (addr - offset);
1623 mach_vm_size_t aligned_length = (mach_vm_size_t) PAGE_ROUND (offset + length);
1626 mach_vm_size_t remaining_length;
1627 mach_vm_address_t region_address;
1628 mach_vm_size_t region_length;
1630 inferior_debug (8, _("darwin_read_write_inferior(task=%x, %s, len=%d)\n"),
1631 task, core_addr_to_string (addr), length);
1633 /* Get memory from inferior with page aligned addresses */
1634 kret = mach_vm_read (task, low_address, aligned_length,
1635 &copied, ©_count);
1636 if (kret != KERN_SUCCESS)
1639 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1640 core_addr_to_string (addr), mach_error_string (kret));
1645 memcpy (rdaddr, (char *)copied + offset, length);
1650 memcpy ((char *)copied + offset, wraddr, length);
1652 /* Do writes atomically.
1653 First check for holes and unwritable memory. */
1654 for (region_address = low_address, remaining_length = aligned_length;
1655 region_address < low_address + aligned_length;
1656 region_address += region_length, remaining_length -= region_length)
1658 vm_region_submap_short_info_data_64_t info;
1659 mach_vm_address_t region_start = region_address;
1660 mach_msg_type_number_t count;
1661 natural_t region_depth;
1663 region_depth = 100000;
1664 count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1665 kret = mach_vm_region_recurse
1666 (task, ®ion_start, ®ion_length, ®ion_depth,
1667 (vm_region_recurse_info_t) &info, &count);
1669 if (kret != KERN_SUCCESS)
1671 inferior_debug (1, _("darwin_read_write_inferior: "
1672 "mach_vm_region_recurse failed at %s: %s\n"),
1673 core_addr_to_string (region_address),
1674 mach_error_string (kret));
1679 (9, _("darwin_read_write_inferior: "
1680 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1681 core_addr_to_string (region_address),
1682 core_addr_to_string (region_start),
1683 core_addr_to_string (region_length));
1685 /* Check for holes in memory */
1686 if (region_start > region_address)
1688 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1689 core_addr_to_string (region_address),
1690 core_addr_to_string (region_start),
1691 (unsigned)region_length);
1696 /* Adjust the length. */
1697 region_length -= (region_address - region_start);
1699 if (!(info.max_protection & VM_PROT_WRITE))
1701 kret = mach_vm_protect
1702 (task, region_address, region_length,
1703 TRUE, info.max_protection | VM_PROT_WRITE | VM_PROT_COPY);
1704 if (kret != KERN_SUCCESS)
1706 warning (_("darwin_read_write_inf: "
1707 "mach_vm_protect max failed at %s: %s"),
1708 core_addr_to_string (region_address),
1709 mach_error_string (kret));
1715 if (!(info.protection & VM_PROT_WRITE))
1717 kret = mach_vm_protect (task, region_address, region_length,
1718 FALSE, info.protection | VM_PROT_WRITE);
1719 if (kret != KERN_SUCCESS)
1721 warning (_("darwin_read_write_inf: "
1722 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1723 core_addr_to_string (region_address),
1724 (unsigned long)region_length, mach_error_string (kret));
1731 kret = mach_vm_write (task, low_address, copied, aligned_length);
1733 if (kret != KERN_SUCCESS)
1735 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1736 mach_error_string (kret));
1740 mach_vm_deallocate (mach_task_self (), copied, copy_count);
1745 /* Return 0 on failure, number of bytes handled otherwise. TARGET
1748 darwin_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int write,
1749 struct mem_attrib *attrib, struct target_ops *target)
1751 struct inferior *inf = current_inferior ();
1752 task_t task = inf->private->task;
1754 if (task == MACH_PORT_NULL)
1757 inferior_debug (8, _("darwin_xfer_memory(%s, %d, %c)\n"),
1758 core_addr_to_string (memaddr), len, write ? 'w' : 'r');
1761 return darwin_read_write_inferior (task, memaddr, NULL, myaddr, len);
1763 return darwin_read_write_inferior (task, memaddr, myaddr, NULL, len);
1767 darwin_xfer_partial (struct target_ops *ops,
1768 enum target_object object, const char *annex,
1769 gdb_byte *readbuf, const gdb_byte *writebuf,
1770 ULONGEST offset, LONGEST len)
1772 struct inferior *inf = current_inferior ();
1775 (8, _("darwin_xfer_partial(%s, %d, rbuf=%s, wbuf=%s) pid=%u\n"),
1776 core_addr_to_string (offset), (int)len,
1777 host_address_to_string (readbuf), host_address_to_string (writebuf),
1780 if (object != TARGET_OBJECT_MEMORY)
1783 return darwin_read_write_inferior (inf->private->task, offset,
1784 readbuf, writebuf, len);
1788 set_enable_mach_exceptions (char *args, int from_tty,
1789 struct cmd_list_element *c)
1791 if (!ptid_equal (inferior_ptid, null_ptid))
1793 struct inferior *inf = current_inferior ();
1794 exception_mask_t mask;
1797 if (enable_mach_exceptions)
1798 mask = EXC_MASK_ALL;
1801 darwin_restore_exception_ports (inf->private);
1802 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
1804 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port,
1805 EXCEPTION_DEFAULT, THREAD_STATE_NONE);
1806 MACH_CHECK_ERROR (kret);
1811 darwin_pid_to_exec_file (int pid)
1816 path = xmalloc (MAXPATHLEN);
1817 make_cleanup (xfree, path);
1819 res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, MAXPATHLEN);
1827 darwin_get_ada_task_ptid (long lwp, long thread)
1832 struct inferior *inf = current_inferior ();
1834 mach_port_name_array_t names;
1835 mach_msg_type_number_t names_count;
1836 mach_port_type_array_t types;
1837 mach_msg_type_number_t types_count;
1840 /* First linear search. */
1842 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1844 if (t->inf_port == lwp)
1845 return ptid_build (ptid_get_pid (inferior_ptid), 0, t->gdb_port);
1847 /* Maybe the port was never extract. Do it now. */
1849 /* First get inferior port names. */
1850 kret = mach_port_names (inf->private->task, &names, &names_count, &types,
1852 MACH_CHECK_ERROR (kret);
1853 if (kret != KERN_SUCCESS)
1856 /* For each name, copy the right in the gdb space and then compare with
1857 our view of the inferior threads. We don't forget to deallocate the
1859 for (i = 0; i < names_count; i++)
1861 mach_port_t local_name;
1862 mach_msg_type_name_t local_type;
1864 /* We just need to know the corresponding name in gdb name space.
1865 So extract and deallocate the right. */
1866 kret = mach_port_extract_right (inf->private->task, names[i],
1867 MACH_MSG_TYPE_COPY_SEND,
1868 &local_name, &local_type);
1869 if (kret != KERN_SUCCESS)
1871 mach_port_deallocate (gdb_task, local_name);
1874 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1876 if (t->gdb_port == local_name)
1878 t->inf_port = names[i];
1879 if (names[i] == lwp)
1884 vm_deallocate (gdb_task, (vm_address_t) names,
1885 names_count * sizeof (mach_port_t));
1888 return ptid_build (ptid_get_pid (inferior_ptid), 0, res);
1894 darwin_supports_multi_process (void)
1900 _initialize_darwin_inferior (void)
1904 gdb_task = mach_task_self ();
1905 darwin_host_self = mach_host_self ();
1907 /* Read page size. */
1908 kret = host_page_size (darwin_host_self, &mach_page_size);
1909 if (kret != KERN_SUCCESS)
1911 mach_page_size = 0x1000;
1912 MACH_CHECK_ERROR (kret);
1915 darwin_ops = inf_child_target ();
1917 darwin_ops->to_shortname = "darwin-child";
1918 darwin_ops->to_longname = _("Darwin child process");
1919 darwin_ops->to_doc =
1920 _("Darwin child process (started by the \"run\" command).");
1921 darwin_ops->to_create_inferior = darwin_create_inferior;
1922 darwin_ops->to_attach = darwin_attach;
1923 darwin_ops->to_attach_no_wait = 0;
1924 darwin_ops->to_detach = darwin_detach;
1925 darwin_ops->to_files_info = darwin_files_info;
1926 darwin_ops->to_wait = darwin_wait_to;
1927 darwin_ops->to_mourn_inferior = darwin_mourn_inferior;
1928 darwin_ops->to_kill = darwin_kill_inferior;
1929 darwin_ops->to_stop = darwin_stop;
1930 darwin_ops->to_resume = darwin_resume_to;
1931 darwin_ops->to_thread_alive = darwin_thread_alive;
1932 darwin_ops->to_pid_to_str = darwin_pid_to_str;
1933 darwin_ops->to_pid_to_exec_file = darwin_pid_to_exec_file;
1934 darwin_ops->to_load = NULL;
1935 darwin_ops->deprecated_xfer_memory = darwin_xfer_memory;
1936 darwin_ops->to_xfer_partial = darwin_xfer_partial;
1937 darwin_ops->to_supports_multi_process = darwin_supports_multi_process;
1938 darwin_ops->to_get_ada_task_ptid = darwin_get_ada_task_ptid;
1940 darwin_complete_target (darwin_ops);
1942 add_target (darwin_ops);
1944 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
1947 add_setshow_zinteger_cmd ("darwin", class_obscure,
1948 &darwin_debug_flag, _("\
1949 Set if printing inferior communication debugging statements."), _("\
1950 Show if printing inferior communication debugging statements."), NULL,
1952 &setdebuglist, &showdebuglist);
1954 add_setshow_boolean_cmd ("mach-exceptions", class_support,
1955 &enable_mach_exceptions, _("\
1956 Set if mach exceptions are caught."), _("\
1957 Show if mach exceptions are caught."), _("\
1958 When this mode is on, all low level exceptions are reported before being\n\
1959 reported by the kernel."),
1960 &set_enable_mach_exceptions, NULL,
1961 &setlist, &showlist);