简体   繁体   中英

Oracle PL/SQL debug procedure - using $$plsql_line

I'm looking to create a procedure that helps with debugging specific variables, and for the sake of re-usability I'd like to store it in it's own procedure and pass in the specific variable when needed such as

debugz(var_x);

and the debug procedure would do the following -

PROCEDURE debugz (var_x VARCHAR2(1000))
AS
BEGIN
DBMS_OUTPUT.put_line ('Variable value: '|| var_x || ' | Line number: ' || $$plsql_line ||' | Unit: '|| $$plsql_unit);
END;

The problem is, I want the PL/SQL line number and PL/SQL unit to be based off of where the procedure call originates, ie the line/unit of " debugz(var_x); ". Is there anyway for debugz to output that line number without passing in additional information?

Check out DBMS_UTILITY.FORMAT_CALL_STACK

You may need/want to parse the output depending on your exact requirements, but may be a good place to start.

Oracle 10g onwards.

Also note DBMS_UTILITY.FORMAT_ERROR_BACKTRACE and DBMS_UTILITY.FORMAT_ERROR_STACK for error handling.

Have you tried remote debugging? Here's how you can do it in SQL Developer:

1) Reference: http://sueharper.blogspot.ca/2006/07/remote-debugging-with-sql-developer_13.html

2) User privileges: grant EXECUTE on DBMS_DEBUG_JDWP to USERXX; grant DEBUG CONNECT SESSION to USERXX; grant DEBUG ANY PROCEDURE to USERXX;

3) Set Remote Debug on USERXX connection in SQL Developer: Port: 80 (use 4000 if not blocked by firewall) Local Address: IP address of your local machine

4) Compile code (package, procedure or function) in USERXX in SQL Developer for debug

5) Set breakpoints in code

6) On the remote (this could be your application invoking the PLSQL code): before invoking the PLSQL code run/include the following: DBMS_DEBUG_JDWP.CONNECT_TCP( 'IP address in 3', port in 3 ) when this is run SQL developer will switch to debug mode.

7) Run application or invoke procedure from remote as USERXX

8) SQL developer stops at first breakpoint, step into, view/change values and etc.

OWA_UTIL.WHO_CALLED_ME(
   owner          OUT      VARCHAR2,
   name           OUT      VARCHAR2,
   lineno         OUT      NUMBER,
   caller_t       OUT      VARCHAR2);

There is a very good debugging package unit called debugf which provides good functionality

The debug file contains information such as session id,date and time,packages being called and the line number of each debug message and the debug message itself

Example usage is given below

This is used to intialize the debug, the first parameter 'ALL' meaning all modules(can be function,procedure or package etc) and SYSTEM meaning the schema i want to debug

 debug.init(p_modules => 'ALL',p_file =>'C:\debugf123\temp\test.txt',p_user =>'SYSTEM',p_show_date => 'YES',p_date_format =>'DD/MM/YYYY  HH24:MI:SS',p_name_len => 30,p_show_sesid => 'YES');

This one works like printf in C and the maximum you can give is 10 parameters where v_word1 is a parameter

debug.f('the first is %s',v_word1); 

This is same as debug.f but here you can give more than 10 parameters

debug.fa('the third is %s and %s',debug.argv(v_word1,v_amount)); 

The source code for this package is available at

http://gerardnico.com/wiki/database/oracle/debugf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM