1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "frame-unwind.h"
31 #include "trad-frame.h"
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
36 #include "amd64-tdep.h"
37 #include "i387-tdep.h"
38 #include "solib-svr4.h"
39 #include "bsd-uthread.h"
41 /* Support for core dumps. */
44 amd64obsd_supply_regset (const struct regset *regset,
45 struct regcache *regcache, int regnum,
46 const void *regs, size_t len)
48 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
50 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
52 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
53 amd64_supply_fxsave (regcache, regnum,
54 ((const gdb_byte *)regs) + tdep->sizeof_gregset);
57 static const struct regset *
58 amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
59 const char *sect_name, size_t sect_size)
61 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
63 /* OpenBSD core dumps don't use seperate register sets for the
64 general-purpose and floating-point registers. */
66 if (strcmp (sect_name, ".reg") == 0
67 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
69 if (tdep->gregset == NULL)
70 tdep->gregset = regset_alloc (gdbarch, amd64obsd_supply_regset, NULL);
78 /* Support for signal handlers. */
80 /* Default page size. */
81 static const int amd64obsd_page_size = 4096;
83 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
87 amd64obsd_sigtramp_p (struct frame_info *this_frame)
89 CORE_ADDR pc = get_frame_pc (this_frame);
90 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
91 const gdb_byte sigreturn[] =
94 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
95 0xcd, 0x80 /* int $0x80 */
97 size_t buflen = (sizeof sigreturn) + 1;
101 /* If the function has a valid symbol name, it isn't a
103 find_pc_partial_function (pc, &name, NULL, NULL);
107 /* If the function lives in a valid section (even without a starting
108 point) it isn't a trampoline. */
109 if (find_pc_section (pc) != NULL)
112 /* If we can't read the instructions at START_PC, return zero. */
113 buf = alloca ((sizeof sigreturn) + 1);
114 if (!safe_frame_unwind_memory (this_frame, start_pc + 6, buf, buflen))
117 /* Check for sigreturn(2). Depending on how the assembler encoded
118 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
120 if (memcmp (buf, sigreturn, sizeof sigreturn)
121 && memcpy (buf + 1, sigreturn, sizeof sigreturn))
127 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
128 address of the associated sigcontext structure. */
131 amd64obsd_sigcontext_addr (struct frame_info *this_frame)
133 CORE_ADDR pc = get_frame_pc (this_frame);
134 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
136 /* The %rsp register points at `struct sigcontext' upon entry of a
137 signal trampoline. The relevant part of the trampoline is
142 movq $SYS_sigreturn,%rax
145 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
146 instruction clobbers %rsp, but its value is saved in `%rdi'. */
149 return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
151 return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
154 /* OpenBSD 3.5 or later. */
156 /* Mapping between the general-purpose registers in `struct reg'
157 format and GDB's register cache layout. */
159 /* From <machine/reg.h>. */
160 int amd64obsd_r_reg_offset[] =
177 11 * 8, /* ... %r15 */
179 17 * 8, /* %eflags */
188 /* From <machine/signal.h>. */
189 static int amd64obsd_sc_reg_offset[] =
206 11 * 8, /* ... %r15 */
208 23 * 8, /* %eflags */
217 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
218 static int amd64obsd_uthread_reg_offset[] =
228 12 * 8, /* %r8 ... */
235 5 * 8, /* ... %r15 */
246 /* Offset within the thread structure where we can find the saved
247 stack pointer (%esp). */
248 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
251 amd64obsd_supply_uthread (struct regcache *regcache,
252 int regnum, CORE_ADDR addr)
254 struct gdbarch *gdbarch = get_regcache_arch (regcache);
255 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
256 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
261 gdb_assert (regnum >= -1);
263 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
267 /* Fetch stack pointer from thread structure. */
268 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
270 /* Adjust the stack pointer such that it looks as if we just
271 returned from _thread_machdep_switch. */
272 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
273 store_unsigned_integer (buf, 8, byte_order, sp + offset);
274 regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
277 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
279 if (amd64obsd_uthread_reg_offset[i] != -1
280 && (regnum == -1 || regnum == i))
282 /* Fetch stack pointer from thread structure (if we didn't
285 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
287 /* Read the saved register from the stack frame. */
288 read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
289 regcache_raw_supply (regcache, i, buf);
295 amd64obsd_collect_uthread (const struct regcache *regcache,
296 int regnum, CORE_ADDR addr)
298 struct gdbarch *gdbarch = get_regcache_arch (regcache);
299 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
300 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
305 gdb_assert (regnum >= -1);
307 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
311 /* Calculate the stack pointer (frame pointer) that will be
312 stored into the thread structure. */
313 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
314 regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
315 sp = extract_unsigned_integer (buf, 8, byte_order) - offset;
317 /* Store the stack pointer. */
318 write_memory_unsigned_integer (sp_addr, 8, byte_order, sp);
320 /* The stack pointer was (potentially) modified. Make sure we
321 build a proper stack frame. */
325 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
327 if (amd64obsd_uthread_reg_offset[i] != -1
328 && (regnum == -1 || regnum == i))
330 /* Fetch stack pointer from thread structure (if we didn't
331 calculate it already). */
333 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
335 /* Write the register into the stack frame. */
336 regcache_raw_collect (regcache, i, buf);
337 write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
341 /* Kernel debugging support. */
343 /* From <machine/frame.h>. Easy since `struct trapframe' matches
344 `struct sigcontext'. */
345 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
347 static struct trad_frame_cache *
348 amd64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
350 struct gdbarch *gdbarch = get_frame_arch (this_frame);
351 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
352 struct trad_frame_cache *cache;
353 CORE_ADDR func, sp, addr;
361 cache = trad_frame_cache_zalloc (this_frame);
364 func = get_frame_func (this_frame);
365 sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
367 find_pc_partial_function (func, &name, NULL, NULL);
368 if (name && strncmp (name, "Xintr", 5) == 0)
369 addr = sp + 8; /* It's an interrupt frame. */
373 for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
374 if (amd64obsd_tf_reg_offset[i] != -1)
375 trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
377 /* Read %cs from trap frame. */
378 addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
379 cs = read_memory_unsigned_integer (addr, 8, byte_order);
380 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
382 /* Trap from user space; terminate backtrace. */
383 trad_frame_set_id (cache, outer_frame_id);
387 /* Construct the frame ID using the function start. */
388 trad_frame_set_id (cache, frame_id_build (sp + 16, func));
395 amd64obsd_trapframe_this_id (struct frame_info *this_frame,
396 void **this_cache, struct frame_id *this_id)
398 struct trad_frame_cache *cache =
399 amd64obsd_trapframe_cache (this_frame, this_cache);
401 trad_frame_get_id (cache, this_id);
404 static struct value *
405 amd64obsd_trapframe_prev_register (struct frame_info *this_frame,
406 void **this_cache, int regnum)
408 struct trad_frame_cache *cache =
409 amd64obsd_trapframe_cache (this_frame, this_cache);
411 return trad_frame_get_register (cache, this_frame, regnum);
415 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
416 struct frame_info *this_frame,
417 void **this_prologue_cache)
422 /* Check Current Privilege Level and bail out if we're not executing
424 cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
425 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
428 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
429 return (name && ((strcmp (name, "calltrap") == 0)
430 || (strcmp (name, "osyscall1") == 0)
431 || (strcmp (name, "Xsyscall") == 0)
432 || (strncmp (name, "Xintr", 5) == 0)));
435 static const struct frame_unwind amd64obsd_trapframe_unwind = {
436 /* FIXME: kettenis/20051219: This really is more like an interrupt
437 frame, but SIGTRAMP_FRAME would print <signal handler called>,
438 which really is not what we want here. */
440 amd64obsd_trapframe_this_id,
441 amd64obsd_trapframe_prev_register,
443 amd64obsd_trapframe_sniffer
448 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
450 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
452 amd64_init_abi (info, gdbarch);
454 /* Initialize general-purpose register set details. */
455 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
456 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
457 tdep->sizeof_gregset = 24 * 8;
459 set_gdbarch_regset_from_core_section (gdbarch,
460 amd64obsd_regset_from_core_section);
462 tdep->jb_pc_offset = 7 * 8;
464 tdep->sigtramp_p = amd64obsd_sigtramp_p;
465 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
466 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
467 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
469 /* OpenBSD provides a user-level threads implementation. */
470 bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
471 bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
473 /* OpenBSD uses SVR4-style shared libraries. */
474 set_solib_svr4_fetch_link_map_offsets
475 (gdbarch, svr4_lp64_fetch_link_map_offsets);
477 /* Unwind kernel trap frames correctly. */
478 frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
482 /* Provide a prototype to silence -Wmissing-prototypes. */
483 void _initialize_amd64obsd_tdep (void);
486 _initialize_amd64obsd_tdep (void)
488 /* The OpenBSD/amd64 native dependent code makes this assumption. */
489 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
491 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
492 GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
494 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
495 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
496 GDB_OSABI_NETBSD_AOUT, amd64obsd_init_abi);