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