2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/sysctl.h>
39 #include <readline/readline.h>
40 #include <readline/tilde.h>
43 #include <frame-unwind.h>
46 #include <gdbthread.h>
58 * We suppress the call to add_target() of core_ops in corelow.c because if
59 * there are multiple core_stratum targets, the find_core_target() function
60 * won't know which one to return and returns none. We need it to return
61 * our target. We only have to do that when we're building a cross-debugger
62 * because fbsd-threads.c is part of a native debugger and it too defines
63 * coreops_suppress_target with 1 as the initializer.
65 int coreops_suppress_target = 1;
68 static CORE_ADDR stoppcbs;
70 static void kgdb_core_cleanup(void *);
73 static struct target_ops kgdb_trgt_ops;
76 static char kvm_err[_POSIX2_LINE_MAX];
78 #define KERNOFF (kgdb_kernbase ())
79 #define PINKERNEL(x) ((x) >= KERNOFF)
84 static CORE_ADDR kernbase;
85 struct minimal_symbol *sym;
88 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
92 kernbase = SYMBOL_VALUE_ADDRESS (sym);
99 kgdb_trgt_open(char *filename, int from_tty)
101 struct cleanup *old_chain;
102 struct thread_info *ti;
108 target_preopen (from_tty);
110 error ("No vmcore file specified.");
112 error ("Can't open a vmcore without a kernel");
114 filename = tilde_expand (filename);
115 if (filename[0] != '/') {
116 temp = concat (current_directory, "/", filename, NULL);
121 old_chain = make_cleanup (xfree, filename);
123 nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
124 write_files ? O_RDWR : O_RDONLY, kvm_err);
126 error ("Failed to open vmcore: %s", kvm_err);
128 /* Don't free the filename now and close any previous vmcore. */
129 discard_cleanups(old_chain);
130 unpush_target(&kgdb_trgt_ops);
134 old_chain = make_cleanup(kgdb_core_cleanup, NULL);
136 ontop = !push_target (&kgdb_trgt_ops);
137 discard_cleanups (old_chain);
142 kt = kgdb_thr_init();
144 ti = add_thread(pid_to_ptid(kt->tid));
145 kt = kgdb_thr_next(kt);
148 inferior_ptid = pid_to_ptid(curkthr->tid);
151 /* XXX: fetch registers? */
153 flush_cached_frames();
154 select_frame (get_current_frame());
155 print_stack_frame(get_selected_frame(),
156 frame_relative_level(get_selected_frame()), 1);
159 "you won't be able to access this vmcore until you terminate\n\
160 your %s; do ``info files''", target_longname);
164 kgdb_trgt_close(int quitting)
168 inferior_ptid = null_ptid;
170 if (kvm_close(kvm) != 0)
171 warning("cannot close \"%s\": %s", vmcore,
176 if (kgdb_trgt_ops.to_sections) {
177 xfree(kgdb_trgt_ops.to_sections);
178 kgdb_trgt_ops.to_sections = NULL;
179 kgdb_trgt_ops.to_sections_end = NULL;
185 kgdb_core_cleanup(void *arg)
192 kgdb_trgt_detach(char *args, int from_tty)
196 error ("Too many arguments");
197 unpush_target(&kgdb_trgt_ops);
198 reinit_frame_cache();
200 printf_filtered("No vmcore file now.\n");
204 kgdb_trgt_extra_thread_info(struct thread_info *ti)
207 return (kgdb_thr_extra_thread_info(ptid_get_pid(ti->ptid)));
211 kgdb_trgt_files_info(struct target_ops *target)
214 printf_filtered ("\t`%s', ", vmcore);
216 printf_filtered ("file type %s.\n", "FreeBSD kernel vmcore");
220 kgdb_trgt_find_new_threads(void)
222 struct target_ops *tb;
227 tb = find_target_beneath(&kgdb_trgt_ops);
228 if (tb->to_find_new_threads != NULL)
229 tb->to_find_new_threads();
233 kgdb_trgt_pid_to_str(ptid_t ptid)
237 snprintf(buf, sizeof(buf), "Thread %d", ptid_get_pid(ptid));
242 kgdb_trgt_thread_alive(ptid_t ptid)
244 return (kgdb_thr_lookup_tid(ptid_get_pid(ptid)) != NULL);
248 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
249 struct mem_attrib *attrib, struct target_ops *target)
251 struct target_ops *tb;
257 return (kvm_read(kvm, memaddr, myaddr, len));
259 return (kvm_write(kvm, memaddr, myaddr, len));
261 tb = find_target_beneath(target);
262 return (tb->to_xfer_memory(memaddr, myaddr, len, write, attrib, tb));
266 kgdb_trgt_ignore_breakpoints(CORE_ADDR addr, char *contents)
273 kgdb_switch_to_thread(int tid)
278 thread_id = pid_to_thread_id(pid_to_ptid(tid));
280 error ("invalid tid");
281 snprintf(buf, sizeof(buf), "%d", thread_id);
282 gdb_thread_select(uiout, buf);
286 kgdb_set_proc_cmd (char *arg, int from_tty)
292 error_no_arg ("proc address for the new context");
295 error ("only supported for core file target");
297 addr = (CORE_ADDR) parse_and_eval_address (arg);
299 if (!PINKERNEL (addr)) {
300 thr = kgdb_thr_lookup_pid((int)addr);
302 error ("invalid pid");
304 thr = kgdb_thr_lookup_paddr(addr);
306 error("invalid proc address");
308 kgdb_switch_to_thread(thr->tid);
312 kgdb_set_tid_cmd (char *arg, int from_tty)
318 error_no_arg ("TID or thread address for the new context");
320 addr = (CORE_ADDR) parse_and_eval_address (arg);
322 if (kvm != NULL && PINKERNEL (addr)) {
323 thr = kgdb_thr_lookup_taddr(addr);
325 error("invalid thread address");
328 kgdb_switch_to_thread(addr);
331 int fbsdcoreops_suppress_target = 1;
334 initialize_kgdb_target(void)
337 kgdb_trgt_ops.to_magic = OPS_MAGIC;
338 kgdb_trgt_ops.to_shortname = "kernel";
339 kgdb_trgt_ops.to_longname = "kernel core dump file";
340 kgdb_trgt_ops.to_doc =
341 "Use a vmcore file as a target. Specify the filename of the vmcore file.";
342 kgdb_trgt_ops.to_stratum = core_stratum;
343 kgdb_trgt_ops.to_has_memory = 1;
344 kgdb_trgt_ops.to_has_registers = 1;
345 kgdb_trgt_ops.to_has_stack = 1;
347 kgdb_trgt_ops.to_open = kgdb_trgt_open;
348 kgdb_trgt_ops.to_close = kgdb_trgt_close;
349 kgdb_trgt_ops.to_attach = find_default_attach;
350 kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
351 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
352 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
353 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
354 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
355 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
356 kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
357 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
358 kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
359 kgdb_trgt_ops.to_insert_breakpoint = kgdb_trgt_ignore_breakpoints;
360 kgdb_trgt_ops.to_remove_breakpoint = kgdb_trgt_ignore_breakpoints;
362 add_target(&kgdb_trgt_ops);
364 add_com ("proc", class_obscure, kgdb_set_proc_cmd,
365 "Set current process context");
366 add_com ("tid", class_obscure, kgdb_set_tid_cmd,
367 "Set current thread context");
371 kgdb_trgt_stop_pcb(u_int cpuid, u_int pcbsz)
375 if (stoppcbs == 0 && !once) {
377 stoppcbs = kgdb_lookup("stoppcbs");
382 return (stoppcbs + pcbsz * cpuid);