gdb/i386fbsd-nat.c - gdb

Functions defined

Source code

  1. /* Native-dependent code for FreeBSD/i386.

  2.    Copyright (C) 2001-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 <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <sys/sysctl.h>

  21. #include "fbsd-nat.h"
  22. #include "i386-tdep.h"
  23. #include "x86-nat.h"
  24. #include "i386bsd-nat.h"

  25. /* Resume execution of the inferior process.  If STEP is nonzero,
  26.    single-step it.  If SIGNAL is nonzero, give it that signal.  */

  27. static void
  28. i386fbsd_resume (struct target_ops *ops,
  29.                  ptid_t ptid, int step, enum gdb_signal signal)
  30. {
  31.   pid_t pid = ptid_get_pid (ptid);
  32.   int request = PT_STEP;

  33.   if (pid == -1)
  34.     /* Resume all threads.  This only gets used in the non-threaded
  35.        case, where "resume all threads" and "resume inferior_ptid" are
  36.        the same.  */
  37.     pid = ptid_get_pid (inferior_ptid);

  38.   if (!step)
  39.     {
  40.       struct regcache *regcache = get_current_regcache ();
  41.       ULONGEST eflags;

  42.       /* Workaround for a bug in FreeBSD.  Make sure that the trace
  43.           flag is off when doing a continue.  There is a code path
  44.           through the kernel which leaves the flag set when it should
  45.           have been cleared.  If a process has a signal pending (such
  46.           as SIGALRM) and we do a PT_STEP, the process never really has
  47.           a chance to run because the kernel needs to notify the
  48.           debugger that a signal is being sent.  Therefore, the process
  49.           never goes through the kernel's trap() function which would
  50.           normally clear it.  */

  51.       regcache_cooked_read_unsigned (regcache, I386_EFLAGS_REGNUM,
  52.                                      &eflags);
  53.       if (eflags & 0x0100)
  54.         regcache_cooked_write_unsigned (regcache, I386_EFLAGS_REGNUM,
  55.                                         eflags & ~0x0100);

  56.       request = PT_CONTINUE;
  57.     }

  58.   /* An addres of (caddr_t) 1 tells ptrace to continue from where it
  59.      was.  (If GDB wanted it to start some other way, we have already
  60.      written a new PC value to the child.)  */
  61.   if (ptrace (request, pid, (caddr_t) 1,
  62.               gdb_signal_to_host (signal)) == -1)
  63.     perror_with_name (("ptrace"));
  64. }


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

  66. #include <machine/pcb.h>

  67. #include "bsd-kvm.h"

  68. static int
  69. i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  70. {
  71.   /* The following is true for FreeBSD 4.7:

  72.      The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
  73.      This accounts for all callee-saved registers specified by the
  74.      psABI and then some.  Here %esp contains the stack pointer at the
  75.      point just after the call to cpu_switch().  From this information
  76.      we reconstruct the register state as it would look when we just
  77.      returned from cpu_switch().  */

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

  81.   pcb->pcb_esp += 4;
  82.   regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi);
  83.   regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi);
  84.   regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
  85.   regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
  86.   regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx);
  87.   regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip);
  88.   regcache_raw_supply (regcache, I386_GS_REGNUM, &pcb->pcb_gs);

  89.   return 1;
  90. }


  91. /* Prevent warning from -Wmissing-prototypes.  */
  92. void _initialize_i386fbsd_nat (void);

  93. void
  94. _initialize_i386fbsd_nat (void)
  95. {
  96.   struct target_ops *t;

  97.   /* Add some extra features to the common *BSD/i386 target.  */
  98.   t = i386bsd_target ();

  99. #ifdef HAVE_PT_GETDBREGS

  100.   x86_use_watchpoints (t);

  101.   x86_dr_low.set_control = i386bsd_dr_set_control;
  102.   x86_dr_low.set_addr = i386bsd_dr_set_addr;
  103.   x86_dr_low.get_addr = i386bsd_dr_get_addr;
  104.   x86_dr_low.get_status = i386bsd_dr_get_status;
  105.   x86_dr_low.get_control = i386bsd_dr_get_control;
  106.   x86_set_debug_register_length (4);

  107. #endif /* HAVE_PT_GETDBREGS */


  108.   t->to_resume = i386fbsd_resume;
  109.   t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
  110.   t->to_find_memory_regions = fbsd_find_memory_regions;
  111.   add_target (t);

  112.   /* Support debugging kernel virtual memory images.  */
  113.   bsd_kvm_add_target (i386fbsd_supply_pcb);

  114.   /* FreeBSD provides a kern.ps_strings sysctl that we can use to
  115.      locate the sigtramp.  That way we can still recognize a sigtramp
  116.      if its location is changed in a new kernel.  Of course this is
  117.      still based on the assumption that the sigtramp is placed
  118.      directly under the location where the program arguments and
  119.      environment can be found.  */
  120. #ifdef KERN_PS_STRINGS
  121.   {
  122.     int mib[2];
  123.     u_long ps_strings;
  124.     size_t len;

  125.     mib[0] = CTL_KERN;
  126.     mib[1] = KERN_PS_STRINGS;
  127.     len = sizeof (ps_strings);
  128.     if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0)
  129.       {
  130.         i386fbsd_sigtramp_start_addr = ps_strings - 128;
  131.         i386fbsd_sigtramp_end_addr = ps_strings;
  132.       }
  133.   }
  134. #endif
  135. }