liveBook · Manning

Debugger

As explained in Troubleshooting Java: Read, debug, and optimize JVM applications

A software tool that lets a developer inspect and control a program's execution to examine its behavior or diagnose faults.

A debugger helps you understand what a program actually does. It pauses execution at selected lines, lets you run instructions manually, and shows how data changes. It is useful for finding faults and understanding unfamiliar logic, but it is only one of several investigation techniques. section 1.1 section 2.0

A practical debugging workflow:

  • Read the code first and identify the earliest instruction you need to understand. Set a breakpoint there, then run the app with the debugger. section 2.2
  • When execution pauses, inspect the variables in scope and use the execution stack to see where you are and how you got there. Evaluate expressions or use watches to explore values. section 2.2
  • Step over an instruction to continue in the current method; step into it when you need to examine its details. Prefer stepping over when possible, so the investigation stays focused. section 2.4
  • After stepping into a method, read its code. If it is clear, step out; otherwise, identify the first unclear instruction and investigate from there. section 2.4

Investigating a decoder

A Java decoder loops through strings, extracts digits from each one, and adds them to a total. If the extractor’s behavior is unclear, place a breakpoint on the line that constructs it and calls extractDigits(). Run the app, inspect the current input and variables, and step into the call if you need to see how it works. Compare what you observe with what you expected rather than relying on the method name alone. section 2.1 section 2.2

Listing 2.2 A piece of code we want to understand
public class Decoder {
 
  public Integer decode(List<String> input) {
    int total = 0;
    for (String s : input) {
      var digits = new StringDigitExtractor(s).extractDigits();
      total += digits.stream().collect(Collectors.summingInt(i -> i));
    }
 
    return total;
  }
}

Use the decoder listing to locate the extraction call and follow how its result contributes to the total. section 2.2

A debugger works best once you know where to start. If the relevant logic is hidden in a large app, a profiler can help identify code in execution before you choose a breakpoint. Also, pausing a multithreaded app can change its behavior, making the original problem harder to observe. Combine techniques when a debugger alone does not reveal the cause. section 1.2.1 section 2.2

How Troubleshooting Java: Read, debug, and optimize JVM applications covers it

How deep it goes
Walks through breakpoints, variable inspection, expression evaluation, and stepping; assumes the relevant code has already been identified.
Examples
Uses a Java decoder that extracts and totals digits, alongside a scenario involving missing database records.
What it assumes
Assumes language fundamentals and an identified starting point, while introducing basic debugger navigation and inspection.
What you can do after
Diagnose or understand code behavior by setting breakpoints, inspecting values, and choosing how to step through execution.
How it connects
Connects breakpoints and execution stacks with profiling and multithreading as complementary concerns in code investigation.

Also explained in

Compare how 8 books explain debugger →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java: Read, debug, and optimize JVM applications. The section links point to the parts of the book it draws on.