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