Skip to content

Commit 4578cd9

Browse files
Rtoaxyonghong-song
authored andcommitted
tools/execsnoop: Add -M,--print-pcomm argument
Sometimes the parent process is executed instantly, tracing the parent command would be a good choice know who executed the command. Thus, add PCOMM. At the same time, rename the original PCOMM to COMM, and use PCOMM as the parent command. Before: $ sudo ./execsnoop.py COMM PID PPID RET ARGS sh 44789 44682 0 /bin/sh -c cd /home/... gcc 44788 44682 0 /usr/bin/gcc -DCAPST... After: $ sudo ./execsnoop.py -M COMM PID PCOMM PPID RET ARGS sh 44789 make 44682 0 /bin/sh -c cd /home/... gcc 44788 make 44682 0 /usr/bin/gcc -DCAPST... ^^^^^^^^^^^^^^^^ Signed-off-by: Jiang Guirong <[email protected]> Signed-off-by: Rong Tao <[email protected]>
1 parent 5d2ef17 commit 4578cd9

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

tools/execsnoop.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def parse_uid(user):
5555
./execsnoop -P 181 # only trace new processes whose parent PID is 181
5656
./execsnoop -U # include UID
5757
./execsnoop -C # include CPU
58+
./execsnoop -M # include PCOMM
5859
./execsnoop -u 1000 # only trace UID 1000
5960
./execsnoop -u user # get user UID and trace only them
6061
./execsnoop -t # include timestamps
@@ -93,6 +94,8 @@ def parse_uid(user):
9394
help="print UID column")
9495
parser.add_argument("-C", "--print-cpu", action="store_true",
9596
help="print CPU column")
97+
parser.add_argument("-M", "--print-pcomm", action="store_true",
98+
help="print parent command")
9699
parser.add_argument("--max-args", default="20",
97100
help="maximum number of arguments parsed and displayed, defaults to 20")
98101
parser.add_argument("-P", "--ppid",
@@ -131,6 +134,7 @@ def check_cpu_filed():
131134
u32 uid;
132135
u32 cpu;
133136
char comm[TASK_COMM_LEN];
137+
char pcomm[TASK_COMM_LEN];
134138
enum event_type type;
135139
char argv[ARGSIZE];
136140
int retval;
@@ -180,6 +184,7 @@ def check_cpu_filed():
180184
// as the real_parent->tgid.
181185
// We use the get_ppid function as a fallback in those cases. (#1883)
182186
data.ppid = task->real_parent->tgid;
187+
bpf_probe_read_kernel_str(&data.pcomm, sizeof(data.pcomm), task->real_parent->comm);
183188
184189
PPID_FILTER
185190
@@ -222,6 +227,7 @@ def check_cpu_filed():
222227
// as the real_parent->tgid.
223228
// We use the get_ppid function as a fallback in those cases. (#1883)
224229
data.ppid = task->real_parent->tgid;
230+
bpf_probe_read_kernel_str(&data.pcomm, sizeof(data.pcomm), task->real_parent->comm);
225231
data.cpu = CPU_RUNNING_ON;
226232
227233
PPID_FILTER
@@ -275,10 +281,13 @@ def check_cpu_filed():
275281
print("%-8s" % ("TIME(s)"), end="")
276282
if args.print_uid:
277283
print("%-6s" % ("UID"), end="")
284+
print("%-16s %-7s " % ("COMM", "PID"), end="")
285+
if args.print_pcomm:
286+
print("%-16s " % ("PCOMM"), end="")
287+
print("%-7s " % ("PPID"), end="")
278288
if args.print_cpu:
279-
print("%-16s %-7s %-7s %-4s %3s %s" % ("PCOMM", "PID", "PPID", "CPU", "RET", "ARGS"))
280-
else:
281-
print("%-16s %-7s %-7s %3s %s" % ("PCOMM", "PID", "PPID", "RET", "ARGS"))
289+
print("%-4s " % ("CPU"), end="")
290+
print("%3s %s" % ("RET", "ARGS"))
282291

283292
class EventType(object):
284293
EVENT_ARG = 0
@@ -332,12 +341,13 @@ def print_event(cpu, data, size):
332341
ppid = event.ppid if event.ppid > 0 else get_ppid(event.pid)
333342
ppid = b"%d" % ppid if ppid > 0 else b"?"
334343
argv_text = b' '.join(argv[event.pid]).replace(b'\n', b'\\n')
344+
printb(b"%-16s %-7d " % (event.comm, event.pid), nl="")
345+
if args.print_pcomm:
346+
printb(b"%-16s " % (event.pcomm), nl="")
347+
printb(b"%-7s " % (ppid), nl="")
335348
if args.print_cpu:
336-
printb(b"%-16s %-7d %-7s %-4d %3d %s" % (event.comm, event.pid,
337-
ppid, event.cpu, event.retval, argv_text))
338-
else:
339-
printb(b"%-16s %-7d %-7s %3d %s" % (event.comm, event.pid,
340-
ppid, event.retval, argv_text))
349+
printb(b"%-4d " % (event.cpu), nl="")
350+
printb(b"%3d %s" % (event.retval, argv_text))
341351
try:
342352
del(argv[event.pid])
343353
except Exception:

tools/execsnoop_example.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ execsnoop traces new processes. For example, tracing the commands invoked when
55
running "man ls":
66

77
# ./execsnoop
8-
PCOMM PID RET ARGS
8+
COMM PID RET ARGS
99
bash 15887 0 /usr/bin/man ls
1010
preconv 15894 0 /usr/bin/preconv -e UTF-8
1111
man 15896 0 /usr/bin/tbl
@@ -16,8 +16,8 @@ nroff 15901 0 /usr/bin/groff -mtty-char -Tutf8 -mandoc -rLL=169n -
1616
groff 15902 0 /usr/bin/troff -mtty-char -mandoc -rLL=169n -rLT=169n -Tutf8
1717
groff 15903 0 /usr/bin/grotty
1818

19-
The output shows the parent process/command name (PCOMM), the PID, the return
20-
value of the exec() (RET), and the filename with arguments (ARGS).
19+
The output shows the process/command name (COMM), the PID, the return value of
20+
the exec() (RET), and the filename with arguments (ARGS).
2121

2222
This works by traces the execve() system call (commonly used exec() variant),
2323
and shows details of the arguments and return value. This catches new processes
@@ -29,7 +29,7 @@ processes, which won't be included in the execsnoop output.
2929
The -x option can be used to include failed exec()s. For example:
3030

3131
# ./execsnoop -x
32-
PCOMM PID RET ARGS
32+
COMM PID RET ARGS
3333
supervise 9660 0 ./run
3434
supervise 9661 0 ./run
3535
mkdir 9662 0 /bin/mkdir -p ./main
@@ -57,7 +57,7 @@ are allowed.
5757
For example, matching commands containing "mount":
5858

5959
# ./execsnoop -Ttn mount
60-
TIME TIME(s) PCOMM PID PPID RET ARGS
60+
TIME TIME(s) COMM PID PPID RET ARGS
6161
14:08:23 2.849 mount 18049 1045 0 /bin/mount -p
6262

6363
The -l option can be used to only show command where one of the arguments
@@ -66,7 +66,7 @@ arguments of the command. For example, matching all command where one of the arg
6666
is "testpkg":
6767

6868
# ./execsnoop.py -l testpkg
69-
PCOMM PID PPID RET ARGS
69+
COMM PID PPID RET ARGS
7070
service 3344535 4146419 0 /usr/sbin/service testpkg status
7171
systemctl 3344535 4146419 0 /bin/systemctl status testpkg.service
7272
yum 3344856 4146419 0 /usr/local/bin/yum remove testpkg
@@ -89,15 +89,15 @@ The -U option include UID on output:
8989

9090
# ./execsnoop -U
9191

92-
UID PCOMM PID PPID RET ARGS
92+
UID COMM PID PPID RET ARGS
9393
1000 ls 171318 133702 0 /bin/ls --color=auto
9494
1000 w 171322 133702 0 /usr/bin/w
9595

9696
The -u options filters output based process UID. You also can use username as
9797
argument, in that cause UID will be looked up using getpwnam (see man 3 getpwnam).
9898

9999
# ./execsnoop -Uu 1000
100-
UID PCOMM PID PPID RET ARGS
100+
UID COMM PID PPID RET ARGS
101101
1000 ls 171335 133702 0 /bin/ls --color=auto
102102
1000 man 171340 133702 0 /usr/bin/man getpwnam
103103
1000 bzip2 171341 171340 0 /bin/bzip2 -dc

0 commit comments

Comments
 (0)