gdb/amd64fbsd-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

  18. #include <signal.h>
  19. #include <sys/types.h>
  20. #include <sys/ptrace.h>
  21. #include <sys/sysctl.h>
  22. #include <machine/reg.h>

  23. #include "fbsd-nat.h"
  24. #include "amd64-tdep.h"
  25. #include "amd64-nat.h"
  26. #include "amd64bsd-nat.h"
  27. #include "x86-nat.h"


  28. /* Offset in `struct reg' where MEMBER is stored.  */
  29. #define REG_OFFSET(member) offsetof (struct reg, member)

  30. /* At amd64fbsd64_r_reg_offset[REGNUM] you'll find the offset in
  31.    `struct reg' location where the GDB register REGNUM is stored.
  32.    Unsupported registers are marked with `-1'.  */
  33. static int amd64fbsd64_r_reg_offset[] =
  34. {
  35.   REG_OFFSET (r_rax),
  36.   REG_OFFSET (r_rbx),
  37.   REG_OFFSET (r_rcx),
  38.   REG_OFFSET (r_rdx),
  39.   REG_OFFSET (r_rsi),
  40.   REG_OFFSET (r_rdi),
  41.   REG_OFFSET (r_rbp),
  42.   REG_OFFSET (r_rsp),
  43.   REG_OFFSET (r_r8),
  44.   REG_OFFSET (r_r9),
  45.   REG_OFFSET (r_r10),
  46.   REG_OFFSET (r_r11),
  47.   REG_OFFSET (r_r12),
  48.   REG_OFFSET (r_r13),
  49.   REG_OFFSET (r_r14),
  50.   REG_OFFSET (r_r15),
  51.   REG_OFFSET (r_rip),
  52.   REG_OFFSET (r_rflags),
  53.   REG_OFFSET (r_cs),
  54.   REG_OFFSET (r_ss),
  55.   -1,
  56.   -1,
  57.   -1,
  58.   -1
  59. };


  60. /* Mapping between the general-purpose registers in FreeBSD/amd64
  61.    `struct reg' format and GDB's register cache layout for
  62.    FreeBSD/i386.

  63.    Note that most FreeBSD/amd64 registers are 64-bit, while the
  64.    FreeBSD/i386 registers are all 32-bit, but since we're
  65.    little-endian we get away with that.  */

  66. /* From <machine/reg.h>.  */
  67. static int amd64fbsd32_r_reg_offset[I386_NUM_GREGS] =
  68. {
  69.   14 * 8, 13 * 8,                /* %eax, %ecx */
  70.   12 * 8, 11 * 8,                /* %edx, %ebx */
  71.   20 * 8, 10 * 8,                /* %esp, %ebp */
  72.   9 * 8, 8 * 8,                        /* %esi, %edi */
  73.   17 * 8, 19 * 8,                /* %eip, %eflags */
  74.   18 * 8, 21 * 8,                /* %cs, %ss */
  75.   -1, -1, -1, -1                /* %ds, %es, %fs, %gs */
  76. };


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

  78. #include <machine/pcb.h>
  79. #include <osreldate.h>

  80. #include "bsd-kvm.h"

  81. static int
  82. amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  83. {
  84.   /* The following is true for FreeBSD 5.2:

  85.      The pcb contains %rip, %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15,
  86.      %ds, %es, %fs and %gs.  This accounts for all callee-saved
  87.      registers specified by the psABI and then some.  Here %esp
  88.      contains the stack pointer at the point just after the call to
  89.      cpu_switch().  From this information we reconstruct the register
  90.      state as it would like when we just returned from cpu_switch().  */

  91.   /* The stack pointer shouldn't be zero.  */
  92.   if (pcb->pcb_rsp == 0)
  93.     return 0;

  94.   pcb->pcb_rsp += 8;
  95.   regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &pcb->pcb_rip);
  96.   regcache_raw_supply (regcache, AMD64_RBX_REGNUM, &pcb->pcb_rbx);
  97.   regcache_raw_supply (regcache, AMD64_RSP_REGNUM, &pcb->pcb_rsp);
  98.   regcache_raw_supply (regcache, AMD64_RBP_REGNUM, &pcb->pcb_rbp);
  99.   regcache_raw_supply (regcache, 12, &pcb->pcb_r12);
  100.   regcache_raw_supply (regcache, 13, &pcb->pcb_r13);
  101.   regcache_raw_supply (regcache, 14, &pcb->pcb_r14);
  102.   regcache_raw_supply (regcache, 15, &pcb->pcb_r15);
  103. #if (__FreeBSD_version < 800075) && (__FreeBSD_kernel_version < 800075)
  104.   /* struct pcb provides the pcb_ds/pcb_es/pcb_fs/pcb_gs fields only
  105.      up until __FreeBSD_version 800074: The removal of these fields
  106.      occurred on 2009-04-01 while the __FreeBSD_version number was
  107.      bumped to 800075 on 2009-04-06.  So 800075 is the closest version
  108.      number where we should not try to access these fields.  */
  109.   regcache_raw_supply (regcache, AMD64_DS_REGNUM, &pcb->pcb_ds);
  110.   regcache_raw_supply (regcache, AMD64_ES_REGNUM, &pcb->pcb_es);
  111.   regcache_raw_supply (regcache, AMD64_FS_REGNUM, &pcb->pcb_fs);
  112.   regcache_raw_supply (regcache, AMD64_GS_REGNUM, &pcb->pcb_gs);
  113. #endif

  114.   return 1;
  115. }


  116. static void (*super_mourn_inferior) (struct target_ops *ops);

  117. static void
  118. amd64fbsd_mourn_inferior (struct target_ops *ops)
  119. {
  120. #ifdef HAVE_PT_GETDBREGS
  121.   x86_cleanup_dregs ();
  122. #endif
  123.   super_mourn_inferior (ops);
  124. }

  125. /* Provide a prototype to silence -Wmissing-prototypes.  */
  126. void _initialize_amd64fbsd_nat (void);

  127. void
  128. _initialize_amd64fbsd_nat (void)
  129. {
  130.   struct target_ops *t;
  131.   int offset;

  132.   amd64_native_gregset32_reg_offset = amd64fbsd32_r_reg_offset;
  133.   amd64_native_gregset64_reg_offset = amd64fbsd64_r_reg_offset;

  134.   /* Add some extra features to the common *BSD/i386 target.  */
  135.   t = amd64bsd_target ();

  136. #ifdef HAVE_PT_GETDBREGS

  137.   x86_use_watchpoints (t);

  138.   x86_dr_low.set_control = amd64bsd_dr_set_control;
  139.   x86_dr_low.set_addr = amd64bsd_dr_set_addr;
  140.   x86_dr_low.get_addr = amd64bsd_dr_get_addr;
  141.   x86_dr_low.get_status = amd64bsd_dr_get_status;
  142.   x86_dr_low.get_control = amd64bsd_dr_get_control;
  143.   x86_set_debug_register_length (8);

  144. #endif /* HAVE_PT_GETDBREGS */

  145.   super_mourn_inferior = t->to_mourn_inferior;
  146.   t->to_mourn_inferior = amd64fbsd_mourn_inferior;

  147.   t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
  148.   t->to_find_memory_regions = fbsd_find_memory_regions;
  149.   add_target (t);

  150.   /* Support debugging kernel virtual memory images.  */
  151.   bsd_kvm_add_target (amd64fbsd_supply_pcb);

  152.   /* To support the recognition of signal handlers, i386bsd-tdep.c
  153.      hardcodes some constants.  Inclusion of this file means that we
  154.      are compiling a native debugger, which means that we can use the
  155.      system header files and sysctl(3) to get at the relevant
  156.      information.  */

  157. #define SC_REG_OFFSET amd64fbsd_sc_reg_offset

  158.   /* We only check the program counter, stack pointer and frame
  159.      pointer since these members of `struct sigcontext' are essential
  160.      for providing backtraces.  */

  161. #define SC_RIP_OFFSET SC_REG_OFFSET[AMD64_RIP_REGNUM]
  162. #define SC_RSP_OFFSET SC_REG_OFFSET[AMD64_RSP_REGNUM]
  163. #define SC_RBP_OFFSET SC_REG_OFFSET[AMD64_RBP_REGNUM]

  164.   /* Override the default value for the offset of the program counter
  165.      in the sigcontext structure.  */
  166.   offset = offsetof (struct sigcontext, sc_rip);

  167.   if (SC_RIP_OFFSET != offset)
  168.     {
  169.       warning (_("\
  170. offsetof (struct sigcontext, sc_rip) yields %d instead of %d.\n\
  171. Please report this to <bug-gdb@gnu.org>."),
  172.                offset, SC_RIP_OFFSET);
  173.     }

  174.   SC_RIP_OFFSET = offset;

  175.   /* Likewise for the stack pointer.  */
  176.   offset = offsetof (struct sigcontext, sc_rsp);

  177.   if (SC_RSP_OFFSET != offset)
  178.     {
  179.       warning (_("\
  180. offsetof (struct sigcontext, sc_rsp) yields %d instead of %d.\n\
  181. Please report this to <bug-gdb@gnu.org>."),
  182.                offset, SC_RSP_OFFSET);
  183.     }

  184.   SC_RSP_OFFSET = offset;

  185.   /* And the frame pointer.  */
  186.   offset = offsetof (struct sigcontext, sc_rbp);

  187.   if (SC_RBP_OFFSET != offset)
  188.     {
  189.       warning (_("\
  190. offsetof (struct sigcontext, sc_rbp) yields %d instead of %d.\n\
  191. Please report this to <bug-gdb@gnu.org>."),
  192.                offset, SC_RBP_OFFSET);
  193.     }

  194.   SC_RBP_OFFSET = offset;

  195.   /* FreeBSD provides a kern.ps_strings sysctl that we can use to
  196.      locate the sigtramp.  That way we can still recognize a sigtramp
  197.      if its location is changed in a new kernel.  Of course this is
  198.      still based on the assumption that the sigtramp is placed
  199.      directly under the location where the program arguments and
  200.      environment can be found.  */
  201.   {
  202.     int mib[2];
  203.     long ps_strings;
  204.     size_t len;

  205.     mib[0] = CTL_KERN;
  206.     mib[1] = KERN_PS_STRINGS;
  207.     len = sizeof (ps_strings);
  208.     if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0)
  209.       {
  210.         amd64fbsd_sigtramp_start_addr = ps_strings - 32;
  211.         amd64fbsd_sigtramp_end_addr = ps_strings;
  212.       }
  213.   }
  214. }