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