1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-2015 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 "obsd-tdep.h"
33 #include "amd64-tdep.h"
34 #include "i387-tdep.h"
35 #include "solib-svr4.h"
36 #include "bsd-uthread.h"
38 /* Support for core dumps. */
41 amd64obsd_supply_regset (const struct regset
*regset
,
42 struct regcache
*regcache
, int regnum
,
43 const void *regs
, size_t len
)
45 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
46 const struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
48 gdb_assert (len
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FXSAVE
);
50 i386_supply_gregset (regset
, regcache
, regnum
, regs
, tdep
->sizeof_gregset
);
51 amd64_supply_fxsave (regcache
, regnum
,
52 ((const gdb_byte
*)regs
) + tdep
->sizeof_gregset
);
55 static const struct regset amd64obsd_combined_regset
=
57 NULL
, amd64obsd_supply_regset
, NULL
61 amd64obsd_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
62 iterate_over_regset_sections_cb
*cb
,
64 const struct regcache
*regcache
)
66 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
68 /* OpenBSD core dumps don't use seperate register sets for the
69 general-purpose and floating-point registers. */
71 cb (".reg", tdep
->sizeof_gregset
+ I387_SIZEOF_FXSAVE
,
72 &amd64obsd_combined_regset
, NULL
, cb_data
);
76 /* Support for signal handlers. */
78 /* Default page size. */
79 static const int amd64obsd_page_size
= 4096;
81 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
85 amd64obsd_sigtramp_p (struct frame_info
*this_frame
)
87 CORE_ADDR pc
= get_frame_pc (this_frame
);
88 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
89 const gdb_byte osigreturn
[] =
92 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
93 0xcd, 0x80 /* int $0x80 */
95 const gdb_byte sigreturn
[] =
98 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
99 0x0f, 0x05 /* syscall */
101 size_t buflen
= (sizeof sigreturn
) + 1;
105 /* If the function has a valid symbol name, it isn't a
107 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
111 /* If the function lives in a valid section (even without a starting
112 point) it isn't a trampoline. */
113 if (find_pc_section (pc
) != NULL
)
116 /* If we can't read the instructions at START_PC, return zero. */
117 buf
= alloca ((sizeof sigreturn
) + 1);
118 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, buf
, buflen
))
121 /* Check for sigreturn(2). Depending on how the assembler encoded
122 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
123 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
124 versions use `int $0x80'. Check for both. */
125 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
126 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
127 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
128 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
134 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
135 address of the associated sigcontext structure. */
138 amd64obsd_sigcontext_addr (struct frame_info
*this_frame
)
140 CORE_ADDR pc
= get_frame_pc (this_frame
);
141 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
143 /* The %rsp register points at `struct sigcontext' upon entry of a
144 signal trampoline. The relevant part of the trampoline is
149 movq $SYS_sigreturn,%rax
152 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
153 instruction clobbers %rsp, but its value is saved in `%rdi'. */
156 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
158 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
161 /* OpenBSD 3.5 or later. */
163 /* Mapping between the general-purpose registers in `struct reg'
164 format and GDB's register cache layout. */
166 /* From <machine/reg.h>. */
167 int amd64obsd_r_reg_offset
[] =
184 11 * 8, /* ... %r15 */
186 17 * 8, /* %eflags */
195 /* From <machine/signal.h>. */
196 static int amd64obsd_sc_reg_offset
[] =
213 11 * 8, /* ... %r15 */
215 23 * 8, /* %eflags */
224 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
225 static int amd64obsd_uthread_reg_offset
[] =
235 12 * 8, /* %r8 ... */
242 5 * 8, /* ... %r15 */
253 /* Offset within the thread structure where we can find the saved
254 stack pointer (%esp). */
255 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
258 amd64obsd_supply_uthread (struct regcache
*regcache
,
259 int regnum
, CORE_ADDR addr
)
261 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
262 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
263 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
268 gdb_assert (regnum
>= -1);
270 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
274 /* Fetch stack pointer from thread structure. */
275 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
277 /* Adjust the stack pointer such that it looks as if we just
278 returned from _thread_machdep_switch. */
279 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
280 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
281 regcache_raw_supply (regcache
, AMD64_RSP_REGNUM
, buf
);
284 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
286 if (amd64obsd_uthread_reg_offset
[i
] != -1
287 && (regnum
== -1 || regnum
== i
))
289 /* Fetch stack pointer from thread structure (if we didn't
292 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
294 /* Read the saved register from the stack frame. */
295 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
296 regcache_raw_supply (regcache
, i
, buf
);
302 amd64obsd_collect_uthread (const struct regcache
*regcache
,
303 int regnum
, CORE_ADDR addr
)
305 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
306 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
307 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
312 gdb_assert (regnum
>= -1);
314 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
318 /* Calculate the stack pointer (frame pointer) that will be
319 stored into the thread structure. */
320 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
321 regcache_raw_collect (regcache
, AMD64_RSP_REGNUM
, buf
);
322 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
324 /* Store the stack pointer. */
325 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
327 /* The stack pointer was (potentially) modified. Make sure we
328 build a proper stack frame. */
332 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
334 if (amd64obsd_uthread_reg_offset
[i
] != -1
335 && (regnum
== -1 || regnum
== i
))
337 /* Fetch stack pointer from thread structure (if we didn't
338 calculate it already). */
340 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
342 /* Write the register into the stack frame. */
343 regcache_raw_collect (regcache
, i
, buf
);
344 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
348 /* Kernel debugging support. */
350 /* From <machine/frame.h>. Easy since `struct trapframe' matches
351 `struct sigcontext'. */
352 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
354 static struct trad_frame_cache
*
355 amd64obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
357 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
358 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
359 struct trad_frame_cache
*cache
;
360 CORE_ADDR func
, sp
, addr
;
368 cache
= trad_frame_cache_zalloc (this_frame
);
371 func
= get_frame_func (this_frame
);
372 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
374 find_pc_partial_function (func
, &name
, NULL
, NULL
);
375 if (name
&& startswith (name
, "Xintr"))
376 addr
= sp
+ 8; /* It's an interrupt frame. */
380 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
381 if (amd64obsd_tf_reg_offset
[i
] != -1)
382 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
384 /* Read %cs from trap frame. */
385 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
386 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
387 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
389 /* Trap from user space; terminate backtrace. */
390 trad_frame_set_id (cache
, outer_frame_id
);
394 /* Construct the frame ID using the function start. */
395 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
402 amd64obsd_trapframe_this_id (struct frame_info
*this_frame
,
403 void **this_cache
, struct frame_id
*this_id
)
405 struct trad_frame_cache
*cache
=
406 amd64obsd_trapframe_cache (this_frame
, this_cache
);
408 trad_frame_get_id (cache
, this_id
);
411 static struct value
*
412 amd64obsd_trapframe_prev_register (struct frame_info
*this_frame
,
413 void **this_cache
, int regnum
)
415 struct trad_frame_cache
*cache
=
416 amd64obsd_trapframe_cache (this_frame
, this_cache
);
418 return trad_frame_get_register (cache
, this_frame
, regnum
);
422 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
423 struct frame_info
*this_frame
,
424 void **this_prologue_cache
)
429 /* Check Current Privilege Level and bail out if we're not executing
431 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
432 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
435 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
436 return (name
&& ((strcmp (name
, "calltrap") == 0)
437 || (strcmp (name
, "osyscall1") == 0)
438 || (strcmp (name
, "Xsyscall") == 0)
439 || (startswith (name
, "Xintr"))));
442 static const struct frame_unwind amd64obsd_trapframe_unwind
= {
443 /* FIXME: kettenis/20051219: This really is more like an interrupt
444 frame, but SIGTRAMP_FRAME would print <signal handler called>,
445 which really is not what we want here. */
447 default_frame_unwind_stop_reason
,
448 amd64obsd_trapframe_this_id
,
449 amd64obsd_trapframe_prev_register
,
451 amd64obsd_trapframe_sniffer
456 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
458 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
460 amd64_init_abi (info
, gdbarch
);
461 obsd_init_abi (info
, gdbarch
);
463 /* Initialize general-purpose register set details. */
464 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
465 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
466 tdep
->sizeof_gregset
= 24 * 8;
468 tdep
->jb_pc_offset
= 7 * 8;
470 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
471 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
472 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
473 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
475 /* OpenBSD provides a user-level threads implementation. */
476 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
477 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
479 /* OpenBSD uses SVR4-style shared libraries. */
480 set_solib_svr4_fetch_link_map_offsets
481 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
483 /* Unwind kernel trap frames correctly. */
484 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
487 /* Traditional (a.out) NetBSD-style core dumps. */
490 amd64obsd_core_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
492 amd64obsd_init_abi (info
, gdbarch
);
494 set_gdbarch_iterate_over_regset_sections
495 (gdbarch
, amd64obsd_iterate_over_regset_sections
);
499 /* Provide a prototype to silence -Wmissing-prototypes. */
500 void _initialize_amd64obsd_tdep (void);
503 _initialize_amd64obsd_tdep (void)
505 /* The OpenBSD/amd64 native dependent code makes this assumption. */
506 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
508 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
509 GDB_OSABI_OPENBSD_ELF
, amd64obsd_init_abi
);
511 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
512 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
513 GDB_OSABI_NETBSD_AOUT
, amd64obsd_core_init_abi
);