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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <machine/pcb.h>
32 #include <machine/frame.h>
39 #include <gdbthread.h>
42 #include <frame-unwind.h>
43 #include <amd64-tdep.h>
48 kgdb_trgt_fetch_registers(int regno __unused)
53 kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
56 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
57 warnx("kvm_read: %s", kvm_geterr(kvm));
58 memset(&pcb, 0, sizeof(pcb));
61 supply_register(AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx);
62 supply_register(AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp);
63 supply_register(AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp);
64 supply_register(AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12);
65 supply_register(AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13);
66 supply_register(AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14);
67 supply_register(AMD64_R15_REGNUM, (char *)&pcb.pcb_r15);
68 supply_register(AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip);
69 amd64_supply_fxsave(current_regcache, -1, &pcb.pcb_user_save);
73 kgdb_trgt_store_registers(int regno __unused)
75 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
79 kgdb_trgt_new_objfile(struct objfile *objfile)
83 struct kgdb_frame_cache {
88 static int kgdb_trgt_frame_offset[20] = {
89 offsetof(struct trapframe, tf_rax),
90 offsetof(struct trapframe, tf_rbx),
91 offsetof(struct trapframe, tf_rcx),
92 offsetof(struct trapframe, tf_rdx),
93 offsetof(struct trapframe, tf_rsi),
94 offsetof(struct trapframe, tf_rdi),
95 offsetof(struct trapframe, tf_rbp),
96 offsetof(struct trapframe, tf_rsp),
97 offsetof(struct trapframe, tf_r8),
98 offsetof(struct trapframe, tf_r9),
99 offsetof(struct trapframe, tf_r10),
100 offsetof(struct trapframe, tf_r11),
101 offsetof(struct trapframe, tf_r12),
102 offsetof(struct trapframe, tf_r13),
103 offsetof(struct trapframe, tf_r14),
104 offsetof(struct trapframe, tf_r15),
105 offsetof(struct trapframe, tf_rip),
106 offsetof(struct trapframe, tf_rflags),
107 offsetof(struct trapframe, tf_cs),
108 offsetof(struct trapframe, tf_ss)
111 static struct kgdb_frame_cache *
112 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
114 char buf[MAX_REGISTER_SIZE];
115 struct kgdb_frame_cache *cache;
119 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
121 cache->pc = frame_func_unwind(next_frame);
122 frame_unwind_register(next_frame, SP_REGNUM, buf);
123 cache->sp = extract_unsigned_integer(buf,
124 register_size(current_gdbarch, SP_REGNUM));
130 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
131 struct frame_id *this_id)
133 struct kgdb_frame_cache *cache;
135 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
136 *this_id = frame_id_build(cache->sp, cache->pc);
140 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
141 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
142 CORE_ADDR *addrp, int *realnump, void *valuep)
144 char dummy_valuep[MAX_REGISTER_SIZE];
145 struct kgdb_frame_cache *cache;
148 regsz = register_size(current_gdbarch, regnum);
151 valuep = dummy_valuep;
152 memset(valuep, 0, regsz);
158 ofs = (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_EFLAGS_REGNUM + 2)
159 ? kgdb_trgt_frame_offset[regnum] : -1;
163 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
164 *addrp = cache->sp + ofs;
165 *lvalp = lval_memory;
166 target_read_memory(*addrp, valuep, regsz);
169 static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
171 &kgdb_trgt_trapframe_this_id,
172 &kgdb_trgt_trapframe_prev_register
175 const struct frame_unwind *
176 kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
181 pc = frame_pc_unwind(next_frame);
183 find_pc_partial_function(pc, &pname, NULL, NULL);
186 if (strcmp(pname, "calltrap") == 0 ||
187 strcmp(pname, "nmi_calltrap") == 0 ||
188 (pname[0] == 'X' && pname[1] != '_'))
189 return (&kgdb_trgt_trapframe_unwind);
190 /* printf("%s: %lx =%s\n", __func__, pc, pname); */