IntelliJ IDEA 2023.2 Help

Examine suspended program

After the debugger session has started, the Debug tool window will appear, and the program will run normally until one of the following happens:

After that, the program is suspended, allowing you to examine its current state, control its further execution, and test various scenarios at runtime.

Examine frames

The state of the program is represented by frames. When the program is suspended, the current frame stack is displayed on the Frames tab of the Debug tool window.

Frames tab

A frame corresponds to the active method or function call. It stores the local variables of the called method or function, its arguments, and the code context that enables expression evaluation.

Frames and threads are selected in the Frames tab

To better understand the concept of frames, let's look into what happens when a program is run. The execution of the program starts from the main method, which in turn calls other methods. Each of these methods may do more method calls. The set of local variables and parameters for each method call is represented by a frame. Each time a method is called, a new frame is added to the top of the stack. When the execution of a method is complete, the corresponding frame is removed from the stack (in the last in, first out fashion).

Examining frames helps you understand why particular parameters were passed to a method and what the state of the caller was at the time of calling.

Thread Statuses

Thread statuses are provided by Java to reflect what is currently happening to the thread in the program.

Thread status

Description

MONITOR

The thread is waiting on a Java monitor.

NOT_STARTED

The thread has not yet been started.

RUNNING

The thread is active and running.

SLEEPING

The thread is sleeping because Thread.sleep() or JVM_Sleep() was called.

UNKNOWN

The thread status is unknown.

WAIT

The thread is waiting after Object.wait() or JVM_MonitorWait() was called.

ZOMBIE

The thread has completed execution.

Thread Icons

Icons near each thread indicate the status of the thread relative to the current debugging session.

Icon

Description

Current thread

The current thread in suspended state.

Running thread

An active thread.

Thread at breakpoint

The thread that has hit the current breakpoint.

Suspended thread

A suspended thread. Threads are marked as suspended when they were paused by the debugger.

Frozen thread

A frozen thread. Threads are marked as frozen when they were manually paused.

Hide frames from libraries

  • If your program uses external libraries, and you want to omit calls made in library classes, click the Hide Frames from Libraries button Hide Frames from Libraries button located in the top-right part of the Frames pane.

Copy stack to clipboard

  • To copy the call stack for the current thread, right-click anywhere on the Frames tab and select Copy Stack.

Export threads

If you need to get a report containing the state of each thread and its stack trace, use the Export Threads option. This is useful when you need to share the information about the threads in text format.

  1. Right-click anywhere on the Frames pane and select Export Threads from the menu.

    Export Threads menu item
  2. To save a report as a text file, specify the path to the file in the Export Threads dialog and click Save, or click Copy to copy it to the clipboard.

    Export Preview dialog

Examine/update variables

The Variables tab shows the list of the variables in the selected frame/thread. Examining the variables can help you understand why the program operates in a certain way.

The Variables tab shows you the variables visible from the current execution point

The icon on the left of each variable indicates its type.

Variable types

Icon

Description

Static members

Static members of the enclosing class

A field

Fields of an object (both static and nonstatic)

A field containing a self-reference

Fields containing a self-referencing object (for example, cause in Throwable before the field gets initialized)

A final field

Final fields

A static field

Static fields

A thrown exception

A thrown exception (only displayed when an exception breakpoint was hit)

A method return value

A method return value (only displayed when the Show Method Return Values option is enabled)

A parameter

Method parameters

An enum constant

Enum constants

An array

Local arrays

A primitive

Local primitive types

A watch

Watches and auto-variables.

A primitive

Local reference variables

Pin fields

If an object has numerous fields, you can pin some of them, so that they are always displayed at the top of the list. The same priority will apply to other instances of this class.

  • In the Variables pane, click the icon that indicates the variable type.

    Two fields are pinned and have blue flags against them

When a field is pinned, a blue flag App icons pin to top pinned item replaces the original icon. To unpin the field, click this flag.

Copy variables

When examining variables, you may need to copy a variable name or value to paste it somewhere else or to compare it with another variable.

  • To copy the value that a variable holds, right-click the variable and select Copy Value Control+C. For types other than String, the toString representation is copied.

  • To copy the name of a variable, right-click the variable and select Copy Name.

Compare variables with clipboard

When you need to compare a variable value with some other value, use the Compare Value with Clipboard option. This is helpful, for example, when a variable holds a long string, and you need to compare it with another long string.

  1. Copy the content you want to compare (for example, from a text file).

    RSA key in a text editor
  2. In the Variables tab, right-click the variable that you want to compare with and select Compare Value with Clipboard.

  3. Examine the differences in the Diff Viewer that opens. For more information about efficient using of the Diff Viewer, refer to the Comparing Files and Folders topic.

    Clipboard vs Selected dialog

View variables in a dedicated dialog

