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
)
131 bsd_kvm_fetch_pcb (bsd_kvm_paddr
);
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";
138 if (kvm_nlist (core_kd
, nl
) == -1)
139 error ("%s", kvm_geterr (core_kd
));
141 if (nl
[0].n_value
!= 0)
143 /* Found dumppcb. If it contains a valid context, return
145 if (bsd_kvm_fetch_pcb ((struct pcb
*) nl
[0].n_value
))
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
153 memset (nl
, 0, sizeof nl
);
154 nl
[0].n_name
= "_proc0paddr";
156 if (kvm_nlist (core_kd
, nl
) == -1)
157 error ("%s", kvm_geterr (core_kd
));
159 if (nl
[0].n_value
!= 0)
163 /* Found proc0paddr. */
164 if (kvm_read (core_kd
, nl
[0].n_value
, &paddr
, sizeof paddr
) == -1)
165 error ("%s", kvm_geterr (core_kd
));
167 bsd_kvm_fetch_pcb (paddr
);
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". */
177 memset (nl
, 0, sizeof nl
);
178 nl
[0].n_name
= "_thread0";
180 if (kvm_nlist (core_kd
, nl
) == -1)
181 error ("%s", kvm_geterr (core_kd
));
183 if (nl
[0].n_value
!= 0)
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
));
192 bsd_kvm_fetch_pcb (paddr
);
197 error ("Cannot find a valid PCB");
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. */
206 bsd_kvm_add_target (int (*supply_pcb
)(struct regcache
*, struct pcb
*))
208 gdb_assert (bsd_kvm_supply_pcb
== NULL
);
209 bsd_kvm_supply_pcb
= supply_pcb
;
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
;
225 add_target (&bsd_kvm_ops
);
This page took 0.053775 seconds and 4 git commands to generate.