]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/darwin-nat.c
2012-04-11 Tristan Gingold <gingold@adacore.com>
[thirdparty/binutils-gdb.git] / gdb / darwin-nat.c
CommitLineData
a80b95ba 1/* Darwin support for GDB, the GNU debugger.
0b302171 2 Copyright (C) 2008-2012 Free Software Foundation, Inc.
a80b95ba
TG
3
4 Contributed by AdaCore.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>.
20*/
21
22#include "defs.h"
23#include "top.h"
24#include "inferior.h"
25#include "target.h"
26#include "symfile.h"
27#include "symtab.h"
28#include "objfiles.h"
29#include "gdb.h"
30#include "gdbcmd.h"
31#include "gdbcore.h"
32#include "gdbthread.h"
33#include "regcache.h"
34#include "event-top.h"
35#include "inf-loop.h"
36#include "gdb_stat.h"
37#include "exceptions.h"
38#include "inf-child.h"
39#include "value.h"
40#include "arch-utils.h"
41#include "bfd.h"
ece0061f 42#include "bfd/mach-o.h"
a80b95ba
TG
43
44#include <sys/ptrace.h>
45#include <sys/signal.h>
46#include <machine/setjmp.h>
47#include <sys/types.h>
48#include <unistd.h>
49#include <signal.h>
50#include <string.h>
51#include <ctype.h>
52#include <sys/param.h>
53#include <sys/sysctl.h>
54#include <sys/proc.h>
bb00b29d
TG
55#include <libproc.h>
56#include <sys/syscall.h>
e69860f1 57#include <spawn.h>
a80b95ba
TG
58
59#include <mach/mach_error.h>
60#include <mach/mach_vm.h>
61#include <mach/mach_init.h>
62#include <mach/vm_map.h>
63#include <mach/task.h>
64#include <mach/mach_port.h>
65#include <mach/thread_act.h>
66#include <mach/port.h>
67
68#include "darwin-nat.h"
69
70/* Quick overview.
71 Darwin kernel is Mach + BSD derived kernel. Note that they share the
72 same memory space and are linked together (ie there is no micro-kernel).
73
74 Although ptrace(2) is available on Darwin, it is not complete. We have
75 to use Mach calls to read and write memory and to modify registers. We
76 also use Mach to get inferior faults. As we cannot use select(2) or
77 signals with Mach port (the Mach communication channel), signals are
78 reported to gdb as an exception. Furthermore we detect death of the
79 inferior through a Mach notification message. This way we only wait
80 on Mach ports.
81
82 Some Mach documentation is available for Apple xnu source package or
83 from the web. */
84
85
86#define PTRACE(CMD, PID, ADDR, SIG) \
87 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG))
88
89extern boolean_t exc_server (mach_msg_header_t *in, mach_msg_header_t *out);
90
91static void darwin_stop (ptid_t);
92
bb00b29d
TG
93static void darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step,
94 enum target_signal signal);
95static void darwin_resume (ptid_t ptid, int step,
96 enum target_signal signal);
97
98static ptid_t darwin_wait_to (struct target_ops *ops, ptid_t ptid,
99 struct target_waitstatus *status, int options);
100static ptid_t darwin_wait (ptid_t ptid, struct target_waitstatus *status);
101
a80b95ba
TG
102static void darwin_mourn_inferior (struct target_ops *ops);
103
2ab1c2d9 104static void darwin_kill_inferior (struct target_ops *ops);
a80b95ba
TG
105
106static void darwin_ptrace_me (void);
107
108static void darwin_ptrace_him (int pid);
109
110static void darwin_create_inferior (struct target_ops *ops, char *exec_file,
111 char *allargs, char **env, int from_tty);
112
113static void darwin_files_info (struct target_ops *ops);
114
bb00b29d
TG
115static char *darwin_pid_to_str (struct target_ops *ops, ptid_t tpid);
116
117static int darwin_thread_alive (struct target_ops *ops, ptid_t tpid);
a80b95ba
TG
118
119/* Target operations for Darwin. */
120static struct target_ops *darwin_ops;
121
122/* Task identifier of gdb. */
123static task_t gdb_task;
124
125/* A copy of mach_host_self (). */
126mach_port_t darwin_host_self;
127
128/* Exception port. */
129mach_port_t darwin_ex_port;
130
a80b95ba
TG
131/* Port set. */
132mach_port_t darwin_port_set;
133
0963b4bd 134/* Page size. */
a80b95ba
TG
135static vm_size_t mach_page_size;
136
137/* If Set, catch all mach exceptions (before they are converted to signals
138 by the kernel). */
139static int enable_mach_exceptions;
140
bb00b29d
TG
141/* Inferior that should report a fake stop event. */
142static struct inferior *darwin_inf_fake_stop;
143
a80b95ba
TG
144#define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1))
145#define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1)
146
bb00b29d 147/* This controls output of inferior debugging. */
a80b95ba
TG
148static int darwin_debug_flag = 0;
149
15c19d39
TG
150/* Create a __TEXT __info_plist section in the executable so that gdb could
151 be signed. This is required to get an authorization for task_for_pid.
152
153 Once gdb is built, you can either:
154 * make it setgid procmod
155 * or codesign it with any system-trusted signing authority.
156 See taskgated(8) for details. */
157static const unsigned char info_plist[]
158__attribute__ ((section ("__TEXT,__info_plist"),used)) =
159 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
160 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
161 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
162 "<plist version=\"1.0\">\n"
163 "<dict>\n"
164 " <key>CFBundleIdentifier</key>\n"
165 " <string>org.gnu.gdb</string>\n"
166 " <key>CFBundleName</key>\n"
167 " <string>gdb</string>\n"
168 " <key>CFBundleVersion</key>\n"
169 " <string>1.0</string>\n"
170 " <key>SecTaskAccess</key>\n"
171 " <array>\n"
172 " <string>allowed</string>\n"
173 " <string>debug</string>\n"
174 " </array>\n"
175 "</dict>\n"
176 "</plist>\n";
177
a80b95ba
TG
178static void
179inferior_debug (int level, const char *fmt, ...)
180{
181 va_list ap;
182
183 if (darwin_debug_flag < level)
184 return;
185
186 va_start (ap, fmt);
187 printf_unfiltered (_("[%d inferior]: "), getpid ());
188 vprintf_unfiltered (fmt, ap);
189 va_end (ap);
190}
191
192void
193mach_check_error (kern_return_t ret, const char *file,
194 unsigned int line, const char *func)
195{
196 if (ret == KERN_SUCCESS)
197 return;
198 if (func == NULL)
199 func = _("[UNKNOWN]");
bb00b29d 200
4f1cdeec 201 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"),
bb00b29d 202 file, line, func, mach_error_string (ret), (unsigned long) ret);
a80b95ba
TG
203}
204
205static const char *
206unparse_exception_type (unsigned int i)
207{
208 static char unknown_exception_buf[32];
209
210 switch (i)
211 {
212 case EXC_BAD_ACCESS:
213 return "EXC_BAD_ACCESS";
214 case EXC_BAD_INSTRUCTION:
215 return "EXC_BAD_INSTRUCTION";
216 case EXC_ARITHMETIC:
217 return "EXC_ARITHMETIC";
218 case EXC_EMULATION:
219 return "EXC_EMULATION";
220 case EXC_SOFTWARE:
221 return "EXC_SOFTWARE";
222 case EXC_BREAKPOINT:
223 return "EXC_BREAKPOINT";
224 case EXC_SYSCALL:
225 return "EXC_SYSCALL";
226 case EXC_MACH_SYSCALL:
227 return "EXC_MACH_SYSCALL";
228 case EXC_RPC_ALERT:
229 return "EXC_RPC_ALERT";
230 case EXC_CRASH:
231 return "EXC_CRASH";
232 default:
233 snprintf (unknown_exception_buf, 32, _("unknown (%d)"), i);
234 return unknown_exception_buf;
235 }
236}
237
a7aa0d73
JB
238/* Set errno to zero, and then call ptrace with the given arguments.
239 If inferior debugging traces are on, then also print a debug
240 trace.
241
242 The returned value is the same as the value returned by ptrace,
243 except in the case where that value is -1 but errno is zero.
244 This case is documented to be a non-error situation, so we
245 return zero in that case. */
246
a80b95ba
TG
247static int
248darwin_ptrace (const char *name,
249 int request, int pid, PTRACE_TYPE_ARG3 arg3, int arg4)
250{
251 int ret;
252
a7aa0d73 253 errno = 0;
a80b95ba 254 ret = ptrace (request, pid, (caddr_t) arg3, arg4);
a7aa0d73
JB
255 if (ret == -1 && errno == 0)
256 ret = 0;
a80b95ba 257
bb00b29d 258 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"),
a80b95ba 259 name, pid, arg3, arg4, ret,
a3224241 260 (ret != 0) ? safe_strerror (errno) : _("no error"));
a80b95ba
TG
261 return ret;
262}
263
264static int
265cmp_thread_t (const void *l, const void *r)
266{
bb00b29d
TG
267 thread_t tl = *(const thread_t *)l;
268 thread_t tr = *(const thread_t *)r;
269 return (int)(tl - tr);
a80b95ba
TG
270}
271
272static void
bb00b29d 273darwin_check_new_threads (struct inferior *inf)
a80b95ba
TG
274{
275 kern_return_t kret;
276 unsigned int i;
277 thread_array_t thread_list;
278 unsigned int new_nbr;
279 unsigned int old_nbr;
280 unsigned int new_ix, old_ix;
bb00b29d
TG
281 darwin_inferior *darwin_inf = inf->private;
282 VEC (darwin_thread_t) *thread_vec;
a80b95ba
TG
283
284 /* Get list of threads. */
bb00b29d 285 kret = task_threads (darwin_inf->task, &thread_list, &new_nbr);
a80b95ba
TG
286 MACH_CHECK_ERROR (kret);
287 if (kret != KERN_SUCCESS)
288 return;
289
bb00b29d 290 /* Sort the list. */
a80b95ba
TG
291 if (new_nbr > 1)
292 qsort (thread_list, new_nbr, sizeof (thread_t), cmp_thread_t);
293
bb00b29d
TG
294 if (darwin_inf->threads)
295 old_nbr = VEC_length (darwin_thread_t, darwin_inf->threads);
a80b95ba
TG
296 else
297 old_nbr = 0;
298
bb00b29d
TG
299 /* Quick check for no changes. */
300 if (old_nbr == new_nbr)
301 {
302 for (i = 0; i < new_nbr; i++)
303 if (thread_list[i]
304 != VEC_index (darwin_thread_t, darwin_inf->threads, i)->gdb_port)
305 break;
306 if (i == new_nbr)
307 {
308 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
309 new_nbr * sizeof (int));
310 MACH_CHECK_ERROR (kret);
311 return;
312 }
313 }
314
315 thread_vec = VEC_alloc (darwin_thread_t, new_nbr);
316
a80b95ba
TG
317 for (new_ix = 0, old_ix = 0; new_ix < new_nbr || old_ix < old_nbr;)
318 {
319 thread_t new_id = (new_ix < new_nbr) ?
320 thread_list[new_ix] : THREAD_NULL;
bb00b29d
TG
321 darwin_thread_t *old = (old_ix < old_nbr) ?
322 VEC_index (darwin_thread_t, darwin_inf->threads, old_ix) : NULL;
323 thread_t old_id = old ? old->gdb_port : THREAD_NULL;
324
325 inferior_debug
326 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:%x old_id:%x\n"),
327 new_ix, new_nbr, old_ix, old_nbr, new_id, old_id);
a80b95ba
TG
328
329 if (old_id == new_id)
330 {
331 /* Thread still exist. */
bb00b29d 332 VEC_safe_push (darwin_thread_t, thread_vec, old);
a80b95ba
TG
333 new_ix++;
334 old_ix++;
335
336 kret = mach_port_deallocate (gdb_task, old_id);
337 MACH_CHECK_ERROR (kret);
338 continue;
339 }
bb00b29d
TG
340 if (new_ix < new_nbr && new_id == MACH_PORT_DEAD)
341 {
342 /* Ignore dead ports.
343 In some weird cases, we might get dead ports. They should
344 correspond to dead thread so they could safely be ignored. */
345 new_ix++;
346 continue;
347 }
348 if (new_ix < new_nbr && (old_ix == old_nbr || new_id < old_id))
a80b95ba
TG
349 {
350 /* A thread was created. */
351 struct thread_info *tp;
bb00b29d 352 struct private_thread_info *pti;
a80b95ba 353
bb00b29d
TG
354 pti = XZALLOC (struct private_thread_info);
355 pti->gdb_port = new_id;
356 pti->msg_state = DARWIN_RUNNING;
357
358 /* Add a new thread unless this is the first one ever met. */
359 if (!(old_nbr == 0 && new_ix == 0))
360 tp = add_thread_with_info (ptid_build (inf->pid, 0, new_id), pti);
361 else
362 {
363 tp = find_thread_ptid (ptid_build (inf->pid, 0, 0));
364 gdb_assert (tp);
365 tp->private = pti;
366 }
367 VEC_safe_push (darwin_thread_t, thread_vec, pti);
a80b95ba
TG
368 new_ix++;
369 continue;
370 }
bb00b29d 371 if (old_ix < old_nbr && (new_ix == new_nbr || new_id > old_id))
a80b95ba
TG
372 {
373 /* A thread was removed. */
374 delete_thread (ptid_build (inf->pid, 0, old_id));
375 kret = mach_port_deallocate (gdb_task, old_id);
376 MACH_CHECK_ERROR (kret);
377 old_ix++;
bb00b29d 378 continue;
a80b95ba 379 }
f3574227 380 gdb_assert_not_reached ("unexpected thread case");
a80b95ba
TG
381 }
382
bb00b29d
TG
383 if (darwin_inf->threads)
384 VEC_free (darwin_thread_t, darwin_inf->threads);
385 darwin_inf->threads = thread_vec;
a80b95ba
TG
386
387 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list,
388 new_nbr * sizeof (int));
389 MACH_CHECK_ERROR (kret);
390}
391
bb00b29d
TG
392static int
393find_inferior_task_it (struct inferior *inf, void *port_ptr)
a80b95ba 394{
bb00b29d
TG
395 return inf->private->task == *(task_t*)port_ptr;
396}
a80b95ba 397
bb00b29d
TG
398static int
399find_inferior_notify_it (struct inferior *inf, void *port_ptr)
400{
401 return inf->private->notify_port == *(task_t*)port_ptr;
a80b95ba
TG
402}
403
bb00b29d
TG
404/* Return an inferior by task port. */
405static struct inferior *
406darwin_find_inferior_by_task (task_t port)
a80b95ba 407{
bb00b29d
TG
408 return iterate_over_inferiors (&find_inferior_task_it, &port);
409}
a80b95ba 410
bb00b29d
TG
411/* Return an inferior by notification port. */
412static struct inferior *
413darwin_find_inferior_by_notify (mach_port_t port)
414{
415 return iterate_over_inferiors (&find_inferior_notify_it, &port);
416}
a80b95ba 417
bb00b29d
TG
418/* Return a thread by port. */
419static darwin_thread_t *
420darwin_find_thread (struct inferior *inf, thread_t thread)
421{
422 darwin_thread_t *t;
423 int k;
424
425 for (k = 0;
426 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
427 k++)
428 if (t->gdb_port == thread)
429 return t;
430 return NULL;
431}
a80b95ba 432
bb00b29d 433/* Suspend (ie stop) an inferior at Mach level. */
a80b95ba 434
bb00b29d
TG
435static void
436darwin_suspend_inferior (struct inferior *inf)
437{
438 if (!inf->private->suspended)
a80b95ba 439 {
bb00b29d 440 kern_return_t kret;
a80b95ba 441
bb00b29d
TG
442 kret = task_suspend (inf->private->task);
443 MACH_CHECK_ERROR (kret);
a80b95ba 444
bb00b29d
TG
445 inf->private->suspended = 1;
446 }
447}
a80b95ba 448
bb00b29d 449/* Resume an inferior at Mach level. */
a80b95ba 450
bb00b29d
TG
451static void
452darwin_resume_inferior (struct inferior *inf)
453{
454 if (inf->private->suspended)
455 {
456 kern_return_t kret;
a80b95ba 457
bb00b29d
TG
458 kret = task_resume (inf->private->task);
459 MACH_CHECK_ERROR (kret);
460
461 inf->private->suspended = 0;
a80b95ba
TG
462 }
463}
464
bb00b29d
TG
465/* Iterator functions. */
466
467static int
468darwin_suspend_inferior_it (struct inferior *inf, void *arg)
a80b95ba 469{
bb00b29d
TG
470 darwin_suspend_inferior (inf);
471 darwin_check_new_threads (inf);
472 return 0;
473}
474
475static int
476darwin_resume_inferior_it (struct inferior *inf, void *arg)
477{
478 darwin_resume_inferior (inf);
479 return 0;
a80b95ba
TG
480}
481
bb00b29d
TG
482static void
483darwin_dump_message (mach_msg_header_t *hdr, int disp_body)
484{
485 printf_unfiltered (_("message header:\n"));
486 printf_unfiltered (_(" bits: 0x%x\n"), hdr->msgh_bits);
487 printf_unfiltered (_(" size: 0x%x\n"), hdr->msgh_size);
488 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr->msgh_remote_port);
489 printf_unfiltered (_(" local-port: 0x%x\n"), hdr->msgh_local_port);
490 printf_unfiltered (_(" reserved: 0x%x\n"), hdr->msgh_reserved);
491 printf_unfiltered (_(" id: 0x%x\n"), hdr->msgh_id);
492
493 if (disp_body)
494 {
495 const unsigned char *data;
496 const unsigned long *ldata;
497 int size;
498 int i;
499
500 data = (unsigned char *)(hdr + 1);
501 size = hdr->msgh_size - sizeof (mach_msg_header_t);
502
503 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)
504 {
505 mach_msg_body_t *bod = (mach_msg_body_t*)data;
506 mach_msg_port_descriptor_t *desc =
507 (mach_msg_port_descriptor_t *)(bod + 1);
508 int k;
509 NDR_record_t *ndr;
510 printf_unfiltered (_("body: descriptor_count=%u\n"),
511 bod->msgh_descriptor_count);
512 data += sizeof (mach_msg_body_t);
513 size -= sizeof (mach_msg_body_t);
514 for (k = 0; k < bod->msgh_descriptor_count; k++)
515 switch (desc[k].type)
516 {
517 case MACH_MSG_PORT_DESCRIPTOR:
518 printf_unfiltered
519 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"),
520 k, desc[k].type, desc[k].name, desc[k].disposition);
521 break;
522 default:
523 printf_unfiltered (_(" descr %d: type=%u\n"),
524 k, desc[k].type);
525 break;
526 }
527 data += bod->msgh_descriptor_count
528 * sizeof (mach_msg_port_descriptor_t);
529 size -= bod->msgh_descriptor_count
530 * sizeof (mach_msg_port_descriptor_t);
531 ndr = (NDR_record_t *)(desc + bod->msgh_descriptor_count);
532 printf_unfiltered
533 (_("NDR: mig=%02x if=%02x encod=%02x "
534 "int=%02x char=%02x float=%02x\n"),
535 ndr->mig_vers, ndr->if_vers, ndr->mig_encoding,
536 ndr->int_rep, ndr->char_rep, ndr->float_rep);
537 data += sizeof (NDR_record_t);
538 size -= sizeof (NDR_record_t);
539 }
540
541 printf_unfiltered (_(" data:"));
542 ldata = (const unsigned long *)data;
543 for (i = 0; i < size / sizeof (unsigned long); i++)
544 printf_unfiltered (" %08lx", ldata[i]);
545 printf_unfiltered (_("\n"));
546 }
547}
548
549static int
550darwin_decode_exception_message (mach_msg_header_t *hdr,
551 struct inferior **pinf,
552 darwin_thread_t **pthread)
a80b95ba 553{
bb00b29d
TG
554 mach_msg_body_t *bod = (mach_msg_body_t*)(hdr + 1);
555 mach_msg_port_descriptor_t *desc = (mach_msg_port_descriptor_t *)(bod + 1);
556 NDR_record_t *ndr;
557 integer_t *data;
558 struct inferior *inf;
559 darwin_thread_t *thread;
560 task_t task_port;
561 thread_t thread_port;
a80b95ba 562 kern_return_t kret;
bb00b29d 563 int i;
a80b95ba 564
bb00b29d
TG
565 /* Check message identifier. 2401 is exc. */
566 if (hdr->msgh_id != 2401)
567 return -1;
568
569 /* Check message header. */
570 if (!(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX))
571 return -1;
572
573 /* Check descriptors. */
574 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
575 + sizeof (*ndr) + 2 * sizeof (integer_t))
576 || bod->msgh_descriptor_count != 2
577 || desc[0].type != MACH_MSG_PORT_DESCRIPTOR
578 || desc[0].disposition != MACH_MSG_TYPE_MOVE_SEND
579 || desc[1].type != MACH_MSG_PORT_DESCRIPTOR
580 || desc[1].disposition != MACH_MSG_TYPE_MOVE_SEND)
581 return -1;
582
583 /* Check data representation. */
584 ndr = (NDR_record_t *)(desc + 2);
585 if (ndr->mig_vers != NDR_PROTOCOL_2_0
586 || ndr->if_vers != NDR_PROTOCOL_2_0
587 || ndr->mig_encoding != NDR_record.mig_encoding
588 || ndr->int_rep != NDR_record.int_rep
589 || ndr->char_rep != NDR_record.char_rep
590 || ndr->float_rep != NDR_record.float_rep)
591 return -1;
592
593 /* Ok, the hard work. */
594 data = (integer_t *)(ndr + 1);
595
596 /* Find process by port. */
597 task_port = desc[1].name;
598 thread_port = desc[0].name;
599 inf = darwin_find_inferior_by_task (task_port);
600 if (inf == NULL)
601 return -1;
602 *pinf = inf;
603
604 /* Find thread by port. */
605 /* Check for new threads. Do it early so that the port in the exception
606 message can be deallocated. */
607 darwin_check_new_threads (inf);
608
609 /* We got new rights to the task and the thread. Get rid of them. */
a80b95ba
TG
610 kret = mach_port_deallocate (mach_task_self (), task_port);
611 MACH_CHECK_ERROR (kret);
612 kret = mach_port_deallocate (mach_task_self (), thread_port);
613 MACH_CHECK_ERROR (kret);
614
bb00b29d
TG
615 thread = darwin_find_thread (inf, thread_port);
616 if (thread == NULL)
617 return -1;
618 *pthread = thread;
619
484a26a8
TG
620 /* The thread should be running. However we have observed cases where a thread
621 got a SIGTTIN message after being stopped. */
622 gdb_assert (thread->msg_state != DARWIN_MESSAGE);
623
bb00b29d 624 /* Finish decoding. */
bb00b29d
TG
625 thread->event.header = *hdr;
626 thread->event.thread_port = thread_port;
627 thread->event.task_port = task_port;
628 thread->event.ex_type = data[0];
629 thread->event.data_count = data[1];
630
631 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc)
632 + sizeof (*ndr) + 2 * sizeof (integer_t)
633 + data[1] * sizeof (integer_t)))
634 return -1;
635 for (i = 0; i < data[1]; i++)
636 thread->event.ex_data[i] = data[2 + i];
637
638 thread->msg_state = DARWIN_MESSAGE;
639
640 return 0;
641}
642
643static void
644darwin_encode_reply (mig_reply_error_t *reply, mach_msg_header_t *hdr,
645 integer_t code)
646{
647 mach_msg_header_t *rh = &reply->Head;
648 rh->msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(hdr->msgh_bits), 0);
649 rh->msgh_remote_port = hdr->msgh_remote_port;
650 rh->msgh_size = (mach_msg_size_t)sizeof(mig_reply_error_t);
651 rh->msgh_local_port = MACH_PORT_NULL;
652 rh->msgh_id = hdr->msgh_id + 100;
653
654 reply->NDR = NDR_record;
655 reply->RetCode = code;
a80b95ba
TG
656}
657
bb00b29d
TG
658static void
659darwin_send_reply (struct inferior *inf, darwin_thread_t *thread)
a80b95ba
TG
660{
661 kern_return_t kret;
bb00b29d 662 mig_reply_error_t reply;
a80b95ba 663
bb00b29d
TG
664 darwin_encode_reply (&reply, &thread->event.header, KERN_SUCCESS);
665
666 kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT,
667 reply.Head.msgh_size, 0,
668 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
669 MACH_PORT_NULL);
a80b95ba
TG
670 MACH_CHECK_ERROR (kret);
671
bb00b29d
TG
672 inf->private->pending_messages--;
673}
674
675static void
676darwin_resume_thread (struct inferior *inf, darwin_thread_t *thread,
677 int step, int nsignal)
678{
679 kern_return_t kret;
680 int res;
681
a80b95ba 682 inferior_debug
bb00b29d
TG
683 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
684 thread->msg_state, thread->gdb_port, step, nsignal);
685
686 switch (thread->msg_state)
a80b95ba 687 {
bb00b29d
TG
688 case DARWIN_MESSAGE:
689 if (thread->event.ex_type == EXC_SOFTWARE
690 && thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
691 {
692 /* Either deliver a new signal or cancel the signal received. */
693 res = PTRACE (PT_THUPDATE, inf->pid,
694 (void *)(uintptr_t)thread->gdb_port, nsignal);
695 if (res < 0)
696 inferior_debug (1, _("ptrace THUP: res=%d\n"), res);
697 }
698 else if (nsignal)
699 {
700 /* Note: ptrace is allowed only if the process is stopped.
701 Directly send the signal to the thread. */
702 res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal);
703 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
704 thread->gdb_port, nsignal, res);
705 thread->signaled = 1;
706 }
707
708 /* Set single step. */
709 inferior_debug (4, _("darwin_set_sstep (thread=%x, enable=%d)\n"),
710 thread->gdb_port, step);
711 darwin_set_sstep (thread->gdb_port, step);
712 thread->single_step = step;
713
714 darwin_send_reply (inf, thread);
715 thread->msg_state = DARWIN_RUNNING;
716 break;
717
718 case DARWIN_RUNNING:
719 break;
720
721 case DARWIN_STOPPED:
722 kret = thread_resume (thread->gdb_port);
723 MACH_CHECK_ERROR (kret);
724
725 thread->msg_state = DARWIN_RUNNING;
726 break;
a80b95ba 727 }
bb00b29d 728}
a80b95ba 729
bb00b29d 730/* Resume all threads of the inferior. */
a80b95ba 731
bb00b29d
TG
732static void
733darwin_resume_inferior_threads (struct inferior *inf, int step, int nsignal)
734{
735 darwin_thread_t *thread;
736 int k;
737
738 for (k = 0;
739 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread);
740 k++)
741 darwin_resume_thread (inf, thread, step, nsignal);
a80b95ba
TG
742}
743
bb00b29d 744struct resume_inferior_threads_param
a80b95ba 745{
bb00b29d
TG
746 int step;
747 int nsignal;
748};
749
750static int
751darwin_resume_inferior_threads_it (struct inferior *inf, void *param)
752{
753 int step = ((struct resume_inferior_threads_param *)param)->step;
754 int nsignal = ((struct resume_inferior_threads_param *)param)->nsignal;
755
756 darwin_resume_inferior_threads (inf, step, nsignal);
757
758 return 0;
759}
760
761/* Suspend all threads of INF. */
762
763static void
764darwin_suspend_inferior_threads (struct inferior *inf)
765{
766 darwin_thread_t *thread;
a80b95ba 767 kern_return_t kret;
bb00b29d
TG
768 int k;
769
770 for (k = 0;
771 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread);
772 k++)
773 switch (thread->msg_state)
774 {
775 case DARWIN_STOPPED:
776 case DARWIN_MESSAGE:
777 break;
778 case DARWIN_RUNNING:
779 kret = thread_suspend (thread->gdb_port);
780 MACH_CHECK_ERROR (kret);
781 thread->msg_state = DARWIN_STOPPED;
782 break;
783 }
784}
a80b95ba 785
bb00b29d
TG
786static void
787darwin_resume (ptid_t ptid, int step, enum target_signal signal)
788{
789 struct target_waitstatus status;
790 int pid;
a80b95ba 791
bb00b29d
TG
792 kern_return_t kret;
793 int res;
794 int nsignal;
795 struct inferior *inf;
a80b95ba 796
bb00b29d
TG
797 inferior_debug
798 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"),
799 ptid_get_pid (ptid), ptid_get_tid (ptid), step, signal);
800
801 if (signal == TARGET_SIGNAL_0)
802 nsignal = 0;
803 else
804 nsignal = target_signal_to_host (signal);
a80b95ba 805
bb00b29d
TG
806 /* Don't try to single step all threads. */
807 if (step)
808 ptid = inferior_ptid;
809
810 /* minus_one_ptid is RESUME_ALL. */
811 if (ptid_equal (ptid, minus_one_ptid))
a80b95ba 812 {
bb00b29d
TG
813 struct resume_inferior_threads_param param;
814
815 param.nsignal = nsignal;
816 param.step = step;
a80b95ba 817
bb00b29d
TG
818 /* Resume threads. */
819 iterate_over_inferiors (darwin_resume_inferior_threads_it, &param);
820 /* Resume tasks. */
821 iterate_over_inferiors (darwin_resume_inferior_it, NULL);
822 }
823 else
a80b95ba 824 {
bb00b29d
TG
825 struct inferior *inf = find_inferior_pid (ptid_get_pid (ptid));
826 long tid = ptid_get_tid (ptid);
827
828 /* Stop the inferior (should be useless). */
829 darwin_suspend_inferior (inf);
830
831 if (tid == 0)
832 darwin_resume_inferior_threads (inf, step, nsignal);
833 else
834 {
835 darwin_thread_t *thread;
836
837 /* Suspend threads of the task. */
838 darwin_suspend_inferior_threads (inf);
839
840 /* Resume the selected thread. */
841 thread = darwin_find_thread (inf, tid);
842 gdb_assert (thread);
843 darwin_resume_thread (inf, thread, step, nsignal);
844 }
845
846 /* Resume the task. */
847 darwin_resume_inferior (inf);
a80b95ba 848 }
bb00b29d 849}
a80b95ba 850
bb00b29d
TG
851static void
852darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step,
853 enum target_signal signal)
854{
855 return darwin_resume (ptid, step, signal);
856}
857
858static ptid_t
859darwin_decode_message (mach_msg_header_t *hdr,
860 darwin_thread_t **pthread,
861 struct inferior **pinf,
862 struct target_waitstatus *status)
863{
864 darwin_thread_t *thread;
865 struct inferior *inf;
a80b95ba
TG
866
867 /* Exception message. */
868 if (hdr->msgh_local_port == darwin_ex_port)
869 {
bb00b29d
TG
870 int res;
871
872 /* Decode message. */
873 res = darwin_decode_exception_message (hdr, &inf, &thread);
874
875 if (res < 0)
a80b95ba 876 {
bb00b29d
TG
877 /* Should not happen... */
878 printf_unfiltered (_("darwin_wait: ill-formatted message (id=%x)\n"),
a80b95ba 879 hdr->msgh_id);
bb00b29d 880 /* FIXME: send a failure reply? */
a80b95ba
TG
881 status->kind = TARGET_WAITKIND_SPURIOUS;
882 return minus_one_ptid;
883 }
bb00b29d
TG
884 *pinf = inf;
885 *pthread = thread;
886 inf->private->pending_messages++;
a80b95ba
TG
887
888 status->kind = TARGET_WAITKIND_STOPPED;
bb00b29d
TG
889 thread->msg_state = DARWIN_MESSAGE;
890
891 inferior_debug (4, _("darwin_wait: thread=%x, got %s\n"),
892 thread->gdb_port,
893 unparse_exception_type (thread->event.ex_type));
a80b95ba 894
bb00b29d 895 switch (thread->event.ex_type)
a80b95ba
TG
896 {
897 case EXC_BAD_ACCESS:
898 status->value.sig = TARGET_EXC_BAD_ACCESS;
899 break;
900 case EXC_BAD_INSTRUCTION:
901 status->value.sig = TARGET_EXC_BAD_INSTRUCTION;
902 break;
903 case EXC_ARITHMETIC:
904 status->value.sig = TARGET_EXC_ARITHMETIC;
905 break;
906 case EXC_EMULATION:
907 status->value.sig = TARGET_EXC_EMULATION;
908 break;
909 case EXC_SOFTWARE:
bb00b29d 910 if (thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
a80b95ba 911 {
bb00b29d
TG
912 status->value.sig =
913 target_signal_from_host (thread->event.ex_data[1]);
914 inferior_debug (5, _(" (signal %d: %s)\n"),
915 thread->event.ex_data[1],
a80b95ba 916 target_signal_to_name (status->value.sig));
bb00b29d
TG
917
918 /* If the thread is stopped because it has received a signal
919 that gdb has just sent, continue. */
920 if (thread->signaled)
921 {
922 thread->signaled = 0;
923 darwin_send_reply (inf, thread);
924 thread->msg_state = DARWIN_RUNNING;
925 status->kind = TARGET_WAITKIND_IGNORE;
926 }
a80b95ba
TG
927 }
928 else
bb00b29d 929 status->value.sig = TARGET_EXC_SOFTWARE;
a80b95ba
TG
930 break;
931 case EXC_BREAKPOINT:
932 /* Many internal GDB routines expect breakpoints to be reported
933 as TARGET_SIGNAL_TRAP, and will report TARGET_EXC_BREAKPOINT
0963b4bd 934 as a spurious signal. */
a80b95ba
TG
935 status->value.sig = TARGET_SIGNAL_TRAP;
936 break;
937 default:
938 status->value.sig = TARGET_SIGNAL_UNKNOWN;
939 break;
940 }
941
bb00b29d
TG
942 return ptid_build (inf->pid, 0, thread->gdb_port);
943 }
944
945 *pinf = NULL;
946 *pthread = NULL;
947
948 inf = darwin_find_inferior_by_notify (hdr->msgh_local_port);
949 if (inf != NULL)
950 {
951 if (!inf->private->no_ptrace)
952 {
953 pid_t res;
954 int wstatus;
955
956 res = wait4 (inf->pid, &wstatus, 0, NULL);
957 if (res < 0 || res != inf->pid)
958 {
959 printf_unfiltered (_("wait4: res=%d: %s\n"),
960 res, safe_strerror (errno));
961 status->kind = TARGET_WAITKIND_SPURIOUS;
962 return minus_one_ptid;
963 }
964 if (WIFEXITED (wstatus))
965 {
966 status->kind = TARGET_WAITKIND_EXITED;
967 status->value.integer = WEXITSTATUS (wstatus);
968 }
969 else
970 {
971 status->kind = TARGET_WAITKIND_SIGNALLED;
972 status->value.sig = WTERMSIG (wstatus);
973 }
974
975 inferior_debug (4, _("darwin_wait: pid=%d exit, status=%x\n"),
976 res, wstatus);
977
978 /* Looks necessary on Leopard and harmless... */
979 wait4 (inf->pid, &wstatus, 0, NULL);
980
981 return ptid_build (inf->pid, 0, 0);
982 }
983 else
984 {
985 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf->pid);
986 status->kind = TARGET_WAITKIND_EXITED;
987 status->value.integer = 0; /* Don't know. */
988 return ptid_build (inf->pid, 0, 0);
989 }
990 }
991
992 printf_unfiltered (_("Bad local-port: %x\n"), hdr->msgh_local_port);
993 status->kind = TARGET_WAITKIND_SPURIOUS;
994 return minus_one_ptid;
995}
996
997static int
998cancel_breakpoint (ptid_t ptid)
999{
0963b4bd 1000 /* Arrange for a breakpoint to be hit again later. We will handle
bb00b29d
TG
1001 the current event, eventually we will resume this thread, and this
1002 breakpoint will trap again.
1003
1004 If we do not do this, then we run the risk that the user will
1005 delete or disable the breakpoint, but the thread will have already
1006 tripped on it. */
1007
1008 struct regcache *regcache = get_thread_regcache (ptid);
1009 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1010 CORE_ADDR pc;
a80b95ba 1011
bb00b29d 1012 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
40625c9d 1013 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
bb00b29d
TG
1014 {
1015 inferior_debug (4, "cancel_breakpoint for thread %x\n",
1016 ptid_get_tid (ptid));
1017
1018 /* Back up the PC if necessary. */
1019 if (gdbarch_decr_pc_after_break (gdbarch))
1020 regcache_write_pc (regcache, pc);
1021
1022 return 1;
a80b95ba 1023 }
bb00b29d
TG
1024 return 0;
1025}
1026
1027static ptid_t
1028darwin_wait (ptid_t ptid, struct target_waitstatus *status)
1029{
1030 kern_return_t kret;
1031 union
1032 {
1033 mach_msg_header_t hdr;
1034 char data[0x100];
1035 } msgin;
1036 mach_msg_header_t *hdr = &msgin.hdr;
1037 ptid_t res;
1038 darwin_thread_t *thread;
1039 struct inferior *inf;
1040
1041 inferior_debug
1042 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
1043 ptid_get_pid (ptid), ptid_get_tid (ptid));
1044
1045 /* Handle fake stop events at first. */
1046 if (darwin_inf_fake_stop != NULL)
1047 {
1048 inf = darwin_inf_fake_stop;
1049 darwin_inf_fake_stop = NULL;
1050
1051 status->kind = TARGET_WAITKIND_STOPPED;
1052 status->value.sig = TARGET_SIGNAL_TRAP;
1053 thread = VEC_index (darwin_thread_t, inf->private->threads, 0);
1054 thread->msg_state = DARWIN_STOPPED;
1055 return ptid_build (inf->pid, 0, thread->gdb_port);
1056 }
1057
1058 do
1059 {
1060 /* set_sigint_trap (); */
1061
1062 /* Wait for a message. */
1063 kret = mach_msg (&msgin.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0,
1064 sizeof (msgin.data), darwin_port_set, 0, MACH_PORT_NULL);
1065
1066 /* clear_sigint_trap (); */
1067
1068 if (kret == MACH_RCV_INTERRUPTED)
1069 {
1070 status->kind = TARGET_WAITKIND_IGNORE;
1071 return minus_one_ptid;
1072 }
1073
1074 if (kret != MACH_MSG_SUCCESS)
1075 {
1076 inferior_debug (5, _("mach_msg: ret=%x\n"), kret);
1077 status->kind = TARGET_WAITKIND_SPURIOUS;
1078 return minus_one_ptid;
1079 }
1080
1081 /* Debug: display message. */
1082 if (darwin_debug_flag > 10)
1083 darwin_dump_message (hdr, darwin_debug_flag > 11);
1084
1085 res = darwin_decode_message (hdr, &thread, &inf, status);
1086
1087 if (inf == NULL)
1088 return res;
1089 }
1090 while (status->kind == TARGET_WAITKIND_IGNORE);
1091
1092 /* Stop all tasks. */
1093 iterate_over_inferiors (darwin_suspend_inferior_it, NULL);
1094
1095 /* Read pending messages. */
1096 while (1)
a80b95ba 1097 {
bb00b29d
TG
1098 struct target_waitstatus status2;
1099 ptid_t ptid2;
1100
1101 kret = mach_msg (&msgin.hdr,
1102 MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
1103 sizeof (msgin.data), darwin_port_set, 1, MACH_PORT_NULL);
1104
1105 if (kret == MACH_RCV_TIMED_OUT)
1106 break;
1107 if (kret != MACH_MSG_SUCCESS)
1108 {
1109 inferior_debug
1110 (5, _("darwin_wait: mach_msg(pending) ret=%x\n"), kret);
1111 break;
1112 }
1113
1114 ptid2 = darwin_decode_message (hdr, &thread, &inf, &status2);
a80b95ba 1115
bb00b29d
TG
1116 if (inf != NULL && thread != NULL
1117 && thread->event.ex_type == EXC_BREAKPOINT)
a80b95ba 1118 {
bb00b29d
TG
1119 if (thread->single_step
1120 || cancel_breakpoint (ptid_build (inf->pid, 0, thread->gdb_port)))
1121 {
1122 gdb_assert (thread->msg_state == DARWIN_MESSAGE);
1123 darwin_send_reply (inf, thread);
1124 thread->msg_state = DARWIN_RUNNING;
1125 }
1126 else
1127 inferior_debug
1128 (3, _("darwin_wait: thread %x hit a non-gdb breakpoint\n"),
1129 thread->gdb_port);
a80b95ba 1130 }
bb00b29d
TG
1131 else
1132 inferior_debug (3, _("darwin_wait: unhandled pending message\n"));
1133 }
1134 return res;
1135}
a80b95ba 1136
bb00b29d
TG
1137static ptid_t
1138darwin_wait_to (struct target_ops *ops,
1139 ptid_t ptid, struct target_waitstatus *status, int options)
1140{
1141 return darwin_wait (ptid, status);
1142}
a80b95ba 1143
bb00b29d
TG
1144static void
1145darwin_stop (ptid_t t)
1146{
1147 struct inferior *inf = current_inferior ();
a80b95ba 1148
bb00b29d
TG
1149 /* FIXME: handle in no_ptrace mode. */
1150 gdb_assert (!inf->private->no_ptrace);
1151 kill (inf->pid, SIGINT);
a80b95ba
TG
1152}
1153
1154static void
1155darwin_mourn_inferior (struct target_ops *ops)
1156{
1157 struct inferior *inf = current_inferior ();
1158 kern_return_t kret;
1159 mach_port_t prev;
1160 int i;
1161
1162 unpush_target (darwin_ops);
1163
1164 /* Deallocate threads. */
bb00b29d 1165 if (inf->private->threads)
a80b95ba
TG
1166 {
1167 int k;
bb00b29d
TG
1168 darwin_thread_t *t;
1169 for (k = 0;
1170 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1171 k++)
a80b95ba 1172 {
bb00b29d 1173 kret = mach_port_deallocate (gdb_task, t->gdb_port);
a80b95ba
TG
1174 MACH_CHECK_ERROR (kret);
1175 }
bb00b29d
TG
1176 VEC_free (darwin_thread_t, inf->private->threads);
1177 inf->private->threads = NULL;
a80b95ba
TG
1178 }
1179
bb00b29d
TG
1180 kret = mach_port_move_member (gdb_task,
1181 inf->private->notify_port, MACH_PORT_NULL);
1182 gdb_assert (kret == KERN_SUCCESS);
1183
1184 kret = mach_port_request_notification (gdb_task, inf->private->task,
a80b95ba 1185 MACH_NOTIFY_DEAD_NAME, 0,
bb00b29d 1186 MACH_PORT_NULL,
a80b95ba
TG
1187 MACH_MSG_TYPE_MAKE_SEND_ONCE,
1188 &prev);
1189 /* This can fail if the task is dead. */
bb00b29d
TG
1190 inferior_debug (4, "task=%x, prev=%x, notify_port=%x\n",
1191 inf->private->task, prev, inf->private->notify_port);
1192
a80b95ba
TG
1193 if (kret == KERN_SUCCESS)
1194 {
1195 kret = mach_port_deallocate (gdb_task, prev);
1196 MACH_CHECK_ERROR (kret);
1197 }
1198
bb00b29d
TG
1199 kret = mach_port_destroy (gdb_task, inf->private->notify_port);
1200 MACH_CHECK_ERROR (kret);
1201
1202
a80b95ba 1203 /* Deallocate saved exception ports. */
bb00b29d 1204 for (i = 0; i < inf->private->exception_info.count; i++)
a80b95ba
TG
1205 {
1206 kret = mach_port_deallocate
bb00b29d 1207 (gdb_task, inf->private->exception_info.ports[i]);
a80b95ba
TG
1208 MACH_CHECK_ERROR (kret);
1209 }
bb00b29d 1210 inf->private->exception_info.count = 0;
a80b95ba 1211
bb00b29d 1212 kret = mach_port_deallocate (gdb_task, inf->private->task);
a80b95ba
TG
1213 MACH_CHECK_ERROR (kret);
1214
bb00b29d
TG
1215 xfree (inf->private);
1216 inf->private = NULL;
a80b95ba
TG
1217
1218 generic_mourn_inferior ();
1219}
1220
1221static void
bb00b29d 1222darwin_reply_to_all_pending_messages (struct inferior *inf)
a80b95ba 1223{
bb00b29d
TG
1224 int k;
1225 darwin_thread_t *t;
a80b95ba 1226
bb00b29d
TG
1227 for (k = 0;
1228 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1229 k++)
1230 {
1231 if (t->msg_state == DARWIN_MESSAGE)
1232 darwin_resume_thread (inf, t, 0, 0);
1233 }
a80b95ba
TG
1234}
1235
1236static void
bb00b29d 1237darwin_stop_inferior (struct inferior *inf)
a80b95ba
TG
1238{
1239 struct target_waitstatus wstatus;
1240 ptid_t ptid;
1241 kern_return_t kret;
1242 int status;
1243 int res;
1244
bb00b29d 1245 gdb_assert (inf != NULL);
a80b95ba 1246
bb00b29d 1247 darwin_suspend_inferior (inf);
a80b95ba 1248
bb00b29d 1249 darwin_reply_to_all_pending_messages (inf);
a80b95ba 1250
bb00b29d
TG
1251 if (inf->private->no_ptrace)
1252 return;
a80b95ba 1253
bb00b29d
TG
1254 res = kill (inf->pid, SIGSTOP);
1255 if (res != 0)
b37520b6 1256 warning (_("cannot kill: %s"), safe_strerror (errno));
a80b95ba 1257
bb00b29d
TG
1258 /* Wait until the process is really stopped. */
1259 while (1)
a80b95ba 1260 {
bb00b29d
TG
1261 ptid = darwin_wait (inferior_ptid, &wstatus);
1262 if (wstatus.kind == TARGET_WAITKIND_STOPPED
1263 && wstatus.value.sig == TARGET_SIGNAL_STOP)
1264 break;
a80b95ba
TG
1265 }
1266}
1267
1268static kern_return_t
1269darwin_save_exception_ports (darwin_inferior *inf)
1270{
1271 kern_return_t kret;
1272
1273 inf->exception_info.count =
1274 sizeof (inf->exception_info.ports) / sizeof (inf->exception_info.ports[0]);
1275
1276 kret = task_get_exception_ports
1277 (inf->task, EXC_MASK_ALL, inf->exception_info.masks,
1278 &inf->exception_info.count, inf->exception_info.ports,
1279 inf->exception_info.behaviors, inf->exception_info.flavors);
1280 return kret;
1281}
1282
1283static kern_return_t
1284darwin_restore_exception_ports (darwin_inferior *inf)
1285{
1286 int i;
1287 kern_return_t kret;
1288
1289 for (i = 0; i < inf->exception_info.count; i++)
1290 {
1291 kret = task_set_exception_ports
1292 (inf->task, inf->exception_info.masks[i], inf->exception_info.ports[i],
1293 inf->exception_info.behaviors[i], inf->exception_info.flavors[i]);
1294 if (kret != KERN_SUCCESS)
1295 return kret;
1296 }
1297
1298 return KERN_SUCCESS;
1299}
1300
1301static void
bb00b29d
TG
1302darwin_kill_inferior (struct target_ops *ops)
1303{
1304 struct inferior *inf = current_inferior ();
1305 struct target_waitstatus wstatus;
1306 ptid_t ptid;
1307 kern_return_t kret;
1308 int status;
1309 int res;
1310
1311 if (ptid_equal (inferior_ptid, null_ptid))
1312 return;
1313
1314 gdb_assert (inf != NULL);
1315
ee41036f
TG
1316 kret = darwin_restore_exception_ports (inf->private);
1317 MACH_CHECK_ERROR (kret);
bb00b29d 1318
ee41036f 1319 darwin_reply_to_all_pending_messages (inf);
bb00b29d 1320
ee41036f 1321 res = kill (inf->pid, 9);
bb00b29d 1322
ee41036f 1323 if (res == 0)
bb00b29d 1324 {
bb00b29d 1325 darwin_resume_inferior (inf);
ee41036f 1326
bb00b29d
TG
1327 ptid = darwin_wait (inferior_ptid, &wstatus);
1328 }
ee41036f
TG
1329 else if (errno != ESRCH)
1330 warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"),
1331 inf->pid, safe_strerror (errno));
bb00b29d
TG
1332
1333 target_mourn_inferior ();
1334}
1335
1336static void
1337darwin_attach_pid (struct inferior *inf)
a80b95ba 1338{
a80b95ba
TG
1339 kern_return_t kret;
1340 mach_port_t prev_port;
1341 int traps_expected;
bb00b29d 1342 mach_port_t prev_not;
a80b95ba
TG
1343 exception_mask_t mask;
1344
bb00b29d
TG
1345 inf->private = XZALLOC (darwin_inferior);
1346
1347 kret = task_for_pid (gdb_task, inf->pid, &inf->private->task);
a80b95ba
TG
1348 if (kret != KERN_SUCCESS)
1349 {
1350 int status;
a80b95ba
TG
1351
1352 if (!inf->attach_flag)
1353 {
bb00b29d
TG
1354 kill (inf->pid, 9);
1355 waitpid (inf->pid, &status, 0);
a80b95ba
TG
1356 }
1357
1358 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n"
15c19d39 1359 " (please check gdb is codesigned - see taskgated(8))"),
bb00b29d 1360 inf->pid, mach_error_string (kret), (unsigned long) kret);
a80b95ba
TG
1361 }
1362
bb00b29d
TG
1363 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"),
1364 inf->private->task, inf->pid);
a80b95ba
TG
1365
1366 if (darwin_ex_port == MACH_PORT_NULL)
1367 {
1368 /* Create a port to get exceptions. */
1369 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
1370 &darwin_ex_port);
1371 gdb_assert (kret == KERN_SUCCESS);
1372
1373 kret = mach_port_insert_right (gdb_task, darwin_ex_port, darwin_ex_port,
1374 MACH_MSG_TYPE_MAKE_SEND);
1375 gdb_assert (kret == KERN_SUCCESS);
1376
1377 /* Create a port set and put ex_port in it. */
1378 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_PORT_SET,
1379 &darwin_port_set);
1380 gdb_assert (kret == KERN_SUCCESS);
1381
1382 kret = mach_port_move_member (gdb_task, darwin_ex_port, darwin_port_set);
1383 gdb_assert (kret == KERN_SUCCESS);
bb00b29d 1384 }
a80b95ba 1385
bb00b29d
TG
1386 /* Create a port to be notified when the child task terminates. */
1387 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE,
1388 &inf->private->notify_port);
1389 gdb_assert (kret == KERN_SUCCESS);
a80b95ba 1390
bb00b29d
TG
1391 kret = mach_port_move_member (gdb_task,
1392 inf->private->notify_port, darwin_port_set);
1393 gdb_assert (kret == KERN_SUCCESS);
a80b95ba 1394
bb00b29d 1395 kret = mach_port_request_notification (gdb_task, inf->private->task,
a80b95ba 1396 MACH_NOTIFY_DEAD_NAME, 0,
bb00b29d 1397 inf->private->notify_port,
a80b95ba 1398 MACH_MSG_TYPE_MAKE_SEND_ONCE,
bb00b29d 1399 &prev_not);
a80b95ba 1400 gdb_assert (kret == KERN_SUCCESS);
bb00b29d 1401 gdb_assert (prev_not == MACH_PORT_NULL);
a80b95ba 1402
bb00b29d 1403 kret = darwin_save_exception_ports (inf->private);
a80b95ba
TG
1404 gdb_assert (kret == KERN_SUCCESS);
1405
1406 /* Set exception port. */
1407 if (enable_mach_exceptions)
1408 mask = EXC_MASK_ALL;
1409 else
bb00b29d
TG
1410 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
1411 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port,
1412 EXCEPTION_DEFAULT, THREAD_STATE_NONE);
a80b95ba
TG
1413 gdb_assert (kret == KERN_SUCCESS);
1414
1415 push_target (darwin_ops);
1416}
1417
1418static void
bb00b29d 1419darwin_init_thread_list (struct inferior *inf)
a80b95ba 1420{
bb00b29d
TG
1421 darwin_thread_t *thread;
1422 ptid_t new_ptid;
a80b95ba
TG
1423
1424 darwin_check_new_threads (inf);
1425
bb00b29d
TG
1426 gdb_assert (inf->private->threads
1427 && VEC_length (darwin_thread_t, inf->private->threads) > 0);
1428 thread = VEC_index (darwin_thread_t, inf->private->threads, 0);
1429
1430 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid.
1431 Fix up. */
1432 new_ptid = ptid_build (inf->pid, 0, thread->gdb_port);
1433 thread_change_ptid (inferior_ptid, new_ptid);
1434 inferior_ptid = new_ptid;
1435}
1436
1437/* The child must synchronize with gdb: gdb must set the exception port
1438 before the child call PTRACE_SIGEXC. We use a pipe to achieve this.
1439 FIXME: is there a lighter way ? */
1440static int ptrace_fds[2];
1441
1442static void
1443darwin_ptrace_me (void)
1444{
1445 int res;
1446 char c;
1447
1448 /* Close write end point. */
1449 close (ptrace_fds[1]);
1450
1451 /* Wait until gdb is ready. */
1452 res = read (ptrace_fds[0], &c, 1);
1453 gdb_assert (res == 0);
1454 close (ptrace_fds[0]);
1455
1456 /* Get rid of privileges. */
1457 setegid (getgid ());
1458
1459 /* Set TRACEME. */
1460 PTRACE (PT_TRACE_ME, 0, 0, 0);
1461
1462 /* Redirect signals to exception port. */
1463 PTRACE (PT_SIGEXC, 0, 0, 0);
1464}
1465
1466/* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */
1467static void
1468darwin_pre_ptrace (void)
1469{
1470 if (pipe (ptrace_fds) != 0)
1471 {
1472 ptrace_fds[0] = -1;
1473 ptrace_fds[1] = -1;
1474 error (_("unable to create a pipe: %s"), safe_strerror (errno));
1475 }
a80b95ba
TG
1476}
1477
1478static void
1479darwin_ptrace_him (int pid)
1480{
1481 task_t itask;
1482 kern_return_t kret;
1483 mach_port_t prev_port;
1484 int traps_expected;
bb00b29d 1485 struct inferior *inf = current_inferior ();
a80b95ba 1486
bb00b29d 1487 darwin_attach_pid (inf);
a80b95ba
TG
1488
1489 /* Let's the child run. */
1490 close (ptrace_fds[0]);
1491 close (ptrace_fds[1]);
1492
bb00b29d
TG
1493 darwin_init_thread_list (inf);
1494
a80b95ba
TG
1495 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
1496}
1497
e69860f1
TG
1498static void
1499darwin_execvp (const char *file, char * const argv[], char * const env[])
1500{
1501 posix_spawnattr_t attr;
1502 short ps_flags = 0;
f00c55f8 1503 int res;
e69860f1 1504
f00c55f8
TG
1505 res = posix_spawnattr_init (&attr);
1506 if (res != 0)
e69860f1
TG
1507 {
1508 fprintf_unfiltered
1509 (gdb_stderr, "Cannot initialize attribute for posix_spawn\n");
1510 return;
1511 }
1512
1513 /* Do like execve: replace the image. */
1514 ps_flags = POSIX_SPAWN_SETEXEC;
1515
1516 /* Disable ASLR. The constant doesn't look to be available outside the
1517 kernel include files. */
1518#ifndef _POSIX_SPAWN_DISABLE_ASLR
1519#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
1520#endif
1521 ps_flags |= _POSIX_SPAWN_DISABLE_ASLR;
f00c55f8
TG
1522 res = posix_spawnattr_setflags (&attr, ps_flags);
1523 if (res != 0)
e69860f1 1524 {
f00c55f8 1525 fprintf_unfiltered (gdb_stderr, "Cannot set posix_spawn flags\n");
e69860f1
TG
1526 return;
1527 }
1528
ece0061f
TG
1529 /* Specify the same binary preference to spawn the shell as the
1530 exec binary. This avoids spawning a 64bit shell while debugging
1531 a 32bit program, which may confuse gdb.
1532 Also, this slightly breaks internal layers as we suppose the binary
1533 is Mach-O. Doesn't harm in practice. */
1534 if (exec_bfd != NULL)
1535 {
1536 cpu_type_t pref;
1537 size_t ocount;
1538
1539 pref = bfd_mach_o_get_data (exec_bfd)->header.cputype;
1540 res = posix_spawnattr_setbinpref_np (&attr, 1, &pref, &ocount);
1541 if (res != 0 || ocount != 1)
1542 fprintf_unfiltered (gdb_stderr, "Cannot set posix_spawn binpref\n");
1543 }
1544
e69860f1
TG
1545 posix_spawnp (NULL, argv[0], NULL, &attr, argv, env);
1546}
1547
a80b95ba
TG
1548static void
1549darwin_create_inferior (struct target_ops *ops, char *exec_file,
1550 char *allargs, char **env, int from_tty)
1551{
1552 /* Do the hard work. */
1553 fork_inferior (exec_file, allargs, env, darwin_ptrace_me, darwin_ptrace_him,
e69860f1 1554 darwin_pre_ptrace, NULL, darwin_execvp);
bb00b29d 1555
a80b95ba
TG
1556 /* Return now in case of error. */
1557 if (ptid_equal (inferior_ptid, null_ptid))
1558 return;
1559}
1560\f
1561
1562/* Attach to process PID, then initialize for debugging it
1563 and wait for the trace-trap that results from attaching. */
1564static void
1565darwin_attach (struct target_ops *ops, char *args, int from_tty)
1566{
1567 pid_t pid;
1568 pid_t pid2;
1569 int wstatus;
1570 int res;
1571 struct inferior *inf;
bb00b29d 1572 kern_return_t kret;
a80b95ba 1573
74164c56 1574 pid = parse_pid_to_attach (args);
a80b95ba 1575
74164c56 1576 if (pid == getpid ()) /* Trying to masturbate? */
a80b95ba
TG
1577 error (_("I refuse to debug myself!"));
1578
1579 if (from_tty)
bb00b29d
TG
1580 {
1581 char *exec_file = get_exec_file (0);
a80b95ba 1582
bb00b29d
TG
1583 if (exec_file)
1584 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
1585 target_pid_to_str (pid_to_ptid (pid)));
1586 else
1587 printf_unfiltered (_("Attaching to %s\n"),
1588 target_pid_to_str (pid_to_ptid (pid)));
1589
1590 gdb_flush (gdb_stdout);
1591 }
1592
1593 if (pid == 0 || kill (pid, 0) < 0)
1594 error (_("Can't attach to process %d: %s (%d)"),
1595 pid, safe_strerror (errno), errno);
a80b95ba 1596
bb00b29d 1597 inferior_ptid = pid_to_ptid (pid);
6c95b8df
PA
1598 inf = current_inferior ();
1599 inferior_appeared (inf, pid);
a80b95ba 1600 inf->attach_flag = 1;
6c95b8df 1601
bb00b29d
TG
1602 /* Always add a main thread. */
1603 add_thread_silent (inferior_ptid);
a80b95ba 1604
bb00b29d 1605 darwin_attach_pid (inf);
a80b95ba 1606
bb00b29d 1607 darwin_suspend_inferior (inf);
a80b95ba 1608
bb00b29d 1609 darwin_init_thread_list (inf);
a80b95ba 1610
bb00b29d 1611 darwin_check_osabi (inf->private, ptid_get_tid (inferior_ptid));
a80b95ba 1612
bb00b29d
TG
1613 gdb_assert (darwin_inf_fake_stop == NULL);
1614 darwin_inf_fake_stop = inf;
1615 inf->private->no_ptrace = 1;
a80b95ba
TG
1616}
1617
1618/* Take a program previously attached to and detaches it.
1619 The program resumes execution and will no longer stop
1620 on signals, etc. We'd better not have left any breakpoints
1621 in the program or it'll die when it hits one. For this
1622 to work, it may be necessary for the process to have been
1623 previously attached. It *might* work if the program was
1624 started via fork. */
1625static void
1626darwin_detach (struct target_ops *ops, char *args, int from_tty)
1627{
bb00b29d
TG
1628 pid_t pid = ptid_get_pid (inferior_ptid);
1629 struct inferior *inf = current_inferior ();
a80b95ba
TG
1630 kern_return_t kret;
1631 int res;
1632
bb00b29d 1633 /* Display message. */
a80b95ba
TG
1634 if (from_tty)
1635 {
1636 char *exec_file = get_exec_file (0);
1637 if (exec_file == 0)
1638 exec_file = "";
bb00b29d
TG
1639 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
1640 target_pid_to_str (pid_to_ptid (pid)));
a80b95ba
TG
1641 gdb_flush (gdb_stdout);
1642 }
1643
bb00b29d
TG
1644 /* If ptrace() is in use, stop the process. */
1645 if (!inf->private->no_ptrace)
1646 darwin_stop_inferior (inf);
a80b95ba 1647
bb00b29d 1648 kret = darwin_restore_exception_ports (inf->private);
a80b95ba
TG
1649 MACH_CHECK_ERROR (kret);
1650
bb00b29d 1651 if (!inf->private->no_ptrace)
a80b95ba 1652 {
bb00b29d
TG
1653 res = PTRACE (PT_DETACH, inf->pid, 0, 0);
1654 if (res != 0)
1655 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"),
1656 inf->pid, safe_strerror (errno), errno);
a80b95ba
TG
1657 }
1658
bb00b29d 1659 darwin_reply_to_all_pending_messages (inf);
a80b95ba 1660
5e9bc145
JB
1661 /* When using ptrace, we have just performed a PT_DETACH, which
1662 resumes the inferior. On the other hand, when we are not using
1663 ptrace, we need to resume its execution ourselves. */
1664 if (inf->private->no_ptrace)
1665 darwin_resume_inferior (inf);
a80b95ba
TG
1666
1667 darwin_mourn_inferior (ops);
1668}
1669
1670static void
1671darwin_files_info (struct target_ops *ops)
1672{
a80b95ba
TG
1673}
1674
1675static char *
117de6a9 1676darwin_pid_to_str (struct target_ops *ops, ptid_t ptid)
a80b95ba 1677{
bb00b29d
TG
1678 static char buf[80];
1679 long tid = ptid_get_tid (ptid);
1680
1681 if (tid != 0)
1682 {
1683 snprintf (buf, sizeof (buf), _("Thread 0x%lx of process %u"),
1684 tid, ptid_get_pid (ptid));
1685 return buf;
1686 }
a80b95ba 1687
bb00b29d 1688 return normal_pid_to_str (ptid);
a80b95ba
TG
1689}
1690
1691static int
28439f5e 1692darwin_thread_alive (struct target_ops *ops, ptid_t ptid)
a80b95ba
TG
1693{
1694 return 1;
1695}
1696
1697/* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and
1698 copy it to RDADDR in gdb's address space.
1699 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it
1700 to ADDR in inferior task's address space.
f00c55f8 1701 Return 0 on failure; number of bytes read / writen otherwise. */
a80b95ba
TG
1702static int
1703darwin_read_write_inferior (task_t task, CORE_ADDR addr,
1704 char *rdaddr, const char *wraddr, int length)
1705{
bb00b29d 1706 kern_return_t kret;
a80b95ba
TG
1707 mach_vm_address_t offset = addr & (mach_page_size - 1);
1708 mach_vm_address_t low_address = (mach_vm_address_t) (addr - offset);
1709 mach_vm_size_t aligned_length = (mach_vm_size_t) PAGE_ROUND (offset + length);
1710 pointer_t copied;
1711 int copy_count;
1712 mach_vm_size_t remaining_length;
1713 mach_vm_address_t region_address;
1714 mach_vm_size_t region_length;
1715
bb00b29d
TG
1716 inferior_debug (8, _("darwin_read_write_inferior(task=%x, %s, len=%d)\n"),
1717 task, core_addr_to_string (addr), length);
1718
0963b4bd 1719 /* Get memory from inferior with page aligned addresses. */
bb00b29d 1720 kret = mach_vm_read (task, low_address, aligned_length,
a80b95ba 1721 &copied, &copy_count);
bb00b29d 1722 if (kret != KERN_SUCCESS)
a80b95ba 1723 {
bb00b29d
TG
1724 inferior_debug
1725 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"),
1726 core_addr_to_string (addr), mach_error_string (kret));
a80b95ba
TG
1727 return 0;
1728 }
1729
1730 if (rdaddr != NULL)
1731 memcpy (rdaddr, (char *)copied + offset, length);
1732
1733 if (wraddr == NULL)
1734 goto out;
1735
1736 memcpy ((char *)copied + offset, wraddr, length);
1737
1738 /* Do writes atomically.
1739 First check for holes and unwritable memory. */
1740 for (region_address = low_address, remaining_length = aligned_length;
1741 region_address < low_address + aligned_length;
1742 region_address += region_length, remaining_length -= region_length)
1743 {
bb00b29d
TG
1744 vm_region_submap_short_info_data_64_t info;
1745 mach_vm_address_t region_start = region_address;
a80b95ba 1746 mach_msg_type_number_t count;
bb00b29d
TG
1747 natural_t region_depth;
1748
1749 region_depth = 100000;
1750 count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
1751 kret = mach_vm_region_recurse
1752 (task, &region_start, &region_length, &region_depth,
1753 (vm_region_recurse_info_t) &info, &count);
1754
1755 if (kret != KERN_SUCCESS)
a80b95ba 1756 {
bb00b29d
TG
1757 inferior_debug (1, _("darwin_read_write_inferior: "
1758 "mach_vm_region_recurse failed at %s: %s\n"),
1759 core_addr_to_string (region_address),
1760 mach_error_string (kret));
a80b95ba
TG
1761 goto out;
1762 }
1763
bb00b29d
TG
1764 inferior_debug
1765 (9, _("darwin_read_write_inferior: "
1766 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"),
1767 core_addr_to_string (region_address),
1768 core_addr_to_string (region_start),
1769 core_addr_to_string (region_length));
1770
0963b4bd 1771 /* Check for holes in memory. */
bb00b29d 1772 if (region_start > region_address)
a80b95ba 1773 {
0963b4bd 1774 warning (_("No memory at %s (vs %s+0x%x). Nothing written"),
a80b95ba 1775 core_addr_to_string (region_address),
bb00b29d 1776 core_addr_to_string (region_start),
a80b95ba
TG
1777 (unsigned)region_length);
1778 length = 0;
1779 goto out;
1780 }
1781
bb00b29d
TG
1782 /* Adjust the length. */
1783 region_length -= (region_address - region_start);
1784
a80b95ba
TG
1785 if (!(info.max_protection & VM_PROT_WRITE))
1786 {
bb00b29d
TG
1787 kret = mach_vm_protect
1788 (task, region_address, region_length,
1789 TRUE, info.max_protection | VM_PROT_WRITE | VM_PROT_COPY);
1790 if (kret != KERN_SUCCESS)
1791 {
1792 warning (_("darwin_read_write_inf: "
1793 "mach_vm_protect max failed at %s: %s"),
1794 core_addr_to_string (region_address),
1795 mach_error_string (kret));
1796 length = 0;
1797 goto out;
1798 }
a80b95ba
TG
1799 }
1800
1801 if (!(info.protection & VM_PROT_WRITE))
1802 {
bb00b29d 1803 kret = mach_vm_protect (task, region_address, region_length,
a80b95ba 1804 FALSE, info.protection | VM_PROT_WRITE);
bb00b29d 1805 if (kret != KERN_SUCCESS)
a80b95ba 1806 {
bb00b29d
TG
1807 warning (_("darwin_read_write_inf: "
1808 "mach_vm_protect failed at %s (len=0x%lx): %s"),
1809 core_addr_to_string (region_address),
1810 (unsigned long)region_length, mach_error_string (kret));
a80b95ba
TG
1811 length = 0;
1812 goto out;
1813 }
1814 }
1815 }
1816
bb00b29d 1817 kret = mach_vm_write (task, low_address, copied, aligned_length);
a80b95ba 1818
bb00b29d 1819 if (kret != KERN_SUCCESS)
a80b95ba
TG
1820 {
1821 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"),
bb00b29d 1822 mach_error_string (kret));
a80b95ba
TG
1823 length = 0;
1824 }
1825out:
1826 mach_vm_deallocate (mach_task_self (), copied, copy_count);
1827 return length;
1828}
1829
f00c55f8
TG
1830/* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them
1831 to RDADDR.
1832 Return 0 on failure; number of bytes read / writen otherwise. */
1833
1834static int
1835darwin_read_dyld_info (task_t task, CORE_ADDR addr, char *rdaddr, int length)
1836{
1837 struct task_dyld_info task_dyld_info;
1838 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
1839 int sz = TASK_DYLD_INFO_COUNT * sizeof (natural_t);
1840 kern_return_t kret;
1841
1842 if (addr >= sz)
1843 return 0;
1844
1845 kret = task_info (task, TASK_DYLD_INFO, (task_info_t) &task_dyld_info, &count);
1846 MACH_CHECK_ERROR (kret);
1847 if (kret != KERN_SUCCESS)
1848 return -1;
1849 /* Truncate. */
1850 if (addr + length > sz)
1851 length = sz - addr;
1852 memcpy (rdaddr, (char *)&task_dyld_info + addr, length);
1853 return length;
1854}
1855
a80b95ba
TG
1856\f
1857/* Return 0 on failure, number of bytes handled otherwise. TARGET
0963b4bd 1858 is ignored. */
a80b95ba
TG
1859static int
1860darwin_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int write,
1861 struct mem_attrib *attrib, struct target_ops *target)
1862{
bb00b29d
TG
1863 struct inferior *inf = current_inferior ();
1864 task_t task = inf->private->task;
a80b95ba
TG
1865
1866 if (task == MACH_PORT_NULL)
1867 return 0;
1868
1869 inferior_debug (8, _("darwin_xfer_memory(%s, %d, %c)\n"),
1870 core_addr_to_string (memaddr), len, write ? 'w' : 'r');
1871
1872 if (write)
1873 return darwin_read_write_inferior (task, memaddr, NULL, myaddr, len);
1874 else
1875 return darwin_read_write_inferior (task, memaddr, myaddr, NULL, len);
1876}
1877
1878static LONGEST
1879darwin_xfer_partial (struct target_ops *ops,
1880 enum target_object object, const char *annex,
1881 gdb_byte *readbuf, const gdb_byte *writebuf,
1882 ULONGEST offset, LONGEST len)
1883{
bb00b29d
TG
1884 struct inferior *inf = current_inferior ();
1885
1886 inferior_debug
854f4b0e
TG
1887 (8, _("darwin_xfer_partial(%s, %d, rbuf=%s, wbuf=%s) pid=%u\n"),
1888 core_addr_to_string (offset), (int)len,
1889 host_address_to_string (readbuf), host_address_to_string (writebuf),
1890 inf->pid);
a80b95ba 1891
f00c55f8
TG
1892 switch (object)
1893 {
1894 case TARGET_OBJECT_MEMORY:
1895 return darwin_read_write_inferior (inf->private->task, offset,
1896 readbuf, writebuf, len);
1897 case TARGET_OBJECT_DARWIN_DYLD_INFO:
1898 if (writebuf != NULL || readbuf == NULL)
1899 {
1900 /* Support only read. */
1901 return -1;
1902 }
1903 return darwin_read_dyld_info (inf->private->task, offset, readbuf, len);
1904 default:
1905 return -1;
1906 }
a80b95ba 1907
a80b95ba
TG
1908}
1909
1910static void
1911set_enable_mach_exceptions (char *args, int from_tty,
1912 struct cmd_list_element *c)
1913{
bb00b29d 1914 if (!ptid_equal (inferior_ptid, null_ptid))
a80b95ba 1915 {
bb00b29d 1916 struct inferior *inf = current_inferior ();
a80b95ba
TG
1917 exception_mask_t mask;
1918 kern_return_t kret;
1919
1920 if (enable_mach_exceptions)
1921 mask = EXC_MASK_ALL;
1922 else
1923 {
bb00b29d
TG
1924 darwin_restore_exception_ports (inf->private);
1925 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT;
a80b95ba 1926 }
bb00b29d 1927 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port,
a80b95ba
TG
1928 EXCEPTION_DEFAULT, THREAD_STATE_NONE);
1929 MACH_CHECK_ERROR (kret);
1930 }
1931}
1932
bb00b29d
TG
1933static char *
1934darwin_pid_to_exec_file (int pid)
1935{
1936 char *path;
1937 int res;
1938
1939 path = xmalloc (MAXPATHLEN);
1940 make_cleanup (xfree, path);
1941
1942 res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, MAXPATHLEN);
1943 if (res >= 0)
1944 return path;
1945 else
1946 return NULL;
1947}
1948
1949static ptid_t
1950darwin_get_ada_task_ptid (long lwp, long thread)
1951{
1952 int i;
1953 darwin_thread_t *t;
1954 int k;
1955 struct inferior *inf = current_inferior ();
1956 kern_return_t kret;
1957 mach_port_name_array_t names;
1958 mach_msg_type_number_t names_count;
1959 mach_port_type_array_t types;
1960 mach_msg_type_number_t types_count;
1961 long res = 0;
1962
1963 /* First linear search. */
1964 for (k = 0;
1965 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1966 k++)
1967 if (t->inf_port == lwp)
1968 return ptid_build (ptid_get_pid (inferior_ptid), 0, t->gdb_port);
1969
1970 /* Maybe the port was never extract. Do it now. */
1971
1972 /* First get inferior port names. */
1973 kret = mach_port_names (inf->private->task, &names, &names_count, &types,
1974 &types_count);
1975 MACH_CHECK_ERROR (kret);
1976 if (kret != KERN_SUCCESS)
1977 return null_ptid;
1978
1979 /* For each name, copy the right in the gdb space and then compare with
1980 our view of the inferior threads. We don't forget to deallocate the
1981 right. */
1982 for (i = 0; i < names_count; i++)
1983 {
1984 mach_port_t local_name;
1985 mach_msg_type_name_t local_type;
1986
1987 /* We just need to know the corresponding name in gdb name space.
1988 So extract and deallocate the right. */
1989 kret = mach_port_extract_right (inf->private->task, names[i],
1990 MACH_MSG_TYPE_COPY_SEND,
1991 &local_name, &local_type);
1992 if (kret != KERN_SUCCESS)
1993 continue;
1994 mach_port_deallocate (gdb_task, local_name);
1995
1996 for (k = 0;
1997 VEC_iterate (darwin_thread_t, inf->private->threads, k, t);
1998 k++)
1999 if (t->gdb_port == local_name)
2000 {
2001 t->inf_port = names[i];
2002 if (names[i] == lwp)
2003 res = t->gdb_port;
2004 }
2005 }
2006
2007 vm_deallocate (gdb_task, (vm_address_t) names,
2008 names_count * sizeof (mach_port_t));
2009
2010 if (res)
2011 return ptid_build (ptid_get_pid (inferior_ptid), 0, res);
2012 else
2013 return null_ptid;
2014}
2015
2016static int
2017darwin_supports_multi_process (void)
2018{
2019 return 1;
2020}
2021
c381a3f6
JB
2022/* -Wmissing-prototypes */
2023extern initialize_file_ftype _initialize_darwin_inferior;
2024
a80b95ba 2025void
bb00b29d 2026_initialize_darwin_inferior (void)
a80b95ba
TG
2027{
2028 kern_return_t kret;
2029
a80b95ba
TG
2030 gdb_task = mach_task_self ();
2031 darwin_host_self = mach_host_self ();
2032
2033 /* Read page size. */
2034 kret = host_page_size (darwin_host_self, &mach_page_size);
2035 if (kret != KERN_SUCCESS)
2036 {
2037 mach_page_size = 0x1000;
2038 MACH_CHECK_ERROR (kret);
2039 }
2040
a80b95ba
TG
2041 darwin_ops = inf_child_target ();
2042
2043 darwin_ops->to_shortname = "darwin-child";
2044 darwin_ops->to_longname = _("Darwin child process");
2045 darwin_ops->to_doc =
2046 _("Darwin child process (started by the \"run\" command).");
2047 darwin_ops->to_create_inferior = darwin_create_inferior;
2048 darwin_ops->to_attach = darwin_attach;
2049 darwin_ops->to_attach_no_wait = 0;
2050 darwin_ops->to_detach = darwin_detach;
2051 darwin_ops->to_files_info = darwin_files_info;
bb00b29d 2052 darwin_ops->to_wait = darwin_wait_to;
a80b95ba
TG
2053 darwin_ops->to_mourn_inferior = darwin_mourn_inferior;
2054 darwin_ops->to_kill = darwin_kill_inferior;
2055 darwin_ops->to_stop = darwin_stop;
bb00b29d 2056 darwin_ops->to_resume = darwin_resume_to;
a80b95ba
TG
2057 darwin_ops->to_thread_alive = darwin_thread_alive;
2058 darwin_ops->to_pid_to_str = darwin_pid_to_str;
bb00b29d 2059 darwin_ops->to_pid_to_exec_file = darwin_pid_to_exec_file;
a80b95ba
TG
2060 darwin_ops->to_load = NULL;
2061 darwin_ops->deprecated_xfer_memory = darwin_xfer_memory;
2062 darwin_ops->to_xfer_partial = darwin_xfer_partial;
bb00b29d
TG
2063 darwin_ops->to_supports_multi_process = darwin_supports_multi_process;
2064 darwin_ops->to_get_ada_task_ptid = darwin_get_ada_task_ptid;
a80b95ba
TG
2065
2066 darwin_complete_target (darwin_ops);
2067
2068 add_target (darwin_ops);
2069
2070 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (),
2071 getpid ());
2072
2073 add_setshow_zinteger_cmd ("darwin", class_obscure,
2074 &darwin_debug_flag, _("\
2075Set if printing inferior communication debugging statements."), _("\
2076Show if printing inferior communication debugging statements."), NULL,
2077 NULL, NULL,
2078 &setdebuglist, &showdebuglist);
2079
2080 add_setshow_boolean_cmd ("mach-exceptions", class_support,
2081 &enable_mach_exceptions, _("\
2082Set if mach exceptions are caught."), _("\
2083Show if mach exceptions are caught."), _("\
2084When this mode is on, all low level exceptions are reported before being\n\
2085reported by the kernel."),
2086 &set_enable_mach_exceptions, NULL,
2087 &setlist, &showlist);
2088}