stacktrace.hh revision 2745
12239SN/A/*
22239SN/A * Copyright (c) 2005 The Regents of The University of Michigan
32239SN/A * All rights reserved.
42239SN/A *
52239SN/A * Redistribution and use in source and binary forms, with or without
62239SN/A * modification, are permitted provided that the following conditions are
72239SN/A * met: redistributions of source code must retain the above copyright
82239SN/A * notice, this list of conditions and the following disclaimer;
92239SN/A * redistributions in binary form must reproduce the above copyright
102239SN/A * notice, this list of conditions and the following disclaimer in the
112239SN/A * documentation and/or other materials provided with the distribution;
122239SN/A * neither the name of the copyright holders nor the names of its
132239SN/A * contributors may be used to endorse or promote products derived from
142239SN/A * this software without specific prior written permission.
152239SN/A *
162239SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172239SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182239SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192239SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202239SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212239SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222239SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232239SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242239SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252239SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262239SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Korey Sewell
292239SN/A */
302239SN/A
312745Sksewell@umich.edu#ifndef __ARCH_MIPS_STACKTRACE_HH__
322745Sksewell@umich.edu#define __ARCH_MIPS_STACKTRACE_HH__
332239SN/A
342239SN/A#include "base/trace.hh"
352239SN/A#include "cpu/static_inst.hh"
362239SN/A
372680Sktlim@umich.educlass ThreadContext;
382239SN/Aclass StackTrace;
392239SN/A
402239SN/Aclass ProcessInfo
412239SN/A{
422239SN/A  private:
432680Sktlim@umich.edu    ThreadContext *tc;
442239SN/A
452239SN/A    int thread_info_size;
462239SN/A    int task_struct_size;
472239SN/A    int task_off;
482239SN/A    int pid_off;
492239SN/A    int name_off;
502239SN/A
512239SN/A  public:
522680Sktlim@umich.edu    ProcessInfo(ThreadContext *_tc);
532239SN/A
542239SN/A    Addr task(Addr ksp) const;
552239SN/A    int pid(Addr ksp) const;
562239SN/A    std::string name(Addr ksp) const;
572239SN/A};
582239SN/A
592239SN/Aclass StackTrace
602239SN/A{
612239SN/A  protected:
622239SN/A    typedef TheISA::MachInst MachInst;
632239SN/A  private:
642680Sktlim@umich.edu    ThreadContext *tc;
652239SN/A    std::vector<Addr> stack;
662239SN/A
672239SN/A  private:
682239SN/A    bool isEntry(Addr addr);
692239SN/A    bool decodePrologue(Addr sp, Addr callpc, Addr func, int &size, Addr &ra);
702239SN/A    bool decodeSave(MachInst inst, int &reg, int &disp);
712239SN/A    bool decodeStack(MachInst inst, int &disp);
722239SN/A
732680Sktlim@umich.edu    void trace(ThreadContext *tc, bool is_call);
742239SN/A
752239SN/A  public:
762239SN/A    StackTrace();
772680Sktlim@umich.edu    StackTrace(ThreadContext *tc, StaticInstPtr inst);
782239SN/A    ~StackTrace();
792239SN/A
802239SN/A    void clear()
812239SN/A    {
822680Sktlim@umich.edu        tc = 0;
832239SN/A        stack.clear();
842239SN/A    }
852239SN/A
862680Sktlim@umich.edu    bool valid() const { return tc != NULL; }
872680Sktlim@umich.edu    bool trace(ThreadContext *tc, StaticInstPtr inst);
882239SN/A
892239SN/A  public:
902239SN/A    const std::vector<Addr> &getstack() const { return stack; }
912239SN/A
922239SN/A    static const int user = 1;
932239SN/A    static const int console = 2;
942239SN/A    static const int unknown = 3;
952239SN/A
962239SN/A#if TRACING_ON
972239SN/A  private:
982239SN/A    void dump();
992239SN/A
1002239SN/A  public:
1012239SN/A    void dprintf() { if (DTRACE(Stack)) dump(); }
1022239SN/A#else
1032239SN/A  public:
1042239SN/A    void dprintf() {}
1052239SN/A#endif
1062239SN/A};
1072239SN/A
1082239SN/Ainline bool
1092680Sktlim@umich.eduStackTrace::trace(ThreadContext *tc, StaticInstPtr inst)
1102239SN/A{
1112239SN/A    if (!inst->isCall() && !inst->isReturn())
1122239SN/A        return false;
1132239SN/A
1142239SN/A    if (valid())
1152239SN/A        clear();
1162239SN/A
1172680Sktlim@umich.edu    trace(tc, !inst->isReturn());
1182239SN/A    return true;
1192239SN/A}
1202239SN/A
1212745Sksewell@umich.edu#endif // __ARCH_MIPS_STACKTRACE_HH__
122