gdb/amd64obsd-nat.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Native-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 "gdbcore.h"
  16. #include "regcache.h"
  17. #include "target.h"

  18. #include "amd64-tdep.h"
  19. #include "amd64-nat.h"
  20. #include "obsd-nat.h"

  21. /* Mapping between the general-purpose registers in OpenBSD/amd64
  22.    `struct reg' format and GDB's register cache layout for
  23.    OpenBSD/i386.

  24.    Note that most (if not all) OpenBSD/amd64 registers are 64-bit,
  25.    while the OpenBSD/i386 registers are all 32-bit, but since we're
  26.    little-endian we get away with that.  */

  27. /* From <machine/reg.h>.  */
  28. static int amd64obsd32_r_reg_offset[] =
  29. {
  30.   14 * 8,                        /* %eax */
  31.   3 * 8,                        /* %ecx */
  32.   2 * 8,                        /* %edx */
  33.   13 * 8,                        /* %ebx */
  34.   15 * 8,                        /* %esp */
  35.   12 * 8,                        /* %ebp */
  36.   1 * 8,                        /* %esi */
  37.   0 * 8,                        /* %edi */
  38.   16 * 8,                        /* %eip */
  39.   17 * 8,                        /* %eflags */
  40.   18 * 8,                        /* %cs */
  41.   19 * 8,                        /* %ss */
  42.   20 * 8,                        /* %ds */
  43.   21 * 8,                        /* %es */
  44.   22 * 8,                        /* %fs */
  45.   23 * 8                        /* %gs */
  46. };


  47. /* Support for debugging kernel virtual memory images.  */

  48. #include <sys/types.h>
  49. #include <machine/frame.h>
  50. #include <machine/pcb.h>

  51. #include "bsd-kvm.h"

  52. static int
  53. amd64obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  54. {
  55.   struct switchframe sf;
  56.   int regnum;

  57.   /* The following is true for OpenBSD 3.5:

  58.      The pcb contains the stack pointer at the point of the context
  59.      switch in cpu_switch().  At that point we have a stack frame as
  60.      described by `struct switchframe', which for OpenBSD 3.5 has the
  61.      following layout:

  62.      interrupt level
  63.      %r15
  64.      %r14
  65.      %r13
  66.      %r12
  67.      %rbp
  68.      %rbx
  69.      return address

  70.      Together with %rsp in the pcb, this accounts for all callee-saved
  71.      registers specified by the psABI.  From this information we
  72.      reconstruct the register state as it would look when we just
  73.      returned from cpu_switch().

  74.      For core dumps the pcb is saved by savectx().  In that case the
  75.      stack frame only contains the return address, and there is no way
  76.      to recover the other registers.  */

  77.   /* The stack pointer shouldn't be zero.  */
  78.   if (pcb->pcb_rsp == 0)
  79.     return 0;

  80.   /* Read the stack frame, and check its validity.  */
  81.   read_memory (pcb->pcb_rsp, (gdb_byte *) &sf, sizeof sf);
  82.   if (sf.sf_rbp == pcb->pcb_rbp)
  83.     {
  84.       /* Yes, we have a frame that matches cpu_switch().  */
  85.       pcb->pcb_rsp += sizeof (struct switchframe);
  86.       regcache_raw_supply (regcache, 12, &sf.sf_r12);
  87.       regcache_raw_supply (regcache, 13, &sf.sf_r13);
  88.       regcache_raw_supply (regcache, 14, &sf.sf_r14);
  89.       regcache_raw_supply (regcache, 15, &sf.sf_r15);
  90.       regcache_raw_supply (regcache, AMD64_RBX_REGNUM, &sf.sf_rbx);
  91.       regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf.sf_rip);
  92.     }
  93.   else
  94.     {
  95.       /* No, the pcb must have been last updated by savectx().  */
  96.       pcb->pcb_rsp += 8;
  97.       regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf);
  98.     }

  99.   regcache_raw_supply (regcache, AMD64_RSP_REGNUM, &pcb->pcb_rsp);
  100.   regcache_raw_supply (regcache, AMD64_RBP_REGNUM, &pcb->pcb_rbp);

  101.   return 1;
  102. }


  103. /* Provide a prototype to silence -Wmissing-prototypes.  */
  104. void _initialize_amd64obsd_nat (void);

  105. void
  106. _initialize_amd64obsd_nat (void)
  107. {
  108.   amd64_native_gregset32_reg_offset = amd64obsd32_r_reg_offset;
  109.   amd64_native_gregset32_num_regs = ARRAY_SIZE (amd64obsd32_r_reg_offset);
  110.   amd64_native_gregset64_reg_offset = amd64obsd_r_reg_offset;

  111.   /* Add some extra features to the common *BSD/amd64 target.  */
  112.   obsd_add_target (amd64bsd_target ());

  113.   /* Support debugging kernel virtual memory images.  */
  114.   bsd_kvm_add_target (amd64obsd_supply_pcb);
  115. }