The opportunity to write this article is due to a practical problem. This problem is not difficult, but when I analyzed the problem and wrote a diary, I suddenly felt that it was necessary to record the analysis process of this problem and share it with everyone. Some tools and methods were used in the analysis process, and I also learned some analysis skills and tool usage skills from another smart friend Mei Ming. The analysis process in this article includes some of the tools I mentioned in my previous article Overview of the causes of lag and frame drop in Android - Methodology, including: Reproduction Video\ Event Log\ Android Studio Source Code and App Debug\ Android Studio Profile\ Systrace\ Dumpsys\ PS, etc. Most of these tools have been used in the development process, and this analysis used these tools to work together to find the cause of the problem. You may think, is it necessary to write an article for such a simple question? The purpose of writing this article is to record it for myself, and secondly, I think the analysis process is more universal, including the analysis ideas and the use of tools. If it can help everyone, it would be the best. If you also have good ideas or unique debugging skills, you are welcome to scan the QR code of the discussion group in About Me to join the group chat and make progress together! PhenomenonThis problem was reported directly by the test. The bug description is a typical description of the phenomenon: " Desktop icons load slowly when returning to the desktop from the application ." The test provides the recorded video and captured log, as well as the corresponding Systrace, etc. Since the phenomenon and log are all there, let's start analyzing. Analysis processDetermine when the problem occurred
Then we can simply restore the problem here. The test report is that the desktop icons load slowly when returning from the application . From the Event Log, the desktop displays slowly because the desktop is killed . Therefore, when returning from the App, the desktop needs to be reloaded. It takes 413ms from the creation of the desktop process to the full display of the desktop (it actually takes at least 2s to fully display the desktop, because the Launcher needs to reload the content when it is cold started). Analysis of the reasons for the killingFrom the above analysis, we need to find out why the Launcher was killed. From the phenomenon, it seems to be related to the process com.jx.cmcc.ict.ibelieve, but we currently have no way to confirm it. Here we focus on this Event Log
Here we can see that the reason why Launcher was killed is kill background. Looking at the corresponding source code, we can see that reason = kill background is issued by AMS.killBackgroundProcesses. ActivityManagerService.killBackgroundProcesses
Students who are familiar with the source code can quickly find out that the AMS.killBackgroundProcesses interface will be provided to third-party applications for calling, and its Binder client is in ActivityManager.killBackgroundProcesses here ActivityManager.killBackgroundProcesses
Debug the SystemServer processKnowing the logic of the above code, what we need to do is to find which application calls ActivityManager.killBackgroundProcesses to kill the desktop in this scenario. Since we don't know which application it is (although we suspect it is com.jx.cmcc.ict.ibelieve, but there is no evidence), we first debug the SystemServer process. 1. First debug the source code. First, click the debug button in Android, select the system_process process (what we call SystemServer), and then click OK. The code breakpoint is set in the ActivityManagerService.killBackgroundProcesses method listed above. 2. Click to start the suspected App (you can work backwards from EventLog and the video to find the more suspicious App, and perform local testing to reproduce it after installation. Here we select several apps that appear in the video, including com.jx.cmcc.ict.ibelieve-和信that we suspected before). Clicking other apps will not enter this breakpoint, but clicking the 和信App will reach the breakpoint after it is started. 3. Here we can see that the call stack is a Binder call. We need to find the client of this Binder call. Continue to operate in AS, click the calculator button as shown below, enter getRealCallingPid() and click Evaluate below to see the result. Result = 29771 4. Use the PS command to view the app corresponding to this pid. You can see that it is this application that calls killBackgroundProcesses. Debug the App processTo investigate further, we debugged the app. Since we don’t have the source code, we set the breakpoint directly to android/app/ActivityManager.killBackgroundProcesses (because this is client code, we can set the breakpoint directly when debugging the App process). I installed this application locally for debugging and found that after logging in and starting it again, the desktop would be killed, confirming that the problem was with this App. At this point, we have basically determined that the problem is caused by this app, but if we want to see more detailed calls, we can use Android Studio Profile. Using the Android Studio Profiler toolOpen Android Studio, click the Profiler button, click the + sign, select the com.jx.cmcc.ict.ibelieve process, and then click the CPU column. Select Trace Java Methods here, then click Record next to it to start the operation. After the operation is completed, click Stop and AS will automatically start parsing. We can see the analysis results here: At the bottom is the detailed function call stack corresponding to the operation just now, which is displayed in the actual running order. (I often use this tool to view the source code logic and the code logic of third-party applications. It is a very good method whether it is for learning or solving problems.) We use ctrl+f to search for killBackgroundProcesses. If there is one, it will be highlighted. We just need to zoom in with the mouse to see the detailed call stack. You can see that the app executes the killBackground method in the loadComplete callback. (At this point, the app developer already knows what the problem is and can fix it quickly). Analysis of permissions issuesAs shown above, calling killBackgroundProcesses requires the permission Manifest.permission.KILL_BACKGROUND_PROCESSES.
Execute adb shell dumpsys package com.jx.cmcc.ict.ibelieve to view the permissions applied for by the com.jx.cmcc.ict.ibelieve process. It is found that this application applied for the KILL_BACKGROUND_PROCESSES permission when it was installed, and it is granted by default.
The corresponding permission level is normal. In other words, all third-party applications can have this permission by default, as long as you apply for it. In this case, it was because this App applied for this permission and performed the wrong behavior, which killed the desktop and seriously affected the user experience. Sad! Can the Systrace tool find out the culprit that killed the desktop?Since Systrace is often used, can Systrace find the culprit? The answer is yes (if you don't know how to view the wakeup information on Systrace, you can read this article Preliminary knowledge for analyzing Systrace). Let's record a Systrace and install the following sequence to see 1. First, look at the trace corresponding to the system_server process, find the point corresponding to killProcessGroup, and check its wake-up status. You can see that thread 19688 wakes up the killProcessGroup that executes AMS. Searching for 19688 in Systrace, you can see that it is Binder:1295_1E, and 1295 is SystemServer. Check the corresponding Binder:1295_1E to see which thread wakes up this thread. Search the thread 7289 and you can see that this thread is the main thread of the Woxin App. Check 7289 and confirm that it is the process com.jx.cmcc.ict.ibelieve. That is, the same as the WoXin App (tumor). We can also infer that the Kill was initiated by the Woxin App. We can further confirm this by using the MethodTrace of the AS above. SummarizeFrom the above analysis, we can see that this problem is caused by the application applying for inappropriate permissions and incorrectly using the corresponding functions, which seriously affects user usage. Generally speaking, our work is basically completed after this step of analysis. We only need to communicate with the store and contact the App developer to make modifications. But what surprised me was that Google was so lax about the permission android.permission.KILL_BACKGROUND_PROCESSES. I always thought that this permission should be applied for specifically to prevent apps from abusing it or using it for nothing (after all, it involves the life and death of other apps). 5. About MeI am a system development engineer in a small factory. For more information, please click About me. I really hope to communicate with you and make progress together. |
<<: Some thoughts on Android APP performance optimization
>>: Android 11, a detailed account of the vivid history from version 1.0 to 10.0
Since its inception, card-drawing mobile games ha...
In many countries, people do not have the habit o...
Is space-time pixelated? Just like looking at a d...
Electron microscope view of a diatom Silicon chip...
The launch date of Apple Watch has been delayed a...
Reporter recently According to the Third Affiliat...
[[198303]] The popular open source libraries on G...
Just like a metaphor, the TV that had been hangin...
Durex and Jiang Xiaobai took advantage of the pos...
The launch of mini programs has brought convenien...
Yesterday afternoon, Douyin short video announced...
1. Capture iOS Crash 1. Set an exception breakpoi...
52 years ago, China's first artificial satell...
1. Turn off automatic system updates [[398263]] S...
Many of my friends are deeply harmed by harassing...