一、 简介
oradebug是一个未被文档记录的Oracle命令,可以用来检测实例和会话中出现的故障,
拥有管理员的权限才可以运行此命令。使用oradebug你可以完成如下工作(并不局限于此):
l 启用/禁用其他用户会话的SQL tracing
l 挂起问题进程
l 在共享内存和信号量中查找信息
l 关闭trace文件以便生成新的trace文件
l 操作、转储内部结构
l 唤醒进程
二、 Oradebug使用实例
下面通过两个实用的例子展示oradebug命令的使用。
1、 使用Oradebug获得trace文件的名称和位置
[oracle@sunny ~]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Oct 16 17:35:12 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> alter session set events '10046 trace name context forever, level 12';
Session altered.
SQL> oradebug setmypid
Statement processed.
SQL> oradebug tracefile_name
/u01/app/oracle/admin/STUDY/udump/study_ora_5179.trc
2、 使用Oradebug调用另一个会话的跟踪(10046)
[oracle@sunny ~]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Oct 16 17:40:40 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select spid
2 from v$process
3 where addr = (select paddr from v$session where username = 'SUNNY');
SPID
------------
5366
SQL> oradebug setospid 5366
Oracle pid: 18, Unix process pid: 5366, image: oracle@sunny.syp.com (TNS V1-V3)
SQL> oradebug event 10046 trace name context forever,level 12
Statement processed.
SQL> oradebug tracefile_name
/u01/app/oracle/admin/STUDY/udump/study_ora_5366.trc
SQL> oradebug event 10046 trace name context off
Statement processed.
三、 生成Hang/Locking问题的分析文件
Hang/Locking问题相比于系统缓慢来说是更为棘手的问题,所以及时、合理的处理是非
常必要的。当类似问题发生时应首先判断是否确实遇到了Hang/Locking问题,数据库Hang住可以表现为:
l 数据库不再允许用户连接
l 数据库不再工作
l Select 1 from dual没有输出
l Create table不能完成
相应的,Locking问题可以表现为:
l 一个或者多个会话完全停止工作
如果确认遇到了Hang/Locking问题,那么下一步就是收集相应的信息,已确定问题产生的原因并寻找对应的解决方法。hanganalyze和systenstate对于分析此类问题有很大的帮助,建议的生成方式如下:
1、 export ORACLE_SID=生产环境ORACLE_SID
2、 使用SQL*Plus以sysdba身份登入:
sqlplus -prelim / as sysdba
重复三次,创建三个会话(SQL1, SQL2, and SQL3)
3、 在SQL1执行如下命令 :
SQL1> oradebug setmypid
SQL1> oradebug unlimit
SQL1> oradebug hanganalyze 3
4、 在SQL2 执行如下命令:
SQL2> oradebug setmypid
SQL2> oradebug unlimit
SQL2> oradebug dump systemstate 266
5、 等待至少两分钟.
6、 在SQL1 执行如下命令:
SQL1> oradebug hanganalyze 3
7、 在SQL3 执行如下命令:
SQL3> oradebug setmypid
SQL3> oradebug unlimit
SQL3> oradebug dump systemstate 266
三份新的trace文件会在user_dump_destination下生成,其中一份包含两次hanganalyze的dump,另外两份各包含一次systemstate dump。
对于集群环境,可以采用如下步骤:
1、 export ORACLE_SID=生产环境ORACLE_SID
2、 通过SQL*Plus以sysdba身份连接数据库:
sqlplus -prelim / as sysdba
3、 执行如下命令:
SQL> oradebug setmypid
SQL> oradebug unlimit
SQL> oradebug -g all hanganalyze 3
SQL> oradebug -g all dump systemstate 266
4、 等待至少两分钟
5、 执行如下命令:
SQL> oradebug -g all hanganalyze 3
SQL> oradebug -g all dump systemstate 266
trace文件会被写入到diag后台进程的trace文件中,在每个节点的background_dump_destination目录中。
参考文档:
How to Use Oradebug to Get Trace File Name and Location [ID 310830.1]
How to Enable SQL_TRACE for Another Session or in MTS Using Oradebug
[ID 1058210.6]
ORADEBUG - UNDOCUMENTED ORACLE UTILITY By Miladin Modrakovic
Oracle Performance Diagnostic Guide Hang/Locking
10046 Trace启动方法
开启当前会话的10046 Trace
使用sql_trace参数
sql_trace应该是简单快捷的开启Trace的方法了,不过通过sql_trace只能开启级别为1的Trace,而无法开启其他更高级的Trace。
1 2 3 4 5 | -- 开启Trace ALTER SESSION SET sql_trace=true;
-- 关闭Trace ALTER SESSION SET sql_trace=false; |
使用set event开启Trace
使用set event打开10046事件Trace是最常用的了。
1 2 3 4 5 | -- 开启级别为12的Trace,level后面的数字设置了Trace的级别 ALTER SESSION SET EVENTS '10046 trace name context forever, level 12'
-- 关闭Trace,任何级别 ALTER SESSION SET EVENTS '10046 trace name context off' |
我们可以通过编写登陆触发器来开启10046 Trace,使用这种方法开启Trace的代码和开启当前会话的是一样的,不同的就是这些开启代码是包含在一个after logon触发器里面的。
1 2 3 4 5 6 7 8 9 10 | -- 代码来自《Optimazing Oracle Performance》 P116 CREATE OR REPLACE TRIGGER trace_test_user AFTER LOGON ON DATABASE BEGIN IF USER LIKE '%\_test' ESCAPE '\' THEN EXECUTE IMMEDIATE 'ALTER SESSION SET timed_statistics=true'; EXECUTE IMMEDIATE 'ALTER SESSION SET max_dump_file_size=unlimited'; EXECUTE IMMEDIATE 'ALTER SESSION SET EVENTS ''10046 trace name context forever, level 8'' '; END IF; END; / |
使用oradebug工具
使用oradebug工具必须要知道所要处理的进程的OS进程PID,OS PID可以使用下面的语句得到:
1 2 3 4 5 6 | SELECT S.USERNAME, P.SPID OS_PROCESS_ID, P.PID ORACLE_PROCESS_ID FROM V$SESSION S, V$PROCESS P WHERE S.PADDR = P.ADDR AND S.USERNAME = UPPER('&USER_NAME'); |
得到PID之后就可以使用oradebug工具了,注意需要使用sysdba登陆:
1 2 3 4 5 6 7 8 | -- 假设9999为会话的OS PID oradebug setospid 9999; -- 设置Trace文件大小 oradebug unlimit; -- 开启级别为12的Trace oradebug event 10046 trace name context forever ,level 12; --关闭trace Oradebug event 10046 trace name context off; |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29209863/viewspace-2138299/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29209863/viewspace-2138299/