How does Android fully debug the framework-layer code?
1. I wrote an article about how to debug system_process for Android boot.
2 google's eclipse plug-in ADT has been able to easily debug the Android apk, but the application entered the Activity at the time of debugging.
3. If you want to know the Startup Process of the Activity, do you have to read the code + log output? This article tells you: NO!
4. I believe all of you are interested in code. Here we will release the code first.
ZygoteInit. java
[Java]View plaincopy
- Public static List <String> readCommandOutput (String command ){
- Runtime rt = Runtime. getRuntime ();
- Java. lang. Processproc;
- Try {
- Proc implements rt.exe c (command );
- If (proc. waitFor ()! = 0 ){
- Return null;
- }
- Shortlist <String> list = new shortlist <String> ();
- InputStreamReader ir = new InputStreamReader (proc. getInputStream ());
- BufferedReader in = new BufferedReader (ir );
- String line = null;
- While (line = in. readLine ())! = Null ){
- List. add (line );
- }
- Return list;
- } Catch (InterruptedException e ){
- E. printStackTrace ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- Return null;
- }
- Public static String getPackageName (){
- String strPid = Integer. toString (android. OS. Process. myPid ());
- String cmd = "ps ";
- List <String> result = readCommandOutput (cmd );
- If (result = null ){
- Return "";
- }
- For (String info: result ){
- If (info. contains (strPid )){
- Int index = info. lastIndexOf ("");
- If (index> = 0 ){
- StringsubStr = info. substring (index + 1 );
- Log. I (TAG, subStr );
- Return subStr;
- }
- }
- }
- Return "";
- }
- Public static boolean needDebug (String packageName ){
- String debugProcess = android. OS. SystemProperties. get ("persist. sys. debug ");
- Log. I (TAG, debugProcess );
- If (debugProcess. equals (packageName )){
- Return true;
- }
- Return false;
- }
- Public static void main (Stringargv []) {
- Try {
- // Start profiling the zygote initialization.
- SamplingProfilerIntegration. start ();
- RegisterZygoteSocket ();
- EventLog. writeEvent (LOG_BOOT_PROGRESS_PRELOAD_START,
- SystemClock. uptimeMillis ());
- Preload ();
- EventLog. writeEvent (LOG_BOOT_PROGRESS_PRELOAD_END,
- SystemClock. uptimeMillis ());
- // Finish profiling the zygote initialization.
- SamplingProfilerIntegration. writeZygoteSnapshot ();
- // Do an initial gc to cleanup after startup
- Gc ();
- // If requested, start system server directlyfrom Zygote
- If (argv. length! = 2 ){
- Throw new RuntimeException (argv [0] + USAGE_STRING );
- }
- If (argv [1]. equals ("start-system-server ")){
- StartSystemServer ();
- } Else if (! Argv [1]. equals ("")){
- Throw new RuntimeException (argv [0] + USAGE_STRING );
- }
- Log. I (TAG, "Accepting command socket connections ");
- If (ZYGOTE_FORK_MODE ){
- RunForkMode ();
- } Else {
- RunSelectLoopMode ();
- }
- CloseServerSocket ();
- } Catch (MethodAndArgsCaller caller ){
- String packageName = getPackageName ();
- If (needDebug (packageName )){
- Android. ddm. DdmHandleAppName. setAppName (packageName, UserHandle. myUserId ());
- Android. OS. Debug. waitForDebugger ();
- }
- Caller. run ();
- } Catch (RuntimeException ex ){
- Log. e (TAG, "Zygote died with exception", ex );
- CloseServerSocket ();
- Throw ex;
- }
- }
5. If you are interested, continue reading!
6 readCommandOutput: used to execute the command and obtain the command output.
7 getPackageName () is used to obtain the package name of the current process.
Here, the default process name is the package name.
Obtain ps output
Then, use the pid to find the output line of the program.
Extract the output line package name of the program.
8 needDebug () is used to determine whether the current process requires debugging. The principle is as follows:
You can use setprop persist. sys. debug to set the package name.
NeedDebug gets persist. sys. debug
Compare with the package name of the process to determine whether debugging is required.
9 The subsequent actions are the same as those in Android system_process for boot debugging:
Set the name of the App in DDM:
Android. ddm. DdmHandleAppName. setAppName (packageName, UserHandle. myUserId ());
Wait for the debugger to connect:
Android. OS. Debug. waitForDebugger ();
10 next, recompile and burn the records. mmm... is a long process, but the result will prove that it is indeed worthwhile.
11. Create a pseudo project and change the package in Manifest to the package name to be debugged.
12. Introduce the framework-layer code to be debugged in the project.
13 next, the adb shell connects to android and runs the command line
14. Run the following command:
Setprop persist. sys. debug package name
15. set breakpoints on the code to be debugged
16 next, start the application. Note that it is to start the program directly, rather than start debugging through eclipse!
17 enjoy it!
18 (end)
In android, how can I view the changed effect after the framework layer code is changed?
1. the following method applies to the real machine: Download the android source code, compile the modified framwork code, generate framework. jar, push it to the system/framework directory, and restart the machine! OK
2. The following method is suitable for simulators:
(1) decompress the package with unyaffs. In the sdk directory you downloaded, replace system. img with the framework. jar and compress it into a new system. img. Then, start the simulator and OK.
(2): You can also directly compile the source code and generate system. img to replace system. img in the simulator.
How to debug official android code
If it refers to the Framework layer (such as controls and system services), you can download it from SDK Manager and re-associate it with Eclipse. The specific association method is to press Ctrl: Button and TextView. On the new page that appears, click Change Attached Source in the upper-left corner, click External Folder, find the sdk directory, and click the sources directory, select the system version folder you want to download and click OK.
If you want to see the underlying android source code, you need the Linux system environment. I have never practiced this. The specific process is not very clear. You should check Baidu's "android source code, the link will not be given, so it will not be blocked again ......