Tracing Android with jdb

Source: Internet
Author: User

You may have debug Android in eclipse, Have you ever used jdb tracing Android. Since Dalvikvm support jdwp, we can use jdb to debug the program. there are 2 ways

Android as debug host
Steps

1) update libandroid_runtime

Change below

[Java]
Frameworks/base/core/jni/AndroidRuntime. cpp
620 opt. optionString =
621 "-agentlib: jdwp = transport = dt_android_adb, suspend = n, server = y ";
 
As
 
Opt. optionString = "-agentlib: jdwp = transport = dt_socket, address = 8011, server = y, suspend = y ";

Frameworks/base/core/jni/AndroidRuntime. cpp
620 opt. optionString =
621 "-agentlib: jdwp = transport = dt_android_adb, suspend = n, server = y ";

As

Opt. optionString = "-agentlib: jdwp = transport = dt_socket, address = 8011, server = y, suspend = y ";


Use "mmm frameworks/base/core/jni" to re-generate system/lib/libandroid_runtime.so, then copy to your runing env (system/lib)

 

2) change system/root/default. prop on your running env

Change "ro. debuggable = 0" to "ro. debuggable = 1"

 


If you won not do it, you can specify AndroidManifest. xml

<Application android: label = "@ string/app_name" android: icon = "@ drawable/plasma" android: debuggable = "true">

Or the App can not be debugged

 


3) start the app


Check stop you will see below

[Java]
I/Zygote (6843/6843): Accepting command socket connections
 
I/jdwp (7385/7385): JDWP will wait for debugger on port 8011

I/Zygote (6843/6843): Accepting command socket connections

I/jdwp (7385/7385): JDWP will wait for debugger on port 8011

 


4) use jdb to attach the port

Jdb-attach local host: 8011

--- You will see ---
[Java]
Initializing jdb...
>
VM Started: "thread = <1> main", dalvik. system. Zygote. nativeForkSystemServer (), line =-1 bci =-1
 
<1> main [1]

Initializing jdb...
>
VM Started: "thread = <1> main", dalvik. system. Zygote. nativeForkSystemServer (), line =-1 bci =-1

<1> main [1]


 

5) include source dirs by "use" command, you will see source code

<1> main [1] use/home/user/jb/frameworks/base/core/java: /home/user/jb/frameworks/base/packages/SettingsProvider/src/:/home/user/jb/frameworks/base/services/java

 
6) set break and trace

[Java]
Use commands: stop in, cont, list, next, you will able to trace your code
 
<1> main [1] stop in com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList
 
Deferring breakpoint com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList.
 
<1> main [1] cont
 
> Ignoring cmd 268435570/199/1 from the VM
 
Set deferred breakpoint com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList
 
Breakpoint hit: "thread = <12> android. server. ServerThread", com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList (), line = 845 bci = 0
 
845 synchronized (synchronized hodmap ){
 

 
<12> android. server. ServerThread [1] list
 
841
 
842 @ Override
 
843 public List <InputMethodSubtype> getEnabledInputMethodSubtypeList (InputMethodInfo imi,
 
844 boolean allowsImplicitlySelectedSubtypes ){
 
845 => synchronized (synchronized hodmap ){
 
846 return getEnabledInputMethodSubtypeListLocked (imi, allowsImplicitlySelectedSubtypes );
 
847}
 
848}
 
849
 
850 @ Override
 
<12> android. server. ServerThread [1] next
 
Step completed: "thread = <12> android. server. ServerThread", com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList (), line = 846 bci = 3
 
846 return getEnabledInputMethodSubtypeListLocked (imi, allowsImplicitlySelectedSubtypes );
 
<12> android. server. ServerThread [1]

Use commands: stop in, cont, list, next, you will able to trace your code

<1> main [1] stop in com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList

Deferring breakpoint com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList.

<1> main [1] cont

> Ignoring cmd 268435570/199/1 from the VM

Set deferred breakpoint com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList

Breakpoint hit: "thread = <12> android. server. ServerThread", com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList (), line = 845 bci = 0

845 synchronized (synchronized hodmap ){

 

<12> android. server. ServerThread [1] list

841

842 @ Override

843 public List <InputMethodSubtype> getEnabledInputMethodSubtypeList (InputMethodInfo imi,

844 boolean allowsImplicitlySelectedSubtypes ){

845 => synchronized (synchronized hodmap ){

846 return getEnabledInputMethodSubtypeListLocked (imi, allowsImplicitlySelectedSubtypes );

847}

848}

849

850 @ Override

<12> android. server. ServerThread [1] next

Step completed: "thread = <12> android. server. ServerThread", com. android. server. InputMethodManagerService. getEnabledInputMethodSubtypeList (), line = 846 bci = 3

846 return getEnabledInputMethodSubtypeListLocked (imi, allowsImplicitlySelectedSubtypes );

<12> android. server. ServerThread [1]

 


Jdb as debug host
There are always comings in "Android as host", because every time the JavaVM starts up, it will move to listen status, So the SystemServer will listen, even we don't want. so "Jdb as debug host" is a better idea.

 

 

1) update libandroid_runtime

Change below

[Java]
Frameworks/base/core/jni/AndroidRuntime. cpp
620 opt. optionString =
621 "-agentlib: jdwp = transport = dt_android_adb, suspend = n, server = y ";
 
As
 
Opt. optionString = "-agentlib: jdwp = transport = dt_socket, address = localhost: 8011, server = n, suspend = y ";

Frameworks/base/core/jni/AndroidRuntime. cpp
620 opt. optionString =
621 "-agentlib: jdwp = transport = dt_android_adb, suspend = n, server = y ";

As

Opt. optionString = "-agentlib: jdwp = transport = dt_socket, address = localhost: 8011, server = n, suspend = y ";

If you won't run jdb on the same host, change "localhost" to other host/ip

2) change system/root/default. prop on your running env
Change "ro. debuggable = 0" to "ro. debuggable = 1"

3) use jdb to listen the port
Jdb-listen 8011.

4) Start Android or APPs

When your Dalvik VM start, it will connect 8011

The good advantages of "Jdb as Host" is that when you need to debug, you let jdb listen; if you won't listen, Android will run as normal.

For example, when I want to debug the App, I run "jdb-listen 8011", then I click the App icon on the Launcher and start to debug.

Attention:

The Apps have launch timeout limits or It will be terminated by AM, when you enter jdb console, you need to quickly set breakpoint, and enter "cont" to run

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.