diff mbox series

[1/6] v4l2-tracer: look in more places for libv4l2tracer

Message ID 5498b213f40e99addd4e84ca1dbd9e1412b867eb.1701390439.git.deborah.brouwer@collabora.com (mailing list archive)
State New, archived
Headers show
Series v4l2-tracer: misc fixes and improvements | expand

Commit Message

Deborah Brouwer Dec. 1, 2023, 12:46 a.m. UTC
The v4l2-tracer already looks in the build and install directories for its
library, but now it will also look in a truncated version of the
LIBTRACER_PATH which is helpful if it was installed by a cross-build. If
all else fails, it also allows the user to set a custom path using the
LD_PRELOAD environment variable.

Exit if the libv4l2tracer.so can’t be found.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 utils/v4l2-tracer/v4l2-tracer.cpp | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
index a039f528..37e17eb6 100644
--- a/utils/v4l2-tracer/v4l2-tracer.cpp
+++ b/utils/v4l2-tracer/v4l2-tracer.cpp
@@ -306,13 +306,31 @@  int tracer(int argc, char *argv[], bool retrace)
 	else
 		idx++;
 
-	/* look for libv4l2tracer next to the executable */
 	libv4l2tracer_path = program.replace(program.begin() + idx, program.end(), "libv4l2tracer.so");
 
-	/* otherwise, use the installation path */
-	if (stat(libv4l2tracer_path.c_str(), &sb) == -1)
+	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. */
+		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);
+				}
+			}
+		}
+	}
+
 	if (is_verbose())
 		fprintf(stderr, "Loading libv4l2tracer: %s\n", libv4l2tracer_path.c_str());
 	setenv("LD_PRELOAD", libv4l2tracer_path.c_str(), 0);
@@ -355,6 +373,7 @@  int tracer(int argc, char *argv[], bool retrace)
 	fprintf(stderr, "%s", trace_filename.c_str());
 	fprintf(stderr, "\n");
 
+	unsetenv("LD_PRELOAD");
 	return exec_result;
 }