Debugging
To troubleshoot the issues that are related to connection to a database, refer to the Troubleshooting section.
note
Debug functionality is only supported for Oracle databases.
The debugger helps you find errors in your code by investigating the runtime behavior of the code. With the debugger in DataGrip, you can go through the code line by line, step in and out of called routines, evaluate expressions, and watch variables as they change their values.
tip
During a debug session, you can step through the code, evaluate expressions, examine suspended program, set watches.
note
Before starting a debugging session, check that your user has
DEBUG CONNECT SESSION
andDEBUG ANY PROCEDURE
user privileges. Also, check that access to$SYS.V_SESSION
and to thedbms_debug
package is granted to your user.
The debugger is based on the Oracle Probe that uses API of the DBMS_DEBUG package and should work on Oracle servers 9.0 and later.
In Oracle, you can debug the following program units (PL/SQL programs): anonymous blocks, packages, procedures, functions, and triggers.
Right-click the Oracle data source and select New | Query Console.
Alternatively, select one of the existing consoles from Query Consoles list (CtrlShiftF10).
Type or paste your code in the console.
Click the Execute button or press CtrlEnter to run the procedure code.
As a result, you see a created object in the Database Explorer (View | Tool Windows | Database Explorer).
A code snippet of the procedure:
CREATE PROCEDURE simpleprocedure (inval NUMBER)
{...}
To enable debugging for a PL/SQL code, you need to compile it with the DEBUG
option. The process of compilation converts PL/SQL code to Pro*C, which is then compiled to Oracle shared libraries. The compilation helps the Oracle interpreter to process your code faster.
Right-click the PL/SQL object that you want to debug and select SQL Scripts | Recompile.
In the Recompile dialog, select With DEBUG option.
Click OK.
PL/SQL program units organize the code into blocks. A block without a name is an anonymous block. The anonymous block is not stored in the Oracle database. In the debugging process, you use the anonymous block to pass values for parameters.
To debug procedures, packages, and functions, write an anonymous block that calls the necessary routine.
In the Database Explorer (View | Tool Windows | Database Explorer) , double-click the PL/SQL object that you created and compiled for debugging.
Click the Run Procedure button . If the session is not selected, select a session from the list. For more information about managing sessions, refer to Sessions.
From the Execute Routine dialog,
copy and paste the anonymous block to the console
.
Place breakpoints in the anonymous block and in the PL/SQL program object that is referenced in this anonymous block.
(Optional) Modify parameter values.
Click Debug.
A code snippet of the procedure:
CREATE PROCEDURE simpleprocedure (inval NUMBER)
{...}
A package is a schema object that groups logically related PL/SQL types, items, and subprograms.
Just like with procedures and functions, to debug a package, write the anonymous block that calls the necessary routine.
In the Database Explorer (View | Tool Windows | Database Explorer) , double-click the package that you created and compiled for debugging.
Place breakpoints in the package.
Right-click the Oracle data source and select New | Query Console.
Alternatively, select one of the existing consoles from Query Consoles list (CtrlShiftF10).
In the Oracle console, write an anonymous block that triggers the procedure.
Click Debug.
A code snippet of the package:
CREATE PACKAGE PKG_DBGD AS
{...}
A code snippet of the anonymous block:
DECLARE
{...}
A trigger is a PL/SQL program unit that is automatically called by the DBMS when you issue INSERT
, UPDATE
or DELETE
queries. Triggers are associated with a table and are called before or after you insert, update, or delete a data row. A table can have several triggers.
To debug a trigger, write an INSERT
, UPDATE
or DELETE
query to a table or a view.
Right-click the Oracle data source and select New | Query Console.
Alternatively, select one of the existing consoles from Query Consoles list (CtrlShiftF10).
Type a query in the console.
Place breakpoints in a trigger.
Click Debug.
A code snippet of the trigger:
create table Table_with_Triggers
{...}
When the debugger session is running, you can pause/resume it using the buttons on the toolbar of the Debug tool window:
To pause a debugger session, click .
To resume a debugger session, click F9.
note
Pausing the program is not an alternative to using breakpoints as this method doesn't let you use the full range of debugger functionality. For example, you cannot evaluate expressions after pausing the program.
Click the Rerun button in the Debug tool window or press CtrlF5.
Click in the Debug tool window.
Alternatively, press CtrlF2 and select the process to terminate (if there are two or more of them).
In the Settings dialog (CtrlAlt0S) , navigate to Tools | Debugger.
Action | Hotkey |
---|---|
CtrlF8 | |
Resume program | F9 |
F8 | |
F7 | |
Stop | CtrlF2 |
CtrlShiftF8 | |
Debug code at caret | ShiftF9 |
Thanks for your feedback!