1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by AdaCore.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "gdbthread.h"
34 #include "event-top.h"
37 #include "exceptions.h"
38 #include "inf-child.h"
40 #include "arch-utils.h"
43 #include <sys/ptrace.h>
44 #include <sys/signal.h>
45 #include <machine/setjmp.h>
46 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/sysctl.h>
55 #include <sys/syscall.h>
57 #include <mach/mach_error.h>
58 #include <mach/mach_vm.h>
59 #include <mach/mach_init.h>
60 #include <mach/vm_map.h>
61 #include <mach/task.h>
62 #include <mach/mach_port.h>
63 #include <mach/thread_act.h>
64 #include <mach/port.h>
66 #include "darwin-nat.h"
69 Darwin kernel is Mach + BSD derived kernel. Note that they share the
70 same memory space and are linked together (ie there is no micro-kernel).
72 Although ptrace(2) is available on Darwin, it is not complete. We have
73 to use Mach calls to read and write memory and to modify registers. We
74 also use Mach to get inferior faults. As we cannot use select(2) or
75 signals with Mach port (the Mach communication channel), signals are
76 reported to gdb as an exception. Furthermore we detect death of the
77 inferior through a Mach notification message. This way we only wait
80 Some Mach documentation is available for Apple xnu source package or
84 #define PTRACE(CMD, PID, ADDR, SIG) \
85 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
87 extern boolean_t
exc_server (mach_msg_header_t
*in
, mach_msg_header_t
*out
);
89 static void darwin_stop (ptid_t
);
91 static void darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
92 enum target_signal signal
);
93 static void darwin_resume (ptid_t ptid
, int step
,
94 enum target_signal signal
);
96 static ptid_t
darwin_wait_to (struct target_ops
*ops
, ptid_t ptid
,
97 struct target_waitstatus
*status
, int options
);
98 static ptid_t
darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
);
100 static void darwin_mourn_inferior (struct target_ops
*ops
);
102 static void darwin_kill_inferior (struct target_ops
*ops
);
104 static void darwin_ptrace_me (void);
106 static void darwin_ptrace_him (int pid
);
108 static void darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
109 char *allargs
, char **env
, int from_tty
);
111 static void darwin_files_info (struct target_ops
*ops
);
113 static char *darwin_pid_to_str (struct target_ops
*ops
, ptid_t tpid
);
115 static int darwin_thread_alive (struct target_ops
*ops
, ptid_t tpid
);
117 /* Target operations for Darwin. */
118 static struct target_ops
*darwin_ops
;
120 /* Task identifier of gdb. */
121 static task_t gdb_task
;
123 /* A copy of mach_host_self (). */
124 mach_port_t darwin_host_self
;
126 /* Exception port. */
127 mach_port_t darwin_ex_port
;
130 mach_port_t darwin_port_set
;
133 static vm_size_t mach_page_size
;
135 /* If Set, catch all mach exceptions (before they are converted to signals
137 static int enable_mach_exceptions
;
139 /* Inferior that should report a fake stop event. */
140 static struct inferior
*darwin_inf_fake_stop
;
142 #define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1))
143 #define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1)
145 /* This controls output of inferior debugging. */
146 static int darwin_debug_flag
= 0;
148 /* Create a __TEXT __info_plist section in the executable so that gdb could
149 be signed. This is required to get an authorization for task_for_pid.
151 Once gdb is built, you can either:
152 * make it setgid procmod
153 * or codesign it with any system-trusted signing authority.
154 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)\n"),
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
;
237 darwin_ptrace (const char *name
,
238 int request
, int pid
, PTRACE_TYPE_ARG3 arg3
, int arg4
)
242 ret
= ptrace (request
, pid
, (caddr_t
) arg3
, arg4
);
244 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
245 name
, pid
, arg3
, arg4
, ret
,
246 (ret
!= 0) ? safe_strerror (errno
) : _("no error"));
251 cmp_thread_t (const void *l
, const void *r
)
253 thread_t tl
= *(const thread_t
*)l
;
254 thread_t tr
= *(const thread_t
*)r
;
255 return (int)(tl
- tr
);
259 darwin_check_new_threads (struct inferior
*inf
)
263 thread_array_t thread_list
;
264 unsigned int new_nbr
;
265 unsigned int old_nbr
;
266 unsigned int new_ix
, old_ix
;
267 darwin_inferior
*darwin_inf
= inf
->private;
268 VEC (darwin_thread_t
) *thread_vec
;
270 /* Get list of threads. */
271 kret
= task_threads (darwin_inf
->task
, &thread_list
, &new_nbr
);
272 MACH_CHECK_ERROR (kret
);
273 if (kret
!= KERN_SUCCESS
)
278 qsort (thread_list
, new_nbr
, sizeof (thread_t
), cmp_thread_t
);
280 if (darwin_inf
->threads
)
281 old_nbr
= VEC_length (darwin_thread_t
, darwin_inf
->threads
);
285 /* Quick check for no changes. */
286 if (old_nbr
== new_nbr
)
288 for (i
= 0; i
< new_nbr
; i
++)
290 != VEC_index (darwin_thread_t
, darwin_inf
->threads
, i
)->gdb_port
)
294 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
295 new_nbr
* sizeof (int));
296 MACH_CHECK_ERROR (kret
);
301 thread_vec
= VEC_alloc (darwin_thread_t
, new_nbr
);
303 for (new_ix
= 0, old_ix
= 0; new_ix
< new_nbr
|| old_ix
< old_nbr
;)
305 thread_t new_id
= (new_ix
< new_nbr
) ?
306 thread_list
[new_ix
] : THREAD_NULL
;
307 darwin_thread_t
*old
= (old_ix
< old_nbr
) ?
308 VEC_index (darwin_thread_t
, darwin_inf
->threads
, old_ix
) : NULL
;
309 thread_t old_id
= old
? old
->gdb_port
: THREAD_NULL
;
312 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:%x old_id:%x\n"),
313 new_ix
, new_nbr
, old_ix
, old_nbr
, new_id
, old_id
);
315 if (old_id
== new_id
)
317 /* Thread still exist. */
318 VEC_safe_push (darwin_thread_t
, thread_vec
, old
);
322 kret
= mach_port_deallocate (gdb_task
, old_id
);
323 MACH_CHECK_ERROR (kret
);
326 if (new_ix
< new_nbr
&& new_id
== MACH_PORT_DEAD
)
328 /* Ignore dead ports.
329 In some weird cases, we might get dead ports. They should
330 correspond to dead thread so they could safely be ignored. */
334 if (new_ix
< new_nbr
&& (old_ix
== old_nbr
|| new_id
< old_id
))
336 /* A thread was created. */
337 struct thread_info
*tp
;
338 struct private_thread_info
*pti
;
340 pti
= XZALLOC (struct private_thread_info
);
341 pti
->gdb_port
= new_id
;
342 pti
->msg_state
= DARWIN_RUNNING
;
344 /* Add a new thread unless this is the first one ever met. */
345 if (!(old_nbr
== 0 && new_ix
== 0))
346 tp
= add_thread_with_info (ptid_build (inf
->pid
, 0, new_id
), pti
);
349 tp
= find_thread_ptid (ptid_build (inf
->pid
, 0, 0));
353 VEC_safe_push (darwin_thread_t
, thread_vec
, pti
);
357 if (old_ix
< old_nbr
&& (new_ix
== new_nbr
|| new_id
> old_id
))
359 /* A thread was removed. */
360 delete_thread (ptid_build (inf
->pid
, 0, old_id
));
361 kret
= mach_port_deallocate (gdb_task
, old_id
);
362 MACH_CHECK_ERROR (kret
);
366 gdb_assert_not_reached ("unexpected thread case");
369 if (darwin_inf
->threads
)
370 VEC_free (darwin_thread_t
, darwin_inf
->threads
);
371 darwin_inf
->threads
= thread_vec
;
373 kret
= vm_deallocate (gdb_task
, (vm_address_t
) thread_list
,
374 new_nbr
* sizeof (int));
375 MACH_CHECK_ERROR (kret
);
379 find_inferior_task_it (struct inferior
*inf
, void *port_ptr
)
381 return inf
->private->task
== *(task_t
*)port_ptr
;
385 find_inferior_notify_it (struct inferior
*inf
, void *port_ptr
)
387 return inf
->private->notify_port
== *(task_t
*)port_ptr
;
390 /* Return an inferior by task port. */
391 static struct inferior
*
392 darwin_find_inferior_by_task (task_t port
)
394 return iterate_over_inferiors (&find_inferior_task_it
, &port
);
397 /* Return an inferior by notification port. */
398 static struct inferior
*
399 darwin_find_inferior_by_notify (mach_port_t port
)
401 return iterate_over_inferiors (&find_inferior_notify_it
, &port
);
404 /* Return a thread by port. */
405 static darwin_thread_t
*
406 darwin_find_thread (struct inferior
*inf
, thread_t thread
)
412 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
414 if (t
->gdb_port
== thread
)
419 /* Suspend (ie stop) an inferior at Mach level. */
422 darwin_suspend_inferior (struct inferior
*inf
)
424 if (!inf
->private->suspended
)
428 kret
= task_suspend (inf
->private->task
);
429 MACH_CHECK_ERROR (kret
);
431 inf
->private->suspended
= 1;
435 /* Resume an inferior at Mach level. */
438 darwin_resume_inferior (struct inferior
*inf
)
440 if (inf
->private->suspended
)
444 kret
= task_resume (inf
->private->task
);
445 MACH_CHECK_ERROR (kret
);
447 inf
->private->suspended
= 0;
451 /* Iterator functions. */
454 darwin_suspend_inferior_it (struct inferior
*inf
, void *arg
)
456 darwin_suspend_inferior (inf
);
457 darwin_check_new_threads (inf
);
462 darwin_resume_inferior_it (struct inferior
*inf
, void *arg
)
464 darwin_resume_inferior (inf
);
469 darwin_dump_message (mach_msg_header_t
*hdr
, int disp_body
)
471 printf_unfiltered (_("message header:\n"));
472 printf_unfiltered (_(" bits: 0x%x\n"), hdr
->msgh_bits
);
473 printf_unfiltered (_(" size: 0x%x\n"), hdr
->msgh_size
);
474 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr
->msgh_remote_port
);
475 printf_unfiltered (_(" local-port: 0x%x\n"), hdr
->msgh_local_port
);
476 printf_unfiltered (_(" reserved: 0x%x\n"), hdr
->msgh_reserved
);
477 printf_unfiltered (_(" id: 0x%x\n"), hdr
->msgh_id
);
481 const unsigned char *data
;
482 const unsigned long *ldata
;
486 data
= (unsigned char *)(hdr
+ 1);
487 size
= hdr
->msgh_size
- sizeof (mach_msg_header_t
);
489 if (hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
491 mach_msg_body_t
*bod
= (mach_msg_body_t
*)data
;
492 mach_msg_port_descriptor_t
*desc
=
493 (mach_msg_port_descriptor_t
*)(bod
+ 1);
496 printf_unfiltered (_("body: descriptor_count=%u\n"),
497 bod
->msgh_descriptor_count
);
498 data
+= sizeof (mach_msg_body_t
);
499 size
-= sizeof (mach_msg_body_t
);
500 for (k
= 0; k
< bod
->msgh_descriptor_count
; k
++)
501 switch (desc
[k
].type
)
503 case MACH_MSG_PORT_DESCRIPTOR
:
505 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
506 k
, desc
[k
].type
, desc
[k
].name
, desc
[k
].disposition
);
509 printf_unfiltered (_(" descr %d: type=%u\n"),
513 data
+= bod
->msgh_descriptor_count
514 * sizeof (mach_msg_port_descriptor_t
);
515 size
-= bod
->msgh_descriptor_count
516 * sizeof (mach_msg_port_descriptor_t
);
517 ndr
= (NDR_record_t
*)(desc
+ bod
->msgh_descriptor_count
);
519 (_("NDR: mig=%02x if=%02x encod=%02x "
520 "int=%02x char=%02x float=%02x\n"),
521 ndr
->mig_vers
, ndr
->if_vers
, ndr
->mig_encoding
,
522 ndr
->int_rep
, ndr
->char_rep
, ndr
->float_rep
);
523 data
+= sizeof (NDR_record_t
);
524 size
-= sizeof (NDR_record_t
);
527 printf_unfiltered (_(" data:"));
528 ldata
= (const unsigned long *)data
;
529 for (i
= 0; i
< size
/ sizeof (unsigned long); i
++)
530 printf_unfiltered (" %08lx", ldata
[i
]);
531 printf_unfiltered (_("\n"));
536 darwin_decode_exception_message (mach_msg_header_t
*hdr
,
537 struct inferior
**pinf
,
538 darwin_thread_t
**pthread
)
540 mach_msg_body_t
*bod
= (mach_msg_body_t
*)(hdr
+ 1);
541 mach_msg_port_descriptor_t
*desc
= (mach_msg_port_descriptor_t
*)(bod
+ 1);
544 struct inferior
*inf
;
545 darwin_thread_t
*thread
;
547 thread_t thread_port
;
551 /* Check message identifier. 2401 is exc. */
552 if (hdr
->msgh_id
!= 2401)
555 /* Check message header. */
556 if (!(hdr
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
559 /* Check descriptors. */
560 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
561 + sizeof (*ndr
) + 2 * sizeof (integer_t
))
562 || bod
->msgh_descriptor_count
!= 2
563 || desc
[0].type
!= MACH_MSG_PORT_DESCRIPTOR
564 || desc
[0].disposition
!= MACH_MSG_TYPE_MOVE_SEND
565 || desc
[1].type
!= MACH_MSG_PORT_DESCRIPTOR
566 || desc
[1].disposition
!= MACH_MSG_TYPE_MOVE_SEND
)
569 /* Check data representation. */
570 ndr
= (NDR_record_t
*)(desc
+ 2);
571 if (ndr
->mig_vers
!= NDR_PROTOCOL_2_0
572 || ndr
->if_vers
!= NDR_PROTOCOL_2_0
573 || ndr
->mig_encoding
!= NDR_record
.mig_encoding
574 || ndr
->int_rep
!= NDR_record
.int_rep
575 || ndr
->char_rep
!= NDR_record
.char_rep
576 || ndr
->float_rep
!= NDR_record
.float_rep
)
579 /* Ok, the hard work. */
580 data
= (integer_t
*)(ndr
+ 1);
582 /* Find process by port. */
583 task_port
= desc
[1].name
;
584 thread_port
= desc
[0].name
;
585 inf
= darwin_find_inferior_by_task (task_port
);
590 /* Find thread by port. */
591 /* Check for new threads. Do it early so that the port in the exception
592 message can be deallocated. */
593 darwin_check_new_threads (inf
);
595 /* We got new rights to the task and the thread. Get rid of them. */
596 kret
= mach_port_deallocate (mach_task_self (), task_port
);
597 MACH_CHECK_ERROR (kret
);
598 kret
= mach_port_deallocate (mach_task_self (), thread_port
);
599 MACH_CHECK_ERROR (kret
);
601 thread
= darwin_find_thread (inf
, thread_port
);
606 /* Finish decoding. */
607 gdb_assert (thread
->msg_state
== DARWIN_RUNNING
);
608 thread
->event
.header
= *hdr
;
609 thread
->event
.thread_port
= thread_port
;
610 thread
->event
.task_port
= task_port
;
611 thread
->event
.ex_type
= data
[0];
612 thread
->event
.data_count
= data
[1];
614 if (hdr
->msgh_size
< (sizeof (*hdr
) + sizeof (*bod
) + 2 * sizeof (*desc
)
615 + sizeof (*ndr
) + 2 * sizeof (integer_t
)
616 + data
[1] * sizeof (integer_t
)))
618 for (i
= 0; i
< data
[1]; i
++)
619 thread
->event
.ex_data
[i
] = data
[2 + i
];
621 thread
->msg_state
= DARWIN_MESSAGE
;
627 darwin_encode_reply (mig_reply_error_t
*reply
, mach_msg_header_t
*hdr
,
630 mach_msg_header_t
*rh
= &reply
->Head
;
631 rh
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(hdr
->msgh_bits
), 0);
632 rh
->msgh_remote_port
= hdr
->msgh_remote_port
;
633 rh
->msgh_size
= (mach_msg_size_t
)sizeof(mig_reply_error_t
);
634 rh
->msgh_local_port
= MACH_PORT_NULL
;
635 rh
->msgh_id
= hdr
->msgh_id
+ 100;
637 reply
->NDR
= NDR_record
;
638 reply
->RetCode
= code
;
642 darwin_send_reply (struct inferior
*inf
, darwin_thread_t
*thread
)
645 mig_reply_error_t reply
;
647 darwin_encode_reply (&reply
, &thread
->event
.header
, KERN_SUCCESS
);
649 kret
= mach_msg (&reply
.Head
, MACH_SEND_MSG
| MACH_SEND_INTERRUPT
,
650 reply
.Head
.msgh_size
, 0,
651 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
653 MACH_CHECK_ERROR (kret
);
655 inf
->private->pending_messages
--;
659 darwin_resume_thread (struct inferior
*inf
, darwin_thread_t
*thread
,
660 int step
, int nsignal
)
666 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
667 thread
->msg_state
, thread
->gdb_port
, step
, nsignal
);
669 switch (thread
->msg_state
)
672 if (thread
->event
.ex_type
== EXC_SOFTWARE
673 && thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
675 /* Either deliver a new signal or cancel the signal received. */
676 res
= PTRACE (PT_THUPDATE
, inf
->pid
,
677 (void *)(uintptr_t)thread
->gdb_port
, nsignal
);
679 inferior_debug (1, _("ptrace THUP: res=%d\n"), res
);
683 /* Note: ptrace is allowed only if the process is stopped.
684 Directly send the signal to the thread. */
685 res
= syscall (SYS___pthread_kill
, thread
->gdb_port
, nsignal
);
686 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
687 thread
->gdb_port
, nsignal
, res
);
688 thread
->signaled
= 1;
691 /* Set single step. */
692 inferior_debug (4, _("darwin_set_sstep (thread=%x, enable=%d)\n"),
693 thread
->gdb_port
, step
);
694 darwin_set_sstep (thread
->gdb_port
, step
);
695 thread
->single_step
= step
;
697 darwin_send_reply (inf
, thread
);
698 thread
->msg_state
= DARWIN_RUNNING
;
705 kret
= thread_resume (thread
->gdb_port
);
706 MACH_CHECK_ERROR (kret
);
708 thread
->msg_state
= DARWIN_RUNNING
;
713 /* Resume all threads of the inferior. */
716 darwin_resume_inferior_threads (struct inferior
*inf
, int step
, int nsignal
)
718 darwin_thread_t
*thread
;
722 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
724 darwin_resume_thread (inf
, thread
, step
, nsignal
);
727 struct resume_inferior_threads_param
734 darwin_resume_inferior_threads_it (struct inferior
*inf
, void *param
)
736 int step
= ((struct resume_inferior_threads_param
*)param
)->step
;
737 int nsignal
= ((struct resume_inferior_threads_param
*)param
)->nsignal
;
739 darwin_resume_inferior_threads (inf
, step
, nsignal
);
744 /* Suspend all threads of INF. */
747 darwin_suspend_inferior_threads (struct inferior
*inf
)
749 darwin_thread_t
*thread
;
754 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, thread
);
756 switch (thread
->msg_state
)
762 kret
= thread_suspend (thread
->gdb_port
);
763 MACH_CHECK_ERROR (kret
);
764 thread
->msg_state
= DARWIN_STOPPED
;
770 darwin_resume (ptid_t ptid
, int step
, enum target_signal signal
)
772 struct target_waitstatus status
;
778 struct inferior
*inf
;
781 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
782 ptid_get_pid (ptid
), ptid_get_tid (ptid
), step
, signal
);
784 if (signal
== TARGET_SIGNAL_0
)
787 nsignal
= target_signal_to_host (signal
);
789 /* Don't try to single step all threads. */
791 ptid
= inferior_ptid
;
793 /* minus_one_ptid is RESUME_ALL. */
794 if (ptid_equal (ptid
, minus_one_ptid
))
796 struct resume_inferior_threads_param param
;
798 param
.nsignal
= nsignal
;
801 /* Resume threads. */
802 iterate_over_inferiors (darwin_resume_inferior_threads_it
, ¶m
);
804 iterate_over_inferiors (darwin_resume_inferior_it
, NULL
);
808 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (ptid
));
809 long tid
= ptid_get_tid (ptid
);
811 /* Stop the inferior (should be useless). */
812 darwin_suspend_inferior (inf
);
815 darwin_resume_inferior_threads (inf
, step
, nsignal
);
818 darwin_thread_t
*thread
;
820 /* Suspend threads of the task. */
821 darwin_suspend_inferior_threads (inf
);
823 /* Resume the selected thread. */
824 thread
= darwin_find_thread (inf
, tid
);
826 darwin_resume_thread (inf
, thread
, step
, nsignal
);
829 /* Resume the task. */
830 darwin_resume_inferior (inf
);
835 darwin_resume_to (struct target_ops
*ops
, ptid_t ptid
, int step
,
836 enum target_signal signal
)
838 return darwin_resume (ptid
, step
, signal
);
842 darwin_decode_message (mach_msg_header_t
*hdr
,
843 darwin_thread_t
**pthread
,
844 struct inferior
**pinf
,
845 struct target_waitstatus
*status
)
847 darwin_thread_t
*thread
;
848 struct inferior
*inf
;
850 /* Exception message. */
851 if (hdr
->msgh_local_port
== darwin_ex_port
)
855 /* Decode message. */
856 res
= darwin_decode_exception_message (hdr
, &inf
, &thread
);
860 /* Should not happen... */
861 printf_unfiltered (_("darwin_wait: ill-formatted message (id=%x)\n"),
863 /* FIXME: send a failure reply? */
864 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
865 return minus_one_ptid
;
869 inf
->private->pending_messages
++;
871 status
->kind
= TARGET_WAITKIND_STOPPED
;
872 thread
->msg_state
= DARWIN_MESSAGE
;
874 inferior_debug (4, _("darwin_wait: thread=%x, got %s\n"),
876 unparse_exception_type (thread
->event
.ex_type
));
878 switch (thread
->event
.ex_type
)
881 status
->value
.sig
= TARGET_EXC_BAD_ACCESS
;
883 case EXC_BAD_INSTRUCTION
:
884 status
->value
.sig
= TARGET_EXC_BAD_INSTRUCTION
;
887 status
->value
.sig
= TARGET_EXC_ARITHMETIC
;
890 status
->value
.sig
= TARGET_EXC_EMULATION
;
893 if (thread
->event
.ex_data
[0] == EXC_SOFT_SIGNAL
)
896 target_signal_from_host (thread
->event
.ex_data
[1]);
897 inferior_debug (5, _(" (signal %d: %s)\n"),
898 thread
->event
.ex_data
[1],
899 target_signal_to_name (status
->value
.sig
));
901 /* If the thread is stopped because it has received a signal
902 that gdb has just sent, continue. */
903 if (thread
->signaled
)
905 thread
->signaled
= 0;
906 darwin_send_reply (inf
, thread
);
907 thread
->msg_state
= DARWIN_RUNNING
;
908 status
->kind
= TARGET_WAITKIND_IGNORE
;
912 status
->value
.sig
= TARGET_EXC_SOFTWARE
;
915 /* Many internal GDB routines expect breakpoints to be reported
916 as TARGET_SIGNAL_TRAP, and will report TARGET_EXC_BREAKPOINT
917 as a spurious signal. */
918 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
921 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
925 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
931 inf
= darwin_find_inferior_by_notify (hdr
->msgh_local_port
);
934 if (!inf
->private->no_ptrace
)
939 res
= wait4 (inf
->pid
, &wstatus
, 0, NULL
);
940 if (res
< 0 || res
!= inf
->pid
)
942 printf_unfiltered (_("wait4: res=%d: %s\n"),
943 res
, safe_strerror (errno
));
944 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
945 return minus_one_ptid
;
947 if (WIFEXITED (wstatus
))
949 status
->kind
= TARGET_WAITKIND_EXITED
;
950 status
->value
.integer
= WEXITSTATUS (wstatus
);
954 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
955 status
->value
.sig
= WTERMSIG (wstatus
);
958 inferior_debug (4, _("darwin_wait: pid=%d exit, status=%x\n"),
961 /* Looks necessary on Leopard and harmless... */
962 wait4 (inf
->pid
, &wstatus
, 0, NULL
);
964 return ptid_build (inf
->pid
, 0, 0);
968 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf
->pid
);
969 status
->kind
= TARGET_WAITKIND_EXITED
;
970 status
->value
.integer
= 0; /* Don't know. */
971 return ptid_build (inf
->pid
, 0, 0);
975 printf_unfiltered (_("Bad local-port: %x\n"), hdr
->msgh_local_port
);
976 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
977 return minus_one_ptid
;
981 cancel_breakpoint (ptid_t ptid
)
983 /* Arrange for a breakpoint to be hit again later. We will handle
984 the current event, eventually we will resume this thread, and this
985 breakpoint will trap again.
987 If we do not do this, then we run the risk that the user will
988 delete or disable the breakpoint, but the thread will have already
991 struct regcache
*regcache
= get_thread_regcache (ptid
);
992 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
995 pc
= regcache_read_pc (regcache
) - gdbarch_decr_pc_after_break (gdbarch
);
996 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
), pc
))
998 inferior_debug (4, "cancel_breakpoint for thread %x\n",
999 ptid_get_tid (ptid
));
1001 /* Back up the PC if necessary. */
1002 if (gdbarch_decr_pc_after_break (gdbarch
))
1003 regcache_write_pc (regcache
, pc
);
1011 darwin_wait (ptid_t ptid
, struct target_waitstatus
*status
)
1016 mach_msg_header_t hdr
;
1019 mach_msg_header_t
*hdr
= &msgin
.hdr
;
1021 darwin_thread_t
*thread
;
1022 struct inferior
*inf
;
1025 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1026 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
1028 /* Handle fake stop events at first. */
1029 if (darwin_inf_fake_stop
!= NULL
)
1031 inf
= darwin_inf_fake_stop
;
1032 darwin_inf_fake_stop
= NULL
;
1034 status
->kind
= TARGET_WAITKIND_STOPPED
;
1035 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1036 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1037 thread
->msg_state
= DARWIN_STOPPED
;
1038 return ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1043 /* set_sigint_trap (); */
1045 /* Wait for a message. */
1046 kret
= mach_msg (&msgin
.hdr
, MACH_RCV_MSG
| MACH_RCV_INTERRUPT
, 0,
1047 sizeof (msgin
.data
), darwin_port_set
, 0, MACH_PORT_NULL
);
1049 /* clear_sigint_trap (); */
1051 if (kret
== MACH_RCV_INTERRUPTED
)
1053 status
->kind
= TARGET_WAITKIND_IGNORE
;
1054 return minus_one_ptid
;
1057 if (kret
!= MACH_MSG_SUCCESS
)
1059 inferior_debug (5, _("mach_msg: ret=%x\n"), kret
);
1060 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
1061 return minus_one_ptid
;
1064 /* Debug: display message. */
1065 if (darwin_debug_flag
> 10)
1066 darwin_dump_message (hdr
, darwin_debug_flag
> 11);
1068 res
= darwin_decode_message (hdr
, &thread
, &inf
, status
);
1073 while (status
->kind
== TARGET_WAITKIND_IGNORE
);
1075 /* Stop all tasks. */
1076 iterate_over_inferiors (darwin_suspend_inferior_it
, NULL
);
1078 /* Read pending messages. */
1081 struct target_waitstatus status2
;
1084 kret
= mach_msg (&msgin
.hdr
,
1085 MACH_RCV_MSG
| MACH_RCV_TIMEOUT
, 0,
1086 sizeof (msgin
.data
), darwin_port_set
, 1, MACH_PORT_NULL
);
1088 if (kret
== MACH_RCV_TIMED_OUT
)
1090 if (kret
!= MACH_MSG_SUCCESS
)
1093 (5, _("darwin_wait: mach_msg(pending) ret=%x\n"), kret
);
1097 ptid2
= darwin_decode_message (hdr
, &thread
, &inf
, &status2
);
1099 if (inf
!= NULL
&& thread
!= NULL
1100 && thread
->event
.ex_type
== EXC_BREAKPOINT
)
1102 if (thread
->single_step
1103 || cancel_breakpoint (ptid_build (inf
->pid
, 0, thread
->gdb_port
)))
1105 gdb_assert (thread
->msg_state
== DARWIN_MESSAGE
);
1106 darwin_send_reply (inf
, thread
);
1107 thread
->msg_state
= DARWIN_RUNNING
;
1111 (3, _("darwin_wait: thread %x hit a non-gdb breakpoint\n"),
1115 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1121 darwin_wait_to (struct target_ops
*ops
,
1122 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
1124 return darwin_wait (ptid
, status
);
1128 darwin_stop (ptid_t t
)
1130 struct inferior
*inf
= current_inferior ();
1132 /* FIXME: handle in no_ptrace mode. */
1133 gdb_assert (!inf
->private->no_ptrace
);
1134 kill (inf
->pid
, SIGINT
);
1138 darwin_mourn_inferior (struct target_ops
*ops
)
1140 struct inferior
*inf
= current_inferior ();
1145 unpush_target (darwin_ops
);
1147 /* Deallocate threads. */
1148 if (inf
->private->threads
)
1153 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1156 kret
= mach_port_deallocate (gdb_task
, t
->gdb_port
);
1157 MACH_CHECK_ERROR (kret
);
1159 VEC_free (darwin_thread_t
, inf
->private->threads
);
1160 inf
->private->threads
= NULL
;
1163 kret
= mach_port_move_member (gdb_task
,
1164 inf
->private->notify_port
, MACH_PORT_NULL
);
1165 gdb_assert (kret
== KERN_SUCCESS
);
1167 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1168 MACH_NOTIFY_DEAD_NAME
, 0,
1170 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1172 /* This can fail if the task is dead. */
1173 inferior_debug (4, "task=%x, prev=%x, notify_port=%x\n",
1174 inf
->private->task
, prev
, inf
->private->notify_port
);
1176 if (kret
== KERN_SUCCESS
)
1178 kret
= mach_port_deallocate (gdb_task
, prev
);
1179 MACH_CHECK_ERROR (kret
);
1182 kret
= mach_port_destroy (gdb_task
, inf
->private->notify_port
);
1183 MACH_CHECK_ERROR (kret
);
1186 /* Deallocate saved exception ports. */
1187 for (i
= 0; i
< inf
->private->exception_info
.count
; i
++)
1189 kret
= mach_port_deallocate
1190 (gdb_task
, inf
->private->exception_info
.ports
[i
]);
1191 MACH_CHECK_ERROR (kret
);
1193 inf
->private->exception_info
.count
= 0;
1195 kret
= mach_port_deallocate (gdb_task
, inf
->private->task
);
1196 MACH_CHECK_ERROR (kret
);
1198 xfree (inf
->private);
1199 inf
->private = NULL
;
1201 generic_mourn_inferior ();
1205 darwin_reply_to_all_pending_messages (struct inferior
*inf
)
1211 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1214 if (t
->msg_state
== DARWIN_MESSAGE
)
1215 darwin_resume_thread (inf
, t
, 0, 0);
1220 darwin_stop_inferior (struct inferior
*inf
)
1222 struct target_waitstatus wstatus
;
1228 gdb_assert (inf
!= NULL
);
1230 darwin_suspend_inferior (inf
);
1232 darwin_reply_to_all_pending_messages (inf
);
1234 if (inf
->private->no_ptrace
)
1237 res
= kill (inf
->pid
, SIGSTOP
);
1239 warning (_("cannot kill: %s\n"), safe_strerror (errno
));
1241 /* Wait until the process is really stopped. */
1244 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1245 if (wstatus
.kind
== TARGET_WAITKIND_STOPPED
1246 && wstatus
.value
.sig
== TARGET_SIGNAL_STOP
)
1251 static kern_return_t
1252 darwin_save_exception_ports (darwin_inferior
*inf
)
1256 inf
->exception_info
.count
=
1257 sizeof (inf
->exception_info
.ports
) / sizeof (inf
->exception_info
.ports
[0]);
1259 kret
= task_get_exception_ports
1260 (inf
->task
, EXC_MASK_ALL
, inf
->exception_info
.masks
,
1261 &inf
->exception_info
.count
, inf
->exception_info
.ports
,
1262 inf
->exception_info
.behaviors
, inf
->exception_info
.flavors
);
1266 static kern_return_t
1267 darwin_restore_exception_ports (darwin_inferior
*inf
)
1272 for (i
= 0; i
< inf
->exception_info
.count
; i
++)
1274 kret
= task_set_exception_ports
1275 (inf
->task
, inf
->exception_info
.masks
[i
], inf
->exception_info
.ports
[i
],
1276 inf
->exception_info
.behaviors
[i
], inf
->exception_info
.flavors
[i
]);
1277 if (kret
!= KERN_SUCCESS
)
1281 return KERN_SUCCESS
;
1285 darwin_kill_inferior (struct target_ops
*ops
)
1287 struct inferior
*inf
= current_inferior ();
1288 struct target_waitstatus wstatus
;
1294 if (ptid_equal (inferior_ptid
, null_ptid
))
1297 gdb_assert (inf
!= NULL
);
1299 if (!inf
->private->no_ptrace
)
1301 darwin_stop_inferior (inf
);
1303 res
= PTRACE (PT_KILL
, inf
->pid
, 0, 0);
1304 gdb_assert (res
== 0);
1306 darwin_reply_to_all_pending_messages (inf
);
1308 darwin_resume_inferior (inf
);
1310 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1314 kret
= darwin_restore_exception_ports (inf
->private);
1315 MACH_CHECK_ERROR (kret
);
1317 darwin_reply_to_all_pending_messages (inf
);
1319 darwin_resume_inferior (inf
);
1321 res
= kill (inf
->pid
, 9);
1323 ptid
= darwin_wait (inferior_ptid
, &wstatus
);
1326 target_mourn_inferior ();
1330 darwin_attach_pid (struct inferior
*inf
)
1333 mach_port_t prev_port
;
1335 mach_port_t prev_not
;
1336 exception_mask_t mask
;
1338 inf
->private = XZALLOC (darwin_inferior
);
1340 kret
= task_for_pid (gdb_task
, inf
->pid
, &inf
->private->task
);
1341 if (kret
!= KERN_SUCCESS
)
1345 if (!inf
->attach_flag
)
1348 waitpid (inf
->pid
, &status
, 0);
1351 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
1352 " (please check gdb is codesigned - see taskgated(8))"),
1353 inf
->pid
, mach_error_string (kret
), (unsigned long) kret
);
1356 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1357 inf
->private->task
, inf
->pid
);
1359 if (darwin_ex_port
== MACH_PORT_NULL
)
1361 /* Create a port to get exceptions. */
1362 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1364 gdb_assert (kret
== KERN_SUCCESS
);
1366 kret
= mach_port_insert_right (gdb_task
, darwin_ex_port
, darwin_ex_port
,
1367 MACH_MSG_TYPE_MAKE_SEND
);
1368 gdb_assert (kret
== KERN_SUCCESS
);
1370 /* Create a port set and put ex_port in it. */
1371 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_PORT_SET
,
1373 gdb_assert (kret
== KERN_SUCCESS
);
1375 kret
= mach_port_move_member (gdb_task
, darwin_ex_port
, darwin_port_set
);
1376 gdb_assert (kret
== KERN_SUCCESS
);
1379 /* Create a port to be notified when the child task terminates. */
1380 kret
= mach_port_allocate (gdb_task
, MACH_PORT_RIGHT_RECEIVE
,
1381 &inf
->private->notify_port
);
1382 gdb_assert (kret
== KERN_SUCCESS
);
1384 kret
= mach_port_move_member (gdb_task
,
1385 inf
->private->notify_port
, darwin_port_set
);
1386 gdb_assert (kret
== KERN_SUCCESS
);
1388 kret
= mach_port_request_notification (gdb_task
, inf
->private->task
,
1389 MACH_NOTIFY_DEAD_NAME
, 0,
1390 inf
->private->notify_port
,
1391 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
1393 gdb_assert (kret
== KERN_SUCCESS
);
1394 gdb_assert (prev_not
== MACH_PORT_NULL
);
1396 kret
= darwin_save_exception_ports (inf
->private);
1397 gdb_assert (kret
== KERN_SUCCESS
);
1399 /* Set exception port. */
1400 if (enable_mach_exceptions
)
1401 mask
= EXC_MASK_ALL
;
1403 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1404 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1405 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1406 gdb_assert (kret
== KERN_SUCCESS
);
1408 push_target (darwin_ops
);
1412 darwin_init_thread_list (struct inferior
*inf
)
1414 darwin_thread_t
*thread
;
1417 darwin_check_new_threads (inf
);
1419 gdb_assert (inf
->private->threads
1420 && VEC_length (darwin_thread_t
, inf
->private->threads
) > 0);
1421 thread
= VEC_index (darwin_thread_t
, inf
->private->threads
, 0);
1423 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1425 new_ptid
= ptid_build (inf
->pid
, 0, thread
->gdb_port
);
1426 thread_change_ptid (inferior_ptid
, new_ptid
);
1427 inferior_ptid
= new_ptid
;
1430 /* The child must synchronize with gdb: gdb must set the exception port
1431 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1432 FIXME: is there a lighter way ? */
1433 static int ptrace_fds
[2];
1436 darwin_ptrace_me (void)
1441 /* Close write end point. */
1442 close (ptrace_fds
[1]);
1444 /* Wait until gdb is ready. */
1445 res
= read (ptrace_fds
[0], &c
, 1);
1446 gdb_assert (res
== 0);
1447 close (ptrace_fds
[0]);
1449 /* Get rid of privileges. */
1450 setegid (getgid ());
1453 PTRACE (PT_TRACE_ME
, 0, 0, 0);
1455 /* Redirect signals to exception port. */
1456 PTRACE (PT_SIGEXC
, 0, 0, 0);
1459 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1461 darwin_pre_ptrace (void)
1463 if (pipe (ptrace_fds
) != 0)
1467 error (_("unable to create a pipe: %s"), safe_strerror (errno
));
1472 darwin_ptrace_him (int pid
)
1476 mach_port_t prev_port
;
1478 struct inferior
*inf
= current_inferior ();
1480 darwin_attach_pid (inf
);
1482 /* Let's the child run. */
1483 close (ptrace_fds
[0]);
1484 close (ptrace_fds
[1]);
1486 darwin_init_thread_list (inf
);
1488 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
1492 darwin_create_inferior (struct target_ops
*ops
, char *exec_file
,
1493 char *allargs
, char **env
, int from_tty
)
1495 /* Do the hard work. */
1496 fork_inferior (exec_file
, allargs
, env
, darwin_ptrace_me
, darwin_ptrace_him
,
1497 darwin_pre_ptrace
, NULL
);
1499 /* Return now in case of error. */
1500 if (ptid_equal (inferior_ptid
, null_ptid
))
1505 /* Attach to process PID, then initialize for debugging it
1506 and wait for the trace-trap that results from attaching. */
1508 darwin_attach (struct target_ops
*ops
, char *args
, int from_tty
)
1514 struct inferior
*inf
;
1517 pid
= parse_pid_to_attach (args
);
1519 if (pid
== getpid ()) /* Trying to masturbate? */
1520 error (_("I refuse to debug myself!"));
1524 char *exec_file
= get_exec_file (0);
1527 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
1528 target_pid_to_str (pid_to_ptid (pid
)));
1530 printf_unfiltered (_("Attaching to %s\n"),
1531 target_pid_to_str (pid_to_ptid (pid
)));
1533 gdb_flush (gdb_stdout
);
1536 if (pid
== 0 || kill (pid
, 0) < 0)
1537 error (_("Can't attach to process %d: %s (%d)"),
1538 pid
, safe_strerror (errno
), errno
);
1540 inferior_ptid
= pid_to_ptid (pid
);
1541 inf
= current_inferior ();
1542 inferior_appeared (inf
, pid
);
1543 inf
->attach_flag
= 1;
1545 /* Always add a main thread. */
1546 add_thread_silent (inferior_ptid
);
1548 darwin_attach_pid (inf
);
1550 darwin_suspend_inferior (inf
);
1552 darwin_init_thread_list (inf
);
1554 darwin_check_osabi (inf
->private, ptid_get_tid (inferior_ptid
));
1556 gdb_assert (darwin_inf_fake_stop
== NULL
);
1557 darwin_inf_fake_stop
= inf
;
1558 inf
->private->no_ptrace
= 1;
1561 /* Take a program previously attached to and detaches it.
1562 The program resumes execution and will no longer stop
1563 on signals, etc. We'd better not have left any breakpoints
1564 in the program or it'll die when it hits one. For this
1565 to work, it may be necessary for the process to have been
1566 previously attached. It *might* work if the program was
1567 started via fork. */
1569 darwin_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1571 pid_t pid
= ptid_get_pid (inferior_ptid
);
1572 struct inferior
*inf
= current_inferior ();
1576 /* Display message. */
1579 char *exec_file
= get_exec_file (0);
1582 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
1583 target_pid_to_str (pid_to_ptid (pid
)));
1584 gdb_flush (gdb_stdout
);
1587 /* If ptrace() is in use, stop the process. */
1588 if (!inf
->private->no_ptrace
)
1589 darwin_stop_inferior (inf
);
1591 kret
= darwin_restore_exception_ports (inf
->private);
1592 MACH_CHECK_ERROR (kret
);
1594 if (!inf
->private->no_ptrace
)
1596 res
= PTRACE (PT_DETACH
, inf
->pid
, 0, 0);
1598 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1599 inf
->pid
, safe_strerror (errno
), errno
);
1602 darwin_reply_to_all_pending_messages (inf
);
1604 darwin_resume_inferior (inf
);
1606 darwin_mourn_inferior (ops
);
1610 darwin_files_info (struct target_ops
*ops
)
1615 darwin_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1617 static char buf
[80];
1618 long tid
= ptid_get_tid (ptid
);
1622 snprintf (buf
, sizeof (buf
), _("Thread 0x%lx of process %u"),
1623 tid
, ptid_get_pid (ptid
));
1627 return normal_pid_to_str (ptid
);
1631 darwin_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1636 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1637 copy it to RDADDR in gdb's address space.
1638 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1639 to ADDR in inferior task's address space.
1640 Return 0 on failure; number of bytes read / writen otherwise. */
1642 darwin_read_write_inferior (task_t task
, CORE_ADDR addr
,
1643 char *rdaddr
, const char *wraddr
, int length
)
1646 mach_vm_address_t offset
= addr
& (mach_page_size
- 1);
1647 mach_vm_address_t low_address
= (mach_vm_address_t
) (addr
- offset
);
1648 mach_vm_size_t aligned_length
= (mach_vm_size_t
) PAGE_ROUND (offset
+ length
);
1651 mach_vm_size_t remaining_length
;
1652 mach_vm_address_t region_address
;
1653 mach_vm_size_t region_length
;
1655 inferior_debug (8, _("darwin_read_write_inferior(task=%x, %s, len=%d)\n"),
1656 task
, core_addr_to_string (addr
), length
);
1658 /* Get memory from inferior with page aligned addresses */
1659 kret
= mach_vm_read (task
, low_address
, aligned_length
,
1660 &copied
, ©_count
);
1661 if (kret
!= KERN_SUCCESS
)
1664 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1665 core_addr_to_string (addr
), mach_error_string (kret
));
1670 memcpy (rdaddr
, (char *)copied
+ offset
, length
);
1675 memcpy ((char *)copied
+ offset
, wraddr
, length
);
1677 /* Do writes atomically.
1678 First check for holes and unwritable memory. */
1679 for (region_address
= low_address
, remaining_length
= aligned_length
;
1680 region_address
< low_address
+ aligned_length
;
1681 region_address
+= region_length
, remaining_length
-= region_length
)
1683 vm_region_submap_short_info_data_64_t info
;
1684 mach_vm_address_t region_start
= region_address
;
1685 mach_msg_type_number_t count
;
1686 natural_t region_depth
;
1688 region_depth
= 100000;
1689 count
= VM_REGION_SUBMAP_SHORT_INFO_COUNT_64
;
1690 kret
= mach_vm_region_recurse
1691 (task
, ®ion_start
, ®ion_length
, ®ion_depth
,
1692 (vm_region_recurse_info_t
) &info
, &count
);
1694 if (kret
!= KERN_SUCCESS
)
1696 inferior_debug (1, _("darwin_read_write_inferior: "
1697 "mach_vm_region_recurse failed at %s: %s\n"),
1698 core_addr_to_string (region_address
),
1699 mach_error_string (kret
));
1704 (9, _("darwin_read_write_inferior: "
1705 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1706 core_addr_to_string (region_address
),
1707 core_addr_to_string (region_start
),
1708 core_addr_to_string (region_length
));
1710 /* Check for holes in memory */
1711 if (region_start
> region_address
)
1713 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
1714 core_addr_to_string (region_address
),
1715 core_addr_to_string (region_start
),
1716 (unsigned)region_length
);
1721 /* Adjust the length. */
1722 region_length
-= (region_address
- region_start
);
1724 if (!(info
.max_protection
& VM_PROT_WRITE
))
1726 kret
= mach_vm_protect
1727 (task
, region_address
, region_length
,
1728 TRUE
, info
.max_protection
| VM_PROT_WRITE
| VM_PROT_COPY
);
1729 if (kret
!= KERN_SUCCESS
)
1731 warning (_("darwin_read_write_inf: "
1732 "mach_vm_protect max failed at %s: %s"),
1733 core_addr_to_string (region_address
),
1734 mach_error_string (kret
));
1740 if (!(info
.protection
& VM_PROT_WRITE
))
1742 kret
= mach_vm_protect (task
, region_address
, region_length
,
1743 FALSE
, info
.protection
| VM_PROT_WRITE
);
1744 if (kret
!= KERN_SUCCESS
)
1746 warning (_("darwin_read_write_inf: "
1747 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1748 core_addr_to_string (region_address
),
1749 (unsigned long)region_length
, mach_error_string (kret
));
1756 kret
= mach_vm_write (task
, low_address
, copied
, aligned_length
);
1758 if (kret
!= KERN_SUCCESS
)
1760 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
1761 mach_error_string (kret
));
1765 mach_vm_deallocate (mach_task_self (), copied
, copy_count
);
1770 /* Return 0 on failure, number of bytes handled otherwise. TARGET
1773 darwin_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
, int write
,
1774 struct mem_attrib
*attrib
, struct target_ops
*target
)
1776 struct inferior
*inf
= current_inferior ();
1777 task_t task
= inf
->private->task
;
1779 if (task
== MACH_PORT_NULL
)
1782 inferior_debug (8, _("darwin_xfer_memory(%s, %d, %c)\n"),
1783 core_addr_to_string (memaddr
), len
, write
? 'w' : 'r');
1786 return darwin_read_write_inferior (task
, memaddr
, NULL
, myaddr
, len
);
1788 return darwin_read_write_inferior (task
, memaddr
, myaddr
, NULL
, len
);
1792 darwin_xfer_partial (struct target_ops
*ops
,
1793 enum target_object object
, const char *annex
,
1794 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1795 ULONGEST offset
, LONGEST len
)
1797 struct inferior
*inf
= current_inferior ();
1800 (8, _("darwin_xfer_partial(%s, %d, rbuf=%s, wbuf=%s) pid=%u\n"),
1801 core_addr_to_string (offset
), (int)len
,
1802 host_address_to_string (readbuf
), host_address_to_string (writebuf
),
1805 if (object
!= TARGET_OBJECT_MEMORY
)
1808 return darwin_read_write_inferior (inf
->private->task
, offset
,
1809 readbuf
, writebuf
, len
);
1813 set_enable_mach_exceptions (char *args
, int from_tty
,
1814 struct cmd_list_element
*c
)
1816 if (!ptid_equal (inferior_ptid
, null_ptid
))
1818 struct inferior
*inf
= current_inferior ();
1819 exception_mask_t mask
;
1822 if (enable_mach_exceptions
)
1823 mask
= EXC_MASK_ALL
;
1826 darwin_restore_exception_ports (inf
->private);
1827 mask
= EXC_MASK_SOFTWARE
| EXC_MASK_BREAKPOINT
;
1829 kret
= task_set_exception_ports (inf
->private->task
, mask
, darwin_ex_port
,
1830 EXCEPTION_DEFAULT
, THREAD_STATE_NONE
);
1831 MACH_CHECK_ERROR (kret
);
1836 darwin_pid_to_exec_file (int pid
)
1841 path
= xmalloc (MAXPATHLEN
);
1842 make_cleanup (xfree
, path
);
1844 res
= proc_pidinfo (pid
, PROC_PIDPATHINFO
, 0, path
, MAXPATHLEN
);
1852 darwin_get_ada_task_ptid (long lwp
, long thread
)
1857 struct inferior
*inf
= current_inferior ();
1859 mach_port_name_array_t names
;
1860 mach_msg_type_number_t names_count
;
1861 mach_port_type_array_t types
;
1862 mach_msg_type_number_t types_count
;
1865 /* First linear search. */
1867 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1869 if (t
->inf_port
== lwp
)
1870 return ptid_build (ptid_get_pid (inferior_ptid
), 0, t
->gdb_port
);
1872 /* Maybe the port was never extract. Do it now. */
1874 /* First get inferior port names. */
1875 kret
= mach_port_names (inf
->private->task
, &names
, &names_count
, &types
,
1877 MACH_CHECK_ERROR (kret
);
1878 if (kret
!= KERN_SUCCESS
)
1881 /* For each name, copy the right in the gdb space and then compare with
1882 our view of the inferior threads. We don't forget to deallocate the
1884 for (i
= 0; i
< names_count
; i
++)
1886 mach_port_t local_name
;
1887 mach_msg_type_name_t local_type
;
1889 /* We just need to know the corresponding name in gdb name space.
1890 So extract and deallocate the right. */
1891 kret
= mach_port_extract_right (inf
->private->task
, names
[i
],
1892 MACH_MSG_TYPE_COPY_SEND
,
1893 &local_name
, &local_type
);
1894 if (kret
!= KERN_SUCCESS
)
1896 mach_port_deallocate (gdb_task
, local_name
);
1899 VEC_iterate (darwin_thread_t
, inf
->private->threads
, k
, t
);
1901 if (t
->gdb_port
== local_name
)
1903 t
->inf_port
= names
[i
];
1904 if (names
[i
] == lwp
)
1909 vm_deallocate (gdb_task
, (vm_address_t
) names
,
1910 names_count
* sizeof (mach_port_t
));
1913 return ptid_build (ptid_get_pid (inferior_ptid
), 0, res
);
1919 darwin_supports_multi_process (void)
1925 _initialize_darwin_inferior (void)
1929 gdb_task
= mach_task_self ();
1930 darwin_host_self
= mach_host_self ();
1932 /* Read page size. */
1933 kret
= host_page_size (darwin_host_self
, &mach_page_size
);
1934 if (kret
!= KERN_SUCCESS
)
1936 mach_page_size
= 0x1000;
1937 MACH_CHECK_ERROR (kret
);
1940 darwin_ops
= inf_child_target ();
1942 darwin_ops
->to_shortname
= "darwin-child";
1943 darwin_ops
->to_longname
= _("Darwin child process");
1944 darwin_ops
->to_doc
=
1945 _("Darwin child process (started by the \"run\" command).");
1946 darwin_ops
->to_create_inferior
= darwin_create_inferior
;
1947 darwin_ops
->to_attach
= darwin_attach
;
1948 darwin_ops
->to_attach_no_wait
= 0;
1949 darwin_ops
->to_detach
= darwin_detach
;
1950 darwin_ops
->to_files_info
= darwin_files_info
;
1951 darwin_ops
->to_wait
= darwin_wait_to
;
1952 darwin_ops
->to_mourn_inferior
= darwin_mourn_inferior
;
1953 darwin_ops
->to_kill
= darwin_kill_inferior
;
1954 darwin_ops
->to_stop
= darwin_stop
;
1955 darwin_ops
->to_resume
= darwin_resume_to
;
1956 darwin_ops
->to_thread_alive
= darwin_thread_alive
;
1957 darwin_ops
->to_pid_to_str
= darwin_pid_to_str
;
1958 darwin_ops
->to_pid_to_exec_file
= darwin_pid_to_exec_file
;
1959 darwin_ops
->to_load
= NULL
;
1960 darwin_ops
->deprecated_xfer_memory
= darwin_xfer_memory
;
1961 darwin_ops
->to_xfer_partial
= darwin_xfer_partial
;
1962 darwin_ops
->to_supports_multi_process
= darwin_supports_multi_process
;
1963 darwin_ops
->to_get_ada_task_ptid
= darwin_get_ada_task_ptid
;
1965 darwin_complete_target (darwin_ops
);
1967 add_target (darwin_ops
);
1969 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
1972 add_setshow_zinteger_cmd ("darwin", class_obscure
,
1973 &darwin_debug_flag
, _("\
1974 Set if printing inferior communication debugging statements."), _("\
1975 Show if printing inferior communication debugging statements."), NULL
,
1977 &setdebuglist
, &showdebuglist
);
1979 add_setshow_boolean_cmd ("mach-exceptions", class_support
,
1980 &enable_mach_exceptions
, _("\
1981 Set if mach exceptions are caught."), _("\
1982 Show if mach exceptions are caught."), _("\
1983 When this mode is on, all low level exceptions are reported before being\n\
1984 reported by the kernel."),
1985 &set_enable_mach_exceptions
, NULL
,
1986 &setlist
, &showlist
);