Monday, December 18, 2006

JPDA Tutorial

Introduction
JPDA stands for Java Platform Debugger Architecture. It’s a multi-layered debugging architecture which enables a tool developer to easily and effectively develop a debugger application which will run across platform, VM implementations and JDK versions.
Note: This discussion is for Java SE 5.0!

Architecture
JPDA consists of two interfaces (JVMTI and JDI), a protocol (JDWP) and two software components which tie them together (back-end and front-end). For the sake of simplicity JDWP is referred as an interface only.

Components
Debuggee
  • The debuggee is the process being debugged; it consists of the (java) application being debugged, the VM running the application and the back-end of the debugger.
  • The back-end of the debugger is responsible for communicating requests from the debugger front-end to the debuggee VM and for communicating the response to these requests (including desired events) to the front-end. The back-end communicates with the front-end over a communications channel using the Java Debug Wire Protocol (JDWP). The back-end communicates with the debuggee VM using the Java Virtual Machine Tool Interface (JVMTI).
The communications channel
  • The communications channel is the link between the front and back ends of the debugger. It consists of two mechanisms: a connector and a transport.
  • A connector is a JDI object that is the means by which a connection is established between the front and back-ends. JPDA defines three types of connectors:
    • Listening connectors: The front-end listens for an incoming connection from the back-end
    • Attaching connectors: The front-end attaches to an already running back-end.
    • Launching connectors: The front-end actually launches the java process that will run the debuggee code and the back-end.
  • A transport is the underlying mechanism used to move bits between the front-end and the back-end. The transport mechanism used is unspecified; possible mechanisms include: sockets, serial lines, and shared memory. However, the format and semantics of the serialized bit-stream flowing over the channel is specified by the Java Debug Wire Protocol (JDWP).
Debugger
  • The debugger front-end implements the high-level Java Debug Interface (JDI). The front-end uses the information from the low-level Java Debug Wire Protocol (JDWP) and gives to UI to display in user understandable and acceptable format.

Interfaces
Java Virtual Machine Tool Interface (JVMTI)
A native interface implemented by the VM. Defines the services a VM must provide for debugging. Includes requests for information (e.g. current stack frame), actions (e.g. set a breakpoint), and notification (e.g. when a breakpoint has been hit).

Java Debug Wire Protocol (JDWP)
Defines the format of information and requests transferred between the debuggee process and the debugger front-end. It does not define the transport mechanism (e.g. socket, serial line, shared memory etc). The specification of the protocol allows the debuggee and debugger front-end to run under separate VM implementations and/or on separate platforms.

Java Debug Interface (JDI)
A 100% Java interface implemented by the front-end. Defines information and requests at a user code level. While debugger implementers could directly use the Java Debug Wire Protocol (JDWP) or Java Virtual Machine Tool Interface (JVMTI), this interface greatly facilitates the integration of debugging capabilities into development environments.

How it works
Each interface provides are two types of activities: requests and events. Requests originate on the debugger side and include queries for information, setting of state changes in the remote VM/application, and setting of debugging state. Events originate on the debuggee side and denote change of state in the remote VM/application.

