gdb/amd64-nat.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Native-dependent code for 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 "gdbarch.h"
  16. #include "regcache.h"

  17. #include "i386-tdep.h"
  18. #include "amd64-tdep.h"
  19. #include "amd64-nat.h"

  20. /* The following bits of code help with implementing debugging 32-bit
  21.    code natively on AMD64.  The idea is to define two mappings between
  22.    the register number as used by GDB and the register set used by the
  23.    host to represent the general-purpose registers; one for 32-bit
  24.    code and one for 64-bit code.  The mappings are specified by the
  25.    follwing variables and consist of an array of offsets within the
  26.    register set indexed by register number, and the number of
  27.    registers supported by the mapping.  We don't need mappings for the
  28.    floating-point and SSE registers, since the difference between
  29.    64-bit and 32-bit variants are negligable.  The difference in the
  30.    number of SSE registers is already handled by the target code.  */

  31. /* General-purpose register mapping for native 32-bit code.  */
  32. int *amd64_native_gregset32_reg_offset;
  33. int amd64_native_gregset32_num_regs = I386_NUM_GREGS;

  34. /* General-purpose register mapping for native 64-bit code.  */
  35. int *amd64_native_gregset64_reg_offset;
  36. int amd64_native_gregset64_num_regs = AMD64_NUM_GREGS;

  37. /* Return the offset of REGNUM within the appropriate native
  38.    general-purpose register set.  */

  39. static int
  40. amd64_native_gregset_reg_offset (struct gdbarch *gdbarch, int regnum)
  41. {
  42.   int *reg_offset = amd64_native_gregset64_reg_offset;
  43.   int num_regs = amd64_native_gregset64_num_regs;

  44.   gdb_assert (regnum >= 0);

  45.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  46.     {
  47.       reg_offset = amd64_native_gregset32_reg_offset;
  48.       num_regs = amd64_native_gregset32_num_regs;
  49.     }

  50.   if (num_regs > gdbarch_num_regs (gdbarch))
  51.     num_regs = gdbarch_num_regs (gdbarch);

  52.   if (regnum < num_regs && regnum < gdbarch_num_regs (gdbarch))
  53.     return reg_offset[regnum];

  54.   return -1;
  55. }

  56. /* Return whether the native general-purpose register set supplies
  57.    register REGNUM.  */

  58. int
  59. amd64_native_gregset_supplies_p (struct gdbarch *gdbarch, int regnum)
  60. {
  61.   return (amd64_native_gregset_reg_offset (gdbarch, regnum) != -1);
  62. }


  63. /* Supply register REGNUM, whose contents are stored in GREGS, to
  64.    REGCACHE.  If REGNUM is -1, supply all appropriate registers.  */

  65. void
  66. amd64_supply_native_gregset (struct regcache *regcache,
  67.                              const void *gregs, int regnum)
  68. {
  69.   const char *regs = gregs;
  70.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  71.   int num_regs = amd64_native_gregset64_num_regs;
  72.   int i;

  73.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  74.     num_regs = amd64_native_gregset32_num_regs;

  75.   if (num_regs > gdbarch_num_regs (gdbarch))
  76.     num_regs = gdbarch_num_regs (gdbarch);

  77.   for (i = 0; i < num_regs; i++)
  78.     {
  79.       if (regnum == -1 || regnum == i)
  80.         {
  81.           int offset = amd64_native_gregset_reg_offset (gdbarch, i);

  82.           if (offset != -1)
  83.             regcache_raw_supply (regcache, i, regs + offset);
  84.         }
  85.     }
  86. }

  87. /* Collect register REGNUM from REGCACHE and store its contents in
  88.    GREGS.  If REGNUM is -1, collect and store all appropriate
  89.    registers.  */

  90. void
  91. amd64_collect_native_gregset (const struct regcache *regcache,
  92.                               void *gregs, int regnum)
  93. {
  94.   char *regs = gregs;
  95.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  96.   int num_regs = amd64_native_gregset64_num_regs;
  97.   int i;

  98.   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
  99.     {
  100.       num_regs = amd64_native_gregset32_num_regs;

  101.       /* Make sure %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp, %esp and
  102.          %eip get zero-extended to 64 bits.  */
  103.       for (i = 0; i <= I386_EIP_REGNUM; i++)
  104.         {
  105.           if (regnum == -1 || regnum == i)
  106.             memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
  107.         }
  108.       /* Ditto for %cs, %ss, %ds, %es, %fs, and %gs.  */
  109.       for (i = I386_CS_REGNUM; i <= I386_GS_REGNUM; i++)
  110.         {
  111.           if (regnum == -1 || regnum == i)
  112.             memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
  113.         }
  114.     }

  115.   if (num_regs > gdbarch_num_regs (gdbarch))
  116.     num_regs = gdbarch_num_regs (gdbarch);

  117.   for (i = 0; i < num_regs; i++)
  118.     {
  119.       if (regnum == -1 || regnum == i)
  120.         {
  121.           int offset = amd64_native_gregset_reg_offset (gdbarch, i);

  122.           if (offset != -1)
  123.             regcache_raw_collect (regcache, i, regs + offset);
  124.         }
  125.     }
  126. }