Sunday, April 07, 2013

Rooting & Unrooting Micromax Funbook Pro

Well, here is another rooting guide from me. This time it's on Funbook Pro P500, a 10 inch tablet from the Indian manufacturer Micromax. I am not going to talk much about the tablet here, there are several reviews on it across the internet and a good Google search could land several results on this. All I can say about it is that I have been using it over the past 6-7 months and it has been an awesome tablet at its price point.

Before I started writing this blogpost I noticed that there are several articles on the internet emphasizing on rooting Funbook Pro, but most of them focus of either using an application or a tool to get this done. However in this post, I am going to use only the Android Debug Bridge (ADB) keeping you in control of the process.

In one of my previous posts (Rooting & Unrooting HCL Me X1), I have already mentioned the advantages and risks of Rooting, please do go through it before proceeding further. Let me reiterate one point from that post - Trying to root your device might brick the device. I am not responsible if you brick your device, so proceed at your own risk.

Rooting the Device

You don't really require any exploit or any application to root the Funbook Pro, it is possible to root the device using the adb shell so make sure you have the Android Debug Bridge (ADB) on your system.

Connect the device through the USB and open the shell using adb shell at the command prompt. You would be seeing a $ here indicating that the shell does not have root access. Exit from the adb shell and restart the adb on the device using adb root, a message stating that restarting adbd as root would appear. Go back to adb shell, you would see a # now indicating that the shell has root access.

Inspecting the property either using getprop or from /default.prop would return a value of 1. This indicates that the adb will not login as root by default and hence we have to use the adb root command when we want root access using the adb shell.

Though the adb shell has root access, the su command which is used in Linux doesn't work in Android since there is no su binary in Android. So our next step is to setup the su command on the device. Download the su binary and the Superuser application from here.

There are two steps for rooting the device -

  1. Copying the su binary to the /system/bin directory

Unlike most other devices, the /system file system in Micromax Funbook Pro isn't read-only making things simpler for us. Use adb push and copy the su binary to the device with the command adb push su /system/bin. Don't forget to set the permissions of the binary using chmod 4777 su.

  1. Install the Superuser.apk from the system/app directory of the zip

Install the application using adb install or transfer the file to the sdcard and install it from there. The Superuser application intercepts the calls made to the su binary and informs the user about the usage so that unauthorized applications cannot attain root access.

Unrooting the Device

To unroot the device, uninstall the Superuser.apk and delete the su binary from the /system/bin directory.

Sunday, March 31, 2013

jQuery getScript, FireBug And Caching

Recently in one of my projects I had a requirement of loading JavaScript files dynamically. It was then I took a look at the jQuery getScript method. At first it looked perfect for my requirement, however as I started using it for my project I realized there were two drawbacks with its default implementation.

  1. There isn't any caching for the scripts loaded by getScript
  2. Debugging these scripts in FireBug is quite difficult since FireBug cannot map the script code to the script filename

In this post, I am going emphasize on my experiences with getScript and how I got both it to work with both these requirements. I am sure people who are spending time to get these features working will find this useful.

To help me with this, I put up an example Visual Studio project available here. I used Visual Studio for comfort but any text editor will work since it is all about HTML and JavaScript.

Basic Example

Well, getScript is pretty easy to use. In its simplest form, all we have to do is provide it with the script name. Here's an example on how it looks.

But the interesting point is the way it works. Here's a screenshot in FireBug which show three calls being made to the server since getScript appends a random number to the server call causing the browser cache to fail. And the script debugger is also unable to recognize the script name.

Enabling Caching

Solving the caching issue was simple. Following the recommendation on the jQuery getScript documentation page, I setup AJAX caching using ajaxSetup as show below.

This worked well but the only problem that I noticed was that ajaxSetup was a global option and setting it there would make all AJAX calls cached which could mean trouble for the regular data calls we make. Fortunately the documentation page also had a solution for this. I wrote a new jQuery method using that solution as shown below.

Making getScript FireBug Compatible

