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