1132624Smarcel/* 2132624Smarcel * Copyright (c) 2004 Marcel Moolenaar 3132624Smarcel * All rights reserved. 4132624Smarcel * 5132624Smarcel * Redistribution and use in source and binary forms, with or without 6132624Smarcel * modification, are permitted provided that the following conditions 7132624Smarcel * are met: 8132624Smarcel * 9132624Smarcel * 1. Redistributions of source code must retain the above copyright 10132624Smarcel * notice, this list of conditions and the following disclaimer. 11132624Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12132624Smarcel * notice, this list of conditions and the following disclaimer in the 13132624Smarcel * documentation and/or other materials provided with the distribution. 14132624Smarcel * 15132624Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16132624Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17132624Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18132624Smarcel * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19132624Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20132624Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21132624Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22132624Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23132624Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24132624Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25132624Smarcel */ 26132624Smarcel 27132624Smarcel#include <sys/cdefs.h> 28132624Smarcel__FBSDID("$FreeBSD$"); 29132624Smarcel 30132624Smarcel#include <sys/types.h> 31132624Smarcel#include <machine/pcb.h> 32149957Smarcel#include <machine/frame.h> 33132624Smarcel#include <err.h> 34132624Smarcel#include <kvm.h> 35132624Smarcel#include <string.h> 36132624Smarcel 37132624Smarcel#include <defs.h> 38132624Smarcel#include <target.h> 39132624Smarcel#include <gdbthread.h> 40132624Smarcel#include <inferior.h> 41132624Smarcel#include <regcache.h> 42149954Smarcel#include <frame-unwind.h> 43149957Smarcel#include <amd64-tdep.h> 44132624Smarcel 45149954Smarcel#include "kgdb.h" 46149954Smarcel 47246893SmarcelCORE_ADDR 48246893Smarcelkgdb_trgt_core_pcb(u_int cpuid) 49246893Smarcel{ 50246893Smarcel return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb))); 51246893Smarcel} 52246893Smarcel 53132624Smarcelvoid 54132624Smarcelkgdb_trgt_fetch_registers(int regno __unused) 55132624Smarcel{ 56132624Smarcel struct kthr *kt; 57132624Smarcel struct pcb pcb; 58132624Smarcel 59178713Sjhb kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid)); 60132624Smarcel if (kt == NULL) 61132624Smarcel return; 62132624Smarcel if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) { 63132624Smarcel warnx("kvm_read: %s", kvm_geterr(kvm)); 64132624Smarcel memset(&pcb, 0, sizeof(pcb)); 65132624Smarcel } 66132624Smarcel 67149957Smarcel supply_register(AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx); 68149957Smarcel supply_register(AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp); 69149957Smarcel supply_register(AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp); 70149957Smarcel supply_register(AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12); 71149957Smarcel supply_register(AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13); 72149957Smarcel supply_register(AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14); 73149957Smarcel supply_register(AMD64_R15_REGNUM, (char *)&pcb.pcb_r15); 74149957Smarcel supply_register(AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip); 75132624Smarcel} 76132624Smarcel 77132624Smarcelvoid 78132624Smarcelkgdb_trgt_store_registers(int regno __unused) 79132624Smarcel{ 80132624Smarcel fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__); 81132624Smarcel} 82149954Smarcel 83178670Sjhbvoid 84178670Sjhbkgdb_trgt_new_objfile(struct objfile *objfile) 85178670Sjhb{ 86178670Sjhb} 87178670Sjhb 88149957Smarcelstruct kgdb_frame_cache { 89149957Smarcel CORE_ADDR pc; 90149957Smarcel CORE_ADDR sp; 91149957Smarcel}; 92149957Smarcel 93149957Smarcelstatic int kgdb_trgt_frame_offset[20] = { 94149957Smarcel offsetof(struct trapframe, tf_rax), 95149957Smarcel offsetof(struct trapframe, tf_rbx), 96149957Smarcel offsetof(struct trapframe, tf_rcx), 97149957Smarcel offsetof(struct trapframe, tf_rdx), 98149957Smarcel offsetof(struct trapframe, tf_rsi), 99149957Smarcel offsetof(struct trapframe, tf_rdi), 100149957Smarcel offsetof(struct trapframe, tf_rbp), 101149957Smarcel offsetof(struct trapframe, tf_rsp), 102149957Smarcel offsetof(struct trapframe, tf_r8), 103149957Smarcel offsetof(struct trapframe, tf_r9), 104149957Smarcel offsetof(struct trapframe, tf_r10), 105149957Smarcel offsetof(struct trapframe, tf_r11), 106149957Smarcel offsetof(struct trapframe, tf_r12), 107149957Smarcel offsetof(struct trapframe, tf_r13), 108149957Smarcel offsetof(struct trapframe, tf_r14), 109149957Smarcel offsetof(struct trapframe, tf_r15), 110149957Smarcel offsetof(struct trapframe, tf_rip), 111149957Smarcel offsetof(struct trapframe, tf_rflags), 112149957Smarcel offsetof(struct trapframe, tf_cs), 113149957Smarcel offsetof(struct trapframe, tf_ss) 114149957Smarcel}; 115149957Smarcel 116149957Smarcelstatic struct kgdb_frame_cache * 117149957Smarcelkgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache) 118149957Smarcel{ 119149957Smarcel char buf[MAX_REGISTER_SIZE]; 120149957Smarcel struct kgdb_frame_cache *cache; 121149957Smarcel 122149957Smarcel cache = *this_cache; 123149957Smarcel if (cache == NULL) { 124149957Smarcel cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache); 125149957Smarcel *this_cache = cache; 126149957Smarcel cache->pc = frame_func_unwind(next_frame); 127149957Smarcel frame_unwind_register(next_frame, SP_REGNUM, buf); 128149957Smarcel cache->sp = extract_unsigned_integer(buf, 129149957Smarcel register_size(current_gdbarch, SP_REGNUM)); 130149957Smarcel } 131149957Smarcel return (cache); 132149957Smarcel} 133149957Smarcel 134149954Smarcelstatic void 135149954Smarcelkgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache, 136149954Smarcel struct frame_id *this_id) 137149954Smarcel{ 138149957Smarcel struct kgdb_frame_cache *cache; 139149957Smarcel 140149957Smarcel cache = kgdb_trgt_frame_cache(next_frame, this_cache); 141149957Smarcel *this_id = frame_id_build(cache->sp, cache->pc); 142149954Smarcel} 143149954Smarcel 144149954Smarcelstatic void 145149954Smarcelkgdb_trgt_trapframe_prev_register(struct frame_info *next_frame, 146149954Smarcel void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp, 147149954Smarcel CORE_ADDR *addrp, int *realnump, void *valuep) 148149954Smarcel{ 149149957Smarcel char dummy_valuep[MAX_REGISTER_SIZE]; 150149957Smarcel struct kgdb_frame_cache *cache; 151149957Smarcel int ofs, regsz; 152149957Smarcel 153149957Smarcel regsz = register_size(current_gdbarch, regnum); 154149957Smarcel 155149957Smarcel if (valuep == NULL) 156149957Smarcel valuep = dummy_valuep; 157149957Smarcel memset(valuep, 0, regsz); 158149957Smarcel *optimizedp = 0; 159149957Smarcel *addrp = 0; 160149957Smarcel *lvalp = not_lval; 161149957Smarcel *realnump = -1; 162149957Smarcel 163149957Smarcel ofs = (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_EFLAGS_REGNUM + 2) 164149957Smarcel ? kgdb_trgt_frame_offset[regnum] : -1; 165149957Smarcel if (ofs == -1) 166149957Smarcel return; 167149957Smarcel 168161555Sjhb cache = kgdb_trgt_frame_cache(next_frame, this_cache); 169149957Smarcel *addrp = cache->sp + ofs; 170149957Smarcel *lvalp = lval_memory; 171149957Smarcel target_read_memory(*addrp, valuep, regsz); 172149954Smarcel} 173149954Smarcel 174149954Smarcelstatic const struct frame_unwind kgdb_trgt_trapframe_unwind = { 175149954Smarcel UNKNOWN_FRAME, 176149954Smarcel &kgdb_trgt_trapframe_this_id, 177149954Smarcel &kgdb_trgt_trapframe_prev_register 178149954Smarcel}; 179149954Smarcel 180149954Smarcelconst struct frame_unwind * 181149954Smarcelkgdb_trgt_trapframe_sniffer(struct frame_info *next_frame) 182149954Smarcel{ 183149957Smarcel char *pname; 184149957Smarcel CORE_ADDR pc; 185149954Smarcel 186149957Smarcel pc = frame_pc_unwind(next_frame); 187149957Smarcel pname = NULL; 188149957Smarcel find_pc_partial_function(pc, &pname, NULL, NULL); 189149957Smarcel if (pname == NULL) 190149957Smarcel return (NULL); 191149976Smarcel if (strcmp(pname, "calltrap") == 0 || 192171924Sjhb strcmp(pname, "nmi_calltrap") == 0 || 193149976Smarcel (pname[0] == 'X' && pname[1] != '_')) 194149957Smarcel return (&kgdb_trgt_trapframe_unwind); 195149957Smarcel /* printf("%s: %lx =%s\n", __func__, pc, pname); */ 196149957Smarcel return (NULL); 197149954Smarcel} 198