Command Line Android Application Debugging

http://codeseekah.com/2012/02/16/command-line-android-development-debugging/

I personally have a distaste towards IDEs, preferring lightweight solutions, with maybe less convenience. I addition to saving resources and having direct control over what happens and what doesn’t, I find that by doing things the low level way you begin to better understand how things work.

Sure, Eclipse will let you debug in 2 clicks, but what do you learn besides that you application has a bug? There should always be time to learn a thing or two about the underlying technologies. What if one day, you have to SSH into a server and debug a Java application right there and then? If you’ve never seen anything beyond Eclipse in your life you’re in for some hair pulling. So let’s learn some low level stuff.

The Dalvik VM adheres to the Java Debug Wire Protocol, although it does not support all of the protocol features. This means that any JDWP-compliant debugger should be able to attach itself to an Android application. The Eclipse debugger uses this protocol. But there’s a Java debugger which ships with the JDK and would usually be already installed.

It’s called jdb – the Java Debugger, a command line debugger, just like gdb.

Debugging Android Applications with jdb
In order to attach jdb to an Android application, which is running inside the Dalvik VM, we have to use adb, the Android debug bridge. adb bridges the gap between an application and a development/debugging environment. The Dalvik VM creates a JDWP thread for every application to allow debuggers to attach to it on certain ports/process IDs.

In order to find out the JDWP ID of a debuggable application, issue the adb jdwp command. This will return a list of currently active JDWP processes. The very last number corresponds to the JDWP the last debuggable application launched.

To attach jdb to the remote VM we have to have adb forward the remote JDWP port/process ID to a local port. This is done with the forward command, like so: adb forward tcp:7777 jdwp:JDWP_PORT. adb will open a local TCP socket that you can connect to, and will forward all data sent to the local TCP socket to the JDWP process running on the device/emulator.

Next, attach jdb like so: jdb -sourcepath /your/project/src -attach localhost:7777. The sourcepath is the path to your project’s src directory, if you’re launching jdb from you project directory simply state -sourcepath src or -sourcepath ./src. Press return and you should be attached to your application.

As an example of the full procedure, I’ve built (don’t forget to add android:debuggable="true" to your application’s AndroidManifest.xml file) and launched the WordPress Android application:

[email protected]:~/code/wordpress-android/2.0.5$ adb jdwp
5384
6385
7051 # <- last launched
soul[email protected]:~/code/wordpress-android/2.0.5$ adb forward tcp:7777 jdwp:7051
[email protected]:~/code/wordpress-android/2.0.5$ jdb -sourcepath src -attach localhost:7777
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
>
Now what? The application is running, there’s nothing to debug. By default jdb will not set any breakpoints. It will break on Exceptions only. In order to add breakpoints you issue stop in and stop at commands, the former will break on entering a method, while the latter will break on a specific line in a class:

> stop in org.wordpress.android.AddAccount.onCreate
Set breakpoint org.wordpress.android.AddAccount.onCreate
>
Breakpoint hit: "thread=<1> main", org.wordpress.android.AddAccount.onCreate(), line=67 bci=2
67 super.onCreate(savedInstanceState);

<1> main[1] list
63 private int blogCtr = 0;
64 public ArrayList<CharSequence> aBlogNames = new ArrayList<CharSequence>();
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 => super.onCreate(savedInstanceState);
68 setContentView(R.layout.add_account);
69
70 this.setTitle("WordPress - " + getResources().getText(R.string.add_account));
71
72 if (WordPress.wpDB == null)
<1> main[1]
I’ve set the breakpoint on the onCreate method of the AddAccount Activity class. The breakpoint was hit when I tapped “Add self-hosted WordPress blog”. The list command gives us the source listing. We can advance by issuing step (execute current line, step into), next (step over), step up (step until current method returns). Remember you can issue help anytime for a quick look at the available commands.

I order to automate breakpoint position settings, especially if you’d like to set a breakpoint in the onCreate method of the main Activity, you’ll have to use either .jdbrc or jdb.ini files. You can create these in the directory you’re launching jdb from. These files can contain any valid jdb commands. These will be executed upon attaching.

In order to freeze the application when launching, use the android.os.Debug.waitForDebugger() method. The application will freeze and wait for a debugger to attach itself before continuing.

ddms
The Dalvik Debug Monitor Server allows you do quite a lot of neat stuff, like take screenshots from Android device. The ddms will also forward JDWP ports to local TCP ports automatically.

logcat
logcat is another very important command line tool. It shows you debug output from all applications. Highly useful to view tracedumps before positioning breakpoints. Although jdb should break on caught Exceptions.

Conclusion
If you’re racing against time, jdb won’t help you get things done quickly. But know that it’s there when you want to get more intimate with your application and the Dalvik VM. There are lot of visual JDWP-compliant debuggers out there, which can be used if you don’t want to install a whole IDE just for quick debugging, like me. One JDWP-compliant debugger I like and use when I’m racing against time is JSwat.

If you have any debugging tips and tricks you’d like to share with the rest of us I wholeheartedly encourage you to do so, using the comments below.

时间: 2024-10-11 04:22:38

Command Line Android Application Debugging的相关文章

Android Studio集成SVN报错:can&#39;t use subversion command line client : svn

Android Studio集成SVN插件,check out出代码后,每次开启都会在右上角出现如下错误: Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it. Errors found while svn working copies detection. Fix it. 下面直接上解决方案吧: 1.安装客户端http://ncu.dl

How to build .apk file from command line(转)

How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and

Chrome-Console( Command Line API Reference)

来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Command Line API contains a collection of convenience functions for performing common tasks: selecting and inspecting DOM elements, displaying data in read

Android application testing with the Android test framework

目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional tests 3 JUnit 3 4 Running tests on a server without display Test hooks into the Android framework 1 Instrumentation 2 How the Android system executes

Android Application Fundamentals——Android应用程序基础知识

Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvider Intent In this document--在这篇文章中 Application Components--应用程序组件 Activating components: intents--激活组件:意图 Shutting down components--关闭组件 The manifest

How To use RHEVM Command Line?

本文简单的描述下如何连接rhev shell以及简单的使用.关于更详细的用法请参考官方文档. 1.如何连接到rhevm? 要想连接到rhevm,必须拥有一个有效的证书.此证书一般安装完rhevm后会自动产生.下面是如何获取到证书. [[email protected] ~]# wget -O rhevm.cer http://rhevm.xzxj.edu.cn/ca.crt --2014-05-02 09:29:34-- http://rhevm.xzxj.edu.cn/ca.crt Resol

【转载】Data Science at the Command Line

Data Science at the Command Line Data Science at the Command Line is a new book written by Jeroen Janssens. This website contains information about the upcoming workshop in London, the webcast from August 20th, instructions on how to install the Data

Command Line Skills

Part 1: Command Line Interface(CLI) The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer kn

Xcode Command Line Tool 介绍

Xcode Command Line Tool 1. MAC Yosemite安装Xcode Command Line Tool 确保已经安装有git,先打开Xcode,file->project->os x application -> Command Line Tool,如果已经存在,就不用安装了. 如果没有,就安装:在终端输入xcode-select --install, 就会弹出相应的弹出窗口,选择安装即可,大概3分钟就可以安装完成了. 2.介绍 类似于windows的控制台应用