[20260825]利用proc查看对应地址信息的方法.txt

[20260825]利用proc查看对应地址信息的方法.txt

--//昨天测试 [20260823]数字字典对象hash的计算验证.txt时,遇到的问题:
krqhsh Begin
[15:57:23] kgghash #11 pid=3809 hash_text=m\x00\x00\x00\x04\x00DEPT\x00...
...
x00\x00\x00\x00\x00\x01\....
...\x00\x00 length=528 seed=0
[15:57:23] kgghash #11 pid=3809 return hash_value=000000001f71eba5 527559589 0000000000006ba5 27557
[15:57:23] kgghash #12 pid=3809 hash_text=BOOK01P length=7 seed=1f71eba5
[15:57:23] kgghash #12 pid=3809 return hash_value=000000000d782271 225976945 0000000000002271 8817
krqhsh End

--//有没有更好的查询对应信息,感觉可以利用/proc/<spid>/mem直接查询,只要知道开始地址以及显示长度.
--//注:这类方式仅仅用于测试环境以及学习,不要在生产系统使用这类非常规的方式。

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.测试:
SYS@book> alter system flush SHARED_POOL;
System altered.

SYS@book> @ dc/dc_objects "DC_OBJ_NAME='DEPT'"
no rows selected

SCOTT@book01p> @ spid
==============================
SID                           : 21
SERIAL#                       : 57889
PROCESS                       : 4429
SERVER                        : DEDICATED
SPID                          : 4431
PID                           : 8
P_SERIAL#                     : 9
KILL_COMMAND                  : alter system kill session '21,57889' immediate;
PL/SQL procedure successfully completed.

$ ls -l /proc/4431/mem
-rw-------. 1 oracle oinstall 0 2026-08-26 16:40:53 /proc/4431/mem
--//oracle用户有读写权限,大小等于0。并不影响查询。测试过root用户也可以查看。

--//如下执行报错,但是并不影响加入-s参数的执行.
$ xxd /proc/4431/mem | head -1
xxd: Input/output error

--///proc/<spid>/mem的读取非常特殊,仅仅支持open,lseek,read模式,而且读取的地址仅仅在该进程对应的地址范围,参考
--///proc/<pid>/maps确定。并且像hexdump,od等命令行工具不行,采用pread的读取模式,不在支持之列。

$ hexdump  -s $[0x00000000778C4998+16+6777*24] -n 48 /proc/4431/mem
$ od -t x8 -j $[0x00000000778EC500] -N 48  -A x -w24 /proc/4431/mem
od: /proc/3058/mem: read error: Input/output error
778ec500

--//继续测试:

# bpftrace dc_hash.bt 4431
Attached 6 probes
Tracing Oracle kgghash... Target PID: 4431
Hit Ctrl-C to stop.

SCOTT@book01p> @ desc dept
           Name                            Null?    Type
           ------------------------------- -------- ----------------------------
    1      DEPTNO                          NOT NULL NUMBER(2)
    2      DNAME                                    VARCHAR2(14)
    3      LOC                                      VARCHAR2(13)

# bpftrace dc_hash.bt 4431
....

[17:04:57] kgghash #6190 pid=4431 ptr=000000007e726e2c hash_text=BOOK01P length=7 seed=0
[17:04:57] kgghash #6190 pid=4431 return hash_value=000000003c45f11b 1011216667 000000000000711b 28955

# xxd -s 0x000000007e726e2c -l 7  /proc/4431/mem
7e726e2c: 424f 4f4b 3031 50                        BOOK01P

krqhsh Begin
[16:56:55] kgghash #712 pid=4431 ptr=00007fff916708e0 hash_text=m\x00\x00\x00\x04\x00DEPT...\x00\x00 length=528 seed=0
[16:56:55] kgghash #712 pid=4431 return hash_value=000000001f71eba5 527559589 0000000000006ba5 27557
[16:56:55] kgghash #713 pid=4431 ptr=000000007e726e2c hash_text=BOOK01P length=7 seed=1f71eba5
[16:56:55] kgghash #713 pid=4431 return hash_value=000000000d782271 225976945 0000000000002271 8817
krqhsh End