At first I had had no clue on why this was happening. But thanks to the guys at TechMonks who wrote an absolutely brilliant article on this problem, I could get this fixed. In short the problem was because of how jQuery implements getScript wherein it uses AJAX to the script content and makes it inline. Do read the entire article here for more details.

Here is a code snippet which make getScript compatible with FireBug. The reason why this works is because it loads the JavaScripts files as external resources instead of making it inline and FireBug shows them in the debugging panel.

Internal Caching

The new firebugGetScript from TechMonks works great with FireBug and also facilitates browser caching. However I had one more issue with it. My web application was completely dynamic and had to load a lot of scripts on the fly. Because of this there were scenarios where I was calling the same script through firebugGetScript. However the method wasn't smart enough to recognize this and added the script element multiple times.

I added the ability to recognize this and changed the code to the following.

And things looked perfect with this. I have been using this for quite some time in two different web applications and it's working as expected.

That's it for this post, hope the next time isn't as far away as this ;)

Saturday, June 30, 2012

Dynamic Expression Evaluation In .NET

Many applications need to evaluate expressions at run-time. This can be done using a dynamic expression evaluator. Examples of such applications are data analysis applications that perform calculations from user-defined expressions, spreadsheet software like Excel which allows users to define formulae, etc.

There are several ways of doing it in .NET, each one having its own pros and cons. This post covers the various approaches and at the end compares their performance along with code examples for each one of them. These approaches can be categorized as follows


One of the most common ways of achieving this is by writing a parser. The parser can parse the input expression, generate data structures and eventually evaluate the sub-expressions to find the result of the expression.

Developers can either write their parser or use one of the already available parsers. The biggest challenge with developers writing their own parser is that building a parser is quite complicated and requires a lot of coding effort. The advantage of using parsers for expression evaluation is that it doesn't require any external processes and the implementation can be pure managed .NET code.

Here is an expression evaluator from Code Project written in 100% Managed .NET. As the article points out, the expression goes through various phases like tokenization, parsing and interpretation before the final result is obtained. Fast Lightweight Expression Evaluator (Flee) is another expression evaluator from CodePlex. It uses a custom compiler, strongly-typed expression language and lightweight codegen to compile expressions directly to IL.

Runtime Compilation

Instead of building a custom parser, another alternative is using the .NET compilers. They can parse expressions in all the .NET languages and generate IL code. The best thing about compiled code is that execution of the code is quite fast but on the other hand it takes a significant time to compile the code.

The .NET framework includes a mechanism called the Code Document Object Model (CodeDOM) for this requirement. The System.CodeDom.Compiler namespace defines types for generating source code from CodeDOM graphs and managing the compilation of source code in supported languages. The .NET framework includes code generators and code compilers for C#, JScript and Visual Basic. Here is a tutorial on how CodeDom can be used for compiling C# code.

CodeDOM uses the compiler to compile the code, so it expects the compiler to exist on the clients system. The problem with CodeDom is that a new assembly is created and loaded into the AppDomain; and since we cannot unload an assembly in .NET, we end up with several unnecessary assemblies in memory. There are two solutions for this

  • Another temporary AppDomain can be created where the assemblies are loaded and the AppDomain can be disposed at a later time
  • Using DynamicMethod to create a method at runtime. The IL code for this can be obtained from the CodeDOM. Here is a tutorial for this

The Eval Function

