This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Agent.java
480 lines (423 loc) · 16.5 KB
/
Agent.java
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in
// the LICENSE file in the root directory of this source tree. An
// additional grant of patent rights can be found in the PATENTS file
// in the same directory.
//
package com.facebook.fbadb.agent;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Process;
import android.util.JsonWriter;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Agent {
private static final String PROGNAME = "fb-adb-agent";
private static final int QUERY_PROCESSES = (1<<0);
private static final int QUERY_PACKAGES = (1<<1);
private static final int GET_ACTIVITIES = (1<<2);
private static final int USER_OWNER = 0;
private static Object cachedPm = null;
private static ActivityManager getAm() throws Exception {
Constructor amConstructor = ActivityManager.class.getDeclaredConstructor(
Context.class, Handler.class);
amConstructor.setAccessible(true);
// Normally, ActivityManager doesn't actually touch its constructor parameters for anything
// that we use it for. However, on certain Samsung devices, ActivityManager tries to fetch
// a system service immediately upon construction. Because, Samsung. So, give it a stubbed
// out Context object.
return (ActivityManager) amConstructor.newInstance(new AgentFakeContext(), null);
}
private static void writeSingleAppProcessInfo(
JsonWriter writer,
ActivityManager.RunningAppProcessInfo info) throws Exception {
writer.beginObject()
.name("importance").value(info.importance)
.name("importanceReasonCode").value(info.importanceReasonCode)
.name("importanceReasonPid").value(info.importanceReasonPid)
.name("lru").value(info.lru)
.name("pid").value(info.pid)
.name("processName").value(info.processName)
.name("uid").value(info.uid)
;
writer.name("pkgList");
writeStringArray(writer, info.pkgList);
writer.endObject();
}
private static void writeAppProcessInfo(JsonWriter writer, ActivityManager am)
throws Exception {
writer.beginArray();
for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
writeSingleAppProcessInfo(writer, info);
}
writer.endArray();
}
private static void writeSingleServiceInfo(
JsonWriter writer,
ActivityManager.RunningServiceInfo info) throws Exception {
writer.beginObject()
.name("activeSince").value(info.activeSince)
.name("clientCount").value(info.clientCount)
.name("clientLabel").value(info.clientLabel)
.name("clientPackage").value(info.clientPackage)
.name("crashCount").value(info.crashCount)
.name("flags").value(info.flags)
.name("foreground").value(info.foreground)
.name("lastActivityTime").value(info.lastActivityTime)
.name("pid").value(info.pid)
.name("process").value(info.process)
.name("restarting").value(info.restarting)
.name("service").value(info.service.flattenToString())
.name("started").value(info.started)
.name("uid").value(info.uid)
.endObject();
}
private static void writeServiceProcessInfo(JsonWriter writer, ActivityManager am)
throws Exception {
writer.beginArray();
for (ActivityManager.RunningServiceInfo info : am.getRunningServices(Integer.MAX_VALUE)) {
writeSingleServiceInfo(writer, info);
}
writer.endArray();
}
private static void writeSingleErrorInfo(
JsonWriter writer,
ActivityManager.ProcessErrorStateInfo info)
throws Exception {
writer.beginObject()
.name("condition").value(info.condition)
.name("longMsg").value(info.longMsg)
.name("pid").value(info.pid)
.name("processName").value(info.processName)
.name("shortMsg").value(info.shortMsg)
.name("stackTrace").value(info.stackTrace)
.name("tag").value(info.tag)
.name("uid").value(info.uid)
.endObject();
}
private static void writeErrorProcessInfo(JsonWriter writer, ActivityManager am)
throws Exception {
List<ActivityManager.ProcessErrorStateInfo> ei = am.getProcessesInErrorState();
writer.beginArray();
// Yes, the API is documented as returning null instead of an empty list.
if (ei != null) {
for (ActivityManager.ProcessErrorStateInfo info : ei) {
writeSingleErrorInfo(writer, info);
}
}
writer.endArray();
}
private static void processDump(JsonWriter writer) throws Exception {
ActivityManager am = getAm();
writer.setIndent(" ");
writer.name("running_processes");
writeAppProcessInfo(writer, am);
writer.name("running_services");
writeServiceProcessInfo(writer, am);
writer.name("error_processes");
writeErrorProcessInfo(writer, am);
}
private static Object getIPackageManager() throws Exception {
if (cachedPm == null) {
Method getService = Class.forName("android.os.ServiceManager")
.getMethod("getService", String.class);
cachedPm = Class.forName("android.content.pm.IPackageManager$Stub")
.getMethod("asInterface", android.os.IBinder.class)
.invoke(null, getService.invoke(null, "package"));
}
return cachedPm;
}
private static List<PackageInfo> getInstalledPackages(int flags)
throws Exception {
Class clsIPackageManager = Class.forName("android.content.pm.IPackageManager");
try {
Object packageListSlice;
try {
packageListSlice = clsIPackageManager
.getMethod("getInstalledPackages", int.class, int.class)
.invoke(getIPackageManager(), flags, USER_OWNER);
} catch (NoSuchMethodException ex) {
// Try old UID-less version.
packageListSlice = clsIPackageManager
.getMethod("getInstalledPackages", int.class)
.invoke(getIPackageManager(), flags);
}
return (List<PackageInfo>) packageListSlice
.getClass()
.getMethod("getList")
.invoke(packageListSlice);
} catch (NoSuchMethodException ex) {
// Try the old batched API. Loop until we've added all slices to the list. AOSP comments
// indicate the paging mechanism exists to work around IPC size limits --- size limits
// that disappeared in KitKat, when the above simpler API appeared?
List<PackageInfo> list = new ArrayList<PackageInfo>();
Object slice;
PackageInfo lastItem = null;
do {
final String lastKey = lastItem != null ? lastItem.packageName : null;
slice = clsIPackageManager
.getMethod("getInstalledPackages", int.class, String.class)
.invoke(getIPackageManager(), flags, lastKey);
lastItem = (PackageInfo) slice
.getClass()
.getMethod("populateList", List.class, android.os.Parcelable.Creator.class)
.invoke(slice, list, android.content.pm.PackageInfo.CREATOR);
} while (slice.getClass().getMethod("isLastSlice").invoke(slice).equals(false));
return list;
}
}
private static void writeStringArray(JsonWriter writer, String strings[])
throws Exception {
if (strings == null) {
writer.nullValue();
} else {
writer.beginArray();
for (String s : strings) {
writer.value(s);
}
writer.endArray();
}
}
private static void writeApplicationInfo(JsonWriter writer, ApplicationInfo info)
throws Exception {
writer.beginObject()
.name("backupAgentName").value(info.backupAgentName)
.name("className").value(info.className)
.name("dataDir").value(info.dataDir)
.name("descriptionRes").value(info.descriptionRes)
.name("enabled").value(info.enabled)
.name("flags").value(info.flags)
.name("manageSpaceActivityName").value(info.manageSpaceActivityName)
.name("nativeLibraryDir").value(info.nativeLibraryDir)
.name("permission").value(info.permission)
.name("processName").value(info.processName)
.name("publicSourceDir").value(info.publicSourceDir)
.name("sourceDir").value(info.sourceDir)
.name("targetSdkVersion").value(info.targetSdkVersion)
.name("taskAffinity").value(info.taskAffinity)
.name("theme").value(info.theme)
.name("uid").value(info.uid)
;
writer.name("sharedLibraryFiles");
writeStringArray(writer, info.sharedLibraryFiles);
writer.endObject();
}
private static void writeActivityInfo(JsonWriter writer, ActivityInfo acti)
throws Exception {
writer.beginObject()
.name("flags").value(acti.flags)
.name("permission").value(acti.permission)
.name("launchMode").value(acti.launchMode)
.name("targetActivity").value(acti.targetActivity)
.name("taskAffinity").value(acti.taskAffinity)
.name("enabled").value(acti.enabled)
.name("exported").value(acti.exported)
.name("processName").value(acti.processName)
.name("name").value(acti.name)
.endObject();
}
private static void writePackageInfo(JsonWriter writer, PackageInfo info, int flags)
throws Exception {
writer.beginObject()
.name("firstInstallTime").value(info.firstInstallTime)
.name("lastUpdateTime").value(info.lastUpdateTime)
.name("packageName").value(info.packageName)
.name("sharedUserId").value(info.sharedUserId)
.name("sharedUserLabel").value(info.sharedUserLabel)
.name("versionCode").value(info.versionCode)
.name("versionName").value(info.versionName)
;
writer.name("applicationInfo");
writeApplicationInfo(writer, info.applicationInfo);
if ((flags & GET_ACTIVITIES) != 0) {
writer.name("activities");
writer.beginArray();
if (info.activities != null) {
for (ActivityInfo acti : info.activities) {
writeActivityInfo(writer, acti);
}
}
writer.endArray();
}
writer.endObject();
}
private static void packageDump(JsonWriter writer, int flags) throws Exception {
writer.name("packages");
writer.beginArray();
int pmFlags = 0;
if ((flags & GET_ACTIVITIES) != 0) {
pmFlags |= PackageManager.GET_ACTIVITIES;
}
for (PackageInfo info : getInstalledPackages(pmFlags)) {
writePackageInfo(writer, info, flags);
}
writer.endArray();
}
private static List<ResolveInfo> queryIntentActivities(Intent intent, int flags)
throws Exception {
Class clsIPackageManager = Class.forName("android.content.pm.IPackageManager");
Method queryIntentActivities = clsIPackageManager
.getMethod(
"queryIntentActivities",
Intent.class,
String.class,
int.class,
int.class);
Object activities = queryIntentActivities
.invoke(getIPackageManager(), intent, null, 0, USER_OWNER);
if (activities == null) {
return Collections.emptyList();
} else if (!(activities instanceof List)) {
Method getList = activities.getClass().getMethod("getList");
activities = getList.invoke(activities);
}
return (List<ResolveInfo>) activities;
}
private static Intent getLaunchIntentForPackage(String packageName)
throws Exception {
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_INFO);
intentToResolve.setPackage(packageName);
List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
if (ris == null || ris.size() <= 0) {
intentToResolve.removeCategory(Intent.CATEGORY_INFO);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage(packageName);
ris = queryIntentActivities(intentToResolve, 0);
}
if (ris == null || ris.size() <= 0) {
return null;
}
Intent intent = new Intent(intentToResolve);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(ris.get(0).activityInfo.packageName,
ris.get(0).activityInfo.name);
return intent;
}
/**
* Like {@link PackageManager#getLaunchIntentForPackage()}, but prefer an activity explicitly
* marked as a debug entry point to any of the standard activities.
*
* @param packageName Package name to query
* @return Resolved intent or null
*/
private static Intent getDebugStartIntent(String packageName) throws Exception {
ActivityManager am = getAm();
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory("com.facebook.intent.category.DEBUG_ENTRY_POINT");
intentToResolve.setPackage(packageName);
List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
if (ris != null && !ris.isEmpty()) {
Intent intent = new Intent(intentToResolve);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(
ris.get(0).activityInfo.packageName,
ris.get(0).activityInfo.name);
return intent;
}
return getLaunchIntentForPackage(packageName);
}
private static void writeDebugLauncher(
JsonWriter writer,
String packageName) throws Exception {
Intent intent = getDebugStartIntent(packageName);
ComponentName resolved = intent == null ? null : intent.getComponent();
writer
.beginObject()
.name("packageName").value(packageName)
.name("debugIntent").value(
resolved != null
? resolved.flattenToString()
: null)
.endObject();
}
private static void processFindDebugLauncherPackages(
JsonWriter writer,
List<String> packages) throws Exception {
if (!packages.isEmpty()) {
writer.name("debug-launcher");
writer.beginArray();
for (String packageName : packages) {
writeDebugLauncher(writer, packageName);
}
writer.endArray();
}
}
private static void usage() throws Exception {
System.out.println(
String.format(
"%s THING...: write JSON-format system information", PROGNAME));
System.out.println("Each THING is either \"process-dump\" or \"package-dump\"");
System.out.println("");
System.out.println(
String.format(
"%s help: this usage information", PROGNAME));
}
public static void doMain(String[] args) throws Exception {
if (args.length < 1) {
System.err.println(String.format("%s: run -h for help", PROGNAME));
System.exit(1);
}
int flags = 0;
String FIND_DEBUG_LAUNCHER_PREFIX = "find-debug-launcher:";
ArrayList<String> findDebugLauncherPackages = new ArrayList<>();
for (String arg : args) {
if (arg.equals("-h") || arg.equals("--help") || arg.equals("help")) {
usage();
} else if (arg.startsWith(FIND_DEBUG_LAUNCHER_PREFIX)) {
findDebugLauncherPackages.add(
arg.substring(FIND_DEBUG_LAUNCHER_PREFIX.length()));
} else if (arg.equals("process-dump")) {
flags |= QUERY_PROCESSES;
} else if (arg.equals("package-dump")) {
flags |= QUERY_PACKAGES;
} else if (arg.equals("get-activities")) {
flags |= GET_ACTIVITIES;
} else {
System.err.println(String.format("%s: unknown command %s", PROGNAME, arg));
System.exit(1);
}
}
JsonWriter writer = new JsonWriter(
new BufferedWriter(
new OutputStreamWriter(
System.out, "UTF-8")));
writer.setIndent(" ");
writer.beginObject();
if ((flags & QUERY_PROCESSES) != 0) {
processDump(writer);
}
if ((flags & QUERY_PACKAGES) != 0) {
packageDump(writer, flags);
}
processFindDebugLauncherPackages(writer, findDebugLauncherPackages);
writer.endObject();
writer.flush();
}
public static void main(String[] args) {
// Need to explicitly catch at top-level: if we allow the exception to propagate, the VM will
// abort and print the exception only to logcat, not stderr, where we want it.
try {
doMain(args);
} catch (Throwable ex) {
ex.printStackTrace(System.err);
System.exit(2);
}
}
}