# xxd -s 0x00007fff916708e0 -l 528  /proc/4431/mem
7fff916708e0: e81c 6791 ff7f 0000 0000 b16f 0000 0000  ..g........o....
7fff916708f0: c018 ae17 0000 0000 0000 0000 0000 0000  ................
7fff91670900: 0000 0000 0000 0000 0100 0000 0000 0000  ................
7fff91670910: 629f e8d2 5794 18c7 0100 0000 0000 0000  b...W...........
7fff91670920: 88f1 b16f 0000 0000 0100 0000 0000 0000  ...o............
...
--//不行,要配合gdb停在哪里,再使用才行,不然程序一直运行相关地址的信息会被覆盖。

3.查看共享内存段:
SCOTT@book01p> select * from dept where deptno=20;
    DEPTNO DNAME                          LOC
---------- ------------------------------ -------------
        20 RESEARCH                       DALLAS

SCOTT@book01p> @ hashz

HASH_VALUE SQL_ID        CHILD_NUMBER KGL_BUCKET HASH_HEX   SQL_EXEC_START      SQL_EXEC_ID
---------- ------------- ------------ ---------- ---------- ------------------- -----------
  95129850 80baj2c2ur47u            0     102650   5ab90fa  2026-08-26 08:19:08    16777220

SYS@book> set linesize 300
SYS@book> @ sharepool/shp4 80baj2c2ur47u -1
HANDLE_TYPE            KGLHDADR         KGLHDPAR         C40                                        KGLHDLMD   KGLHDPMD   KGLHDIVC KGLOBHD0         KGLOBHD6           KGLOBHS0   KGLOBHS6   KGLOBT16   N0_6_16                   N20   KGLNAHSH KGLOBT03        KGLHDBID   KGLOBT09
---------------------- ---------------- ---------------- ---------------------------------------- ---------- ---------- ---------- ---------------- ---------------- ---------- ---------- ---------- --------- --------------------- ---------- ------------- ---------- ----------
child handle address   0000000069D951C8 0000000069DA77C0 select * from dept where deptno=20                1          0          0 0000000069D95008 000000006FE62EB0       8128      20224       3299     31651                 31651   95129850 80baj2c2ur47u     102650          0
parent handle address  0000000069DA77C0 0000000069DA77C0 select * from dept where deptno=20                1          0          0 0000000069DA8208 00                     4064          0          0      4064                  4064   95129850 80baj2c2ur47u     102650      65535

--//sql语句信息记录在父游标句柄+0x1c8的偏移处。注11g:0x1a8,写成如下:

$ xxd -c 40 -s $[0x0000000069DA77C0+0x1c8] -l 40 /proc/3058/mem
69da7988: 7365 6c65 6374 202a 2066 726f 6d20 6465 7074 2077 6865 7265 2064 6570 746e 6f3d 3230 0000 0000 0000  select * from dept where deptno=20......

