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.5 2005/09/11 05:36:30 marcel Exp $
27 * $DragonFly: src/gnu/usr.bin/gdb/kgdb/trgt_i386.c,v 1.1 2006/03/07 15:48:11 corecode Exp $
30 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <machine/thread.h>
34 #include <sys/thread.h>
35 #include <machine/pcb.h>
36 #include <machine/frame.h>
43 #include <gdbthread.h>
46 #include <frame-unwind.h>
47 #include <i386-tdep.h>
52 kgdb_trgt_fetch_registers(int regno __unused)
57 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
62 * XXX Detect pure thread
63 * XXX handle pure thread dumping
70 addr = kt->kaddr + offsetof(struct thread, td_sp);
71 kvm_read(kvm, addr, &sp, sizeof(sp));
82 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
83 warnx("kvm_read: %s", kvm_geterr(kvm));
84 memset(regs, 0, sizeof(regs));
86 supply_register(I386_EDI_REGNUM, ®s[0]);
87 supply_register(I386_ESI_REGNUM, ®s[1]);
88 supply_register(I386_EBX_REGNUM, ®s[2]);
89 supply_register(I386_EBP_REGNUM, ®s[3]);
90 supply_register(I386_EIP_REGNUM, ®s[4]);
94 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
95 warnx("kvm_read: %s", kvm_geterr(kvm));
96 memset(&pcb, 0, sizeof(pcb));
98 supply_register(I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
99 supply_register(I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
100 supply_register(I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
101 supply_register(I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
102 supply_register(I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
103 supply_register(I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
107 kgdb_trgt_store_registers(int regno __unused)
109 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
112 struct kgdb_frame_cache {
118 static int kgdb_trgt_frame_offset[15] = {
119 offsetof(struct trapframe, tf_eax),
120 offsetof(struct trapframe, tf_ecx),
121 offsetof(struct trapframe, tf_edx),
122 offsetof(struct trapframe, tf_ebx),
123 offsetof(struct trapframe, tf_esp),
124 offsetof(struct trapframe, tf_ebp),
125 offsetof(struct trapframe, tf_esi),
126 offsetof(struct trapframe, tf_edi),
127 offsetof(struct trapframe, tf_eip),
128 offsetof(struct trapframe, tf_eflags),
129 offsetof(struct trapframe, tf_cs),
130 offsetof(struct trapframe, tf_ss),
131 offsetof(struct trapframe, tf_ds),
132 offsetof(struct trapframe, tf_es),
133 offsetof(struct trapframe, tf_fs)
136 static struct kgdb_frame_cache *
137 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
139 char buf[MAX_REGISTER_SIZE];
140 struct kgdb_frame_cache *cache;
145 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
147 cache->pc = frame_func_unwind(next_frame);
148 find_pc_partial_function(cache->pc, &pname, NULL, NULL);
149 cache->intrframe = (pname[0] == 'X') ? 1 : 0;
150 frame_unwind_register(next_frame, SP_REGNUM, buf);
151 cache->sp = extract_unsigned_integer(buf,
152 register_size(current_gdbarch, SP_REGNUM));
158 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
159 struct frame_id *this_id)
161 struct kgdb_frame_cache *cache;
163 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
164 *this_id = frame_id_build(cache->sp, cache->pc);
168 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
169 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
170 CORE_ADDR *addrp, int *realnump, void *valuep)
172 char dummy_valuep[MAX_REGISTER_SIZE];
173 struct kgdb_frame_cache *cache;
176 regsz = register_size(current_gdbarch, regnum);
179 valuep = dummy_valuep;
180 memset(valuep, 0, regsz);
186 if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
189 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
191 ofs = kgdb_trgt_frame_offset[regnum];
192 *addrp = cache->sp + ofs + (cache->intrframe ? 4 : 0);
195 * If we are in the kernel, we don't have esp stored in the
196 * trapframe, but we can calculate it simply by subtracting
197 * the size of the frame.
199 if (regnum == I386_ESP_REGNUM) {
202 frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
203 if (extract_unsigned_integer(buf, 4) != SEL_UPL) {
204 store_unsigned_integer(valuep, regsz, *addrp);
209 *lvalp = lval_memory;
210 target_read_memory(*addrp, valuep, regsz);
214 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
215 struct frame_info *next_frame,
216 void **this_prologue_cache)
221 pc = frame_unwind_address_in_block(next_frame);
223 find_pc_partial_function(pc, &pname, NULL, NULL);
226 if (strcmp(pname, "calltrap") == 0 ||
227 strcmp(pname, "dblfault_handler") == 0 ||
228 (pname[0] == 'X' && pname[1] != '_'))
230 /* printf("%s: %llx =%s\n", __func__, pc, pname); */
234 const struct frame_unwind kgdb_trgt_trapframe_unwind = {
236 &kgdb_trgt_trapframe_this_id,
237 &kgdb_trgt_trapframe_prev_register,
238 .sniffer = kgdb_trgt_trapframe_sniffer