[20260901]表exp_head$的exp_id如何计算.txt

[20260901]表exp_head$的exp_id如何计算.txt

--//实际上如何计算表sys.exp_head$的exp_id并不重要,主要目的学习bpftrace脚本,以及那些信息参与运算。

1.环境:
SCOTT@book01p> @ ver2
==============================
PORT_STRING                   : x86_64/Linux 2.4.xx
VERSION                       : 21.0.0.0.0
BANNER                        : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
BANNER_FULL                   : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
BANNER_LEGACY                 : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
CON_ID                        : 0
PL/SQL procedure successfully completed.

2.测试:
SCOTT@book01p> @ spid
==============================
SID                           : 11
SERIAL#                       : 27184
PROCESS                       : 4040
SERVER                        : DEDICATED
SPID                          : 4042
PID                           : 8
P_SERIAL#                     : 11
KILL_COMMAND                  : alter system kill session '11,27184' immediate;
PL/SQL procedure successfully completed.

SCOTT@book01p> @ oname scott.^dept$
SCOTT@book01p> @ pr
==============================
O_OWNER                       : SCOTT
O_OBJECT_NAME                 : DEPT
O_OBJECT_TYPE                 : TABLE
SUBOBJECT_NAME                :
CREATED                       : 2026-06-25 09:20:13
LAST_DDL_TIME                 : 2026-06-25 09:20:13
O_STATUS                      : VALID
DATA_OBJECT_ID                : 75828
OBJECT_ID                     : 75828
PL/SQL procedure successfully completed.

SCOTT@book01p> @ 10to16 75828
10 to 16 HEX (LOWER) 10 TO 16 HEX (UPPER) REVERSE16_LOWER      REVERSE16_UPPER
-------------------- -------------------- -------------------- --------------------
0000000000012834     0000000000012834     0x34280100-00000000  0X34280100-00000000
--//object_id=75828,16进制0x0000000000012834

--//执行select dname,loc from dept where deptno=20;语句,一边调试一边写脚本每次替换1个字符变成大写,保证每次硬解析.

# bpftrace kgghash2.bt 4042
Attached 8 probes
Tracing Oracle kgghash2... Target PID: 4042
Hit Ctrl-C to stop.

qosdGetOptDir Begin
[09:54:49] kgghash2 #01 pid=4042 ptr=00007f386c75f1b0 hash_text=\x01 length=1 seed=0x0000000000012834
[09:54:49] kgghash2 #01 pid=4042 return hash_value=04f24b967a0d16cd 356430430180546253
qosdGetOptDir End
--//这里计算比较特别 seed=0x12834,参与运算0x01,估计表示namespace,前者对应表dept的object_id.

qosdExprTravTrackOpn Begin
[09:54:49] kgghash2 #02 pid=4042 ptr=00007f386fa57000 hash_text=T75828,1 length=8 seed=0x0000000000000000
[09:54:49] kgghash2 #02 pid=4042 return hash_value=4bdef074c2769905 5467071381943785733
[09:54:49] kgghash2 #03 pid=4042 ptr=00007fff066bb2b8 hash_text=4(\x01\x00 length=4 seed=0x4bdef074c2769905
[09:54:49] kgghash2 #03 pid=4042 return hash_value=d289e49c85282b06 15170908180674259718
[09:54:49] kgghash2 #04 pid=4042 ptr=00007fff066bb2a0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0xd289e49c85282b06
[09:54:49] kgghash2 #04 pid=4042 return hash_value=691d3d6a148d180e 7574277674127661070
qosdExprTravTrackOpn End

qosdExprTravTrackOpn Begin
[09:54:49] kgghash2 #05 pid=4042 ptr=00007f386fa596d0 hash_text=T75828,2 length=8 seed=0x0000000000000000
[09:54:49] kgghash2 #05 pid=4042 return hash_value=95fedc9553dfa586 10808318689651172742
[09:54:49] kgghash2 #06 pid=4042 ptr=00007fff066bb3c8 hash_text=4(\x01\x00 length=4 seed=0x95fedc9553dfa586
[09:54:49] kgghash2 #06 pid=4042 return hash_value=8500f19f6d979982 9583925674085161346
[09:54:49] kgghash2 #07 pid=4042 ptr=00007fff066bb3b0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0x8500f19f6d979982
[09:54:49] kgghash2 #07 pid=4042 return hash_value=3a58369ddfc089ad 4204170303842060717
qosdExprTravTrackOpn End

