Skip to content

Commit 9d53382

Browse files
committed
More bad behavior: ANR and long-term wedge in system process.
Use ActivityController (the special monkey hook) to wedge ActivityManagerService, useful for triggering system ANRs and for setting off the watchdog. Also add more logging so it's clear what happens when.
1 parent 82ace4a commit 9d53382

File tree

4 files changed

+149
-52
lines changed

4 files changed

+149
-52
lines changed

apps/Development/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
2929
<uses-permission android:name="android.permission.REBOOT" />
3030
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
31+
<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" />
3132
<uses-permission android:name="android.permission.SET_ALWAYS_FINISH" />
3233
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE" />
3334
<uses-permission android:name="android.permission.SET_DEBUG_APP" />

apps/Development/res/layout/bad_behavior.xml

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,70 @@
1414
limitations under the License.
1515
-->
1616

17-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18-
android:orientation="vertical"
17+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
1918
android:layout_width="match_parent"
2019
android:layout_height="match_parent">
2120

22-
<Button android:id="@+id/bad_behavior_crash_system"
23-
android:layout_width="wrap_content"
24-
android:layout_height="wrap_content"
25-
android:text="@string/bad_behavior_crash_system_label" />
26-
27-
<Button android:id="@+id/bad_behavior_crash_main"
28-
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"
30-
android:text="@string/bad_behavior_crash_main_label" />
31-
32-
<Button android:id="@+id/bad_behavior_crash_thread"
33-
android:layout_width="wrap_content"
34-
android:layout_height="wrap_content"
35-
android:text="@string/bad_behavior_crash_thread_label" />
36-
37-
<Button android:id="@+id/bad_behavior_crash_native"
38-
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"
40-
android:text="@string/bad_behavior_crash_native_label" />
41-
42-
<Button android:id="@+id/bad_behavior_wtf"
43-
android:layout_width="wrap_content"
44-
android:layout_height="wrap_content"
45-
android:text="@string/bad_behavior_wtf_label" />
46-
47-
<Button android:id="@+id/bad_behavior_anr"
48-
android:layout_width="wrap_content"
49-
android:layout_height="wrap_content"
50-
android:text="@string/bad_behavior_anr_label" />
51-
52-
<Button android:id="@+id/bad_behavior_anr_activity"
53-
android:layout_width="wrap_content"
54-
android:layout_height="wrap_content"
55-
android:text="@string/bad_behavior_anr_activity_label" />
56-
57-
<Button android:id="@+id/bad_behavior_anr_broadcast"
58-
android:layout_width="wrap_content"
59-
android:layout_height="wrap_content"
60-
android:text="@string/bad_behavior_anr_broadcast_label" />
61-
62-
<Button android:id="@+id/bad_behavior_anr_service"
63-
android:layout_width="wrap_content"
64-
android:layout_height="wrap_content"
65-
android:text="@string/bad_behavior_anr_service_label" />
66-
67-
</LinearLayout>
21+
<LinearLayout
22+
android:orientation="vertical"
23+
android:layout_width="match_parent"
24+
android:layout_height="match_parent">
25+
26+
<Button android:id="@+id/bad_behavior_crash_main"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:text="@string/bad_behavior_crash_main_label" />
30+
31+
<Button android:id="@+id/bad_behavior_crash_thread"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:text="@string/bad_behavior_crash_thread_label" />
35+
36+
<Button android:id="@+id/bad_behavior_crash_native"
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:text="@string/bad_behavior_crash_native_label" />
40+
41+
<Button android:id="@+id/bad_behavior_crash_system"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:text="@string/bad_behavior_crash_system_label" />
45+
46+
<Button android:id="@+id/bad_behavior_wtf"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:text="@string/bad_behavior_wtf_label" />
50+
51+
<Button android:id="@+id/bad_behavior_anr"
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content"
54+
android:text="@string/bad_behavior_anr_label" />
55+
56+
<Button android:id="@+id/bad_behavior_anr_activity"
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:text="@string/bad_behavior_anr_activity_label" />
60+
61+
<Button android:id="@+id/bad_behavior_anr_broadcast"
62+
android:layout_width="match_parent"
63+
android:layout_height="wrap_content"
64+
android:text="@string/bad_behavior_anr_broadcast_label" />
65+
66+
<Button android:id="@+id/bad_behavior_anr_service"
67+
android:layout_width="match_parent"
68+
android:layout_height="wrap_content"
69+
android:text="@string/bad_behavior_anr_service_label" />
70+
71+
<Button android:id="@+id/bad_behavior_anr_system"
72+
android:layout_width="match_parent"
73+
android:layout_height="wrap_content"
74+
android:text="@string/bad_behavior_anr_system_label" />
75+
76+
<Button android:id="@+id/bad_behavior_wedge_system"
77+
android:layout_width="match_parent"
78+
android:layout_height="wrap_content"
79+
android:text="@string/bad_behavior_wedge_system_label" />
80+
81+
</LinearLayout>
82+
83+
</ScrollView>

