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