PL/SQL精明的调用栈分析

PL/SQL精明的调用栈分析
原文: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
12C曾经的3个工具函数(DBMS_UTILITY.FORMAT_CALL_STACK,DBMS_UTILITY.FORMAT_ERROR_STACK,DBMS_UTILITY.FORMAT_ERROR_ BACKTRACE)
已经给PL/SQL程序分析和问题解决提供了关键的帮助。
12C開始引入的UTL_CALL_STACK包意识到改类数据的重要性并进一步增强以使PL/SQL开发人员能够获得很多其它深层次的实用的信息。

–调用栈 Call Stacks :DBMS_UTILITY.FORMAT_CALL_STACK
回答了 “How did I get here?

” 。我是怎么一步一步到达这里的?比如:

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

–弊端:
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 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.
假设我们只想看下近期运行的子程序名称,还得去解析冗长的字符串。尽管这并非难事,但无疑加重了开发人员的负担。

The object handle value is, for all practical purposes, “noise.” PL/SQL developers—outside of Oracle, at least—never use it.
object handle值是个鸡肋,无实际用途。

–错误栈 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 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 calls B calls C calls B, 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

0
0
查看评论
* 以上用户言论仅仅代表其个人观点,不代表CSDN站点的观点或立场

PLSQL获取异常堆栈信息-dbms_utility.format_error_backtrace(正确抛出异常信息)

结论: dbms_utility.format_error_backtrace    能够直接定位到详细异常的位置  记录了异常的详细的栈的信息。显示了异常的详细的传递过程。对于我们调试PL/S...
  • lingjian519
  • lingjian519
  • 2014-11-29 21:24
  • 779

《程序调试思想与实践》.(The.Science.of.Debugging)读书笔记

全书以bug为中心。环绕调试维护进行组织。

1 bug 1.1 关于bug 1、作为一个词,bug被软件开发组织用来描写叙述一些须要修复的问题。 2、Bug的定义 简单定义:bug是...

  • qq_31082427
  • qq_31082427
  • 2017-05-19 15:47
  • 562

PL/SQL精明的调用栈分析

PL/SQL精明的调用栈分析 原文:http://www.oracle.com/technetwork/issue-archive/2014/14-jan/o14plsql-2045346.html...
  • IndexMan
  • IndexMan
  • 2015-07-02 14:36
  • 1181

使用PL/Scope分析PL/SQL代码

使用PL/Scope分析你的PL/SQL代码从11g開始Oracle引入了PL/Scope 用于编译器收集PL/SQL程序单元的全部标识符(变量名、常量名、程序名等)。

收集到的信息可通过一系列静态...

  • IndexMan
  • IndexMan
  • 2015-07-22 13:43
  • 1080

PL/SQL Developer配置使用说明

PL/SQL Developer是一个集成开发环境,专门面向Oracle数据库存储程序单元的开发。现在。有越来越多的商业逻辑和应用逻辑转向了Oracle Server。因此。PL/SQL编程也成了整个...
  • tennysonsky
  • tennysonsky
  • 2015-09-14 22:35
  • 4388

在pl/sql中调用shell命令的4种方法

本来想是一个非常easy的操作。可惜Oracle没有提供简单的一个命令(或许我不知道吧),仅仅好进行一些复杂点的操作了。一般有三种方式实现:     1. 利用DBMS_PIPE包并创建OS上执行的守护进程...
  • a9529lty
  • a9529lty
  • 2011-11-11 08:21
  • 5155

oracle PL/SQL进行 建表、存储过程、序列、存储过程调用、Job

/* --create test table create table T_JOB_TEST ( JID NUMBER(20) not null, JDate Date, constr...
  • Matol
  • Matol
  • 2013-05-27 14:10
  • 997

ORACLE触发器+PL/SQL调用JAVA方法

案例:当表sys_user新增、改动、删除时后。触发触发器记录操作状态。

第一步:创建更新记录表 create table user_update_log     (       id number(...

  • huangwuyi
  • huangwuyi
  • 2013-12-02 15:26
  • 4010

[oracle]pl/sql in/out变量的使用和过程中过程的调用

--in/out 类型变量的使用 以及过程中调用过程。获取out类型的返回值 --建一张表books --简单过程一 插入一条记录  [sql] view plain...
  • hly_coder
  • hly_coder
  • 2017-11-20 16:53
  • 304

在PL/SQL中调用Oracle ERP请求(一)-提交单个请求

近期开发一个从PLM系统中通过工作流程向ORACLE ERP系统中导入项目信息的程序,Oracle ERP系统有独立的请求模块,所以这样我们仅仅需调用到ERP模块中的请求就可以。因为PLM系统用的是win...
  • kingaming
  • kingaming
  • 2014-04-28 17:49
  • 1323
    个人资料
    • 訪问:1403040次
    • 积分:10186
    • 等级:
    • 排名:第1930名
    • 原创:157篇
    • 转载:48篇
    • 译文:22篇
    • 评论:113条
    博客专栏
    最新评论
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/llguanli/p/8267922.html