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