How to Use jdb to debug Android Java programs

Source: Internet
Author: User
Tags throwable

How to Use jdb to debug Android Java programs

When I get used to GDB, I always feel that eclipse is too bloated and uncomfortable. See Li Xijing wrote an article "using jdb/jdbshell debugging Android program" (http://www.limodev.cn/blog/archives/1281), with the next, feel more comfortable than eclipse. Jdb commands are so awkward. I wrote a jdbshell and added the command history and alias (several common gdb commands ).

The following is a simple example of how to debug the browser application.

1) download and compile jdbshell

Www.limodev.cn/blog /? Dl_name1_jdbshell.tar.gz

Because I basically execute the threads variable at the beginning, I added a sentence before starting the wile loop.

Else
{
Close (parent_to_child [0]);
Write (parent_to_child [1], "Threads \ n", strlen ("Threads \ n "));
While (1)
{
Int I = 0;
Line = Readline ("");

...

}

}

After compilation, place the jdbshell IN ~ /Bin, or add the directory to the path

If Readline. h cannot be opened, install libreadline6 and libreadline6-dev

2) Add the jdbclient function to build/envsetup. Sh, following the example of gdbclient.

Function jdbclient ()
{
Local my_src_path = "$1"
If ["$ my_src_path"]; then
My_src_path = $1
Echo $ my_src_path
Else
Echo "root" $ our_root
My_src_path = "app_process"
Fi

Local Port = "$2"
If ["$ port"]; then
Port = $2
Else
Port = ": 9000"
Fi

Local PID
Local prog = "$3"
If ["$ prog"]; then
PID = 'pid $3'
Echo TCP $ port jdwp $ PID
ADB forward "TCP $ port" "jdwp: $ PID"
Echo jdbshell-sourcepath $ my_src_path-Attach localhost $ Port
Jdbshell-sourcepath $ my_src_path-Attach localhost $ Port
Sleep 2
Else
Echo ""
Echo "Please specify app you want to debug :"
Echo ""
Fi
}

3) Start the android simulator and run the browser

4) jdbclient packets/APP/Browser/src/: 9000 Browser
The first parameter is the address of the code you want to load. If you want to load multiple codes (which can be viewed during debugging), you can
Export debug_src_path =
Then
Jdbclient $ debug_src_path: 9000 Browser

The second parameter is the attach port. If port 9000 is in use, you can use another port.
The third parameter is the application to be debugged.
PID browser to obtain the process ID, so make sure that
ADB shell PS | grep Browser
View the corresponding process

For example ~ /. In bashrc, debug_src_path is set.

Export android_src =/home/Tom/work/gingerbread
Export debug_src_path = $ android_src/frameworks/base/OpenGL/Java: $ android_src/frameworks/base/AWT/Java: $ android_src/frameworks/base/CORE/Java: $ android_src/frameworks/base/location/Java: $ android_src/frameworks/base/sax/Java: $ android_src/frameworks/base/graphics/Java: $ android_src/frameworks/base/telephony/Java: $ android_src/frameworks/base/services/Java: $ android_src/frameworks/base/Media/Java: $ android_src/frameworks/base/WiFi/Java: $ android_src/frameworks/base/IM/Java: $ android_src/Dalvik/libcore/suncompat/src/main/Java: $ android_src/Dalvik/libcore/nio_char/src/main/Java: $ android_src/Dalvik/libcore/security-kernel/src/main/Java: $ android_src/Dalvik/libcore/security/src/main/Java: $ android_src/Dalvik/libcore/archive/src/main/Java: $ android_src/Dalvik/libcore/AWT-kernel/src/main/Java: $ android_src/Dalvik/libcore/luni/src/main/Java: $ android_src/Dalvik/libcore/Math/src/main/Java: $ android_src/Dalvik/libcore/X-net/src/main/Java: $ android_src/Dalvik/libcore/OpenSSL/src/main/Java: $ android_src/Dalvik/libcore/Dalvik/src/main/Java: $ android_src/Dalvik/libcore/auth/src/main/Java: $ android_src/Dalvik/libcore/concurrent/src/main/Java: $ android_src/Dalvik/libcore/SQL/src/main/Java: $ android_src/Dalvik/libcore/prefs/src/main/Java: $ android_src/Dalvik/libcore/XML/src/main/Java: $ android_src/Dalvik/libcore/text/src/main/Java: $ android_src/Dalvik/libcore/text/src/main/Java: $ android_src/Dalvik/libcore/luni-kernel/src/main/Java: $ android_src/Dalvik/libcore/luni-kernel/src/main/Java: $ android_src/Dalvik/libcore/RegEx/src/main/Java: $ android_src/Dalvik/libcore/RegEx/src/main/Java: $ android_src/Dalvik/libcore/NiO/src/main/Java: $ android_src/Dalvik/libcore/NiO/src/main/Java: $ android_src/Dalvik/libcore/JSON/src/main/Java: $ android_src/Dalvik/libcore/crypto/src/main/Java: $ android_src/Dalvik/libcore/ICU/src/main/Java: $ android_src/Dalvik/libcore/annotation/src/main/Java: $ android_src/Dalvik/libcore/JUnit/src/main/Java: $ android_src/Dalvik/libcore/logging/src/main/Java: $ android_src/Dalvik/libcore/logging/src/main/Java: $ android_src/Dalvik/libcore-disabled/instrument/src/main/Java: $ android_src/Dalvik/libcore-disabled/instrument/src/main/Java: $ android_src/Dalvik/libcore-disabled/sound/src/main/Java: packages/apps/Browser/src/

