From 56e5c19aa94fbd7da8a52eb87c93c7745e68747a Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 13 Dec 2018 19:23:07 -0500 Subject: [PATCH] Add debug/arg-stack, and add slots debug/stack. These features should help implementing a debugger. --- src/core/debug.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/core/debug.c b/src/core/debug.c index 7e9c679f..f4b7edb7 100644 --- a/src/core/debug.c +++ b/src/core/debug.c @@ -203,6 +203,8 @@ static Janet doframe(JanetStackFrame *frame) { janet_table_put(t, janet_csymbolv(":tail"), janet_wrap_true()); } if (frame->func && frame->pc) { + Janet *stack = (Janet *)frame + JANET_FRAME_SIZE; + JanetArray *slots; off = (int32_t) (frame->pc - def->bytecode); janet_table_put(t, janet_csymbolv(":pc"), janet_wrap_integer(off)); if (def->sourcemap) { @@ -213,6 +215,11 @@ static Janet doframe(JanetStackFrame *frame) { if (def->source) { janet_table_put(t, janet_csymbolv(":source"), janet_wrap_string(def->source)); } + /* Add stack arguments */ + slots = janet_array(def->slotcount); + memcpy(slots->data, stack, sizeof(Janet) * def->slotcount); + slots->count = def->slotcount; + janet_table_put(t, janet_csymbolv(":slots"), janet_wrap_array(slots)); } return janet_wrap_table(t); } @@ -235,6 +242,17 @@ static int cfun_stack(JanetArgs args) { JANET_RETURN_ARRAY(args, array); } +static int cfun_argstack(JanetArgs args) { + JanetFiber *fiber; + JanetArray *array; + JANET_FIXARITY(args, 1); + JANET_ARG_FIBER(fiber, args, 0); + array = janet_array(fiber->stacktop - fiber->stackstart); + memcpy(array->data, fiber->data + fiber->stackstart, array->capacity * sizeof(Janet)); + array->count = array->capacity; + JANET_RETURN_ARRAY(args, array); +} + static const JanetReg cfuns[] = { {"debug/break", cfun_break, "(debug/break source byte-offset)\n\n" @@ -256,6 +274,11 @@ static const JanetReg cfuns[] = { {"debug/funbreak", cfun_unfbreak, "(debug/fbreak fun [,pc=0])\n\n" "Unset a breakpoint set with debug/fbreak."}, + {"debug/arg-stack", cfun_argstack, + "(debug/arg-stack fiber)\n\n" + "Gets all values currently on the fiber's argument stack. Normally, " + "this should be empty unless the fiber signals while pushing arguments " + "to make a function call. Returns a new array."}, {"debug/stack", cfun_stack, "(debug/stack fib)\n\n" "Gets information about the stack as an array of tables. Each table " @@ -269,6 +292,7 @@ static const JanetReg cfuns[] = { "\t:name - the human friendly name of the function\n" "\t:pc - integer indicating the location of the program counter\n" "\t:source - string with filename or other identifier for the source code\n" + "\t:slots - array of all values in each slot\n" "\t:tail - boolean indicating a tail call" }, {"debug/lineage", cfun_lineage,