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