Skip to content

Commit 5acef13

Browse files
committed
Support snapshot interval on tracing snapshot policy
This also makes the intervals actual UInt's so it can represent 0xffffffff which disables full snapshots in tracing mode.
1 parent f653d4b commit 5acef13

5 files changed

Lines changed: 33 additions & 28 deletions

File tree

src/main/kotlin/be/ugent/topl/mio/debugger/Debugger.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,18 @@ open class Debugger(private val connection: Connection, start: Boolean = true, p
515515
class AtEveryInstruction() : SnapshotPolicy(1) {
516516
override fun toString() = "Snapshot at every instruction"
517517
}
518-
data class Checkpointing(val interval: Int = 20) : SnapshotPolicy(2) {
518+
data class Checkpointing(val interval: UInt = 20U) : SnapshotPolicy(2) {
519519
override fun serialize(): String {
520520
return super.serialize() + HexaEncoder.serializeUInt32BE(interval)
521521
}
522522
}
523-
data class Tracing(val states: List<ExecutionState>, val minimumArgCount: Int = 1) : SnapshotPolicy(3) {
523+
data class Tracing(val states: List<ExecutionState>, val minimumArgCount: Int = 1, val interval: UInt = 0xffffffffU) : SnapshotPolicy(3) {
524524
override fun serialize(): String {
525525
return super.serialize() +
526526
HexaEncoder.convertToLEB128(minimumArgCount) +
527527
HexaEncoder.convertToLEB128(states.size) +
528-
states.joinToString("") { HexaEncoder.convertToLEB128(it.ordinal + 1) }
528+
states.joinToString("") { HexaEncoder.convertToLEB128(it.ordinal + 1) } +
529+
HexaEncoder.serializeUInt32BE(interval)
529530
}
530531
}
531532
}

src/main/kotlin/be/ugent/topl/mio/woodstate/HexaEncoder.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@ import kotlin.streams.toList
55
class HexaEncoder {
66
companion object {
77
fun serializeBool(b: Boolean): String {
8-
return serializeUInt(if (b) 1 else 0, 1, true)
8+
return serializeUInt(if (b) 1U else 0U, 1, true)
99
}
1010

1111
fun serializeUInt8(n: Int): String {
12-
return serializeUInt(n, 1, true)
12+
return serializeUInt(n.toUInt(), 1, true)
1313
}
1414

1515
fun serializeUInt16BE(n: Int): String {
16-
return serializeUInt(n, 2, true)
16+
return serializeUInt(n.toUInt(), 2, true)
1717
}
1818

1919
fun serializeUInt32BE(n: Int): String {
20+
return serializeUInt(n.toUInt(), 4, true)
21+
}
22+
23+
fun serializeUInt32BE(n: UInt): String {
2024
return serializeUInt(n, 4, true)
2125
}
2226

2327
fun serializeInt32LE(n: Int): String {
24-
return serializeUInt(n, 4, false)
28+
return serializeUInt(n.toUInt(), 4, false)
2529
}
2630

2731
fun serializeUInt32LE(n: Int): String {
28-
return serializeUInt(n, 4, false)
32+
return serializeUInt(n.toUInt(), 4, false)
2933
}
3034

3135
fun serializeBigUInt64LE(n: Long): String {
@@ -41,15 +45,15 @@ class HexaEncoder {
4145
return if (bigendian) str else hexStringBEToLE(str)
4246
}
4347

44-
fun serializeUInt(n: Int, amountBytes: Int, bigendian: Boolean): String {
48+
fun serializeUInt(n: UInt, amountBytes: Int, bigendian: Boolean): String {
4549
if (amountBytes < 1 || amountBytes > 4) {
4650
throw Error("invalid amount of bytes")
4751
}
4852
// We have 4 bytes, our number is limited to amountBytes so we throw away the other bytes.
4953
val shift = (4 - amountBytes) * 8
50-
val nLimited = ((n shl shift) ushr shift)
54+
val nLimited = ((n shl shift) shr shift)
5155

52-
val str = String.format("%0${amountBytes*2}x", nLimited)
56+
val str = String.format("%0${amountBytes*2}x", nLimited.toLong())
5357
if (!bigendian) {
5458
return str.chunked(2).reversed().joinToString(separator = "")
5559
}

src/test/kotlin/DebuggerTests.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class DebuggerTests : DebuggerTestBase() {
5959
val results = mutableListOf<Triple<Int, Double, Debugger.SnapshotPolicy>>()
6060
for (policy in listOf(
6161
// Debugger.SnapshotPolicy.None(),
62-
Debugger.SnapshotPolicy.Checkpointing(1),
62+
Debugger.SnapshotPolicy.Checkpointing(1U),
6363
/*Debugger.SnapshotPolicy.Checkpointing(5),
6464
Debugger.SnapshotPolicy.Checkpointing(10),
6565
Debugger.SnapshotPolicy.Checkpointing(50),
@@ -96,7 +96,7 @@ class DebuggerTests : DebuggerTestBase() {
9696
//val connection = ProcessConnection(wdcliPath, getFile("blink.wasm").path, "--no-socket")
9797
val connection = ProcessConnection(wdcliPath, getFile("/home/maarten/Documents/Projects/maarten-thesis-23-24/wardbg/src/test/resources/prime/prime-no-mem.wasm").path, "--no-socket")
9898
val debugger = Debugger(connection)
99-
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(100))
99+
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(100U))
100100
debugger.pause()
101101
val count = debugger.checkpoints.size
102102
debugger.continueFor(1000)
@@ -108,8 +108,8 @@ class DebuggerTests : DebuggerTestBase() {
108108
fun `Test snapshot policy serialization`() {
109109
assertEquals("00", Debugger.SnapshotPolicy.None().serialize())
110110
assertEquals("01", Debugger.SnapshotPolicy.AtEveryInstruction().serialize())
111-
assertEquals("0205", Debugger.SnapshotPolicy.Checkpointing(5).serialize())
112-
assertEquals("02ff", Debugger.SnapshotPolicy.Checkpointing(255).serialize())
111+
assertEquals("0205", Debugger.SnapshotPolicy.Checkpointing(5U).serialize())
112+
assertEquals("02ff", Debugger.SnapshotPolicy.Checkpointing(255U).serialize())
113113
}
114114

115115
@Test
@@ -139,7 +139,7 @@ class DebuggerTests : DebuggerTestBase() {
139139
xSorted.sort()
140140
for (n in xSorted) {
141141
runWithDebugger(wasmFile, emulator = false) { debugger ->
142-
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(checkpointInterval))
142+
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(checkpointInterval.toUInt()))
143143
var time = 0L
144144
//val repeatCount = 50
145145
//val repeatCount = 5
@@ -184,10 +184,10 @@ class DebuggerTests : DebuggerTestBase() {
184184
fun `Test stepBack performance, min max average time`(stepSize: Int) {*/
185185
fun `Test stepBack performance`() {
186186
val fileWriter = File("results.txt").bufferedWriter()
187-
val results = mutableListOf<Pair<Triple<Int, Int, Int>, Triple<Long, Long, Double>>>()
187+
val results = mutableListOf<Pair<Triple<Int, Int, UInt>, Triple<Long, Long, Double>>>()
188188
//for (checkpointInterval in listOf(1, 2, 5, 10)) {
189189
//for (checkpointInterval in listOf(1, 5, 10, 50, 100)) {
190-
for (checkpointInterval in listOf(5, 10, 50, 100)) {
190+
for (checkpointInterval in listOf(5U, 10U, 50U, 100U)) {
191191
for (stepSize in listOf(1, 3, 5, 8, 10, 15)) {
192192
//for (stepSize in listOf(5, 8, 10, 15)) {
193193
val n = 200
@@ -250,7 +250,7 @@ class DebuggerTests : DebuggerTestBase() {
250250
@Test
251251
fun `x` () {
252252
runWithDebugger("prime/prime.wasm", false) {
253-
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(10))
253+
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(10U))
254254
it.continueFor(50)
255255
}
256256
}
@@ -265,7 +265,7 @@ class DebuggerTests : DebuggerTestBase() {
265265
runWithDebugger(wasmFile, false) {
266266
var t = 0
267267
val timings = mutableListOf<Pair<Int, Long>>()
268-
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(0xffffff))
268+
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(0xffffffU))
269269
it.stepInto()
270270
it.checkpoints[it.checkpoints.size - 1] = null
271271
timings.add(Pair(t, timeElapsed {
@@ -312,7 +312,7 @@ class DebuggerTests : DebuggerTestBase() {
312312
val connection = ProcessConnection(wdcliPath, getFile(wasmFile).path, "--no-socket")
313313
val binaryInfo = getBinaryInfo(config.symbolicWdcliPath, getFile(wasmFile).absolutePath)
314314
val debugger = MultiverseDebugger(connection, WasmBinary(File(wasmFile), binaryInfo.getOrThrow()), config.symbolicWdcliPath)
315-
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(10))
315+
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(10U))
316316
debugger.pause()
317317
//debugger.continueFor(5)
318318
debugger.step(5)

src/test/kotlin/benchmarks/Benchmarks.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class Benchmarks : DebuggerTestBase() {
2727
val results = mutableListOf<Triple<Int, Double, Debugger.SnapshotPolicy>>()
2828
for (policy in listOf(
2929
Debugger.SnapshotPolicy.None(),
30-
Debugger.SnapshotPolicy.Checkpointing(1),
31-
Debugger.SnapshotPolicy.Checkpointing(5),
32-
Debugger.SnapshotPolicy.Checkpointing(10),
33-
Debugger.SnapshotPolicy.Checkpointing(50),
34-
Debugger.SnapshotPolicy.Checkpointing(100))) {
30+
Debugger.SnapshotPolicy.Checkpointing(1U),
31+
Debugger.SnapshotPolicy.Checkpointing(5U),
32+
Debugger.SnapshotPolicy.Checkpointing(10U),
33+
Debugger.SnapshotPolicy.Checkpointing(50U),
34+
Debugger.SnapshotPolicy.Checkpointing(100U))) {
3535

3636
for (n in 250 ..< 1500 step 250) {
3737
println("Progress $n/1500")
@@ -71,7 +71,7 @@ class Benchmarks : DebuggerTestBase() {
7171
runWithDebugger(wasmFile, useEmulator()) {
7272
var t = 0
7373
val timings = mutableListOf<Pair<Int, Long>>()
74-
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(0xffffff))
74+
it.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing(0xffffffU))
7575
it.stepInto()
7676
it.checkpoints[it.checkpoints.size - 1] = null
7777
timings.add(Pair(t, timeElapsed {

src/test/kotlin/benchmarks/TracePerformance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TracePerformance : DebuggerTestBase() {
1414
for (policy in listOf(
1515
Debugger.SnapshotPolicy.None(),
1616
Debugger.SnapshotPolicy.Tracing(listOf(ExecutionState.ProgramCounter)),
17-
Debugger.SnapshotPolicy.Checkpointing(0xffff)
17+
Debugger.SnapshotPolicy.Checkpointing(0xffffU)
1818
)) {
1919
println("Policy $policy")
2020
val pairs = mutableListOf<Pair<Long, Long>>()

0 commit comments

Comments
 (0)