stacktrace.cc revision 5254
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
455222Sksewell@umich.eduProcessInfo::ProcessInfo(ThreadContext *_tc)
465222Sksewell@umich.edu    : tc(_tc)
475222Sksewell@umich.edu{
485222Sksewell@umich.edu//     Addr addr = 0;
495222Sksewell@umich.edu
505222Sksewell@umich.edu    VirtualPort *vp;
515222Sksewell@umich.edu
525222Sksewell@umich.edu    vp = tc->getVirtPort();
535222Sksewell@umich.edu
545222Sksewell@umich.edu//     if (!tc->getSystemPtr()->kernelSymtab->findAddress("thread_info_size", addr))
555222Sksewell@umich.edu//         panic("thread info not compiled into kernel\n");
565222Sksewell@umich.edu//     thread_info_size = vp->readGtoH<int32_t>(addr);
575222Sksewell@umich.edu
585222Sksewell@umich.edu//     if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_size", addr))
595222Sksewell@umich.edu//         panic("thread info not compiled into kernel\n");
605222Sksewell@umich.edu//     task_struct_size = vp->readGtoH<int32_t>(addr);
615222Sksewell@umich.edu
625222Sksewell@umich.edu//     if (!tc->getSystemPtr()->kernelSymtab->findAddress("thread_info_task", addr))
635222Sksewell@umich.edu//         panic("thread info not compiled into kernel\n");
645222Sksewell@umich.edu//     task_off = vp->readGtoH<int32_t>(addr);
655222Sksewell@umich.edu
665222Sksewell@umich.edu//     if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_pid", addr))
675222Sksewell@umich.edu//         panic("thread info not compiled into kernel\n");
685222Sksewell@umich.edu//     pid_off = vp->readGtoH<int32_t>(addr);
695222Sksewell@umich.edu
705222Sksewell@umich.edu//     if (!tc->getSystemPtr()->kernelSymtab->findAddress("task_struct_comm", addr))
715222Sksewell@umich.edu//         panic("thread info not compiled into kernel\n");
725222Sksewell@umich.edu//     name_off = vp->readGtoH<int32_t>(addr);
735222Sksewell@umich.edu
745222Sksewell@umich.edu    tc->delVirtPort(vp);
755222Sksewell@umich.edu}
765222Sksewell@umich.edu
775222Sksewell@umich.eduAddr
785222Sksewell@umich.eduProcessInfo::task(Addr ksp) const
795222Sksewell@umich.edu{
805222Sksewell@umich.edu    Addr base = ksp & ~0x3fff;
815222Sksewell@umich.edu    if (base == ULL(0xfffffc0000000000))
825222Sksewell@umich.edu        return 0;
835222Sksewell@umich.edu
845222Sksewell@umich.edu    Addr tsk;
855222Sksewell@umich.edu
865222Sksewell@umich.edu    VirtualPort *vp;
875222Sksewell@umich.edu
885222Sksewell@umich.edu    vp = tc->getVirtPort();
895222Sksewell@umich.edu    tsk = vp->readGtoH<Addr>(base + task_off);
905222Sksewell@umich.edu    tc->delVirtPort(vp);
915222Sksewell@umich.edu
925222Sksewell@umich.edu    return tsk;
935222Sksewell@umich.edu}
945222Sksewell@umich.edu
955222Sksewell@umich.eduint
965222Sksewell@umich.eduProcessInfo::pid(Addr ksp) const
975222Sksewell@umich.edu{
985222Sksewell@umich.edu    Addr task = this->task(ksp);
995222Sksewell@umich.edu    if (!task)
1005222Sksewell@umich.edu        return -1;
1015222Sksewell@umich.edu
1025222Sksewell@umich.edu    uint16_t pd;
1035222Sksewell@umich.edu
1045222Sksewell@umich.edu    VirtualPort *vp;
1055222Sksewell@umich.edu
1065222Sksewell@umich.edu    vp = tc->getVirtPort();
1075222Sksewell@umich.edu    pd = vp->readGtoH<uint16_t>(task + pid_off);
1085222Sksewell@umich.edu    tc->delVirtPort(vp);
1095222Sksewell@umich.edu
1105222Sksewell@umich.edu    return pd;
1115222Sksewell@umich.edu}
1125222Sksewell@umich.edu
1135222Sksewell@umich.edustring
1145222Sksewell@umich.eduProcessInfo::name(Addr ksp) const
1155222Sksewell@umich.edu{
1165222Sksewell@umich.edu    Addr task = this->task(ksp);
1175222Sksewell@umich.edu    if (!task)
1185222Sksewell@umich.edu        return "console";
1195222Sksewell@umich.edu
1205222Sksewell@umich.edu    char comm[256];
1215222Sksewell@umich.edu    CopyStringOut(tc, comm, task + name_off, sizeof(comm));
1225222Sksewell@umich.edu    if (!comm[0])
1235222Sksewell@umich.edu        return "startup";
1245222Sksewell@umich.edu
1255222Sksewell@umich.edu    return comm;
1265222Sksewell@umich.edu}
1275222Sksewell@umich.edu
1285222Sksewell@umich.eduStackTrace::StackTrace()
1295222Sksewell@umich.edu    : tc(0), stack(64)
1305222Sksewell@umich.edu{
1315222Sksewell@umich.edu}
1325222Sksewell@umich.edu
1335222Sksewell@umich.eduStackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst)
1345222Sksewell@umich.edu    : tc(0), stack(64)
1355222Sksewell@umich.edu{
1365222Sksewell@umich.edu    trace(_tc, inst);
1375222Sksewell@umich.edu}
1385222Sksewell@umich.edu
1395222Sksewell@umich.eduStackTrace::~StackTrace()
1405222Sksewell@umich.edu{
1415222Sksewell@umich.edu}
1425222Sksewell@umich.edu
1435222Sksewell@umich.eduvoid
1445222Sksewell@umich.eduStackTrace::trace(ThreadContext *_tc, bool is_call)
1455222Sksewell@umich.edu{
1465222Sksewell@umich.edu    tc = _tc;
1475222Sksewell@umich.edu    /* FIXME - Jaidev - What is IPR_DTB_CM in Alpha? */
1485222Sksewell@umich.edu    bool usermode = 0;
1495222Sksewell@umich.edu      //(tc->readMiscReg(MipsISA::IPR_DTB_CM) & 0x18) != 0;
1505222Sksewell@umich.edu
1515222Sksewell@umich.edu//     Addr pc = tc->readNextPC();
1525222Sksewell@umich.edu//     bool kernel = tc->getSystemPtr()->kernelStart <= pc &&
1535222Sksewell@umich.edu//         pc <= tc->getSystemPtr()->kernelEnd;
1545222Sksewell@umich.edu
1555222Sksewell@umich.edu    if (usermode) {
1565222Sksewell@umich.edu        stack.push_back(user);
1575222Sksewell@umich.edu        return;
1585222Sksewell@umich.edu    }
1595222Sksewell@umich.edu
1605222Sksewell@umich.edu//     if (!kernel) {
1615222Sksewell@umich.edu//         stack.push_back(console);
1625222Sksewell@umich.edu//         return;
1635222Sksewell@umich.edu//     }
1645222Sksewell@umich.edu
1655222Sksewell@umich.edu//     SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
1665222Sksewell@umich.edu//     Addr ksp = tc->readIntReg(TheISA::StackPointerReg);
1675222Sksewell@umich.edu//     Addr bottom = ksp & ~0x3fff;
1685222Sksewell@umich.edu//     Addr addr;
1695222Sksewell@umich.edu
1705222Sksewell@umich.edu//     if (is_call) {
1715222Sksewell@umich.edu//         if (!symtab->findNearestAddr(pc, addr))
1725222Sksewell@umich.edu//             panic("could not find address %#x", pc);
1735222Sksewell@umich.edu
1745222Sksewell@umich.edu//         stack.push_back(addr);
1755222Sksewell@umich.edu//         pc = tc->readPC();
1765222Sksewell@umich.edu//     }
1775222Sksewell@umich.edu
1785222Sksewell@umich.edu//     Addr ra;
1795222Sksewell@umich.edu//     int size;
1805222Sksewell@umich.edu
1815222Sksewell@umich.edu//     while (ksp > bottom) {
1825222Sksewell@umich.edu//         if (!symtab->findNearestAddr(pc, addr))
1835222Sksewell@umich.edu//             panic("could not find symbol for pc=%#x", pc);
1845222Sksewell@umich.edu//         assert(pc >= addr && "symbol botch: callpc < func");
1855222Sksewell@umich.edu
1865222Sksewell@umich.edu//         stack.push_back(addr);
1875222Sksewell@umich.edu
1885222Sksewell@umich.edu//         if (isEntry(addr))
1895222Sksewell@umich.edu//             return;
1905222Sksewell@umich.edu
1915222Sksewell@umich.edu//         if (decodePrologue(ksp, pc, addr, size, ra)) {
1925222Sksewell@umich.edu//             if (!ra)
1935222Sksewell@umich.edu//                 return;
1945222Sksewell@umich.edu
1955222Sksewell@umich.edu//             if (size <= 0) {
1965222Sksewell@umich.edu//                 stack.push_back(unknown);
1975222Sksewell@umich.edu//                 return;
1985222Sksewell@umich.edu//             }
1995222Sksewell@umich.edu
2005222Sksewell@umich.edu//             pc = ra;
2015222Sksewell@umich.edu//             ksp += size;
2025222Sksewell@umich.edu//         } else {
2035222Sksewell@umich.edu//             stack.push_back(unknown);
2045222Sksewell@umich.edu//             return;
2055222Sksewell@umich.edu//         }
2065222Sksewell@umich.edu
2075222Sksewell@umich.edu//         bool kernel = tc->getSystemPtr()->kernelStart <= pc &&
2085222Sksewell@umich.edu//             pc <= tc->getSystemPtr()->kernelEnd;
2095222Sksewell@umich.edu//         if (!kernel)
2105222Sksewell@umich.edu//             return;
2115222Sksewell@umich.edu
2125222Sksewell@umich.edu//         if (stack.size() >= 1000)
2135222Sksewell@umich.edu//             panic("unwinding too far");
2145222Sksewell@umich.edu//     }
2155222Sksewell@umich.edu
2165222Sksewell@umich.edu//     panic("unwinding too far");
2175222Sksewell@umich.edu}
2185222Sksewell@umich.edu
2195222Sksewell@umich.edubool
2205222Sksewell@umich.eduStackTrace::isEntry(Addr addr)
2215222Sksewell@umich.edu{
2225222Sksewell@umich.edu  /*    if (addr == tc->readMiscReg(MipsISA::IPR_PALtemp2))
2235222Sksewell@umich.edu        return true;*/
2245222Sksewell@umich.edu
2255222Sksewell@umich.edu    return false;
2265222Sksewell@umich.edu}
2275222Sksewell@umich.edu
2285222Sksewell@umich.edubool
2295222Sksewell@umich.eduStackTrace::decodeStack(MachInst inst, int &disp)
2305222Sksewell@umich.edu{
2315222Sksewell@umich.edu    // lda $sp, -disp($sp)
2325222Sksewell@umich.edu    //
2335222Sksewell@umich.edu    // Opcode<31:26> == 0x08
2345222Sksewell@umich.edu    // RA<25:21> == 30
2355222Sksewell@umich.edu    // RB<20:16> == 30
2365222Sksewell@umich.edu    // Disp<15:0>
2375222Sksewell@umich.edu    const MachInst mem_mask = 0xffff0000;
2385222Sksewell@umich.edu    const MachInst lda_pattern = 0x23de0000;
2395222Sksewell@umich.edu    const MachInst lda_disp_mask = 0x0000ffff;
2405222Sksewell@umich.edu
2415222Sksewell@umich.edu    // subq $sp, disp, $sp
2425222Sksewell@umich.edu    // addq $sp, disp, $sp
2435222Sksewell@umich.edu    //
2445222Sksewell@umich.edu    // Opcode<31:26> == 0x10
2455222Sksewell@umich.edu    // RA<25:21> == 30
2465222Sksewell@umich.edu    // Lit<20:13>
2475222Sksewell@umich.edu    // One<12> = 1
2485222Sksewell@umich.edu    // Func<11:5> == 0x20 (addq)
2495222Sksewell@umich.edu    // Func<11:5> == 0x29 (subq)
2505222Sksewell@umich.edu    // RC<4:0> == 30
2515222Sksewell@umich.edu    const MachInst intop_mask = 0xffe01fff;
2525222Sksewell@umich.edu    const MachInst addq_pattern = 0x43c0141e;
2535222Sksewell@umich.edu    const MachInst subq_pattern = 0x43c0153e;
2545222Sksewell@umich.edu    const MachInst intop_disp_mask = 0x001fe000;
2555222Sksewell@umich.edu    const int intop_disp_shift = 13;
2565222Sksewell@umich.edu
2575222Sksewell@umich.edu    if ((inst & mem_mask) == lda_pattern)
2585222Sksewell@umich.edu        disp = -sext<16>(inst & lda_disp_mask);
2595222Sksewell@umich.edu    else if ((inst & intop_mask) == addq_pattern)
2605222Sksewell@umich.edu        disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
2615222Sksewell@umich.edu    else if ((inst & intop_mask) == subq_pattern)
2625222Sksewell@umich.edu        disp = int((inst & intop_disp_mask) >> intop_disp_shift);
2635222Sksewell@umich.edu    else
2645222Sksewell@umich.edu        return false;
2655222Sksewell@umich.edu
2665222Sksewell@umich.edu    return true;
2675222Sksewell@umich.edu}
2685222Sksewell@umich.edu
2695222Sksewell@umich.edubool
2705222Sksewell@umich.eduStackTrace::decodeSave(MachInst inst, int &reg, int &disp)
2715222Sksewell@umich.edu{
2725222Sksewell@umich.edu    // lda $stq, disp($sp)
2735222Sksewell@umich.edu    //
2745222Sksewell@umich.edu    // Opcode<31:26> == 0x08
2755222Sksewell@umich.edu    // RA<25:21> == ?
2765222Sksewell@umich.edu    // RB<20:16> == 30
2775222Sksewell@umich.edu    // Disp<15:0>
2785222Sksewell@umich.edu    const MachInst stq_mask = 0xfc1f0000;
2795222Sksewell@umich.edu    const MachInst stq_pattern = 0xb41e0000;
2805222Sksewell@umich.edu    const MachInst stq_disp_mask = 0x0000ffff;
2815222Sksewell@umich.edu    const MachInst reg_mask = 0x03e00000;
2825222Sksewell@umich.edu    const int reg_shift = 21;
2835222Sksewell@umich.edu
2845222Sksewell@umich.edu    if ((inst & stq_mask) == stq_pattern) {
2855222Sksewell@umich.edu        reg = (inst & reg_mask) >> reg_shift;
2865222Sksewell@umich.edu        disp = sext<16>(inst & stq_disp_mask);
2875222Sksewell@umich.edu    } else {
2885222Sksewell@umich.edu        return false;
2895222Sksewell@umich.edu    }
2905222Sksewell@umich.edu
2915222Sksewell@umich.edu    return true;
2925222Sksewell@umich.edu}
2935222Sksewell@umich.edu
2945222Sksewell@umich.edu/*
2955222Sksewell@umich.edu * Decode the function prologue for the function we're in, and note
2965222Sksewell@umich.edu * which registers are stored where, and how large the stack frame is.
2975222Sksewell@umich.edu */
2985222Sksewell@umich.edubool
2995222Sksewell@umich.eduStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
3005222Sksewell@umich.edu                           int &size, Addr &ra)
3015222Sksewell@umich.edu{
3025222Sksewell@umich.edu    size = 0;
3035222Sksewell@umich.edu    ra = 0;
3045222Sksewell@umich.edu
3055222Sksewell@umich.edu    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
3065222Sksewell@umich.edu        MachInst inst;
3075222Sksewell@umich.edu        CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst));
3085222Sksewell@umich.edu
3095222Sksewell@umich.edu        int reg, disp;
3105222Sksewell@umich.edu        if (decodeStack(inst, disp)) {
3115222Sksewell@umich.edu            if (size) {
3125222Sksewell@umich.edu                // panic("decoding frame size again");
3135222Sksewell@umich.edu                return true;
3145222Sksewell@umich.edu            }
3155222Sksewell@umich.edu            size += disp;
3165222Sksewell@umich.edu        } else if (decodeSave(inst, reg, disp)) {
3175222Sksewell@umich.edu            if (!ra && reg == ReturnAddressReg) {
3185222Sksewell@umich.edu                CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
3195222Sksewell@umich.edu                if (!ra) {
3205222Sksewell@umich.edu                    // panic("no return address value pc=%#x\n", pc);
3215222Sksewell@umich.edu                    return false;
3225222Sksewell@umich.edu                }
3235222Sksewell@umich.edu            }
3245222Sksewell@umich.edu        }
3255222Sksewell@umich.edu    }
3265222Sksewell@umich.edu
3275222Sksewell@umich.edu    return true;
3285222Sksewell@umich.edu}
3295222Sksewell@umich.edu
3305222Sksewell@umich.edu#if TRACING_ON
3315222Sksewell@umich.eduvoid
3325222Sksewell@umich.eduStackTrace::dump()
3335222Sksewell@umich.edu{
3345222Sksewell@umich.edu    StringWrap name(tc->getCpuPtr()->name());
3355222Sksewell@umich.edu//     SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
3365222Sksewell@umich.edu
3375222Sksewell@umich.edu    DPRINTFN("------ Stack ------\n");
3385222Sksewell@umich.edu
3395222Sksewell@umich.edu//     string symbol;
3405222Sksewell@umich.edu//     for (int i = 0, size = stack.size(); i < size; ++i) {
3415222Sksewell@umich.edu//         Addr addr = stack[size - i - 1];
3425222Sksewell@umich.edu//         if (addr == user)
3435222Sksewell@umich.edu//             symbol = "user";
3445222Sksewell@umich.edu//         else if (addr == console)
3455222Sksewell@umich.edu//             symbol = "console";
3465222Sksewell@umich.edu//         else if (addr == unknown)
3475222Sksewell@umich.edu//             symbol = "unknown";
3485222Sksewell@umich.edu//         else
3495222Sksewell@umich.edu//             symtab->findSymbol(addr, symbol);
3505222Sksewell@umich.edu
3515222Sksewell@umich.edu//         DPRINTFN("%#x: %s\n", addr, symbol);
3525222Sksewell@umich.edu//     }
3535222Sksewell@umich.edu}
3545222Sksewell@umich.edu#endif
355