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 amd64obsd_combined_regset
=
60 NULL
, amd64obsd_supply_regset
, NULL
63 static const struct regset
*
64 amd64obsd_regset_from_core_section (struct gdbarch
*gdbarch
,
65 const char *sect_name
, size_t sect_size
)
67 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
69 /* OpenBSD core dumps don't use seperate register sets for the
70 general-purpose and floating-point registers. */
72 if (strcmp (sect_name
, ".reg") == 0
73 && sect_size
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FXSAVE
)
74 return &amd64obsd_combined_regset
;
80 /* Support for signal handlers. */
82 /* Default page size. */
83 static const int amd64obsd_page_size
= 4096;
85 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
89 amd64obsd_sigtramp_p (struct frame_info
*this_frame
)
91 CORE_ADDR pc
= get_frame_pc (this_frame
);
92 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
93 const gdb_byte osigreturn
[] =
96 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
97 0xcd, 0x80 /* int $0x80 */
99 const gdb_byte sigreturn
[] =
102 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
103 0x0f, 0x05 /* syscall */
105 size_t buflen
= (sizeof sigreturn
) + 1;
109 /* If the function has a valid symbol name, it isn't a
111 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
115 /* If the function lives in a valid section (even without a starting
116 point) it isn't a trampoline. */
117 if (find_pc_section (pc
) != NULL
)
120 /* If we can't read the instructions at START_PC, return zero. */
121 buf
= alloca ((sizeof sigreturn
) + 1);
122 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, buf
, buflen
))
125 /* Check for sigreturn(2). Depending on how the assembler encoded
126 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
127 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
128 versions use `int $0x80'. Check for both. */
129 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
130 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
131 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
132 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
138 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
139 address of the associated sigcontext structure. */
142 amd64obsd_sigcontext_addr (struct frame_info
*this_frame
)
144 CORE_ADDR pc
= get_frame_pc (this_frame
);
145 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
147 /* The %rsp register points at `struct sigcontext' upon entry of a
148 signal trampoline. The relevant part of the trampoline is
153 movq $SYS_sigreturn,%rax
156 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
157 instruction clobbers %rsp, but its value is saved in `%rdi'. */
160 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
162 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
165 /* OpenBSD 3.5 or later. */
167 /* Mapping between the general-purpose registers in `struct reg'
168 format and GDB's register cache layout. */
170 /* From <machine/reg.h>. */
171 int amd64obsd_r_reg_offset
[] =
188 11 * 8, /* ... %r15 */
190 17 * 8, /* %eflags */
199 /* From <machine/signal.h>. */
200 static int amd64obsd_sc_reg_offset
[] =
217 11 * 8, /* ... %r15 */
219 23 * 8, /* %eflags */
228 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
229 static int amd64obsd_uthread_reg_offset
[] =
239 12 * 8, /* %r8 ... */
246 5 * 8, /* ... %r15 */
257 /* Offset within the thread structure where we can find the saved
258 stack pointer (%esp). */
259 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
262 amd64obsd_supply_uthread (struct regcache
*regcache
,
263 int regnum
, CORE_ADDR addr
)
265 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
266 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
267 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
272 gdb_assert (regnum
>= -1);
274 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
278 /* Fetch stack pointer from thread structure. */
279 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
281 /* Adjust the stack pointer such that it looks as if we just
282 returned from _thread_machdep_switch. */
283 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
284 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
285 regcache_raw_supply (regcache
, AMD64_RSP_REGNUM
, buf
);
288 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
290 if (amd64obsd_uthread_reg_offset
[i
] != -1
291 && (regnum
== -1 || regnum
== i
))
293 /* Fetch stack pointer from thread structure (if we didn't
296 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
298 /* Read the saved register from the stack frame. */
299 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
300 regcache_raw_supply (regcache
, i
, buf
);
306 amd64obsd_collect_uthread (const struct regcache
*regcache
,
307 int regnum
, CORE_ADDR addr
)
309 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
310 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
311 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
316 gdb_assert (regnum
>= -1);
318 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
322 /* Calculate the stack pointer (frame pointer) that will be
323 stored into the thread structure. */
324 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
325 regcache_raw_collect (regcache
, AMD64_RSP_REGNUM
, buf
);
326 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
328 /* Store the stack pointer. */
329 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
331 /* The stack pointer was (potentially) modified. Make sure we
332 build a proper stack frame. */
336 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
338 if (amd64obsd_uthread_reg_offset
[i
] != -1
339 && (regnum
== -1 || regnum
== i
))
341 /* Fetch stack pointer from thread structure (if we didn't
342 calculate it already). */
344 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
346 /* Write the register into the stack frame. */
347 regcache_raw_collect (regcache
, i
, buf
);
348 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
352 /* Kernel debugging support. */
354 /* From <machine/frame.h>. Easy since `struct trapframe' matches
355 `struct sigcontext'. */
356 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
358 static struct trad_frame_cache
*
359 amd64obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
361 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
362 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
363 struct trad_frame_cache
*cache
;
364 CORE_ADDR func
, sp
, addr
;
372 cache
= trad_frame_cache_zalloc (this_frame
);
375 func
= get_frame_func (this_frame
);
376 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
378 find_pc_partial_function (func
, &name
, NULL
, NULL
);
379 if (name
&& strncmp (name
, "Xintr", 5) == 0)
380 addr
= sp
+ 8; /* It's an interrupt frame. */
384 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
385 if (amd64obsd_tf_reg_offset
[i
] != -1)
386 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
388 /* Read %cs from trap frame. */
389 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
390 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
391 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
393 /* Trap from user space; terminate backtrace. */
394 trad_frame_set_id (cache
, outer_frame_id
);
398 /* Construct the frame ID using the function start. */
399 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
406 amd64obsd_trapframe_this_id (struct frame_info
*this_frame
,
407 void **this_cache
, struct frame_id
*this_id
)
409 struct trad_frame_cache
*cache
=
410 amd64obsd_trapframe_cache (this_frame
, this_cache
);
412 trad_frame_get_id (cache
, this_id
);
415 static struct value
*
416 amd64obsd_trapframe_prev_register (struct frame_info
*this_frame
,
417 void **this_cache
, int regnum
)
419 struct trad_frame_cache
*cache
=
420 amd64obsd_trapframe_cache (this_frame
, this_cache
);
422 return trad_frame_get_register (cache
, this_frame
, regnum
);
426 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
427 struct frame_info
*this_frame
,
428 void **this_prologue_cache
)
433 /* Check Current Privilege Level and bail out if we're not executing
435 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
436 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
439 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
440 return (name
&& ((strcmp (name
, "calltrap") == 0)
441 || (strcmp (name
, "osyscall1") == 0)
442 || (strcmp (name
, "Xsyscall") == 0)
443 || (strncmp (name
, "Xintr", 5) == 0)));
446 static const struct frame_unwind amd64obsd_trapframe_unwind
= {
447 /* FIXME: kettenis/20051219: This really is more like an interrupt
448 frame, but SIGTRAMP_FRAME would print <signal handler called>,
449 which really is not what we want here. */
451 default_frame_unwind_stop_reason
,
452 amd64obsd_trapframe_this_id
,
453 amd64obsd_trapframe_prev_register
,
455 amd64obsd_trapframe_sniffer
460 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
462 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
464 amd64_init_abi (info
, gdbarch
);
465 obsd_init_abi (info
, gdbarch
);
467 /* Initialize general-purpose register set details. */
468 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
469 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
470 tdep
->sizeof_gregset
= 24 * 8;
472 tdep
->jb_pc_offset
= 7 * 8;
474 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
475 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
476 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
477 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
479 /* OpenBSD provides a user-level threads implementation. */
480 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
481 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
483 /* OpenBSD uses SVR4-style shared libraries. */
484 set_solib_svr4_fetch_link_map_offsets
485 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
487 /* Unwind kernel trap frames correctly. */
488 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
491 /* Traditional (a.out) NetBSD-style core dumps. */
494 amd64obsd_core_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
496 amd64obsd_init_abi (info
, gdbarch
);
498 set_gdbarch_regset_from_core_section
499 (gdbarch
, amd64obsd_regset_from_core_section
);
503 /* Provide a prototype to silence -Wmissing-prototypes. */
504 void _initialize_amd64obsd_tdep (void);
507 _initialize_amd64obsd_tdep (void)
509 /* The OpenBSD/amd64 native dependent code makes this assumption. */
510 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
512 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
513 GDB_OSABI_OPENBSD_ELF
, amd64obsd_init_abi
);
515 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
516 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
517 GDB_OSABI_NETBSD_AOUT
, amd64obsd_core_init_abi
);