1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-2014 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"
35 #include "obsd-tdep.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 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
49 const struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
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 THIS_FRAME corresponds to an OpenBSD sigtramp
88 amd64obsd_sigtramp_p (struct frame_info
*this_frame
)
90 CORE_ADDR pc
= get_frame_pc (this_frame
);
91 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
92 const gdb_byte osigreturn
[] =
95 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
96 0xcd, 0x80 /* int $0x80 */
98 const gdb_byte sigreturn
[] =
101 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
102 0x0f, 0x05 /* syscall */
104 size_t buflen
= (sizeof sigreturn
) + 1;
108 /* If the function has a valid symbol name, it isn't a
110 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
114 /* If the function lives in a valid section (even without a starting
115 point) it isn't a trampoline. */
116 if (find_pc_section (pc
) != NULL
)
119 /* If we can't read the instructions at START_PC, return zero. */
120 buf
= alloca ((sizeof sigreturn
) + 1);
121 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, buf
, buflen
))
124 /* Check for sigreturn(2). Depending on how the assembler encoded
125 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
126 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
127 versions use `int $0x80'. Check for both. */
128 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
129 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
130 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
131 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
137 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
138 address of the associated sigcontext structure. */
141 amd64obsd_sigcontext_addr (struct frame_info
*this_frame
)
143 CORE_ADDR pc
= get_frame_pc (this_frame
);
144 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
146 /* The %rsp register points at `struct sigcontext' upon entry of a
147 signal trampoline. The relevant part of the trampoline is
152 movq $SYS_sigreturn,%rax
155 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
156 instruction clobbers %rsp, but its value is saved in `%rdi'. */
159 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
161 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
164 /* OpenBSD 3.5 or later. */
166 /* Mapping between the general-purpose registers in `struct reg'
167 format and GDB's register cache layout. */
169 /* From <machine/reg.h>. */
170 int amd64obsd_r_reg_offset
[] =
187 11 * 8, /* ... %r15 */
189 17 * 8, /* %eflags */
198 /* From <machine/signal.h>. */
199 static int amd64obsd_sc_reg_offset
[] =
216 11 * 8, /* ... %r15 */
218 23 * 8, /* %eflags */
227 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
228 static int amd64obsd_uthread_reg_offset
[] =
238 12 * 8, /* %r8 ... */
245 5 * 8, /* ... %r15 */
256 /* Offset within the thread structure where we can find the saved
257 stack pointer (%esp). */
258 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
261 amd64obsd_supply_uthread (struct regcache
*regcache
,
262 int regnum
, CORE_ADDR addr
)
264 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
265 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
266 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
271 gdb_assert (regnum
>= -1);
273 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
277 /* Fetch stack pointer from thread structure. */
278 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
280 /* Adjust the stack pointer such that it looks as if we just
281 returned from _thread_machdep_switch. */
282 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
283 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
284 regcache_raw_supply (regcache
, AMD64_RSP_REGNUM
, buf
);
287 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
289 if (amd64obsd_uthread_reg_offset
[i
] != -1
290 && (regnum
== -1 || regnum
== i
))
292 /* Fetch stack pointer from thread structure (if we didn't
295 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
297 /* Read the saved register from the stack frame. */
298 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
299 regcache_raw_supply (regcache
, i
, buf
);
305 amd64obsd_collect_uthread (const struct regcache
*regcache
,
306 int regnum
, CORE_ADDR addr
)
308 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
309 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
310 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
315 gdb_assert (regnum
>= -1);
317 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
321 /* Calculate the stack pointer (frame pointer) that will be
322 stored into the thread structure. */
323 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
324 regcache_raw_collect (regcache
, AMD64_RSP_REGNUM
, buf
);
325 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
327 /* Store the stack pointer. */
328 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
330 /* The stack pointer was (potentially) modified. Make sure we
331 build a proper stack frame. */
335 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
337 if (amd64obsd_uthread_reg_offset
[i
] != -1
338 && (regnum
== -1 || regnum
== i
))
340 /* Fetch stack pointer from thread structure (if we didn't
341 calculate it already). */
343 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
345 /* Write the register into the stack frame. */
346 regcache_raw_collect (regcache
, i
, buf
);
347 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
351 /* Kernel debugging support. */
353 /* From <machine/frame.h>. Easy since `struct trapframe' matches
354 `struct sigcontext'. */
355 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
357 static struct trad_frame_cache
*
358 amd64obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
360 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
361 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
362 struct trad_frame_cache
*cache
;
363 CORE_ADDR func
, sp
, addr
;
371 cache
= trad_frame_cache_zalloc (this_frame
);
374 func
= get_frame_func (this_frame
);
375 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
377 find_pc_partial_function (func
, &name
, NULL
, NULL
);
378 if (name
&& strncmp (name
, "Xintr", 5) == 0)
379 addr
= sp
+ 8; /* It's an interrupt frame. */
383 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
384 if (amd64obsd_tf_reg_offset
[i
] != -1)
385 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
387 /* Read %cs from trap frame. */
388 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
389 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
390 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
392 /* Trap from user space; terminate backtrace. */
393 trad_frame_set_id (cache
, outer_frame_id
);
397 /* Construct the frame ID using the function start. */
398 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
405 amd64obsd_trapframe_this_id (struct frame_info
*this_frame
,
406 void **this_cache
, struct frame_id
*this_id
)
408 struct trad_frame_cache
*cache
=
409 amd64obsd_trapframe_cache (this_frame
, this_cache
);
411 trad_frame_get_id (cache
, this_id
);
414 static struct value
*
415 amd64obsd_trapframe_prev_register (struct frame_info
*this_frame
,
416 void **this_cache
, int regnum
)
418 struct trad_frame_cache
*cache
=
419 amd64obsd_trapframe_cache (this_frame
, this_cache
);
421 return trad_frame_get_register (cache
, this_frame
, regnum
);
425 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
426 struct frame_info
*this_frame
,
427 void **this_prologue_cache
)
432 /* Check Current Privilege Level and bail out if we're not executing
434 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
435 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
438 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
439 return (name
&& ((strcmp (name
, "calltrap") == 0)
440 || (strcmp (name
, "osyscall1") == 0)
441 || (strcmp (name
, "Xsyscall") == 0)
442 || (strncmp (name
, "Xintr", 5) == 0)));
445 static const struct frame_unwind amd64obsd_trapframe_unwind
= {
446 /* FIXME: kettenis/20051219: This really is more like an interrupt
447 frame, but SIGTRAMP_FRAME would print <signal handler called>,
448 which really is not what we want here. */
450 default_frame_unwind_stop_reason
,
451 amd64obsd_trapframe_this_id
,
452 amd64obsd_trapframe_prev_register
,
454 amd64obsd_trapframe_sniffer
459 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
461 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
463 amd64_init_abi (info
, gdbarch
);
464 obsd_init_abi (info
, gdbarch
);
466 /* Initialize general-purpose register set details. */
467 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
468 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
469 tdep
->sizeof_gregset
= 24 * 8;
471 tdep
->jb_pc_offset
= 7 * 8;
473 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
474 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
475 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
476 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
478 /* OpenBSD provides a user-level threads implementation. */
479 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
480 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
482 /* OpenBSD uses SVR4-style shared libraries. */
483 set_solib_svr4_fetch_link_map_offsets
484 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
486 /* Unwind kernel trap frames correctly. */
487 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
490 /* Traditional (a.out) NetBSD-style core dumps. */
493 amd64obsd_core_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
495 amd64obsd_init_abi (info
, gdbarch
);
497 set_gdbarch_regset_from_core_section
498 (gdbarch
, amd64obsd_regset_from_core_section
);
502 /* Provide a prototype to silence -Wmissing-prototypes. */
503 void _initialize_amd64obsd_tdep (void);
506 _initialize_amd64obsd_tdep (void)
508 /* The OpenBSD/amd64 native dependent code makes this assumption. */
509 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
511 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
512 GDB_OSABI_OPENBSD_ELF
, amd64obsd_init_abi
);
514 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
515 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
516 GDB_OSABI_NETBSD_AOUT
, amd64obsd_core_init_abi
);