Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1519
- Loading branch information
Showing
15 changed files
with
215 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
shark-android/src/main/java/shark/AndroidMetadataExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package shark | ||
|
||
object AndroidMetadataExtractor : MetadataExtractor { | ||
override fun extractMetadata(graph: HeapGraph): Map<String, String> { | ||
val build = AndroidBuildMirror.fromHeapGraph(graph) | ||
|
||
val leakCanaryVersion = readLeakCanaryVersion(graph) | ||
val processName = readProcessName(graph) | ||
|
||
return mapOf( | ||
"Build.VERSION.SDK_INT" to build.sdkInt.toString(), | ||
"Build.MANUFACTURER" to build.manufacturer, | ||
"LeakCanary version" to leakCanaryVersion, | ||
"App process name" to processName | ||
) | ||
} | ||
|
||
private fun readLeakCanaryVersion(graph: HeapGraph): String { | ||
val versionHolderClass = graph.findClassByName("leakcanary.internal.InternalLeakCanary") | ||
return versionHolderClass?.get("version")?.value?.readAsJavaString() ?: "Unknown" | ||
} | ||
|
||
private fun readProcessName(graph: HeapGraph): String { | ||
val activityThread = graph.findClassByName("android.app.ActivityThread") | ||
?.get("sCurrentActivityThread") | ||
?.valueAsInstance | ||
val appBindData = activityThread?.get("android.app.ActivityThread", "mBoundApplication") | ||
?.valueAsInstance | ||
val appInfo = appBindData?.get("android.app.ActivityThread\$AppBindData", "appInfo") | ||
?.valueAsInstance | ||
|
||
return appInfo?.get( | ||
"android.content.pm.ApplicationInfo", "processName" | ||
)?.valueAsInstance?.readAsJavaString() ?: "Unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package shark | ||
|
||
import shark.MetadataExtractor.Companion.invoke | ||
import shark.ObjectInspector.Companion.invoke | ||
|
||
/** | ||
* Extracts metadata from a hprof to be reported in [HeapAnalysisSuccess.metadata]. | ||
* | ||
* You can create a [MetadataExtractor] from a lambda by calling [invoke]. | ||
*/ | ||
interface MetadataExtractor { | ||
fun extractMetadata(graph: HeapGraph): Map<String, String> | ||
|
||
companion object { | ||
|
||
/** | ||
* A no-op [MetadataExtractor] | ||
*/ | ||
val NO_OP = MetadataExtractor { emptyMap() } | ||
|
||
/** | ||
* Utility function to create a [MetadataExtractor] from the passed in [block] lambda instead of | ||
* using the anonymous `object : MetadataExtractor` syntax. | ||
* | ||
* Usage: | ||
* | ||
* ```kotlin | ||
* val inspector = MetadataExtractor { graph -> | ||
* | ||
* } | ||
* ``` | ||
*/ | ||
inline operator fun invoke(crossinline block: (HeapGraph) -> Map<String, String>): MetadataExtractor = | ||
object : MetadataExtractor { | ||
override fun extractMetadata(graph: HeapGraph): Map<String, String> = block(graph) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package shark | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
|
||
class MetadataExtractorTest { | ||
|
||
@get:Rule | ||
var testFolder = TemporaryFolder() | ||
private lateinit var hprofFile: File | ||
|
||
@Before | ||
fun setUp() { | ||
hprofFile = testFolder.newFile("temp.hprof") | ||
} | ||
|
||
@Test fun extractStaticStringField() { | ||
HprofWriter.open(hprofFile) | ||
.helper { | ||
val helloString = string("Hello") | ||
clazz( | ||
"World", staticFields = listOf( | ||
"message" to helloString | ||
) | ||
) | ||
} | ||
|
||
val extractor = object : MetadataExtractor { | ||
override fun extractMetadata(graph: HeapGraph): Map<String, String> { | ||
val message = | ||
graph.findClassByName("World")!!["message"]!!.valueAsInstance!!.readAsJavaString()!! | ||
return mapOf("World message" to message) | ||
} | ||
} | ||
|
||
val analysis = hprofFile.checkForLeaks<HeapAnalysisSuccess>(metadataExtractor = extractor) | ||
|
||
val metadata = analysis.metadata | ||
|
||
assertThat(metadata).isEqualTo(mapOf("World message" to "Hello")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters