Skip to content

Instantly share code, notes, and snippets.

@bakpakin
Created September 22, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakpakin/1353b493cccc316960652e9a348aeb8e to your computer and use it in GitHub Desktop.
Save bakpakin/1353b493cccc316960652e9a348aeb8e to your computer and use it in GitHub Desktop.
Add line, column to stacktraces
diff --git a/src/core/debug.c b/src/core/debug.c
index f433869..2aa238d 100644
--- a/src/core/debug.c
+++ b/src/core/debug.c
@@ -150,8 +150,44 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
if (frame->func && frame->pc) {
int32_t off = (int32_t)(frame->pc - def->bytecode);
if (def->sourcemap) {
+ /* Try to get line and column information */
JanetSourceMapping mapping = def->sourcemap[off];
- fprintf(out, " at (%d:%d)", mapping.start, mapping.end);
+ char buf[1024];
+ size_t nread;
+ int32_t offset = 0;
+ int32_t line = 1;
+ int32_t col = 1;
+ int notdone = 1;
+ char last = 0;
+ FILE *f;
+ if (def->source && (f = fopen((const char *)def->source, "rb"))) {
+ while (notdone && (nread = fread(buf, 1, sizeof(buf), f)) > 0) {
+ for(size_t i = 0; i < nread; i++) {
+ char c = buf[i];
+ if (c == '\r') {
+ line++;
+ col = 1;
+ } if (c == '\n') {
+ col = 1;
+ if (last != '\r') {
+ line++;
+ }
+ } else {
+ col++;
+ }
+ last = c;
+ if (offset == mapping.start) {
+ fprintf(out, " on line %d, column %d", line, col);
+ notdone = 0;
+ break;
+ }
+ offset++;
+ }
+ }
+ fclose(f);
+ } else {
+ fprintf(out, " at (%d:%d)", mapping.start, mapping.end);
+ }
} else {
fprintf(out, " pc=%d", off);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment