1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008-2014 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/>. */
31 #include "gdbthread.h"
33 #include "event-top.h"
36 #include "exceptions.h"
37 #include "inf-child.h"
39 #include "arch-utils.h"
41 #include "bfd/mach-o.h"
43 #include <sys/ptrace.h>
44 #include <sys/signal.h>
45 #include <machine/setjmp.h>
46 #include <sys/types.h>
51 #include <sys/sysctl.h>
54 #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"
67 #include "common/filestuff.h"
70 Darwin kernel is Mach + BSD derived kernel. Note that they share the
71 same memory space and are linked together (ie there is no micro-kernel).
73 Although ptrace(2) is available on Darwin, it is not complete. We have
74 to use Mach calls to read and write memory and to modify registers. We
75 also use Mach to get inferior faults. As we cannot use select(2) or
76 signals with Mach port (the Mach communication channel), signals are
77 reported to gdb as an exception. Furthermore we detect death of the
78 inferior through a Mach notification message. This way we only wait
81 Some Mach documentation is available for Apple xnu source package or
85 #define PTRACE(CMD, PID, ADDR, SIG) \
86 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
88 static void darwin_stop (struct target_ops
*self
, ptid_t
);
90 static void darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
91 enum gdb_signal signal
);
92 static void darwin_resume (ptid_t ptid
, int step
,
93 enum gdb_signal signal
);
95 static ptid_t
darwin_wait_to (struct target_ops
*ops
, ptid_t ptid
,
96 struct target_waitstatus
*status
, int options
);
97 static ptid_t
darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
);
99 static void darwin_mourn_inferior (struct target_ops
*ops
);
101 static void darwin_kill_inferior (struct target_ops
*ops
);
103 static void darwin_ptrace_me (void);
105 static void darwin_ptrace_him (int pid
);
107 static void darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
108 char *allargs
, char **env
, int from_tty
);
110 static void darwin_files_info (struct target_ops
*ops
);
112 static char *darwin_pid_to_str (struct target_ops
*ops
, ptid_t tpid
);
114 static int darwin_thread_alive (struct target_ops
*ops
, ptid_t tpid
);
116 static void darwin_encode_reply (mig_reply_error_t
*reply
,
117 mach_msg_header_t
*hdr
, integer_t code
);
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
;
131 /* Port set, to wait for answer on all ports. */
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 unsigned 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 must codesign it with any system-trusted signing
154 authority. See taskgated(8) for details. */
155 static const unsigned char info_plist
[]
156 __attribute__ ((section ("__TEXT,__info_plist"),used
)) =
157 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
158 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
159 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
160 "<plist version=\"1.0\">\n"
162 " <key>CFBundleIdentifier</key>\n"
163 " <string>org.gnu.gdb</string>\n"
164 " <key>CFBundleName</key>\n"
165 " <string>gdb</string>\n"
166 " <key>CFBundleVersion</key>\n"
167 " <string>1.0</string>\n"
168 " <key>SecTaskAccess</key>\n"
170 " <string>allowed</string>\n"
171 " <string>debug</string>\n"
177 inferior_debug (int level
, const char *fmt
, ...)
181 if (darwin_debug_flag
< level
)
185 printf_unfiltered (_("[%d inferior]: "), getpid ());
186 vprintf_unfiltered (fmt
, ap
);
191 mach_check_error (kern_return_t ret
, const char *file
,
192 unsigned int line
, const char *func
)
194 if (ret
== KERN_SUCCESS
)
197 func
= _("[UNKNOWN]");
199 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"),
200 file
, line
, func
, mach_error_string (ret
), (unsigned long) ret
);
204 unparse_exception_type (unsigned int i
)
206 static char unknown_exception_buf
[32];
211 return "EXC_BAD_ACCESS";
212 case EXC_BAD_INSTRUCTION
:
213 return "EXC_BAD_INSTRUCTION";
215 return "EXC_ARITHMETIC";
217 return "EXC_EMULATION";
219 return "EXC_SOFTWARE";
221 return "EXC_BREAKPOINT";
223 return "EXC_SYSCALL";
224 case EXC_MACH_SYSCALL
:
225 return "EXC_MACH_SYSCALL";
227 return "EXC_RPC_ALERT";
231 snprintf (unknown_exception_buf
, 32, _("unknown (%d)"), i
);
232 return unknown_exception_buf
;
236 /* Set errno to zero, and then call ptrace with the given arguments.
237 If inferior debugging traces are on, then also print a debug
240 The returned value is the same as the value returned by ptrace,
241 except in the case where that value is -1 but errno is zero.
242 This case is documented to be a non-error situation, so we
243 return zero in that case. */
246 darwin_ptrace (const char *name
,
247 int request
, int pid
, PTRACE_TYPE_ARG3 arg3
, int arg4
)
252 ret
= ptrace (request
, pid
, (caddr_t
) arg3
, arg4
);
253 if (ret
== -1 && errno
== 0)
256 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
257 name
, pid
, arg3
, arg4
, ret
,
258 (ret
!= 0) ? safe_strerror (errno
) : _("no error"));
263 cmp_thread_t (const void *l
, const void *r
)
265 thread_t tl
= *(const thread_t
*)l
;
266 thread_t tr
= *(const thread_t
*)r
;
267 return (int)(tl
- tr
);
271 darwin_check_new_threads (struct inferior
*inf
)
275 thread_array_t thread_list
;
276 unsigned int new_nbr
;
277 unsigned int old_nbr
;
278 unsigned int new_ix
, old_ix
;
279 darwin_inferior
*darwin_inf
= inf
->private;
280 VEC (darwin_thread_t
) *thread_vec
;
282 /* Get list of threads. */
283 kret
= task_threads (darwin_inf
->task
, &thread_list
, &new_nbr
);
284 MACH_CHECK_ERROR (kret
);
285 if (kret
!= KERN_SUCCESS
)
290 qsort (thread_list
, new_nbr
, sizeof (thread_t
), cmp_thread_t
);
292 if (darwin_inf
->threads
)
293 old_nbr
= VEC_length (darwin_thread_t
, darwin_inf
->threads
);
297 /* Quick check for no changes. */
298 if (old_nbr
== new_nbr
)
300 for (i
= 0; i
< new_nbr
; i
++)
302 != VEC_index (darwin_thread_t
, darwin_inf
->threads
, i
)->gdb_port
)
306 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
307 new_nbr
* sizeof (int));
308 MACH_CHECK_ERROR (kret
);
313 thread_vec
= VEC_alloc (darwin_thread_t
, new_nbr
);
315 for (new_ix
= 0, old_ix
= 0; new_ix
< new_nbr
|| old_ix
< old_nbr
;)
317 thread_t new_id
= (new_ix
< new_nbr
) ?
318 thread_list
[new_ix
] : THREAD_NULL
;
319 darwin_thread_t
*old
= (old_ix
< old_nbr
) ?
320 VEC_index (darwin_thread_t
, darwin_inf
->threads
, old_ix
) : NULL
;
321 thread_t old_id
= old
? old
->gdb_port
: THREAD_NULL
;
324 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:0x%x old_id:0x%x\n"),
325 new_ix
, new_nbr
, old_ix
, old_nbr
, new_id
, old_id
);
327 if (old_id
== new_id
)
329 /* Thread still exist. */
330 VEC_safe_push (darwin_thread_t
, thread_vec
, old
);
334 kret
= mach_port_deallocate (gdb_task
, old_id
);
335 MACH_CHECK_ERROR (kret
);
338 if (new_ix
< new_nbr
&& new_id
== MACH_PORT_DEAD
)
340 /* Ignore dead ports.
341 In some weird cases, we might get dead ports. They should
342 correspond to dead thread so they could safely be ignored. */
346 if (new_ix
< new_nbr
&& (old_ix
== old_nbr
|| new_id
< old_id
))
348 /* A thread was created. */
349 struct thread_info
*tp
;
350 struct private_thread_info
*pti
;
352 pti
= XCNEW (struct private_thread_info
);
353 pti
->gdb_port
= new_id
;
354 pti
->msg_state
= DARWIN_RUNNING
;
356 /* Add a new thread unless this is the first one ever met. */
357 if (!(old_nbr
== 0 && new_ix
== 0))
358 tp
= add_thread_with_info (ptid_build (inf
->pid
, 0, new_id
), pti
);
361 tp
= find_thread_ptid (ptid_build (inf
->pid
, 0, 0));
365 VEC_safe_push (darwin_thread_t
, thread_vec
, pti
);
369 if (old_ix
< old_nbr
&& (new_ix
== new_nbr
|| new_id
> old_id
))
371 /* A thread was removed. */
372 delete_thread (ptid_build (inf
->pid
, 0, old_id
));
373 kret
= mach_port_deallocate (gdb_task
, old_id
);
374 MACH_CHECK_ERROR (kret
);
378 gdb_assert_not_reached ("unexpected thread case");
381 if (darwin_inf
->threads
)
382 VEC_free (darwin_thread_t
, darwin_inf
->threads
);
383 darwin_inf
->threads
= thread_vec
;
385 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
386 new_nbr
* sizeof (int));
387 MACH_CHECK_ERROR (kret
);
391 find_inferior_task_it (struct inferior
*inf
, void *port_ptr
)
393 return inf
->private->task
== *(task_t
*)port_ptr
;
397 find_inferior_notify_it (struct inferior
*inf
, void *port_ptr
)
399 return inf
->private->notify_port
== *(task_t
*)port_ptr
;
402 /* Return an inferior by task port. */
403 static struct inferior
*
404 darwin_find_inferior_by_task (task_t port
)
406 return iterate_over_inferiors (&find_inferior_task_it
, &port
);
409 /* Return an inferior by notification port. */
410 static struct inferior
*
411 darwin_find_inferior_by_notify (mach_port_t port
)
413 return iterate_over_inferiors (&find_inferior_notify_it
, &port
);
416 /* Return a thread by port. */
417 static darwin_thread_t
*
418 darwin_find_thread (struct inferior
*inf
, thread_t thread
)
424 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
426 if (t
->gdb_port
== thread
)
431 /* Suspend (ie stop) an inferior at Mach level. */
434 darwin_suspend_inferior (struct inferior
*inf
)
436 if (!inf
->private->suspended
)
440 kret
= task_suspend (inf
->private->task
);
441 MACH_CHECK_ERROR (kret
);
443 inf
->private->suspended
= 1;
447 /* Resume an inferior at Mach level. */
450 darwin_resume_inferior (struct inferior
*inf
)
452 if (inf
->private->suspended
)
456 kret
= task_resume (inf
->private->task
);
457 MACH_CHECK_ERROR (kret
);
459 inf
->private->suspended
= 0;
463 /* Iterator functions. */
466 darwin_suspend_inferior_it (struct inferior
*inf
, void *arg
)
468 darwin_suspend_inferior (inf
);
469 darwin_check_new_threads (inf
);
474 darwin_resume_inferior_it (struct inferior
*inf
, void *arg
)
476 darwin_resume_inferior (inf
);
481 darwin_dump_message (mach_msg_header_t
*hdr
, int disp_body
)
483 printf_unfiltered (_("message header:\n"));
484 printf_unfiltered (_(" bits: 0x%x\n"), hdr
->msgh_bits
);
485 printf_unfiltered (_(" size: 0x%x\n"), hdr
->msgh_size
);
486 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr
->msgh_remote_port
);
487 printf_unfiltered (_(" local-port: 0x%x\n"), hdr
->msgh_local_port
);
488 printf_unfiltered (_(" reserved: 0x%x\n"), hdr
->msgh_reserved
);
489 printf_unfiltered (_(" id: 0x%x\n"), hdr
->msgh_id
);
493 const unsigned char *data
;
494 const unsigned int *ldata
;
498 data
= (unsigned char *)(hdr
+ 1);
499 size
= hdr
->msgh_size
- sizeof (mach_msg_header_t
);
501 if (hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
503 mach_msg_body_t
*bod
= (mach_msg_body_t
*)data
;
504 mach_msg_port_descriptor_t
*desc
=
505 (mach_msg_port_descriptor_t
*)(bod
+ 1);
508 printf_unfiltered (_("body: descriptor_count=%u\n"),
509 bod
->msgh_descriptor_count
);
510 data
+= sizeof (mach_msg_body_t
);
511 size
-= sizeof (mach_msg_body_t
);
512 for (k
= 0; k
< bod
->msgh_descriptor_count
; k
++)
513 switch (desc
[k
].type
)
515 case MACH_MSG_PORT_DESCRIPTOR
:
517 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
518 k
, desc
[k
].type
, desc
[k
].name
, desc
[k
].disposition
);
521 printf_unfiltered (_(" descr %d: type=%u\n"),
525 data
+= bod
->msgh_descriptor_count
526 * sizeof (mach_msg_port_descriptor_t
);
527 size
-= bod
->msgh_descriptor_count
528 * sizeof (mach_msg_port_descriptor_t
);
529 ndr
= (NDR_record_t
*)(desc
+ bod
->msgh_descriptor_count
);
531 (_("NDR: mig=%02x if=%02x encod=%02x "
532 "int=%02x char=%02x float=%02x\n"),
533 ndr
->mig_vers
, ndr
->if_vers
, ndr
->mig_encoding
,
534 ndr
->int_rep
, ndr
->char_rep
, ndr
->float_rep
);
535 data
+= sizeof (NDR_record_t
);
536 size
-= sizeof (NDR_record_t
);
539 printf_unfiltered (_(" data:"));
540 ldata
= (const unsigned int *)data
;
541 for (i
= 0; i
< size
/ sizeof (unsigned int); i
++)
542 printf_unfiltered (" %08x", ldata
[i
]);
543 printf_unfiltered (_("\n"));
548 darwin_decode_exception_message (mach_msg_header_t
*hdr
,
549 struct inferior
**pinf
,
550 darwin_thread_t
**pthread
)
552 mach_msg_body_t
*bod
= (mach_msg_body_t
*)(hdr
+ 1);
553 mach_msg_port_descriptor_t
*desc
= (mach_msg_port_descriptor_t
*)(bod
+ 1);
556 struct inferior
*inf
;
557 darwin_thread_t
*thread
;
559 thread_t thread_port
;
563 /* Check message destination. */
564 if (hdr
->msgh_local_port
!= darwin_ex_port
)
567 /* Check message header. */
568 if (!(hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
571 /* Check descriptors. */
572 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
573 + sizeof (*ndr
) + 2 * sizeof (integer_t
))
574 || bod
->msgh_descriptor_count
!= 2
575 || desc
[0].type
!= MACH_MSG_PORT_DESCRIPTOR
576 || desc
[0].disposition
!= MACH_MSG_TYPE_MOVE_SEND
577 || desc
[1].type
!= MACH_MSG_PORT_DESCRIPTOR
578 || desc
[1].disposition
!= MACH_MSG_TYPE_MOVE_SEND
)
581 /* Check data representation. */
582 ndr
= (NDR_record_t
*)(desc
+ 2);
583 if (ndr
->mig_vers
!= NDR_PROTOCOL_2_0
584 || ndr
->if_vers
!= NDR_PROTOCOL_2_0
585 || ndr
->mig_encoding
!= NDR_record
.mig_encoding
586 || ndr
->int_rep
!= NDR_record
.int_rep
587 || ndr
->char_rep
!= NDR_record
.char_rep
588 || ndr
->float_rep
!= NDR_record
.float_rep
)
591 /* Ok, the hard work. */
592 data
= (integer_t
*)(ndr
+ 1);
594 task_port
= desc
[1].name
;
595 thread_port
= desc
[0].name
;
597 /* We got new rights to the task and the thread. Get rid of them. */
598 kret
= mach_port_deallocate (mach_task_self (), task_port
);
599 MACH_CHECK_ERROR (kret
);
600 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
601 MACH_CHECK_ERROR (kret
);
603 /* Find process by port. */
604 inf
= darwin_find_inferior_by_task (task_port
);
608 /* Not a known inferior. This could happen if the child fork, as
609 the created process will inherit its exception port.
610 FIXME: should the exception port be restored ? */
612 mig_reply_error_t reply
;
614 darwin_encode_reply (&reply
, hdr
, KERN_SUCCESS
);
616 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
617 reply
.Head
.msgh_size
, 0,
618 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
620 MACH_CHECK_ERROR (kret
);
625 /* Find thread by port. */
626 /* Check for new threads. Do it early so that the port in the exception
627 message can be deallocated. */
628 darwin_check_new_threads (inf
);
630 thread
= darwin_find_thread (inf
, thread_port
);
635 /* The thread should be running. However we have observed cases where a
636 thread got a SIGTTIN message after being stopped. */
637 gdb_assert (thread
->msg_state
!= DARWIN_MESSAGE
);
639 /* Finish decoding. */
640 thread
->event
.header
= *hdr
;
641 thread
->event
.thread_port
= thread_port
;
642 thread
->event
.task_port
= task_port
;
643 thread
->event
.ex_type
= data
[0];
644 thread
->event
.data_count
= data
[1];
646 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
647 + sizeof (*ndr
) + 2 * sizeof (integer_t
)
648 + data
[1] * sizeof (integer_t
)))
650 for (i
= 0; i
< data
[1]; i
++)
651 thread
->event
.ex_data
[i
] = data
[2 + i
];
653 thread
->msg_state
= DARWIN_MESSAGE
;
659 darwin_encode_reply (mig_reply_error_t
*reply
, mach_msg_header_t
*hdr
,
662 mach_msg_header_t
*rh
= &reply
->Head
;
664 rh
->msgh_bits
= MACH_MSGH_BITS (MACH_MSGH_BITS_REMOTE (hdr
->msgh_bits
), 0);
665 rh
->msgh_remote_port
= hdr
->msgh_remote_port
;
666 rh
->msgh_size
= (mach_msg_size_t
) sizeof (mig_reply_error_t
);
667 rh
->msgh_local_port
= MACH_PORT_NULL
;
668 rh
->msgh_id
= hdr
->msgh_id
+ 100;
670 reply
->NDR
= NDR_record
;
671 reply
->RetCode
= code
;
675 darwin_send_reply (struct inferior
*inf
, darwin_thread_t
*thread
)
678 mig_reply_error_t reply
;
680 darwin_encode_reply (&reply
, &thread
->event
.header
, KERN_SUCCESS
);
682 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
683 reply
.Head
.msgh_size
, 0,
684 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
686 MACH_CHECK_ERROR (kret
);
688 inf
->private->pending_messages
--;
692 darwin_resume_thread (struct inferior
*inf
, darwin_thread_t
*thread
,
693 int step
, int nsignal
)
699 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
700 thread
->msg_state
, thread
->gdb_port
, step
, nsignal
);
702 switch (thread
->msg_state
)
705 if (thread
->event
.ex_type
== EXC_SOFTWARE
706 && thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
708 /* Either deliver a new signal or cancel the signal received. */
709 res
= PTRACE (PT_THUPDATE
, inf
->pid
,
710 (void *)(uintptr_t)thread
->gdb_port
, nsignal
);
712 inferior_debug (1, _("ptrace THUP: res=%d\n"), res
);
716 /* Note: ptrace is allowed only if the process is stopped.
717 Directly send the signal to the thread. */
718 res
= syscall (SYS___pthread_kill
, thread
->gdb_port
, nsignal
);
719 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
720 thread
->gdb_port
, nsignal
, res
);
721 thread
->signaled
= 1;
724 /* Set or reset single step. */
725 if (step
!= thread
->single_step
)
727 inferior_debug (4, _("darwin_set_sstep (thread=0x%x, enable=%d)\n"),
728 thread
->gdb_port
, step
);
729 darwin_set_sstep (thread
->gdb_port
, step
);
730 thread
->single_step
= step
;
733 darwin_send_reply (inf
, thread
);
734 thread
->msg_state
= DARWIN_RUNNING
;
741 kret
= thread_resume (thread
->gdb_port
);
742 MACH_CHECK_ERROR (kret
);
744 thread
->msg_state
= DARWIN_RUNNING
;
749 /* Resume all threads of the inferior. */
752 darwin_resume_inferior_threads (struct inferior
*inf
, int step
, int nsignal
)
754 darwin_thread_t
*thread
;
758 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
760 darwin_resume_thread (inf
, thread
, step
, nsignal
);
763 struct resume_inferior_threads_param
770 darwin_resume_inferior_threads_it (struct inferior
*inf
, void *param
)
772 int step
= ((struct resume_inferior_threads_param
*)param
)->step
;
773 int nsignal
= ((struct resume_inferior_threads_param
*)param
)->nsignal
;
775 darwin_resume_inferior_threads (inf
, step
, nsignal
);
780 /* Suspend all threads of INF. */
783 darwin_suspend_inferior_threads (struct inferior
*inf
)
785 darwin_thread_t
*thread
;
790 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
792 switch (thread
->msg_state
)
798 kret
= thread_suspend (thread
->gdb_port
);
799 MACH_CHECK_ERROR (kret
);
800 thread
->msg_state
= DARWIN_STOPPED
;
806 darwin_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
808 struct target_waitstatus status
;
814 struct inferior
*inf
;
817 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
818 ptid_get_pid (ptid
), ptid_get_tid (ptid
), step
, signal
);
820 if (signal
== GDB_SIGNAL_0
)
823 nsignal
= gdb_signal_to_host (signal
);
825 /* Don't try to single step all threads. */
827 ptid
= inferior_ptid
;
829 /* minus_one_ptid is RESUME_ALL. */
830 if (ptid_equal (ptid
, minus_one_ptid
))
832 struct resume_inferior_threads_param param
;
834 param
.nsignal
= nsignal
;
837 /* Resume threads. */
838 iterate_over_inferiors (darwin_resume_inferior_threads_it
, ¶m
);
840 iterate_over_inferiors (darwin_resume_inferior_it
, NULL
);
844 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ptid
));
845 long tid
= ptid_get_tid (ptid
);
847 /* Stop the inferior (should be useless). */
848 darwin_suspend_inferior (inf
);
851 darwin_resume_inferior_threads (inf
, step
, nsignal
);
854 darwin_thread_t
*thread
;
856 /* Suspend threads of the task. */
857 darwin_suspend_inferior_threads (inf
);
859 /* Resume the selected thread. */
860 thread
= darwin_find_thread (inf
, tid
);
862 darwin_resume_thread (inf
, thread
, step
, nsignal
);
865 /* Resume the task. */
866 darwin_resume_inferior (inf
);
871 darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
872 enum gdb_signal signal
)
874 return darwin_resume (ptid
, step
, signal
);
878 darwin_decode_message (mach_msg_header_t
*hdr
,
879 darwin_thread_t
**pthread
,
880 struct inferior
**pinf
,
881 struct target_waitstatus
*status
)
883 darwin_thread_t
*thread
;
884 struct inferior
*inf
;
886 /* Exception message. 2401 == 0x961 is exc. */
887 if (hdr
->msgh_id
== 2401)
891 /* Decode message. */
892 res
= darwin_decode_exception_message (hdr
, &inf
, &thread
);
896 /* Should not happen... */
898 (_("darwin_wait: ill-formatted message (id=0x%x)\n"), hdr
->msgh_id
);
899 /* FIXME: send a failure reply? */
900 status
->kind
= TARGET_WAITKIND_IGNORE
;
901 return minus_one_ptid
;
905 status
->kind
= TARGET_WAITKIND_IGNORE
;
906 return minus_one_ptid
;
910 inf
->private->pending_messages
++;
912 status
->kind
= TARGET_WAITKIND_STOPPED
;
913 thread
->msg_state
= DARWIN_MESSAGE
;
915 inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"),
917 unparse_exception_type (thread
->event
.ex_type
));
919 switch (thread
->event
.ex_type
)
922 status
->value
.sig
= GDB_EXC_BAD_ACCESS
;
924 case EXC_BAD_INSTRUCTION
:
925 status
->value
.sig
= GDB_EXC_BAD_INSTRUCTION
;
928 status
->value
.sig
= GDB_EXC_ARITHMETIC
;
931 status
->value
.sig
= GDB_EXC_EMULATION
;
934 if (thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
937 gdb_signal_from_host (thread
->event
.ex_data
[1]);
938 inferior_debug (5, _(" (signal %d: %s)\n"),
939 thread
->event
.ex_data
[1],
940 gdb_signal_to_name (status
->value
.sig
));
942 /* If the thread is stopped because it has received a signal
943 that gdb has just sent, continue. */
944 if (thread
->signaled
)
946 thread
->signaled
= 0;
947 darwin_send_reply (inf
, thread
);
948 thread
->msg_state
= DARWIN_RUNNING
;
949 status
->kind
= TARGET_WAITKIND_IGNORE
;
953 status
->value
.sig
= GDB_EXC_SOFTWARE
;
956 /* Many internal GDB routines expect breakpoints to be reported
957 as GDB_SIGNAL_TRAP, and will report GDB_EXC_BREAKPOINT
958 as a spurious signal. */
959 status
->value
.sig
= GDB_SIGNAL_TRAP
;
962 status
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
966 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
968 else if (hdr
->msgh_id
== 0x48)
970 /* MACH_NOTIFY_DEAD_NAME: notification for exit. */
974 inf
= darwin_find_inferior_by_notify (hdr
->msgh_local_port
);
977 if (!inf
->private->no_ptrace
)
982 res
= wait4 (inf
->pid
, &wstatus
, 0, NULL
);
983 if (res
< 0 || res
!= inf
->pid
)
985 printf_unfiltered (_("wait4: res=%d: %s\n"),
986 res
, safe_strerror (errno
));
987 status
->kind
= TARGET_WAITKIND_IGNORE
;
988 return minus_one_ptid
;
990 if (WIFEXITED (wstatus
))
992 status
->kind
= TARGET_WAITKIND_EXITED
;
993 status
->value
.integer
= WEXITSTATUS (wstatus
);
997 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
998 status
->value
.sig
= WTERMSIG (wstatus
);
1001 inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"),
1004 /* Looks necessary on Leopard and harmless... */
1005 wait4 (inf
->pid
, &wstatus
, 0, NULL
);
1007 return ptid_build (inf
->pid
, 0, 0);
1011 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf
->pid
);
1012 status
->kind
= TARGET_WAITKIND_EXITED
;
1013 status
->value
.integer
= 0; /* Don't know. */
1014 return ptid_build (inf
->pid
, 0, 0);
1019 /* Unknown message. */
1020 warning (_("darwin: got unknown message, id: 0x%x\n"), hdr
->msgh_id
);
1021 status
->kind
= TARGET_WAITKIND_IGNORE
;
1022 return minus_one_ptid
;
1026 cancel_breakpoint (ptid_t ptid
)
1028 /* Arrange for a breakpoint to be hit again later. We will handle
1029 the current event, eventually we will resume this thread, and this
1030 breakpoint will trap again.
1032 If we do not do this, then we run the risk that the user will
1033 delete or disable the breakpoint, but the thread will have already
1036 struct regcache
*regcache
= get_thread_regcache (ptid
);
1037 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1040 pc
= regcache_read_pc (regcache
) - target_decr_pc_after_break (gdbarch
);
1041 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
1043 inferior_debug (4, "cancel_breakpoint for thread 0x%x\n",
1044 ptid_get_tid (ptid
));
1046 /* Back up the PC if necessary. */
1047 if (target_decr_pc_after_break (gdbarch
))
1048 regcache_write_pc (regcache
, pc
);
1056 darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
)
1061 mach_msg_header_t hdr
;
1064 mach_msg_header_t
*hdr
= &msgin
.hdr
;
1066 darwin_thread_t
*thread
;
1067 struct inferior
*inf
;
1070 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1071 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
1073 /* Handle fake stop events at first. */
1074 if (darwin_inf_fake_stop
!= NULL
)
1076 inf
= darwin_inf_fake_stop
;
1077 darwin_inf_fake_stop
= NULL
;
1079 status
->kind
= TARGET_WAITKIND_STOPPED
;
1080 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1081 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1082 thread
->msg_state
= DARWIN_STOPPED
;
1083 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1088 /* set_sigint_trap (); */
1090 /* Wait for a message. */
1091 kret
= mach_msg (&msgin
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
, 0,
1092 sizeof (msgin
.data
), darwin_port_set
, 0, MACH_PORT_NULL
);
1094 /* clear_sigint_trap (); */
1096 if (kret
== MACH_RCV_INTERRUPTED
)
1098 status
->kind
= TARGET_WAITKIND_IGNORE
;
1099 return minus_one_ptid
;
1102 if (kret
!= MACH_MSG_SUCCESS
)
1104 inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret
);
1105 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
1106 return minus_one_ptid
;
1109 /* Debug: display message. */
1110 if (darwin_debug_flag
> 10)
1111 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1113 res
= darwin_decode_message (hdr
, &thread
, &inf
, status
);
1114 if (ptid_equal (res
, minus_one_ptid
))
1117 /* Early return in case an inferior has exited. */
1121 while (status
->kind
== TARGET_WAITKIND_IGNORE
);
1123 /* Stop all tasks. */
1124 iterate_over_inferiors (darwin_suspend_inferior_it
, NULL
);
1126 /* Read pending messages. */
1129 struct target_waitstatus status2
;
1132 kret
= mach_msg (&msgin
.hdr
,
1133 MACH_RCV_MSG
| MACH_RCV_TIMEOUT
, 0,
1134 sizeof (msgin
.data
), darwin_port_set
, 1, MACH_PORT_NULL
);
1136 if (kret
== MACH_RCV_TIMED_OUT
)
1138 if (kret
!= MACH_MSG_SUCCESS
)
1141 (5, _("darwin_wait: mach_msg(pending) ret=0x%x\n"), kret
);
1145 /* Debug: display message. */
1146 if (darwin_debug_flag
> 10)
1147 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1149 ptid2
= darwin_decode_message (hdr
, &thread
, &inf
, &status2
);
1151 if (inf
!= NULL
&& thread
!= NULL
1152 && thread
->event
.ex_type
== EXC_BREAKPOINT
)
1154 if (thread
->single_step
1155 || cancel_breakpoint (ptid_build (inf
->pid
, 0, thread
->gdb_port
)))
1157 gdb_assert (thread
->msg_state
== DARWIN_MESSAGE
);
1158 darwin_send_reply (inf
, thread
);
1159 thread
->msg_state
= DARWIN_RUNNING
;
1163 (3, _("darwin_wait: thread 0x%x hit a non-gdb breakpoint\n"),
1167 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1173 darwin_wait_to (struct target_ops
*ops
,
1174 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1176 return darwin_wait (ptid
, status
);
1180 darwin_stop (struct target_ops
*self
, ptid_t t
)
1182 struct inferior
*inf
= current_inferior ();
1184 /* FIXME: handle in no_ptrace mode. */
1185 gdb_assert (!inf
->private->no_ptrace
);
1186 kill (inf
->pid
, SIGINT
);
1190 darwin_mourn_inferior (struct target_ops
*ops
)
1192 struct inferior
*inf
= current_inferior ();
1197 unpush_target (darwin_ops
);
1199 /* Deallocate threads. */
1200 if (inf
->private->threads
)
1205 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1208 kret
= mach_port_deallocate (gdb_task
, t
->gdb_port
);
1209 MACH_CHECK_ERROR (kret
);
1211 VEC_free (darwin_thread_t
, inf
->private->threads
);
1212 inf
->private->threads
= NULL
;
1215 kret
= mach_port_move_member (gdb_task
,
1216 inf
->private->notify_port
, MACH_PORT_NULL
);
1217 MACH_CHECK_ERROR (kret
);
1219 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1220 MACH_NOTIFY_DEAD_NAME
, 0,
1222 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1224 /* This can fail if the task is dead. */
1225 inferior_debug (4, "task=0x%x, prev=0x%x, notify_port=0x%x\n",
1226 inf
->private->task
, prev
, inf
->private->notify_port
);
1228 if (kret
== KERN_SUCCESS
)
1230 kret
= mach_port_deallocate (gdb_task
, prev
);
1231 MACH_CHECK_ERROR (kret
);
1234 kret
= mach_port_destroy (gdb_task
, inf
->private->notify_port
);
1235 MACH_CHECK_ERROR (kret
);
1238 /* Deallocate saved exception ports. */
1239 for (i
= 0; i
< inf
->private->exception_info
.count
; i
++)
1241 kret
= mach_port_deallocate
1242 (gdb_task
, inf
->private->exception_info
.ports
[i
]);
1243 MACH_CHECK_ERROR (kret
);
1245 inf
->private->exception_info
.count
= 0;
1247 kret
= mach_port_deallocate (gdb_task
, inf
->private->task
);
1248 MACH_CHECK_ERROR (kret
);
1250 xfree (inf
->private);
1251 inf
->private = NULL
;
1253 generic_mourn_inferior ();
1257 darwin_reply_to_all_pending_messages (struct inferior
*inf
)
1263 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1266 if (t
->msg_state
== DARWIN_MESSAGE
)
1267 darwin_resume_thread (inf
, t
, 0, 0);
1272 darwin_stop_inferior (struct inferior
*inf
)
1274 struct target_waitstatus wstatus
;
1280 gdb_assert (inf
!= NULL
);
1282 darwin_suspend_inferior (inf
);
1284 darwin_reply_to_all_pending_messages (inf
);
1286 if (inf
->private->no_ptrace
)
1289 res
= kill (inf
->pid
, SIGSTOP
);
1291 warning (_("cannot kill: %s"), safe_strerror (errno
));
1293 /* Wait until the process is really stopped. */
1296 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1297 if (wstatus
.kind
== TARGET_WAITKIND_STOPPED
1298 && wstatus
.value
.sig
== GDB_SIGNAL_STOP
)
1303 static kern_return_t
1304 darwin_save_exception_ports (darwin_inferior
*inf
)
1308 inf
->exception_info
.count
=
1309 sizeof (inf
->exception_info
.ports
) / sizeof (inf
->exception_info
.ports
[0]);
1311 kret
= task_get_exception_ports
1312 (inf
->task
, EXC_MASK_ALL
, inf
->exception_info
.masks
,
1313 &inf
->exception_info
.count
, inf
->exception_info
.ports
,
1314 inf
->exception_info
.behaviors
, inf
->exception_info
.flavors
);
1318 static kern_return_t
1319 darwin_restore_exception_ports (darwin_inferior
*inf
)
1324 for (i
= 0; i
< inf
->exception_info
.count
; i
++)
1326 kret
= task_set_exception_ports
1327 (inf
->task
, inf
->exception_info
.masks
[i
], inf
->exception_info
.ports
[i
],
1328 inf
->exception_info
.behaviors
[i
], inf
->exception_info
.flavors
[i
]);
1329 if (kret
!= KERN_SUCCESS
)
1333 return KERN_SUCCESS
;
1337 darwin_kill_inferior (struct target_ops
*ops
)
1339 struct inferior
*inf
= current_inferior ();
1340 struct target_waitstatus wstatus
;
1346 if (ptid_equal (inferior_ptid
, null_ptid
))
1349 gdb_assert (inf
!= NULL
);
1351 kret
= darwin_restore_exception_ports (inf
->private);
1352 MACH_CHECK_ERROR (kret
);
1354 darwin_reply_to_all_pending_messages (inf
);
1356 res
= kill (inf
->pid
, 9);
1360 darwin_resume_inferior (inf
);
1362 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1364 else if (errno
!= ESRCH
)
1365 warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"),
1366 inf
->pid
, safe_strerror (errno
));
1368 target_mourn_inferior ();
1372 darwin_attach_pid (struct inferior
*inf
)
1375 mach_port_t prev_port
;
1377 mach_port_t prev_not
;
1378 exception_mask_t mask
;
1380 inf
->private = XCNEW (darwin_inferior
);
1382 kret
= task_for_pid (gdb_task
, inf
->pid
, &inf
->private->task
);
1383 if (kret
!= KERN_SUCCESS
)
1387 if (!inf
->attach_flag
)
1390 waitpid (inf
->pid
, &status
, 0);
1393 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1394 " (please check gdb is codesigned - see taskgated(8))"),
1395 inf
->pid
, mach_error_string (kret
), (unsigned long) kret
);
1398 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1399 inf
->private->task
, inf
->pid
);
1401 if (darwin_ex_port
== MACH_PORT_NULL
)
1403 /* Create a port to get exceptions. */
1404 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1406 if (kret
!= KERN_SUCCESS
)
1407 error (_("Unable to create exception port, mach_port_allocate "
1411 kret
= mach_port_insert_right (gdb_task
, darwin_ex_port
, darwin_ex_port
,
1412 MACH_MSG_TYPE_MAKE_SEND
);
1413 if (kret
!= KERN_SUCCESS
)
1414 error (_("Unable to create exception port, mach_port_insert_right "
1418 /* Create a port set and put ex_port in it. */
1419 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_PORT_SET
,
1421 if (kret
!= KERN_SUCCESS
)
1422 error (_("Unable to create port set, mach_port_allocate "
1426 kret
= mach_port_move_member (gdb_task
, darwin_ex_port
, darwin_port_set
);
1427 if (kret
!= KERN_SUCCESS
)
1428 error (_("Unable to move exception port into new port set, "
1429 "mach_port_move_member\n"
1434 /* Create a port to be notified when the child task terminates. */
1435 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1436 &inf
->private->notify_port
);
1437 if (kret
!= KERN_SUCCESS
)
1438 error (_("Unable to create notification port, mach_port_allocate "
1442 kret
= mach_port_move_member (gdb_task
,
1443 inf
->private->notify_port
, darwin_port_set
);
1444 if (kret
!= KERN_SUCCESS
)
1445 error (_("Unable to move notification port into new port set, "
1446 "mach_port_move_member\n"
1450 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1451 MACH_NOTIFY_DEAD_NAME
, 0,
1452 inf
->private->notify_port
,
1453 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1455 if (kret
!= KERN_SUCCESS
)
1456 error (_("Termination notification request failed, "
1457 "mach_port_request_notification\n"
1460 if (prev_not
!= MACH_PORT_NULL
)
1462 /* This is unexpected, as there should not be any previously
1463 registered notification request. But this is not a fatal
1464 issue, so just emit a warning. */
1466 A task termination request was registered before the debugger registered\n\
1467 its own. This is unexpected, but should otherwise not have any actual\n\
1468 impact on the debugging session."));
1471 kret
= darwin_save_exception_ports (inf
->private);
1472 if (kret
!= KERN_SUCCESS
)
1473 error (_("Unable to save exception ports, task_get_exception_ports"
1477 /* Set exception port. */
1478 if (enable_mach_exceptions
)
1479 mask
= EXC_MASK_ALL
;
1481 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1482 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1483 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1484 if (kret
!= KERN_SUCCESS
)
1485 error (_("Unable to set exception ports, task_set_exception_ports"
1489 push_target (darwin_ops
);
1493 darwin_init_thread_list (struct inferior
*inf
)
1495 darwin_thread_t
*thread
;
1498 darwin_check_new_threads (inf
);
1500 gdb_assert (inf
->private->threads
1501 && VEC_length (darwin_thread_t
, inf
->private->threads
) > 0);
1502 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1504 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1506 new_ptid
= ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1507 thread_change_ptid (inferior_ptid
, new_ptid
);
1508 inferior_ptid
= new_ptid
;
1511 /* The child must synchronize with gdb: gdb must set the exception port
1512 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1513 FIXME: is there a lighter way ? */
1514 static int ptrace_fds
[2];
1517 darwin_ptrace_me (void)
1522 /* Close write end point. */
1523 close (ptrace_fds
[1]);
1525 /* Wait until gdb is ready. */
1526 res
= read (ptrace_fds
[0], &c
, 1);
1528 error (_("unable to read from pipe, read returned: %d"), res
);
1529 close (ptrace_fds
[0]);
1531 /* Get rid of privileges. */
1532 setegid (getgid ());
1535 PTRACE (PT_TRACE_ME
, 0, 0, 0);
1537 /* Redirect signals to exception port. */
1538 PTRACE (PT_SIGEXC
, 0, 0, 0);
1541 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1543 darwin_pre_ptrace (void)
1545 if (pipe (ptrace_fds
) != 0)
1549 error (_("unable to create a pipe: %s"), safe_strerror (errno
));
1552 mark_fd_no_cloexec (ptrace_fds
[0]);
1553 mark_fd_no_cloexec (ptrace_fds
[1]);
1557 darwin_ptrace_him (int pid
)
1561 mach_port_t prev_port
;
1563 struct inferior
*inf
= current_inferior ();
1565 darwin_attach_pid (inf
);
1567 /* Let's the child run. */
1568 close (ptrace_fds
[0]);
1569 close (ptrace_fds
[1]);
1571 unmark_fd_no_cloexec (ptrace_fds
[0]);
1572 unmark_fd_no_cloexec (ptrace_fds
[1]);
1574 darwin_init_thread_list (inf
);
1576 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
1580 darwin_execvp (const char *file
, char * const argv
[], char * const env
[])
1582 posix_spawnattr_t attr
;
1586 res
= posix_spawnattr_init (&attr
);
1590 (gdb_stderr
, "Cannot initialize attribute for posix_spawn\n");
1594 /* Do like execve: replace the image. */
1595 ps_flags
= POSIX_SPAWN_SETEXEC
;
1597 /* Disable ASLR. The constant doesn't look to be available outside the
1598 kernel include files. */
1599 #ifndef _POSIX_SPAWN_DISABLE_ASLR
1600 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1602 ps_flags
|= _POSIX_SPAWN_DISABLE_ASLR
;
1603 res
= posix_spawnattr_setflags (&attr
, ps_flags
);
1606 fprintf_unfiltered (gdb_stderr
, "Cannot set posix_spawn flags\n");
1610 posix_spawnp (NULL
, argv
[0], NULL
, &attr
, argv
, env
);
1614 darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
1615 char *allargs
, char **env
, int from_tty
)
1617 /* Do the hard work. */
1618 fork_inferior (exec_file
, allargs
, env
, darwin_ptrace_me
, darwin_ptrace_him
,
1619 darwin_pre_ptrace
, NULL
, darwin_execvp
);
1621 /* Return now in case of error. */
1622 if (ptid_equal (inferior_ptid
, null_ptid
))
1627 /* Set things up such that the next call to darwin_wait will immediately
1628 return a fake stop event for inferior INF.
1630 This assumes that the inferior's thread list has been initialized,
1631 as it will suspend the inferior's first thread. */
1634 darwin_setup_fake_stop_event (struct inferior
*inf
)
1636 darwin_thread_t
*thread
;
1639 gdb_assert (darwin_inf_fake_stop
== NULL
);
1640 darwin_inf_fake_stop
= inf
;
1642 /* When detecting a fake pending stop event, darwin_wait returns
1643 an event saying that the first thread is in a DARWIN_STOPPED
1644 state. To make that accurate, we need to suspend that thread
1645 as well. Otherwise, we'll try resuming it when resuming the
1646 inferior, and get a warning because the thread's suspend count
1647 is already zero, making the resume request useless. */
1648 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1649 kret
= thread_suspend (thread
->gdb_port
);
1650 MACH_CHECK_ERROR (kret
);
1653 /* Attach to process PID, then initialize for debugging it
1654 and wait for the trace-trap that results from attaching. */
1656 darwin_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1662 struct inferior
*inf
;
1665 pid
= parse_pid_to_attach (args
);
1667 if (pid
== getpid ()) /* Trying to masturbate? */
1668 error (_("I refuse to debug myself!"));
1672 char *exec_file
= get_exec_file (0);
1675 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
1676 target_pid_to_str (pid_to_ptid (pid
)));
1678 printf_unfiltered (_("Attaching to %s\n"),
1679 target_pid_to_str (pid_to_ptid (pid
)));
1681 gdb_flush (gdb_stdout
);
1684 if (pid
== 0 || kill (pid
, 0) < 0)
1685 error (_("Can't attach to process %d: %s (%d)"),
1686 pid
, safe_strerror (errno
), errno
);
1688 inferior_ptid
= pid_to_ptid (pid
);
1689 inf
= current_inferior ();
1690 inferior_appeared (inf
, pid
);
1691 inf
->attach_flag
= 1;
1693 /* Always add a main thread. */
1694 add_thread_silent (inferior_ptid
);
1696 darwin_attach_pid (inf
);
1698 darwin_suspend_inferior (inf
);
1700 darwin_init_thread_list (inf
);
1702 darwin_check_osabi (inf
->private, ptid_get_tid (inferior_ptid
));
1704 darwin_setup_fake_stop_event (inf
);
1706 inf
->private->no_ptrace
= 1;
1709 /* Take a program previously attached to and detaches it.
1710 The program resumes execution and will no longer stop
1711 on signals, etc. We'd better not have left any breakpoints
1712 in the program or it'll die when it hits one. For this
1713 to work, it may be necessary for the process to have been
1714 previously attached. It *might* work if the program was
1715 started via fork. */
1717 darwin_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1719 pid_t pid
= ptid_get_pid (inferior_ptid
);
1720 struct inferior
*inf
= current_inferior ();
1724 /* Display message. */
1727 char *exec_file
= get_exec_file (0);
1730 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
1731 target_pid_to_str (pid_to_ptid (pid
)));
1732 gdb_flush (gdb_stdout
);
1735 /* If ptrace() is in use, stop the process. */
1736 if (!inf
->private->no_ptrace
)
1737 darwin_stop_inferior (inf
);
1739 kret
= darwin_restore_exception_ports (inf
->private);
1740 MACH_CHECK_ERROR (kret
);
1742 if (!inf
->private->no_ptrace
)
1744 res
= PTRACE (PT_DETACH
, inf
->pid
, 0, 0);
1746 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1747 inf
->pid
, safe_strerror (errno
), errno
);
1750 darwin_reply_to_all_pending_messages (inf
);
1752 /* When using ptrace, we have just performed a PT_DETACH, which
1753 resumes the inferior. On the other hand, when we are not using
1754 ptrace, we need to resume its execution ourselves. */
1755 if (inf
->private->no_ptrace
)
1756 darwin_resume_inferior (inf
);
1758 darwin_mourn_inferior (ops
);
1762 darwin_files_info (struct target_ops
*ops
)
1767 darwin_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1769 static char buf
[80];
1770 long tid
= ptid_get_tid (ptid
);
1774 snprintf (buf
, sizeof (buf
), _("Thread 0x%lx of process %u"),
1775 tid
, ptid_get_pid (ptid
));
1779 return normal_pid_to_str (ptid
);
1783 darwin_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1788 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1789 copy it to RDADDR in gdb's address space.
1790 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1791 to ADDR in inferior task's address space.
1792 Return 0 on failure; number of bytes read / writen otherwise. */
1794 darwin_read_write_inferior (task_t task
, CORE_ADDR addr
,
1795 gdb_byte
*rdaddr
, const gdb_byte
*wraddr
,
1799 mach_vm_address_t offset
= addr
& (mach_page_size
- 1);
1800 mach_vm_address_t low_address
= (mach_vm_address_t
) (addr
- offset
);
1801 mach_vm_size_t aligned_length
= (mach_vm_size_t
) PAGE_ROUND (offset
+ length
);
1803 mach_msg_type_number_t copy_count
;
1804 mach_vm_size_t remaining_length
;
1805 mach_vm_address_t region_address
;
1806 mach_vm_size_t region_length
;
1808 inferior_debug (8, _("darwin_read_write_inferior(task=0x%x, %s, len=%s)\n"),
1809 task
, core_addr_to_string (addr
), pulongest (length
));
1811 /* Get memory from inferior with page aligned addresses. */
1812 kret
= mach_vm_read (task
, low_address
, aligned_length
,
1813 &copied
, ©_count
);
1814 if (kret
!= KERN_SUCCESS
)
1817 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1818 core_addr_to_string (addr
), mach_error_string (kret
));
1823 memcpy (rdaddr
, (char *)copied
+ offset
, length
);
1828 memcpy ((char *)copied
+ offset
, wraddr
, length
);
1830 /* Do writes atomically.
1831 First check for holes and unwritable memory. */
1832 for (region_address
= low_address
, remaining_length
= aligned_length
;
1833 region_address
< low_address
+ aligned_length
;
1834 region_address
+= region_length
, remaining_length
-= region_length
)
1836 vm_region_submap_short_info_data_64_t info
;
1837 mach_vm_address_t region_start
= region_address
;
1838 mach_msg_type_number_t count
;
1839 natural_t region_depth
;
1841 region_depth
= 100000;
1842 count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
1843 kret
= mach_vm_region_recurse
1844 (task
, ®ion_start
, ®ion_length
, ®ion_depth
,
1845 (vm_region_recurse_info_t
) &info
, &count
);
1847 if (kret
!= KERN_SUCCESS
)
1849 inferior_debug (1, _("darwin_read_write_inferior: "
1850 "mach_vm_region_recurse failed at %s: %s\n"),
1851 core_addr_to_string (region_address
),
1852 mach_error_string (kret
));
1857 (9, _("darwin_read_write_inferior: "
1858 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1859 core_addr_to_string (region_address
),
1860 core_addr_to_string (region_start
),
1861 core_addr_to_string (region_length
));
1863 /* Check for holes in memory. */
1864 if (region_start
> region_address
)
1866 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1867 core_addr_to_string (region_address
),
1868 core_addr_to_string (region_start
),
1869 (unsigned)region_length
);
1874 /* Adjust the length. */
1875 region_length
-= (region_address
- region_start
);
1877 if (!(info
.max_protection
& VM_PROT_WRITE
))
1879 kret
= mach_vm_protect
1880 (task
, region_address
, region_length
,
1881 TRUE
, info
.max_protection
| VM_PROT_WRITE
| VM_PROT_COPY
);
1882 if (kret
!= KERN_SUCCESS
)
1884 warning (_("darwin_read_write_inf: "
1885 "mach_vm_protect max failed at %s: %s"),
1886 core_addr_to_string (region_address
),
1887 mach_error_string (kret
));
1893 if (!(info
.protection
& VM_PROT_WRITE
))
1895 kret
= mach_vm_protect (task
, region_address
, region_length
,
1896 FALSE
, info
.protection
| VM_PROT_WRITE
);
1897 if (kret
!= KERN_SUCCESS
)
1899 warning (_("darwin_read_write_inf: "
1900 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1901 core_addr_to_string (region_address
),
1902 (unsigned long)region_length
, mach_error_string (kret
));
1909 kret
= mach_vm_write (task
, low_address
, copied
, aligned_length
);
1911 if (kret
!= KERN_SUCCESS
)
1913 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1914 mach_error_string (kret
));
1918 mach_vm_deallocate (mach_task_self (), copied
, copy_count
);
1922 /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
1924 Return 0 on failure; number of bytes read / written otherwise. */
1926 #ifdef TASK_DYLD_INFO_COUNT
1927 /* This is not available in Darwin 9. */
1928 static enum target_xfer_status
1929 darwin_read_dyld_info (task_t task
, CORE_ADDR addr
, gdb_byte
*rdaddr
,
1930 ULONGEST length
, ULONGEST
*xfered_len
)
1932 struct task_dyld_info task_dyld_info
;
1933 mach_msg_type_number_t count
= TASK_DYLD_INFO_COUNT
;
1934 int sz
= TASK_DYLD_INFO_COUNT
* sizeof (natural_t
);
1938 return TARGET_XFER_EOF
;
1940 kret
= task_info (task
, TASK_DYLD_INFO
, (task_info_t
) &task_dyld_info
, &count
);
1941 MACH_CHECK_ERROR (kret
);
1942 if (kret
!= KERN_SUCCESS
)
1943 return TARGET_XFER_E_IO
;
1945 if (addr
+ length
> sz
)
1947 memcpy (rdaddr
, (char *)&task_dyld_info
+ addr
, length
);
1948 *xfered_len
= (ULONGEST
) length
;
1949 return TARGET_XFER_OK
;
1955 static enum target_xfer_status
1956 darwin_xfer_partial (struct target_ops
*ops
,
1957 enum target_object object
, const char *annex
,
1958 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1959 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1961 struct inferior
*inf
= current_inferior ();
1964 (8, _("darwin_xfer_partial(%s, %s, rbuf=%s, wbuf=%s) pid=%u\n"),
1965 core_addr_to_string (offset
), pulongest (len
),
1966 host_address_to_string (readbuf
), host_address_to_string (writebuf
),
1971 case TARGET_OBJECT_MEMORY
:
1973 int l
= darwin_read_write_inferior (inf
->private->task
, offset
,
1974 readbuf
, writebuf
, len
);
1977 return TARGET_XFER_EOF
;
1981 *xfered_len
= (ULONGEST
) l
;
1982 return TARGET_XFER_OK
;
1985 #ifdef TASK_DYLD_INFO_COUNT
1986 case TARGET_OBJECT_DARWIN_DYLD_INFO
:
1987 if (writebuf
!= NULL
|| readbuf
== NULL
)
1989 /* Support only read. */
1990 return TARGET_XFER_E_IO
;
1992 return darwin_read_dyld_info (inf
->private->task
, offset
, readbuf
, len
,
1996 return TARGET_XFER_E_IO
;
2002 set_enable_mach_exceptions (char *args
, int from_tty
,
2003 struct cmd_list_element
*c
)
2005 if (!ptid_equal (inferior_ptid
, null_ptid
))
2007 struct inferior
*inf
= current_inferior ();
2008 exception_mask_t mask
;
2011 if (enable_mach_exceptions
)
2012 mask
= EXC_MASK_ALL
;
2015 darwin_restore_exception_ports (inf
->private);
2016 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
2018 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
2019 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
2020 MACH_CHECK_ERROR (kret
);
2025 darwin_pid_to_exec_file (struct target_ops
*self
, int pid
)
2027 static char path
[PATH_MAX
];
2030 res
= proc_pidinfo (pid
, PROC_PIDPATHINFO
, 0, path
, PATH_MAX
);
2038 darwin_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
2043 struct inferior
*inf
= current_inferior ();
2045 mach_port_name_array_t names
;
2046 mach_msg_type_number_t names_count
;
2047 mach_port_type_array_t types
;
2048 mach_msg_type_number_t types_count
;
2051 /* First linear search. */
2053 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2055 if (t
->inf_port
== lwp
)
2056 return ptid_build (ptid_get_pid (inferior_ptid
), 0, t
->gdb_port
);
2058 /* Maybe the port was never extract. Do it now. */
2060 /* First get inferior port names. */
2061 kret
= mach_port_names (inf
->private->task
, &names
, &names_count
, &types
,
2063 MACH_CHECK_ERROR (kret
);
2064 if (kret
!= KERN_SUCCESS
)
2067 /* For each name, copy the right in the gdb space and then compare with
2068 our view of the inferior threads. We don't forget to deallocate the
2070 for (i
= 0; i
< names_count
; i
++)
2072 mach_port_t local_name
;
2073 mach_msg_type_name_t local_type
;
2075 /* We just need to know the corresponding name in gdb name space.
2076 So extract and deallocate the right. */
2077 kret
= mach_port_extract_right (inf
->private->task
, names
[i
],
2078 MACH_MSG_TYPE_COPY_SEND
,
2079 &local_name
, &local_type
);
2080 if (kret
!= KERN_SUCCESS
)
2082 mach_port_deallocate (gdb_task
, local_name
);
2085 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2087 if (t
->gdb_port
== local_name
)
2089 t
->inf_port
= names
[i
];
2090 if (names
[i
] == lwp
)
2095 vm_deallocate (gdb_task
, (vm_address_t
) names
,
2096 names_count
* sizeof (mach_port_t
));
2099 return ptid_build (ptid_get_pid (inferior_ptid
), 0, res
);
2105 darwin_supports_multi_process (struct target_ops
*self
)
2110 /* -Wmissing-prototypes */
2111 extern initialize_file_ftype _initialize_darwin_inferior
;
2114 _initialize_darwin_inferior (void)
2118 gdb_task
= mach_task_self ();
2119 darwin_host_self
= mach_host_self ();
2121 /* Read page size. */
2122 kret
= host_page_size (darwin_host_self
, &mach_page_size
);
2123 if (kret
!= KERN_SUCCESS
)
2125 mach_page_size
= 0x1000;
2126 MACH_CHECK_ERROR (kret
);
2129 darwin_ops
= inf_child_target ();
2131 darwin_ops
->to_shortname
= "darwin-child";
2132 darwin_ops
->to_longname
= _("Darwin child process");
2133 darwin_ops
->to_doc
=
2134 _("Darwin child process (started by the \"run\" command).");
2135 darwin_ops
->to_create_inferior
= darwin_create_inferior
;
2136 darwin_ops
->to_attach
= darwin_attach
;
2137 darwin_ops
->to_attach_no_wait
= 0;
2138 darwin_ops
->to_detach
= darwin_detach
;
2139 darwin_ops
->to_files_info
= darwin_files_info
;
2140 darwin_ops
->to_wait
= darwin_wait_to
;
2141 darwin_ops
->to_mourn_inferior
= darwin_mourn_inferior
;
2142 darwin_ops
->to_kill
= darwin_kill_inferior
;
2143 darwin_ops
->to_stop
= darwin_stop
;
2144 darwin_ops
->to_resume
= darwin_resume_to
;
2145 darwin_ops
->to_thread_alive
= darwin_thread_alive
;
2146 darwin_ops
->to_pid_to_str
= darwin_pid_to_str
;
2147 darwin_ops
->to_pid_to_exec_file
= darwin_pid_to_exec_file
;
2148 darwin_ops
->to_load
= NULL
;
2149 darwin_ops
->to_xfer_partial
= darwin_xfer_partial
;
2150 darwin_ops
->to_supports_multi_process
= darwin_supports_multi_process
;
2151 darwin_ops
->to_get_ada_task_ptid
= darwin_get_ada_task_ptid
;
2153 darwin_complete_target (darwin_ops
);
2155 add_target (darwin_ops
);
2157 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
2160 add_setshow_zuinteger_cmd ("darwin", class_obscure
,
2161 &darwin_debug_flag
, _("\
2162 Set if printing inferior communication debugging statements."), _("\
2163 Show if printing inferior communication debugging statements."), NULL
,
2165 &setdebuglist
, &showdebuglist
);
2167 add_setshow_boolean_cmd ("mach-exceptions", class_support
,
2168 &enable_mach_exceptions
, _("\
2169 Set if mach exceptions are caught."), _("\
2170 Show if mach exceptions are caught."), _("\
2171 When this mode is on, all low level exceptions are reported before being\n\
2172 reported by the kernel."),
2173 &set_enable_mach_exceptions
, NULL
,
2174 &setlist
, &showlist
);