qosdExprTravTrackOpn Begin
[09:54:49] kgghash2 #08 pid=4042 ptr=00007f386fa595f8 hash_text=T75828,3 length=8 seed=0x0000000000000000
[09:54:49] kgghash2 #08 pid=4042 return hash_value=491cc0e18cf091eb 5268297739081388523
[09:54:49] kgghash2 #09 pid=4042 ptr=00007fff066bb3c8 hash_text=4(\x01\x00 length=4 seed=0x491cc0e18cf091eb
[09:54:49] kgghash2 #09 pid=4042 return hash_value=da8706046d37b7ee 15746561238206232558
[09:54:49] kgghash2 #10 pid=4042 ptr=00007fff066bb3b0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0xda8706046d37b7ee
[09:54:49] kgghash2 #10 pid=4042 return hash_value=4be05ea993188ae9 5467474030038125289
qosdExprTravTrackOpn End

--//基本可以猜测参与运算的对象是那些信息,拿最后一种情况解析,实际上对应的就是dept.loc字段:

--//[09:54:49] kgghash2 #08 pid=4042 ptr=00007f386fa595f8 hash_text=T75828,3 length=8 seed=0x0000000000000000
--//[09:54:49] kgghash2 #08 pid=4042 return hash_value=491cc0e18cf091eb 5268297739081388523
--// T75828,3  T表示表  75828表示表dept的OBJECT_ID ,3表示第3个字段,看前面的sql语句执行部分。

--//[09:54:49] kgghash2 #09 pid=4042 ptr=00007fff066bb3c8 hash_text=4(\x01\x00 length=4 seed=0x491cc0e18cf091eb
--//[09:54:49] kgghash2 #09 pid=4042 return hash_value=da8706046d37b7ee 15746561238206232558
--//实际上参与运算的是0x34 0x28 0x01 0x00,实际上还是OBJECT_ID,考虑intel cpu的Little endian。

--//[09:54:49] kgghash2 #10 pid=4042 ptr=00007fff066bb3b0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0xda8706046d37b7ee
--//[09:54:49] kgghash2 #10 pid=4042 return hash_value=4be05ea993188ae9 5467474030038125289
--//实际上参与运算的是 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00

--//每次的返回值,作为seed再次参与运算,最后算出的exp_id = 5467474030038125289.

SCOTT@book01p> @ exp_head scott.dept
             EXP_ID  OBJN  SUB_ID             FIXED_COST TEXT     COL_LIST FLAGS CTIME
------------------- ----- ------- ---------------------- -------- -------- ----- --------------------
7574277674127661070 75828       0 5.5108187853454700E-08 "DEPTNO" 1            8 2026-06-25 09:20:18
4204170303842060717 75828       0 5.5108187853454700E-08 "DNAME"  2            8 2026-06-25 09:20:18
5467474030038125289 75828       0 5.5108187853454700E-08 "LOC"    3            8 2026-06-25 09:20:18
--//可以猜测字段改名以及DATA_OBJECT_ID变化,并不会改变exp_id值。

3.继续:
--//加入表达式:
SCOTT@book01p> SELECt dname||'text',loc from dept where deptno=20;
DNAME||'TEXT'      LOC
------------------ -------------
RESEARCHtext       DALLAS