IntelliJ IDEA allows you to inspect variables in a dedicated dialog. This is useful when you need to keep track of some variable (or the object whose reference it holds) and at the same time be able to navigate between frames and threads.

  • Right-click a variable or a watch and select Inspect.

    Inspect dialog

View variables as arrays

  1. In the Variables tab of the Debug tool window, select an array or a DataFrame.

  2. Click a link View as Array/View as DataFrame to the right.

    Alternatively, you can choose View as Array or View as DataFrame from the context menu.

    The Data View tool window appears.

    View array or data frame

Set variable values

If you want to test how the program would behave in certain conditions or fix its current behavior at runtime, you can do that by setting/changing the variable values.

  1. Select a variable and press F2. Alternatively, select Set Value from the context menu.

  2. Enter the value for the variable and press Enter.

    Enter new value for the variable in the field right next to its name

If you need to look into the source code where some variable or class is declared, you can move there right from the Variables pane.

  • To navigate to the code where the variable is declared, right-click the variable and select Jump to Source F4.

    Jump to Source takes you to the place where the variable is declared
  • To navigate to the class declaration of the variable type, right-click the variable and select Jump to Type Source Shift+F4.

    Jump to Type Source takes you to the place where the type of the variable is defined
  • To navigate to a method body from a stack trace element, click Navigate near the stack trace element on the Variables pane.

    Clicking Navigate takes you to the code of the corresponding method

Examine object references

IntelliJ IDEA provides you with the information on the currently existing objects, which hold a reference to the objects on the Variables tab. The feature also detects indirect references, like those in anonymous classes using outside variables.

  • To view the list of referring objects, right-click a variable on the Variables tab and select Show Referring Objects.

Referring Objects dialog

The Referring Objects dialog opens, allowing you to inspect the references to the selected object and to the referring objects themselves.

When you need the summary on all existing objects of some type, you can get it using the Memory tab of the Debug tool window.

Also, you can configure how different types are displayed. For example, you can use the 'toString' representation or create a custom renderer.

Evaluate expressions

IntelliJ IDEA lets you evaluate expressions during a debugging session to obtain additional details about the program state or test various scenarios at runtime.

Evaluate a simple expression in the editor

To quickly evaluate an expression, point at it in the editor. Note that method calls cannot be evaluated this way.

  1. Point at the expression you want to evaluate. The result of the expression appears in a tooltip.

    Value tooltip
  2. To view child elements of the resulting object, click the Expand button or press Control+F1.

    Resulting objects are represented by trees. This helps you view their internal state

If you find value tooltips distracting, you can increase the delay or disable them altogether. To do this, in the Settings dialog (Control+Alt+S), go to Build, Execution, Deployment | Debugger | Data Views and set the Show value tooltip and Value tooltip delay options as required.

Evaluate a complex expression in the editor

If you want to evaluate an expression in the code that involves a method call, or you want to be specific about which portion of expression to evaluate, use the Quick Evaluate Expression option.

  1. Place the caret at the expression (to evaluate the closest matching expression) or select a portion of it (if you want to be specific about which part of a complex expression to evaluate).

    Select an entire expression or its part to evaluate
  2. Click Run | Debugging Actions | Quick Evaluate Expression Control+Alt+F8. Alternatively, hold Alt and click the selection.

    Value tooltip appears

You can configure Quick Evaluate to work for a piece of code on just selecting it (without using the menu/shortcut). Use this option carefully, as you can accidentally call methods when it is enabled.

To configure Quick Evaluate on code selection, go to Settings | Build, Execution, Deployment | Debugger | Data Views and set the Show value tooltip on code selection option as preferred.

Evaluate arbitrary expressions

Evaluating arbitrary expressions is the most flexible evaluating option. It lets you evaluate any code as long as it is in the context of the current frame. Using it, you can evaluate declarations, method calls, switch expressions, anonymous classes, lambdas, loops, and so on.

Use this feature to get additional information about the current state of the program and test various scenarios all within the same debugging session. This saves a lot of time by reducing the number of sessions you have to run.

The quickest way to evaluate an arbitrary expression is to enter it in the expression field in the Variables pane and press Enter

Expression in the Variables tab

The result is displayed right below. You can also add the expression to watches by clicking App client debugger add to watch in the right-hand part of the expression field.

Result of an expression in the Variables tab

If you want to evaluate long code blocks, you may want to use a dedicated dialog for that:

Evaluate expressions in a dedicated dialog

  1. If you want to start with some expression or a variable, which is currently in front of you (for example, in the editor or on the Variables pane), select it.

    Select the expression to start from
  2. Click Run | Debugging Actions | Evaluate Expression Alt+F8. The shortcut may not work on Ubuntu (for correct operation, adjust the shortcut configuration).

  3. In the Evaluate dialog, modify the selected expression or enter a new one in the Expression field. Click Expand Shift+Enter to modify a multiline code fragment.

    The expression is entered in the Code Fragment field
  4. Click Evaluate (Control+Enter for multiline mode). The expression result appears in the Result field.

    The result of the expression is taken from the return statement. When there is no return statement, the result is taken from the last line of code (it does not even have to be an expression: a single literal works too). When there is no valid line to take the value from, the result is undefined. If the specified expression cannot be evaluated, the Result field indicates the reason.

    Expression result is calculated