Eval is a function which evaluates a string as though it was an expression and returns a result. Most of the languages that support eval are scripting languages like JavaScript, Python, etc. This is because most of the scripting languages are interpreted, not compiled. This provides these languages enormous capability to evaluate dynamic expressions since an eval call is nothing but another line to interpret at runtime. Compiled languages like Java and the .NET languages (like C# and VB.NET) don't have this flexibility since they have to generate IL/Byte code before they run. However some of the .NET language do support eval even though they are compiled.

JScript.NET is Microsoft's implementation of ECMAScript in the .NET initiative. JScript.NET contains an eval function which can evaluate dynamic expression. It compiles the code every time eval called and then runs it, so there definitely is a performance hit. The downside of eval has been explained in this article.

IronPython and IronRuby are the .NET implementations of the Python and Ruby scripting languages. These are dynamic scripting languages unlike C# and VB.NET which run on the Dynamic Language Runtime. With the DLR, several dynamic constructs like eval could be implemented and since it is based on .NET, it is interoperable with C# and VB.NET. Here is a tutorial on embedding IronPython in C#.

Microsoft Roslyn

Microsoft Roslyn is Microsoft's biggest change in compilers since the advent of .NET. However this is currently in CTP and isn't available in .NET 4.0. Roslyn provides the compiler as a service (library). This is better than CodeDOM since it won't create new processes. Roslyn has wonderful features especially for Dynamic Compilations, Code Reviewers, etc. Here is a tutorial on using Roslyn with C#.

The performance benchmarks of these approaches are as follows. The code used to obtain these benchmarks is available here.

Sunday, November 27, 2011

Backup & Restore HCL Me X1

If you are planning to experiment with any Android phone/tablet, the first thing you would like to do is take a backup of it just in case things get messy. Typically in such situations you would like to take a backup of not only the user-data but also the operating system.

In this post I will be discussing on how to create a backup and restore the backup. You don't need to root your device for this since adb already has root access in HCL Me X1. But you will need fastboot which can be obtained by compiling the Android source code.

Remember that the source code can be built only on a Linux distribution, so make sure you have one (I would recommend Ubuntu 11.10). fastboot will be available at <source_code_dir>/out/host/linux-x86/bin/fastboot after the compilation. If you don't want to compile the source, you can try a compiled version of fastboot from the link specified at the end of the post; I built it on Ubuntu 11.10.

Replacing the Recovery Image

Before replacing the recovery image, create a backup of it in case you want to revert it back. Open the shell of the device using adb shell. Find the mount-point of the recovery image using cat /proc/mtd. There will be an entry like - mtd6: 00a00000 00200000 "recovery"

The recovery image can be created by reading the contents of the recovery mount-point and writing it into a file. This can be done using the following command - cat /dev/mtd/mtd6 > /sdcard/recovery.img

The recovery image is written to the sdcard in this case, you can redirect it anywhere you want to. The recovery image can be flashed back using fastboot. For this you have to boot the device into the bootloader using adb reboot-bootloader. The image can be flashed using the following command - fastboot flash recovery recovery.img

You can backup other mount points like system, userdata, etc. using this method but it's a lot easier to do so using the ClockworkMod's Recovery Image. You can download it from the link specified at the end of the post. Flash it using the same fastboot command as above.

Creating a Backup

Reboot the device into the ClockworkMod's recovery mode using adb reboot recovery. This console has options to backup and restore the operating system. However it does this using the sdcard, so make sure you have one in the device before you proceed.

From the recovery console, select Backup and Restore > Backup. After a series of messages the ROM will be backed up.

Restoring a Backup

From the recovery console, select Backup and Restore > Restore. Select the backup you want to restore. After a series of messages the ROM will be restored.

In case you are looking for the stock ROM you can find it here. It's not the exact stock ROM but an unrooted factory reseted ROM. And the original recovery image, ClockworkMod's recovery image and the compiled version of fastboot is available here.

Saturday, November 19, 2011

Rooting & Unrooting HCL Me X1

Android is primarily an operating system based on the Linux kernel. So similar to other Linux based operating systems Android has a special user account known as the root or super-user. Unlike other user accounts which have limited control over the operating system this account has complete access to the system.

However several (almost every) carriers and manufacturers don't allow users to access this account mainly due to security concerns. Few carriers and manufacturers also do this because they don't want users to access features for free when they can sell them :). Rooting is a process that allows users of phones/tablets to gain this privileged control (the root access). Here is an article with the Top 10 Reasons To Root Your Android Phone.

Though rooting your device is not illegal it might void the warranty of the device. So think twice (make that trice) before rooting your device and make sure you have a good reason to do so. Read these articles on The dangers of rooting your Android phone and What is Rooting on Android? The Advantages and Disadvantages

The process of rooting a device changes from device to device. It usually involves using an exploit to either gain temporary root access or to flash a custom recovery image to attain permanent root access. There are popular exploits like GingerBreak, psneuter, etc. and applications like GingerBreak.apk, SuperOneClick, etc. for rooting a device.

