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