diff mbox series

[v3] trace-cmd: Load trace-cmd plugins from build folder, if exists

Message ID 20190423143344.30645-1-tstoyanov@vmware.com (mailing list archive)
State Accepted
Commit a8faf36eae170f02f3ec2445538c74a90bba61bf
Headers show
Series [v3] trace-cmd: Load trace-cmd plugins from build folder, if exists | expand

Commit Message

Tzvetomir Stoyanov April 23, 2019, 2:33 p.m. UTC
[
 v3 changes:
  - Simplified the logic, using standard library functions.
  - Added check to ensure the binary is inside the source tree.
 v2 changes:
  - Removed the logic looking for plugins in the build path.
  - Added logic which gets the full path of the running trace-cmd
    binary and loads plugins from "plugins" directory around it, if exist.
]

When a development version of trace-cmd is built and run on the machine,
by default it loads all plugins from predefined drierctories :
  (install_preffix)/lib/traceevent/plugins
  ~/.traceevent/plugins
  the path specified in TRACEEVENT_PLUGIN_DIR environment variable.
Thus, the development plugins will not be loaded. To simplify the development
process, a new logic is added:
  At plugins load time, check the location of trace-cmd application and look
  for "plugins" directory around it. If found, load plugins from it. Those
  pluigins will be loaded last, so in case of duplication the "development"
  plugins win.

Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
---
 lib/trace-cmd/trace-util.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Steven Rostedt April 23, 2019, 2:58 p.m. UTC | #1
On Tue, 23 Apr 2019 17:33:44 +0300
Tzvetomir Stoyanov <tstoyanov@vmware.com> wrote:

> [
>  v3 changes:
>   - Simplified the logic, using standard library functions.
>   - Added check to ensure the binary is inside the source tree.
>  v2 changes:
>   - Removed the logic looking for plugins in the build path.
>   - Added logic which gets the full path of the running trace-cmd
>     binary and loads plugins from "plugins" directory around it, if exist.
> ]

Hi Tzvetomir,

FYI, change versions should go below the three dashes "---" under the
Signed-off-by, as that will keep them from being added to the git logs.
Patch change versions are only for reviewers information not for git
history.

Other than that, the patch looks fine.

Thanks!

-- Steve

> 
> When a development version of trace-cmd is built and run on the machine,
> by default it loads all plugins from predefined drierctories :
>   (install_preffix)/lib/traceevent/plugins
>   ~/.traceevent/plugins
>   the path specified in TRACEEVENT_PLUGIN_DIR environment variable.
> Thus, the development plugins will not be loaded. To simplify the development
> process, a new logic is added:
>   At plugins load time, check the location of trace-cmd application and look
>   for "plugins" directory around it. If found, load plugins from it. Those
>   pluigins will be loaded last, so in case of duplication the "development"
>   plugins win.
> 
> Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
> ---
>  lib/trace-cmd/trace-util.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
> index 8d21fb2..190cf74 100644
> --- a/lib/trace-cmd/trace-util.c
> +++ b/lib/trace-cmd/trace-util.c
> @@ -14,6 +14,7 @@
>  #include <unistd.h>
>  #include <ctype.h>
>  #include <limits.h>
> +#include <libgen.h>
>  #include <sys/mount.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
> @@ -1364,6 +1365,28 @@ static int add_plugin_file(struct tep_handle *pevent, const char *path,
>  	return -ENOMEM;
>  }
>  
> +static char *trace_util_get_source_plugins_dir(void)
> +{
> +	char *p, path[PATH_MAX+1];
> +	int ret;
> +
> +	ret = readlink("/proc/self/exe", path, PATH_MAX);
> +	if (ret > PATH_MAX || ret < 0)
> +		return NULL;
> +
> +	dirname(path);
> +	p = strrchr(path, '/');
> +	if (!p)
> +		return NULL;
> +	/* Check if we are in the the source tree */
> +	if (strcmp(p, "/tracecmd") != 0)
> +		return NULL;
> +
> +	strcpy(p, "/plugins");
> +	return strdup(path);
> +}
> +
> +
>  int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
>  			    int (*load_plugin)(struct tep_handle *pevent,
>  					       const char *path,
> @@ -1404,6 +1427,12 @@ int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
>  	trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
>  
>  	free(path);
> +
> +	path = trace_util_get_source_plugins_dir();
> +	if (path) {
> +		trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
> +		free(path);
> +	}
>  	return 0;
>  }
>
Steven Rostedt April 23, 2019, 3:02 p.m. UTC | #2
On Tue, 23 Apr 2019 17:33:44 +0300
Tzvetomir Stoyanov <tstoyanov@vmware.com> wrote:

