Skip to content

Commit

Permalink
Show file/line in stack traces (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Oct 6, 2022
1 parent 9503e92 commit a08bb00
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1691,10 +1691,11 @@ UMKA_API int umkaGetFunc(void *umka, const char *moduleName, const char *funcNam
Gets an Umka function that can be called from C/C++ using `umkaCall()`. Here, `umka` is the interpreter instance handle, `moduleName` is the Umka module name, `funcName` is the Umka function name. Returns the function entry point offset.

```
UMKA_API bool umkaGetCallStack(void *umka, int depth, int *offset, char *name, int size);
UMKA_API bool umkaGetCallStack(void *umka, int depth, int nameSize,
int *offset, char *fileName, char *fnName, int *line);
```

Gets the Umka function `name` and entry point `offset` from the call stack at the specified call `depth`. If `depth` is zero, the current function information is retrieved. Here, `umka` is the interpreter instance handle and `size` is the `name` buffer size, including the null character. Returns `true` on success.
Gets the Umka call stack entry at the specified call `depth`. The entry is defined by the bytecode `offset`, source file `fileName`, function `fnName` and source `line`. If `depth` is zero, the current function information is retrieved. Here, `umka` is the interpreter instance handle, `nameSize` is the `fileName` or `fnName` buffer size, including the null character. Returns `true` on success.

```
UMKA_API void umkaSetHook(void *umka, UmkaHookEvent event, UmkaHookFunc hook);
Expand Down
8 changes: 4 additions & 4 deletions src/umka.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ void printRuntimeError(void *umka)

for (int depth = 0; depth < MAX_CALL_STACK_DEPTH; depth++)
{
char fnName[UMKA_MSG_LEN + 1];
int fnOffset;
char fileName[UMKA_MSG_LEN + 1], fnName[UMKA_MSG_LEN + 1];
int line;

if (!umkaGetCallStack(umka, depth, &fnOffset, fnName, UMKA_MSG_LEN + 1))
if (!umkaGetCallStack(umka, depth, UMKA_MSG_LEN + 1, NULL, fileName, fnName, &line))
break;

fprintf(stderr, "%08d: %s\n", fnOffset, fnName);
fprintf(stderr, " %s: %s (%d)\n", fnName, fileName, line);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/umka_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ UMKA_API int umkaGetFunc(void *umka, const char *moduleName, const char *funcNam
}


UMKA_API bool umkaGetCallStack(void *umka, int depth, int *offset, char *name, int size)
UMKA_API bool umkaGetCallStack(void *umka, int depth, int nameSize, int *offset, char *fileName, char *fnName, int *line)
{
Compiler *comp = umka;
Slot *base = comp->vm.fiber->base;
Expand All @@ -197,8 +197,14 @@ UMKA_API bool umkaGetCallStack(void *umka, int depth, int *offset, char *name, i
if (offset)
*offset = ip;

if (name)
snprintf(name, size, "%s", comp->vm.fiber->debugPerInstr[ip].fnName);
if (fileName)
snprintf(fileName, nameSize, "%s", comp->vm.fiber->debugPerInstr[ip].fileName);

if (fnName)
snprintf(fnName, nameSize, "%s", comp->vm.fiber->debugPerInstr[ip].fnName);

if (line)
*line = comp->vm.fiber->debugPerInstr[ip].line;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/umka_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ UMKA_API void umkaAsm (void *umka, char *buf, int size);
UMKA_API bool umkaAddModule (void *umka, const char *fileName, const char *sourceString);
UMKA_API bool umkaAddFunc (void *umka, const char *name, UmkaExternFunc func);
UMKA_API int umkaGetFunc (void *umka, const char *moduleName, const char *funcName);
UMKA_API bool umkaGetCallStack (void *umka, int depth, int *offset, char *name, int size);
UMKA_API bool umkaGetCallStack (void *umka, int depth, int nameSize, int *offset, char *fileName, char *fnName, int *line);
UMKA_API void umkaSetHook (void *umka, UmkaHookEvent event, UmkaHookFunc hook);
UMKA_API void *umkaAllocData (void *umka, int size, UmkaExternFunc onFree);
UMKA_API void umkaIncRef (void *umka, void *ptr);
Expand Down

0 comments on commit a08bb00

Please sign in to comment.