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