trace-cmd: Load trace-cmd plugins from build folder, if exists
PATCH v2 1/1 trace-cmd: Load trace-cmd plugins from build folder, if exists
[
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.
--- lib/trace-cmd/trace-util.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
lib/trace-cmd/trace-util.c +34 -0
@@ -1364,6 +1364,34 @@ static int add_plugin_file(struct tep_handle *pevent, const char *path,return -ENOMEM ; } +static char *trace_util_get_source_plugins_dir (void ) +{ +char *plugins ="/plugins" ; +char path[PATH_MAX +1 ]; +int slash =0 ; +int ret; + + ret =readlink ("/proc/self/exe" , path,PATH_MAX ); +if (ret >PATH_MAX || ret <0 ) +return NULL ; + +while (ret) { +if ('/' == path[ret--]) + slash++; +if (slash ==2 ) { + path[ret+1 ] =0 ; +if (strlen (path) +strlen (plugins) >PATH_MAX ) +break ; +strcat (path, plugins); +return strdup (path); + } + } + +return NULL ; + +} + + int trace_util_load_plugins(struct tep_handle *pevent, const char *suffix, int (*load_plugin)(struct tep_handle *pevent, const char *path,
@@ -1404,6 +1432,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 ; }
Signature · 1 line
2.20.1
Quoted 51 lines · Tzvetomir Stoyanov
[
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 inTRACEEVENT_PLUGIN_DIRenvironment 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 <[email protected]>--- lib/trace-cmd/trace-util.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)lib/trace-cmd/trace-util.c +12
-0@@ -1364,6 +1364,34 @@ static int add_plugin_file(struct tep_handle *pevent, const char *path, return -ENOMEM ; } +static char *trace_util_get_source_plugins_dir (void ) +{ +char *plugins ="/plugins" ; +char path[PATH_MAX +1 ]; +int slash =0 ; +int ret; + + ret =readlink ("/proc/self/exe" , path,PATH_MAX ); +if (ret >PATH_MAX || ret <0 ) +return NULL ; + +while (ret) {
lib/trace-cmd/trace-util.c static int add_plugin_file(struct tep_handle · line 1389
Quoted 10 lines
lib/trace-cmd/trace-util.c +10
-0+if ('/' == path[ret--]) + slash++; +if (slash ==2 ) { + path[ret+1 ] =0 ; +if (strlen (path) +strlen (plugins) >PATH_MAX ) +break ; +strcat (path, plugins); +return strdup (path); + } + }
Looks good. I think that this while() loop can be simplified using the standard library to something like:
char resolved[PATH_MAX+1]; dirname(path); strcat(path, "/../plugins"); if (!realpath(path, resolved)) return NULL;
The realpath() above is also not strictly necessary.
Cheers,
-- Slavi
Quoted 26 lines · On Tue, 23 Apr 2019 13:10:35 +0000 Slavomir Kaslev <[email protected]> wrote:
Quoted 12 lines
lib/trace-cmd/trace-util.c +12
-0+static char *trace_util_get_source_plugins_dir (void ) +{ +char *plugins ="/plugins" ; +char path[PATH_MAX +1 ]; +int slash =0 ; +int ret; + + ret =readlink ("/proc/self/exe" , path,PATH_MAX ); +if (ret >PATH_MAX || ret <0 ) +return NULL ; + +while (ret) {lib/trace-cmd/trace-util.c static int add_plugin_file(struct tep_handle *pevent, const char *path, · line 1388
Quoted 10 lines
lib/trace-cmd/trace-util.c +10
-0+if ('/' == path[ret--]) + slash++; +if (slash ==2 ) { + path[ret+1 ] =0 ; +if (strlen (path) +strlen (plugins) >PATH_MAX ) +break ; +strcat (path, plugins); +return strdup (path); + } + }Looks good. I think that this
while()loop can be simplified using the standard library to something like:
Quoted 8 lines
char resolved[PATH_MAX+1]; dirname(path); strcat(path, "/../plugins"); if (!realpath(path, resolved)) return NULL;The
realpath()above is also not strictly necessary.
Or:
char *p; dirname(path); p = strrchr(path, '/'); if (!p) return NULL; /* It's only in the source if we are in the tracecmd directory */ if (strcmp(p, "/tracecmd") != 0) return NULL; strcpy(p, "/plugins");
-- Steve
Quoted 55 lines · Steven Rostedt
Quoted 26 lines · On Tue, 23 Apr 2019 13:10:35 +0000 Slavomir Kaslev <[email protected]> wrote:
Quoted 12 lines
lib/trace-cmd/trace-util.c +12
-0+static char *trace_util_get_source_plugins_dir (void ) +{ +char *plugins ="/plugins" ; +char path[PATH_MAX +1 ]; +int slash =0 ; +int ret; + + ret =readlink ("/proc/self/exe" , path,PATH_MAX ); +if (ret >PATH_MAX || ret <0 ) +return NULL ; + +while (ret) {lib/trace-cmd/trace-util.c static int add_plugin_file(struct tep_handle *pevent, const char *path, · line 1388
Quoted 10 lines
lib/trace-cmd/trace-util.c +10
-0+if ('/' == path[ret--]) + slash++; +if (slash ==2 ) { + path[ret+1 ] =0 ; +if (strlen (path) +strlen (plugins) >PATH_MAX ) +break ; +strcat (path, plugins); +return strdup (path); + } + }Looks good. I think that this
while()loop can be simplified using the standard library to something like:Quoted 8 lines
char resolved[PATH_MAX+1]; dirname(path); strcat(path, "/../plugins"); if (!realpath(path, resolved)) return NULL;The
realpath()above is also not strictly necessary.Or:
char *p; dirname(path); p = strrchr(path, '/'); if (!p) return NULL; /* It's only in the source if we are in the tracecmd directory */ if (strcmp(p, "/tracecmd") != 0) return NULL; strcpy(p, "/plugins");
Quoted 2 lines
-- Steve
It makes sense to check for tracecmd directory, though it narrows the use cases. I'll send the next version with this.
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center