Skip to content

Commit 20fe4d0

Browse files
Ye Binmhiramat
authored andcommitted
tracing/probes: support '%pD' type for print struct file's name
As like '%pd' type, this patch supports print type '%pD' for print file's name. For example "name=$arg1:%pD" casts the `$arg1` as (struct file*), dereferences the "file.f_path.dentry.d_name.name" field and stores it to "name" argument as a kernel string. Here is an example: [tracing]# echo 'p:testprobe vfs_read name=$arg1:%pD' > kprobe_event [tracing]# echo 1 > events/kprobes/testprobe/enable [tracing]# grep -q "1" events/kprobes/testprobe/enable [tracing]# echo 0 > events/kprobes/testprobe/enable [tracing]# grep "vfs_read" trace | grep "enable" grep-15108 [003] ..... 5228.328609: testprobe: (vfs_read+0x4/0xbb0) name="enable" Note that this expects the given argument (e.g. $arg1) is an address of struct file. User must ensure it. Link: https://lore.kernel.org/all/[email protected]/ [Masami: replaced "previous patch" with '%pd' type] Signed-off-by: Ye Bin <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent d9b1522 commit 20fe4d0

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

kernel/trace/trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5540,7 +5540,7 @@ static const char readme_msg[] =
55405540
"\t kernel return probes support: $retval, $arg<N>, $comm\n"
55415541
"\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
55425542
"\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5543-
"\t symstr, %pd, <type>\\[<array-size>\\]\n"
5543+
"\t symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
55445544
#ifdef CONFIG_HIST_TRIGGERS
55455545
"\t field: <stype> <name>;\n"
55465546
"\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"

kernel/trace/trace_probe.c

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define pr_fmt(fmt) "trace_probe: " fmt
1313

1414
#include <linux/bpf.h>
15+
#include <linux/fs.h>
1516
#include "trace_btf.h"
1617

1718
#include "trace_probe.h"
@@ -1751,35 +1752,47 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
17511752

17521753
used = 0;
17531754
for (i = 0; i < argc; i++) {
1754-
if (glob_match("*:%pd", argv[i])) {
1755-
char *tmp;
1756-
char *equal;
1757-
1758-
if (!tmpbuf) {
1759-
tmpbuf = kmalloc(bufsize, GFP_KERNEL);
1760-
if (!tmpbuf)
1761-
return -ENOMEM;
1762-
}
1755+
char *tmp;
1756+
char *equal;
1757+
size_t arg_len;
17631758

1764-
tmp = kstrdup(argv[i], GFP_KERNEL);
1765-
if (!tmp)
1766-
goto nomem;
1759+
if (!glob_match("*:%p[dD]", argv[i]))
1760+
continue;
17671761

1768-
equal = strchr(tmp, '=');
1769-
if (equal)
1770-
*equal = '\0';
1771-
tmp[strlen(argv[i]) - 4] = '\0';
1762+
if (!tmpbuf) {
1763+
tmpbuf = kmalloc(bufsize, GFP_KERNEL);
1764+
if (!tmpbuf)
1765+
return -ENOMEM;
1766+
}
1767+
1768+
tmp = kstrdup(argv[i], GFP_KERNEL);
1769+
if (!tmp)
1770+
goto nomem;
1771+
1772+
equal = strchr(tmp, '=');
1773+
if (equal)
1774+
*equal = '\0';
1775+
arg_len = strlen(argv[i]);
1776+
tmp[arg_len - 4] = '\0';
1777+
if (argv[i][arg_len - 1] == 'd')
17721778
ret = snprintf(tmpbuf + used, bufsize - used,
17731779
"%s%s+0x0(+0x%zx(%s)):string",
17741780
equal ? tmp : "", equal ? "=" : "",
17751781
offsetof(struct dentry, d_name.name),
17761782
equal ? equal + 1 : tmp);
1777-
kfree(tmp);
1778-
if (ret >= bufsize - used)
1779-
goto nomem;
1780-
argv[i] = tmpbuf + used;
1781-
used += ret + 1;
1782-
}
1783+
else
1784+
ret = snprintf(tmpbuf + used, bufsize - used,
1785+
"%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
1786+
equal ? tmp : "", equal ? "=" : "",
1787+
offsetof(struct dentry, d_name.name),
1788+
offsetof(struct file, f_path.dentry),
1789+
equal ? equal + 1 : tmp);
1790+
1791+
kfree(tmp);
1792+
if (ret >= bufsize - used)
1793+
goto nomem;
1794+
argv[i] = tmpbuf + used;
1795+
used += ret + 1;
17831796
}
17841797

17851798
*buf = tmpbuf;

0 commit comments

Comments
 (0)