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.
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.12 2008/05/01 20:36:48 jhb Exp $
29 #include <sys/cdefs.h>
31 #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>
57 static void kgdb_core_cleanup(void *);
60 static struct target_ops kgdb_trgt_ops;
63 static char kvm_err[_POSIX2_LINE_MAX];
65 #define KERNOFF (kgdb_kernbase ())
66 #define INKERNEL(x) ((x) >= KERNOFF)
71 static CORE_ADDR kernbase;
72 struct minimal_symbol *sym;
75 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
79 kernbase = SYMBOL_VALUE_ADDRESS (sym);
86 kgdb_trgt_open(char *filename, int from_tty)
88 struct cleanup *old_chain;
90 struct inferior *inf8;
91 struct program_space *pspace;
94 int first_inferior = 1;
96 target_preopen (from_tty);
98 error ("No vmcore file specified.");
100 error ("Can't open a vmcore without a kernel");
102 filename = tilde_expand (filename);
103 if (filename[0] != '/') {
104 temp = concat (current_directory, "/", filename, NULL);
109 old_chain = make_cleanup (xfree, filename);
111 nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
112 write_files ? O_RDWR : O_RDONLY, kvm_err);
114 error ("Failed to open vmcore: %s", kvm_err);
116 /* Don't free the filename now and close any previous vmcore. */
117 discard_cleanups(old_chain);
118 unpush_target(&kgdb_trgt_ops);
122 old_chain = make_cleanup(kgdb_core_cleanup, NULL);
124 push_target (&kgdb_trgt_ops);
125 discard_cleanups (old_chain);
130 kt = kgdb_thr_init();
132 if (!in_inferior_list(kt->pid)) {
133 if (first_inferior) {
135 inf8 = current_inferior();
137 inferior_appeared (inf8, kt->pid);
138 pspace = current_program_space;
140 pspace->ebfd_mtime = 0;
142 inf8 = add_inferior(kt->pid);
143 pspace = add_program_space(new_address_space());
144 pspace->symfile_object_file = symfile_objfile;
145 pspace->objfiles = object_files;
146 pspace->target_sections =
147 current_program_space->target_sections;
149 inf8->pspace = pspace;
150 inf8->aspace = pspace->aspace;
152 add_thread(ptid_build(kt->pid, 0, kt->tid));
153 kt = kgdb_thr_next(kt);
156 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
158 frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind);
160 /* XXX: fetch registers? */
162 reinit_frame_cache();
163 select_frame (get_current_frame());
164 print_stack_frame(get_selected_frame(NULL),
165 frame_relative_level(get_selected_frame(NULL)), 1);
169 kgdb_trgt_close(int quitting)
173 inferior_ptid = null_ptid;
175 if (kvm_close(kvm) != 0)
176 warning("cannot close \"%s\": %s", vmcore,
185 kgdb_core_cleanup(void *arg)
192 kgdb_trgt_detach(struct target_ops *target, char *args, int from_tty)
196 error ("Too many arguments");
197 unpush_target(target);
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_tid(ti->ptid)));
211 kgdb_trgt_files_info(struct target_ops *target)
214 printf_filtered ("\t`%s', ", vmcore);
216 printf_filtered ("file type %s.\n", "DragonFly kernel vmcore");
220 kgdb_trgt_find_new_threads(struct target_ops *target_ops)
222 struct target_ops *tb;
227 tb = find_target_beneath(target_ops);
228 if (tb->to_find_new_threads != NULL)
229 tb->to_find_new_threads(target_ops);
233 kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid)
235 return (kgdb_thr_pid_to_str(ptid));
239 kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid)
241 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
245 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object,
246 const char *annex, gdb_byte *readbuf,
247 const gdb_byte *writebuf,
248 ULONGEST offset, LONGEST len)
253 if (writebuf != NULL)
254 return (kvm_write(kvm, offset, writebuf, len));
256 return (kvm_read(kvm, offset, readbuf, len));
258 return (ops->beneath->to_xfer_partial(ops->beneath, object, annex,
259 readbuf, writebuf, offset, len));
263 kgdb_switch_to_thread(struct kthr *thr)
269 thread_id = pid_to_thread_id(ptid_build(thr->pid, 0, thr->tid));
271 error ("invalid tid");
272 snprintf(buf, sizeof(buf), "%d", thread_id);
273 if (!gdb_thread_select(uiout, buf, &err))
278 kgdb_set_proc_cmd (char *arg, int from_tty)
284 error_no_arg ("proc address for the new context");
287 error ("only supported for core file target");
289 addr = (CORE_ADDR) parse_and_eval_address (arg);
291 if (!INKERNEL (addr)) {
292 thr = kgdb_thr_lookup_pid((int)addr);
294 error ("invalid pid");
296 thr = kgdb_thr_lookup_paddr(addr);
298 error("invalid proc address");
300 kgdb_switch_to_thread(thr);
304 kgdb_set_tid_cmd (char *arg, int from_tty)
310 error_no_arg ("Thread address for the new context");
312 addr = (CORE_ADDR) parse_and_eval_address (arg);
313 thr = kgdb_thr_lookup_taddr(addr);
316 error("invalid thread address");
318 kgdb_switch_to_thread(thr);
321 int fbsdcoreops_suppress_target = 1;
324 initialize_kgdb_target(void)
326 kgdb_trgt_ops.to_magic = OPS_MAGIC;
327 kgdb_trgt_ops.to_shortname = "kernel";
328 kgdb_trgt_ops.to_longname = "kernel core dump file";
329 kgdb_trgt_ops.to_doc =
330 "Use a vmcore file as a target. Specify the filename of the vmcore file.";
331 kgdb_trgt_ops.to_stratum = process_stratum;
332 kgdb_trgt_ops.to_has_registers = default_child_has_registers;
333 kgdb_trgt_ops.to_has_memory = default_child_has_memory;
334 kgdb_trgt_ops.to_has_stack = default_child_has_stack;
336 kgdb_trgt_ops.to_open = kgdb_trgt_open;
337 kgdb_trgt_ops.to_close = kgdb_trgt_close;
338 kgdb_trgt_ops.to_attach = find_default_attach;
339 kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
340 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
341 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
342 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
343 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
344 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
346 kgdb_trgt_ops.to_store_registers = NULL;
348 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
349 kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial;
350 add_target(&kgdb_trgt_ops);
352 add_com ("proc", class_obscure, kgdb_set_proc_cmd,
353 "Set current process context");
354 add_com ("tid", class_obscure, kgdb_set_tid_cmd,
355 "Set current thread context");