* bsd-kvm.c (bsd_kvm_fetch_registers): Directly return after
[deliverable/binutils-gdb.git] / 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 "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 {
132 bsd_kvm_fetch_pcb (bsd_kvm_paddr);
133 return;
134 }
135
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";
140
141 if (kvm_nlist (core_kd, nl) == -1)
142 error ("%s", kvm_geterr (core_kd));
143
144 if (nl[0].n_value != 0)
145 {
146 /* Found dumppcb. If it contains a valid context, return
147 immediately. */
148 if (bsd_kvm_fetch_pcb ((struct pcb *) nl[0].n_value))
149 return;
150 }
151
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
154 "proc0paddr". */
155
156 memset (nl, 0, sizeof nl);
157 nl[0].n_name = "_proc0paddr";
158
159 if (kvm_nlist (core_kd, nl) == -1)
160 error ("%s", kvm_geterr (core_kd));
161
162 if (nl[0].n_value != 0)
163 {
164 struct pcb *paddr;
165
166 /* Found proc0paddr. */
167 if (kvm_read (core_kd, nl[0].n_value, &paddr, sizeof paddr) == -1)
168 error ("%s", kvm_geterr (core_kd));
169
170 bsd_kvm_fetch_pcb (paddr);
171 return;
172 }
173
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". */
179
180 memset (nl, 0, sizeof nl);
181 nl[0].n_name = "_thread0";
182
183 if (kvm_nlist (core_kd, nl) == -1)
184 error ("%s", kvm_geterr (core_kd));
185
186 if (nl[0].n_value != 0)
187 {
188 struct pcb *paddr;
189
190 /* Found thread0. */
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));
194
195 bsd_kvm_fetch_pcb (paddr);
196 return;
197 }
198 #endif
199
200 error ("Cannot find a valid PCB");
201 }
202 \f
203
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. */
207
208 void
209 bsd_kvm_add_target (int (*supply_pcb)(struct regcache *, struct pcb *))
210 {
211 gdb_assert (bsd_kvm_supply_pcb == NULL);
212 bsd_kvm_supply_pcb = supply_pcb;
213
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;
227
228 add_target (&bsd_kvm_ops);
229 }
This page took 0.042456 seconds and 5 git commands to generate.