MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / bsd-kvm.c
blobbcfefca1b66b12fd5403983fb7efb6d305a657b2
1 /* BSD Kernel Data Access Library (libkvm) interface.
3 Copyright 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "defs.h"
23 #include "cli/cli-cmds.h"
24 #include "command.h"
25 #include "frame.h"
26 #include "regcache.h"
27 #include "target.h"
28 #include "value.h"
30 #include "gdb_assert.h"
31 #include <fcntl.h>
32 #include <kvm.h>
33 #include <nlist.h>
34 #include "readline/readline.h"
35 #include <sys/param.h>
36 #include <sys/user.h>
38 #include "bsd-kvm.h"
40 /* Kernel memory interface descriptor. */
41 kvm_t *core_kd;
43 /* Address of process control block. */
44 struct pcb *bsd_kvm_paddr;
46 /* Pointer to architecture-specific function that reconstructs the
47 register state from PCB and supplies it to REGCACHE. */
48 int (*bsd_kvm_supply_pcb)(struct regcache *regcache, struct pcb *pcb);
50 /* Target ops for libkvm interface. */
51 struct target_ops bsd_kvm_ops;
53 static void
54 bsd_kvm_open (char *filename, int from_tty)
56 char errbuf[_POSIX2_LINE_MAX];
57 char *execfile = NULL;
58 kvm_t *temp_kd;
60 target_preopen (from_tty);
62 if (filename)
64 char *temp;
66 filename = tilde_expand (filename);
67 if (filename[0] != '/')
69 temp = concat (current_directory, "/", filename, NULL);
70 xfree (filename);
71 filename = temp;
75 temp_kd = kvm_openfiles (execfile, filename, NULL, O_RDONLY, errbuf);
76 if (temp_kd == NULL)
77 error ("%s", errbuf);
79 unpush_target (&bsd_kvm_ops);
80 core_kd = temp_kd;
81 push_target (&bsd_kvm_ops);
83 target_fetch_registers (-1);
85 flush_cached_frames ();
86 select_frame (get_current_frame ());
87 print_stack_frame (get_selected_frame (), -1, 1);
90 static void
91 bsd_kvm_close (int quitting)
93 if (core_kd)
95 if (kvm_close (core_kd) == -1)
96 warning ("%s", kvm_geterr(core_kd));
97 core_kd = NULL;
101 static int
102 bsd_kvm_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
103 int write, struct mem_attrib *attrib,
104 struct target_ops *ops)
106 if (write)
107 return kvm_write (core_kd, memaddr, myaddr, len);
108 else
109 return kvm_read (core_kd, memaddr, myaddr, len);
111 return -1;
114 /* Fetch process control block at address PADDR. */
116 static int
117 bsd_kvm_fetch_pcb (struct pcb *paddr)
119 struct pcb pcb;
121 if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
122 error ("%s", kvm_geterr (core_kd));
124 gdb_assert (bsd_kvm_supply_pcb);
125 return bsd_kvm_supply_pcb (current_regcache, &pcb);
128 static void
129 bsd_kvm_fetch_registers (int regnum)
131 struct nlist nl[2];
133 if (bsd_kvm_paddr)
135 bsd_kvm_fetch_pcb (bsd_kvm_paddr);
136 return;
139 /* On dumping core, BSD kernels store the faulting context (PCB)
140 in the variable "dumppcb". */
141 memset (nl, 0, sizeof nl);
142 nl[0].n_name = "_dumppcb";
144 if (kvm_nlist (core_kd, nl) == -1)
145 error ("%s", kvm_geterr (core_kd));
147 if (nl[0].n_value != 0)
149 /* Found dumppcb. If it contains a valid context, return
150 immediately. */
151 if (bsd_kvm_fetch_pcb ((struct pcb *) nl[0].n_value))
152 return;
155 /* Traditional BSD kernels have a process proc0 that should always
156 be present. The address of proc0's PCB is stored in the variable
157 "proc0paddr". */
159 memset (nl, 0, sizeof nl);
160 nl[0].n_name = "_proc0paddr";
162 if (kvm_nlist (core_kd, nl) == -1)
163 error ("%s", kvm_geterr (core_kd));
165 if (nl[0].n_value != 0)
167 struct pcb *paddr;
169 /* Found proc0paddr. */
170 if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
171 error ("%s", kvm_geterr (core_kd));
173 bsd_kvm_fetch_pcb (paddr);
174 return;
177 #ifdef HAVE_STRUCT_THREAD_TD_PCB
178 /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer
179 lives in `struct proc' but in `struct thread'. The `struct
180 thread' for the initial thread for proc0 can be found in the
181 variable "thread0". */
183 memset (nl, 0, sizeof nl);
184 nl[0].n_name = "_thread0";
186 if (kvm_nlist (core_kd, nl) == -1)
187 error ("%s", kvm_geterr (core_kd));
189 if (nl[0].n_value != 0)
191 struct pcb *paddr;
193 /* Found thread0. */
194 nl[0].n_value += offsetof (struct thread, td_pcb);
195 if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
196 error ("%s", kvm_geterr (core_kd));
198 bsd_kvm_fetch_pcb (paddr);
199 return;
201 #endif
203 error ("Cannot find a valid PCB");
207 /* Kernel memory interface commands. */
208 struct cmd_list_element *bsd_kvm_cmdlist;
210 static void
211 bsd_kvm_cmd (char *arg, int fromtty)
213 /* ??? Should this become an alias for "target kvm"? */
216 #ifndef HAVE_STRUCT_THREAD_TD_PCB
218 static void
219 bsd_kvm_proc_cmd (char *arg, int fromtty)
221 CORE_ADDR addr;
223 if (arg == NULL)
224 error_no_arg ("proc address");
226 if (core_kd == NULL)
227 error ("No kernel memory image.");
229 addr = parse_and_eval_address (arg);
230 addr += offsetof (struct proc, p_addr);
232 if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
233 error ("%s", kvm_geterr (core_kd));
235 target_fetch_registers (-1);
237 flush_cached_frames ();
238 select_frame (get_current_frame ());
239 print_stack_frame (get_selected_frame (), -1, 1);
242 #endif
244 static void
245 bsd_kvm_pcb_cmd (char *arg, int fromtty)
247 if (arg == NULL)
248 error_no_arg ("pcb address");
250 if (core_kd == NULL)
251 error ("No kernel memory image.");
253 bsd_kvm_paddr = (struct pcb *) parse_and_eval_address (arg);
255 target_fetch_registers (-1);
257 flush_cached_frames ();
258 select_frame (get_current_frame ());
259 print_stack_frame (get_selected_frame (), -1, 1);
262 /* Add the libkvm interface to the list of all possible targets and
263 register CUPPLY_PCB as the architecture-specific process control
264 block interpreter. */
266 void
267 bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
269 gdb_assert (bsd_kvm_supply_pcb == NULL);
270 bsd_kvm_supply_pcb = supply_pcb;
272 bsd_kvm_ops.to_shortname = "kvm";
273 bsd_kvm_ops.to_longname = "Kernel memory interface";
274 bsd_kvm_ops.to_doc = "Use a kernel virtual memory image as a target.\n\
275 Optionally specify the filename of a core dump.";
276 bsd_kvm_ops.to_open = bsd_kvm_open;
277 bsd_kvm_ops.to_close = bsd_kvm_close;
278 bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers;
279 bsd_kvm_ops.to_xfer_memory = bsd_kvm_xfer_memory;
280 bsd_kvm_ops.to_stratum = process_stratum;
281 bsd_kvm_ops.to_has_memory = 1;
282 bsd_kvm_ops.to_has_stack = 1;
283 bsd_kvm_ops.to_has_registers = 1;
284 bsd_kvm_ops.to_magic = OPS_MAGIC;
286 add_target (&bsd_kvm_ops);
288 add_prefix_cmd ("kvm", class_obscure, bsd_kvm_cmd, "\
289 Generic command for manipulating the kernel memory interface.",
290 &bsd_kvm_cmdlist, "kvm ", 0, &cmdlist);
292 #ifndef HAVE_STRUCT_THREAD_TD_PCB
293 add_cmd ("proc", class_obscure, bsd_kvm_proc_cmd,
294 "Set current context from proc address", &bsd_kvm_cmdlist);
295 #endif
296 add_cmd ("pcb", class_obscure, bsd_kvm_pcb_cmd,
297 "Set current context from pcb address", &bsd_kvm_cmdlist);