GDB (API)
/home/stan/gdb/src/gdb/bsd-kvm.c
Go to the documentation of this file.
00001 /* BSD Kernel Data Access Library (libkvm) interface.
00002 
00003    Copyright (C) 2004-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "cli/cli-cmds.h"
00022 #include "command.h"
00023 #include "frame.h"
00024 #include "regcache.h"
00025 #include "target.h"
00026 #include "value.h"
00027 #include "gdbcore.h"            /* for get_exec_file */
00028 #include "gdbthread.h"
00029 
00030 #include "gdb_assert.h"
00031 #include <fcntl.h>
00032 #include <kvm.h>
00033 #ifdef HAVE_NLIST_H
00034 #include <nlist.h>
00035 #endif
00036 #include <paths.h>
00037 #include "readline/readline.h"
00038 #include <sys/proc.h>
00039 #include <sys/user.h>
00040 
00041 #include "bsd-kvm.h"
00042 
00043 /* Kernel memory device file.  */
00044 static const char *bsd_kvm_corefile;
00045 
00046 /* Kernel memory interface descriptor.  */
00047 static kvm_t *core_kd;
00048 
00049 /* Address of process control block.  */
00050 static struct pcb *bsd_kvm_paddr;
00051 
00052 /* Pointer to architecture-specific function that reconstructs the
00053    register state from PCB and supplies it to REGCACHE.  */
00054 static int (*bsd_kvm_supply_pcb)(struct regcache *regcache, struct pcb *pcb);
00055 
00056 /* Target ops for libkvm interface.  */
00057 static struct target_ops bsd_kvm_ops;
00058 
00059 /* This is the ptid we use while we're connected to kvm.  The kvm
00060    target currently doesn't export any view of the running processes,
00061    so this represents the kernel task.  */
00062 static ptid_t bsd_kvm_ptid;
00063 
00064 static void
00065 bsd_kvm_open (char *filename, int from_tty)
00066 {
00067   char errbuf[_POSIX2_LINE_MAX];
00068   char *execfile = NULL;
00069   kvm_t *temp_kd;
00070 
00071   target_preopen (from_tty);
00072 
00073   if (filename)
00074     {
00075       char *temp;
00076 
00077       filename = tilde_expand (filename);
00078       if (filename[0] != '/')
00079         {
00080           temp = concat (current_directory, "/", filename, (char *)NULL);
00081           xfree (filename);
00082           filename = temp;
00083         }
00084     }
00085 
00086   execfile = get_exec_file (0);
00087   temp_kd = kvm_openfiles (execfile, filename, NULL,
00088                            write_files ? O_RDWR : O_RDONLY, errbuf);
00089   if (temp_kd == NULL)
00090     error (("%s"), errbuf);
00091 
00092   bsd_kvm_corefile = filename;
00093   unpush_target (&bsd_kvm_ops);
00094   core_kd = temp_kd;
00095   push_target (&bsd_kvm_ops);
00096 
00097   add_thread_silent (bsd_kvm_ptid);
00098   inferior_ptid = bsd_kvm_ptid;
00099 
00100   target_fetch_registers (get_current_regcache (), -1);
00101 
00102   reinit_frame_cache ();
00103   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
00104 }
00105 
00106 static void
00107 bsd_kvm_close (void)
00108 {
00109   if (core_kd)
00110     {
00111       if (kvm_close (core_kd) == -1)
00112         warning (("%s"), kvm_geterr(core_kd));
00113       core_kd = NULL;
00114     }
00115 
00116   inferior_ptid = null_ptid;
00117   delete_thread_silent (bsd_kvm_ptid);
00118 }
00119 
00120 static LONGEST
00121 bsd_kvm_xfer_memory (CORE_ADDR addr, ULONGEST len,
00122                      gdb_byte *readbuf, const gdb_byte *writebuf)
00123 {
00124   ssize_t nbytes = len;
00125 
00126   if (readbuf)
00127     nbytes = kvm_read (core_kd, addr, readbuf, nbytes);
00128   if (writebuf && nbytes > 0)
00129     nbytes = kvm_write (core_kd, addr, writebuf, nbytes);
00130   return nbytes;
00131 }
00132 
00133 static LONGEST
00134 bsd_kvm_xfer_partial (struct target_ops *ops, enum target_object object,
00135                       const char *annex, gdb_byte *readbuf,
00136                       const gdb_byte *writebuf,
00137                       ULONGEST offset, LONGEST len)
00138 {
00139   switch (object)
00140     {
00141     case TARGET_OBJECT_MEMORY:
00142       return bsd_kvm_xfer_memory (offset, len, readbuf, writebuf);
00143 
00144     default:
00145       return -1;
00146     }
00147 }
00148 
00149 static void
00150 bsd_kvm_files_info (struct target_ops *ops)
00151 {
00152   if (bsd_kvm_corefile && strcmp (bsd_kvm_corefile, _PATH_MEM) != 0)
00153     printf_filtered (_("\tUsing the kernel crash dump %s.\n"),
00154                      bsd_kvm_corefile);
00155   else
00156     printf_filtered (_("\tUsing the currently running kernel.\n"));
00157 }
00158 
00159 /* Fetch process control block at address PADDR.  */
00160 
00161 static int
00162 bsd_kvm_fetch_pcb (struct regcache *regcache, struct pcb *paddr)
00163 {
00164   struct pcb pcb;
00165 
00166   if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
00167     error (("%s"), kvm_geterr (core_kd));
00168 
00169   gdb_assert (bsd_kvm_supply_pcb);
00170   return bsd_kvm_supply_pcb (regcache, &pcb);
00171 }
00172 
00173 static void
00174 bsd_kvm_fetch_registers (struct target_ops *ops,
00175                          struct regcache *regcache, int regnum)
00176 {
00177   struct nlist nl[2];
00178 
00179   if (bsd_kvm_paddr)
00180     {
00181       bsd_kvm_fetch_pcb (regcache, bsd_kvm_paddr);
00182       return;
00183     }
00184 
00185   /* On dumping core, BSD kernels store the faulting context (PCB)
00186      in the variable "dumppcb".  */
00187   memset (nl, 0, sizeof nl);
00188   nl[0].n_name = "_dumppcb";
00189 
00190   if (kvm_nlist (core_kd, nl) == -1)
00191     error (("%s"), kvm_geterr (core_kd));
00192 
00193   if (nl[0].n_value != 0)
00194     {
00195       /* Found dumppcb.  If it contains a valid context, return
00196          immediately.  */
00197       if (bsd_kvm_fetch_pcb (regcache, (struct pcb *) nl[0].n_value))
00198         return;
00199     }
00200 
00201   /* Traditional BSD kernels have a process proc0 that should always
00202      be present.  The address of proc0's PCB is stored in the variable
00203      "proc0paddr".  */
00204 
00205   memset (nl, 0, sizeof nl);
00206   nl[0].n_name = "_proc0paddr";
00207 
00208   if (kvm_nlist (core_kd, nl) == -1)
00209     error (("%s"), kvm_geterr (core_kd));
00210 
00211   if (nl[0].n_value != 0)
00212     {
00213       struct pcb *paddr;
00214 
00215       /* Found proc0paddr.  */
00216       if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
00217         error (("%s"), kvm_geterr (core_kd));
00218 
00219       bsd_kvm_fetch_pcb (regcache, paddr);
00220       return;
00221     }
00222 
00223 #ifdef HAVE_STRUCT_THREAD_TD_PCB
00224   /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer
00225      lives in `struct proc' but in `struct thread'.  The `struct
00226      thread' for the initial thread for proc0 can be found in the
00227      variable "thread0".  */
00228 
00229   memset (nl, 0, sizeof nl);
00230   nl[0].n_name = "_thread0";
00231 
00232   if (kvm_nlist (core_kd, nl) == -1)
00233     error (("%s"), kvm_geterr (core_kd));
00234 
00235   if (nl[0].n_value != 0)
00236     {
00237       struct pcb *paddr;
00238 
00239       /* Found thread0.  */
00240       nl[0].n_value += offsetof (struct thread, td_pcb);
00241       if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
00242         error (("%s"), kvm_geterr (core_kd));
00243 
00244       bsd_kvm_fetch_pcb (regcache, paddr);
00245       return;
00246     }
00247 #endif
00248 
00249   /* i18n: PCB == "Process Control Block".  */
00250   error (_("Cannot find a valid PCB"));
00251 }
00252 
00253 
00254 /* Kernel memory interface commands.  */
00255 struct cmd_list_element *bsd_kvm_cmdlist;
00256 
00257 static void
00258 bsd_kvm_cmd (char *arg, int fromtty)
00259 {
00260   /* ??? Should this become an alias for "target kvm"?  */
00261 }
00262 
00263 #ifndef HAVE_STRUCT_THREAD_TD_PCB
00264 
00265 static void
00266 bsd_kvm_proc_cmd (char *arg, int fromtty)
00267 {
00268   CORE_ADDR addr;
00269 
00270   if (arg == NULL)
00271     error_no_arg (_("proc address"));
00272 
00273   if (core_kd == NULL)
00274     error (_("No kernel memory image."));
00275 
00276   addr = parse_and_eval_address (arg);
00277 #ifdef HAVE_STRUCT_LWP
00278   addr += offsetof (struct lwp, l_addr);
00279 #else
00280   addr += offsetof (struct proc, p_addr);
00281 #endif
00282 
00283   if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
00284     error (("%s"), kvm_geterr (core_kd));
00285 
00286   target_fetch_registers (get_current_regcache (), -1);
00287 
00288   reinit_frame_cache ();
00289   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
00290 }
00291 
00292 #endif
00293 
00294 static void
00295 bsd_kvm_pcb_cmd (char *arg, int fromtty)
00296 {
00297   if (arg == NULL)
00298     /* i18n: PCB == "Process Control Block".  */
00299     error_no_arg (_("pcb address"));
00300 
00301   if (core_kd == NULL)
00302     error (_("No kernel memory image."));
00303 
00304   bsd_kvm_paddr = (struct pcb *)(u_long) parse_and_eval_address (arg);
00305 
00306   target_fetch_registers (get_current_regcache (), -1);
00307 
00308   reinit_frame_cache ();
00309   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
00310 }
00311 
00312 static int
00313 bsd_kvm_thread_alive (struct target_ops *ops,
00314                       ptid_t ptid)
00315 {
00316   return 1;
00317 }
00318 
00319 static char *
00320 bsd_kvm_pid_to_str (struct target_ops *ops, ptid_t ptid)
00321 {
00322   static char buf[64];
00323   xsnprintf (buf, sizeof buf, "<kvm>");
00324   return buf;
00325 }
00326 
00327 static int
00328 bsd_kvm_return_one (struct target_ops *ops)
00329 {
00330   return 1;
00331 }
00332 
00333 /* Add the libkvm interface to the list of all possible targets and
00334    register CUPPLY_PCB as the architecture-specific process control
00335    block interpreter.  */
00336 
00337 void
00338 bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
00339 {
00340   gdb_assert (bsd_kvm_supply_pcb == NULL);
00341   bsd_kvm_supply_pcb = supply_pcb;
00342 
00343   bsd_kvm_ops.to_shortname = "kvm";
00344   bsd_kvm_ops.to_longname = _("Kernel memory interface");
00345   bsd_kvm_ops.to_doc = _("Use a kernel virtual memory image as a target.\n\
00346 Optionally specify the filename of a core dump.");
00347   bsd_kvm_ops.to_open = bsd_kvm_open;
00348   bsd_kvm_ops.to_close = bsd_kvm_close;
00349   bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers;
00350   bsd_kvm_ops.to_xfer_partial = bsd_kvm_xfer_partial;
00351   bsd_kvm_ops.to_files_info = bsd_kvm_files_info;
00352   bsd_kvm_ops.to_thread_alive = bsd_kvm_thread_alive;
00353   bsd_kvm_ops.to_pid_to_str = bsd_kvm_pid_to_str;
00354   bsd_kvm_ops.to_stratum = process_stratum;
00355   bsd_kvm_ops.to_has_memory = bsd_kvm_return_one;
00356   bsd_kvm_ops.to_has_stack = bsd_kvm_return_one;
00357   bsd_kvm_ops.to_has_registers = bsd_kvm_return_one;
00358   bsd_kvm_ops.to_magic = OPS_MAGIC;
00359 
00360   add_target (&bsd_kvm_ops);
00361   
00362   add_prefix_cmd ("kvm", class_obscure, bsd_kvm_cmd, _("\
00363 Generic command for manipulating the kernel memory interface."),
00364                   &bsd_kvm_cmdlist, "kvm ", 0, &cmdlist);
00365 
00366 #ifndef HAVE_STRUCT_THREAD_TD_PCB
00367   add_cmd ("proc", class_obscure, bsd_kvm_proc_cmd,
00368            _("Set current context from proc address"), &bsd_kvm_cmdlist);
00369 #endif
00370   add_cmd ("pcb", class_obscure, bsd_kvm_pcb_cmd,
00371            /* i18n: PCB == "Process Control Block".  */
00372            _("Set current context from pcb address"), &bsd_kvm_cmdlist);
00373 
00374   /* Some notes on the ptid usage on this target.
00375 
00376      The pid field represents the kvm inferior instance.  Currently,
00377      we don't support multiple kvm inferiors, but we start at 1
00378      anyway.  The lwp field is set to != 0, in case the core wants to
00379      refer to the whole kvm inferior with ptid(1,0,0).
00380 
00381      If kvm is made to export running processes as gdb threads,
00382      the following form can be used:
00383      ptid (1, 1, 0) -> kvm inferior 1, in kernel
00384      ptid (1, 1, 1) -> kvm inferior 1, process 1
00385      ptid (1, 1, 2) -> kvm inferior 1, process 2
00386      ptid (1, 1, n) -> kvm inferior 1, process n  */
00387 
00388   bsd_kvm_ptid = ptid_build (1, 1, 0);
00389 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines