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