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