Android11系统源码分析:从binder trace视角看Activity冷启动

Android11系统源码分析:从binder trace视角看Activity冷启动

结论先行关注主干流程,不分析细节、由来。进程间ipc流程看binder trace,进程内流程借助AS断点。

一、 流程与TRACE

1.1、Launcher-- startActivity --> ATMS

1223 Traces for process: com.android.launcher3
1519 Count: 1
1520 Trace: java.lang.Throwable
1521     at android.os.BinderProxy.transact(BinderProxy.java:519)
1522     at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3692)
1523     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1723)
1524     at android.app.Activity.startActivityForResult(Activity.java:5320)
1525     at com.android.launcher3.Launcher.startActivityForResult(Launcher.java:1582)
1526     at com.android.launcher3.BaseQuickstepLauncher.startActivityForResult(BaseQuickstepLauncher.java:160)
1527     at android.app.Activity.startActivity(Activity.java:5660)
1528     at com.android.launcher3.BaseDraggingActivity.startActivitySafely(BaseDraggingActivity.java:182)
1529     at com.android.launcher3.Launcher.startActivitySafely(Launcher.java:1907)
1530     at com.android.launcher3.uioverrides.QuickstepLauncher.startActivitySafely(QuickstepLauncher.java:137)
1531     at com.android.launcher3.touch.ItemClickHandler.startAppShortcutOrInfoActivity(ItemClickHandler.java:285)
1532     at com.android.launcher3.touch.ItemClickHandler.onClick(ItemClickHandler.java:99)
1533     at com.android.launcher3.touch.ItemClickHandler.lambda$getInstance$0(ItemClickHandler.java:80)
1534     at com.android.launcher3.touch.-$$Lambda$ItemClickHandler$_rHy-J5yxnvGlFKWSh6CdrSf-lA.onClick(Unknown Source:2)
1535     at android.view.View.performClick(View.java:7448)
1536     at android.view.View.performClickInternal(View.java:7425)
1537     at android.view.View.access$3600(View.java:810)
1538     at android.view.View$PerformClick.run(View.java:28305)
1539     at android.os.Handler.handleCallback(Handler.java:938)
1540     at android.os.Handler.dispatchMessage(Handler.java:99)
1541     at android.os.Looper.loop(Looper.java:223)
1542     at android.app.ActivityThread.main(ActivityThread.java:7672)
1543     at java.lang.reflect.Method.invoke(Native Method)
1544     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
1545     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

1.2、ATMS -- startPausing --> Launcher

   3 Traces for process: system
 159 Count: 1
 160 Trace: java.lang.Throwable
 161     at android.os.BinderProxy.transact(BinderProxy.java:519)
 162     at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:2504)
 163     at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:136)
 164     at com.android.server.wm.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
 165     at com.android.server.wm.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:69)
 166     at com.android.server.wm.ActivityStack.startPausingLocked(ActivityStack.java:1105)
 167     at com.android.server.wm.TaskDisplayArea.pauseBackStacks(TaskDisplayArea.java:1200)
 168     at com.android.server.wm.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1658)
 169     at com.android.server.wm.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:1507)
 170     at com.android.server.wm.RootWindowContainer.resumeFocusedStacksTopActivities(RootWindowContainer.java:2306)
 171     at com.android.server.wm.ActivityStarter.startActivityInner(ActivityStarter.java:1733)
 172     at com.android.server.wm.ActivityStarter.startActivityUnchecked(ActivityStarter.java:1522)
 173     at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1186)
 174     at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:669)
 175     at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1096)
 176     at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1068)
 177     at com.android.server.wm.ActivityTaskManagerService.startActivity(ActivityTaskManagerService.java:1043)                        
 178     at android.app.IActivityTaskManager$Stub.onTransact(IActivityTaskManager.java:1422)
 179     at android.os.Binder.execTransactInternal(Binder.java:1154)
 180     at android.os.Binder.execTransact(Binder.java:1123)

1.3、Launcher -- activityPaused --> ATMS

