Overview

3 Finding problem root causes using advanced debugging techniques

Many developers rely on basic stepping commands when debugging, but this chapter shows that a debugger can do much more to speed up root-cause analysis. It introduces several advanced techniques that help you focus on the right execution path, avoid unnecessary stepping, and better understand how a program behaves under specific conditions.

One major theme is using conditional breakpoints to stop only when a particular state occurs, such as when a variable matches a target value or an unexpected result appears. The chapter also explains how breakpoints can be configured to log information instead of suspending execution, which is especially useful for collecting variable values and stack traces in scenarios where pausing would be impractical or disruptive.

Another useful technique is changing variable values on the fly to simulate cases that are hard to reproduce, slow to reach, or unavailable in the current environment. Finally, the chapter covers dropping execution frames, which lets you revisit earlier points in a call stack and replay code paths, while warning that this does not undo side effects outside the JVM, such as database writes, file creation, external calls, or sent messages. Together, these methods give developers more flexible ways to investigate bugs and validate suspicions efficiently.

Using a conditional breakpoint to pause the execution just for specific cases. In this figure, we want to pause the execution on line 14 only if sum is zero. We can apply a condition on the breakpoint that instructs the debugger to consider that breakpoint only if the given state is true. This helps you more quickly get to a scenario you want to investigate.
A conditional breakpoint. Line 14 in the figure was executed multiple times, but the debugger only paused the execution when the variable sum was zero. This way, we skipped over all the cases we were not interested in so that we can start with the conditions relevant to our investigation.
To apply advanced configuration on the breakpoint in IntelliJ, you can click the More button.
Not all IDEs offer the same debugging tools. All IDEs give you the basic operations, but some features, such as logging the execution details instead of pausing the execution, may be missing. In Eclipse, you can define conditional breakpoints, but you can’t use the logging feature.
Conditional breakpoint advanced configuration. In addition to specifying a condition for the breakpoint, you can instruct the debugger to not suspend the execution for the given breakpoint. Instead, you can simply log the data you need to understand your case.
Using breakpoints without pausing the execution. Instead, the debugger logs a message when the line has been reached. The debugger also logs the value of the digits variable and the execution stack trace.
Usually, when investigating issues in a long-running process, using breakpoints is not really an option. It can take a long time for the execution to reach the part of code you are investigating, and if you have to rerun the process several times, you will definitely spend too much time on it.
Setting a new value in a variable in scope. The debugger shows you the values for the variables in scope when it pauses the execution on a given line. You can also change the values to create a new investigation case. In some cases, this approach can help you to validate your suspicions about what the code does.
Change the variable's value to observe how the app’s execution behaves in different conditions.
When using the step over operation, the app uses the new value you set to the s variable. extractDigits() returns an empty list because string "abcd" doesn’t contain digits. Setting values in variables on the fly allows you to test different scenarios even if you don’t have the input data you need.
Stepping out closes the current investigation plan by executing the method and then pausing the execution right after the method call. This operation allows you to continue the execution and return one layer in the execution stack.
When you drop a frame, you return to the previous layer in the execution stack trace before the method call. This way, you can replay the method execution either by stepping into it again or stepping over it.
Dropping a frame versus stepping out. When you drop a frame, you return to the line before the method’s execution. When you step out, you continue the execution but close the current investigation plan (represented by the current layer in the execution stack).
When using IntelliJ IDEA, you can drop a frame by right-clicking the method’s layer in the execution stack trace and then selecting Drop Frame.
Using the drop frame operation can result in some events that can’t be undone. Examples include changing data in the database, changing data in the filesystem, calling another app, or sending an email message.

Summary

  • A conditional breakpoint is a breakpoint associated with a Boolean condition. The debugger pauses the execution only if the provided condition is true—that is, only when particular conditions apply. This way, you save the time of navigating through code until you get to the desired point to begin your investigation.
  • You can use breakpoints to log the values of certain variables in the console that don’t suspend the app’s execution. This approach is quite helpful because you can add log messages without changing the code.
  • When the debugger pauses the execution on specific lines of code, you can alter the data on the fly to create custom scenarios according to what you want to investigate. This way, you don’t have to wait until the execution gets to a conditional breakpoint. In some cases, when you don’t have an appropriate environment, changing data while debugging saves you time you would have needed to prepare the data in the environment.
  • Changing variables’ values to create a custom investigation scenario can be an efficient technique when trying to understand just a piece of the logic of a long-running process or when you don’t have the desired data in the environment where you run the app. However, changing more than one or two variable values at a time may add considerable complexity and make your investigation more challenging.
  • You can step out of an investigation plan and return to the point before the method was called. This is called dropping a frame, but it can sometimes introduce an unwanted side effect. If the app changed anything externally (e.g., committed a transaction and changed some database records, changed a file in the filesystem, or made a RESTful call to another app), returning to a previous execution step won’t undo these changes.

FAQ

What is a conditional breakpoint, and when is it most useful?A conditional breakpoint pauses execution only when a Boolean condition is true. It is most useful when you want to investigate a specific scenario, such as when a variable reaches a certain value, without stepping through every other case.
How do conditional breakpoints help reduce investigation time?They let the debugger run until the exact situation you care about appears, instead of forcing you to step through many irrelevant iterations. This is especially helpful in loops, repeated logic, or cases that are hard to reach manually.
Can breakpoints be used to log information without stopping execution?Yes. Some IDEs let you configure a breakpoint to act like a log event instead of suspending execution. This allows you to print values, stack traces, or other details to the console while the app keeps running.
Why would you use a breakpoint to log values instead of adding temporary print statements?Using a breakpoint for logging is faster and cleaner because you do not need to modify the source code. It is a convenient way to inspect values such as variables and stack traces during debugging without introducing extra code changes.
What can you learn from the execution stack trace while debugging?The execution stack trace shows how the current line of code was reached. It helps you understand the call path, identify the calling methods, and see where the investigation should continue.
When is changing a variable’s value during debugging useful?It is useful when you want to simulate a scenario you cannot easily reproduce, or when the needed input data is unavailable. It is also helpful when the code runs too quickly or only fails in an environment you cannot access directly.
How do you decide between using a conditional breakpoint and changing values on the fly?Use a conditional breakpoint when you already have the data needed to reproduce the case and the code does not take too long to reach it. Change values on the fly when you lack the exact data or when waiting for the right scenario would take too much time.
What is dropping an execution frame?Dropping an execution frame moves the debugger back to the previous stack frame, effectively rewinding the investigation one step. It lets you replay a method call and re-examine the execution from before that method ran.
How is dropping a frame different from stepping out?Stepping out continues execution until the current method returns, then pauses at the caller. Dropping a frame instead resets the debugger to the point before the method call, so you can re-run that method again from the beginning.
What are the limitations of dropping frames during debugging?Dropping a frame cannot undo changes made outside the JVM’s internal memory. For example, it will not reverse database updates, file creation, messages sent to another app or queue, or emails already sent.