If you have read the articles and still want to go ahead let me remind you once again that trying to root your device might brick the device. I am not responsible if you brick your device, so proceed at your own risk.

Rooting the Device

You don't require any exploit or any application to root HCL Me X1 and in-fact vulnerabilities like GingerBreak have already been patched. However it is possible to root the device using the adb shell so make sure you have the Android Debug Bridge (ADB) on your system.

Connect the device through the USB and open the shell using adb shell at the command prompt. If you are seeing the # here then that indicates that your adb shell has root access. Instead if you are seeing a $ this process won't work for your device. Please put in a comment if you see $ so that me and the other readers know that HCL has changed something.

Typically in most of the devices the adb shell does not have root access. However, looks like the Android build of Me X1 isn't a production build. Not sure if this was done knowingly or unknowingly by HCL but either way the tablet has root access through the adb shell. You can confirm this by trying one of these -

  • Type adb root at the command prompt, it will show you a message stating that adbd is already running as root
  • Type getprop at the adb shell, it shows 0
  • The property can also be examined from the /default.prop. If this is 0 then it indicates that the adb will login as root by default. Note that this file cannot be changed since it's a part of the ramdisk

Though the adb shell has root access, the su command which is used in Linux doesn't work in Android since there is no su binary in Android. Download the su binary and the Superuser application from here (backup).

There are two steps for rooting the device -

  1. Copying the su binary to the /system/bin directory

Since this directory is on the Android PATH, applications will be able to call su directly. However since the /system is a read-only file system (type mount at the adb shell to check this), the file cannot be directly copied. Push the su binary from the system/bin directory in the zip to a temporary folder of the device (like /data/local/tmp) using adb push. However note that the su binary does not work directly from this directory since applications on the /data file system do not have the setuid bit on. Remount the /system with read-write capabilities with the mount -o remount -rw -t yaffs2 /dev/block/mtdblock2 /system command (check the actual device mount-point using mount). Copy the su binary to /system/bin and set the permissions of the binary using chmod 4777 su.

  1. Install the Superuser.apk from the system/app directory of the zip

Install the application using adb install or transfer the file to the sdcard and install it from there. The Superuser application intercepts the calls made to the su binary and informs the user about the usage so that unauthorized applications cannot attain root access.

Unrooting the Device

To unroot the device, uninstall the Superuser.apk and delete the su binary from the /system/bin directory.

Friday, November 04, 2011

A Primer To Android Development

As most of you know, Android is a mobile operating system initially developed by Android Inc. and presently being developed by the Open Handset Alliance led by Google. Android consists of a modified Linux kernel and software built on the Apache Harmony framework (an open source Java implementation). The best thing I like about the Android platform is its versioning. The code names are released in alphabetic order of dessert items - Cupcake, Donut, Éclair, Froyo, Gingerbread, Ice Cream Sandwich and Jelly Bean.

I am a newbie myself when it comes to Android, so the past few days I was trying to understand the various development models of Android. During my search I came across several good tutorials. This blog post is primarily to consolidate these tutorials for beginners like me.

Android, like most open source Operating Systems allows developers to look at development from two different perspectives - working on Android (Application development) and working with Android (Operating System development, not possible with proprietary Operating Systems). Google understands this and therefore has two different sites for Android developers - Android Developers (has the Android SDK to build applications) and Android Open Source Project (has the Android source code).

Android Developers

The Android Developers site provides various Development Kits to create applications for the Android platform. The Android SDK allows developers to write applications for the Android platform in a slightly modified version of Java which runs on a virtual machine called Dalvik.

The Android SDK can be installed by following the instructions specified here - Installing the SDK. And in case you want to start programming right away, here's a classic Hello World tutorial.

The Java APIs are pretty much what most developers use. However there might be situations where you would like to harness the true potential of the underlying Linux kernel through a low level language like C. Android does allow developers to do that but they cannot call the C binary directly as an application, they will need to go through Java with the JNI (Java Native Interface). This methodology of development is supported by the NDK (Native Development Kit).

