1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "frame-unwind.h"
30 #include "trad-frame.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 #include "amd64-tdep.h"
36 #include "i387-tdep.h"
37 #include "solib-svr4.h"
38 #include "bsd-uthread.h"
40 /* Support for core dumps. */
43 amd64obsd_supply_regset (const struct regset *regset,
44 struct regcache *regcache, int regnum,
45 const void *regs, size_t len)
47 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
49 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
51 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
52 amd64_supply_fxsave (regcache, regnum,
53 ((const gdb_byte *)regs) + tdep->sizeof_gregset);
56 static const struct regset *
57 amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
58 const char *sect_name, size_t sect_size)
60 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
62 /* OpenBSD core dumps don't use seperate register sets for the
63 general-purpose and floating-point registers. */
65 if (strcmp (sect_name, ".reg") == 0
66 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
68 if (tdep->gregset == NULL)
69 tdep->gregset = regset_alloc (gdbarch, amd64obsd_supply_regset, NULL);
77 /* Support for signal handlers. */
79 /* Default page size. */
80 static const int amd64obsd_page_size = 4096;
82 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
86 amd64obsd_sigtramp_p (struct frame_info *this_frame)
88 CORE_ADDR pc = get_frame_pc (this_frame);
89 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
90 const gdb_byte sigreturn[] =
93 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
94 0xcd, 0x80 /* int $0x80 */
96 size_t buflen = (sizeof sigreturn) + 1;
100 /* If the function has a valid symbol name, it isn't a
102 find_pc_partial_function (pc, &name, NULL, NULL);
106 /* If the function lives in a valid section (even without a starting
107 point) it isn't a trampoline. */
108 if (find_pc_section (pc) != NULL)
111 /* If we can't read the instructions at START_PC, return zero. */
112 buf = alloca ((sizeof sigreturn) + 1);
113 if (!safe_frame_unwind_memory (this_frame, start_pc + 6, buf, buflen))
116 /* Check for sigreturn(2). Depending on how the assembler encoded
117 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
119 if (memcmp (buf, sigreturn, sizeof sigreturn)
120 && memcpy (buf + 1, sigreturn, sizeof sigreturn))
126 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
127 address of the associated sigcontext structure. */
130 amd64obsd_sigcontext_addr (struct frame_info *this_frame)
132 CORE_ADDR pc = get_frame_pc (this_frame);
133 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
135 /* The %rsp register points at `struct sigcontext' upon entry of a
136 signal trampoline. The relevant part of the trampoline is
141 movq $SYS_sigreturn,%rax
144 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
145 instruction clobbers %rsp, but its value is saved in `%rdi'. */
148 return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
150 return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
153 /* OpenBSD 3.5 or later. */
155 /* Mapping between the general-purpose registers in `struct reg'
156 format and GDB's register cache layout. */
158 /* From <machine/reg.h>. */
159 int amd64obsd_r_reg_offset[] =
176 11 * 8, /* ... %r15 */
178 17 * 8, /* %eflags */
187 /* From <machine/signal.h>. */
188 static int amd64obsd_sc_reg_offset[] =
205 11 * 8, /* ... %r15 */
207 23 * 8, /* %eflags */
216 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
217 static int amd64obsd_uthread_reg_offset[] =
227 12 * 8, /* %r8 ... */
234 5 * 8, /* ... %r15 */
245 /* Offset within the thread structure where we can find the saved
246 stack pointer (%esp). */
247 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
250 amd64obsd_supply_uthread (struct regcache *regcache,
251 int regnum, CORE_ADDR addr)
253 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
258 gdb_assert (regnum >= -1);
260 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
264 /* Fetch stack pointer from thread structure. */
265 sp = read_memory_unsigned_integer (sp_addr, 8);
267 /* Adjust the stack pointer such that it looks as if we just
268 returned from _thread_machdep_switch. */
269 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
270 store_unsigned_integer (buf, 8, sp + offset);
271 regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
274 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
276 if (amd64obsd_uthread_reg_offset[i] != -1
277 && (regnum == -1 || regnum == i))
279 /* Fetch stack pointer from thread structure (if we didn't
282 sp = read_memory_unsigned_integer (sp_addr, 8);
284 /* Read the saved register from the stack frame. */
285 read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
286 regcache_raw_supply (regcache, i, buf);
292 amd64obsd_collect_uthread (const struct regcache *regcache,
293 int regnum, CORE_ADDR addr)
295 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
300 gdb_assert (regnum >= -1);
302 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
306 /* Calculate the stack pointer (frame pointer) that will be
307 stored into the thread structure. */
308 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
309 regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
310 sp = extract_unsigned_integer (buf, 8) - offset;
312 /* Store the stack pointer. */
313 write_memory_unsigned_integer (sp_addr, 8, sp);
315 /* The stack pointer was (potentially) modified. Make sure we
316 build a proper stack frame. */
320 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
322 if (amd64obsd_uthread_reg_offset[i] != -1
323 && (regnum == -1 || regnum == i))
325 /* Fetch stack pointer from thread structure (if we didn't
326 calculate it already). */
328 sp = read_memory_unsigned_integer (sp_addr, 8);
330 /* Write the register into the stack frame. */
331 regcache_raw_collect (regcache, i, buf);
332 write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
336 /* Kernel debugging support. */
338 /* From <machine/frame.h>. Easy since `struct trapframe' matches
339 `struct sigcontext'. */
340 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
342 static struct trad_frame_cache *
343 amd64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
345 struct trad_frame_cache *cache;
346 CORE_ADDR func, sp, addr;
354 cache = trad_frame_cache_zalloc (this_frame);
357 func = get_frame_func (this_frame);
358 sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
360 find_pc_partial_function (func, &name, NULL, NULL);
361 if (name && strncmp (name, "Xintr", 5) == 0)
362 addr = sp + 8; /* It's an interrupt frame. */
366 for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
367 if (amd64obsd_tf_reg_offset[i] != -1)
368 trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
370 /* Read %cs from trap frame. */
371 addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
372 cs = read_memory_unsigned_integer (addr, 8);
373 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
375 /* Trap from user space; terminate backtrace. */
376 trad_frame_set_id (cache, null_frame_id);
380 /* Construct the frame ID using the function start. */
381 trad_frame_set_id (cache, frame_id_build (sp + 16, func));
388 amd64obsd_trapframe_this_id (struct frame_info *this_frame,
389 void **this_cache, struct frame_id *this_id)
391 struct trad_frame_cache *cache =
392 amd64obsd_trapframe_cache (this_frame, this_cache);
394 trad_frame_get_id (cache, this_id);
397 static struct value *
398 amd64obsd_trapframe_prev_register (struct frame_info *this_frame,
399 void **this_cache, int regnum)
401 struct trad_frame_cache *cache =
402 amd64obsd_trapframe_cache (this_frame, this_cache);
404 return trad_frame_get_register (cache, this_frame, regnum);
408 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
409 struct frame_info *this_frame,
410 void **this_prologue_cache)
415 /* Check Current Privilege Level and bail out if we're not executing
417 cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
418 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
421 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
422 return (name && ((strcmp (name, "calltrap") == 0)
423 || (strcmp (name, "osyscall1") == 0)
424 || (strcmp (name, "Xsyscall") == 0)
425 || (strncmp (name, "Xintr", 5) == 0)));
428 static const struct frame_unwind amd64obsd_trapframe_unwind = {
429 /* FIXME: kettenis/20051219: This really is more like an interrupt
430 frame, but SIGTRAMP_FRAME would print <signal handler called>,
431 which really is not what we want here. */
433 amd64obsd_trapframe_this_id,
434 amd64obsd_trapframe_prev_register,
436 amd64obsd_trapframe_sniffer
441 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
443 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
445 amd64_init_abi (info, gdbarch);
447 /* Initialize general-purpose register set details. */
448 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
449 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
450 tdep->sizeof_gregset = 24 * 8;
452 set_gdbarch_regset_from_core_section (gdbarch,
453 amd64obsd_regset_from_core_section);
455 tdep->jb_pc_offset = 7 * 8;
457 tdep->sigtramp_p = amd64obsd_sigtramp_p;
458 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
459 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
460 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
462 /* OpenBSD provides a user-level threads implementation. */
463 bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
464 bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
466 /* OpenBSD uses SVR4-style shared libraries. */
467 set_solib_svr4_fetch_link_map_offsets
468 (gdbarch, svr4_lp64_fetch_link_map_offsets);
470 /* Unwind kernel trap frames correctly. */
471 frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
475 /* Provide a prototype to silence -Wmissing-prototypes. */
476 void _initialize_amd64obsd_tdep (void);
479 _initialize_amd64obsd_tdep (void)
481 /* The OpenBSD/amd64 native dependent code makes this assumption. */
482 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
484 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
485 GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
487 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
488 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
489 GDB_OSABI_NETBSD_AOUT, amd64obsd_init_abi);