gem5  v20.0.0.3
stacktrace.cc
Go to the documentation of this file.
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 
29 #include "arch/x86/stacktrace.hh"
30 
31 #include <string>
32 
33 #include "arch/x86/isa_traits.hh"
34 #include "base/bitfield.hh"
35 #include "base/trace.hh"
36 #include "cpu/base.hh"
37 #include "cpu/thread_context.hh"
38 #include "mem/port_proxy.hh"
39 #include "sim/system.hh"
40 
41 namespace X86ISA
42 {
43 
44 static int32_t
45 readSymbol(ThreadContext *tc, const std::string name)
46 {
47  PortProxy &vp = tc->getVirtProxy();
48  const auto *symtab = tc->getSystemPtr()->workload->symtab(tc);
49 
50  Addr addr;
51  if (!symtab->findAddress(name, addr))
52  panic("thread info not compiled into kernel\n");
53 
54  return vp.read<int32_t>(addr, GuestByteOrder);
55 }
56 
58 {
59  thread_info_size = readSymbol(tc, "thread_info_size");
60  task_struct_size = readSymbol(tc, "task_struct_size");
61  task_off = readSymbol(tc, "thread_info_task");
62  pid_off = readSymbol(tc, "task_struct_pid");
63  name_off = readSymbol(tc, "task_struct_comm");
64 }
65 
66 Addr
68 {
69  Addr base = ksp & ~0x3fff;
70  if (base == ULL(0xfffffc0000000000))
71  return 0;
72 
73  Addr tsk;
74 
75  PortProxy &vp = tc->getVirtProxy();
76  tsk = vp.read<Addr>(base + task_off, GuestByteOrder);
77 
78  return tsk;
79 }
80 
81 int
83 {
84  Addr task = this->task(ksp);
85  if (!task)
86  return -1;
87 
88  uint16_t pd;
89 
90  PortProxy &vp = tc->getVirtProxy();
91  pd = vp.read<uint16_t>(task + pid_off, GuestByteOrder);
92 
93  return pd;
94 }
95 
96 std::string
98 {
99  Addr task = this->task(ksp);
100  if (!task)
101  return "console";
102 
103  char comm[256];
104  tc->getVirtProxy().readString(comm, task + name_off, sizeof(comm));
105  if (!comm[0])
106  return "startup";
107 
108  return comm;
109 }
110 
112  : tc(0), stack(64)
113 {
114 }
115 
117  : tc(0), stack(64)
118 {
119  trace(_tc, inst);
120 }
121 
123 {
124 }
125 
126 void
127 StackTrace::trace(ThreadContext *_tc, bool is_call)
128 {
129 }
130 
131 bool
133 {
134  return false;
135 }
136 
137 bool
139 {
140  disp = 0;
141  return true;
142 }
143 
144 bool
145 StackTrace::decodeSave(MachInst inst, int &reg, int &disp)
146 {
147  reg = 0;
148  disp = 0;
149  return true;
150 }
151 
152 /*
153  * Decode the function prologue for the function we're in, and note
154  * which registers are stored where, and how large the stack frame is.
155  */
156 bool
158  int &size, Addr &ra)
159 {
160  size = 0;
161  ra = 0;
162 
163  for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
164  MachInst inst = tc->getVirtProxy().read<MachInst>(pc);
165 
166  int reg, disp;
167  if (decodeStack(inst, disp)) {
168  if (size) {
169  // panic("decoding frame size again");
170  return true;
171  }
172  size += disp;
173  } else if (decodeSave(inst, reg, disp)) {
174  if (!ra && reg == ReturnAddressReg) {
175  ra = tc->getVirtProxy().read<Addr>(sp + disp);
176  if (!ra) {
177  // panic("no return address value pc=%#x\n", pc);
178  return false;
179  }
180  }
181  }
182  }
183 
184  return true;
185 }
186 
187 #if TRACING_ON
188 void
190 {
192  const auto *symtab = tc->getSystemPtr()->workload->symtab(tc);
193 
194  DPRINTFN("------ Stack ------\n");
195 
196  std::string symbol;
197  for (int i = 0, size = stack.size(); i < size; ++i) {
198  Addr addr = stack[size - i - 1];
199  if (addr == user)
200  symbol = "user";
201  else if (addr == console)
202  symbol = "console";
203  else if (addr == unknown)
204  symbol = "unknown";
205  else
206  symtab->findSymbol(addr, symbol);
207 
208  DPRINTFN("%#x: %s\n", addr, symbol);
209  }
210 }
211 
212 #endif
213 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:163
virtual System * getSystemPtr()=0
virtual const Loader::SymbolTable * symtab(ThreadContext *tc)=0
Bitfield< 5, 3 > reg
Definition: types.hh:87
ThreadContext * tc
Definition: stacktrace.hh:63
const std::string & name()
Definition: trace.cc:50
Bitfield< 7 > i
ThreadContext * tc
Definition: stacktrace.hh:44
ProcessInfo(ThreadContext *_tc)
Definition: stacktrace.cc:57
Addr task(Addr ksp) const
Definition: stacktrace.cc:67
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:48
virtual PortProxy & getVirtProxy()=0
Bitfield< 0 > sp
T read(Addr address) const
Read sizeof(T) bytes from address and return as object T.
Definition: port_proxy.hh:282
virtual BaseCPU * getCpuPtr()=0
Bitfield< 19 > pc
Definition: misc.hh:805
bool isEntry(Addr addr)
Definition: stacktrace.cc:132
ThreadContext is the external interface to all thread state for anything outside of the CPU...
#define DPRINTFN(...)
Definition: trace.hh:229
bool decodePrologue(Addr sp, Addr callpc, Addr func, int &size, Addr &ra)
Definition: stacktrace.cc:157
const int ReturnAddressReg
Definition: registers.hh:87
bool decodeStack(MachInst inst, int &disp)
Definition: stacktrace.cc:138
PortProxy Object Declaration.
std::string name(Addr ksp) const
Definition: stacktrace.cc:97
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
static const int unknown
Definition: stacktrace.hh:93
uint64_t MachInst
Definition: types.hh:52
Workload * workload
OS kernel.
Definition: system.hh:213
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
#define ULL(N)
uint64_t constant
Definition: types.hh:48
bool decodeSave(MachInst inst, int &reg, int &disp)
Definition: stacktrace.cc:145
Bitfield< 20, 16 > ra
Definition: types.hh:45
Bitfield< 17, 16 > stack
Definition: misc.hh:587
This object is a proxy for a port or other object which implements the functional response protocol...
Definition: port_proxy.hh:80
void trace(ThreadContext *tc, bool is_call)
Definition: stacktrace.cc:127
void readString(std::string &str, Addr addr) const
Same as tryReadString, but insists on success.
Definition: port_proxy.hh:255
virtual const std::string name() const
Definition: sim_object.hh:129
static const int console
Definition: stacktrace.hh:92
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
static const int user
Definition: stacktrace.hh:91
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:560
int pid(Addr ksp) const
Definition: stacktrace.cc:82
Bitfield< 3 > addr
Definition: types.hh:79
static int32_t readSymbol(ThreadContext *tc, const std::string name)
Definition: stacktrace.cc:45
std::vector< Addr > stack
Definition: stacktrace.hh:64

Generated on Fri Jul 3 2020 15:42:40 for gem5 by doxygen 1.8.13