diff mbox series

[v3,4/7] tracing/probes: support '%pD' type for print struct file's name

Message ID 20240123025608.2370978-5-yebin10@huawei.com (mailing list archive)
State Superseded
Headers show
Series support '%pd' and '%pD' for print file name | expand

Commit Message

yebin (H) Jan. 23, 2024, 2:56 a.m. UTC
Similar to '%pD' for printk, use '%pD' for print struct file's name.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 kernel/trace/trace_probe.c | 41 ++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 15 deletions(-)

Comments

Steven Rostedt Jan. 23, 2024, 3:03 a.m. UTC | #1
On Tue, 23 Jan 2024 10:56:05 +0800
Ye Bin <yebin10@huawei.com> wrote:

> Similar to '%pD' for printk, use '%pD' for print struct file's name.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>
> ---
>  kernel/trace/trace_probe.c | 41 ++++++++++++++++++++++++--------------
>  1 file changed, 26 insertions(+), 15 deletions(-)
> 
> diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
> index cc8bd7ea5341..6215b9573793 100644
> --- a/kernel/trace/trace_probe.c
> +++ b/kernel/trace/trace_probe.c
> @@ -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"
> @@ -1574,28 +1575,38 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char *buf,
>  	for (i = 0; i < argc; i++) {
>  		size_t idx;
>  
> -		if (str_has_suffix(argv[i], ":%pd", &idx)) {
> -			char *tmp = kstrdup(argv[i], GFP_KERNEL);
> -			char *equal;
> +		if (!str_has_suffix(argv[i], ":%pd", &idx) &&
> +		    !str_has_suffix(argv[i], ":%pD", &idx))
> +			continue;
>  
> -			if (!tmp)
> -				return -ENOMEM;
> +		char *tmp = kstrdup(argv[i], GFP_KERNEL);
> +		char *equal;
> +
> +		if (!tmp)
> +			return -ENOMEM;
>  
> -			equal = strchr(tmp, '=');
> -			if (equal)
> -				*equal = '\0';
> -			tmp[idx] = '\0';
> +		equal = strchr(tmp, '=');
> +		if (equal)
> +			*equal = '\0';
> +		tmp[idx] = '\0';
> +		if (argv[i][strlen(argv[i]) - 1] == 'd')

You can use idx again here:

		if (argv[i][idx + 3] == 'd')

and save from doing the strlen(argv[i]);

idx will point to ':', and + 3 would point to either 'd' or 'D'

-- Steve

>  			ret = snprintf(buf + 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)
> -				return -ENOMEM;
> -			argv[i] = buf + used;
> -			used += ret + 1;
> -		}
> +		else
> +			ret = snprintf(buf + 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)
> +			return -ENOMEM;
> +		argv[i] = buf + used;
> +		used += ret + 1;
>  	}
>  
>  	return 0;
diff mbox series

Patch

diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
index cc8bd7ea5341..6215b9573793 100644
--- a/kernel/trace/trace_probe.c
+++ b/kernel/trace/trace_probe.c
@@ -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"
@@ -1574,28 +1575,38 @@  int traceprobe_expand_dentry_args(int argc, const char *argv[], char *buf,
 	for (i = 0; i < argc; i++) {
 		size_t idx;
 
-		if (str_has_suffix(argv[i], ":%pd", &idx)) {
-			char *tmp = kstrdup(argv[i], GFP_KERNEL);
-			char *equal;
+		if (!str_has_suffix(argv[i], ":%pd", &idx) &&
+		    !str_has_suffix(argv[i], ":%pD", &idx))
+			continue;
 
-			if (!tmp)
-				return -ENOMEM;
+		char *tmp = kstrdup(argv[i], GFP_KERNEL);
+		char *equal;
+
+		if (!tmp)
+			return -ENOMEM;
 
-			equal = strchr(tmp, '=');
-			if (equal)
-				*equal = '\0';
-			tmp[idx] = '\0';
+		equal = strchr(tmp, '=');
+		if (equal)
+			*equal = '\0';
+		tmp[idx] = '\0';
+		if (argv[i][strlen(argv[i]) - 1] == 'd')
 			ret = snprintf(buf + 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)
-				return -ENOMEM;
-			argv[i] = buf + used;
-			used += ret + 1;
-		}
+		else
+			ret = snprintf(buf + 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)
+			return -ENOMEM;
+		argv[i] = buf + used;
+		used += ret + 1;
 	}
 
 	return 0;