5) the following information is displayed, indicating that the debugger is started.
Set uncaught java. Lang. throwable
Set deferred uncaught java. Lang. throwable
Initializing jdb...
Group system:
(Java. Lang. Thread) 0xc14050d0c0 <6> compiler cond. Waiting
(Java. Lang. Thread) 0xc14050cf50 <4> signal catcher cond. Waiting
(Java. Lang. Thread) 0xc14050cea8 <3> GC cond. Waiting
(Java. Lang. Thread) 0xc14050cdf0 <2> heapworker cond. Waiting
Group Main:
(Java. Lang. Thread) 0xc14001f1a8 <1> main running
(Java. Lang. Thread) 0xc140562ad8 <21> asynctask #5 cond. Waiting
(Android. OS. handlerthread) 0xc1405528f8 <20> webviewworkerthread running
(Android.net. http. connectionthread) 0xc14053a780 <19> http3 cond. Waiting
(Android.net. http. connectionthread) 0xc14053a5a8 <18> http2 cond. Waiting
(Android.net. http. connectionthread) 0xc140530f90 <17> http1 cond. Waiting
(Android.net. http. connectionthread) 0xc140530da0 <16> http0 cond. Waiting
(Java. Lang. Thread) 0xc14058c008 <15> thread-18 running
(Java. Lang. Thread) 0xc140593370 <14> asynctask #4 cond. Waiting
(Java. Lang. Thread) 0xc140595dd0 <13> asynctask #3 cond. Waiting
(Java. Lang. Thread) 0xc14058f9f0 <12> asynctask #2 cond. Waiting
(Java. Lang. Thread) 0xc14055a820 <11> webviewcorethread running
(Java. Lang. Thread) 0xc140591bc8 <10> asynctask #1 cond. Waiting
(Java. Lang. Thread) 0xc140589710 <9> cookiesyncmanager running
(Java. Lang. Thread) 0xc14050f288 <8> binder thread #2 running
(Java. Lang. Thread) 0xc14050e900 <7> binder thread #1 running
>

6) Select the thread of interest
Most applications run in the main thread, and browser is no exception.
Thread thread ID

> Thread 0xc14001f1a8
<1> main [1]

7) suspend a thread
Suspend thread ID
<1> main [1] suspend 0xc14001f1a8

8) set breakpoints
Command:
Stop at <class >:< row number> or
Stop in <class>. <Method Name> [(parameter type,...)]

<1> main [1] Stop com. Android. browser. browseractivity: 2689
Set breakpoint com. Android. browser. browseractivity: 2689

It is a little troublesome to write a class name in this place. Which of the following prawns can be converted into direct file names like GDB?

9) continue execution
Command C (ONT)

<1> main [1] C
>

10) Click a link on the webpage.
We can see that the program is disconnected at browseractivty: 2689 (shouldoverideurlloading ).
Now you can view the stack
Command: BT/wherei
<1> main [1] BT
[1] com. Android. browser. browseractivity. shouldoverrideurlloading (browseractivity. Java: 2,689), Pc = 8
[2] com. Android. browser. Tab $2. shouldoverrideurlloading (Tab. Java: 552), Pc = 44
[3] Android. WebKit. callbackproxy. uioverrideurlloading (callbackproxy. Java: 216), Pc = 19
[4] Android. WebKit. callbackproxy. handlemessage (callbackproxy. Java: 323), Pc = 347
[5] Android. OS. handler. dispatchmessage (handler. Java: 99), Pc = 20
[6] Android. OS. lorule. Loop (lorule. Java: 123), Pc = 75
[7] Android. App. activitythread. Main (activitythread. Java: 3,683), Pc = 31
[8] java. Lang. Reflect. method. invokenative (native method)
[9] java. Lang. Reflect. method. Invoke (method. Java: 507), Pc = 18
[10] com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 839), Pc = 11
[11] com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 597), Pc = 84
[12] Dalvik. system. nativestart. Main (native method)

