diff mbox series

[1/8] v4l2-info/v4l2-tracer: add macro to mark the trace

Message ID f80a3f7f25f465034ce16262aa2952049242ee53.1699904350.git.deborah.brouwer@collabora.com
State New
Headers show
Series v4l2-tracer: expand to stateful decoding | expand

Commit Message

Deborah Brouwer Nov. 13, 2023, 8:06 p.m. UTC
Add a macro to write to /dev/null. A v4l-utils application that is being
traced can call this macro to inject a comment into the JSON trace file.
It is helpful for debugging.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 utils/common/v4l2-info.h            |  7 +++++++
 utils/v4l2-tracer/libv4l2tracer.cpp | 22 ++++++++++++++++++++++
 utils/v4l2-tracer/retrace.cpp       |  3 +++
 3 files changed, 32 insertions(+)
diff mbox series

Patch

diff --git a/utils/common/v4l2-info.h b/utils/common/v4l2-info.h
index 6de5654c..2142952a 100644
--- a/utils/common/v4l2-info.h
+++ b/utils/common/v4l2-info.h
@@ -11,6 +11,13 @@ 
 #include <linux/videodev2.h>
 #include <linux/v4l2-subdev.h>
 
+#define v4l2_tracer_info(fmt, args...)					\
+	do {								\
+		char msg[256];						\
+		snprintf(msg, sizeof(msg), "v4l2-tracer: " fmt, ##args);\
+		write(open("/dev/null", O_WRONLY), msg, strlen(msg));	\
+	} while (0)
+
 /*
  * The max value comes from a check in the kernel source code
  * drivers/media/v4l2-core/v4l2-ioctl.c check_array_args()
diff --git a/utils/v4l2-tracer/libv4l2tracer.cpp b/utils/v4l2-tracer/libv4l2tracer.cpp
index 7286f321..c6a74afb 100644
--- a/utils/v4l2-tracer/libv4l2tracer.cpp
+++ b/utils/v4l2-tracer/libv4l2tracer.cpp
@@ -88,6 +88,28 @@  int open(const char *path, int oflag, ...)
 	return fd;
 }
 
+ssize_t write(int fd, const void *buf, size_t count)
+{
+	ssize_t (*original_write)(int fd, const void *buf, size_t count) = nullptr;
+	original_write = (ssize_t (*)(int, const void *, size_t)) dlsym(RTLD_NEXT, "write");
+	ssize_t ret = (*original_write)(fd, buf, count);
+
+	/*
+	 * If the write message starts with "v4l2-tracer", then assume it came from the
+	 * v4l2_tracer_info macro and trace it.
+	 */
+	std::string buf_string(static_cast<const char*>(buf), count);
+	if (buf_string.find("v4l2-tracer") == 0) {
+
+		json_object *write_obj = json_object_new_object();
+		json_object_object_add(write_obj, "write", json_object_new_string((const char*)buf));
+		write_json_object_to_json_file(write_obj);
+		json_object_put(write_obj);
+	}
+
+	return ret;
+}
+
 #if defined(linux) && defined(__GLIBC__)
 int open64(const char *path, int oflag, ...)
 {
diff --git a/utils/v4l2-tracer/retrace.cpp b/utils/v4l2-tracer/retrace.cpp
index 88e70ea9..14c42568 100644
--- a/utils/v4l2-tracer/retrace.cpp
+++ b/utils/v4l2-tracer/retrace.cpp
@@ -1507,6 +1507,9 @@  void retrace_object(json_object *jobj)
 	if (json_object_object_get_ex(jobj, "Trace", &temp_obj)) {
 		return;
 	}
+	if (json_object_object_get_ex(jobj, "write", &temp_obj)) {
+		return;
+	}
 	line_info("\n\tWarning: unexpected JSON object in trace file.");
 }