stacktrace.cc revision 8852:c744483edfcf
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 <string>
32
33#include "arch/alpha/isa_traits.hh"
34#include "arch/alpha/stacktrace.hh"
35#include "arch/alpha/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 "mem/fs_translating_port_proxy.hh"
41#include "sim/system.hh"
42
43using namespace std;
44
45namespace AlphaISA {
46
47ProcessInfo::ProcessInfo(ThreadContext *_tc)
48    : tc(_tc)
49{
50    Addr addr = 0;
51    FSTranslatingPortProxy &vp = tc->getVirtProxy();
52    SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
53
54    if (!symtab->findAddress("thread_info_size", addr))
55        panic("thread info not compiled into kernel\n");
56    thread_info_size = vp.readGtoH<int32_t>(addr);
57
58    if (!symtab->findAddress("task_struct_size", addr))
59        panic("thread info not compiled into kernel\n");
60    task_struct_size = vp.readGtoH<int32_t>(addr);
61
62    if (!symtab->findAddress("thread_info_task", addr))
63        panic("thread info not compiled into kernel\n");
64    task_off = vp.readGtoH<int32_t>(addr);
65
66    if (!symtab->findAddress("task_struct_pid", addr))
67        panic("thread info not compiled into kernel\n");
68    pid_off = vp.readGtoH<int32_t>(addr);
69
70    if (!symtab->findAddress("task_struct_comm", addr))
71        panic("thread info not compiled into kernel\n");
72    name_off = vp.readGtoH<int32_t>(addr);
73}
74
75Addr
76ProcessInfo::task(Addr ksp) const
77{
78    Addr base = ksp & ~0x3fff;
79    if (base == ULL(0xfffffc0000000000))
80        return 0;
81
82    Addr tsk;
83
84    FSTranslatingPortProxy &vp = tc->getVirtProxy();
85    tsk = vp.readGtoH<Addr>(base + task_off);
86
87    return tsk;
88}
89
90int
91ProcessInfo::pid(Addr ksp) const
92{
93    Addr task = this->task(ksp);
94    if (!task)
95        return -1;
96
97    uint16_t pd;
98
99    FSTranslatingPortProxy &vp = tc->getVirtProxy();
100    pd = vp.readGtoH<uint16_t>(task + pid_off);
101
102    return pd;
103}
104
105string
106ProcessInfo::name(Addr ksp) const
107{
108    Addr task = this->task(ksp);
109    if (!task)
110        return "console";
111
112    char comm[256];
113    CopyStringOut(tc, comm, task + name_off, sizeof(comm));
114    if (!comm[0])
115        return "startup";
116
117    return comm;
118}
119
120StackTrace::StackTrace()
121    : tc(0), stack(64)
122{
123}
124
125StackTrace::StackTrace(ThreadContext *_tc, StaticInstPtr inst)
126    : tc(0), stack(64)
127{
128    trace(_tc, inst);
129}
130
131StackTrace::~StackTrace()
132{
133}
134
135void
136StackTrace::trace(ThreadContext *_tc, bool is_call)
137{
138    tc = _tc;
139
140    System *sys = tc->getSystemPtr();
141
142    bool usermode =
143        (tc->readMiscRegNoEffect(IPR_DTB_CM) & 0x18) != 0;
144
145    Addr pc = tc->pcState().pc();
146    bool kernel = sys->kernelStart <= pc && pc <= sys->kernelEnd;
147
148    if (usermode) {
149        stack.push_back(user);
150        return;
151    }
152
153    if (!kernel) {
154        stack.push_back(console);
155        return;
156    }
157
158    SymbolTable *symtab = sys->kernelSymtab;
159    Addr ksp = tc->readIntReg(StackPointerReg);
160    Addr bottom = ksp & ~0x3fff;
161
162    if (is_call) {
163        Addr addr;
164        if (!symtab->findNearestAddr(pc, addr))
165            panic("could not find address %#x", pc);
166
167        stack.push_back(addr);
168        pc = tc->pcState().pc();
169    }
170
171    while (ksp > bottom) {
172        Addr addr;
173        if (!symtab->findNearestAddr(pc, addr))
174            panic("could not find symbol for pc=%#x", pc);
175        assert(pc >= addr && "symbol botch: callpc < func");
176
177        stack.push_back(addr);
178
179        if (isEntry(addr))
180            return;
181
182        Addr ra;
183        int size;
184        if (decodePrologue(ksp, pc, addr, size, ra)) {
185            if (!ra)
186                return;
187
188            if (size <= 0) {
189                stack.push_back(unknown);
190                return;
191            }
192
193            pc = ra;
194            ksp += size;
195        } else {
196            stack.push_back(unknown);
197            return;
198        }
199
200        bool kernel = sys->kernelStart <= pc && pc <= sys->kernelEnd;
201        if (!kernel)
202            return;
203
204        if (stack.size() >= 1000)
205            panic("unwinding too far");
206    }
207
208    panic("unwinding too far");
209}
210
211bool
212StackTrace::isEntry(Addr addr)
213{
214    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp12))
215        return true;
216
217    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp7))
218        return true;
219
220    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp11))
221        return true;
222
223    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp21))
224        return true;
225
226    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp9))
227        return true;
228
229    if (addr == tc->readMiscRegNoEffect(IPR_PALtemp2))
230        return true;
231
232    return false;
233}
234
235bool
236StackTrace::decodeStack(MachInst inst, int &disp)
237{
238    // lda $sp, -disp($sp)
239    //
240    // Opcode<31:26> == 0x08
241    // RA<25:21> == 30
242    // RB<20:16> == 30
243    // Disp<15:0>
244    const MachInst mem_mask = 0xffff0000;
245    const MachInst lda_pattern = 0x23de0000;
246    const MachInst lda_disp_mask = 0x0000ffff;
247
248    // subq $sp, disp, $sp
249    // addq $sp, disp, $sp
250    //
251    // Opcode<31:26> == 0x10
252    // RA<25:21> == 30
253    // Lit<20:13>
254    // One<12> = 1
255    // Func<11:5> == 0x20 (addq)
256    // Func<11:5> == 0x29 (subq)
257    // RC<4:0> == 30
258    const MachInst intop_mask = 0xffe01fff;
259    const MachInst addq_pattern = 0x43c0141e;
260    const MachInst subq_pattern = 0x43c0153e;
261    const MachInst intop_disp_mask = 0x001fe000;
262    const int intop_disp_shift = 13;
263
264    if ((inst & mem_mask) == lda_pattern)
265        disp = -sext<16>(inst & lda_disp_mask);
266    else if ((inst & intop_mask) == addq_pattern)
267        disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
268    else if ((inst & intop_mask) == subq_pattern)
269        disp = int((inst & intop_disp_mask) >> intop_disp_shift);
270    else
271        return false;
272
273    return true;
274}
275
276bool
277StackTrace::decodeSave(MachInst inst, int &reg, int &disp)
278{
279    // lda $stq, disp($sp)
280    //
281    // Opcode<31:26> == 0x08
282    // RA<25:21> == ?
283    // RB<20:16> == 30
284    // Disp<15:0>
285    const MachInst stq_mask = 0xfc1f0000;
286    const MachInst stq_pattern = 0xb41e0000;
287    const MachInst stq_disp_mask = 0x0000ffff;
288    const MachInst reg_mask = 0x03e00000;
289    const int reg_shift = 21;
290
291    if ((inst & stq_mask) == stq_pattern) {
292        reg = (inst & reg_mask) >> reg_shift;
293        disp = sext<16>(inst & stq_disp_mask);
294    } else {
295        return false;
296    }
297
298    return true;
299}
300
301/*
302 * Decode the function prologue for the function we're in, and note
303 * which registers are stored where, and how large the stack frame is.
304 */
305bool
306StackTrace::decodePrologue(Addr sp, Addr callpc, Addr func, int &size,
307                           Addr &ra)
308{
309    size = 0;
310    ra = 0;
311
312    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
313        MachInst inst;
314        CopyOut(tc, (uint8_t *)&inst, pc, sizeof(MachInst));
315
316        int reg, disp;
317        if (decodeStack(inst, disp)) {
318            if (size) {
319                // panic("decoding frame size again");
320                return true;
321            }
322            size += disp;
323        } else if (decodeSave(inst, reg, disp)) {
324            if (!ra && reg == ReturnAddressReg) {
325                CopyOut(tc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
326                if (!ra) {
327                    // panic("no return address value pc=%#x\n", pc);
328                    return false;
329                }
330            }
331        }
332    }
333
334    return true;
335}
336
337#if TRACING_ON
338void
339StackTrace::dump()
340{
341    StringWrap name(tc->getCpuPtr()->name());
342    SymbolTable *symtab = tc->getSystemPtr()->kernelSymtab;
343
344    DPRINTFN("------ Stack ------\n");
345
346    string symbol;
347    for (int i = 0, size = stack.size(); i < size; ++i) {
348        Addr addr = stack[size - i - 1];
349        if (addr == user)
350            symbol = "user";
351        else if (addr == console)
352            symbol = "console";
353        else if (addr == unknown)
354            symbol = "unknown";
355        else
356            symtab->findSymbol(addr, symbol);
357
358        DPRINTFN("%#x: %s\n", addr, symbol);
359    }
360}
361#endif
362
363} // namespace AlphaISA
364