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_amd64.c,v 1.10 2008/05/01 20:36:48 jhb Exp $
29 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/thread.h>
33 #include <machine/pcb.h>
34 #include <machine/frame.h>
41 #include <gdbthread.h>
44 #include <frame-unwind.h>
45 #include <amd64-tdep.h>
50 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
51 struct frame_info *next_frame,
52 void **this_prologue_cache);
55 kgdb_trgt_fetch_registers(struct target_ops *target_ops, struct regcache *regcache, int regno)
60 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
62 regcache_raw_supply(regcache, regno, NULL);
67 * kt->pcb == 0 is a marker for "non-dumping kernel thread".
74 addr = kt->kaddr + offsetof(struct thread, td_sp);
75 kvm_read(kvm, addr, &sp, sizeof(sp));
88 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
89 warnx("kvm_read: %s", kvm_geterr(kvm));
90 memset(regs, 0, sizeof(regs));
92 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 7, ®s[0]);
93 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 6, ®s[1]);
94 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 5, ®s[2]);
95 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 4, ®s[3]);
96 regcache_raw_supply(regcache, AMD64_RBX_REGNUM, ®s[4]);
97 regcache_raw_supply(regcache, AMD64_RBP_REGNUM, ®s[5]);
98 regcache_raw_supply(regcache, AMD64_RIP_REGNUM, ®s[6]);
99 sp += 9 * sizeof(regs[0]);
100 regcache_raw_supply(regcache, AMD64_RSP_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));
109 regcache_raw_supply(regcache, AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx);
110 regcache_raw_supply(regcache, AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp);
111 regcache_raw_supply(regcache, AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp);
112 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12);
113 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13);
114 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14);
115 regcache_raw_supply(regcache, AMD64_R15_REGNUM, (char *)&pcb.pcb_r15);
116 regcache_raw_supply(regcache, AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip);
119 struct kgdb_frame_cache {
124 static int kgdb_trgt_frame_offset[20] = {
125 offsetof(struct trapframe, tf_rax),
126 offsetof(struct trapframe, tf_rbx),
127 offsetof(struct trapframe, tf_rcx),
128 offsetof(struct trapframe, tf_rdx),
129 offsetof(struct trapframe, tf_rsi),
130 offsetof(struct trapframe, tf_rdi),
131 offsetof(struct trapframe, tf_rbp),
132 offsetof(struct trapframe, tf_rsp),
133 offsetof(struct trapframe, tf_r8),
134 offsetof(struct trapframe, tf_r9),
135 offsetof(struct trapframe, tf_r10),
136 offsetof(struct trapframe, tf_r11),
137 offsetof(struct trapframe, tf_r12),
138 offsetof(struct trapframe, tf_r13),
139 offsetof(struct trapframe, tf_r14),
140 offsetof(struct trapframe, tf_r15),
141 offsetof(struct trapframe, tf_rip),
142 offsetof(struct trapframe, tf_rflags),
143 offsetof(struct trapframe, tf_cs),
144 offsetof(struct trapframe, tf_ss)
147 static struct kgdb_frame_cache *
148 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
150 enum bfd_endian byte_order = gdbarch_byte_order(get_frame_arch(next_frame));
151 char buf[MAX_REGISTER_SIZE];
152 struct kgdb_frame_cache *cache;
156 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
158 cache->pc = get_frame_address_in_block(next_frame);
159 frame_unwind_register(next_frame, AMD64_RSP_REGNUM, buf);
160 cache->sp = extract_unsigned_integer(buf,
161 register_size(get_frame_arch(next_frame), AMD64_RSP_REGNUM),
168 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
169 struct frame_id *this_id)
171 struct kgdb_frame_cache *cache;
173 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
174 *this_id = frame_id_build(cache->sp, cache->pc);
177 static struct value *
178 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
179 void **this_cache, int regnum)
182 struct kgdb_frame_cache *cache;
185 if (regnum < AMD64_RAX_REGNUM || regnum > AMD64_EFLAGS_REGNUM + 2)
186 return frame_unwind_got_register(next_frame, regnum, regnum);
188 ofs = kgdb_trgt_frame_offset[regnum];
190 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
192 addrp = cache->sp + ofs;
193 return frame_unwind_got_memory(next_frame, regnum, addrp);
196 const struct frame_unwind kgdb_trgt_trapframe_unwind = {
198 &kgdb_trgt_trapframe_this_id,
199 &kgdb_trgt_trapframe_prev_register,
200 .sniffer = kgdb_trgt_trapframe_sniffer
204 kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
205 struct frame_info *next_frame,
206 void **this_prologue_cache)
211 pc = get_frame_address_in_block(next_frame);
213 find_pc_partial_function(pc, &pname, NULL, NULL);
216 if (strcmp(pname, "calltrap") == 0 ||
217 strcmp(pname, "dblfault_handler") == 0 ||
218 strcmp(pname, "nmi_calltrap") == 0 ||
219 (pname[0] == 'X' && pname[1] != '_'))