Skip to content

Commit 1b2db30

Browse files
committed
Add tracing checkpoint policy
This option is configurable, this way you can choose when it sends data, for example: only after input primitives. Additionally, the data it sends can also be configured, for example: only send the program counter.
1 parent 8479bb8 commit 1b2db30

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/Debug/debugger.cpp

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Debugger::Debugger(Channel *duplex) {
2525
this->checkpointInterval = 10;
2626
this->instructions_executed = 0;
2727
this->fidx_called = {};
28+
this->min_return_values = 10;
29+
this->checkpoint_state = nullptr;
30+
this->checkpoint_state_size = 0;
2831
this->remaining_instructions = -1;
2932
}
3033

@@ -965,11 +968,33 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray,
965968
}
966969

967970
void Debugger::setSnapshotPolicy(Module *m, uint8_t *interruptData) {
968-
snapshotPolicy = SnapshotPolicy{*interruptData};
971+
uint8_t **data_ptr = &interruptData;
972+
if (*interruptData <= 2) {
973+
snapshotPolicy = SnapshotPolicy{*interruptData};
974+
min_return_values = 0;
975+
if (checkpoint_state) {
976+
free(checkpoint_state);
977+
}
978+
checkpoint_state = nullptr;
979+
checkpoint_state_size = 0;
980+
} else {
981+
snapshotPolicy = SnapshotPolicy::checkpointing;
982+
*data_ptr += 1;
983+
min_return_values = read_LEB_32(data_ptr);
984+
if (checkpoint_state) {
985+
free(checkpoint_state);
986+
}
987+
checkpoint_state_size = read_LEB_32(data_ptr);
988+
checkpoint_state = new uint8_t[checkpoint_state_size];
989+
for (int i = 0; i < checkpoint_state_size; i++) {
990+
checkpoint_state[i] = **data_ptr;
991+
*data_ptr += 1;
992+
}
993+
}
969994

970995
// Make a checkpoint when you first enable checkpointing
971996
if (snapshotPolicy == SnapshotPolicy::checkpointing) {
972-
uint8_t *ptr = interruptData + 1;
997+
uint8_t *ptr = *data_ptr + 1;
973998
checkpointInterval = read_B32(&ptr);
974999
checkpoint(m, true);
9751000
}
@@ -999,7 +1024,14 @@ void Debugger::handleSnapshotPolicy(Module *m) {
9991024
this->channel->write("\n");
10001025
} else if (snapshotPolicy == SnapshotPolicy::checkpointing) {
10011026
if (instructions_executed >= checkpointInterval || fidx_called) {
1002-
checkpoint(m);
1027+
if (min_return_values == 0) {
1028+
checkpoint(m);
1029+
} else if (fidx_called) {
1030+
const Type *type = m->functions[*fidx_called].type;
1031+
if (type->result_count >= min_return_values) {
1032+
checkpoint(m);
1033+
}
1034+
}
10031035
}
10041036
instructions_executed++;
10051037

@@ -1032,9 +1064,23 @@ void Debugger::checkpoint(Module *m, bool force) {
10321064
comma = true;
10331065
}
10341066
this->channel->write("], ");
1067+
1068+
// Return values:
1069+
this->channel->write(R"("returns": [)");
1070+
comma = false;
1071+
for (uint32_t i = 0; i < func_block.type->result_count; i++) {
1072+
channel->write("%s%d", comma ? ", " : "",
1073+
m->stack[m->sp - i].value.uint32);
1074+
comma = true;
1075+
}
1076+
this->channel->write("], ");
1077+
}
1078+
this->channel->write(R"("snapshot": )");
1079+
if (!checkpoint_state) {
1080+
snapshot(m);
1081+
} else {
1082+
inspect(m, checkpoint_state_size, checkpoint_state);
10351083
}
1036-
this->channel->write(R"("snapshot": )", instructions_executed);
1037-
snapshot(m);
10381084
this->channel->write("}\n");
10391085
instructions_executed = 0;
10401086
}

src/Debug/debugger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class Debugger {
138138
uint32_t instructions_executed; // #instructions since last checkpoint
139139
std::optional<uint32_t> fidx_called; // The primitive that was executed
140140
uint32_t prim_args[8]; // The arguments of the executed prim
141+
uint32_t min_return_values;
142+
uint32_t checkpoint_state_size;
143+
uint8_t *checkpoint_state;
141144

142145
// Continue for
143146
int32_t remaining_instructions;

0 commit comments

Comments
 (0)