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