apps/Development/res/values/strings.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,15 @@
196196
<string name="select_account_to_sync">Select account to sync</string>
197197

198198
<!-- BadBehaviorActivity -->
199-
<string name="bad_behavior_crash_system_label">Crash the system server</string>
200199
<string name="bad_behavior_crash_main_label">Crash the main app thread</string>
201200
<string name="bad_behavior_crash_thread_label">Crash an auxiliary app thread</string>
202201
<string name="bad_behavior_crash_native_label">Crash the native process</string>
202+
<string name="bad_behavior_crash_system_label">Crash the system server</string>
203203
<string name="bad_behavior_wtf_label">Report a WTF condition</string>
204204
<string name="bad_behavior_anr_label">ANR (Stop responding for 20 seconds)</string>
205-
<string name="bad_behavior_anr_activity_label">ANR launching a new Activity</string>
205+
<string name="bad_behavior_anr_activity_label">ANR starting an Activity</string>
206206
<string name="bad_behavior_anr_broadcast_label">ANR receiving a broadcast Intent</string>
207207
<string name="bad_behavior_anr_service_label">ANR starting a Service</string>
208+
<string name="bad_behavior_anr_system_label">System ANR (in ActivityManager)</string>
209+
<string name="bad_behavior_wedge_system_label">Wedge system (5 minute system ANR)</string>
208210
</resources>

apps/Development/src/com/android/development/BadBehaviorActivity.java

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.android.development;
1818

