Import gdb-7.0
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / bsd-kvm.c
1 /* BSD Kernel Data Access Library (libkvm) interface.
2
3    Copyright 2004 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
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"
29
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>
37
38 #include "bsd-kvm.h"
39
40 /* Kernel memory interface descriptor.  */
41 kvm_t *core_kd;
42
43 /* Address of process control block.  */
44 struct pcb *bsd_kvm_paddr;
45
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);
49
50 /* Target ops for libkvm interface.  */
51 struct target_ops bsd_kvm_ops;
52
53 static void
54 bsd_kvm_open (char *filename, int from_tty)
55 {
56   char errbuf[_POSIX2_LINE_MAX];
57   char *execfile = NULL;
58   kvm_t *temp_kd;
59
60   target_preopen (from_tty);
61
62   if (filename)
63     {
64       char *temp;
65
66       filename = tilde_expand (filename);
67       if (filename[0] != '/')
68         {
69           temp = concat (current_directory, "/", filename, NULL);
70           xfree (filename);
71           filename = temp;
72         }
73     }
74
75   temp_kd = kvm_openfiles (execfile, filename, NULL, O_RDONLY, errbuf);
76   if (temp_kd == NULL)
77     error ("%s", errbuf);
78
79   unpush_target (&bsd_kvm_ops);
80   core_kd = temp_kd;
81   push_target (&bsd_kvm_ops);
82
83   target_fetch_registers (-1);
84
85   flush_cached_frames ();
86   select_frame (get_current_frame ());
87   print_stack_frame (get_selected_frame (), -1, 1);
88 }
89
90 static void
91 bsd_kvm_close (int quitting)
92 {
93   if (core_kd)
94     {
95       if (kvm_close (core_kd) == -1)
96         warning ("%s", kvm_geterr(core_kd));
97       core_kd = NULL;
98     }
99 }
100
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)
105 {
106   if (write)
107     return kvm_write (core_kd, memaddr, myaddr, len);
108   else
109     return kvm_read (core_kd, memaddr, myaddr, len);
110
111   return -1;
112 }
113
114 /* Fetch process control block at address PADDR.  */
115
116 static int
117 bsd_kvm_fetch_pcb (struct pcb *paddr)
118 {
119   struct pcb pcb;
120
121   if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
122     error ("%s", kvm_geterr (core_kd));
123
124   gdb_assert (bsd_kvm_supply_pcb);
125   return bsd_kvm_supply_pcb (current_regcache, &pcb);
126 }
127
128 static void
129 bsd_kvm_fetch_registers (int regnum)
130 {
131   struct nlist nl[2];
132
133   if (bsd_kvm_paddr)
134     {
135       bsd_kvm_fetch_pcb (bsd_kvm_paddr);
136       return;
137     }
138
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";
143
144   if (kvm_nlist (core_kd, nl) == -1)
145     error ("%s", kvm_geterr (core_kd));
146
147   if (nl[0].n_value != 0)
148     {
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;
153     }
154
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".  */
158
159   memset (nl, 0, sizeof nl);
160   nl[0].n_name = "_proc0paddr";
161
162   if (kvm_nlist (core_kd, nl) == -1)
163     error ("%s", kvm_geterr (core_kd));
164
165   if (nl[0].n_value != 0)
166     {
167       struct pcb *paddr;
168
169       /* Found proc0paddr.  */
170       if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
171         error ("%s", kvm_geterr (core_kd));
172
173       bsd_kvm_fetch_pcb (paddr);
174       return;
175     }
176
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".  */
182
183   memset (nl, 0, sizeof nl);
184   nl[0].n_name = "_thread0";
185
186   if (kvm_nlist (core_kd, nl) == -1)
187     error ("%s", kvm_geterr (core_kd));
188
189   if (nl[0].n_value != 0)
190     {
191       struct pcb *paddr;
192
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));
197
198       bsd_kvm_fetch_pcb (paddr);
199       return;
200     }
201 #endif
202
203   error ("Cannot find a valid PCB");
204 }
205 \f
206
207 /* Kernel memory interface commands.  */
208 struct cmd_list_element *bsd_kvm_cmdlist;
209
210 static void
211 bsd_kvm_cmd (char *arg, int fromtty)
212 {
213   /* ??? Should this become an alias for "target kvm"?  */
214 }
215
216 #ifndef HAVE_STRUCT_THREAD_TD_PCB
217
218 static void
219 bsd_kvm_proc_cmd (char *arg, int fromtty)
220 {
221   CORE_ADDR addr;
222
223   if (arg == NULL)
224     error_no_arg ("proc address");
225
226   if (core_kd == NULL)
227     error ("No kernel memory image.");
228
229   addr = parse_and_eval_address (arg);
230   addr += offsetof (struct proc, p_addr);
231
232   if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
233     error ("%s", kvm_geterr (core_kd));
234
235   target_fetch_registers (-1);
236
237   flush_cached_frames ();
238   select_frame (get_current_frame ());
239   print_stack_frame (get_selected_frame (), -1, 1);
240 }
241
242 #endif
243
244 static void
245 bsd_kvm_pcb_cmd (char *arg, int fromtty)
246 {
247   if (arg == NULL)
248     error_no_arg ("pcb address");
249
250   if (core_kd == NULL)
251     error ("No kernel memory image.");
252
253   bsd_kvm_paddr = (struct pcb *) parse_and_eval_address (arg);
254
255   target_fetch_registers (-1);
256
257   flush_cached_frames ();
258   select_frame (get_current_frame ());
259   print_stack_frame (get_selected_frame (), -1, 1);
260 }
261
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.  */
265
266 void
267 bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
268 {
269   gdb_assert (bsd_kvm_supply_pcb == NULL);
270   bsd_kvm_supply_pcb = supply_pcb;
271
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;
285
286   add_target (&bsd_kvm_ops);
287   
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);
291
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);
298 }