stacktrace.cc revision 13884:6f9486e267a4
1/*
2 * Copyright (c) 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 "arch/x86/stacktrace.hh"
32
33#include <string>
34
35#include "arch/x86/isa_traits.hh"
36#include "arch/x86/vtophys.hh"
37#include "base/bitfield.hh"
38#include "base/trace.hh"
39#include "cpu/base.hh"
40#include "cpu/thread_context.hh"
41#include "mem/fs_translating_port_proxy.hh"
42#include "sim/system.hh"
43
44using namespace std;
45namespace X86ISA
46{
47
48ProcessInfo::ProcessInfo(ThreadContext *_tc)
49    : tc(_tc)
50{
51    Addr addr = 0;
52
53    FSTranslatingPortProxy &vp = tc->getVirtProxy();
54
55    if (!tc->getSystemPtr()->kernelSymtab->findAddress(
56                "thread_info_size", addr)) {
57        panic("thread info not compiled into kernel\n");
58    }
59    thread_info_size = vp.readGtoH<int32_t>(addr);
60
61    if (!tc->getSystemPtr()->kernelSymtab->findAddress(
62                "task_struct_size", addr)) {
63        panic("thread info not compiled into kernel\n");
64    }
65    task_struct_size = vp.readGtoH<int32_t>(addr);
66
67    if (!tc->getSystemPtr()->kernelSymtab->findAddress(
68                "thread_info_task", addr)) {
69        panic("thread info not compiled into kernel\n");
70    }
71    task_off = vp.readGtoH<int32_t>(addr);
72
73    if (!tc->getSystemPtr()->kernelSymtab->findAddress(
74                "task_struct_pid", addr)) {
75        panic("thread info not compiled into kernel\n");
76    }
77    pid_off = vp.readGtoH<int32_t>(addr);
78
79    if (!tc->getSystemPtr()->kernelSymtab->findAddress(
80                "task_struct_comm", addr)) {
81        panic("thread info not compiled into kernel\n");
82    }
83    name_off = vp.readGtoH<int32_t>(addr);
84}
85
86Addr
87ProcessInfo::task(Addr ksp) const
88{
89    Addr base = ksp & ~0x3fff;
90    if (base == ULL(0xfffffc0000000000))
91        return 0;
92
93    Addr tsk;
94
95    FSTranslatingPortProxy &vp = tc->getVirtProxy();
96    tsk = vp.readGtoH<Addr>(base + task_off);
97
98    return tsk;
99}
100
101int
102ProcessInfo::pid(Addr ksp) const
103{
104    Addr task = this->task(ksp);
105    if (!task)
106        return -1;
107
108    uint16_t pd;
109
110    FSTranslatingPortProxy &vp = tc->getVirtProxy();
111    pd = vp.readGtoH<uint16_t>(task + pid_off);
112
113    return pd;
114}
115
116string
117ProcessInfo::name(Addr ksp) const
118{
119    Addr task = this->task(ksp);
120    if (!task)
121        return "console";
122
123    char comm[256];
124    CopyStringOut(tc, comm, task + name_off, sizeof(comm));
125    if (!comm[0])
126        return "startup";
127
128    return comm;
129}
130
131StackTrace::StackTrace()
132    : tc(0), stack(64)
133{
134}
135
136StackTrace::StackTrace(ThreadContext *_tc, const StaticInstPtr &inst)
137    : tc(0), stack(64)
138{
139    trace(_tc, inst);
140}
141
142StackTrace::~StackTrace()
143{
144}
145
146void
147StackTrace::trace(ThreadContext *_tc, bool is_call)
148{
149}
150
151bool
152StackTrace::isEntry(Addr addr)
153{
154    return false;
155}
156
157bool
158StackTrace::decodeStack(MachInst inst, int &disp)
159{
160    disp = 0;
161    return true;
162}
163
164bool
165StackTrace::decodeSave(MachInst inst, int &reg, int &disp)
166{
167    reg = 0;
168    disp = 0;
169    return true;
170}
171
172/*
173 * Decode the function prologue for the function we're in, and note
174 * which registers are stored where, and how large the stack frame is.
175 */
176bool
177StackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
178                           int &size, Addr &ra)
179{
180    size = 0;
181    ra = 0;
182
183    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
184        MachInst inst;
185        CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst));
186
187        int reg, disp;
188        if (decodeStack(inst, disp)) {
189            if (size) {
190                // panic("decoding frame size again");
191                return true;
192            }
193            size += disp;
194        } else if (decodeSave(inst, reg, disp)) {
195            if (!ra && reg == ReturnAddressReg) {
196                CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
197                if (!ra) {
198                    // panic("no return address value pc=%#x\n", pc);
199                    return false;
200                }
201            }
202        }
203    }
204
205    return true;
206}
207
208#if TRACING_ON
209void
210StackTrace::dump()
211{
212    StringWrap name(tc->getCpuPtr()->name());
213    SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
214
215    DPRINTFN("------ Stack ------\n");
216
217    string symbol;
218    for (int i = 0, size = stack.size(); i < size; ++i) {
219        Addr addr = stack[size - i - 1];
220        if (addr == user)
221            symbol = "user";
222        else if (addr == console)
223            symbol = "console";
224        else if (addr == unknown)
225            symbol = "unknown";
226        else
227            symtab->findSymbol(addr, symbol);
228
229        DPRINTFN("%#x: %s\n", addr, symbol);
230    }
231}
232
233#endif
234}
235