2 * Copyright (c) 2006 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 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <machine/pcb.h>
32 #include <machine/frame.h>
39 #include <gdbthread.h>
42 #include <frame-unwind.h>
48 kgdb_trgt_core_pcb(u_int cpuid)
50 return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
54 kgdb_trgt_fetch_registers(int regno __unused)
58 struct gdbarch_tdep *tdep;
61 tdep = gdbarch_tdep (current_gdbarch);
63 kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
66 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
67 warnx("kvm_read: %s", kvm_geterr(kvm));
68 memset(&pcb, 0, sizeof(pcb));
72 * r14-r31 are saved in the pcb
74 for (i = 14; i <= 31; i++) {
75 supply_register(tdep->ppc_gp0_regnum + i,
76 (char *)&pcb.pcb_context[i]);
79 /* r1 is saved in the sp field */
80 supply_register(tdep->ppc_gp0_regnum + 1, (char *)&pcb.pcb_sp);
81 /* r2 is saved in the toc field */
82 supply_register(tdep->ppc_gp0_regnum + 2, (char *)&pcb.pcb_toc);
84 supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
85 supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
89 kgdb_trgt_store_registers(int regno __unused)
91 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
95 kgdb_trgt_new_objfile(struct objfile *objfile)
99 struct kgdb_frame_cache {
104 static struct kgdb_frame_cache *
105 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
107 char buf[MAX_REGISTER_SIZE];
108 struct kgdb_frame_cache *cache;
112 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
114 cache->pc = frame_func_unwind(next_frame);
115 frame_unwind_register(next_frame, SP_REGNUM, buf);
116 cache->sp = extract_unsigned_integer(buf,
117 register_size(current_gdbarch, SP_REGNUM));
123 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
124 struct frame_id *this_id)
126 struct kgdb_frame_cache *cache;
128 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
129 *this_id = frame_id_build(cache->sp, cache->pc);
133 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
134 void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
135 CORE_ADDR *addrp, int *realnump, void *valuep)
137 char dummy_valuep[MAX_REGISTER_SIZE];
138 struct gdbarch_tdep *tdep;
139 struct kgdb_frame_cache *cache;
142 tdep = gdbarch_tdep(current_gdbarch);
143 regsz = register_size(current_gdbarch, regnum);
146 valuep = dummy_valuep;
147 memset(valuep, 0, regsz);
153 if (regnum >= tdep->ppc_gp0_regnum &&
154 regnum <= tdep->ppc_gplast_regnum)
155 ofs = offsetof(struct trapframe,
156 fixreg[regnum - tdep->ppc_gp0_regnum]);
157 else if (regnum == tdep->ppc_lr_regnum)
158 ofs = offsetof(struct trapframe, lr);
159 else if (regnum == tdep->ppc_cr_regnum)
160 ofs = offsetof(struct trapframe, cr);
161 else if (regnum == tdep->ppc_xer_regnum)
162 ofs = offsetof(struct trapframe, xer);
163 else if (regnum == tdep->ppc_ctr_regnum)
164 ofs = offsetof(struct trapframe, ctr);
165 else if (regnum == PC_REGNUM)
166 ofs = offsetof(struct trapframe, srr0);
170 cache = kgdb_trgt_frame_cache(next_frame, this_cache);
171 *addrp = cache->sp + 48 + ofs;
172 *lvalp = lval_memory;
173 target_read_memory(*addrp, valuep, regsz);
176 static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
178 &kgdb_trgt_trapframe_this_id,
179 &kgdb_trgt_trapframe_prev_register
182 const struct frame_unwind *
183 kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
188 pc = frame_pc_unwind(next_frame);
190 find_pc_partial_function(pc, &pname, NULL, NULL);
193 if (strcmp(pname, "asttrapexit") == 0 ||
194 strcmp(pname, "trapexit") == 0)
195 return (&kgdb_trgt_trapframe_unwind);
196 /* printf("%s: %llx =%s\n", __func__, pc, pname); */
201 * This function ensures, that the PC is inside the
202 * function section which is understood by GDB.
204 * Return 0 when fixup is necessary, -1 otherwise.
207 kgdb_trgt_pc_fixup(CORE_ADDR *pc __unused)