--//再比如前几天写的 Row Cache Mutex 定位具体对象的方法.txt:
SYS@book> @mutexprofz idn,maddr,idnhex,hash,loc "ts >= trunc(sysdate)+16/24+07/1440+03/86400 and mutex_type='Row Cache'"
-- MutexProf by Tanel Poder (http://www.tanelpoder.com)
-- Showing profile of top 50 sleeps...
-- column info : id idn hash hash_value=>hash_value ts=>sleep_timestamp
--               req=>requesting_session blk=>blocking_session val=>mutex_value maddr=>mutex_addr

SUM_SLEEPS GETS_DIFF MUTEX_TYPE        IDN mutex_addr           IDNHEX          HASH GET_LOCATION                      SQL_ID  OBJECT_NAME
---------- --------- ---------- ---------- -------------------- --------- ---------- --------------------------------- ------- -----------------------------
        26    417989 Row Cache           0 0000000067304770     00000000             [10] kqreqd                               (name not found)
        24    761382 Row Cache   134224505 00000000778EC500     08001A79             [19] kqrpre                               (name not found)
        23   1070425 Row Cache   134224506 00000000778EC518     08001A7A             [17] kqrCreateUsingSecondaryKey           (name not found)
        23    311544 Row Cache           0 0000000067311450     00000000             [10] kqreqd                               (name not found)
        23   3167254 Row Cache   134224506 00000000778EC518     08001A7A             [19] kqrpre                               (name not found)
        17    274410 Row Cache           0 0000000067311450     00000000             [14] kqrScan                              (name not found)
        16    235869 Row Cache           0 0000000067304770     00000000             [14] kqrScan                              (name not found)
        14   1331002 Row Cache   134224505 00000000778EC500     08001A79             [17] kqrCreateUsingSecondaryKey           (name not found)
....

SYS@book> @ fchaz 00000000778EC500
LOC  KSMCHPTR           KSMCHIDX   KSMCHDUR KSMCHCOM                           KSMCHSIZ KSMCHCLS   KSMCHTYP KSMCHPAR         KSMCHPTR_BEGIN   KSMCHPTR_END+1
---- ---------------- ---------- ---------- -------------------------------- ---------- -------- ---------- ---------------- ---------------- -----------------
VSGA 00000000778C4998          1          1 row cache mutex                      786448 perm              0 0000000077004000 00000000778C4998 00000000779849A8

--//前面测试已经知道该类mutex每个结构体占用24字节。
--// 0- 7 字节是muext的值。
--// 8-11 字节是mutex gets的数量。
--//12-15 字节是mutex sleep的数量。
--//16-19 字节是Bucket桶号。
--//20-23 字节是转储看到的6,而opeek看都是0,表示什么不是很清楚。

$ xxd -c 24 -s $[0x00000000778EC500] -l 48 /proc/3058/mem
778ec500: 0000 0000 0000 0000 0200 0000 0000 0000 791a 0008 0000 0000  ................y.......
                                                  ~~~~~~~~~
778ec518: 0000 0000 0000 0000 0900 0000 0000 0000 7a1a 0008 0000 0000  ................z.......
--//这样每行24字节,非常好显示mutex结构。下划线表示bucket号,0x1a79 = 6777
--//缺省-g 等于2.可以改成4,8,  实现4,8个一起输出.

$ xxd -g 4 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3058/mem
778ec500: 00000000 00000000 02000000 00000000 791a0008 00000000  ................y.......
778ec518: 00000000 00000000 09000000 00000000 7a1a0008 00000000  ................z.......

$ xxd -g 8 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3058/mem
778ec500: 0000000000000000 0200000000000000 791a000800000000  ................y.......
778ec518: 0000000000000000 0900000000000000 7a1a000800000000  ................z.......

--//根据KSMCHPTR地址+16字节头+6777*24,结果一样.
$ xxd  -g 8 -c 24 -s $[0x00000000778C4998+16+6777*24] -l 48 /proc/3058/mem
778ec500: 0000000000000000 0200000000000000 791a000800000000  ................y.......
778ec518: 0000000000000000 0900000000000000 7a1a000800000000  ................z.......

--//补充可以通过过滤实现一些特殊需求,例子:
$ xxd -g 4 -c 24 -s $[0x00000000778C4998+0x10] -l $[24*0x1a7b] /proc/3135/mem | tail -2
778ec500: 00000000 00000000 12000000 00000000 791a0008 00000000  ................y.......
778ec518: 00000000 00000000 15000000 00000000 7a1a0008 00000000  ................z.......

$ xxd -g 4 -c 24 -s $[0x00000000778C4998+0x10] -l $[24*0x1a7b] /proc/3135/mem | sed -n  '6778p;6779p'
778ec500: 00000000 00000000 12000000 00000000 791a0008 00000000  ................y.......
778ec518: 00000000 00000000 15000000 00000000 7a1a0008 00000000  ................z.......

--//如果因为intel cpu 大小头问题,通过od处理满足需求。
$ xxd -g 4 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3135/mem| cut -d: -f2- | xxd -r -p | od -t x4 -A d -w24
0000000 00000000 00000000 00000012 00000000 08001a79 00000000
0000024 00000000 00000000 00000015 00000000 08001a7a 00000000
0000048

$ xxd -g 4 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3135/mem| cut -d: -f2- | xxd -r -p | od -t x2 -A d -w24
0000000 0000 0000 0000 0000 0012 0000 0000 0000 1a79 0800 0000 0000
0000024 0000 0000 0000 0000 0015 0000 0000 0000 1a7a 0800 0000 0000
0000048

$ xxd -g 4 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3135/mem| cut -d: -f2- | xxd -r -p | od -t d4 -A d -w24
0000000           0           0          18           0   134224505           0
0000024           0           0          21           0   134224506           0
0000048

$ xxd -g 4 -c 24 -s $[0x00000000778EC500] -l 48 /proc/3135/mem| cut -d: -f2- | xxd -r -p | od -t d2 -A d -w24
0000000      0      0      0      0     18      0      0      0   6777   2048      0      0
0000024      0      0      0      0     21      0      0      0   6778   2048      0      0
0000048

4.查看数据块:
SCOTT@book01p> select rowid,dept.* from dept;
ROWID                  DEPTNO DNAME                          LOC
------------------ ---------- ------------------------------ -------------
AAASg0AAMAAAACGAAA         10 ACCOUNTING                     NEW YORK
AAASg0AAMAAAACGAAB         20 RESEARCH                       DALLAS
AAASg0AAMAAAACGAAC         30 SALES                          CHICAGO
AAASg0AAMAAAACGAAD         40 OPERATIONS                     BOSTON

SCOTT@book01p> @ rowid AAASg0AAMAAAACGAAA
DATA_OBJECT_ID       FILE      BLOCK        ROW ROWID_DBA                                DBA                  TEXT
-------------- ---------- ---------- ---------- ---------------------------------------- -------------------- --------------------------------------------------
         75828         12        134          0  0x3000086                               12,134               alter system dump datafile 12 block 134 ;

SYS@book> @ bh 12 134
   INST_ID HLADDR           DBARFIL      DBABLK CLASS CLASS_TYPE         STATE             TCH CR_SCN_BAS CR_SCN_WRP CR_UBA_FIL CR_UBA_BLK CR_UBA_SEQ BA               LE_ADDR          OBJECT_NAME
---------- ---------------- ------- ----------- ----- ------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------- ---------------- --------------------
         1 000000007BDDDA08      12         134     1 data block         xcur                3          0          0          0          0          0 00000000966FE000 00

--//BA=00000000966FE000,数据块在共享内存段位置。

$ xxd -a  -c 64 -s $[0x00000000966FE000] -l 8192 /proc/3058/mem
966fe000: 06a2 0000 8600 0003 f5d1 2900 0000 0106 80d0 0000 0100 0000 3428 0100 e8d1 2900 0080 0000 0200 3200 8000 0003 0600 1100 7202 0000 981b 4002 c600 3300 0420 0000  ..........).............4(....).......2.........r.....@...3.. ..
966fe040: f5d1 2900 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0400 ffff 1a00 3c1f 221f 221f 0000 0400 7e1f 681f 541f 3c1f 0000  ..).........................................<.".".....~.h.T.<...
966fe080: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000  ................................................................
*
966fff80: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 2c01 0302 c129 0a4f 5045 5241 5449 4f4e 5306 424f 5354 4f4e 2c01 0302 c11f 0553  ................................,....).OPERATIONS.BOSTON,......S
966fffc0: 414c 4553 0743 4849 4341 474f 2c01 0302 c115 0852 4553 4541 5243 4806 4441 4c4c 4153 2c01 0302 c10b 0a41 4343 4f55 4e54 494e 4708 4e45 5720 594f 524b 0106 f5d1  ALES.CHICAGO,......RESEARCH.DALLAS,......ACCOUNTING.NEW YORK....
posted @ 2026-08-27 21:22  lfree  阅读(4)  评论(0)    收藏  举报