gdb: Adjust makefiles for gdb 7.3
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt_i386.c
CommitLineData
8b6a428f
SS
1/*
2 * Copyright (c) 2004 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
14 *
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.
25 *
f17ea84b 26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt_i386.c,v 1.13 2008/09/27 15:58:37 kib Exp $
8b6a428f
SS
27 */
28
29#include <sys/cdefs.h>
30
31#include <sys/types.h>
32#include <machine/thread.h>
33#include <sys/thread.h>
f17ea84b 34#include <machine/globaldata.h>
8b6a428f
SS
35#include <machine/pcb.h>
36#include <machine/frame.h>
f17ea84b
SS
37#include <machine/segments.h>
38#include <machine/tss.h>
8b6a428f
SS
39#include <err.h>
40#include <kvm.h>
41#include <string.h>
42
43#include <defs.h>
44#include <target.h>
45#include <gdbthread.h>
46#include <inferior.h>
47#include <regcache.h>
48#include <frame-unwind.h>
49#include <i386-tdep.h>
50
51#include "kgdb.h"
52
f17ea84b
SS
53static int
54kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
55 struct frame_info *next_frame,
56 void **this_prologue_cache);
57
8b6a428f 58void
f17ea84b 59kgdb_trgt_fetch_registers(struct target_ops *target_ops, struct regcache *regcache, int regno)
8b6a428f
SS
60{
61 struct kthr *kt;
62 struct pcb pcb;
63
64 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
0c3d4888
SS
65 if (kt == NULL) {
66 regcache_raw_supply(regcache, regno, NULL);
8b6a428f 67 return;
0c3d4888 68 }
8b6a428f
SS
69
70 /*
3641b7ca 71 * kt->pcb == 0 is a marker for "non-dumping kernel thread".
8b6a428f 72 */
3641b7ca 73 if (kt->pcb == 0) {
8b6a428f
SS
74 uintptr_t regs[5];
75 uintptr_t addr;
76 uintptr_t sp;
77
78 addr = kt->kaddr + offsetof(struct thread, td_sp);
79 kvm_read(kvm, addr, &sp, sizeof(sp));
80 /*
81 * Stack is:
82 * -2 ret
83 * -1 popfl
84 * 0 popl %edi
85 * 1 popl %esi
86 * 2 popl %ebx
87 * 3 popl %ebp
88 * 4 ret
89 */
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));
93 }
0c3d4888
SS
94 regcache_raw_supply(regcache, I386_EDI_REGNUM, &regs[0]);
95 regcache_raw_supply(regcache, I386_ESI_REGNUM, &regs[1]);
96 regcache_raw_supply(regcache, I386_EBX_REGNUM, &regs[2]);
97 regcache_raw_supply(regcache, I386_EBP_REGNUM, &regs[3]);
98 regcache_raw_supply(regcache, I386_EIP_REGNUM, &regs[4]);
fb6749dc 99 sp += 7 * sizeof(regs[0]);
0c3d4888 100 regcache_raw_supply(regcache, I386_ESP_REGNUM, &sp);
8b6a428f
SS
101 return;
102 }
103
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));
107 }
0c3d4888
SS
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);
8b6a428f
SS
114}
115
f17ea84b
SS
116struct kgdb_tss_cache {
117 CORE_ADDR pc;
118 CORE_ADDR sp;
119 CORE_ADDR tss;
120};
121
122static 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)
138};
139
140/*
141 * If the current thread is executing on a CPU, fetch the common_tss
142 * for that CPU.
143 *
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.
147 */
148static CORE_ADDR
149kgdb_trgt_fetch_tss(void)
150{
151 struct kthr *kt;
152 struct segment_descriptor sd;
153 uintptr_t addr, tss;
154
155 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
156 if (kt == NULL || kt->gd == 0)
157 return (0);
158
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));
162 return (0);
163 }
164 if (sd.sd_type != SDT_SYS386BSY) {
165 warnx("descriptor is not a busy TSS");
166 return (0);
167 }
168 tss = kt->gd + offsetof(struct mdglobaldata, gd_common_tss);
169
170 return ((CORE_ADDR)tss);
171}
172
173static struct kgdb_tss_cache *
174kgdb_trgt_tss_cache(struct frame_info *next_frame, void **this_cache)
175{
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;
180
181 cache = *this_cache;
182 if (cache == NULL) {
183 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_tss_cache);
184 *this_cache = 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),
189 byte_order);
190 cache->tss = kgdb_trgt_fetch_tss();
191 }
192 return (cache);
193}
194
195static void
196kgdb_trgt_dblfault_this_id(struct frame_info *next_frame, void **this_cache,
197 struct frame_id *this_id)
198{
199 struct kgdb_tss_cache *cache;
200
201 cache = kgdb_trgt_tss_cache(next_frame, this_cache);
202 *this_id = frame_id_build(cache->sp, cache->pc);
203}
204
205static struct value *
206kgdb_trgt_dblfault_prev_register(struct frame_info *next_frame,
207 void **this_cache, int regnum)
8b6a428f 208{
f17ea84b
SS
209 CORE_ADDR addrp;
210 struct kgdb_tss_cache *cache;
211 int ofs;
212
213 if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
214 return frame_unwind_got_register(next_frame, regnum, regnum);
215
216 ofs = kgdb_trgt_tss_offset[regnum];
217
218 cache = kgdb_trgt_tss_cache(next_frame, this_cache);
219 if (cache->tss == 0)
220 return frame_unwind_got_register(next_frame, regnum, regnum);
221
222 addrp = cache->tss + ofs;
223 return frame_unwind_got_memory(next_frame, regnum, addrp);
8b6a428f
SS
224}
225
0c3d3a6f
JM
226static enum unwind_stop_reason
227kgdb_trgt_dblfault_unwind_reason(struct frame_info *next_frame,
228 void **this_cache)
229{
230 /* XXX marino : populate logic to determine unwind stoppage */
231 return UNWIND_NO_REASON;
232}
233
f17ea84b
SS
234static const struct frame_unwind kgdb_trgt_dblfault_unwind = {
235 NORMAL_FRAME,
0c3d3a6f 236 &kgdb_trgt_dblfault_unwind_reason,
f17ea84b
SS
237 &kgdb_trgt_dblfault_this_id,
238 &kgdb_trgt_dblfault_prev_register,
239 .sniffer = kgdb_trgt_trapframe_sniffer
240};
241
8b6a428f 242struct kgdb_frame_cache {
f17ea84b 243 int frame_type;
8b6a428f
SS
244 CORE_ADDR pc;
245 CORE_ADDR sp;
246};
f17ea84b
SS
247#define FT_NORMAL 1
248#define FT_INTRFRAME 2
249/*#define FT_INTRTRAPFRAME 3*/
250#define FT_TIMERFRAME 4
939fa31e 251#define FT_CALLTRAP 5
8b6a428f
SS
252
253static int kgdb_trgt_frame_offset[15] = {
254 offsetof(struct trapframe, tf_eax),
255 offsetof(struct trapframe, tf_ecx),
256 offsetof(struct trapframe, tf_edx),
257 offsetof(struct trapframe, tf_ebx),
258 offsetof(struct trapframe, tf_esp),
259 offsetof(struct trapframe, tf_ebp),
260 offsetof(struct trapframe, tf_esi),
261 offsetof(struct trapframe, tf_edi),
262 offsetof(struct trapframe, tf_eip),
263 offsetof(struct trapframe, tf_eflags),
264 offsetof(struct trapframe, tf_cs),
265 offsetof(struct trapframe, tf_ss),
266 offsetof(struct trapframe, tf_ds),
267 offsetof(struct trapframe, tf_es),
268 offsetof(struct trapframe, tf_fs)
269};
270
271static struct kgdb_frame_cache *
272kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
273{
8b6a428f
SS
274 struct kgdb_frame_cache *cache;
275 char *pname;
276
277 cache = *this_cache;
278 if (cache == NULL) {
279 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
280 *this_cache = cache;
0c3d4888 281 cache->pc = get_frame_address_in_block(next_frame);
939fa31e 282 cache->sp = get_frame_sp(next_frame);
8b6a428f 283 find_pc_partial_function(cache->pc, &pname, NULL, NULL);
cf759448 284
939fa31e
MD
285 if (strcmp(pname, "calltrap") == 0)
286 cache->frame_type = FT_CALLTRAP;
287 else if (pname[0] != 'X')
f17ea84b
SS
288 cache->frame_type = FT_NORMAL;
289 else if (strcmp(pname, "Xtimerint") == 0)
290 cache->frame_type = FT_TIMERFRAME;
291 /* else if (strcmp(pname, "Xcpustop") == 0 ||
292 strcmp(pname, "Xrendezvous") == 0 ||
293 strcmp(pname, "Xipi_intr_bitmap_handler") == 0 ||
294 strcmp(pname, "Xlazypmap") == 0)
295 cache->frame_type = FT_INTRTRAPFRAME;
296 */
297 else
298 cache->frame_type = FT_INTRFRAME;
8b6a428f
SS
299 }
300 return (cache);
301}
302
303static void
304kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
305 struct frame_id *this_id)
306{
307 struct kgdb_frame_cache *cache;
308
309 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
310 *this_id = frame_id_build(cache->sp, cache->pc);
311}
312
f17ea84b 313static struct value *
8b6a428f 314kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
f17ea84b 315 void **this_cache, int regnum)
8b6a428f 316{
f17ea84b 317 CORE_ADDR addrp;
8b6a428f 318 struct kgdb_frame_cache *cache;
f17ea84b 319 int ofs;
8b6a428f
SS
320
321 if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
f17ea84b
SS
322 return frame_unwind_got_register(next_frame, regnum, regnum);
323
324 ofs = kgdb_trgt_frame_offset[regnum] + 4;
8b6a428f
SS
325
326 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
939fa31e 327
f17ea84b
SS
328 switch (cache->frame_type) {
329 case FT_NORMAL:
330 break;
331 case FT_INTRFRAME:
332 ofs += 4;
333 break;
334 case FT_TIMERFRAME:
335 break;
336 /*
337 case FT_INTRTRAPFRAME:
338 ofs -= ofs_fix;
339 break;
340 */
939fa31e
MD
341 case FT_CALLTRAP:
342 ofs += 0;
343 break;
f17ea84b
SS
344 default:
345 fprintf_unfiltered(gdb_stderr, "Correct FT_XXX frame offsets "
346 "for %d\n", cache->frame_type);
347 break;
348 }
8b6a428f 349
f17ea84b 350 addrp = cache->sp + ofs;
8b6a428f 351
f17ea84b 352#if 0
8b6a428f
SS
353 /*
354 * If we are in the kernel, we don't have esp stored in the
355 * trapframe, but we can calculate it simply by subtracting
356 * the size of the frame.
357 */
358 if (regnum == I386_ESP_REGNUM) {
359 char buf[4];
360
361 frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
f17ea84b
SS
362 if (extract_unsigned_integer(buf, 4, byte_order) != SEL_UPL)
363 return frame_unwind_got_address(next_frame, regnum, addrp);
364 /* else FALLTHROUGH */
8b6a428f 365 }
f17ea84b
SS
366#endif
367
368 return frame_unwind_got_memory(next_frame, regnum, addrp);
8b6a428f
SS
369}
370
0c3d3a6f
JM
371static enum unwind_stop_reason
372kgdb_trgt_trapframe_unwind_reason(struct frame_info *next_frame,
373 void **this_cache)
374{
375 /* XXX marino : populate logic to determine unwind stoppage */
376 return UNWIND_NO_REASON;
377}
378
f17ea84b
SS
379const struct frame_unwind kgdb_trgt_trapframe_unwind = {
380 NORMAL_FRAME,
0c3d3a6f 381 &kgdb_trgt_trapframe_unwind_reason,
f17ea84b
SS
382 &kgdb_trgt_trapframe_this_id,
383 &kgdb_trgt_trapframe_prev_register,
384 .sniffer = kgdb_trgt_trapframe_sniffer
385};
386
8b6a428f
SS
387static int
388kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
389 struct frame_info *next_frame,
390 void **this_prologue_cache)
391{
392 char *pname;
393 CORE_ADDR pc;
394
f17ea84b 395 pc = get_frame_address_in_block(next_frame);
8b6a428f
SS
396 pname = NULL;
397 find_pc_partial_function(pc, &pname, NULL, NULL);
398 if (pname == NULL)
3641b7ca 399 return (0);
f17ea84b
SS
400
401 /*
402 * This is a combined sniffer, since only the
403 * function names change.
404 */
405
406 /*
407 * If we're the sniffer for a trapframe, deal with
408 * all these function names.
409 */
410 if (self == &kgdb_trgt_trapframe_unwind &&
411 (strcmp(pname, "calltrap") == 0 ||
412 (pname[0] == 'X' && pname[1] != '_')))
413 return (1);
414
415 /*
416 * If we're a double fault sniffer, only look for
417 * the double fault name.
418 */
419 if(self == &kgdb_trgt_dblfault_unwind &&
420 strcmp(pname, "dblfault_handler") == 0)
421 return (1);
422
8b6a428f 423 /* printf("%s: %llx =%s\n", __func__, pc, pname); */
f17ea84b 424 return (0);
8b6a428f 425}