1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2005, 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/>. */
27 #include "floatformat.h"
33 #include "i387-tdep.h"
34 #include "i386-tdep.h"
35 #include "amd64-tdep.h"
40 #include "gdb_assert.h"
41 #include "i386-darwin-tdep.h"
43 #include "solib-darwin.h"
44 #include "dwarf2-frame.h"
46 /* Offsets into the struct i386_thread_state where we'll find the saved regs.
47 From <mach/i386/thread_status.h> and i386-tdep.h. */
48 int i386_darwin_thread_state_reg_offset[] =
68 const int i386_darwin_thread_state_num_regs =
69 ARRAY_SIZE (i386_darwin_thread_state_reg_offset);
71 /* Offsets into the struct x86_thread_state64 where we'll find the saved regs.
72 From <mach/i386/thread_status.h> and amd64-tdep.h. */
73 int amd64_darwin_thread_state_reg_offset[] =
90 15 * 8, /* ... %r15 */
101 const int amd64_darwin_thread_state_num_regs =
102 ARRAY_SIZE (amd64_darwin_thread_state_reg_offset);
104 /* Assuming THIS_FRAME is a Darwin sigtramp routine, return the
105 address of the associated sigcontext structure. */
108 i386_darwin_sigcontext_addr (struct frame_info *this_frame)
110 struct gdbarch *gdbarch = get_frame_arch (this_frame);
111 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
116 get_frame_register (this_frame, I386_EBP_REGNUM, buf);
117 bp = extract_unsigned_integer (buf, 4, byte_order);
119 /* A pointer to the ucontext is passed as the fourth argument
120 to the signal handler. */
121 read_memory (bp + 24, buf, 4);
122 si = extract_unsigned_integer (buf, 4, byte_order);
124 /* The pointer to mcontext is at offset 28. */
125 read_memory (si + 28, buf, 4);
127 /* First register (eax) is at offset 12. */
128 return extract_unsigned_integer (buf, 4, byte_order) + 12;
132 amd64_darwin_sigcontext_addr (struct frame_info *this_frame)
134 struct gdbarch *gdbarch = get_frame_arch (this_frame);
135 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
140 /* A pointer to the ucontext is passed as the fourth argument
141 to the signal handler, which is saved in rbx. */
142 get_frame_register (this_frame, AMD64_RBX_REGNUM, buf);
143 rbx = extract_unsigned_integer (buf, 8, byte_order);
145 /* The pointer to mcontext is at offset 48. */
146 read_memory (rbx + 48, buf, 8);
148 /* First register (rax) is at offset 16. */
149 return extract_unsigned_integer (buf, 8, byte_order) + 16;
152 /* Return true if the PC of THIS_FRAME is in a signal trampoline which
153 may have DWARF-2 CFI.
155 On Darwin, signal trampolines have DWARF-2 CFI but it has only one FDE
156 that covers only the indirect call to the user handler.
157 Without this function, the frame is recognized as a normal frame which is
161 darwin_dwarf_signal_frame_p (struct gdbarch *gdbarch,
162 struct frame_info *this_frame)
164 return i386_sigtramp_p (this_frame);
168 i386_darwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
170 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
172 /* We support the SSE registers. */
173 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
174 set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
176 dwarf2_frame_set_signal_frame_p (gdbarch, darwin_dwarf_signal_frame_p);
178 tdep->struct_return = reg_struct_return;
180 tdep->sigtramp_p = i386_sigtramp_p;
181 tdep->sigcontext_addr = i386_darwin_sigcontext_addr;
182 tdep->sc_reg_offset = i386_darwin_thread_state_reg_offset;
183 tdep->sc_num_regs = i386_darwin_thread_state_num_regs;
185 tdep->jb_pc_offset = 20;
187 set_solib_ops (gdbarch, &darwin_so_ops);
191 x86_darwin_init_abi_64 (struct gdbarch_info info, struct gdbarch *gdbarch)
193 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
195 amd64_init_abi (info, gdbarch);
197 tdep->struct_return = reg_struct_return;
199 dwarf2_frame_set_signal_frame_p (gdbarch, darwin_dwarf_signal_frame_p);
201 tdep->sigtramp_p = i386_sigtramp_p;
202 tdep->sigcontext_addr = amd64_darwin_sigcontext_addr;
203 tdep->sc_reg_offset = amd64_darwin_thread_state_reg_offset;
204 tdep->sc_num_regs = amd64_darwin_thread_state_num_regs;
206 tdep->jb_pc_offset = 148;
208 set_solib_ops (gdbarch, &darwin_so_ops);
211 static enum gdb_osabi
212 i386_mach_o_osabi_sniffer (bfd *abfd)
214 if (!bfd_check_format (abfd, bfd_object))
215 return GDB_OSABI_UNKNOWN;
217 if (bfd_get_arch (abfd) == bfd_arch_i386)
218 return GDB_OSABI_DARWIN;
220 return GDB_OSABI_UNKNOWN;
224 _initialize_i386_darwin_tdep (void)
226 gdbarch_register_osabi_sniffer (bfd_arch_unknown, bfd_target_mach_o_flavour,
227 i386_mach_o_osabi_sniffer);
229 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_i386_i386,
230 GDB_OSABI_DARWIN, i386_darwin_init_abi);
232 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
233 GDB_OSABI_DARWIN, x86_darwin_init_abi_64);