Example of request

  • A user clicks on a local variable in a stack view in an IDE, requesting its value. The IDE uses the JDI to get the value; in particular it calls the getValue method on the stack frame.
    E.g. stackFrame.getValue(localVariable).
    Where stackFrame is a com.sun.jdi.StackFrame and localVariable is a com.sun.jdi.LocalVariable.
  • The front-end then sends this query over a communications channel (let's say a socket) to the back-end running in the debuggee process. It sends it by formatting it into a byte stream in accordance with the JDWP. In particular, it sends a GetValues command (byte value: 1) in the StackFrame command set (byte value: 16), followed by the thread ID, frame ID, etc.
  • The back-end deciphers the byte-stream and sends the query off to the VM through the JVMTI. In particular, let's say the requested value is an integer, the following JVMTI function call is made:
    error = jvmti->GetLocalInt(frame, slot, &intValue);
  • The back-end sends back across the socket, a response packet, which will include the value of intValue, and which will be formatted according to JDWP.
  • The front-end deciphers the response packet and returns the value as the value of the getValue method call. The IDE then displays the value.
Example of event
  • The virtual machine sends an event (say break point occurred) across the JVMTI interface. In particular, it calls the event handling function passing the breakpoint.
  • The back-end has set the event handling function to be:
    static void Breakpoint(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, jmethodID method, jlocation location) { ... }
    This back-end function starts a chain of activity which filters the event to see if it is interesting, queues it, and sends it across the socket in the JDWP format defined for breakpoint events.
  • The front-end decodes and processes the event, eventually generating a JDI event. In particular, the JDI event is exposing it as a com.sun.tools.jdi.event.BreakpointEvent. The IDE then gets the event by removing it from the event queue.
    E.g. eventQueue.remove()
    Where eventQueue is a com.sun.jdi.event.EventQueue.
  • The IDE will probably update its displays by making many query calls across the JDI.
Reference Implementation
  • In the reference implementation of JPDA by Sun , JVMTI is implemented by the Java HotSpot VM and the client is the reference implementation of the back-end, supplied as a native shared library (jdwp.so, jdwp.dll), which is shipped with the JDK; which implement JVWP.
  • The reference implementation of the back-end (above) provides the debuggee side of JPDA architecture, and the reference implementation of the front-end (a Java programming language component of the JDK, located in tools.jar and jdb ) provides the debugger side; which implements JDI.

Examples
  1. Start a program which will listen for debug requests using socket as transport on port 8000. Use jdb to connect and debug it.
    C:\> java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=y Hello Listening for transport dt_socket at address: 8000
    C:\> jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000
    Set uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    Initializing jdb ...
    > VM Started: No frames on the current call stack main[1]

  2. Start a program which will listen for debug requests using shared memory as transport. Use jdb to connect and debug it.
    C:\> java -agentlib:jdwp=transport=dt_shmem,server=y,address=mymem,suspend=y Hello Listening for transport dt_shmem at address: mymem
    C:\> jdb -attach mymem Set uncaught java.lang.Throwable
    Set deferred uncaught java.lang.Throwable
    Initializing jdb ...
    VM Started: > No frames on the current call stack main[1]

  3. Launch a program using jdb and debug it.
    C:\> jdb Hello Initializing jdb ... >
References
http://java.sun.com/products/jpda/doc/index.html
http://java.sun.com/products/jpda/

Thursday, July 28, 2005

Manish’s story of small boy and his color box

This story has been told to me by Manish… I found this very helpful as a young programmer/designer

Scene1:
One day a dad gives a brand new camlin color box to his son as a gift. The small boy becomes very happy by seeing 12 fresh colors in the box and asks dad “Which picture shall I draw first?” Dad said, “Son, go ahead with my picture!” Without wasting a single minute the boy takes pencil and draws his dads picture and paints it. He was so excited that was willing to use all 12 brand new colors; so he made dads face red, one arm blue other black, shirt green, pant brown and so on…

Scene2:
Some other day some other dad gives a brand new camlin color box to his son as a gift. The small boy becomes very happy by seeing 12 fresh colors in the box and asks dad “Which picture shall I draw first?” Here again dad said, “Son, go ahead with my picture!” But this boy thinks for a while; takes pencil and draws his dads picture and paints it. He thinks on body color and create the body color shade by mixing red and some other color, paints all body parts with that color; paints blue shirt and black pant. Thus he uses only 3-4 colors from his new color box!


Moral of the story – As a programmer/designer we have a lot of technologies in front of us, but this does not mean that we have to use all these; lets think/study them, see which ones our application really demands and use those or combination of those only and be the second clever boy!

Monday, July 25, 2005

Shikha on my ‘PST’ (preetam’s standard time) timings!

It’s half past-eight in the office and the lights are still on, computers are still running, coffee machines are still buzzing. So, who’s this, still at work?
Almost all the ‘workaholic’ specimens are 20-something males of the species.
A close look reveals that almost all of them are bachelors. The question then arises, why are they sitting late?
Working hard? Not by a long shot! Any guesses?
On second thoughts, let’s hear it from the horse’s mouth.
I quote – “Arey yaar, what’s there to do after going home. Idhar to net hain, AC hain, phone hain, khaana hain, chai-coffee hain… toh jam ke khaao, jam ke piyo (burps), jam ke chatting/phone karo aur thak jaane par ghar jaao… aur boss bhi kush thinking that I am working late…(burps again) aur khaane ka paisa bhi bachtaa hain.”

This is the scene in most software companies and other off-shoring company offices. Bachelors killing time during late hours in the office just coz they’ve nothing else to do, is more the norm than the exception.

So, what is my point, you might ask. Well, the consequences of such ‘unfair practices’ can be quite drastic, especially for the other employees.

  • “Working” (for the record only) late hours soon becomes a part of the company culture.

  • With bosses more than eager to provide support to those ‘working late’ in the form of conveyance reimbursement and food vouchers, of course the much sought after pat on the back. (Oh, he’s a hard worker/sincere employee… goes home only to change!), things don’t get any better.
    It’s the rare perceptive boss who understands the difference between ‘sitting’ late and ‘working’ late!!

  • Very soon, the managers start expecting all employees to put in extra hours.

Most bachelors are completely oblivious to the fact that life changes when one gets married. Work does not remain the be-all and end-all of one’s existence and one is kept busy juggling work and family commitments.

Many bosses are not able to digest the fact that, the erstwhile ‘hardworking’ guy gets transformed into an ‘early leaver’, even when he leaves an hour beyond regular working hours, and more importantly is able to get the same amount of work done.

Employees leaving on time after doing their tasks for the day are labeled work-shirkers.

Women who, with good reason, generally (its changing now-a days… though) leave on time are labeled “not up to it”. All the while, the bachelor as snug as a bug in a rug carries on ‘working’, not realizing that he’s spoiling the work culture, which is sure to boomerang on them some time.

So, what’s the moral of the story??
  • LEAVE ON TIME!

  • Never put in extra time unless it’s really needed.


Don’t stay back un-necessarily and spoil your company work culture, which will in turn cause inconvenience to you and your colleagues. There are lots of things one can do to make good use of his leisure. You could, for instance, learn music or a foreign language; take up sporting activities; get yourself a girlfriend and take her around town; also, cyber surfing rates have dropped to an all-time low (and there are no firewalls there); try cooking for a change.

Take tip from the Smirnoff as: “Life’s calling, where are you??”

Please pass on this message to all your colleagues including those who stay back in the office for everything other than work.

And please do it before leaving time, don’t stay back till midnight to forward this!

Regards,
Shikha

Saturday, July 09, 2005

Abhi's baby

Yesterday i.e. 7th July 2005 (07-07-2005) , Abhi has given birth to a baby boy ! We called that event as Abhi's baby Release 1.0 ;-)

Here are some pics of Abhi's baby ...

a brighter one --

a close up --

Abhi, When will be the release 2.0 ??? =))

Wednesday, June 15, 2005

"Hello World" blog !

I seen my friends’ blogging on web and then I thought what this blog is? Lets give it a shot! And here is my blog … u r reading!

I was always thinking of having my own home page but till date I have not managed that L. So lets blog first!

Life moves very fast, a lot of things happen in our day-to-day routine, we laugh, shout and even cry … I think this is all Life … lets take it as it comes; its very exciting and lets make it more. I always say, “Are yar life main kuch excitement hona chahiye!” Lets share that excitement here!
I can see/feel excitement literarily in anything… let it be my bug free program, let it be a sat-sun at Deccon or even let it be a travel by PMT or my rejected BITS application!

After all somewhere I had read “Great Minds Are Open”…

What I will be posting? Anything! Starting from experiences/thoughts/fundas to tech/hot/cool stuff! (And even nothing)......