gdb/bsd-kvm.c - gdb

Global variables defined

Functions defined

Source code

  1. /* BSD Kernel Data Access Library (libkvm) interface.

  2.    Copyright (C) 2004-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 "cli/cli-cmds.h"
  16. #include "command.h"
  17. #include "frame.h"
  18. #include "regcache.h"
  19. #include "target.h"
  20. #include "value.h"
  21. #include "gdbcore.h"                /* for get_exec_file */
  22. #include "gdbthread.h"

  23. #include <fcntl.h>
  24. #include <kvm.h>
  25. #ifdef HAVE_NLIST_H
  26. #include <nlist.h>
  27. #endif
  28. #include <paths.h>
  29. #include "readline/readline.h"
  30. #include <sys/param.h>
  31. #include <sys/proc.h>
  32. #include <sys/user.h>

  33. #include "bsd-kvm.h"

  34. /* Kernel memory device file.  */
  35. static const char *bsd_kvm_corefile;

  36. /* Kernel memory interface descriptor.  */
  37. static kvm_t *core_kd;

  38. /* Address of process control block.  */
  39. static struct pcb *bsd_kvm_paddr;

  40. /* Pointer to architecture-specific function that reconstructs the
  41.    register state from PCB and supplies it to REGCACHE.  */
  42. static int (*bsd_kvm_supply_pcb)(struct regcache *regcache, struct pcb *pcb);

  43. /* Target ops for libkvm interface.  */
  44. static struct target_ops bsd_kvm_ops;

  45. /* This is the ptid we use while we're connected to kvm.  The kvm
  46.    target currently doesn't export any view of the running processes,
  47.    so this represents the kernel task.  */
  48. static ptid_t bsd_kvm_ptid;

  49. static void
  50. bsd_kvm_open (const char *arg, int from_tty)
  51. {
  52.   char errbuf[_POSIX2_LINE_MAX];
  53.   char *execfile = NULL;
  54.   kvm_t *temp_kd;
  55.   char *filename = NULL;

  56.   target_preopen (from_tty);

  57.   if (arg)
  58.     {
  59.       char *temp;

  60.       filename = tilde_expand (arg);
  61.       if (filename[0] != '/')
  62.         {
  63.           temp = concat (current_directory, "/", filename, (char *)NULL);
  64.           xfree (filename);
  65.           filename = temp;
  66.         }
  67.     }

  68.   execfile = get_exec_file (0);
  69.   temp_kd = kvm_openfiles (execfile, filename, NULL,
  70.                            write_files ? O_RDWR : O_RDONLY, errbuf);
  71.   if (temp_kd == NULL)
  72.     error (("%s"), errbuf);

  73.   bsd_kvm_corefile = filename;
  74.   unpush_target (&bsd_kvm_ops);
  75.   core_kd = temp_kd;
  76.   push_target (&bsd_kvm_ops);

  77.   add_thread_silent (bsd_kvm_ptid);
  78.   inferior_ptid = bsd_kvm_ptid;

  79.   target_fetch_registers (get_current_regcache (), -1);

  80.   reinit_frame_cache ();
  81.   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
  82. }

  83. static void
  84. bsd_kvm_close (struct target_ops *self)
  85. {
  86.   if (core_kd)
  87.     {
  88.       if (kvm_close (core_kd) == -1)
  89.         warning (("%s"), kvm_geterr(core_kd));
  90.       core_kd = NULL;
  91.     }

  92.   inferior_ptid = null_ptid;
  93.   delete_thread_silent (bsd_kvm_ptid);
  94. }

  95. static LONGEST
  96. bsd_kvm_xfer_memory (CORE_ADDR addr, ULONGEST len,
  97.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  98. {
  99.   ssize_t nbytes = len;

  100.   if (readbuf)
  101.     nbytes = kvm_read (core_kd, addr, readbuf, nbytes);
  102.   if (writebuf && nbytes > 0)
  103.     nbytes = kvm_write (core_kd, addr, writebuf, nbytes);
  104.   return nbytes;
  105. }

  106. static enum target_xfer_status
  107. bsd_kvm_xfer_partial (struct target_ops *ops, enum target_object object,
  108.                       const char *annex, gdb_byte *readbuf,
  109.                       const gdb_byte *writebuf,
  110.                       ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  111. {
  112.   switch (object)
  113.     {
  114.     case TARGET_OBJECT_MEMORY:
  115.       {
  116.         LONGEST ret = bsd_kvm_xfer_memory (offset, len, readbuf, writebuf);

  117.         if (ret < 0)
  118.           return TARGET_XFER_E_IO;
  119.         else if (ret == 0)
  120.           return TARGET_XFER_EOF;
  121.         else
  122.           {
  123.             *xfered_len = (ULONGEST) ret;
  124.             return TARGET_XFER_OK;
  125.           }
  126.       }

  127.     default:
  128.       return TARGET_XFER_E_IO;
  129.     }
  130. }

  131. static void
  132. bsd_kvm_files_info (struct target_ops *ops)
  133. {
  134.   if (bsd_kvm_corefile && strcmp (bsd_kvm_corefile, _PATH_MEM) != 0)
  135.     printf_filtered (_("\tUsing the kernel crash dump %s.\n"),
  136.                      bsd_kvm_corefile);
  137.   else
  138.     printf_filtered (_("\tUsing the currently running kernel.\n"));
  139. }

  140. /* Fetch process control block at address PADDR.  */

  141. static int
  142. bsd_kvm_fetch_pcb (struct regcache *regcache, struct pcb *paddr)
  143. {
  144.   struct pcb pcb;

  145.   if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
  146.     error (("%s"), kvm_geterr (core_kd));

  147.   gdb_assert (bsd_kvm_supply_pcb);
  148.   return bsd_kvm_supply_pcb (regcache, &pcb);
  149. }

  150. static void
  151. bsd_kvm_fetch_registers (struct target_ops *ops,
  152.                          struct regcache *regcache, int regnum)
  153. {
  154.   struct nlist nl[2];

  155.   if (bsd_kvm_paddr)
  156.     {
  157.       bsd_kvm_fetch_pcb (regcache, bsd_kvm_paddr);
  158.       return;
  159.     }

  160.   /* On dumping core, BSD kernels store the faulting context (PCB)
  161.      in the variable "dumppcb".  */
  162.   memset (nl, 0, sizeof nl);
  163.   nl[0].n_name = "_dumppcb";

  164.   if (kvm_nlist (core_kd, nl) == -1)
  165.     error (("%s"), kvm_geterr (core_kd));

  166.   if (nl[0].n_value != 0)
  167.     {
  168.       /* Found dumppcb.  If it contains a valid context, return
  169.          immediately.  */
  170.       if (bsd_kvm_fetch_pcb (regcache, (struct pcb *) nl[0].n_value))
  171.         return;
  172.     }

  173.   /* Traditional BSD kernels have a process proc0 that should always
  174.      be present.  The address of proc0's PCB is stored in the variable
  175.      "proc0paddr".  */

  176.   memset (nl, 0, sizeof nl);
  177.   nl[0].n_name = "_proc0paddr";

  178.   if (kvm_nlist (core_kd, nl) == -1)
  179.     error (("%s"), kvm_geterr (core_kd));

  180.   if (nl[0].n_value != 0)
  181.     {
  182.       struct pcb *paddr;

  183.       /* Found proc0paddr.  */
  184.       if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
  185.         error (("%s"), kvm_geterr (core_kd));

  186.       bsd_kvm_fetch_pcb (regcache, paddr);
  187.       return;
  188.     }

  189. #ifdef HAVE_STRUCT_THREAD_TD_PCB
  190.   /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer
  191.      lives in `struct proc' but in `struct thread'.  The `struct
  192.      thread' for the initial thread for proc0 can be found in the
  193.      variable "thread0".  */

  194.   memset (nl, 0, sizeof nl);
  195.   nl[0].n_name = "_thread0";

  196.   if (kvm_nlist (core_kd, nl) == -1)
  197.     error (("%s"), kvm_geterr (core_kd));

  198.   if (nl[0].n_value != 0)
  199.     {
  200.       struct pcb *paddr;

  201.       /* Found thread0.  */
  202.       nl[0].n_value += offsetof (struct thread, td_pcb);
  203.       if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
  204.         error (("%s"), kvm_geterr (core_kd));

  205.       bsd_kvm_fetch_pcb (regcache, paddr);
  206.       return;
  207.     }
  208. #endif

  209.   /* i18n: PCB == "Process Control Block".  */
  210.   error (_("Cannot find a valid PCB"));
  211. }


  212. /* Kernel memory interface commands.  */
  213. struct cmd_list_element *bsd_kvm_cmdlist;

  214. static void
  215. bsd_kvm_cmd (char *arg, int fromtty)
  216. {
  217.   /* ??? Should this become an alias for "target kvm"?  */
  218. }

  219. #ifndef HAVE_STRUCT_THREAD_TD_PCB

  220. static void
  221. bsd_kvm_proc_cmd (char *arg, int fromtty)
  222. {
  223.   CORE_ADDR addr;

  224.   if (arg == NULL)
  225.     error_no_arg (_("proc address"));

  226.   if (core_kd == NULL)
  227.     error (_("No kernel memory image."));

  228.   addr = parse_and_eval_address (arg);
  229. #ifdef HAVE_STRUCT_LWP
  230.   addr += offsetof (struct lwp, l_addr);
  231. #else
  232.   addr += offsetof (struct proc, p_addr);
  233. #endif

  234.   if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
  235.     error (("%s"), kvm_geterr (core_kd));

  236.   target_fetch_registers (get_current_regcache (), -1);

  237.   reinit_frame_cache ();
  238.   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
  239. }

  240. #endif

  241. static void
  242. bsd_kvm_pcb_cmd (char *arg, int fromtty)
  243. {
  244.   if (arg == NULL)
  245.     /* i18n: PCB == "Process Control Block".  */
  246.     error_no_arg (_("pcb address"));

  247.   if (core_kd == NULL)
  248.     error (_("No kernel memory image."));

  249.   bsd_kvm_paddr = (struct pcb *)(u_long) parse_and_eval_address (arg);

  250.   target_fetch_registers (get_current_regcache (), -1);

  251.   reinit_frame_cache ();
  252.   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
  253. }

  254. static int
  255. bsd_kvm_thread_alive (struct target_ops *ops,
  256.                       ptid_t ptid)
  257. {
  258.   return 1;
  259. }

  260. static char *
  261. bsd_kvm_pid_to_str (struct target_ops *ops, ptid_t ptid)
  262. {
  263.   static char buf[64];
  264.   xsnprintf (buf, sizeof buf, "<kvm>");
  265.   return buf;
  266. }

  267. static int
  268. bsd_kvm_return_one (struct target_ops *ops)
  269. {
  270.   return 1;
  271. }

  272. /* Add the libkvm interface to the list of all possible targets and
  273.    register CUPPLY_PCB as the architecture-specific process control
  274.    block interpreter.  */

  275. void
  276. bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
  277. {
  278.   gdb_assert (bsd_kvm_supply_pcb == NULL);
  279.   bsd_kvm_supply_pcb = supply_pcb;

  280.   bsd_kvm_ops.to_shortname = "kvm";
  281.   bsd_kvm_ops.to_longname = _("Kernel memory interface");
  282.   bsd_kvm_ops.to_doc = _("Use a kernel virtual memory image as a target.\n\
  283. Optionally specify the filename of a core dump.");
  284.   bsd_kvm_ops.to_open = bsd_kvm_open;
  285.   bsd_kvm_ops.to_close = bsd_kvm_close;
  286.   bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers;
  287.   bsd_kvm_ops.to_xfer_partial = bsd_kvm_xfer_partial;
  288.   bsd_kvm_ops.to_files_info = bsd_kvm_files_info;
  289.   bsd_kvm_ops.to_thread_alive = bsd_kvm_thread_alive;
  290.   bsd_kvm_ops.to_pid_to_str = bsd_kvm_pid_to_str;
  291.   bsd_kvm_ops.to_stratum = process_stratum;
  292.   bsd_kvm_ops.to_has_memory = bsd_kvm_return_one;
  293.   bsd_kvm_ops.to_has_stack = bsd_kvm_return_one;
  294.   bsd_kvm_ops.to_has_registers = bsd_kvm_return_one;
  295.   bsd_kvm_ops.to_magic = OPS_MAGIC;

  296.   add_target (&bsd_kvm_ops);

  297.   add_prefix_cmd ("kvm", class_obscure, bsd_kvm_cmd, _("\
  298. Generic command for manipulating the kernel memory interface."),
  299.                   &bsd_kvm_cmdlist, "kvm ", 0, &cmdlist);

  300. #ifndef HAVE_STRUCT_THREAD_TD_PCB
  301.   add_cmd ("proc", class_obscure, bsd_kvm_proc_cmd,
  302.            _("Set current context from proc address"), &bsd_kvm_cmdlist);
  303. #endif
  304.   add_cmd ("pcb", class_obscure, bsd_kvm_pcb_cmd,
  305.            /* i18n: PCB == "Process Control Block".  */
  306.            _("Set current context from pcb address"), &bsd_kvm_cmdlist);

  307.   /* Some notes on the ptid usage on this target.

  308.      The pid field represents the kvm inferior instance.  Currently,
  309.      we don't support multiple kvm inferiors, but we start at 1
  310.      anyway.  The lwp field is set to != 0, in case the core wants to
  311.      refer to the whole kvm inferior with ptid(1,0,0).

  312.      If kvm is made to export running processes as gdb threads,
  313.      the following form can be used:
  314.      ptid (1, 1, 0) -> kvm inferior 1, in kernel
  315.      ptid (1, 1, 1) -> kvm inferior 1, process 1
  316.      ptid (1, 1, 2) -> kvm inferior 1, process 2
  317.      ptid (1, 1, n) -> kvm inferior 1, process n  */

  318.   bsd_kvm_ptid = ptid_build (1, 1, 0);
  319. }