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    PortProxy &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.read<int32_t>(addr, GuestByteOrder);
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    PortProxy &vp = tc->getVirtProxy();
79    tsk = vp.read<Addr>(base + task_off, GuestByteOrder);
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    PortProxy &vp = tc->getVirtProxy();
94    pd = vp.read<uint16_t>(task + pid_off, GuestByteOrder);
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    tc->getVirtProxy().readString(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 = tc->getVirtProxy().read<MachInst>(pc);
168
169        int reg, disp;
170        if (decodeStack(inst, disp)) {
171            if (size) {
172                // panic("decoding frame size again");
173                return true;
174            }
175            size += disp;
176        } else if (decodeSave(inst, reg, disp)) {
177            if (!ra && reg == ReturnAddressReg) {
178                ra = tc->getVirtProxy().read<Addr>(sp + disp);
179                if (!ra) {
180                    // panic("no return address value pc=%#x\n", pc);
181                    return false;
182                }
183            }
184        }
185    }
186
187    return true;
188}
189
190#if TRACING_ON
191void
192StackTrace::dump()
193{
194    StringWrap name(tc->getCpuPtr()->name());
195    SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
196
197    DPRINTFN("------ Stack ------\n");
198
199    std::string symbol;
200    for (int i = 0, size = stack.size(); i < size; ++i) {
201        Addr addr = stack[size - i - 1];
202        if (addr == user)
203            symbol = "user";
204        else if (addr == console)
205            symbol = "console";
206        else if (addr == unknown)
207            symbol = "unknown";
208        else
209            symtab->findSymbol(addr, symbol);
210
211        DPRINTFN("%#x: %s\n", addr, symbol);
212    }
213}
214
215#endif
216}
217