stacktrace.cc revision 7720
11917SN/A/* 21917SN/A * Copyright (c) 2005 The Regents of The University of Michigan 31917SN/A * All rights reserved. 41917SN/A * 51917SN/A * Redistribution and use in source and binary forms, with or without 61917SN/A * modification, are permitted provided that the following conditions are 71917SN/A * met: redistributions of source code must retain the above copyright 81917SN/A * notice, this list of conditions and the following disclaimer; 91917SN/A * redistributions in binary form must reproduce the above copyright 101917SN/A * notice, this list of conditions and the following disclaimer in the 111917SN/A * documentation and/or other materials provided with the distribution; 121917SN/A * neither the name of the copyright holders nor the names of its 131917SN/A * contributors may be used to endorse or promote products derived from 141917SN/A * this software without specific prior written permission. 151917SN/A * 161917SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171917SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181917SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191917SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201917SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211917SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221917SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231917SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241917SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251917SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261917SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 291917SN/A */ 301917SN/A 311917SN/A#include <string> 321917SN/A 331917SN/A#include "arch/alpha/isa_traits.hh" 341917SN/A#include "arch/alpha/stacktrace.hh" 351917SN/A#include "arch/alpha/vtophys.hh" 361917SN/A#include "base/bitfield.hh" 371917SN/A#include "base/trace.hh" 381917SN/A#include "cpu/base.hh" 392680Sktlim@umich.edu#include "cpu/thread_context.hh" 402235SN/A#include "sim/system.hh" 411917SN/A 421917SN/Ausing namespace std; 431917SN/A 445569Snate@binkert.orgnamespace AlphaISA { 455569Snate@binkert.org 465569Snate@binkert.orgProcessInfo::ProcessInfo(ThreadContext *_tc) 475569Snate@binkert.org : tc(_tc) 481917SN/A{ 495569Snate@binkert.org Addr addr = 0; 505569Snate@binkert.org VirtualPort *vp = tc->getVirtPort(); 515569Snate@binkert.org SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab; 521917SN/A 535569Snate@binkert.org if (!symtab->findAddress("thread_info_size", addr)) 545569Snate@binkert.org panic("thread info not compiled into kernel\n"); 555569Snate@binkert.org thread_info_size = vp->readGtoH<int32_t>(addr); 562684Ssaidi@eecs.umich.edu 575569Snate@binkert.org if (!symtab->findAddress("task_struct_size", addr)) 585569Snate@binkert.org panic("thread info not compiled into kernel\n"); 595569Snate@binkert.org task_struct_size = vp->readGtoH<int32_t>(addr); 602684Ssaidi@eecs.umich.edu 615569Snate@binkert.org if (!symtab->findAddress("thread_info_task", addr)) 625569Snate@binkert.org panic("thread info not compiled into kernel\n"); 635569Snate@binkert.org task_off = vp->readGtoH<int32_t>(addr); 641917SN/A 655569Snate@binkert.org if (!symtab->findAddress("task_struct_pid", addr)) 665569Snate@binkert.org panic("thread info not compiled into kernel\n"); 675569Snate@binkert.org pid_off = vp->readGtoH<int32_t>(addr); 681917SN/A 695569Snate@binkert.org if (!symtab->findAddress("task_struct_comm", addr)) 705569Snate@binkert.org panic("thread info not compiled into kernel\n"); 715569Snate@binkert.org name_off = vp->readGtoH<int32_t>(addr); 725569Snate@binkert.org} 731917SN/A 745569Snate@binkert.orgAddr 755569Snate@binkert.orgProcessInfo::task(Addr ksp) const 765569Snate@binkert.org{ 775569Snate@binkert.org Addr base = ksp & ~0x3fff; 785569Snate@binkert.org if (base == ULL(0xfffffc0000000000)) 795569Snate@binkert.org return 0; 801917SN/A 815569Snate@binkert.org Addr tsk; 825569Snate@binkert.org 835569Snate@binkert.org VirtualPort *vp; 845569Snate@binkert.org 855569Snate@binkert.org vp = tc->getVirtPort(); 865569Snate@binkert.org tsk = vp->readGtoH<Addr>(base + task_off); 875569Snate@binkert.org 885569Snate@binkert.org return tsk; 895569Snate@binkert.org} 905569Snate@binkert.org 915569Snate@binkert.orgint 925569Snate@binkert.orgProcessInfo::pid(Addr ksp) const 935569Snate@binkert.org{ 945569Snate@binkert.org Addr task = this->task(ksp); 955569Snate@binkert.org if (!task) 965569Snate@binkert.org return -1; 975569Snate@binkert.org 985569Snate@binkert.org uint16_t pd; 995569Snate@binkert.org 1005569Snate@binkert.org VirtualPort *vp; 1015569Snate@binkert.org 1025569Snate@binkert.org vp = tc->getVirtPort(); 1035569Snate@binkert.org pd = vp->readGtoH<uint16_t>(task + pid_off); 1045569Snate@binkert.org 1055569Snate@binkert.org return pd; 1065569Snate@binkert.org} 1075569Snate@binkert.org 1085569Snate@binkert.orgstring 1095569Snate@binkert.orgProcessInfo::name(Addr ksp) const 1105569Snate@binkert.org{ 1115569Snate@binkert.org Addr task = this->task(ksp); 1125569Snate@binkert.org if (!task) 1135569Snate@binkert.org return "console"; 1145569Snate@binkert.org 1155569Snate@binkert.org char comm[256]; 1165569Snate@binkert.org CopyStringOut(tc, comm, task + name_off, sizeof(comm)); 1175569Snate@binkert.org if (!comm[0]) 1185569Snate@binkert.org return "startup"; 1195569Snate@binkert.org 1205569Snate@binkert.org return comm; 1215569Snate@binkert.org} 1225569Snate@binkert.org 1235569Snate@binkert.orgStackTrace::StackTrace() 1245569Snate@binkert.org : tc(0), stack(64) 1255569Snate@binkert.org{ 1265569Snate@binkert.org} 1275569Snate@binkert.org 1285569Snate@binkert.orgStackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst) 1295569Snate@binkert.org : tc(0), stack(64) 1305569Snate@binkert.org{ 1315569Snate@binkert.org trace(_tc, inst); 1325569Snate@binkert.org} 1335569Snate@binkert.org 1345569Snate@binkert.orgStackTrace::~StackTrace() 1355569Snate@binkert.org{ 1365569Snate@binkert.org} 1375569Snate@binkert.org 1385569Snate@binkert.orgvoid 1395569Snate@binkert.orgStackTrace::trace(ThreadContext *_tc, bool is_call) 1405569Snate@binkert.org{ 1415569Snate@binkert.org tc = _tc; 1425569Snate@binkert.org 1435569Snate@binkert.org System *sys = tc->getSystemPtr(); 1445569Snate@binkert.org 1455569Snate@binkert.org bool usermode = 1465569Snate@binkert.org (tc->readMiscRegNoEffect(IPR_DTB_CM) & 0x18) != 0; 1475569Snate@binkert.org 1487720Sgblack@eecs.umich.edu Addr pc = tc->pcState().pc(); 1495569Snate@binkert.org bool kernel = sys->kernelStart <= pc && pc <= sys->kernelEnd; 1505569Snate@binkert.org 1515569Snate@binkert.org if (usermode) { 1525569Snate@binkert.org stack.push_back(user); 1535569Snate@binkert.org return; 1541917SN/A } 1551917SN/A 1565569Snate@binkert.org if (!kernel) { 1575569Snate@binkert.org stack.push_back(console); 1585569Snate@binkert.org return; 1591917SN/A } 1601917SN/A 1615569Snate@binkert.org SymbolTable *symtab = sys->kernelSymtab; 1625569Snate@binkert.org Addr ksp = tc->readIntReg(StackPointerReg); 1635569Snate@binkert.org Addr bottom = ksp & ~0x3fff; 1641917SN/A 1655569Snate@binkert.org if (is_call) { 1665569Snate@binkert.org Addr addr; 1675569Snate@binkert.org if (!symtab->findNearestAddr(pc, addr)) 1685569Snate@binkert.org panic("could not find address %#x", pc); 1691917SN/A 1705569Snate@binkert.org stack.push_back(addr); 1717720Sgblack@eecs.umich.edu pc = tc->pcState().pc(); 1721917SN/A } 1731917SN/A 1745569Snate@binkert.org while (ksp > bottom) { 1755569Snate@binkert.org Addr addr; 1765569Snate@binkert.org if (!symtab->findNearestAddr(pc, addr)) 1775569Snate@binkert.org panic("could not find symbol for pc=%#x", pc); 1785569Snate@binkert.org assert(pc >= addr && "symbol botch: callpc < func"); 1791917SN/A 1805569Snate@binkert.org stack.push_back(addr); 1811917SN/A 1825569Snate@binkert.org if (isEntry(addr)) 1831917SN/A return; 1843570Sgblack@eecs.umich.edu 1853570Sgblack@eecs.umich.edu Addr ra; 1863570Sgblack@eecs.umich.edu int size; 1875569Snate@binkert.org if (decodePrologue(ksp, pc, addr, size, ra)) { 1885569Snate@binkert.org if (!ra) 1891917SN/A return; 1901917SN/A 1915569Snate@binkert.org if (size <= 0) { 1921977SN/A stack.push_back(unknown); 1931977SN/A return; 1941977SN/A } 1951977SN/A 1965569Snate@binkert.org pc = ra; 1975569Snate@binkert.org ksp += size; 1985569Snate@binkert.org } else { 1995569Snate@binkert.org stack.push_back(unknown); 2005569Snate@binkert.org return; 2011917SN/A } 2021917SN/A 2035569Snate@binkert.org bool kernel = sys->kernelStart <= pc && pc <= sys->kernelEnd; 2045569Snate@binkert.org if (!kernel) 2055569Snate@binkert.org return; 2065569Snate@binkert.org 2075569Snate@binkert.org if (stack.size() >= 1000) 2085569Snate@binkert.org panic("unwinding too far"); 2091917SN/A } 2101917SN/A 2115569Snate@binkert.org panic("unwinding too far"); 2125569Snate@binkert.org} 2131917SN/A 2145569Snate@binkert.orgbool 2155569Snate@binkert.orgStackTrace::isEntry(Addr addr) 2165569Snate@binkert.org{ 2175569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp12)) 2185569Snate@binkert.org return true; 2191917SN/A 2205569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp7)) 2215569Snate@binkert.org return true; 2221917SN/A 2235569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp11)) 2245569Snate@binkert.org return true; 2251917SN/A 2265569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp21)) 2275569Snate@binkert.org return true; 2281917SN/A 2295569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp9)) 2305569Snate@binkert.org return true; 2311917SN/A 2325569Snate@binkert.org if (addr == tc->readMiscRegNoEffect(IPR_PALtemp2)) 2335569Snate@binkert.org return true; 2345569Snate@binkert.org 2355569Snate@binkert.org return false; 2365569Snate@binkert.org} 2375569Snate@binkert.org 2385569Snate@binkert.orgbool 2395569Snate@binkert.orgStackTrace::decodeStack(MachInst inst, int &disp) 2405569Snate@binkert.org{ 2415569Snate@binkert.org // lda $sp, -disp($sp) 2425569Snate@binkert.org // 2435569Snate@binkert.org // Opcode<31:26> == 0x08 2445569Snate@binkert.org // RA<25:21> == 30 2455569Snate@binkert.org // RB<20:16> == 30 2465569Snate@binkert.org // Disp<15:0> 2475569Snate@binkert.org const MachInst mem_mask = 0xffff0000; 2485569Snate@binkert.org const MachInst lda_pattern = 0x23de0000; 2495569Snate@binkert.org const MachInst lda_disp_mask = 0x0000ffff; 2505569Snate@binkert.org 2515569Snate@binkert.org // subq $sp, disp, $sp 2525569Snate@binkert.org // addq $sp, disp, $sp 2535569Snate@binkert.org // 2545569Snate@binkert.org // Opcode<31:26> == 0x10 2555569Snate@binkert.org // RA<25:21> == 30 2565569Snate@binkert.org // Lit<20:13> 2575569Snate@binkert.org // One<12> = 1 2585569Snate@binkert.org // Func<11:5> == 0x20 (addq) 2595569Snate@binkert.org // Func<11:5> == 0x29 (subq) 2605569Snate@binkert.org // RC<4:0> == 30 2615569Snate@binkert.org const MachInst intop_mask = 0xffe01fff; 2625569Snate@binkert.org const MachInst addq_pattern = 0x43c0141e; 2635569Snate@binkert.org const MachInst subq_pattern = 0x43c0153e; 2645569Snate@binkert.org const MachInst intop_disp_mask = 0x001fe000; 2655569Snate@binkert.org const int intop_disp_shift = 13; 2665569Snate@binkert.org 2675569Snate@binkert.org if ((inst & mem_mask) == lda_pattern) 2685569Snate@binkert.org disp = -sext<16>(inst & lda_disp_mask); 2695569Snate@binkert.org else if ((inst & intop_mask) == addq_pattern) 2705569Snate@binkert.org disp = -int((inst & intop_disp_mask) >> intop_disp_shift); 2715569Snate@binkert.org else if ((inst & intop_mask) == subq_pattern) 2725569Snate@binkert.org disp = int((inst & intop_disp_mask) >> intop_disp_shift); 2735569Snate@binkert.org else 2745569Snate@binkert.org return false; 2755569Snate@binkert.org 2765569Snate@binkert.org return true; 2775569Snate@binkert.org} 2785569Snate@binkert.org 2795569Snate@binkert.orgbool 2805569Snate@binkert.orgStackTrace::decodeSave(MachInst inst, int ®, int &disp) 2815569Snate@binkert.org{ 2825569Snate@binkert.org // lda $stq, disp($sp) 2835569Snate@binkert.org // 2845569Snate@binkert.org // Opcode<31:26> == 0x08 2855569Snate@binkert.org // RA<25:21> == ? 2865569Snate@binkert.org // RB<20:16> == 30 2875569Snate@binkert.org // Disp<15:0> 2885569Snate@binkert.org const MachInst stq_mask = 0xfc1f0000; 2895569Snate@binkert.org const MachInst stq_pattern = 0xb41e0000; 2905569Snate@binkert.org const MachInst stq_disp_mask = 0x0000ffff; 2915569Snate@binkert.org const MachInst reg_mask = 0x03e00000; 2925569Snate@binkert.org const int reg_shift = 21; 2935569Snate@binkert.org 2945569Snate@binkert.org if ((inst & stq_mask) == stq_pattern) { 2955569Snate@binkert.org reg = (inst & reg_mask) >> reg_shift; 2965569Snate@binkert.org disp = sext<16>(inst & stq_disp_mask); 2975569Snate@binkert.org } else { 2981917SN/A return false; 2991917SN/A } 3001917SN/A 3015569Snate@binkert.org return true; 3025569Snate@binkert.org} 3031917SN/A 3045569Snate@binkert.org/* 3055569Snate@binkert.org * Decode the function prologue for the function we're in, and note 3065569Snate@binkert.org * which registers are stored where, and how large the stack frame is. 3075569Snate@binkert.org */ 3085569Snate@binkert.orgbool 3095569Snate@binkert.orgStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func, int &size, 3105569Snate@binkert.org Addr &ra) 3115569Snate@binkert.org{ 3125569Snate@binkert.org size = 0; 3135569Snate@binkert.org ra = 0; 3141917SN/A 3155569Snate@binkert.org for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) { 3165569Snate@binkert.org MachInst inst; 3175569Snate@binkert.org CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst)); 3181917SN/A 3195569Snate@binkert.org int reg, disp; 3205569Snate@binkert.org if (decodeStack(inst, disp)) { 3215569Snate@binkert.org if (size) { 3225569Snate@binkert.org // panic("decoding frame size again"); 3235569Snate@binkert.org return true; 3245569Snate@binkert.org } 3255569Snate@binkert.org size += disp; 3265569Snate@binkert.org } else if (decodeSave(inst, reg, disp)) { 3275569Snate@binkert.org if (!ra && reg == ReturnAddressReg) { 3285569Snate@binkert.org CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr)); 3295569Snate@binkert.org if (!ra) { 3305569Snate@binkert.org // panic("no return address value pc=%#x\n", pc); 3315569Snate@binkert.org return false; 3321917SN/A } 3331917SN/A } 3341917SN/A } 3351917SN/A } 3361917SN/A 3375569Snate@binkert.org return true; 3385569Snate@binkert.org} 3395569Snate@binkert.org 3403570Sgblack@eecs.umich.edu#if TRACING_ON 3415569Snate@binkert.orgvoid 3425569Snate@binkert.orgStackTrace::dump() 3435569Snate@binkert.org{ 3445569Snate@binkert.org StringWrap name(tc->getCpuPtr()->name()); 3455569Snate@binkert.org SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab; 3463570Sgblack@eecs.umich.edu 3475569Snate@binkert.org DPRINTFN("------ Stack ------\n"); 3483570Sgblack@eecs.umich.edu 3495569Snate@binkert.org string symbol; 3505569Snate@binkert.org for (int i = 0, size = stack.size(); i < size; ++i) { 3515569Snate@binkert.org Addr addr = stack[size - i - 1]; 3525569Snate@binkert.org if (addr == user) 3535569Snate@binkert.org symbol = "user"; 3545569Snate@binkert.org else if (addr == console) 3555569Snate@binkert.org symbol = "console"; 3565569Snate@binkert.org else if (addr == unknown) 3575569Snate@binkert.org symbol = "unknown"; 3585569Snate@binkert.org else 3595569Snate@binkert.org symtab->findSymbol(addr, symbol); 3603570Sgblack@eecs.umich.edu 3615569Snate@binkert.org DPRINTFN("%#x: %s\n", addr, symbol); 3623570Sgblack@eecs.umich.edu } 3635569Snate@binkert.org} 3643570Sgblack@eecs.umich.edu#endif 3655569Snate@binkert.org 3665569Snate@binkert.org} // namespace AlphaISA 367