1223 Traces for process: com.android.launcher3
1475 Count: 1
1476 Trace: java.lang.Throwable
1477     at android.os.BinderProxy.transact(BinderProxy.java:519)
1478     at android.app.IActivityTaskManager$Stub$Proxy.activityPaused(IActivityTaskManager.java:4368)   
1479     at android.app.servertransaction.PauseActivityItem.postExecute(PauseActivityItem.java:64)
1480     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:177)
1481     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
1482     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
1483     at android.os.Handler.dispatchMessage(Handler.java:106)
1484     at android.os.Looper.loop(Looper.java:223)
1485     at android.app.ActivityThread.main(ActivityThread.java:7672)
1486     at java.lang.reflect.Method.invoke(Native Method)
1487     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
1488     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

1.4、ATMS --> zygote fork APP

该阶段,没有体现在binder trace上。因为system_server进程使用socket通信到zygote进程,完成的fork。下面的调用栈用AS断点然后Get Thread dump得来。

就system_server进程沟通zygote的过程,分成了三个线程三段代码。binder线程收消息,display线程收集参数,ActivityManager:procStart线程真正沟通zygote,write,flush发出启动参数。

第一段:ATMS#startProcessAsync

"Binder:497_6@24045" prio=5 tid=0x76 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	 blocks android.display@24072
	 blocks android.anim@24073
	  at com.android.server.wm.ActivityTaskManagerService.startProcessAsync(ActivityTaskManagerService.java:5612)
	  at com.android.server.wm.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1680)
	  at com.android.server.wm.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:1507)
	  at com.android.server.wm.RootWindowContainer.resumeFocusedStacksTopActivities(RootWindowContainer.java:2306)
	  at com.android.server.wm.ActivityStarter.startActivityInner(ActivityStarter.java:1733)
	  at com.android.server.wm.ActivityStarter.startActivityUnchecked(ActivityStarter.java:1522)
	  at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1186)
	  at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:669)
	  - locked <0x5e78> (a com.android.server.wm.WindowManagerGlobalLock)
	  at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1096)
	  at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1068)
	  at com.android.server.wm.ActivityTaskManagerService.startActivity(ActivityTaskManagerService.java:1043)
	  at android.app.IActivityTaskManager$Stub.onTransact(IActivityTaskManager.java:1422)
	  at android.os.Binder.execTransactInternal(Binder.java:1154)
	  at android.os.Binder.execTransact(Binder.java:1123)

第二段:ProcessList#startProcessLocked

"android.display@24057" prio=5 tid=0x16 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	 blocks main@24228
	 blocks android.fg@24238
	 blocks ActivityManager@24245
	 blocks AlarmManager@24270
	  at com.android.server.am.ProcessList.startProcessLocked(ProcessList.java:1994)
	  at com.android.server.am.ProcessList.startProcessLocked(ProcessList.java:1943)
	  at com.android.server.am.ProcessList.startProcessLocked(ProcessList.java:2319)
	  at com.android.server.am.ProcessList.startProcessLocked(ProcessList.java:2441)
	  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3154)
	  at com.android.server.am.ActivityManagerService$LocalService.startProcess(ActivityManagerService.java:19627)
	  - locked <0x5e11> (a com.android.server.am.ActivityManagerService)
	  at com.android.server.wm.-$$Lambda$qMFJUmfG50ZSjk7Tac67xBia0d4.accept(lambda:-1)
	  at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:360)
	  at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:201)
	  at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:97)
	  at android.os.Handler.handleCallback(Handler.java:938)
	  at android.os.Handler.dispatchMessage(Handler.java:99)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.os.HandlerThread.run(HandlerThread.java:67)
	  at com.android.server.ServiceThread.run(ServiceThread.java:44)

第三段:ZygoteProcess#attemptZygoteSendArgsAndGetResult

