PL/SQL smart call stack analysis, plsql

Source: Internet
Author: User

PL/SQL smart call stack analysis, plsql

PL/SQL smart call stack Analysis
Original article: http://www.oracle.com/technetwork/issue-archive/2014/14-jan/o14plsql-2045346.html

The three DBMS_UTILITY functions
(DBMS_UTILITY.FORMAT_CALL_STACK, DBMS_UTILITY.FORMAT_ERROR_STACK, and DBMS_UTILITY.FORMAT_ERROR _ BACKTRACE) have been crucial aids in diagnosing and resolving problems in PL/SQL code. the UTL_CALL_STACK package recognizes the importance of this data and takes a big step forward in giving PL/SQL developers access to more in-depth and useful information
Three tool functions before 12C (DBMS_UTILITY.FORMAT_CALL_STACK, DBMS_UTILITY.FORMAT_ERROR_STACK, DBMS_UTILITY.FORMAT_ERROR _ BACKTRACE)
It has provided key help for PL/SQL program analysis and problem solving.
The UTL_CALL_STACK package introduced by 12C realizes the importance of modifying class data and further enhances it so that PL/SQL developers can obtain more in-depth and useful information.

-Call Stacks: DBMS_UTILITY.FORMAT_CALL_STACK
Answered "How did I get here ?" How did I get here Step by Step? For example:

SQL> CREATE OR REPLACE PROCEDURE proc1  2  IS  3  BEGIN  4     DBMS_OUTPUT.put_line (DBMS_UTILITY.format_call_stack);  5  END;  6  /SQL> CREATE OR REPLACE PACKAGE pkg1  2  IS  3     PROCEDURE proc2;  4  END pkg1;  5  /SQL> CREATE OR REPLACE PACKAGE BODY pkg1  2  IS  3     PROCEDURE proc2  4     IS  5     BEGIN  6        proc1;  7     END;  8  END pkg1;  9  /SQL> CREATE OR REPLACE PROCEDURE proc3  2  IS  3  BEGIN  4     FOR indx IN 1 .. 1000  5     LOOP  6        NULL;  7     END LOOP;  8  9     pkg1.proc2; 10  END; 11  /SQL> BEGIN  2     proc3;  3  END;  4  /

------- PL/SQL Call Stack -------
Object handle line number object name
000007FF7EA83240 4 procedure HR. PROC1
000007FF7E9CC3B0 6 package body HR. PKG1
000007FF7EA0A3B0 9 procedure HR. PROC3
000007FF7EA07C00 2 anonymous block

-Disadvantages:
If you call a subprogram in a package, the formatted call stack will show only the package name, not the subprogram name and certainly not the names of nested subprograms defined within that packaged subprogram.
If we call a subroutine in the package, this function can only display registration, and does not display the name of the subroutine, let alone the name of the nested subroutine.

If you simply want the name of the most recently executed subprogram, you will have to parse the string. this is not hard to do, but it's more code that you have to write and maintain.
If we only want to see the name of the recently executed subroutine, We have to parse lengthy strings. Although this is not difficult, it will undoubtedly increase the burden on developers.

The object handle value is, for all practical purposes, "noise." PL/SQL developers-outside of Oracle, at least-never use it.
The object handle value is a chicken rib and has no practical use.

-Error stack Error Stacks: DBMS_UTILITY.FORMAT_ERROR_STACK Similar to SQLERRM
The DBMS_UTILITY.FORMAT_ERROR_STACK function differs from SQLERRM in two ways:

It can return an error message as long as 1,899 characters, thereby avoiding (or at least making extremely unlikely) truncation issues when the error stack gets long. (SQLERRM truncates at only 510 characters .)

You cannot pass an error code number to this function, and it cannot be used to return the message for an error code.

-Error backtracking Error Backtraces: DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
Returns a formatted string that displays a stack of programs and line numbers tracing back to the line on which the error was originally raised.

12c: UTL_CALL_STACK package
Name Description
BACKTRACE_DEPTH Returns the number of backtrace items in the backtrace
BACKTRACE_LINE Returns the line number of the unit at the specified backtrace depth
BACKTRACE_UNIT Returns the name of the unit at the specified backtrace depth
CONCATENATE_SUBPROGRAM Returns a concatenated form of a unit-qualified name
DYNAMIC_DEPTH Returns the number of subprograms in the call stack, including SQL, Java, and other non-PL/SQL contexts invoked along the way-for example, if A callb callc callb, this stack, written as a line with dynamic depths underneath it, will look like this:

A B C B
4 3 2 1

ERROR_DEPTH Returns the number of 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

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.