Busy Java Developer's Guide to Debugging

ted@tedneward.com | Blog: http://blogs.tedneward.com | Twitter: tedneward | Github: tedneward | LinkedIn: tedneward

Objectives

"Your code broke: fix it!"

Debugging Concepts

Thinking the bugs out

Concepts

Debugging

"the art of identifying and fixing a problem after it has been spotted"

Concepts

What is a "bug"?

Or, succinctly, "Bugs are unmet expectations"

Concepts

Process bugs

Concepts

Prerequisites to effective debugging

Concepts

Attitudes of the effective debugger

Concepts

Attitudes of the effective debugger, continued

Exception discipline

How to deal with the unexpected

Exception discipline

Most languages have some concept of "exceptions"

Exception discipline

Using exceptions effectively is not simple or straightforward

Exception discipline

Exception discipline crosses many languages

Exception discpline

1: Distinguish between different kinds of exceptions

Exception discipline

Programmer failures should never be caught/handled

Exception discipline

Resource failures should never be ignored

Exception discipline

User failures are commonplace

Exception discipline

2: Layer exceptions at component boundaries

Exception discipline

3: Arrange exceptions carefully in logical groupings

Exception discipline

4: Keep exceptions relevant to the component

Exception discipline

5: Never ignore an exception

Exception discipline

6: Only throw exceptions when the caller needs to know

Exception discipline

7: Remember that exceptions represent another flow of control

Tools

JDK command-line tools

Tools

JDK command-line tools

Tools

JDK command-line tools

Tools

JVM diagnostic flags

Tools

JVM "XX" diagnostic flags

Tools

OS-specific tools

Tools

FindBugs

Tools

Diagnostic logging

Tools

Data access

JPDA

Java Platform Debugging Architecture

JPDA

Java Platform Debugging Architecture (JPDA)

JPDA

JPDA options

JPDA

agentlib:jdwp=transport=dt_socket,server=y,address=8000

Listen for a socket connection on port 8000. Suspend this VM before main class loads (suspend=y by default)

agentlib:jdwp=transport=dt_shmem,server=y,suspend=n

Choose an available shared memory transport address and print it to stdout. Listen for a shared memory connection at that address. Allow the VM to begin executing before the debugger application attaches.

JPDA

agentlib:jdwp=transport=dt_socket,server=y,address=8000,onthrow=java.io.IOException,launch=/usr/local/bin/app

Wait for an instance of java.io.IOException to be thrown in this VM. Suspend the VM (suspend=y by default). Listen for a socket connection on port 8000. Execute the following: "/usr/local/bin/debugstub dt_socket myhost:8000". This program can launch a debugger process in a separate window which will attach to this VM and begin debugging it.

JPDA

agentlib:jdwp=transport=dt_shmem,server=y,onuncaught=y,launch=d:\bin\debugstub.exe

Wait for an uncaught exception to be thrown in this VM. Suspend the VM. Select a shared memory transport address and listen for a connection at that address. Execute the following: "d:\bin\debugstub.exe dt_shmem <address>", where <address> is the selected shared memory address. This program can launch a debugger process in a separate window which will attach to this VM and begin debugging it.

JVMTI

Java Virtual Machine Tools Interface

Tools

JVMTI: Java Virtual Machine Tools Interface

JVM Resources

Online resources

Summary

Debugging is equal parts attitude, process, tools, and knowledge

Credentials

Who is this guy?