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