2018-12-13 23:46:53 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2018-12-13 23:46:53 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-12-13 23:46:53 +00:00
|
|
|
#include "gc.h"
|
|
|
|
#include "state.h"
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.h"
|
2019-01-31 04:07:30 +00:00
|
|
|
#include "vector.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-12-13 23:46:53 +00:00
|
|
|
|
|
|
|
/* Implements functionality to build a debugger from within janet.
|
|
|
|
* The repl should also be able to serve as pretty featured debugger
|
|
|
|
* out of the box. */
|
|
|
|
|
|
|
|
/* Add a break point to a function */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_debug_break(JanetFuncDef *def, int32_t pc) {
|
2018-12-13 23:46:53 +00:00
|
|
|
if (pc >= def->bytecode_length || pc < 0)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("invalid bytecode offset");
|
2018-12-13 23:46:53 +00:00
|
|
|
def->bytecode[pc] |= 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a break point from a function */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_debug_unbreak(JanetFuncDef *def, int32_t pc) {
|
2018-12-13 23:46:53 +00:00
|
|
|
if (pc >= def->bytecode_length || pc < 0)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("invalid bytecode offset");
|
2018-12-13 23:46:53 +00:00
|
|
|
def->bytecode[pc] &= ~((uint32_t)0x80);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a location for a breakpoint given a source file an
|
|
|
|
* location.
|
|
|
|
*/
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_debug_find(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetFuncDef **def_out, int32_t *pc_out,
|
2019-09-22 22:18:28 +00:00
|
|
|
const uint8_t *source, int32_t sourceLine, int32_t sourceColumn) {
|
2018-12-13 23:46:53 +00:00
|
|
|
/* Scan the heap for right func def */
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject *current = janet_vm_blocks;
|
2018-12-13 23:46:53 +00:00
|
|
|
/* Keep track of the best source mapping we have seen so far */
|
|
|
|
int32_t besti = -1;
|
2019-09-22 22:18:28 +00:00
|
|
|
int32_t best_line = -1;
|
|
|
|
int32_t best_column = -1;
|
2018-12-13 23:46:53 +00:00
|
|
|
JanetFuncDef *best_def = NULL;
|
|
|
|
while (NULL != current) {
|
|
|
|
if ((current->flags & JANET_MEM_TYPEBITS) == JANET_MEMORY_FUNCDEF) {
|
2019-09-22 22:18:28 +00:00
|
|
|
JanetFuncDef *def = (JanetFuncDef *)(current);
|
2018-12-13 23:46:53 +00:00
|
|
|
if (def->sourcemap &&
|
|
|
|
def->source &&
|
|
|
|
!janet_string_compare(source, def->source)) {
|
|
|
|
/* Correct source file, check mappings. The chosen
|
2019-09-22 22:18:28 +00:00
|
|
|
* pc index is the instruction closest to the given line column, but
|
|
|
|
* not after. */
|
2018-12-13 23:46:53 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < def->bytecode_length; i++) {
|
2019-09-22 22:18:28 +00:00
|
|
|
int32_t line = def->sourcemap[i].line;
|
|
|
|
int32_t column = def->sourcemap[i].column;
|
|
|
|
if (line <= sourceLine && line >= best_line) {
|
2019-09-22 23:08:38 +00:00
|
|
|
if (column <= sourceColumn &&
|
|
|
|
(line > best_line || column > best_column)) {
|
2019-09-22 22:18:28 +00:00
|
|
|
best_line = line;
|
|
|
|
best_column = column;
|
|
|
|
besti = i;
|
|
|
|
best_def = def;
|
|
|
|
}
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
if (best_def) {
|
|
|
|
*def_out = best_def;
|
|
|
|
*pc_out = besti;
|
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("could not find breakpoint");
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 04:07:30 +00:00
|
|
|
/* Error reporting. This can be emulated from within Janet, but for
|
|
|
|
* consitency with the top level code it is defined once. */
|
|
|
|
void janet_stacktrace(JanetFiber *fiber, Janet err) {
|
|
|
|
int32_t fi;
|
|
|
|
const char *errstr = (const char *)janet_to_string(err);
|
|
|
|
JanetFiber **fibers = NULL;
|
|
|
|
int wrote_error = 0;
|
|
|
|
|
2019-08-13 00:20:01 +00:00
|
|
|
int print_color = janet_truthy(janet_dyn("err-color"));
|
2019-10-19 14:44:27 +00:00
|
|
|
if (print_color) janet_eprintf("\x1b[31m");
|
2019-08-13 00:20:01 +00:00
|
|
|
|
2019-01-31 04:07:30 +00:00
|
|
|
while (fiber) {
|
|
|
|
janet_v_push(fibers, fiber);
|
|
|
|
fiber = fiber->child;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (fi = janet_v_count(fibers) - 1; fi >= 0; fi--) {
|
|
|
|
fiber = fibers[fi];
|
|
|
|
int32_t i = fiber->frame;
|
|
|
|
while (i > 0) {
|
|
|
|
JanetStackFrame *frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
|
|
|
JanetFuncDef *def = NULL;
|
|
|
|
i = frame->prevframe;
|
|
|
|
|
|
|
|
/* Print prelude to stack frame */
|
|
|
|
if (!wrote_error) {
|
|
|
|
JanetFiberStatus status = janet_fiber_status(fiber);
|
|
|
|
const char *prefix = status == JANET_STATUS_ERROR ? "" : "status ";
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf("%s%s: %s\n",
|
|
|
|
prefix,
|
|
|
|
janet_status_names[status],
|
|
|
|
errstr);
|
2019-01-31 04:07:30 +00:00
|
|
|
wrote_error = 1;
|
|
|
|
}
|
|
|
|
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" in");
|
2019-01-31 04:07:30 +00:00
|
|
|
|
|
|
|
if (frame->func) {
|
|
|
|
def = frame->func->def;
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" %s", def->name ? (const char *)def->name : "<anonymous>");
|
2019-01-31 04:07:30 +00:00
|
|
|
if (def->source) {
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" [%s]", (const char *)def->source);
|
2019-01-31 04:07:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JanetCFunction cfun = (JanetCFunction)(frame->pc);
|
|
|
|
if (cfun) {
|
|
|
|
Janet name = janet_table_get(janet_vm_registry, janet_wrap_cfunction(cfun));
|
|
|
|
if (!janet_checktype(name, JANET_NIL))
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" %s", (const char *)janet_to_string(name));
|
2019-01-31 04:07:30 +00:00
|
|
|
else
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" <cfunction>");
|
2019-01-31 04:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frame->flags & JANET_STACKFRAME_TAILCALL)
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" (tailcall)");
|
2019-01-31 04:07:30 +00:00
|
|
|
if (frame->func && frame->pc) {
|
2019-02-20 01:51:34 +00:00
|
|
|
int32_t off = (int32_t)(frame->pc - def->bytecode);
|
2019-01-31 04:07:30 +00:00
|
|
|
if (def->sourcemap) {
|
|
|
|
JanetSourceMapping mapping = def->sourcemap[off];
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" on line %d, column %d", mapping.line, mapping.column);
|
2019-01-31 04:07:30 +00:00
|
|
|
} else {
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf(" pc=%d", off);
|
2019-01-31 04:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-19 14:44:27 +00:00
|
|
|
janet_eprintf("\n");
|
2019-01-31 04:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 14:44:27 +00:00
|
|
|
if (print_color) janet_eprintf("\x1b[0m");
|
2019-08-13 00:20:01 +00:00
|
|
|
|
2019-01-31 04:07:30 +00:00
|
|
|
janet_v_free(fibers);
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:46:53 +00:00
|
|
|
/*
|
|
|
|
* CFuns
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Helper to find funcdef and bytecode offset to insert or remove breakpoints.
|
|
|
|
* Takes a source file name and byte offset. */
|
2019-01-06 01:09:03 +00:00
|
|
|
static void helper_find(int32_t argc, Janet *argv, JanetFuncDef **def, int32_t *bytecode_offset) {
|
2019-09-22 22:18:28 +00:00
|
|
|
janet_fixarity(argc, 3);
|
2019-01-06 01:09:03 +00:00
|
|
|
const uint8_t *source = janet_getstring(argv, 0);
|
2019-09-22 22:18:28 +00:00
|
|
|
int32_t line = janet_getinteger(argv, 1);
|
|
|
|
int32_t col = janet_getinteger(argv, 2);
|
|
|
|
janet_debug_find(def, bytecode_offset, source, line, col);
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to find funcdef and bytecode offset to insert or remove breakpoints.
|
|
|
|
* Takes a function and byte offset*/
|
2019-01-06 01:09:03 +00:00
|
|
|
static void helper_find_fun(int32_t argc, Janet *argv, JanetFuncDef **def, int32_t *bytecode_offset) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
JanetFunction *func = janet_getfunction(argv, 0);
|
|
|
|
int32_t offset = (argc == 2) ? janet_getinteger(argv, 1) : 0;
|
2018-12-13 23:46:53 +00:00
|
|
|
*def = func->def;
|
|
|
|
*bytecode_offset = offset;
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_break(int32_t argc, Janet *argv) {
|
2018-12-13 23:46:53 +00:00
|
|
|
JanetFuncDef *def;
|
|
|
|
int32_t offset;
|
2019-01-06 01:09:03 +00:00
|
|
|
helper_find(argc, argv, &def, &offset);
|
|
|
|
janet_debug_break(def, offset);
|
|
|
|
return janet_wrap_nil();
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_unbreak(int32_t argc, Janet *argv) {
|
2018-12-13 23:46:53 +00:00
|
|
|
JanetFuncDef *def;
|
2019-02-22 17:10:27 +00:00
|
|
|
int32_t offset = 0;
|
2019-01-06 01:09:03 +00:00
|
|
|
helper_find(argc, argv, &def, &offset);
|
|
|
|
janet_debug_unbreak(def, offset);
|
|
|
|
return janet_wrap_nil();
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_fbreak(int32_t argc, Janet *argv) {
|
2018-12-13 23:46:53 +00:00
|
|
|
JanetFuncDef *def;
|
2019-02-22 17:10:27 +00:00
|
|
|
int32_t offset = 0;
|
2019-01-06 01:09:03 +00:00
|
|
|
helper_find_fun(argc, argv, &def, &offset);
|
|
|
|
janet_debug_break(def, offset);
|
|
|
|
return janet_wrap_nil();
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_unfbreak(int32_t argc, Janet *argv) {
|
2018-12-13 23:46:53 +00:00
|
|
|
JanetFuncDef *def;
|
|
|
|
int32_t offset;
|
2019-01-06 01:09:03 +00:00
|
|
|
helper_find_fun(argc, argv, &def, &offset);
|
|
|
|
janet_debug_unbreak(def, offset);
|
|
|
|
return janet_wrap_nil();
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_lineage(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
JanetArray *array = janet_array(0);
|
2018-12-13 23:46:53 +00:00
|
|
|
while (fiber) {
|
|
|
|
janet_array_push(array, janet_wrap_fiber(fiber));
|
|
|
|
fiber = fiber->child;
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_array(array);
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract info from one stack frame */
|
|
|
|
static Janet doframe(JanetStackFrame *frame) {
|
|
|
|
int32_t off;
|
|
|
|
JanetTable *t = janet_table(3);
|
|
|
|
JanetFuncDef *def = NULL;
|
|
|
|
if (frame->func) {
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("function"), janet_wrap_function(frame->func));
|
2018-12-13 23:46:53 +00:00
|
|
|
def = frame->func->def;
|
|
|
|
if (def->name) {
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("name"), janet_wrap_string(def->name));
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JanetCFunction cfun = (JanetCFunction)(frame->pc);
|
|
|
|
if (cfun) {
|
|
|
|
Janet name = janet_table_get(janet_vm_registry, janet_wrap_cfunction(cfun));
|
|
|
|
if (!janet_checktype(name, JANET_NIL)) {
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("name"), name);
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("c"), janet_wrap_true());
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
if (frame->flags & JANET_STACKFRAME_TAILCALL) {
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("tail"), janet_wrap_true());
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
if (frame->func && frame->pc) {
|
2018-12-14 00:23:07 +00:00
|
|
|
Janet *stack = (Janet *)frame + JANET_FRAME_SIZE;
|
|
|
|
JanetArray *slots;
|
2019-02-20 01:51:34 +00:00
|
|
|
off = (int32_t)(frame->pc - def->bytecode);
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("pc"), janet_wrap_integer(off));
|
2018-12-13 23:46:53 +00:00
|
|
|
if (def->sourcemap) {
|
|
|
|
JanetSourceMapping mapping = def->sourcemap[off];
|
2019-09-22 22:18:28 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("source-line"), janet_wrap_integer(mapping.line));
|
|
|
|
janet_table_put(t, janet_ckeywordv("source-column"), janet_wrap_integer(mapping.column));
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
if (def->source) {
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("source"), janet_wrap_string(def->source));
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
2018-12-14 00:23:07 +00:00
|
|
|
/* Add stack arguments */
|
|
|
|
slots = janet_array(def->slotcount);
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(slots->data, stack, sizeof(Janet) * def->slotcount);
|
2018-12-14 00:23:07 +00:00
|
|
|
slots->count = def->slotcount;
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(t, janet_ckeywordv("slots"), janet_wrap_array(slots));
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
return janet_wrap_table(t);
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_stack(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
JanetArray *array = janet_array(0);
|
2018-12-13 23:46:53 +00:00
|
|
|
{
|
|
|
|
int32_t i = fiber->frame;
|
|
|
|
JanetStackFrame *frame;
|
|
|
|
while (i > 0) {
|
|
|
|
frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
|
|
|
janet_array_push(array, doframe(frame));
|
|
|
|
i = frame->prevframe;
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_array(array);
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:07:30 +00:00
|
|
|
static Janet cfun_debug_stacktrace(int32_t argc, Janet *argv) {
|
2021-01-17 19:55:40 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
2019-01-31 04:07:30 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
2021-01-17 19:55:40 +00:00
|
|
|
Janet x = argc == 1 ? janet_wrap_nil() : argv[1];
|
|
|
|
x = janet_checktype(x, JANET_NIL) ? fiber->last_value : x;
|
|
|
|
janet_stacktrace(fiber, x);
|
2019-01-31 04:07:30 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_debug_argstack(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
JanetArray *array = janet_array(fiber->stacktop - fiber->stackstart);
|
2018-12-14 00:23:07 +00:00
|
|
|
memcpy(array->data, fiber->data + fiber->stackstart, array->capacity * sizeof(Janet));
|
|
|
|
array->count = array->capacity;
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_array(array);
|
2018-12-14 00:23:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 00:14:34 +00:00
|
|
|
static Janet cfun_debug_step(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
Janet out = janet_wrap_nil();
|
|
|
|
janet_step(fiber, argc == 1 ? janet_wrap_nil() : argv[1], &out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg debug_cfuns[] = {
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/break", cfun_debug_break,
|
2021-01-10 18:02:28 +00:00
|
|
|
JDOC("(debug/break source line col)\n\n"
|
|
|
|
"Sets a breakpoint in `source` at a given line and column. "
|
2019-09-22 22:18:28 +00:00
|
|
|
"Will throw an error if the breakpoint location "
|
2019-02-20 01:51:34 +00:00
|
|
|
"cannot be found. For example\n\n"
|
2021-01-10 18:02:28 +00:00
|
|
|
"\t(debug/break \"core.janet\" 10 4)\n\n"
|
|
|
|
"wil set a breakpoint at line 10, 4th column of the file core.janet.")
|
2019-01-06 06:49:56 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/unbreak", cfun_debug_unbreak,
|
2019-09-22 22:18:28 +00:00
|
|
|
JDOC("(debug/unbreak source line column)\n\n"
|
|
|
|
"Remove a breakpoint with a source key at a given line and column. "
|
|
|
|
"Will throw an error if the breakpoint "
|
2019-02-20 01:51:34 +00:00
|
|
|
"cannot be found.")
|
2019-01-06 06:49:56 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/fbreak", cfun_debug_fbreak,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(debug/fbreak fun &opt pc)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Set a breakpoint in a given function. pc is an optional offset, which "
|
|
|
|
"is in bytecode instructions. fun is a function value. Will throw an error "
|
|
|
|
"if the offset is too large or negative.")
|
2019-01-06 06:49:56 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/unfbreak", cfun_debug_unfbreak,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(debug/unfbreak fun &opt pc)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Unset a breakpoint set with debug/fbreak.")
|
2019-01-06 06:49:56 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/arg-stack", cfun_debug_argstack,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(debug/arg-stack fiber)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"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.")
|
2018-12-13 23:46:53 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/stack", cfun_debug_stack,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(debug/stack fib)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Gets information about the stack as an array of tables. Each table "
|
2021-01-13 22:47:50 +00:00
|
|
|
"in the array contains information about a stack frame. The top-most, current "
|
|
|
|
"stack frame is the first table in the array, and the bottom-most stack frame "
|
2019-02-20 01:51:34 +00:00
|
|
|
"is the last value. Each stack frame contains some of the following attributes:\n\n"
|
2021-01-13 22:47:50 +00:00
|
|
|
"* :c - true if the stack frame is a c function invocation\n\n"
|
|
|
|
"* :column - the current source column of the stack frame\n\n"
|
|
|
|
"* :function - the function that the stack frame represents\n\n"
|
|
|
|
"* :line - the current source line of the stack frame\n\n"
|
|
|
|
"* :name - the human-friendly name of the function\n\n"
|
|
|
|
"* :pc - integer indicating the location of the program counter\n\n"
|
|
|
|
"* :source - string with the file path or other identifier for the source code\n\n"
|
|
|
|
"* :slots - array of all values in each slot\n\n"
|
|
|
|
"* :tail - boolean indicating a tail call")
|
2019-01-06 06:49:56 +00:00
|
|
|
},
|
2019-01-31 04:07:30 +00:00
|
|
|
{
|
|
|
|
"debug/stacktrace", cfun_debug_stacktrace,
|
2021-01-17 19:55:40 +00:00
|
|
|
JDOC("(debug/stacktrace fiber &opt err)\n\n"
|
|
|
|
"Prints a nice looking stacktrace for a fiber. Can optionally provide "
|
|
|
|
"an error value to print the stack trace with. If `err` is nil or not "
|
|
|
|
"provided, will default to `(fiber/last-value fiber)`. Returns the fiber.")
|
2019-01-31 04:07:30 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"debug/lineage", cfun_debug_lineage,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(debug/lineage fib)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns an array of all child fibers from a root fiber. This function "
|
|
|
|
"is useful when a fiber signals or errors to an ancestor fiber. Using this function, "
|
|
|
|
"the fiber handling the error can see which fiber raised the signal. This function should "
|
|
|
|
"be used mostly for debugging purposes.")
|
2018-12-13 23:46:53 +00:00
|
|
|
},
|
2019-11-26 00:14:34 +00:00
|
|
|
{
|
|
|
|
"debug/step", cfun_debug_step,
|
|
|
|
JDOC("(debug/step fiber &opt x)\n\n"
|
|
|
|
"Run a fiber for one virtual instruction of the Janet machine. Can optionally "
|
|
|
|
"pass in a value that will be passed as the resuming value. Returns the signal value, "
|
|
|
|
"which will usually be nil, as breakpoints raise nil signals.")
|
|
|
|
},
|
2018-12-13 23:46:53 +00:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_debug(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, debug_cfuns);
|
2018-12-13 23:46:53 +00:00
|
|
|
}
|