--//贴出不同的部分:
qosdExprTravTrackOpn Begin
[09:57:23] kgghash2 #21 pid=4042 ptr=00007f386c703558 hash_text=T75828,2S873,1,4,textO28,2 length=26 seed=0x0000000000000000
[09:57:23] kgghash2 #21 pid=4042 return hash_value=bafea10cbca3be02 13474384211215629826
[09:57:23] kgghash2 #22 pid=4042 ptr=00007fff066bb7e8 hash_text=4(\x01\x00 length=4 seed=0xbafea10cbca3be02
[09:57:23] kgghash2 #22 pid=4042 return hash_value=bd5b69479221e2f0 13644615252164338416
[09:57:23] kgghash2 #23 pid=4042 ptr=00007fff066bb7d0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0xbd5b69479221e2f0
[09:57:23] kgghash2 #23 pid=4042 return hash_value=5908e2245c872da0 6415626314988334496
qosdExprTravTrackOpn End

--//hash_text=T75828,2S873,1,4,textO28,2
--//表示什么就很难猜测了,开头T75828,2前面已经做了解析,中间text也可以猜测。剩下部分无法解析。

SCOTT@book01p> SELECt dname||'Text1234',loc from dept where deptno=20;
DNAME||'TEXT1234'      LOC
---------------------- -------------
RESEARCHText1234       DALLAS

qosdExprTravTrackOpn Begin
[10:03:56] kgghash2 #47 pid=4042 ptr=00007f386c703558 hash_text=T75828,2S873,1,8,Text1234O28,2 length=30 seed=0x0000000000000000
[10:03:56] kgghash2 #47 pid=4042 return hash_value=f5b580d781d8085c 17705199173161715804
[10:03:56] kgghash2 #48 pid=4042 ptr=00007fff066bb7e8 hash_text=4(\x01\x00 length=4 seed=0xf5b580d781d8085c
[10:03:56] kgghash2 #48 pid=4042 return hash_value=665f066bdc202ddf 7376621774980525535
[10:03:56] kgghash2 #49 pid=4042 ptr=00007fff066bb7d0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0x665f066bdc202ddf
[10:03:56] kgghash2 #49 pid=4042 return hash_value=f8eab0fa6c87d225 17936343055565574693
qosdExprTravTrackOpn End

--//hash_text=T75828,2S873,1,8,Text1234O28,2
--//1,8表示8个字符吗? S873 表示|| ,O28(注意大写O),不清楚oracle如何编码这些信息...