"ActivityManager:procStart@24043" prio=5 tid=0x1c nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.os.ZygoteProcess.attemptZygoteSendArgsAndGetResult(ZygoteProcess.java:470)
	  at android.os.ZygoteProcess.zygoteSendArgsAndGetResult(ZygoteProcess.java:460)
	  at android.os.ZygoteProcess.startViaZygote(ZygoteProcess.java:795)
	  - locked <0x5e7f> (a java.lang.Object)
	  at android.os.ZygoteProcess.start(ZygoteProcess.java:372)
	  at android.os.Process.start(Process.java:649)
	  at com.android.server.am.ProcessList.startProcess(ProcessList.java:2296)
	  at com.android.server.am.ProcessList.handleProcessStart(ProcessList.java:2055)
	  at com.android.server.am.ProcessList.lambda$startProcessLocked$0$ProcessList(ProcessList.java:1991)
	  at com.android.server.am.-$$Lambda$ProcessList$jjoaAPSQT_weAnGqu0R0SCcvKqw.run(lambda:-1)
	  at android.os.Handler.handleCallback(Handler.java:938)
	  at android.os.Handler.dispatchMessage(Handler.java:99)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.os.HandlerThread.run(HandlerThread.java:67)
	  at com.android.server.ServiceThread.run(ServiceThread.java:44)

1.5、APP -- attachApplication --> ATMS

1548 Traces for process: com.example.myapplication
1757 Count: 1
1758 Trace: java.lang.Throwable
1759     at android.os.BinderProxy.transact(BinderProxy.java:519)
1760     at android.app.IActivityManager$Stub$Proxy.attachApplication(IActivityManager.java:5628)
1761     at android.app.ActivityThread.attach(ActivityThread.java:7346)                     
1762     at android.app.ActivityThread.main(ActivityThread.java:7659)
1763     at java.lang.reflect.Method.invoke(Native Method)
1764     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
1765     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

1.6、ATMS -- bindApplication --> APP

system_server进程发起端的调用栈

   3 Traces for process: system
 323 Count: 1
 324 Trace: java.lang.Throwable
 325     at android.os.BinderProxy.transact(BinderProxy.java:519)
 326     at android.app.IApplicationThread$Stub$Proxy.bindApplication(IApplicationThread.java:1513)
 327     at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:5318)                      
 328     at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:5442)
 329     at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2336)
 330     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2867)
 331     at android.os.Binder.execTransactInternal(Binder.java:1154)
 332     at android.os.Binder.execTransact(Binder.java:1123)

1.7、ATMS -- realStartActivity --> APP

   3 Traces for process: system
 118 Count: 1
 119 Trace: java.lang.Throwable
 120     at android.os.BinderProxy.transact(BinderProxy.java:519)
 121     at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:2504)
 122     at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:136)
 123     at com.android.server.wm.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
 124     at com.android.server.wm.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:868)                     
 125     at com.android.server.wm.RootWindowContainer.startActivityForAttachedApplicationIfNeeded(RootWindowContainer.java:1972)
 126     at com.android.server.wm.RootWindowContainer.lambda$5fbF65VSmaJkPHxEhceOGTat7JE(Unknown Source:0)
 127     at com.android.server.wm.-$$Lambda$RootWindowContainer$5fbF65VSmaJkPHxEhceOGTat7JE.apply(Unknown Source:8)
 128     at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:315)
 129     at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:201)
 130     at com.android.internal.util.function.pooled.OmniFunction.apply(OmniFunction.java:78)
 131     at com.android.server.wm.ActivityRecord.forAllActivities(ActivityRecord.java:3637)
 132     at com.android.server.wm.WindowContainer.forAllActivities(WindowContainer.java:1331)
 133     at com.android.server.wm.WindowContainer.forAllActivities(WindowContainer.java:1324)
 134     at com.android.server.wm.RootWindowContainer.attachApplication(RootWindowContainer.java:1949)
 135     at com.android.server.wm.ActivityTaskManagerService$LocalService.attachApplication(ActivityTaskManagerService.java:6888)
 136     at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:5362)
 137     at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:5442)
 138     at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2336)
 139     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2867)
 140     at android.os.Binder.execTransactInternal(Binder.java:1154)
 141     at android.os.Binder.execTransact(Binder.java:1123)

1.8、APP生命周期

onCreate

"main@15764" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at com.example.myapplication.MainActivity3.onCreate(MainActivity3.java:12)
	  at android.app.Activity.performCreate(Activity.java:8000)
	  at android.app.Activity.performCreate(Activity.java:7984)
	  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
	  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
	  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
	  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
	  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

onStart

"main@15764" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.app.Activity.onStart(Activity.java:1829)
	  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
	  at android.app.Activity.performStart(Activity.java:8024)
	  at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3457)
	  at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
	  at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
	  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

