Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Koshal.

Asked: May 09, 2001 - 11:16 am UTC

Last updated: May 14, 2008 - 3:38 pm UTC

Version: 8.1.7.0

Viewed 1000+ times

You Asked

Hi Tom

With yr valuable suggestions/guidance we have migrated to 8.1.7.0.

Can u pl give detailed explanation/example of how to use
dbms_debug package. I had gone through the documentation but
there is no detailed example.

Thanks in Advance.

Regards




and Tom said...

it is not really intended for you to use -- it is intended to be used by someone who wants to write a PLSQL debugger.

Here is a note on it:

Article-ID: <Note:67899.1>
Alias: PLSQL:DBMS_DEBUG
Circulation: PUBLISHED (EXTERNAL)
Folder: PLSQL
Topic: DBMS_DEBUG Package
Title: PACKAGE DBMS_DEBUG Specification
Document-Type: REFERENCE
Impact: MEDIUM
Skill-Level: CASUAL
Server-Version: 08 to 08.01
Updated-Date: 05-JAN-1999 15:22:38
References:
Shared-Refs:
Authors: AUTO
Attachments: NONE
Content-Type: TEXT/PLAIN
Products: 11/PLSQL;
Platforms: GENERIC;

PACKAGE:DBMS_DEBUG
~~~~~~~~~~~~~~~~~~
Version/s: 8.0, 8.1 (Notes here based on Oracle8i)


OVERVIEW
DBMS_DEBUG is a PL/SQL API to the PL/SQL debugger layer ('Probe') in
the Oracle server. It is intended mainly for vendors implementing
server-side debuggers, and provides a way to debug server-side PL/SQL
(procedures, functions, packages, triggers, anonymous blocks, types,
etc.)
DBMS_DEBUG uses a 'pull' event model: in order to debug server-side
code it is necessary to have two database sessions: a session in which
the code will be executed in debug-mode (known as the target session),
and a second session (known as the debug session) to supervise the
target session.

To use DBMS_DEBUG, the target session first makes the appropriate calls
to initialize itself (described later). This marks the session so that
the PL/SQL interpreter runs in debug-mode and generates debug events
(described later). As debug events are generated, they are posted from
the session. In most cases, debug events require return notification:
the interpreter pauses awaiting a reply.
Meanwhile the debug session must also initialize itself via DBMS_DEBUG:
this tells it what target session to supervise. The debug session may
then call entrypoints in DBMS_DEBUG to read events that were posted
from the target session and to communicate with the target session.

DBMS_DEBUG does not provide any interface to the PL/SQL compiler. But
it does depend on debug information that is optionally generated by
the compiler. Without debug information it is not possible to look at
or modify the values of parameters or variables. There are two ways
to ensure that debug information is generated: via a session switch or
via individual recompilation.
To set the session switch, execute this statement:
ALTER SESSION SET PLSQL_DEBUG=true;
This instructs the compiler to generate debug information for the
remainder of the session. It does not recompile any existing PL/SQL.
To generate debug information for existing PL/SQL code, use one of the
following statements (the second recompiles a package or type body):
ALTER [PROCEDURE | FUNCTION | PACKAGE | TRIGGER | TYPE] <name>
COMPILE DEBUG;
ALTER [PACKAGE | TYPE] <name> COMPILE DEBUG BODY;


The diagrams below give the general idea.
Each box contains a description and the appropriate DBMS_DEBUG call.

Target Session
**************
+--------------------------------------+
| Initialize session for debugging, |
| and generate/specify unique debugID. |
| DBMS_DEBUG.initialize() |
+--------------------------------------+
|
+---------------------------------->|
| V
| +--------------------+-----------------+
| V | V
| +-----------------------+ | +------------------------+
| | Start debugging | | | Stop debugging |
| | DBMS_DEBUG.debug_on() | | | DBMS_DEBUG.debug_off() |
| +-----------------------+ | +------------------------+
| V V V
| +--------------------+-----------------+
| V
| +-------------------------+
| | Execute PL/SQL programs |
| +-------------------------+
| V
+-----------------------------------+



