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 /* Deallocate ports. */
307 for (i
= 0; i
< new_nbr
; i
++)
309 kret
= mach_port_deallocate (mach_task_self (), thread_list
[i
]);
310 MACH_CHECK_ERROR (kret
);
313 /* Deallocate the buffer. */
314 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
315 new_nbr
* sizeof (int));
316 MACH_CHECK_ERROR (kret
);
322 thread_vec
= VEC_alloc (darwin_thread_t
, new_nbr
);
324 for (new_ix
= 0, old_ix
= 0; new_ix
< new_nbr
|| old_ix
< old_nbr
;)
326 thread_t new_id
= (new_ix
< new_nbr
) ?
327 thread_list
[new_ix
] : THREAD_NULL
;
328 darwin_thread_t
*old
= (old_ix
< old_nbr
) ?
329 VEC_index (darwin_thread_t
, darwin_inf
->threads
, old_ix
) : NULL
;
330 thread_t old_id
= old
? old
->gdb_port
: THREAD_NULL
;
333 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:0x%x old_id:0x%x\n"),
334 new_ix
, new_nbr
, old_ix
, old_nbr
, new_id
, old_id
);
336 if (old_id
== new_id
)
338 /* Thread still exist. */
339 VEC_safe_push (darwin_thread_t
, thread_vec
, old
);
343 /* Deallocate the port. */
344 kret
= mach_port_deallocate (gdb_task
, new_id
);
345 MACH_CHECK_ERROR (kret
);
349 if (new_ix
< new_nbr
&& new_id
== MACH_PORT_DEAD
)
351 /* Ignore dead ports.
352 In some weird cases, we might get dead ports. They should
353 correspond to dead thread so they could safely be ignored. */
357 if (new_ix
< new_nbr
&& (old_ix
== old_nbr
|| new_id
< old_id
))
359 /* A thread was created. */
360 struct thread_info
*tp
;
361 struct private_thread_info
*pti
;
363 pti
= XCNEW (struct private_thread_info
);
364 pti
->gdb_port
= new_id
;
365 pti
->msg_state
= DARWIN_RUNNING
;
367 /* Add a new thread unless this is the first one ever met. */
368 if (!(old_nbr
== 0 && new_ix
== 0))
369 tp
= add_thread_with_info (ptid_build (inf
->pid
, 0, new_id
), pti
);
372 tp
= find_thread_ptid (ptid_build (inf
->pid
, 0, 0));
376 VEC_safe_push (darwin_thread_t
, thread_vec
, pti
);
380 if (old_ix
< old_nbr
&& (new_ix
== new_nbr
|| new_id
> old_id
))
382 /* A thread was removed. */
383 delete_thread (ptid_build (inf
->pid
, 0, old_id
));
384 kret
= mach_port_deallocate (gdb_task
, old_id
);
385 MACH_CHECK_ERROR (kret
);
389 gdb_assert_not_reached ("unexpected thread case");
392 if (darwin_inf
->threads
)
393 VEC_free (darwin_thread_t
, darwin_inf
->threads
);
394 darwin_inf
->threads
= thread_vec
;
396 /* Deallocate the buffer. */
397 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
398 new_nbr
* sizeof (int));
399 MACH_CHECK_ERROR (kret
);
403 find_inferior_task_it (struct inferior
*inf
, void *port_ptr
)
405 return inf
->private->task
== *(task_t
*)port_ptr
;
409 find_inferior_notify_it (struct inferior
*inf
, void *port_ptr
)
411 return inf
->private->notify_port
== *(task_t
*)port_ptr
;
414 /* Return an inferior by task port. */
415 static struct inferior
*
416 darwin_find_inferior_by_task (task_t port
)
418 return iterate_over_inferiors (&find_inferior_task_it
, &port
);
421 /* Return an inferior by notification port. */
422 static struct inferior
*
423 darwin_find_inferior_by_notify (mach_port_t port
)
425 return iterate_over_inferiors (&find_inferior_notify_it
, &port
);
428 /* Return a thread by port. */
429 static darwin_thread_t
*
430 darwin_find_thread (struct inferior
*inf
, thread_t thread
)
436 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
438 if (t
->gdb_port
== thread
)
443 /* Suspend (ie stop) an inferior at Mach level. */
446 darwin_suspend_inferior (struct inferior
*inf
)
448 if (!inf
->private->suspended
)
452 kret
= task_suspend (inf
->private->task
);
453 MACH_CHECK_ERROR (kret
);
455 inf
->private->suspended
= 1;
459 /* Resume an inferior at Mach level. */
462 darwin_resume_inferior (struct inferior
*inf
)
464 if (inf
->private->suspended
)
468 kret
= task_resume (inf
->private->task
);
469 MACH_CHECK_ERROR (kret
);
471 inf
->private->suspended
= 0;
475 /* Iterator functions. */
478 darwin_suspend_inferior_it (struct inferior
*inf
, void *arg
)
480 darwin_suspend_inferior (inf
);
481 darwin_check_new_threads (inf
);
486 darwin_resume_inferior_it (struct inferior
*inf
, void *arg
)
488 darwin_resume_inferior (inf
);
493 darwin_dump_message (mach_msg_header_t
*hdr
, int disp_body
)
495 printf_unfiltered (_("message header:\n"));
496 printf_unfiltered (_(" bits: 0x%x\n"), hdr
->msgh_bits
);
497 printf_unfiltered (_(" size: 0x%x\n"), hdr
->msgh_size
);
498 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr
->msgh_remote_port
);
499 printf_unfiltered (_(" local-port: 0x%x\n"), hdr
->msgh_local_port
);
500 printf_unfiltered (_(" reserved: 0x%x\n"), hdr
->msgh_reserved
);
501 printf_unfiltered (_(" id: 0x%x\n"), hdr
->msgh_id
);
505 const unsigned char *data
;
506 const unsigned int *ldata
;
510 data
= (unsigned char *)(hdr
+ 1);
511 size
= hdr
->msgh_size
- sizeof (mach_msg_header_t
);
513 if (hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
515 mach_msg_body_t
*bod
= (mach_msg_body_t
*)data
;
516 mach_msg_port_descriptor_t
*desc
=
517 (mach_msg_port_descriptor_t
*)(bod
+ 1);
520 printf_unfiltered (_("body: descriptor_count=%u\n"),
521 bod
->msgh_descriptor_count
);
522 data
+= sizeof (mach_msg_body_t
);
523 size
-= sizeof (mach_msg_body_t
);
524 for (k
= 0; k
< bod
->msgh_descriptor_count
; k
++)
525 switch (desc
[k
].type
)
527 case MACH_MSG_PORT_DESCRIPTOR
:
529 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
530 k
, desc
[k
].type
, desc
[k
].name
, desc
[k
].disposition
);
533 printf_unfiltered (_(" descr %d: type=%u\n"),
537 data
+= bod
->msgh_descriptor_count
538 * sizeof (mach_msg_port_descriptor_t
);
539 size
-= bod
->msgh_descriptor_count
540 * sizeof (mach_msg_port_descriptor_t
);
541 ndr
= (NDR_record_t
*)(desc
+ bod
->msgh_descriptor_count
);
543 (_("NDR: mig=%02x if=%02x encod=%02x "
544 "int=%02x char=%02x float=%02x\n"),
545 ndr
->mig_vers
, ndr
->if_vers
, ndr
->mig_encoding
,
546 ndr
->int_rep
, ndr
->char_rep
, ndr
->float_rep
);
547 data
+= sizeof (NDR_record_t
);
548 size
-= sizeof (NDR_record_t
);
551 printf_unfiltered (_(" data:"));
552 ldata
= (const unsigned int *)data
;
553 for (i
= 0; i
< size
/ sizeof (unsigned int); i
++)
554 printf_unfiltered (" %08x", ldata
[i
]);
555 printf_unfiltered (_("\n"));
560 darwin_decode_exception_message (mach_msg_header_t
*hdr
,
561 struct inferior
**pinf
,
562 darwin_thread_t
**pthread
)
564 mach_msg_body_t
*bod
= (mach_msg_body_t
*)(hdr
+ 1);
565 mach_msg_port_descriptor_t
*desc
= (mach_msg_port_descriptor_t
*)(bod
+ 1);
568 struct inferior
*inf
;
569 darwin_thread_t
*thread
;
571 thread_t thread_port
;
575 /* Check message destination. */
576 if (hdr
->msgh_local_port
!= darwin_ex_port
)
579 /* Check message header. */
580 if (!(hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
583 /* Check descriptors. */
584 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
585 + sizeof (*ndr
) + 2 * sizeof (integer_t
))
586 || bod
->msgh_descriptor_count
!= 2
587 || desc
[0].type
!= MACH_MSG_PORT_DESCRIPTOR
588 || desc
[0].disposition
!= MACH_MSG_TYPE_MOVE_SEND
589 || desc
[1].type
!= MACH_MSG_PORT_DESCRIPTOR
590 || desc
[1].disposition
!= MACH_MSG_TYPE_MOVE_SEND
)
593 /* Check data representation. */
594 ndr
= (NDR_record_t
*)(desc
+ 2);
595 if (ndr
->mig_vers
!= NDR_PROTOCOL_2_0
596 || ndr
->if_vers
!= NDR_PROTOCOL_2_0
597 || ndr
->mig_encoding
!= NDR_record
.mig_encoding
598 || ndr
->int_rep
!= NDR_record
.int_rep
599 || ndr
->char_rep
!= NDR_record
.char_rep
600 || ndr
->float_rep
!= NDR_record
.float_rep
)
603 /* Ok, the hard work. */
604 data
= (integer_t
*)(ndr
+ 1);
606 task_port
= desc
[1].name
;
607 thread_port
= desc
[0].name
;
609 /* We got new rights to the task, get rid of it. Do not get rid of thread
610 right, as we will need it to find the thread. */
611 kret
= mach_port_deallocate (mach_task_self (), task_port
);
612 MACH_CHECK_ERROR (kret
);
614 /* Find process by port. */
615 inf
= darwin_find_inferior_by_task (task_port
);
619 /* Not a known inferior. This could happen if the child fork, as
620 the created process will inherit its exception port.
621 FIXME: should the exception port be restored ? */
623 mig_reply_error_t reply
;
625 /* Free thread port (we don't know it). */
626 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
627 MACH_CHECK_ERROR (kret
);
629 darwin_encode_reply (&reply
, hdr
, KERN_SUCCESS
);
631 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
632 reply
.Head
.msgh_size
, 0,
633 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
635 MACH_CHECK_ERROR (kret
);
640 /* Find thread by port. */
641 /* Check for new threads. Do it early so that the port in the exception
642 message can be deallocated. */
643 darwin_check_new_threads (inf
);
645 /* Free the thread port (as gdb knows the thread, it has already has a right
646 for it, so this just decrement a reference counter). */
647 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
648 MACH_CHECK_ERROR (kret
);
650 thread
= darwin_find_thread (inf
, thread_port
);
655 /* The thread should be running. However we have observed cases where a
656 thread got a SIGTTIN message after being stopped. */
657 gdb_assert (thread
->msg_state
!= DARWIN_MESSAGE
);
659 /* Finish decoding. */
660 thread
->event
.header
= *hdr
;
661 thread
->event
.thread_port
= thread_port
;
662 thread
->event
.task_port
= task_port
;
663 thread
->event
.ex_type
= data
[0];
664 thread
->event
.data_count
= data
[1];
666 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
667 + sizeof (*ndr
) + 2 * sizeof (integer_t
)
668 + data
[1] * sizeof (integer_t
)))
670 for (i
= 0; i
< data
[1]; i
++)
671 thread
->event
.ex_data
[i
] = data
[2 + i
];
673 thread
->msg_state
= DARWIN_MESSAGE
;
679 darwin_encode_reply (mig_reply_error_t
*reply
, mach_msg_header_t
*hdr
,
682 mach_msg_header_t
*rh
= &reply
->Head
;
684 rh
->msgh_bits
= MACH_MSGH_BITS (MACH_MSGH_BITS_REMOTE (hdr
->msgh_bits
), 0);
685 rh
->msgh_remote_port
= hdr
->msgh_remote_port
;
686 rh
->msgh_size
= (mach_msg_size_t
) sizeof (mig_reply_error_t
);
687 rh
->msgh_local_port
= MACH_PORT_NULL
;
688 rh
->msgh_id
= hdr
->msgh_id
+ 100;
690 reply
->NDR
= NDR_record
;
691 reply
->RetCode
= code
;
695 darwin_send_reply (struct inferior
*inf
, darwin_thread_t
*thread
)
698 mig_reply_error_t reply
;
700 darwin_encode_reply (&reply
, &thread
->event
.header
, KERN_SUCCESS
);
702 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
703 reply
.Head
.msgh_size
, 0,
704 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
706 MACH_CHECK_ERROR (kret
);
708 inf
->private->pending_messages
--;
712 darwin_resume_thread (struct inferior
*inf
, darwin_thread_t
*thread
,
713 int step
, int nsignal
)
719 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
720 thread
->msg_state
, thread
->gdb_port
, step
, nsignal
);
722 switch (thread
->msg_state
)
725 if (thread
->event
.ex_type
== EXC_SOFTWARE
726 && thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
728 /* Either deliver a new signal or cancel the signal received. */
729 res
= PTRACE (PT_THUPDATE
, inf
->pid
,
730 (void *)(uintptr_t)thread
->gdb_port
, nsignal
);
732 inferior_debug (1, _("ptrace THUP: res=%d\n"), res
);
736 /* Note: ptrace is allowed only if the process is stopped.
737 Directly send the signal to the thread. */
738 res
= syscall (SYS___pthread_kill
, thread
->gdb_port
, nsignal
);
739 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
740 thread
->gdb_port
, nsignal
, res
);
741 thread
->signaled
= 1;
744 /* Set or reset single step. */
745 if (step
!= thread
->single_step
)
747 inferior_debug (4, _("darwin_set_sstep (thread=0x%x, enable=%d)\n"),
748 thread
->gdb_port
, step
);
749 darwin_set_sstep (thread
->gdb_port
, step
);
750 thread
->single_step
= step
;
753 darwin_send_reply (inf
, thread
);
754 thread
->msg_state
= DARWIN_RUNNING
;
761 kret
= thread_resume (thread
->gdb_port
);
762 MACH_CHECK_ERROR (kret
);
764 thread
->msg_state
= DARWIN_RUNNING
;
769 /* Resume all threads of the inferior. */
772 darwin_resume_inferior_threads (struct inferior
*inf
, int step
, int nsignal
)
774 darwin_thread_t
*thread
;
778 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
780 darwin_resume_thread (inf
, thread
, step
, nsignal
);
783 struct resume_inferior_threads_param
790 darwin_resume_inferior_threads_it (struct inferior
*inf
, void *param
)
792 int step
= ((struct resume_inferior_threads_param
*)param
)->step
;
793 int nsignal
= ((struct resume_inferior_threads_param
*)param
)->nsignal
;
795 darwin_resume_inferior_threads (inf
, step
, nsignal
);
800 /* Suspend all threads of INF. */
803 darwin_suspend_inferior_threads (struct inferior
*inf
)
805 darwin_thread_t
*thread
;
810 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
812 switch (thread
->msg_state
)
818 kret
= thread_suspend (thread
->gdb_port
);
819 MACH_CHECK_ERROR (kret
);
820 thread
->msg_state
= DARWIN_STOPPED
;
826 darwin_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
828 struct target_waitstatus status
;
834 struct inferior
*inf
;
837 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
838 ptid_get_pid (ptid
), ptid_get_tid (ptid
), step
, signal
);
840 if (signal
== GDB_SIGNAL_0
)
843 nsignal
= gdb_signal_to_host (signal
);
845 /* Don't try to single step all threads. */
847 ptid
= inferior_ptid
;
849 /* minus_one_ptid is RESUME_ALL. */
850 if (ptid_equal (ptid
, minus_one_ptid
))
852 struct resume_inferior_threads_param param
;
854 param
.nsignal
= nsignal
;
857 /* Resume threads. */
858 iterate_over_inferiors (darwin_resume_inferior_threads_it
, ¶m
);
860 iterate_over_inferiors (darwin_resume_inferior_it
, NULL
);
864 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ptid
));
865 long tid
= ptid_get_tid (ptid
);
867 /* Stop the inferior (should be useless). */
868 darwin_suspend_inferior (inf
);
871 darwin_resume_inferior_threads (inf
, step
, nsignal
);
874 darwin_thread_t
*thread
;
876 /* Suspend threads of the task. */
877 darwin_suspend_inferior_threads (inf
);
879 /* Resume the selected thread. */
880 thread
= darwin_find_thread (inf
, tid
);
882 darwin_resume_thread (inf
, thread
, step
, nsignal
);
885 /* Resume the task. */
886 darwin_resume_inferior (inf
);
891 darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
892 enum gdb_signal signal
)
894 return darwin_resume (ptid
, step
, signal
);
898 darwin_decode_message (mach_msg_header_t
*hdr
,
899 darwin_thread_t
**pthread
,
900 struct inferior
**pinf
,
901 struct target_waitstatus
*status
)
903 darwin_thread_t
*thread
;
904 struct inferior
*inf
;
906 /* Exception message. 2401 == 0x961 is exc. */
907 if (hdr
->msgh_id
== 2401)
911 /* Decode message. */
912 res
= darwin_decode_exception_message (hdr
, &inf
, &thread
);
916 /* Should not happen... */
918 (_("darwin_wait: ill-formatted message (id=0x%x)\n"), hdr
->msgh_id
);
919 /* FIXME: send a failure reply? */
920 status
->kind
= TARGET_WAITKIND_IGNORE
;
921 return minus_one_ptid
;
925 status
->kind
= TARGET_WAITKIND_IGNORE
;
926 return minus_one_ptid
;
930 inf
->private->pending_messages
++;
932 status
->kind
= TARGET_WAITKIND_STOPPED
;
933 thread
->msg_state
= DARWIN_MESSAGE
;
935 inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"),
937 unparse_exception_type (thread
->event
.ex_type
));
939 switch (thread
->event
.ex_type
)
942 status
->value
.sig
= GDB_EXC_BAD_ACCESS
;
944 case EXC_BAD_INSTRUCTION
:
945 status
->value
.sig
= GDB_EXC_BAD_INSTRUCTION
;
948 status
->value
.sig
= GDB_EXC_ARITHMETIC
;
951 status
->value
.sig
= GDB_EXC_EMULATION
;
954 if (thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
957 gdb_signal_from_host (thread
->event
.ex_data
[1]);
958 inferior_debug (5, _(" (signal %d: %s)\n"),
959 thread
->event
.ex_data
[1],
960 gdb_signal_to_name (status
->value
.sig
));
962 /* If the thread is stopped because it has received a signal
963 that gdb has just sent, continue. */
964 if (thread
->signaled
)
966 thread
->signaled
= 0;
967 darwin_send_reply (inf
, thread
);
968 thread
->msg_state
= DARWIN_RUNNING
;
969 status
->kind
= TARGET_WAITKIND_IGNORE
;
973 status
->value
.sig
= GDB_EXC_SOFTWARE
;
976 /* Many internal GDB routines expect breakpoints to be reported
977 as GDB_SIGNAL_TRAP, and will report GDB_EXC_BREAKPOINT
978 as a spurious signal. */
979 status
->value
.sig
= GDB_SIGNAL_TRAP
;
982 status
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
986 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
988 else if (hdr
->msgh_id
== 0x48)
990 /* MACH_NOTIFY_DEAD_NAME: notification for exit. */
994 inf
= darwin_find_inferior_by_notify (hdr
->msgh_local_port
);
997 if (!inf
->private->no_ptrace
)
1002 res
= wait4 (inf
->pid
, &wstatus
, 0, NULL
);
1003 if (res
< 0 || res
!= inf
->pid
)
1005 printf_unfiltered (_("wait4: res=%d: %s\n"),
1006 res
, safe_strerror (errno
));
1007 status
->kind
= TARGET_WAITKIND_IGNORE
;
1008 return minus_one_ptid
;
1010 if (WIFEXITED (wstatus
))
1012 status
->kind
= TARGET_WAITKIND_EXITED
;
1013 status
->value
.integer
= WEXITSTATUS (wstatus
);
1017 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1018 status
->value
.sig
= WTERMSIG (wstatus
);
1021 inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"),
1024 /* Looks necessary on Leopard and harmless... */
1025 wait4 (inf
->pid
, &wstatus
, 0, NULL
);
1027 return ptid_build (inf
->pid
, 0, 0);
1031 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf
->pid
);
1032 status
->kind
= TARGET_WAITKIND_EXITED
;
1033 status
->value
.integer
= 0; /* Don't know. */
1034 return ptid_build (inf
->pid
, 0, 0);
1039 /* Unknown message. */
1040 warning (_("darwin: got unknown message, id: 0x%x"), hdr
->msgh_id
);
1041 status
->kind
= TARGET_WAITKIND_IGNORE
;
1042 return minus_one_ptid
;
1046 cancel_breakpoint (ptid_t ptid
)
1048 /* Arrange for a breakpoint to be hit again later. We will handle
1049 the current event, eventually we will resume this thread, and this
1050 breakpoint will trap again.
1052 If we do not do this, then we run the risk that the user will
1053 delete or disable the breakpoint, but the thread will have already
1056 struct regcache
*regcache
= get_thread_regcache (ptid
);
1057 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1060 pc
= regcache_read_pc (regcache
) - target_decr_pc_after_break (gdbarch
);
1061 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
1063 inferior_debug (4, "cancel_breakpoint for thread 0x%x\n",
1064 ptid_get_tid (ptid
));
1066 /* Back up the PC if necessary. */
1067 if (target_decr_pc_after_break (gdbarch
))
1068 regcache_write_pc (regcache
, pc
);
1076 darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
)
1081 mach_msg_header_t hdr
;
1084 mach_msg_header_t
*hdr
= &msgin
.hdr
;
1086 darwin_thread_t
*thread
;
1087 struct inferior
*inf
;
1090 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1091 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
1093 /* Handle fake stop events at first. */
1094 if (darwin_inf_fake_stop
!= NULL
)
1096 inf
= darwin_inf_fake_stop
;
1097 darwin_inf_fake_stop
= NULL
;
1099 status
->kind
= TARGET_WAITKIND_STOPPED
;
1100 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1101 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1102 thread
->msg_state
= DARWIN_STOPPED
;
1103 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1108 /* set_sigint_trap (); */
1110 /* Wait for a message. */
1111 kret
= mach_msg (&msgin
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
, 0,
1112 sizeof (msgin
.data
), darwin_port_set
, 0, MACH_PORT_NULL
);
1114 /* clear_sigint_trap (); */
1116 if (kret
== MACH_RCV_INTERRUPTED
)
1118 status
->kind
= TARGET_WAITKIND_IGNORE
;
1119 return minus_one_ptid
;
1122 if (kret
!= MACH_MSG_SUCCESS
)
1124 inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret
);
1125 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
1126 return minus_one_ptid
;
1129 /* Debug: display message. */
1130 if (darwin_debug_flag
> 10)
1131 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1133 res
= darwin_decode_message (hdr
, &thread
, &inf
, status
);
1134 if (ptid_equal (res
, minus_one_ptid
))
1137 /* Early return in case an inferior has exited. */
1141 while (status
->kind
== TARGET_WAITKIND_IGNORE
);
1143 /* Stop all tasks. */
1144 iterate_over_inferiors (darwin_suspend_inferior_it
, NULL
);
1146 /* Read pending messages. */
1149 struct target_waitstatus status2
;
1152 kret
= mach_msg (&msgin
.hdr
,
1153 MACH_RCV_MSG
| MACH_RCV_TIMEOUT
, 0,
1154 sizeof (msgin
.data
), darwin_port_set
, 1, MACH_PORT_NULL
);
1156 if (kret
== MACH_RCV_TIMED_OUT
)
1158 if (kret
!= MACH_MSG_SUCCESS
)
1161 (5, _("darwin_wait: mach_msg(pending) ret=0x%x\n"), kret
);
1165 /* Debug: display message. */
1166 if (darwin_debug_flag
> 10)
1167 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1169 ptid2
= darwin_decode_message (hdr
, &thread
, &inf
, &status2
);
1171 if (inf
!= NULL
&& thread
!= NULL
1172 && thread
->event
.ex_type
== EXC_BREAKPOINT
)
1174 if (thread
->single_step
1175 || cancel_breakpoint (ptid_build (inf
->pid
, 0, thread
->gdb_port
)))
1177 gdb_assert (thread
->msg_state
== DARWIN_MESSAGE
);
1178 darwin_send_reply (inf
, thread
);
1179 thread
->msg_state
= DARWIN_RUNNING
;
1183 (3, _("darwin_wait: thread 0x%x hit a non-gdb breakpoint\n"),
1187 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1193 darwin_wait_to (struct target_ops
*ops
,
1194 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1196 return darwin_wait (ptid
, status
);
1200 darwin_stop (struct target_ops
*self
, ptid_t t
)
1202 struct inferior
*inf
= current_inferior ();
1204 /* FIXME: handle in no_ptrace mode. */
1205 gdb_assert (!inf
->private->no_ptrace
);
1206 kill (inf
->pid
, SIGINT
);
1210 darwin_mourn_inferior (struct target_ops
*ops
)
1212 struct inferior
*inf
= current_inferior ();
1217 unpush_target (darwin_ops
);
1219 /* Deallocate threads. */
1220 if (inf
->private->threads
)
1225 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1228 kret
= mach_port_deallocate (gdb_task
, t
->gdb_port
);
1229 MACH_CHECK_ERROR (kret
);
1231 VEC_free (darwin_thread_t
, inf
->private->threads
);
1232 inf
->private->threads
= NULL
;
1235 kret
= mach_port_move_member (gdb_task
,
1236 inf
->private->notify_port
, MACH_PORT_NULL
);
1237 MACH_CHECK_ERROR (kret
);
1239 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1240 MACH_NOTIFY_DEAD_NAME
, 0,
1242 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1244 /* This can fail if the task is dead. */
1245 inferior_debug (4, "task=0x%x, prev=0x%x, notify_port=0x%x\n",
1246 inf
->private->task
, prev
, inf
->private->notify_port
);
1248 if (kret
== KERN_SUCCESS
)
1250 kret
= mach_port_deallocate (gdb_task
, prev
);
1251 MACH_CHECK_ERROR (kret
);
1254 kret
= mach_port_destroy (gdb_task
, inf
->private->notify_port
);
1255 MACH_CHECK_ERROR (kret
);
1258 /* Deallocate saved exception ports. */
1259 for (i
= 0; i
< inf
->private->exception_info
.count
; i
++)
1261 kret
= mach_port_deallocate
1262 (gdb_task
, inf
->private->exception_info
.ports
[i
]);
1263 MACH_CHECK_ERROR (kret
);
1265 inf
->private->exception_info
.count
= 0;
1267 kret
= mach_port_deallocate (gdb_task
, inf
->private->task
);
1268 MACH_CHECK_ERROR (kret
);
1270 xfree (inf
->private);
1271 inf
->private = NULL
;
1273 generic_mourn_inferior ();
1277 darwin_reply_to_all_pending_messages (struct inferior
*inf
)
1283 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1286 if (t
->msg_state
== DARWIN_MESSAGE
)
1287 darwin_resume_thread (inf
, t
, 0, 0);
1292 darwin_stop_inferior (struct inferior
*inf
)
1294 struct target_waitstatus wstatus
;
1300 gdb_assert (inf
!= NULL
);
1302 darwin_suspend_inferior (inf
);
1304 darwin_reply_to_all_pending_messages (inf
);
1306 if (inf
->private->no_ptrace
)
1309 res
= kill (inf
->pid
, SIGSTOP
);
1311 warning (_("cannot kill: %s"), safe_strerror (errno
));
1313 /* Wait until the process is really stopped. */
1316 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1317 if (wstatus
.kind
== TARGET_WAITKIND_STOPPED
1318 && wstatus
.value
.sig
== GDB_SIGNAL_STOP
)
1323 static kern_return_t
1324 darwin_save_exception_ports (darwin_inferior
*inf
)
1328 inf
->exception_info
.count
=
1329 sizeof (inf
->exception_info
.ports
) / sizeof (inf
->exception_info
.ports
[0]);
1331 kret
= task_get_exception_ports
1332 (inf
->task
, EXC_MASK_ALL
, inf
->exception_info
.masks
,
1333 &inf
->exception_info
.count
, inf
->exception_info
.ports
,
1334 inf
->exception_info
.behaviors
, inf
->exception_info
.flavors
);
1338 static kern_return_t
1339 darwin_restore_exception_ports (darwin_inferior
*inf
)
1344 for (i
= 0; i
< inf
->exception_info
.count
; i
++)
1346 kret
= task_set_exception_ports
1347 (inf
->task
, inf
->exception_info
.masks
[i
], inf
->exception_info
.ports
[i
],
1348 inf
->exception_info
.behaviors
[i
], inf
->exception_info
.flavors
[i
]);
1349 if (kret
!= KERN_SUCCESS
)
1353 return KERN_SUCCESS
;
1357 darwin_kill_inferior (struct target_ops
*ops
)
1359 struct inferior
*inf
= current_inferior ();
1360 struct target_waitstatus wstatus
;
1366 if (ptid_equal (inferior_ptid
, null_ptid
))
1369 gdb_assert (inf
!= NULL
);
1371 kret
= darwin_restore_exception_ports (inf
->private);
1372 MACH_CHECK_ERROR (kret
);
1374 darwin_reply_to_all_pending_messages (inf
);
1376 res
= kill (inf
->pid
, 9);
1380 darwin_resume_inferior (inf
);
1382 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1384 else if (errno
!= ESRCH
)
1385 warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"),
1386 inf
->pid
, safe_strerror (errno
));
1388 target_mourn_inferior ();
1392 darwin_attach_pid (struct inferior
*inf
)
1395 mach_port_t prev_port
;
1397 mach_port_t prev_not
;
1398 exception_mask_t mask
;
1400 inf
->private = XCNEW (darwin_inferior
);
1402 kret
= task_for_pid (gdb_task
, inf
->pid
, &inf
->private->task
);
1403 if (kret
!= KERN_SUCCESS
)
1407 if (!inf
->attach_flag
)
1410 waitpid (inf
->pid
, &status
, 0);
1413 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1414 " (please check gdb is codesigned - see taskgated(8))"),
1415 inf
->pid
, mach_error_string (kret
), (unsigned long) kret
);
1418 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1419 inf
->private->task
, inf
->pid
);
1421 if (darwin_ex_port
== MACH_PORT_NULL
)
1423 /* Create a port to get exceptions. */
1424 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1426 if (kret
!= KERN_SUCCESS
)
1427 error (_("Unable to create exception port, mach_port_allocate "
1431 kret
= mach_port_insert_right (gdb_task
, darwin_ex_port
, darwin_ex_port
,
1432 MACH_MSG_TYPE_MAKE_SEND
);
1433 if (kret
!= KERN_SUCCESS
)
1434 error (_("Unable to create exception port, mach_port_insert_right "
1438 /* Create a port set and put ex_port in it. */
1439 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_PORT_SET
,
1441 if (kret
!= KERN_SUCCESS
)
1442 error (_("Unable to create port set, mach_port_allocate "
1446 kret
= mach_port_move_member (gdb_task
, darwin_ex_port
, darwin_port_set
);
1447 if (kret
!= KERN_SUCCESS
)
1448 error (_("Unable to move exception port into new port set, "
1449 "mach_port_move_member\n"
1454 /* Create a port to be notified when the child task terminates. */
1455 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1456 &inf
->private->notify_port
);
1457 if (kret
!= KERN_SUCCESS
)
1458 error (_("Unable to create notification port, mach_port_allocate "
1462 kret
= mach_port_move_member (gdb_task
,
1463 inf
->private->notify_port
, darwin_port_set
);
1464 if (kret
!= KERN_SUCCESS
)
1465 error (_("Unable to move notification port into new port set, "
1466 "mach_port_move_member\n"
1470 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1471 MACH_NOTIFY_DEAD_NAME
, 0,
1472 inf
->private->notify_port
,
1473 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1475 if (kret
!= KERN_SUCCESS
)
1476 error (_("Termination notification request failed, "
1477 "mach_port_request_notification\n"
1480 if (prev_not
!= MACH_PORT_NULL
)
1482 /* This is unexpected, as there should not be any previously
1483 registered notification request. But this is not a fatal
1484 issue, so just emit a warning. */
1486 A task termination request was registered before the debugger registered\n\
1487 its own. This is unexpected, but should otherwise not have any actual\n\
1488 impact on the debugging session."));
1491 kret
= darwin_save_exception_ports (inf
->private);
1492 if (kret
!= KERN_SUCCESS
)
1493 error (_("Unable to save exception ports, task_get_exception_ports"
1497 /* Set exception port. */
1498 if (enable_mach_exceptions
)
1499 mask
= EXC_MASK_ALL
;
1501 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1502 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1503 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1504 if (kret
!= KERN_SUCCESS
)
1505 error (_("Unable to set exception ports, task_set_exception_ports"
1509 push_target (darwin_ops
);
1513 darwin_init_thread_list (struct inferior
*inf
)
1515 darwin_thread_t
*thread
;
1518 darwin_check_new_threads (inf
);
1520 gdb_assert (inf
->private->threads
1521 && VEC_length (darwin_thread_t
, inf
->private->threads
) > 0);
1522 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1524 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1526 new_ptid
= ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1527 thread_change_ptid (inferior_ptid
, new_ptid
);
1528 inferior_ptid
= new_ptid
;
1531 /* The child must synchronize with gdb: gdb must set the exception port
1532 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1533 FIXME: is there a lighter way ? */
1534 static int ptrace_fds
[2];
1537 darwin_ptrace_me (void)
1542 /* Close write end point. */
1543 close (ptrace_fds
[1]);
1545 /* Wait until gdb is ready. */
1546 res
= read (ptrace_fds
[0], &c
, 1);
1548 error (_("unable to read from pipe, read returned: %d"), res
);
1549 close (ptrace_fds
[0]);
1551 /* Get rid of privileges. */
1552 setegid (getgid ());
1555 PTRACE (PT_TRACE_ME
, 0, 0, 0);
1557 /* Redirect signals to exception port. */
1558 PTRACE (PT_SIGEXC
, 0, 0, 0);
1561 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1563 darwin_pre_ptrace (void)
1565 if (pipe (ptrace_fds
) != 0)
1569 error (_("unable to create a pipe: %s"), safe_strerror (errno
));
1572 mark_fd_no_cloexec (ptrace_fds
[0]);
1573 mark_fd_no_cloexec (ptrace_fds
[1]);
1577 darwin_ptrace_him (int pid
)
1581 mach_port_t prev_port
;
1583 struct inferior
*inf
= current_inferior ();
1585 darwin_attach_pid (inf
);
1587 /* Let's the child run. */
1588 close (ptrace_fds
[0]);
1589 close (ptrace_fds
[1]);
1591 unmark_fd_no_cloexec (ptrace_fds
[0]);
1592 unmark_fd_no_cloexec (ptrace_fds
[1]);
1594 darwin_init_thread_list (inf
);
1596 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
1600 darwin_execvp (const char *file
, char * const argv
[], char * const env
[])
1602 posix_spawnattr_t attr
;
1606 res
= posix_spawnattr_init (&attr
);
1610 (gdb_stderr
, "Cannot initialize attribute for posix_spawn\n");
1614 /* Do like execve: replace the image. */
1615 ps_flags
= POSIX_SPAWN_SETEXEC
;
1617 /* Disable ASLR. The constant doesn't look to be available outside the
1618 kernel include files. */
1619 #ifndef _POSIX_SPAWN_DISABLE_ASLR
1620 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1622 ps_flags
|= _POSIX_SPAWN_DISABLE_ASLR
;
1623 res
= posix_spawnattr_setflags (&attr
, ps_flags
);
1626 fprintf_unfiltered (gdb_stderr
, "Cannot set posix_spawn flags\n");
1630 posix_spawnp (NULL
, argv
[0], NULL
, &attr
, argv
, env
);
1634 darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
1635 char *allargs
, char **env
, int from_tty
)
1637 /* Do the hard work. */
1638 fork_inferior (exec_file
, allargs
, env
, darwin_ptrace_me
, darwin_ptrace_him
,
1639 darwin_pre_ptrace
, NULL
, darwin_execvp
);
1641 /* Return now in case of error. */
1642 if (ptid_equal (inferior_ptid
, null_ptid
))
1647 /* Set things up such that the next call to darwin_wait will immediately
1648 return a fake stop event for inferior INF.
1650 This assumes that the inferior's thread list has been initialized,
1651 as it will suspend the inferior's first thread. */
1654 darwin_setup_fake_stop_event (struct inferior
*inf
)
1656 darwin_thread_t
*thread
;
1659 gdb_assert (darwin_inf_fake_stop
== NULL
);
1660 darwin_inf_fake_stop
= inf
;
1662 /* When detecting a fake pending stop event, darwin_wait returns
1663 an event saying that the first thread is in a DARWIN_STOPPED
1664 state. To make that accurate, we need to suspend that thread
1665 as well. Otherwise, we'll try resuming it when resuming the
1666 inferior, and get a warning because the thread's suspend count
1667 is already zero, making the resume request useless. */
1668 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1669 kret
= thread_suspend (thread
->gdb_port
);
1670 MACH_CHECK_ERROR (kret
);
1673 /* Attach to process PID, then initialize for debugging it
1674 and wait for the trace-trap that results from attaching. */
1676 darwin_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1682 struct inferior
*inf
;
1685 pid
= parse_pid_to_attach (args
);
1687 if (pid
== getpid ()) /* Trying to masturbate? */
1688 error (_("I refuse to debug myself!"));
1692 char *exec_file
= get_exec_file (0);
1695 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
1696 target_pid_to_str (pid_to_ptid (pid
)));
1698 printf_unfiltered (_("Attaching to %s\n"),
1699 target_pid_to_str (pid_to_ptid (pid
)));
1701 gdb_flush (gdb_stdout
);
1704 if (pid
== 0 || kill (pid
, 0) < 0)
1705 error (_("Can't attach to process %d: %s (%d)"),
1706 pid
, safe_strerror (errno
), errno
);
1708 inferior_ptid
= pid_to_ptid (pid
);
1709 inf
= current_inferior ();
1710 inferior_appeared (inf
, pid
);
1711 inf
->attach_flag
= 1;
1713 /* Always add a main thread. */
1714 add_thread_silent (inferior_ptid
);
1716 darwin_attach_pid (inf
);
1718 darwin_suspend_inferior (inf
);
1720 darwin_init_thread_list (inf
);
1722 darwin_check_osabi (inf
->private, ptid_get_tid (inferior_ptid
));
1724 darwin_setup_fake_stop_event (inf
);
1726 inf
->private->no_ptrace
= 1;
1729 /* Take a program previously attached to and detaches it.
1730 The program resumes execution and will no longer stop
1731 on signals, etc. We'd better not have left any breakpoints
1732 in the program or it'll die when it hits one. For this
1733 to work, it may be necessary for the process to have been
1734 previously attached. It *might* work if the program was
1735 started via fork. */
1737 darwin_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1739 pid_t pid
= ptid_get_pid (inferior_ptid
);
1740 struct inferior
*inf
= current_inferior ();
1744 /* Display message. */
1747 char *exec_file
= get_exec_file (0);
1750 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
1751 target_pid_to_str (pid_to_ptid (pid
)));
1752 gdb_flush (gdb_stdout
);
1755 /* If ptrace() is in use, stop the process. */
1756 if (!inf
->private->no_ptrace
)
1757 darwin_stop_inferior (inf
);
1759 kret
= darwin_restore_exception_ports (inf
->private);
1760 MACH_CHECK_ERROR (kret
);
1762 if (!inf
->private->no_ptrace
)
1764 res
= PTRACE (PT_DETACH
, inf
->pid
, 0, 0);
1766 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1767 inf
->pid
, safe_strerror (errno
), errno
);
1770 darwin_reply_to_all_pending_messages (inf
);
1772 /* When using ptrace, we have just performed a PT_DETACH, which
1773 resumes the inferior. On the other hand, when we are not using
1774 ptrace, we need to resume its execution ourselves. */
1775 if (inf
->private->no_ptrace
)
1776 darwin_resume_inferior (inf
);
1778 darwin_mourn_inferior (ops
);
1782 darwin_files_info (struct target_ops
*ops
)
1787 darwin_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1789 static char buf
[80];
1790 long tid
= ptid_get_tid (ptid
);
1794 snprintf (buf
, sizeof (buf
), _("Thread 0x%lx of process %u"),
1795 tid
, ptid_get_pid (ptid
));
1799 return normal_pid_to_str (ptid
);
1803 darwin_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1808 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1809 copy it to RDADDR in gdb's address space.
1810 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1811 to ADDR in inferior task's address space.
1812 Return 0 on failure; number of bytes read / writen otherwise. */
1814 darwin_read_write_inferior (task_t task
, CORE_ADDR addr
,
1815 gdb_byte
*rdaddr
, const gdb_byte
*wraddr
,
1819 mach_vm_address_t offset
= addr
& (mach_page_size
- 1);
1820 mach_vm_address_t low_address
= (mach_vm_address_t
) (addr
- offset
);
1821 mach_vm_size_t aligned_length
= (mach_vm_size_t
) PAGE_ROUND (offset
+ length
);
1823 mach_msg_type_number_t copy_count
;
1824 mach_vm_size_t remaining_length
;
1825 mach_vm_address_t region_address
;
1826 mach_vm_size_t region_length
;
1828 inferior_debug (8, _("darwin_read_write_inferior(task=0x%x, %s, len=%s)\n"),
1829 task
, core_addr_to_string (addr
), pulongest (length
));
1831 /* Get memory from inferior with page aligned addresses. */
1832 kret
= mach_vm_read (task
, low_address
, aligned_length
,
1833 &copied
, ©_count
);
1834 if (kret
!= KERN_SUCCESS
)
1837 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1838 core_addr_to_string (addr
), mach_error_string (kret
));
1843 memcpy (rdaddr
, (char *)copied
+ offset
, length
);
1848 memcpy ((char *)copied
+ offset
, wraddr
, length
);
1850 /* Do writes atomically.
1851 First check for holes and unwritable memory. */
1852 for (region_address
= low_address
, remaining_length
= aligned_length
;
1853 region_address
< low_address
+ aligned_length
;
1854 region_address
+= region_length
, remaining_length
-= region_length
)
1856 vm_region_submap_short_info_data_64_t info
;
1857 mach_vm_address_t region_start
= region_address
;
1858 mach_msg_type_number_t count
;
1859 natural_t region_depth
;
1861 region_depth
= 100000;
1862 count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
1863 kret
= mach_vm_region_recurse
1864 (task
, ®ion_start
, ®ion_length
, ®ion_depth
,
1865 (vm_region_recurse_info_t
) &info
, &count
);
1867 if (kret
!= KERN_SUCCESS
)
1869 inferior_debug (1, _("darwin_read_write_inferior: "
1870 "mach_vm_region_recurse failed at %s: %s\n"),
1871 core_addr_to_string (region_address
),
1872 mach_error_string (kret
));
1877 (9, _("darwin_read_write_inferior: "
1878 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1879 core_addr_to_string (region_address
),
1880 core_addr_to_string (region_start
),
1881 core_addr_to_string (region_length
));
1883 /* Check for holes in memory. */
1884 if (region_start
> region_address
)
1886 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1887 core_addr_to_string (region_address
),
1888 core_addr_to_string (region_start
),
1889 (unsigned)region_length
);
1894 /* Adjust the length. */
1895 region_length
-= (region_address
- region_start
);
1897 if (!(info
.max_protection
& VM_PROT_WRITE
))
1899 kret
= mach_vm_protect
1900 (task
, region_address
, region_length
,
1901 TRUE
, info
.max_protection
| VM_PROT_WRITE
| VM_PROT_COPY
);
1902 if (kret
!= KERN_SUCCESS
)
1904 warning (_("darwin_read_write_inf: "
1905 "mach_vm_protect max failed at %s: %s"),
1906 core_addr_to_string (region_address
),
1907 mach_error_string (kret
));
1913 if (!(info
.protection
& VM_PROT_WRITE
))
1915 kret
= mach_vm_protect (task
, region_address
, region_length
,
1916 FALSE
, info
.protection
| VM_PROT_WRITE
);
1917 if (kret
!= KERN_SUCCESS
)
1919 warning (_("darwin_read_write_inf: "
1920 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1921 core_addr_to_string (region_address
),
1922 (unsigned long)region_length
, mach_error_string (kret
));
1929 kret
= mach_vm_write (task
, low_address
, copied
, aligned_length
);
1931 if (kret
!= KERN_SUCCESS
)
1933 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1934 mach_error_string (kret
));
1938 mach_vm_deallocate (mach_task_self (), copied
, copy_count
);
1942 /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
1944 Return 0 on failure; number of bytes read / written otherwise. */
1946 #ifdef TASK_DYLD_INFO_COUNT
1947 /* This is not available in Darwin 9. */
1948 static enum target_xfer_status
1949 darwin_read_dyld_info (task_t task
, CORE_ADDR addr
, gdb_byte
*rdaddr
,
1950 ULONGEST length
, ULONGEST
*xfered_len
)
1952 struct task_dyld_info task_dyld_info
;
1953 mach_msg_type_number_t count
= TASK_DYLD_INFO_COUNT
;
1954 int sz
= TASK_DYLD_INFO_COUNT
* sizeof (natural_t
);
1958 return TARGET_XFER_EOF
;
1960 kret
= task_info (task
, TASK_DYLD_INFO
, (task_info_t
) &task_dyld_info
, &count
);
1961 MACH_CHECK_ERROR (kret
);
1962 if (kret
!= KERN_SUCCESS
)
1963 return TARGET_XFER_E_IO
;
1965 if (addr
+ length
> sz
)
1967 memcpy (rdaddr
, (char *)&task_dyld_info
+ addr
, length
);
1968 *xfered_len
= (ULONGEST
) length
;
1969 return TARGET_XFER_OK
;
1975 static enum target_xfer_status
1976 darwin_xfer_partial (struct target_ops
*ops
,
1977 enum target_object object
, const char *annex
,
1978 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1979 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1981 struct inferior
*inf
= current_inferior ();
1984 (8, _("darwin_xfer_partial(%s, %s, rbuf=%s, wbuf=%s) pid=%u\n"),
1985 core_addr_to_string (offset
), pulongest (len
),
1986 host_address_to_string (readbuf
), host_address_to_string (writebuf
),
1991 case TARGET_OBJECT_MEMORY
:
1993 int l
= darwin_read_write_inferior (inf
->private->task
, offset
,
1994 readbuf
, writebuf
, len
);
1997 return TARGET_XFER_EOF
;
2001 *xfered_len
= (ULONGEST
) l
;
2002 return TARGET_XFER_OK
;
2005 #ifdef TASK_DYLD_INFO_COUNT
2006 case TARGET_OBJECT_DARWIN_DYLD_INFO
:
2007 if (writebuf
!= NULL
|| readbuf
== NULL
)
2009 /* Support only read. */
2010 return TARGET_XFER_E_IO
;
2012 return darwin_read_dyld_info (inf
->private->task
, offset
, readbuf
, len
,
2016 return TARGET_XFER_E_IO
;
2022 set_enable_mach_exceptions (char *args
, int from_tty
,
2023 struct cmd_list_element
*c
)
2025 if (!ptid_equal (inferior_ptid
, null_ptid
))
2027 struct inferior
*inf
= current_inferior ();
2028 exception_mask_t mask
;
2031 if (enable_mach_exceptions
)
2032 mask
= EXC_MASK_ALL
;
2035 darwin_restore_exception_ports (inf
->private);
2036 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
2038 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
2039 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
2040 MACH_CHECK_ERROR (kret
);
2045 darwin_pid_to_exec_file (struct target_ops
*self
, int pid
)
2047 static char path
[PATH_MAX
];
2050 res
= proc_pidinfo (pid
, PROC_PIDPATHINFO
, 0, path
, PATH_MAX
);
2058 darwin_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
2063 struct inferior
*inf
= current_inferior ();
2065 mach_port_name_array_t names
;
2066 mach_msg_type_number_t names_count
;
2067 mach_port_type_array_t types
;
2068 mach_msg_type_number_t types_count
;
2071 /* First linear search. */
2073 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2075 if (t
->inf_port
== lwp
)
2076 return ptid_build (ptid_get_pid (inferior_ptid
), 0, t
->gdb_port
);
2078 /* Maybe the port was never extract. Do it now. */
2080 /* First get inferior port names. */
2081 kret
= mach_port_names (inf
->private->task
, &names
, &names_count
, &types
,
2083 MACH_CHECK_ERROR (kret
);
2084 if (kret
!= KERN_SUCCESS
)
2087 /* For each name, copy the right in the gdb space and then compare with
2088 our view of the inferior threads. We don't forget to deallocate the
2090 for (i
= 0; i
< names_count
; i
++)
2092 mach_port_t local_name
;
2093 mach_msg_type_name_t local_type
;
2095 /* We just need to know the corresponding name in gdb name space.
2096 So extract and deallocate the right. */
2097 kret
= mach_port_extract_right (inf
->private->task
, names
[i
],
2098 MACH_MSG_TYPE_COPY_SEND
,
2099 &local_name
, &local_type
);
2100 if (kret
!= KERN_SUCCESS
)
2102 mach_port_deallocate (gdb_task
, local_name
);
2105 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2107 if (t
->gdb_port
== local_name
)
2109 t
->inf_port
= names
[i
];
2110 if (names
[i
] == lwp
)
2115 vm_deallocate (gdb_task
, (vm_address_t
) names
,
2116 names_count
* sizeof (mach_port_t
));
2119 return ptid_build (ptid_get_pid (inferior_ptid
), 0, res
);
2125 darwin_supports_multi_process (struct target_ops
*self
)
2130 /* -Wmissing-prototypes */
2131 extern initialize_file_ftype _initialize_darwin_inferior
;
2134 _initialize_darwin_inferior (void)
2138 gdb_task
= mach_task_self ();
2139 darwin_host_self
= mach_host_self ();
2141 /* Read page size. */
2142 kret
= host_page_size (darwin_host_self
, &mach_page_size
);
2143 if (kret
!= KERN_SUCCESS
)
2145 mach_page_size
= 0x1000;
2146 MACH_CHECK_ERROR (kret
);
2149 darwin_ops
= inf_child_target ();
2151 darwin_ops
->to_shortname
= "darwin-child";
2152 darwin_ops
->to_longname
= _("Darwin child process");
2153 darwin_ops
->to_doc
=
2154 _("Darwin child process (started by the \"run\" command).");
2155 darwin_ops
->to_create_inferior
= darwin_create_inferior
;
2156 darwin_ops
->to_attach
= darwin_attach
;
2157 darwin_ops
->to_attach_no_wait
= 0;
2158 darwin_ops
->to_detach
= darwin_detach
;
2159 darwin_ops
->to_files_info
= darwin_files_info
;
2160 darwin_ops
->to_wait
= darwin_wait_to
;
2161 darwin_ops
->to_mourn_inferior
= darwin_mourn_inferior
;
2162 darwin_ops
->to_kill
= darwin_kill_inferior
;
2163 darwin_ops
->to_stop
= darwin_stop
;
2164 darwin_ops
->to_resume
= darwin_resume_to
;
2165 darwin_ops
->to_thread_alive
= darwin_thread_alive
;
2166 darwin_ops
->to_pid_to_str
= darwin_pid_to_str
;
2167 darwin_ops
->to_pid_to_exec_file
= darwin_pid_to_exec_file
;
2168 darwin_ops
->to_load
= NULL
;
2169 darwin_ops
->to_xfer_partial
= darwin_xfer_partial
;
2170 darwin_ops
->to_supports_multi_process
= darwin_supports_multi_process
;
2171 darwin_ops
->to_get_ada_task_ptid
= darwin_get_ada_task_ptid
;
2173 darwin_complete_target (darwin_ops
);
2175 add_target (darwin_ops
);
2177 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
2180 add_setshow_zuinteger_cmd ("darwin", class_obscure
,
2181 &darwin_debug_flag
, _("\
2182 Set if printing inferior communication debugging statements."), _("\
2183 Show if printing inferior communication debugging statements."), NULL
,
2185 &setdebuglist
, &showdebuglist
);
2187 add_setshow_boolean_cmd ("mach-exceptions", class_support
,
2188 &enable_mach_exceptions
, _("\
2189 Set if mach exceptions are caught."), _("\
2190 Show if mach exceptions are caught."), _("\
2191 When this mode is on, all low level exceptions are reported before being\n\
2192 reported by the kernel."),
2193 &set_enable_mach_exceptions
, NULL
,
2194 &setlist
, &showlist
);