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>
38 #include <gdbthread.h>
45 static struct target_ops kgdb_trgt_ops;
47 #define KERNOFF (kgdb_kernbase ())
48 #define INKERNEL(x) ((x) >= KERNOFF)
53 static CORE_ADDR kernbase;
54 struct minimal_symbol *sym;
58 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
62 kernbase = SYMBOL_VALUE_ADDRESS (sym);
69 kgdb_trgt_extra_thread_info(struct thread_info *ti)
71 return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid)));
75 kgdb_trgt_find_new_threads(void)
80 kgdb_trgt_pid_to_str(ptid_t ptid)
84 snprintf(buf, sizeof(buf), "PID %5d TID %5ld", ptid_get_pid(ptid),
90 kgdb_trgt_thread_alive(ptid_t ptid)
92 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
96 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
97 struct mem_attrib *attrib __unused, struct target_ops *target __unused)
103 return (kvm_read(kvm, memaddr, myaddr, len));
105 return (kvm_write(kvm, memaddr, myaddr, len));
109 kgdb_switch_to_thread(struct kthr *thr)
111 if (thr->tid == ptid_get_tid(inferior_ptid))
114 inferior_ptid = ptid_build(thr->pid, 0, thr->tid);
115 flush_cached_frames ();
116 registers_changed ();
117 stop_pc = read_pc ();
118 select_frame (get_current_frame ());
122 kgdb_set_proc_cmd (char *arg, int from_tty)
128 error_no_arg ("proc address for the new context");
131 error ("no kernel core file");
133 addr = (CORE_ADDR) parse_and_eval_address (arg);
135 if (!INKERNEL (addr)) {
136 thr = kgdb_thr_lookup_pid((int)addr);
138 error ("invalid pid");
140 thr = kgdb_thr_lookup_paddr(addr);
142 error("invalid proc address");
144 kgdb_switch_to_thread(thr);
148 kgdb_set_tid_cmd (char *arg, int from_tty)
154 error_no_arg ("TID or thread address for the new context");
157 error ("no kernel core file");
159 addr = (CORE_ADDR) parse_and_eval_address (arg);
161 if (!INKERNEL (addr)) {
162 thr = kgdb_thr_lookup_tid((int)addr);
164 error ("invalid TID");
166 thr = kgdb_thr_lookup_taddr(addr);
168 error("invalid thread address");
170 kgdb_switch_to_thread(thr);
177 struct thread_info *ti;
179 kgdb_trgt_ops.to_magic = OPS_MAGIC;
180 kgdb_trgt_ops.to_shortname = "kernel";
181 kgdb_trgt_ops.to_longname = "kernel core files.";
182 kgdb_trgt_ops.to_doc = "Kernel core files.";
183 kgdb_trgt_ops.to_stratum = thread_stratum;
184 kgdb_trgt_ops.to_has_memory = 1;
185 kgdb_trgt_ops.to_has_registers = 1;
186 kgdb_trgt_ops.to_has_stack = 1;
188 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
189 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
190 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
191 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
192 kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
193 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
194 kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
195 add_target(&kgdb_trgt_ops);
196 push_target(&kgdb_trgt_ops);
198 kt = kgdb_thr_first();
200 ti = add_thread(ptid_build(kt->pid, 0, kt->tid));
201 kt = kgdb_thr_next(kt);
203 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
204 add_com ("proc", class_obscure, kgdb_set_proc_cmd,
205 "Set current process context");
206 add_com ("tid", class_obscure, kgdb_set_tid_cmd,
207 "Set current process context");