The Android NDK can be installed by following the instructions specified here - Installing the NDK. The NDK does come with the Hello World example but it isn't as easy as the regular one.

The most useful tool Android provides developers with is the Android Debug Bridge. It is a command line utility that supports several commands like pulling/pushing files to and fro the device, installing applications, etc. This is one tool that every Android developer will have to use irrespective of whether he is developing applications or playing with the source code. Please note that the adb is part of "Platform Tools" which can be installed through the Android SDK Manager. If you stuck trying to install adb, read this.

Android Open Source Project

The Android Open Source Project site provides access to the source code of Android. A majority of the operating system infrastructure of Android is distributed under the Apache License and the core kernel is licensed under the GPLv2 License.

Certain developers use the Android Open Source Project to find vulnerabilities in the Android operating system and exploit them to execute tasks which cannot be done normally like attaining root access. Examples of these exploits are GingerBreak, psneuter, etc. Typically these vulnerabilities are patched for security reasons.

A majority of developers use the Android Open Source Project to modify and customize the operating system to create custom images. Several vendors add device drivers for their hardware and install pre-release applications to the operating system before flashing it onto the device. There have also been situations wherein vendors have customized the UI to suit their product lines before flashing.

You can obtain the complete source of the Android operating system by Downloading the Source Tree. There is an awesome tutorial on building Android 2.3.7 on Ubuntu 11.10 (64 bit) here. Even though the AOSP site specifies instructions on compiling the code, they are a bit outdated so visit the above link if you are stuck anywhere.

Underneath the layers of application frameworks, Android is primarily a Linux kernel and therefore is built on a C library. Unlike the traditional Linux based operating systems, Android uses a custom tiny libc called bionic. C programs can be written in an Linux environment running on x86/x64 architectures and cross compiled to Android running on ARM by linking Bionic to gcc. Here is an excellent tutorial which shows you exactly that. The compiled code can be run using adb shell. However note that codes compiled this way aren't recommended to be used with Java applications, the NDK should be used instead.

Sunday, October 30, 2011

HCL Me X1 - A Customers Review

HCL Me X1 is HCL Technologies latest tablet and is one of the cheapest tablets available in India priced at Rs. 10,490. HCL released the X1 on October 14th. This is HCLs third tablet in the Me Series after the AE7-A1 and AM7-A1. I ordered the tablet on 19th through the HCL Store and received it this Tuesday. The entire week I have been toying with the tablet and here are my initial impressions on the Me X1.

This is my first Android tablet (my first Android device in fact) and one of the main reasons I bought the tab was to put up with the Android craze and have my share of fun with the Android platform. So expect to see several posts on the Android platform and the HCL Me X1 on this blog :).

I have been researching Android budget tablets for quite some time and finally decided to get myself the Me X1 (The Lenovo IdeaPad A1 was another option but decided to go with Me X1 after Lenovo priced it at Rs. 15000). Before going deeper into the review let me start by saying that the HCL Me X1 is a good tablet from a price to performance perspective and does a decent job in gaming, music and movies.

The specifications of the HCL Me X1 can be found here. Though HCL hasn't specified the actual Application Processor they are using, it is expected to be the Marvell ARMADA 610.

As evident from the above specifications, the HCL Me X1 doesn't have a SIM card slot and so doesn't support 3G directly. However it does support 3G through a USB dongle. This wasn't much of an issue for me since I was planning to use my Nokia X6 as a WiFi hotspot (haven't tried this yet). The touch responsiveness of the device is good and Gingerbread performs well on the device thanks to the 1 GHz processor.

However the HCL Me X1 does have its shortcomings,

  • A major drawback of the Me X1 is that it doesn't have the Android Market on it. Whatever HCL may say, its App Store is no match to the Android Market
  • The device works for around 6-8 hours (usage + standby) with a single charge. I wasn't satisfied with this as my Nokia X6 gives me much more than this. However benchmarking a tablet to a phone isn't correct
  • The display would have been better if it was a slightly brighter

