Skip to content

Commit

Permalink
tools/shmsnoop: Print shmctl command string
Browse files Browse the repository at this point in the history
Print shmctl command:

    $ sudo ./shmsnoop.py
    PID    COMM        SYS           RET ARGs
    16413  writer   SHMGET            31 key: 0xd2032983, size: 4194304, shmflg: 0x3a4 (IPC_CREAT|0644)
    16413  writer    SHMAT  7f7f572b8000 shmid: 0x31, shmaddr: 0x0, shmflg: 0x0
    16413  writer    SHMDT             0 shmaddr: 0x7f7f572b8000
    16418  reader   SHMGET            31 key: 0xd2032983, size: 4194304, shmflg: 0x1a4 (0644)
    16418  reader    SHMAT  7f28e81a4000 shmid: 0x31, shmaddr: 0x0, shmflg: 0x0
    16418  reader    SHMDT             0 shmaddr: 0x7f28e81a4000
    16418  reader   SHMCTL             0 shmid: 0x31, cmd: 0 (IPC_RMID), buf: 0x0
                                                             ^^^^^^^^^^

Signed-off-by: Rong Tao <[email protected]>
  • Loading branch information
Rtoax authored and yonghong-song committed Aug 21, 2024
1 parent e4da840 commit 3130fe8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
25 changes: 24 additions & 1 deletion tools/shmsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,28 @@ def sys_name(sys):
{ 'name' : 'SHM_EXEC', 'value' : 0o100000 },
]

shmctl_cmds = [
('IPC_RMID', 0),
('IPC_SET', 1),
('IPC_STAT', 2),
('IPC_INFO', 3),
('SHM_LOCK', 11),
('SHM_UNLOCK', 12),
('SHM_STAT', 13),
('SHM_INFO', 14),
('SHM_STAT_ANY', 15),
]

def _decode_cmd(cmd, cmd_list):
for str_cmd, cmd_val in cmd_list:
if cmd == cmd_val:
return str_cmd
return '0x{:x}'.format(cmd)

def shmctl_cmd(cmd):
return _decode_cmd(cmd, shmctl_cmds)


def shmflg_str(val, flags):
cur = filter(lambda x : x['value'] & val, flags)
str = "0x%x" % val
Expand Down Expand Up @@ -293,7 +315,8 @@ def print_event(cpu, data, size):
print("shmaddr: 0x%lx" % (event.shmaddr))

if event.sys == SYS_SHMCTL:
print("shmid: 0x%lx, cmd: %lu, buf: 0x%x" % (event.shmid, event.cmd, event.buf))
print("shmid: 0x%lx, cmd: %lu (%s), buf: 0x%x" % (event.shmid,
event.cmd, shmctl_cmd(event.cmd), event.buf))

# loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64)
Expand Down
6 changes: 3 additions & 3 deletions tools/shmsnoop_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PID COMM SYS RET ARGs
19816 client SHMAT 7f4fd8ee7000 shmid: 0x10000, shmaddr: 0x0, shmflg: 0x0
19816 client SHMDT 0 shmaddr: 0x7f4fd8ee7000
19813 server SHMDT 0 shmaddr: 0x7f1cf8b1f000
19813 server SHMCTL 0 shmid: 0x10000, cmd: 0, buf: 0x0
19813 server SHMCTL 0 shmid: 0x10000, cmd: 0 (IPC_RMID), buf: 0x0


Every call the shm* syscall (SHM column) is displayed
Expand All @@ -32,15 +32,15 @@ containing "server" with timestamps:
# ./shmsnoop.py -T -n server
TIME(s) PID COMM SYS RET ARGs
0.563194000 19825 server SHMDT 0 shmaddr: 0x7f74362e4000
0.563237000 19825 server SHMCTL 0 shmid: 0x18000, cmd: 0, buf: 0x0
0.563237000 19825 server SHMCTL 0 shmid: 0x18000, cmd: 0 (IPC_RMID), buf: 0x0


A -p option can be used to trace only selected process:

# ./shmsnoop.py -p 19855
PID COMM SYS RET ARGs
19855 server SHMDT 0 shmaddr: 0x7f4329ff8000
19855 server SHMCTL 0 shmid: 0x20000, cmd: 0, buf: 0x0
19855 server SHMCTL 0 shmid: 0x20000, cmd: 0 (IPC_RMID), buf: 0x0

USAGE message:
# ./shmsnoop.py -h
Expand Down

0 comments on commit 3130fe8

Please sign in to comment.