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 /* Target operations for Darwin. */
117 static struct target_ops
*darwin_ops
;
119 /* Task identifier of gdb. */
120 static task_t gdb_task
;
122 /* A copy of mach_host_self (). */
123 mach_port_t darwin_host_self
;
125 /* Exception port. */
126 mach_port_t darwin_ex_port
;
128 /* Port set, to wait for answer on all ports. */
129 mach_port_t darwin_port_set
;
132 static vm_size_t mach_page_size
;
134 /* If Set, catch all mach exceptions (before they are converted to signals
136 static int enable_mach_exceptions
;
138 /* Inferior that should report a fake stop event. */
139 static struct inferior
*darwin_inf_fake_stop
;
141 #define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1))
142 #define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1)
144 /* This controls output of inferior debugging. */
145 static unsigned int darwin_debug_flag
= 0;
147 /* Create a __TEXT __info_plist section in the executable so that gdb could
148 be signed. This is required to get an authorization for task_for_pid.
150 Once gdb is built, you must codesign it with any system-trusted signing
151 authority. See taskgated(8) for details. */
152 static const unsigned char info_plist
[]
153 __attribute__ ((section ("__TEXT,__info_plist"),used
)) =
154 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
155 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
156 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
157 "<plist version=\"1.0\">\n"
159 " <key>CFBundleIdentifier</key>\n"
160 " <string>org.gnu.gdb</string>\n"
161 " <key>CFBundleName</key>\n"
162 " <string>gdb</string>\n"
163 " <key>CFBundleVersion</key>\n"
164 " <string>1.0</string>\n"
165 " <key>SecTaskAccess</key>\n"
167 " <string>allowed</string>\n"
168 " <string>debug</string>\n"
174 inferior_debug (int level
, const char *fmt
, ...)
178 if (darwin_debug_flag
< level
)
182 printf_unfiltered (_("[%d inferior]: "), getpid ());
183 vprintf_unfiltered (fmt
, ap
);
188 mach_check_error (kern_return_t ret
, const char *file
,
189 unsigned int line
, const char *func
)
191 if (ret
== KERN_SUCCESS
)
194 func
= _("[UNKNOWN]");
196 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"),
197 file
, line
, func
, mach_error_string (ret
), (unsigned long) ret
);
201 unparse_exception_type (unsigned int i
)
203 static char unknown_exception_buf
[32];
208 return "EXC_BAD_ACCESS";
209 case EXC_BAD_INSTRUCTION
:
210 return "EXC_BAD_INSTRUCTION";
212 return "EXC_ARITHMETIC";
214 return "EXC_EMULATION";
216 return "EXC_SOFTWARE";
218 return "EXC_BREAKPOINT";
220 return "EXC_SYSCALL";
221 case EXC_MACH_SYSCALL
:
222 return "EXC_MACH_SYSCALL";
224 return "EXC_RPC_ALERT";
228 snprintf (unknown_exception_buf
, 32, _("unknown (%d)"), i
);
229 return unknown_exception_buf
;
233 /* Set errno to zero, and then call ptrace with the given arguments.
234 If inferior debugging traces are on, then also print a debug
237 The returned value is the same as the value returned by ptrace,
238 except in the case where that value is -1 but errno is zero.
239 This case is documented to be a non-error situation, so we
240 return zero in that case. */
243 darwin_ptrace (const char *name
,
244 int request
, int pid
, PTRACE_TYPE_ARG3 arg3
, int arg4
)
249 ret
= ptrace (request
, pid
, (caddr_t
) arg3
, arg4
);
250 if (ret
== -1 && errno
== 0)
253 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
254 name
, pid
, arg3
, arg4
, ret
,
255 (ret
!= 0) ? safe_strerror (errno
) : _("no error"));
260 cmp_thread_t (const void *l
, const void *r
)
262 thread_t tl
= *(const thread_t
*)l
;
263 thread_t tr
= *(const thread_t
*)r
;
264 return (int)(tl
- tr
);
268 darwin_check_new_threads (struct inferior
*inf
)
272 thread_array_t thread_list
;
273 unsigned int new_nbr
;
274 unsigned int old_nbr
;
275 unsigned int new_ix
, old_ix
;
276 darwin_inferior
*darwin_inf
= inf
->private;
277 VEC (darwin_thread_t
) *thread_vec
;
279 /* Get list of threads. */
280 kret
= task_threads (darwin_inf
->task
, &thread_list
, &new_nbr
);
281 MACH_CHECK_ERROR (kret
);
282 if (kret
!= KERN_SUCCESS
)
287 qsort (thread_list
, new_nbr
, sizeof (thread_t
), cmp_thread_t
);
289 if (darwin_inf
->threads
)
290 old_nbr
= VEC_length (darwin_thread_t
, darwin_inf
->threads
);
294 /* Quick check for no changes. */
295 if (old_nbr
== new_nbr
)
297 for (i
= 0; i
< new_nbr
; i
++)
299 != VEC_index (darwin_thread_t
, darwin_inf
->threads
, i
)->gdb_port
)
303 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
304 new_nbr
* sizeof (int));
305 MACH_CHECK_ERROR (kret
);
310 thread_vec
= VEC_alloc (darwin_thread_t
, new_nbr
);
312 for (new_ix
= 0, old_ix
= 0; new_ix
< new_nbr
|| old_ix
< old_nbr
;)
314 thread_t new_id
= (new_ix
< new_nbr
) ?
315 thread_list
[new_ix
] : THREAD_NULL
;
316 darwin_thread_t
*old
= (old_ix
< old_nbr
) ?
317 VEC_index (darwin_thread_t
, darwin_inf
->threads
, old_ix
) : NULL
;
318 thread_t old_id
= old
? old
->gdb_port
: THREAD_NULL
;
321 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:0x%x old_id:0x%x\n"),
322 new_ix
, new_nbr
, old_ix
, old_nbr
, new_id
, old_id
);
324 if (old_id
== new_id
)
326 /* Thread still exist. */
327 VEC_safe_push (darwin_thread_t
, thread_vec
, old
);
331 kret
= mach_port_deallocate (gdb_task
, old_id
);
332 MACH_CHECK_ERROR (kret
);
335 if (new_ix
< new_nbr
&& new_id
== MACH_PORT_DEAD
)
337 /* Ignore dead ports.
338 In some weird cases, we might get dead ports. They should
339 correspond to dead thread so they could safely be ignored. */
343 if (new_ix
< new_nbr
&& (old_ix
== old_nbr
|| new_id
< old_id
))
345 /* A thread was created. */
346 struct thread_info
*tp
;
347 struct private_thread_info
*pti
;
349 pti
= XCNEW (struct private_thread_info
);
350 pti
->gdb_port
= new_id
;
351 pti
->msg_state
= DARWIN_RUNNING
;
353 /* Add a new thread unless this is the first one ever met. */
354 if (!(old_nbr
== 0 && new_ix
== 0))
355 tp
= add_thread_with_info (ptid_build (inf
->pid
, 0, new_id
), pti
);
358 tp
= find_thread_ptid (ptid_build (inf
->pid
, 0, 0));
362 VEC_safe_push (darwin_thread_t
, thread_vec
, pti
);
366 if (old_ix
< old_nbr
&& (new_ix
== new_nbr
|| new_id
> old_id
))
368 /* A thread was removed. */
369 delete_thread (ptid_build (inf
->pid
, 0, old_id
));
370 kret
= mach_port_deallocate (gdb_task
, old_id
);
371 MACH_CHECK_ERROR (kret
);
375 gdb_assert_not_reached ("unexpected thread case");
378 if (darwin_inf
->threads
)
379 VEC_free (darwin_thread_t
, darwin_inf
->threads
);
380 darwin_inf
->threads
= thread_vec
;
382 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
383 new_nbr
* sizeof (int));
384 MACH_CHECK_ERROR (kret
);
388 find_inferior_task_it (struct inferior
*inf
, void *port_ptr
)
390 return inf
->private->task
== *(task_t
*)port_ptr
;
394 find_inferior_notify_it (struct inferior
*inf
, void *port_ptr
)
396 return inf
->private->notify_port
== *(task_t
*)port_ptr
;
399 /* Return an inferior by task port. */
400 static struct inferior
*
401 darwin_find_inferior_by_task (task_t port
)
403 return iterate_over_inferiors (&find_inferior_task_it
, &port
);
406 /* Return an inferior by notification port. */
407 static struct inferior
*
408 darwin_find_inferior_by_notify (mach_port_t port
)
410 return iterate_over_inferiors (&find_inferior_notify_it
, &port
);
413 /* Return a thread by port. */
414 static darwin_thread_t
*
415 darwin_find_thread (struct inferior
*inf
, thread_t thread
)
421 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
423 if (t
->gdb_port
== thread
)
428 /* Suspend (ie stop) an inferior at Mach level. */
431 darwin_suspend_inferior (struct inferior
*inf
)
433 if (!inf
->private->suspended
)
437 kret
= task_suspend (inf
->private->task
);
438 MACH_CHECK_ERROR (kret
);
440 inf
->private->suspended
= 1;
444 /* Resume an inferior at Mach level. */
447 darwin_resume_inferior (struct inferior
*inf
)
449 if (inf
->private->suspended
)
453 kret
= task_resume (inf
->private->task
);
454 MACH_CHECK_ERROR (kret
);
456 inf
->private->suspended
= 0;
460 /* Iterator functions. */
463 darwin_suspend_inferior_it (struct inferior
*inf
, void *arg
)
465 darwin_suspend_inferior (inf
);
466 darwin_check_new_threads (inf
);
471 darwin_resume_inferior_it (struct inferior
*inf
, void *arg
)
473 darwin_resume_inferior (inf
);
478 darwin_dump_message (mach_msg_header_t
*hdr
, int disp_body
)
480 printf_unfiltered (_("message header:\n"));
481 printf_unfiltered (_(" bits: 0x%x\n"), hdr
->msgh_bits
);
482 printf_unfiltered (_(" size: 0x%x\n"), hdr
->msgh_size
);
483 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr
->msgh_remote_port
);
484 printf_unfiltered (_(" local-port: 0x%x\n"), hdr
->msgh_local_port
);
485 printf_unfiltered (_(" reserved: 0x%x\n"), hdr
->msgh_reserved
);
486 printf_unfiltered (_(" id: 0x%x\n"), hdr
->msgh_id
);
490 const unsigned char *data
;
491 const unsigned int *ldata
;
495 data
= (unsigned char *)(hdr
+ 1);
496 size
= hdr
->msgh_size
- sizeof (mach_msg_header_t
);
498 if (hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
500 mach_msg_body_t
*bod
= (mach_msg_body_t
*)data
;
501 mach_msg_port_descriptor_t
*desc
=
502 (mach_msg_port_descriptor_t
*)(bod
+ 1);
505 printf_unfiltered (_("body: descriptor_count=%u\n"),
506 bod
->msgh_descriptor_count
);
507 data
+= sizeof (mach_msg_body_t
);
508 size
-= sizeof (mach_msg_body_t
);
509 for (k
= 0; k
< bod
->msgh_descriptor_count
; k
++)
510 switch (desc
[k
].type
)
512 case MACH_MSG_PORT_DESCRIPTOR
:
514 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
515 k
, desc
[k
].type
, desc
[k
].name
, desc
[k
].disposition
);
518 printf_unfiltered (_(" descr %d: type=%u\n"),
522 data
+= bod
->msgh_descriptor_count
523 * sizeof (mach_msg_port_descriptor_t
);
524 size
-= bod
->msgh_descriptor_count
525 * sizeof (mach_msg_port_descriptor_t
);
526 ndr
= (NDR_record_t
*)(desc
+ bod
->msgh_descriptor_count
);
528 (_("NDR: mig=%02x if=%02x encod=%02x "
529 "int=%02x char=%02x float=%02x\n"),
530 ndr
->mig_vers
, ndr
->if_vers
, ndr
->mig_encoding
,
531 ndr
->int_rep
, ndr
->char_rep
, ndr
->float_rep
);
532 data
+= sizeof (NDR_record_t
);
533 size
-= sizeof (NDR_record_t
);
536 printf_unfiltered (_(" data:"));
537 ldata
= (const unsigned int *)data
;
538 for (i
= 0; i
< size
/ sizeof (unsigned int); i
++)
539 printf_unfiltered (" %08x", ldata
[i
]);
540 printf_unfiltered (_("\n"));
545 darwin_decode_exception_message (mach_msg_header_t
*hdr
,
546 struct inferior
**pinf
,
547 darwin_thread_t
**pthread
)
549 mach_msg_body_t
*bod
= (mach_msg_body_t
*)(hdr
+ 1);
550 mach_msg_port_descriptor_t
*desc
= (mach_msg_port_descriptor_t
*)(bod
+ 1);
553 struct inferior
*inf
;
554 darwin_thread_t
*thread
;
556 thread_t thread_port
;
560 /* Check message identifier. 2401 == 0x961 is exc. */
561 if (hdr
->msgh_id
!= 2401)
564 /* Check message header. */
565 if (!(hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
568 /* Check descriptors. */
569 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
570 + sizeof (*ndr
) + 2 * sizeof (integer_t
))
571 || bod
->msgh_descriptor_count
!= 2
572 || desc
[0].type
!= MACH_MSG_PORT_DESCRIPTOR
573 || desc
[0].disposition
!= MACH_MSG_TYPE_MOVE_SEND
574 || desc
[1].type
!= MACH_MSG_PORT_DESCRIPTOR
575 || desc
[1].disposition
!= MACH_MSG_TYPE_MOVE_SEND
)
578 /* Check data representation. */
579 ndr
= (NDR_record_t
*)(desc
+ 2);
580 if (ndr
->mig_vers
!= NDR_PROTOCOL_2_0
581 || ndr
->if_vers
!= NDR_PROTOCOL_2_0
582 || ndr
->mig_encoding
!= NDR_record
.mig_encoding
583 || ndr
->int_rep
!= NDR_record
.int_rep
584 || ndr
->char_rep
!= NDR_record
.char_rep
585 || ndr
->float_rep
!= NDR_record
.float_rep
)
588 /* Ok, the hard work. */
589 data
= (integer_t
*)(ndr
+ 1);
591 /* Find process by port. */
592 task_port
= desc
[1].name
;
593 thread_port
= desc
[0].name
;
594 inf
= darwin_find_inferior_by_task (task_port
);
599 /* Find thread by port. */
600 /* Check for new threads. Do it early so that the port in the exception
601 message can be deallocated. */
602 darwin_check_new_threads (inf
);
604 /* We got new rights to the task and the thread. Get rid of them. */
605 kret
= mach_port_deallocate (mach_task_self (), task_port
);
606 MACH_CHECK_ERROR (kret
);
607 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
608 MACH_CHECK_ERROR (kret
);
610 thread
= darwin_find_thread (inf
, thread_port
);
615 /* The thread should be running. However we have observed cases where a
616 thread got a SIGTTIN message after being stopped. */
617 gdb_assert (thread
->msg_state
!= DARWIN_MESSAGE
);
619 /* Finish decoding. */
620 thread
->event
.header
= *hdr
;
621 thread
->event
.thread_port
= thread_port
;
622 thread
->event
.task_port
= task_port
;
623 thread
->event
.ex_type
= data
[0];
624 thread
->event
.data_count
= data
[1];
626 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
627 + sizeof (*ndr
) + 2 * sizeof (integer_t
)
628 + data
[1] * sizeof (integer_t
)))
630 for (i
= 0; i
< data
[1]; i
++)
631 thread
->event
.ex_data
[i
] = data
[2 + i
];
633 thread
->msg_state
= DARWIN_MESSAGE
;
639 darwin_encode_reply (mig_reply_error_t
*reply
, mach_msg_header_t
*hdr
,
642 mach_msg_header_t
*rh
= &reply
->Head
;
644 rh
->msgh_bits
= MACH_MSGH_BITS (MACH_MSGH_BITS_REMOTE (hdr
->msgh_bits
), 0);
645 rh
->msgh_remote_port
= hdr
->msgh_remote_port
;
646 rh
->msgh_size
= (mach_msg_size_t
) sizeof (mig_reply_error_t
);
647 rh
->msgh_local_port
= MACH_PORT_NULL
;
648 rh
->msgh_id
= hdr
->msgh_id
+ 100;
650 reply
->NDR
= NDR_record
;
651 reply
->RetCode
= code
;
655 darwin_send_reply (struct inferior
*inf
, darwin_thread_t
*thread
)
658 mig_reply_error_t reply
;
660 darwin_encode_reply (&reply
, &thread
->event
.header
, KERN_SUCCESS
);
662 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
663 reply
.Head
.msgh_size
, 0,
664 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
666 MACH_CHECK_ERROR (kret
);
668 inf
->private->pending_messages
--;
672 darwin_resume_thread (struct inferior
*inf
, darwin_thread_t
*thread
,
673 int step
, int nsignal
)
679 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
680 thread
->msg_state
, thread
->gdb_port
, step
, nsignal
);
682 switch (thread
->msg_state
)
685 if (thread
->event
.ex_type
== EXC_SOFTWARE
686 && thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
688 /* Either deliver a new signal or cancel the signal received. */
689 res
= PTRACE (PT_THUPDATE
, inf
->pid
,
690 (void *)(uintptr_t)thread
->gdb_port
, nsignal
);
692 inferior_debug (1, _("ptrace THUP: res=%d\n"), res
);
696 /* Note: ptrace is allowed only if the process is stopped.
697 Directly send the signal to the thread. */
698 res
= syscall (SYS___pthread_kill
, thread
->gdb_port
, nsignal
);
699 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
700 thread
->gdb_port
, nsignal
, res
);
701 thread
->signaled
= 1;
704 /* Set or reset single step. */
705 if (step
!= thread
->single_step
)
707 inferior_debug (4, _("darwin_set_sstep (thread=0x%x, enable=%d)\n"),
708 thread
->gdb_port
, step
);
709 darwin_set_sstep (thread
->gdb_port
, step
);
710 thread
->single_step
= step
;
713 darwin_send_reply (inf
, thread
);
714 thread
->msg_state
= DARWIN_RUNNING
;
721 kret
= thread_resume (thread
->gdb_port
);
722 MACH_CHECK_ERROR (kret
);
724 thread
->msg_state
= DARWIN_RUNNING
;
729 /* Resume all threads of the inferior. */
732 darwin_resume_inferior_threads (struct inferior
*inf
, int step
, int nsignal
)
734 darwin_thread_t
*thread
;
738 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
740 darwin_resume_thread (inf
, thread
, step
, nsignal
);
743 struct resume_inferior_threads_param
750 darwin_resume_inferior_threads_it (struct inferior
*inf
, void *param
)
752 int step
= ((struct resume_inferior_threads_param
*)param
)->step
;
753 int nsignal
= ((struct resume_inferior_threads_param
*)param
)->nsignal
;
755 darwin_resume_inferior_threads (inf
, step
, nsignal
);
760 /* Suspend all threads of INF. */
763 darwin_suspend_inferior_threads (struct inferior
*inf
)
765 darwin_thread_t
*thread
;
770 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
772 switch (thread
->msg_state
)
778 kret
= thread_suspend (thread
->gdb_port
);
779 MACH_CHECK_ERROR (kret
);
780 thread
->msg_state
= DARWIN_STOPPED
;
786 darwin_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
788 struct target_waitstatus status
;
794 struct inferior
*inf
;
797 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
798 ptid_get_pid (ptid
), ptid_get_tid (ptid
), step
, signal
);
800 if (signal
== GDB_SIGNAL_0
)
803 nsignal
= gdb_signal_to_host (signal
);
805 /* Don't try to single step all threads. */
807 ptid
= inferior_ptid
;
809 /* minus_one_ptid is RESUME_ALL. */
810 if (ptid_equal (ptid
, minus_one_ptid
))
812 struct resume_inferior_threads_param param
;
814 param
.nsignal
= nsignal
;
817 /* Resume threads. */
818 iterate_over_inferiors (darwin_resume_inferior_threads_it
, ¶m
);
820 iterate_over_inferiors (darwin_resume_inferior_it
, NULL
);
824 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ptid
));
825 long tid
= ptid_get_tid (ptid
);
827 /* Stop the inferior (should be useless). */
828 darwin_suspend_inferior (inf
);
831 darwin_resume_inferior_threads (inf
, step
, nsignal
);
834 darwin_thread_t
*thread
;
836 /* Suspend threads of the task. */
837 darwin_suspend_inferior_threads (inf
);
839 /* Resume the selected thread. */
840 thread
= darwin_find_thread (inf
, tid
);
842 darwin_resume_thread (inf
, thread
, step
, nsignal
);
845 /* Resume the task. */
846 darwin_resume_inferior (inf
);
851 darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
852 enum gdb_signal signal
)
854 return darwin_resume (ptid
, step
, signal
);
858 darwin_decode_message (mach_msg_header_t
*hdr
,
859 darwin_thread_t
**pthread
,
860 struct inferior
**pinf
,
861 struct target_waitstatus
*status
)
863 darwin_thread_t
*thread
;
864 struct inferior
*inf
;
866 /* Exception message. */
867 if (hdr
->msgh_local_port
== darwin_ex_port
)
871 /* Decode message. */
872 res
= darwin_decode_exception_message (hdr
, &inf
, &thread
);
876 /* Should not happen... */
878 (_("darwin_wait: ill-formatted message (id=0x%x)\n"), hdr
->msgh_id
);
879 /* FIXME: send a failure reply? */
880 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
881 return minus_one_ptid
;
885 inf
->private->pending_messages
++;
887 status
->kind
= TARGET_WAITKIND_STOPPED
;
888 thread
->msg_state
= DARWIN_MESSAGE
;
890 inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"),
892 unparse_exception_type (thread
->event
.ex_type
));
894 switch (thread
->event
.ex_type
)
897 status
->value
.sig
= GDB_EXC_BAD_ACCESS
;
899 case EXC_BAD_INSTRUCTION
:
900 status
->value
.sig
= GDB_EXC_BAD_INSTRUCTION
;
903 status
->value
.sig
= GDB_EXC_ARITHMETIC
;
906 status
->value
.sig
= GDB_EXC_EMULATION
;
909 if (thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
912 gdb_signal_from_host (thread
->event
.ex_data
[1]);
913 inferior_debug (5, _(" (signal %d: %s)\n"),
914 thread
->event
.ex_data
[1],
915 gdb_signal_to_name (status
->value
.sig
));
917 /* If the thread is stopped because it has received a signal
918 that gdb has just sent, continue. */
919 if (thread
->signaled
)
921 thread
->signaled
= 0;
922 darwin_send_reply (inf
, thread
);
923 thread
->msg_state
= DARWIN_RUNNING
;
924 status
->kind
= TARGET_WAITKIND_IGNORE
;
928 status
->value
.sig
= GDB_EXC_SOFTWARE
;
931 /* Many internal GDB routines expect breakpoints to be reported
932 as GDB_SIGNAL_TRAP, and will report GDB_EXC_BREAKPOINT
933 as a spurious signal. */
934 status
->value
.sig
= GDB_SIGNAL_TRAP
;
937 status
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
941 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
947 inf
= darwin_find_inferior_by_notify (hdr
->msgh_local_port
);
950 if (!inf
->private->no_ptrace
)
955 res
= wait4 (inf
->pid
, &wstatus
, 0, NULL
);
956 if (res
< 0 || res
!= inf
->pid
)
958 printf_unfiltered (_("wait4: res=%d: %s\n"),
959 res
, safe_strerror (errno
));
960 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
961 return minus_one_ptid
;
963 if (WIFEXITED (wstatus
))
965 status
->kind
= TARGET_WAITKIND_EXITED
;
966 status
->value
.integer
= WEXITSTATUS (wstatus
);
970 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
971 status
->value
.sig
= WTERMSIG (wstatus
);
974 inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"),
977 /* Looks necessary on Leopard and harmless... */
978 wait4 (inf
->pid
, &wstatus
, 0, NULL
);
980 return ptid_build (inf
->pid
, 0, 0);
984 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf
->pid
);
985 status
->kind
= TARGET_WAITKIND_EXITED
;
986 status
->value
.integer
= 0; /* Don't know. */
987 return ptid_build (inf
->pid
, 0, 0);
991 printf_unfiltered (_("Bad local-port: 0x%x\n"), hdr
->msgh_local_port
);
992 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
993 return minus_one_ptid
;
997 cancel_breakpoint (ptid_t ptid
)
999 /* Arrange for a breakpoint to be hit again later. We will handle
1000 the current event, eventually we will resume this thread, and this
1001 breakpoint will trap again.
1003 If we do not do this, then we run the risk that the user will
1004 delete or disable the breakpoint, but the thread will have already
1007 struct regcache
*regcache
= get_thread_regcache (ptid
);
1008 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1011 pc
= regcache_read_pc (regcache
) - target_decr_pc_after_break (gdbarch
);
1012 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
1014 inferior_debug (4, "cancel_breakpoint for thread 0x%x\n",
1015 ptid_get_tid (ptid
));
1017 /* Back up the PC if necessary. */
1018 if (target_decr_pc_after_break (gdbarch
))
1019 regcache_write_pc (regcache
, pc
);
1027 darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
)
1032 mach_msg_header_t hdr
;
1035 mach_msg_header_t
*hdr
= &msgin
.hdr
;
1037 darwin_thread_t
*thread
;
1038 struct inferior
*inf
;
1041 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1042 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
1044 /* Handle fake stop events at first. */
1045 if (darwin_inf_fake_stop
!= NULL
)
1047 inf
= darwin_inf_fake_stop
;
1048 darwin_inf_fake_stop
= NULL
;
1050 status
->kind
= TARGET_WAITKIND_STOPPED
;
1051 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1052 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1053 thread
->msg_state
= DARWIN_STOPPED
;
1054 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1059 /* set_sigint_trap (); */
1061 /* Wait for a message. */
1062 kret
= mach_msg (&msgin
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
, 0,
1063 sizeof (msgin
.data
), darwin_port_set
, 0, MACH_PORT_NULL
);
1065 /* clear_sigint_trap (); */
1067 if (kret
== MACH_RCV_INTERRUPTED
)
1069 status
->kind
= TARGET_WAITKIND_IGNORE
;
1070 return minus_one_ptid
;
1073 if (kret
!= MACH_MSG_SUCCESS
)
1075 inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret
);
1076 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
1077 return minus_one_ptid
;
1080 /* Debug: display message. */
1081 if (darwin_debug_flag
> 10)
1082 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1084 res
= darwin_decode_message (hdr
, &thread
, &inf
, status
);
1089 while (status
->kind
== TARGET_WAITKIND_IGNORE
);
1091 /* Stop all tasks. */
1092 iterate_over_inferiors (darwin_suspend_inferior_it
, NULL
);
1094 /* Read pending messages. */
1097 struct target_waitstatus status2
;
1100 kret
= mach_msg (&msgin
.hdr
,
1101 MACH_RCV_MSG
| MACH_RCV_TIMEOUT
, 0,
1102 sizeof (msgin
.data
), darwin_port_set
, 1, MACH_PORT_NULL
);
1104 if (kret
== MACH_RCV_TIMED_OUT
)
1106 if (kret
!= MACH_MSG_SUCCESS
)
1109 (5, _("darwin_wait: mach_msg(pending) ret=0x%x\n"), kret
);
1113 ptid2
= darwin_decode_message (hdr
, &thread
, &inf
, &status2
);
1115 if (inf
!= NULL
&& thread
!= NULL
1116 && thread
->event
.ex_type
== EXC_BREAKPOINT
)
1118 if (thread
->single_step
1119 || cancel_breakpoint (ptid_build (inf
->pid
, 0, thread
->gdb_port
)))
1121 gdb_assert (thread
->msg_state
== DARWIN_MESSAGE
);
1122 darwin_send_reply (inf
, thread
);
1123 thread
->msg_state
= DARWIN_RUNNING
;
1127 (3, _("darwin_wait: thread 0x%x hit a non-gdb breakpoint\n"),
1131 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1137 darwin_wait_to (struct target_ops
*ops
,
1138 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1140 return darwin_wait (ptid
, status
);
1144 darwin_stop (struct target_ops
*self
, ptid_t t
)
1146 struct inferior
*inf
= current_inferior ();
1148 /* FIXME: handle in no_ptrace mode. */
1149 gdb_assert (!inf
->private->no_ptrace
);
1150 kill (inf
->pid
, SIGINT
);
1154 darwin_mourn_inferior (struct target_ops
*ops
)
1156 struct inferior
*inf
= current_inferior ();
1161 unpush_target (darwin_ops
);
1163 /* Deallocate threads. */
1164 if (inf
->private->threads
)
1169 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1172 kret
= mach_port_deallocate (gdb_task
, t
->gdb_port
);
1173 MACH_CHECK_ERROR (kret
);
1175 VEC_free (darwin_thread_t
, inf
->private->threads
);
1176 inf
->private->threads
= NULL
;
1179 kret
= mach_port_move_member (gdb_task
,
1180 inf
->private->notify_port
, MACH_PORT_NULL
);
1181 MACH_CHECK_ERROR (kret
);
1183 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1184 MACH_NOTIFY_DEAD_NAME
, 0,
1186 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1188 /* This can fail if the task is dead. */
1189 inferior_debug (4, "task=0x%x, prev=0x%x, notify_port=0x%x\n",
1190 inf
->private->task
, prev
, inf
->private->notify_port
);
1192 if (kret
== KERN_SUCCESS
)
1194 kret
= mach_port_deallocate (gdb_task
, prev
);
1195 MACH_CHECK_ERROR (kret
);
1198 kret
= mach_port_destroy (gdb_task
, inf
->private->notify_port
);
1199 MACH_CHECK_ERROR (kret
);
1202 /* Deallocate saved exception ports. */
1203 for (i
= 0; i
< inf
->private->exception_info
.count
; i
++)
1205 kret
= mach_port_deallocate
1206 (gdb_task
, inf
->private->exception_info
.ports
[i
]);
1207 MACH_CHECK_ERROR (kret
);
1209 inf
->private->exception_info
.count
= 0;
1211 kret
= mach_port_deallocate (gdb_task
, inf
->private->task
);
1212 MACH_CHECK_ERROR (kret
);
1214 xfree (inf
->private);
1215 inf
->private = NULL
;
1217 generic_mourn_inferior ();
1221 darwin_reply_to_all_pending_messages (struct inferior
*inf
)
1227 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1230 if (t
->msg_state
== DARWIN_MESSAGE
)
1231 darwin_resume_thread (inf
, t
, 0, 0);
1236 darwin_stop_inferior (struct inferior
*inf
)
1238 struct target_waitstatus wstatus
;
1244 gdb_assert (inf
!= NULL
);
1246 darwin_suspend_inferior (inf
);
1248 darwin_reply_to_all_pending_messages (inf
);
1250 if (inf
->private->no_ptrace
)
1253 res
= kill (inf
->pid
, SIGSTOP
);
1255 warning (_("cannot kill: %s"), safe_strerror (errno
));
1257 /* Wait until the process is really stopped. */
1260 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1261 if (wstatus
.kind
== TARGET_WAITKIND_STOPPED
1262 && wstatus
.value
.sig
== GDB_SIGNAL_STOP
)
1267 static kern_return_t
1268 darwin_save_exception_ports (darwin_inferior
*inf
)
1272 inf
->exception_info
.count
=
1273 sizeof (inf
->exception_info
.ports
) / sizeof (inf
->exception_info
.ports
[0]);
1275 kret
= task_get_exception_ports
1276 (inf
->task
, EXC_MASK_ALL
, inf
->exception_info
.masks
,
1277 &inf
->exception_info
.count
, inf
->exception_info
.ports
,
1278 inf
->exception_info
.behaviors
, inf
->exception_info
.flavors
);
1282 static kern_return_t
1283 darwin_restore_exception_ports (darwin_inferior
*inf
)
1288 for (i
= 0; i
< inf
->exception_info
.count
; i
++)
1290 kret
= task_set_exception_ports
1291 (inf
->task
, inf
->exception_info
.masks
[i
], inf
->exception_info
.ports
[i
],
1292 inf
->exception_info
.behaviors
[i
], inf
->exception_info
.flavors
[i
]);
1293 if (kret
!= KERN_SUCCESS
)
1297 return KERN_SUCCESS
;
1301 darwin_kill_inferior (struct target_ops
*ops
)
1303 struct inferior
*inf
= current_inferior ();
1304 struct target_waitstatus wstatus
;
1310 if (ptid_equal (inferior_ptid
, null_ptid
))
1313 gdb_assert (inf
!= NULL
);
1315 kret
= darwin_restore_exception_ports (inf
->private);
1316 MACH_CHECK_ERROR (kret
);
1318 darwin_reply_to_all_pending_messages (inf
);
1320 res
= kill (inf
->pid
, 9);
1324 darwin_resume_inferior (inf
);
1326 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1328 else if (errno
!= ESRCH
)
1329 warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"),
1330 inf
->pid
, safe_strerror (errno
));
1332 target_mourn_inferior ();
1336 darwin_attach_pid (struct inferior
*inf
)
1339 mach_port_t prev_port
;
1341 mach_port_t prev_not
;
1342 exception_mask_t mask
;
1344 inf
->private = XCNEW (darwin_inferior
);
1346 kret
= task_for_pid (gdb_task
, inf
->pid
, &inf
->private->task
);
1347 if (kret
!= KERN_SUCCESS
)
1351 if (!inf
->attach_flag
)
1354 waitpid (inf
->pid
, &status
, 0);
1357 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1358 " (please check gdb is codesigned - see taskgated(8))"),
1359 inf
->pid
, mach_error_string (kret
), (unsigned long) kret
);
1362 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1363 inf
->private->task
, inf
->pid
);
1365 if (darwin_ex_port
== MACH_PORT_NULL
)
1367 /* Create a port to get exceptions. */
1368 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1370 if (kret
!= KERN_SUCCESS
)
1371 error (_("Unable to create exception port, mach_port_allocate "
1375 kret
= mach_port_insert_right (gdb_task
, darwin_ex_port
, darwin_ex_port
,
1376 MACH_MSG_TYPE_MAKE_SEND
);
1377 if (kret
!= KERN_SUCCESS
)
1378 error (_("Unable to create exception port, mach_port_insert_right "
1382 /* Create a port set and put ex_port in it. */
1383 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_PORT_SET
,
1385 if (kret
!= KERN_SUCCESS
)
1386 error (_("Unable to create port set, mach_port_allocate "
1390 kret
= mach_port_move_member (gdb_task
, darwin_ex_port
, darwin_port_set
);
1391 if (kret
!= KERN_SUCCESS
)
1392 error (_("Unable to move exception port into new port set, "
1393 "mach_port_move_member\n"
1398 /* Create a port to be notified when the child task terminates. */
1399 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1400 &inf
->private->notify_port
);
1401 if (kret
!= KERN_SUCCESS
)
1402 error (_("Unable to create notification port, mach_port_allocate "
1406 kret
= mach_port_move_member (gdb_task
,
1407 inf
->private->notify_port
, darwin_port_set
);
1408 if (kret
!= KERN_SUCCESS
)
1409 error (_("Unable to move notification port into new port set, "
1410 "mach_port_move_member\n"
1414 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1415 MACH_NOTIFY_DEAD_NAME
, 0,
1416 inf
->private->notify_port
,
1417 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1419 if (kret
!= KERN_SUCCESS
)
1420 error (_("Termination notification request failed, "
1421 "mach_port_request_notification\n"
1424 if (prev_not
!= MACH_PORT_NULL
)
1426 /* This is unexpected, as there should not be any previously
1427 registered notification request. But this is not a fatal
1428 issue, so just emit a warning. */
1430 A task termination request was registered before the debugger registered\n\
1431 its own. This is unexpected, but should otherwise not have any actual\n\
1432 impact on the debugging session."));
1435 kret
= darwin_save_exception_ports (inf
->private);
1436 if (kret
!= KERN_SUCCESS
)
1437 error (_("Unable to save exception ports, task_get_exception_ports"
1441 /* Set exception port. */
1442 if (enable_mach_exceptions
)
1443 mask
= EXC_MASK_ALL
;
1445 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1446 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1447 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1448 if (kret
!= KERN_SUCCESS
)
1449 error (_("Unable to set exception ports, task_set_exception_ports"
1453 push_target (darwin_ops
);
1457 darwin_init_thread_list (struct inferior
*inf
)
1459 darwin_thread_t
*thread
;
1462 darwin_check_new_threads (inf
);
1464 gdb_assert (inf
->private->threads
1465 && VEC_length (darwin_thread_t
, inf
->private->threads
) > 0);
1466 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1468 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1470 new_ptid
= ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1471 thread_change_ptid (inferior_ptid
, new_ptid
);
1472 inferior_ptid
= new_ptid
;
1475 /* The child must synchronize with gdb: gdb must set the exception port
1476 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1477 FIXME: is there a lighter way ? */
1478 static int ptrace_fds
[2];
1481 darwin_ptrace_me (void)
1486 /* Close write end point. */
1487 close (ptrace_fds
[1]);
1489 /* Wait until gdb is ready. */
1490 res
= read (ptrace_fds
[0], &c
, 1);
1492 error (_("unable to read from pipe, read returned: %d"), res
);
1493 close (ptrace_fds
[0]);
1495 /* Get rid of privileges. */
1496 setegid (getgid ());
1499 PTRACE (PT_TRACE_ME
, 0, 0, 0);
1501 /* Redirect signals to exception port. */
1502 PTRACE (PT_SIGEXC
, 0, 0, 0);
1505 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1507 darwin_pre_ptrace (void)
1509 if (pipe (ptrace_fds
) != 0)
1513 error (_("unable to create a pipe: %s"), safe_strerror (errno
));
1516 mark_fd_no_cloexec (ptrace_fds
[0]);
1517 mark_fd_no_cloexec (ptrace_fds
[1]);
1521 darwin_ptrace_him (int pid
)
1525 mach_port_t prev_port
;
1527 struct inferior
*inf
= current_inferior ();
1529 darwin_attach_pid (inf
);
1531 /* Let's the child run. */
1532 close (ptrace_fds
[0]);
1533 close (ptrace_fds
[1]);
1535 unmark_fd_no_cloexec (ptrace_fds
[0]);
1536 unmark_fd_no_cloexec (ptrace_fds
[1]);
1538 darwin_init_thread_list (inf
);
1540 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
1544 darwin_execvp (const char *file
, char * const argv
[], char * const env
[])
1546 posix_spawnattr_t attr
;
1550 res
= posix_spawnattr_init (&attr
);
1554 (gdb_stderr
, "Cannot initialize attribute for posix_spawn\n");
1558 /* Do like execve: replace the image. */
1559 ps_flags
= POSIX_SPAWN_SETEXEC
;
1561 /* Disable ASLR. The constant doesn't look to be available outside the
1562 kernel include files. */
1563 #ifndef _POSIX_SPAWN_DISABLE_ASLR
1564 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1566 ps_flags
|= _POSIX_SPAWN_DISABLE_ASLR
;
1567 res
= posix_spawnattr_setflags (&attr
, ps_flags
);
1570 fprintf_unfiltered (gdb_stderr
, "Cannot set posix_spawn flags\n");
1574 posix_spawnp (NULL
, argv
[0], NULL
, &attr
, argv
, env
);
1578 darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
1579 char *allargs
, char **env
, int from_tty
)
1581 /* Do the hard work. */
1582 fork_inferior (exec_file
, allargs
, env
, darwin_ptrace_me
, darwin_ptrace_him
,
1583 darwin_pre_ptrace
, NULL
, darwin_execvp
);
1585 /* Return now in case of error. */
1586 if (ptid_equal (inferior_ptid
, null_ptid
))
1591 /* Set things up such that the next call to darwin_wait will immediately
1592 return a fake stop event for inferior INF.
1594 This assumes that the inferior's thread list has been initialized,
1595 as it will suspend the inferior's first thread. */
1598 darwin_setup_fake_stop_event (struct inferior
*inf
)
1600 darwin_thread_t
*thread
;
1603 gdb_assert (darwin_inf_fake_stop
== NULL
);
1604 darwin_inf_fake_stop
= inf
;
1606 /* When detecting a fake pending stop event, darwin_wait returns
1607 an event saying that the first thread is in a DARWIN_STOPPED
1608 state. To make that accurate, we need to suspend that thread
1609 as well. Otherwise, we'll try resuming it when resuming the
1610 inferior, and get a warning because the thread's suspend count
1611 is already zero, making the resume request useless. */
1612 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1613 kret
= thread_suspend (thread
->gdb_port
);
1614 MACH_CHECK_ERROR (kret
);
1617 /* Attach to process PID, then initialize for debugging it
1618 and wait for the trace-trap that results from attaching. */
1620 darwin_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1626 struct inferior
*inf
;
1629 pid
= parse_pid_to_attach (args
);
1631 if (pid
== getpid ()) /* Trying to masturbate? */
1632 error (_("I refuse to debug myself!"));
1636 char *exec_file
= get_exec_file (0);
1639 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
1640 target_pid_to_str (pid_to_ptid (pid
)));
1642 printf_unfiltered (_("Attaching to %s\n"),
1643 target_pid_to_str (pid_to_ptid (pid
)));
1645 gdb_flush (gdb_stdout
);
1648 if (pid
== 0 || kill (pid
, 0) < 0)
1649 error (_("Can't attach to process %d: %s (%d)"),
1650 pid
, safe_strerror (errno
), errno
);
1652 inferior_ptid
= pid_to_ptid (pid
);
1653 inf
= current_inferior ();
1654 inferior_appeared (inf
, pid
);
1655 inf
->attach_flag
= 1;
1657 /* Always add a main thread. */
1658 add_thread_silent (inferior_ptid
);
1660 darwin_attach_pid (inf
);
1662 darwin_suspend_inferior (inf
);
1664 darwin_init_thread_list (inf
);
1666 darwin_check_osabi (inf
->private, ptid_get_tid (inferior_ptid
));
1668 darwin_setup_fake_stop_event (inf
);
1670 inf
->private->no_ptrace
= 1;
1673 /* Take a program previously attached to and detaches it.
1674 The program resumes execution and will no longer stop
1675 on signals, etc. We'd better not have left any breakpoints
1676 in the program or it'll die when it hits one. For this
1677 to work, it may be necessary for the process to have been
1678 previously attached. It *might* work if the program was
1679 started via fork. */
1681 darwin_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1683 pid_t pid
= ptid_get_pid (inferior_ptid
);
1684 struct inferior
*inf
= current_inferior ();
1688 /* Display message. */
1691 char *exec_file
= get_exec_file (0);
1694 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
1695 target_pid_to_str (pid_to_ptid (pid
)));
1696 gdb_flush (gdb_stdout
);
1699 /* If ptrace() is in use, stop the process. */
1700 if (!inf
->private->no_ptrace
)
1701 darwin_stop_inferior (inf
);
1703 kret
= darwin_restore_exception_ports (inf
->private);
1704 MACH_CHECK_ERROR (kret
);
1706 if (!inf
->private->no_ptrace
)
1708 res
= PTRACE (PT_DETACH
, inf
->pid
, 0, 0);
1710 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1711 inf
->pid
, safe_strerror (errno
), errno
);
1714 darwin_reply_to_all_pending_messages (inf
);
1716 /* When using ptrace, we have just performed a PT_DETACH, which
1717 resumes the inferior. On the other hand, when we are not using
1718 ptrace, we need to resume its execution ourselves. */
1719 if (inf
->private->no_ptrace
)
1720 darwin_resume_inferior (inf
);
1722 darwin_mourn_inferior (ops
);
1726 darwin_files_info (struct target_ops
*ops
)
1731 darwin_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1733 static char buf
[80];
1734 long tid
= ptid_get_tid (ptid
);
1738 snprintf (buf
, sizeof (buf
), _("Thread 0x%lx of process %u"),
1739 tid
, ptid_get_pid (ptid
));
1743 return normal_pid_to_str (ptid
);
1747 darwin_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1752 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1753 copy it to RDADDR in gdb's address space.
1754 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1755 to ADDR in inferior task's address space.
1756 Return 0 on failure; number of bytes read / writen otherwise. */
1758 darwin_read_write_inferior (task_t task
, CORE_ADDR addr
,
1759 gdb_byte
*rdaddr
, const gdb_byte
*wraddr
,
1763 mach_vm_address_t offset
= addr
& (mach_page_size
- 1);
1764 mach_vm_address_t low_address
= (mach_vm_address_t
) (addr
- offset
);
1765 mach_vm_size_t aligned_length
= (mach_vm_size_t
) PAGE_ROUND (offset
+ length
);
1767 mach_msg_type_number_t copy_count
;
1768 mach_vm_size_t remaining_length
;
1769 mach_vm_address_t region_address
;
1770 mach_vm_size_t region_length
;
1772 inferior_debug (8, _("darwin_read_write_inferior(task=0x%x, %s, len=%s)\n"),
1773 task
, core_addr_to_string (addr
), pulongest (length
));
1775 /* Get memory from inferior with page aligned addresses. */
1776 kret
= mach_vm_read (task
, low_address
, aligned_length
,
1777 &copied
, ©_count
);
1778 if (kret
!= KERN_SUCCESS
)
1781 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1782 core_addr_to_string (addr
), mach_error_string (kret
));
1787 memcpy (rdaddr
, (char *)copied
+ offset
, length
);
1792 memcpy ((char *)copied
+ offset
, wraddr
, length
);
1794 /* Do writes atomically.
1795 First check for holes and unwritable memory. */
1796 for (region_address
= low_address
, remaining_length
= aligned_length
;
1797 region_address
< low_address
+ aligned_length
;
1798 region_address
+= region_length
, remaining_length
-= region_length
)
1800 vm_region_submap_short_info_data_64_t info
;
1801 mach_vm_address_t region_start
= region_address
;
1802 mach_msg_type_number_t count
;
1803 natural_t region_depth
;
1805 region_depth
= 100000;
1806 count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
1807 kret
= mach_vm_region_recurse
1808 (task
, ®ion_start
, ®ion_length
, ®ion_depth
,
1809 (vm_region_recurse_info_t
) &info
, &count
);
1811 if (kret
!= KERN_SUCCESS
)
1813 inferior_debug (1, _("darwin_read_write_inferior: "
1814 "mach_vm_region_recurse failed at %s: %s\n"),
1815 core_addr_to_string (region_address
),
1816 mach_error_string (kret
));
1821 (9, _("darwin_read_write_inferior: "
1822 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1823 core_addr_to_string (region_address
),
1824 core_addr_to_string (region_start
),
1825 core_addr_to_string (region_length
));
1827 /* Check for holes in memory. */
1828 if (region_start
> region_address
)
1830 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1831 core_addr_to_string (region_address
),
1832 core_addr_to_string (region_start
),
1833 (unsigned)region_length
);
1838 /* Adjust the length. */
1839 region_length
-= (region_address
- region_start
);
1841 if (!(info
.max_protection
& VM_PROT_WRITE
))
1843 kret
= mach_vm_protect
1844 (task
, region_address
, region_length
,
1845 TRUE
, info
.max_protection
| VM_PROT_WRITE
| VM_PROT_COPY
);
1846 if (kret
!= KERN_SUCCESS
)
1848 warning (_("darwin_read_write_inf: "
1849 "mach_vm_protect max failed at %s: %s"),
1850 core_addr_to_string (region_address
),
1851 mach_error_string (kret
));
1857 if (!(info
.protection
& VM_PROT_WRITE
))
1859 kret
= mach_vm_protect (task
, region_address
, region_length
,
1860 FALSE
, info
.protection
| VM_PROT_WRITE
);
1861 if (kret
!= KERN_SUCCESS
)
1863 warning (_("darwin_read_write_inf: "
1864 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1865 core_addr_to_string (region_address
),
1866 (unsigned long)region_length
, mach_error_string (kret
));
1873 kret
= mach_vm_write (task
, low_address
, copied
, aligned_length
);
1875 if (kret
!= KERN_SUCCESS
)
1877 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1878 mach_error_string (kret
));
1882 mach_vm_deallocate (mach_task_self (), copied
, copy_count
);
1886 /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
1888 Return 0 on failure; number of bytes read / written otherwise. */
1890 #ifdef TASK_DYLD_INFO_COUNT
1891 /* This is not available in Darwin 9. */
1892 static enum target_xfer_status
1893 darwin_read_dyld_info (task_t task
, CORE_ADDR addr
, gdb_byte
*rdaddr
,
1894 ULONGEST length
, ULONGEST
*xfered_len
)
1896 struct task_dyld_info task_dyld_info
;
1897 mach_msg_type_number_t count
= TASK_DYLD_INFO_COUNT
;
1898 int sz
= TASK_DYLD_INFO_COUNT
* sizeof (natural_t
);
1902 return TARGET_XFER_EOF
;
1904 kret
= task_info (task
, TASK_DYLD_INFO
, (task_info_t
) &task_dyld_info
, &count
);
1905 MACH_CHECK_ERROR (kret
);
1906 if (kret
!= KERN_SUCCESS
)
1907 return TARGET_XFER_E_IO
;
1909 if (addr
+ length
> sz
)
1911 memcpy (rdaddr
, (char *)&task_dyld_info
+ addr
, length
);
1912 *xfered_len
= (ULONGEST
) length
;
1913 return TARGET_XFER_OK
;
1919 static enum target_xfer_status
1920 darwin_xfer_partial (struct target_ops
*ops
,
1921 enum target_object object
, const char *annex
,
1922 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1923 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1925 struct inferior
*inf
= current_inferior ();
1928 (8, _("darwin_xfer_partial(%s, %s, rbuf=%s, wbuf=%s) pid=%u\n"),
1929 core_addr_to_string (offset
), pulongest (len
),
1930 host_address_to_string (readbuf
), host_address_to_string (writebuf
),
1935 case TARGET_OBJECT_MEMORY
:
1937 int l
= darwin_read_write_inferior (inf
->private->task
, offset
,
1938 readbuf
, writebuf
, len
);
1941 return TARGET_XFER_EOF
;
1945 *xfered_len
= (ULONGEST
) l
;
1946 return TARGET_XFER_OK
;
1949 #ifdef TASK_DYLD_INFO_COUNT
1950 case TARGET_OBJECT_DARWIN_DYLD_INFO
:
1951 if (writebuf
!= NULL
|| readbuf
== NULL
)
1953 /* Support only read. */
1954 return TARGET_XFER_E_IO
;
1956 return darwin_read_dyld_info (inf
->private->task
, offset
, readbuf
, len
,
1960 return TARGET_XFER_E_IO
;
1966 set_enable_mach_exceptions (char *args
, int from_tty
,
1967 struct cmd_list_element
*c
)
1969 if (!ptid_equal (inferior_ptid
, null_ptid
))
1971 struct inferior
*inf
= current_inferior ();
1972 exception_mask_t mask
;
1975 if (enable_mach_exceptions
)
1976 mask
= EXC_MASK_ALL
;
1979 darwin_restore_exception_ports (inf
->private);
1980 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1982 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1983 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1984 MACH_CHECK_ERROR (kret
);
1989 darwin_pid_to_exec_file (struct target_ops
*self
, int pid
)
1991 static char path
[PATH_MAX
];
1994 res
= proc_pidinfo (pid
, PROC_PIDPATHINFO
, 0, path
, PATH_MAX
);
2002 darwin_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
2007 struct inferior
*inf
= current_inferior ();
2009 mach_port_name_array_t names
;
2010 mach_msg_type_number_t names_count
;
2011 mach_port_type_array_t types
;
2012 mach_msg_type_number_t types_count
;
2015 /* First linear search. */
2017 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2019 if (t
->inf_port
== lwp
)
2020 return ptid_build (ptid_get_pid (inferior_ptid
), 0, t
->gdb_port
);
2022 /* Maybe the port was never extract. Do it now. */
2024 /* First get inferior port names. */
2025 kret
= mach_port_names (inf
->private->task
, &names
, &names_count
, &types
,
2027 MACH_CHECK_ERROR (kret
);
2028 if (kret
!= KERN_SUCCESS
)
2031 /* For each name, copy the right in the gdb space and then compare with
2032 our view of the inferior threads. We don't forget to deallocate the
2034 for (i
= 0; i
< names_count
; i
++)
2036 mach_port_t local_name
;
2037 mach_msg_type_name_t local_type
;
2039 /* We just need to know the corresponding name in gdb name space.
2040 So extract and deallocate the right. */
2041 kret
= mach_port_extract_right (inf
->private->task
, names
[i
],
2042 MACH_MSG_TYPE_COPY_SEND
,
2043 &local_name
, &local_type
);
2044 if (kret
!= KERN_SUCCESS
)
2046 mach_port_deallocate (gdb_task
, local_name
);
2049 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
2051 if (t
->gdb_port
== local_name
)
2053 t
->inf_port
= names
[i
];
2054 if (names
[i
] == lwp
)
2059 vm_deallocate (gdb_task
, (vm_address_t
) names
,
2060 names_count
* sizeof (mach_port_t
));
2063 return ptid_build (ptid_get_pid (inferior_ptid
), 0, res
);
2069 darwin_supports_multi_process (struct target_ops
*self
)
2074 /* -Wmissing-prototypes */
2075 extern initialize_file_ftype _initialize_darwin_inferior
;
2078 _initialize_darwin_inferior (void)
2082 gdb_task
= mach_task_self ();
2083 darwin_host_self
= mach_host_self ();
2085 /* Read page size. */
2086 kret
= host_page_size (darwin_host_self
, &mach_page_size
);
2087 if (kret
!= KERN_SUCCESS
)
2089 mach_page_size
= 0x1000;
2090 MACH_CHECK_ERROR (kret
);
2093 darwin_ops
= inf_child_target ();
2095 darwin_ops
->to_shortname
= "darwin-child";
2096 darwin_ops
->to_longname
= _("Darwin child process");
2097 darwin_ops
->to_doc
=
2098 _("Darwin child process (started by the \"run\" command).");
2099 darwin_ops
->to_create_inferior
= darwin_create_inferior
;
2100 darwin_ops
->to_attach
= darwin_attach
;
2101 darwin_ops
->to_attach_no_wait
= 0;
2102 darwin_ops
->to_detach
= darwin_detach
;
2103 darwin_ops
->to_files_info
= darwin_files_info
;
2104 darwin_ops
->to_wait
= darwin_wait_to
;
2105 darwin_ops
->to_mourn_inferior
= darwin_mourn_inferior
;
2106 darwin_ops
->to_kill
= darwin_kill_inferior
;
2107 darwin_ops
->to_stop
= darwin_stop
;
2108 darwin_ops
->to_resume
= darwin_resume_to
;
2109 darwin_ops
->to_thread_alive
= darwin_thread_alive
;
2110 darwin_ops
->to_pid_to_str
= darwin_pid_to_str
;
2111 darwin_ops
->to_pid_to_exec_file
= darwin_pid_to_exec_file
;
2112 darwin_ops
->to_load
= NULL
;
2113 darwin_ops
->to_xfer_partial
= darwin_xfer_partial
;
2114 darwin_ops
->to_supports_multi_process
= darwin_supports_multi_process
;
2115 darwin_ops
->to_get_ada_task_ptid
= darwin_get_ada_task_ptid
;
2117 darwin_complete_target (darwin_ops
);
2119 add_target (darwin_ops
);
2121 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
2124 add_setshow_zuinteger_cmd ("darwin", class_obscure
,
2125 &darwin_debug_flag
, _("\
2126 Set if printing inferior communication debugging statements."), _("\
2127 Show if printing inferior communication debugging statements."), NULL
,
2129 &setdebuglist
, &showdebuglist
);
2131 add_setshow_boolean_cmd ("mach-exceptions", class_support
,
2132 &enable_mach_exceptions
, _("\
2133 Set if mach exceptions are caught."), _("\
2134 Show if mach exceptions are caught."), _("\
2135 When this mode is on, all low level exceptions are reported before being\n\
2136 reported by the kernel."),
2137 &set_enable_mach_exceptions
, NULL
,
2138 &setlist
, &showlist
);