trgt_arm.c revision 162303
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/gnu/usr.bin/gdb/kgdb/trgt_arm.c 162303 2006-09-14 07:51:52Z imp $");
29
30#include <sys/types.h>
31#ifndef CROSS_DEBUGGER
32#include <machine/pcb.h>
33#include <machine/frame.h>
34#include <machine/armreg.h>
35#endif
36#include <err.h>
37#include <kvm.h>
38#include <string.h>
39
40#include <defs.h>
41#include <target.h>
42#include <gdbthread.h>
43#include <inferior.h>
44#include <regcache.h>
45#include <frame-unwind.h>
46#include <arm-tdep.h>
47
48#include "kgdb.h"
49
50void
51kgdb_trgt_fetch_registers(int regno __unused)
52{
53#ifndef CROSS_DEBUGGER
54	struct kthr *kt;
55	struct pcb pcb;
56	int i, reg;
57
58	kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
59	if (kt == NULL)
60		return;
61	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
62		warnx("kvm_read: %s", kvm_geterr(kvm));
63		memset(&pcb, 0, sizeof(pcb));
64	}
65	for (i = ARM_A1_REGNUM + 8; i <= ARM_SP_REGNUM; i++) {
66		supply_register(i, (char *)&pcb.un_32.pcb32_r8 +
67		    (i - (ARM_A1_REGNUM + 8 )) * 4);
68	}
69	if (pcb.un_32.pcb32_sp != 0) {
70		for (i = 0; i < 4; i++) {
71			if (kvm_read(kvm, pcb.un_32.pcb32_sp + (i) * 4,
72			    &reg, 4) != 4) {
73				warnx("kvm_read: %s", kvm_geterr(kvm));
74				break;
75			}
76			supply_register(ARM_A1_REGNUM + 4 + i, (char *)&reg);
77		}
78		if (kvm_read(kvm, pcb.un_32.pcb32_sp + 4 * 4, &reg, 4) != 4)
79			warnx("kvm_read :%s", kvm_geterr(kvm));
80		else
81			supply_register(ARM_PC_REGNUM, (char *)&reg);
82	}
83#endif
84}
85
86void
87kgdb_trgt_store_registers(int regno __unused)
88{
89	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
90}
91
92#ifndef CROSS_DEBUGGER
93struct kgdb_frame_cache {
94	CORE_ADDR	fp;
95	CORE_ADDR	sp;
96};
97
98static int kgdb_trgt_frame_offset[26] = {
99	offsetof(struct trapframe, tf_r0),
100	offsetof(struct trapframe, tf_r1),
101	offsetof(struct trapframe, tf_r2),
102	offsetof(struct trapframe, tf_r3),
103	offsetof(struct trapframe, tf_r4),
104	offsetof(struct trapframe, tf_r5),
105	offsetof(struct trapframe, tf_r6),
106	offsetof(struct trapframe, tf_r7),
107	offsetof(struct trapframe, tf_r8),
108	offsetof(struct trapframe, tf_r9),
109	offsetof(struct trapframe, tf_r10),
110	offsetof(struct trapframe, tf_r11),
111	offsetof(struct trapframe, tf_r12),
112	offsetof(struct trapframe, tf_svc_sp),
113	offsetof(struct trapframe, tf_svc_lr),
114	offsetof(struct trapframe, tf_pc),
115	-1, -1, -1, -1, -1, -1, -1, -1, -1,
116	offsetof(struct trapframe, tf_spsr)
117};
118
119static struct kgdb_frame_cache *
120kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
121{
122	char buf[MAX_REGISTER_SIZE];
123	struct kgdb_frame_cache *cache;
124
125	cache = *this_cache;
126	if (cache == NULL) {
127		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
128		*this_cache = cache;
129		frame_unwind_register(next_frame, ARM_SP_REGNUM, buf);
130		cache->sp = extract_unsigned_integer(buf,
131		    register_size(current_gdbarch, ARM_SP_REGNUM));
132		frame_unwind_register(next_frame, ARM_FP_REGNUM, buf);
133		cache->fp = extract_unsigned_integer(buf,
134		    register_size(current_gdbarch, ARM_FP_REGNUM));
135	}
136	return (cache);
137}
138
139static int is_undef;
140
141static void
142kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
143    struct frame_id *this_id)
144{
145	struct kgdb_frame_cache *cache;
146
147	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
148	*this_id = frame_id_build(cache->fp, 0);
149}
150
151static void
152kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
153    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
154    CORE_ADDR *addrp, int *realnump, void *valuep)
155{
156	char dummy_valuep[MAX_REGISTER_SIZE];
157	struct kgdb_frame_cache *cache;
158	int ofs, regsz;
159	int is_undefined = 0;
160
161	regsz = register_size(current_gdbarch, regnum);
162
163	if (valuep == NULL)
164		valuep = dummy_valuep;
165	memset(valuep, 0, regsz);
166	*optimizedp = 0;
167	*addrp = 0;
168	*lvalp = not_lval;
169	*realnump = -1;
170
171	ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM)
172	    ? kgdb_trgt_frame_offset[regnum] : -1;
173	if (ofs == -1)
174		return;
175
176	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
177
178	if (is_undef && (regnum == ARM_SP_REGNUM || regnum == ARM_PC_REGNUM)) {
179		*addrp = cache->sp + offsetof(struct trapframe, tf_spsr);
180		target_read_memory(*addrp, valuep, regsz);
181		is_undefined = 1;
182		ofs = kgdb_trgt_frame_offset[ARM_SP_REGNUM];
183
184	}
185	*addrp = cache->sp + ofs;
186	*lvalp = lval_memory;
187	target_read_memory(*addrp, valuep, regsz);
188
189	if (is_undefined) {
190		*addrp = *(unsigned int *)valuep + (regnum == ARM_SP_REGNUM ?
191		    0 : 8);
192		target_read_memory(*addrp, valuep, regsz);
193
194	}
195}
196
197static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
198        UNKNOWN_FRAME,
199        &kgdb_trgt_trapframe_this_id,
200        &kgdb_trgt_trapframe_prev_register
201};
202#endif
203
204const struct frame_unwind *
205kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
206{
207#ifndef CROSS_DEBUGGER
208	char *pname;
209	CORE_ADDR pc;
210
211	pc = frame_pc_unwind(next_frame);
212	pname = NULL;
213	find_pc_partial_function(pc, &pname, NULL, NULL);
214	if (pname == NULL) {
215		is_undef = 0;
216		return (NULL);
217	}
218	if (!strcmp(pname, "undefinedinstruction"))
219		is_undef = 1;
220	if (strcmp(pname, "Laddress_exception_entry") == 0 ||
221	    strcmp(pname, "undefined_entry") == 0 ||
222	    strcmp(pname, "exception_exit") == 0 ||
223	    strcmp(pname, "Laddress_exception_msg") == 0 ||
224	    strcmp(pname, "irq_entry") == 0)
225		return (&kgdb_trgt_trapframe_unwind);
226	if (!strcmp(pname, "undefinedinstruction"))
227		is_undef = 1;
228	else
229		is_undef = 0;
230#endif
231	return (NULL);
232}
233