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