Cache debug information from bytecode in Android debug
This commit is contained in:
@@ -38,10 +38,6 @@ import org.jetbrains.kotlin.utils.getOrPutNullable
|
|||||||
import org.jetbrains.org.objectweb.asm.*
|
import org.jetbrains.org.objectweb.asm.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
// TODO: Don't read same bytecode file again and again
|
|
||||||
// TODO: Build line mapping for the whole file
|
|
||||||
// TODO: Quick caching for the same location
|
|
||||||
|
|
||||||
fun noStrataLineNumber(location: Location, isDexDebug: Boolean, project: Project, preferInlined: Boolean = false): Int {
|
fun noStrataLineNumber(location: Location, isDexDebug: Boolean, project: Project, preferInlined: Boolean = false): Int {
|
||||||
if (isDexDebug) {
|
if (isDexDebug) {
|
||||||
if (!preferInlined) {
|
if (!preferInlined) {
|
||||||
@@ -68,33 +64,40 @@ fun getLastLineNumberForLocation(location: Location, project: Project, searchSco
|
|||||||
val fileName = location.sourceName()
|
val fileName = location.sourceName()
|
||||||
|
|
||||||
val method = location.method() ?: return null
|
val method = location.method() ?: return null
|
||||||
|
val name = method.name() ?: return null
|
||||||
|
val signature = method.signature() ?: return null
|
||||||
|
|
||||||
val bytes = findAndReadClassFile(fqName, fileName, project, searchScope, { isInlineFunctionLineNumber(it, lineNumber, project) }) ?: return null
|
val debugInfo = findAndReadClassFile(fqName, fileName, project, searchScope, { isInlineFunctionLineNumber(it, lineNumber, project) }) ?: return null
|
||||||
|
|
||||||
fun readLineNumberTableMapping(bytes: ByteArray): Map<String, Set<Int>> {
|
val lineMapping = debugInfo.lineTableMapping[BytecodeMethodKey(name, signature)] ?: return null
|
||||||
val labelsToAllStrings = HashMap<String, MutableSet<Int>>()
|
return lineMapping.values.firstOrNull { it.contains(lineNumber) }?.last()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun readLineNumberTableMapping(bytes: ByteArray): Map<BytecodeMethodKey, Map<String, Set<Int>>> {
|
||||||
|
val lineNumberMapping = HashMap<BytecodeMethodKey, Map<String, Set<Int>>>()
|
||||||
|
|
||||||
ClassReader(bytes).accept(object : ClassVisitor(InlineCodegenUtil.API) {
|
ClassReader(bytes).accept(object : ClassVisitor(InlineCodegenUtil.API) {
|
||||||
override fun visitMethod(access: Int, name: String?, desc: String?, signature: String?, exceptions: Array<out String>?): MethodVisitor? {
|
override fun visitMethod(access: Int, name: String?, desc: String?, signature: String?, exceptions: Array<out String>?): MethodVisitor? {
|
||||||
if (!(name == method.name() && desc == method.signature())) {
|
if (name == null || desc == null) {
|
||||||
|
// TODO: check constructors
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val methodKey = BytecodeMethodKey(name, desc)
|
||||||
|
val methodLinesMapping = HashMap<String, MutableSet<Int>>()
|
||||||
|
lineNumberMapping[methodKey] = methodLinesMapping
|
||||||
|
|
||||||
return object : MethodVisitor(Opcodes.ASM5, null) {
|
return object : MethodVisitor(Opcodes.ASM5, null) {
|
||||||
override fun visitLineNumber(line: Int, start: Label?) {
|
override fun visitLineNumber(line: Int, start: Label?) {
|
||||||
if (start != null) {
|
if (start != null) {
|
||||||
labelsToAllStrings.getOrPutNullable(start.toString(), { LinkedHashSet<Int>() }).add(line)
|
methodLinesMapping.getOrPutNullable(start.toString(), { LinkedHashSet<Int>() }).add(line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, ClassReader.SKIP_FRAMES and ClassReader.SKIP_CODE)
|
}, ClassReader.SKIP_FRAMES and ClassReader.SKIP_CODE)
|
||||||
|
|
||||||
return labelsToAllStrings
|
return lineNumberMapping
|
||||||
}
|
|
||||||
|
|
||||||
val lineMapping = readLineNumberTableMapping(bytes)
|
|
||||||
return lineMapping.values.firstOrNull { it.contains(lineNumber) }?.last()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun getOriginalPositionOfInlinedLine(location: Location, project: Project): Pair<KtFile, Int>? {
|
internal fun getOriginalPositionOfInlinedLine(location: Location, project: Project): Pair<KtFile, Int>? {
|
||||||
@@ -103,14 +106,16 @@ internal fun getOriginalPositionOfInlinedLine(location: Location, project: Proje
|
|||||||
val fileName = location.sourceName()
|
val fileName = location.sourceName()
|
||||||
val searchScope = GlobalSearchScope.allScope(project)
|
val searchScope = GlobalSearchScope.allScope(project)
|
||||||
|
|
||||||
val bytes = findAndReadClassFile(fqName, fileName, project, searchScope, { isInlineFunctionLineNumber(it, lineNumber, project) }) ?: return null
|
val debugInfo = findAndReadClassFile(fqName, fileName, project, searchScope, { isInlineFunctionLineNumber(it, lineNumber, project) }) ?:
|
||||||
val smapData = readDebugInfo(bytes) ?: return null
|
return null
|
||||||
|
val smapData = debugInfo.smapData ?: return null
|
||||||
|
|
||||||
return mapStacktraceLineToSource(smapData, lineNumber, project, SourceLineKind.EXECUTED_LINE, searchScope)
|
return mapStacktraceLineToSource(smapData, lineNumber, project, SourceLineKind.EXECUTED_LINE, searchScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun findAndReadClassFile(
|
internal fun findAndReadClassFile(
|
||||||
fqName: FqName, fileName: String, project: Project, searchScope: GlobalSearchScope,
|
fqName: FqName, fileName: String, project: Project, searchScope: GlobalSearchScope,
|
||||||
fileFilter: (VirtualFile) -> Boolean): ByteArray? {
|
fileFilter: (VirtualFile) -> Boolean): BytecodeDebugInfo? {
|
||||||
val internalName = fqName.asString().replace('.', '/')
|
val internalName = fqName.asString().replace('.', '/')
|
||||||
val jvmClassName = JvmClassName.byInternalName(internalName)
|
val jvmClassName = JvmClassName.byInternalName(internalName)
|
||||||
|
|
||||||
@@ -151,8 +156,8 @@ private fun inlinedLinesNumbers(
|
|||||||
|
|
||||||
val virtualFile = file.virtualFile ?: return listOf()
|
val virtualFile = file.virtualFile ?: return listOf()
|
||||||
|
|
||||||
val bytes = readClassFile(project, jvmClassName, virtualFile) ?: return listOf()
|
val debugInfo = readClassFile(project, jvmClassName, virtualFile) ?: return listOf()
|
||||||
val smapData = readDebugInfo(bytes) ?: return listOf()
|
val smapData = debugInfo.smapData ?: return listOf()
|
||||||
|
|
||||||
val smap = smapData.kotlinStrata ?: return listOf()
|
val smap = smapData.kotlinStrata ?: return listOf()
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.intellij.debugger.engine.evaluation.EvaluationContextImpl
|
|||||||
import com.intellij.openapi.components.ServiceManager
|
import com.intellij.openapi.components.ServiceManager
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.roots.libraries.LibraryUtil
|
import com.intellij.openapi.roots.libraries.LibraryUtil
|
||||||
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.util.CachedValueProvider
|
import com.intellij.psi.util.CachedValueProvider
|
||||||
import com.intellij.psi.util.CachedValuesManager
|
import com.intellij.psi.util.CachedValuesManager
|
||||||
@@ -38,11 +39,15 @@ import org.jetbrains.kotlin.config.CompilerConfiguration
|
|||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeAndGetResult
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeAndGetResult
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||||
|
import org.jetbrains.kotlin.idea.debugger.BinaryCacheKey
|
||||||
|
import org.jetbrains.kotlin.idea.debugger.BytecodeDebugInfo
|
||||||
|
import org.jetbrains.kotlin.idea.debugger.WeakConcurrentBinaryStorage
|
||||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
import org.jetbrains.kotlin.psi.KtCodeFragment
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
|
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
@@ -69,6 +74,13 @@ class KotlinDebuggerCaches(project: Project) {
|
|||||||
PsiModificationTracker.MODIFICATION_COUNT)
|
PsiModificationTracker.MODIFICATION_COUNT)
|
||||||
}, false)
|
}, false)
|
||||||
|
|
||||||
|
private val binaryCache = CachedValuesManager.getManager(project).createCachedValue(
|
||||||
|
{
|
||||||
|
CachedValueProvider.Result<WeakConcurrentBinaryStorage>(
|
||||||
|
WeakConcurrentBinaryStorage(),
|
||||||
|
PsiModificationTracker.MODIFICATION_COUNT)
|
||||||
|
}, false)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val LOG = Logger.getLogger(KotlinDebuggerCaches::class.java)!!
|
private val LOG = Logger.getLogger(KotlinDebuggerCaches::class.java)!!
|
||||||
|
|
||||||
@@ -154,6 +166,14 @@ class KotlinDebuggerCaches(project: Project) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun readFileContent(
|
||||||
|
project: Project,
|
||||||
|
jvmName: JvmClassName,
|
||||||
|
file: VirtualFile): BytecodeDebugInfo? {
|
||||||
|
val cache = getInstance(project)
|
||||||
|
return cache.binaryCache.value[BinaryCacheKey(project, jvmName, file)]
|
||||||
|
}
|
||||||
|
|
||||||
private fun getElementToCreateTypeMapperForLibraryFile(element: PsiElement?) =
|
private fun getElementToCreateTypeMapperForLibraryFile(element: PsiElement?) =
|
||||||
runReadAction { element as? KtElement ?: PsiTreeUtil.getParentOfType(element, KtElement::class.java)!! }
|
runReadAction { element as? KtElement ?: PsiTreeUtil.getParentOfType(element, KtElement::class.java)!! }
|
||||||
|
|
||||||
|
|||||||
@@ -22,10 +22,13 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.openapi.roots.ProjectFileIndex
|
import com.intellij.openapi.roots.ProjectFileIndex
|
||||||
import com.intellij.openapi.vfs.VirtualFile
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import com.intellij.util.containers.ConcurrentWeakFactoryMap
|
||||||
|
import com.intellij.util.containers.ContainerUtil
|
||||||
import org.jetbrains.kotlin.codegen.inline.FileMapping
|
import org.jetbrains.kotlin.codegen.inline.FileMapping
|
||||||
import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil
|
import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil
|
||||||
import org.jetbrains.kotlin.codegen.inline.SMAP
|
import org.jetbrains.kotlin.codegen.inline.SMAP
|
||||||
import org.jetbrains.kotlin.codegen.inline.SMAPParser
|
import org.jetbrains.kotlin.codegen.inline.SMAPParser
|
||||||
|
import org.jetbrains.kotlin.idea.debugger.evaluate.KotlinDebuggerCaches
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineCount
|
import org.jetbrains.kotlin.idea.refactoring.getLineCount
|
||||||
import org.jetbrains.kotlin.idea.refactoring.toPsiFile
|
import org.jetbrains.kotlin.idea.refactoring.toPsiFile
|
||||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||||
@@ -49,7 +52,33 @@ fun isInlineFunctionLineNumber(file: VirtualFile, lineNumber: Int, project: Proj
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WeakConcurrentBinaryStorage : ConcurrentWeakFactoryMap<BinaryCacheKey, BytecodeDebugInfo?>() {
|
||||||
|
override fun create(key: BinaryCacheKey): BytecodeDebugInfo? {
|
||||||
|
val bytes = readClassFileImpl(key.project, key.jvmName, key.file) ?: return null
|
||||||
|
|
||||||
|
val smapData = readDebugInfo(bytes)
|
||||||
|
val lineNumberMapping = readLineNumberTableMapping(bytes)
|
||||||
|
|
||||||
|
return BytecodeDebugInfo(smapData, lineNumberMapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun createMap(): Map<BinaryCacheKey, BytecodeDebugInfo?> {
|
||||||
|
return ContainerUtil.createConcurrentWeakKeyWeakValueMap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun readClassFile(project: Project,
|
fun readClassFile(project: Project,
|
||||||
|
jvmName: JvmClassName,
|
||||||
|
file: VirtualFile): BytecodeDebugInfo? {
|
||||||
|
return KotlinDebuggerCaches.readFileContent(project, jvmName, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
class BytecodeDebugInfo(val smapData: SmapData?, val lineTableMapping: Map<BytecodeMethodKey, Map<String, Set<Int>>>)
|
||||||
|
|
||||||
|
data class BytecodeMethodKey(val methodName: String, val signature: String)
|
||||||
|
data class BinaryCacheKey(val project: Project, val jvmName: JvmClassName, val file: VirtualFile)
|
||||||
|
|
||||||
|
private fun readClassFileImpl(project: Project,
|
||||||
jvmName: JvmClassName,
|
jvmName: JvmClassName,
|
||||||
file: VirtualFile): ByteArray? {
|
file: VirtualFile): ByteArray? {
|
||||||
val fqNameWithInners = jvmName.fqNameForClassNameWithoutDollars.tail(jvmName.packageFqName)
|
val fqNameWithInners = jvmName.fqNameForClassNameWithoutDollars.tail(jvmName.packageFqName)
|
||||||
@@ -84,6 +113,7 @@ fun readClassFile(project: Project,
|
|||||||
classByDirectory = findClassFileByPath(jvmName.packageFqName.asString(), className, androidTestOutputDir) ?: return null
|
classByDirectory = findClassFileByPath(jvmName.packageFqName.asString(), className, androidTestOutputDir) ?: return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println("Read file: " + classByDirectory)
|
||||||
return classByDirectory.readBytes()
|
return classByDirectory.readBytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,8 +75,8 @@ class KotlinExceptionFilter(private val searchScope: GlobalSearchScope) : Filter
|
|||||||
private fun createHyperlinks(jvmName: JvmClassName, file: VirtualFile, line: Int, project: Project): InlineFunctionHyperLinkInfo? {
|
private fun createHyperlinks(jvmName: JvmClassName, file: VirtualFile, line: Int, project: Project): InlineFunctionHyperLinkInfo? {
|
||||||
if (!isInlineFunctionLineNumber(file, line, project)) return null
|
if (!isInlineFunctionLineNumber(file, line, project)) return null
|
||||||
|
|
||||||
val bytes = readClassFile(project, jvmName, file) ?: return null
|
val debugInfo = readClassFile(project, jvmName, file) ?: return null
|
||||||
val smapData = readDebugInfo(bytes) ?: return null
|
val smapData = debugInfo.smapData ?: return null
|
||||||
|
|
||||||
val inlineInfos = arrayListOf<InlineFunctionHyperLinkInfo.InlineInfo>()
|
val inlineInfos = arrayListOf<InlineFunctionHyperLinkInfo.InlineInfo>()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user