1/* BSD Kernel Data Access Library (libkvm) interface.
2
3   Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010, 2011
4   Free Software Foundation, Inc.
5
6   This file is part of GDB.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21#include "defs.h"
22#include "cli/cli-cmds.h"
23#include "command.h"
24#include "frame.h"
25#include "regcache.h"
26#include "target.h"
27#include "value.h"
28#include "gdbcore.h"		/* for get_exec_file */
29#include "gdbthread.h"
30
31#include "gdb_assert.h"
32#include <fcntl.h>
33#include <kvm.h>
34#ifdef HAVE_NLIST_H
35#include <nlist.h>
36#endif
37#include <paths.h>
38#include "readline/readline.h"
39#include <sys/param.h>
40#define _KMEMUSER
41#include <sys/proc.h>
42#include <sys/user.h>
43
44#include "bsd-kvm.h"
45
46/* Kernel memory device file.  */
47static const char *bsd_kvm_corefile;
48
49/* Kernel memory interface descriptor.  */
50static kvm_t *core_kd;
51
52/* Address of process control block.  */
53static struct pcb *bsd_kvm_paddr;
54
55/* Pointer to architecture-specific function that reconstructs the
56   register state from PCB and supplies it to REGCACHE.  */
57static int (*bsd_kvm_supply_pcb)(struct regcache *regcache, struct pcb *pcb);
58
59/* Target ops for libkvm interface.  */
60static struct target_ops bsd_kvm_ops;
61
62/* This is the ptid we use while we're connected to kvm.  The kvm
63   target currently doesn't export any view of the running processes,
64   so this represents the kernel task.  */
65static ptid_t bsd_kvm_ptid;
66
67static void
68bsd_kvm_open (char *filename, int from_tty)
69{
70  char errbuf[_POSIX2_LINE_MAX];
71  char *execfile = NULL;
72  kvm_t *temp_kd;
73  struct inferior *inf;
74
75  target_preopen (from_tty);
76
77  if (filename)
78    {
79      char *temp;
80
81      filename = tilde_expand (filename);
82      if (filename[0] != '/')
83	{
84	  temp = concat (current_directory, "/", filename, (char *)NULL);
85	  xfree (filename);
86	  filename = temp;
87	}
88    }
89
90  execfile = get_exec_file (0);
91  temp_kd = kvm_openfiles (execfile, filename, NULL,
92			   write_files ? O_RDWR : O_RDONLY, errbuf);
93  if (temp_kd == NULL)
94    error (("%s"), errbuf);
95
96  bsd_kvm_corefile = filename;
97  unpush_target (&bsd_kvm_ops);
98  core_kd = temp_kd;
99  push_target (&bsd_kvm_ops);
100
101  inf = add_inferior_silent (PIDGET(bsd_kvm_ptid));
102  inf->aspace = maybe_new_address_space ();
103  inf->pspace = add_program_space (inf->aspace);
104
105  add_thread_silent (bsd_kvm_ptid);
106  inferior_ptid = bsd_kvm_ptid;
107
108  target_fetch_registers (get_current_regcache (), -1);
109
110  reinit_frame_cache ();
111  print_stack_frame (get_selected_frame (NULL), -1, 1);
112}
113
114static void
115bsd_kvm_close (int quitting)
116{
117  if (core_kd)
118    {
119      if (kvm_close (core_kd) == -1)
120	warning (("%s"), kvm_geterr(core_kd));
121      core_kd = NULL;
122    }
123
124  inferior_ptid = null_ptid;
125  delete_thread_silent (bsd_kvm_ptid);
126}
127
128static LONGEST
129bsd_kvm_xfer_memory (CORE_ADDR addr, ULONGEST len,
130		     gdb_byte *readbuf, const gdb_byte *writebuf)
131{
132  ssize_t nbytes = len;
133
134  if (readbuf)
135    nbytes = kvm_read (core_kd, addr, readbuf, nbytes);
136  if (writebuf && nbytes > 0)
137    nbytes = kvm_write (core_kd, addr, writebuf, nbytes);
138  return nbytes;
139}
140
141static LONGEST
142bsd_kvm_xfer_partial (struct target_ops *ops, enum target_object object,
143		      const char *annex, gdb_byte *readbuf,
144		      const gdb_byte *writebuf,
145		      ULONGEST offset, LONGEST len)
146{
147  switch (object)
148    {
149    case TARGET_OBJECT_MEMORY:
150      return bsd_kvm_xfer_memory (offset, len, readbuf, writebuf);
151
152    default:
153      return -1;
154    }
155}
156
157static void
158bsd_kvm_files_info (struct target_ops *ops)
159{
160  if (bsd_kvm_corefile && strcmp (bsd_kvm_corefile, _PATH_MEM) != 0)
161    printf_filtered (_("\tUsing the kernel crash dump %s.\n"),
162		     bsd_kvm_corefile);
163  else
164    printf_filtered (_("\tUsing the currently running kernel.\n"));
165}
166
167/* Fetch process control block at address PADDR.  */
168
169static int
170bsd_kvm_fetch_pcb (struct regcache *regcache, struct pcb *paddr)
171{
172  struct pcb pcb;
173
174  if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
175    error (("%s"), kvm_geterr (core_kd));
176
177  gdb_assert (bsd_kvm_supply_pcb);
178  return bsd_kvm_supply_pcb (regcache, &pcb);
179}
180
181static void
182bsd_kvm_fetch_registers (struct target_ops *ops,
183			 struct regcache *regcache, int regnum)
184{
185  struct nlist nl[2];
186
187  if (bsd_kvm_paddr)
188    {
189      bsd_kvm_fetch_pcb (regcache, bsd_kvm_paddr);
190      return;
191    }
192
193  /* On dumping core, BSD kernels store the faulting context (PCB)
194     in the variable "dumppcb".  */
195  memset (nl, 0, sizeof nl);
196  nl[0].n_name = "_dumppcb";
197
198  if (kvm_nlist (core_kd, nl) == -1)
199    error (("%s"), kvm_geterr (core_kd));
200
201  if (nl[0].n_value != 0)
202    {
203      /* Found dumppcb.  If it contains a valid context, return
204	 immediately.  */
205      if (bsd_kvm_fetch_pcb (regcache, (struct pcb *) nl[0].n_value))
206	return;
207    }
208
209  /* Traditional BSD kernels have a process proc0 that should always
210     be present.  The address of proc0's PCB is stored in the variable
211     "proc0paddr".  */
212
213  memset (nl, 0, sizeof nl);
214  nl[0].n_name = "_proc0paddr";
215
216  if (kvm_nlist (core_kd, nl) == -1)
217    error (("%s"), kvm_geterr (core_kd));
218
219  if (nl[0].n_value != 0)
220    {
221      struct pcb *paddr;
222
223      /* Found proc0paddr.  */
224      if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
225	error (("%s"), kvm_geterr (core_kd));
226
227      bsd_kvm_fetch_pcb (regcache, paddr);
228      return;
229    }
230
231#if 1 /* TODO: HAVE_STRUCT_LWP_L_ADDR */
232  memset (nl, 0, sizeof nl);
233  nl[0].n_name = "_lwp0";
234
235  if (kvm_nlist (core_kd, nl) == -1)
236    error (("%s"), kvm_geterr (core_kd));
237
238  if (nl[0].n_value != 0)
239    {
240      struct pcb *paddr;
241
242      /* Found lwp0.  */
243      nl[0].n_value += offsetof (struct lwp, l_addr);
244      if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
245	error (("%s"), kvm_geterr (core_kd));
246
247      bsd_kvm_fetch_pcb (regcache, paddr);
248      return;
249    }
250#endif
251
252#ifdef HAVE_STRUCT_THREAD_TD_PCB
253  /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer
254     lives in `struct proc' but in `struct thread'.  The `struct
255     thread' for the initial thread for proc0 can be found in the
256     variable "thread0".  */
257
258  memset (nl, 0, sizeof nl);
259  nl[0].n_name = "_thread0";
260
261  if (kvm_nlist (core_kd, nl) == -1)
262    error (("%s"), kvm_geterr (core_kd));
263
264  if (nl[0].n_value != 0)
265    {
266      struct pcb *paddr;
267
268      /* Found thread0.  */
269      nl[0].n_value += offsetof (struct thread, td_pcb);
270      if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
271	error (("%s"), kvm_geterr (core_kd));
272
273      bsd_kvm_fetch_pcb (regcache, paddr);
274      return;
275    }
276#endif
277
278  /* i18n: PCB == "Process Control Block".  */
279  error (_("Cannot find a valid PCB"));
280}
281
282
283/* Kernel memory interface commands.  */
284struct cmd_list_element *bsd_kvm_cmdlist;
285
286static void
287bsd_kvm_cmd (char *arg, int fromtty)
288{
289  /* ??? Should this become an alias for "target kvm"?  */
290}
291
292#ifndef HAVE_STRUCT_THREAD_TD_PCB
293
294static void
295bsd_kvm_proc_cmd (char *arg, int fromtty)
296{
297  CORE_ADDR addr;
298
299  if (arg == NULL)
300    error_no_arg (_("proc address"));
301
302  if (core_kd == NULL)
303    error (_("No kernel memory image."));
304
305  addr = parse_and_eval_address (arg);
306#ifdef HAVE_STRUCT_LWP
307  addr += offsetof (struct lwp, l_addr);
308#else
309  addr += offsetof (struct proc, p_addr);
310#endif
311
312  if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
313    error (("%s"), kvm_geterr (core_kd));
314
315  target_fetch_registers (get_current_regcache (), -1);
316
317  reinit_frame_cache ();
318  print_stack_frame (get_selected_frame (NULL), -1, 1);
319}
320
321#endif
322
323static void
324bsd_kvm_pcb_cmd (char *arg, int fromtty)
325{
326  if (arg == NULL)
327    /* i18n: PCB == "Process Control Block".  */
328    error_no_arg (_("pcb address"));
329
330  if (core_kd == NULL)
331    error (_("No kernel memory image."));
332
333  bsd_kvm_paddr = (struct pcb *)(u_long) parse_and_eval_address (arg);
334
335  target_fetch_registers (get_current_regcache (), -1);
336
337  reinit_frame_cache ();
338  print_stack_frame (get_selected_frame (NULL), -1, 1);
339}
340
341static int
342bsd_kvm_thread_alive (struct target_ops *ops,
343		      ptid_t ptid)
344{
345  return 1;
346}
347
348static char *
349bsd_kvm_pid_to_str (struct target_ops *ops, ptid_t ptid)
350{
351  static char buf[64];
352  xsnprintf (buf, sizeof buf, "<kvm>");
353  return buf;
354}
355
356static int
357bsd_kvm_return_one (struct target_ops *ops)
358{
359  return 1;
360}
361
362/* Add the libkvm interface to the list of all possible targets and
363   register CUPPLY_PCB as the architecture-specific process control
364   block interpreter.  */
365
366void
367bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
368{
369  gdb_assert (bsd_kvm_supply_pcb == NULL);
370  bsd_kvm_supply_pcb = supply_pcb;
371
372  bsd_kvm_ops.to_shortname = "kvm";
373  bsd_kvm_ops.to_longname = _("Kernel memory interface");
374  bsd_kvm_ops.to_doc = _("Use a kernel virtual memory image as a target.\n\
375Optionally specify the filename of a core dump.");
376  bsd_kvm_ops.to_open = bsd_kvm_open;
377  bsd_kvm_ops.to_close = bsd_kvm_close;
378  bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers;
379  bsd_kvm_ops.to_xfer_partial = bsd_kvm_xfer_partial;
380  bsd_kvm_ops.to_files_info = bsd_kvm_files_info;
381  bsd_kvm_ops.to_thread_alive = bsd_kvm_thread_alive;
382  bsd_kvm_ops.to_pid_to_str = bsd_kvm_pid_to_str;
383  bsd_kvm_ops.to_stratum = process_stratum;
384  bsd_kvm_ops.to_has_memory = bsd_kvm_return_one;
385  bsd_kvm_ops.to_has_stack = bsd_kvm_return_one;
386  bsd_kvm_ops.to_has_registers = bsd_kvm_return_one;
387  bsd_kvm_ops.to_magic = OPS_MAGIC;
388
389  add_target (&bsd_kvm_ops);
390
391  add_prefix_cmd ("kvm", class_obscure, bsd_kvm_cmd, _("\
392Generic command for manipulating the kernel memory interface."),
393		  &bsd_kvm_cmdlist, "kvm ", 0, &cmdlist);
394
395#ifndef HAVE_STRUCT_THREAD_TD_PCB
396  add_cmd ("proc", class_obscure, bsd_kvm_proc_cmd,
397	   _("Set current context from proc address"), &bsd_kvm_cmdlist);
398#endif
399  add_cmd ("pcb", class_obscure, bsd_kvm_pcb_cmd,
400	   /* i18n: PCB == "Process Control Block".  */
401	   _("Set current context from pcb address"), &bsd_kvm_cmdlist);
402
403  /* Some notes on the ptid usage on this target.
404
405     The pid field represents the kvm inferior instance.  Currently,
406     we don't support multiple kvm inferiors, but we start at 1
407     anyway.  The lwp field is set to != 0, in case the core wants to
408     refer to the whole kvm inferior with ptid(1,0,0).
409
410     If kvm is made to export running processes as gdb threads,
411     the following form can be used:
412     ptid (1, 1, 0) -> kvm inferior 1, in kernel
413     ptid (1, 1, 1) -> kvm inferior 1, process 1
414     ptid (1, 1, 2) -> kvm inferior 1, process 2
415     ptid (1, 1, n) -> kvm inferior 1, process n  */
416
417  bsd_kvm_ptid = ptid_build (1, 1, 0);
418}
419