gdb: add our changes
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt_amd64.c
CommitLineData
301e0930
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 *
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt_amd64.c,v 1.6 2005/09/28 07:40:27 peter Exp $
48d74c1d 27 * $DragonFly: src/gnu/usr.bin/gdb/kgdb/trgt_amd64.c,v 1.2 2008/01/31 14:30:52 corecode Exp $
301e0930
SS
28 */
29
30#include <sys/cdefs.h>
31
32#include <sys/types.h>
5f9c1196 33#include <sys/thread.h>
301e0930
SS
34#include <machine/pcb.h>
35#include <machine/frame.h>
36#include <err.h>
37#include <kvm.h>
38#include <string.h>
39
40#include <defs.h>
41#include <target.h>
42#include <gdbthread.h>
43#include <inferior.h>
44#include <regcache.h>
45#include <frame-unwind.h>
46#include <amd64-tdep.h>
47
48#include "kgdb.h"
49
50void
48d74c1d 51kgdb_trgt_fetch_registers(struct regcache *regcache, int regno)
301e0930
SS
52{
53 struct kthr *kt;
54 struct pcb pcb;
55
56 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
48d74c1d
SS
57 if (kt == NULL) {
58 regcache_raw_supply(regcache, regno, NULL);
301e0930 59 return;
48d74c1d 60 }
5f9c1196
MD
61
62 /*
63 * kt->pcb == 0 is a marker for "non-dumping kernel thread".
64 */
65 if (kt->pcb == 0) {
66 uintptr_t regs[7];
67 uintptr_t addr;
68 uintptr_t sp;
69
70 addr = kt->kaddr + offsetof(struct thread, td_sp);
71 kvm_read(kvm, addr, &sp, sizeof(sp));
72 /*
73 * Stack is:
74 * -2 ret
75 * -1 popfq
76 * 0 popq %r15 edi
77 * 1 popq %r14
78 * 2 popq %r13
79 * 3 popq %r12
80 * 4 popq %rbx
81 * 5 popq %rbp
82 * 6 ret
83 */
84 if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
85 warnx("kvm_read: %s", kvm_geterr(kvm));
86 memset(regs, 0, sizeof(regs));
87 }
88 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 7, &regs[0]);
89 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 6, &regs[1]);
90 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 5, &regs[2]);
91 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 4, &regs[3]);
92 regcache_raw_supply(regcache, AMD64_RBX_REGNUM, &regs[4]);
93 regcache_raw_supply(regcache, AMD64_RBP_REGNUM, &regs[5]);
94 regcache_raw_supply(regcache, AMD64_RIP_REGNUM, &regs[6]);
95 sp += 9 * sizeof(regs[0]);
96 regcache_raw_supply(regcache, AMD64_RSP_REGNUM, &sp);
97 return;
98 }
99
301e0930
SS
100 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
101 warnx("kvm_read: %s", kvm_geterr(kvm));
102 memset(&pcb, 0, sizeof(pcb));
103 }
104
48d74c1d
SS
105 regcache_raw_supply(regcache, AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx);
106 regcache_raw_supply(regcache, AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp);
107 regcache_raw_supply(regcache, AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp);
108 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12);
109 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13);
110 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14);
111 regcache_raw_supply(regcache, AMD64_R15_REGNUM, (char *)&pcb.pcb_r15);
112 regcache_raw_supply(regcache, AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip);
301e0930
SS
113}
114
115void
48d74c1d 116kgdb_trgt_store_registers(struct regcache *regcache, int regno __unused)
301e0930
SS
117{
118 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
119}
120
121struct kgdb_frame_cache {
122 CORE_ADDR pc;
123 CORE_ADDR sp;
124};
125
126static int kgdb_trgt_frame_offset[20] = {
127 offsetof(struct trapframe, tf_rax),
128 offsetof(struct trapframe, tf_rbx),
129 offsetof(struct trapframe, tf_rcx),
130 offsetof(struct trapframe, tf_rdx),
131 offsetof(struct trapframe, tf_rsi),
132 offsetof(struct trapframe, tf_rdi),
133 offsetof(struct trapframe, tf_rbp),
134 offsetof(struct trapframe, tf_rsp),
135 offsetof(struct trapframe, tf_r8),
136 offsetof(struct trapframe, tf_r9),
137 offsetof(struct trapframe, tf_r10),
138 offsetof(struct trapframe, tf_r11),
139 offsetof(struct trapframe, tf_r12),
140 offsetof(struct trapframe, tf_r13),
141 offsetof(struct trapframe, tf_r14),
142 offsetof(struct trapframe, tf_r15),
143 offsetof(struct trapframe, tf_rip),
144 offsetof(struct trapframe, tf_rflags),
145 offsetof(struct trapframe, tf_cs),
146 offsetof(struct trapframe, tf_ss)
147};
148
149static struct kgdb_frame_cache *
150kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
151{
152 char buf[MAX_REGISTER_SIZE];
153 struct kgdb_frame_cache *cache;
154
155 cache = *this_cache;
156 if (cache == NULL) {
157 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
158 *this_cache = cache;
48d74c1d
SS
159 cache->pc = get_frame_address_in_block(next_frame);
160 frame_unwind_register(next_frame, AMD64_RSP_REGNUM, buf);
301e0930 161 cache->sp = extract_unsigned_integer(buf,
48d74c1d 162 register_size(current_gdbarch, AMD64_RSP_REGNUM));
301e0930
SS
163 }
164 return (cache);
165}
166
167static void
168kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
169 struct frame_id *this_id)
170{
171 struct kgdb_frame_cache *cache;
172
173 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
174 *this_id = frame_id_build(cache->sp, cache->pc);
175}
176
177static void
178kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
179 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
180 CORE_ADDR *addrp, int *realnump, void *valuep)
181{
182 char dummy_valuep[MAX_REGISTER_SIZE];
183 struct kgdb_frame_cache *cache;
184 int ofs, regsz;
185
186 regsz = register_size(current_gdbarch, regnum);
187
188 if (valuep == NULL)
189 valuep = dummy_valuep;
190 memset(valuep, 0, regsz);
191 *optimizedp = 0;
192 *addrp = 0;
193 *lvalp = not_lval;
194 *realnump = -1;
195
196 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
197 if (cache->pc == 0)
198 return;
199
200 ofs = (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_EFLAGS_REGNUM + 2)
201 ? kgdb_trgt_frame_offset[regnum] : -1;
202 if (ofs == -1)
203 return;
204
205 *addrp = cache->sp + ofs;
206 *lvalp = lval_memory;
207 target_read_memory(*addrp, valuep, regsz);
208}
209
3cdd3f79
MD
210int
211kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
212 struct frame_info *next_frame,
213 void **this_prologue_cache)
301e0930
SS
214{
215 char *pname;
216 CORE_ADDR pc;
217
3cdd3f79 218 pc = frame_unwind_address_in_block(next_frame, NORMAL_FRAME);
301e0930
SS
219 pname = NULL;
220 find_pc_partial_function(pc, &pname, NULL, NULL);
221 if (pname == NULL)
3cdd3f79 222 return (0);
301e0930 223 if (strcmp(pname, "calltrap") == 0 ||
3cdd3f79
MD
224 strcmp(pname, "dblfault_handler") == 0 ||
225 (pname[0] == 'X' && pname[1] == '_'))
226 return (1);
227 return (0);
301e0930 228}
3cdd3f79
MD
229
230const struct frame_unwind kgdb_trgt_trapframe_unwind = {
231 NORMAL_FRAME,
232 &kgdb_trgt_trapframe_this_id,
233 &kgdb_trgt_trapframe_prev_register,
234 .sniffer = kgdb_trgt_trapframe_sniffer
235};