2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #ifndef CROSS_DEBUGGER
32 #include <machine/pcb.h>
33 #include <machine/frame.h>
34 #include <machine/armreg.h>
42 #include <gdbthread.h>
45 #include <frame-unwind.h>
51 kgdb_trgt_core_pcb(u_int cpuid)
53 #ifndef CROSS_DEBUGGER
54 return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
61 kgdb_trgt_fetch_registers(int regno __unused)
63 #ifndef CROSS_DEBUGGER
68 kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
71 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
72 warnx("kvm_read: %s", kvm_geterr(kvm));
73 memset(&pcb, 0, sizeof(pcb));
75 for (i = ARM_A1_REGNUM + 4; i <= ARM_SP_REGNUM; i++) {
76 supply_register(i, (char *)&pcb.pcb_regs.sf_r4 +
77 (i - (ARM_A1_REGNUM + 4 )) * 4);
79 supply_register(ARM_PC_REGNUM, (char *)&pcb.pcb_regs.sf_pc);
80 supply_register(ARM_LR_REGNUM, (char *)&pcb.pcb_regs.sf_lr);
85 kgdb_trgt_store_registers(int regno __unused)
87 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
91 kgdb_trgt_new_objfile(struct objfile *objfile)
95 #ifndef CROSS_DEBUGGER
96 struct kgdb_frame_cache {
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)
123 static struct kgdb_frame_cache *
124 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
126 char buf[MAX_REGISTER_SIZE];
127 struct kgdb_frame_cache *cache;
131 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_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 cache->pc = frame_func_unwind(next_frame);
147 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
148 struct frame_id *this_id)
150 struct kgdb_frame_cache *cache;
152 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
153 *this_id = frame_id_build(cache->sp, cache->pc);
157 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
158 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
159 CORE_ADDR *addrp, int *realnump, void *valuep)
161 char dummy_valuep[MAX_REGISTER_SIZE];
162 struct kgdb_frame_cache *cache;
166 regsz = register_size(current_gdbarch, regnum);
169 valuep = dummy_valuep;
170 memset(valuep, 0, regsz);
176 ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM)
177 ? kgdb_trgt_frame_offset[regnum] : -1;
181 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
184 ofs = kgdb_trgt_frame_offset[regnum];
186 *lvalp = lval_memory;
187 target_read_memory(*addrp, valuep, regsz);
190 static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
192 &kgdb_trgt_trapframe_this_id,
193 &kgdb_trgt_trapframe_prev_register
197 const struct frame_unwind *
198 kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
200 #ifndef CROSS_DEBUGGER
204 pc = frame_pc_unwind(next_frame);
206 find_pc_partial_function(pc, &pname, NULL, NULL);
211 if (!strcmp(pname, "undefinedinstruction"))
213 if (strcmp(pname, "Laddress_exception_entry") == 0 ||
214 strcmp(pname, "undefined_entry") == 0 ||
215 strcmp(pname, "exception_exit") == 0 ||
216 strcmp(pname, "Laddress_exception_msg") == 0 ||
217 strcmp(pname, "irq_entry") == 0)
218 return (&kgdb_trgt_trapframe_unwind);
219 if (!strcmp(pname, "undefinedinstruction"))
228 * This function ensures, that the PC is inside the
229 * function section which is understood by GDB.
231 * Return 0 when fixup is necessary, -1 otherwise.
234 kgdb_trgt_pc_fixup(CORE_ADDR *pc)
236 #ifndef CROSS_DEBUGGER
237 struct minimal_symbol *msymbol;
241 * exception_exit and swi_exit are special. These functions
242 * are artificially injected into the stack to be executed
243 * as the last entry in calling chain when all functions exit.
244 * Treat them differently.
246 msymbol = lookup_minimal_symbol_by_pc(*pc);
247 if (msymbol != NULL) {
248 if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "exception_exit") == 0)
250 if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "swi_exit") == 0)
255 * kdb_enter contains an invalid instruction which is supposed
256 * to generate a trap. BFD does not understand it and treats
257 * this part of function as a separate function. Move PC
258 * two instruction earlier to be inside kdb_enter section.
260 target_read_memory(*pc - 4, (char*)&valpc, 4);
261 if (valpc == 0xe7ffffff) {
267 * When the panic/vpanic is the last (noreturn) function,
268 * the bottom of the calling function looks as below.
271 * Normally, GDB is not able to detect function boundaries,
272 * so move the PC two instruction earlier where it can deal
274 * Match this pair of instructions: mov lr, pc followed with
277 if ((valpc & 0xff000000) == 0xea000000) {
278 target_read_memory(*pc - 8, (char*)&valpc, 4);
279 if (valpc == 0xe1a0e00f) {