Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix bug
  • Loading branch information
egiptipavel committed Aug 9, 2023
commit e186e1708447095faa19098d6097895f92aa2bf4
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,27 @@ fun createClassVisitorForComputeMapOfRangesForBranchCoverage(
return MethodProbesCollector(strategy, writer)
}

class ProbeIdGenerator(private val f: (Int) -> Long) {

private var localId: Int = 0

fun currentId(): Long = f(localId)

fun nextId(): Long = f(localId++)

}

fun createClassVisitorForTracingBranchInstructions(
className: String,
storage: ProcessingStorage,
writer: ClassWriter
): ClassProbesAdapter {
val strategy = TraceStrategy()
val probeIdGenerator = ProbeIdGenerator { localId ->
storage.computeId(className, localId)
}
return ClassProbesAdapter(
TraceClassInstrumenter(strategy, writer, storage) { localId ->
storage.computeId(className, localId)
},
TraceClassInstrumenter(strategy, writer, storage, probeIdGenerator),
false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TraceClassInstrumenter(
private val probeArrayStrategy: IProbeArrayStrategy,
cv: ClassVisitor,
private val storage: ProcessingStorage,
private val nextIdGenerator: (localId: Int) -> Long
private val probeIdGenerator: ProbeIdGenerator
) : ClassInstrumenter(probeArrayStrategy, cv) {

override fun visitMethod(
Expand All @@ -26,10 +26,10 @@ class TraceClassInstrumenter(

val frameEliminator = DuplicateFrameEliminator(mv)
val probeVariableInserter = TraceProbeInserter(
access, name, desc, frameEliminator, probeArrayStrategy, nextIdGenerator
access, name, desc, frameEliminator, probeArrayStrategy, probeIdGenerator
)
return TraceMethodInstrumenter(
name, desc, probeVariableInserter, probeVariableInserter, storage, nextIdGenerator
name, desc, probeVariableInserter, probeVariableInserter, storage, probeIdGenerator
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,51 @@ internal class TraceMethodInstrumenter(
mv: MethodVisitor,
private val probeInserter: IProbeInserter,
private val storage: ProcessingStorage,
private val nextIdGenerator: (localId: Int) -> Long
private val probeIdGenerator: ProbeIdGenerator
) : MethodInstrumenter(mv, probeInserter) {

private var currentLineNumber: Int = 0
private val currentMethodSignature: String = methodName + descriptor

// === MethodVisitor ===

override fun visitCode() {
storeInstruction(CommonInstruction(currentLineNumber, currentMethodSignature))
super.visitCode()
}

override fun visitLineNumber(line: Int, start: Label?) {
currentLineNumber = line
super.visitLineNumber(line, start)
}

// === MethodInstrumenter ===

private fun storeInstruction(localId: Int, instructionData: InstructionData) {
val id = nextIdGenerator.invoke(localId)
private fun storeInstruction(instructionData: InstructionData) {
val id = probeIdGenerator.currentId()
storage.addInstruction(id, instructionData)
}

override fun visitProbe(localId: Int) {
storeInstruction(localId, CommonInstruction(currentLineNumber, currentMethodSignature))
storeInstruction(CommonInstruction(currentLineNumber, currentMethodSignature))
super.visitProbe(localId)
}

override fun visitInsnWithProbe(opcode: Int, localId: Int) {
when (opcode) {
in returnInsns -> {
storeInstruction(localId, ReturnInstruction(currentLineNumber, currentMethodSignature))
storeInstruction(ReturnInstruction(currentLineNumber, currentMethodSignature))
}

Opcodes.ATHROW -> {
storeInstruction(localId, ExplicitThrowInstruction(currentLineNumber, currentMethodSignature))
storeInstruction(ExplicitThrowInstruction(currentLineNumber, currentMethodSignature))
}
}
super.visitInsnWithProbe(opcode, localId)
}

override fun visitJumpInsnWithProbe(opcode: Int, label: Label?, localId: Int, frame: IFrame?) {
storeInstruction(localId, CommonInstruction(currentLineNumber, currentMethodSignature))
storeInstruction(CommonInstruction(currentLineNumber, currentMethodSignature))
super.visitJumpInsnWithProbe(opcode, label, localId, frame)
}

Expand Down Expand Up @@ -119,7 +124,7 @@ internal class TraceMethodInstrumenter(
if (localId != LabelInfo.NO_PROBE && !LabelInfo.isDone(label)) {
mv.visitLabel(LabelInfo.getIntermediateLabel(label))
frame.accept(mv)
storeInstruction(localId, CommonInstruction(currentLineNumber, currentMethodSignature))
storeInstruction(CommonInstruction(currentLineNumber, currentMethodSignature))
probeInserter.insertProbe(localId)
mv.visitJumpInsn(Opcodes.GOTO, label)
LabelInfo.setDone(label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class TraceProbeInserter(
desc: String,
mv: MethodVisitor,
arrayStrategy: IProbeArrayStrategy,
private val nextIdGenerator: (localId: Int) -> Long,
private val probeIdGenerator: ProbeIdGenerator,
) : ProbeInserter(access, name, desc, mv, arrayStrategy) {

private val internalName = Type.getInternalName(RuntimeTraceStorage::class.java)
Expand All @@ -27,11 +27,16 @@ internal class TraceProbeInserter(
variable = pos
}

override fun insertProbe(localId: Int) {
override fun insertProbe(ignored: Int) {
mv.visitVarInsn(Opcodes.ILOAD, variable)
val id = nextIdGenerator.invoke(localId)
val id = probeIdGenerator.nextId()
mv.visitLdcInsn(id)
mv.visitMethodInsn(Opcodes.INVOKESTATIC, internalName, "visit", visitMethodDescriptor, false)
}

override fun visitCode() {
super.visitCode()
insertProbe(0)
}

}