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