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
{
126 #define FT_INTRFRAME 2
127 /*#define FT_INTRTRAPFRAME 3*/
128 #define FT_TIMERFRAME 4
129 #define FT_CALLTRAP 5
131 static int kgdb_trgt_frame_offset
[20] = {
132 offsetof(struct trapframe
, tf_rax
),
133 offsetof(struct trapframe
, tf_rbx
),
134 offsetof(struct trapframe
, tf_rcx
),
135 offsetof(struct trapframe
, tf_rdx
),
136 offsetof(struct trapframe
, tf_rsi
),
137 offsetof(struct trapframe
, tf_rdi
),
138 offsetof(struct trapframe
, tf_rbp
),
139 offsetof(struct trapframe
, tf_rsp
),
140 offsetof(struct trapframe
, tf_r8
),
141 offsetof(struct trapframe
, tf_r9
),
142 offsetof(struct trapframe
, tf_r10
),
143 offsetof(struct trapframe
, tf_r11
),
144 offsetof(struct trapframe
, tf_r12
),
145 offsetof(struct trapframe
, tf_r13
),
146 offsetof(struct trapframe
, tf_r14
),
147 offsetof(struct trapframe
, tf_r15
),
148 offsetof(struct trapframe
, tf_rip
),
149 offsetof(struct trapframe
, tf_rflags
),
150 offsetof(struct trapframe
, tf_cs
),
151 offsetof(struct trapframe
, tf_ss
)
154 static struct kgdb_frame_cache
*
155 kgdb_trgt_frame_cache(struct frame_info
*next_frame
, void **this_cache
)
157 struct kgdb_frame_cache
*cache
;
162 cache
= FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache
);
164 cache
->pc
= get_frame_address_in_block(next_frame
);
165 cache
->sp
= get_frame_sp(next_frame
);
166 find_pc_partial_function(cache
->pc
, &pname
, NULL
, NULL
);
168 if (strcmp(pname
, "calltrap") == 0)
169 cache
->frame_type
= FT_CALLTRAP
;
170 else if (pname
[0] != 'X')
171 cache
->frame_type
= FT_NORMAL
;
172 else if (strcmp(pname
, "Xtimerint") == 0)
173 cache
->frame_type
= FT_TIMERFRAME
;
175 cache
->frame_type
= FT_INTRFRAME
;
181 kgdb_trgt_trapframe_this_id(struct frame_info
*next_frame
, void **this_cache
,
182 struct frame_id
*this_id
)
184 struct kgdb_frame_cache
*cache
;
186 cache
= kgdb_trgt_frame_cache(next_frame
, this_cache
);
187 *this_id
= frame_id_build(cache
->sp
, cache
->pc
);
190 static struct value
*
191 kgdb_trgt_trapframe_prev_register(struct frame_info
*next_frame
,
192 void **this_cache
, int regnum
)
195 struct kgdb_frame_cache
*cache
;
198 if (regnum
< AMD64_RAX_REGNUM
|| regnum
> AMD64_EFLAGS_REGNUM
+ 2)
199 return frame_unwind_got_register(next_frame
, regnum
, regnum
);
201 ofs
= kgdb_trgt_frame_offset
[regnum
];
203 cache
= kgdb_trgt_frame_cache(next_frame
, this_cache
);
205 switch (cache
->frame_type
) {
214 case FT_INTRTRAPFRAME:
222 fprintf_unfiltered(gdb_stderr
, "Correct FT_XXX frame offsets "
223 "for %d\n", cache
->frame_type
);
227 addrp
= cache
->sp
+ ofs
;
228 return frame_unwind_got_memory(next_frame
, regnum
, addrp
);
231 const struct frame_unwind kgdb_trgt_trapframe_unwind
= {
233 &kgdb_trgt_trapframe_this_id
,
234 &kgdb_trgt_trapframe_prev_register
,
235 .sniffer
= kgdb_trgt_trapframe_sniffer
239 kgdb_trgt_trapframe_sniffer(const struct frame_unwind
*self
,
240 struct frame_info
*next_frame
,
241 void **this_prologue_cache
)
246 pc
= get_frame_address_in_block(next_frame
);
248 find_pc_partial_function(pc
, &pname
, NULL
, NULL
);
251 if (strcmp(pname
, "calltrap") == 0 ||
252 strcmp(pname
, "dblfault_handler") == 0 ||
253 strcmp(pname
, "nmi_calltrap") == 0 ||
254 (pname
[0] == 'X' && pname
[1] != '_'))