1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-2017 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 signal handlers. */
40 /* Default page size. */
41 static const int amd64obsd_page_size
= 4096;
43 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
47 amd64obsd_sigtramp_p (struct frame_info
*this_frame
)
49 CORE_ADDR pc
= get_frame_pc (this_frame
);
50 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
51 const gdb_byte osigreturn
[] =
54 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
55 0xcd, 0x80 /* int $0x80 */
57 const gdb_byte sigreturn
[] =
60 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
61 0x0f, 0x05 /* syscall */
63 size_t buflen
= (sizeof sigreturn
) + 1;
67 /* If the function has a valid symbol name, it isn't a
69 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
73 /* If the function lives in a valid section (even without a starting
74 point) it isn't a trampoline. */
75 if (find_pc_section (pc
) != NULL
)
78 /* If we can't read the instructions at START_PC, return zero. */
79 buf
= (gdb_byte
*) alloca ((sizeof sigreturn
) + 1);
80 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, buf
, buflen
))
83 /* Check for sigreturn(2). Depending on how the assembler encoded
84 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
85 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
86 versions use `int $0x80'. Check for both. */
87 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
88 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
89 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
90 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
96 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
97 address of the associated sigcontext structure. */
100 amd64obsd_sigcontext_addr (struct frame_info
*this_frame
)
102 CORE_ADDR pc
= get_frame_pc (this_frame
);
103 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
105 /* The %rsp register points at `struct sigcontext' upon entry of a
106 signal trampoline. The relevant part of the trampoline is
111 movq $SYS_sigreturn,%rax
114 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
115 instruction clobbers %rsp, but its value is saved in `%rdi'. */
118 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
120 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
123 /* OpenBSD 3.5 or later. */
125 /* Mapping between the general-purpose registers in `struct reg'
126 format and GDB's register cache layout. */
128 /* From <machine/reg.h>. */
129 int amd64obsd_r_reg_offset
[] =
146 11 * 8, /* ... %r15 */
148 17 * 8, /* %eflags */
157 /* From <machine/signal.h>. */
158 static int amd64obsd_sc_reg_offset
[] =
175 11 * 8, /* ... %r15 */
177 23 * 8, /* %eflags */
186 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
187 static int amd64obsd_uthread_reg_offset
[] =
197 12 * 8, /* %r8 ... */
204 5 * 8, /* ... %r15 */
215 /* Offset within the thread structure where we can find the saved
216 stack pointer (%esp). */
217 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
220 amd64obsd_supply_uthread (struct regcache
*regcache
,
221 int regnum
, CORE_ADDR addr
)
223 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
224 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
225 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
230 gdb_assert (regnum
>= -1);
232 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
236 /* Fetch stack pointer from thread structure. */
237 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
239 /* Adjust the stack pointer such that it looks as if we just
240 returned from _thread_machdep_switch. */
241 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
242 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
243 regcache_raw_supply (regcache
, AMD64_RSP_REGNUM
, buf
);
246 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
248 if (amd64obsd_uthread_reg_offset
[i
] != -1
249 && (regnum
== -1 || regnum
== i
))
251 /* Fetch stack pointer from thread structure (if we didn't
254 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
256 /* Read the saved register from the stack frame. */
257 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
258 regcache_raw_supply (regcache
, i
, buf
);
264 amd64obsd_collect_uthread (const struct regcache
*regcache
,
265 int regnum
, CORE_ADDR addr
)
267 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
268 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
269 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
274 gdb_assert (regnum
>= -1);
276 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
280 /* Calculate the stack pointer (frame pointer) that will be
281 stored into the thread structure. */
282 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
283 regcache_raw_collect (regcache
, AMD64_RSP_REGNUM
, buf
);
284 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
286 /* Store the stack pointer. */
287 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
289 /* The stack pointer was (potentially) modified. Make sure we
290 build a proper stack frame. */
294 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
296 if (amd64obsd_uthread_reg_offset
[i
] != -1
297 && (regnum
== -1 || regnum
== i
))
299 /* Fetch stack pointer from thread structure (if we didn't
300 calculate it already). */
302 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
304 /* Write the register into the stack frame. */
305 regcache_raw_collect (regcache
, i
, buf
);
306 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
310 /* Kernel debugging support. */
312 /* From <machine/frame.h>. Easy since `struct trapframe' matches
313 `struct sigcontext'. */
314 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
316 static struct trad_frame_cache
*
317 amd64obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
319 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
320 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
321 struct trad_frame_cache
*cache
;
322 CORE_ADDR func
, sp
, addr
;
328 return (struct trad_frame_cache
*) *this_cache
;
330 cache
= trad_frame_cache_zalloc (this_frame
);
333 func
= get_frame_func (this_frame
);
334 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
336 find_pc_partial_function (func
, &name
, NULL
, NULL
);
337 if (name
&& startswith (name
, "Xintr"))
338 addr
= sp
+ 8; /* It's an interrupt frame. */
342 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
343 if (amd64obsd_tf_reg_offset
[i
] != -1)
344 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
346 /* Read %cs from trap frame. */
347 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
348 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
349 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
351 /* Trap from user space; terminate backtrace. */
352 trad_frame_set_id (cache
, outer_frame_id
);
356 /* Construct the frame ID using the function start. */
357 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
364 amd64obsd_trapframe_this_id (struct frame_info
*this_frame
,
365 void **this_cache
, struct frame_id
*this_id
)
367 struct trad_frame_cache
*cache
=
368 amd64obsd_trapframe_cache (this_frame
, this_cache
);
370 trad_frame_get_id (cache
, this_id
);
373 static struct value
*
374 amd64obsd_trapframe_prev_register (struct frame_info
*this_frame
,
375 void **this_cache
, int regnum
)
377 struct trad_frame_cache
*cache
=
378 amd64obsd_trapframe_cache (this_frame
, this_cache
);
380 return trad_frame_get_register (cache
, this_frame
, regnum
);
384 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
385 struct frame_info
*this_frame
,
386 void **this_prologue_cache
)
391 /* Check Current Privilege Level and bail out if we're not executing
393 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
394 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
397 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
398 return (name
&& ((strcmp (name
, "calltrap") == 0)
399 || (strcmp (name
, "osyscall1") == 0)
400 || (strcmp (name
, "Xsyscall") == 0)
401 || (startswith (name
, "Xintr"))));
404 static const struct frame_unwind amd64obsd_trapframe_unwind
= {
405 /* FIXME: kettenis/20051219: This really is more like an interrupt
406 frame, but SIGTRAMP_FRAME would print <signal handler called>,
407 which really is not what we want here. */
409 default_frame_unwind_stop_reason
,
410 amd64obsd_trapframe_this_id
,
411 amd64obsd_trapframe_prev_register
,
413 amd64obsd_trapframe_sniffer
418 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
420 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
422 amd64_init_abi (info
, gdbarch
);
423 obsd_init_abi (info
, gdbarch
);
425 /* Initialize general-purpose register set details. */
426 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
427 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
428 tdep
->sizeof_gregset
= 24 * 8;
430 tdep
->jb_pc_offset
= 7 * 8;
432 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
433 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
434 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
435 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
437 /* OpenBSD provides a user-level threads implementation. */
438 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
439 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
441 /* OpenBSD uses SVR4-style shared libraries. */
442 set_solib_svr4_fetch_link_map_offsets
443 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
445 /* Unwind kernel trap frames correctly. */
446 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
450 /* Provide a prototype to silence -Wmissing-prototypes. */
451 void _initialize_amd64obsd_tdep (void);
454 _initialize_amd64obsd_tdep (void)
456 /* The OpenBSD/amd64 native dependent code makes this assumption. */
457 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
459 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
460 GDB_OSABI_OPENBSD
, amd64obsd_init_abi
);