GDB (API)
|
00001 /* Darwin support for GDB, the GNU debugger. 00002 Copyright (C) 2008-2013 Free Software Foundation, Inc. 00003 00004 Contributed by AdaCore. 00005 00006 This file is part of GDB. 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00020 00021 #include "defs.h" 00022 #include "top.h" 00023 #include "inferior.h" 00024 #include "target.h" 00025 #include "symfile.h" 00026 #include "symtab.h" 00027 #include "objfiles.h" 00028 #include "gdb.h" 00029 #include "gdbcmd.h" 00030 #include "gdbcore.h" 00031 #include "gdbthread.h" 00032 #include "regcache.h" 00033 #include "event-top.h" 00034 #include "inf-loop.h" 00035 #include "gdb_stat.h" 00036 #include "exceptions.h" 00037 #include "inf-child.h" 00038 #include "value.h" 00039 #include "arch-utils.h" 00040 #include "bfd.h" 00041 #include "bfd/mach-o.h" 00042 00043 #include <sys/ptrace.h> 00044 #include <sys/signal.h> 00045 #include <machine/setjmp.h> 00046 #include <sys/types.h> 00047 #include <unistd.h> 00048 #include <signal.h> 00049 #include <string.h> 00050 #include <ctype.h> 00051 #include <sys/sysctl.h> 00052 #include <sys/proc.h> 00053 #include <libproc.h> 00054 #include <sys/syscall.h> 00055 #include <spawn.h> 00056 00057 #include <mach/mach_error.h> 00058 #include <mach/mach_vm.h> 00059 #include <mach/mach_init.h> 00060 #include <mach/vm_map.h> 00061 #include <mach/task.h> 00062 #include <mach/mach_port.h> 00063 #include <mach/thread_act.h> 00064 #include <mach/port.h> 00065 00066 #include "darwin-nat.h" 00067 #include "common/filestuff.h" 00068 00069 /* Quick overview. 00070 Darwin kernel is Mach + BSD derived kernel. Note that they share the 00071 same memory space and are linked together (ie there is no micro-kernel). 00072 00073 Although ptrace(2) is available on Darwin, it is not complete. We have 00074 to use Mach calls to read and write memory and to modify registers. We 00075 also use Mach to get inferior faults. As we cannot use select(2) or 00076 signals with Mach port (the Mach communication channel), signals are 00077 reported to gdb as an exception. Furthermore we detect death of the 00078 inferior through a Mach notification message. This way we only wait 00079 on Mach ports. 00080 00081 Some Mach documentation is available for Apple xnu source package or 00082 from the web. */ 00083 00084 00085 #define PTRACE(CMD, PID, ADDR, SIG) \ 00086 darwin_ptrace(#CMD, CMD, (PID), (ADDR), (SIG)) 00087 00088 extern boolean_t exc_server (mach_msg_header_t *in, mach_msg_header_t *out); 00089 00090 static void darwin_stop (ptid_t); 00091 00092 static void darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step, 00093 enum gdb_signal signal); 00094 static void darwin_resume (ptid_t ptid, int step, 00095 enum gdb_signal signal); 00096 00097 static ptid_t darwin_wait_to (struct target_ops *ops, ptid_t ptid, 00098 struct target_waitstatus *status, int options); 00099 static ptid_t darwin_wait (ptid_t ptid, struct target_waitstatus *status); 00100 00101 static void darwin_mourn_inferior (struct target_ops *ops); 00102 00103 static void darwin_kill_inferior (struct target_ops *ops); 00104 00105 static void darwin_ptrace_me (void); 00106 00107 static void darwin_ptrace_him (int pid); 00108 00109 static void darwin_create_inferior (struct target_ops *ops, char *exec_file, 00110 char *allargs, char **env, int from_tty); 00111 00112 static void darwin_files_info (struct target_ops *ops); 00113 00114 static char *darwin_pid_to_str (struct target_ops *ops, ptid_t tpid); 00115 00116 static int darwin_thread_alive (struct target_ops *ops, ptid_t tpid); 00117 00118 /* Target operations for Darwin. */ 00119 static struct target_ops *darwin_ops; 00120 00121 /* Task identifier of gdb. */ 00122 static task_t gdb_task; 00123 00124 /* A copy of mach_host_self (). */ 00125 mach_port_t darwin_host_self; 00126 00127 /* Exception port. */ 00128 mach_port_t darwin_ex_port; 00129 00130 /* Port set. */ 00131 mach_port_t darwin_port_set; 00132 00133 /* Page size. */ 00134 static vm_size_t mach_page_size; 00135 00136 /* If Set, catch all mach exceptions (before they are converted to signals 00137 by the kernel). */ 00138 static int enable_mach_exceptions; 00139 00140 /* Inferior that should report a fake stop event. */ 00141 static struct inferior *darwin_inf_fake_stop; 00142 00143 #define PAGE_TRUNC(x) ((x) & ~(mach_page_size - 1)) 00144 #define PAGE_ROUND(x) PAGE_TRUNC((x) + mach_page_size - 1) 00145 00146 /* This controls output of inferior debugging. */ 00147 static unsigned int darwin_debug_flag = 0; 00148 00149 /* Create a __TEXT __info_plist section in the executable so that gdb could 00150 be signed. This is required to get an authorization for task_for_pid. 00151 00152 Once gdb is built, you can either: 00153 * make it setgid procmod 00154 * or codesign it with any system-trusted signing authority. 00155 See taskgated(8) for details. */ 00156 static const unsigned char info_plist[] 00157 __attribute__ ((section ("__TEXT,__info_plist"),used)) = 00158 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 00159 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"" 00160 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" 00161 "<plist version=\"1.0\">\n" 00162 "<dict>\n" 00163 " <key>CFBundleIdentifier</key>\n" 00164 " <string>org.gnu.gdb</string>\n" 00165 " <key>CFBundleName</key>\n" 00166 " <string>gdb</string>\n" 00167 " <key>CFBundleVersion</key>\n" 00168 " <string>1.0</string>\n" 00169 " <key>SecTaskAccess</key>\n" 00170 " <array>\n" 00171 " <string>allowed</string>\n" 00172 " <string>debug</string>\n" 00173 " </array>\n" 00174 "</dict>\n" 00175 "</plist>\n"; 00176 00177 static void 00178 inferior_debug (int level, const char *fmt, ...) 00179 { 00180 va_list ap; 00181 00182 if (darwin_debug_flag < level) 00183 return; 00184 00185 va_start (ap, fmt); 00186 printf_unfiltered (_("[%d inferior]: "), getpid ()); 00187 vprintf_unfiltered (fmt, ap); 00188 va_end (ap); 00189 } 00190 00191 void 00192 mach_check_error (kern_return_t ret, const char *file, 00193 unsigned int line, const char *func) 00194 { 00195 if (ret == KERN_SUCCESS) 00196 return; 00197 if (func == NULL) 00198 func = _("[UNKNOWN]"); 00199 00200 warning (_("Mach error at \"%s:%u\" in function \"%s\": %s (0x%lx)"), 00201 file, line, func, mach_error_string (ret), (unsigned long) ret); 00202 } 00203 00204 static const char * 00205 unparse_exception_type (unsigned int i) 00206 { 00207 static char unknown_exception_buf[32]; 00208 00209 switch (i) 00210 { 00211 case EXC_BAD_ACCESS: 00212 return "EXC_BAD_ACCESS"; 00213 case EXC_BAD_INSTRUCTION: 00214 return "EXC_BAD_INSTRUCTION"; 00215 case EXC_ARITHMETIC: 00216 return "EXC_ARITHMETIC"; 00217 case EXC_EMULATION: 00218 return "EXC_EMULATION"; 00219 case EXC_SOFTWARE: 00220 return "EXC_SOFTWARE"; 00221 case EXC_BREAKPOINT: 00222 return "EXC_BREAKPOINT"; 00223 case EXC_SYSCALL: 00224 return "EXC_SYSCALL"; 00225 case EXC_MACH_SYSCALL: 00226 return "EXC_MACH_SYSCALL"; 00227 case EXC_RPC_ALERT: 00228 return "EXC_RPC_ALERT"; 00229 case EXC_CRASH: 00230 return "EXC_CRASH"; 00231 default: 00232 snprintf (unknown_exception_buf, 32, _("unknown (%d)"), i); 00233 return unknown_exception_buf; 00234 } 00235 } 00236 00237 /* Set errno to zero, and then call ptrace with the given arguments. 00238 If inferior debugging traces are on, then also print a debug 00239 trace. 00240 00241 The returned value is the same as the value returned by ptrace, 00242 except in the case where that value is -1 but errno is zero. 00243 This case is documented to be a non-error situation, so we 00244 return zero in that case. */ 00245 00246 static int 00247 darwin_ptrace (const char *name, 00248 int request, int pid, PTRACE_TYPE_ARG3 arg3, int arg4) 00249 { 00250 int ret; 00251 00252 errno = 0; 00253 ret = ptrace (request, pid, (caddr_t) arg3, arg4); 00254 if (ret == -1 && errno == 0) 00255 ret = 0; 00256 00257 inferior_debug (4, _("ptrace (%s, %d, 0x%x, %d): %d (%s)\n"), 00258 name, pid, arg3, arg4, ret, 00259 (ret != 0) ? safe_strerror (errno) : _("no error")); 00260 return ret; 00261 } 00262 00263 static int 00264 cmp_thread_t (const void *l, const void *r) 00265 { 00266 thread_t tl = *(const thread_t *)l; 00267 thread_t tr = *(const thread_t *)r; 00268 return (int)(tl - tr); 00269 } 00270 00271 static void 00272 darwin_check_new_threads (struct inferior *inf) 00273 { 00274 kern_return_t kret; 00275 unsigned int i; 00276 thread_array_t thread_list; 00277 unsigned int new_nbr; 00278 unsigned int old_nbr; 00279 unsigned int new_ix, old_ix; 00280 darwin_inferior *darwin_inf = inf->private; 00281 VEC (darwin_thread_t) *thread_vec; 00282 00283 /* Get list of threads. */ 00284 kret = task_threads (darwin_inf->task, &thread_list, &new_nbr); 00285 MACH_CHECK_ERROR (kret); 00286 if (kret != KERN_SUCCESS) 00287 return; 00288 00289 /* Sort the list. */ 00290 if (new_nbr > 1) 00291 qsort (thread_list, new_nbr, sizeof (thread_t), cmp_thread_t); 00292 00293 if (darwin_inf->threads) 00294 old_nbr = VEC_length (darwin_thread_t, darwin_inf->threads); 00295 else 00296 old_nbr = 0; 00297 00298 /* Quick check for no changes. */ 00299 if (old_nbr == new_nbr) 00300 { 00301 for (i = 0; i < new_nbr; i++) 00302 if (thread_list[i] 00303 != VEC_index (darwin_thread_t, darwin_inf->threads, i)->gdb_port) 00304 break; 00305 if (i == new_nbr) 00306 { 00307 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list, 00308 new_nbr * sizeof (int)); 00309 MACH_CHECK_ERROR (kret); 00310 return; 00311 } 00312 } 00313 00314 thread_vec = VEC_alloc (darwin_thread_t, new_nbr); 00315 00316 for (new_ix = 0, old_ix = 0; new_ix < new_nbr || old_ix < old_nbr;) 00317 { 00318 thread_t new_id = (new_ix < new_nbr) ? 00319 thread_list[new_ix] : THREAD_NULL; 00320 darwin_thread_t *old = (old_ix < old_nbr) ? 00321 VEC_index (darwin_thread_t, darwin_inf->threads, old_ix) : NULL; 00322 thread_t old_id = old ? old->gdb_port : THREAD_NULL; 00323 00324 inferior_debug 00325 (12, _(" new_ix:%d/%d, old_ix:%d/%d, new_id:0x%x old_id:0x%x\n"), 00326 new_ix, new_nbr, old_ix, old_nbr, new_id, old_id); 00327 00328 if (old_id == new_id) 00329 { 00330 /* Thread still exist. */ 00331 VEC_safe_push (darwin_thread_t, thread_vec, old); 00332 new_ix++; 00333 old_ix++; 00334 00335 kret = mach_port_deallocate (gdb_task, old_id); 00336 MACH_CHECK_ERROR (kret); 00337 continue; 00338 } 00339 if (new_ix < new_nbr && new_id == MACH_PORT_DEAD) 00340 { 00341 /* Ignore dead ports. 00342 In some weird cases, we might get dead ports. They should 00343 correspond to dead thread so they could safely be ignored. */ 00344 new_ix++; 00345 continue; 00346 } 00347 if (new_ix < new_nbr && (old_ix == old_nbr || new_id < old_id)) 00348 { 00349 /* A thread was created. */ 00350 struct thread_info *tp; 00351 struct private_thread_info *pti; 00352 00353 pti = XZALLOC (struct private_thread_info); 00354 pti->gdb_port = new_id; 00355 pti->msg_state = DARWIN_RUNNING; 00356 00357 /* Add a new thread unless this is the first one ever met. */ 00358 if (!(old_nbr == 0 && new_ix == 0)) 00359 tp = add_thread_with_info (ptid_build (inf->pid, 0, new_id), pti); 00360 else 00361 { 00362 tp = find_thread_ptid (ptid_build (inf->pid, 0, 0)); 00363 gdb_assert (tp); 00364 tp->private = pti; 00365 } 00366 VEC_safe_push (darwin_thread_t, thread_vec, pti); 00367 new_ix++; 00368 continue; 00369 } 00370 if (old_ix < old_nbr && (new_ix == new_nbr || new_id > old_id)) 00371 { 00372 /* A thread was removed. */ 00373 delete_thread (ptid_build (inf->pid, 0, old_id)); 00374 kret = mach_port_deallocate (gdb_task, old_id); 00375 MACH_CHECK_ERROR (kret); 00376 old_ix++; 00377 continue; 00378 } 00379 gdb_assert_not_reached ("unexpected thread case"); 00380 } 00381 00382 if (darwin_inf->threads) 00383 VEC_free (darwin_thread_t, darwin_inf->threads); 00384 darwin_inf->threads = thread_vec; 00385 00386 kret = vm_deallocate (gdb_task, (vm_address_t) thread_list, 00387 new_nbr * sizeof (int)); 00388 MACH_CHECK_ERROR (kret); 00389 } 00390 00391 static int 00392 find_inferior_task_it (struct inferior *inf, void *port_ptr) 00393 { 00394 return inf->private->task == *(task_t*)port_ptr; 00395 } 00396 00397 static int 00398 find_inferior_notify_it (struct inferior *inf, void *port_ptr) 00399 { 00400 return inf->private->notify_port == *(task_t*)port_ptr; 00401 } 00402 00403 /* Return an inferior by task port. */ 00404 static struct inferior * 00405 darwin_find_inferior_by_task (task_t port) 00406 { 00407 return iterate_over_inferiors (&find_inferior_task_it, &port); 00408 } 00409 00410 /* Return an inferior by notification port. */ 00411 static struct inferior * 00412 darwin_find_inferior_by_notify (mach_port_t port) 00413 { 00414 return iterate_over_inferiors (&find_inferior_notify_it, &port); 00415 } 00416 00417 /* Return a thread by port. */ 00418 static darwin_thread_t * 00419 darwin_find_thread (struct inferior *inf, thread_t thread) 00420 { 00421 darwin_thread_t *t; 00422 int k; 00423 00424 for (k = 0; 00425 VEC_iterate (darwin_thread_t, inf->private->threads, k, t); 00426 k++) 00427 if (t->gdb_port == thread) 00428 return t; 00429 return NULL; 00430 } 00431 00432 /* Suspend (ie stop) an inferior at Mach level. */ 00433 00434 static void 00435 darwin_suspend_inferior (struct inferior *inf) 00436 { 00437 if (!inf->private->suspended) 00438 { 00439 kern_return_t kret; 00440 00441 kret = task_suspend (inf->private->task); 00442 MACH_CHECK_ERROR (kret); 00443 00444 inf->private->suspended = 1; 00445 } 00446 } 00447 00448 /* Resume an inferior at Mach level. */ 00449 00450 static void 00451 darwin_resume_inferior (struct inferior *inf) 00452 { 00453 if (inf->private->suspended) 00454 { 00455 kern_return_t kret; 00456 00457 kret = task_resume (inf->private->task); 00458 MACH_CHECK_ERROR (kret); 00459 00460 inf->private->suspended = 0; 00461 } 00462 } 00463 00464 /* Iterator functions. */ 00465 00466 static int 00467 darwin_suspend_inferior_it (struct inferior *inf, void *arg) 00468 { 00469 darwin_suspend_inferior (inf); 00470 darwin_check_new_threads (inf); 00471 return 0; 00472 } 00473 00474 static int 00475 darwin_resume_inferior_it (struct inferior *inf, void *arg) 00476 { 00477 darwin_resume_inferior (inf); 00478 return 0; 00479 } 00480 00481 static void 00482 darwin_dump_message (mach_msg_header_t *hdr, int disp_body) 00483 { 00484 printf_unfiltered (_("message header:\n")); 00485 printf_unfiltered (_(" bits: 0x%x\n"), hdr->msgh_bits); 00486 printf_unfiltered (_(" size: 0x%x\n"), hdr->msgh_size); 00487 printf_unfiltered (_(" remote-port: 0x%x\n"), hdr->msgh_remote_port); 00488 printf_unfiltered (_(" local-port: 0x%x\n"), hdr->msgh_local_port); 00489 printf_unfiltered (_(" reserved: 0x%x\n"), hdr->msgh_reserved); 00490 printf_unfiltered (_(" id: 0x%x\n"), hdr->msgh_id); 00491 00492 if (disp_body) 00493 { 00494 const unsigned char *data; 00495 const unsigned long *ldata; 00496 int size; 00497 int i; 00498 00499 data = (unsigned char *)(hdr + 1); 00500 size = hdr->msgh_size - sizeof (mach_msg_header_t); 00501 00502 if (hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX) 00503 { 00504 mach_msg_body_t *bod = (mach_msg_body_t*)data; 00505 mach_msg_port_descriptor_t *desc = 00506 (mach_msg_port_descriptor_t *)(bod + 1); 00507 int k; 00508 NDR_record_t *ndr; 00509 printf_unfiltered (_("body: descriptor_count=%u\n"), 00510 bod->msgh_descriptor_count); 00511 data += sizeof (mach_msg_body_t); 00512 size -= sizeof (mach_msg_body_t); 00513 for (k = 0; k < bod->msgh_descriptor_count; k++) 00514 switch (desc[k].type) 00515 { 00516 case MACH_MSG_PORT_DESCRIPTOR: 00517 printf_unfiltered 00518 (_(" descr %d: type=%u (port) name=0x%x, dispo=%d\n"), 00519 k, desc[k].type, desc[k].name, desc[k].disposition); 00520 break; 00521 default: 00522 printf_unfiltered (_(" descr %d: type=%u\n"), 00523 k, desc[k].type); 00524 break; 00525 } 00526 data += bod->msgh_descriptor_count 00527 * sizeof (mach_msg_port_descriptor_t); 00528 size -= bod->msgh_descriptor_count 00529 * sizeof (mach_msg_port_descriptor_t); 00530 ndr = (NDR_record_t *)(desc + bod->msgh_descriptor_count); 00531 printf_unfiltered 00532 (_("NDR: mig=%02x if=%02x encod=%02x " 00533 "int=%02x char=%02x float=%02x\n"), 00534 ndr->mig_vers, ndr->if_vers, ndr->mig_encoding, 00535 ndr->int_rep, ndr->char_rep, ndr->float_rep); 00536 data += sizeof (NDR_record_t); 00537 size -= sizeof (NDR_record_t); 00538 } 00539 00540 printf_unfiltered (_(" data:")); 00541 ldata = (const unsigned long *)data; 00542 for (i = 0; i < size / sizeof (unsigned long); i++) 00543 printf_unfiltered (" %08lx", ldata[i]); 00544 printf_unfiltered (_("\n")); 00545 } 00546 } 00547 00548 static int 00549 darwin_decode_exception_message (mach_msg_header_t *hdr, 00550 struct inferior **pinf, 00551 darwin_thread_t **pthread) 00552 { 00553 mach_msg_body_t *bod = (mach_msg_body_t*)(hdr + 1); 00554 mach_msg_port_descriptor_t *desc = (mach_msg_port_descriptor_t *)(bod + 1); 00555 NDR_record_t *ndr; 00556 integer_t *data; 00557 struct inferior *inf; 00558 darwin_thread_t *thread; 00559 task_t task_port; 00560 thread_t thread_port; 00561 kern_return_t kret; 00562 int i; 00563 00564 /* Check message identifier. 2401 is exc. */ 00565 if (hdr->msgh_id != 2401) 00566 return -1; 00567 00568 /* Check message header. */ 00569 if (!(hdr->msgh_bits & MACH_MSGH_BITS_COMPLEX)) 00570 return -1; 00571 00572 /* Check descriptors. */ 00573 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc) 00574 + sizeof (*ndr) + 2 * sizeof (integer_t)) 00575 || bod->msgh_descriptor_count != 2 00576 || desc[0].type != MACH_MSG_PORT_DESCRIPTOR 00577 || desc[0].disposition != MACH_MSG_TYPE_MOVE_SEND 00578 || desc[1].type != MACH_MSG_PORT_DESCRIPTOR 00579 || desc[1].disposition != MACH_MSG_TYPE_MOVE_SEND) 00580 return -1; 00581 00582 /* Check data representation. */ 00583 ndr = (NDR_record_t *)(desc + 2); 00584 if (ndr->mig_vers != NDR_PROTOCOL_2_0 00585 || ndr->if_vers != NDR_PROTOCOL_2_0 00586 || ndr->mig_encoding != NDR_record.mig_encoding 00587 || ndr->int_rep != NDR_record.int_rep 00588 || ndr->char_rep != NDR_record.char_rep 00589 || ndr->float_rep != NDR_record.float_rep) 00590 return -1; 00591 00592 /* Ok, the hard work. */ 00593 data = (integer_t *)(ndr + 1); 00594 00595 /* Find process by port. */ 00596 task_port = desc[1].name; 00597 thread_port = desc[0].name; 00598 inf = darwin_find_inferior_by_task (task_port); 00599 if (inf == NULL) 00600 return -1; 00601 *pinf = inf; 00602 00603 /* Find thread by port. */ 00604 /* Check for new threads. Do it early so that the port in the exception 00605 message can be deallocated. */ 00606 darwin_check_new_threads (inf); 00607 00608 /* We got new rights to the task and the thread. Get rid of them. */ 00609 kret = mach_port_deallocate (mach_task_self (), task_port); 00610 MACH_CHECK_ERROR (kret); 00611 kret = mach_port_deallocate (mach_task_self (), thread_port); 00612 MACH_CHECK_ERROR (kret); 00613 00614 thread = darwin_find_thread (inf, thread_port); 00615 if (thread == NULL) 00616 return -1; 00617 *pthread = thread; 00618 00619 /* The thread should be running. However we have observed cases where a thread 00620 got a SIGTTIN message after being stopped. */ 00621 gdb_assert (thread->msg_state != DARWIN_MESSAGE); 00622 00623 /* Finish decoding. */ 00624 thread->event.header = *hdr; 00625 thread->event.thread_port = thread_port; 00626 thread->event.task_port = task_port; 00627 thread->event.ex_type = data[0]; 00628 thread->event.data_count = data[1]; 00629 00630 if (hdr->msgh_size < (sizeof (*hdr) + sizeof (*bod) + 2 * sizeof (*desc) 00631 + sizeof (*ndr) + 2 * sizeof (integer_t) 00632 + data[1] * sizeof (integer_t))) 00633 return -1; 00634 for (i = 0; i < data[1]; i++) 00635 thread->event.ex_data[i] = data[2 + i]; 00636 00637 thread->msg_state = DARWIN_MESSAGE; 00638 00639 return 0; 00640 } 00641 00642 static void 00643 darwin_encode_reply (mig_reply_error_t *reply, mach_msg_header_t *hdr, 00644 integer_t code) 00645 { 00646 mach_msg_header_t *rh = &reply->Head; 00647 rh->msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(hdr->msgh_bits), 0); 00648 rh->msgh_remote_port = hdr->msgh_remote_port; 00649 rh->msgh_size = (mach_msg_size_t)sizeof(mig_reply_error_t); 00650 rh->msgh_local_port = MACH_PORT_NULL; 00651 rh->msgh_id = hdr->msgh_id + 100; 00652 00653 reply->NDR = NDR_record; 00654 reply->RetCode = code; 00655 } 00656 00657 static void 00658 darwin_send_reply (struct inferior *inf, darwin_thread_t *thread) 00659 { 00660 kern_return_t kret; 00661 mig_reply_error_t reply; 00662 00663 darwin_encode_reply (&reply, &thread->event.header, KERN_SUCCESS); 00664 00665 kret = mach_msg (&reply.Head, MACH_SEND_MSG | MACH_SEND_INTERRUPT, 00666 reply.Head.msgh_size, 0, 00667 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, 00668 MACH_PORT_NULL); 00669 MACH_CHECK_ERROR (kret); 00670 00671 inf->private->pending_messages--; 00672 } 00673 00674 static void 00675 darwin_resume_thread (struct inferior *inf, darwin_thread_t *thread, 00676 int step, int nsignal) 00677 { 00678 kern_return_t kret; 00679 int res; 00680 00681 inferior_debug 00682 (3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"), 00683 thread->msg_state, thread->gdb_port, step, nsignal); 00684 00685 switch (thread->msg_state) 00686 { 00687 case DARWIN_MESSAGE: 00688 if (thread->event.ex_type == EXC_SOFTWARE 00689 && thread->event.ex_data[0] == EXC_SOFT_SIGNAL) 00690 { 00691 /* Either deliver a new signal or cancel the signal received. */ 00692 res = PTRACE (PT_THUPDATE, inf->pid, 00693 (void *)(uintptr_t)thread->gdb_port, nsignal); 00694 if (res < 0) 00695 inferior_debug (1, _("ptrace THUP: res=%d\n"), res); 00696 } 00697 else if (nsignal) 00698 { 00699 /* Note: ptrace is allowed only if the process is stopped. 00700 Directly send the signal to the thread. */ 00701 res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal); 00702 inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"), 00703 thread->gdb_port, nsignal, res); 00704 thread->signaled = 1; 00705 } 00706 00707 /* Set or reset single step. */ 00708 if (step != thread->single_step) 00709 { 00710 inferior_debug (4, _("darwin_set_sstep (thread=0x%x, enable=%d)\n"), 00711 thread->gdb_port, step); 00712 darwin_set_sstep (thread->gdb_port, step); 00713 thread->single_step = step; 00714 } 00715 00716 darwin_send_reply (inf, thread); 00717 thread->msg_state = DARWIN_RUNNING; 00718 break; 00719 00720 case DARWIN_RUNNING: 00721 break; 00722 00723 case DARWIN_STOPPED: 00724 kret = thread_resume (thread->gdb_port); 00725 MACH_CHECK_ERROR (kret); 00726 00727 thread->msg_state = DARWIN_RUNNING; 00728 break; 00729 } 00730 } 00731 00732 /* Resume all threads of the inferior. */ 00733 00734 static void 00735 darwin_resume_inferior_threads (struct inferior *inf, int step, int nsignal) 00736 { 00737 darwin_thread_t *thread; 00738 int k; 00739 00740 for (k = 0; 00741 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread); 00742 k++) 00743 darwin_resume_thread (inf, thread, step, nsignal); 00744 } 00745 00746 struct resume_inferior_threads_param 00747 { 00748 int step; 00749 int nsignal; 00750 }; 00751 00752 static int 00753 darwin_resume_inferior_threads_it (struct inferior *inf, void *param) 00754 { 00755 int step = ((struct resume_inferior_threads_param *)param)->step; 00756 int nsignal = ((struct resume_inferior_threads_param *)param)->nsignal; 00757 00758 darwin_resume_inferior_threads (inf, step, nsignal); 00759 00760 return 0; 00761 } 00762 00763 /* Suspend all threads of INF. */ 00764 00765 static void 00766 darwin_suspend_inferior_threads (struct inferior *inf) 00767 { 00768 darwin_thread_t *thread; 00769 kern_return_t kret; 00770 int k; 00771 00772 for (k = 0; 00773 VEC_iterate (darwin_thread_t, inf->private->threads, k, thread); 00774 k++) 00775 switch (thread->msg_state) 00776 { 00777 case DARWIN_STOPPED: 00778 case DARWIN_MESSAGE: 00779 break; 00780 case DARWIN_RUNNING: 00781 kret = thread_suspend (thread->gdb_port); 00782 MACH_CHECK_ERROR (kret); 00783 thread->msg_state = DARWIN_STOPPED; 00784 break; 00785 } 00786 } 00787 00788 static void 00789 darwin_resume (ptid_t ptid, int step, enum gdb_signal signal) 00790 { 00791 struct target_waitstatus status; 00792 int pid; 00793 00794 kern_return_t kret; 00795 int res; 00796 int nsignal; 00797 struct inferior *inf; 00798 00799 inferior_debug 00800 (2, _("darwin_resume: pid=%d, tid=0x%x, step=%d, signal=%d\n"), 00801 ptid_get_pid (ptid), ptid_get_tid (ptid), step, signal); 00802 00803 if (signal == GDB_SIGNAL_0) 00804 nsignal = 0; 00805 else 00806 nsignal = gdb_signal_to_host (signal); 00807 00808 /* Don't try to single step all threads. */ 00809 if (step) 00810 ptid = inferior_ptid; 00811 00812 /* minus_one_ptid is RESUME_ALL. */ 00813 if (ptid_equal (ptid, minus_one_ptid)) 00814 { 00815 struct resume_inferior_threads_param param; 00816 00817 param.nsignal = nsignal; 00818 param.step = step; 00819 00820 /* Resume threads. */ 00821 iterate_over_inferiors (darwin_resume_inferior_threads_it, ¶m); 00822 /* Resume tasks. */ 00823 iterate_over_inferiors (darwin_resume_inferior_it, NULL); 00824 } 00825 else 00826 { 00827 struct inferior *inf = find_inferior_pid (ptid_get_pid (ptid)); 00828 long tid = ptid_get_tid (ptid); 00829 00830 /* Stop the inferior (should be useless). */ 00831 darwin_suspend_inferior (inf); 00832 00833 if (tid == 0) 00834 darwin_resume_inferior_threads (inf, step, nsignal); 00835 else 00836 { 00837 darwin_thread_t *thread; 00838 00839 /* Suspend threads of the task. */ 00840 darwin_suspend_inferior_threads (inf); 00841 00842 /* Resume the selected thread. */ 00843 thread = darwin_find_thread (inf, tid); 00844 gdb_assert (thread); 00845 darwin_resume_thread (inf, thread, step, nsignal); 00846 } 00847 00848 /* Resume the task. */ 00849 darwin_resume_inferior (inf); 00850 } 00851 } 00852 00853 static void 00854 darwin_resume_to (struct target_ops *ops, ptid_t ptid, int step, 00855 enum gdb_signal signal) 00856 { 00857 return darwin_resume (ptid, step, signal); 00858 } 00859 00860 static ptid_t 00861 darwin_decode_message (mach_msg_header_t *hdr, 00862 darwin_thread_t **pthread, 00863 struct inferior **pinf, 00864 struct target_waitstatus *status) 00865 { 00866 darwin_thread_t *thread; 00867 struct inferior *inf; 00868 00869 /* Exception message. */ 00870 if (hdr->msgh_local_port == darwin_ex_port) 00871 { 00872 int res; 00873 00874 /* Decode message. */ 00875 res = darwin_decode_exception_message (hdr, &inf, &thread); 00876 00877 if (res < 0) 00878 { 00879 /* Should not happen... */ 00880 printf_unfiltered 00881 (_("darwin_wait: ill-formatted message (id=0x%x)\n"), hdr->msgh_id); 00882 /* FIXME: send a failure reply? */ 00883 status->kind = TARGET_WAITKIND_SPURIOUS; 00884 return minus_one_ptid; 00885 } 00886 *pinf = inf; 00887 *pthread = thread; 00888 inf->private->pending_messages++; 00889 00890 status->kind = TARGET_WAITKIND_STOPPED; 00891 thread->msg_state = DARWIN_MESSAGE; 00892 00893 inferior_debug (4, _("darwin_wait: thread=0x%x, got %s\n"), 00894 thread->gdb_port, 00895 unparse_exception_type (thread->event.ex_type)); 00896 00897 switch (thread->event.ex_type) 00898 { 00899 case EXC_BAD_ACCESS: 00900 status->value.sig = TARGET_EXC_BAD_ACCESS; 00901 break; 00902 case EXC_BAD_INSTRUCTION: 00903 status->value.sig = TARGET_EXC_BAD_INSTRUCTION; 00904 break; 00905 case EXC_ARITHMETIC: 00906 status->value.sig = TARGET_EXC_ARITHMETIC; 00907 break; 00908 case EXC_EMULATION: 00909 status->value.sig = TARGET_EXC_EMULATION; 00910 break; 00911 case EXC_SOFTWARE: 00912 if (thread->event.ex_data[0] == EXC_SOFT_SIGNAL) 00913 { 00914 status->value.sig = 00915 gdb_signal_from_host (thread->event.ex_data[1]); 00916 inferior_debug (5, _(" (signal %d: %s)\n"), 00917 thread->event.ex_data[1], 00918 gdb_signal_to_name (status->value.sig)); 00919 00920 /* If the thread is stopped because it has received a signal 00921 that gdb has just sent, continue. */ 00922 if (thread->signaled) 00923 { 00924 thread->signaled = 0; 00925 darwin_send_reply (inf, thread); 00926 thread->msg_state = DARWIN_RUNNING; 00927 status->kind = TARGET_WAITKIND_IGNORE; 00928 } 00929 } 00930 else 00931 status->value.sig = TARGET_EXC_SOFTWARE; 00932 break; 00933 case EXC_BREAKPOINT: 00934 /* Many internal GDB routines expect breakpoints to be reported 00935 as GDB_SIGNAL_TRAP, and will report TARGET_EXC_BREAKPOINT 00936 as a spurious signal. */ 00937 status->value.sig = GDB_SIGNAL_TRAP; 00938 break; 00939 default: 00940 status->value.sig = GDB_SIGNAL_UNKNOWN; 00941 break; 00942 } 00943 00944 return ptid_build (inf->pid, 0, thread->gdb_port); 00945 } 00946 00947 *pinf = NULL; 00948 *pthread = NULL; 00949 00950 inf = darwin_find_inferior_by_notify (hdr->msgh_local_port); 00951 if (inf != NULL) 00952 { 00953 if (!inf->private->no_ptrace) 00954 { 00955 pid_t res; 00956 int wstatus; 00957 00958 res = wait4 (inf->pid, &wstatus, 0, NULL); 00959 if (res < 0 || res != inf->pid) 00960 { 00961 printf_unfiltered (_("wait4: res=%d: %s\n"), 00962 res, safe_strerror (errno)); 00963 status->kind = TARGET_WAITKIND_SPURIOUS; 00964 return minus_one_ptid; 00965 } 00966 if (WIFEXITED (wstatus)) 00967 { 00968 status->kind = TARGET_WAITKIND_EXITED; 00969 status->value.integer = WEXITSTATUS (wstatus); 00970 } 00971 else 00972 { 00973 status->kind = TARGET_WAITKIND_SIGNALLED; 00974 status->value.sig = WTERMSIG (wstatus); 00975 } 00976 00977 inferior_debug (4, _("darwin_wait: pid=%d exit, status=0x%x\n"), 00978 res, wstatus); 00979 00980 /* Looks necessary on Leopard and harmless... */ 00981 wait4 (inf->pid, &wstatus, 0, NULL); 00982 00983 return ptid_build (inf->pid, 0, 0); 00984 } 00985 else 00986 { 00987 inferior_debug (4, _("darwin_wait: pid=%d\n"), inf->pid); 00988 status->kind = TARGET_WAITKIND_EXITED; 00989 status->value.integer = 0; /* Don't know. */ 00990 return ptid_build (inf->pid, 0, 0); 00991 } 00992 } 00993 00994 printf_unfiltered (_("Bad local-port: 0x%x\n"), hdr->msgh_local_port); 00995 status->kind = TARGET_WAITKIND_SPURIOUS; 00996 return minus_one_ptid; 00997 } 00998 00999 static int 01000 cancel_breakpoint (ptid_t ptid) 01001 { 01002 /* Arrange for a breakpoint to be hit again later. We will handle 01003 the current event, eventually we will resume this thread, and this 01004 breakpoint will trap again. 01005 01006 If we do not do this, then we run the risk that the user will 01007 delete or disable the breakpoint, but the thread will have already 01008 tripped on it. */ 01009 01010 struct regcache *regcache = get_thread_regcache (ptid); 01011 struct gdbarch *gdbarch = get_regcache_arch (regcache); 01012 CORE_ADDR pc; 01013 01014 pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch); 01015 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc)) 01016 { 01017 inferior_debug (4, "cancel_breakpoint for thread 0x%x\n", 01018 ptid_get_tid (ptid)); 01019 01020 /* Back up the PC if necessary. */ 01021 if (gdbarch_decr_pc_after_break (gdbarch)) 01022 regcache_write_pc (regcache, pc); 01023 01024 return 1; 01025 } 01026 return 0; 01027 } 01028 01029 static ptid_t 01030 darwin_wait (ptid_t ptid, struct target_waitstatus *status) 01031 { 01032 kern_return_t kret; 01033 union 01034 { 01035 mach_msg_header_t hdr; 01036 char data[0x100]; 01037 } msgin; 01038 mach_msg_header_t *hdr = &msgin.hdr; 01039 ptid_t res; 01040 darwin_thread_t *thread; 01041 struct inferior *inf; 01042 01043 inferior_debug 01044 (2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"), 01045 ptid_get_pid (ptid), ptid_get_tid (ptid)); 01046 01047 /* Handle fake stop events at first. */ 01048 if (darwin_inf_fake_stop != NULL) 01049 { 01050 inf = darwin_inf_fake_stop; 01051 darwin_inf_fake_stop = NULL; 01052 01053 status->kind = TARGET_WAITKIND_STOPPED; 01054 status->value.sig = GDB_SIGNAL_TRAP; 01055 thread = VEC_index (darwin_thread_t, inf->private->threads, 0); 01056 thread->msg_state = DARWIN_STOPPED; 01057 return ptid_build (inf->pid, 0, thread->gdb_port); 01058 } 01059 01060 do 01061 { 01062 /* set_sigint_trap (); */ 01063 01064 /* Wait for a message. */ 01065 kret = mach_msg (&msgin.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0, 01066 sizeof (msgin.data), darwin_port_set, 0, MACH_PORT_NULL); 01067 01068 /* clear_sigint_trap (); */ 01069 01070 if (kret == MACH_RCV_INTERRUPTED) 01071 { 01072 status->kind = TARGET_WAITKIND_IGNORE; 01073 return minus_one_ptid; 01074 } 01075 01076 if (kret != MACH_MSG_SUCCESS) 01077 { 01078 inferior_debug (5, _("mach_msg: ret=0x%x\n"), kret); 01079 status->kind = TARGET_WAITKIND_SPURIOUS; 01080 return minus_one_ptid; 01081 } 01082 01083 /* Debug: display message. */ 01084 if (darwin_debug_flag > 10) 01085 darwin_dump_message (hdr, darwin_debug_flag > 11); 01086 01087 res = darwin_decode_message (hdr, &thread, &inf, status); 01088 01089 if (inf == NULL) 01090 return res; 01091 } 01092 while (status->kind == TARGET_WAITKIND_IGNORE); 01093 01094 /* Stop all tasks. */ 01095 iterate_over_inferiors (darwin_suspend_inferior_it, NULL); 01096 01097 /* Read pending messages. */ 01098 while (1) 01099 { 01100 struct target_waitstatus status2; 01101 ptid_t ptid2; 01102 01103 kret = mach_msg (&msgin.hdr, 01104 MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, 01105 sizeof (msgin.data), darwin_port_set, 1, MACH_PORT_NULL); 01106 01107 if (kret == MACH_RCV_TIMED_OUT) 01108 break; 01109 if (kret != MACH_MSG_SUCCESS) 01110 { 01111 inferior_debug 01112 (5, _("darwin_wait: mach_msg(pending) ret=0x%x\n"), kret); 01113 break; 01114 } 01115 01116 ptid2 = darwin_decode_message (hdr, &thread, &inf, &status2); 01117 01118 if (inf != NULL && thread != NULL 01119 && thread->event.ex_type == EXC_BREAKPOINT) 01120 { 01121 if (thread->single_step 01122 || cancel_breakpoint (ptid_build (inf->pid, 0, thread->gdb_port))) 01123 { 01124 gdb_assert (thread->msg_state == DARWIN_MESSAGE); 01125 darwin_send_reply (inf, thread); 01126 thread->msg_state = DARWIN_RUNNING; 01127 } 01128 else 01129 inferior_debug 01130 (3, _("darwin_wait: thread 0x%x hit a non-gdb breakpoint\n"), 01131 thread->gdb_port); 01132 } 01133 else 01134 inferior_debug (3, _("darwin_wait: unhandled pending message\n")); 01135 } 01136 return res; 01137 } 01138 01139 static ptid_t 01140 darwin_wait_to (struct target_ops *ops, 01141 ptid_t ptid, struct target_waitstatus *status, int options) 01142 { 01143 return darwin_wait (ptid, status); 01144 } 01145 01146 static void 01147 darwin_stop (ptid_t t) 01148 { 01149 struct inferior *inf = current_inferior (); 01150 01151 /* FIXME: handle in no_ptrace mode. */ 01152 gdb_assert (!inf->private->no_ptrace); 01153 kill (inf->pid, SIGINT); 01154 } 01155 01156 static void 01157 darwin_mourn_inferior (struct target_ops *ops) 01158 { 01159 struct inferior *inf = current_inferior (); 01160 kern_return_t kret; 01161 mach_port_t prev; 01162 int i; 01163 01164 unpush_target (darwin_ops); 01165 01166 /* Deallocate threads. */ 01167 if (inf->private->threads) 01168 { 01169 int k; 01170 darwin_thread_t *t; 01171 for (k = 0; 01172 VEC_iterate (darwin_thread_t, inf->private->threads, k, t); 01173 k++) 01174 { 01175 kret = mach_port_deallocate (gdb_task, t->gdb_port); 01176 MACH_CHECK_ERROR (kret); 01177 } 01178 VEC_free (darwin_thread_t, inf->private->threads); 01179 inf->private->threads = NULL; 01180 } 01181 01182 kret = mach_port_move_member (gdb_task, 01183 inf->private->notify_port, MACH_PORT_NULL); 01184 MACH_CHECK_ERROR (kret); 01185 01186 kret = mach_port_request_notification (gdb_task, inf->private->task, 01187 MACH_NOTIFY_DEAD_NAME, 0, 01188 MACH_PORT_NULL, 01189 MACH_MSG_TYPE_MAKE_SEND_ONCE, 01190 &prev); 01191 /* This can fail if the task is dead. */ 01192 inferior_debug (4, "task=0x%x, prev=0x%x, notify_port=0x%x\n", 01193 inf->private->task, prev, inf->private->notify_port); 01194 01195 if (kret == KERN_SUCCESS) 01196 { 01197 kret = mach_port_deallocate (gdb_task, prev); 01198 MACH_CHECK_ERROR (kret); 01199 } 01200 01201 kret = mach_port_destroy (gdb_task, inf->private->notify_port); 01202 MACH_CHECK_ERROR (kret); 01203 01204 01205 /* Deallocate saved exception ports. */ 01206 for (i = 0; i < inf->private->exception_info.count; i++) 01207 { 01208 kret = mach_port_deallocate 01209 (gdb_task, inf->private->exception_info.ports[i]); 01210 MACH_CHECK_ERROR (kret); 01211 } 01212 inf->private->exception_info.count = 0; 01213 01214 kret = mach_port_deallocate (gdb_task, inf->private->task); 01215 MACH_CHECK_ERROR (kret); 01216 01217 xfree (inf->private); 01218 inf->private = NULL; 01219 01220 generic_mourn_inferior (); 01221 } 01222 01223 static void 01224 darwin_reply_to_all_pending_messages (struct inferior *inf) 01225 { 01226 int k; 01227 darwin_thread_t *t; 01228 01229 for (k = 0; 01230 VEC_iterate (darwin_thread_t, inf->private->threads, k, t); 01231 k++) 01232 { 01233 if (t->msg_state == DARWIN_MESSAGE) 01234 darwin_resume_thread (inf, t, 0, 0); 01235 } 01236 } 01237 01238 static void 01239 darwin_stop_inferior (struct inferior *inf) 01240 { 01241 struct target_waitstatus wstatus; 01242 ptid_t ptid; 01243 kern_return_t kret; 01244 int status; 01245 int res; 01246 01247 gdb_assert (inf != NULL); 01248 01249 darwin_suspend_inferior (inf); 01250 01251 darwin_reply_to_all_pending_messages (inf); 01252 01253 if (inf->private->no_ptrace) 01254 return; 01255 01256 res = kill (inf->pid, SIGSTOP); 01257 if (res != 0) 01258 warning (_("cannot kill: %s"), safe_strerror (errno)); 01259 01260 /* Wait until the process is really stopped. */ 01261 while (1) 01262 { 01263 ptid = darwin_wait (inferior_ptid, &wstatus); 01264 if (wstatus.kind == TARGET_WAITKIND_STOPPED 01265 && wstatus.value.sig == GDB_SIGNAL_STOP) 01266 break; 01267 } 01268 } 01269 01270 static kern_return_t 01271 darwin_save_exception_ports (darwin_inferior *inf) 01272 { 01273 kern_return_t kret; 01274 01275 inf->exception_info.count = 01276 sizeof (inf->exception_info.ports) / sizeof (inf->exception_info.ports[0]); 01277 01278 kret = task_get_exception_ports 01279 (inf->task, EXC_MASK_ALL, inf->exception_info.masks, 01280 &inf->exception_info.count, inf->exception_info.ports, 01281 inf->exception_info.behaviors, inf->exception_info.flavors); 01282 return kret; 01283 } 01284 01285 static kern_return_t 01286 darwin_restore_exception_ports (darwin_inferior *inf) 01287 { 01288 int i; 01289 kern_return_t kret; 01290 01291 for (i = 0; i < inf->exception_info.count; i++) 01292 { 01293 kret = task_set_exception_ports 01294 (inf->task, inf->exception_info.masks[i], inf->exception_info.ports[i], 01295 inf->exception_info.behaviors[i], inf->exception_info.flavors[i]); 01296 if (kret != KERN_SUCCESS) 01297 return kret; 01298 } 01299 01300 return KERN_SUCCESS; 01301 } 01302 01303 static void 01304 darwin_kill_inferior (struct target_ops *ops) 01305 { 01306 struct inferior *inf = current_inferior (); 01307 struct target_waitstatus wstatus; 01308 ptid_t ptid; 01309 kern_return_t kret; 01310 int status; 01311 int res; 01312 01313 if (ptid_equal (inferior_ptid, null_ptid)) 01314 return; 01315 01316 gdb_assert (inf != NULL); 01317 01318 kret = darwin_restore_exception_ports (inf->private); 01319 MACH_CHECK_ERROR (kret); 01320 01321 darwin_reply_to_all_pending_messages (inf); 01322 01323 res = kill (inf->pid, 9); 01324 01325 if (res == 0) 01326 { 01327 darwin_resume_inferior (inf); 01328 01329 ptid = darwin_wait (inferior_ptid, &wstatus); 01330 } 01331 else if (errno != ESRCH) 01332 warning (_("Failed to kill inferior: kill (%d, 9) returned [%s]"), 01333 inf->pid, safe_strerror (errno)); 01334 01335 target_mourn_inferior (); 01336 } 01337 01338 static void 01339 darwin_attach_pid (struct inferior *inf) 01340 { 01341 kern_return_t kret; 01342 mach_port_t prev_port; 01343 int traps_expected; 01344 mach_port_t prev_not; 01345 exception_mask_t mask; 01346 01347 inf->private = XZALLOC (darwin_inferior); 01348 01349 kret = task_for_pid (gdb_task, inf->pid, &inf->private->task); 01350 if (kret != KERN_SUCCESS) 01351 { 01352 int status; 01353 01354 if (!inf->attach_flag) 01355 { 01356 kill (inf->pid, 9); 01357 waitpid (inf->pid, &status, 0); 01358 } 01359 01360 error (_("Unable to find Mach task port for process-id %d: %s (0x%lx).\n" 01361 " (please check gdb is codesigned - see taskgated(8))"), 01362 inf->pid, mach_error_string (kret), (unsigned long) kret); 01363 } 01364 01365 inferior_debug (2, _("inferior task: 0x%x, pid: %d\n"), 01366 inf->private->task, inf->pid); 01367 01368 if (darwin_ex_port == MACH_PORT_NULL) 01369 { 01370 /* Create a port to get exceptions. */ 01371 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE, 01372 &darwin_ex_port); 01373 if (kret != KERN_SUCCESS) 01374 error (_("Unable to create exception port, mach_port_allocate " 01375 "returned: %d"), 01376 kret); 01377 01378 kret = mach_port_insert_right (gdb_task, darwin_ex_port, darwin_ex_port, 01379 MACH_MSG_TYPE_MAKE_SEND); 01380 if (kret != KERN_SUCCESS) 01381 error (_("Unable to create exception port, mach_port_insert_right " 01382 "returned: %d"), 01383 kret); 01384 01385 /* Create a port set and put ex_port in it. */ 01386 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_PORT_SET, 01387 &darwin_port_set); 01388 if (kret != KERN_SUCCESS) 01389 error (_("Unable to create port set, mach_port_allocate " 01390 "returned: %d"), 01391 kret); 01392 01393 kret = mach_port_move_member (gdb_task, darwin_ex_port, darwin_port_set); 01394 if (kret != KERN_SUCCESS) 01395 error (_("Unable to move exception port into new port set, " 01396 "mach_port_move_member\n" 01397 "returned: %d"), 01398 kret); 01399 } 01400 01401 /* Create a port to be notified when the child task terminates. */ 01402 kret = mach_port_allocate (gdb_task, MACH_PORT_RIGHT_RECEIVE, 01403 &inf->private->notify_port); 01404 if (kret != KERN_SUCCESS) 01405 error (_("Unable to create notification port, mach_port_allocate " 01406 "returned: %d"), 01407 kret); 01408 01409 kret = mach_port_move_member (gdb_task, 01410 inf->private->notify_port, darwin_port_set); 01411 if (kret != KERN_SUCCESS) 01412 error (_("Unable to move notification port into new port set, " 01413 "mach_port_move_member\n" 01414 "returned: %d"), 01415 kret); 01416 01417 kret = mach_port_request_notification (gdb_task, inf->private->task, 01418 MACH_NOTIFY_DEAD_NAME, 0, 01419 inf->private->notify_port, 01420 MACH_MSG_TYPE_MAKE_SEND_ONCE, 01421 &prev_not); 01422 if (kret != KERN_SUCCESS) 01423 error (_("Termination notification request failed, " 01424 "mach_port_request_notification\n" 01425 "returned: %d"), 01426 kret); 01427 if (prev_not != MACH_PORT_NULL) 01428 { 01429 /* This is unexpected, as there should not be any previously 01430 registered notification request. But this is not a fatal 01431 issue, so just emit a warning. */ 01432 warning (_("\ 01433 A task termination request was registered before the debugger registered\n\ 01434 its own. This is unexpected, but should otherwise not have any actual\n\ 01435 impact on the debugging session.")); 01436 } 01437 01438 kret = darwin_save_exception_ports (inf->private); 01439 if (kret != KERN_SUCCESS) 01440 error (_("Unable to save exception ports, task_get_exception_ports" 01441 "returned: %d"), 01442 kret); 01443 01444 /* Set exception port. */ 01445 if (enable_mach_exceptions) 01446 mask = EXC_MASK_ALL; 01447 else 01448 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT; 01449 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port, 01450 EXCEPTION_DEFAULT, THREAD_STATE_NONE); 01451 if (kret != KERN_SUCCESS) 01452 error (_("Unable to set exception ports, task_set_exception_ports" 01453 "returned: %d"), 01454 kret); 01455 01456 push_target (darwin_ops); 01457 } 01458 01459 static void 01460 darwin_init_thread_list (struct inferior *inf) 01461 { 01462 darwin_thread_t *thread; 01463 ptid_t new_ptid; 01464 01465 darwin_check_new_threads (inf); 01466 01467 gdb_assert (inf->private->threads 01468 && VEC_length (darwin_thread_t, inf->private->threads) > 0); 01469 thread = VEC_index (darwin_thread_t, inf->private->threads, 0); 01470 01471 /* Note: fork_inferior automatically add a thead but it uses a wrong ptid. 01472 Fix up. */ 01473 new_ptid = ptid_build (inf->pid, 0, thread->gdb_port); 01474 thread_change_ptid (inferior_ptid, new_ptid); 01475 inferior_ptid = new_ptid; 01476 } 01477 01478 /* The child must synchronize with gdb: gdb must set the exception port 01479 before the child call PTRACE_SIGEXC. We use a pipe to achieve this. 01480 FIXME: is there a lighter way ? */ 01481 static int ptrace_fds[2]; 01482 01483 static void 01484 darwin_ptrace_me (void) 01485 { 01486 int res; 01487 char c; 01488 01489 /* Close write end point. */ 01490 close (ptrace_fds[1]); 01491 01492 /* Wait until gdb is ready. */ 01493 res = read (ptrace_fds[0], &c, 1); 01494 if (res != 0) 01495 error (_("unable to read from pipe, read returned: %d"), res); 01496 close (ptrace_fds[0]); 01497 01498 /* Get rid of privileges. */ 01499 setegid (getgid ()); 01500 01501 /* Set TRACEME. */ 01502 PTRACE (PT_TRACE_ME, 0, 0, 0); 01503 01504 /* Redirect signals to exception port. */ 01505 PTRACE (PT_SIGEXC, 0, 0, 0); 01506 } 01507 01508 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2). */ 01509 static void 01510 darwin_pre_ptrace (void) 01511 { 01512 if (pipe (ptrace_fds) != 0) 01513 { 01514 ptrace_fds[0] = -1; 01515 ptrace_fds[1] = -1; 01516 error (_("unable to create a pipe: %s"), safe_strerror (errno)); 01517 } 01518 01519 mark_fd_no_cloexec (ptrace_fds[0]); 01520 mark_fd_no_cloexec (ptrace_fds[1]); 01521 } 01522 01523 static void 01524 darwin_ptrace_him (int pid) 01525 { 01526 task_t itask; 01527 kern_return_t kret; 01528 mach_port_t prev_port; 01529 int traps_expected; 01530 struct inferior *inf = current_inferior (); 01531 01532 darwin_attach_pid (inf); 01533 01534 /* Let's the child run. */ 01535 close (ptrace_fds[0]); 01536 close (ptrace_fds[1]); 01537 01538 unmark_fd_no_cloexec (ptrace_fds[0]); 01539 unmark_fd_no_cloexec (ptrace_fds[1]); 01540 01541 darwin_init_thread_list (inf); 01542 01543 startup_inferior (START_INFERIOR_TRAPS_EXPECTED); 01544 } 01545 01546 static void 01547 darwin_execvp (const char *file, char * const argv[], char * const env[]) 01548 { 01549 posix_spawnattr_t attr; 01550 short ps_flags = 0; 01551 int res; 01552 01553 res = posix_spawnattr_init (&attr); 01554 if (res != 0) 01555 { 01556 fprintf_unfiltered 01557 (gdb_stderr, "Cannot initialize attribute for posix_spawn\n"); 01558 return; 01559 } 01560 01561 /* Do like execve: replace the image. */ 01562 ps_flags = POSIX_SPAWN_SETEXEC; 01563 01564 /* Disable ASLR. The constant doesn't look to be available outside the 01565 kernel include files. */ 01566 #ifndef _POSIX_SPAWN_DISABLE_ASLR 01567 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 01568 #endif 01569 ps_flags |= _POSIX_SPAWN_DISABLE_ASLR; 01570 res = posix_spawnattr_setflags (&attr, ps_flags); 01571 if (res != 0) 01572 { 01573 fprintf_unfiltered (gdb_stderr, "Cannot set posix_spawn flags\n"); 01574 return; 01575 } 01576 01577 posix_spawnp (NULL, argv[0], NULL, &attr, argv, env); 01578 } 01579 01580 static void 01581 darwin_create_inferior (struct target_ops *ops, char *exec_file, 01582 char *allargs, char **env, int from_tty) 01583 { 01584 /* Do the hard work. */ 01585 fork_inferior (exec_file, allargs, env, darwin_ptrace_me, darwin_ptrace_him, 01586 darwin_pre_ptrace, NULL, darwin_execvp); 01587 01588 /* Return now in case of error. */ 01589 if (ptid_equal (inferior_ptid, null_ptid)) 01590 return; 01591 } 01592 01593 01594 /* Set things up such that the next call to darwin_wait will immediately 01595 return a fake stop event for inferior INF. 01596 01597 This assumes that the inferior's thread list has been initialized, 01598 as it will suspend the inferior's first thread. */ 01599 01600 static void 01601 darwin_setup_fake_stop_event (struct inferior *inf) 01602 { 01603 darwin_thread_t *thread; 01604 kern_return_t kret; 01605 01606 gdb_assert (darwin_inf_fake_stop == NULL); 01607 darwin_inf_fake_stop = inf; 01608 01609 /* When detecting a fake pending stop event, darwin_wait returns 01610 an event saying that the first thread is in a DARWIN_STOPPED 01611 state. To make that accurate, we need to suspend that thread 01612 as well. Otherwise, we'll try resuming it when resuming the 01613 inferior, and get a warning because the thread's suspend count 01614 is already zero, making the resume request useless. */ 01615 thread = VEC_index (darwin_thread_t, inf->private->threads, 0); 01616 kret = thread_suspend (thread->gdb_port); 01617 MACH_CHECK_ERROR (kret); 01618 } 01619 01620 /* Attach to process PID, then initialize for debugging it 01621 and wait for the trace-trap that results from attaching. */ 01622 static void 01623 darwin_attach (struct target_ops *ops, char *args, int from_tty) 01624 { 01625 pid_t pid; 01626 pid_t pid2; 01627 int wstatus; 01628 int res; 01629 struct inferior *inf; 01630 kern_return_t kret; 01631 01632 pid = parse_pid_to_attach (args); 01633 01634 if (pid == getpid ()) /* Trying to masturbate? */ 01635 error (_("I refuse to debug myself!")); 01636 01637 if (from_tty) 01638 { 01639 char *exec_file = get_exec_file (0); 01640 01641 if (exec_file) 01642 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, 01643 target_pid_to_str (pid_to_ptid (pid))); 01644 else 01645 printf_unfiltered (_("Attaching to %s\n"), 01646 target_pid_to_str (pid_to_ptid (pid))); 01647 01648 gdb_flush (gdb_stdout); 01649 } 01650 01651 if (pid == 0 || kill (pid, 0) < 0) 01652 error (_("Can't attach to process %d: %s (%d)"), 01653 pid, safe_strerror (errno), errno); 01654 01655 inferior_ptid = pid_to_ptid (pid); 01656 inf = current_inferior (); 01657 inferior_appeared (inf, pid); 01658 inf->attach_flag = 1; 01659 01660 /* Always add a main thread. */ 01661 add_thread_silent (inferior_ptid); 01662 01663 darwin_attach_pid (inf); 01664 01665 darwin_suspend_inferior (inf); 01666 01667 darwin_init_thread_list (inf); 01668 01669 darwin_check_osabi (inf->private, ptid_get_tid (inferior_ptid)); 01670 01671 darwin_setup_fake_stop_event (inf); 01672 01673 inf->private->no_ptrace = 1; 01674 } 01675 01676 /* Take a program previously attached to and detaches it. 01677 The program resumes execution and will no longer stop 01678 on signals, etc. We'd better not have left any breakpoints 01679 in the program or it'll die when it hits one. For this 01680 to work, it may be necessary for the process to have been 01681 previously attached. It *might* work if the program was 01682 started via fork. */ 01683 static void 01684 darwin_detach (struct target_ops *ops, char *args, int from_tty) 01685 { 01686 pid_t pid = ptid_get_pid (inferior_ptid); 01687 struct inferior *inf = current_inferior (); 01688 kern_return_t kret; 01689 int res; 01690 01691 /* Display message. */ 01692 if (from_tty) 01693 { 01694 char *exec_file = get_exec_file (0); 01695 if (exec_file == 0) 01696 exec_file = ""; 01697 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file, 01698 target_pid_to_str (pid_to_ptid (pid))); 01699 gdb_flush (gdb_stdout); 01700 } 01701 01702 /* If ptrace() is in use, stop the process. */ 01703 if (!inf->private->no_ptrace) 01704 darwin_stop_inferior (inf); 01705 01706 kret = darwin_restore_exception_ports (inf->private); 01707 MACH_CHECK_ERROR (kret); 01708 01709 if (!inf->private->no_ptrace) 01710 { 01711 res = PTRACE (PT_DETACH, inf->pid, 0, 0); 01712 if (res != 0) 01713 printf_unfiltered (_("Unable to detach from process-id %d: %s (%d)"), 01714 inf->pid, safe_strerror (errno), errno); 01715 } 01716 01717 darwin_reply_to_all_pending_messages (inf); 01718 01719 /* When using ptrace, we have just performed a PT_DETACH, which 01720 resumes the inferior. On the other hand, when we are not using 01721 ptrace, we need to resume its execution ourselves. */ 01722 if (inf->private->no_ptrace) 01723 darwin_resume_inferior (inf); 01724 01725 darwin_mourn_inferior (ops); 01726 } 01727 01728 static void 01729 darwin_files_info (struct target_ops *ops) 01730 { 01731 } 01732 01733 static char * 01734 darwin_pid_to_str (struct target_ops *ops, ptid_t ptid) 01735 { 01736 static char buf[80]; 01737 long tid = ptid_get_tid (ptid); 01738 01739 if (tid != 0) 01740 { 01741 snprintf (buf, sizeof (buf), _("Thread 0x%lx of process %u"), 01742 tid, ptid_get_pid (ptid)); 01743 return buf; 01744 } 01745 01746 return normal_pid_to_str (ptid); 01747 } 01748 01749 static int 01750 darwin_thread_alive (struct target_ops *ops, ptid_t ptid) 01751 { 01752 return 1; 01753 } 01754 01755 /* If RDADDR is not NULL, read inferior task's LEN bytes from ADDR and 01756 copy it to RDADDR in gdb's address space. 01757 If WRADDR is not NULL, write gdb's LEN bytes from WRADDR and copy it 01758 to ADDR in inferior task's address space. 01759 Return 0 on failure; number of bytes read / writen otherwise. */ 01760 static int 01761 darwin_read_write_inferior (task_t task, CORE_ADDR addr, 01762 gdb_byte *rdaddr, const gdb_byte *wraddr, 01763 int length) 01764 { 01765 kern_return_t kret; 01766 mach_vm_address_t offset = addr & (mach_page_size - 1); 01767 mach_vm_address_t low_address = (mach_vm_address_t) (addr - offset); 01768 mach_vm_size_t aligned_length = (mach_vm_size_t) PAGE_ROUND (offset + length); 01769 pointer_t copied; 01770 mach_msg_type_number_t copy_count; 01771 mach_vm_size_t remaining_length; 01772 mach_vm_address_t region_address; 01773 mach_vm_size_t region_length; 01774 01775 inferior_debug (8, _("darwin_read_write_inferior(task=0x%x, %s, len=%d)\n"), 01776 task, core_addr_to_string (addr), length); 01777 01778 /* Get memory from inferior with page aligned addresses. */ 01779 kret = mach_vm_read (task, low_address, aligned_length, 01780 &copied, ©_count); 01781 if (kret != KERN_SUCCESS) 01782 { 01783 inferior_debug 01784 (1, _("darwin_read_write_inferior: mach_vm_read failed at %s: %s"), 01785 core_addr_to_string (addr), mach_error_string (kret)); 01786 return 0; 01787 } 01788 01789 if (rdaddr != NULL) 01790 memcpy (rdaddr, (char *)copied + offset, length); 01791 01792 if (wraddr == NULL) 01793 goto out; 01794 01795 memcpy ((char *)copied + offset, wraddr, length); 01796 01797 /* Do writes atomically. 01798 First check for holes and unwritable memory. */ 01799 for (region_address = low_address, remaining_length = aligned_length; 01800 region_address < low_address + aligned_length; 01801 region_address += region_length, remaining_length -= region_length) 01802 { 01803 vm_region_submap_short_info_data_64_t info; 01804 mach_vm_address_t region_start = region_address; 01805 mach_msg_type_number_t count; 01806 natural_t region_depth; 01807 01808 region_depth = 100000; 01809 count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; 01810 kret = mach_vm_region_recurse 01811 (task, ®ion_start, ®ion_length, ®ion_depth, 01812 (vm_region_recurse_info_t) &info, &count); 01813 01814 if (kret != KERN_SUCCESS) 01815 { 01816 inferior_debug (1, _("darwin_read_write_inferior: " 01817 "mach_vm_region_recurse failed at %s: %s\n"), 01818 core_addr_to_string (region_address), 01819 mach_error_string (kret)); 01820 goto out; 01821 } 01822 01823 inferior_debug 01824 (9, _("darwin_read_write_inferior: " 01825 "mach_vm_region_recurse addr=%s, start=%s, len=%s\n"), 01826 core_addr_to_string (region_address), 01827 core_addr_to_string (region_start), 01828 core_addr_to_string (region_length)); 01829 01830 /* Check for holes in memory. */ 01831 if (region_start > region_address) 01832 { 01833 warning (_("No memory at %s (vs %s+0x%x). Nothing written"), 01834 core_addr_to_string (region_address), 01835 core_addr_to_string (region_start), 01836 (unsigned)region_length); 01837 length = 0; 01838 goto out; 01839 } 01840 01841 /* Adjust the length. */ 01842 region_length -= (region_address - region_start); 01843 01844 if (!(info.max_protection & VM_PROT_WRITE)) 01845 { 01846 kret = mach_vm_protect 01847 (task, region_address, region_length, 01848 TRUE, info.max_protection | VM_PROT_WRITE | VM_PROT_COPY); 01849 if (kret != KERN_SUCCESS) 01850 { 01851 warning (_("darwin_read_write_inf: " 01852 "mach_vm_protect max failed at %s: %s"), 01853 core_addr_to_string (region_address), 01854 mach_error_string (kret)); 01855 length = 0; 01856 goto out; 01857 } 01858 } 01859 01860 if (!(info.protection & VM_PROT_WRITE)) 01861 { 01862 kret = mach_vm_protect (task, region_address, region_length, 01863 FALSE, info.protection | VM_PROT_WRITE); 01864 if (kret != KERN_SUCCESS) 01865 { 01866 warning (_("darwin_read_write_inf: " 01867 "mach_vm_protect failed at %s (len=0x%lx): %s"), 01868 core_addr_to_string (region_address), 01869 (unsigned long)region_length, mach_error_string (kret)); 01870 length = 0; 01871 goto out; 01872 } 01873 } 01874 } 01875 01876 kret = mach_vm_write (task, low_address, copied, aligned_length); 01877 01878 if (kret != KERN_SUCCESS) 01879 { 01880 warning (_("darwin_read_write_inferior: mach_vm_write failed: %s"), 01881 mach_error_string (kret)); 01882 length = 0; 01883 } 01884 out: 01885 mach_vm_deallocate (mach_task_self (), copied, copy_count); 01886 return length; 01887 } 01888 01889 /* Read LENGTH bytes at offset ADDR of task_dyld_info for TASK, and copy them 01890 to RDADDR. 01891 Return 0 on failure; number of bytes read / written otherwise. */ 01892 01893 #ifdef TASK_DYLD_INFO_COUNT 01894 /* This is not available in Darwin 9. */ 01895 static int 01896 darwin_read_dyld_info (task_t task, CORE_ADDR addr, gdb_byte *rdaddr, 01897 int length) 01898 { 01899 struct task_dyld_info task_dyld_info; 01900 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 01901 int sz = TASK_DYLD_INFO_COUNT * sizeof (natural_t); 01902 kern_return_t kret; 01903 01904 if (addr >= sz) 01905 return 0; 01906 01907 kret = task_info (task, TASK_DYLD_INFO, (task_info_t) &task_dyld_info, &count); 01908 MACH_CHECK_ERROR (kret); 01909 if (kret != KERN_SUCCESS) 01910 return -1; 01911 /* Truncate. */ 01912 if (addr + length > sz) 01913 length = sz - addr; 01914 memcpy (rdaddr, (char *)&task_dyld_info + addr, length); 01915 return length; 01916 } 01917 #endif 01918 01919 01920 01921 static LONGEST 01922 darwin_xfer_partial (struct target_ops *ops, 01923 enum target_object object, const char *annex, 01924 gdb_byte *readbuf, const gdb_byte *writebuf, 01925 ULONGEST offset, LONGEST len) 01926 { 01927 struct inferior *inf = current_inferior (); 01928 01929 inferior_debug 01930 (8, _("darwin_xfer_partial(%s, %d, rbuf=%s, wbuf=%s) pid=%u\n"), 01931 core_addr_to_string (offset), (int)len, 01932 host_address_to_string (readbuf), host_address_to_string (writebuf), 01933 inf->pid); 01934 01935 switch (object) 01936 { 01937 case TARGET_OBJECT_MEMORY: 01938 return darwin_read_write_inferior (inf->private->task, offset, 01939 readbuf, writebuf, len); 01940 #ifdef TASK_DYLD_INFO_COUNT 01941 case TARGET_OBJECT_DARWIN_DYLD_INFO: 01942 if (writebuf != NULL || readbuf == NULL) 01943 { 01944 /* Support only read. */ 01945 return -1; 01946 } 01947 return darwin_read_dyld_info (inf->private->task, offset, readbuf, len); 01948 #endif 01949 default: 01950 return -1; 01951 } 01952 01953 } 01954 01955 static void 01956 set_enable_mach_exceptions (char *args, int from_tty, 01957 struct cmd_list_element *c) 01958 { 01959 if (!ptid_equal (inferior_ptid, null_ptid)) 01960 { 01961 struct inferior *inf = current_inferior (); 01962 exception_mask_t mask; 01963 kern_return_t kret; 01964 01965 if (enable_mach_exceptions) 01966 mask = EXC_MASK_ALL; 01967 else 01968 { 01969 darwin_restore_exception_ports (inf->private); 01970 mask = EXC_MASK_SOFTWARE | EXC_MASK_BREAKPOINT; 01971 } 01972 kret = task_set_exception_ports (inf->private->task, mask, darwin_ex_port, 01973 EXCEPTION_DEFAULT, THREAD_STATE_NONE); 01974 MACH_CHECK_ERROR (kret); 01975 } 01976 } 01977 01978 static char * 01979 darwin_pid_to_exec_file (int pid) 01980 { 01981 char *path; 01982 int res; 01983 01984 path = xmalloc (PATH_MAX); 01985 make_cleanup (xfree, path); 01986 01987 res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, PATH_MAX); 01988 if (res >= 0) 01989 return path; 01990 else 01991 return NULL; 01992 } 01993 01994 static ptid_t 01995 darwin_get_ada_task_ptid (long lwp, long thread) 01996 { 01997 int i; 01998 darwin_thread_t *t; 01999 int k; 02000 struct inferior *inf = current_inferior (); 02001 kern_return_t kret; 02002 mach_port_name_array_t names; 02003 mach_msg_type_number_t names_count; 02004 mach_port_type_array_t types; 02005 mach_msg_type_number_t types_count; 02006 long res = 0; 02007 02008 /* First linear search. */ 02009 for (k = 0; 02010 VEC_iterate (darwin_thread_t, inf->private->threads, k, t); 02011 k++) 02012 if (t->inf_port == lwp) 02013 return ptid_build (ptid_get_pid (inferior_ptid), 0, t->gdb_port); 02014 02015 /* Maybe the port was never extract. Do it now. */ 02016 02017 /* First get inferior port names. */ 02018 kret = mach_port_names (inf->private->task, &names, &names_count, &types, 02019 &types_count); 02020 MACH_CHECK_ERROR (kret); 02021 if (kret != KERN_SUCCESS) 02022 return null_ptid; 02023 02024 /* For each name, copy the right in the gdb space and then compare with 02025 our view of the inferior threads. We don't forget to deallocate the 02026 right. */ 02027 for (i = 0; i < names_count; i++) 02028 { 02029 mach_port_t local_name; 02030 mach_msg_type_name_t local_type; 02031 02032 /* We just need to know the corresponding name in gdb name space. 02033 So extract and deallocate the right. */ 02034 kret = mach_port_extract_right (inf->private->task, names[i], 02035 MACH_MSG_TYPE_COPY_SEND, 02036 &local_name, &local_type); 02037 if (kret != KERN_SUCCESS) 02038 continue; 02039 mach_port_deallocate (gdb_task, local_name); 02040 02041 for (k = 0; 02042 VEC_iterate (darwin_thread_t, inf->private->threads, k, t); 02043 k++) 02044 if (t->gdb_port == local_name) 02045 { 02046 t->inf_port = names[i]; 02047 if (names[i] == lwp) 02048 res = t->gdb_port; 02049 } 02050 } 02051 02052 vm_deallocate (gdb_task, (vm_address_t) names, 02053 names_count * sizeof (mach_port_t)); 02054 02055 if (res) 02056 return ptid_build (ptid_get_pid (inferior_ptid), 0, res); 02057 else 02058 return null_ptid; 02059 } 02060 02061 static int 02062 darwin_supports_multi_process (void) 02063 { 02064 return 1; 02065 } 02066 02067 /* -Wmissing-prototypes */ 02068 extern initialize_file_ftype _initialize_darwin_inferior; 02069 02070 void 02071 _initialize_darwin_inferior (void) 02072 { 02073 kern_return_t kret; 02074 02075 gdb_task = mach_task_self (); 02076 darwin_host_self = mach_host_self (); 02077 02078 /* Read page size. */ 02079 kret = host_page_size (darwin_host_self, &mach_page_size); 02080 if (kret != KERN_SUCCESS) 02081 { 02082 mach_page_size = 0x1000; 02083 MACH_CHECK_ERROR (kret); 02084 } 02085 02086 darwin_ops = inf_child_target (); 02087 02088 darwin_ops->to_shortname = "darwin-child"; 02089 darwin_ops->to_longname = _("Darwin child process"); 02090 darwin_ops->to_doc = 02091 _("Darwin child process (started by the \"run\" command)."); 02092 darwin_ops->to_create_inferior = darwin_create_inferior; 02093 darwin_ops->to_attach = darwin_attach; 02094 darwin_ops->to_attach_no_wait = 0; 02095 darwin_ops->to_detach = darwin_detach; 02096 darwin_ops->to_files_info = darwin_files_info; 02097 darwin_ops->to_wait = darwin_wait_to; 02098 darwin_ops->to_mourn_inferior = darwin_mourn_inferior; 02099 darwin_ops->to_kill = darwin_kill_inferior; 02100 darwin_ops->to_stop = darwin_stop; 02101 darwin_ops->to_resume = darwin_resume_to; 02102 darwin_ops->to_thread_alive = darwin_thread_alive; 02103 darwin_ops->to_pid_to_str = darwin_pid_to_str; 02104 darwin_ops->to_pid_to_exec_file = darwin_pid_to_exec_file; 02105 darwin_ops->to_load = NULL; 02106 darwin_ops->to_xfer_partial = darwin_xfer_partial; 02107 darwin_ops->to_supports_multi_process = darwin_supports_multi_process; 02108 darwin_ops->to_get_ada_task_ptid = darwin_get_ada_task_ptid; 02109 02110 darwin_complete_target (darwin_ops); 02111 02112 add_target (darwin_ops); 02113 02114 inferior_debug (2, _("GDB task: 0x%lx, pid: %d\n"), mach_task_self (), 02115 getpid ()); 02116 02117 add_setshow_zuinteger_cmd ("darwin", class_obscure, 02118 &darwin_debug_flag, _("\ 02119 Set if printing inferior communication debugging statements."), _("\ 02120 Show if printing inferior communication debugging statements."), NULL, 02121 NULL, NULL, 02122 &setdebuglist, &showdebuglist); 02123 02124 add_setshow_boolean_cmd ("mach-exceptions", class_support, 02125 &enable_mach_exceptions, _("\ 02126 Set if mach exceptions are caught."), _("\ 02127 Show if mach exceptions are caught."), _("\ 02128 When this mode is on, all low level exceptions are reported before being\n\ 02129 reported by the kernel."), 02130 &set_enable_mach_exceptions, NULL, 02131 &setlist, &showlist); 02132 }