Debug Session
*************
+-----------------------------+
Input: | Initialize |
debugID from --> | DBMS_DEBUG.attach_session() |
target session +-----------------------------+
|
+------------------------------------>|
| +-------------------<+<------------------+
| | | |
| | +--------------------------------+ |
| | | Manipulate breakpoints | |
| | | DBMS_DEBUG.set_breakpoint() | |
| | | DBMS_DEBUG.delete_breakpoint() | |
| | | DBMS_DEBUG.disable_breakpoint()| |
| | | DBMS_DEBUG.enable_breakpoint() | |
| | | DBMS_DEBUG.show_breakpoints() | |
| | +--------------------------------+ |
| | V |
| +------------------->+>------------------+
| |
| V
| +--------------------------------------+
| | Read first event from target session |
| | DBMS_DEBUG.synchronize() |
| +--------------------------------------+
| |
| +---------------------->|
| | +------------------>|
| | | V
| | | +----------------------->+
| | | | |
| | | V |
| | | +------------------------------+ |
| | | | Show stack |-------->+
| | | | DBMS_DEBUG.print_backtrace() | V
| | | +------------------------------+ |
| | | |
| | | +------------------------<+
| | | V V
| | | +------------------------+ |
| | | | Get/set values | |
| | | | DBMS_DEBUG.get_value() |-------------->+
| | | | DBMS_DEBUG.set_value() | V
| | | +------------------------+ |
| | | |
| | | +---------------------------<+
| | | V V
| | | +------------------------+ |
| | | | Manipulate breakpoints |-------------->+
| | | +------------------------+ V
| | | |
| | | +--------------------------<+
| | | V V
| | | +--------------------------+ |
| | | | Show source |------------>+
| | | | DBMS_DEBUG.show_source() | V
| | | +--------------------------+ |
| | | |
| | +----------------------<+-------------------<+
| | |
| | V
| | +--------------------------------------------+
| | | Continue execution and wait for next event |
| | | DBMS_DEBUG.continue() |
| | +--------------------------------------------+
| |
| | +---------------------------------------+
| +- No - | Program terminated? |
| | (event is DBMS_DEBUG.reason_knl_exit) |
| +---------------------------------------+
| |
| Yes
+---------------------------+
next program to debug V
|
+-----------------------------+
| Detach session |
| DBMS_DEBUG.detach_session() |
+-----------------------------+

Control of the interpreter
The interpreter will pause execution in the following places:
1. At startup of the interpreter (so that any deferred breakpoints
may be installed prior to execution)
2. At any line containing an enabled breakpoint
3. At any line where an 'interesting' event occurs. The set of
interesting events is determined by the flags passed to
DBMS_DEBUG.continue (in the 'breakflags' parameter).
See the section on 'Break Flags' below for more details.

Terminology
+ Program unit - a PL/SQL program of any kind (procedure, function,
package, package body, trigger, anonymous block, object type, or
object type body).

General notes:
+ Session termination
There is no event for session termination. Therefore it is the
responsibility of the debug session to check and make sure that
the target session has not terminated. A call to
dbms_debug.synchronize() after the target session has terminated
will cause the debug session to hang until it times out.

+ Deferred operations
The diagram suggests that it is possible to set breakpoints prior
to having a target session. This is true: in this case Probe caches
the breakpoint request and transmits it to the target session at
first synchronization. However note that if a breakpoint request is
deferred in this fashion then:
1. set_breakpoint() does not set the breakpoint number (it can be
obtained later from show_breakpoints() if necesary)
2. set_breakpoint() does not validate the breakpoint request: if
the requested source line does not exist then the error will
silently occur at synchronization and no breakpoint will be
set.

+ Stepping into packages.
Packages are instantiated upon first use, and any initialization
code (in either the package spec or body) is executed at that time.
Thus a call to a procedure/function in a package may look to the
debugger like several calls all on the same line (one call to
initialize the spec if necessary, one to initialize the body if
necessary, and one to call the desired entrypoint).

+ Diagnostic output
To debug Probe, there are 'diagnostics' parameters to some of the
calls in DBMS_DEBUG. These parameters specify whether to place
diagnostic output in the rdbms tracefile. (If output to the rdbms
tracefile is disabled then these parameters will be no-ops.)