The Evaluate dialog is non-modal, so you can switch the focus back to the editor to copy other variables and expressions. You can also open multiple Evaluate dialogs if necessary.

View values inline

IntelliJ IDEA shows the values of the variables right next to their usage.

Variable values are displayed at the lines where they are used

Once the variable value has changed, the inline view is updated with the new value and changes its color.

Inline values of the variables change with each step

If a line contains a reference to an object, you can examine its fields right in the editor. From this popup, you can also change the variable values and add inline watches.

Inline debugger hints

The inline view is enabled by default. To turn it off, in the Settings dialog (Control+Alt+S), go to Build, Execution, Deployment | Debugger | Data Views and disable the Show values inline option.

Add an Inline Watch

If you want the result of some expression to appear on a particular line, you can set up an inline watch for that. Inline watches are persistent and remain active after session restart.

  1. Click the inline hint referring to the object whose field you want to track.

  2. In the popup, select the field and click Add as Inline Watch.

    Adding an inline watch option
  3. Fine-tune the watch if needed. You can use any valid Java expression as a watch.

    Setting an inline watch

Inline watches you set in the editor are also shown under Inline Watches in the Variables tab of the Debug tool window.

To remove an inline watch, hover over the watch and click the cross near it.

DFA-assisted debugging

IntelliJ IDEA also provides hints on what is going to happen later in the executed piece of code. This analysis includes exceptions, boolean expression results, and code paths:

A hint showing the result of a future boolean condition
A hint showing that an exception will be thrown
Unreachable branches are greyed out

To disable the analysis of further execution, go to Build, Execution, Deployment | Debugger | Data Views | Java and clear the Predict condition values and exceptions based on data flow analysis checkbox.

Watches

If you want to keep track of some variable or the result of a more complex expression, set up a watch for this variable or expression. This is useful when you need to evaluate something that is not regularly displayed on the list of variables.

Watches are evaluated in the context of the selected frame. Watches cannot be evaluated when they are out of context or when they fail to compile. If this is the case, the watch is marked with the error icon Error icon.

By default, watches are shown together with variables in the Variables pane. To hide/reveal the Watches pane, use the Separate watches option in the Layout Settingsthe Restore Layout button menu.

Add a watch

  • For a regular watch that is evaluated everywhere, enter the expression in the top part of the Variables pane, then click Add to Watches.

    The expression in the Variables tab

    If you only want the watch to be shown when an object of a particular type is inspected, right-click such object in the Variables pane and select New Class Level Watch.

After you have added a variable/expression to Watches, it stays there and is evaluated for each step, providing you with the result in the current context.

Edit a watch

  • Right-click the desired watch and select Edit.

Delete a watch

  • To remove a single watch, right-click it and select Remove Watch. Alternatively, select the watch and press Delete.

  • To remove all watches, right-click anywhere on the Variables/ Watches pane and select Remove All Watches.

Watches allow for the same actions as variables do. For example, you can view them in a dedicated dialog or use them to navigate to the source code.

Watches are a part of your project. This means you can stop and rerun the debugging session without risk of losing them.

Labels

During debugging, it is sometimes useful to mark some instance so that it is easily identifiable in any context. For this, you can add labels. Once attached, a label accompanies the object for its entire lifetime.

A labeled instance on the Variables tab

This is especially helpful where conditions or expressions are used – you can refer to that object by label instead of searching for the reference to this object.

Using a labeled object in comparison (Evaluate dialog)

If you want to keep track of an instance regardless of the context, create a watch for this label. With this type of watch, the object is always at hand irrespective of the current frame and thread.

Add label

  1. On the Variables tab, right-click a variable or a watch which is currently referring to the object you want to track. From the menu, select Mark Object F11.

  2. Enter the name for the label. Optionally, you can select the display color. For this, click Click to select color and select the color. After you have finished with configuration, click OK.

    Configure label name and color in the Select Object Label dialog

Remove label

  • Right-click the label that you want to remove and select Unmark Object F11 from the menu.

Return to the current execution point

Examining the program state involves navigating in code, and you often need to return to the place where your program is suspended.

Do one of the following:

  • Go to Run | Debugging Actions | Show Execution Point.

  • Press Alt+F10.

  • Click The Show Execution Point button on the stepping toolbar of the Debug tool window.

The current execution point is indicated with a blue line. The code at this line has not been executed yet.

Blue line indicating the current execution point
Last modified: 30 October 2023