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