Skip to content

Commit

Permalink
Add line numbers to JIT stack traces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doom2fan committed Dec 18, 2018
1 parent 27ecae2 commit e885376
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/scripting/vm/jit.cpp
Expand Up @@ -98,17 +98,33 @@ asmjit::CCFunc *JitCompiler::Codegen()
{
Setup();

LatestLine = { 0, (ptrdiff_t)0, -1, {} };

pc = sfunc->Code;
auto end = pc + sfunc->CodeSize;
while (pc != end)
{
int i = (int)(ptrdiff_t)(pc - sfunc->Code);
op = pc->op;

int curLine = sfunc->PCToLine(pc);
auto label = cc.newLabel ();
cc.bind (label);
LatestLine.Label = label;
if (curLine != LatestLine.LineNumber)
{
FString linfo;
linfo.Format ("; line %d ========== DEBUG ==========", curLine);
cc.comment (linfo.GetChars (), linfo.Len ());
LatestLine.LineNumber = curLine;
LatestLine.VMInstructionIndex = i;
LineInfo.Push (LatestLine);
}

if (op != OP_PARAM && op != OP_PARAMI && op != OP_VTBL)
{
FString lineinfo;
lineinfo.Format("; line %d: %02x%02x%02x%02x %s", sfunc->PCToLine(pc), pc->op, pc->a, pc->b, pc->c, OpNames[op]);
lineinfo.Format("; line %d: %02x%02x%02x%02x %s", curLine, pc->op, pc->a, pc->b, pc->c, OpNames[op]);
cc.comment("", 0);
cc.comment(lineinfo.GetChars(), lineinfo.Len());
}
Expand All @@ -125,6 +141,21 @@ asmjit::CCFunc *JitCompiler::Codegen()
cc.endFunc();
cc.finalize();

auto code = cc.getCode ();
for (unsigned int j = 0; j < LineInfo.Size (); j++)
{
auto info = LineInfo[j];

if (!code->isLabelValid (info.Label))
{
continue;
}

info.InstructionIndex = code->getLabelOffset (info.Label);

LineInfo[j] = info;
}

return func;
}

Expand Down
18 changes: 17 additions & 1 deletion src/scripting/vm/jit_runtime.cpp
Expand Up @@ -10,6 +10,7 @@ struct JitFuncInfo
{
FString name;
FString filename;
TArray<JitLineInfo> LineInfo;
void *start;
void *end;
};
Expand Down Expand Up @@ -293,7 +294,7 @@ void *AddJitFunction(asmjit::CodeHolder* code, JitCompiler *compiler)
I_Error("RtlAddFunctionTable failed");
#endif

JitDebugInfo.Push({ compiler->GetScriptFunction()->PrintableName, compiler->GetScriptFunction()->SourceFileName, startaddr, endaddr });
JitDebugInfo.Push({ compiler->GetScriptFunction()->PrintableName, compiler->GetScriptFunction()->SourceFileName, compiler->LineInfo, startaddr, endaddr });

return p;
}
Expand Down Expand Up @@ -840,6 +841,20 @@ static int CaptureStackTrace(int max_frames, void **out_frames)
#endif
}

int JITPCToLine(uint8_t *pc, const JitFuncInfo *info)
{
int PCIndex = int(pc - ((uint8_t *) (info->start)));
if (info->LineInfo.Size () == 1) return info->LineInfo[0].LineNumber;
for (unsigned i = 1; i < info->LineInfo.Size (); i++)
{
if (info->LineInfo[i].InstructionIndex >= PCIndex)
{
return info->LineInfo[i - 1].LineNumber;
}
}
return -1;
}

FString JitGetStackFrameName(void *pc)
{
FString s;
Expand All @@ -850,6 +865,7 @@ FString JitGetStackFrameName(void *pc)
if (pc >= info.start && pc < info.end)
{
int line = -1;
line = JITPCToLine ((uint8_t *)pc, &info);
/*for (unsigned int j = 0; j < info.lines.Size(); j++)
{
if (info.lines[j].pc <= pc)
Expand Down
12 changes: 12 additions & 0 deletions src/scripting/vm/jitintern.h
Expand Up @@ -25,6 +25,14 @@ extern int VMCalls[10];
#define ABCs (pc[0].i24)
#define JMPOFS(x) ((x)->i24)

struct JitLineInfo
{
uint16_t VMInstructionIndex;
ptrdiff_t InstructionIndex;
int32_t LineNumber;
asmjit::Label Label;
};

class JitCompiler
{
public:
Expand All @@ -33,6 +41,8 @@ class JitCompiler
asmjit::CCFunc *Codegen();
VMScriptFunction *GetScriptFunction() { return sfunc; }

TArray<JitLineInfo> LineInfo;

private:
// Declare EmitXX functions for the opcodes:
#define xx(op, name, mode, alt, kreg, ktype) void Emit##op();
Expand Down Expand Up @@ -260,6 +270,8 @@ class JitCompiler
TArray<asmjit::X86Gp> regA;
TArray<asmjit::X86Gp> regS;

JitLineInfo LatestLine;

struct OpcodeLabel
{
asmjit::CBNode *cursor = nullptr;
Expand Down

0 comments on commit e885376

Please sign in to comment.