/
Main.kt
262 lines (223 loc) · 7.84 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package shark
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit.SECONDS
import kotlin.system.exitProcess
fun main(args: Array<String>) {
SharkLog.logger = CLILogger()
if (args.isEmpty()) {
printHelp()
return
}
var argIndex = -1
when (val command = args[++argIndex]) {
"dump-process" -> {
val packageName = args[++argIndex]
argIndex++
val remainderArgs = args.drop(argIndex)
val deviceId = readDeviceIdFromArgs(remainderArgs)
dumpHeap(packageName, deviceId)
}
"analyze-process" -> {
val packageName = args[++argIndex]
argIndex++
val remainderArgs = args.drop(argIndex)
val deviceId = readDeviceIdFromArgs(remainderArgs)
val heapDumpFile = dumpHeap(packageName, deviceId)
val mappingFile = readMappingFileFromArgs(remainderArgs)
analyze(heapDumpFile, mappingFile)
}
"analyze-hprof" -> {
val hprofPath = args[++argIndex]
argIndex++
val remainderArgs = args.asList()
.subList(argIndex, args.size)
val mappingFile = readMappingFileFromArgs(remainderArgs)
analyze(File(hprofPath), mappingFile)
}
"strip-hprof" -> {
val hprofPath = args[++argIndex]
stripHprof(File(hprofPath))
}
else -> {
SharkLog.d {
"Error: unknown command [$command]"
}
printHelp()
}
}
}
private fun readMappingFileFromArgs(args: List<String>): File? {
val tagIndex = args.indexOfFirst {
it == "-p" || it == "--proguard-mapping"
}
if (tagIndex == -1 || tagIndex == args.lastIndex) {
return null
}
return File(args[tagIndex + 1])
}
private fun readDeviceIdFromArgs(args: List<String>): String? {
val tagIndex = args.indexOfFirst {
it == "-d" || it == "--device"
}
if (tagIndex == -1 || tagIndex == args.lastIndex) {
return null
}
return args[tagIndex + 1]
}
fun printHelp() {
val workingDirectory = File(System.getProperty("user.dir"))
// ASCII art is a remix of a shark from -David "TAZ" Baltazar- and chick from jgs.
SharkLog.d {
"""
Shark CLI, running in directory $workingDirectory
^`. .=""=.
^_ \ \ / _ _ \
\ \ { \ | d b |
{ \ / `~~~--__ \ /\ /
{ \___----~~' `~~-_/'-=\/=-'\,
\ /// a `~. \ \
/ /~~~~-, ,__. , /// __,,,,) \ |
\/ \/ `~~~; ,---~~-_`/ \ / \/
/ / '. .'
'._.' _|`~~`|_
/|\ /|\
Commands: [analyze-process, dump-process, analyze-hprof, strip-hprof]
analyze-process: Dumps the heap for the provided process name, pulls the hprof file and analyzes it.
USAGE: analyze-process PROCESS_PACKAGE_NAME
[-d ID, --device ID] optional device/emulator id
[-p PATH, --proguard-mapping PATH] optional path to Proguard mapping file
dump-process: Dumps the heap for the provided process name and pulls the hprof file.
USAGE: dump-process PROCESS_PACKAGE_NAME
[-d ID, --device ID] optional device/emulator id
analyze-hprof: Analyzes the provided hprof file.
USAGE: analyze-hprof HPROF_FILE_PATH
[-p PATH, --proguard-mapping PATH] optional path to Proguard mapping file
strip-hprof: Replaces all primitive arrays from the provided hprof file with arrays of zeroes and generates a new "-stripped" hprof file.
USAGE: strip-hprof HPROF_FILE_PATH
""".trimIndent()
}
}
private fun dumpHeap(
packageName: String,
maybeDeviceId: String?
): File {
val workingDirectory = File(System.getProperty("user.dir"))
val deviceList = runCommand(workingDirectory, "adb", "devices")
val connectedDevices = deviceList.lines()
.drop(1)
.filter { it.isNotBlank() }
.map { SPACE_PATTERN.split(it)[0] }
val deviceId = if (connectedDevices.isEmpty()) {
SharkLog.d { "Error: No device connected to adb" }
exitProcess(1)
} else if (maybeDeviceId == null) {
if (connectedDevices.size == 1) {
connectedDevices[0]
} else {
SharkLog.d {
"Error: more than one device/emulator connected to adb," +
" use '--device ID' argument with one of $connectedDevices"
}
exitProcess(1)
}
} else {
if (maybeDeviceId in connectedDevices) {
maybeDeviceId
} else {
SharkLog.d { "Error: device '$maybeDeviceId' not in the list of connected devices $connectedDevices" }
exitProcess(1)
}
}
val processList = runCommand(workingDirectory, "adb", "-s", deviceId, "shell", "ps")
val matchingProcesses = processList.lines()
.filter { it.contains(packageName) }
.map {
val columns = SPACE_PATTERN.split(it)
columns[8] to columns[1]
}
val (processName, processId) = if (matchingProcesses.size == 1) {
matchingProcesses[0]
} else if (matchingProcesses.isEmpty()) {
SharkLog.d { "Error: No process matching \"$packageName\"" }
exitProcess(1)
} else {
val matchingExactly = matchingProcesses.firstOrNull { it.first == packageName }
if (matchingExactly != null) {
matchingExactly
} else {
SharkLog.d {
"Error: More than one process matches \"$packageName\" but none matches exactly: ${matchingProcesses.map { it.first }}"
}
exitProcess(1)
}
}
val heapDumpFileName =
SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS'-$processName.hprof'", Locale.US).format(
Date()
)
val heapDumpDevicePath = "/data/local/tmp/$heapDumpFileName"
SharkLog.d {
"Dumping heap for process \"$processName\" with pid $processId to $heapDumpDevicePath"
}
runCommand(
workingDirectory, "adb", "-s", deviceId, "shell", "am", "dumpheap", processId,
heapDumpDevicePath
)
// Dump heap takes time but adb returns immediately.
Thread.sleep(5000)
SharkLog.d { "Pulling $heapDumpDevicePath" }
val pullResult = runCommand(workingDirectory, "adb", "-s", deviceId, "pull", heapDumpDevicePath)
SharkLog.d { pullResult }
SharkLog.d { "Removing $heapDumpDevicePath" }
runCommand(workingDirectory, "adb", "-s", deviceId, "shell", "rm", heapDumpDevicePath)
val heapDumpFile = File(workingDirectory, heapDumpFileName)
SharkLog.d { "Pulled heap dump to $heapDumpFile" }
return heapDumpFile
}
private fun runCommand(
directory: File,
vararg arguments: String
): String {
val process = ProcessBuilder(*arguments)
.directory(directory)
.start()
.also { it.waitFor(10, SECONDS) }
if (process.exitValue() != 0) {
throw Exception(
"Failed command: '${arguments.joinToString(
" "
)}', error output: '${process.errorStream.bufferedReader().readText()}'"
)
}
return process.inputStream.bufferedReader()
.readText()
}
private fun analyze(
heapDumpFile: File,
proguardMappingFile: File?
) {
val listener = OnAnalysisProgressListener { step ->
SharkLog.d { step.name }
}
val proguardMapping = proguardMappingFile?.let {
ProguardMappingReader(it.inputStream()).readProguardMapping()
}
val heapAnalyzer = HeapAnalyzer(listener)
SharkLog.d { "Analyzing heap dump $heapDumpFile" }
val heapAnalysis = heapAnalyzer.analyze(
heapDumpFile, AndroidReferenceMatchers.appDefaults, true,
AndroidObjectInspectors.appDefaults,
proguardMapping = proguardMapping
)
SharkLog.d { heapAnalysis.toString() }
}
private fun stripHprof(heapDumpFile: File) {
SharkLog.d { "Stripping primitive arrays in heap dump $heapDumpFile" }
val stripper = HprofPrimitiveArrayStripper()
val outputFile = stripper.stripPrimitiveArrays(heapDumpFile)
SharkLog.d { "Stripped primitive arrays to $outputFile" }
}
private val SPACE_PATTERN = Regex("\\s+")