diff mbox series

v4l2-tracer: Rewrite libv4l2tracer.so lookup

Message ID 20240227200640.578868-1-nicolas@ndufresne.ca
State New
Headers show
Series v4l2-tracer: Rewrite libv4l2tracer.so lookup | expand

Commit Message

Nicolas Dufresne Feb. 27, 2024, 8:06 p.m. UTC
From: Nicolas Dufresne <nicolas.dufresne@collabora.com>

The lookup had become messy and unreliable. In this rewrite, we
honor LD_PRELOAD if already set. Following, we use the new env
V4L2_TRACER if set. This env is now set by meson devenv to point
to the build version whenever uninstalled setup is used. Finally,
lookup for the installed path. Simply fail without fallback if the
env are set but aren't pointing to a file.

Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
---
 utils/v4l2-tracer/meson.build     |  5 ++++
 utils/v4l2-tracer/v4l2-tracer.cpp | 42 ++++++++++---------------------
 2 files changed, 18 insertions(+), 29 deletions(-)
diff mbox series

Patch

diff --git a/utils/v4l2-tracer/meson.build b/utils/v4l2-tracer/meson.build
index d30e2eae..d059eb6d 100644
--- a/utils/v4l2-tracer/meson.build
+++ b/utils/v4l2-tracer/meson.build
@@ -71,3 +71,8 @@  v4l2_tracer = executable('v4l2-tracer',
                          include_directories : v4l2_tracer_incdir)
 
 man_pages += [[ meson.current_source_dir(), 'v4l2-tracer', 1 ]]
+
+# devenv support
+env = environment()
+env.set('V4L2_TRACER', libv4l2tracer.full_path())
+meson.add_devenv(env)
diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
index 05e5b368..d9e4561c 100644
--- a/utils/v4l2-tracer/v4l2-tracer.cpp
+++ b/utils/v4l2-tracer/v4l2-tracer.cpp
@@ -296,41 +296,25 @@  int tracer(int argc, char *argv[], bool retrace)
 	fclose(trace_file);
 
 	/*
-	 * Preload the libv4l2tracer library. The libv4l2tracer is looked up next to
-	 * the executable first in order to support uninstalled build.
+	 * Preload the libv4l2tracer library. The tracer is looked up using following order:
+	 * 1. Check if LD_PRELOAD is already set, in which case just honor it
+	 * 2. Check V4L2_TRACER_PATH env is set (meson devenv / uninstalled)
+	 * 3. Check in the prefix/libdir path for an installed tracer.
 	 */
 	std::string libv4l2tracer_path;
-	std::string program = argv[0];
-	std::size_t idx = program.rfind("/");
-	struct stat sb;
-
-	if (idx == std::string::npos)
-		idx = 0;
+	if (getenv("LD_PRELOAD"))
+		libv4l2tracer_path = std::string(getenv("LD_PRELOAD"));
+	else if (getenv("V4L2_TRACER"))
+		libv4l2tracer_path = std::string(getenv("V4L2_TRACER"));
 	else
-		idx++;
-
-	libv4l2tracer_path = program.replace(program.begin() + idx, program.end(), "libv4l2tracer.so");
-
-	if (stat(libv4l2tracer_path.c_str(), &sb) == -1) {
-		/* If not found, get the libv4l2tracer library from the meson install path 'prefix' */
 		libv4l2tracer_path = std::string(LIBTRACER_PATH) + "/libv4l2tracer.so";
 
-		/* Otherwise, guess where the library might be for a cross-build. */
+	struct stat sb;
+	if (stat(libv4l2tracer_path.c_str(), &sb) == -1) {
 		if (stat(libv4l2tracer_path.c_str(), &sb) == -1) {
-			std::size_t idx =  libv4l2tracer_path.find("/home");
-			libv4l2tracer_path = libv4l2tracer_path.substr(idx);
-
-			/* Finally, check if the user set a custom path using LD_PRELOAD. */
-			if (stat(libv4l2tracer_path.c_str(), &sb) == -1) {
-				if (getenv("LD_PRELOAD"))
-					libv4l2tracer_path = std::string(getenv("LD_PRELOAD"));
-
-				if (stat(libv4l2tracer_path.c_str(), &sb) == -1) {
-					fprintf(stderr, "Exiting: can't find libv4l2tracer library\n");
-					fprintf(stderr, "Set a custom libv4l2tracer library path using: LD_PRELOAD\n");
-					exit(EXIT_FAILURE);
-				}
-			}
+			fprintf(stderr, "Exiting: can't find libv4l2tracer library in %s\n", libv4l2tracer_path.c_str());
+			fprintf(stderr, "If you are using a different location, try setting the env 'V4L2_TRACER'\n");
+			exit(EXIT_FAILURE);
 		}
 	}