The biggest weakness of HCL is their delivery service. When most online sites ship within 2-3 days, HCL as a policy takes a minimum of 7 to 10 days to ship the product. However they sent me the devices in 7 days when I pestered them a lot. In fact I would recommend people to buy the device at a local Sangeetha store where you can get it immediately. On the other hand their customer care was good, especially the Live Chat, they answered a lot of questions and followed up on phone as well.

I think the biggest technical improvement opportunities the Me X1 has are the integration of the Android Market and getting out an Ice Cream Sandwich as soon as possible. This should attract more customers towards the product.

Before I wrap up, here are some excellent reviews on the tablet which helped me take a decision on the purchase -

Update 2011/11/10

After several discussions at arpandeb, we finally concluded that the actual Application Processor being used is the Telechips TCC 88xx. We also found striking similarities between HCL Me X1 and Coby Kyros MID7022.

Sunday, October 16, 2011

Named Groups In Regular Expressions - Java 7

One of the new features added in Java 7 was the introduction of named groups in regular expressions through the java.util.regex package. This article covers the various features of named groups in regular expressions and their syntax in Java.

Parts of a Regular Expression can be grouped together by placing them inside round brackets. A major advantage of grouping is that various regex operators can be applied to these groups. Grouping is also useful for back-referencing a match. This allows developers to write regular expressions involving complex repetition patterns more easily.

The value of the captured group can be retrieved directly or be referenced in replacement patterns for changing the format of an input string. Though Java supported Regular Expressions and Grouping through the java.util.regex package from Java 1.4, it was limited to numbered groups.

Here's an example showing how numbered groups can be used in Java. To read more about Grouping and Backreferences, check this out.

Parallel to numbered groups exists named groups. Named groups do not change the underlying concept of grouping and back-referencing but provide more readability to the regular expressions and make back-referencing easier for developers since remembering names is easier than remembering the relative position of a group.

Named groups are already present in languages like Perl, Python and .NET but wasn't available to Java prior to Java 7. Well, better late than never.

The newly added RegEx constructs in Java 7 are as follows -

  • (?) defines a named group "name"
  • \\k back-references a named group "name"
  • ${name} can be used to reference a captured group in a replacement string
  • group(String name) returns the value of the captured group

Here's an example showing how named groups can be used in Java 7. To read more about Named Groups, check this out.

However if you are using Java 7, I would strongly recommend using the java.util.regex package but if you are using a Java version older than 7, there are other alternatives available for named groups in Java like Named-RegExp and JRegex. These alternatives were highly recommended by other developers at stack overflow.

Thursday, September 01, 2011

DC Comics Reboots The Universe

Today's a big day in the DC Universe. For people wondering what DC Comics is - DC Comics Inc. is a subsidiary of Warner Bros. and is one of the largest and most successful companies operating in the comic books industry. It introduced a majority of the popular superheroes we have grown up with like Superman, Batman, Green Lantern, Catwoman, etc.

Effective this month, their entire comic series has taken a major overhaul and all DC Universe titles being cancelled or restarted at #1. Titles which have been around for more than 70 years like Action Comics and Detective Comics, each having around 900 issues credited to them are also being rebooted making it a very big thing in the comic world.

The DC Comics Reboot (AKA The New 52) has been termed as the biggest thing to happen to comic books since Crisis On Infinite Earths (which happened 25 years ago, before I was born :P). This revamp is going to affect not only the storylines but also the way books are being marketed.

Things are changing a lot in the comic world - print is dying, comic book sales are going down year after year and the revenue streams for comic book writers are going dry. With movies like The Dark Knight, X-Men, Superman, etc. making big impressions at the box office, their sources of inspiration going down would be seem very strange but it's true.

I personally would attribute to two major reasons -

  • Complex Story Lines - Movies aren't driving comic sales since movies are far far behind the comic series. Today a newbie can understand The Dark Knight or play Batman - Arkham Asylum but when they grab a copy of Batman comic off the shelf they seem out of sync
  • Digital Media - Complex Story Lines are a problem but a far bigger problem is the influence of digital media on the world. People today are far more addicted to sites like Facebook, Twitter, Youtube, etc. and are hooked to devices like iPads, Nooks, Kindles, etc. than reading a physical book. Gone are the good old days when we visited a store to grab a book; we today either download an ebook or wait the movie to come out ;). I don't blame anyone but this is price which we have to pay for embracing technology. The bigger point is that organizations have to understand this and adapt to it