SCOTT@book01p> SELECt dname||substr('Text1234',2,5),loc from dept where deptno=20;
DNAME||SUBSTR('TEXT LOC
------------------- -------------
RESEARCHext12       DALLAS

qosdExprTravTrackOpn Begin
[10:08:36] kgghash2 #73 pid=4042 ptr=00007f386c76fb68 hash_text=T75828,2S873,1,5,ext12O28,2 length=27 seed=0x0000000000000000
[10:08:36] kgghash2 #73 pid=4042 return hash_value=a7d81e1fb8416e30 12094449920887582256
[10:08:36] kgghash2 #74 pid=4042 ptr=00007fff066bb7e8 hash_text=4(\x01\x00 length=4 seed=0xa7d81e1fb8416e30
[10:08:36] kgghash2 #74 pid=4042 return hash_value=381f6efd28746da4 4044073023986494884
[10:08:36] kgghash2 #75 pid=4042 ptr=00007fff066bb7d0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0x381f6efd28746da4
[10:08:36] kgghash2 #75 pid=4042 return hash_value=38177f8a719c25f7 4041839423176582647
qosdExprTravTrackOpn End
--//hash_text=T75828,2S873,1,5,ext12O28,2

SCOTT@book01p> SELECt upper(dname),loc from dept where deptno=20;
UPPER(DNAME)   LOC
-------------- -------------
RESEARCH       DALLAS

qosdExprTravTrackOpn Begin
[10:12:47] kgghash2 #86 pid=4042 ptr=00007f386c701f40 hash_text=T75828,2O33,1 length=13 seed=0x0000000000000000
[10:12:47] kgghash2 #86 pid=4042 return hash_value=504d45f8b9bd3b49 5786358030811347785
[10:12:47] kgghash2 #87 pid=4042 ptr=00007fff066bb7e8 hash_text=4(\x01\x00 length=4 seed=0x504d45f8b9bd3b49
[10:12:47] kgghash2 #87 pid=4042 return hash_value=184a25a328d55be3 1750252787881827299
[10:12:47] kgghash2 #88 pid=4042 ptr=00007fff066bb7d0 hash_text=\x00\x00\x00\x00\x00\x00\x00\x00 length=8 seed=0x184a25a328d55be3
[10:12:47] kgghash2 #88 pid=4042 return hash_value=8d17411c6a768043 10166666274113224771
qosdExprTravTrackOpn End

SCOTT@book01p> @ exp_head scott.dept
              EXP_ID  OBJN SUB_ID             FIXED_COST TEXT                COL_LIST FLAGS CTIME
-------------------- ----- ------ ---------------------- ------------------- -------- ----- ----------------------
 7574277674127661070 75828      0 5.5108187853454700E-08 "DEPTNO"            1            8 2026-06-25 09:20:18
 4204170303842060717 75828      0 5.5108187853454700E-08 "DNAME"             2            8 2026-06-25 09:20:18
 5467474030038125289 75828      0 5.5108187853454700E-08 "LOC"               3            8 2026-06-25 09:20:18
 6415626314988334496 75828      0  .00000551081878534547 "DNAME"||'text'     2            0 2026-09-01 09:57:23
 7820668048614395749 75828      0  .00000551081878534547 "DNAME"||'Text'     2            0 2026-09-01 10:03:28
17936343055565574693 75828      0  .00000551081878534547 "DNAME"||'Text1234' 2            0 2026-09-01 10:03:56
 6003667274989002431 75828      0  .00000551081878534547 "DNAME"||'Tex'      2            0 2026-09-01 10:07:43
 4041839423176582647 75828      0  .00000551081878534547 "DNAME"||'ext12'    2            0 2026-09-01 10:08:36
14013714092153857160 75828      0  .00000551081878534547 "DNAME"||'b'        2            0 2026-09-01 09:16:49
10363015206532447744 75828      0  .00000551081878534547 "DNAME"||'a'        2            0 2026-08-31 18:05:13
10166666274113224771 75828      0  .00000551081878534547 UPPER("DNAME")      2            0 2026-09-01 09:17:44
11 rows selected.

--//dname||substr('Text1234',2,5) 记录的是 "DNAME"||'ext12'

4.附上bpftrace脚本:
$ cat kgghash2.bt
BEGIN
{
    printf("Tracing Oracle kgghash2... Target PID: %d\n", $1);
    printf("Hit Ctrl-C to stop.\n\n");
}

uprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:qosdGetOptDir
/ $1 == 0 || pid == $1 /
{
        printf("\nqosdGetOptDir Begin\n");
}

uretprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:qosdGetOptDir
/ $1 == 0 || pid == $1 /
{
        printf("qosdGetOptDir End\n");
}

uprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:qosdExprTravTrackOpn
/ $1 == 0 || pid == $1 /
{
        printf("\nqosdExprTravTrackOpn Begin\n");
}

uretprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:qosdExprTravTrackOpn
/ $1 == 0 || pid == $1 /
{
        printf("qosdExprTravTrackOpn End\n");
}


uprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:kgghash2
/ $1 == 0 || pid == $1 /
{
    @kgg++;
    @is_kgg[pid] = 1;
    printf("[%s] kgghash2 #%02d pid=%d ptr=%016lx hash_text=%s length=%d seed=0x%016lx \n", strftime("%H:%M:%S", nsecs), @kgg, pid,arg0,buf(arg0,arg1), arg1,arg2);
}

uretprobe:/u01/app/oracle/product/21.0.0/dbhome_1/bin/oracle:kgghash2
/ $1 == 0 || pid == $1 /
{
    if (@is_kgg[pid] == 1) {
        printf("[%s] kgghash2 #%02d pid=%d return hash_value=%016lx %lu\n", strftime("%H:%M:%S", nsecs), @kgg,pid, retval, retval);
        delete(@is_kgg[pid]);
    }
}

END
{
    printf("\nTotal kgghash2: %d\n", @kgg);
    delete(@kgg);

}


posted @ 2026-09-04 20:53  lfree  阅读(2)  评论(0)    收藏  举报