stacktrace.cc revision 6378
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
315222Sksewell@umich.edu#include <string>
325222Sksewell@umich.edu
335222Sksewell@umich.edu#include "arch/mips/isa_traits.hh"
345222Sksewell@umich.edu#include "arch/mips/stacktrace.hh"
355222Sksewell@umich.edu#include "arch/mips/vtophys.hh"
365222Sksewell@umich.edu#include "base/bitfield.hh"
375222Sksewell@umich.edu#include "base/trace.hh"
385222Sksewell@umich.edu#include "cpu/base.hh"
395222Sksewell@umich.edu#include "cpu/thread_context.hh"
405222Sksewell@umich.edu#include "sim/system.hh"
415222Sksewell@umich.edu
425222Sksewell@umich.eduusing namespace std;
435222Sksewell@umich.eduusing namespace MipsISA;
445222Sksewell@umich.edu
456378Sgblack@eecs.umich.eduProcessInfo::ProcessInfo(ThreadContext *_tc) : tc(_tc)
466378Sgblack@eecs.umich.edu{}
475222Sksewell@umich.edu
485222Sksewell@umich.eduAddr
495222Sksewell@umich.eduProcessInfo::task(Addr ksp) const
505222Sksewell@umich.edu{
515222Sksewell@umich.edu    Addr base = ksp & ~0x3fff;
525222Sksewell@umich.edu    if (base == ULL(0xfffffc0000000000))
535222Sksewell@umich.edu        return 0;
545222Sksewell@umich.edu
555222Sksewell@umich.edu    Addr tsk;
565222Sksewell@umich.edu
575222Sksewell@umich.edu    VirtualPort *vp;
585222Sksewell@umich.edu
595222Sksewell@umich.edu    vp = tc->getVirtPort();
605222Sksewell@umich.edu    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
745222Sksewell@umich.edu    VirtualPort *vp;
755222Sksewell@umich.edu
765222Sksewell@umich.edu    vp = tc->getVirtPort();
775222Sksewell@umich.edu    pd = vp->readGtoH<uint16_t>(task + pid_off);
785222Sksewell@umich.edu
795222Sksewell@umich.edu    return pd;
805222Sksewell@umich.edu}
815222Sksewell@umich.edu
825222Sksewell@umich.edustring
835222Sksewell@umich.eduProcessInfo::name(Addr ksp) const
845222Sksewell@umich.edu{
855222Sksewell@umich.edu    Addr task = this->task(ksp);
865222Sksewell@umich.edu    if (!task)
875222Sksewell@umich.edu        return "console";
885222Sksewell@umich.edu
895222Sksewell@umich.edu    char comm[256];
905222Sksewell@umich.edu    CopyStringOut(tc, comm, task + name_off, sizeof(comm));
915222Sksewell@umich.edu    if (!comm[0])
925222Sksewell@umich.edu        return "startup";
935222Sksewell@umich.edu
945222Sksewell@umich.edu    return comm;
955222Sksewell@umich.edu}
965222Sksewell@umich.edu
975222Sksewell@umich.eduStackTrace::StackTrace()
985222Sksewell@umich.edu    : tc(0), stack(64)
995222Sksewell@umich.edu{
1005222Sksewell@umich.edu}
1015222Sksewell@umich.edu
1025222Sksewell@umich.eduStackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst)
1035222Sksewell@umich.edu    : tc(0), stack(64)
1045222Sksewell@umich.edu{
1055222Sksewell@umich.edu    trace(_tc, inst);
1065222Sksewell@umich.edu}
1075222Sksewell@umich.edu
1085222Sksewell@umich.eduStackTrace::~StackTrace()
1095222Sksewell@umich.edu{
1105222Sksewell@umich.edu}
1115222Sksewell@umich.edu
1125222Sksewell@umich.eduvoid
1135222Sksewell@umich.eduStackTrace::trace(ThreadContext *_tc, bool is_call)
1145222Sksewell@umich.edu{
1155222Sksewell@umich.edu    tc = _tc;
1165222Sksewell@umich.edu    bool usermode = 0;
1175222Sksewell@umich.edu
1185222Sksewell@umich.edu    if (usermode) {
1195222Sksewell@umich.edu        stack.push_back(user);
1205222Sksewell@umich.edu        return;
1215222Sksewell@umich.edu    }
1225222Sksewell@umich.edu}
1235222Sksewell@umich.edu
1245222Sksewell@umich.edubool
1255222Sksewell@umich.eduStackTrace::isEntry(Addr addr)
1265222Sksewell@umich.edu{
1275222Sksewell@umich.edu    return false;
1285222Sksewell@umich.edu}
1295222Sksewell@umich.edu
1305222Sksewell@umich.edubool
1315222Sksewell@umich.eduStackTrace::decodeStack(MachInst inst, int &disp)
1325222Sksewell@umich.edu{
1335222Sksewell@umich.edu    // lda $sp, -disp($sp)
1345222Sksewell@umich.edu    //
1355222Sksewell@umich.edu    // Opcode<31:26> == 0x08
1365222Sksewell@umich.edu    // RA<25:21> == 30
1375222Sksewell@umich.edu    // RB<20:16> == 30
1385222Sksewell@umich.edu    // Disp<15:0>
1395222Sksewell@umich.edu    const MachInst mem_mask = 0xffff0000;
1405222Sksewell@umich.edu    const MachInst lda_pattern = 0x23de0000;
1415222Sksewell@umich.edu    const MachInst lda_disp_mask = 0x0000ffff;
1425222Sksewell@umich.edu
1435222Sksewell@umich.edu    // subq $sp, disp, $sp
1445222Sksewell@umich.edu    // addq $sp, disp, $sp
1455222Sksewell@umich.edu    //
1465222Sksewell@umich.edu    // Opcode<31:26> == 0x10
1475222Sksewell@umich.edu    // RA<25:21> == 30
1485222Sksewell@umich.edu    // Lit<20:13>
1495222Sksewell@umich.edu    // One<12> = 1
1505222Sksewell@umich.edu    // Func<11:5> == 0x20 (addq)
1515222Sksewell@umich.edu    // Func<11:5> == 0x29 (subq)
1525222Sksewell@umich.edu    // RC<4:0> == 30
1535222Sksewell@umich.edu    const MachInst intop_mask = 0xffe01fff;
1545222Sksewell@umich.edu    const MachInst addq_pattern = 0x43c0141e;
1555222Sksewell@umich.edu    const MachInst subq_pattern = 0x43c0153e;
1565222Sksewell@umich.edu    const MachInst intop_disp_mask = 0x001fe000;
1575222Sksewell@umich.edu    const int intop_disp_shift = 13;
1585222Sksewell@umich.edu
1595222Sksewell@umich.edu    if ((inst & mem_mask) == lda_pattern)
1605222Sksewell@umich.edu        disp = -sext<16>(inst & lda_disp_mask);
1615222Sksewell@umich.edu    else if ((inst & intop_mask) == addq_pattern)
1625222Sksewell@umich.edu        disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
1635222Sksewell@umich.edu    else if ((inst & intop_mask) == subq_pattern)
1645222Sksewell@umich.edu        disp = int((inst & intop_disp_mask) >> intop_disp_shift);
1655222Sksewell@umich.edu    else
1665222Sksewell@umich.edu        return false;
1675222Sksewell@umich.edu
1685222Sksewell@umich.edu    return true;
1695222Sksewell@umich.edu}
1705222Sksewell@umich.edu
1715222Sksewell@umich.edubool
1725222Sksewell@umich.eduStackTrace::decodeSave(MachInst inst, int &reg, int &disp)
1735222Sksewell@umich.edu{
1745222Sksewell@umich.edu    // lda $stq, disp($sp)
1755222Sksewell@umich.edu    //
1765222Sksewell@umich.edu    // Opcode<31:26> == 0x08
1775222Sksewell@umich.edu    // RA<25:21> == ?
1785222Sksewell@umich.edu    // RB<20:16> == 30
1795222Sksewell@umich.edu    // Disp<15:0>
1805222Sksewell@umich.edu    const MachInst stq_mask = 0xfc1f0000;
1815222Sksewell@umich.edu    const MachInst stq_pattern = 0xb41e0000;
1825222Sksewell@umich.edu    const MachInst stq_disp_mask = 0x0000ffff;
1835222Sksewell@umich.edu    const MachInst reg_mask = 0x03e00000;
1845222Sksewell@umich.edu    const int reg_shift = 21;
1855222Sksewell@umich.edu
1865222Sksewell@umich.edu    if ((inst & stq_mask) == stq_pattern) {
1875222Sksewell@umich.edu        reg = (inst & reg_mask) >> reg_shift;
1885222Sksewell@umich.edu        disp = sext<16>(inst & stq_disp_mask);
1895222Sksewell@umich.edu    } else {
1905222Sksewell@umich.edu        return false;
1915222Sksewell@umich.edu    }
1925222Sksewell@umich.edu
1935222Sksewell@umich.edu    return true;
1945222Sksewell@umich.edu}
1955222Sksewell@umich.edu
1965222Sksewell@umich.edu/*
1975222Sksewell@umich.edu * Decode the function prologue for the function we're in, and note
1985222Sksewell@umich.edu * which registers are stored where, and how large the stack frame is.
1995222Sksewell@umich.edu */
2005222Sksewell@umich.edubool
2015222Sksewell@umich.eduStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
2025222Sksewell@umich.edu                           int &size, Addr &ra)
2035222Sksewell@umich.edu{
2045222Sksewell@umich.edu    size = 0;
2055222Sksewell@umich.edu    ra = 0;
2065222Sksewell@umich.edu
2075222Sksewell@umich.edu    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
2085222Sksewell@umich.edu        MachInst inst;
2095222Sksewell@umich.edu        CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst));
2105222Sksewell@umich.edu
2115222Sksewell@umich.edu        int reg, disp;
2125222Sksewell@umich.edu        if (decodeStack(inst, disp)) {
2135222Sksewell@umich.edu            if (size) {
2145222Sksewell@umich.edu                return true;
2155222Sksewell@umich.edu            }
2165222Sksewell@umich.edu            size += disp;
2175222Sksewell@umich.edu        } else if (decodeSave(inst, reg, disp)) {
2185222Sksewell@umich.edu            if (!ra && reg == ReturnAddressReg) {
2195222Sksewell@umich.edu                CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
2205222Sksewell@umich.edu                if (!ra) {
2215222Sksewell@umich.edu                    return false;
2225222Sksewell@umich.edu                }
2235222Sksewell@umich.edu            }
2245222Sksewell@umich.edu        }
2255222Sksewell@umich.edu    }
2265222Sksewell@umich.edu
2275222Sksewell@umich.edu    return true;
2285222Sksewell@umich.edu}
2295222Sksewell@umich.edu
2305222Sksewell@umich.edu#if TRACING_ON
2315222Sksewell@umich.eduvoid
2325222Sksewell@umich.eduStackTrace::dump()
2335222Sksewell@umich.edu{
2346378Sgblack@eecs.umich.edu    panic("Stack trace dump not implemented.\n");
2355222Sksewell@umich.edu}
2365222Sksewell@umich.edu#endif
237