Kaolin Fire with GUD Issues 0 through 5

kaolin fire presents :: software :: tutorial :: jdb/gjdb

Debugging java with jdb and gjdb

In this tutorial, we looked at two things. How to get started on a project, and how to use the gjdb. (actually, I was using the jdb in the tutorial and didn't notice til someone pointed it out that the reader went over the gnu version. there are slight differences.)

First, I'll go over the "how to get started with a project" bit, and then I'll use that code to give you a little kickstart with the jdb/gjdb. Part of Project 2 is to do some web crawling. So... what's the first thing you're going to have to get down? (well, besides the first part of the project which is supposed to be the data structure you use in the second).

The FIRST step (in my opinion) is to open a page... any page at all... and print the results to the screen. This ought to give you some idea of what's going on with the networking. (of course, the rest is a piece of cake for you, right? *just kidding, honest*) Now this first step may seem hard to some of you, and juvenile to others, but in any case it is a chunk, and it's a chunk you can do.

Or at least, with my tutorial's help we whipped it out in about 5 minutes, looking at a similar example in a book.... (note: copying other code is called "modularity" as long as you say where you got it... but if you modify it then you don't have to call it anything. :) )

    import java.io.*;
    import java.net.*;
    public class Browse {
      public static void main(String[] argv) {
        if (argv.length != 1) {
          System.out.println("usage: java Browse ");
        String dest=argv[0];
        try {
          URL url=new URL(dest);
          BufferedReader in = 
            new BufferedReader(new InputStreamReader(url.openStream()));
          String line;
          while ((line=in.readLine()) != null) {
        } catch (Exception e) {
          System.out.println("Whee! I got an exception.");
So... First... we take one agument from the command line... this is the url we are going to open... for example, http://transbay.net/~eruditio. If we're not given one argument, we do the cool thing and tell the user what they should have done, and exit gracefully.

Next, we set up out input stream with some somewhat self-explanatory wrappers. url.openStream returns an InputStream for a new socket for the port we connected at. We give that to an InputStreamReader so that we have an easier time of reading the stream, and then we give that to a BufferedReader because well, buffering is good. (if you want me to explain WHY buffering, I'll try).

What the while loop does is simply milk the input stream for any information it can, and dump it straight to the screen. The exception will most likely only happen if the URL does not exist, although there are other possibilities. Then the program will end.

Now you know how to open a URL and see what the output looks like!

And now I'll go over the gjdb a bit. If you've ever used the gdb, it's somewhat similar, but if you haven't, DON'T PANIC. (pretend there's a green smiley-face here sticking its tongue out at you) The gjdb is really snifty at telling you what your program is doing when, and it comes with the jdk.... (well, the jdb does, so, close enough, and I'll give you both versions as best as I can remember).

First, a note: REMEMBER TO COMPILE WITH -g (i.e.: javac -g Browse.java)
This makes your class files bigger, and puts debugging and line number info into them.

Another note: gjdb seems to be somewhat slower in starting... but is more "standardized" in its usage...

The easiest way to start the debugger is:

Actually, there's several ways to get started. Lots. So... we'll just start with this, and you can figure out what else works. There's not too much to it. This explanation isn't here because it's confusing, just because I figured a walkthrough would get you started looking at it.

Next, let's say we want to trace through our Browse program....

    load Browse
load Browse
And then to set where we want to "break"... Let's say, we want to see what happens when we start the main method...
    stop in Browse.main
break Browse.main
Next, to start it running... with a command line argument to make Browse actually print something meaningful... not just exit saying that we didn't use it right...
    run http://erif.org/
run http://erif.org/
This will stop at the very first line *inside* of the main function. Now, we could step through this... but that would be sorta ugly... We'd wind up stepping through lots of things we (well, *I'm* not) aren't interested in, like the methods that assign a String its value, and stuff like that. So, maybe that wasn't the best breakpoint.

Looking at the code, a really neat spot to break would be right inside the while loop... line 17, where it prints the line... because there we have access to most of what's going on.

    stop at Browse:17
 break Browse:17
Now, we want to go to the next break, running through the code in-between, but not having to stop at every command... so we 'continue'.
We're in the center of the program. Why are we here? Well, we wanted to see what's going on. What's core to what's going on? What's in the variables! So....
info locals
Now you can 'step' through it to your heart's content, looking at variables, setting breakpoints, and being a menace.


Both engines take either 'exit' or 'quit' when you're done with them.

Also, emacs supposedly has some snifty functionality with the gjdb setup... but. I don't recommend Emacs, so... well... I dunno. And the default path for it seemed to be set wrong somehow anyway.

I am soooo fake pre-loading this image so the navigation doesn't skip while loading the over state.  I know I could use the sliding doors technique to avoid this fate, but I am too lazy.