> [
>  v3 changes:
>   - Simplified the logic, using standard library functions.
>   - Added check to ensure the binary is inside the source tree.
>  v2 changes:
>   - Removed the logic looking for plugins in the build path.
>   - Added logic which gets the full path of the running trace-cmd
>     binary and loads plugins from "plugins" directory around it, if exist.
> ]
> 
> When a development version of trace-cmd is built and run on the machine,
> by default it loads all plugins from predefined drierctories :
>   (install_preffix)/lib/traceevent/plugins
>   ~/.traceevent/plugins
>   the path specified in TRACEEVENT_PLUGIN_DIR environment variable.
> Thus, the development plugins will not be loaded. To simplify the development
> process, a new logic is added:
>   At plugins load time, check the location of trace-cmd application and look
>   for "plugins" directory around it. If found, load plugins from it. Those
>   pluigins will be loaded last, so in case of duplication the "development"
>   plugins win.
> 

Slavomir,

Can you review this patch, and if it looks good to you, add your
"Reviewed-by"?

Thanks!

-- Steve

> Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
> ---
>  lib/trace-cmd/trace-util.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
> index 8d21fb2..190cf74 100644
> --- a/lib/trace-cmd/trace-util.c
> +++ b/lib/trace-cmd/trace-util.c
> @@ -14,6 +14,7 @@
>  #include <unistd.h>
>  #include <ctype.h>
>  #include <limits.h>
> +#include <libgen.h>
>  #include <sys/mount.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
> @@ -1364,6 +1365,28 @@ static int add_plugin_file(struct tep_handle *pevent, const char *path,
>  	return -ENOMEM;
>  }
>  
> +static char *trace_util_get_source_plugins_dir(void)
> +{
> +	char *p, path[PATH_MAX+1];
> +	int ret;
> +
> +	ret = readlink("/proc/self/exe", path, PATH_MAX);
> +	if (ret > PATH_MAX || ret < 0)
> +		return NULL;
> +
> +	dirname(path);
> +	p = strrchr(path, '/');
> +	if (!p)
> +		return NULL;
> +	/* Check if we are in the the source tree */
> +	if (strcmp(p, "/tracecmd") != 0)
> +		return NULL;
> +
> +	strcpy(p, "/plugins");
> +	return strdup(path);
> +}
> +
> +
>  int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
>  			    int (*load_plugin)(struct tep_handle *pevent,
>  					       const char *path,
> @@ -1404,6 +1427,12 @@ int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
>  	trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
>  
>  	free(path);
> +
> +	path = trace_util_get_source_plugins_dir();
> +	if (path) {
> +		trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
> +		free(path);
> +	}
>  	return 0;
>  }
>
Slavomir Kaslev April 23, 2019, 3:08 p.m. UTC | #3
On Tue, 2019-04-23 at 11:02 -0400, Steven Rostedt wrote:
> Slavomir,
> 
> Can you review this patch, and if it looks good to you, add your
> "Reviewed-by"?

Sure, tried it locally and seems to be working correctly

Reviewed-by: Slavomir Kaslev <kaslevs@vmware.com>

Cheers,

-- Slavi
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
index 8d21fb2..190cf74 100644
--- a/lib/trace-cmd/trace-util.c
+++ b/lib/trace-cmd/trace-util.c
@@ -14,6 +14,7 @@ 
 #include <unistd.h>
 #include <ctype.h>
 #include <limits.h>
+#include <libgen.h>
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -1364,6 +1365,28 @@  static int add_plugin_file(struct tep_handle *pevent, const char *path,
 	return -ENOMEM;
 }
 
+static char *trace_util_get_source_plugins_dir(void)
+{
+	char *p, path[PATH_MAX+1];
+	int ret;
+
+	ret = readlink("/proc/self/exe", path, PATH_MAX);
+	if (ret > PATH_MAX || ret < 0)
+		return NULL;
+
+	dirname(path);
+	p = strrchr(path, '/');
+	if (!p)
+		return NULL;
+	/* Check if we are in the the source tree */
+	if (strcmp(p, "/tracecmd") != 0)
+		return NULL;
+
+	strcpy(p, "/plugins");
+	return strdup(path);
+}
+
+
 int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
 			    int (*load_plugin)(struct tep_handle *pevent,
 					       const char *path,
@@ -1404,6 +1427,12 @@  int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix,
 	trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
 
 	free(path);
+
+	path = trace_util_get_source_plugins_dir();
+	if (path) {
+		trace_util_load_plugins_dir(pevent, suffix, path, load_plugin, data);
+		free(path);
+	}
 	return 0;
 }