Rating

  (15 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

I did want to write a PL/SQL debugger

change_on_install, March 01, 2002 - 8:24 am UTC

i want to write a PL/SQL develop tool just
like DBArtisan. i use delphi and have almost
completed. but i have no experience on
DBMS_DEBUG,can you give me a example, or any
other advices is also welcome.

best regards,
change_on_install

Tom Kyte
March 01, 2002 - 9:11 am UTC

Preety much everything I know about it is listed above.

It is documented in the supplied packages guide as well
</code> http://docs.oracle.com/docs/cd/A87860_01/doc/appdev.817/a76936/dbms_deb.htm#1003779 <code>
...

Error While Executing alter session plsql_debug = true

Kumar, April 16, 2002 - 5:53 pm UTC

I am getting error while executing
alter session plsql_debug = true
Insufficent privileges

What type of privilige is needed to execute the abouve statement

Thanks
Kumar

Tom Kyte
April 16, 2002 - 9:50 pm UTC

you need ALTER SESSION


ops$tkyte@ORA817DEV.US.ORACLE.COM> drop user a cascade;

User dropped.

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> grant create session to a identified by a;

Grant succeeded.

ops$tkyte@ORA817DEV.US.ORACLE.COM> 
ops$tkyte@ORA817DEV.US.ORACLE.COM> @connect a/a
ops$tkyte@ORA817DEV.US.ORACLE.COM> set termout off
a@ORA817DEV.US.ORACLE.COM> REM GET afiedt.buf NOLIST
a@ORA817DEV.US.ORACLE.COM> set termout on
a@ORA817DEV.US.ORACLE.COM> alter session set plsql_debug=true;
ERROR:
ORA-01031: insufficient privileges


a@ORA817DEV.US.ORACLE.COM> @connect /
a@ORA817DEV.US.ORACLE.COM> set termout off
ops$tkyte@ORA817DEV.US.ORACLE.COM> REM GET afiedt.buf NOLIST
ops$tkyte@ORA817DEV.US.ORACLE.COM> set termout on
ops$tkyte@ORA817DEV.US.ORACLE.COM> grant alter session to a;

Grant succeeded.

ops$tkyte@ORA817DEV.US.ORACLE.COM> @connect a/a
ops$tkyte@ORA817DEV.US.ORACLE.COM> set termout off
a@ORA817DEV.US.ORACLE.COM> REM GET afiedt.buf NOLIST
a@ORA817DEV.US.ORACLE.COM> set termout on
a@ORA817DEV.US.ORACLE.COM> alter session set plsql_debug=true;

Session altered.

a@ORA817DEV.US.ORACLE.COM>  

Thx tom for quick response

Kumar, April 17, 2002 - 8:31 am UTC


Is there a Demo tool available which has implemented the debugger

Mani, January 10, 2003 - 2:00 pm UTC

Is there a tool available, which I can use to debug by PL/SQL procedures.

Tom Kyte
January 10, 2003 - 2:26 pm UTC

Yes, jdeveloper believe it or not. It is pretty darn awesome actually at helping you write and debug plsql:

</code> http://otn.oracle.com/software/products/jdev/content.html <code>

yes, it works with 8i databases.

Debugging package

Arun Mathur, January 29, 2004 - 10:38 am UTC

Hello Tom,

In your Expert One On One Oracle book, you mention a debug package which you and Christopher Beck wrote. I checked the WROX site for it, but didn't get any luck. Do you know of any site(s) which may have it?

As always, thank you.

Arun


Tom Kyte
January 29, 2004 - 1:25 pm UTC

it is on the apress.com website for that book (wrox went under and got sold in bits)

Nevermind - I just found it

Arun Mathur, January 29, 2004 - 10:40 am UTC


A reader, April 15, 2004 - 10:20 am UTC

Tom,

How can I find out which users are using dbms_debug package.
Will your script showsql.sql do that.

Thanks.

Tom Kyte
April 15, 2004 - 10:40 am UTC

show sql shows what they are currently running (doubtful you would catch a call -- you would generally see what they are debugging)


dba_ddl_locks can be useful to see what sessions have references to what objects.

debug

Raaghid, September 23, 2004 - 9:45 am UTC

I am following the tips you have given in writing PLSQL (Write package instead of proc., use correct literals etc etc.. Ref: you both books + this forum). Can you pl give tips on "how to debug the plsql" (Both general and link if any)

Tom Kyte
September 24, 2004 - 8:38 am UTC

Effective Oracle by Design goes through that with "the tools I use" (jdev has a source level debugger).

Also, in beginning Oracle Programming (Apress) there is a "debug.f" package -- if you goto my home page and goto "presentations" (Link near bottom), and get the BROUG slides -- it has the packge and a demo of using it. It is great for creating your own TRACE files.

debug plsql

Raaghid, September 23, 2004 - 9:49 am UTC

I am following the tips you have given in writing PLSQL (Write package instead of proc., use correct literals etc etc.. Ref: you both books + this forum). Can you pl give tips on "how to debug the plsql" (Both general and link if any)

how to trap the bug in function

Safrin, March 05, 2005 - 7:17 am UTC

I was asked by interviewer recently as follows:

"In a production environment user complains that the function returns result of 1256 instead of 1256.60." - How would you DEBUG the same. (Here, I have given exact content what the interviewer said)

I have answered like this:
1. Modify package/proc/function to get the output in different states using "dbms_output"
2. Modify package/proc/function to get the output in different states using "UTL FILE"
3. The method given in the "Begining oracle programming" (debug package tool)

But he (interviewer) is NOT at all impressed and asked me, how can you replicate the same in production when it is going on production.

CAN YOU PLEASE SHARE YOUR THOUGHTS IN THIS QUESTION. How should I answer for this question.









Tom Kyte
March 05, 2005 - 7:40 am UTC

It was a not so good question maybe, I mean -- lots to be inferred.

Did you write the code in a good way? That is, did you make the coders instrument it? If so, just turn on your diagnostics and lets see the inputs and outputs. You know, just like we do when the database is having a problem and we need to diagnose it. Defensive coding starts at home.

But ok, lets say -- no, no you didn't. I'd go into test and see if it reproduces. We must reproduce the issue, if you cannot -- well, we need to get to a point where we can. If test doesn't reproduce the issue, must be the unique set of data in production, so have the DBA restore that over here -- as we don't like to play in production (and since your code isn't already littered with debug.f statements -- we'll have to fix that once you hire me on :)

So, if we restore -- it should reproduce -- but if not, it could be something unique about the production environment -- might not even be the database, could be the client tool. Perhaps the end user set numformat 999999 in their login.sql or set a format in whatever tool they are using

In short, given I don't know your environment, your application, you level of instrumentation, your tools, your client applications -- I'd be guessing. I'd be asking lots of questions -- but the first goal would be to get it to reproduce in a test environment so we can find out what is wrong (if nothing obvious in the production environment was wrong -- I'd be asking about the client tools and such before diving into anything too deep, need more facts)




