stacktrace.cc revision 11793
15222Sksewell@umich.edu/* 25254Sksewell@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 35254Sksewell@umich.edu * All rights reserved. 45222Sksewell@umich.edu * 55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are 75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 145254Sksewell@umich.edu * this software without specific prior written permission. 155222Sksewell@umich.edu * 165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275222Sksewell@umich.edu * 285254Sksewell@umich.edu * Authors: Nathan Binkert 295222Sksewell@umich.edu */ 305222Sksewell@umich.edu 3111793Sbrandon.potter@amd.com#include "arch/mips/stacktrace.hh" 3211793Sbrandon.potter@amd.com 335222Sksewell@umich.edu#include <string> 345222Sksewell@umich.edu 355222Sksewell@umich.edu#include "arch/mips/isa_traits.hh" 365222Sksewell@umich.edu#include "arch/mips/vtophys.hh" 375222Sksewell@umich.edu#include "base/bitfield.hh" 385222Sksewell@umich.edu#include "base/trace.hh" 395222Sksewell@umich.edu#include "cpu/base.hh" 405222Sksewell@umich.edu#include "cpu/thread_context.hh" 418799Sgblack@eecs.umich.edu#include "mem/fs_translating_port_proxy.hh" 425222Sksewell@umich.edu#include "sim/system.hh" 435222Sksewell@umich.edu 445222Sksewell@umich.eduusing namespace std; 455222Sksewell@umich.eduusing namespace MipsISA; 465222Sksewell@umich.edu 476378Sgblack@eecs.umich.eduProcessInfo::ProcessInfo(ThreadContext *_tc) : tc(_tc) 486378Sgblack@eecs.umich.edu{} 495222Sksewell@umich.edu 505222Sksewell@umich.eduAddr 515222Sksewell@umich.eduProcessInfo::task(Addr ksp) const 525222Sksewell@umich.edu{ 535222Sksewell@umich.edu Addr base = ksp & ~0x3fff; 545222Sksewell@umich.edu if (base == ULL(0xfffffc0000000000)) 555222Sksewell@umich.edu return 0; 565222Sksewell@umich.edu 575222Sksewell@umich.edu Addr tsk; 585222Sksewell@umich.edu 598852Sandreas.hansson@arm.com FSTranslatingPortProxy &vp = tc->getVirtProxy(); 608852Sandreas.hansson@arm.com tsk = vp.readGtoH<Addr>(base + task_off); 615222Sksewell@umich.edu 625222Sksewell@umich.edu return tsk; 635222Sksewell@umich.edu} 645222Sksewell@umich.edu 655222Sksewell@umich.eduint 665222Sksewell@umich.eduProcessInfo::pid(Addr ksp) const 675222Sksewell@umich.edu{ 685222Sksewell@umich.edu Addr task = this->task(ksp); 695222Sksewell@umich.edu if (!task) 705222Sksewell@umich.edu return -1; 715222Sksewell@umich.edu 725222Sksewell@umich.edu uint16_t pd; 735222Sksewell@umich.edu 748852Sandreas.hansson@arm.com FSTranslatingPortProxy &vp = tc->getVirtProxy(); 758852Sandreas.hansson@arm.com pd = vp.readGtoH<uint16_t>(task + pid_off); 765222Sksewell@umich.edu 775222Sksewell@umich.edu return pd; 785222Sksewell@umich.edu} 795222Sksewell@umich.edu 805222Sksewell@umich.edustring 815222Sksewell@umich.eduProcessInfo::name(Addr ksp) const 825222Sksewell@umich.edu{ 835222Sksewell@umich.edu Addr task = this->task(ksp); 845222Sksewell@umich.edu if (!task) 855222Sksewell@umich.edu return "console"; 865222Sksewell@umich.edu 875222Sksewell@umich.edu char comm[256]; 885222Sksewell@umich.edu CopyStringOut(tc, comm, task + name_off, sizeof(comm)); 895222Sksewell@umich.edu if (!comm[0]) 905222Sksewell@umich.edu return "startup"; 915222Sksewell@umich.edu 925222Sksewell@umich.edu return comm; 935222Sksewell@umich.edu} 945222Sksewell@umich.edu 955222Sksewell@umich.eduStackTrace::StackTrace() 965222Sksewell@umich.edu : tc(0), stack(64) 975222Sksewell@umich.edu{ 985222Sksewell@umich.edu} 995222Sksewell@umich.edu 10010417Sandreas.hansson@arm.comStackTrace::StackTrace(ThreadContext *_tc, const StaticInstPtr &inst) 1015222Sksewell@umich.edu : tc(0), stack(64) 1025222Sksewell@umich.edu{ 1035222Sksewell@umich.edu trace(_tc, inst); 1045222Sksewell@umich.edu} 1055222Sksewell@umich.edu 1065222Sksewell@umich.eduStackTrace::~StackTrace() 1075222Sksewell@umich.edu{ 1085222Sksewell@umich.edu} 1095222Sksewell@umich.edu 1105222Sksewell@umich.eduvoid 1115222Sksewell@umich.eduStackTrace::trace(ThreadContext *_tc, bool is_call) 1125222Sksewell@umich.edu{ 1135222Sksewell@umich.edu tc = _tc; 1145222Sksewell@umich.edu bool usermode = 0; 1155222Sksewell@umich.edu 1165222Sksewell@umich.edu if (usermode) { 1175222Sksewell@umich.edu stack.push_back(user); 1185222Sksewell@umich.edu return; 1195222Sksewell@umich.edu } 1205222Sksewell@umich.edu} 1215222Sksewell@umich.edu 1225222Sksewell@umich.edubool 1235222Sksewell@umich.eduStackTrace::isEntry(Addr addr) 1245222Sksewell@umich.edu{ 1255222Sksewell@umich.edu return false; 1265222Sksewell@umich.edu} 1275222Sksewell@umich.edu 1285222Sksewell@umich.edubool 1295222Sksewell@umich.eduStackTrace::decodeStack(MachInst inst, int &disp) 1305222Sksewell@umich.edu{ 1315222Sksewell@umich.edu // lda $sp, -disp($sp) 1325222Sksewell@umich.edu // 1335222Sksewell@umich.edu // Opcode<31:26> == 0x08 1345222Sksewell@umich.edu // RA<25:21> == 30 1355222Sksewell@umich.edu // RB<20:16> == 30 1365222Sksewell@umich.edu // Disp<15:0> 1375222Sksewell@umich.edu const MachInst mem_mask = 0xffff0000; 1385222Sksewell@umich.edu const MachInst lda_pattern = 0x23de0000; 1395222Sksewell@umich.edu const MachInst lda_disp_mask = 0x0000ffff; 1405222Sksewell@umich.edu 1415222Sksewell@umich.edu // subq $sp, disp, $sp 1425222Sksewell@umich.edu // addq $sp, disp, $sp 1435222Sksewell@umich.edu // 1445222Sksewell@umich.edu // Opcode<31:26> == 0x10 1455222Sksewell@umich.edu // RA<25:21> == 30 1465222Sksewell@umich.edu // Lit<20:13> 1475222Sksewell@umich.edu // One<12> = 1 1485222Sksewell@umich.edu // Func<11:5> == 0x20 (addq) 1495222Sksewell@umich.edu // Func<11:5> == 0x29 (subq) 1505222Sksewell@umich.edu // RC<4:0> == 30 1515222Sksewell@umich.edu const MachInst intop_mask = 0xffe01fff; 1525222Sksewell@umich.edu const MachInst addq_pattern = 0x43c0141e; 1535222Sksewell@umich.edu const MachInst subq_pattern = 0x43c0153e; 1545222Sksewell@umich.edu const MachInst intop_disp_mask = 0x001fe000; 1555222Sksewell@umich.edu const int intop_disp_shift = 13; 1565222Sksewell@umich.edu 1575222Sksewell@umich.edu if ((inst & mem_mask) == lda_pattern) 1585222Sksewell@umich.edu disp = -sext<16>(inst & lda_disp_mask); 1595222Sksewell@umich.edu else if ((inst & intop_mask) == addq_pattern) 1605222Sksewell@umich.edu disp = -int((inst & intop_disp_mask) >> intop_disp_shift); 1615222Sksewell@umich.edu else if ((inst & intop_mask) == subq_pattern) 1625222Sksewell@umich.edu disp = int((inst & intop_disp_mask) >> intop_disp_shift); 1635222Sksewell@umich.edu else 1645222Sksewell@umich.edu return false; 1655222Sksewell@umich.edu 1665222Sksewell@umich.edu return true; 1675222Sksewell@umich.edu} 1685222Sksewell@umich.edu 1695222Sksewell@umich.edubool 1705222Sksewell@umich.eduStackTrace::decodeSave(MachInst inst, int ®, int &disp) 1715222Sksewell@umich.edu{ 1725222Sksewell@umich.edu // lda $stq, disp($sp) 1735222Sksewell@umich.edu // 1745222Sksewell@umich.edu // Opcode<31:26> == 0x08 1755222Sksewell@umich.edu // RA<25:21> == ? 1765222Sksewell@umich.edu // RB<20:16> == 30 1775222Sksewell@umich.edu // Disp<15:0> 1785222Sksewell@umich.edu const MachInst stq_mask = 0xfc1f0000; 1795222Sksewell@umich.edu const MachInst stq_pattern = 0xb41e0000; 1805222Sksewell@umich.edu const MachInst stq_disp_mask = 0x0000ffff; 1815222Sksewell@umich.edu const MachInst reg_mask = 0x03e00000; 1825222Sksewell@umich.edu const int reg_shift = 21; 1835222Sksewell@umich.edu 1845222Sksewell@umich.edu if ((inst & stq_mask) == stq_pattern) { 1855222Sksewell@umich.edu reg = (inst & reg_mask) >> reg_shift; 1865222Sksewell@umich.edu disp = sext<16>(inst & stq_disp_mask); 1875222Sksewell@umich.edu } else { 1885222Sksewell@umich.edu return false; 1895222Sksewell@umich.edu } 1905222Sksewell@umich.edu 1915222Sksewell@umich.edu return true; 1925222Sksewell@umich.edu} 1935222Sksewell@umich.edu 1945222Sksewell@umich.edu/* 1955222Sksewell@umich.edu * Decode the function prologue for the function we're in, and note 1965222Sksewell@umich.edu * which registers are stored where, and how large the stack frame is. 1975222Sksewell@umich.edu */ 1985222Sksewell@umich.edubool 1995222Sksewell@umich.eduStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func, 2005222Sksewell@umich.edu int &size, Addr &ra) 2015222Sksewell@umich.edu{ 2025222Sksewell@umich.edu size = 0; 2035222Sksewell@umich.edu ra = 0; 2045222Sksewell@umich.edu 2055222Sksewell@umich.edu for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) { 2065222Sksewell@umich.edu MachInst inst; 2075222Sksewell@umich.edu CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst)); 2085222Sksewell@umich.edu 2095222Sksewell@umich.edu int reg, disp; 2105222Sksewell@umich.edu if (decodeStack(inst, disp)) { 2115222Sksewell@umich.edu if (size) { 2125222Sksewell@umich.edu return true; 2135222Sksewell@umich.edu } 2145222Sksewell@umich.edu size += disp; 2155222Sksewell@umich.edu } else if (decodeSave(inst, reg, disp)) { 2165222Sksewell@umich.edu if (!ra && reg == ReturnAddressReg) { 2175222Sksewell@umich.edu CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr)); 2185222Sksewell@umich.edu if (!ra) { 2195222Sksewell@umich.edu return false; 2205222Sksewell@umich.edu } 2215222Sksewell@umich.edu } 2225222Sksewell@umich.edu } 2235222Sksewell@umich.edu } 2245222Sksewell@umich.edu 2255222Sksewell@umich.edu return true; 2265222Sksewell@umich.edu} 2275222Sksewell@umich.edu 2285222Sksewell@umich.edu#if TRACING_ON 2295222Sksewell@umich.eduvoid 2305222Sksewell@umich.eduStackTrace::dump() 2315222Sksewell@umich.edu{ 2326378Sgblack@eecs.umich.edu panic("Stack trace dump not implemented.\n"); 2335222Sksewell@umich.edu} 2345222Sksewell@umich.edu#endif 235