trgt_i386.c revision 167143
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_i386.c 167143 2007-03-01 13:56:08Z kib $"); 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 <i386-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 53 kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid)); 54 if (kt == NULL) 55 return; 56 if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) { 57 warnx("kvm_read: %s", kvm_geterr(kvm)); 58 memset(&pcb, 0, sizeof(pcb)); 59 } 60 supply_register(I386_EBX_REGNUM, (char *)&pcb.pcb_ebx); 61 supply_register(I386_ESP_REGNUM, (char *)&pcb.pcb_esp); 62 supply_register(I386_EBP_REGNUM, (char *)&pcb.pcb_ebp); 63 supply_register(I386_ESI_REGNUM, (char *)&pcb.pcb_esi); 64 supply_register(I386_EDI_REGNUM, (char *)&pcb.pcb_edi); 65 supply_register(I386_EIP_REGNUM, (char *)&pcb.pcb_eip); 66} 67 68void 69kgdb_trgt_store_registers(int regno __unused) 70{ 71 fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__); 72} 73 74struct kgdb_frame_cache { 75 int intrframe; 76 CORE_ADDR pc; 77 CORE_ADDR sp; 78}; 79 80static int kgdb_trgt_frame_offset[15] = { 81 offsetof(struct trapframe, tf_eax), 82 offsetof(struct trapframe, tf_ecx), 83 offsetof(struct trapframe, tf_edx), 84 offsetof(struct trapframe, tf_ebx), 85 offsetof(struct trapframe, tf_esp), 86 offsetof(struct trapframe, tf_ebp), 87 offsetof(struct trapframe, tf_esi), 88 offsetof(struct trapframe, tf_edi), 89 offsetof(struct trapframe, tf_eip), 90 offsetof(struct trapframe, tf_eflags), 91 offsetof(struct trapframe, tf_cs), 92 offsetof(struct trapframe, tf_ss), 93 offsetof(struct trapframe, tf_ds), 94 offsetof(struct trapframe, tf_es), 95 offsetof(struct trapframe, tf_fs) 96}; 97 98static struct kgdb_frame_cache * 99kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache) 100{ 101 char buf[MAX_REGISTER_SIZE]; 102 struct kgdb_frame_cache *cache; 103 char *pname; 104 105 cache = *this_cache; 106 if (cache == NULL) { 107 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache); 108 *this_cache = cache; 109 cache->pc = frame_func_unwind(next_frame); 110 find_pc_partial_function(cache->pc, &pname, NULL, NULL); 111 cache->intrframe = (pname[0] == 'X') ? 1 : 0; 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 kgdb_frame_cache *cache; 136 int ofs, regsz; 137 static int ofs_fix = 0; 138 static int ofs_fixed = 0; 139 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 (!ofs_fixed) { 151 uintptr_t calltrap_addr; 152 char calltrap[1]; 153 154 calltrap_addr = kgdb_lookup("calltrap"); 155 if (calltrap_addr != 0) { 156 if (kvm_read(kvm, calltrap_addr, calltrap, 157 sizeof(calltrap)) != sizeof(calltrap)) { 158 warnx("kvm_read: %s", kvm_geterr(kvm)); 159 } else if (calltrap[0] == 0x54) /* push %esp */ { 160 /* 161 * To accomodate for rev. 1.117 of 162 * i386/i386/exception.s 163 */ 164 ofs_fix = 4; 165 } 166 } 167 ofs_fixed = 1; 168 } 169 ofs = (regnum >= I386_EAX_REGNUM && regnum <= I386_FS_REGNUM) 170 ? kgdb_trgt_frame_offset[regnum] + ofs_fix : -1; 171 if (ofs == -1) 172 return; 173 174 cache = kgdb_trgt_frame_cache(next_frame, this_cache); 175 *addrp = cache->sp + ofs + (cache->intrframe ? 4 : 0); 176 *lvalp = lval_memory; 177 target_read_memory(*addrp, valuep, regsz); 178} 179 180static const struct frame_unwind kgdb_trgt_trapframe_unwind = { 181 UNKNOWN_FRAME, 182 &kgdb_trgt_trapframe_this_id, 183 &kgdb_trgt_trapframe_prev_register 184}; 185 186const struct frame_unwind * 187kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame) 188{ 189 char *pname; 190 CORE_ADDR pc; 191 192 pc = frame_pc_unwind(next_frame); 193 pname = NULL; 194 find_pc_partial_function(pc, &pname, NULL, NULL); 195 if (pname == NULL) 196 return (NULL); 197 if (strcmp(pname, "calltrap") == 0 || 198 (pname[0] == 'X' && pname[1] != '_')) 199 return (&kgdb_trgt_trapframe_unwind); 200 /* printf("%s: %llx =%s\n", __func__, pc, pname); */ 201 return (NULL); 202} 203