dbms_debug package - permissions required

Tim, July 20, 2005 - 5:28 pm UTC

I am attempting to use the dbms_debug with Oracle 9.2.0.4.0 through a third party application which has implemented this feature.

Your previous post said that the only system priv which was required was "alter session" priv.

However, I have received the following error and am wondering if perhaps additional privs may be required?

ORA-23322: Privilege error accessing pipe
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 86
ORA-06512: at "SYS.DBMS_PIPE", line 115
ORA-06512: at "SYS.PBREAK", line 256
ORA-06512: at "SYS.PBSDE", line 58
ORA-06512: at "SYS.DBMS_DEBUG", line 211
ORA-06512: at

(error stops there - not like I just stopped typing - character count on the error is 247 - so it may have hit some limit with the app or something - I hope enough there to be useful...)

Any thoughts you may have on this would be greatly appreciated.

Thanks.

Tom Kyte
July 21, 2005 - 3:46 pm UTC

you should not, I see nothing remotely similar in the problem database at all.

I'll have to refer you to support.

No Debugging till object is checked out

Laxman Kondal, August 25, 2005 - 4:34 pm UTC

Hi Tom

This is not an unusual problem but I am trying to figure out is there any better way to control this issue.

We have 5 active Oracle developers and 6 Java developers. Every one logs on as same user ‘OPDEV’ and store procedures are mostly packages, involve around 4000+ lines of code each. Most package will have to call other package.

Developers are creating/modifying package in TOAD or PL-SQL Developer procedure editor. Once code is in editor it can be there for long time and in the mean time another developer gets the same code in his procedure editor and makes some changes and compiles it.

Now the first user can’t see the changes made by second user unless refresh it, which in most case never be. So the net result is, changes made by second user is lost and totally confused why it’s not showing the new results which was dead certain to show up - results to more frustration.

I create a ‘before alter on schema’ trigger to check if same procedure is locked by any one or not. If not locked then throw an error:

RAISE_APPLICATION_ERROR(-20001, ora_dict_obj_name||' not locked for ddl') and then this user need to make call to procedure:

Lock_Code( p_obj_name IN VARCHAR2,
p_lock IN CHAR DEFAULT 'Y')

