1 /*
2 * Copyright (c) 2004 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.12 2008/05/01 20:36:48 jhb Exp $
27 */
28
29 #include <sys/cdefs.h>
30
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34 #include <err.h>
35 #include <fcntl.h>
36 #include <kvm.h>
37
38 #include <defs.h>
39 #include <readline/readline.h>
40 #include <readline/tilde.h>
41 #include <command.h>
42 #include <exec.h>
43 #include <frame-unwind.h>
44 #include <gdb.h>
45 #include <gdbcore.h>
46 #include <gdbthread.h>
47 #include <inferior.h>
48 #include <language.h>
49 #include <regcache.h>
50 #include <solib.h>
51 #include <target.h>
52 #include <ui-out.h>
53 #include <observer.h>
54 #include <arch-utils.h>
55
56 #include "kgdb.h"
57
58 static void kgdb_core_cleanup(void *);
59
60 static char *vmcore;
61 static struct target_ops kgdb_trgt_ops;
62
63 kvm_t *kvm;
64 static char kvm_err[_POSIX2_LINE_MAX];
65
66 #define KERNOFF (kgdb_kernbase ())
67 #define INKERNEL(x) ((x) >= KERNOFF)
68
69 static CORE_ADDR
kgdb_kernbase(void)70 kgdb_kernbase (void)
71 {
72 static CORE_ADDR kernbase;
73 struct minimal_symbol *sym;
74
75 if (kernbase == 0) {
76 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
77 if (sym == NULL) {
78 kernbase = KERNBASE;
79 } else {
80 kernbase = SYMBOL_VALUE_ADDRESS (sym);
81 }
82 }
83 return kernbase;
84 }
85
86 static void
kgdb_trgt_open(char * filename,int from_tty)87 kgdb_trgt_open(char *filename, int from_tty)
88 {
89 struct cleanup *old_chain;
90 struct kthr *kt;
91 struct inferior *inf8;
92 struct program_space *pspace;
93 kvm_t *nkvm;
94 char *temp;
95 int first_inferior = 1;
96 struct gdbarch_info info;
97 struct gdbarch *kgdbarch;
98
99 target_preopen (from_tty);
100 if (!filename)
101 error ("No vmcore file specified.");
102 if (!exec_bfd)
103 error ("Can't open a vmcore without a kernel");
104
105 filename = tilde_expand (filename);
106 if (filename[0] != '/') {
107 temp = concat (current_directory, "/", filename, NULL);
108 xfree(filename);
109 filename = temp;
110 }
111
112 old_chain = make_cleanup (xfree, filename);
113
114 nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
115 write_files ? O_RDWR : O_RDONLY, kvm_err);
116 if (nkvm == NULL)
117 error ("Failed to open vmcore: %s", kvm_err);
118
119 /* Don't free the filename now and close any previous vmcore. */
120 discard_cleanups(old_chain);
121 unpush_target(&kgdb_trgt_ops);
122
123 kvm = nkvm;
124 vmcore = filename;
125 old_chain = make_cleanup(kgdb_core_cleanup, NULL);
126
127 push_target (&kgdb_trgt_ops);
128 discard_cleanups (old_chain);
129
130 kgdb_dmesg();
131
132 gdbarch_info_init (&info);
133 #if defined (__i386__)
134 info.bfd_arch_info = bfd_scan_arch ("i386");
135 #elif defined (__x86_64__)
136 info.bfd_arch_info = bfd_scan_arch ("i386:x86-64");
137 #else
138 #error platform not recognized
139 #endif
140 info.byte_order = BFD_ENDIAN_LITTLE;
141 gdbarch_info_fill (&info);
142 kgdbarch = gdbarch_find_by_info (info);
143
144 init_thread_list();
145 kt = kgdb_thr_init();
146 while (kt != NULL) {
147 if (!in_inferior_list(kt->pid)) {
148 if (first_inferior) {
149 first_inferior = 0;
150 inf8 = current_inferior();
151 inf8->pid = kt->pid;
152 inf8->attach_flag = 1;
153 inferior_appeared (inf8, kt->pid);
154 pspace = current_program_space;
155 pspace->ebfd = 0;
156 pspace->ebfd_mtime = 0;
157 } else {
158 inf8 = add_inferior(kt->pid);
159 inf8->attach_flag = 0;
160 pspace = add_program_space(new_address_space());
161 pspace->symfile_object_file = symfile_objfile;
162 pspace->objfiles = object_files;
163 }
164 inf8->pspace = pspace;
165 inf8->aspace = pspace->aspace;
166 inf8->fake_pid_p = 0;
167 inf8->gdbarch = kgdbarch;
168 }
169 add_thread(ptid_build(kt->pid, kt->lwpid, kt->tid));
170 kt = kgdb_thr_next(kt);
171 }
172 if (curkthr != 0)
173 inferior_ptid = ptid_build(curkthr->pid, curkthr->lwpid,
174 curkthr->tid);
175
176 frame_unwind_prepend_unwinder(kgdbarch, &kgdb_trgt_trapframe_unwind);
177
178 kld_init(kgdbarch);
179 reinit_frame_cache();
180 select_frame (get_current_frame());
181 print_stack_frame(get_selected_frame(NULL), 0, SRC_AND_LOC);
182 }
183
184 static void
kgdb_trgt_close(int quitting)185 kgdb_trgt_close(int quitting)
186 {
187
188 if (kvm != NULL) {
189 inferior_ptid = null_ptid;
190 clear_solib();
191 if (kvm_close(kvm) != 0)
192 warning("cannot close \"%s\": %s", vmcore,
193 kvm_geterr(kvm));
194 kvm = NULL;
195 xfree(vmcore);
196 vmcore = NULL;
197 }
198 }
199
200 static void
kgdb_core_cleanup(void * arg)201 kgdb_core_cleanup(void *arg)
202 {
203
204 kgdb_trgt_close(0);
205 }
206
207 static void
kgdb_trgt_detach(struct target_ops * target,char * args,int from_tty)208 kgdb_trgt_detach(struct target_ops *target, char *args, int from_tty)
209 {
210
211 if (args)
212 error ("Too many arguments");
213 unpush_target(target);
214 reinit_frame_cache();
215 if (from_tty)
216 printf_filtered("No vmcore file now.\n");
217 }
218
219 static char *
kgdb_trgt_extra_thread_info(struct thread_info * ti)220 kgdb_trgt_extra_thread_info(struct thread_info *ti)
221 {
222
223 return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid)));
224 }
225
226 static void
kgdb_trgt_files_info(struct target_ops * target)227 kgdb_trgt_files_info(struct target_ops *target)
228 {
229
230 printf_filtered ("\t`%s', ", vmcore);
231 wrap_here (" ");
232 printf_filtered ("file type %s.\n", "DragonFly kernel vmcore");
233 }
234
235 static void
kgdb_trgt_find_new_threads(struct target_ops * target_ops)236 kgdb_trgt_find_new_threads(struct target_ops *target_ops)
237 {
238 struct target_ops *tb;
239
240 if (kvm != NULL)
241 return;
242
243 tb = find_target_beneath(target_ops);
244 if (tb->to_find_new_threads != NULL)
245 tb->to_find_new_threads(target_ops);
246 }
247
248 static char *
kgdb_trgt_pid_to_str(struct target_ops * target_ops __unused,ptid_t ptid)249 kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid)
250 {
251 return (kgdb_thr_pid_to_str(ptid));
252 }
253
254 static int
kgdb_trgt_thread_alive(struct target_ops * target_ops __unused,ptid_t ptid)255 kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid)
256 {
257 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
258 }
259
260 static LONGEST
kgdb_trgt_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)261 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object,
262 const char *annex, gdb_byte *readbuf,
263 const gdb_byte *writebuf,
264 ULONGEST offset, LONGEST len)
265 {
266 if (kvm != NULL) {
267 if (len == 0)
268 return (0);
269 if (writebuf != NULL)
270 return (kvm_write(kvm, offset, writebuf, len));
271 if (readbuf != NULL)
272 return (kvm_read(kvm, offset, readbuf, len));
273 }
274 return (ops->beneath->to_xfer_partial(ops->beneath, object, annex,
275 readbuf, writebuf, offset, len));
276 }
277
278 static void
kgdb_switch_to_thread(struct kthr * thr)279 kgdb_switch_to_thread(struct kthr *thr)
280 {
281 char buf[16];
282 CORE_ADDR thread_id;
283 char *err;
284
285 thread_id = thr->tid;
286 if (thread_id == 0)
287 error ("invalid tid");
288 snprintf(buf, sizeof(buf), "%lu", thread_id);
289 if (!gdb_thread_select(current_uiout, buf, &err))
290 error ("%s", err);
291 }
292
293 static void
kgdb_set_proc_cmd(char * arg,int from_tty)294 kgdb_set_proc_cmd (char *arg, int from_tty)
295 {
296 CORE_ADDR addr;
297 struct kthr *thr;
298
299 if (!arg)
300 error_no_arg ("proc address for the new context");
301
302 if (kvm == NULL)
303 error ("only supported for core file target");
304
305 addr = (CORE_ADDR) parse_and_eval_address (arg);
306
307 if (!INKERNEL (addr)) {
308 thr = kgdb_thr_lookup_pid((int)addr);
309 if (thr == NULL)
310 error ("invalid pid");
311 } else {
312 thr = kgdb_thr_lookup_paddr(addr);
313 if (thr == NULL)
314 error("invalid proc address");
315 }
316 kgdb_switch_to_thread(thr);
317 }
318
319 static void
kgdb_set_tid_cmd(char * arg,int from_tty)320 kgdb_set_tid_cmd (char *arg, int from_tty)
321 {
322 CORE_ADDR addr;
323 struct kthr *thr;
324
325 if (!arg)
326 error_no_arg ("Thread address for the new context");
327
328 addr = (CORE_ADDR) parse_and_eval_address (arg);
329 thr = kgdb_thr_lookup_taddr(addr);
330
331 if (thr == NULL)
332 error("invalid thread address");
333
334 kgdb_switch_to_thread(thr);
335 }
336
337 int fbsdcoreops_suppress_target = 1;
338
339 void
initialize_kgdb_target(void)340 initialize_kgdb_target(void)
341 {
342 kgdb_trgt_ops.to_magic = OPS_MAGIC;
343 kgdb_trgt_ops.to_shortname = "kernel";
344 kgdb_trgt_ops.to_longname = "kernel core dump file";
345 kgdb_trgt_ops.to_doc =
346 "Use a vmcore file as a target. Specify the filename of the vmcore file.";
347 kgdb_trgt_ops.to_stratum = process_stratum;
348 kgdb_trgt_ops.to_has_registers = default_child_has_registers;
349 kgdb_trgt_ops.to_has_memory = default_child_has_memory;
350 kgdb_trgt_ops.to_has_stack = default_child_has_stack;
351
352 kgdb_trgt_ops.to_open = kgdb_trgt_open;
353 kgdb_trgt_ops.to_close = kgdb_trgt_close;
354 kgdb_trgt_ops.to_attach = find_default_attach;
355 kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
356 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
357 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
358 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
359 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
360 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
361 /*
362 kgdb_trgt_ops.to_store_registers = NULL;
363 */
364 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
365 kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial;
366 add_target(&kgdb_trgt_ops);
367
368 add_com ("proc", class_obscure, kgdb_set_proc_cmd,
369 "Set current process context");
370 add_com ("tid", class_obscure, kgdb_set_tid_cmd,
371 "Set current thread context");
372 }
373