tracing/probes: support '%pD' type for print struct file's name
authorYe Bin <yebin10@huawei.com>
Fri, 22 Mar 2024 06:43:05 +0000 (14:43 +0800)
committerMasami Hiramatsu (Google) <mhiramat@kernel.org>
Wed, 1 May 2024 14:18:47 +0000 (23:18 +0900)
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/20240322064308.284457-3-yebin10@huawei.com/
[Masami: replaced "previous patch" with '%pd' type]

Signed-off-by: Ye Bin <yebin10@huawei.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
kernel/trace/trace.c
kernel/trace/trace_probe.c

index 869978d8467291841a55569a34fc7be5273b791c..7fe4b539a42347ec4729dc7ee236e68bf2a2f9c3 100644 (file)
@@ -5540,7 +5540,7 @@ static const char readme_msg[] =
        "\t     kernel return probes support: $retval, $arg<N>, $comm\n"
        "\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
        "\t           b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
-       "\t           symstr, %pd, <type>\\[<array-size>\\]\n"
+       "\t           symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
 #ifdef CONFIG_HIST_TRIGGERS
        "\t    field: <stype> <name>;\n"
        "\t    stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
index f7ee97e45d1f3aacde1ffae7aef1bbb2998fc2bc..d4a887dac821d6f7274e507079f58198c012d883 100644 (file)
@@ -12,6 +12,7 @@
 #define pr_fmt(fmt)    "trace_probe: " fmt
 
 #include <linux/bpf.h>
+#include <linux/fs.h>
 #include "trace_btf.h"
 
 #include "trace_probe.h"
@@ -1751,35 +1752,47 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
 
        used = 0;
        for (i = 0; i < argc; i++) {
-               if (glob_match("*:%pd", argv[i])) {
-                       char *tmp;
-                       char *equal;
-
-                       if (!tmpbuf) {
-                               tmpbuf = kmalloc(bufsize, GFP_KERNEL);
-                               if (!tmpbuf)
-                                       return -ENOMEM;
-                       }
+               char *tmp;
+               char *equal;
+               size_t arg_len;
 
-                       tmp = kstrdup(argv[i], GFP_KERNEL);
-                       if (!tmp)
-                               goto nomem;
+               if (!glob_match("*:%p[dD]", argv[i]))
+                       continue;
 
-                       equal = strchr(tmp, '=');
-                       if (equal)
-                               *equal = '\0';
-                       tmp[strlen(argv[i]) - 4] = '\0';
+               if (!tmpbuf) {
+                       tmpbuf = kmalloc(bufsize, GFP_KERNEL);
+                       if (!tmpbuf)
+                               return -ENOMEM;
+               }
+
+               tmp = kstrdup(argv[i], GFP_KERNEL);
+               if (!tmp)
+                       goto nomem;
+
+               equal = strchr(tmp, '=');
+               if (equal)
+                       *equal = '\0';
+               arg_len = strlen(argv[i]);
+               tmp[arg_len - 4] = '\0';
+               if (argv[i][arg_len - 1] == 'd')
                        ret = snprintf(tmpbuf + used, bufsize - used,
                                       "%s%s+0x0(+0x%zx(%s)):string",
                                       equal ? tmp : "", equal ? "=" : "",
                                       offsetof(struct dentry, d_name.name),
                                       equal ? equal + 1 : tmp);
-                       kfree(tmp);
-                       if (ret >= bufsize - used)
-                               goto nomem;
-                       argv[i] = tmpbuf + used;
-                       used += ret + 1;
-               }
+               else
+                       ret = snprintf(tmpbuf + used, bufsize - used,
+                                      "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
+                                      equal ? tmp : "", equal ? "=" : "",
+                                      offsetof(struct dentry, d_name.name),
+                                      offsetof(struct file, f_path.dentry),
+                                      equal ? equal + 1 : tmp);
+
+               kfree(tmp);
+               if (ret >= bufsize - used)
+                       goto nomem;
+               argv[i] = tmpbuf + used;
+               used += ret + 1;
        }
 
        *buf = tmpbuf;