1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "frame-unwind.h"
32 #include "trad-frame.h"
34 #include "gdb_assert.h"
35 #include "gdb_string.h"
37 #include "amd64-tdep.h"
38 #include "i387-tdep.h"
39 #include "solib-svr4.h"
40 #include "bsd-uthread.h"
42 /* Support for core dumps. */
45 amd64obsd_supply_regset (const struct regset *regset,
46 struct regcache *regcache, int regnum,
47 const void *regs, size_t len)
49 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
51 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
53 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
54 amd64_supply_fxsave (regcache, regnum,
55 ((const gdb_byte *)regs) + tdep->sizeof_gregset);
58 static const struct regset *
59 amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
60 const char *sect_name, size_t sect_size)
62 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
64 /* OpenBSD core dumps don't use seperate register sets for the
65 general-purpose and floating-point registers. */
67 if (strcmp (sect_name, ".reg") == 0
68 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
70 if (tdep->gregset == NULL)
71 tdep->gregset = regset_alloc (gdbarch, amd64obsd_supply_regset, NULL);
79 /* Support for signal handlers. */
81 /* Default page size. */
82 static const int amd64obsd_page_size = 4096;
84 /* Return whether the frame preceding NEXT_FRAME corresponds to an
85 OpenBSD sigtramp routine. */
88 amd64obsd_sigtramp_p (struct frame_info *next_frame)
90 CORE_ADDR pc = frame_pc_unwind (next_frame);
91 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
92 const gdb_byte sigreturn[] =
95 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
96 0xcd, 0x80 /* int $0x80 */
98 size_t buflen = (sizeof sigreturn) + 1;
102 /* If the function has a valid symbol name, it isn't a
104 find_pc_partial_function (pc, &name, NULL, NULL);
108 /* If the function lives in a valid section (even without a starting
109 point) it isn't a trampoline. */
110 if (find_pc_section (pc) != NULL)
113 /* If we can't read the instructions at START_PC, return zero. */
114 buf = alloca ((sizeof sigreturn) + 1);
115 if (!safe_frame_unwind_memory (next_frame, start_pc + 6, buf, buflen))
118 /* Check for sigreturn(2). Depending on how the assembler encoded
119 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
121 if (memcmp (buf, sigreturn, sizeof sigreturn)
122 && memcpy (buf + 1, sigreturn, sizeof sigreturn))
128 /* Assuming NEXT_FRAME is for a frame following a BSD sigtramp
129 routine, return the address of the associated sigcontext structure. */
132 amd64obsd_sigcontext_addr (struct frame_info *next_frame)
134 CORE_ADDR pc = frame_pc_unwind (next_frame);
135 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
137 /* The %rsp register points at `struct sigcontext' upon entry of a
138 signal trampoline. The relevant part of the trampoline is
143 movq $SYS_sigreturn,%rax
146 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
147 instruction clobbers %rsp, but its value is saved in `%rdi'. */
150 return frame_unwind_register_unsigned (next_frame, AMD64_RDI_REGNUM);
152 return frame_unwind_register_unsigned (next_frame, AMD64_RSP_REGNUM);
155 /* OpenBSD 3.5 or later. */
157 /* Mapping between the general-purpose registers in `struct reg'
158 format and GDB's register cache layout. */
160 /* From <machine/reg.h>. */
161 int amd64obsd_r_reg_offset[] =
178 11 * 8, /* ... %r15 */
180 17 * 8, /* %eflags */
189 /* From <machine/signal.h>. */
190 static int amd64obsd_sc_reg_offset[] =
207 11 * 8, /* ... %r15 */
209 23 * 8, /* %eflags */
218 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
219 static int amd64obsd_uthread_reg_offset[] =
229 12 * 8, /* %r8 ... */
236 5 * 8, /* ... %r15 */
247 /* Offset within the thread structure where we can find the saved
248 stack pointer (%esp). */
249 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
252 amd64obsd_supply_uthread (struct regcache *regcache,
253 int regnum, CORE_ADDR addr)
255 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
260 gdb_assert (regnum >= -1);
262 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
266 /* Fetch stack pointer from thread structure. */
267 sp = read_memory_unsigned_integer (sp_addr, 8);
269 /* Adjust the stack pointer such that it looks as if we just
270 returned from _thread_machdep_switch. */
271 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
272 store_unsigned_integer (buf, 8, sp + offset);
273 regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
276 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
278 if (amd64obsd_uthread_reg_offset[i] != -1
279 && (regnum == -1 || regnum == i))
281 /* Fetch stack pointer from thread structure (if we didn't
284 sp = read_memory_unsigned_integer (sp_addr, 8);
286 /* Read the saved register from the stack frame. */
287 read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
288 regcache_raw_supply (regcache, i, buf);
294 amd64obsd_collect_uthread (const struct regcache *regcache,
295 int regnum, CORE_ADDR addr)
297 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
302 gdb_assert (regnum >= -1);
304 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
308 /* Calculate the stack pointer (frame pointer) that will be
309 stored into the thread structure. */
310 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
311 regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
312 sp = extract_unsigned_integer (buf, 8) - offset;
314 /* Store the stack pointer. */
315 write_memory_unsigned_integer (sp_addr, 8, sp);
317 /* The stack pointer was (potentially) modified. Make sure we
318 build a proper stack frame. */
322 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
324 if (amd64obsd_uthread_reg_offset[i] != -1
325 && (regnum == -1 || regnum == i))
327 /* Fetch stack pointer from thread structure (if we didn't
328 calculate it already). */
330 sp = read_memory_unsigned_integer (sp_addr, 8);
332 /* Write the register into the stack frame. */
333 regcache_raw_collect (regcache, i, buf);
334 write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
338 /* Kernel debugging support. */
340 /* From <machine/frame.h>. Easy since `struct trapframe' matches
341 `struct sigcontext'. */
342 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
344 static struct trad_frame_cache *
345 amd64obsd_trapframe_cache(struct frame_info *next_frame, void **this_cache)
347 struct trad_frame_cache *cache;
348 CORE_ADDR func, sp, addr;
356 cache = trad_frame_cache_zalloc (next_frame);
359 /* NORMAL_FRAME matches the type in amd64obsd_trapframe_unwind, but
360 SIGTRAMP_FRAME might be more appropriate. */
361 func = frame_func_unwind (next_frame, NORMAL_FRAME);
362 sp = frame_unwind_register_unsigned (next_frame, AMD64_RSP_REGNUM);
364 find_pc_partial_function (func, &name, NULL, NULL);
365 if (name && strncmp (name, "Xintr", 5) == 0)
366 addr = sp + 8; /* It's an interrupt frame. */
370 for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
371 if (amd64obsd_tf_reg_offset[i] != -1)
372 trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
374 /* Read %cs from trap frame. */
375 addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
376 cs = read_memory_unsigned_integer (addr, 8);
377 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
379 /* Trap from user space; terminate backtrace. */
380 trad_frame_set_id (cache, null_frame_id);
384 /* Construct the frame ID using the function start. */
385 trad_frame_set_id (cache, frame_id_build (sp + 16, func));
392 amd64obsd_trapframe_this_id (struct frame_info *next_frame,
393 void **this_cache, struct frame_id *this_id)
395 struct trad_frame_cache *cache =
396 amd64obsd_trapframe_cache (next_frame, this_cache);
398 trad_frame_get_id (cache, this_id);
402 amd64obsd_trapframe_prev_register (struct frame_info *next_frame,
403 void **this_cache, int regnum,
404 int *optimizedp, enum lval_type *lvalp,
405 CORE_ADDR *addrp, int *realnump,
408 struct trad_frame_cache *cache =
409 amd64obsd_trapframe_cache (next_frame, this_cache);
411 trad_frame_get_register (cache, next_frame, regnum,
412 optimizedp, lvalp, addrp, realnump, valuep);
416 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
417 struct frame_info *next_frame,
418 void **this_prologue_cache)
423 /* Check Current Privilege Level and bail out if we're not executing
425 cs = frame_unwind_register_unsigned (next_frame, AMD64_CS_REGNUM);
426 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
429 find_pc_partial_function (frame_pc_unwind (next_frame), &name, NULL, NULL);
430 return (name && ((strcmp (name, "calltrap") == 0)
431 || (strcmp (name, "osyscall1") == 0)
432 || (strcmp (name, "Xsyscall") == 0)
433 || (strncmp (name, "Xintr", 5) == 0)));
436 static const struct frame_unwind amd64obsd_trapframe_unwind = {
437 /* FIXME: kettenis/20051219: This really is more like an interrupt
438 frame, but SIGTRAMP_FRAME would print <signal handler called>,
439 which really is not what we want here. */
441 amd64obsd_trapframe_this_id,
442 amd64obsd_trapframe_prev_register,
444 amd64obsd_trapframe_sniffer
449 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
451 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
453 amd64_init_abi (info, gdbarch);
455 /* Initialize general-purpose register set details. */
456 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
457 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
458 tdep->sizeof_gregset = 24 * 8;
460 set_gdbarch_regset_from_core_section (gdbarch,
461 amd64obsd_regset_from_core_section);
463 tdep->jb_pc_offset = 7 * 8;
465 tdep->sigtramp_p = amd64obsd_sigtramp_p;
466 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
467 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
468 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
470 /* OpenBSD provides a user-level threads implementation. */
471 bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
472 bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
474 /* OpenBSD uses SVR4-style shared libraries. */
475 set_solib_svr4_fetch_link_map_offsets
476 (gdbarch, svr4_lp64_fetch_link_map_offsets);
478 /* Unwind kernel trap frames correctly. */
479 frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
483 /* Provide a prototype to silence -Wmissing-prototypes. */
484 void _initialize_amd64obsd_tdep (void);
487 _initialize_amd64obsd_tdep (void)
489 /* The OpenBSD/amd64 native dependent code makes this assumption. */
490 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
492 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
493 GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
495 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
496 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
497 GDB_OSABI_NETBSD_AOUT, amd64obsd_init_abi);