gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * 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"
42 #include "sim/system.hh"
43 
44 namespace X86ISA
45 {
46 
47 static int32_t
48 readSymbol(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 
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 
69 Addr
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 
84 int
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 
99 std::string
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 
115  : tc(0), stack(64)
116 {
117 }
118 
120  : tc(0), stack(64)
121 {
122  trace(_tc, inst);
123 }
124 
126 {
127 }
128 
129 void
130 StackTrace::trace(ThreadContext *_tc, bool is_call)
131 {
132 }
133 
134 bool
136 {
137  return false;
138 }
139 
140 bool
142 {
143  disp = 0;
144  return true;
145 }
146 
147 bool
148 StackTrace::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  */
159 bool
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
191 void
193 {
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 }
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
virtual System * getSystemPtr()=0
Bitfield< 5, 3 > reg
Definition: types.hh:89
ThreadContext * tc
Definition: stacktrace.hh:65
const std::string & name()
Definition: trace.cc:54
Bitfield< 7 > i
ThreadContext * tc
Definition: stacktrace.hh:46
ProcessInfo(ThreadContext *_tc)
Definition: stacktrace.cc:60
Addr task(Addr ksp) const
Definition: stacktrace.cc:70
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:50
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:284
virtual BaseCPU * getCpuPtr()=0
Bitfield< 19 > pc
Definition: misc.hh:807
bool isEntry(Addr addr)
Definition: stacktrace.cc:135
ThreadContext is the external interface to all thread state for anything outside of the CPU...
#define DPRINTFN(...)
Definition: trace.hh:233
bool decodePrologue(Addr sp, Addr callpc, Addr func, int &size, Addr &ra)
Definition: stacktrace.cc:160
const int ReturnAddressReg
Definition: registers.hh:89
bool decodeStack(MachInst inst, int &disp)
Definition: stacktrace.cc:141
bool findAddress(const std::string &symbol, Addr &address) const
Definition: symtab.hh:97
std::string name(Addr ksp) const
Definition: stacktrace.cc:100
Bitfield< 51, 12 > base
Definition: pagetable.hh:142
static const int unknown
Definition: stacktrace.hh:95
uint64_t MachInst
Definition: types.hh:54
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
#define ULL(N)
uint64_t constant
Definition: types.hh:50
virtual const std::string name() const
Definition: sim_object.hh:120
bool decodeSave(MachInst inst, int &reg, int &disp)
Definition: stacktrace.cc:148
SymbolTable * kernelSymtab
kernel symbol table
Definition: system.hh:221
Bitfield< 20, 16 > ra
Definition: types.hh:47
Bitfield< 17, 16 > stack
Definition: misc.hh:589
This object is a proxy for a port or other object which implements the functional response protocol...
Definition: port_proxy.hh:82
void trace(ThreadContext *tc, bool is_call)
Definition: stacktrace.cc:130
void readString(std::string &str, Addr addr) const
Same as tryReadString, but insists on success.
Definition: port_proxy.hh:257
static const int console
Definition: stacktrace.hh:94
This is exposed globally, independent of the ISA.
Definition: acpi.hh:57
static const int user
Definition: stacktrace.hh:93
TranslatingPortProxy Object Declaration for FS.
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:561
int pid(Addr ksp) const
Definition: stacktrace.cc:85
Bitfield< 3 > addr
Definition: types.hh:81
static int32_t readSymbol(ThreadContext *tc, const std::string name)
Definition: stacktrace.cc:48
std::vector< Addr > stack
Definition: stacktrace.hh:66

Generated on Fri Feb 28 2020 16:26:56 for gem5 by doxygen 1.8.13