gdb/amd64obsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for OpenBSD/amd64.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-unwind.h"
  17. #include "gdbcore.h"
  18. #include "symtab.h"
  19. #include "objfiles.h"
  20. #include "osabi.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "target.h"
  24. #include "trad-frame.h"

  25. #include "obsd-tdep.h"
  26. #include "amd64-tdep.h"
  27. #include "i387-tdep.h"
  28. #include "solib-svr4.h"
  29. #include "bsd-uthread.h"

  30. /* Support for core dumps.  */

  31. static void
  32. amd64obsd_supply_regset (const struct regset *regset,
  33.                          struct regcache *regcache, int regnum,
  34.                          const void *regs, size_t len)
  35. {
  36.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  37.   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  38.   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);

  39.   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
  40.   amd64_supply_fxsave (regcache, regnum,
  41.                        ((const gdb_byte *)regs) + tdep->sizeof_gregset);
  42. }

  43. static const struct regset amd64obsd_combined_regset =
  44.   {
  45.     NULL, amd64obsd_supply_regset, NULL
  46.   };

  47. static void
  48. amd64obsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  49.                                         iterate_over_regset_sections_cb *cb,
  50.                                         void *cb_data,
  51.                                         const struct regcache *regcache)
  52. {
  53.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  54.   /* OpenBSD core dumps don't use seperate register sets for the
  55.      general-purpose and floating-point registers.  */

  56.   cb (".reg", tdep->sizeof_gregset + I387_SIZEOF_FXSAVE,
  57.       &amd64obsd_combined_regset, NULL, cb_data);
  58. }


  59. /* Support for signal handlers.  */

  60. /* Default page size.  */
  61. static const int amd64obsd_page_size = 4096;

  62. /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
  63.    routine.  */

  64. static int
  65. amd64obsd_sigtramp_p (struct frame_info *this_frame)
  66. {
  67.   CORE_ADDR pc = get_frame_pc (this_frame);
  68.   CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
  69.   const gdb_byte osigreturn[] =
  70.   {
  71.     0x48, 0xc7, 0xc0,
  72.     0x67, 0x00, 0x00, 0x00,        /* movq $SYS_sigreturn, %rax */
  73.     0xcd, 0x80                        /* int $0x80 */
  74.   };
  75.   const gdb_byte sigreturn[] =
  76.   {
  77.     0x48, 0xc7, 0xc0,
  78.     0x67, 0x00, 0x00, 0x00,        /* movq $SYS_sigreturn, %rax */
  79.     0x0f, 0x05                        /* syscall */
  80.   };
  81.   size_t buflen = (sizeof sigreturn) + 1;
  82.   gdb_byte *buf;
  83.   const char *name;

  84.   /* If the function has a valid symbol name, it isn't a
  85.      trampoline.  */
  86.   find_pc_partial_function (pc, &name, NULL, NULL);
  87.   if (name != NULL)
  88.     return 0;

  89.   /* If the function lives in a valid section (even without a starting
  90.      point) it isn't a trampoline.  */
  91.   if (find_pc_section (pc) != NULL)
  92.     return 0;

  93.   /* If we can't read the instructions at START_PC, return zero.  */
  94.   buf = alloca ((sizeof sigreturn) + 1);
  95.   if (!safe_frame_unwind_memory (this_frame, start_pc + 6, buf, buflen))
  96.     return 0;

  97.   /* Check for sigreturn(2).  Depending on how the assembler encoded
  98.      the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
  99.      7.  OpenBSD 5.0 and later use the `syscall' instruction.  Older
  100.      versions use `int $0x80'.  Check for both.  */
  101.   if (memcmp (buf, sigreturn, sizeof sigreturn)
  102.       && memcmp (buf + 1, sigreturn, sizeof sigreturn)
  103.       && memcmp (buf, osigreturn, sizeof osigreturn)
  104.       && memcmp (buf + 1, osigreturn, sizeof osigreturn))
  105.     return 0;

  106.   return 1;
  107. }

  108. /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
  109.    address of the associated sigcontext structure.  */

  110. static CORE_ADDR
  111. amd64obsd_sigcontext_addr (struct frame_info *this_frame)
  112. {
  113.   CORE_ADDR pc = get_frame_pc (this_frame);
  114.   ULONGEST offset = (pc & (amd64obsd_page_size - 1));

  115.   /* The %rsp register points at `struct sigcontext' upon entry of a
  116.      signal trampoline.  The relevant part of the trampoline is

  117.         call    *%rax
  118.         movq    %rsp, %rdi
  119.         pushq   %rdi
  120.         movq    $SYS_sigreturn,%rax
  121.         int     $0x80

  122.      (see /usr/src/sys/arch/amd64/amd64/locore.S).  The `pushq'
  123.      instruction clobbers %rsp, but its value is saved in `%rdi'.  */

  124.   if (offset > 5)
  125.     return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
  126.   else
  127.     return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
  128. }

  129. /* OpenBSD 3.5 or later.  */

  130. /* Mapping between the general-purpose registers in `struct reg'
  131.    format and GDB's register cache layout.  */

  132. /* From <machine/reg.h>.  */
  133. int amd64obsd_r_reg_offset[] =
  134. {
  135.   14 * 8,                        /* %rax */
  136.   13 * 8,                        /* %rbx */
  137.   3 * 8,                        /* %rcx */
  138.   2 * 8,                        /* %rdx */
  139.   1 * 8,                        /* %rsi */
  140.   0 * 8,                        /* %rdi */
  141.   12 * 8,                        /* %rbp */
  142.   15 * 8,                        /* %rsp */
  143.   4 * 8,                        /* %r8 ..  */
  144.   5 * 8,
  145.   6 * 8,
  146.   7 * 8,
  147.   8 * 8,
  148.   9 * 8,
  149.   10 * 8,
  150.   11 * 8,                        /* ... %r15 */
  151.   16 * 8,                        /* %rip */
  152.   17 * 8,                        /* %eflags */
  153.   18 * 8,                        /* %cs */
  154.   19 * 8,                        /* %ss */
  155.   20 * 8,                        /* %ds */
  156.   21 * 8,                        /* %es */
  157.   22 * 8,                        /* %fs */
  158.   23 * 8                        /* %gs */
  159. };

  160. /* From <machine/signal.h>.  */
  161. static int amd64obsd_sc_reg_offset[] =
  162. {
  163.   14 * 8,                        /* %rax */
  164.   13 * 8,                        /* %rbx */
  165.   3 * 8,                        /* %rcx */
  166.   2 * 8,                        /* %rdx */
  167.   1 * 8,                        /* %rsi */
  168.   0 * 8,                        /* %rdi */
  169.   12 * 8,                        /* %rbp */
  170.   24 * 8,                        /* %rsp */
  171.   4 * 8,                        /* %r8 ...  */
  172.   5 * 8,
  173.   6 * 8,
  174.   7 * 8,
  175.   8 * 8,
  176.   9 * 8,
  177.   10 * 8,
  178.   11 * 8,                        /* ... %r15 */
  179.   21 * 8,                        /* %rip */
  180.   23 * 8,                        /* %eflags */
  181.   22 * 8,                        /* %cs */
  182.   25 * 8,                        /* %ss */
  183.   18 * 8,                        /* %ds */
  184.   17 * 8,                        /* %es */
  185.   16 * 8,                        /* %fs */
  186.   15 * 8                        /* %gs */
  187. };

  188. /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c.  */
  189. static int amd64obsd_uthread_reg_offset[] =
  190. {
  191.   19 * 8,                        /* %rax */
  192.   16 * 8,                        /* %rbx */
  193.   18 * 8,                        /* %rcx */
  194.   17 * 8,                        /* %rdx */
  195.   14 * 8,                        /* %rsi */
  196.   13 * 8,                        /* %rdi */
  197.   15 * 8,                        /* %rbp */
  198.   -1,                                /* %rsp */
  199.   12 * 8,                        /* %r8 ...  */
  200.   11 * 8,
  201.   10 * 8,
  202.   9 * 8,
  203.   8 * 8,
  204.   7 * 8,
  205.   6 * 8,
  206.   5 * 8,                        /* ... %r15 */
  207.   20 * 8,                        /* %rip */
  208.   4 * 8,                        /* %eflags */
  209.   21 * 8,                        /* %cs */
  210.   -1,                                /* %ss */
  211.   3 * 8,                        /* %ds */
  212.   2 * 8,                        /* %es */
  213.   1 * 8,                        /* %fs */
  214.   0 * 8                                /* %gs */
  215. };

  216. /* Offset within the thread structure where we can find the saved
  217.    stack pointer (%esp).  */
  218. #define AMD64OBSD_UTHREAD_RSP_OFFSET        400

  219. static void
  220. amd64obsd_supply_uthread (struct regcache *regcache,
  221.                           int regnum, CORE_ADDR addr)
  222. {
  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;
  226.   CORE_ADDR sp = 0;
  227.   gdb_byte buf[8];
  228.   int i;

  229.   gdb_assert (regnum >= -1);

  230.   if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
  231.     {
  232.       int offset;

  233.       /* Fetch stack pointer from thread structure.  */
  234.       sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);

  235.       /* Adjust the stack pointer such that it looks as if we just
  236.          returned from _thread_machdep_switch.  */
  237.       offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
  238.       store_unsigned_integer (buf, 8, byte_order, sp + offset);
  239.       regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
  240.     }

  241.   for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
  242.     {
  243.       if (amd64obsd_uthread_reg_offset[i] != -1
  244.           && (regnum == -1 || regnum == i))
  245.         {
  246.           /* Fetch stack pointer from thread structure (if we didn't
  247.              do so already).  */
  248.           if (sp == 0)
  249.             sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);

  250.           /* Read the saved register from the stack frame.  */
  251.           read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
  252.           regcache_raw_supply (regcache, i, buf);
  253.         }
  254.     }
  255. }

  256. static void
  257. amd64obsd_collect_uthread (const struct regcache *regcache,
  258.                            int regnum, CORE_ADDR addr)
  259. {
  260.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  261.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  262.   CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
  263.   CORE_ADDR sp = 0;
  264.   gdb_byte buf[8];
  265.   int i;

  266.   gdb_assert (regnum >= -1);

  267.   if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
  268.     {
  269.       int offset;

  270.       /* Calculate the stack pointer (frame pointer) that will be
  271.          stored into the thread structure.  */
  272.       offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
  273.       regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
  274.       sp = extract_unsigned_integer (buf, 8, byte_order) - offset;

  275.       /* Store the stack pointer.  */
  276.       write_memory_unsigned_integer (sp_addr, 8, byte_order, sp);

  277.       /* The stack pointer was (potentially) modified.  Make sure we
  278.          build a proper stack frame.  */
  279.       regnum = -1;
  280.     }

  281.   for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
  282.     {
  283.       if (amd64obsd_uthread_reg_offset[i] != -1
  284.           && (regnum == -1 || regnum == i))
  285.         {
  286.           /* Fetch stack pointer from thread structure (if we didn't
  287.              calculate it already).  */
  288.           if (sp == 0)
  289.             sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);

  290.           /* Write the register into the stack frame.  */
  291.           regcache_raw_collect (regcache, i, buf);
  292.           write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
  293.         }
  294.     }
  295. }
  296. /* Kernel debugging support.  */

  297. /* From <machine/frame.h>.  Easy since `struct trapframe' matches
  298.    `struct sigcontext'.  */
  299. #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset

  300. static struct trad_frame_cache *
  301. amd64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  302. {
  303.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  304.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  305.   struct trad_frame_cache *cache;
  306.   CORE_ADDR func, sp, addr;
  307.   ULONGEST cs;
  308.   const char *name;
  309.   int i;

  310.   if (*this_cache)
  311.     return *this_cache;

  312.   cache = trad_frame_cache_zalloc (this_frame);
  313.   *this_cache = cache;

  314.   func = get_frame_func (this_frame);
  315.   sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);

  316.   find_pc_partial_function (func, &name, NULL, NULL);
  317.   if (name && strncmp (name, "Xintr", 5) == 0)
  318.     addr = sp + 8;                /* It's an interrupt frame.  */
  319.   else
  320.     addr = sp;

  321.   for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
  322.     if (amd64obsd_tf_reg_offset[i] != -1)
  323.       trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);

  324.   /* Read %cs from trap frame.  */
  325.   addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
  326.   cs = read_memory_unsigned_integer (addr, 8, byte_order);
  327.   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  328.     {
  329.       /* Trap from user space; terminate backtrace.  */
  330.       trad_frame_set_id (cache, outer_frame_id);
  331.     }
  332.   else
  333.     {
  334.       /* Construct the frame ID using the function start.  */
  335.       trad_frame_set_id (cache, frame_id_build (sp + 16, func));
  336.     }

  337.   return cache;
  338. }

  339. static void
  340. amd64obsd_trapframe_this_id (struct frame_info *this_frame,
  341.                              void **this_cache, struct frame_id *this_id)
  342. {
  343.   struct trad_frame_cache *cache =
  344.     amd64obsd_trapframe_cache (this_frame, this_cache);

  345.   trad_frame_get_id (cache, this_id);
  346. }

  347. static struct value *
  348. amd64obsd_trapframe_prev_register (struct frame_info *this_frame,
  349.                                    void **this_cache, int regnum)
  350. {
  351.   struct trad_frame_cache *cache =
  352.     amd64obsd_trapframe_cache (this_frame, this_cache);

  353.   return trad_frame_get_register (cache, this_frame, regnum);
  354. }

  355. static int
  356. amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
  357.                              struct frame_info *this_frame,
  358.                              void **this_prologue_cache)
  359. {
  360.   ULONGEST cs;
  361.   const char *name;

  362.   /* Check Current Privilege Level and bail out if we're not executing
  363.      in kernel space.  */
  364.   cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
  365.   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  366.     return 0;

  367.   find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
  368.   return (name && ((strcmp (name, "calltrap") == 0)
  369.                    || (strcmp (name, "osyscall1") == 0)
  370.                    || (strcmp (name, "Xsyscall") == 0)
  371.                    || (strncmp (name, "Xintr", 5) == 0)));
  372. }

  373. static const struct frame_unwind amd64obsd_trapframe_unwind = {
  374.   /* FIXME: kettenis/20051219: This really is more like an interrupt
  375.      frame, but SIGTRAMP_FRAME would print <signal handler called>,
  376.      which really is not what we want here.  */
  377.   NORMAL_FRAME,
  378.   default_frame_unwind_stop_reason,
  379.   amd64obsd_trapframe_this_id,
  380.   amd64obsd_trapframe_prev_register,
  381.   NULL,
  382.   amd64obsd_trapframe_sniffer
  383. };


  384. static void
  385. amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  386. {
  387.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  388.   amd64_init_abi (info, gdbarch);
  389.   obsd_init_abi (info, gdbarch);

  390.   /* Initialize general-purpose register set details.  */
  391.   tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
  392.   tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
  393.   tdep->sizeof_gregset = 24 * 8;

  394.   tdep->jb_pc_offset = 7 * 8;

  395.   tdep->sigtramp_p = amd64obsd_sigtramp_p;
  396.   tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
  397.   tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
  398.   tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);

  399.   /* OpenBSD provides a user-level threads implementation.  */
  400.   bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
  401.   bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);

  402.   /* OpenBSD uses SVR4-style shared libraries.  */
  403.   set_solib_svr4_fetch_link_map_offsets
  404.     (gdbarch, svr4_lp64_fetch_link_map_offsets);

  405.   /* Unwind kernel trap frames correctly.  */
  406.   frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
  407. }

  408. /* Traditional (a.out) NetBSD-style core dumps.  */

  409. static void
  410. amd64obsd_core_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  411. {
  412.   amd64obsd_init_abi (info, gdbarch);

  413.   set_gdbarch_iterate_over_regset_sections
  414.     (gdbarch, amd64obsd_iterate_over_regset_sections);
  415. }


  416. /* Provide a prototype to silence -Wmissing-prototypes.  */
  417. void _initialize_amd64obsd_tdep (void);

  418. void
  419. _initialize_amd64obsd_tdep (void)
  420. {
  421.   /* The OpenBSD/amd64 native dependent code makes this assumption.  */
  422.   gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);

  423.   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
  424.                           GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);

  425.   /* OpenBSD uses traditional (a.out) NetBSD-style core dumps.  */
  426.   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
  427.                           GDB_OSABI_NETBSD_AOUT, amd64obsd_core_init_abi);
  428. }