The DC Reboot primarily addresses both these issues; a reboot means that things start from beginning and today's generation can understand the storyline from scratch without having to dig back for older books. On the darker side, this will certainly have a mixed reaction from the existing collector base which has to be dealt carefully.

Also every DC Comics book from here on out will be digitally distributed "day and date". This means that on the same day individual issues hit the shelves of the comic stores, they will also be available to read digitally. The digital distribution of the comic books is something which I liked a lot but I can foresee a major problem of piracy :(. But still it's a great move and if the pricing is maintained properly for people around the globe, it can create a huge impact for the comic world.

For those of you wondering about character origins and histories, it's still unclear what's going to happen - a few of them may be left untouched while the others may see a major change. For instance I didn't see the Martian Manhunter in the Justice League #1.

This move will definitely stir up some hype and I can't wait to see how Marvel responds to this :). Either way, this year's Comic Cons are going to have a lot to talk about and this move will surely have an impact on the future of the comic book industry.

Check out the trailer of DC 52 below -

Check out these cool articles on the DC Reboot -

Thursday, July 28, 2011

Remote Debugging Java Applications

After my previous article about Remote Debugging .NET Applications using Visual Studio 2010, I was curious to see how Remote Debugging works for Java Applications. This blog post covers Remote Debugging Java Applications using NetBeans 7.0.

The basic concepts of Remote Debugging are the same for .NET and Java but the process of setting up the host and remote computers varies between Visual Studio and NetBeans. Similar to the .NET application used in the previous post, the screen shots correspond to a simple Java application which would popup a MessageDialog on a button click. Get the code here.

Before configuring the host and remote computers, it is vital to understand the Java Platform Debugger Architecture (JPDA). JPDA provides the infrastructure you need to build end-user debugger applications for the Java Platform. It includes the following APIs broken into three layers -

  • Java Debug Interface (JDI), a high-level Java programming language interface including support for remote debugging
  • Java Debug Wire Protocol (JDWP), which defines the format of information and requests transferred between the process being debugged and the debugger front end
  • JVM Tools Interface (JVM TI), which is a low-level native interface that defines the services a JVM provides for tools such debuggers and profilers

Configuring the Remote Computer

Run the Java application using the -Xdebug and -Xrunjdwp options from the command line.

java -Xdebug -Xrunjdwp:transport=dt_socket,address=6000,server=y -jar RemoteDebugging.jar

Here's a description of the options of the java command -

-Xdebug Enables debugging support in the VM
-Xrunjdwp Loads in-process debugging libraries and specifies the kind of connection to be made

The -Xrunjdwp option has several sub options. Here are the descriptions of the ones that are used above -

transport Name of the transport to user in connecting to debugger application
address Transport address for the connection
If server=n, attempt to attach to debugger application at this address
If server=y, listen for a connection at this address
server If y, listen for a debugger application to attach
If n, attach to the debugger application at the specified address

By default the application starts in suspended mode. In suspended mode the application waits for a debugger to attach itself to the server at the specified port before the application starts.

Configuring the Host Computer

The host computer is the system running NetBeans 7.0. Open the code of the application in NetBeans and select "Attach Debugger". Specify the Connector as SocketAttach, the Host as the hostname of the remote system and the Transport and Port as specified above.

Specify the breakpoints in the code and they would hit appropriately. There are two major bottlenecks in Remote Debugging -

  • The code from which the executable was built should be available at the time of debugging
  • Applications cannot be configured for Remote Debugging at runtime. The -Xdebug option must be specified at the instantiation of the application, making debugging live production code difficult

Before I conclude, here's an article from OTN (Oracle Technology Network) on the Java Platform Debugger Architecture. Visit it if you would like a deeper insight into Java Debugging. The schematic of the JPDA is from a weblog, check it out here.