onResume

"main@15764" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.app.Activity.onResume(Activity.java:1905)
	  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
	  at android.app.Activity.performResume(Activity.java:8135)
	  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4428)
	  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
	  at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
	  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

到此,Activity冷启动流程结束,下面是额外补充

onPause

"main@15801" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.app.Activity.onPause(Activity.java:2347)
	  at android.app.Activity.performPause(Activity.java:8174)
	  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1510)
	  at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4729)
	  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4690)
	  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4641)
	  at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
	  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

onStop

"main@15801" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.app.Activity.onStop(Activity.java:2589)
	  at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1476)
	  at android.app.Activity.performStop(Activity.java:8215)
	  at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4844)
	  at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4823)
	  at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4897)
	  at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:40)
	  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

onRestart

"main@15801" prio=5 tid=0x2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at android.app.Activity.onRestart(Activity.java:1862)
	  at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1445)
	  at android.app.Activity.performRestart(Activity.java:8102)
	  at android.app.ActivityThread.performRestartActivity(ActivityThread.java:4932)
	  at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:243)
	  at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
	  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
	  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:223)
	  at android.app.ActivityThread.main(ActivityThread.java:7672)
	  at java.lang.reflect.Method.invoke(Method.java:-1)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

1.9、activityIdle

启动后,在app进程消息队列空闲时,会触发idle。

1548 Traces for process: com.example.myapplication
1929 Count: 1
1930 Trace: java.lang.Throwable
1931     at android.os.BinderProxy.transact(BinderProxy.java:519)
1932     at android.app.IActivityTaskManager$Stub$Proxy.activityIdle(IActivityTaskManager.java:4314)                                    
1933     at android.app.ActivityThread$Idler.queueIdle(ActivityThread.java:2113)
1934     at android.os.MessageQueue.next(MessageQueue.java:404)
1935     at android.os.Looper.loop(Looper.java:183)
1936     at android.app.ActivityThread.main(ActivityThread.java:7672)
1937     at java.lang.reflect.Method.invoke(Native Method)
1938     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
1939     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

二、问题:冷启动bindpplication之前无法抓binder trace

App进程刚起来时加个属性同步下。ActivityThread.javaActivityManagerService.java

frameworks/base$ git diff core/java/android/app/ActivityThread.java
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index b47d44d60790..0eca8ddf993c 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -7610,6 +7617,11 @@ public final class ActivityThread extends ClientTransactionHandler {
 
     public static void main(String[] args) {
         Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
+        //set binder tracing
+        boolean enable = "1".equals(SystemProperties.get("sys.appboot.bindertracing"));
+        if (( Build.IS_DEBUGGABLE) && enable) {
+            Binder.enableTracing();
+        }
 
         // Install selective syscall interception
         AndroidOs.install();
----------------------------------------------------------------------------
frameworks/base$ git diff services/core/java/com/android/server/am/ActivityManagerService.java
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 3f4878e38029..13c3ff601ba6 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -18862,6 +18862,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                     Log.v(TAG, "Process disappared");
                 }
             }
+            SystemProperties.set("sys.appboot.bindertracing","1");
             return true;
         }
     }
@@ -18869,6 +18870,7 @@ public class ActivityManagerService extends IActivityManager.Stub
     public boolean stopBinderTrackingAndDump(ParcelFileDescriptor fd) throws RemoteException {
         try {
             synchronized (this) {
+                SystemProperties.set("sys.appboot.bindertracing","0");
                 mBinderTransactionTrackingEnabled = false;
                 // TODO: hijacking SET_ACTIVITY_WATCHER, but should be changed to its own
                 // permission (same as profileControl).

1.5小节的trace就是靠这个抓到的。

三、总结

一张图展示大致流程,12个阶段。

  • 本文只贴了调用栈,展示发生了什么。关于为什么,关于细节,要去断点读代码,读博客,读书。

  • 本文关注了进程、生命周期相关的操作,activity显示流程本文没有体现。

  • am trace-ipc展示跨进程脉络,AS断点探索进程内流程。

  • 我抓的一份binder trace。example-app-cold-start-binder.trace

posted @ 2021-08-08 18:09  秋城  阅读(1834)  评论(0编辑  收藏  举报