1919
import android.app.Activity;
20+
import android.app.ActivityManagerNative;
21+
import android.app.IActivityController;
22+
import android.app.IActivityManager;
2023
import android.app.Service;
2124
import android.content.BroadcastReceiver;
2225
import android.content.Context;
@@ -45,8 +48,9 @@ private static class BadBehaviorException extends RuntimeException {
4548
public static class BadReceiver extends BroadcastReceiver {
4649
@Override
4750
public void onReceive(Context context, Intent intent) {
48-
Log.i(TAG, "in BadReceiver.onReceive() -- about to hang");
51+
Log.i(TAG, "in broadcast receiver -- about to hang");
4952
try { Thread.sleep(20000); } catch (InterruptedException e) { Log.wtf(TAG, e); }
53+
Log.i(TAG, "broadcast receiver hang finished -- returning");
5054
}
5155
};
5256

@@ -58,20 +62,63 @@ public IBinder onBind(Intent intent) {
5862

5963
@Override
6064
public int onStartCommand(Intent intent, int flags, int id) {
61-
Log.i(TAG, "in BadService.onStartCommand() -- about to hang");
65+
Log.i(TAG, "in service start -- about to hang");
6266
try { Thread.sleep(30000); } catch (InterruptedException e) { Log.wtf(TAG, e); }
67+
Log.i(TAG, "service hang finished -- stopping and returning");
6368
stopSelf();
6469
return START_NOT_STICKY;
6570
}
6671
}
6772

73+
public static class BadController extends IActivityController.Stub {
74+
private int mDelay;
75+
76+
public BadController(int delay) { mDelay = delay; }
77+
78+
public boolean activityStarting(Intent intent, String pkg) {
79+
try {
80+
ActivityManagerNative.getDefault().setActivityController(null);
81+
} catch (RemoteException e) {
82+
Log.e(TAG, "Can't call IActivityManager.setActivityController", e);
83+
}
84+
85+
if (mDelay > 0) {
86+
Log.i(TAG, "in activity controller -- about to hang");
87+
try { Thread.sleep(mDelay); } catch (InterruptedException e) { Log.wtf(TAG, e); }
88+
Log.i(TAG, "activity controller hang finished -- disabling and returning");
89+
mDelay = 0;
90+
}
91+
92+
return true;
93+
}
94+
95+
public boolean activityResuming(String pkg) {
96+
return true;
97+
}
98+
99+
public boolean appCrashed(String proc, int pid, String m, String m2, long time, String st) {
100+
return true;
101+
}
102+
103+
public int appNotResponding(String proc, int pid, String st) {
104+
return 0;
105+
}
106+
}
107+
68108
@Override
69109
public void onCreate(Bundle icicle) {
70110
super.onCreate(icicle);
71111

72112
if (getIntent().getBooleanExtra("anr", false)) {
73113
Log.i(TAG, "in ANR activity -- about to hang");
74114
try { Thread.sleep(20000); } catch (InterruptedException e) { Log.wtf(TAG, e); }
115+
Log.i(TAG, "activity hang finished -- finishing");
116+
finish();
117+
return;
118+
}
119+
120+
if (getIntent().getBooleanExtra("dummy", false)) {
121+
Log.i(TAG, "in dummy activity -- finishing");
75122
finish();
76123
return;
77124
}
@@ -127,6 +174,7 @@ public void onClick(View v) {
127174
public void onClick(View v) {
128175
Log.i(TAG, "ANR pressed -- about to hang");
129176
try { Thread.sleep(20000); } catch (InterruptedException e) { Log.wtf(TAG, e); }
177+
Log.i(TAG, "hang finished -- returning");
130178
}
131179
});
132180

@@ -154,5 +202,35 @@ public void onClick(View v) {
154202
startService(new Intent(BadBehaviorActivity.this, BadService.class));
155203
}
156204
});
205+
206+
Button anr_system = (Button) findViewById(R.id.bad_behavior_anr_system);
207+
anr_system.setOnClickListener(new View.OnClickListener() {
208+
public void onClick(View v) {
209+
Intent intent = new Intent(BadBehaviorActivity.this, BadBehaviorActivity.class);
210+
Log.i(TAG, "ANR system pressed -- about to engage");
211+
try {
212+
ActivityManagerNative.getDefault().setActivityController(
213+
new BadController(20000));
214+
} catch (RemoteException e) {
215+
Log.e(TAG, "Can't call IActivityManager.setActivityController", e);
216+
}
217+
startActivity(intent.putExtra("dummy", true));
218+
}
219+
});
220+
221+
Button wedge_system = (Button) findViewById(R.id.bad_behavior_wedge_system);
222+
wedge_system.setOnClickListener(new View.OnClickListener() {
223+
public void onClick(View v) {
224+
Intent intent = new Intent(BadBehaviorActivity.this, BadBehaviorActivity.class);
225+
Log.i(TAG, "Wedge system pressed -- about to engage");
226+
try {
227+
ActivityManagerNative.getDefault().setActivityController(
228+
new BadController(300000));
229+
} catch (RemoteException e) {
230+
Log.e(TAG, "Can't call IActivityManager.setActivityController", e);
231+
}
232+
startActivity(intent.putExtra("dummy", true));
233+
}
234+
});
157235
}
158236
}

0 commit comments

Comments
 (0)