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. */
27 #include "gdb_assert.h"
31 #include "readline/readline.h"
32 #include <sys/param.h>
37 /* Kernel memory interface descriptor. */
40 /* Address of process control block. */
41 struct pcb *bsd_kvm_paddr;
43 /* Pointer to architecture-specific function that reconstructs the
44 register state from PCB and supplies it to REGCACHE. */
45 int (*bsd_kvm_supply_pcb)(struct regcache *regcache, struct pcb *pcb);
47 /* Target ops for libkvm interface. */
48 struct target_ops bsd_kvm_ops;
51 bsd_kvm_open (char *filename, int from_tty)
53 char errbuf[_POSIX2_LINE_MAX];
54 char *execfile = NULL;
57 target_preopen (from_tty);
63 filename = tilde_expand (filename);
64 if (filename[0] != '/')
66 temp = concat (current_directory, "/", filename, NULL);
72 temp_kd = kvm_openfiles (execfile, filename, NULL, O_RDONLY, errbuf);
76 unpush_target (&bsd_kvm_ops);
78 push_target (&bsd_kvm_ops);
80 target_fetch_registers (-1);
82 flush_cached_frames ();
83 select_frame (get_current_frame ());
84 print_stack_frame (get_selected_frame (), -1, 1);
88 bsd_kvm_close (int quitting)
92 if (kvm_close (core_kd) == -1)
93 warning ("%s", kvm_geterr(core_kd));
99 bsd_kvm_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
100 int write, struct mem_attrib *attrib,
101 struct target_ops *ops)
104 return kvm_write (core_kd, memaddr, myaddr, len);
106 return kvm_read (core_kd, memaddr, myaddr, len);
111 /* Fetch process control block at address PADDR. */
114 bsd_kvm_fetch_pcb (struct pcb *paddr)
118 if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1)
119 error ("%s", kvm_geterr (core_kd));
121 gdb_assert (bsd_kvm_supply_pcb);
122 return bsd_kvm_supply_pcb (current_regcache, &pcb);
126 bsd_kvm_fetch_registers (int regnum)
132 bsd_kvm_fetch_pcb (bsd_kvm_paddr);
136 /* On dumping core, BSD kernels store the faulting context (PCB)
137 in the variable "dumppcb". */
138 memset (nl, 0, sizeof nl);
139 nl[0].n_name = "_dumppcb";
141 if (kvm_nlist (core_kd, nl) == -1)
142 error ("%s", kvm_geterr (core_kd));
144 if (nl[0].n_value != 0)
146 /* Found dumppcb. If it contains a valid context, return
148 if (bsd_kvm_fetch_pcb ((struct pcb *) nl[0].n_value))
152 /* Traditional BSD kernels have a process proc0 that should always
153 be present. The address of proc0's PCB is stored in the variable
156 memset (nl, 0, sizeof nl);
157 nl[0].n_name = "_proc0paddr";
159 if (kvm_nlist (core_kd, nl) == -1)
160 error ("%s", kvm_geterr (core_kd));
162 if (nl[0].n_value != 0)
166 /* Found proc0paddr. */
167 if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
168 error ("%s", kvm_geterr (core_kd));
170 bsd_kvm_fetch_pcb (paddr);
174 #ifdef HAVE_STRUCT_THREAD_TD_PCB
175 /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer
176 lives in `struct proc' but in `struct thread'. The `struct
177 thread' for the initial thread for proc0 can be found in the
178 variable "thread0". */
180 memset (nl, 0, sizeof nl);
181 nl[0].n_name = "_thread0";
183 if (kvm_nlist (core_kd, nl) == -1)
184 error ("%s", kvm_geterr (core_kd));
186 if (nl[0].n_value != 0)
191 nl[0].n_value += offsetof (struct thread, td_pcb);
192 if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
193 error ("%s", kvm_geterr (core_kd));
195 bsd_kvm_fetch_pcb (paddr);
200 error ("Cannot find a valid PCB");
204 /* Add the libkvm interface to the list of all possible targets and
205 register CUPPLY_PCB as the architecture-specific process control
206 block interpreter. */
209 bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
211 gdb_assert (bsd_kvm_supply_pcb == NULL);
212 bsd_kvm_supply_pcb = supply_pcb;
214 bsd_kvm_ops.to_shortname = "kvm";
215 bsd_kvm_ops.to_longname = "Kernel memory interface";
216 bsd_kvm_ops.to_doc = "Use a kernel virtual memory image as a target.\n\
217 Optionally specify the filename of a core dump.";
218 bsd_kvm_ops.to_open = bsd_kvm_open;
219 bsd_kvm_ops.to_close = bsd_kvm_close;
220 bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers;
221 bsd_kvm_ops.to_xfer_memory = bsd_kvm_xfer_memory;
222 bsd_kvm_ops.to_stratum = process_stratum;
223 bsd_kvm_ops.to_has_memory = 1;
224 bsd_kvm_ops.to_has_stack = 1;
225 bsd_kvm_ops.to_has_registers = 1;
226 bsd_kvm_ops.to_magic = OPS_MAGIC;
228 add_target (&bsd_kvm_ops);