kernel - More readdirplus deadlock avoidance work
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt_x86_64.c
... / ...
CommitLineData
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.10 2008/05/01 20:36:48 jhb Exp $
27 */
28
29#include <sys/cdefs.h>
30
31#include <sys/types.h>
32#include <sys/thread.h>
33#include <machine/pcb.h>
34#include <machine/frame.h>
35#include <err.h>
36#include <kvm.h>
37#include <string.h>
38
39#include <defs.h>
40#include <target.h>
41#include <gdbthread.h>
42#include <inferior.h>
43#include <regcache.h>
44#include <frame-unwind.h>
45#include <amd64-tdep.h>
46
47#include "kgdb.h"
48
49static int
50kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
51 struct frame_info *next_frame,
52 void **this_prologue_cache);
53
54void
55kgdb_trgt_fetch_registers(struct target_ops *target_ops, struct regcache *regcache, int regno)
56{
57 struct kthr *kt;
58 struct pcb pcb;
59
60 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
61 if (kt == NULL) {
62 regcache_raw_supply(regcache, regno, NULL);
63 return;
64 }
65
66 /*
67 * kt->pcb == 0 is a marker for "non-dumping kernel thread".
68 */
69 if (kt->pcb == 0) {
70 uintptr_t regs[7];
71 uintptr_t addr;
72 uintptr_t sp;
73
74 addr = kt->kaddr + offsetof(struct thread, td_sp);
75 kvm_read(kvm, addr, &sp, sizeof(sp));
76 /*
77 * Stack is:
78 * -2 ret
79 * -1 popfq
80 * 0 popq %r15 edi
81 * 1 popq %r14
82 * 2 popq %r13
83 * 3 popq %r12
84 * 4 popq %rbx
85 * 5 popq %rbp
86 * 6 ret
87 */
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));
91 }
92 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 7, &regs[0]);
93 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 6, &regs[1]);
94 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 5, &regs[2]);
95 regcache_raw_supply(regcache, AMD64_R8_REGNUM + 4, &regs[3]);
96 regcache_raw_supply(regcache, AMD64_RBX_REGNUM, &regs[4]);
97 regcache_raw_supply(regcache, AMD64_RBP_REGNUM, &regs[5]);
98 regcache_raw_supply(regcache, AMD64_RIP_REGNUM, &regs[6]);
99 sp += 9 * sizeof(regs[0]);
100 regcache_raw_supply(regcache, AMD64_RSP_REGNUM, &sp);
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 }
108
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);
117}
118
119struct kgdb_frame_cache {
120 CORE_ADDR pc;
121 CORE_ADDR sp;
122};
123
124static 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)
145};
146
147static struct kgdb_frame_cache *
148kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
149{
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;
153
154 cache = *this_cache;
155 if (cache == NULL) {
156 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
157 *this_cache = 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),
162 byte_order);
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 struct value *
178kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
179 void **this_cache, int regnum)
180{
181 CORE_ADDR addrp;
182 struct kgdb_frame_cache *cache;
183 int ofs;
184
185 if (regnum < AMD64_RAX_REGNUM || regnum > AMD64_EFLAGS_REGNUM + 2)
186 return frame_unwind_got_register(next_frame, regnum, regnum);
187
188 ofs = kgdb_trgt_frame_offset[regnum];
189
190 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
191
192 addrp = cache->sp + ofs;
193 return frame_unwind_got_memory(next_frame, regnum, addrp);
194}
195
196const struct frame_unwind kgdb_trgt_trapframe_unwind = {
197 NORMAL_FRAME,
198 &kgdb_trgt_trapframe_this_id,
199 &kgdb_trgt_trapframe_prev_register,
200 .sniffer = kgdb_trgt_trapframe_sniffer
201};
202
203static int
204kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
205 struct frame_info *next_frame,
206 void **this_prologue_cache)
207{
208 char *pname;
209 CORE_ADDR pc;
210
211 pc = get_frame_address_in_block(next_frame);
212 pname = NULL;
213 find_pc_partial_function(pc, &pname, NULL, NULL);
214 if (pname == NULL)
215 return (0);
216 if (strcmp(pname, "calltrap") == 0 ||
217 strcmp(pname, "dblfault_handler") == 0 ||
218 strcmp(pname, "nmi_calltrap") == 0 ||
219 (pname[0] == 'X' && pname[1] != '_'))
220 return (1);
221 return (0);
222}