PL/SQL精明的调用栈分析(二)

2015-07-24 06:34:09 · 作者: · 浏览: 1
errors in the call stack
ERROR_MSG Returns the error message of the error at the specified error depth
ERROR_NUMBER Returns the error number of the error at the specified error depth
LEXICAL_DEPTH Returns the lexical nesting level of the subprogram at the specified dynamic depth
OWNER Returns the owner name of the unit of the subprogram at the specified dynamic depth
UNIT_LINE Returns the line number of the unit of the subprogram at the specified dynamic depth
SUBPROGRAM Returns the unit-qualified name of the subprogram at the specified dynamic depth

SQL> CREATE OR REPLACE PROCEDURE format_call_stack_12c 2 IS 3 BEGIN 4 DBMS_OUTPUT.put_line ( 5 'LexDepth Depth LineNo Name'); 6 DBMS_OUTPUT.put_line ( 7 '-------- ----- ------ ----'); 8 9 FOR the_depth IN REVERSE 1 .. 10 utl_call_stack.dynamic_depth () 11 LOOP 12 DBMS_OUTPUT.put_line ( 13 RPAD ( 14 utl_call_stack.lexical_depth ( 15 the_depth), 16 9) 17 || RPAD (the_depth, 5) 18 || RPAD ( 19 TO_CHAR ( 20 utl_call_stack.unit_line ( 21 the_depth), 22 '99'), 23 8) 24 || utl_call_stack.concatenate_subprogram ( 25 utl_call_stack.subprogram ( 26 the_depth))); 27 END LOOP; 28 END; 29 / SQL> CREATE OR REPLACE PACKAGE pkg 2 IS 3 PROCEDURE do_stuff; 4 END; 5 / SQL> CREATE OR REPLACE PACKAGE BODY pkg 2 IS 3 PROCEDURE do_stuff 4 IS 5 PROCEDURE np1 6 IS 7 PROCEDURE np2 8 IS 9 PROCEDURE np3 10 IS 11 BEGIN 12 format_call_stack_12c; 13 END; 14 BEGIN 15 np3; 16 END; 17 BEGIN 18 np2; 19 END; 20 BEGIN 21 np1; 22 END; 23 END; 24 / SQL> BEGIN 2 pkg.do_stuff; 3 END; 4 / 

LexDepth Depth LineNo Name

——————— ——————— ———————— ——————————————————————————
0 6 2 __anonymous_block
1 5 21 PKG.DO_STUFF
2 4 18 PKG.DO_STUFF.NP1
3 3 15 PKG.DO_STUFF.NP1.NP2
4 2 12 PKG.DO_STUFF.NP1.NP2.NP3
0 1 12 FORMAT_CALL_STACK_12C

SQL> CREATE OR REPLACE FUNCTION backtrace_to 2 RETURN VARCHAR2 3 IS 4 BEGIN 5 RETURN 6 utl_call_stack.backtrace_unit ( 7 utl_call_stack.error_depth) 8 || ' line ' 9 || 10 utl_call_stack.backtrace_line ( 11 utl_call_stack.error_depth); 12 END; 13 / SQL> CREATE OR REPLACE PACKAGE pkg1 2 IS 3 PROCEDURE proc1; 4 PROCEDURE proc2; 5 END; 6 / SQL> CREATE OR REPLACE PACKAGE BODY pkg1 2 IS 3 PROCEDURE proc1 4 IS 5 PROCEDURE nested_in_proc1 6 IS 7 BEGIN 8 RAISE VALUE_ERROR; 9 END; 10 BEGIN 11 nested_in_proc1; 12 END; 13 14 PROCEDURE proc2 15 IS 16 BEGIN 17 proc1; 18 EXCEPTION 19 WHEN OTHERS THEN RAISE NO_DATA_FOUND; 20 END; 21 END pkg1; 22 / SQL> CREATE OR REPLACE PROCEDURE proc3 2 IS 3 BEGIN 4 pkg1.proc2; 5 END; 6 / SQL> BEGIN 2 proc3; 3 EXCEPTION 4 WHEN OTHERS 5 THEN 6 DBMS_OUTPUT.put_line (backtrace_to); 7 END; 8 /

HR.PKG1 line 19