stacktrace.cc revision 5222
15222Sksewell@umich.edu/* 25222Sksewell@umich.edu * Copyright .AN) 2007 MIPS Technologies, Inc. All Rights Reserved 35222Sksewell@umich.edu * 45222Sksewell@umich.edu * This software is part of the M5 simulator. 55222Sksewell@umich.edu * 65222Sksewell@umich.edu * THIS IS A LEGAL AGREEMENT. BY DOWNLOADING, USING, COPYING, CREATING 75222Sksewell@umich.edu * DERIVATIVE WORKS, AND/OR DISTRIBUTING THIS SOFTWARE YOU ARE AGREEING 85222Sksewell@umich.edu * TO THESE TERMS AND CONDITIONS. 95222Sksewell@umich.edu * 105222Sksewell@umich.edu * Permission is granted to use, copy, create derivative works and 115222Sksewell@umich.edu * distribute this software and such derivative works for any purpose, 125222Sksewell@umich.edu * so long as (1) the copyright notice above, this grant of permission, 135222Sksewell@umich.edu * and the disclaimer below appear in all copies and derivative works 145222Sksewell@umich.edu * made, (2) the copyright notice above is augmented as appropriate to 155222Sksewell@umich.edu * reflect the addition of any new copyrightable work in a derivative 165222Sksewell@umich.edu * work (e.g., Copyright .AN) <Publication Year> Copyright Owner), and (3) 175222Sksewell@umich.edu * the name of MIPS Technologies, Inc. ($B!H(BMIPS$B!I(B) is not used in any 185222Sksewell@umich.edu * advertising or publicity pertaining to the use or distribution of 195222Sksewell@umich.edu * this software without specific, written prior authorization. 205222Sksewell@umich.edu * 215222Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED $B!H(BAS IS.$B!I(B MIPS MAKES NO WARRANTIES AND 225222Sksewell@umich.edu * DISCLAIMS ALL WARRANTIES, WHETHER EXPRESS, STATUTORY, IMPLIED OR 235222Sksewell@umich.edu * OTHERWISE, INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 245222Sksewell@umich.edu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND 255222Sksewell@umich.edu * NON-INFRINGEMENT OF THIRD PARTY RIGHTS, REGARDING THIS SOFTWARE. 265222Sksewell@umich.edu * IN NO EVENT SHALL MIPS BE LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, 275222Sksewell@umich.edu * INDIRECT, INCIDENTAL, CONSEQUENTIAL, SPECIAL, OR PUNITIVE DAMAGES OF 285222Sksewell@umich.edu * ANY KIND OR NATURE, ARISING OUT OF OR IN CONNECTION WITH THIS AGREEMENT, 295222Sksewell@umich.edu * THIS SOFTWARE AND/OR THE USE OF THIS SOFTWARE, WHETHER SUCH LIABILITY 305222Sksewell@umich.edu * IS ASSERTED ON THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE OR 315222Sksewell@umich.edu * STRICT LIABILITY), OR OTHERWISE, EVEN IF MIPS HAS BEEN WARNED OF THE 325222Sksewell@umich.edu * POSSIBILITY OF ANY SUCH LOSS OR DAMAGE IN ADVANCE. 335222Sksewell@umich.edu * 345222Sksewell@umich.edu * Authors: Nathan L. Binkert 355222Sksewell@umich.edu */ 365222Sksewell@umich.edu 375222Sksewell@umich.edu#include <string> 385222Sksewell@umich.edu 395222Sksewell@umich.edu#include "arch/mips/isa_traits.hh" 405222Sksewell@umich.edu#include "arch/mips/stacktrace.hh" 415222Sksewell@umich.edu#include "arch/mips/vtophys.hh" 425222Sksewell@umich.edu#include "base/bitfield.hh" 435222Sksewell@umich.edu#include "base/trace.hh" 445222Sksewell@umich.edu#include "cpu/base.hh" 455222Sksewell@umich.edu#include "cpu/thread_context.hh" 465222Sksewell@umich.edu#include "sim/system.hh" 475222Sksewell@umich.edu 485222Sksewell@umich.eduusing namespace std; 495222Sksewell@umich.eduusing namespace MipsISA; 505222Sksewell@umich.edu 515222Sksewell@umich.eduProcessInfo::ProcessInfo(ThreadContext *_tc) 525222Sksewell@umich.edu : tc(_tc) 535222Sksewell@umich.edu{ 545222Sksewell@umich.edu// Addr addr = 0; 555222Sksewell@umich.edu 565222Sksewell@umich.edu VirtualPort *vp; 575222Sksewell@umich.edu 585222Sksewell@umich.edu vp = tc->getVirtPort(); 595222Sksewell@umich.edu 605222Sksewell@umich.edu// if (!tc->getSystemPtr()->kernelSymtab->findAddress("thread_info_size", addr)) 615222Sksewell@umich.edu// panic("thread info not compiled into kernel\n"); 625222Sksewell@umich.edu// thread_info_size = vp->readGtoH<int32_t>(addr); 635222Sksewell@umich.edu 645222Sksewell@umich.edu// if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_size", addr)) 655222Sksewell@umich.edu// panic("thread info not compiled into kernel\n"); 665222Sksewell@umich.edu// task_struct_size = vp->readGtoH<int32_t>(addr); 675222Sksewell@umich.edu 685222Sksewell@umich.edu// if (!tc->getSystemPtr()->kernelSymtab->findAddress("thread_info_task", addr)) 695222Sksewell@umich.edu// panic("thread info not compiled into kernel\n"); 705222Sksewell@umich.edu// task_off = vp->readGtoH<int32_t>(addr); 715222Sksewell@umich.edu 725222Sksewell@umich.edu// if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_pid", addr)) 735222Sksewell@umich.edu// panic("thread info not compiled into kernel\n"); 745222Sksewell@umich.edu// pid_off = vp->readGtoH<int32_t>(addr); 755222Sksewell@umich.edu 765222Sksewell@umich.edu// if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_comm", addr)) 775222Sksewell@umich.edu// panic("thread info not compiled into kernel\n"); 785222Sksewell@umich.edu// name_off = vp->readGtoH<int32_t>(addr); 795222Sksewell@umich.edu 805222Sksewell@umich.edu tc->delVirtPort(vp); 815222Sksewell@umich.edu} 825222Sksewell@umich.edu 835222Sksewell@umich.eduAddr 845222Sksewell@umich.eduProcessInfo::task(Addr ksp) const 855222Sksewell@umich.edu{ 865222Sksewell@umich.edu Addr base = ksp & ~0x3fff; 875222Sksewell@umich.edu if (base == ULL(0xfffffc0000000000)) 885222Sksewell@umich.edu return 0; 895222Sksewell@umich.edu 905222Sksewell@umich.edu Addr tsk; 915222Sksewell@umich.edu 925222Sksewell@umich.edu VirtualPort *vp; 935222Sksewell@umich.edu 945222Sksewell@umich.edu vp = tc->getVirtPort(); 955222Sksewell@umich.edu tsk = vp->readGtoH<Addr>(base + task_off); 965222Sksewell@umich.edu tc->delVirtPort(vp); 975222Sksewell@umich.edu 985222Sksewell@umich.edu return tsk; 995222Sksewell@umich.edu} 1005222Sksewell@umich.edu 1015222Sksewell@umich.eduint 1025222Sksewell@umich.eduProcessInfo::pid(Addr ksp) const 1035222Sksewell@umich.edu{ 1045222Sksewell@umich.edu Addr task = this->task(ksp); 1055222Sksewell@umich.edu if (!task) 1065222Sksewell@umich.edu return -1; 1075222Sksewell@umich.edu 1085222Sksewell@umich.edu uint16_t pd; 1095222Sksewell@umich.edu 1105222Sksewell@umich.edu VirtualPort *vp; 1115222Sksewell@umich.edu 1125222Sksewell@umich.edu vp = tc->getVirtPort(); 1135222Sksewell@umich.edu pd = vp->readGtoH<uint16_t>(task + pid_off); 1145222Sksewell@umich.edu tc->delVirtPort(vp); 1155222Sksewell@umich.edu 1165222Sksewell@umich.edu return pd; 1175222Sksewell@umich.edu} 1185222Sksewell@umich.edu 1195222Sksewell@umich.edustring 1205222Sksewell@umich.eduProcessInfo::name(Addr ksp) const 1215222Sksewell@umich.edu{ 1225222Sksewell@umich.edu Addr task = this->task(ksp); 1235222Sksewell@umich.edu if (!task) 1245222Sksewell@umich.edu return "console"; 1255222Sksewell@umich.edu 1265222Sksewell@umich.edu char comm[256]; 1275222Sksewell@umich.edu CopyStringOut(tc, comm, task + name_off, sizeof(comm)); 1285222Sksewell@umich.edu if (!comm[0]) 1295222Sksewell@umich.edu return "startup"; 1305222Sksewell@umich.edu 1315222Sksewell@umich.edu return comm; 1325222Sksewell@umich.edu} 1335222Sksewell@umich.edu 1345222Sksewell@umich.eduStackTrace::StackTrace() 1355222Sksewell@umich.edu : tc(0), stack(64) 1365222Sksewell@umich.edu{ 1375222Sksewell@umich.edu} 1385222Sksewell@umich.edu 1395222Sksewell@umich.eduStackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst) 1405222Sksewell@umich.edu : tc(0), stack(64) 1415222Sksewell@umich.edu{ 1425222Sksewell@umich.edu trace(_tc, inst); 1435222Sksewell@umich.edu} 1445222Sksewell@umich.edu 1455222Sksewell@umich.eduStackTrace::~StackTrace() 1465222Sksewell@umich.edu{ 1475222Sksewell@umich.edu} 1485222Sksewell@umich.edu 1495222Sksewell@umich.eduvoid 1505222Sksewell@umich.eduStackTrace::trace(ThreadContext *_tc, bool is_call) 1515222Sksewell@umich.edu{ 1525222Sksewell@umich.edu tc = _tc; 1535222Sksewell@umich.edu /* FIXME - Jaidev - What is IPR_DTB_CM in Alpha? */ 1545222Sksewell@umich.edu bool usermode = 0; 1555222Sksewell@umich.edu //(tc->readMiscReg(MipsISA::IPR_DTB_CM) & 0x18) != 0; 1565222Sksewell@umich.edu 1575222Sksewell@umich.edu// Addr pc = tc->readNextPC(); 1585222Sksewell@umich.edu// bool kernel = tc->getSystemPtr()->kernelStart <= pc && 1595222Sksewell@umich.edu// pc <= tc->getSystemPtr()->kernelEnd; 1605222Sksewell@umich.edu 1615222Sksewell@umich.edu if (usermode) { 1625222Sksewell@umich.edu stack.push_back(user); 1635222Sksewell@umich.edu return; 1645222Sksewell@umich.edu } 1655222Sksewell@umich.edu 1665222Sksewell@umich.edu// if (!kernel) { 1675222Sksewell@umich.edu// stack.push_back(console); 1685222Sksewell@umich.edu// return; 1695222Sksewell@umich.edu// } 1705222Sksewell@umich.edu 1715222Sksewell@umich.edu// SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab; 1725222Sksewell@umich.edu// Addr ksp = tc->readIntReg(TheISA::StackPointerReg); 1735222Sksewell@umich.edu// Addr bottom = ksp & ~0x3fff; 1745222Sksewell@umich.edu// Addr addr; 1755222Sksewell@umich.edu 1765222Sksewell@umich.edu// if (is_call) { 1775222Sksewell@umich.edu// if (!symtab->findNearestAddr(pc, addr)) 1785222Sksewell@umich.edu// panic("could not find address %#x", pc); 1795222Sksewell@umich.edu 1805222Sksewell@umich.edu// stack.push_back(addr); 1815222Sksewell@umich.edu// pc = tc->readPC(); 1825222Sksewell@umich.edu// } 1835222Sksewell@umich.edu 1845222Sksewell@umich.edu// Addr ra; 1855222Sksewell@umich.edu// int size; 1865222Sksewell@umich.edu 1875222Sksewell@umich.edu// while (ksp > bottom) { 1885222Sksewell@umich.edu// if (!symtab->findNearestAddr(pc, addr)) 1895222Sksewell@umich.edu// panic("could not find symbol for pc=%#x", pc); 1905222Sksewell@umich.edu// assert(pc >= addr && "symbol botch: callpc < func"); 1915222Sksewell@umich.edu 1925222Sksewell@umich.edu// stack.push_back(addr); 1935222Sksewell@umich.edu 1945222Sksewell@umich.edu// if (isEntry(addr)) 1955222Sksewell@umich.edu// return; 1965222Sksewell@umich.edu 1975222Sksewell@umich.edu// if (decodePrologue(ksp, pc, addr, size, ra)) { 1985222Sksewell@umich.edu// if (!ra) 1995222Sksewell@umich.edu// return; 2005222Sksewell@umich.edu 2015222Sksewell@umich.edu// if (size <= 0) { 2025222Sksewell@umich.edu// stack.push_back(unknown); 2035222Sksewell@umich.edu// return; 2045222Sksewell@umich.edu// } 2055222Sksewell@umich.edu 2065222Sksewell@umich.edu// pc = ra; 2075222Sksewell@umich.edu// ksp += size; 2085222Sksewell@umich.edu// } else { 2095222Sksewell@umich.edu// stack.push_back(unknown); 2105222Sksewell@umich.edu// return; 2115222Sksewell@umich.edu// } 2125222Sksewell@umich.edu 2135222Sksewell@umich.edu// bool kernel = tc->getSystemPtr()->kernelStart <= pc && 2145222Sksewell@umich.edu// pc <= tc->getSystemPtr()->kernelEnd; 2155222Sksewell@umich.edu// if (!kernel) 2165222Sksewell@umich.edu// return; 2175222Sksewell@umich.edu 2185222Sksewell@umich.edu// if (stack.size() >= 1000) 2195222Sksewell@umich.edu// panic("unwinding too far"); 2205222Sksewell@umich.edu// } 2215222Sksewell@umich.edu 2225222Sksewell@umich.edu// panic("unwinding too far"); 2235222Sksewell@umich.edu} 2245222Sksewell@umich.edu 2255222Sksewell@umich.edubool 2265222Sksewell@umich.eduStackTrace::isEntry(Addr addr) 2275222Sksewell@umich.edu{ 2285222Sksewell@umich.edu /* if (addr == tc->readMiscReg(MipsISA::IPR_PALtemp2)) 2295222Sksewell@umich.edu return true;*/ 2305222Sksewell@umich.edu 2315222Sksewell@umich.edu return false; 2325222Sksewell@umich.edu} 2335222Sksewell@umich.edu 2345222Sksewell@umich.edubool 2355222Sksewell@umich.eduStackTrace::decodeStack(MachInst inst, int &disp) 2365222Sksewell@umich.edu{ 2375222Sksewell@umich.edu // lda $sp, -disp($sp) 2385222Sksewell@umich.edu // 2395222Sksewell@umich.edu // Opcode<31:26> == 0x08 2405222Sksewell@umich.edu // RA<25:21> == 30 2415222Sksewell@umich.edu // RB<20:16> == 30 2425222Sksewell@umich.edu // Disp<15:0> 2435222Sksewell@umich.edu const MachInst mem_mask = 0xffff0000; 2445222Sksewell@umich.edu const MachInst lda_pattern = 0x23de0000; 2455222Sksewell@umich.edu const MachInst lda_disp_mask = 0x0000ffff; 2465222Sksewell@umich.edu 2475222Sksewell@umich.edu // subq $sp, disp, $sp 2485222Sksewell@umich.edu // addq $sp, disp, $sp 2495222Sksewell@umich.edu // 2505222Sksewell@umich.edu // Opcode<31:26> == 0x10 2515222Sksewell@umich.edu // RA<25:21> == 30 2525222Sksewell@umich.edu // Lit<20:13> 2535222Sksewell@umich.edu // One<12> = 1 2545222Sksewell@umich.edu // Func<11:5> == 0x20 (addq) 2555222Sksewell@umich.edu // Func<11:5> == 0x29 (subq) 2565222Sksewell@umich.edu // RC<4:0> == 30 2575222Sksewell@umich.edu const MachInst intop_mask = 0xffe01fff; 2585222Sksewell@umich.edu const MachInst addq_pattern = 0x43c0141e; 2595222Sksewell@umich.edu const MachInst subq_pattern = 0x43c0153e; 2605222Sksewell@umich.edu const MachInst intop_disp_mask = 0x001fe000; 2615222Sksewell@umich.edu const int intop_disp_shift = 13; 2625222Sksewell@umich.edu 2635222Sksewell@umich.edu if ((inst & mem_mask) == lda_pattern) 2645222Sksewell@umich.edu disp = -sext<16>(inst & lda_disp_mask); 2655222Sksewell@umich.edu else if ((inst & intop_mask) == addq_pattern) 2665222Sksewell@umich.edu disp = -int((inst & intop_disp_mask) >> intop_disp_shift); 2675222Sksewell@umich.edu else if ((inst & intop_mask) == subq_pattern) 2685222Sksewell@umich.edu disp = int((inst & intop_disp_mask) >> intop_disp_shift); 2695222Sksewell@umich.edu else 2705222Sksewell@umich.edu return false; 2715222Sksewell@umich.edu 2725222Sksewell@umich.edu return true; 2735222Sksewell@umich.edu} 2745222Sksewell@umich.edu 2755222Sksewell@umich.edubool 2765222Sksewell@umich.eduStackTrace::decodeSave(MachInst inst, int ®, int &disp) 2775222Sksewell@umich.edu{ 2785222Sksewell@umich.edu // lda $stq, disp($sp) 2795222Sksewell@umich.edu // 2805222Sksewell@umich.edu // Opcode<31:26> == 0x08 2815222Sksewell@umich.edu // RA<25:21> == ? 2825222Sksewell@umich.edu // RB<20:16> == 30 2835222Sksewell@umich.edu // Disp<15:0> 2845222Sksewell@umich.edu const MachInst stq_mask = 0xfc1f0000; 2855222Sksewell@umich.edu const MachInst stq_pattern = 0xb41e0000; 2865222Sksewell@umich.edu const MachInst stq_disp_mask = 0x0000ffff; 2875222Sksewell@umich.edu const MachInst reg_mask = 0x03e00000; 2885222Sksewell@umich.edu const int reg_shift = 21; 2895222Sksewell@umich.edu 2905222Sksewell@umich.edu if ((inst & stq_mask) == stq_pattern) { 2915222Sksewell@umich.edu reg = (inst & reg_mask) >> reg_shift; 2925222Sksewell@umich.edu disp = sext<16>(inst & stq_disp_mask); 2935222Sksewell@umich.edu } else { 2945222Sksewell@umich.edu return false; 2955222Sksewell@umich.edu } 2965222Sksewell@umich.edu 2975222Sksewell@umich.edu return true; 2985222Sksewell@umich.edu} 2995222Sksewell@umich.edu 3005222Sksewell@umich.edu/* 3015222Sksewell@umich.edu * Decode the function prologue for the function we're in, and note 3025222Sksewell@umich.edu * which registers are stored where, and how large the stack frame is. 3035222Sksewell@umich.edu */ 3045222Sksewell@umich.edubool 3055222Sksewell@umich.eduStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func, 3065222Sksewell@umich.edu int &size, Addr &ra) 3075222Sksewell@umich.edu{ 3085222Sksewell@umich.edu size = 0; 3095222Sksewell@umich.edu ra = 0; 3105222Sksewell@umich.edu 3115222Sksewell@umich.edu for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) { 3125222Sksewell@umich.edu MachInst inst; 3135222Sksewell@umich.edu CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst)); 3145222Sksewell@umich.edu 3155222Sksewell@umich.edu int reg, disp; 3165222Sksewell@umich.edu if (decodeStack(inst, disp)) { 3175222Sksewell@umich.edu if (size) { 3185222Sksewell@umich.edu // panic("decoding frame size again"); 3195222Sksewell@umich.edu return true; 3205222Sksewell@umich.edu } 3215222Sksewell@umich.edu size += disp; 3225222Sksewell@umich.edu } else if (decodeSave(inst, reg, disp)) { 3235222Sksewell@umich.edu if (!ra && reg == ReturnAddressReg) { 3245222Sksewell@umich.edu CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr)); 3255222Sksewell@umich.edu if (!ra) { 3265222Sksewell@umich.edu // panic("no return address value pc=%#x\n", pc); 3275222Sksewell@umich.edu return false; 3285222Sksewell@umich.edu } 3295222Sksewell@umich.edu } 3305222Sksewell@umich.edu } 3315222Sksewell@umich.edu } 3325222Sksewell@umich.edu 3335222Sksewell@umich.edu return true; 3345222Sksewell@umich.edu} 3355222Sksewell@umich.edu 3365222Sksewell@umich.edu#if TRACING_ON 3375222Sksewell@umich.eduvoid 3385222Sksewell@umich.eduStackTrace::dump() 3395222Sksewell@umich.edu{ 3405222Sksewell@umich.edu StringWrap name(tc->getCpuPtr()->name()); 3415222Sksewell@umich.edu// SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab; 3425222Sksewell@umich.edu 3435222Sksewell@umich.edu DPRINTFN("------ Stack ------\n"); 3445222Sksewell@umich.edu 3455222Sksewell@umich.edu// string symbol; 3465222Sksewell@umich.edu// for (int i = 0, size = stack.size(); i < size; ++i) { 3475222Sksewell@umich.edu// Addr addr = stack[size - i - 1]; 3485222Sksewell@umich.edu// if (addr == user) 3495222Sksewell@umich.edu// symbol = "user"; 3505222Sksewell@umich.edu// else if (addr == console) 3515222Sksewell@umich.edu// symbol = "console"; 3525222Sksewell@umich.edu// else if (addr == unknown) 3535222Sksewell@umich.edu// symbol = "unknown"; 3545222Sksewell@umich.edu// else 3555222Sksewell@umich.edu// symtab->findSymbol(addr, symbol); 3565222Sksewell@umich.edu 3575222Sksewell@umich.edu// DPRINTFN("%#x: %s\n", addr, symbol); 3585222Sksewell@umich.edu// } 3595222Sksewell@umich.edu} 3605222Sksewell@umich.edu#endif 361