trgt_powerpc.c revision 234739
1/*-
2 * Copyright (c) 2006 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 AUTHOR ``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 AUTHOR 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_powerpc.c 234739 2012-04-27 20:16:20Z marcel $");
29
30#include <sys/types.h>
31#ifdef CROSS_DEBUGGER
32#include <sys/powerpc/include/pcb.h>
33#include <sys/powerpc/include/frame.h>
34#else
35#include <machine/pcb.h>
36#include <machine/frame.h>
37#endif
38#include <err.h>
39#include <kvm.h>
40#include <string.h>
41
42#include <defs.h>
43#include <target.h>
44#include <gdbthread.h>
45#include <inferior.h>
46#include <regcache.h>
47#include <frame-unwind.h>
48#include <ppc-tdep.h>
49
50#include "kgdb.h"
51
52void
53kgdb_trgt_fetch_registers(int regno __unused)
54{
55	struct kthr *kt;
56	struct pcb pcb;
57	struct gdbarch_tdep *tdep;
58	int i;
59
60	tdep = gdbarch_tdep (current_gdbarch);
61
62	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
63	if (kt == NULL)
64		return;
65	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
66		warnx("kvm_read: %s", kvm_geterr(kvm));
67		memset(&pcb, 0, sizeof(pcb));
68	}
69
70	/*
71	 * r14-r31 are saved in the pcb
72	 */
73	for (i = 14; i <= 31; i++) {
74		supply_register(tdep->ppc_gp0_regnum + i,
75		    (char *)&pcb.pcb_context[i]);
76	}
77
78	/* r1 is saved in the sp field */
79	supply_register(tdep->ppc_gp0_regnum + 1, (char *)&pcb.pcb_sp);
80
81	supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
82	supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
83}
84
85void
86kgdb_trgt_store_registers(int regno __unused)
87{
88	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
89}
90
91void
92kgdb_trgt_new_objfile(struct objfile *objfile)
93{
94}
95
96struct kgdb_frame_cache {
97	CORE_ADDR	pc;
98	CORE_ADDR	sp;
99};
100
101static struct kgdb_frame_cache *
102kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
103{
104	char buf[MAX_REGISTER_SIZE];
105	struct kgdb_frame_cache *cache;
106
107	cache = *this_cache;
108	if (cache == NULL) {
109		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
110		*this_cache = cache;
111		cache->pc = frame_func_unwind(next_frame);
112		frame_unwind_register(next_frame, SP_REGNUM, buf);
113		cache->sp = extract_unsigned_integer(buf,
114		    register_size(current_gdbarch, SP_REGNUM));
115	}
116	return (cache);
117}
118
119static void
120kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
121    struct frame_id *this_id)
122{
123	struct kgdb_frame_cache *cache;
124
125	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
126	*this_id = frame_id_build(cache->sp, cache->pc);
127}
128
129static void
130kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
131    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
132    CORE_ADDR *addrp, int *realnump, void *valuep)
133{
134	char dummy_valuep[MAX_REGISTER_SIZE];
135	struct gdbarch_tdep *tdep;
136	struct kgdb_frame_cache *cache;
137	int ofs, regsz;
138
139	tdep = gdbarch_tdep(current_gdbarch);
140	regsz = register_size(current_gdbarch, regnum);
141
142	if (valuep == NULL)
143		valuep = dummy_valuep;
144	memset(valuep, 0, regsz);
145	*optimizedp = 0;
146	*addrp = 0;
147	*lvalp = not_lval;
148	*realnump = -1;
149
150	if (regnum >= tdep->ppc_gp0_regnum &&
151	    regnum <= tdep->ppc_gplast_regnum)
152		ofs = offsetof(struct trapframe,
153		    fixreg[regnum - tdep->ppc_gp0_regnum]);
154	else if (regnum == tdep->ppc_lr_regnum)
155		ofs = offsetof(struct trapframe, lr);
156	else if (regnum == tdep->ppc_cr_regnum)
157		ofs = offsetof(struct trapframe, cr);
158	else if (regnum == tdep->ppc_xer_regnum)
159		ofs = offsetof(struct trapframe, xer);
160	else if (regnum == tdep->ppc_ctr_regnum)
161		ofs = offsetof(struct trapframe, ctr);
162	else if (regnum == PC_REGNUM)
163		ofs = offsetof(struct trapframe, srr0);
164	else
165		return;
166
167	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
168	*addrp = cache->sp + 8 + ofs;
169	*lvalp = lval_memory;
170	target_read_memory(*addrp, valuep, regsz);
171}
172
173static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
174        UNKNOWN_FRAME,
175        &kgdb_trgt_trapframe_this_id,
176        &kgdb_trgt_trapframe_prev_register
177};
178
179const struct frame_unwind *
180kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
181{
182	char *pname;
183	CORE_ADDR pc;
184
185	pc = frame_pc_unwind(next_frame);
186	pname = NULL;
187	find_pc_partial_function(pc, &pname, NULL, NULL);
188	if (pname == NULL)
189		return (NULL);
190	if (strcmp(pname, "asttrapexit") == 0 ||
191	    strcmp(pname, "trapexit") == 0)
192		return (&kgdb_trgt_trapframe_unwind);
193	/* printf("%s: %llx =%s\n", __func__, pc, pname); */
194	return (NULL);
195}
196