Commit | Line | Data |
---|---|---|
2e0c3539 MK |
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 "frame.h" | |
24 | #include "regcache.h" | |
25 | #include "target.h" | |
26 | ||
27 | #include "gdb_assert.h" | |
28 | #include <fcntl.h> | |
29 | #include <kvm.h> | |
30 | #include <nlist.h> | |
31 | #include "readline/readline.h" | |
32 | #include <sys/param.h> | |
33 | #include <sys/user.h> | |
34 | ||
35 | #include "bsd-kvm.h" | |
36 | ||
37 | /* Kernel memory interface descriptor. */ | |
38 | kvm_t *core_kd; | |
39 | ||
40 | /* Address of process control block. */ | |
41 | struct pcb *bsd_kvm_paddr; | |
42 | ||
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); | |
46 | ||
47 | /* Target ops for libkvm interface. */ | |
48 | struct target_ops bsd_kvm_ops; | |
49 | ||
50 | static void | |
51 | bsd_kvm_open (char *filename, int from_tty) | |
52 | { | |
53 | char errbuf[_POSIX2_LINE_MAX]; | |
54 | char *execfile = NULL; | |
55 | kvm_t *temp_kd; | |
56 | ||
57 | target_preopen (from_tty); | |
58 | ||
59 | if (filename) | |
60 | { | |
61 | char *temp; | |
62 | ||
63 | filename = tilde_expand (filename); | |
64 | if (filename[0] != '/') | |
65 | { | |
66 | temp = concat (current_directory, "/", filename, NULL); | |
67 | xfree (filename); | |
68 | filename = temp; | |
69 | } | |
70 | } | |
71 | ||
72 | temp_kd = kvm_openfiles (execfile, filename, NULL, O_RDONLY, errbuf); | |
73 | if (temp_kd == NULL) | |
74 | error ("%s", errbuf); | |
75 | ||
76 | unpush_target (&bsd_kvm_ops); | |
77 | core_kd = temp_kd; | |
78 | push_target (&bsd_kvm_ops); | |
79 | ||
80 | target_fetch_registers (-1); | |
81 | ||
82 | flush_cached_frames (); | |
83 | select_frame (get_current_frame ()); | |
84 | print_stack_frame (get_selected_frame (), -1, 1); | |
85 | } | |
86 | ||
87 | static void | |
88 | bsd_kvm_close (int quitting) | |
89 | { | |
90 | if (core_kd) | |
91 | { | |
92 | if (kvm_close (core_kd) == -1) | |
93 | warning ("%s", kvm_geterr(core_kd)); | |
94 | core_kd = NULL; | |
95 | } | |
96 | } | |
97 | ||
98 | static int | |
99 | bsd_kvm_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, | |
100 | int write, struct mem_attrib *attrib, | |
101 | struct target_ops *ops) | |
102 | { | |
103 | if (write) | |
104 | return kvm_write (core_kd, memaddr, myaddr, len); | |
105 | else | |
106 | return kvm_read (core_kd, memaddr, myaddr, len); | |
107 | ||
108 | return -1; | |
109 | } | |
110 | ||
111 | /* Fetch process control block at address PADDR. */ | |
112 | ||
113 | static int | |
114 | bsd_kvm_fetch_pcb (struct pcb *paddr) | |
115 | { | |
116 | struct pcb pcb; | |
117 | ||
118 | if (kvm_read (core_kd, (unsigned long) paddr, &pcb, sizeof pcb) == -1) | |
119 | error ("%s", kvm_geterr (core_kd)); | |
120 | ||
121 | gdb_assert (bsd_kvm_supply_pcb); | |
122 | return bsd_kvm_supply_pcb (current_regcache, &pcb); | |
123 | } | |
124 | ||
125 | static void | |
126 | bsd_kvm_fetch_registers (int regnum) | |
127 | { | |
128 | struct nlist nl[2]; | |
129 | ||
130 | if (bsd_kvm_paddr) | |
131 | bsd_kvm_fetch_pcb (bsd_kvm_paddr); | |
132 | ||
133 | /* On dumping core, BSD kernels store the faulting context (PCB) | |
134 | in the variable "dumppcb". */ | |
135 | memset (nl, 0, sizeof nl); | |
136 | nl[0].n_name = "_dumppcb"; | |
137 | ||
138 | if (kvm_nlist (core_kd, nl) == -1) | |
139 | error ("%s", kvm_geterr (core_kd)); | |
140 | ||
141 | if (nl[0].n_value != 0) | |
142 | { | |
143 | /* Found dumppcb. If it contains a valid context, return | |
144 | immediately. */ | |
145 | if (bsd_kvm_fetch_pcb ((struct pcb *) nl[0].n_value)) | |
146 | return; | |
147 | } | |
148 | ||
149 | /* Traditional BSD kernels have a process proc0 that should always | |
150 | be present. The address of proc0's PCB is stored in the variable | |
151 | "proc0paddr". */ | |
152 | ||
153 | memset (nl, 0, sizeof nl); | |
154 | nl[0].n_name = "_proc0paddr"; | |
155 | ||
156 | if (kvm_nlist (core_kd, nl) == -1) | |
157 | error ("%s", kvm_geterr (core_kd)); | |
158 | ||
159 | if (nl[0].n_value != 0) | |
160 | { | |
161 | struct pcb *paddr; | |
162 | ||
163 | /* Found proc0paddr. */ | |
164 | if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1) | |
165 | error ("%s", kvm_geterr (core_kd)); | |
166 | ||
167 | bsd_kvm_fetch_pcb (paddr); | |
168 | return; | |
169 | } | |
170 | ||
171 | #ifdef HAVE_STRUCT_THREAD_TD_PCB | |
172 | /* In FreeBSD kernels for 5.0-RELEASE and later, the PCB no longer | |
173 | lives in `struct proc' but in `struct thread'. The `struct | |
174 | thread' for the initial thread for proc0 can be found in the | |
175 | variable "thread0". */ | |
176 | ||
177 | memset (nl, 0, sizeof nl); | |
178 | nl[0].n_name = "_thread0"; | |
179 | ||
180 | if (kvm_nlist (core_kd, nl) == -1) | |
181 | error ("%s", kvm_geterr (core_kd)); | |
182 | ||
183 | if (nl[0].n_value != 0) | |
184 | { | |
185 | struct pcb *paddr; | |
186 | ||
187 | /* Found thread0. */ | |
188 | nl[1].n_value += offsetof (struct thread, td_pcb); | |
189 | if (kvm_read (core_kd, nl[1].n_value, &paddr, sizeof paddr) == -1) | |
190 | error ("%s", kvm_geterr (core_kd)); | |
191 | ||
192 | bsd_kvm_fetch_pcb (paddr); | |
193 | return; | |
194 | } | |
195 | #endif | |
196 | ||
197 | error ("Cannot find a valid PCB"); | |
198 | } | |
199 | \f | |
200 | ||
201 | /* Add the libkvm interface to the list of all possible targets and | |
202 | register CUPPLY_PCB as the architecture-specific process control | |
203 | block interpreter. */ | |
204 | ||
205 | void | |
206 | bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *)) | |
207 | { | |
208 | gdb_assert (bsd_kvm_supply_pcb == NULL); | |
209 | bsd_kvm_supply_pcb = supply_pcb; | |
210 | ||
211 | bsd_kvm_ops.to_shortname = "kvm"; | |
212 | bsd_kvm_ops.to_longname = "Kernel memory interface"; | |
213 | bsd_kvm_ops.to_doc = "Use a kernel virtual memory image as a target.\n\ | |
214 | Optionally specify the filename of a core dump."; | |
215 | bsd_kvm_ops.to_open = bsd_kvm_open; | |
216 | bsd_kvm_ops.to_close = bsd_kvm_close; | |
217 | bsd_kvm_ops.to_fetch_registers = bsd_kvm_fetch_registers; | |
218 | bsd_kvm_ops.to_xfer_memory = bsd_kvm_xfer_memory; | |
219 | bsd_kvm_ops.to_stratum = process_stratum; | |
220 | bsd_kvm_ops.to_has_memory = 1; | |
221 | bsd_kvm_ops.to_has_stack = 1; | |
222 | bsd_kvm_ops.to_has_registers = 1; | |
223 | bsd_kvm_ops.to_magic = OPS_MAGIC; | |
224 | ||
225 | add_target (&bsd_kvm_ops); | |
226 | } |