stacktrace.cc revision 8706
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#include <string>
32
33#include "arch/mips/isa_traits.hh"
34#include "arch/mips/stacktrace.hh"
35#include "arch/mips/vtophys.hh"
36#include "base/bitfield.hh"
37#include "base/trace.hh"
38#include "cpu/base.hh"
39#include "cpu/thread_context.hh"
40#include "sim/system.hh"
41
42using namespace std;
43using namespace MipsISA;
44
45ProcessInfo::ProcessInfo(ThreadContext *_tc) : tc(_tc)
46{}
47
48Addr
49ProcessInfo::task(Addr ksp) const
50{
51    Addr base = ksp & ~0x3fff;
52    if (base == ULL(0xfffffc0000000000))
53        return 0;
54
55    Addr tsk;
56
57    FSTranslatingPortProxy* vp;
58
59    vp = tc->getVirtProxy();
60    tsk = vp->readGtoH<Addr>(base + task_off);
61
62    return tsk;
63}
64
65int
66ProcessInfo::pid(Addr ksp) const
67{
68    Addr task = this->task(ksp);
69    if (!task)
70        return -1;
71
72    uint16_t pd;
73
74    FSTranslatingPortProxy* vp;
75
76    vp = tc->getVirtProxy();
77    pd = vp->readGtoH<uint16_t>(task + pid_off);
78
79    return pd;
80}
81
82string
83ProcessInfo::name(Addr ksp) const
84{
85    Addr task = this->task(ksp);
86    if (!task)
87        return "console";
88
89    char comm[256];
90    CopyStringOut(tc, comm, task + name_off, sizeof(comm));
91    if (!comm[0])
92        return "startup";
93
94    return comm;
95}
96
97StackTrace::StackTrace()
98    : tc(0), stack(64)
99{
100}
101
102StackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst)
103    : tc(0), stack(64)
104{
105    trace(_tc, inst);
106}
107
108StackTrace::~StackTrace()
109{
110}
111
112void
113StackTrace::trace(ThreadContext *_tc, bool is_call)
114{
115    tc = _tc;
116    bool usermode = 0;
117
118    if (usermode) {
119        stack.push_back(user);
120        return;
121    }
122}
123
124bool
125StackTrace::isEntry(Addr addr)
126{
127    return false;
128}
129
130bool
131StackTrace::decodeStack(MachInst inst, int &disp)
132{
133    // lda $sp, -disp($sp)
134    //
135    // Opcode<31:26> == 0x08
136    // RA<25:21> == 30
137    // RB<20:16> == 30
138    // Disp<15:0>
139    const MachInst mem_mask = 0xffff0000;
140    const MachInst lda_pattern = 0x23de0000;
141    const MachInst lda_disp_mask = 0x0000ffff;
142
143    // subq $sp, disp, $sp
144    // addq $sp, disp, $sp
145    //
146    // Opcode<31:26> == 0x10
147    // RA<25:21> == 30
148    // Lit<20:13>
149    // One<12> = 1
150    // Func<11:5> == 0x20 (addq)
151    // Func<11:5> == 0x29 (subq)
152    // RC<4:0> == 30
153    const MachInst intop_mask = 0xffe01fff;
154    const MachInst addq_pattern = 0x43c0141e;
155    const MachInst subq_pattern = 0x43c0153e;
156    const MachInst intop_disp_mask = 0x001fe000;
157    const int intop_disp_shift = 13;
158
159    if ((inst & mem_mask) == lda_pattern)
160        disp = -sext<16>(inst & lda_disp_mask);
161    else if ((inst & intop_mask) == addq_pattern)
162        disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
163    else if ((inst & intop_mask) == subq_pattern)
164        disp = int((inst & intop_disp_mask) >> intop_disp_shift);
165    else
166        return false;
167
168    return true;
169}
170
171bool
172StackTrace::decodeSave(MachInst inst, int &reg, int &disp)
173{
174    // lda $stq, disp($sp)
175    //
176    // Opcode<31:26> == 0x08
177    // RA<25:21> == ?
178    // RB<20:16> == 30
179    // Disp<15:0>
180    const MachInst stq_mask = 0xfc1f0000;
181    const MachInst stq_pattern = 0xb41e0000;
182    const MachInst stq_disp_mask = 0x0000ffff;
183    const MachInst reg_mask = 0x03e00000;
184    const int reg_shift = 21;
185
186    if ((inst & stq_mask) == stq_pattern) {
187        reg = (inst & reg_mask) >> reg_shift;
188        disp = sext<16>(inst & stq_disp_mask);
189    } else {
190        return false;
191    }
192
193    return true;
194}
195
196/*
197 * Decode the function prologue for the function we're in, and note
198 * which registers are stored where, and how large the stack frame is.
199 */
200bool
201StackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
202                           int &size, Addr &ra)
203{
204    size = 0;
205    ra = 0;
206
207    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
208        MachInst inst;
209        CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst));
210
211        int reg, disp;
212        if (decodeStack(inst, disp)) {
213            if (size) {
214                return true;
215            }
216            size += disp;
217        } else if (decodeSave(inst, reg, disp)) {
218            if (!ra && reg == ReturnAddressReg) {
219                CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
220                if (!ra) {
221                    return false;
222                }
223            }
224        }
225    }
226
227    return true;
228}
229
230#if TRACING_ON
231void
232StackTrace::dump()
233{
234    panic("Stack trace dump not implemented.\n");
235}
236#endif
237