and this inserts a record in table:

CREATE TABLE Ddl_Check_Out_Check_Ins
(
obj_name VARCHAR2(50) NOT NULL,
obj_type VARCHAR2(50) NOT NULL,
ddl_by VARCHAR2(30) NOT NULL,
ddl_begin VARCHAR2(30) NOT NULL,
machine VARCHAR2(30),
program VARCHAR2(30) NOT NULL,
terminal VARCHAR2(30) NOT NULL,
ddl_end VARCHAR2(30)
)

as long as there is a record in this table and ddl_end is null no other user can compile that object.

I have ‘after create on schema’ trigger which takes care of new procedure by checking is object doesn’t exists then enters the record in to the table and user continues without interruption.

Up to this it works fine but when user wants to debug it first throws an error from ‘before alter on schema’ trigger:
RAISE_APPLICATION_ERROR(-20012, ora_dict_obj_name||' not locked for DDL');
and still continues with debugging.

Problem is if user has made some code change and straight hits debug its going to compile and make change to the original code.

I used DBMS_DEBUG.DEBUG_OFF it throws an error and still continues with debugging:
.....
EXCEPTION
--no one has lock on this code
WHEN NO_DATA_FOUND THEN
dbms_debug.debug_off;
RAISE_APPLICATION_ERROR(-20012, ora_dict_obj_name||' not locked for DDL');
END;

Since all logs on as same user ‘OPDEV’ but have different machine, SID, serial# - is there any way to stop debugging if no record found in Ddl_Check_Out_Check_Ins table.

I used dba_ddl_locks to get the session_id and name but this turns out be for the ‘before alter trigger’ name and not the object being debugged.

Is there any table/view in Oracle9iR2, I can get SID and Serial# and kill that session in ‘before alter trigger’ before it compiles that object for debugging.

Or do you suggest any other method to not to allow debug unless object is locked.

Thanks and regards.


Tom Kyte
August 25, 2005 - 6:46 pm UTC

you need a thing called source code control, it is this thing whereby you don't read the code out of the dictionary, but you check it out.

(not being sarcastic, really - this is source code control, treat your PLSQL like they treat their java code)

Sample code usign dbms_debug package

maverick, August 29, 2005 - 12:46 pm UTC

Tom, Can you provide an example of using dbms_debug package in my packages/procedures , like how to set this on and off in 10g?

I couldn't find in your site ..

Thanks,


Tom Kyte
August 29, 2005 - 2:01 pm UTC

file:///c:/Documents%20and%20Settings/tkyte/My%20Documents/docs/allOraDoc/10gr2/B19306_01/appdev.102/b14258/d_debug.htm#sthref2331


....
30 DBMS_DEBUG

DBMS_DEBUG is a PL/SQL interface to the PL/SQL debugger layer, Probe, in the Oracle server.

This API is primarily intended to implement server-side debuggers and it provides a way to debug server-side PL/SQL program units.
..........

if you want to debug, don't write your own, just download jdeveloper (it is free)

Not Clear

A reader, August 29, 2005 - 3:25 pm UTC

Tom,
Let's say i do not want to use Jdeveloper. Can you show me a way[with samples] to debug?

I think the link you provided is from your Local drive [looks like].

Thanks,


Tom Kyte
August 30, 2005 - 12:55 am UTC

it would be a screen shot, you just go "file/debug" or something like that. It is a gui.


</code> https://docs.oracle.com#index-STR <code>

you use the GUI to do it.

debug with dbms_application_info

Lise, May 14, 2008 - 8:48 am UTC

I hope this falls within this stream. If not I do apologise.

dbms_application_info is used throughout our PL/SQL packages to set the module and action where appropriate. We then use these settings within our error handling routine, and also within our archive table entries.
Apart from this and the ability to view the settings through v$session, I would like to extend the usage to be able to trace through my code when I need to (i.e. where I cannot use debug due to privileges).
A historical view of v$session would be great!
However, I could also write my module and action settings to an internal table, if I have switched the trace on say. I have wrapped my dbms_application_info package. It would mean that I would have to check everytime if I am suppose to trace or not, and that would add to performance unneccessary.

Any other ideas as to how I can use dbms_application_info settings with some kind of debug.
Tom Kyte
May 14, 2008 - 3:38 pm UTC

can you check just once at 'startup' or once every 100 calls or something? Use a global application context perhaps (no table, in sga)

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library