Add amd64 target sources
[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 $
27 * $DragonFly: src/gnu/usr.bin/gdb/kgdb/trgt_amd64.c,v 1.1 2006/07/27 00:39:53 corecode Exp $
28 */
29
30#include <sys/cdefs.h>
31
32#include <sys/types.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
49void
50kgdb_trgt_fetch_registers(int regno __unused)
51{
52 struct kthr *kt;
53 struct pcb pcb;
54
55 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
56 if (kt == NULL)
57 return;
58 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
59 warnx("kvm_read: %s", kvm_geterr(kvm));
60 memset(&pcb, 0, sizeof(pcb));
61 }
62
63 supply_register(AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx);
64 supply_register(AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp);
65 supply_register(AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp);
66 supply_register(AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12);
67 supply_register(AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13);
68 supply_register(AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14);
69 supply_register(AMD64_R15_REGNUM, (char *)&pcb.pcb_r15);
70 supply_register(AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip);
71}
72
73void
74kgdb_trgt_store_registers(int regno __unused)
75{
76 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
77}
78
79struct kgdb_frame_cache {
80 CORE_ADDR pc;
81 CORE_ADDR sp;
82};
83
84static int kgdb_trgt_frame_offset[20] = {
85 offsetof(struct trapframe, tf_rax),
86 offsetof(struct trapframe, tf_rbx),
87 offsetof(struct trapframe, tf_rcx),
88 offsetof(struct trapframe, tf_rdx),
89 offsetof(struct trapframe, tf_rsi),
90 offsetof(struct trapframe, tf_rdi),
91 offsetof(struct trapframe, tf_rbp),
92 offsetof(struct trapframe, tf_rsp),
93 offsetof(struct trapframe, tf_r8),
94 offsetof(struct trapframe, tf_r9),
95 offsetof(struct trapframe, tf_r10),
96 offsetof(struct trapframe, tf_r11),
97 offsetof(struct trapframe, tf_r12),
98 offsetof(struct trapframe, tf_r13),
99 offsetof(struct trapframe, tf_r14),
100 offsetof(struct trapframe, tf_r15),
101 offsetof(struct trapframe, tf_rip),
102 offsetof(struct trapframe, tf_rflags),
103 offsetof(struct trapframe, tf_cs),
104 offsetof(struct trapframe, tf_ss)
105};
106
107static struct kgdb_frame_cache *
108kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
109{
110 char buf[MAX_REGISTER_SIZE];
111 struct kgdb_frame_cache *cache;
112
113 cache = *this_cache;
114 if (cache == NULL) {
115 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
116 *this_cache = cache;
117 cache->pc = frame_func_unwind(next_frame);
118 frame_unwind_register(next_frame, SP_REGNUM, buf);
119 cache->sp = extract_unsigned_integer(buf,
120 register_size(current_gdbarch, SP_REGNUM));
121 }
122 return (cache);
123}
124
125static void
126kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
127 struct frame_id *this_id)
128{
129 struct kgdb_frame_cache *cache;
130
131 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
132 *this_id = frame_id_build(cache->sp, cache->pc);
133}
134
135static void
136kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
137 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
138 CORE_ADDR *addrp, int *realnump, void *valuep)
139{
140 char dummy_valuep[MAX_REGISTER_SIZE];
141 struct kgdb_frame_cache *cache;
142 int ofs, regsz;
143
144 regsz = register_size(current_gdbarch, regnum);
145
146 if (valuep == NULL)
147 valuep = dummy_valuep;
148 memset(valuep, 0, regsz);
149 *optimizedp = 0;
150 *addrp = 0;
151 *lvalp = not_lval;
152 *realnump = -1;
153
154 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
155 if (cache->pc == 0)
156 return;
157
158 ofs = (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_EFLAGS_REGNUM + 2)
159 ? kgdb_trgt_frame_offset[regnum] : -1;
160 if (ofs == -1)
161 return;
162
163 *addrp = cache->sp + ofs;
164 *lvalp = lval_memory;
165 target_read_memory(*addrp, valuep, regsz);
166}
167
168const struct frame_unwind kgdb_trgt_trapframe_unwind = {
169 UNKNOWN_FRAME,
170 &kgdb_trgt_trapframe_this_id,
171 &kgdb_trgt_trapframe_prev_register
172};
173
174const struct frame_unwind *
175kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
176{
177 char *pname;
178 CORE_ADDR pc;
179
180 pc = frame_pc_unwind(next_frame);
181 if (pc == 0)
182 return (&kgdb_trgt_trapframe_unwind);
183 pname = NULL;
184 find_pc_partial_function(pc, &pname, NULL, NULL);
185 if (pname == NULL)
186 return (NULL);
187 if (strcmp(pname, "calltrap") == 0 ||
188 (pname[0] == 'X' && pname[1] != '_'))
189 return (&kgdb_trgt_trapframe_unwind);
190 /* printf("%s: %lx =%s\n", __func__, pc, pname); */
191 return (NULL);
192}