1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008-2012 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"
42 #include "bfd/mach-o.h"
44 #include <sys/ptrace.h>
45 #include <sys/signal.h>
46 #include <machine/setjmp.h>
47 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/sysctl.h>
56 #include <sys/syscall.h>
59 #include <mach/mach_error.h>
60 #include <mach/mach_vm.h>
61 #include <mach/mach_init.h>
62 #include <mach/vm_map.h>
63 #include <mach/task.h>
64 #include <mach/mach_port.h>
65 #include <mach/thread_act.h>
66 #include <mach/port.h>
68 #include "darwin-nat.h"
71 Darwin kernel is Mach + BSD derived kernel. Note that they share the
72 same memory space and are linked together (ie there is no micro-kernel).
74 Although ptrace(2) is available on Darwin, it is not complete. We have
75 to use Mach calls to read and write memory and to modify registers. We
76 also use Mach to get inferior faults. As we cannot use select(2) or
77 signals with Mach port (the Mach communication channel), signals are
78 reported to gdb as an exception. Furthermore we detect death of the
79 inferior through a Mach notification message. This way we only wait
82 Some Mach documentation is available for Apple xnu source package or
86 #define PTRACE(CMD, PID, ADDR, SIG) \
87 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
89 extern boolean_t
exc_server (mach_msg_header_t
*in
, mach_msg_header_t
*out
);
91 static void darwin_stop (ptid_t
);
93 static void darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
94 enum target_signal signal
);
95 static void darwin_resume (ptid_t ptid
, int step
,
96 enum target_signal signal
);
98 static ptid_t
darwin_wait_to (struct target_ops
*ops
, ptid_t ptid
,
99 struct target_waitstatus
*status
, int options
);
100 static ptid_t
darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
);
102 static void darwin_mourn_inferior (struct target_ops
*ops
);
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;
150 /* Create a __TEXT __info_plist section in the executable so that gdb could
151 be signed. This is required to get an authorization for task_for_pid.
153 Once gdb is built, you can either:
154 * make it setgid procmod
155 * or codesign it with any system-trusted signing authority.
156 See taskgated(8) for details. */
157 static const unsigned char info_plist
[]
158 __attribute__ ((section ("__TEXT,__info_plist"),used
)) =
159 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
160 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
161 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
162 "<plist version=\"1.0\">\n"
164 " <key>CFBundleIdentifier</key>\n"
165 " <string>org.gnu.gdb</string>\n"
166 " <key>CFBundleName</key>\n"
167 " <string>gdb</string>\n"
168 " <key>CFBundleVersion</key>\n"
169 " <string>1.0</string>\n"
170 " <key>SecTaskAccess</key>\n"
172 " <string>allowed</string>\n"
173 " <string>debug</string>\n"
179 inferior_debug (int level
, const char *fmt
, ...)
183 if (darwin_debug_flag
< level
)
187 printf_unfiltered (_("[%d inferior]: "), getpid ());
188 vprintf_unfiltered (fmt
, ap
);
193 mach_check_error (kern_return_t ret
, const char *file
,
194 unsigned int line
, const char *func
)
196 if (ret
== KERN_SUCCESS
)
199 func
= _("[UNKNOWN]");
201 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"),
202 file
, line
, func
, mach_error_string (ret
), (unsigned long) ret
);
206 unparse_exception_type (unsigned int i
)
208 static char unknown_exception_buf
[32];
213 return "EXC_BAD_ACCESS";
214 case EXC_BAD_INSTRUCTION
:
215 return "EXC_BAD_INSTRUCTION";
217 return "EXC_ARITHMETIC";
219 return "EXC_EMULATION";
221 return "EXC_SOFTWARE";
223 return "EXC_BREAKPOINT";
225 return "EXC_SYSCALL";
226 case EXC_MACH_SYSCALL
:
227 return "EXC_MACH_SYSCALL";
229 return "EXC_RPC_ALERT";
233 snprintf (unknown_exception_buf
, 32, _("unknown (%d)"), i
);
234 return unknown_exception_buf
;
238 /* Set errno to zero, and then call ptrace with the given arguments.
239 If inferior debugging traces are on, then also print a debug
242 The returned value is the same as the value returned by ptrace,
243 except in the case where that value is -1 but errno is zero.
244 This case is documented to be a non-error situation, so we
245 return zero in that case. */
248 darwin_ptrace (const char *name
,
249 int request
, int pid
, PTRACE_TYPE_ARG3 arg3
, int arg4
)
254 ret
= ptrace (request
, pid
, (caddr_t
) arg3
, arg4
);
255 if (ret
== -1 && errno
== 0)
258 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
259 name
, pid
, arg3
, arg4
, ret
,
260 (ret
!= 0) ? safe_strerror (errno
) : _("no error"));
265 cmp_thread_t (const void *l
, const void *r
)
267 thread_t tl
= *(const thread_t
*)l
;
268 thread_t tr
= *(const thread_t
*)r
;
269 return (int)(tl
- tr
);
273 darwin_check_new_threads (struct inferior
*inf
)
277 thread_array_t thread_list
;
278 unsigned int new_nbr
;
279 unsigned int old_nbr
;
280 unsigned int new_ix
, old_ix
;
281 darwin_inferior
*darwin_inf
= inf
->private;
282 VEC (darwin_thread_t
) *thread_vec
;
284 /* Get list of threads. */
285 kret
= task_threads (darwin_inf
->task
, &thread_list
, &new_nbr
);
286 MACH_CHECK_ERROR (kret
);
287 if (kret
!= KERN_SUCCESS
)
292 qsort (thread_list
, new_nbr
, sizeof (thread_t
), cmp_thread_t
);
294 if (darwin_inf
->threads
)
295 old_nbr
= VEC_length (darwin_thread_t
, darwin_inf
->threads
);
299 /* Quick check for no changes. */
300 if (old_nbr
== new_nbr
)
302 for (i
= 0; i
< new_nbr
; i
++)
304 != VEC_index (darwin_thread_t
, darwin_inf
->threads
, i
)->gdb_port
)
308 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
309 new_nbr
* sizeof (int));
310 MACH_CHECK_ERROR (kret
);
315 thread_vec
= VEC_alloc (darwin_thread_t
, new_nbr
);
317 for (new_ix
= 0, old_ix
= 0; new_ix
< new_nbr
|| old_ix
< old_nbr
;)
319 thread_t new_id
= (new_ix
< new_nbr
) ?
320 thread_list
[new_ix
] : THREAD_NULL
;
321 darwin_thread_t
*old
= (old_ix
< old_nbr
) ?
322 VEC_index (darwin_thread_t
, darwin_inf
->threads
, old_ix
) : NULL
;
323 thread_t old_id
= old
? old
->gdb_port
: THREAD_NULL
;
326 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:%x old_id:%x\n"),
327 new_ix
, new_nbr
, old_ix
, old_nbr
, new_id
, old_id
);
329 if (old_id
== new_id
)
331 /* Thread still exist. */
332 VEC_safe_push (darwin_thread_t
, thread_vec
, old
);
336 kret
= mach_port_deallocate (gdb_task
, old_id
);
337 MACH_CHECK_ERROR (kret
);
340 if (new_ix
< new_nbr
&& new_id
== MACH_PORT_DEAD
)
342 /* Ignore dead ports.
343 In some weird cases, we might get dead ports. They should
344 correspond to dead thread so they could safely be ignored. */
348 if (new_ix
< new_nbr
&& (old_ix
== old_nbr
|| new_id
< old_id
))
350 /* A thread was created. */
351 struct thread_info
*tp
;
352 struct private_thread_info
*pti
;
354 pti
= XZALLOC (struct private_thread_info
);
355 pti
->gdb_port
= new_id
;
356 pti
->msg_state
= DARWIN_RUNNING
;
358 /* Add a new thread unless this is the first one ever met. */
359 if (!(old_nbr
== 0 && new_ix
== 0))
360 tp
= add_thread_with_info (ptid_build (inf
->pid
, 0, new_id
), pti
);
363 tp
= find_thread_ptid (ptid_build (inf
->pid
, 0, 0));
367 VEC_safe_push (darwin_thread_t
, thread_vec
, pti
);
371 if (old_ix
< old_nbr
&& (new_ix
== new_nbr
|| new_id
> old_id
))
373 /* A thread was removed. */
374 delete_thread (ptid_build (inf
->pid
, 0, old_id
));
375 kret
= mach_port_deallocate (gdb_task
, old_id
);
376 MACH_CHECK_ERROR (kret
);
380 gdb_assert_not_reached ("unexpected thread case");
383 if (darwin_inf
->threads
)
384 VEC_free (darwin_thread_t
, darwin_inf
->threads
);
385 darwin_inf
->threads
= thread_vec
;
387 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
388 new_nbr
* sizeof (int));
389 MACH_CHECK_ERROR (kret
);
393 find_inferior_task_it (struct inferior
*inf
, void *port_ptr
)
395 return inf
->private->task
== *(task_t
*)port_ptr
;
399 find_inferior_notify_it (struct inferior
*inf
, void *port_ptr
)
401 return inf
->private->notify_port
== *(task_t
*)port_ptr
;
404 /* Return an inferior by task port. */
405 static struct inferior
*
406 darwin_find_inferior_by_task (task_t port
)
408 return iterate_over_inferiors (&find_inferior_task_it
, &port
);
411 /* Return an inferior by notification port. */
412 static struct inferior
*
413 darwin_find_inferior_by_notify (mach_port_t port
)
415 return iterate_over_inferiors (&find_inferior_notify_it
, &port
);
418 /* Return a thread by port. */
419 static darwin_thread_t
*
420 darwin_find_thread (struct inferior
*inf
, thread_t thread
)
426 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
428 if (t
->gdb_port
== thread
)
433 /* Suspend (ie stop) an inferior at Mach level. */
436 darwin_suspend_inferior (struct inferior
*inf
)
438 if (!inf
->private->suspended
)
442 kret
= task_suspend (inf
->private->task
);
443 MACH_CHECK_ERROR (kret
);
445 inf
->private->suspended
= 1;
449 /* Resume an inferior at Mach level. */
452 darwin_resume_inferior (struct inferior
*inf
)
454 if (inf
->private->suspended
)
458 kret
= task_resume (inf
->private->task
);
459 MACH_CHECK_ERROR (kret
);
461 inf
->private->suspended
= 0;
465 /* Iterator functions. */
468 darwin_suspend_inferior_it (struct inferior
*inf
, void *arg
)
470 darwin_suspend_inferior (inf
);
471 darwin_check_new_threads (inf
);
476 darwin_resume_inferior_it (struct inferior
*inf
, void *arg
)
478 darwin_resume_inferior (inf
);
483 darwin_dump_message (mach_msg_header_t
*hdr
, int disp_body
)
485 printf_unfiltered (_("message header:\n"));
486 printf_unfiltered (_(" bits: 0x%x\n"), hdr
->msgh_bits
);
487 printf_unfiltered (_(" size: 0x%x\n"), hdr
->msgh_size
);
488 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr
->msgh_remote_port
);
489 printf_unfiltered (_(" local-port: 0x%x\n"), hdr
->msgh_local_port
);
490 printf_unfiltered (_(" reserved: 0x%x\n"), hdr
->msgh_reserved
);
491 printf_unfiltered (_(" id: 0x%x\n"), hdr
->msgh_id
);
495 const unsigned char *data
;
496 const unsigned long *ldata
;
500 data
= (unsigned char *)(hdr
+ 1);
501 size
= hdr
->msgh_size
- sizeof (mach_msg_header_t
);
503 if (hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
505 mach_msg_body_t
*bod
= (mach_msg_body_t
*)data
;
506 mach_msg_port_descriptor_t
*desc
=
507 (mach_msg_port_descriptor_t
*)(bod
+ 1);
510 printf_unfiltered (_("body: descriptor_count=%u\n"),
511 bod
->msgh_descriptor_count
);
512 data
+= sizeof (mach_msg_body_t
);
513 size
-= sizeof (mach_msg_body_t
);
514 for (k
= 0; k
< bod
->msgh_descriptor_count
; k
++)
515 switch (desc
[k
].type
)
517 case MACH_MSG_PORT_DESCRIPTOR
:
519 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
520 k
, desc
[k
].type
, desc
[k
].name
, desc
[k
].disposition
);
523 printf_unfiltered (_(" descr %d: type=%u\n"),
527 data
+= bod
->msgh_descriptor_count
528 * sizeof (mach_msg_port_descriptor_t
);
529 size
-= bod
->msgh_descriptor_count
530 * sizeof (mach_msg_port_descriptor_t
);
531 ndr
= (NDR_record_t
*)(desc
+ bod
->msgh_descriptor_count
);
533 (_("NDR: mig=%02x if=%02x encod=%02x "
534 "int=%02x char=%02x float=%02x\n"),
535 ndr
->mig_vers
, ndr
->if_vers
, ndr
->mig_encoding
,
536 ndr
->int_rep
, ndr
->char_rep
, ndr
->float_rep
);
537 data
+= sizeof (NDR_record_t
);
538 size
-= sizeof (NDR_record_t
);
541 printf_unfiltered (_(" data:"));
542 ldata
= (const unsigned long *)data
;
543 for (i
= 0; i
< size
/ sizeof (unsigned long); i
++)
544 printf_unfiltered (" %08lx", ldata
[i
]);
545 printf_unfiltered (_("\n"));
550 darwin_decode_exception_message (mach_msg_header_t
*hdr
,
551 struct inferior
**pinf
,
552 darwin_thread_t
**pthread
)
554 mach_msg_body_t
*bod
= (mach_msg_body_t
*)(hdr
+ 1);
555 mach_msg_port_descriptor_t
*desc
= (mach_msg_port_descriptor_t
*)(bod
+ 1);
558 struct inferior
*inf
;
559 darwin_thread_t
*thread
;
561 thread_t thread_port
;
565 /* Check message identifier. 2401 is exc. */
566 if (hdr
->msgh_id
!= 2401)
569 /* Check message header. */
570 if (!(hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
573 /* Check descriptors. */
574 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
575 + sizeof (*ndr
) + 2 * sizeof (integer_t
))
576 || bod
->msgh_descriptor_count
!= 2
577 || desc
[0].type
!= MACH_MSG_PORT_DESCRIPTOR
578 || desc
[0].disposition
!= MACH_MSG_TYPE_MOVE_SEND
579 || desc
[1].type
!= MACH_MSG_PORT_DESCRIPTOR
580 || desc
[1].disposition
!= MACH_MSG_TYPE_MOVE_SEND
)
583 /* Check data representation. */
584 ndr
= (NDR_record_t
*)(desc
+ 2);
585 if (ndr
->mig_vers
!= NDR_PROTOCOL_2_0
586 || ndr
->if_vers
!= NDR_PROTOCOL_2_0
587 || ndr
->mig_encoding
!= NDR_record
.mig_encoding
588 || ndr
->int_rep
!= NDR_record
.int_rep
589 || ndr
->char_rep
!= NDR_record
.char_rep
590 || ndr
->float_rep
!= NDR_record
.float_rep
)
593 /* Ok, the hard work. */
594 data
= (integer_t
*)(ndr
+ 1);
596 /* Find process by port. */
597 task_port
= desc
[1].name
;
598 thread_port
= desc
[0].name
;
599 inf
= darwin_find_inferior_by_task (task_port
);
604 /* Find thread by port. */
605 /* Check for new threads. Do it early so that the port in the exception
606 message can be deallocated. */
607 darwin_check_new_threads (inf
);
609 /* We got new rights to the task and the thread. Get rid of them. */
610 kret
= mach_port_deallocate (mach_task_self (), task_port
);
611 MACH_CHECK_ERROR (kret
);
612 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
613 MACH_CHECK_ERROR (kret
);
615 thread
= darwin_find_thread (inf
, thread_port
);
620 /* The thread should be running. However we have observed cases where a thread
621 got a SIGTTIN message after being stopped. */
622 gdb_assert (thread
->msg_state
!= DARWIN_MESSAGE
);
624 /* Finish decoding. */
625 thread
->event
.header
= *hdr
;
626 thread
->event
.thread_port
= thread_port
;
627 thread
->event
.task_port
= task_port
;
628 thread
->event
.ex_type
= data
[0];
629 thread
->event
.data_count
= data
[1];
631 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
632 + sizeof (*ndr
) + 2 * sizeof (integer_t
)
633 + data
[1] * sizeof (integer_t
)))
635 for (i
= 0; i
< data
[1]; i
++)
636 thread
->event
.ex_data
[i
] = data
[2 + i
];
638 thread
->msg_state
= DARWIN_MESSAGE
;
644 darwin_encode_reply (mig_reply_error_t
*reply
, mach_msg_header_t
*hdr
,
647 mach_msg_header_t
*rh
= &reply
->Head
;
648 rh
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(hdr
->msgh_bits
), 0);
649 rh
->msgh_remote_port
= hdr
->msgh_remote_port
;
650 rh
->msgh_size
= (mach_msg_size_t
)sizeof(mig_reply_error_t
);
651 rh
->msgh_local_port
= MACH_PORT_NULL
;
652 rh
->msgh_id
= hdr
->msgh_id
+ 100;
654 reply
->NDR
= NDR_record
;
655 reply
->RetCode
= code
;
659 darwin_send_reply (struct inferior
*inf
, darwin_thread_t
*thread
)
662 mig_reply_error_t reply
;
664 darwin_encode_reply (&reply
, &thread
->event
.header
, KERN_SUCCESS
);
666 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
667 reply
.Head
.msgh_size
, 0,
668 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
670 MACH_CHECK_ERROR (kret
);
672 inf
->private->pending_messages
--;
676 darwin_resume_thread (struct inferior
*inf
, darwin_thread_t
*thread
,
677 int step
, int nsignal
)
683 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
684 thread
->msg_state
, thread
->gdb_port
, step
, nsignal
);
686 switch (thread
->msg_state
)
689 if (thread
->event
.ex_type
== EXC_SOFTWARE
690 && thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
692 /* Either deliver a new signal or cancel the signal received. */
693 res
= PTRACE (PT_THUPDATE
, inf
->pid
,
694 (void *)(uintptr_t)thread
->gdb_port
, nsignal
);
696 inferior_debug (1, _("ptrace THUP: res=%d\n"), res
);
700 /* Note: ptrace is allowed only if the process is stopped.
701 Directly send the signal to the thread. */
702 res
= syscall (SYS___pthread_kill
, thread
->gdb_port
, nsignal
);
703 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
704 thread
->gdb_port
, nsignal
, res
);
705 thread
->signaled
= 1;
708 /* Set single step. */
709 inferior_debug (4, _("darwin_set_sstep (thread=%x, enable=%d)\n"),
710 thread
->gdb_port
, step
);
711 darwin_set_sstep (thread
->gdb_port
, step
);
712 thread
->single_step
= step
;
714 darwin_send_reply (inf
, thread
);
715 thread
->msg_state
= DARWIN_RUNNING
;
722 kret
= thread_resume (thread
->gdb_port
);
723 MACH_CHECK_ERROR (kret
);
725 thread
->msg_state
= DARWIN_RUNNING
;
730 /* Resume all threads of the inferior. */
733 darwin_resume_inferior_threads (struct inferior
*inf
, int step
, int nsignal
)
735 darwin_thread_t
*thread
;
739 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
741 darwin_resume_thread (inf
, thread
, step
, nsignal
);
744 struct resume_inferior_threads_param
751 darwin_resume_inferior_threads_it (struct inferior
*inf
, void *param
)
753 int step
= ((struct resume_inferior_threads_param
*)param
)->step
;
754 int nsignal
= ((struct resume_inferior_threads_param
*)param
)->nsignal
;
756 darwin_resume_inferior_threads (inf
, step
, nsignal
);
761 /* Suspend all threads of INF. */
764 darwin_suspend_inferior_threads (struct inferior
*inf
)
766 darwin_thread_t
*thread
;
771 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
773 switch (thread
->msg_state
)
779 kret
= thread_suspend (thread
->gdb_port
);
780 MACH_CHECK_ERROR (kret
);
781 thread
->msg_state
= DARWIN_STOPPED
;
787 darwin_resume (ptid_t ptid
, int step
, enum target_signal signal
)
789 struct target_waitstatus status
;
795 struct inferior
*inf
;
798 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
799 ptid_get_pid (ptid
), ptid_get_tid (ptid
), step
, signal
);
801 if (signal
== TARGET_SIGNAL_0
)
804 nsignal
= target_signal_to_host (signal
);
806 /* Don't try to single step all threads. */
808 ptid
= inferior_ptid
;
810 /* minus_one_ptid is RESUME_ALL. */
811 if (ptid_equal (ptid
, minus_one_ptid
))
813 struct resume_inferior_threads_param param
;
815 param
.nsignal
= nsignal
;
818 /* Resume threads. */
819 iterate_over_inferiors (darwin_resume_inferior_threads_it
, ¶m
);
821 iterate_over_inferiors (darwin_resume_inferior_it
, NULL
);
825 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ptid
));
826 long tid
= ptid_get_tid (ptid
);
828 /* Stop the inferior (should be useless). */
829 darwin_suspend_inferior (inf
);
832 darwin_resume_inferior_threads (inf
, step
, nsignal
);
835 darwin_thread_t
*thread
;
837 /* Suspend threads of the task. */
838 darwin_suspend_inferior_threads (inf
);
840 /* Resume the selected thread. */
841 thread
= darwin_find_thread (inf
, tid
);
843 darwin_resume_thread (inf
, thread
, step
, nsignal
);
846 /* Resume the task. */
847 darwin_resume_inferior (inf
);
852 darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
853 enum target_signal signal
)
855 return darwin_resume (ptid
, step
, signal
);
859 darwin_decode_message (mach_msg_header_t
*hdr
,
860 darwin_thread_t
**pthread
,
861 struct inferior
**pinf
,
862 struct target_waitstatus
*status
)
864 darwin_thread_t
*thread
;
865 struct inferior
*inf
;
867 /* Exception message. */
868 if (hdr
->msgh_local_port
== darwin_ex_port
)
872 /* Decode message. */
873 res
= darwin_decode_exception_message (hdr
, &inf
, &thread
);
877 /* Should not happen... */
878 printf_unfiltered (_("darwin_wait: ill-formatted message (id=%x)\n"),
880 /* FIXME: send a failure reply? */
881 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
882 return minus_one_ptid
;
886 inf
->private->pending_messages
++;
888 status
->kind
= TARGET_WAITKIND_STOPPED
;
889 thread
->msg_state
= DARWIN_MESSAGE
;
891 inferior_debug (4, _("darwin_wait: thread=%x, got %s\n"),
893 unparse_exception_type (thread
->event
.ex_type
));
895 switch (thread
->event
.ex_type
)
898 status
->value
.sig
= TARGET_EXC_BAD_ACCESS
;
900 case EXC_BAD_INSTRUCTION
:
901 status
->value
.sig
= TARGET_EXC_BAD_INSTRUCTION
;
904 status
->value
.sig
= TARGET_EXC_ARITHMETIC
;
907 status
->value
.sig
= TARGET_EXC_EMULATION
;
910 if (thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
913 target_signal_from_host (thread
->event
.ex_data
[1]);
914 inferior_debug (5, _(" (signal %d: %s)\n"),
915 thread
->event
.ex_data
[1],
916 target_signal_to_name (status
->value
.sig
));
918 /* If the thread is stopped because it has received a signal
919 that gdb has just sent, continue. */
920 if (thread
->signaled
)
922 thread
->signaled
= 0;
923 darwin_send_reply (inf
, thread
);
924 thread
->msg_state
= DARWIN_RUNNING
;
925 status
->kind
= TARGET_WAITKIND_IGNORE
;
929 status
->value
.sig
= TARGET_EXC_SOFTWARE
;
932 /* Many internal GDB routines expect breakpoints to be reported
933 as TARGET_SIGNAL_TRAP, and will report TARGET_EXC_BREAKPOINT
934 as a spurious signal. */
935 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
938 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
942 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
948 inf
= darwin_find_inferior_by_notify (hdr
->msgh_local_port
);
951 if (!inf
->private->no_ptrace
)
956 res
= wait4 (inf
->pid
, &wstatus
, 0, NULL
);
957 if (res
< 0 || res
!= inf
->pid
)
959 printf_unfiltered (_("wait4: res=%d: %s\n"),
960 res
, safe_strerror (errno
));
961 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
962 return minus_one_ptid
;
964 if (WIFEXITED (wstatus
))
966 status
->kind
= TARGET_WAITKIND_EXITED
;
967 status
->value
.integer
= WEXITSTATUS (wstatus
);
971 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
972 status
->value
.sig
= WTERMSIG (wstatus
);
975 inferior_debug (4, _("darwin_wait: pid=%d exit, status=%x\n"),
978 /* Looks necessary on Leopard and harmless... */
979 wait4 (inf
->pid
, &wstatus
, 0, NULL
);
981 return ptid_build (inf
->pid
, 0, 0);
985 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf
->pid
);
986 status
->kind
= TARGET_WAITKIND_EXITED
;
987 status
->value
.integer
= 0; /* Don't know. */
988 return ptid_build (inf
->pid
, 0, 0);
992 printf_unfiltered (_("Bad local-port: %x\n"), hdr
->msgh_local_port
);
993 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
994 return minus_one_ptid
;
998 cancel_breakpoint (ptid_t ptid
)
1000 /* Arrange for a breakpoint to be hit again later. We will handle
1001 the current event, eventually we will resume this thread, and this
1002 breakpoint will trap again.
1004 If we do not do this, then we run the risk that the user will
1005 delete or disable the breakpoint, but the thread will have already
1008 struct regcache
*regcache
= get_thread_regcache (ptid
);
1009 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1012 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
1013 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
1015 inferior_debug (4, "cancel_breakpoint for thread %x\n",
1016 ptid_get_tid (ptid
));
1018 /* Back up the PC if necessary. */
1019 if (gdbarch_decr_pc_after_break (gdbarch
))
1020 regcache_write_pc (regcache
, pc
);
1028 darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
)
1033 mach_msg_header_t hdr
;
1036 mach_msg_header_t
*hdr
= &msgin
.hdr
;
1038 darwin_thread_t
*thread
;
1039 struct inferior
*inf
;
1042 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1043 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
1045 /* Handle fake stop events at first. */
1046 if (darwin_inf_fake_stop
!= NULL
)
1048 inf
= darwin_inf_fake_stop
;
1049 darwin_inf_fake_stop
= NULL
;
1051 status
->kind
= TARGET_WAITKIND_STOPPED
;
1052 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1053 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1054 thread
->msg_state
= DARWIN_STOPPED
;
1055 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1060 /* set_sigint_trap (); */
1062 /* Wait for a message. */
1063 kret
= mach_msg (&msgin
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
, 0,
1064 sizeof (msgin
.data
), darwin_port_set
, 0, MACH_PORT_NULL
);
1066 /* clear_sigint_trap (); */
1068 if (kret
== MACH_RCV_INTERRUPTED
)
1070 status
->kind
= TARGET_WAITKIND_IGNORE
;
1071 return minus_one_ptid
;
1074 if (kret
!= MACH_MSG_SUCCESS
)
1076 inferior_debug (5, _("mach_msg: ret=%x\n"), kret
);
1077 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
1078 return minus_one_ptid
;
1081 /* Debug: display message. */
1082 if (darwin_debug_flag
> 10)
1083 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1085 res
= darwin_decode_message (hdr
, &thread
, &inf
, status
);
1090 while (status
->kind
== TARGET_WAITKIND_IGNORE
);
1092 /* Stop all tasks. */
1093 iterate_over_inferiors (darwin_suspend_inferior_it
, NULL
);
1095 /* Read pending messages. */
1098 struct target_waitstatus status2
;
1101 kret
= mach_msg (&msgin
.hdr
,
1102 MACH_RCV_MSG
| MACH_RCV_TIMEOUT
, 0,
1103 sizeof (msgin
.data
), darwin_port_set
, 1, MACH_PORT_NULL
);
1105 if (kret
== MACH_RCV_TIMED_OUT
)
1107 if (kret
!= MACH_MSG_SUCCESS
)
1110 (5, _("darwin_wait: mach_msg(pending) ret=%x\n"), kret
);
1114 ptid2
= darwin_decode_message (hdr
, &thread
, &inf
, &status2
);
1116 if (inf
!= NULL
&& thread
!= NULL
1117 && thread
->event
.ex_type
== EXC_BREAKPOINT
)
1119 if (thread
->single_step
1120 || cancel_breakpoint (ptid_build (inf
->pid
, 0, thread
->gdb_port
)))
1122 gdb_assert (thread
->msg_state
== DARWIN_MESSAGE
);
1123 darwin_send_reply (inf
, thread
);
1124 thread
->msg_state
= DARWIN_RUNNING
;
1128 (3, _("darwin_wait: thread %x hit a non-gdb breakpoint\n"),
1132 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1138 darwin_wait_to (struct target_ops
*ops
,
1139 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1141 return darwin_wait (ptid
, status
);
1145 darwin_stop (ptid_t t
)
1147 struct inferior
*inf
= current_inferior ();
1149 /* FIXME: handle in no_ptrace mode. */
1150 gdb_assert (!inf
->private->no_ptrace
);
1151 kill (inf
->pid
, SIGINT
);
1155 darwin_mourn_inferior (struct target_ops
*ops
)
1157 struct inferior
*inf
= current_inferior ();
1162 unpush_target (darwin_ops
);
1164 /* Deallocate threads. */
1165 if (inf
->private->threads
)
1170 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1173 kret
= mach_port_deallocate (gdb_task
, t
->gdb_port
);
1174 MACH_CHECK_ERROR (kret
);
1176 VEC_free (darwin_thread_t
, inf
->private->threads
);
1177 inf
->private->threads
= NULL
;
1180 kret
= mach_port_move_member (gdb_task
,
1181 inf
->private->notify_port
, MACH_PORT_NULL
);
1182 gdb_assert (kret
== KERN_SUCCESS
);
1184 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1185 MACH_NOTIFY_DEAD_NAME
, 0,
1187 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1189 /* This can fail if the task is dead. */
1190 inferior_debug (4, "task=%x, prev=%x, notify_port=%x\n",
1191 inf
->private->task
, prev
, inf
->private->notify_port
);
1193 if (kret
== KERN_SUCCESS
)
1195 kret
= mach_port_deallocate (gdb_task
, prev
);
1196 MACH_CHECK_ERROR (kret
);
1199 kret
= mach_port_destroy (gdb_task
, inf
->private->notify_port
);
1200 MACH_CHECK_ERROR (kret
);
1203 /* Deallocate saved exception ports. */
1204 for (i
= 0; i
< inf
->private->exception_info
.count
; i
++)
1206 kret
= mach_port_deallocate
1207 (gdb_task
, inf
->private->exception_info
.ports
[i
]);
1208 MACH_CHECK_ERROR (kret
);
1210 inf
->private->exception_info
.count
= 0;
1212 kret
= mach_port_deallocate (gdb_task
, inf
->private->task
);
1213 MACH_CHECK_ERROR (kret
);
1215 xfree (inf
->private);
1216 inf
->private = NULL
;
1218 generic_mourn_inferior ();
1222 darwin_reply_to_all_pending_messages (struct inferior
*inf
)
1228 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1231 if (t
->msg_state
== DARWIN_MESSAGE
)
1232 darwin_resume_thread (inf
, t
, 0, 0);
1237 darwin_stop_inferior (struct inferior
*inf
)
1239 struct target_waitstatus wstatus
;
1245 gdb_assert (inf
!= NULL
);
1247 darwin_suspend_inferior (inf
);
1249 darwin_reply_to_all_pending_messages (inf
);
1251 if (inf
->private->no_ptrace
)
1254 res
= kill (inf
->pid
, SIGSTOP
);
1256 warning (_("cannot kill: %s"), safe_strerror (errno
));
1258 /* Wait until the process is really stopped. */
1261 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1262 if (wstatus
.kind
== TARGET_WAITKIND_STOPPED
1263 && wstatus
.value
.sig
== TARGET_SIGNAL_STOP
)
1268 static kern_return_t
1269 darwin_save_exception_ports (darwin_inferior
*inf
)
1273 inf
->exception_info
.count
=
1274 sizeof (inf
->exception_info
.ports
) / sizeof (inf
->exception_info
.ports
[0]);
1276 kret
= task_get_exception_ports
1277 (inf
->task
, EXC_MASK_ALL
, inf
->exception_info
.masks
,
1278 &inf
->exception_info
.count
, inf
->exception_info
.ports
,
1279 inf
->exception_info
.behaviors
, inf
->exception_info
.flavors
);
1283 static kern_return_t
1284 darwin_restore_exception_ports (darwin_inferior
*inf
)
1289 for (i
= 0; i
< inf
->exception_info
.count
; i
++)
1291 kret
= task_set_exception_ports
1292 (inf
->task
, inf
->exception_info
.masks
[i
], inf
->exception_info
.ports
[i
],
1293 inf
->exception_info
.behaviors
[i
], inf
->exception_info
.flavors
[i
]);
1294 if (kret
!= KERN_SUCCESS
)
1298 return KERN_SUCCESS
;
1302 darwin_kill_inferior (struct target_ops
*ops
)
1304 struct inferior
*inf
= current_inferior ();
1305 struct target_waitstatus wstatus
;
1311 if (ptid_equal (inferior_ptid
, null_ptid
))
1314 gdb_assert (inf
!= NULL
);
1316 if (!inf
->private->no_ptrace
)
1318 darwin_stop_inferior (inf
);
1320 res
= PTRACE (PT_KILL
, inf
->pid
, 0, 0);
1322 warning (_("Failed to kill inferior: ptrace returned %d "
1324 res
, safe_strerror (errno
), inf
->pid
);
1326 darwin_reply_to_all_pending_messages (inf
);
1328 darwin_resume_inferior (inf
);
1330 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1334 kret
= darwin_restore_exception_ports (inf
->private);
1335 MACH_CHECK_ERROR (kret
);
1337 darwin_reply_to_all_pending_messages (inf
);
1339 darwin_resume_inferior (inf
);
1341 res
= kill (inf
->pid
, 9);
1343 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1346 target_mourn_inferior ();
1350 darwin_attach_pid (struct inferior
*inf
)
1353 mach_port_t prev_port
;
1355 mach_port_t prev_not
;
1356 exception_mask_t mask
;
1358 inf
->private = XZALLOC (darwin_inferior
);
1360 kret
= task_for_pid (gdb_task
, inf
->pid
, &inf
->private->task
);
1361 if (kret
!= KERN_SUCCESS
)
1365 if (!inf
->attach_flag
)
1368 waitpid (inf
->pid
, &status
, 0);
1371 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1372 " (please check gdb is codesigned - see taskgated(8))"),
1373 inf
->pid
, mach_error_string (kret
), (unsigned long) kret
);
1376 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1377 inf
->private->task
, inf
->pid
);
1379 if (darwin_ex_port
== MACH_PORT_NULL
)
1381 /* Create a port to get exceptions. */
1382 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1384 gdb_assert (kret
== KERN_SUCCESS
);
1386 kret
= mach_port_insert_right (gdb_task
, darwin_ex_port
, darwin_ex_port
,
1387 MACH_MSG_TYPE_MAKE_SEND
);
1388 gdb_assert (kret
== KERN_SUCCESS
);
1390 /* Create a port set and put ex_port in it. */
1391 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_PORT_SET
,
1393 gdb_assert (kret
== KERN_SUCCESS
);
1395 kret
= mach_port_move_member (gdb_task
, darwin_ex_port
, darwin_port_set
);
1396 gdb_assert (kret
== KERN_SUCCESS
);
1399 /* Create a port to be notified when the child task terminates. */
1400 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1401 &inf
->private->notify_port
);
1402 gdb_assert (kret
== KERN_SUCCESS
);
1404 kret
= mach_port_move_member (gdb_task
,
1405 inf
->private->notify_port
, darwin_port_set
);
1406 gdb_assert (kret
== KERN_SUCCESS
);
1408 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1409 MACH_NOTIFY_DEAD_NAME
, 0,
1410 inf
->private->notify_port
,
1411 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1413 gdb_assert (kret
== KERN_SUCCESS
);
1414 gdb_assert (prev_not
== MACH_PORT_NULL
);
1416 kret
= darwin_save_exception_ports (inf
->private);
1417 gdb_assert (kret
== KERN_SUCCESS
);
1419 /* Set exception port. */
1420 if (enable_mach_exceptions
)
1421 mask
= EXC_MASK_ALL
;
1423 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1424 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1425 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1426 gdb_assert (kret
== KERN_SUCCESS
);
1428 push_target (darwin_ops
);
1432 darwin_init_thread_list (struct inferior
*inf
)
1434 darwin_thread_t
*thread
;
1437 darwin_check_new_threads (inf
);
1439 gdb_assert (inf
->private->threads
1440 && VEC_length (darwin_thread_t
, inf
->private->threads
) > 0);
1441 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1443 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1445 new_ptid
= ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1446 thread_change_ptid (inferior_ptid
, new_ptid
);
1447 inferior_ptid
= new_ptid
;
1450 /* The child must synchronize with gdb: gdb must set the exception port
1451 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1452 FIXME: is there a lighter way ? */
1453 static int ptrace_fds
[2];
1456 darwin_ptrace_me (void)
1461 /* Close write end point. */
1462 close (ptrace_fds
[1]);
1464 /* Wait until gdb is ready. */
1465 res
= read (ptrace_fds
[0], &c
, 1);
1466 gdb_assert (res
== 0);
1467 close (ptrace_fds
[0]);
1469 /* Get rid of privileges. */
1470 setegid (getgid ());
1473 PTRACE (PT_TRACE_ME
, 0, 0, 0);
1475 /* Redirect signals to exception port. */
1476 PTRACE (PT_SIGEXC
, 0, 0, 0);
1479 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1481 darwin_pre_ptrace (void)
1483 if (pipe (ptrace_fds
) != 0)
1487 error (_("unable to create a pipe: %s"), safe_strerror (errno
));
1492 darwin_ptrace_him (int pid
)
1496 mach_port_t prev_port
;
1498 struct inferior
*inf
= current_inferior ();
1500 darwin_attach_pid (inf
);
1502 /* Let's the child run. */
1503 close (ptrace_fds
[0]);
1504 close (ptrace_fds
[1]);
1506 darwin_init_thread_list (inf
);
1508 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
1512 darwin_execvp (const char *file
, char * const argv
[], char * const env
[])
1514 posix_spawnattr_t attr
;
1518 res
= posix_spawnattr_init (&attr
);
1522 (gdb_stderr
, "Cannot initialize attribute for posix_spawn\n");
1526 /* Do like execve: replace the image. */
1527 ps_flags
= POSIX_SPAWN_SETEXEC
;
1529 /* Disable ASLR. The constant doesn't look to be available outside the
1530 kernel include files. */
1531 #ifndef _POSIX_SPAWN_DISABLE_ASLR
1532 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1534 ps_flags
|= _POSIX_SPAWN_DISABLE_ASLR
;
1535 res
= posix_spawnattr_setflags (&attr
, ps_flags
);
1538 fprintf_unfiltered (gdb_stderr
, "Cannot set posix_spawn flags\n");
1542 /* Specify the same binary preference to spawn the shell as the
1543 exec binary. This avoids spawning a 64bit shell while debugging
1544 a 32bit program, which may confuse gdb.
1545 Also, this slightly breaks internal layers as we suppose the binary
1546 is Mach-O. Doesn't harm in practice. */
1547 if (exec_bfd
!= NULL
)
1552 pref
= bfd_mach_o_get_data (exec_bfd
)->header
.cputype
;
1553 res
= posix_spawnattr_setbinpref_np (&attr
, 1, &pref
, &ocount
);
1554 if (res
!= 0 || ocount
!= 1)
1555 fprintf_unfiltered (gdb_stderr
, "Cannot set posix_spawn binpref\n");
1558 posix_spawnp (NULL
, argv
[0], NULL
, &attr
, argv
, env
);
1562 darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
1563 char *allargs
, char **env
, int from_tty
)
1565 /* Do the hard work. */
1566 fork_inferior (exec_file
, allargs
, env
, darwin_ptrace_me
, darwin_ptrace_him
,
1567 darwin_pre_ptrace
, NULL
, darwin_execvp
);
1569 /* Return now in case of error. */
1570 if (ptid_equal (inferior_ptid
, null_ptid
))
1575 /* Attach to process PID, then initialize for debugging it
1576 and wait for the trace-trap that results from attaching. */
1578 darwin_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1584 struct inferior
*inf
;
1587 pid
= parse_pid_to_attach (args
);
1589 if (pid
== getpid ()) /* Trying to masturbate? */
1590 error (_("I refuse to debug myself!"));
1594 char *exec_file
= get_exec_file (0);
1597 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
1598 target_pid_to_str (pid_to_ptid (pid
)));
1600 printf_unfiltered (_("Attaching to %s\n"),
1601 target_pid_to_str (pid_to_ptid (pid
)));
1603 gdb_flush (gdb_stdout
);
1606 if (pid
== 0 || kill (pid
, 0) < 0)
1607 error (_("Can't attach to process %d: %s (%d)"),
1608 pid
, safe_strerror (errno
), errno
);
1610 inferior_ptid
= pid_to_ptid (pid
);
1611 inf
= current_inferior ();
1612 inferior_appeared (inf
, pid
);
1613 inf
->attach_flag
= 1;
1615 /* Always add a main thread. */
1616 add_thread_silent (inferior_ptid
);
1618 darwin_attach_pid (inf
);
1620 darwin_suspend_inferior (inf
);
1622 darwin_init_thread_list (inf
);
1624 darwin_check_osabi (inf
->private, ptid_get_tid (inferior_ptid
));
1626 gdb_assert (darwin_inf_fake_stop
== NULL
);
1627 darwin_inf_fake_stop
= inf
;
1628 inf
->private->no_ptrace
= 1;
1631 /* Take a program previously attached to and detaches it.
1632 The program resumes execution and will no longer stop
1633 on signals, etc. We'd better not have left any breakpoints
1634 in the program or it'll die when it hits one. For this
1635 to work, it may be necessary for the process to have been
1636 previously attached. It *might* work if the program was
1637 started via fork. */
1639 darwin_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1641 pid_t pid
= ptid_get_pid (inferior_ptid
);
1642 struct inferior
*inf
= current_inferior ();
1646 /* Display message. */
1649 char *exec_file
= get_exec_file (0);
1652 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
1653 target_pid_to_str (pid_to_ptid (pid
)));
1654 gdb_flush (gdb_stdout
);
1657 /* If ptrace() is in use, stop the process. */
1658 if (!inf
->private->no_ptrace
)
1659 darwin_stop_inferior (inf
);
1661 kret
= darwin_restore_exception_ports (inf
->private);
1662 MACH_CHECK_ERROR (kret
);
1664 if (!inf
->private->no_ptrace
)
1666 res
= PTRACE (PT_DETACH
, inf
->pid
, 0, 0);
1668 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1669 inf
->pid
, safe_strerror (errno
), errno
);
1672 darwin_reply_to_all_pending_messages (inf
);
1674 /* When using ptrace, we have just performed a PT_DETACH, which
1675 resumes the inferior. On the other hand, when we are not using
1676 ptrace, we need to resume its execution ourselves. */
1677 if (inf
->private->no_ptrace
)
1678 darwin_resume_inferior (inf
);
1680 darwin_mourn_inferior (ops
);
1684 darwin_files_info (struct target_ops
*ops
)
1689 darwin_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1691 static char buf
[80];
1692 long tid
= ptid_get_tid (ptid
);
1696 snprintf (buf
, sizeof (buf
), _("Thread 0x%lx of process %u"),
1697 tid
, ptid_get_pid (ptid
));
1701 return normal_pid_to_str (ptid
);
1705 darwin_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1710 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1711 copy it to RDADDR in gdb's address space.
1712 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1713 to ADDR in inferior task's address space.
1714 Return 0 on failure; number of bytes read / writen otherwise. */
1716 darwin_read_write_inferior (task_t task
, CORE_ADDR addr
,
1717 char *rdaddr
, const char *wraddr
, int length
)
1720 mach_vm_address_t offset
= addr
& (mach_page_size
- 1);
1721 mach_vm_address_t low_address
= (mach_vm_address_t
) (addr
- offset
);
1722 mach_vm_size_t aligned_length
= (mach_vm_size_t
) PAGE_ROUND (offset
+ length
);
1725 mach_vm_size_t remaining_length
;
1726 mach_vm_address_t region_address
;
1727 mach_vm_size_t region_length
;
1729 inferior_debug (8, _("darwin_read_write_inferior(task=%x, %s, len=%d)\n"),
1730 task
, core_addr_to_string (addr
), length
);
1732 /* Get memory from inferior with page aligned addresses. */
1733 kret
= mach_vm_read (task
, low_address
, aligned_length
,
1734 &copied
, ©_count
);
1735 if (kret
!= KERN_SUCCESS
)
1738 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1739 core_addr_to_string (addr
), mach_error_string (kret
));
1744 memcpy (rdaddr
, (char *)copied
+ offset
, length
);
1749 memcpy ((char *)copied
+ offset
, wraddr
, length
);
1751 /* Do writes atomically.
1752 First check for holes and unwritable memory. */
1753 for (region_address
= low_address
, remaining_length
= aligned_length
;
1754 region_address
< low_address
+ aligned_length
;
1755 region_address
+= region_length
, remaining_length
-= region_length
)
1757 vm_region_submap_short_info_data_64_t info
;
1758 mach_vm_address_t region_start
= region_address
;
1759 mach_msg_type_number_t count
;
1760 natural_t region_depth
;
1762 region_depth
= 100000;
1763 count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
1764 kret
= mach_vm_region_recurse
1765 (task
, ®ion_start
, ®ion_length
, ®ion_depth
,
1766 (vm_region_recurse_info_t
) &info
, &count
);
1768 if (kret
!= KERN_SUCCESS
)
1770 inferior_debug (1, _("darwin_read_write_inferior: "
1771 "mach_vm_region_recurse failed at %s: %s\n"),
1772 core_addr_to_string (region_address
),
1773 mach_error_string (kret
));
1778 (9, _("darwin_read_write_inferior: "
1779 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1780 core_addr_to_string (region_address
),
1781 core_addr_to_string (region_start
),
1782 core_addr_to_string (region_length
));
1784 /* Check for holes in memory. */
1785 if (region_start
> region_address
)
1787 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1788 core_addr_to_string (region_address
),
1789 core_addr_to_string (region_start
),
1790 (unsigned)region_length
);
1795 /* Adjust the length. */
1796 region_length
-= (region_address
- region_start
);
1798 if (!(info
.max_protection
& VM_PROT_WRITE
))
1800 kret
= mach_vm_protect
1801 (task
, region_address
, region_length
,
1802 TRUE
, info
.max_protection
| VM_PROT_WRITE
| VM_PROT_COPY
);
1803 if (kret
!= KERN_SUCCESS
)
1805 warning (_("darwin_read_write_inf: "
1806 "mach_vm_protect max failed at %s: %s"),
1807 core_addr_to_string (region_address
),
1808 mach_error_string (kret
));
1814 if (!(info
.protection
& VM_PROT_WRITE
))
1816 kret
= mach_vm_protect (task
, region_address
, region_length
,
1817 FALSE
, info
.protection
| VM_PROT_WRITE
);
1818 if (kret
!= KERN_SUCCESS
)
1820 warning (_("darwin_read_write_inf: "
1821 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1822 core_addr_to_string (region_address
),
1823 (unsigned long)region_length
, mach_error_string (kret
));
1830 kret
= mach_vm_write (task
, low_address
, copied
, aligned_length
);
1832 if (kret
!= KERN_SUCCESS
)
1834 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1835 mach_error_string (kret
));
1839 mach_vm_deallocate (mach_task_self (), copied
, copy_count
);
1843 /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
1845 Return 0 on failure; number of bytes read / writen otherwise. */
1848 darwin_read_dyld_info (task_t task
, CORE_ADDR addr
, char *rdaddr
, int length
)
1850 struct task_dyld_info task_dyld_info
;
1851 mach_msg_type_number_t count
= TASK_DYLD_INFO_COUNT
;
1852 int sz
= TASK_DYLD_INFO_COUNT
* sizeof (natural_t
);
1858 kret
= task_info (task
, TASK_DYLD_INFO
, (task_info_t
) &task_dyld_info
, &count
);
1859 MACH_CHECK_ERROR (kret
);
1860 if (kret
!= KERN_SUCCESS
)
1863 if (addr
+ length
> sz
)
1865 memcpy (rdaddr
, (char *)&task_dyld_info
+ addr
, length
);
1870 /* Return 0 on failure, number of bytes handled otherwise. TARGET
1873 darwin_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
, int write
,
1874 struct mem_attrib
*attrib
, struct target_ops
*target
)
1876 struct inferior
*inf
= current_inferior ();
1877 task_t task
= inf
->private->task
;
1879 if (task
== MACH_PORT_NULL
)
1882 inferior_debug (8, _("darwin_xfer_memory(%s, %d, %c)\n"),
1883 core_addr_to_string (memaddr
), len
, write
? 'w' : 'r');
1886 return darwin_read_write_inferior (task
, memaddr
, NULL
, myaddr
, len
);
1888 return darwin_read_write_inferior (task
, memaddr
, myaddr
, NULL
, len
);
1892 darwin_xfer_partial (struct target_ops
*ops
,
1893 enum target_object object
, const char *annex
,
1894 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1895 ULONGEST offset
, LONGEST len
)
1897 struct inferior
*inf
= current_inferior ();
1900 (8, _("darwin_xfer_partial(%s, %d, rbuf=%s, wbuf=%s) pid=%u\n"),
1901 core_addr_to_string (offset
), (int)len
,
1902 host_address_to_string (readbuf
), host_address_to_string (writebuf
),
1907 case TARGET_OBJECT_MEMORY
:
1908 return darwin_read_write_inferior (inf
->private->task
, offset
,
1909 readbuf
, writebuf
, len
);
1910 case TARGET_OBJECT_DARWIN_DYLD_INFO
:
1911 if (writebuf
!= NULL
|| readbuf
== NULL
)
1913 /* Support only read. */
1916 return darwin_read_dyld_info (inf
->private->task
, offset
, readbuf
, len
);
1924 set_enable_mach_exceptions (char *args
, int from_tty
,
1925 struct cmd_list_element
*c
)
1927 if (!ptid_equal (inferior_ptid
, null_ptid
))
1929 struct inferior
*inf
= current_inferior ();
1930 exception_mask_t mask
;
1933 if (enable_mach_exceptions
)
1934 mask
= EXC_MASK_ALL
;
1937 darwin_restore_exception_ports (inf
->private);
1938 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1940 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1941 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1942 MACH_CHECK_ERROR (kret
);
1947 darwin_pid_to_exec_file (int pid
)
1952 path
= xmalloc (MAXPATHLEN
);
1953 make_cleanup (xfree
, path
);
1955 res
= proc_pidinfo (pid
, PROC_PIDPATHINFO
, 0, path
, MAXPATHLEN
);
1963 darwin_get_ada_task_ptid (long lwp
, long thread
)
1968 struct inferior
*inf
= current_inferior ();
1970 mach_port_name_array_t names
;
1971 mach_msg_type_number_t names_count
;
1972 mach_port_type_array_t types
;
1973 mach_msg_type_number_t types_count
;
1976 /* First linear search. */
1978 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1980 if (t
->inf_port
== lwp
)
1981 return ptid_build (ptid_get_pid (inferior_ptid
), 0, t
->gdb_port
);
1983 /* Maybe the port was never extract. Do it now. */
1985 /* First get inferior port names. */
1986 kret
= mach_port_names (inf
->private->task
, &names
, &names_count
, &types
,
1988 MACH_CHECK_ERROR (kret
);
1989 if (kret
!= KERN_SUCCESS
)
1992 /* For each name, copy the right in the gdb space and then compare with
1993 our view of the inferior threads. We don't forget to deallocate the
1995 for (i
= 0; i
< names_count
; i
++)
1997 mach_port_t local_name
;
1998 mach_msg_type_name_t local_type
;
2000 /* We just need to know the corresponding name in gdb name space.
2001 So extract and deallocate the right. */
2002 kret
= mach_port_extract_right (inf
->private->task
, names
[i
],
2003 MACH_MSG_TYPE_COPY_SEND
,
2004 &local_name
, &local_type
);
2005 if (kret
!= KERN_SUCCESS
)
2007 mach_port_deallocate (gdb_task
, local_name
);
2010 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2012 if (t
->gdb_port
== local_name
)
2014 t
->inf_port
= names
[i
];
2015 if (names
[i
] == lwp
)
2020 vm_deallocate (gdb_task
, (vm_address_t
) names
,
2021 names_count
* sizeof (mach_port_t
));
2024 return ptid_build (ptid_get_pid (inferior_ptid
), 0, res
);
2030 darwin_supports_multi_process (void)
2035 /* -Wmissing-prototypes */
2036 extern initialize_file_ftype _initialize_darwin_inferior
;
2039 _initialize_darwin_inferior (void)
2043 gdb_task
= mach_task_self ();
2044 darwin_host_self
= mach_host_self ();
2046 /* Read page size. */
2047 kret
= host_page_size (darwin_host_self
, &mach_page_size
);
2048 if (kret
!= KERN_SUCCESS
)
2050 mach_page_size
= 0x1000;
2051 MACH_CHECK_ERROR (kret
);
2054 darwin_ops
= inf_child_target ();
2056 darwin_ops
->to_shortname
= "darwin-child";
2057 darwin_ops
->to_longname
= _("Darwin child process");
2058 darwin_ops
->to_doc
=
2059 _("Darwin child process (started by the \"run\" command).");
2060 darwin_ops
->to_create_inferior
= darwin_create_inferior
;
2061 darwin_ops
->to_attach
= darwin_attach
;
2062 darwin_ops
->to_attach_no_wait
= 0;
2063 darwin_ops
->to_detach
= darwin_detach
;
2064 darwin_ops
->to_files_info
= darwin_files_info
;
2065 darwin_ops
->to_wait
= darwin_wait_to
;
2066 darwin_ops
->to_mourn_inferior
= darwin_mourn_inferior
;
2067 darwin_ops
->to_kill
= darwin_kill_inferior
;
2068 darwin_ops
->to_stop
= darwin_stop
;
2069 darwin_ops
->to_resume
= darwin_resume_to
;
2070 darwin_ops
->to_thread_alive
= darwin_thread_alive
;
2071 darwin_ops
->to_pid_to_str
= darwin_pid_to_str
;
2072 darwin_ops
->to_pid_to_exec_file
= darwin_pid_to_exec_file
;
2073 darwin_ops
->to_load
= NULL
;
2074 darwin_ops
->deprecated_xfer_memory
= darwin_xfer_memory
;
2075 darwin_ops
->to_xfer_partial
= darwin_xfer_partial
;
2076 darwin_ops
->to_supports_multi_process
= darwin_supports_multi_process
;
2077 darwin_ops
->to_get_ada_task_ptid
= darwin_get_ada_task_ptid
;
2079 darwin_complete_target (darwin_ops
);
2081 add_target (darwin_ops
);
2083 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
2086 add_setshow_zinteger_cmd ("darwin", class_obscure
,
2087 &darwin_debug_flag
, _("\
2088 Set if printing inferior communication debugging statements."), _("\
2089 Show if printing inferior communication debugging statements."), NULL
,
2091 &setdebuglist
, &showdebuglist
);
2093 add_setshow_boolean_cmd ("mach-exceptions", class_support
,
2094 &enable_mach_exceptions
, _("\
2095 Set if mach exceptions are caught."), _("\
2096 Show if mach exceptions are caught."), _("\
2097 When this mode is on, all low level exceptions are reported before being\n\
2098 reported by the kernel."),
2099 &set_enable_mach_exceptions
, NULL
,
2100 &setlist
, &showlist
);