* remote.c (remote_pid_to_str): If printing a process id and we
[binutils-gdb.git] / gdb / i386-darwin-nat.c
blob328372357a926e9b9659255fb34f825e823b76a0
1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008, 2009
3 Free Software Foundation, Inc.
5 Contributed by Apple Computer, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "symfile.h"
27 #include "symtab.h"
28 #include "objfiles.h"
29 #include "gdbcmd.h"
30 #include "regcache.h"
31 #include "gdb_assert.h"
32 #include "i386-tdep.h"
33 #include "i387-tdep.h"
34 #include "gdbarch.h"
35 #include "arch-utils.h"
36 #include "gdbcore.h"
38 #include "darwin-nat.h"
39 #include "i386-darwin-tdep.h"
41 #ifdef BFD64
42 #include "amd64-nat.h"
43 #include "amd64-darwin-tdep.h"
44 #endif
46 /* Read register values from the inferior process.
47 If REGNO is -1, do this for all registers.
48 Otherwise, REGNO specifies which register (so we can save time). */
49 static void
50 i386_darwin_fetch_inferior_registers (struct target_ops *ops,
51 struct regcache *regcache, int regno)
53 thread_t current_thread = ptid_get_tid (inferior_ptid);
54 int fetched = 0;
55 struct gdbarch *gdbarch = get_regcache_arch (regcache);
57 #ifdef BFD64
58 if (gdbarch_ptr_bit (gdbarch) == 64)
60 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
62 x86_thread_state_t gp_regs;
63 unsigned int gp_count = x86_THREAD_STATE_COUNT;
64 kern_return_t ret;
66 ret = thread_get_state
67 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
68 &gp_count);
69 if (ret != KERN_SUCCESS)
71 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
72 MACH_CHECK_ERROR (ret);
74 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
75 fetched++;
78 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
80 x86_float_state_t fp_regs;
81 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
82 kern_return_t ret;
84 ret = thread_get_state
85 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
86 &fp_count);
87 if (ret != KERN_SUCCESS)
89 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
90 MACH_CHECK_ERROR (ret);
92 i387_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64);
93 fetched++;
96 else
97 #endif
99 if (regno == -1 || regno < I386_NUM_GREGS)
101 i386_thread_state_t gp_regs;
102 unsigned int gp_count = i386_THREAD_STATE_COUNT;
103 kern_return_t ret;
104 int i;
106 ret = thread_get_state
107 (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
108 &gp_count);
109 if (ret != KERN_SUCCESS)
111 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
112 MACH_CHECK_ERROR (ret);
114 for (i = 0; i < I386_NUM_GREGS; i++)
115 regcache_raw_supply
116 (regcache, i,
117 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
119 fetched++;
122 if (regno == -1
123 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
125 i386_float_state_t fp_regs;
126 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
127 kern_return_t ret;
129 ret = thread_get_state
130 (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
131 &fp_count);
132 if (ret != KERN_SUCCESS)
134 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
135 MACH_CHECK_ERROR (ret);
137 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
138 fetched++;
142 if (! fetched)
144 warning (_("unknown register %d"), regno);
145 regcache_raw_supply (regcache, regno, NULL);
149 /* Store our register values back into the inferior.
150 If REGNO is -1, do this for all registers.
151 Otherwise, REGNO specifies which register (so we can save time). */
153 static void
154 i386_darwin_store_inferior_registers (struct target_ops *ops,
155 struct regcache *regcache, int regno)
157 thread_t current_thread = ptid_get_tid (inferior_ptid);
158 struct gdbarch *gdbarch = get_regcache_arch (regcache);
160 #ifdef BFD64
161 if (gdbarch_ptr_bit (gdbarch) == 64)
163 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
165 x86_thread_state_t gp_regs;
166 kern_return_t ret;
167 unsigned int gp_count = x86_THREAD_STATE_COUNT;
169 ret = thread_get_state
170 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
171 &gp_count);
172 MACH_CHECK_ERROR (ret);
173 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
174 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
176 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
178 ret = thread_set_state (current_thread, x86_THREAD_STATE,
179 (thread_state_t) &gp_regs,
180 x86_THREAD_STATE_COUNT);
181 MACH_CHECK_ERROR (ret);
184 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
186 x86_float_state_t fp_regs;
187 kern_return_t ret;
188 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
190 ret = thread_get_state
191 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
192 &fp_count);
193 MACH_CHECK_ERROR (ret);
194 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
195 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
197 i387_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
199 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
200 (thread_state_t) & fp_regs,
201 x86_FLOAT_STATE_COUNT);
202 MACH_CHECK_ERROR (ret);
205 else
206 #endif
208 if (regno == -1 || regno < I386_NUM_GREGS)
210 i386_thread_state_t gp_regs;
211 kern_return_t ret;
212 unsigned int gp_count = i386_THREAD_STATE_COUNT;
213 int i;
215 ret = thread_get_state
216 (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
217 &gp_count);
218 MACH_CHECK_ERROR (ret);
220 for (i = 0; i < I386_NUM_GREGS; i++)
221 if (regno == -1 || regno == i)
222 regcache_raw_collect
223 (regcache, i,
224 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
226 ret = thread_set_state (current_thread, i386_THREAD_STATE,
227 (thread_state_t) & gp_regs,
228 i386_THREAD_STATE_COUNT);
229 MACH_CHECK_ERROR (ret);
232 if (regno == -1
233 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
235 i386_float_state_t fp_regs;
236 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
237 kern_return_t ret;
239 ret = thread_get_state
240 (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
241 &fp_count);
242 MACH_CHECK_ERROR (ret);
244 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
246 ret = thread_set_state (current_thread, i386_FLOAT_STATE,
247 (thread_state_t) & fp_regs,
248 i386_FLOAT_STATE_COUNT);
249 MACH_CHECK_ERROR (ret);
255 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
257 #ifndef DR_FIRSTADDR
258 #define DR_FIRSTADDR 0
259 #endif
261 #ifndef DR_LASTADDR
262 #define DR_LASTADDR 3
263 #endif
265 #ifndef DR_STATUS
266 #define DR_STATUS 6
267 #endif
269 #ifndef DR_CONTROL
270 #define DR_CONTROL 7
271 #endif
274 static void
275 i386_darwin_dr_set (int regnum, uint32_t value)
277 int current_pid;
278 thread_t current_thread;
279 x86_debug_state_t dr_regs;
280 kern_return_t ret;
281 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
283 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
285 current_thread = ptid_get_tid (inferior_ptid);
287 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
288 dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
289 dr_count = x86_DEBUG_STATE_COUNT;
290 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
291 (thread_state_t) &dr_regs, &dr_count);
293 if (ret != KERN_SUCCESS)
295 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
296 MACH_CHECK_ERROR (ret);
299 switch (regnum)
301 case 0:
302 dr_regs.uds.ds32.__dr0 = value;
303 break;
304 case 1:
305 dr_regs.uds.ds32.__dr1 = value;
306 break;
307 case 2:
308 dr_regs.uds.ds32.__dr2 = value;
309 break;
310 case 3:
311 dr_regs.uds.ds32.__dr3 = value;
312 break;
313 case 4:
314 dr_regs.uds.ds32.__dr4 = value;
315 break;
316 case 5:
317 dr_regs.uds.ds32.__dr5 = value;
318 break;
319 case 6:
320 dr_regs.uds.ds32.__dr6 = value;
321 break;
322 case 7:
323 dr_regs.uds.ds32.__dr7 = value;
324 break;
327 ret = thread_set_state (current_thread, x86_DEBUG_STATE,
328 (thread_state_t) &dr_regs, dr_count);
330 if (ret != KERN_SUCCESS)
332 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
333 MACH_CHECK_ERROR (ret);
337 static uint32_t
338 i386_darwin_dr_get (int regnum)
340 thread_t current_thread;
341 x86_debug_state_t dr_regs;
342 kern_return_t ret;
343 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
345 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
347 current_thread = ptid_get_tid (inferior_ptid);
349 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
350 dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
351 dr_count = x86_DEBUG_STATE_COUNT;
352 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
353 (thread_state_t) &dr_regs, &dr_count);
355 if (ret != KERN_SUCCESS)
357 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
358 MACH_CHECK_ERROR (ret);
361 switch (regnum)
363 case 0:
364 return dr_regs.uds.ds32.__dr0;
365 case 1:
366 return dr_regs.uds.ds32.__dr1;
367 case 2:
368 return dr_regs.uds.ds32.__dr2;
369 case 3:
370 return dr_regs.uds.ds32.__dr3;
371 case 4:
372 return dr_regs.uds.ds32.__dr4;
373 case 5:
374 return dr_regs.uds.ds32.__dr5;
375 case 6:
376 return dr_regs.uds.ds32.__dr6;
377 case 7:
378 return dr_regs.uds.ds32.__dr7;
379 default:
380 return -1;
384 void
385 i386_darwin_dr_set_control (unsigned long control)
387 i386_darwin_dr_set (DR_CONTROL, control);
390 void
391 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
393 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
395 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
398 void
399 i386_darwin_dr_reset_addr (int regnum)
401 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
403 i386_darwin_dr_set (DR_FIRSTADDR + regnum, 0L);
406 unsigned long
407 i386_darwin_dr_get_status (void)
409 return i386_darwin_dr_get (DR_STATUS);
412 void
413 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
415 if (gdbarch_osabi (target_gdbarch) == GDB_OSABI_UNKNOWN)
417 /* Attaching to a process. Let's figure out what kind it is. */
418 x86_thread_state_t gp_regs;
419 struct gdbarch_info info;
420 unsigned int gp_count = x86_THREAD_STATE_COUNT;
421 kern_return_t ret;
423 ret = thread_get_state (thread, x86_THREAD_STATE,
424 (thread_state_t) &gp_regs, &gp_count);
425 if (ret != KERN_SUCCESS)
427 MACH_CHECK_ERROR (ret);
428 return;
431 gdbarch_info_init (&info);
432 gdbarch_info_fill (&info);
433 info.byte_order = gdbarch_byte_order (target_gdbarch);
434 info.osabi = GDB_OSABI_DARWIN;
435 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
436 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
437 bfd_mach_x86_64);
438 else
439 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
440 bfd_mach_i386_i386);
441 gdbarch_update_p (info);
445 #define X86_EFLAGS_T 0x100UL
447 /* Returning from a signal trampoline is done by calling a
448 special system call (sigreturn). This system call
449 restores the registers that were saved when the signal was
450 raised, including %eflags/%rflags. That means that single-stepping
451 won't work. Instead, we'll have to modify the signal context
452 that's about to be restored, and set the trace flag there. */
454 static int
455 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
457 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
458 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
459 gdb_byte buf[sizeof (darwin_syscall)];
461 /* Check if PC is at a sigreturn system call. */
462 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
463 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
464 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
466 ULONGEST uctx_addr;
467 ULONGEST mctx_addr;
468 ULONGEST flags_addr;
469 unsigned int eflags;
471 uctx_addr = read_memory_unsigned_integer
472 (regs->uts.ts32.__esp + 4, 4, byte_order);
473 mctx_addr = read_memory_unsigned_integer
474 (uctx_addr + 28, 4, byte_order);
476 flags_addr = mctx_addr + 12 + 9 * 4;
477 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
478 eflags |= X86_EFLAGS_T;
479 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
481 return 1;
483 return 0;
486 #ifdef BFD64
487 static int
488 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
490 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
491 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
492 gdb_byte buf[sizeof (darwin_syscall)];
494 /* Check if PC is at a sigreturn system call. */
495 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
496 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
497 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
499 ULONGEST mctx_addr;
500 ULONGEST flags_addr;
501 unsigned int rflags;
503 mctx_addr = read_memory_unsigned_integer
504 (regs->uts.ts64.__rdi + 48, 8, byte_order);
505 flags_addr = mctx_addr + 16 + 17 * 8;
507 /* AMD64 is little endian. */
508 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
509 rflags |= X86_EFLAGS_T;
510 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
512 return 1;
514 return 0;
516 #endif
518 void
519 darwin_set_sstep (thread_t thread, int enable)
521 x86_thread_state_t regs;
522 unsigned int count = x86_THREAD_STATE_COUNT;
523 kern_return_t kret;
525 kret = thread_get_state (thread, x86_THREAD_STATE,
526 (thread_state_t) &regs, &count);
527 if (kret != KERN_SUCCESS)
529 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
530 kret, thread);
531 return;
534 switch (regs.tsh.flavor)
536 case x86_THREAD_STATE32:
538 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
540 if (enable && i386_darwin_sstep_at_sigreturn (&regs))
541 return;
542 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
543 return;
544 regs.uts.ts32.__eflags = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
545 kret = thread_set_state (thread, x86_THREAD_STATE,
546 (thread_state_t) &regs, count);
547 MACH_CHECK_ERROR (kret);
549 break;
550 #ifdef BFD64
551 case x86_THREAD_STATE64:
553 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
555 if (enable && amd64_darwin_sstep_at_sigreturn (&regs))
556 return;
557 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
558 return;
559 regs.uts.ts64.__rflags = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
560 kret = thread_set_state (thread, x86_THREAD_STATE,
561 (thread_state_t) &regs, count);
562 MACH_CHECK_ERROR (kret);
564 break;
565 #endif
566 default:
567 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs.tsh.flavor);
571 void
572 darwin_complete_target (struct target_ops *target)
574 #ifdef BFD64
575 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
576 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
577 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
578 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
579 #endif
581 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
582 target->to_store_registers = i386_darwin_store_inferior_registers;