Add support for line and file information in backtraces.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
29 Stack::Stack(Zone *zone)
44 buffer_ = (char *)malloc(STACK_SIZE);
48 top_.sp = reinterpret_cast<LocalSlot *>(buffer_);
51 end_ = buffer_ + STACK_SIZE;
55 InvokeArgs::~InvokeArgs()
57 stack_->popInvokeFrame(*this);
60 NativeArgs::~NativeArgs()
62 stack_->popNativeFrame(*this);
66 SizeForCodeFrame(Code *code)
68 return sizeof(Frame) + sizeof(LocalSlot) * (code->nlocals() + code->maxStackDepth());
72 Stack::pushInvokeFrame(Handle<Function> fun, InvokeArgs &args)
74 size_t nbytes = SizeForCodeFrame(fun->code()) +
75 sizeof(LocalSlot) * fun->nargs();
76 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
81 memset(top_.sp, 0, sizeof(LocalSlot) * fun->nargs());
83 args.nargs_ = fun->nargs();
85 top_.sp += fun->nargs();
87 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
88 new (frame) Frame(top_.fp, top_.pc, fun, fun->code(), zone_->heap().lifoPosition());
90 top_.pc = frame->code()->start();
91 top_.sp = frame->stackBase();
98 Stack::pushNativeFrame(Handle<Native> native, NativeArgs &args, unsigned argc)
100 size_t nbytes = sizeof(intptr_t) * argc + sizeof(Frame);
101 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
106 memset(top_.sp, 0, sizeof(intptr_t) * argc);
107 args.argsBase_ = top_.sp;
109 top_.sp = reinterpret_cast<LocalSlot *>(reinterpret_cast<intptr_t *>(top_.sp) + argc);
111 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
112 new (frame) Frame(top_.fp, top_.pc, native, zone_->heap().lifoPosition());
115 top_.sp = reinterpret_cast<LocalSlot *>(frame + 1);
122 Stack::pushInlineFrame(Handle<Function> fun)
124 Code *code = fun->code();
125 size_t nbytes = SizeForCodeFrame(code);
126 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
127 zone_->reportError(Message_TooMuchRecursion);
131 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
132 new (frame) Frame(top_.fp, top_.pc, fun, code, zone_->heap().lifoPosition());
133 memset(frame->locals(), 0, sizeof(LocalSlot) * code->nlocals());
135 top_.pc = frame->code()->start();
136 top_.sp = frame->stackBase();
144 Stack::popInlineFrame()
146 zone_->heap().unwindLifo(top_.fp->lifoPosition());
148 Frame *prev = top_.fp->prev();
149 Function *fun = top_.fp->fun();
151 top_.pc = top_.fp->prevpc();
152 top_.sp = reinterpret_cast<LocalSlot *>(top_.fp) - fun->nargs();
158 Stack::popInvokeFrame(InvokeArgs &args)
160 zone_->heap().unwindLifo(top_.fp->lifoPosition());
162 top_.pc = top_.fp->prevpc();
163 top_.sp = args.args_;
164 top_.fp = top_.fp->prev();
168 Stack::popNativeFrame(NativeArgs &args)
170 zone_->heap().unwindLifo(top_.fp->lifoPosition());
172 top_.pc = top_.fp->prevpc();
173 top_.sp = args.argsBase_;
174 top_.fp = top_.fp->prev();
178 Frame::accept(VirtualObjectVisitor *visitor)
180 visitor->visit(reinterpret_cast<Object **>(&callee_));
181 visitor->visit(reinterpret_cast<Object **>(&code_));
185 TraceFunctionFrame(VirtualObjectVisitor *visitor, Frame *fp, LocalSlot *sp, bytecode *pc)
187 Code *code = fp->code();
188 LocalSlot *slots = fp->locals();
190 // Trace local variables.
191 for (unsigned i = 0; i < code->nlocals(); i++) {
192 Type *type = code->localType(i);
193 if (type->isTraceable())
194 visitor->visit(&slots[i].obj);
197 // Trace arguments, if a function call. Note that this means we usually
198 // mark arguments twice, once on the caller side and once on the callee.
199 // This makes it easier to deal with invoke frames which have no caller.
200 if (Function *fun = fp->maybeFun()) {
201 for (unsigned i = 0; i < fun->nargs(); i++) {
202 Type *type = fun->argType(i);
203 if (type->isTraceable())
204 visitor->visit(&fp->arg(i).obj);
208 // Finally, trace stack slots. Not every gc-point in the bytecode will
209 // have a safepoint; since they are actually uncommon, we don't emit
210 // them unless at least one stack slot would be marked.
211 if (uint8 *safepoint = code->safepointAt(code->pcToOffset(pc))) {
212 LocalSlot *base = fp->stackBase();
213 unsigned stackDepth = unsigned(sp - fp->stackBase());
214 assert(stackDepth <= code->maxStackDepth());
216 for (unsigned i = 0; i < stackDepth; i++) {
217 if (safepoint[i / 8] & (1 << (i % 8)))
218 visitor->visit(&base[i].obj);
224 Stack::accept(VirtualObjectVisitor *visitor)
226 LocalSlot *sp = top_.sp;
227 bytecode *pc = top_.pc;
229 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
230 if (!frame->isNative())
231 TraceFunctionFrame(visitor, frame, sp, pc);
232 frame->accept(visitor);
234 sp = reinterpret_cast<LocalSlot *>(frame);
235 pc = frame->prevpc();
240 Stack::isInCallStack(Handle<Module> module)
242 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
243 if (frame->code()->module() == module)
249 StackIter::StackIter(Zone *zone)
250 : pc_(zone->stack().top()->pc),
251 fp_(zone->stack().top()->fp)
256 StackIter::done() const
262 StackIter::function() const
265 return fp_->native()->name()->chars();
266 if (Function *fun = fp_->maybeFun())
267 return fun->name()->chars();
272 StackIter::lineNumber() const
276 if (fp_->maybeFun()) {
277 unsigned offs = fp_->code()->pcToOffset(pc_);
278 return fp_->code()->pcToSource(offs).line;
280 return fp_->code()->lineNumber();
284 StackIter::source() const
288 return fp_->code()->module()->file()
289 ? fp_->code()->module()->file()->chars()