1 |
/* $MidnightBSD$ */ |
2 |
/* |
3 |
* Copyright (c) 2004 Marcel Moolenaar |
4 |
* All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the distribution. |
15 |
* |
16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 |
*/ |
27 |
|
28 |
#include <sys/cdefs.h> |
29 |
__FBSDID("$FreeBSD: stable/10/gnu/usr.bin/gdb/kgdb/trgt_arm.c 278614 2015-02-12 04:15:55Z ian $"); |
30 |
|
31 |
#include <sys/types.h> |
32 |
#ifndef CROSS_DEBUGGER |
33 |
#include <machine/pcb.h> |
34 |
#include <machine/frame.h> |
35 |
#include <machine/armreg.h> |
36 |
#endif |
37 |
#include <err.h> |
38 |
#include <kvm.h> |
39 |
#include <string.h> |
40 |
|
41 |
#include <defs.h> |
42 |
#include <target.h> |
43 |
#include <gdbthread.h> |
44 |
#include <inferior.h> |
45 |
#include <regcache.h> |
46 |
#include <frame-unwind.h> |
47 |
#include <arm-tdep.h> |
48 |
|
49 |
#include "kgdb.h" |
50 |
|
51 |
CORE_ADDR |
52 |
kgdb_trgt_core_pcb(u_int cpuid) |
53 |
{ |
54 |
return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb))); |
55 |
} |
56 |
|
57 |
void |
58 |
kgdb_trgt_fetch_registers(int regno __unused) |
59 |
{ |
60 |
#ifndef CROSS_DEBUGGER |
61 |
struct kthr *kt; |
62 |
struct pcb pcb; |
63 |
int i, reg; |
64 |
|
65 |
kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid)); |
66 |
if (kt == NULL) |
67 |
return; |
68 |
if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) { |
69 |
warnx("kvm_read: %s", kvm_geterr(kvm)); |
70 |
memset(&pcb, 0, sizeof(pcb)); |
71 |
} |
72 |
for (i = ARM_A1_REGNUM + 4; i <= ARM_SP_REGNUM; i++) { |
73 |
supply_register(i, (char *)&pcb.pcb_regs.sf_r4 + |
74 |
(i - (ARM_A1_REGNUM + 4 )) * 4); |
75 |
} |
76 |
if (pcb.pcb_regs.sf_sp != 0) { |
77 |
if (kvm_read(kvm, pcb.pcb_regs.sf_sp + 4 * 4, ®, 4) != 4) |
78 |
warnx("kvm_read :%s", kvm_geterr(kvm)); |
79 |
else |
80 |
supply_register(ARM_PC_REGNUM, (char *)®); |
81 |
} |
82 |
#endif |
83 |
} |
84 |
|
85 |
void |
86 |
kgdb_trgt_store_registers(int regno __unused) |
87 |
{ |
88 |
fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__); |
89 |
} |
90 |
|
91 |
void |
92 |
kgdb_trgt_new_objfile(struct objfile *objfile) |
93 |
{ |
94 |
} |
95 |
|
96 |
#ifndef CROSS_DEBUGGER |
97 |
struct kgdb_frame_cache { |
98 |
CORE_ADDR fp; |
99 |
CORE_ADDR sp; |
100 |
}; |
101 |
|
102 |
static int kgdb_trgt_frame_offset[26] = { |
103 |
offsetof(struct trapframe, tf_r0), |
104 |
offsetof(struct trapframe, tf_r1), |
105 |
offsetof(struct trapframe, tf_r2), |
106 |
offsetof(struct trapframe, tf_r3), |
107 |
offsetof(struct trapframe, tf_r4), |
108 |
offsetof(struct trapframe, tf_r5), |
109 |
offsetof(struct trapframe, tf_r6), |
110 |
offsetof(struct trapframe, tf_r7), |
111 |
offsetof(struct trapframe, tf_r8), |
112 |
offsetof(struct trapframe, tf_r9), |
113 |
offsetof(struct trapframe, tf_r10), |
114 |
offsetof(struct trapframe, tf_r11), |
115 |
offsetof(struct trapframe, tf_r12), |
116 |
offsetof(struct trapframe, tf_svc_sp), |
117 |
offsetof(struct trapframe, tf_svc_lr), |
118 |
offsetof(struct trapframe, tf_pc), |
119 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, |
120 |
offsetof(struct trapframe, tf_spsr) |
121 |
}; |
122 |
|
123 |
static struct kgdb_frame_cache * |
124 |
kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache) |
125 |
{ |
126 |
char buf[MAX_REGISTER_SIZE]; |
127 |
struct kgdb_frame_cache *cache; |
128 |
|
129 |
cache = *this_cache; |
130 |
if (cache == NULL) { |
131 |
cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache); |
132 |
*this_cache = cache; |
133 |
frame_unwind_register(next_frame, ARM_SP_REGNUM, buf); |
134 |
cache->sp = extract_unsigned_integer(buf, |
135 |
register_size(current_gdbarch, ARM_SP_REGNUM)); |
136 |
frame_unwind_register(next_frame, ARM_FP_REGNUM, buf); |
137 |
cache->fp = extract_unsigned_integer(buf, |
138 |
register_size(current_gdbarch, ARM_FP_REGNUM)); |
139 |
} |
140 |
return (cache); |
141 |
} |
142 |
|
143 |
static int is_undef; |
144 |
|
145 |
static void |
146 |
kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache, |
147 |
struct frame_id *this_id) |
148 |
{ |
149 |
struct kgdb_frame_cache *cache; |
150 |
|
151 |
cache = kgdb_trgt_frame_cache(next_frame, this_cache); |
152 |
*this_id = frame_id_build(cache->fp, 0); |
153 |
} |
154 |
|
155 |
static void |
156 |
kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame, |
157 |
void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp, |
158 |
CORE_ADDR *addrp, int *realnump, void *valuep) |
159 |
{ |
160 |
char dummy_valuep[MAX_REGISTER_SIZE]; |
161 |
struct kgdb_frame_cache *cache; |
162 |
int ofs, regsz; |
163 |
int is_undefined = 0; |
164 |
|
165 |
regsz = register_size(current_gdbarch, regnum); |
166 |
|
167 |
if (valuep == NULL) |
168 |
valuep = dummy_valuep; |
169 |
memset(valuep, 0, regsz); |
170 |
*optimizedp = 0; |
171 |
*addrp = 0; |
172 |
*lvalp = not_lval; |
173 |
*realnump = -1; |
174 |
|
175 |
ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM) |
176 |
? kgdb_trgt_frame_offset[regnum] : -1; |
177 |
if (ofs == -1) |
178 |
return; |
179 |
|
180 |
cache = kgdb_trgt_frame_cache(next_frame, this_cache); |
181 |
|
182 |
if (is_undef && (regnum == ARM_SP_REGNUM || regnum == ARM_PC_REGNUM)) { |
183 |
*addrp = cache->sp + offsetof(struct trapframe, tf_spsr); |
184 |
target_read_memory(*addrp, valuep, regsz); |
185 |
is_undefined = 1; |
186 |
ofs = kgdb_trgt_frame_offset[ARM_SP_REGNUM]; |
187 |
|
188 |
} |
189 |
*addrp = cache->sp + ofs; |
190 |
*lvalp = lval_memory; |
191 |
target_read_memory(*addrp, valuep, regsz); |
192 |
|
193 |
if (is_undefined) { |
194 |
*addrp = *(unsigned int *)valuep + (regnum == ARM_SP_REGNUM ? |
195 |
0 : 8); |
196 |
target_read_memory(*addrp, valuep, regsz); |
197 |
|
198 |
} |
199 |
} |
200 |
|
201 |
static const struct frame_unwind kgdb_trgt_trapframe_unwind = { |
202 |
UNKNOWN_FRAME, |
203 |
&kgdb_trgt_trapframe_this_id, |
204 |
&kgdb_trgt_trapframe_prev_register |
205 |
}; |
206 |
#endif |
207 |
|
208 |
const struct frame_unwind * |
209 |
kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame) |
210 |
{ |
211 |
#ifndef CROSS_DEBUGGER |
212 |
char *pname; |
213 |
CORE_ADDR pc; |
214 |
|
215 |
pc = frame_pc_unwind(next_frame); |
216 |
pname = NULL; |
217 |
find_pc_partial_function(pc, &pname, NULL, NULL); |
218 |
if (pname == NULL) { |
219 |
is_undef = 0; |
220 |
return (NULL); |
221 |
} |
222 |
if (!strcmp(pname, "undefinedinstruction")) |
223 |
is_undef = 1; |
224 |
if (strcmp(pname, "Laddress_exception_entry") == 0 || |
225 |
strcmp(pname, "undefined_entry") == 0 || |
226 |
strcmp(pname, "exception_exit") == 0 || |
227 |
strcmp(pname, "Laddress_exception_msg") == 0 || |
228 |
strcmp(pname, "irq_entry") == 0) |
229 |
return (&kgdb_trgt_trapframe_unwind); |
230 |
if (!strcmp(pname, "undefinedinstruction")) |
231 |
is_undef = 1; |
232 |
else |
233 |
is_undef = 0; |
234 |
#endif |
235 |
return (NULL); |
236 |
} |