Skip to content

Commit 6ea2a0e

Browse files
authored
libbpf-tools: sigsnoop: display target process's comm (#5272)
Directly print the comm of the target process to display information more intuitively, without having to use commands such as 'ps' to query the comm information of the pid process again. $ sudo ./sigsnoop -n TIME PID COMM SIG TPID TCOMM RESULT 11:29:06 1128124 vte-urlencode-c SIGCHLD 1127674 bash 0 11:29:08 1128126 ls SIGCHLD 1127674 bash 0 11:29:08 1128127 vte-urlencode-c SIGCHLD 1127674 bash 0 11:29:08 1881 Xorg SIGALRM 1881 Xorg 0 11:29:11 0 swapper/3 SIGNAL-34 2545 sssd_kcm 0 11:29:21 1127194 kworker/u48:6 SIGINT 1127674 bash 0 11:29:21 1127674 bash SIGINT 1127674 bash 0 11:29:21 1128130 vte-urlencode-c SIGCHLD 1127674 bash 0 11:29:21 1881 Xorg SIGALRM 1881 Xorg 0 11:29:21 0 swapper/1 SIGNAL-34 2545 sssd_kcm 0 ^^^^^^^^^^^^^^^^ If current kernel is unsupport bpf_task_from_pid(), linux < v6.1 [1], the TCOMM field displays 'N/A': $ sudo ./sigsnoop -n WARNING: Current kernel not support bpf_task_from_pid(), ignore TCOMM field TIME PID COMM SIG TPID TCOMM RESULT 15:01:24 289689 pmsleep SIGCHLD 289348 N/A 0 15:01:24 289691 expr SIGCHLD 289348 N/A 0 15:01:25 289693 pmprobe SIGCHLD 289692 N/A 0 15:01:25 289694 gawk SIGCHLD 289692 N/A 0 15:01:25 289692 pmlogger_check SIGCHLD 289348 N/A 0 15:01:25 289695 pmsleep SIGCHLD 289348 N/A 0 15:01:25 289696 expr SIGCHLD 289348 N/A 0 ^^^^^^^^^^^^^^^^ Link: torvalds/linux@3f0e6f2b41d3 [1] Signed-off-by: Rong Tao <[email protected]>
1 parent 2940304 commit 6ea2a0e

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

libbpf-tools/sigsnoop.bpf.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,31 @@ struct {
2323
__uint(value_size, sizeof(__u32));
2424
} events SEC(".maps");
2525

26-
static __always_inline bool is_target_signal(int sig) {
27-
if (target_signals == 0)
28-
return true;
26+
static __always_inline bool is_target_signal(int sig)
27+
{
28+
if (target_signals == 0)
29+
return true;
30+
31+
if ((target_signals & (1 << (sig - 1))) == 0)
32+
return false;
2933

30-
if ((target_signals & (1 << (sig - 1))) == 0)
31-
return false;
34+
return true;
35+
}
3236

33-
return true;
37+
static __always_inline void get_tcomm(pid_t tpid, char *tcomm, __u32 size)
38+
{
39+
if (bpf_ksym_exists(bpf_task_from_pid)) {
40+
struct task_struct *ttask = bpf_task_from_pid(tpid);
41+
if (ttask) {
42+
bpf_probe_read_kernel(tcomm, size, ttask->comm);
43+
bpf_task_release(ttask);
44+
return;
45+
}
46+
}
47+
tcomm[0] = 'N';
48+
tcomm[1] = '/';
49+
tcomm[2] = 'A';
50+
tcomm[3] = '\0';
3451
}
3552

3653
static int probe_entry(pid_t tpid, int sig)
@@ -48,6 +65,8 @@ static int probe_entry(pid_t tpid, int sig)
4865
if (filtered_pid && pid != filtered_pid)
4966
return 0;
5067

68+
get_tcomm(tpid, event.tcomm, sizeof(event.tcomm));
69+
5170
event.pid = pid;
5271
event.tpid = tpid;
5372
event.sig = sig;
@@ -142,6 +161,8 @@ int sig_trace(struct trace_event_raw_signal_generate *ctx)
142161
if (filtered_pid && pid != filtered_pid)
143162
return 0;
144163

164+
get_tcomm(tpid, event.tcomm, sizeof(event.tcomm));
165+
145166
event.pid = pid;
146167
event.tpid = tpid;
147168
event.sig = sig;

libbpf-tools/sigsnoop.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <time.h>
1414

1515
#include <bpf/bpf.h>
16+
#include <bpf/btf.h>
1617
#include "sigsnoop.h"
1718
#include "sigsnoop.skel.h"
1819

@@ -189,6 +190,23 @@ static void alias_parse(char *prog)
189190
}
190191
}
191192

193+
/**
194+
* since linux commit 3f0e6f2b41d3 ("bpf: Add bpf_task_from_pid() kfunc")
195+
* v6.1-rc4-1163-g3f0e6f2b41d3 support bpf_task_from_pid() helper.
196+
*/
197+
static bool support_bpf_task_from_pid(void)
198+
{
199+
const struct btf *btf = btf__load_vmlinux_btf();
200+
int type_id;
201+
202+
type_id = btf__find_by_name_kind(btf, "bpf_task_from_pid",
203+
BTF_KIND_FUNC);
204+
if (type_id < 0)
205+
return false;
206+
207+
return true;
208+
}
209+
192210
static void sig_int(int signo)
193211
{
194212
exiting = 1;
@@ -205,11 +223,11 @@ static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
205223
tm = localtime(&t);
206224
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
207225
if (signal_name && e->sig < ARRAY_SIZE(sig_name))
208-
printf("%-8s %-7d %-16s %-12s %-7d %-6d\n",
209-
ts, e->pid, e->comm, sig_name[e->sig], e->tpid, e->ret);
226+
printf("%-8s %-7d %-16s %-12s %-7d %-16s %-6d\n",
227+
ts, e->pid, e->comm, sig_name[e->sig], e->tpid, e->tcomm, e->ret);
210228
else
211-
printf("%-8s %-7d %-16s %-12d %-7d %-6d\n",
212-
ts, e->pid, e->comm, e->sig, e->tpid, e->ret);
229+
printf("%-8s %-7d %-16s %-12d %-7d %-16s %-6d\n",
230+
ts, e->pid, e->comm, e->sig, e->tpid, e->tcomm, e->ret);
213231
}
214232

215233
static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
@@ -281,8 +299,12 @@ int main(int argc, char **argv)
281299
goto cleanup;
282300
}
283301

284-
printf("%-8s %-7s %-16s %-12s %-7s %-6s\n",
285-
"TIME", "PID", "COMM", "SIG", "TPID", "RESULT");
302+
if (!support_bpf_task_from_pid())
303+
fprintf(stderr, "WARNING: Current kernel not support "\
304+
"bpf_task_from_pid(), ignore TCOMM field\n");
305+
306+
printf("%-8s %-7s %-16s %-12s %-7s %-16s %-6s\n",
307+
"TIME", "PID", "COMM", "SIG", "TPID", "TCOMM", "RESULT");
286308

287309
while (!exiting) {
288310
err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS);

libbpf-tools/sigsnoop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct event {
1111
int sig;
1212
int ret;
1313
char comm[TASK_COMM_LEN];
14+
char tcomm[TASK_COMM_LEN];
1415
};
1516

1617
#endif /* __SIGSNOOP_H */

0 commit comments

Comments
 (0)