1 wrote an article before: "Android implementation boot Debug System_process"
2 Google Eclipse Plugin ADT has been able to easily debug Android apk, but when debugging the application has entered the activity.
3 If we want to know the activation process of activity, only look at the code + look at the log output? This article can tell you: no!
4 I believe you are more interested in the code, here first put out the code
Zygoteinit.java
[Java]View Plaincopy
- Public static list<string> readcommandoutput (String command) {
- Runtime RT =runtime.getruntime ();
- Java.lang.Processproc;
- try {
- Proc =rt.exec (command);
- if (proc.waitfor ()! = 0) {
- return null;
- }
- Linkedlist<string>list = new linkedlist<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 a 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, keep looking down!
6 Readcommandoutput: Used to execute commands and get output from commands
7 Getpackagename () has a package name to get the current process
The default process name here is the package name
Get the output of PS
The output line where the program is located is then found through the PID.
Extract the name of the output line package where the program is located
8 Needdebug () is used to determine if the current process needs to be debugged, the principle is this:
The user sets the package name by SetProp the Persist.sys.debug package name
Needdebug Get Persist.sys.debug
Compare with the package name of this process to determine if you want to debug
9 The next action will be the same as the "Android implementation boot Debug system_process":
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, mmm ..., a long process, but the results will prove to be worthwhile.
11 Next, create a pseudo-project, modify the package name in the manifest to be debugged
12 then introduce the framework layer code that needs to be debugged in the project
13 Next, the ADB shell connects to Android, making the command line
14 Execute Command:
SetProp Persist.sys.debug Package Name
15 setting breakpoints on code that needs to be debugged
16 Next, start the application, and note that you are launching the program directly, rather than starting debugging through eclipse!
Enjoy it!
18 (end)
How Android fully debugs the framework layer code