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 AUTHORS ``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 AUTHORS 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_i386.c,v 1.13 2008/09/27 15:58:37 kib Exp $
29 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <machine/thread.h>
33 #include <sys/thread.h>
34 #include <machine/globaldata.h>
35 #include <machine/pcb.h>
36 #include <machine/frame.h>
37 #include <machine/segments.h>
38 #include <machine/tss.h>
45 #include <gdbthread.h>
48 #include <frame-unwind.h>
49 #include <i386-tdep.h>
54 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
55 struct frame_info *next_frame,
56 void **this_prologue_cache);
59 kgdb_trgt_fetch_registers(struct target_ops *target_ops, struct regcache *regcache, int regno)
64 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
66 regcache_raw_supply(regcache, regno, NULL);
71 * kt->pcb == 0 is a marker for "non-dumping kernel thread".
78 addr = kt->kaddr + offsetof(struct thread, td_sp);
79 kvm_read(kvm, addr, &sp, sizeof(sp));
90 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
91 warnx("kvm_read: %s", kvm_geterr(kvm));
92 memset(regs, 0, sizeof(regs));
94 regcache_raw_supply(regcache, I386_EDI_REGNUM, ®s[0]);
95 regcache_raw_supply(regcache, I386_ESI_REGNUM, ®s[1]);
96 regcache_raw_supply(regcache, I386_EBX_REGNUM, ®s[2]);
97 regcache_raw_supply(regcache, I386_EBP_REGNUM, ®s[3]);
98 regcache_raw_supply(regcache, I386_EIP_REGNUM, ®s[4]);
99 sp += 7 * sizeof(regs[0]);
100 regcache_raw_supply(regcache, I386_ESP_REGNUM, &sp);
104 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
105 warnx("kvm_read: %s", kvm_geterr(kvm));
106 memset(&pcb, 0, sizeof(pcb));
108 regcache_raw_supply(regcache, I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
109 regcache_raw_supply(regcache, I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
110 regcache_raw_supply(regcache, I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
111 regcache_raw_supply(regcache, I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
112 regcache_raw_supply(regcache, I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
113 regcache_raw_supply(regcache, I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
116 struct kgdb_tss_cache {
122 static int kgdb_trgt_tss_offset[15] = {
123 offsetof(struct i386tss, tss_eax),
124 offsetof(struct i386tss, tss_ecx),
125 offsetof(struct i386tss, tss_edx),
126 offsetof(struct i386tss, tss_ebx),
127 offsetof(struct i386tss, tss_esp),
128 offsetof(struct i386tss, tss_ebp),
129 offsetof(struct i386tss, tss_esi),
130 offsetof(struct i386tss, tss_edi),
131 offsetof(struct i386tss, tss_eip),
132 offsetof(struct i386tss, tss_eflags),
133 offsetof(struct i386tss, tss_cs),
134 offsetof(struct i386tss, tss_ss),
135 offsetof(struct i386tss, tss_ds),
136 offsetof(struct i386tss, tss_es),
137 offsetof(struct i386tss, tss_fs)
141 * If the current thread is executing on a CPU, fetch the common_tss
144 * This is painful because 'struct pcpu' is variant sized, so we can't
145 * use it. Instead, we lookup the GDT selector for this CPU and
146 * extract the base of the TSS from there.
149 kgdb_trgt_fetch_tss(void)
152 struct segment_descriptor sd;
155 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
156 if (kt == NULL || kt->gd == 0)
159 addr = kt->gd + offsetof(struct mdglobaldata, gd_common_tssd);
160 if (kvm_read(kvm, addr, &sd, sizeof(sd)) != sizeof(sd)) {
161 warnx("kvm_read: %s", kvm_geterr(kvm));
164 if (sd.sd_type != SDT_SYS386BSY) {
165 warnx("descriptor is not a busy TSS");
168 tss = kt->gd + offsetof(struct mdglobaldata, gd_common_tss);
170 return ((CORE_ADDR)tss);
173 static struct kgdb_tss_cache *
174 kgdb_trgt_tss_cache(struct frame_info *next_frame, void **this_cache)
176 struct gdbarch *gdbarch = get_frame_arch(next_frame);
177 enum bfd_endian byte_order = gdbarch_byte_order(gdbarch);
178 char buf[MAX_REGISTER_SIZE];
179 struct kgdb_tss_cache *cache;
183 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_tss_cache);
185 cache->pc = get_frame_address_in_block(next_frame);
186 frame_unwind_register(next_frame, I386_ESP_REGNUM, buf);
187 cache->sp = extract_unsigned_integer(buf,
188 register_size(gdbarch, I386_ESP_REGNUM),
190 cache->tss = kgdb_trgt_fetch_tss();
196 kgdb_trgt_dblfault_this_id(struct frame_info *next_frame, void **this_cache,
197 struct frame_id *this_id)
199 struct kgdb_tss_cache *cache;
201 cache = kgdb_trgt_tss_cache(next_frame, this_cache);
202 *this_id = frame_id_build(cache->sp, cache->pc);
205 static struct value *
206 kgdb_trgt_dblfault_prev_register(struct frame_info *next_frame,
207 void **this_cache, int regnum)
210 struct kgdb_tss_cache *cache;
213 if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
214 return frame_unwind_got_register(next_frame, regnum, regnum);
216 ofs = kgdb_trgt_tss_offset[regnum];
218 cache = kgdb_trgt_tss_cache(next_frame, this_cache);
220 return frame_unwind_got_register(next_frame, regnum, regnum);
222 addrp = cache->tss + ofs;
223 return frame_unwind_got_memory(next_frame, regnum, addrp);
226 static const struct frame_unwind kgdb_trgt_dblfault_unwind = {
228 &kgdb_trgt_dblfault_this_id,
229 &kgdb_trgt_dblfault_prev_register,
230 .sniffer = kgdb_trgt_trapframe_sniffer
233 struct kgdb_frame_cache {
239 #define FT_INTRFRAME 2
240 /*#define FT_INTRTRAPFRAME 3*/
241 #define FT_TIMERFRAME 4
243 static int kgdb_trgt_frame_offset[15] = {
244 offsetof(struct trapframe, tf_eax),
245 offsetof(struct trapframe, tf_ecx),
246 offsetof(struct trapframe, tf_edx),
247 offsetof(struct trapframe, tf_ebx),
248 offsetof(struct trapframe, tf_esp),
249 offsetof(struct trapframe, tf_ebp),
250 offsetof(struct trapframe, tf_esi),
251 offsetof(struct trapframe, tf_edi),
252 offsetof(struct trapframe, tf_eip),
253 offsetof(struct trapframe, tf_eflags),
254 offsetof(struct trapframe, tf_cs),
255 offsetof(struct trapframe, tf_ss),
256 offsetof(struct trapframe, tf_ds),
257 offsetof(struct trapframe, tf_es),
258 offsetof(struct trapframe, tf_fs)
261 static struct kgdb_frame_cache *
262 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
264 enum bfd_endian byte_order = gdbarch_byte_order(get_frame_arch(next_frame));
265 char buf[MAX_REGISTER_SIZE];
266 struct kgdb_frame_cache *cache;
271 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
273 cache->pc = get_frame_address_in_block(next_frame);
274 find_pc_partial_function(cache->pc, &pname, NULL, NULL);
277 cache->frame_type = FT_NORMAL;
278 else if (strcmp(pname, "Xtimerint") == 0)
279 cache->frame_type = FT_TIMERFRAME;
280 /* else if (strcmp(pname, "Xcpustop") == 0 ||
281 strcmp(pname, "Xrendezvous") == 0 ||
282 strcmp(pname, "Xipi_intr_bitmap_handler") == 0 ||
283 strcmp(pname, "Xlazypmap") == 0)
284 cache->frame_type = FT_INTRTRAPFRAME;
287 cache->frame_type = FT_INTRFRAME;
289 frame_unwind_register(next_frame, I386_ESP_REGNUM, buf);
290 cache->sp = extract_unsigned_integer(buf,
291 register_size(get_frame_arch(next_frame), I386_ESP_REGNUM),
298 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
299 struct frame_id *this_id)
301 struct kgdb_frame_cache *cache;
303 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
304 *this_id = frame_id_build(cache->sp, cache->pc);
307 static struct value *
308 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
309 void **this_cache, int regnum)
312 struct kgdb_frame_cache *cache;
315 if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
316 return frame_unwind_got_register(next_frame, regnum, regnum);
318 ofs = kgdb_trgt_frame_offset[regnum] + 4;
320 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
321 switch (cache->frame_type) {
330 case FT_INTRTRAPFRAME:
335 fprintf_unfiltered(gdb_stderr, "Correct FT_XXX frame offsets "
336 "for %d\n", cache->frame_type);
340 addrp = cache->sp + ofs;
344 * If we are in the kernel, we don't have esp stored in the
345 * trapframe, but we can calculate it simply by subtracting
346 * the size of the frame.
348 if (regnum == I386_ESP_REGNUM) {
351 frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
352 if (extract_unsigned_integer(buf, 4, byte_order) != SEL_UPL)
353 return frame_unwind_got_address(next_frame, regnum, addrp);
354 /* else FALLTHROUGH */
358 return frame_unwind_got_memory(next_frame, regnum, addrp);
361 const struct frame_unwind kgdb_trgt_trapframe_unwind = {
363 &kgdb_trgt_trapframe_this_id,
364 &kgdb_trgt_trapframe_prev_register,
365 .sniffer = kgdb_trgt_trapframe_sniffer
369 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
370 struct frame_info *next_frame,
371 void **this_prologue_cache)
376 pc = get_frame_address_in_block(next_frame);
378 find_pc_partial_function(pc, &pname, NULL, NULL);
383 * This is a combined sniffer, since only the
384 * function names change.
388 * If we're the sniffer for a trapframe, deal with
389 * all these function names.
391 if (self == &kgdb_trgt_trapframe_unwind &&
392 (strcmp(pname, "calltrap") == 0 ||
393 (pname[0] == 'X' && pname[1] != '_')))
397 * If we're a double fault sniffer, only look for
398 * the double fault name.
400 if(self == &kgdb_trgt_dblfault_unwind &&
401 strcmp(pname, "dblfault_handler") == 0)
404 /* printf("%s: %llx =%s\n", __func__, pc, pname); */