Skip to content

Commit d93cac4

Browse files
committed
Clean the stack traces from BlockHound artifacts
1 parent 838e599 commit d93cac4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

kotlinx-coroutines-debug/test/StracktraceUtils.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null, f
6262
}
6363
}
6464

65+
/** Clean the stacktraces from artifacts of BlockHound instrumentation
66+
*
67+
* BlockHound works by switching a native call by a class generated with ByteBuddy, which, if the blocking
68+
* call is allowed in this context, in turn calls the real native call that is now available under a
69+
* different name.
70+
*
71+
* The traces thus undergo the following two changes when the execution is instrumented:
72+
* - The original native call is replaced with a non-native one with the same FQN, and
73+
* - An additional native call is placed on top of the stack, with the original name that also has
74+
* `$$BlockHound$$_` prepended at the last component.
75+
*/
76+
private fun cleanBlockHoundTraces(frames: List<String>): List<String> {
77+
var result = mutableListOf<String>()
78+
val blockHoundSubstr = "\$\$BlockHound\$\$_"
79+
var i = 0
80+
while (i < frames.size) {
81+
result.add(frames[i].replace(blockHoundSubstr, ""))
82+
if (frames[i].contains(blockHoundSubstr)) {
83+
i += 1
84+
}
85+
i += 1
86+
}
87+
return result
88+
}
89+
6590
public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) {
6691
val baos = ByteArrayOutputStream()
6792
DebugProbes.dumpCoroutines(PrintStream(baos))
@@ -85,7 +110,7 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) {
85110
expected.withIndex().forEach { (index, trace) ->
86111
val actualTrace = actual[index].trimStackTrace().sanitizeAddresses()
87112
val expectedTrace = trace.trimStackTrace().sanitizeAddresses()
88-
val actualLines = actualTrace.split("\n")
113+
val actualLines = cleanBlockHoundTraces(actualTrace.split("\n"))
89114
val expectedLines = expectedTrace.split("\n")
90115
for (i in expectedLines.indices) {
91116
assertEquals(expectedLines[i], actualLines[i])

0 commit comments

Comments
 (0)