//获取等待状态【见小节3.3】
final
int
waitState
=
evaluateCheckerCompletionLocked
();
if
(
waitState
==
COMPLETED
)
{
waitedHalf
=
false
;
continue
;
}
else
if
(
waitState
==
WAITING
)
{
continue
;
}
else
if
(
waitState
==
WAITED_HALF
)
{
if
(
!
waitedHalf
)
{
//第一次进入等待时间过半的状态
ArrayList
pids
=
new
ArrayList
();
pids
.
add
(
Process
.
myPid
());
//则输出栈信息【见小节3.4】
ActivityManagerService
.
dumpStackTraces
(
true
,
pids
,
null
,
null
,
NATIVE_STACKS_OF_INTEREST
);
waitedHalf
=
true
;
}
continue
;
}
//获取被阻塞的checkers
blockedCheckers
=
getBlockedCheckersLocked
();
subject
=
describeCheckersLocked
(
blockedCheckers
);
allowRestart
=
mAllowRestart
;
}
EventLog
.
writeEvent
(
EventLogTags
.
WATCHDOG
,
subject
);
ArrayList
pids
=
new
ArrayList
();
pids
.
add
(
Process
.
myPid
());
if
(
mPhonePid
>
0
)
pids
.
add
(
mPhonePid
);
//waitedHalf=true,则追加输出栈信息【见小节3.4】
final
File
stack
=
ActivityManagerService
.
dumpStackTraces
(
!
waitedHalf
,
pids
,
null
,
null
,
NATIVE_STACKS_OF_INTEREST
);
//系统已被阻塞1分钟,也不在乎多等待2s来确保stack trace信息输出
SystemClock
.
sleep
(
2000
);
if
(
RECORD_KERNEL_THREADS
)
{
//输出kernel栈信息【见小节3.5】
dumpKernelStackTraces
();
}
//触发kernel来dump所有阻塞线程【见小节3.6】
doSysRq
(
'l'
);
//输出dropbox信息【见小节3.7】
Thread
dropboxThread
=
new
Thread
(
"watchdogWriteToDropbox"
)
{
public
void
run
()
{
mActivity
.
addErrorToDropBox
(
"watchdog"
,
null
,
"system_server"
,
null
,
null
,
subject
,
null
,
stack
,
null
);
}
};
dropboxThread
.
start
();
try
{
//等待dropbox线程工作2s
dropboxThread
.
join
(
2000
);
}
catch
(
InterruptedException
ignored
)
{}
IActivityController
controller
;
synchronized
(
this
)
{
controller
=
mController
;
}
if
(
controller
!=
null
)
{
//将阻塞状态报告给activity controller,
try
{
Binder
.
setDumpDisabled
(
"Service dumps disabled due to hung system process."
);
//返回值为1表示继续等待,-1表示杀死系统
int
res
=
controller
.
systemNotResponding
(
subject
);
if
(
res
>=
0
)
{
waitedHalf
=
false
;
//继续等待
continue
;
}
}
catch
(
RemoteException
e
)
{
}
}
//当debugger没有attach时,才杀死进程
if
(
Debug
.
isDebuggerConnected
())
{
debuggerWasConnected
=
2
;
}
if
(
debuggerWasConnected
>=
2
)
{
Slog
.
w
(
TAG
,
"Debugger connected: Watchdog is *not* killing the system process"
);
}
else
if
(
debuggerWasConnected
>
0
)
{
Slog
.
w
(
TAG
,
"Debugger was connected: Watchdog is *not* killing the system process"
);
}
else
if
(
!
allowRestart
)
{
Slog
.
w
(
TAG
,
"Restart not allowed: Watchdog is *not* killing the system process"
);
}
else
{
Slog
.
w
(
TAG
,
"*** WATCHDOG KILLING SYSTEM PROCESS: "
+
subject
);
//遍历输出阻塞线程的栈信息
for
(
int
i
=
0
;
i
blockedCheckers
.
size
();
i
++
)
{
Slog
.
w
(
TAG
,
blockedCheckers
.
get
(
i
).
getName
()
+
" stack trace:"
);
StackTraceElement
[]
stackTrace
=
blockedCheckers
.
get
(
i
).
getThread
().
getStackTrace
();
for
(
StackTraceElement
element
:
stackTrace
)
{
Slog
.
w
(
TAG
,
" at "
+
element
);
}
}
Slog
.
w
(
TAG
,
"*** GOODBYE!"
);
//杀死进程system_server【见小节3.8】
Process
.
killProcess
(
Process
.
myPid
());
System
.
exit
(
10
);
}
waitedHalf
=
false
;
}
}
postAtFrontOfQueue(this),该方法输入参数为Runnable对象,根据消息机制,回调HandlerChecker中的run方法。