用 jdb 追踪 Android

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

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

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 localhost:8011

--- you will see ---
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

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 (mMethodMap) {

 

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

841    

842        @Override

843        public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,

844                boolean allowsImplicitlySelectedSubtypes) {

845 =>         synchronized (mMethodMap) {

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 shortcomings 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 to. so "Jdb as debug host" is a better idea.


1) update libandroid_runtime


change below

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

 
JDK 21中使用JDB(Java Debug Wire Protocol Debugger)是一个命令行调试工具,它允许用户在命令行界面中调试Java程序。要使用JDB,首先需要有一个编译好的Java程序,通常这个程序是带有调试信息的,可以使用带有`-g`选项的`javac`命令来编译。 以下是使用JDB的基本步骤: 1. 编译Java程序,并包含调试信息: ``` javac -g YourProgram.java ``` 2. 启动JDB调试器: ``` jdb YourProgram ``` 3. 在JDB中,你可以使用一系列命令来进行调试,例如: - `run`:运行程序。 - `list`:列出当前类的源代码。 - `step` 或 `s`:单步执行程序,步入方法。 - `next` 或 `n`:单步执行程序,但会跳过方法调用。 - `cont` 或 `c`:继续执行程序到下一个断点。 - `print` 或 `p`:打印变量的值。 - `set`:设置变量的值。 - `where` 或 `w`:显示当前线程的调用栈。 - `kill`:停止当前正在运行的程序。 4. 设置断点,例如: - `stop in 类名.方法名`:在指定方法设置断点。 - `stop at 行号`:在指定行号设置断点。 5. 使用`run`命令开始运行程序,程序会在设置的断点处暂停执行。 6. 使用`cont`命令继续执行程序,或者使用`step`和`next`命令来逐行或逐方法执行程序。 7. 查看变量值或者程序状态,使用`print`命令。 8. 当调试完成,使用`exit`命令退出JDB。 请注意,JDB是一个基本的调试工具,它的功能比集成开发环境(IDE)中的图形化调试器要简单。对于复杂的调试任务,使用IDE提供的调试功能可能会更加方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值