View code
Command: L (IST)
<1> main [1] l
2,685}
2,686}
2,687
2,688 Boolean shouldoverrideurlloading (webview view, string URL ){
2,689 => If (URL. startswith (scheme_wtai )){
2,690 // wtai: // WP/MC; number
2,691 // number = string (phone-number)
2,692 if (URL. startswith (scheme_wtai_mc )){
2,693 intent = new intent (intent. action_view,
2,694 URI. parse (webview. scheme_tel +

View Variables
Command: Print
<1> main [1] print URL
Url = "http://m.baidu.com/img? Tn = bdidxiphone & SSID = 0 & from = 844b & bd_page_type = 1 & uid = wiaui_1320452293_7438 & Pu = SZ % 401320_133 & itj = 41"

Continue execution
S (TEP)-execute the current row
Step up-execute to the current method and return to its calling program
S (TEP) I-execute the current command
N (EXT)-skip a row (skip call)
C (ONT)-continue execution from the breakpoint

No, help is a virtue.
<1> main [1] Help
** Command list **
Connectors -- list available connectors and transports in this VM

Run [class [ARGs] -- start execution of application's main class

Threads [threadgroup] -- list threads
Thread <thread ID> -- Set Default thread
Suspend [thread ID (s)] -- suspend threads (default: All)
Resume [thread ID (s)] -- resume threads (default: All)
Where [<thread ID> | all] -- dump a thread's stack
Wherei [<thread ID> | all] -- dump a thread's stack, with PC info
Up [N frames] -- move up a thread's stack
Down [N frames] -- move down a thread's stack
Kill <thread ID> <expr> -- kill a thread with the given exception object
Interrupt <thread ID> -- interrupt a thread

Print <expr> -- print value of expression
Dump <expr> -- print all object information
Eval <expr> -- evaluate expression (same as print)
Set <lvalue >=< expr> -- assign new value to field/variable/array element
Locals -- print all local variables in current stack frame

Classes -- List currently known classes
Class <class ID> -- Show Details of named class
Methods <class ID> -- list a class's methods
Fields <class ID> -- list a class's Fields

Threadgroups -- list threadgroups
Threadgroup <Name> -- set current threadgroup

Stop in <class ID>. <method> [(argument_type,...)]
-- Set a breakpoint in a method
Stop at <class ID >:< line> -- set a breakpoint at a line
Clear <class ID>. <method> [(argument_type,...)]
-- Clear a breakpoint in a method
Clear <class ID >:< line> -- clear a breakpoint at a line
Clear -- list breakpoints
Catch [uncaught | caught | all] <class ID> | <class pattern>
-- Break when specified exception occurs
Ignore [uncaught | caught | all] <class ID> | <class pattern>
-- Cancel 'catch 'for the specified exception
Watch [access | all] <class ID>. <field Name>
-- Watch Access/modifications to a field
Unwatch [access | all] <class ID>. <field Name>
-- Discontinue watching access/modifications to a field
Trace [go] Methods [thread]
-- TRACE method entries and exits.
-- All threads are suincluded unless 'go' is specified
Trace [go] method exit | exits [thread]
-- Trace the current method's exit, or all methods 'exits
-- All threads are suincluded unless 'go' is specified
Untrace [Methods] -- stop tracing method entrys and/or exits
Step -- execute current line
Step up -- execute until the current method returns to its caller
Stepi -- execute current Instruction
Next -- step one line (step over CILS)
Cont -- continue execution from breakpoint

List [line number | method] -- print source code
Use (or sourcepath) [source file path]
-- Display or change the Source Path
Exclude [<class pattern>,... | "NONE"]
-- Do not report step or method events for specified classes
Classpath -- print classpath info from target VM

Monitor <command> -- execute command each time the program stops
Monitor -- list monitors
Unmonitor <monitor #> -- delete a monitor
Read <FILENAME> -- read and execute a command file

Lock <expr> -- print lock info for an object
Threadlocks [thread id] -- print lock info for a thread

Pop -- pop the stack through and including the current frame
Reenter -- same as pop, but current frame is reentered
Redefine <class ID> <class file name>
-- Redefine the code for a class

Disablegc <expr> -- prevent garbage collection of an object
Enablegc <expr> -- permit garbage collection of an object

!! -- Repeat last command
<N> <command> -- Repeat command n times
# <Command> -- discard (no-op)
Help (or ?) -- LIST commands
Version -- print version information
Exit (or quit) -- exit Debugger

<Class ID>: A full class name with package qualifiers
<Class pattern>: A class name with a leading or trailing wildcard ('*')
<Thread ID>: thread number as reported in the 'threads' command
<Expr>: a Java (TM) programming language expression.
Most common syntax is supported.

Startup commands can be placed in either "jdb. ini" or ". jdbrc"
In User. Home or user. dir

If you want to view the alias ing, run it on the terminal.

Tom @ tom-LAPTOP :~ /Work/gingerbread $ jdbshell
Usage: jdbshell [jdb ARGs]
Example:./jdbshell-sourcepath $ android_src_path-Attach 6107
Command alias:
C -- cont
L -- list
N -- next
R -- run
S -- step
Si -- stepi
F -- step up
BT -- wherei

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.