linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] utils: add v4l2-tracer utility
@ 2022-11-14 18:44 Deborah Brouwer
  2022-11-15 11:36 ` [PATCH] v4l2-tracer: add support for most basic controls Hans Verkuil
  2022-11-16  7:59 ` [PATCHv2] " Hans Verkuil
  0 siblings, 2 replies; 4+ messages in thread
From: Deborah Brouwer @ 2022-11-14 18:44 UTC (permalink / raw)
  To: linux-media
  Cc: daniel.almeida, nfraprado, nicolas.dufresne, hverkuil-cisco,
	deborahbrouwer3563, Deborah Brouwer

The v4l2-tracer traces, records and replays userspace applications that
implement the v4l2 memory-to-memory stateless video decoder interface.
It writes the traced system calls and video frame data to a file in JSON
format. It reads the JSON file to replay the same system calls and send
the same video frame data to the kernel driver. The JSON file can be
replayed in different environments or on different machines.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
Changes since v2:
- remove duplication in entries for numbers/strings
- use media topology to find retrace devices
- add specific ioctls to trace
- in help msg change 'data' -> 'video frame data'
- trace mem_array with space after every byte
- add to trace files package version, SHA, date
- retracer also generates a JSON retrace file
- default trace format is "pretty"
- add a "compact" format option
- consolidate options for tracer/retracer
- remove mutex locks as unnecessary
- add more autogenerated functions
- add 'clean' command to remove fd/addresses

Changes since v1:
- auto-generate code from uAPI headers
- generated code adds some ability to trace
  VP9/HEVC which compiles but still needs work
- generated code adds full support for MPEG2
- fix multiplanar decoding for vicodec
- change library name to libv4l2tracer
- update README
- expand help to show options
- remove extraneous files from gitignore

Example:

Trace an application decoding VP8:

v4l2-tracer trace gst-launch-1.0 -- filesrc 
location=test-25fps.vp8 ! parsebin ! 
v4l2slvp8dec ! videocodectestsink

Or trace an application decoding FWHT:

v4l2-tracer trace v4l2-ctl -d3 --stream-mmap
--stream-out-mmap --stream-from-hdr
test-25fps.fwht --stream-to out.yuv

A trace file is generated: 71827_trace.json

Retrace the trace file:
v4l2-tracer retrace 71827_trace.json

Specify nodes if using a different driver:
v4l2-tracer -d0 -m0 retrace 71827_trace.json

A retrace file is generated: 71827_trace_retrace.json

Remove irrelevant differences (optional)
e.g. file descriptors and addresses:
v4l2-tracer clean 71827_trace.json
v4l2-tracer clean 71827_trace_retrace.json

Clean files are generated:
clean_71827_trace.json
clean_71827_trace_retrace.json

Use an application to compare the files:
kdiff3 clean_71827_trace.json clean_71827_trace_retrace.json

 README                                   |    8 +
 configure.ac                             |   12 +
 utils/Makefile.am                        |    5 +
 utils/v4l2-tracer/.gitignore             |    7 +
 utils/v4l2-tracer/Makefile.am            |   32 +
 utils/v4l2-tracer/libv4l2tracer.cpp      |  307 +++++
 utils/v4l2-tracer/retrace-helper.cpp     |  250 ++++
 utils/v4l2-tracer/retrace.cpp            | 1374 ++++++++++++++++++++++
 utils/v4l2-tracer/retrace.h              |   41 +
 utils/v4l2-tracer/trace-helper.cpp       |  503 ++++++++
 utils/v4l2-tracer/trace.cpp              |  588 +++++++++
 utils/v4l2-tracer/trace.h                |   73 ++
 utils/v4l2-tracer/v4l2-tracer-common.cpp |  453 +++++++
 utils/v4l2-tracer/v4l2-tracer-common.h   |   82 ++
 utils/v4l2-tracer/v4l2-tracer-gen.pl     |  840 +++++++++++++
 utils/v4l2-tracer/v4l2-tracer.1.in       |  111 ++
 utils/v4l2-tracer/v4l2-tracer.cpp        |  412 +++++++
 17 files changed, 5098 insertions(+)
 create mode 100644 utils/v4l2-tracer/.gitignore
 create mode 100644 utils/v4l2-tracer/Makefile.am
 create mode 100644 utils/v4l2-tracer/libv4l2tracer.cpp
 create mode 100644 utils/v4l2-tracer/retrace-helper.cpp
 create mode 100644 utils/v4l2-tracer/retrace.cpp
 create mode 100644 utils/v4l2-tracer/retrace.h
 create mode 100644 utils/v4l2-tracer/trace-helper.cpp
 create mode 100644 utils/v4l2-tracer/trace.cpp
 create mode 100644 utils/v4l2-tracer/trace.h
 create mode 100644 utils/v4l2-tracer/v4l2-tracer-common.cpp
 create mode 100644 utils/v4l2-tracer/v4l2-tracer-common.h
 create mode 100755 utils/v4l2-tracer/v4l2-tracer-gen.pl
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.1.in
 create mode 100644 utils/v4l2-tracer/v4l2-tracer.cpp

diff --git a/README b/README
index a7759171..8c5561ae 100644
--- a/README
+++ b/README
@@ -45,6 +45,8 @@ you'll need also:
 	alsa-lib-devel doxygen libjpeg-turbo-devel qt5-qtbase-devel
 	libudev-devel mesa-libGLU-devel
 
+The v4l2-tracer also needs the json-c library.
+On Debian: libjson-c-dev; on Fedora: json-c-devel.
 
 After downloading and installing the needed packages, you should run:
 
@@ -227,6 +229,12 @@ v4l2-sysfs-path:
 FIXME add description.
 Installed by make install under <prefix>/bin.
 
+v4l2-tracer:
+Tool to trace, record and replay userspace applications
+that implement the v4l2 memory-to-memory stateless video
+decoder interface.
+Installed by 'make install' under <prefix>/bin.
+
 xc3028-firmware:
 Xceive XC2028/3028 tuner module firmware manipulation tool.
 xc3028-firmware does not get installed by make install.
diff --git a/configure.ac b/configure.ac
index 9b7c371d..d2e126d1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -32,6 +32,8 @@ AC_CONFIG_FILES([Makefile
 	utils/v4l2-ctl/Makefile
 	utils/v4l2-dbg/Makefile
 	utils/v4l2-sysfs-path/Makefile
+	utils/v4l2-tracer/Makefile
+	utils/v4l2-tracer/v4l2-tracer.1
 	utils/qv4l2/Makefile
 	utils/libcecutil/Makefile
 	utils/cec-ctl/Makefile
@@ -311,6 +313,15 @@ AS_IF([test "x$with_libudev" != xno -o "x$enable_libdvbv5" != xno],
 
 AC_SUBST([JPEG_LIBS])
 
+JSONC_VERSION_REQUIRED="0.15";
+PKG_CHECK_MODULES(JSONC, [json-c >= $JSONC_VERSION_REQUIRED], [jsonc_pkgconfig=yes], [jsonc_pkgconfig=no])
+AC_SUBST([JSONC_CFLAGS])
+AC_SUBST([JSONC_LIBS])
+AM_CONDITIONAL([HAVE_JSONC], [test x$jsonc_pkgconfig = xyes])
+if test "x$jsonc_pkgconfig" = "xno"; then
+    AC_MSG_WARN(json-c $JSONC_VERSION_REQUIRED or higher required for v4l2-tracer)
+fi
+
 # Check for pthread
 
 AS_IF([test x$enable_shared != xno],
@@ -645,6 +656,7 @@ compile time options summary
     QT version                 : $QT_VERSION
     ALSA support               : $USE_ALSA
     SDL support		       : $sdl_pc
+    JSON-C                     : $jsonc_pkgconfig >= $JSONC_VERSION_REQUIRED
 
     build dynamic libs         : $enable_shared
     build static libs          : $enable_static
diff --git a/utils/Makefile.am b/utils/Makefile.am
index 0e68a612..6f59515e 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -15,6 +15,11 @@ SUBDIRS = \
 	cec-follower \
 	rds-ctl
 
+if HAVE_JSONC
+SUBDIRS += \
+	v4l2-tracer
+endif
+
 if WITH_LIBDVBV5
 SUBDIRS += \
 	dvb
diff --git a/utils/v4l2-tracer/.gitignore b/utils/v4l2-tracer/.gitignore
new file mode 100644
index 00000000..48fbfc47
--- /dev/null
+++ b/utils/v4l2-tracer/.gitignore
@@ -0,0 +1,7 @@
+trace-gen.h
+trace-gen.cpp
+retrace-gen.cpp
+retrace-gen.h
+v4l2-tracer-info-gen.h
+v4l2-tracer
+v4l2-tracer.1
diff --git a/utils/v4l2-tracer/Makefile.am b/utils/v4l2-tracer/Makefile.am
new file mode 100644
index 00000000..1e3a96d0
--- /dev/null
+++ b/utils/v4l2-tracer/Makefile.am
@@ -0,0 +1,32 @@
+if HAVE_JSONC
+
+lib_LTLIBRARIES = libv4l2tracer.la
+libv4l2tracer_la_SOURCES = libv4l2tracer.cpp trace-gen.cpp trace-helper.cpp \
+trace.cpp v4l2-tracer-common.cpp $(top_srcdir)/utils/common/v4l2-info.cpp $(top_srcdir)/utils/common/media-info.cpp
+libv4l2tracer_la_CPPFLAGS = -I$(top_srcdir)/utils/common $(JSONC_CFLAGS)
+libv4l2tracer_la_LDFLAGS = -avoid-version -module -shared -export-dynamic -ldl $(JSONC_LIBS)
+libv4l2tracer_la_LIBTOOLFLAGS = --tag=disable-static
+
+bin_PROGRAMS = v4l2-tracer
+man_MANS = v4l2-tracer.1
+
+v4l2_tracer_SOURCES = v4l2-tracer.cpp retrace-gen.cpp retrace-helper.cpp retrace.cpp \
+v4l2-tracer-common.cpp $(top_srcdir)/utils/common/v4l2-info.cpp $(top_srcdir)/utils/common/media-info.cpp
+v4l2_tracer_CPPFLAGS = -I$(top_srcdir)/utils/common -DLIBTRACER_PATH=$(libdir) $(JSONC_CFLAGS) $(GIT_SHA) $(GIT_COMMIT_CNT) $(GIT_COMMIT_DATE)
+v4l2_tracer_LDFLAGS = -lrt -lpthread $(JSONC_LIBS)
+
+V4L2_TRACER_GEN_PREREQS = $(top_srcdir)/include/linux/v4l2-controls.h \
+$(top_srcdir)/include/linux/videodev2.h $(top_srcdir)/include/linux/media.h $(top_srcdir)/include/linux/v4l2-common.h
+V4L2_TRACER_GEN = trace-gen.cpp trace-gen.h retrace-gen.cpp retrace-gen.h v4l2-tracer-info-gen.h
+v4l2-tracer.cpp: $(V4L2_TRACER_GEN)
+$(V4L2_TRACER_GEN): gen.intermediate;
+.INTERMEDIATE: gen.intermediate
+gen.intermediate: v4l2-tracer-gen.pl $(V4L2_TRACER_GEN_PREREQS)
+	$(AM_V_GEN) $(top_srcdir)/utils/v4l2-tracer/v4l2-tracer-gen.pl $(V4L2_TRACER_GEN_PREREQS)
+
+clean-local:
+	-rm -vf $(V4L2_TRACER_GEN)
+
+EXTRA_DIST = v4l2-tracer.1, v4l2-tracer-gen.pl
+
+endif
diff --git a/utils/v4l2-tracer/libv4l2tracer.cpp b/utils/v4l2-tracer/libv4l2tracer.cpp
new file mode 100644
index 00000000..8eae783f
--- /dev/null
+++ b/utils/v4l2-tracer/libv4l2tracer.cpp
@@ -0,0 +1,307 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include <dlfcn.h>
+#include <stdarg.h>
+#include <config.h> /* For PROMOTED_MODE_T */
+#include "trace.h"
+
+extern struct trace_context ctx_trace;
+
+std::list<unsigned long> ioctls = {
+	VIDIOC_QUERYCAP,
+	VIDIOC_STREAMON,
+	VIDIOC_STREAMOFF,
+	VIDIOC_ENUM_FMT,
+	VIDIOC_G_FMT,
+	VIDIOC_S_FMT,
+	VIDIOC_REQBUFS,
+	VIDIOC_QUERYBUF,
+	VIDIOC_QBUF,
+	VIDIOC_EXPBUF,
+	VIDIOC_DQBUF,
+	VIDIOC_G_PARM,
+	VIDIOC_S_PARM,
+	VIDIOC_G_CTRL,
+	VIDIOC_S_CTRL,
+	VIDIOC_QUERYCTRL,
+	VIDIOC_G_CROP,
+	VIDIOC_S_CROP,
+	VIDIOC_TRY_FMT,
+	VIDIOC_G_EXT_CTRLS,
+	VIDIOC_S_EXT_CTRLS,
+	VIDIOC_TRY_EXT_CTRLS,
+	VIDIOC_ENCODER_CMD,
+	VIDIOC_TRY_ENCODER_CMD,
+	VIDIOC_CREATE_BUFS,
+	VIDIOC_PREPARE_BUF,
+	VIDIOC_G_SELECTION,
+	VIDIOC_S_SELECTION,
+	VIDIOC_DECODER_CMD,
+	VIDIOC_TRY_DECODER_CMD,
+	VIDIOC_QUERY_EXT_CTRL,
+	MEDIA_IOC_REQUEST_ALLOC,
+	MEDIA_REQUEST_IOC_QUEUE,
+	MEDIA_REQUEST_IOC_REINIT,
+};
+
+int open(const char *path, int oflag, ...)
+{
+	errno = 0;
+	mode_t mode = 0;
+
+	if (oflag & O_CREAT) {
+		va_list ap;
+		va_start(ap, oflag);
+		mode = va_arg(ap, PROMOTED_MODE_T);
+		va_end(ap);
+	}
+
+	int (*original_open)(const char *path, int oflag, ...);
+	original_open = (int (*)(const char*, int, ...)) dlsym(RTLD_NEXT, "open");
+	int fd = (*original_open)(path, oflag, mode);
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "fd: %d, path: %s\n", fd, path);
+	}
+
+	/* Don't trace the opening if tracing is paused. */
+	if (getenv("V4L2_TRACER_PAUSE_TRACE"))
+		return fd;
+
+	/* Only trace the opening of video/media devices. */
+	if (is_video_or_media_device(path)) {
+		trace_open(fd, path, oflag, mode, false);
+		add_device(fd, path);
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		print_devices();
+	}
+
+	return fd;
+}
+
+int open64(const char *path, int oflag, ...)
+{
+	errno = 0;
+	mode_t mode = 0;
+	if (oflag & O_CREAT) {
+		va_list ap;
+		va_start(ap, oflag);
+		mode = va_arg(ap, PROMOTED_MODE_T);
+		va_end(ap);
+	}
+
+	int (*original_open64)(const char *path, int oflag, ...);
+	original_open64 = (int (*)(const char*, int, ...)) dlsym(RTLD_NEXT, "open64");
+	int fd = (*original_open64)(path, oflag, mode);
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "fd: %d, path: %s\n", fd, path);
+	}
+
+	/* Don't trace the opening if tracing is paused. */
+	if (getenv("V4L2_TRACER_PAUSE_TRACE"))
+		return fd;
+
+	if (is_video_or_media_device(path)) {
+		add_device(fd, path);
+		trace_open(fd, path, oflag, mode, true);
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		print_devices();
+	}
+
+	return fd;
+}
+
+int close(int fd)
+{
+	errno = 0;
+	int (*original_close)(int fd);
+	original_close = (int (*)(int)) dlsym(RTLD_NEXT, "close");
+
+	if (getenv("V4L2_TRACER_PAUSE_TRACE"))
+		return (*original_close)(fd);
+
+	std::string path = get_device(fd);
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "fd: %d, path: %s\n", fd, path.c_str());
+	}
+
+	/* Only trace the close if a corresponding open was also traced. */
+	if (!path.empty()) {
+		json_object *close_obj = json_object_new_object();
+		json_object_object_add(close_obj, "fd", json_object_new_int(fd));
+		json_object_object_add(close_obj, "close", json_object_new_string(path.c_str()));
+		write_json_object_to_json_file(close_obj);
+		json_object_put(close_obj);
+		ctx_trace.devices.erase(fd);
+
+		/* If we removed the last device, close the json trace file. */
+		if (!ctx_trace.devices.size())
+			close_json_file();
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		print_devices();
+	}
+
+	return (*original_close)(fd);
+}
+
+void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+	errno = 0;
+	void *(*original_mmap)(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+	original_mmap = (void*(*)(void*, size_t, int, int, int, off_t)) dlsym(RTLD_NEXT, "mmap");
+	void *buf_address_pointer = (*original_mmap)(addr, len, prot, flags, fildes, off);
+
+	set_buffer_address_trace(fildes, off, (unsigned long) buf_address_pointer);
+
+	if (buffer_in_trace_context(fildes, off))
+		trace_mmap(addr, len, prot, flags, fildes, off, (unsigned long) buf_address_pointer, false);
+
+	return buf_address_pointer;
+}
+
+void *mmap64(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+	errno = 0;
+	void *(*original_mmap64)(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+	original_mmap64 = (void*(*)(void*, size_t, int, int, int, off_t)) dlsym(RTLD_NEXT, "mmap64");
+	void *buf_address_pointer = (*original_mmap64)(addr, len, prot, flags, fildes, off);
+
+	set_buffer_address_trace(fildes, off, (unsigned long) buf_address_pointer);
+
+	if (buffer_in_trace_context(fildes, off))
+		trace_mmap(addr, len, prot, flags, fildes, off, (unsigned long) buf_address_pointer, true);
+
+	return buf_address_pointer;
+}
+
+int munmap(void *start, size_t length)
+{
+	errno = 0;
+	int(*original_munmap)(void *start, size_t length);
+	original_munmap = (int(*)(void *, size_t)) dlsym(RTLD_NEXT, "munmap");
+	int ret = (*original_munmap)(start, length);
+
+	/* Only trace the unmapping if the original mapping was traced. */
+	if (!buffer_is_mapped((unsigned long) start))
+		return ret;
+
+	json_object *munmap_obj = json_object_new_object();
+
+	if (errno)
+		json_object_object_add(munmap_obj, "errno", json_object_new_string(strerrorname_np(errno)));
+
+	json_object *munmap_args = json_object_new_object();
+	json_object_object_add(munmap_args, "start", json_object_new_int64((int64_t)start));
+	json_object_object_add(munmap_args, "length", json_object_new_uint64(length));
+	json_object_object_add(munmap_obj, "munmap", munmap_args);
+
+	write_json_object_to_json_file(munmap_obj);
+	json_object_put(munmap_obj);
+
+	return ret;
+}
+
+int ioctl(int fd, unsigned long cmd, ...)
+{
+	errno = 0;
+	va_list ap;
+	va_start(ap, cmd);
+	void *arg = va_arg(ap, void *);
+	va_end(ap);
+
+	int (*original_ioctl)(int fd, unsigned long cmd, ...);
+	original_ioctl = (int (*)(int, long unsigned int, ...)) dlsym(RTLD_NEXT, "ioctl");
+
+	/* Don't trace if v4l2-tracer has paused tracing. */
+	if (getenv("V4L2_TRACER_PAUSE_TRACE"))
+		return (*original_ioctl)(fd, cmd, arg);
+
+	/* Don't trace ioctls that are not in the specified ioctls list. */
+	if (find(ioctls.begin(), ioctls.end(), cmd) == ioctls.end())
+		return (*original_ioctl)(fd, cmd, arg);
+
+	json_object *ioctl_obj = json_object_new_object();
+	json_object_object_add(ioctl_obj, "fd", json_object_new_int(fd));
+	json_object_object_add(ioctl_obj, "ioctl",
+	                       json_object_new_string(val2s(cmd, ioctl_val_def).c_str()));
+
+	/* Don't attempt to trace a nullptr. */
+	if (arg == nullptr) {
+		int ret = (*original_ioctl)(fd, cmd, arg);
+		if (errno)
+			json_object_object_add(ioctl_obj, "errno",
+			                       json_object_new_string(strerrorname_np(errno)));
+		write_json_object_to_json_file(ioctl_obj);
+		json_object_put(ioctl_obj);
+		return ret;
+	}
+
+	/* Get info needed for writing the decoded video data to a yuv file. */
+	if (cmd == VIDIOC_S_EXT_CTRLS)
+		s_ext_ctrls_setup(static_cast<struct v4l2_ext_controls*>(arg));
+	if (cmd == VIDIOC_QBUF)
+		qbuf_setup(static_cast<struct v4l2_buffer*>(arg));
+	if (cmd == VIDIOC_STREAMOFF)
+		streamoff_cleanup(*(static_cast<v4l2_buf_type*>(arg)));
+
+	/* Trace userspace arguments if driver will be reading them i.e. _IOW or _IOWR ioctls */
+	if (cmd & IOC_IN) {
+		json_object *ioctl_args_userspace = trace_ioctl_args(cmd, arg);
+		/* Some ioctls won't have arguments to trace e.g. MEDIA_REQUEST_IOC_QUEUE. */
+		if (json_object_object_length(ioctl_args_userspace))
+			json_object_object_add(ioctl_obj, "from_userspace", ioctl_args_userspace);
+		else
+			json_object_put(ioctl_args_userspace);
+	}
+
+	/* Make the original ioctl call. */
+	int ret = (*original_ioctl)(fd, cmd, arg);
+
+	if (errno)
+		json_object_object_add(ioctl_obj, "errno", json_object_new_string(strerrorname_np(errno)));
+
+	/* Trace driver arguments if userspace will be reading them i.e. _IOR or _IOWR ioctls */
+	if (cmd & IOC_OUT) {
+		json_object *ioctl_args_driver = trace_ioctl_args(cmd, arg);
+		/* Some ioctls won't have arguments to trace e.g. MEDIA_REQUEST_IOC_QUEUE. */
+		if (json_object_object_length(ioctl_args_driver))
+			json_object_object_add(ioctl_obj, "from_driver", ioctl_args_driver);
+		else
+			json_object_put(ioctl_args_driver);
+	}
+
+	write_json_object_to_json_file(ioctl_obj);
+	json_object_put(ioctl_obj);
+
+	/* Get additional info from driver for writing the decoded video data to a yuv file. */
+	if (cmd == VIDIOC_G_FMT)
+		g_fmt_setup_trace(static_cast<struct v4l2_format*>(arg));
+	if (cmd == VIDIOC_S_FMT)
+		s_fmt_setup(static_cast<struct v4l2_format*>(arg));
+	if (cmd == VIDIOC_EXPBUF)
+		expbuf_setup(static_cast<struct v4l2_exportbuffer*>(arg));
+	if (cmd == VIDIOC_QUERYBUF)
+		querybuf_setup(fd, static_cast<struct v4l2_buffer*>(arg));
+
+	/* Get info needed for tracing dynamic arrays */
+	if (cmd == VIDIOC_QUERY_EXT_CTRL)
+		query_ext_ctrl_setup(fd, static_cast<struct v4l2_query_ext_ctrl*>(arg));
+
+	return ret;
+}
diff --git a/utils/v4l2-tracer/retrace-helper.cpp b/utils/v4l2-tracer/retrace-helper.cpp
new file mode 100644
index 00000000..c17618d1
--- /dev/null
+++ b/utils/v4l2-tracer/retrace-helper.cpp
@@ -0,0 +1,250 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "retrace.h"
+
+struct retrace_context ctx_retrace = {};
+
+bool buffer_in_retrace_context(int fd, __u32 offset)
+{
+	bool buffer_in_retrace_context = false;
+	for (auto &b : ctx_retrace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			buffer_in_retrace_context = true;
+			break;
+		}
+	}
+	return buffer_in_retrace_context;
+}
+
+int get_buffer_fd_retrace(__u32 type, __u32 index)
+{
+	int fd = 0;
+	for (auto &b : ctx_retrace.buffers) {
+		if ((b.type == type) && (b.index == index)) {
+			fd = b.fd;
+			break;
+		}
+	}
+	return fd;
+}
+
+void add_buffer_retrace(int fd, __u32 type, __u32 index, __u32 offset)
+{
+	struct buffer_retrace buf = {};
+	buf.fd = fd;
+	buf.type = type;
+	buf.index = index;
+	buf.offset = offset;
+	ctx_retrace.buffers.push_front(buf);
+}
+
+void remove_buffer_retrace(int fd)
+{
+	for (auto it = ctx_retrace.buffers.begin(); it != ctx_retrace.buffers.end(); ++it) {
+		if (it->fd == fd) {
+			ctx_retrace.buffers.erase(it);
+			break;
+		}
+	}
+}
+
+void set_buffer_address_retrace(int fd, __u32 offset, long address_trace, long address_retrace)
+{
+	for (auto &b : ctx_retrace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			b.address_trace = address_trace;
+			b.address_retrace = address_retrace;
+			break;
+		}
+	}
+}
+
+long get_retrace_address_from_trace_address(long address_trace)
+{
+	long address_retrace = 0;
+	for (auto &b : ctx_retrace.buffers) {
+		if (b.address_trace == address_trace) {
+			address_retrace = b.address_retrace;
+			break;
+		}
+	}
+	return address_retrace;
+}
+
+void print_buffers_retrace(void)
+{
+	for (auto &b : ctx_retrace.buffers) {
+		fprintf(stderr, "fd: %d, offset: %d, address_trace:%ld, address_retrace:%ld\n",
+		        b.fd, b.offset, b.address_trace, b.address_retrace);
+	}
+}
+
+void add_fd(int fd_trace, int fd_retrace)
+{
+	std::pair<int, int> new_pair;
+	new_pair = std::make_pair(fd_trace, fd_retrace);
+	ctx_retrace.retrace_fds.insert(new_pair);
+}
+
+int get_fd_retrace_from_fd_trace(int fd_trace)
+{
+	int fd_retrace = -1;
+	std::unordered_map<int, int>::const_iterator it;
+	it = ctx_retrace.retrace_fds.find(fd_trace);
+	if (it != ctx_retrace.retrace_fds.end())
+		fd_retrace = it->second;
+	return fd_retrace;
+}
+
+void print_fds(void)
+{
+	if (ctx_retrace.retrace_fds.empty())
+		fprintf(stderr, "all devices closed\n");
+	for (auto it = ctx_retrace.retrace_fds.cbegin(); it != ctx_retrace.retrace_fds.cend(); ++it)
+		fprintf(stderr, "fd_trace: %d, fd_retrace: %d\n", it->first, it->second);
+}
+
+std::string get_path_retrace_from_path_trace(std::string path_trace, json_object *open_obj)
+{
+	bool is_media = path_trace.find("media") != path_trace.npos;
+	bool is_video = path_trace.find("video") != path_trace.npos;
+
+	if (is_media && getenv("V4L2_TRACER_OPTION_SET_MEDIA_DEVICE")) {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "Use path set by user: %s ",
+			        getenv("V4L2_TRACER_OPTION_SET_MEDIA_DEVICE"));
+		}
+		return getenv("V4L2_TRACER_OPTION_SET_MEDIA_DEVICE");
+	}
+
+	if (is_video && getenv("V4L2_TRACER_OPTION_SET_VIDEO_DEVICE")) {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "Use path set by user: %s ",
+			        getenv("V4L2_TRACER_OPTION_SET_VIDEO_DEVICE"));
+		}
+		return getenv("V4L2_TRACER_OPTION_SET_VIDEO_DEVICE");
+	}
+
+	std::string driver;
+	json_object *driver_obj;
+	if (json_object_object_get_ex(open_obj, "driver", &driver_obj))
+		driver = json_object_get_string(driver_obj);
+	if (driver.empty())
+		return "";
+
+	std::string path_media = get_path_media(driver);
+	if (path_media.empty()) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: driver: %s not found\n", driver.c_str());
+		return "";
+	}
+
+	if (is_media)
+		return path_media;
+
+	if (is_video) {
+		std::list<std::string> linked_entities;
+		json_object *le_obj;
+		if (json_object_object_get_ex(open_obj, "linked_entities", &le_obj)) {
+			for (size_t i = 0; i < array_list_length(json_object_get_array(le_obj)); i++) {
+				std::string ename = json_object_get_string(json_object_array_get_idx(le_obj, i));
+				linked_entities.push_back(ename);
+			}
+		}
+		if (linked_entities.size() == 0)
+			return "";
+
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		int media_fd = open(path_media.c_str(), O_RDONLY);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+
+		std::string path_video = get_path_video(media_fd, linked_entities);
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		close(media_fd);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+		return path_video;
+	}
+
+	return "";
+}
+
+void write_to_output_buffer(unsigned char *buffer_pointer, int bytesused, json_object *mem_obj)
+{
+	int byteswritten = 0;
+	json_object *line_obj;
+	size_t number_of_lines;
+	std::string compressed_video_data;
+
+	json_object *mem_array_obj;
+	json_object_object_get_ex(mem_obj, "mem_array", &mem_array_obj);
+	number_of_lines = json_object_array_length(mem_array_obj);
+
+	for (long unsigned int i = 0; i < number_of_lines; i++) {
+		line_obj = json_object_array_get_idx(mem_array_obj, i);
+		compressed_video_data = json_object_get_string(line_obj);
+
+		for (long unsigned i = 0; i < compressed_video_data.length(); i++) {
+			if (std::isspace(compressed_video_data[i]))
+				continue;
+			try {
+				/* Two values from the string e.g. "D9" are needed to write one byte. */
+				*buffer_pointer = (char) std::stoi(compressed_video_data.substr(i,2), nullptr, 16);
+				buffer_pointer++;
+				i++;
+				byteswritten++;
+			} catch (std::invalid_argument& ia) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "\'%s\' is an invalid argument.\n",
+				        compressed_video_data.substr(i,2).c_str());
+			} catch (std::out_of_range& oor) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "\'%s\' is out of range.\n",
+				        compressed_video_data.substr(i,2).c_str());
+			}
+		}
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "bytesused: %d, byteswritten: %d\n", bytesused, byteswritten);
+	}
+}
+
+void compare_program_versions(json_object *v4l2_tracer_info_obj)
+{
+	json_object *package_version_obj;
+	json_object_object_get_ex(v4l2_tracer_info_obj, "package_version", &package_version_obj);
+	std::string package_version_trace = json_object_get_string(package_version_obj);
+	std::string package_version_retrace = PACKAGE_VERSION;
+	if (package_version_trace != package_version_retrace) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: trace package version \'%s\' does not match current: \'%s\':\n",
+		        package_version_trace.c_str(), package_version_retrace.c_str());
+		print_v4l2_tracer_info();
+		return;
+	}
+
+	json_object *git_sha_obj;
+	json_object_object_get_ex(v4l2_tracer_info_obj, "git_sha", &git_sha_obj);
+	std::string git_sha_trace = json_object_get_string(git_sha_obj);
+	std::string git_sha_retrace = (STRING(GIT_SHA));
+	if (git_sha_trace != git_sha_retrace) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: sha in trace file \'%s\' does not match current sha: \'%s\'\n",
+		        git_sha_trace.c_str(),  git_sha_retrace.c_str());
+		print_v4l2_tracer_info();
+		return;
+	}
+}
+
+void print_context(void)
+{
+	print_fds();
+	print_buffers_retrace();
+	fprintf(stderr, "\n");
+}
diff --git a/utils/v4l2-tracer/retrace.cpp b/utils/v4l2-tracer/retrace.cpp
new file mode 100644
index 00000000..b736e835
--- /dev/null
+++ b/utils/v4l2-tracer/retrace.cpp
@@ -0,0 +1,1374 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "retrace.h"
+
+extern struct retrace_context ctx_retrace;
+
+void retrace_mmap(json_object *mmap_obj, bool is_mmap64)
+{
+	json_object *mmap_args_obj;
+	if (is_mmap64)
+		json_object_object_get_ex(mmap_obj, "mmap64", &mmap_args_obj);
+	else
+		json_object_object_get_ex(mmap_obj, "mmap", &mmap_args_obj);
+
+	json_object *len_obj;
+	json_object_object_get_ex(mmap_args_obj, "len", &len_obj);
+	size_t len = (size_t) json_object_get_int(len_obj);
+
+	json_object *prot_obj;
+	json_object_object_get_ex(mmap_args_obj, "prot", &prot_obj);
+	int prot = json_object_get_int(prot_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(mmap_args_obj, "flags", &flags_obj);
+	int flags = s2number(json_object_get_string(flags_obj));
+
+	json_object *fildes_obj;
+	json_object_object_get_ex(mmap_args_obj, "fildes", &fildes_obj);
+	int fd_trace = json_object_get_int(fildes_obj);
+
+	json_object *off_obj;
+	json_object_object_get_ex(mmap_args_obj, "off", &off_obj);
+	off_t off = (off_t) json_object_get_int64(off_obj);
+
+	int fd_retrace = get_fd_retrace_from_fd_trace(fd_trace);
+	if (fd_retrace < 0) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "bad or missing file descriptor.\n");
+		return;
+	}
+
+	/* Only retrace mmap calls that map a buffer. */
+	if (!buffer_in_retrace_context(fd_retrace, off))
+		return;
+
+	void *buf_address_retrace_pointer;
+	if (is_mmap64)
+		buf_address_retrace_pointer = mmap64(0, len, prot, flags, fd_retrace, off);
+	else
+		buf_address_retrace_pointer = mmap(0, len, prot, flags, fd_retrace, off);
+
+	if (buf_address_retrace_pointer == MAP_FAILED) {
+		if (is_mmap64)
+			perror("mmap64");
+		else
+			perror("mmap");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+		exit(EXIT_FAILURE);
+	}
+
+	/*
+	 * Get and store the original trace address so that it can be matched
+	 * with the munmap address later.
+	 */
+	json_object *buffer_address_obj;
+	json_object_object_get_ex(mmap_obj, "buffer_address", &buffer_address_obj);
+	long buf_address_trace = json_object_get_int64(buffer_address_obj);
+	set_buffer_address_retrace(fd_retrace, off, buf_address_trace,
+	                           (long) buf_address_retrace_pointer);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "fd: %d, offset: %ld, ", fd_retrace, off);
+		if (is_mmap64)
+			perror("mmap64");
+		else
+			perror("mmap");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+}
+
+void retrace_munmap(json_object *syscall_obj)
+{
+	json_object *munmap_args_obj;
+	json_object_object_get_ex(syscall_obj, "munmap", &munmap_args_obj);
+
+	json_object *start_obj;
+	json_object_object_get_ex(munmap_args_obj, "start", &start_obj);
+	long start = json_object_get_int64(start_obj);
+
+	json_object *length_obj;
+	json_object_object_get_ex(munmap_args_obj, "length", &length_obj);
+	size_t length = (size_t) json_object_get_int(length_obj);
+
+	long buffer_address_retrace = get_retrace_address_from_trace_address(start);
+
+	if (buffer_address_retrace < 0)
+		return;
+
+	munmap((void *)buffer_address_retrace, length);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "unmapped: %ld, ", buffer_address_retrace);
+		perror("munmap");
+	}
+}
+
+void retrace_open(json_object *jobj, bool is_open64)
+{
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
+	int fd_trace = json_object_get_int(fd_trace_obj);
+
+	json_object *open_args_obj;
+	if (is_open64)
+		json_object_object_get_ex(jobj, "open64", &open_args_obj);
+	else
+		json_object_object_get_ex(jobj, "open", &open_args_obj);
+
+	json_object *path_obj;
+	json_object_object_get_ex(open_args_obj, "path", &path_obj);
+	std::string path_trace = json_object_get_string(path_obj);
+	std::string path_retrace = get_path_retrace_from_path_trace(path_trace, jobj);
+
+	/*
+	 * Don't fail if a retrace path can't be found.
+	 * Try using the same path as in the trace file.
+	 */
+	if (path_retrace.empty()) {
+		if (getenv("V4L2_TRACER_OPTION_VERBOSE")) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "warning: can't find retrace device. Attempting to use: %s\n",
+			        path_trace.c_str());
+		}
+		path_retrace = path_trace;
+	}
+
+	json_object *oflag_obj;
+	json_object_object_get_ex(open_args_obj, "oflag", &oflag_obj);
+	int oflag = s2val(json_object_get_string(oflag_obj), open_val_def);
+
+	int mode = 0;
+	json_object *mode_obj;
+	if (json_object_object_get_ex(open_args_obj, "mode", &mode_obj))
+		mode = s2number(json_object_get_string(mode_obj));
+
+	int fd_retrace = 0;
+	if (is_open64)
+		fd_retrace = open64(path_retrace.c_str(), oflag, mode);
+	else
+		fd_retrace = open(path_retrace.c_str(), oflag, mode);
+
+	if (fd_retrace <= 0) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "cannot open: %s\n", path_retrace.c_str());
+		exit(fd_retrace);
+	}
+
+	add_fd(fd_trace, fd_retrace);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || errno != 0) {
+		fprintf(stderr, "path: %s ", path_retrace.c_str());
+		if (is_open64)
+			perror("open64");
+		else
+			perror("open");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+}
+
+void retrace_close(json_object *jobj)
+{
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
+	int fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
+
+	/* Only close devices that were opened in the retrace context. */
+	if (fd_retrace < 0)
+		return;
+
+	close(fd_retrace);
+	ctx_retrace.retrace_fds.erase(json_object_get_int(fd_trace_obj));
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "fd: %d ", fd_retrace);
+		perror("close");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+}
+
+void retrace_vidioc_reqbufs(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_requestbuffers *ptr = retrace_v4l2_requestbuffers_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_REQBUFS, ptr);
+
+	if (ptr)
+		free(ptr);
+}
+
+struct v4l2_plane *retrace_v4l2_plane(json_object *plane_obj, __u32 memory)
+{
+	struct v4l2_plane *pl = (struct v4l2_plane *) calloc(1, sizeof(v4l2_plane));
+
+	json_object *bytesused_obj;
+	json_object_object_get_ex(plane_obj, "bytesused", &bytesused_obj);
+	pl->bytesused = (__u32) json_object_get_int64(bytesused_obj);
+
+	json_object *length_obj;
+	json_object_object_get_ex(plane_obj, "length", &length_obj);
+	pl->length = (__u32) json_object_get_int64(length_obj);
+
+	json_object *m_obj;
+	json_object_object_get_ex(plane_obj, "m", &m_obj);
+	if (memory == V4L2_MEMORY_MMAP) {
+		json_object *mem_offset_obj;
+		json_object_object_get_ex(m_obj, "mem_offset", &mem_offset_obj);
+		pl->m.mem_offset = (__u32) json_object_get_int64(mem_offset_obj);
+	}
+
+	json_object *data_offset_obj;
+	json_object_object_get_ex(plane_obj, "data_offset", &data_offset_obj);
+	pl->data_offset = (__u32) json_object_get_int64(data_offset_obj);
+
+	return pl;
+}
+
+struct v4l2_buffer *retrace_v4l2_buffer(json_object *ioctl_args)
+{
+	struct v4l2_buffer *buf = (struct v4l2_buffer *) calloc(1, sizeof(struct v4l2_buffer));
+
+	json_object *buf_obj;
+	json_object_object_get_ex(ioctl_args, "v4l2_buffer", &buf_obj);
+
+	json_object *index_obj;
+	json_object_object_get_ex(buf_obj, "index", &index_obj);
+	buf->index = (__u32) json_object_get_uint64(index_obj);
+
+	json_object *type_obj;
+	json_object_object_get_ex(buf_obj, "type", &type_obj);
+	buf->type = s2val(json_object_get_string(type_obj), v4l2_buf_type_val_def);
+
+	json_object *bytesused_obj;
+	json_object_object_get_ex(buf_obj, "bytesused", &bytesused_obj);
+	buf->bytesused = (__u32) json_object_get_uint64(bytesused_obj);
+
+	json_object *flags_obj;
+	json_object_object_get_ex(buf_obj, "flags", &flags_obj);
+	buf->flags = (__u32) s2flags_buffer(json_object_get_string(flags_obj));
+
+	json_object *field_obj;
+	json_object_object_get_ex(buf_obj, "field", &field_obj);
+	buf->field = (__u32) s2val(json_object_get_string(field_obj), v4l2_field_val_def);
+
+	json_object *timestamp_obj;
+	json_object_object_get_ex(buf_obj, "timestamp", &timestamp_obj);
+
+	struct timeval tv = {};
+	json_object *tv_sec_obj;
+	json_object_object_get_ex(timestamp_obj, "tv_sec", &tv_sec_obj);
+	tv.tv_sec = json_object_get_int64(tv_sec_obj);
+	json_object *tv_usec_obj;
+	json_object_object_get_ex(timestamp_obj, "tv_usec", &tv_usec_obj);
+	tv.tv_usec = json_object_get_int64(tv_usec_obj);
+	buf->timestamp = tv;
+
+	json_object *sequence_obj;
+	json_object_object_get_ex(buf_obj, "sequence", &sequence_obj);
+	buf->sequence = (__u32) json_object_get_uint64(sequence_obj);
+
+	json_object *memory_obj;
+	json_object_object_get_ex(buf_obj, "memory", &memory_obj);
+	buf->memory = s2val(json_object_get_string(memory_obj), v4l2_memory_val_def);
+
+	json_object *length_obj;
+	json_object_object_get_ex(buf_obj, "length", &length_obj);
+	buf->length = (__u32) json_object_get_uint64(length_obj);
+
+	json_object *m_obj;
+	json_object_object_get_ex(buf_obj, "m", &m_obj);
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		json_object *planes_obj;
+		json_object_object_get_ex(m_obj, "planes", &planes_obj);
+		 /* TODO add planes > 0 */
+		json_object *plane_obj = json_object_array_get_idx(planes_obj, 0);
+		buf->m.planes = retrace_v4l2_plane(plane_obj, buf->memory);
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
+		if (buf->memory == V4L2_MEMORY_MMAP) {
+			json_object *offset_obj;
+			json_object_object_get_ex(m_obj, "offset", &offset_obj);
+			buf->m.offset = (__u32) json_object_get_uint64(offset_obj);
+		}
+	}
+
+	if (buf->flags & V4L2_BUF_FLAG_REQUEST_FD) {
+		json_object *request_fd_obj;
+		json_object_object_get_ex(buf_obj, "request_fd", &request_fd_obj);
+		buf->request_fd = (__s32) get_fd_retrace_from_fd_trace(json_object_get_int(request_fd_obj));
+		if (buf->request_fd < 0) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "bad or missing file descriptor\n");
+		}
+	}
+
+	return buf;
+}
+
+void retrace_vidioc_querybuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_QUERYBUF, buf);
+
+	if (buf->memory == V4L2_MEMORY_MMAP) {
+		__u32 offset = 0;
+		if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
+		    (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT))
+			offset = buf->m.offset;
+		if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) ||
+		    (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+			offset = buf->m.planes->m.mem_offset;
+		if (!get_buffer_fd_retrace(buf->type, buf->index))
+			add_buffer_retrace(fd_retrace, buf->type, buf->index, offset);
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, index: %d, fd: %d, ",
+		        buftype2s(buf->type).c_str(), buf->index, fd_retrace);
+		perror("VIDIOC_QUERYBUF");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+
+	if (buf)
+		free(buf);
+}
+
+void retrace_vidioc_qbuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *ptr = retrace_v4l2_buffer(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_QBUF, ptr);
+
+	if (ptr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    ptr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (ptr->m.planes != nullptr) {
+			free(ptr->m.planes);
+		}
+	}
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, index: %d, fd: %d, ",
+		        buftype2s(ptr->type).c_str(), ptr->index, fd_retrace);
+		perror("VIDIOC_QBUF");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_dqbuf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	struct pollfd *pfds = (struct pollfd *) calloc(1, sizeof(struct pollfd));
+	if (pfds == NULL)
+		exit(EXIT_FAILURE);
+	pfds[0].fd = fd_retrace;
+	pfds[0].events = POLLIN;
+	poll(pfds, 1, 5000);
+	free(pfds);
+
+	ioctl(fd_retrace, VIDIOC_DQBUF, buf);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, index: %d, fd: %d, ",
+		        buftype2s(buf->type).c_str(), buf->index, fd_retrace);
+		perror("VIDIOC_DQBUF");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	if (buf)
+		free(buf);
+}
+
+void retrace_vidioc_prepare_buf(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_PREPARE_BUF, buf);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, index: %d, fd: %d, ",
+		        buftype2s(buf->type).c_str(), buf->index, fd_retrace);
+		perror("VIDIOC_PREPARE_BUF");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		if (buf->m.planes != nullptr) {
+			free(buf->m.planes);
+		}
+	}
+
+	if (buf)
+		free(buf);
+}
+
+void retrace_vidioc_create_bufs(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_create_buffers *ptr = retrace_v4l2_create_buffers_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_CREATE_BUFS, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		perror("VIDIOC_CREATE_BUFS");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_expbuf(int fd_retrace, json_object *ioctl_args_user, json_object *ioctl_args_driver)
+{
+	struct v4l2_exportbuffer *ptr = retrace_v4l2_exportbuffer_gen(ioctl_args_user);
+	ioctl(fd_retrace, VIDIOC_EXPBUF, ptr);
+
+	int buf_fd_retrace = ptr->fd;
+
+	/*
+	 * If a buffer was previously added to the retrace context using the video device
+	 * file descriptor, replace the video fd with the more specific buffer fd from EXPBUF.
+	 */
+	int fd_found_in_retrace_context = get_buffer_fd_retrace(ptr->type, ptr->index);
+	if (fd_found_in_retrace_context)
+		remove_buffer_retrace(fd_found_in_retrace_context);
+
+	add_buffer_retrace(buf_fd_retrace, ptr->type, ptr->index);
+
+	/* Retrace again to associate the original fd with the current buffer fd. */
+	memset(ptr, 0, sizeof(v4l2_exportbuffer));
+	ptr = retrace_v4l2_exportbuffer_gen(ioctl_args_driver);
+	int buf_fd_trace = ptr->fd;
+	add_fd(buf_fd_trace, buf_fd_retrace);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_EXPBUF");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_streamon(int fd_retrace, json_object *ioctl_args)
+{
+	json_object *type_obj;
+	json_object_object_get_ex(ioctl_args, "type", &type_obj);
+	v4l2_buf_type buf_type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
+	                                               v4l2_buf_type_val_def);
+
+	ioctl(fd_retrace, VIDIOC_STREAMON, &buf_type);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, ", buftype2s(buf_type).c_str());
+		perror("VIDIOC_STREAMON");
+	}
+}
+
+void retrace_vidioc_streamoff(int fd_retrace, json_object *ioctl_args)
+{
+	json_object *type_obj;
+	json_object_object_get_ex(ioctl_args, "type", &type_obj);
+	v4l2_buf_type buf_type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
+	                                               v4l2_buf_type_val_def);
+
+	ioctl(fd_retrace, VIDIOC_STREAMOFF, &buf_type);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		fprintf(stderr, "%s, ", buftype2s(buf_type).c_str());
+		perror("VIDIOC_STREAMOFF");
+	}
+}
+
+void retrace_vidioc_try_fmt(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_TRY_FMT, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_TRY_FMT");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_g_fmt(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_G_FMT, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_FMT");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_s_fmt(int fd_retrace, json_object *ioctl_args_user)
+{
+	struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args_user);
+
+	ioctl(fd_retrace, VIDIOC_S_FMT, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		perror("VIDIOC_S_FMT");
+	}
+
+	if (ptr)
+		free(ptr);
+}
+
+struct v4l2_streamparm *retrace_v4l2_streamparm(json_object *parent_obj, std::string key_name = "")
+{
+	struct v4l2_streamparm *p = (struct v4l2_streamparm *) calloc(1, sizeof(v4l2_streamparm));
+
+	json_object *v4l2_streamparm_obj;
+	json_object_object_get_ex(parent_obj, "v4l2_streamparm", &v4l2_streamparm_obj);
+
+	json_object *type_obj;
+	if (json_object_object_get_ex(v4l2_streamparm_obj, "type", &type_obj))
+		p->type = (__u32) s2val(json_object_get_string(type_obj), v4l2_buf_type_val_def);
+
+	if ((p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
+		p->parm.capture = *retrace_v4l2_captureparm_gen(v4l2_streamparm_obj);
+
+	if ((p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+		p->parm.output = *retrace_v4l2_outputparm_gen(v4l2_streamparm_obj);
+
+	return p;
+}
+
+void retrace_vidioc_g_parm (int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_streamparm *ptr = retrace_v4l2_streamparm(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_G_PARM, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_PARM");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_s_parm (int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_streamparm *ptr = retrace_v4l2_streamparm(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_S_PARM, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_S_PARM");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_queryctrl(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_queryctrl *ptr = retrace_v4l2_queryctrl_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_QUERYCTRL, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_QUERYCTRL");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_g_control(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_control *ptr = retrace_v4l2_control_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_G_CTRL, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_CTRL");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_s_control(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_control *ptr = retrace_v4l2_control_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_S_CTRL, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_S_CTRL");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_g_crop(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_crop *ptr = retrace_v4l2_crop_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_G_CROP, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_CROP");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_s_crop(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_crop *ptr = retrace_v4l2_crop_gen(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_S_CROP, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_S_CROP");
+
+	if (ptr)
+		free(ptr);
+}
+
+int retrace_v4l2_ext_control_value(json_object *ctrl_obj, const val_def *def)
+{
+	__s32 value = -1;
+
+	json_object *value_obj;
+	if (json_object_object_get_ex(ctrl_obj, "value", &value_obj))
+		value = (__s32) s2val(json_object_get_string(value_obj), def);
+
+	return value;
+}
+
+__u32 *retrace_v4l2_dynamic_array(json_object *v4l2_ext_control_obj)
+{
+	__u32 elems = 0;
+	json_object *elems_obj;
+	if (json_object_object_get_ex(v4l2_ext_control_obj, "elems", &elems_obj))
+		elems = (__u32) json_object_get_int64(elems_obj);
+
+	__u32 *ptr = static_cast<__u32 *>(calloc(elems, sizeof(__u32)));
+
+	json_object *p_u32_obj;
+	if (json_object_object_get_ex(v4l2_ext_control_obj, "p_u32", &p_u32_obj)) {
+		for (size_t i = 0; i < elems; i++) {
+			if (json_object_array_get_idx(p_u32_obj, i))
+				ptr[i] = (__u32) json_object_get_int64(json_object_array_get_idx(p_u32_obj, i));
+		}
+	}
+
+	return ptr;
+}
+
+struct v4l2_ext_control *retrace_v4l2_ext_control(json_object *parent_obj, int ctrl_idx)
+{
+	struct v4l2_ext_control *p = (struct v4l2_ext_control *) calloc(1, sizeof(v4l2_ext_control));
+
+	json_object *v4l2_ext_control_obj = json_object_array_get_idx(parent_obj, ctrl_idx);
+	if (!v4l2_ext_control_obj)
+		return p;
+
+	json_object *id_obj;
+	if (json_object_object_get_ex(v4l2_ext_control_obj, "id", &id_obj))
+		p->id = s2val(json_object_get_string(id_obj), control_val_def);
+
+	json_object *size_obj;
+	if (json_object_object_get_ex(v4l2_ext_control_obj, "size", &size_obj))
+		p->size = json_object_get_int64(size_obj);
+
+	switch (p->id) {
+	case V4L2_CID_STATELESS_H264_DECODE_MODE:
+		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
+		                                          v4l2_stateless_h264_decode_mode_val_def);
+		break;
+	case V4L2_CID_STATELESS_H264_START_CODE:
+		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
+		                                          v4l2_stateless_h264_start_code_val_def);
+		break;
+	case V4L2_CID_STATELESS_HEVC_DECODE_MODE:
+		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
+		                                          v4l2_stateless_hevc_decode_mode_val_def);
+		break;
+	case V4L2_CID_STATELESS_HEVC_START_CODE:
+		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
+		                                          v4l2_stateless_hevc_start_code_val_def);
+		break;
+	default:
+		break;
+	}
+
+	/* Don't retrace pointers that were not traced because they were null. */
+	if (p->size == 0)
+		return p;
+
+	switch (p->id) {
+	case V4L2_CID_STATELESS_VP8_FRAME:
+		p->ptr = retrace_v4l2_ctrl_vp8_frame_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SPS:
+		p->ptr = retrace_v4l2_ctrl_h264_sps_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_PPS:
+		p->ptr = retrace_v4l2_ctrl_h264_pps_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SCALING_MATRIX:
+		p->ptr = retrace_v4l2_ctrl_h264_scaling_matrix_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:
+		p->ptr = retrace_v4l2_ctrl_h264_pred_weights_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SLICE_PARAMS:
+		p->ptr = retrace_v4l2_ctrl_h264_slice_params_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
+		p->ptr = retrace_v4l2_ctrl_h264_decode_params_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_FWHT_PARAMS:
+		p->ptr = retrace_v4l2_ctrl_fwht_params_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_VP9_FRAME:
+		p->ptr = retrace_v4l2_ctrl_vp9_frame_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_VP9_COMPRESSED_HDR:
+		p->ptr = retrace_v4l2_ctrl_vp9_compressed_hdr_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SPS:
+		p->ptr = retrace_v4l2_ctrl_hevc_sps_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_PPS:
+		p->ptr = retrace_v4l2_ctrl_hevc_pps_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SLICE_PARAMS:
+		p->ptr = retrace_v4l2_ctrl_hevc_slice_params_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SCALING_MATRIX:
+		p->ptr = retrace_v4l2_ctrl_hevc_scaling_matrix_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_DECODE_PARAMS:
+		p->ptr = retrace_v4l2_ctrl_hevc_decode_params_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS:
+		p->p_u32 = retrace_v4l2_dynamic_array(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_MPEG2_SEQUENCE:
+		p->ptr = retrace_v4l2_ctrl_mpeg2_sequence_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_MPEG2_PICTURE:
+		p->ptr = retrace_v4l2_ctrl_mpeg2_picture_gen(v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
+		p->ptr = retrace_v4l2_ctrl_mpeg2_quantisation_gen(v4l2_ext_control_obj);
+		break;
+	default:
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: cannot retrace control: %s\n",
+		        val2s(p->id, control_val_def).c_str());
+		break;
+	}
+
+	return p;
+}
+
+struct v4l2_ext_controls *retrace_v4l2_ext_controls(json_object *parent_obj)
+{
+	struct v4l2_ext_controls *p = (struct v4l2_ext_controls *) calloc(1, sizeof(v4l2_ext_controls));
+
+	json_object *v4l2_ext_controls_obj;
+	json_object_object_get_ex(parent_obj, "v4l2_ext_controls", &v4l2_ext_controls_obj);
+
+	json_object *which_obj;
+	if (json_object_object_get_ex(v4l2_ext_controls_obj, "which", &which_obj))
+		p->which = (__u32) s2val(json_object_get_string(which_obj), which_val_def);
+
+	json_object *count_obj;
+	if (json_object_object_get_ex(v4l2_ext_controls_obj, "count", &count_obj))
+		p->count = (__u32) json_object_get_int64(count_obj);
+
+	json_object *error_idx_obj;
+	if (json_object_object_get_ex(v4l2_ext_controls_obj, "error_idx", &error_idx_obj))
+		p->error_idx = (__u32) json_object_get_int64(error_idx_obj);
+
+	/* request_fd is only valid for V4L2_CTRL_WHICH_REQUEST_VAL */
+	if (p->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
+		json_object *request_fd_obj;
+		if (json_object_object_get_ex(v4l2_ext_controls_obj, "request_fd", &request_fd_obj)) {
+
+			int request_fd_trace = json_object_get_int(request_fd_obj);
+			int request_fd_retrace = get_fd_retrace_from_fd_trace(request_fd_trace);
+			if (request_fd_retrace < 0) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "bad file descriptor\n");
+				return p;
+			} else {
+				p->request_fd = (__s32) request_fd_retrace;
+			}
+		}
+	}
+
+	json_object *controls_obj;
+	if (json_object_object_get_ex(v4l2_ext_controls_obj, "controls", &controls_obj)) {
+
+		p->controls = (struct v4l2_ext_control *) calloc(p->count, sizeof(v4l2_ext_control));
+
+		for (__u32 i = 0; i < p->count; i++) {
+			void *temp = retrace_v4l2_ext_control(controls_obj, i);
+			if (temp != nullptr) {
+				p->controls[i] = *(static_cast<struct v4l2_ext_control*>(temp));
+			}
+		}
+	}
+
+	return p;
+}
+
+void retrace_vidioc_try_ext_ctrls(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_TRY_EXT_CTRLS, ptr);
+
+	if (ptr)
+		free(ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_TRY_EXT_CTRLS");
+}
+
+void retrace_vidioc_g_ext_ctrls(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_G_EXT_CTRLS, ptr);
+
+	if (ptr)
+		free(ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_EXT_CTRLS");
+}
+
+void retrace_vidioc_s_ext_ctrls(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
+	ioctl(fd_retrace, VIDIOC_S_EXT_CTRLS, ptr);
+
+	if (ptr)
+		free(ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0)) {
+		perror("VIDIOC_S_EXT_CTRLS");
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_context();
+		}
+	}
+}
+
+void retrace_vidioc_try_encoder_cmd(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_encoder_cmd *ptr = retrace_v4l2_encoder_cmd_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_TRY_ENCODER_CMD, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_TRY_ENCODER_CMD");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_encoder_cmd(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_encoder_cmd *ptr = retrace_v4l2_encoder_cmd_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_ENCODER_CMD, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_ENCODER_CMD");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_g_selection(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_selection *ptr = retrace_v4l2_selection_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_G_SELECTION, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_G_SELECTION");
+
+	if (ptr)
+		free(ptr);
+
+}
+
+void retrace_vidioc_s_selection(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_selection *ptr = retrace_v4l2_selection_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_S_SELECTION, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_S_SELECTION");
+
+	if (ptr)
+		free(ptr);
+}
+
+struct v4l2_decoder_cmd *retrace_v4l2_decoder_cmd(json_object *parent_obj)
+{
+	struct v4l2_decoder_cmd *p = (struct v4l2_decoder_cmd *) calloc(1, sizeof(v4l2_decoder_cmd));
+
+	json_object *v4l2_decoder_cmd_obj;
+	json_object_object_get_ex(parent_obj, "v4l2_decoder_cmd", &v4l2_decoder_cmd_obj);
+
+	json_object *cmd_obj;
+	if (json_object_object_get_ex(v4l2_decoder_cmd_obj, "cmd", &cmd_obj))
+		p->cmd = (__u32) s2val(json_object_get_string(cmd_obj), decoder_cmd_val_def);
+	else
+		p->cmd = 0xBAD; /* Since V4L2_DEC_CMD_START is 0 and p->cmd is unsigned */
+
+	std::string flags;
+	json_object *flags_obj;
+	if (json_object_object_get_ex(v4l2_decoder_cmd_obj, "flags", &flags_obj))
+		flags = json_object_get_string(flags_obj);
+
+	switch (p->cmd) {
+	case V4L2_DEC_CMD_START: {
+		if (flags == "V4L2_DEC_CMD_START_MUTE_AUDIO")
+			p->flags = V4L2_DEC_CMD_START_MUTE_AUDIO;
+
+		json_object *start_obj;
+		json_object_object_get_ex(v4l2_decoder_cmd_obj, "start", &start_obj);
+
+		json_object *speed_obj;
+		if (json_object_object_get_ex(start_obj, "speed", &speed_obj))
+			p->start.speed = json_object_get_int(speed_obj);
+
+		std::string format;
+		json_object *format_obj;
+		if (json_object_object_get_ex(start_obj, "format", &format_obj))
+			format = json_object_get_string(format_obj);
+
+		if (format == "V4L2_DEC_START_FMT_GOP")
+			p->start.format = V4L2_DEC_START_FMT_GOP;
+		else if (format == "V4L2_DEC_START_FMT_NONE")
+			p->start.format = V4L2_DEC_START_FMT_NONE;
+		break;
+	}
+	case V4L2_DEC_CMD_STOP: {
+		if (flags == "V4L2_DEC_CMD_STOP_TO_BLACK")
+			p->flags = V4L2_DEC_CMD_STOP_TO_BLACK;
+		else if (flags == "V4L2_DEC_CMD_STOP_IMMEDIATELY")
+			p->flags = V4L2_DEC_CMD_STOP_IMMEDIATELY;
+
+		json_object *stop_obj;
+		json_object_object_get_ex(v4l2_decoder_cmd_obj, "stop", &stop_obj);
+
+		json_object *pts_obj;
+		if (json_object_object_get_ex(stop_obj, "pts", &pts_obj))
+			p->stop.pts = (__u64) json_object_get_uint64(pts_obj);
+		break;
+	}
+	case V4L2_DEC_CMD_PAUSE: {
+		if (flags == "V4L2_DEC_CMD_PAUSE_TO_BLACK")
+			p->flags = V4L2_DEC_CMD_PAUSE_TO_BLACK;
+		break;
+	}
+	default:
+		break;
+	}
+
+	return p;
+}
+
+void retrace_vidioc_try_decoder_cmd(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_decoder_cmd *ptr = retrace_v4l2_decoder_cmd(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_TRY_DECODER_CMD, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_TRY_DECODER_CMD");
+
+	if (ptr)
+		free (ptr);
+}
+
+void retrace_vidioc_decoder_cmd(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_decoder_cmd *ptr = retrace_v4l2_decoder_cmd(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_DECODER_CMD, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_DECODER_CMD");
+
+	if (ptr)
+		free (ptr);
+}
+
+void retrace_vidioc_query_ext_ctrl(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_query_ext_ctrl *ptr = retrace_v4l2_query_ext_ctrl_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_QUERY_EXT_CTRL, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE"))
+		perror("VIDIOC_QUERY_EXT_CTRL");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_enum_fmt(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_fmtdesc *ptr = retrace_v4l2_fmtdesc_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_ENUM_FMT, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE"))
+		perror("VIDIOC_ENUM_FMT");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_vidioc_querycap(int fd_retrace, json_object *ioctl_args)
+{
+	struct v4l2_capability *ptr = retrace_v4l2_capability_gen(ioctl_args);
+
+	ioctl(fd_retrace, VIDIOC_QUERYCAP, ptr);
+
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") || (errno != 0))
+		perror("VIDIOC_QUERYCAP");
+
+	if (ptr)
+		free(ptr);
+}
+
+void retrace_media_ioc_request_alloc(int fd_retrace, json_object *ioctl_args)
+{
+	/* Get the original request file descriptor from the original trace file. */
+	json_object *request_fd_trace_obj;
+	json_object_object_get_ex(ioctl_args, "request_fd", &request_fd_trace_obj);
+	int request_fd_trace = json_object_get_int(request_fd_trace_obj);
+
+	/* Allocate a request in the retrace context. */
+	__s32 request_fd_retrace = 0;
+	ioctl(fd_retrace, MEDIA_IOC_REQUEST_ALLOC, &request_fd_retrace);
+
+	/* Associate the original request file descriptor with the current request file descriptor. */
+	add_fd(request_fd_trace, request_fd_retrace);
+}
+
+void retrace_ioctl(json_object *syscall_obj)
+{
+	long cmd = 0;
+	int fd_retrace = 0;
+
+	json_object *fd_trace_obj;
+	json_object_object_get_ex(syscall_obj, "fd", &fd_trace_obj);
+	fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
+	if (fd_retrace < 0) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "bad file descriptor\n");
+		return;
+	}
+
+	json_object *cmd_obj;
+	json_object_object_get_ex(syscall_obj, "ioctl", &cmd_obj);
+	cmd = s2val(json_object_get_string(cmd_obj), ioctl_val_def);
+
+	json_object *ioctl_args_user;
+	json_object_object_get_ex(syscall_obj, "from_userspace", &ioctl_args_user);
+
+	json_object *ioctl_args_driver;
+	json_object_object_get_ex(syscall_obj, "from_driver", &ioctl_args_driver);
+
+	switch (cmd) {
+	case VIDIOC_QUERYCAP:
+		retrace_vidioc_querycap(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_ENUM_FMT:
+		retrace_vidioc_enum_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_TRY_FMT:
+		retrace_vidioc_try_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_FMT:
+		retrace_vidioc_g_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_FMT:
+		retrace_vidioc_s_fmt(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_REQBUFS:
+		retrace_vidioc_reqbufs(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QUERYBUF:
+		retrace_vidioc_querybuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QBUF:
+		retrace_vidioc_qbuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_EXPBUF:
+		retrace_vidioc_expbuf(fd_retrace, ioctl_args_user, ioctl_args_driver);
+		break;
+	case VIDIOC_DQBUF:
+		retrace_vidioc_dqbuf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_STREAMON:
+		retrace_vidioc_streamon(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_STREAMOFF:
+		retrace_vidioc_streamoff(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_PARM:
+		retrace_vidioc_g_parm(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_PARM:
+		retrace_vidioc_s_parm(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_CTRL:
+		retrace_vidioc_g_control(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_CTRL:
+		retrace_vidioc_s_control(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QUERYCTRL:
+		retrace_vidioc_queryctrl(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_CROP:
+		retrace_vidioc_g_crop(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_CROP:
+		retrace_vidioc_s_crop(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_EXT_CTRLS:
+		retrace_vidioc_g_ext_ctrls(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_TRY_EXT_CTRLS:
+		retrace_vidioc_try_ext_ctrls(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_EXT_CTRLS:
+		retrace_vidioc_s_ext_ctrls(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_TRY_ENCODER_CMD:
+		retrace_vidioc_try_encoder_cmd(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_ENCODER_CMD:
+		retrace_vidioc_encoder_cmd(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_CREATE_BUFS:
+		retrace_vidioc_create_bufs(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_G_SELECTION:
+		retrace_vidioc_g_selection(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_S_SELECTION:
+		retrace_vidioc_s_selection(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_PREPARE_BUF:
+		retrace_vidioc_prepare_buf(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_TRY_DECODER_CMD:
+		retrace_vidioc_try_decoder_cmd(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_DECODER_CMD:
+		retrace_vidioc_decoder_cmd(fd_retrace, ioctl_args_user);
+		break;
+	case VIDIOC_QUERY_EXT_CTRL:
+		retrace_vidioc_query_ext_ctrl(fd_retrace, ioctl_args_user);
+		break;
+	case MEDIA_IOC_REQUEST_ALLOC:
+		retrace_media_ioc_request_alloc(fd_retrace, ioctl_args_driver);
+		break;
+	case MEDIA_REQUEST_IOC_QUEUE:
+		ioctl(fd_retrace, MEDIA_REQUEST_IOC_QUEUE);
+		break;
+	case MEDIA_REQUEST_IOC_REINIT:
+		ioctl(fd_retrace, MEDIA_REQUEST_IOC_REINIT);
+		break;
+	default:
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: \'%s\' will not be retraced.\n", json_object_get_string(cmd_obj));
+		break;
+	}
+}
+
+void retrace_mem(json_object *mem_obj)
+{
+	json_object *type_obj;
+	json_object_object_get_ex(mem_obj, "mem_dump", &type_obj);
+	v4l2_buf_type type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
+	                                           v4l2_buf_type_val_def);
+	json_object *bytesused_obj;
+	json_object_object_get_ex(mem_obj, "bytesused", &bytesused_obj);
+	int bytesused = json_object_get_int64(bytesused_obj);
+	if (!bytesused)
+		return;
+
+	json_object *offset_obj;
+	json_object_object_get_ex(mem_obj, "offset", &offset_obj);
+	__u32 offset = json_object_get_int64(offset_obj);
+
+	json_object *address_obj;
+	json_object_object_get_ex(mem_obj, "address", &address_obj);
+	long buffer_address_trace = json_object_get_int64(address_obj);
+
+	long buffer_address_retrace = get_retrace_address_from_trace_address(buffer_address_trace);
+
+	unsigned char *buffer_pointer = (unsigned char *) buffer_address_retrace;
+
+	/* Get the encoded data from the json file and write it to output buffer memory. */
+	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
+		write_to_output_buffer(buffer_pointer, bytesused, mem_obj);
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "%s, bytesused: %d, offset: %d, addr: %ld\n",
+		        buftype2s(type).c_str(), bytesused, offset, buffer_address_retrace);
+		print_context();
+	}
+}
+
+void retrace_object(json_object *jobj)
+{
+	errno = 0;
+	json_object *temp_obj;
+	if (json_object_object_get_ex(jobj, "ioctl", &temp_obj)) {
+		retrace_ioctl(jobj);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "open", &temp_obj)) {
+		retrace_open(jobj, false);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "open64", &temp_obj)) {
+		retrace_open(jobj, true);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "close", &temp_obj)) {
+		retrace_close(jobj);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "mmap", &temp_obj)) {
+		retrace_mmap(jobj, false);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "mmap64", &temp_obj)) {
+		retrace_mmap(jobj, true);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "munmap", &temp_obj)) {
+		retrace_munmap(jobj);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "mem_dump", &temp_obj)) {
+		retrace_mem(jobj);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "package_version", &temp_obj)) {
+		compare_program_versions(jobj);
+		return;
+	}
+
+	if (json_object_object_get_ex(jobj, "Trace", &temp_obj)) {
+		return;
+	}
+
+	fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+	fprintf(stderr, "warning: unexpected JSON object in trace file.\n");
+}
+
+void retrace_array(json_object *root_array_obj)
+{
+	json_object *jobj;
+	struct array_list *al = json_object_get_array(root_array_obj);
+	size_t json_objects_in_file = array_list_length(al);
+
+	if (json_objects_in_file < 3) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: trace file may be empty.\n");
+	}
+
+	for (size_t i = 0; i < json_objects_in_file; i++) {
+		jobj = (json_object *) array_list_get_idx(al, i);
+		retrace_object(jobj);
+	}
+}
+
+int retracer (std::string trace_filename)
+{
+	FILE *trace_file = fopen(trace_filename.c_str(), "r");
+	if (trace_file == NULL) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "Trace file error: \'%s\'\n", trace_filename.c_str());
+		return 1;
+	}
+	fclose(trace_file);
+
+	fprintf(stderr, "Retracing: %s\n", trace_filename.c_str());
+
+	json_object *root_array_obj = json_object_from_file(trace_filename.c_str());
+
+	if (root_array_obj == nullptr) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "cannot get JSON-object from file: %s\n", trace_filename.c_str());
+		return 1;
+	}
+
+	retrace_array(root_array_obj);
+	json_object_put(root_array_obj);
+
+	return 0;
+}
diff --git a/utils/v4l2-tracer/retrace.h b/utils/v4l2-tracer/retrace.h
new file mode 100644
index 00000000..a4cef1f0
--- /dev/null
+++ b/utils/v4l2-tracer/retrace.h
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef RETRACE_H
+#define RETRACE_H
+
+#include "v4l2-tracer-common.h"
+#include "retrace-gen.h"
+
+struct buffer_retrace {
+	int fd;
+	__u32 type;
+	__u32 index;
+	__u32 offset;
+	long address_trace;
+	long address_retrace;
+};
+
+struct retrace_context {
+	/* Key is a file descriptor from the trace, value is the corresponding fd in the retrace. */
+	std::unordered_map<int, int> retrace_fds;
+	/* List of output and capture buffers being retraced. */
+	std::list<struct buffer_retrace> buffers;
+};
+
+bool buffer_in_retrace_context(int fd, __u32 offset = 0);
+int get_buffer_fd_retrace(__u32 type, __u32 index);
+void add_buffer_retrace(int fd, __u32 type, __u32 index, __u32 offset = 0);
+void remove_buffer_retrace(int fd);
+void set_buffer_address_retrace(int fd, __u32 offset, long address_trace, long address_retrace);
+long get_retrace_address_from_trace_address(long address_trace);
+void add_fd(int fd_trace, int fd_retrace);
+int get_fd_retrace_from_fd_trace(int fd_trace);
+std::string get_path_retrace_from_path_trace(std::string path_trace, json_object *jobj);
+void write_to_output_buffer(unsigned char *buffer_pointer, int bytesused, json_object *mem_obj);
+void compare_program_versions(json_object *v4l2_tracer_info_obj);
+void print_context(void);
+
+#endif
diff --git a/utils/v4l2-tracer/trace-helper.cpp b/utils/v4l2-tracer/trace-helper.cpp
new file mode 100644
index 00000000..ccf88c5d
--- /dev/null
+++ b/utils/v4l2-tracer/trace-helper.cpp
@@ -0,0 +1,503 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include <math.h>
+#include "trace.h"
+
+struct trace_context ctx_trace = {};
+
+bool is_video_or_media_device(const char *path)
+{
+	std::string dev_path_video = "/dev/video";
+	std::string dev_path_media = "/dev/media";
+	if (strncmp(path, dev_path_video.c_str(), dev_path_video.length()) &&
+	    strncmp(path, dev_path_media.c_str(), dev_path_media.length()))
+		return false;
+	return true;
+}
+
+void add_device(int fd, std::string path)
+{
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "fd: %d, path: %s\n", fd, path.c_str());
+	}
+	std::pair<int, std::string> new_pair = std::make_pair(fd, path);
+	ctx_trace.devices.insert(new_pair);
+}
+
+std::string get_device(int fd)
+{
+	std::string path;
+	std::unordered_map<int, std::string>::const_iterator it;
+	it = ctx_trace.devices.find(fd);
+	if (it != ctx_trace.devices.end())
+		path = it->second;
+	return path;
+}
+
+void print_devices(void)
+{
+	if (ctx_trace.devices.size())
+		fprintf(stderr, "Devices:\n");
+	for (auto &device_pair : ctx_trace.devices)
+		fprintf(stderr, "fd: %d, path: %s\n", device_pair.first, device_pair.second.c_str());
+}
+
+void print_decode_order(void)
+{
+	fprintf(stderr, "Decode order: ");
+	for (auto &num : ctx_trace.decode_order)
+		fprintf(stderr, "%ld, ",  num);
+	fprintf(stderr, ".\n");
+}
+
+void set_decode_order(long decode_order)
+{
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "%ld\n", decode_order);
+	}
+
+	std::list<long>::iterator it;
+	it = find(ctx_trace.decode_order.begin(), ctx_trace.decode_order.end(), decode_order);
+	if (it == ctx_trace.decode_order.end())
+		ctx_trace.decode_order.push_front(decode_order);
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+		print_decode_order();
+}
+
+long get_decode_order(void)
+{
+	long decode_order = 0;
+	if (!ctx_trace.decode_order.empty())
+		decode_order = ctx_trace.decode_order.front();
+	return decode_order;
+}
+
+void add_buffer_trace(int fd, __u32 type, __u32 index, __u32 offset = 0)
+{
+	struct buffer_trace buf = {};
+	buf.fd = fd;
+	buf.type = type;
+	buf.index = index;
+	buf.offset = offset;
+	buf.display_order = -1;
+	ctx_trace.buffers.push_front(buf);
+}
+
+void remove_buffer_trace(int fd)
+{
+	for (auto it = ctx_trace.buffers.begin(); it != ctx_trace.buffers.end(); ++it) {
+		if (it->fd == fd) {
+			ctx_trace.buffers.erase(it);
+			break;
+		}
+	}
+}
+
+bool buffer_in_trace_context(int fd, __u32 offset)
+{
+	bool buffer_in_trace_context = false;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			buffer_in_trace_context = true;
+			break;
+		}
+	}
+	return buffer_in_trace_context;
+}
+
+int get_buffer_fd_trace(__u32 type, __u32 index)
+{
+	int fd = 0;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.type == type) && (b.index == index)) {
+			fd = b.fd;
+			break;
+		}
+	}
+	return fd;
+}
+
+__u32 get_buffer_type_trace(int fd, __u32 offset)
+{
+	__u32 type = 0;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			type = b.type;
+			break;
+		}
+	}
+	return type;
+}
+
+int get_buffer_index_trace(int fd, __u32 offset)
+{
+	int index = -1;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			index = b.index;
+			break;
+		}
+	}
+	return index;
+}
+
+__u32 get_buffer_offset_trace(__u32 type, __u32 index)
+{
+	__u32 offset = 0;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.type == type) && (b.index == index)) {
+			offset = b.offset;
+			break;
+		}
+	}
+	return offset;
+}
+
+void set_buffer_bytesused_trace(int fd, __u32 offset, __u32 bytesused)
+{
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			b.bytesused = bytesused;
+			break;
+		}
+	}
+}
+
+long get_buffer_bytesused_trace(int fd, __u32 offset)
+{
+	long bytesused = 0;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			bytesused = b.bytesused;
+			break;
+		}
+	}
+	return bytesused;
+}
+
+void set_buffer_display_order(int fd, __u32 offset, long display_order)
+{
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "%ld\n", display_order);
+	}
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			b.display_order = display_order;
+			break;
+		}
+	}
+}
+
+void set_buffer_address_trace(int fd, __u32 offset, unsigned long address)
+{
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			b.address = address;
+			break;
+		}
+	}
+}
+
+unsigned long get_buffer_address_trace(int fd, __u32 offset)
+{
+	unsigned long address = 0;
+	for (auto &b : ctx_trace.buffers) {
+		if ((b.fd == fd) && (b.offset == offset)) {
+			address = b.address;
+			break;
+		}
+	}
+	return address;
+}
+
+bool buffer_is_mapped(unsigned long buffer_address)
+{
+	bool ret = false;
+	for (auto &b : ctx_trace.buffers) {
+		if (b.address == buffer_address) {
+			ret = true;
+			break;
+		}
+	}
+	return ret;
+}
+
+void print_buffers_trace(void)
+{
+	for (auto &b : ctx_trace.buffers) {
+		fprintf(stderr, "fd: %d, %s, index: %d, display_order: %ld, bytesused: %d, ",
+		        b.fd, buftype2s(b.type).c_str(), b.index, b.display_order, b.bytesused);
+		fprintf(stderr, "address: %lu, offset: %u \n",  b.address, b.offset);
+	}
+}
+
+unsigned get_expected_length_trace()
+{
+	/*
+	 * TODO: this assumes that the stride is equal to the real width and that the
+	 * padding follows the end of the chroma plane. It could be improved by
+	 * following the model in v4l2-ctl-streaming.cpp read_write_padded_frame()
+	 */
+	unsigned expected_length = ctx_trace.width * ctx_trace.height;
+	if (ctx_trace.pixelformat == V4L2_PIX_FMT_NV12 || ctx_trace.pixelformat == V4L2_PIX_FMT_YUV420) {
+		expected_length *= 3;
+		expected_length /= 2;
+		expected_length += (expected_length % 2);
+	}
+	return expected_length;
+}
+
+void s_ext_ctrls_setup(struct v4l2_ext_controls *ext_controls)
+{
+	if (ext_controls->which != V4L2_CTRL_WHICH_REQUEST_VAL)
+		return;
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+		fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+
+	/*
+	 * Since userspace sends H264 frames out of order, get information
+	 * about the correct display order of each frame so that v4l2-tracer
+	 * can write the decoded frames to a file.
+	 */
+	for (__u32 i = 0; i < ext_controls->count; i++) {
+		struct v4l2_ext_control ctrl = ext_controls->controls[i];
+
+		switch (ctrl.id) {
+		case V4L2_CID_STATELESS_H264_SPS: {
+			ctx_trace.max_pic_order_cnt_lsb = pow(2, ctrl.p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
+			break;
+		}
+		case V4L2_CID_STATELESS_H264_DECODE_PARAMS: {
+			long pic_order_cnt_msb;
+			int max = ctx_trace.max_pic_order_cnt_lsb;
+			long prev_pic_order_cnt_msb = get_decode_order();
+			int prev_pic_order_cnt_lsb = ctx_trace.pic_order_cnt_lsb;
+			int pic_order_cnt_lsb = ctrl.p_h264_decode_params->pic_order_cnt_lsb;
+
+			if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+				fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "\tprev_pic_order_cnt_lsb: %d\n", prev_pic_order_cnt_lsb);
+				fprintf(stderr, "\tprev_pic_order_cnt_msb: %ld\n", prev_pic_order_cnt_msb);
+				fprintf(stderr, "\tpic_order_cnt_lsb: %d\n", pic_order_cnt_lsb);
+			}
+
+			/* The first frame. */
+			if (prev_pic_order_cnt_msb == 0)
+				pic_order_cnt_msb = 0;
+
+			/*
+			 * TODO: improve the displaying of decoded frames following H264 specification
+			 * 8.2.1.1. For now, dump all the previously decoded frames when an IDR_PIC is
+			 * received to avoid losing frames although this will still sometimes result
+			 * in frames out of order.
+			 */
+			if (ctrl.p_h264_decode_params->flags & V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC) {
+				if (ctx_trace.compressed_frame_count)
+					trace_mem_decoded();
+			}
+
+			/*
+			 * When pic_order_cnt_lsb wraps around to zero, adjust the total count using
+			 * max to keep the correct display order.
+			 */
+			if ((pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
+				((prev_pic_order_cnt_lsb - pic_order_cnt_lsb) >= (max / 2))) {
+				pic_order_cnt_msb = prev_pic_order_cnt_msb + max;
+			} else if ((pic_order_cnt_lsb > prev_pic_order_cnt_lsb) &&
+				((pic_order_cnt_lsb - prev_pic_order_cnt_lsb) > (max / 2))) {
+				pic_order_cnt_msb = prev_pic_order_cnt_msb - max;
+			} else {
+				pic_order_cnt_msb = prev_pic_order_cnt_msb + (pic_order_cnt_lsb - prev_pic_order_cnt_lsb);
+			}
+
+			if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+				fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "\tpic_order_cnt_msb: %ld\n", pic_order_cnt_msb);
+			}
+			ctx_trace.pic_order_cnt_lsb = pic_order_cnt_lsb;
+			set_decode_order(pic_order_cnt_msb);
+			break;
+		}
+		default:
+			break;
+		}
+	}
+}
+
+void qbuf_setup(struct v4l2_buffer *buf)
+{
+	if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "%s, index: %d\n", buftype2s(buf->type).c_str(), buf->index);
+	}
+
+	int buf_fd = get_buffer_fd_trace(buf->type, buf->index);
+	__u32 buf_offset = get_buffer_offset_trace(buf->type, buf->index);
+
+	__u32 bytesused = 0;
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		bytesused = buf->m.planes[0].bytesused;
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		bytesused = buf->bytesused;
+	set_buffer_bytesused_trace(buf_fd, buf_offset, bytesused);
+
+	/* The output buffer should have compressed data just before it is queued, so trace it. */
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
+		trace_mem_encoded(buf_fd, buf_offset);
+		ctx_trace.compressed_frame_count = ctx_trace.compressed_frame_count + 1;
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+
+		/* If the capture buffer is queued for reuse, trace it before it is reused. */
+		if (ctx_trace.compressed_frame_count)
+			trace_mem_decoded();
+
+		/* H264 sets display order in controls, otherwise display just in the order queued. */
+		if (ctx_trace.compression_format != V4L2_PIX_FMT_H264_SLICE)
+			set_decode_order(get_decode_order() + 1);
+
+		set_buffer_display_order(buf_fd, buf_offset, get_decode_order());
+
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			print_decode_order();
+			print_buffers_trace();
+		}
+	}
+}
+
+void streamoff_cleanup(v4l2_buf_type buf_type)
+{
+
+	if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+		fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE") ||
+	    getenv("V4L2_TRACER_OPTION_WRITE_DECODED_TO_YUV_FILE")) {
+		fprintf(stderr, "VIDIOC_STREAMOFF: %s\n", buftype2s(buf_type).c_str());
+		fprintf(stderr, "%s, %s %s, width: %d, height: %d\n",
+		        val2s(ctx_trace.compression_format, v4l2_pix_fmt_val_def).c_str(),
+		        val2s(ctx_trace.pixelformat, v4l2_pix_fmt_val_def).c_str(),
+		        fcc2s(ctx_trace.pixelformat).c_str(), ctx_trace.width, ctx_trace.height);
+	}
+
+	/*
+	 * Before turning off the stream, trace any remaining capture buffers that were missed
+	 * because they were not queued for reuse.
+	 */
+	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+		if (ctx_trace.compressed_frame_count)
+			trace_mem_decoded();
+	}
+}
+
+void g_fmt_setup_trace(struct v4l2_format *format)
+{
+	if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+		ctx_trace.width = format->fmt.pix.width;
+		ctx_trace.height = format->fmt.pix.height;
+		ctx_trace.pixelformat = format->fmt.pix.pixelformat;
+	}
+	if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		ctx_trace.width = format->fmt.pix_mp.width;
+		ctx_trace.height = format->fmt.pix_mp.height;
+		ctx_trace.pixelformat = format->fmt.pix_mp.pixelformat;
+	}
+	if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
+		ctx_trace.compression_format = format->fmt.pix.pixelformat;
+	if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
+		ctx_trace.compression_format = format->fmt.pix_mp.pixelformat;
+}
+
+void s_fmt_setup(struct v4l2_format *format)
+{
+	if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
+		ctx_trace.compression_format = format->fmt.pix.pixelformat;
+	if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
+		ctx_trace.compression_format = format->fmt.pix_mp.pixelformat;
+}
+
+void expbuf_setup(struct v4l2_exportbuffer *export_buffer)
+{
+	__u32 type = export_buffer->type;
+	__u32 index = export_buffer->index;
+	int fd_found_in_trace_context = get_buffer_fd_trace(type, index);
+
+	/* If the buffer was already added to the trace context don't add it again. */
+	if (fd_found_in_trace_context == export_buffer->fd)
+		return;
+
+	/*
+	 * If a buffer was previously added to the trace context using the video device
+	 * file descriptor, replace the video fd with the more specific buffer fd from EXPBUF.
+	 */
+	if (fd_found_in_trace_context)
+		remove_buffer_trace(fd_found_in_trace_context);
+
+	add_buffer_trace(export_buffer->fd, type, index);
+}
+
+void querybuf_setup(int fd, struct v4l2_buffer *buf)
+{
+	/* If the buffer was already added to the trace context don't add it again. */
+	if (get_buffer_fd_trace(buf->type, buf->index))
+		return;
+
+	if (buf->memory == V4L2_MEMORY_MMAP) {
+		__u32 offset = 0;
+		if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
+		    (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT))
+			offset = buf->m.offset;
+		if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) ||
+		    (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+			offset = buf->m.planes->m.mem_offset;
+		add_buffer_trace(fd, buf->type, buf->index, offset);
+	}
+}
+
+void query_ext_ctrl_setup(int fd, struct v4l2_query_ext_ctrl *p)
+{
+	if (p->flags & (V4L2_CTRL_FLAG_HAS_PAYLOAD|V4L2_CTRL_FLAG_DYNAMIC_ARRAY)) {
+		if (p->id == V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS)
+			ctx_trace.elems = p->elems;
+	}
+}
+
+void write_json_object_to_json_file(json_object *jobj)
+{
+	std::string json_str;
+	if (getenv("V4L2_TRACER_OPTION_COMPACT_PRINT"))
+		json_str = json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PLAIN);
+	else
+		json_str = json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PRETTY);
+
+	if (!ctx_trace.trace_file) {
+		ctx_trace.trace_filename = getenv("TRACE_ID");
+		ctx_trace.trace_filename += ".json";
+		ctx_trace.trace_file = fopen(ctx_trace.trace_filename.c_str(), "a");
+	}
+
+	fwrite(json_str.c_str(), sizeof(char), json_str.length(), ctx_trace.trace_file);
+	fputs(",\n", ctx_trace.trace_file);
+	fflush(ctx_trace.trace_file);
+}
+
+void close_json_file(void)
+{
+	if (ctx_trace.trace_file) {
+		fclose(ctx_trace.trace_file);
+		ctx_trace.trace_file = 0;
+	}
+}
diff --git a/utils/v4l2-tracer/trace.cpp b/utils/v4l2-tracer/trace.cpp
new file mode 100644
index 00000000..f0bd7002
--- /dev/null
+++ b/utils/v4l2-tracer/trace.cpp
@@ -0,0 +1,588 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include "trace.h"
+
+extern struct trace_context ctx_trace;
+
+void trace_open(int fd, const char *path, int oflag, mode_t mode, bool is_open64)
+{
+	json_object *open_obj = json_object_new_object();
+	json_object_object_add(open_obj, "fd", json_object_new_int(fd));
+
+	json_object *open_args = json_object_new_object();
+	json_object_object_add(open_args, "path", json_object_new_string(path));
+	json_object_object_add(open_args, "oflag",
+	                       json_object_new_string(val2s(oflag, open_val_def).c_str()));
+	json_object_object_add(open_args, "mode", json_object_new_string(number2s_oct(mode).c_str()));
+	if (is_open64)
+		json_object_object_add(open_obj, "open64", open_args);
+	else
+		json_object_object_add(open_obj, "open", open_args);
+
+	/* Add additional topology information about device. */
+	std::string path_str = path;
+	bool is_media = path_str.find("media") != path_str.npos;
+	bool is_video = path_str.find("video") != path_str.npos;
+
+	int media_fd = -1;
+	if (is_media)
+		media_fd = fd;
+
+	std::string driver;
+	if (is_video) {
+		struct v4l2_capability cap = {};
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		ioctl(fd, VIDIOC_QUERYCAP, &cap);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+
+		std::string path_media = get_path_media(reinterpret_cast<const char *>(cap.driver));
+
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		media_fd = open(path_media.c_str(), O_RDONLY);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+	}
+
+	struct media_device_info info = {};
+	ioctl(media_fd, MEDIA_IOC_DEVICE_INFO, &info);
+
+	json_object_object_add(open_obj, "driver", json_object_new_string(info.driver));
+	json_object_object_add(open_obj, "bus_info", json_object_new_string(info.bus_info));
+
+	if (is_video) {
+		std::list<std::string> linked_entities = get_linked_entities(media_fd, path_str);
+		json_object *linked_entities_obj = json_object_new_array();
+		for (auto &name : linked_entities)
+			json_object_array_add(linked_entities_obj, json_object_new_string(name.c_str()));
+		json_object_object_add(open_obj, "linked_entities", linked_entities_obj);
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		close(media_fd);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+	}
+
+	write_json_object_to_json_file(open_obj);
+	json_object_put(open_obj);
+}
+
+void trace_mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off, unsigned long buf_address, bool is_mmap64)
+{
+	json_object *mmap_obj = json_object_new_object();
+
+	if (errno)
+		json_object_object_add(mmap_obj, "errno", json_object_new_string(strerrorname_np(errno)));
+
+	json_object *mmap_args = json_object_new_object();
+	json_object_object_add(mmap_args, "addr", json_object_new_int64((int64_t)addr));
+	json_object_object_add(mmap_args, "len", json_object_new_uint64(len));
+	json_object_object_add(mmap_args, "prot", json_object_new_int(prot));
+	json_object_object_add(mmap_args, "flags", json_object_new_string(number2s(flags).c_str()));
+	json_object_object_add(mmap_args, "fildes", json_object_new_int(fildes));
+	json_object_object_add(mmap_args, "off", json_object_new_int64(off));
+
+	if (is_mmap64)
+		json_object_object_add(mmap_obj, "mmap64", mmap_args);
+	else
+		json_object_object_add(mmap_obj, "mmap", mmap_args);
+
+	json_object_object_add(mmap_obj, "buffer_address", json_object_new_uint64(buf_address));
+
+	write_json_object_to_json_file(mmap_obj);
+	json_object_put(mmap_obj);
+}
+
+json_object *trace_buffer(unsigned char *buffer_pointer, __u32 bytesused)
+{
+	char buf[5];
+	std::string s;
+	int byte_count_per_line = 0;
+	json_object *mem_array_obj = json_object_new_array();
+
+	for (__u32 i = 0; i < bytesused; i++) {
+		memset(buf, 0, sizeof(buf));
+		/* Each byte e.g. D9 will write a string of two characters "D9". */
+		sprintf(buf, "%02x", buffer_pointer[i]);
+		s += buf;
+		byte_count_per_line++;
+
+		/*  Add a newline every 32 bytes. */
+		if (byte_count_per_line == 32) {
+			byte_count_per_line = 0;
+			json_object_array_add(mem_array_obj, json_object_new_string(s.c_str()));
+			s.clear();
+		} else if (!getenv("V4L2_TRACER_OPTION_COMPACT_PRINT")) {
+			/* Add a space every two bytes e.g. "012A 4001" */
+			s += " ";
+		}
+	}
+
+	/* Trace the last line if it was less than a full line. */
+	if (byte_count_per_line)
+		json_object_array_add(mem_array_obj, json_object_new_string(s.c_str()));
+
+	return mem_array_obj;
+}
+
+void trace_mem(int fd, __u32 offset, __u32 type, int index, __u32 bytesused, unsigned long start)
+{
+	json_object *mem_obj = json_object_new_object();
+	json_object_object_add(mem_obj, "mem_dump",
+	                       json_object_new_string(val2s(type, v4l2_buf_type_val_def).c_str()));
+	json_object_object_add(mem_obj, "fd", json_object_new_int(fd));
+	json_object_object_add(mem_obj, "offset", json_object_new_uint64(offset));
+	json_object_object_add(mem_obj, "index", json_object_new_int(index));
+	json_object_object_add(mem_obj, "bytesused", json_object_new_uint64(bytesused));
+	json_object_object_add(mem_obj, "address", json_object_new_uint64(start));
+
+	if ((type == V4L2_BUF_TYPE_VIDEO_OUTPUT || type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ||
+	    (getenv("V4L2_TRACER_OPTION_WRITE_DECODED_TO_JSON_FILE"))) {
+		json_object *mem_array_obj = trace_buffer((unsigned char*) start, bytesused);
+		json_object_object_add(mem_obj, "mem_array", mem_array_obj);
+	}
+
+	write_json_object_to_json_file(mem_obj);
+
+	json_object_put(mem_obj);
+}
+
+void trace_mem_encoded(int fd, __u32 offset)
+{
+	int index;
+	__u32 type;
+	__u32 bytesused;
+	unsigned long start = get_buffer_address_trace(fd, offset);
+
+	if (!start)
+		return;
+
+	bytesused = get_buffer_bytesused_trace(fd, offset);
+	type = get_buffer_type_trace(fd, offset);
+	index = get_buffer_index_trace(fd, offset);
+	trace_mem(fd, offset, type, index, bytesused, start);
+}
+
+void trace_mem_decoded(void)
+{
+	int displayed_count = 0;
+	unsigned expected_length = get_expected_length_trace();
+
+	while (!ctx_trace.decode_order.empty()) {
+		std::list<buffer_trace>::iterator it;
+		long next_frame_to_be_displayed = *std::min_element(ctx_trace.decode_order.begin(),
+		                                                    ctx_trace.decode_order.end());
+		for (it = ctx_trace.buffers.begin(); it != ctx_trace.buffers.end(); ++it) {
+			if (it->display_order != next_frame_to_be_displayed)
+				continue;
+			if (!it->address)
+				break;
+			if (it->bytesused < expected_length)
+				break;
+			if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "displaying: %ld, %s, index: %d\n",
+				        it->display_order, buftype2s(it->type).c_str(), it->index);
+			}
+			displayed_count++;
+
+			if (getenv("V4L2_TRACER_OPTION_WRITE_DECODED_TO_YUV_FILE")) {
+				std::string filename = getenv("TRACE_ID");
+				filename +=  ".yuv";
+				FILE *fp = fopen(filename.c_str(), "a");
+				unsigned char *buffer_pointer = (unsigned char*) it->address;
+				for (__u32 i = 0; i < expected_length; i++)
+					fwrite(&buffer_pointer[i], sizeof(unsigned char), 1, fp);
+				fclose(fp);
+			}
+			trace_mem(it->fd, it->offset, it->type, it->index, it->bytesused, it->address);
+			ctx_trace.decode_order.remove(next_frame_to_be_displayed);
+			it->display_order = -1;
+			break;
+		}
+		if (!it->address || it == ctx_trace.buffers.end() || it->bytesused < expected_length)
+			break;
+	}
+	ctx_trace.compressed_frame_count = ctx_trace.compressed_frame_count - displayed_count;
+}
+
+json_object *trace_v4l2_plane(struct v4l2_plane *p, __u32 memory)
+{
+	json_object *plane_obj = json_object_new_object();
+
+	json_object_object_add(plane_obj, "bytesused", json_object_new_int64(p->bytesused));
+	json_object_object_add(plane_obj, "length", json_object_new_int64(p->length));
+
+	json_object *m_obj = json_object_new_object();
+
+	if (memory == V4L2_MEMORY_MMAP)
+		json_object_object_add(m_obj, "mem_offset", json_object_new_int64(p->m.mem_offset));
+	json_object_object_add(plane_obj, "m", m_obj);
+
+	json_object_object_add(plane_obj, "data_offset", json_object_new_int64(p->data_offset));
+
+	return plane_obj;
+}
+
+void trace_v4l2_buffer(void *arg, json_object *ioctl_args)
+{
+	json_object *buf_obj = json_object_new_object();
+	struct v4l2_buffer *buf = static_cast<struct v4l2_buffer*>(arg);
+
+	json_object_object_add(buf_obj, "index", json_object_new_uint64(buf->index));
+	json_object_object_add(buf_obj, "type",
+	                       json_object_new_string(val2s(buf->type, v4l2_buf_type_val_def).c_str()));
+	json_object_object_add(buf_obj, "bytesused", json_object_new_uint64(buf->bytesused));
+	json_object_object_add(buf_obj, "flags", json_object_new_string(fl2s_buffer(buf->flags).c_str()));
+	json_object_object_add(buf_obj, "field",
+	                       json_object_new_string(val2s(buf->field, v4l2_field_val_def).c_str()));
+	json_object *timestamp_obj = json_object_new_object();
+	json_object_object_add(timestamp_obj, "tv_sec", json_object_new_int64(buf->timestamp.tv_sec));
+	json_object_object_add(timestamp_obj, "tv_usec",
+	                       json_object_new_int64(buf->timestamp.tv_usec));
+	json_object_object_add(buf_obj, "timestamp", timestamp_obj);
+	json_object_object_add(buf_obj, "timestamp_ns",
+	                       json_object_new_uint64(v4l2_timeval_to_ns(&buf->timestamp)));
+
+	json_object_object_add(buf_obj, "sequence", json_object_new_uint64(buf->sequence));
+	json_object_object_add(buf_obj, "memory",
+	                       json_object_new_string(val2s(buf->memory, v4l2_memory_val_def).c_str()));
+
+	json_object *m_obj = json_object_new_object();
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
+		json_object *planes_obj = json_object_new_array();
+		/* TODO add planes > 0 */
+		json_object_array_add(planes_obj, trace_v4l2_plane(buf->m.planes, buf->memory));
+		json_object_object_add(m_obj, "planes", planes_obj);
+	}
+
+	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+	    buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
+		if (buf->memory == V4L2_MEMORY_MMAP)
+			json_object_object_add(m_obj, "offset", json_object_new_uint64(buf->m.offset));
+	}
+	json_object_object_add(buf_obj, "m", m_obj);
+	json_object_object_add(buf_obj, "length", json_object_new_uint64(buf->length));
+
+	if (buf->flags & V4L2_BUF_FLAG_REQUEST_FD)
+		json_object_object_add(buf_obj, "request_fd", json_object_new_int(buf->request_fd));
+
+	json_object_object_add(ioctl_args, "v4l2_buffer", buf_obj);
+}
+
+void trace_vidioc_stream(void *arg, json_object *ioctl_args)
+{
+	v4l2_buf_type buf_type = *(static_cast<v4l2_buf_type*>(arg));
+	json_object_object_add(ioctl_args, "type",
+	                       json_object_new_string(val2s(buf_type, v4l2_buf_type_val_def).c_str()));
+}
+
+void trace_v4l2_streamparm(void *arg, json_object *ioctl_args)
+{
+	json_object *v4l2_streamparm_obj = json_object_new_object();
+	struct v4l2_streamparm *streamparm = static_cast<struct v4l2_streamparm*>(arg);
+
+	json_object_object_add(v4l2_streamparm_obj, "type",
+	                       json_object_new_string(val2s(streamparm->type, v4l2_buf_type_val_def).c_str()));
+
+	if ((streamparm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
+	    (streamparm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
+		trace_v4l2_captureparm_gen(&streamparm->parm, v4l2_streamparm_obj);
+
+	if ((streamparm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ||
+	    (streamparm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+		trace_v4l2_outputparm_gen(&streamparm->parm, v4l2_streamparm_obj);
+
+	json_object_object_add(ioctl_args, "v4l2_streamparm", v4l2_streamparm_obj);
+}
+
+void trace_v4l2_ext_control(void *arg, json_object *parent_obj, std::string key_name = "")
+{
+	json_object *v4l2_ext_control_obj = json_object_new_object();
+	struct v4l2_ext_control *p = static_cast<struct v4l2_ext_control*>(arg);
+
+	json_object_object_add(v4l2_ext_control_obj, "id",
+	                       json_object_new_string(val2s(p->id, control_val_def).c_str()));
+	json_object_object_add(v4l2_ext_control_obj, "size", json_object_new_uint64(p->size));
+
+	/* trace controls of type V4L2_CTRL_TYPE_MENU */
+	switch (p->id) {
+	case V4L2_CID_STATELESS_H264_DECODE_MODE: {
+		json_object_object_add(v4l2_ext_control_obj, "value",
+		                       json_object_new_string(val2s(p->value, v4l2_stateless_h264_decode_mode_val_def).c_str()));
+		json_object_array_add(parent_obj, v4l2_ext_control_obj);
+		return;
+	}
+	case V4L2_CID_STATELESS_H264_START_CODE: {
+		json_object_object_add(v4l2_ext_control_obj, "value",
+		                       json_object_new_string(val2s(p->value, v4l2_stateless_h264_start_code_val_def).c_str()));
+		json_object_array_add(parent_obj, v4l2_ext_control_obj);
+		return;
+	}
+	case V4L2_CID_STATELESS_HEVC_DECODE_MODE: {
+		json_object_object_add(v4l2_ext_control_obj, "value",
+		                       json_object_new_string(val2s(p->value, v4l2_stateless_hevc_decode_mode_val_def).c_str()));
+		json_object_array_add(parent_obj, v4l2_ext_control_obj);
+		return;
+	}
+	case V4L2_CID_STATELESS_HEVC_START_CODE: {
+		json_object_object_add(v4l2_ext_control_obj, "value",
+		                       json_object_new_string(val2s(p->value, v4l2_stateless_hevc_start_code_val_def).c_str()));
+		json_object_array_add(parent_obj, v4l2_ext_control_obj);
+		return;
+	}
+	default:
+		break;
+	}
+
+	if (p->ptr == nullptr) {
+		json_object_array_add(parent_obj, v4l2_ext_control_obj);
+		return;
+	}
+
+	switch (p->id) {
+	case V4L2_CID_STATELESS_VP8_FRAME:
+		trace_v4l2_ctrl_vp8_frame_gen(p->p_vp8_frame, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SPS:
+		trace_v4l2_ctrl_h264_sps_gen(p->p_h264_sps, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_PPS:
+		trace_v4l2_ctrl_h264_pps_gen(p->p_h264_pps, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SCALING_MATRIX:
+		trace_v4l2_ctrl_h264_scaling_matrix_gen(p->p_h264_scaling_matrix, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:
+		trace_v4l2_ctrl_h264_pred_weights_gen(p->p_h264_pred_weights, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_SLICE_PARAMS:
+		trace_v4l2_ctrl_h264_slice_params_gen(p->p_h264_slice_params, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
+		trace_v4l2_ctrl_h264_decode_params_gen(p->p_h264_decode_params, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_FWHT_PARAMS:
+		trace_v4l2_ctrl_fwht_params_gen(p->p_fwht_params, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_VP9_FRAME:
+		trace_v4l2_ctrl_vp9_frame_gen(p->p_vp9_frame, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_VP9_COMPRESSED_HDR:
+		trace_v4l2_ctrl_vp9_compressed_hdr_gen(p->p_vp9_compressed_hdr_probs, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SPS:
+		trace_v4l2_ctrl_hevc_sps_gen(p->p_hevc_sps, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_PPS:
+		trace_v4l2_ctrl_hevc_pps_gen(p->p_hevc_pps, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SLICE_PARAMS:
+		trace_v4l2_ctrl_hevc_slice_params_gen(p->p_hevc_slice_params, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_SCALING_MATRIX:
+		trace_v4l2_ctrl_hevc_scaling_matrix_gen(p->p_hevc_scaling_matrix, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_DECODE_PARAMS:
+		trace_v4l2_ctrl_hevc_decode_params_gen(p->p_hevc_decode_params, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS: {
+		/* V4L2_CTRL_TYPE_U32, V4L2_CTRL_FLAG_DYNAMIC_ARRAY */
+		__u32 elems = ctx_trace.elems;
+		json_object_object_add(v4l2_ext_control_obj, "elems", json_object_new_int64(elems));
+		json_object *hevc_entry_point_offsets_obj = json_object_new_array();
+		for (__u32 i = 0; i < elems; i++)
+			json_object_array_add(hevc_entry_point_offsets_obj, json_object_new_int64(p->p_u32[i]));
+		json_object_object_add(v4l2_ext_control_obj, "p_u32", hevc_entry_point_offsets_obj);
+		break;
+	}
+	case V4L2_CID_STATELESS_MPEG2_SEQUENCE:
+		trace_v4l2_ctrl_mpeg2_sequence_gen(p->p_mpeg2_sequence, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_MPEG2_PICTURE:
+		trace_v4l2_ctrl_mpeg2_picture_gen(p->p_mpeg2_picture, v4l2_ext_control_obj);
+		break;
+	case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
+		trace_v4l2_ctrl_mpeg2_quantisation_gen(p->p_mpeg2_quantisation, v4l2_ext_control_obj);
+		break;
+	default:
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
+		break;
+	}
+
+	json_object_array_add(parent_obj, v4l2_ext_control_obj);
+}
+
+void trace_v4l2_ext_controls(void *arg, json_object *ioctl_args)
+{
+	json_object *ext_controls_obj = json_object_new_object();
+	struct v4l2_ext_controls *ext_controls = static_cast<struct v4l2_ext_controls*>(arg);
+
+	json_object_object_add(ext_controls_obj, "which",
+	                       json_object_new_string(val2s(ext_controls->which, which_val_def).c_str()));
+
+	json_object_object_add(ext_controls_obj, "count", json_object_new_int64(ext_controls->count));
+
+	/* error_idx is defined only if the ioctl returned an error  */
+	if (errno)
+		json_object_object_add(ext_controls_obj, "error_idx",
+		                       json_object_new_uint64(ext_controls->error_idx));
+
+	/* request_fd is only valid when "which" == V4L2_CTRL_WHICH_REQUEST_VAL */
+	if (ext_controls->which == V4L2_CTRL_WHICH_REQUEST_VAL)
+		json_object_object_add(ext_controls_obj, "request_fd",
+		                       json_object_new_int(ext_controls->request_fd));
+
+	json_object *controls_obj = json_object_new_array();
+	for (__u32 i = 0; i < ext_controls->count; i++) {
+		if ((void *) ext_controls->controls == nullptr)
+			break;
+		trace_v4l2_ext_control((void *) &ext_controls->controls[i], controls_obj);
+	}
+	json_object_object_add(ext_controls_obj, "controls", controls_obj);
+
+	json_object_object_add(ioctl_args, "v4l2_ext_controls", ext_controls_obj);
+}
+
+void trace_v4l2_decoder_cmd(void *arg, json_object *ioctl_args)
+{
+	json_object *v4l2_decoder_cmd_obj = json_object_new_object();
+	struct v4l2_decoder_cmd *p = static_cast<struct v4l2_decoder_cmd*>(arg);
+
+	json_object_object_add(v4l2_decoder_cmd_obj, "cmd",
+	                       json_object_new_string(val2s(p->cmd, decoder_cmd_val_def).c_str()));
+
+	std::string flags;
+
+	switch (p->cmd) {
+	case V4L2_DEC_CMD_START: {
+		/*  This command has one flag: V4L2_DEC_CMD_START_MUTE_AUDIO. */
+		if (p->flags == V4L2_DEC_CMD_START_MUTE_AUDIO)
+			flags = "V4L2_DEC_CMD_START_MUTE_AUDIO";
+
+		/* struct start */
+		json_object *start_obj = json_object_new_object();
+		json_object_object_add(start_obj, "speed", json_object_new_int(p->start.speed));
+
+		std::string format;
+		/* possible values V4L2_DEC_START_FMT_NONE, V4L2_DEC_START_FMT_GOP */
+		if (p->start.format == V4L2_DEC_START_FMT_GOP)
+			format = "V4L2_DEC_START_FMT_GOP";
+		else if (p->start.format == V4L2_DEC_START_FMT_NONE)
+			format = "V4L2_DEC_START_FMT_NONE";
+		json_object_object_add(start_obj, "format", json_object_new_string(format.c_str()));
+
+		json_object_object_add(v4l2_decoder_cmd_obj, "start", start_obj);
+		break;
+	}
+	case V4L2_DEC_CMD_STOP: {
+		/*  This command has two flags */
+		if (p->flags == V4L2_DEC_CMD_STOP_TO_BLACK)
+			flags = "V4L2_DEC_CMD_STOP_TO_BLACK";
+		else if (p->flags == V4L2_DEC_CMD_STOP_IMMEDIATELY)
+			flags = "V4L2_DEC_CMD_STOP_IMMEDIATELY";
+
+		json_object *stop_obj = json_object_new_object();
+		json_object_object_add(stop_obj, "pts", json_object_new_uint64(p->stop.pts));
+
+		json_object_object_add(v4l2_decoder_cmd_obj, "stop", stop_obj);
+		break;
+	}
+
+	case V4L2_DEC_CMD_PAUSE: {
+		if (p->flags == V4L2_DEC_CMD_PAUSE_TO_BLACK)
+			flags = "V4L2_DEC_CMD_PAUSE_TO_BLACK";
+		break;
+	}
+	case V4L2_DEC_CMD_RESUME:
+	case V4L2_DEC_CMD_FLUSH:
+	default:
+		break;
+	}
+	json_object_object_add(v4l2_decoder_cmd_obj, "flags", json_object_new_string(flags.c_str()));
+
+	json_object_object_add(ioctl_args, "v4l2_decoder_cmd", v4l2_decoder_cmd_obj);
+}
+
+json_object *trace_ioctl_args(unsigned long cmd, void *arg)
+{
+	json_object *ioctl_args = json_object_new_object();
+
+	switch (cmd) {
+	case VIDIOC_QUERYCAP:
+		trace_v4l2_capability_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_ENUM_FMT:
+		trace_v4l2_fmtdesc_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_G_FMT:
+	case VIDIOC_TRY_FMT:
+	case VIDIOC_S_FMT:
+		trace_v4l2_format_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_REQBUFS:
+		trace_v4l2_requestbuffers_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_PREPARE_BUF:
+	case VIDIOC_QUERYBUF:
+	case VIDIOC_QBUF:
+	case VIDIOC_DQBUF:
+		trace_v4l2_buffer(arg, ioctl_args);
+		break;
+	case VIDIOC_EXPBUF:
+		trace_v4l2_exportbuffer_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_STREAMON:
+	case VIDIOC_STREAMOFF:
+		trace_vidioc_stream(arg, ioctl_args);
+		break;
+	case VIDIOC_G_PARM:
+	case VIDIOC_S_PARM:
+		trace_v4l2_streamparm(arg, ioctl_args);
+		break;
+	case VIDIOC_G_CTRL:
+	case VIDIOC_S_CTRL:
+		trace_v4l2_control_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_QUERYCTRL:
+		trace_v4l2_queryctrl_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_G_CROP:
+	case VIDIOC_S_CROP:
+		trace_v4l2_crop_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_G_EXT_CTRLS:
+	case VIDIOC_TRY_EXT_CTRLS:
+	case VIDIOC_S_EXT_CTRLS:
+		trace_v4l2_ext_controls(arg, ioctl_args);
+		break;
+	case VIDIOC_TRY_ENCODER_CMD:
+	case VIDIOC_ENCODER_CMD:
+		trace_v4l2_encoder_cmd_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_CREATE_BUFS:
+		trace_v4l2_create_buffers_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_G_SELECTION:
+	case VIDIOC_S_SELECTION:
+		trace_v4l2_selection_gen(arg, ioctl_args);
+		break;
+	case VIDIOC_TRY_DECODER_CMD:
+	case VIDIOC_DECODER_CMD:
+		trace_v4l2_decoder_cmd(arg, ioctl_args);
+		break;
+	case VIDIOC_QUERY_EXT_CTRL:
+		trace_v4l2_query_ext_ctrl_gen(arg, ioctl_args);
+		break;
+	case MEDIA_IOC_REQUEST_ALLOC: {
+		__s32 *request_fd = static_cast<__s32*>(arg);
+		json_object_object_add(ioctl_args, "request_fd", json_object_new_int(*request_fd));
+		break;
+	}
+	default:
+		break;
+	}
+
+	return ioctl_args;
+}
diff --git a/utils/v4l2-tracer/trace.h b/utils/v4l2-tracer/trace.h
new file mode 100644
index 00000000..7fa0a22a
--- /dev/null
+++ b/utils/v4l2-tracer/trace.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef TRACE_H
+#define TRACE_H
+
+#include "v4l2-tracer-common.h"
+#include "trace-gen.h"
+
+struct buffer_trace {
+	int fd;
+	__u32 type;
+	__u32 index;
+	__u32 offset;
+	__u32 bytesused;
+	long display_order;
+	unsigned long address;
+};
+
+struct trace_context {
+	__u32 elems;
+	__u32 width;
+	__u32 height;
+	FILE *trace_file;
+	__u32 pixelformat;
+	int pic_order_cnt_lsb;
+	int max_pic_order_cnt_lsb;
+	pthread_mutex_t lock;
+	std::string media_device;
+	__u32 compression_format;
+	std::string trace_filename;
+	int compressed_frame_count;
+	std::list<long> decode_order;
+	std::list<struct buffer_trace> buffers;
+	std::unordered_map<int, std::string> devices; /* key:fd, value: path of the device */
+};
+
+void trace_open(int fd, const char *path, int oflag, mode_t mode, bool is_open64);
+void trace_mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off, unsigned long buf_address, bool is_mmap64);
+void trace_mem(int fd, __u32 offset, __u32 type, int index, __u32 bytesused, unsigned long start);
+void trace_mem_encoded(int fd, __u32 offset);
+void trace_mem_decoded(void);
+json_object *trace_ioctl_args(unsigned long cmd, void *arg);
+
+bool is_video_or_media_device(const char *path);
+void add_device(int fd, std::string path);
+std::string get_device(int fd);
+void print_devices(void);
+
+bool buffer_in_trace_context(int fd, __u32 offset = 0);
+__u32 get_buffer_type_trace(int fd, __u32 offset = 0);
+int get_buffer_index_trace(int fd, __u32 offset);
+long get_buffer_bytesused_trace(int fd, __u32 offset);
+void set_buffer_address_trace(int fd, __u32 offset, unsigned long address);
+unsigned long get_buffer_address_trace(int fd, __u32 offset);
+bool buffer_is_mapped(unsigned long buffer_address);
+
+unsigned get_expected_length_trace(void);
+void s_ext_ctrls_setup(struct v4l2_ext_controls *ext_controls);
+void qbuf_setup(struct v4l2_buffer *buf);
+void streamoff_cleanup(v4l2_buf_type buf_type);
+void g_fmt_setup_trace(struct v4l2_format *format);
+void s_fmt_setup(struct v4l2_format *format);
+void expbuf_setup(struct v4l2_exportbuffer *export_buffer);
+void querybuf_setup(int fd, struct v4l2_buffer *buf);
+void query_ext_ctrl_setup(int fd, struct v4l2_query_ext_ctrl *p);
+
+void write_json_object_to_json_file(json_object *jobj);
+void close_json_file(void);
+
+#endif
diff --git a/utils/v4l2-tracer/v4l2-tracer-common.cpp b/utils/v4l2-tracer/v4l2-tracer-common.cpp
new file mode 100644
index 00000000..4d6adcd3
--- /dev/null
+++ b/utils/v4l2-tracer/v4l2-tracer-common.cpp
@@ -0,0 +1,453 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include "v4l2-tracer-common.h"
+
+void print_v4l2_tracer_info(void)
+{
+	fprintf(stderr, "v4l2-tracer %s%s\n", PACKAGE_VERSION, STRING(GIT_COMMIT_CNT));
+	if (strlen(STRING(GIT_SHA)))
+		fprintf(stderr, "v4l2-tracer SHA: '%s' %s\n", STRING(GIT_SHA), STRING(GIT_COMMIT_DATE));
+}
+
+void print_usage(void)
+{
+	print_v4l2_tracer_info();
+	fprintf(stderr, "Usage:\n\tv4l2-tracer [options] trace <tracee>\n"
+	        "\tv4l2-tracer [options] retrace <trace_file>.json\n"
+	        "\tv4l2-tracer clean <trace_file>.json\n\n"
+
+	        "\tCommon options:\n"
+	        "\t\t-c, --compact     Write minimal whitespace in JSON file.\n"
+	        "\t\t-g, --debug       Turn on verbose reporting plus additional debug info.\n"
+	        "\t\t-h, --help        Display this message.\n"
+	        "\t\t-r  --raw         Write decoded video frame data to JSON file.\n"
+	        "\t\t-v, --verbose     Turn on verbose reporting.\n"
+	        "\t\t-y, --yuv         Write decoded video frame data to yuv file.\n\n"
+
+	        "\tRetrace options:\n"
+	        "\t\t-d, --video_device <dev>   Retrace with a specific video device.\n"
+	        "\t\t                           <dev> must be a digit corresponding to\n"
+	        "\t\t                           /dev/video<dev> \n\n"
+	        "\t\t-m, --media_device <dev>   Retrace with a specific media device.\n"
+	        "\t\t                           <dev> must be a digit corresponding to\n"
+	        "\t\t                           /dev/media<dev> \n\n");
+}
+
+void add_separator(std::string &s)
+{
+	if (!s.empty())
+		s += "|";
+}
+
+void clean_string(size_t idx, std::string substring_to_erase, std::string &s)
+{
+	std::string temp = substring_to_erase + '|';
+	if (s.find(temp) != s.npos)
+		s.erase(idx, temp.length());
+	else
+		s.erase(idx, substring_to_erase.length());
+}
+
+std::string ver2s(unsigned int version)
+{
+	char buf[16];
+	sprintf(buf, "%d.%d.%d", version >> 16, (version >> 8) & 0xff, version & 0xff);
+	return buf;
+}
+
+/* Convert a number to an octal string. If num is 0, return an empty string. */
+std::string number2s_oct(long num)
+{
+	std::stringstream ss;
+	ss << std::setfill ('0') << std::setw(5) << std::oct << num;
+	return ss.str();
+}
+
+/* Convert a number to a hex string. If num is 0, return an empty string. */
+std::string number2s(long num)
+{
+	if (!num)
+		return "";
+	std::stringstream ss;
+	ss << std::hex << num;
+	return "0x" + ss.str();
+}
+
+std::string val2s(long val, const val_def *def)
+{
+	std::string s;
+	if (def == nullptr)
+		return number2s(val);
+
+	while ((def->val != -1) && (def->val != val))
+		def++;
+
+	if (def->val == val)
+		return def->str;
+
+	return number2s(val);
+}
+
+std::string fl2s(unsigned val, const flag_def *def)
+{
+	std::string s;
+
+	if (def == nullptr)
+		return number2s(val);
+
+	while (def->flag) {
+		if (val & def->flag) {
+			add_separator(s);
+			s += def->str;
+			val &= ~def->flag;
+		}
+		def++;
+	}
+	if (val) {
+		add_separator(s);
+		s += number2s(val);
+	}
+
+	return s;
+}
+
+std::string fl2s_buffer(__u32 flags)
+{
+	std::string s;
+
+	switch (flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) {
+	case V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN:
+		s += "V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN";
+		flags &= ~V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN;
+		break;
+	case V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC:
+		s += "V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC";
+		flags &= ~V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+		break;
+	case V4L2_BUF_FLAG_TIMESTAMP_COPY:
+		s += "V4L2_BUF_FLAG_TIMESTAMP_COPY";
+		flags &= ~V4L2_BUF_FLAG_TIMESTAMP_COPY;
+		break;
+	default:
+		break;
+	}
+
+	/* Since V4L2_BUF_FLAG_TSTAMP_SRC_EOF == 0, at least this flag will always be added. */
+	add_separator(s);
+	switch (flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK) {
+	case V4L2_BUF_FLAG_TSTAMP_SRC_EOF:
+		s += "V4L2_BUF_FLAG_TSTAMP_SRC_EOF";
+		flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
+		break;
+	case V4L2_BUF_FLAG_TSTAMP_SRC_SOE:
+		s += "V4L2_BUF_FLAG_TSTAMP_SRC_SOE";
+		flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
+		break;
+	default:
+		break;
+	}
+
+	if (flags) {
+		add_separator(s);
+		const unsigned ts_mask = V4L2_BUF_FLAG_TIMESTAMP_MASK | V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
+		s += fl2s(flags & ~ts_mask, v4l2_buf_flag_def);
+	}
+
+	return s;
+}
+
+std::string fl2s_fwht(__u32 flags)
+{
+	std::string s;
+	switch (flags & V4L2_FWHT_FL_PIXENC_MSK) {
+	case V4L2_FWHT_FL_PIXENC_YUV:
+		s += "V4L2_FWHT_FL_PIXENC_YUV";
+		flags &= ~V4L2_FWHT_FL_PIXENC_YUV;
+		break;
+	case V4L2_FWHT_FL_PIXENC_RGB:
+		s += "V4L2_FWHT_FL_PIXENC_RGB";
+		flags &= ~V4L2_FWHT_FL_PIXENC_RGB;
+		break;
+	case V4L2_FWHT_FL_PIXENC_HSV:
+		s += "V4L2_FWHT_FL_PIXENC_HSV";
+		flags &= ~V4L2_FWHT_FL_PIXENC_HSV;
+		break;
+	default:
+		break;
+	}
+	add_separator(s);
+	s += fl2s(flags, v4l2_ctrl_fwht_params_flag_def);
+	return s;
+}
+
+long s2number(std::string s)
+{
+	long num = 0;
+	if (s.empty())
+		return num;
+	try {
+		num = std::stoul(s, nullptr, 0); /* base is auto-detected */
+	} catch (std::invalid_argument& ia) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "string \'%s\' is invalid\n", s.c_str());
+	} catch (std::out_of_range& oor) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "string \'%s\' is out of range\n", s.c_str());
+	}
+	return num;
+}
+
+long s2val(std::string s, const val_def *def)
+{
+	if (s.empty())
+		return 0;
+
+	if (def == nullptr)
+		return s2number(s);
+
+	while ((def->val != -1) && (def->str != s))
+		def++;
+
+	if (def->str == s)
+		return def->val;
+
+	return s2number(s);
+}
+
+unsigned long s2flags(std::string s, const flag_def *def)
+{
+	size_t idx;
+	unsigned long flags = 0;
+
+	if (def == nullptr)
+		return s2number(s);
+
+	while (def->flag) {
+		idx = s.find(def->str);
+		if (idx == s.npos) {
+			def++;
+			continue;
+		}
+		/* Stop false substring matches e.g. in V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS */
+		std::string check = def->str;
+		if (check.length() != s.length()) {
+			idx = s.find(check + '|');
+			if (idx == s.npos) {
+				def++;
+				continue;
+			}
+		}
+		flags += def->flag;
+		clean_string(idx, def->str, s);
+		def++;
+	}
+	if (!s.empty())
+		flags += s2number(s);
+
+	return flags;
+}
+
+unsigned long s2flags_buffer(std::string s)
+{
+	size_t idx = 0;
+	unsigned long flags = 0;
+
+	idx = s.find("V4L2_BUF_FLAG_TIMESTAMP_COPY");
+	if (idx != s.npos) {
+		flags += V4L2_BUF_FLAG_TIMESTAMP_COPY;
+		clean_string(idx, "V4L2_BUF_FLAG_TIMESTAMP_COPY", s);
+	}
+	idx = s.find("V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC");
+	if (idx != s.npos) {
+		flags += V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+		clean_string(idx, "V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC", s);
+	}
+	idx = s.find("V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN");
+	if (idx != s.npos) {
+		flags += V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN;
+		clean_string(idx, "V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN", s);
+	}
+	idx = s.find("V4L2_BUF_FLAG_TSTAMP_SRC_SOE");
+	if (idx != s.npos) {
+		flags += V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
+		clean_string(idx, "V4L2_BUF_FLAG_TSTAMP_SRC_SOE", s);
+	}
+	idx = s.find("V4L2_BUF_FLAG_TSTAMP_SRC_EOF");
+	if (idx != s.npos) {
+		flags += V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
+		clean_string(idx, "V4L2_BUF_FLAG_TSTAMP_SRC_EOF", s);
+	}
+	if (!s.empty())
+		flags += s2flags(s, v4l2_buf_flag_def);
+	return flags;
+}
+
+unsigned long s2flags_fwht(std::string s)
+{
+	size_t idx = 0;
+	unsigned long flags = 0;
+	idx = s.find("V4L2_FWHT_FL_PIXENC_YUV");
+	if (idx != s.npos) {
+		flags += V4L2_FWHT_FL_PIXENC_YUV;
+		clean_string(idx, "V4L2_FWHT_FL_PIXENC_YUV", s);
+	}
+	idx = s.find("V4L2_FWHT_FL_PIXENC_RGB");
+	if (idx != s.npos) {
+		flags += V4L2_FWHT_FL_PIXENC_RGB;
+		clean_string(idx, "V4L2_FWHT_FL_PIXENC_RGB", s);
+	}
+	idx = s.find("V4L2_FWHT_FL_PIXENC_HSV");
+	if (idx != s.npos) {
+		flags += V4L2_FWHT_FL_PIXENC_HSV;
+		clean_string(idx, "V4L2_FWHT_FL_PIXENC_HSV", s);
+	}
+	if (!s.empty())
+		flags += s2flags(s, v4l2_ctrl_fwht_params_flag_def);
+	return flags;
+}
+
+std::string get_path_media(std::string driver)
+{
+	struct dirent *ep;
+	std::string path_media;
+	DIR *dp = opendir("/dev");
+	if (dp == nullptr)
+		return path_media;
+
+	while ((ep = readdir(dp))) {
+		const char *name = ep->d_name;
+		if (memcmp(name, "media", 5) || !isdigit(name[5]))
+			continue;
+
+		std::string media_devname = std::string("/dev/") + name;
+		setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+		int media_fd = open(media_devname.c_str(), O_RDONLY);
+		unsetenv("V4L2_TRACER_PAUSE_TRACE");
+		if (media_fd < 0)
+			continue;
+
+		struct media_device_info info = {};
+		if (ioctl(media_fd, MEDIA_IOC_DEVICE_INFO, &info) || info.driver != driver) {
+			setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+			close(media_fd);
+			unsetenv("V4L2_TRACER_PAUSE_TRACE");
+			continue;
+		} else {
+			path_media = media_devname;
+			setenv("V4L2_TRACER_PAUSE_TRACE", "true", 0);
+			close(media_fd);
+			unsetenv("V4L2_TRACER_PAUSE_TRACE");
+			break;
+		}
+	}
+	closedir(dp);
+	return path_media;
+}
+
+std::string get_path_video(int media_fd, std::list<std::string> linked_entities)
+{
+	int err = 0;
+	std::string path_video;
+	struct media_v2_topology topology = {};
+
+	err = ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
+	if (err < 0)
+		return path_video;
+
+	auto ifaces = new media_v2_interface[topology.num_interfaces];
+	topology.ptr_interfaces = (uintptr_t)ifaces;
+	auto links = new media_v2_link[topology.num_links];
+	topology.ptr_links = (uintptr_t)links;
+	auto ents = new media_v2_entity[topology.num_entities];
+	topology.ptr_entities = (uintptr_t)ents;
+
+	err = ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
+	if (err < 0) {
+		delete [] ifaces;
+		delete [] links;
+		delete [] ents;
+		return path_video;
+	}
+
+	for (auto &name : linked_entities) {
+		/* Find an entity listed in the video device's linked_entities. */
+		for (__u32 i = 0; i < topology.num_entities; i++) {
+			if (ents[i].name != name)
+				continue;
+			/* Find the first link connected to that entity. */
+			for (__u32 j = 0; j < topology.num_links; j++) {
+				if (links[j].sink_id != ents[i].id)
+					continue;
+				/* Find the interface connected to that link. */
+				for (__u32 k = 0; k < topology.num_interfaces; k++) {
+					if (ifaces[k].id != links[j].source_id)
+						continue;
+					std::string video_devname = mi_media_get_device(ifaces[k].devnode.major,
+					                                                ifaces[k].devnode.minor);
+					if (!video_devname.empty()) {
+						path_video = video_devname;
+						break;
+					}
+				}
+			}
+		}
+	}
+	return path_video;
+}
+
+std::list<std::string> get_linked_entities(int media_fd, std::string path_video)
+{
+	int err = 0;
+	std::list<std::string> linked_entities;
+	struct media_v2_topology topology = {};
+
+	err = ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
+	if (err < 0)
+		return linked_entities;
+
+	auto ifaces = new media_v2_interface[topology.num_interfaces];
+	topology.ptr_interfaces = (uintptr_t)ifaces;
+	auto links = new media_v2_link[topology.num_links];
+	topology.ptr_links = (uintptr_t)links;
+	auto ents = new media_v2_entity[topology.num_entities];
+	topology.ptr_entities = (uintptr_t)ents;
+
+	err = ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
+	if (err < 0) {
+		delete [] ifaces;
+		delete [] links;
+		delete [] ents;
+		return linked_entities;
+	}
+
+	/* find the interface corresponding to the path_video */
+	for (__u32 i = 0; i < topology.num_interfaces; i++) {
+		if (path_video != mi_media_get_device(ifaces[i].devnode.major, ifaces[i].devnode.minor))
+			continue;
+		/* find the links from that interface */
+		for (__u32 j = 0; j < topology.num_links; j++) {
+			if (links[j].source_id != ifaces[i].id)
+				continue;
+			/* find the entities connected by that link to the interface */
+			for (__u32 k = 0; k < topology.num_entities; k++) {
+				if (ents[k].id != links[j].sink_id)
+					continue;
+				linked_entities.push_back(ents[k].name);
+			}
+		}
+		if (linked_entities.size())
+			break;
+	}
+
+	delete [] ents;
+	delete [] links;
+	delete [] ifaces;
+
+	return linked_entities;
+}
diff --git a/utils/v4l2-tracer/v4l2-tracer-common.h b/utils/v4l2-tracer/v4l2-tracer-common.h
new file mode 100644
index 00000000..45544062
--- /dev/null
+++ b/utils/v4l2-tracer/v4l2-tracer-common.h
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#ifndef V4L2_TRACER_COMMON_H
+#define V4L2_TRACER_COMMON_H
+
+#include <json.h>
+#include <poll.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <pthread.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <linux/media.h>
+#include <linux/videodev2.h>
+#include <list>
+#include <vector>
+#include <algorithm>
+#include <stdexcept>
+#include <unordered_map>
+#include "v4l2-info.h"
+#include "codec-fwht.h"
+#include "media-info.h"
+#include "config.h"
+
+#define STR(x) #x
+#define STRING(x) STR(x)
+
+struct val_def {
+	long val;
+	const char *str;
+};
+
+void print_v4l2_tracer_info(void);
+void print_usage(void);
+
+std::string ver2s(unsigned int version);
+std::string number2s_oct(long num);
+std::string number2s(long num);
+std::string val2s(long val, const val_def *def);
+std::string fl2s(unsigned val, const flag_def *def);
+std::string fl2s_buffer(__u32 flags);
+std::string fl2s_fwht(__u32 flags);
+
+long s2number(std::string s);
+long s2val(std::string s, const val_def *def);
+long s2ioctl(std::string s);
+unsigned long s2flags(std::string s, const flag_def *def);
+unsigned long s2flags_buffer(std::string s);
+unsigned long s2flags_fwht(std::string s);
+
+std::string which2s(unsigned long which);
+
+std::string get_path_media(std::string driver);
+std::string get_path_video(int media_fd, std::list<std::string> linked_entities);
+std::list<std::string> get_linked_entities(int media_fd, std::string path_video);
+
+#include "v4l2-tracer-info-gen.h"
+
+constexpr val_def which_val_def[] = {
+	{ V4L2_CTRL_WHICH_CUR_VAL,	"V4L2_CTRL_WHICH_CUR_VAL" },
+	{ V4L2_CTRL_WHICH_DEF_VAL,	"V4L2_CTRL_WHICH_DEF_VAL" },
+	{ V4L2_CTRL_WHICH_REQUEST_VAL,	"V4L2_CTRL_WHICH_REQUEST_VAL" },
+	{ -1, "" }
+};
+
+constexpr val_def open_val_def[] = {
+	{ O_RDONLY,	"O_RDONLY" },
+	{ O_WRONLY,	"O_WRONLY" },
+	{ O_RDWR,	"O_RDWR" },
+	{ -1, "" }
+};
+
+#endif
diff --git a/utils/v4l2-tracer/v4l2-tracer-gen.pl b/utils/v4l2-tracer/v4l2-tracer-gen.pl
new file mode 100755
index 00000000..595e5b04
--- /dev/null
+++ b/utils/v4l2-tracer/v4l2-tracer-gen.pl
@@ -0,0 +1,840 @@
+#!/usr/bin/perl
+# SPDX-License-Identifier: GPL-2.0-only */
+# Copyright 2022 Collabora Ltd.
+
+sub convert_type_to_json_type {
+	my $type = shift;
+	if ($type eq __u8 || $type eq char || $type eq __u16 || $type eq __s8 || $type eq __s16 || $type eq __s32 || $type eq 'int') {
+		return "int";
+	}
+	if ($type eq __u32 || $type eq __le32 || $type eq __s64) {
+		return "int64";
+	}
+
+	# unsigned appears just twice in videodev2.h and in both cases it is 'unsigned long'
+	if ($type eq __u64 || $type eq 'v4l2_std_id' || $type eq 'unsigned') {
+		return "uint64";
+	}
+	if ($type eq struct || $type eq union || $type eq void) {
+		return;
+	}
+	print "v4l2_tracer: error: couldn't convert \'$type\' to json_object type.\n";
+	return;
+}
+
+sub get_index_letter {
+	my $index = shift;
+	if ($index eq 0) {return "i";}
+	if ($index eq 1) {return "j";}
+	if ($index eq 2) {return "k";}
+	if ($index eq 3) {return "l";}
+	if ($index eq 4) {return "m";}
+	if ($index eq 5) {return "n";}
+	if ($index eq 6) {return "o";} # "p" is saved for pointer
+	if ($index eq 8) {return "q";}
+	return "z";
+}
+
+$flag_func_name;
+
+sub flag_gen {
+	my $flag_type = shift;
+
+	if ($flag_type =~ /fwht/) {
+		$flag_func_name = v4l2_ctrl_fwht_params_;
+	} elsif ($flag_type =~ /vp8_loop_filter/) {
+		$flag_func_name = v4l2_vp8_loop_filter_;
+	} else {
+		($flag_func_name) = ($_) =~ /#define (\w+_)FL.+/;
+		$flag_func_name = lc $flag_func_name;
+	}
+
+	printf $fh_common_info_h "constexpr flag_def %sflag_def[] = {\n", $flag_func_name;
+
+	($flag) = ($_) =~ /#define\s+(\w+)\s+.+/;
+	printf $fh_common_info_h "\t{ $flag, \"$flag\" },\n"; # get the first flag
+
+	while (<>) {
+		next if ($_ =~ /^\/?\s?\*.*/); # skip comments between flags if any
+		next if $_ =~ /^\s*$/; # skip blank lines between flags if any
+		last if ((grep {!/^#define\s+\w+_FL/} $_) && (grep {!/^#define V4L2_VP8_LF/} $_));
+		($flag) = ($_) =~ /#\s*define\s+(\w+)\s+.+/;
+
+		# don't include flags that are masks
+		next if ($flag_func_name eq v4l2_buf_) && ($flag =~ /.*TIMESTAMP.*/ || $flag =~ /.*TSTAMP.*/);
+		next if ($flag_func_name eq v4l2_ctrl_fwht_params_) && ($flag =~ /.*COMPONENTS.*/ || $flag =~ /.*PIXENC.*/);
+		next if ($flag =~ /.*MEDIA_LNK_FL_LINK_TYPE.*/);
+		next if ($flag =~ /.*MEDIA_ENT_ID_FLAG_NEXT.*/);
+
+		printf $fh_common_info_h "\t{ $flag, \"$flag\" },\n";
+	}
+	printf $fh_common_info_h "\t{ 0, \"\" }\n};\n\n";
+}
+
+sub enum_gen {
+	($enum_name) = ($_) =~ /enum (\w+) {/;
+	printf $fh_common_info_h "constexpr val_def %s_val_def[] = {\n", $enum_name;
+	while (<>) {
+		last if $_ =~ /};/;
+		($name) = ($_) =~ /\s+(\w+)\s?.*/;
+		next if ($name ne uc $name); # skip comments that don't start with *
+		next if ($_ =~ /^\s*\/?\s?\*.*/); # skip comments
+		next if $name =~ /^\s*$/;  # skip blank lines
+		printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $name, $name;
+	}
+	printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+}
+
+sub clean_up_line {
+	my $line = shift;
+	chomp($line);
+	$line =~ s/^\s+//; # remove leading whitespace
+	$line =~ s/.*\# define.*//; # zero out line if it has defines inside a structure (e.g. v4l2_jpegcompression)
+	$line =~ s/^\s*\/?\s?\*.*//; # zero out line if it has comments where the line starts with start with /* / * or just *
+	$line =~ s/\s*\/\*.*//; # remove comments at the end of a line following a member
+	$line =~ s/\*\/$//; # zero out line if it has comments that begin without any slashs or asterisks but end with */
+	# zero out lines that don't have a ; or { because they are comments but without any identifying slashes or asteriks
+	if ($line !~ /.*[\;|\{].*/) {
+		$line =~ s/.*//;
+	}
+	$line =~ s/.*reserved.*//; # zero out lines with reserved members, they will segfault on retracing
+	$line =~ s/.*raw_data.*//;
+	# don't remove semi-colon at end because some functions will look for it
+	return $line;
+}
+
+sub get_val_def_name {
+	my @params = @_;
+	my $member = @params[0];
+	my $struct_name = @params[1];
+
+	$val_def_name = "";
+	if ($member =~ /type/) {
+		if ($struct_name =~ /.*fmt|format|buf|parm|crop|selection|vbi.*/) {
+			$val_def_name = "v4l2_buf_type_val_def";
+		} elsif ($struct_name =~ /ctrl$/) {
+			$val_def_name = "v4l2_ctrl_type_val_def";
+		} else {
+			$val_def_name = "nullptr"; # will print as hex string
+		}
+	}
+	if ($member =~ /pixelformat/) {
+		$val_def_name = "v4l2_pix_fmt_val_def";
+	}
+	if ($member =~ /cmd/) {
+		if ($struct_name =~ /v4l2_decoder_cmd/) {
+			$val_def_name = "decoder_cmd_val_def";
+		}
+		if ($struct_name =~ /v4l2_encoder_cmd/) {
+			$val_def_name = "encoder_cmd_val_def";
+		}
+	}
+	if ($member =~ /memory/) {
+		$val_def_name = "v4l2_memory_val_def";
+	}
+	if ($member =~ /field/) {
+		if ($struct_name =~ /.*pix|buffer.*/) {
+			$val_def_name = "v4l2_field_val_def";
+		} else {
+			$val_def_name = "nullptr"; # will print as hex string
+		}
+	}
+	if ($member =~ /^id$/) {
+		if ($struct_name =~ /.*control|query.*/) {
+			$val_def_name = "control_val_def";
+		} else {
+			$val_def_name = "nullptr"; # will print as hex string
+		}
+	}
+	if ($member =~ /capability|outputmode|capturemode/) {
+		if ($struct_name =~ /.*v4l2_captureparm|v4l2_outputparm.*/) {
+		$val_def_name = "streamparm_val_def";
+		}
+	}
+	if ($member =~ /colorspace/) {
+		$val_def_name = "v4l2_colorspace_val_def";
+	}
+	if ($member =~ /ycbcr_enc/) {
+		$val_def_name = "v4l2_ycbcr_encoding_val_def";
+	}
+	if ($member =~ /quantization/) {
+		$val_def_name = "v4l2_quantization_val_def";
+	}
+	if ($member =~ /xfer_func/) {
+		$val_def_name = "v4l2_xfer_func_val_def";
+	}
+
+	return $val_def_name;
+}
+
+sub get_flag_def_name {
+	my @params = @_;
+	my $member = @params[0];
+	my $struct_name = @params[1];
+
+	$flag_def_name = "";
+	if ($member =~ /flags/) {
+		if ($struct_name =~ /buffers$/) {
+			$flag_def_name = "v4l2_memory_flag_def";
+		} elsif ($struct_name =~ /.*pix_format.*/) {
+			$flag_def_name = "v4l2_pix_fmt_flag_def";
+		} elsif ($struct_name =~ /.*ctrl$/) {
+			$flag_def_name = "v4l2_ctrl_flag_def";
+		} elsif ($struct_name =~ /.*fmtdesc$/) {
+			$flag_def_name = "v4l2_fmt_flag_def";
+		} elsif ($struct_name =~ /.*selection$/) {
+			$flag_def_name = "v4l2_sel_flag_def";
+		} else {
+			$flag_def_name = "nullptr"
+		}
+	}
+
+	if ($member =~ /.*cap.*/) {
+		# v4l2_requestbuffers, v4l2_create_buffers
+		if ($struct_name =~ /buffers$/) {
+			$flag_def_name = "v4l2_buf_cap_flag_def";
+		}
+		# v4l2_capability
+		if ($struct_name =~ /capability$/) {
+			$flag_def_name = "v4l2_cap_flag_def";
+		}
+	}
+
+	return $flag_def_name;
+}
+
+# trace a struct nested in another struct in videodev2.h
+sub handle_struct {
+	printf $fh_trace_cpp "\t//$line\n";
+	printf $fh_retrace_cpp "\t//$line\n";
+
+	# this is a multi-lined nested struct so iterate through it
+	if ($line !~ /.*;$/) {
+		$suppress_struct = true;
+		return;
+	}
+
+	# don't trace struct pointers
+	if ($line =~ /\*/) {
+		return;
+	}
+	# don't trace struct arrays
+	if ($line =~ /\[/) {
+		return;
+	}
+
+	my ($struct_tag) = ($line) =~ /\s*struct (\w+)\s+.*/;
+
+	# structs defined outside of videodev2.h
+	if ($struct_tag =~ /v4l2_ctrl|timeval|timespec/) {
+		return;
+	}
+
+	my ($struct_var) = ($line) =~ /(\w+)\;$/;
+	printf $fh_trace_cpp "\ttrace_%s_gen(&p->%s, %s_obj, \"%s\");\n", $struct_tag, $struct_var, $struct_name, $struct_var;
+	printf $fh_retrace_cpp "\tp->$struct_var = *retrace_%s_gen(%s_obj, \"%s\");\n\n", $struct_tag, $struct_name, $struct_var;
+}
+
+# trace a union in videodev2.h
+sub handle_union {
+	my @params = @_;
+	my $struct_name = @params[0];
+
+	$in_union = true;
+	$suppress_union = true;
+	printf $fh_trace_cpp "\t//union\n";
+	printf $fh_retrace_cpp "\t//union\n";
+
+	if ($struct_name =~ /^v4l2_pix_format/) {
+		$suppress_union = false;
+	}
+
+	if ($struct_name =~ /^v4l2_format$/) {
+		printf $fh_trace_cpp "\tswitch (p->type) {\n";
+		printf $fh_trace_cpp "\tcase V4L2_BUF_TYPE_VIDEO_CAPTURE:\n\tcase V4L2_BUF_TYPE_VIDEO_OUTPUT:\n";
+		printf $fh_trace_cpp "\t\ttrace_v4l2_pix_format_gen(&p->fmt.pix, %s_obj);\n\t\tbreak;\n", $struct_name;
+		printf $fh_trace_cpp "\tcase V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:\n\tcase V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:\n";
+		printf $fh_trace_cpp "\t\ttrace_v4l2_pix_format_mplane_gen(&p->fmt.pix, %s_obj);\n\t\tbreak;\n", $struct_name;
+		printf $fh_trace_cpp "\tdefault:\n\t\tbreak;\n\t}\n";
+
+		printf $fh_retrace_cpp "\tswitch (p->type) {\n";
+		printf $fh_retrace_cpp "\tcase V4L2_BUF_TYPE_VIDEO_CAPTURE:\n\tcase V4L2_BUF_TYPE_VIDEO_OUTPUT:\n";
+		printf $fh_retrace_cpp "\t\tp->fmt.pix = *retrace_v4l2_pix_format_gen(%s_obj);\n\t\tbreak;\n", $struct_name;
+		printf $fh_retrace_cpp "\tcase V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:\n\tcase V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:\n";
+		printf $fh_retrace_cpp "\t\tp->fmt.pix_mp = *retrace_v4l2_pix_format_mplane_gen(%s_obj);\n\t\tbreak;\n", $struct_name;
+		printf $fh_retrace_cpp "\tdefault:\n\t\tbreak;\n\t}\n";
+	}
+
+	return $suppress_union;
+}
+
+# generate functions for structs in videodev2.h
+sub struct_gen {
+	($struct_name) = ($_) =~ /struct (\w+) {/;
+
+	# it's not being used and was generating a warning
+	if ($struct_name =~ /v4l2_mpeg_vbi_ITV0/) {
+		return;
+	}
+	printf $fh_trace_cpp "void trace_%s_gen(void *arg, json_object *parent_obj, std::string key_name = \"\")\n{\n", $struct_name;
+	printf $fh_trace_h "void trace_%s_gen(void *arg, json_object *parent_obj, std::string key_name = \"\");\n", $struct_name;
+	printf $fh_trace_cpp "\tjson_object *%s_obj = json_object_new_object();\n", $struct_name;
+	printf $fh_trace_cpp "\tstruct %s *p = static_cast<struct %s*>(arg);\n\n", $struct_name, $struct_name;
+
+	printf $fh_retrace_h "struct %s *retrace_%s_gen(json_object *parent_obj, std::string key_name = \"\");\n", $struct_name, $struct_name;
+	printf $fh_retrace_cpp "struct %s *retrace_%s_gen(json_object *parent_obj, std::string key_name = \"\")\n{\n", $struct_name, $struct_name;
+
+	printf $fh_retrace_cpp "\tstruct %s *p = (struct %s *) calloc(1, sizeof(%s));\n\n", $struct_name, $struct_name, $struct_name;
+	printf $fh_retrace_cpp "\tjson_object *%s_obj;\n", $struct_name;
+	printf $fh_retrace_cpp "\tif (key_name.empty())\n";
+	printf $fh_retrace_cpp "\t\tjson_object_object_get_ex(parent_obj, \"%s\", &%s_obj);\n", $struct_name, $struct_name;
+	printf $fh_retrace_cpp "\telse\n";
+	printf $fh_retrace_cpp "\t\tjson_object_object_get_ex(parent_obj, key_name.c_str(), &%s_obj);\n\n", $struct_name;
+
+	$suppress_union = false;
+	$suppress_struct = false;
+	while ($line = <>) {
+		chomp($line);
+		$member = "";
+		if ($line =~ /}.*;/) {
+			if ($suppress_struct eq true) {
+				printf $fh_trace_cpp "\t//end of struct $line\n";
+				printf $fh_retrace_cpp "\t//end of struct $line\n";
+				$suppress_struct = false;
+				next;
+			} elsif ($in_union eq true) {
+				if ($suppress_union eq true) {
+					$suppress_union = false; # end of union
+				}
+				printf $fh_trace_cpp "\t//end of union $line\n";
+				printf $fh_retrace_cpp "\t//end of union $line\n";
+				$in_union = false;
+				next;
+			} else {
+				last;
+			}
+		}
+		last if $line =~ /^} __attribute__.*/;
+
+		$line = clean_up_line($line);
+		next if $line =~ /^\s*$/; # ignore blank lines
+
+		@words = split /[\s\[]/, $line; # split on whitespace and also'[' to get char arrays
+		@words = grep  {/^\D/} @words; # remove values that start with digit from inside []
+		@words = grep  {!/\]/} @words; # remove values with brackets e.g. V4L2_H264_REF_LIST_LEN]
+
+		($type) = $words[0];
+
+		# unions inside the struct
+		if ($type eq 'union') {
+			handle_union($struct_name);
+			next;
+		}
+		# suppress anything inside a union including structs nested inside a union
+		if ($suppress_union eq true) {
+			printf $fh_trace_cpp "\t//$line\n";
+			printf $fh_retrace_cpp "\t//$line\n";
+			next;
+		}
+
+		# struct members inside the struct
+		if ($type eq 'struct') {
+			handle_struct();
+			next;
+		}
+		if ($suppress_struct eq true) {
+			printf $fh_trace_cpp "\t//$line\n";
+			printf $fh_retrace_cpp "\t//$line\n";
+			next;
+		}
+
+		$json_type = convert_type_to_json_type($type);
+
+		($member) = $words[scalar @words - 1];
+		$member =~ s/;//; # remove the ;
+
+		if ($member =~ /service_lines/) {
+			printf $fh_trace_cpp "\t//$line\n";
+			printf $fh_retrace_cpp "\t//$line\n";
+			next;
+		}
+
+		# Don't trace members that are pointers
+		if ($member =~ /\*/) {
+			printf $fh_trace_cpp "\t//$line\n";
+			printf $fh_retrace_cpp "\t//$line\n";
+			next;
+		}
+
+		if ($line =~ /dims\[V4L2_CTRL_MAX_DIMS\]/) {
+			printf $fh_trace_cpp "\t\/\* $line \*\/\n"; # add comment
+			printf $fh_trace_cpp "\tjson_object *dims_obj = json_object_new_array();\n";
+			printf $fh_trace_cpp "\tfor (int i = 0; i < (std::min((int) p->nr_of_dims, V4L2_CTRL_MAX_DIMS)); i++) {\n";
+			printf $fh_trace_cpp "\t\tjson_object_array_add(dims_obj, json_object_new_int64(p->dims[i]));\n\t}\n";
+			printf $fh_trace_cpp "\tjson_object_object_add(%s_obj, \"$member\", dims_obj);\n", $struct_name;
+
+			printf $fh_retrace_cpp "\t\/\* $line \*\/\n"; # add comment
+			printf $fh_retrace_cpp "\tjson_object *dims_obj;\n";
+			printf $fh_retrace_cpp "\tif (json_object_object_get_ex(%s_obj, \"$member\", &%s_obj)) {\n", $struct_name, $member;
+			printf $fh_retrace_cpp "\t\tfor (int i = 0; i < (std::min((int) p->nr_of_dims, V4L2_CTRL_MAX_DIMS)); i++) {\n";
+			printf $fh_retrace_cpp "\t\t\tif (json_object_array_get_idx(dims_obj, i))\n";
+			printf $fh_retrace_cpp "\t\t\t\tp->dims[i] = (__u32) json_object_get_int64(json_object_array_get_idx(dims_obj, i));\n\t\t}\n\t}\n";
+			next;
+		}
+
+		printf $fh_retrace_cpp "\tjson_object *%s_obj;\n", $member;
+
+		# struct v4l2_pix_format
+		if ($member =~ /priv/) {
+			printf $fh_trace_cpp "\tif (p->priv == V4L2_PIX_FMT_PRIV_MAGIC)\n";
+			printf $fh_trace_cpp "\t\tjson_object_object_add(%s_obj, \"%s\", json_object_new_string(\"V4L2_PIX_FMT_PRIV_MAGIC\"));\n", $struct_name, $member;
+			printf $fh_trace_cpp "\telse\n";
+			printf $fh_trace_cpp "\t\tjson_object_object_add(%s_obj, \"%s\", json_object_new_string(\"\"));\n", $struct_name, $member;
+
+			printf $fh_retrace_cpp "\tif (json_object_object_get_ex(%s_obj, \"$member\", &%s_obj)) {\n", $struct_name, $member;
+			printf $fh_retrace_cpp "\t\tif (!json_object_get_string(priv_obj))\n\t\t\treturn p;\n";
+			printf $fh_retrace_cpp "\t\tstd::string priv_str = json_object_get_string(priv_obj);\n";
+			printf $fh_retrace_cpp "\t\tif (!priv_str.empty())\n";
+			printf $fh_retrace_cpp "\t\t\tp->priv = V4L2_PIX_FMT_PRIV_MAGIC;\n\t}\n";
+			next;
+		}
+
+		printf $fh_trace_cpp "\tjson_object_object_add(%s_obj, \"%s\", json_object_new_", $struct_name, $member;
+		printf $fh_retrace_cpp "\tif (json_object_object_get_ex(%s_obj, \"$member\", &%s_obj))\n", $struct_name, $member;
+
+		# Convert char array members to string
+		if ($line =~ /.*\[.*/) {
+			if ($member =~ /.*name|driver|card|bus_info|description|model|magic|serial|userbits|APP_data|COM_data|linemask|data|start|count|raw|.*/) {
+				printf $fh_trace_cpp "string(reinterpret_cast<const char *>(p->$member)));\n";
+
+				my @char_array_size = ($line) =~ /\[(\w+)\]/g;
+				printf $fh_retrace_cpp "\t\tmemcpy(p->$member, json_object_get_string(%s_obj), @char_array_size);\n\n", $member;
+				next;
+			}
+		}
+
+		# special strings
+		if ($member =~ /version/) {
+			printf $fh_trace_cpp "string(ver2s(p->$member).c_str()));\n";
+			printf $fh_retrace_cpp "\t\tmemset(&p->$member, 0, sizeof(p->$member));\n\n"; # driver can fill in version
+			next;
+		}
+
+		printf $fh_retrace_cpp "\t\tp->$member = ";
+
+		if ($struct_name =~ /^v4l2_buffer$/) {
+			if ($member =~ /flags/) {
+				printf $fh_trace_cpp "string(fl2s_buffer(p->$member).c_str()));\n";
+				printf $fh_retrace_cpp "(%s) s2flags_buffer(json_object_get_string(%s_obj));\n\n", $type, $member, $flag_def_name;
+				next;
+			}
+		}
+
+		# strings
+		$val_def_name = get_val_def_name($member, $struct_name);
+		if ($val_def_name !~ /^\s*$/) {
+			printf $fh_trace_cpp "string(val2s(p->$member, %s).c_str()));\n", $val_def_name;
+			printf $fh_retrace_cpp "(%s) s2val(json_object_get_string(%s_obj), %s);\n", $type,  $member, $val_def_name;
+			next;
+		}
+
+		$flag_def_name = get_flag_def_name($member, $struct_name);
+		if ($flag_def_name !~ /^\s*$/) {
+			printf $fh_trace_cpp "string(fl2s(p->$member, %s).c_str()));\n", $flag_def_name;
+			printf $fh_retrace_cpp "(%s) s2flags(json_object_get_string(%s_obj), %s);\n\n", $type, $member, $flag_def_name;
+			next;
+		}
+
+		# integers
+		printf $fh_trace_cpp "$json_type(p->$member));\n";
+		printf $fh_retrace_cpp "(%s) json_object_get_%s(%s_obj);\n\n", $type, $json_type, $member;
+
+		# special treatment for v4l2_pix_format_mplane member plane_fmt[VIDEO_MAX_PLANES]
+		# it can only be traced after num_planes is known
+		if ($member =~ /num_planes/) {
+			printf $fh_trace_cpp "\tjson_object *plane_fmt_obj = json_object_new_array();\n";
+			printf $fh_trace_cpp "\tfor (int i = 0; i < (std::min((int) p->num_planes, VIDEO_MAX_PLANES)); i++) {\n";
+			printf $fh_trace_cpp "\t\tjson_object *element_obj = json_object_new_object();\n";
+			printf $fh_trace_cpp "\t\ttrace_v4l2_plane_pix_format_gen(&(p->plane_fmt[i]), element_obj);\n";
+			printf $fh_trace_cpp "\t\tjson_object *element_no_key_obj;\n";
+			printf $fh_trace_cpp "\t\tjson_object_object_get_ex(element_obj, \"v4l2_plane_pix_format\", &element_no_key_obj);\n";
+			printf $fh_trace_cpp "\t\tjson_object_array_add(plane_fmt_obj, element_no_key_obj);\n\t}\n";
+			printf $fh_trace_cpp "\tjson_object_object_add(v4l2_pix_format_mplane_obj, \"plane_fmt\", plane_fmt_obj);\n\n";
+
+			printf $fh_retrace_cpp "\tjson_object *plane_fmt_obj;\n";
+			printf $fh_retrace_cpp "\tif (json_object_object_get_ex(v4l2_pix_format_mplane_obj, \"plane_fmt\", &plane_fmt_obj)) {\n";
+			printf $fh_retrace_cpp "\t\tfor (int i = 0; i < (std::min((int) p->num_planes, VIDEO_MAX_PLANES)); i++) {\n";
+			printf $fh_retrace_cpp "\t\t\tif (json_object_array_get_idx(plane_fmt_obj, i)) {\n";
+			printf $fh_retrace_cpp "\t\t\t\tjson_object *element_obj = json_object_new_object();\n";
+			printf $fh_retrace_cpp "\t\t\t\tjson_object_object_add(element_obj, \"v4l2_plane_pix_format\", json_object_array_get_idx(plane_fmt_obj, i));\n";
+			printf $fh_retrace_cpp "\t\t\t\tp->plane_fmt[i] = *retrace_v4l2_plane_pix_format_gen(element_obj);\n";
+			printf $fh_retrace_cpp "\t\t\t}\n\t\t}\n\t}\n\n";
+		}
+	}
+
+	# The key name option allows a struct to be traced when it is nested inside another struct.
+	printf $fh_trace_cpp "\n\tif (key_name.empty())\n";
+	printf $fh_trace_cpp "\t\tjson_object_object_add(parent_obj, \"%s\", %s_obj);\n", $struct_name, $struct_name;
+	printf $fh_trace_cpp "\telse\n";
+	printf $fh_trace_cpp "\t\tjson_object_object_add(parent_obj, key_name.c_str(), %s_obj);\n", $struct_name;
+	printf $fh_trace_cpp "}\n\n";
+
+	printf $fh_retrace_cpp "\treturn p;\n}\n";
+}
+
+# generate functions for structs in v4l2-controls.h
+sub struct_gen_ctrl {
+	($struct_name) = ($_) =~ /struct (\w+) {/;
+
+	printf $fh_trace_h "void trace_%s_gen(void *ptr, json_object *parent_obj);\n", $struct_name;
+	printf $fh_trace_cpp "void trace_%s_gen(void *ptr, json_object *parent_obj)\n{\n", $struct_name;
+	printf $fh_trace_cpp "\tjson_object *%s_obj = json_object_new_object();\n", $struct_name;
+	printf $fh_trace_cpp "\tstruct %s *p = static_cast<struct %s*>(ptr);\n", $struct_name, $struct_name;
+
+	printf $fh_retrace_h "struct %s *retrace_%s_gen(json_object *ctrl_obj);\n", $struct_name, $struct_name;
+	printf $fh_retrace_cpp "struct %s *retrace_%s_gen(json_object *ctrl_obj)\n{\n", $struct_name, $struct_name;
+	printf $fh_retrace_cpp "\tstruct %s *p = (struct %s *) calloc(1, sizeof(%s));\n", $struct_name, $struct_name, $struct_name;
+	printf $fh_retrace_cpp "\tjson_object *%s_obj;\n", $struct_name;
+	# if a key value isn't found, assume it is retracing an element of an array
+	# e.g. in struct v4l2_ctrl_h264_pred_weights
+	printf $fh_retrace_cpp "\tif (!json_object_object_get_ex(ctrl_obj, \"%s\", &%s_obj))\n", $struct_name, $struct_name;
+	printf $fh_retrace_cpp "\t\t%s_obj = ctrl_obj;\n", $struct_name;
+
+	while ($line = <>) {
+		chomp($line);
+		last if $line =~ /};/;
+		$line = clean_up_line($line);
+		next if $line =~ /^\s*$/; # ignore blank lines
+		$line =~ s/;$//; # remove semi-colon at the end
+		@words = split /[\s\[]/, $line; # also split on '[' to avoid arrays
+		@words = grep  {/^\D/} @words; # remove values that start with digit from inside []
+		@words = grep  {!/\]/} @words; # remove values with brackets e.g. V4L2_H264_REF_LIST_LEN]
+
+		($type) = $words[0];
+		$json_type = convert_type_to_json_type($type);
+
+		($member) = $words[scalar @words - 1];
+
+		# generate members that are arrays
+		if ($line =~ /.*\[.*/) {
+			printf $fh_trace_cpp "\t\/\* %s \*\/\n", $line; # add comment
+			printf $fh_trace_cpp "\tjson_object *%s_obj = json_object_new_array();\n", $member;
+			printf $fh_retrace_cpp "\n\t\/\* %s \*\/\n", $line; # add comment
+
+			my @dimensions = ($line) =~ /\[(\w+)\]/g;
+			$dimensions_count = scalar @dimensions;
+
+			if ($dimensions_count > 1) {
+				printf $fh_retrace_cpp "\tint count_%s = 0;\n", $member;
+			}
+			printf $fh_retrace_cpp "\tjson_object *%s_obj;\n", $member;
+			printf $fh_retrace_cpp "\tif (json_object_object_get_ex(%s_obj, \"%s\", &%s_obj)) {\n", $struct_name, $member, $member;
+
+			for (my $idx = 0; $idx < $dimensions_count; $idx = $idx + 1) {
+				$size = $dimensions[$idx];
+				$index_letter = get_index_letter($idx);
+				printf $fh_trace_cpp "\t" x ($idx + 1);
+				printf $fh_trace_cpp "for (size_t %s = 0; %s < %s\; %s++) {\n", $index_letter, $index_letter, $size, $index_letter;
+
+				printf $fh_retrace_cpp "\t" x ($idx + 1);
+				printf $fh_retrace_cpp "\t";
+				printf $fh_retrace_cpp "for (size_t %s = 0; %s < %s\; %s++) {\n", $index_letter, $index_letter, $size, $index_letter;
+			}
+			printf $fh_trace_cpp "\t" x ($dimensions_count + 1);
+			printf $fh_retrace_cpp "\t" x ($dimensions_count + 1);
+			printf $fh_retrace_cpp "\t";
+
+			# handle arrays of structs
+			if ($type =~ /struct/) {
+				my $struct_name_nested = @words[1];
+				printf $fh_trace_cpp "json_object *element_obj = json_object_new_object();\n";
+				printf $fh_trace_cpp "\t" x ($dimensions_count + 1);
+				printf $fh_trace_cpp "trace_%s_gen(&(p->%s", $struct_name_nested, $member;
+				for (my $idx = 0; $idx < $dimensions_count; $idx = $idx + 1) {
+					printf $fh_trace_cpp "[%s]", get_index_letter($idx);
+				}
+				printf $fh_trace_cpp "), element_obj);\n";
+				printf $fh_trace_cpp "\t" x ($dimensions_count + 1);
+				printf $fh_trace_cpp "json_object *element_no_key_obj;\n";
+				printf $fh_trace_cpp "\t" x ($dimensions_count + 1);
+				printf $fh_trace_cpp "json_object_object_get_ex(element_obj, \"%s\", &element_no_key_obj);\n", $struct_name_nested;
+				printf $fh_trace_cpp "\t" x ($dimensions_count + 1);
+				printf $fh_trace_cpp "json_object_array_add(%s_obj, element_no_key_obj);\n", $member;
+
+				printf $fh_retrace_cpp "p->%s", $member;
+				for (my $idx = 0; $idx < $dimensions_count; $idx = $idx + 1) {
+					printf $fh_retrace_cpp "[%s]", get_index_letter($idx);
+				}
+				printf $fh_retrace_cpp " = *retrace_%s_gen(json_object_array_get_idx(%s_obj, ", $struct_name_nested, $member;
+				if ($dimensions_count > 1) {
+					printf $fh_retrace_cpp "count_%s++", $member;
+				} else {
+					printf $fh_retrace_cpp "i";
+				}
+				printf $fh_retrace_cpp "));\n";
+			} else {
+				# handle arrays of ints
+				printf $fh_trace_cpp "json_object_array_add(%s_obj, json_object_new_%s(p->%s", $member, $json_type, $member;
+				for (my $idx = 0; $idx < $dimensions_count; $idx = $idx + 1) {
+					printf $fh_trace_cpp "[%s]", get_index_letter($idx);
+				}
+				printf $fh_trace_cpp "));\n";
+
+				# add a check to avoid accessing a null array index
+				printf $fh_retrace_cpp "if (json_object_array_get_idx(%s_obj, ", $member;
+				if ($dimensions_count > 1) {
+					printf $fh_retrace_cpp "count_%s", $member;
+				} else {
+					printf $fh_retrace_cpp "i";
+				}
+				printf $fh_retrace_cpp "))\n";
+
+				printf $fh_retrace_cpp "\t" x ($dimensions_count + 1);
+				printf $fh_retrace_cpp "\t\t";
+				printf $fh_retrace_cpp "p->%s", $member;
+				for (my $idx = 0; $idx < $dimensions_count; $idx = $idx + 1) {
+					printf $fh_retrace_cpp "[%s]", get_index_letter($idx);
+				}
+
+				printf $fh_retrace_cpp " = ($type) json_object_get_%s(json_object_array_get_idx(%s_obj, ", $json_type, $member;
+				if ($dimensions_count > 1) {
+					printf $fh_retrace_cpp "count_%s++", $member;
+				} else {
+					printf $fh_retrace_cpp "i";
+				}
+				printf $fh_retrace_cpp "));\n";
+			}
+			# closing brackets for all array types
+			for (my $idx = $dimensions_count - 1; $idx >= 0 ; $idx = $idx - 1) {
+				printf $fh_trace_cpp "\t" x ($idx + 1);
+				printf $fh_trace_cpp "}\n";
+
+				printf $fh_retrace_cpp "\t" x ($idx + 1);
+				printf $fh_retrace_cpp "\t";
+				printf $fh_retrace_cpp "}\n";
+			}
+			printf $fh_retrace_cpp "\t}\n";
+			printf $fh_trace_cpp "\tjson_object_object_add(%s_obj, \"%s\", %s_obj);\n\n", $struct_name, $member, $member;
+		} elsif ($type =~ /struct/) {
+			# member that is a struct but not an array of structs
+			my $struct_name_nested = @words[1];
+			printf $fh_trace_cpp "\t\/\* %s \*\/\n", $line;
+			printf $fh_trace_cpp "\ttrace_%s_gen(&(p->%s), %s_obj);\n", $struct_name_nested, $member, $struct_name;
+
+			printf $fh_retrace_cpp "\t\/\* %s \*\/\n", $line;
+			printf $fh_retrace_cpp "\tjson_object *%s_obj;\n", $member;
+			printf $fh_retrace_cpp "\tjson_object_object_get_ex(%s_obj, \"%s\", &%s_obj);\n", $struct_name, $struct_name_nested, $member;
+			printf $fh_retrace_cpp "\tp->%s = \*retrace_%s_gen(%s_obj);\n\n", $member, $struct_name_nested, $member;
+		} else {
+			printf $fh_trace_cpp "\tjson_object_object_add(%s_obj, \"%s\", json_object_new_", $struct_name, $member;
+			printf $fh_retrace_cpp "\n\tjson_object *%s_obj;\n", $member;
+			printf $fh_retrace_cpp "\tif (json_object_object_get_ex(%s_obj, \"%s\", &%s_obj))\n", $struct_name, $member, $member;
+
+			# strings
+			if ($member =~ /flags/) {
+				if ($struct_name eq "v4l2_ctrl_fwht_params") {
+					printf $fh_trace_cpp "string(fl2s_fwht(p->$member).c_str()));\n";
+					printf $fh_retrace_cpp "\t\tp->%s = ($type) s2flags_fwht(json_object_get_string(%s_obj));\n", $member, $member, $flag_func_name;
+				} else {
+					printf $fh_trace_cpp "string(fl2s(p->$member, %sflag_def).c_str()));\n", $flag_func_name;
+					printf $fh_retrace_cpp "\t\tp->%s = ($type) s2flags(json_object_get_string(%s_obj), %sflag_def);\n", $member, $member, $flag_func_name;
+				}
+				next;
+			}
+
+			# Add members with a single string value (e.g. enums, #define)
+			$val_def_name = get_val_def_name($member, $struct_name);
+			if ($val_def_name !~ /^\s*$/) {
+				printf $fh_trace_cpp "string(val2s(p->$member, %s).c_str()));\n", $val_def_name;
+				printf $fh_retrace_cpp "\t\tp->%s = ($type) s2val(json_object_get_string(%s_obj), $val_def_name);\n", $member, $member;
+				next;
+			}
+
+			# integers
+			printf $fh_trace_cpp "%s(p->%s));\n", $json_type, $member;
+			printf $fh_retrace_cpp "\t\tp->%s = ($type) json_object_get_%s(%s_obj);\n", $member, $json_type, $member;
+		}
+	}
+	printf $fh_trace_cpp "\tjson_object_object_add(parent_obj, \"%s\", %s_obj);\n", $struct_name, $struct_name;
+	printf $fh_trace_cpp "}\n\n";
+
+	printf $fh_retrace_cpp "\n\treturn p;\n";
+	printf $fh_retrace_cpp "}\n\n";
+}
+
+open($fh_trace_cpp, '>', 'trace-gen.cpp') or die "Could not open trace-gen.cpp for writing";
+printf $fh_trace_cpp "/* SPDX-License-Identifier: GPL-2.0-only */\n/*\n * Copyright 2022 Collabora Ltd.\n */\n\n";
+printf $fh_trace_cpp "#include \"v4l2-tracer-common.h\"\n\n";
+
+open($fh_trace_h, '>', 'trace-gen.h') or die "Could not open trace-gen.h for writing";
+printf $fh_trace_h "/* SPDX-License-Identifier: GPL-2.0-only */\n/*\n * Copyright 2022 Collabora Ltd.\n */\n\n";
+printf $fh_trace_h "\#ifndef TRACE_GEN_H\n";
+printf $fh_trace_h "\#define TRACE_GEN_H\n\n";
+
+open($fh_retrace_cpp, '>', 'retrace-gen.cpp') or die "Could not open retrace-gen.cpp for writing";
+printf $fh_retrace_cpp "/* SPDX-License-Identifier: GPL-2.0-only */\n/*\n * Copyright 2022 Collabora Ltd.\n */\n\n";
+printf $fh_retrace_cpp "#include \"v4l2-tracer-common.h\"\n\n";
+
+open($fh_retrace_h, '>', 'retrace-gen.h') or die "Could not open retrace-gen.h for writing";
+printf $fh_retrace_h "/* SPDX-License-Identifier: GPL-2.0-only */\n/*\n * Copyright 2022 Collabora Ltd.\n */\n\n";
+printf $fh_retrace_h "\#ifndef RETRACE_GEN_H\n";
+printf $fh_retrace_h "\#define RETRACE_GEN_H\n\n";
+
+open($fh_common_info_h, '>', 'v4l2-tracer-info-gen.h') or die "Could not open v4l2-tracer-info-gen.h for writing";
+printf $fh_common_info_h "/* SPDX-License-Identifier: GPL-2.0-only */\n/*\n * Copyright 2022 Collabora Ltd.\n */\n\n";
+printf $fh_common_info_h "\#ifndef V4L2_TRACER_INFO_GEN_H\n";
+printf $fh_common_info_h "\#define V4L2_TRACER_INFO_GEN_H\n\n";
+printf $fh_common_info_h "#include \"v4l2-tracer-common.h\"\n\n";
+
+$in_v4l2_controls = true;
+
+while (<>) {
+	if (grep {/#define __LINUX_VIDEODEV2_H/} $_) {
+		$in_v4l2_controls = false;
+	}
+
+	if (grep {/^#define.+FWHT_FL_.+/} $_) {
+		flag_gen("fwht");
+	} elsif (grep {/^#define V4L2_VP8_LF.*/} $_) {
+		flag_gen("vp8_loop_filter");
+	} elsif (grep {/^#define.+_FL_.+/} $_) {  #use to get media flags
+		flag_gen();
+	} elsif (grep {/^#define.+_FLAG_.+/} $_) {
+		flag_gen();
+	}
+
+	if ($in_v4l2_controls eq true) {
+		if (grep {/^struct/} $_) {
+			struct_gen_ctrl();
+		}
+	} else {
+		if (grep {/^struct/} $_) {
+			struct_gen();
+		}
+	}
+
+	if (grep {/^enum/} $_) {
+		enum_gen();
+	}
+
+	if (grep {/^#define (V4L2_CID\w*)\s*.*/} $_) {
+		push (@controls, $_);
+	}
+
+	if (grep {/^\/\* Control classes \*\//} $_) {
+		printf $fh_common_info_h "constexpr val_def ctrlclass_val_def[] = {\n";
+		while (<>) {
+			last if $_ =~ /^\s*$/; # last if blank line
+			($ctrl_class) = ($_) =~ /#define\s*(\w+)\s+.*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $ctrl_class, $ctrl_class;
+		}
+		printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+	}
+
+	if (grep {/\/\* Values for 'capabilities' field \*\//} $_) {
+		printf $fh_common_info_h "constexpr flag_def v4l2_cap_flag_def[] = {\n";
+		while (<>) {
+			last if $_ =~ /.*V I D E O   I M A G E   F O R M A T.*/;
+			next if ($_ =~ /^\/?\s?\*.*/); # skip comments
+			next if $_ =~ /^\s*$/; # skip blank lines
+			($cap) = ($_) =~ /#define\s+(\w+)\s+.+/;
+			printf $fh_common_info_h "\t{ $cap, \"$cap\" },\n"
+		}
+		printf $fh_common_info_h "\t{ 0, \"\" }\n};\n\n";
+	}
+
+	if (grep {/\*      Pixel format         FOURCC                          depth  Description  \*\//} $_) {
+		printf $fh_common_info_h "constexpr val_def v4l2_pix_fmt_val_def[] = {\n";
+		while (<>) {
+			last if $_ =~ /.*SDR formats - used only for Software Defined Radio devices.*/;
+			next if ($_ =~ /^\s*\/\*.*/); # skip comments
+			next if $_ =~ /^\s*$/; # skip blank lines
+			($pixfmt) = ($_) =~ /#define (\w+)\s+.*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $pixfmt, $pixfmt;
+		}
+		printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+	}
+
+	if (grep {/^#define V4L2_BUF_CAP_SUPPORTS_MMAP.*/} $_) {
+		printf $fh_common_info_h "constexpr flag_def v4l2_buf_cap_flag_def[] = {\n";
+		($buf_cap) = ($_) =~ /#define (\w+)\s+.*/;
+		printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $buf_cap, $buf_cap;
+		while (<>) {
+			last if $_ =~ /^\s*$/; # blank line
+			($buf_cap) = ($_) =~ /#define (\w+)\s+.*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $buf_cap, $buf_cap;
+		}
+		printf $fh_common_info_h "\t{ 0, \"\" }\n};\n\n";
+	}
+
+	if (grep {/.*Flags for 'capability' and 'capturemode' fields.*/} $_) {
+		printf $fh_common_info_h "constexpr val_def streamparm_val_def[] = {\n";
+		while (<>) {
+			last if $_ =~ /^\s*$/; # blank line
+			($streamparm) = ($_) =~ /^#define\s*(\w+)\s*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $streamparm, $streamparm;
+		}
+		printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+	}
+
+	if (grep {/.*V4L2_ENC_CMD_START.*/} $_) {
+		printf $fh_common_info_h "constexpr val_def encoder_cmd_val_def[] = {\n";
+		($enc_cmd) = ($_) =~ /^#define\s*(\w+)\s*/;
+		printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $enc_cmd, $enc_cmd;
+		while (<>) {
+			last if $_ =~ /^\s*$/; # blank line
+			($enc_cmd) = ($_) =~ /^#define\s*(\w+)\s*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $enc_cmd, $enc_cmd;
+		}
+		printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+	}
+
+	if (grep {/.*Decoder commands.*/} $_) {
+		printf $fh_common_info_h "constexpr val_def decoder_cmd_val_def[] = {\n";
+		while (<>) {
+			last if $_ =~ /^\s*$/; # blank line
+			($dec_cmd) = ($_) =~ /^#define\s*(\w+)\s*/;
+			printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $dec_cmd, $dec_cmd;
+		}
+		printf $fh_common_info_h "\t{ -1, \"\" }\n};\n\n";
+	}
+
+	if (grep {/^#define\s+(VIDIOC_\w*)\s*.*/} $_) {
+		push (@ioctls, $_);
+	}
+
+	if (grep {/^#define\s+(MEDIA_IOC\w*)\s*.*/} $_) {
+		push (@ioctls, $_);
+	}
+
+	if (grep {/^#define\s+(MEDIA_REQUEST_IOC\w*)\s*.*/} $_) {
+		push (@ioctls, $_);
+	}
+}
+
+printf $fh_common_info_h "constexpr val_def control_val_def[] = {\n";
+foreach (@controls) {
+	($control) = ($_) =~ /^#define\s*(\w+)\s*/;
+	next if ($control =~ /BASE$/);
+	printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $control, $control;
+}
+printf $fh_common_info_h "\t{ -1, \"\" }\n};\n";
+
+printf $fh_common_info_h "constexpr val_def ioctl_val_def[] = {\n";
+foreach (@ioctls) {
+	($ioctl) = ($_) =~ /^#define\s*(\w+)\s*/;
+	printf $fh_common_info_h "\t{ %s,\t\"%s\" },\n", $ioctl, $ioctl;
+}
+printf $fh_common_info_h "\t{ -1, \"\" }\n};\n";
+
+
+printf $fh_trace_h "\n#endif\n";
+close $fh_trace_h;
+close $fh_trace_cpp;
+
+printf $fh_retrace_h "\n#endif\n";
+close $fh_retrace_h;
+close $fh_retrace_cpp;
+
+printf $fh_common_info_h "\n#endif\n";
+close $fh_common_info_h;
diff --git a/utils/v4l2-tracer/v4l2-tracer.1.in b/utils/v4l2-tracer/v4l2-tracer.1.in
new file mode 100644
index 00000000..13942eba
--- /dev/null
+++ b/utils/v4l2-tracer/v4l2-tracer.1.in
@@ -0,0 +1,111 @@
+.TH "V4L2-TRACER" "1" "November 2022" "v4l-utils @PACKAGE_VERSION@" "User Commands"
+.SH NAME
+v4l2-tracer - An application to trace and replay stateless video decoding.
+.SH SYNOPSIS
+\fBv4l2-tracer \fR[options] \fBtrace\fR <\fItracee\fR>
+.RS
+.RE
+\fBv4l2-tracer \fR[options] \fBretrace\fR  <\fItrace_file\fR>\fB.json\fR
+.RS
+.RE
+\fBv4l2-tracer clean\fR  <\fIfile\fR>\fB.json\fR
+.RS
+.RE
+
+.SH DESCRIPTION
+The v4l2-tracer utility traces, records and replays userspace applications
+that implement the v4l2 memory-to-memory stateless video decoder interface.
+.SS Trace
+Trace system calls and video frame data passed by userspace application <\fItracee\fR> to kernel driver.
+All stateless codec controls in user-space API can be traced. Outputs a JSON-formatted trace file.
+.SS Retrace
+Read the JSON-formatted <\fItrace_file\fR>\fB.json\fR. Replay the same system calls and pass the same video frame data to kernel driver.
+Outputs a JSON-formatted retrace file.
+
+.SS Clean
+Remove lines with irrelevant differences (e.g. file descriptors and memory addresses) from JSON files.
+Outputs a clean copy, not necessarily still in JSON-format.
+
+.SH OPTIONS
+.SS Common Options
+.TP
+\fB\-c\fR, \fB\-\-compact\fR
+Write minimal whitespace in JSON file.
+.TP
+\fB\-g\fR, \fB\-\-debug\fR
+Turn on verbose reporting plus additional debug info.
+.TP
+\fB\-h\fR, \fB\-\-help\fR
+Display this message.
+.TP
+\fB\-r\fR, \fB\-\-raw\fR
+Write decoded video frame data to JSON file.
+.TP
+\fB\-v\fR, \fB\-\-verbose\fR
+Turn on verbose reporting.
+.TP
+\fB\-y\fR, \fB\-\-yuv\fR
+Write decoded video frame data to yuv file.
+
+.SS Retrace Options
+.TP
+\fB\-d\fR, \fB\-\-device\fR <\fIdev\fR>
+Use a different video device than specified in the trace file.
+.RS
+<\fIdev\fR> must be a digit corresponding to an existing /dev/video<\fIdev\fR>
+.RE
+.TP
+\fB\-m\fR, \fB\-\-media\fR <\fIdev\fR>
+Use a different media device than specified in the trace file.
+.RS
+<\fIdev\fR> must be a digit corresponding to an existing /dev/media<\fIdev\fR>
+.RE
+
+.SH EXIT STATUS
+On success, it returns 0. Otherwise, it will return 1 or an error code.
+
+.SH EXAMPLE
+.TP
+Trace an application decoding VP8 video:
+.EX
+\fIv4l2-tracer trace gst-launch-1.0 -- filesrc location=test-25fps.vp8 ! parsebin ! v4l2slvp8dec ! videocodectestsink\fR
+.EE
+.TP
+A trace file is generated:
+.EE
+\fI71827_trace.json\fR
+.TP
+Retrace the trace file:
+.EX
+\fIv4l2-tracer retrace 71827_trace.json\fR
+.EE
+.TP
+Specify device nodes if retracing on a different driver:
+.EX
+\fIv4l2-tracer -d0 -m0 retrace 71827_trace.json\fR
+.EE
+.TP
+A retrace file is generated:
+.EE
+\fI71827_trace_retrace.json\fR
+.EX
+.TP
+Remove file descriptors and addresses (optional):
+.EX
+\fIv4l2-tracer clean 71827_trace.json\fR
+.EE
+.EX
+\fIv4l2-tracer clean 71827_trace_retrace.json\fR
+.EE
+.TP
+Clean files are generated for comparison:
+.EX
+\fIclean_71827_trace.json\fR
+.EE
+.EE
+\fIclean_71827_trace_retrace.json\fR
+.EX
+
+.SH BUGS
+Bug reports or questions about this utility should be sent to the
+linux-media@vger.kernel.org mailinglist.
diff --git a/utils/v4l2-tracer/v4l2-tracer.cpp b/utils/v4l2-tracer/v4l2-tracer.cpp
new file mode 100644
index 00000000..114bcce4
--- /dev/null
+++ b/utils/v4l2-tracer/v4l2-tracer.cpp
@@ -0,0 +1,412 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2022 Collabora Ltd.
+ */
+
+#include <time.h>
+#include <sys/wait.h>
+#include <climits>
+#include "v4l2-tracer-common.h"
+
+int tracer(int argc, char *argv[], bool retrace = false);
+int retracer(std::string trace_filename);
+
+enum Options {
+	V4l2TracerOptCompactPrint = 'c',
+	V4l2TracerOptSetVideoDevice = 'd',
+	V4l2TracerOptDebug = 'g',
+	V4l2TracerOptHelp = 'h',
+	V4l2TracerOptSetMediaDevice = 'm',
+	V4l2TracerOptWriteDecodedToJson = 'r',
+	V4l2TracerOptVerbose = 'v',
+	V4l2TracerOptWriteDecodedToYUVFile = 'y',
+};
+
+static struct option long_options[] = {
+	{ "compact", no_argument, nullptr, V4l2TracerOptCompactPrint },
+	{ "video_device", required_argument, nullptr, V4l2TracerOptSetVideoDevice },
+	{ "debug", no_argument, nullptr, V4l2TracerOptDebug },
+	{ "help", no_argument, nullptr, V4l2TracerOptHelp },
+	{ "media_device", required_argument, nullptr, V4l2TracerOptSetMediaDevice },
+	{ "raw", no_argument, nullptr, V4l2TracerOptWriteDecodedToJson },
+	{ "verbose", no_argument, nullptr, V4l2TracerOptVerbose },
+	{ "yuv", no_argument, nullptr, V4l2TracerOptWriteDecodedToYUVFile },
+	{ nullptr, 0, nullptr, 0 }
+};
+
+char short_options[] = {
+	V4l2TracerOptCompactPrint,
+	V4l2TracerOptSetVideoDevice, ':',
+	V4l2TracerOptDebug,
+	V4l2TracerOptHelp,
+	V4l2TracerOptSetMediaDevice, ':',
+	V4l2TracerOptWriteDecodedToJson,
+	V4l2TracerOptVerbose,
+	V4l2TracerOptWriteDecodedToYUVFile
+};
+
+int get_options(int argc, char *argv[])
+{
+	int ch;
+
+	do {
+		/* If there are no commands after the valid options, return err. */
+		if (optind == argc) {
+			print_usage();
+			return -1;
+		}
+
+		/* Avoid reading the tracee's options. */
+		if ((strcmp(argv[optind], "trace") == 0) || (strcmp(argv[optind], "retrace") == 0))
+			return 0;
+
+		ch = getopt_long(argc, argv, short_options, long_options, NULL);
+		switch (ch) {
+		case V4l2TracerOptCompactPrint: {
+			setenv("V4L2_TRACER_OPTION_COMPACT_PRINT", "true", 0);
+			break;
+		}
+		case V4l2TracerOptSetVideoDevice: {
+			std::string device_num = optarg;
+			try {
+				std::stoi(device_num, nullptr, 0);
+			} catch (std::exception& e) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "can't convert <dev> \'%s\' to integer\n", device_num.c_str());
+				return -1;
+			}
+			if (device_num[0] >= '0' && device_num[0] <= '9' && device_num.length() <= 3) {
+				std::string path_video = "/dev/video";
+				path_video += optarg;
+				setenv("V4L2_TRACER_OPTION_SET_VIDEO_DEVICE", path_video.c_str(), 0);
+			} else {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "cannot use device number\'%s\'\n", device_num.c_str());
+				return -1;
+			}
+			break;
+		}
+		case V4l2TracerOptDebug:
+			setenv("V4L2_TRACER_OPTION_VERBOSE", "true", 0);
+			setenv("V4L2_TRACER_OPTION_DEBUG", "true", 0);
+			break;
+		case V4l2TracerOptHelp:
+			print_usage();
+			return -1;
+		case V4l2TracerOptSetMediaDevice: {
+			std::string device_num = optarg;
+			try {
+				std::stoi(device_num, nullptr, 0);
+			} catch (std::exception& e) {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "can't convert <dev> \'%s\' to integer\n", device_num.c_str());
+				return -1;
+			}
+			if (device_num[0] >= '0' && device_num[0] <= '9' && device_num.length() <= 3) {
+				std::string path_media = "/dev/media";
+				path_media += optarg;
+				setenv("V4L2_TRACER_OPTION_SET_MEDIA_DEVICE", path_media.c_str(), 0);
+			} else {
+				fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+				fprintf(stderr, "cannot use device number\'%s\'\n", device_num.c_str());
+				return -1;
+			}
+			break;
+		}
+		case V4l2TracerOptWriteDecodedToJson:
+			setenv("V4L2_TRACER_OPTION_WRITE_DECODED_TO_JSON_FILE", "true", 0);
+			break;
+		case V4l2TracerOptVerbose:
+			setenv("V4L2_TRACER_OPTION_VERBOSE", "true", 0);
+			break;
+		case V4l2TracerOptWriteDecodedToYUVFile:
+			setenv("V4L2_TRACER_OPTION_WRITE_DECODED_TO_YUV_FILE", "true", 0);
+			break;
+		default:
+			break;
+		}
+
+		/* invalid option */
+		if (optopt > 0) {
+			print_usage();
+			return -1;
+		}
+
+	} while (ch != -1);
+
+	return 0;
+}
+
+int clean(std::string trace_filename)
+{
+	FILE *trace_file = fopen(trace_filename.c_str(), "r");
+	if (trace_file == NULL) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "cannot open \'%s\'\n", trace_filename.c_str());
+		return 1;
+	}
+
+	fprintf(stderr, "Cleaning: %s\n", trace_filename.c_str());
+
+	std::string clean_filename = "clean_" + trace_filename;
+	FILE *clean_file = fopen(clean_filename.c_str(), "w");
+	if (clean_file == NULL) {
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "cannot open \'%s\'\n", clean_filename.c_str());
+		return 1;
+	}
+
+	std::string line;
+	char buf[SHRT_MAX];
+	int count_total = 0;
+	int count_lines_removed = 0;
+
+	while (fgets(buf, SHRT_MAX, trace_file) != nullptr) {
+		line = buf;
+		count_total++;
+		if (line.find("fd") != line.npos) {
+			count_lines_removed++;
+			continue;
+		}
+		if (line.find("address") != line.npos) {
+			count_lines_removed++;
+			continue;
+		}
+		if (line.find("fildes") != line.npos) {
+			count_lines_removed++;
+			continue;
+		}
+		if (line.find("\"start\"") != line.npos) {
+			count_lines_removed++;
+			continue;
+		}
+		if (line.find("\"name\"") != line.npos) {
+			count_lines_removed++;
+			continue;
+		}
+
+		fputs(buf, clean_file);
+	}
+
+	fclose(trace_file);
+	fclose(clean_file);
+	fprintf(stderr, "Removed %d lines of %d total lines: %s\n",
+	        count_lines_removed, count_total, clean_filename.c_str());
+
+	return 0;
+}
+
+int tracer(int argc, char *argv[], bool retrace)
+{
+	char *exec[argc];
+	int exec_index = 0;
+
+	char retrace_command[] = "__retrace";
+
+	if (retrace) {
+		std::string trace_file = argv[optind];
+		if (trace_file.find(".json") == trace_file.npos) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "Trace file \'%s\' must have .json file extension\n",
+			        trace_file.c_str());
+			print_usage();
+			return -1;
+		}
+	}
+
+	/* Get the application to be traced. */
+	if (retrace) {
+		exec[exec_index++] = argv[0]; /* tracee is v4l2-tracer, local or installed */
+		exec[exec_index++] = retrace_command;
+		exec[exec_index++] = argv[optind]; /* json file to be retraced */
+	} else {
+		while (optind < argc)
+			exec[exec_index++] = argv[optind++];
+	}
+	exec[exec_index] = nullptr;
+
+	/* Create a unique trace filename and open a file. */
+	std::string trace_id;
+	if (retrace) {
+		std::string json_file_name = argv[optind];
+		trace_id = json_file_name.substr(0, json_file_name.find(".json"));
+		trace_id += "_retrace";
+	} else {
+		trace_id = std::to_string(time(nullptr));
+		trace_id = trace_id.substr(5, trace_id.npos) + "_trace";
+	}
+	setenv("TRACE_ID", trace_id.c_str(), 0);
+	std::string trace_filename = trace_id + ".json";
+	FILE *trace_file = fopen(trace_filename.c_str(), "w");
+	if (trace_file == nullptr) {
+		fprintf(stderr, "Could not open trace file: %s\n", trace_filename.c_str());
+		perror("");
+		return errno;
+	}
+
+	/* Open the json array.*/
+	fputs("[\n", trace_file);
+
+	/* Add v4l-utils package and git info to the top of the trace file. */
+	std::string json_str;
+	json_object *v4l2_tracer_info_obj = json_object_new_object();
+	json_object_object_add(v4l2_tracer_info_obj, "package_version",
+	                       json_object_new_string(PACKAGE_VERSION));
+	std::string git_commit_cnt = STRING(GIT_COMMIT_CNT);
+	git_commit_cnt = git_commit_cnt.erase(0, 1); /* remove the hyphen in front of git_commit_cnt */
+	json_object_object_add(v4l2_tracer_info_obj, "git_commit_cnt",
+	                       json_object_new_string(git_commit_cnt.c_str()));
+	json_object_object_add(v4l2_tracer_info_obj, "git_sha",
+	                       json_object_new_string(STRING(GIT_SHA)));
+	json_object_object_add(v4l2_tracer_info_obj, "git_commit_date",
+	                       json_object_new_string(STRING(GIT_COMMIT_DATE)));
+	json_str = json_object_to_json_string(v4l2_tracer_info_obj);
+	fwrite(json_str.c_str(), sizeof(char), json_str.length(), trace_file);
+	fputs(",\n", trace_file);
+	json_object_put(v4l2_tracer_info_obj);
+
+	/* Add v4l2-tracer command line to the top of the trace file. */
+	json_object *tracee_obj = json_object_new_object();
+	std::string tracee;
+	for (int i = 0; i < argc; i++) {
+		tracee += argv[i];
+		tracee += " ";
+	}
+	json_object_object_add(tracee_obj, "Trace", json_object_new_string(tracee.c_str()));
+	const time_t t = time(nullptr);
+	json_object_object_add(tracee_obj, "Timestamp", json_object_new_string(ctime(&t)));
+
+	json_str = json_object_to_json_string(tracee_obj);
+	fwrite(json_str.c_str(), sizeof(char), json_str.length(), trace_file);
+	fputs(",\n", trace_file);
+	json_object_put(tracee_obj);
+	fclose(trace_file);
+
+	/*
+	 * Preload the libv4l2tracer library. If the program is installed, load the library
+	 * from its installed location, otherwise load it locally. If it's loaded locally,
+	 * use ./configure --disable-dyn-libv4l.
+	 */
+	std::string libv4l2tracer_path;
+	std::string program = argv[0];
+	std::size_t idx = program.rfind("/v4l2-tracer");
+	if (idx != program.npos) {
+		libv4l2tracer_path = program.replace(program.begin() + idx + 1, program.end(), ".libs");
+		DIR *dp = opendir(libv4l2tracer_path.c_str());
+		if (dp == nullptr)
+			libv4l2tracer_path = program.replace(program.begin() + idx, program.end(), "./.libs");
+		closedir(dp);
+	} else {
+		libv4l2tracer_path = STRING(LIBTRACER_PATH);
+	}
+	libv4l2tracer_path += "/libv4l2tracer.so";
+	if (getenv("V4L2_TRACER_OPTION_VERBOSE"))
+		fprintf(stderr, "Loading libv4l2tracer: %s\n", libv4l2tracer_path.c_str());
+	setenv("LD_PRELOAD", libv4l2tracer_path.c_str(), 0);
+
+	if (fork() == 0) {
+
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "tracee: ");
+			for (int i = 0; i < exec_index; i++)
+				fprintf(stderr,"%s ", exec[i]);
+			fprintf(stderr, "\n");
+		}
+
+		execvpe(exec[0], (char* const*) exec, environ);
+
+		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+		fprintf(stderr, "could not execute application \'%s\'", exec[0]);
+		perror(" ");
+		return errno;
+	}
+
+	int exec_result;
+	wait(&exec_result);
+
+	if (WEXITSTATUS(exec_result)) {
+		fprintf(stderr, "Trace error: %s\n", trace_filename.c_str());
+
+		trace_file = fopen(trace_filename.c_str(), "a");
+		fseek(trace_file, 0L, SEEK_END);
+		fputs("\n]\n", trace_file);
+		fclose(trace_file);
+
+		exit(EXIT_FAILURE);
+	}
+
+	/* Close the json-array and the trace file. */
+	trace_file = fopen(trace_filename.c_str(), "a");
+	fseek(trace_file, 0L, SEEK_END);
+	fputs("\n]\n", trace_file);
+	fclose(trace_file);
+
+	if (retrace)
+		fprintf(stderr, "Retrace complete: ");
+	else
+		fprintf(stderr, "Trace complete: ");
+	fprintf(stderr, "%s", trace_filename.c_str());
+	fprintf(stderr, "\n");
+
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int ret = -1;
+
+	if (argc <= 1) {
+		print_usage();
+		return ret;
+	}
+
+	ret = get_options(argc, argv);
+
+	if (ret < 0) {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		return ret;
+	}
+
+	if (optind == argc) {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		print_usage();
+		return ret;
+	}
+
+	std::string command = argv[optind++];
+
+	if (optind == argc) {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG"))
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+		print_usage();
+		return ret;
+	}
+
+	if (command == "trace") {
+		ret = tracer(argc, argv);
+	} else if (command == "retrace") {
+		ret = tracer(argc, argv, true);
+	} else if (command == "__retrace") {
+		/*
+		 * This command is meant to be used only internally to allow
+		 * v4l2-tracer to recursively trace itself during a retrace.
+		 */
+		ret = retracer(argv[optind]);
+	} else if (command == "clean") {
+		ret = clean (argv[optind]);
+	} else {
+		if (getenv("V4L2_TRACER_OPTION_DEBUG")) {
+			fprintf(stderr, "%s:%s:%d\n", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "tracee: ");
+			for (int i = 0; i < argc; i++)
+				fprintf(stderr,"%s ", argv[i]);
+			fprintf(stderr, "\n");
+		}
+		print_usage();
+	}
+
+	return ret;
+}
-- 
2.38.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] v4l2-tracer: add support for most basic controls
  2022-11-14 18:44 [PATCH v3] utils: add v4l2-tracer utility Deborah Brouwer
@ 2022-11-15 11:36 ` Hans Verkuil
  2022-11-16  5:04   ` Deborah Brouwer
  2022-11-16  7:59 ` [PATCHv2] " Hans Verkuil
  1 sibling, 1 reply; 4+ messages in thread
From: Hans Verkuil @ 2022-11-15 11:36 UTC (permalink / raw)
  To: Deborah Brouwer
  Cc: daniel.almeida, nfraprado, nicolas.dufresne, deborahbrouwer3563,
	linux-media

The v4l2-tracer utility didn't support tracing simple controls without
a payload (i.e. the 'size' field is 0) and just a simple value.

This adds support for that, plus the four standard INTEGER64 controls
that use field value64.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
---
Hi Deb,

I think this is a nice addition for your v3 patch. Support for these
common simple controls is easy to add, and it makes for much better
tracing.

	Hans
---
diff --git a/utils/v4l2-tracer/retrace.cpp b/utils/v4l2-tracer/retrace.cpp
index b736e835..f0353988 100644
--- a/utils/v4l2-tracer/retrace.cpp
+++ b/utils/v4l2-tracer/retrace.cpp
@@ -734,7 +734,23 @@ struct v4l2_ext_control *retrace_v4l2_ext_control(json_object *parent_obj, int c
 		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
 		                                          v4l2_stateless_hevc_start_code_val_def);
 		break;
+	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
+	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
+	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
+	case V4L2_CID_PIXEL_RATE: {
+		json_object *value64_obj;
+
+		if (json_object_object_get_ex(v4l2_ext_control_obj, "value64", &value64_obj))
+			p->value64 = json_object_get_int64(value64_obj);
+		break;
+	}
 	default:
+		if (!p->size) {
+			json_object *value_obj;
+
+			if (json_object_object_get_ex(v4l2_ext_control_obj, "value", &value_obj))
+				p->value = json_object_get_int64(value_obj);
+		}
 		break;
 	}

diff --git a/utils/v4l2-tracer/trace.cpp b/utils/v4l2-tracer/trace.cpp
index f0bd7002..e26d6e6d 100644
--- a/utils/v4l2-tracer/trace.cpp
+++ b/utils/v4l2-tracer/trace.cpp
@@ -405,9 +405,19 @@ void trace_v4l2_ext_control(void *arg, json_object *parent_obj, std::string key_
 	case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
 		trace_v4l2_ctrl_mpeg2_quantisation_gen(p->p_mpeg2_quantisation, v4l2_ext_control_obj);
 		break;
+	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
+	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
+	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
+	case V4L2_CID_PIXEL_RATE:
+		json_object_object_add(v4l2_ext_control_obj, "value64", json_object_new_uint64(p->value64));
+		break;
 	default:
-		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
-		fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
+		if (p->size) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
+		} else {
+			json_object_object_add(v4l2_ext_control_obj, "value", json_object_new_uint64(p->value));
+		}
 		break;
 	}



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] v4l2-tracer: add support for most basic controls
  2022-11-15 11:36 ` [PATCH] v4l2-tracer: add support for most basic controls Hans Verkuil
@ 2022-11-16  5:04   ` Deborah Brouwer
  0 siblings, 0 replies; 4+ messages in thread
From: Deborah Brouwer @ 2022-11-16  5:04 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: daniel.almeida, nfraprado, nicolas.dufresne, deborahbrouwer3563,
	linux-media

Hi Hans,
Thanks for this! I have one question below

On Tue, Nov 15, 2022 at 12:36:44PM +0100, Hans Verkuil wrote:
> The v4l2-tracer utility didn't support tracing simple controls without
> a payload (i.e. the 'size' field is 0) and just a simple value.
> 
> This adds support for that, plus the four standard INTEGER64 controls
> that use field value64.
> 
> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
> ---
> Hi Deb,
> 
> I think this is a nice addition for your v3 patch. Support for these
> common simple controls is easy to add, and it makes for much better
> tracing.
> 
> 	Hans
> ---
> diff --git a/utils/v4l2-tracer/retrace.cpp b/utils/v4l2-tracer/retrace.cpp
> index b736e835..f0353988 100644
> --- a/utils/v4l2-tracer/retrace.cpp
> +++ b/utils/v4l2-tracer/retrace.cpp
> @@ -734,7 +734,23 @@ struct v4l2_ext_control *retrace_v4l2_ext_control(json_object *parent_obj, int c
>  		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
>  		                                          v4l2_stateless_hevc_start_code_val_def);
>  		break;
> +	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
> +	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
> +	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
> +	case V4L2_CID_PIXEL_RATE: {
> +		json_object *value64_obj;
> +
> +		if (json_object_object_get_ex(v4l2_ext_control_obj, "value64", &value64_obj))
> +			p->value64 = json_object_get_int64(value64_obj);
> +		break;
> +	}
>  	default:
> +		if (!p->size) {
> +			json_object *value_obj;
> +
> +			if (json_object_object_get_ex(v4l2_ext_control_obj, "value", &value_obj))
> +				p->value = json_object_get_int64(value_obj);

The json library integers don't map to exactly what we need but should
this be just a plain int since value is __s32?

> +		}
>  		break;
>  	}
> 
> diff --git a/utils/v4l2-tracer/trace.cpp b/utils/v4l2-tracer/trace.cpp
> index f0bd7002..e26d6e6d 100644
> --- a/utils/v4l2-tracer/trace.cpp
> +++ b/utils/v4l2-tracer/trace.cpp
> @@ -405,9 +405,19 @@ void trace_v4l2_ext_control(void *arg, json_object *parent_obj, std::string key_
>  	case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
>  		trace_v4l2_ctrl_mpeg2_quantisation_gen(p->p_mpeg2_quantisation, v4l2_ext_control_obj);
>  		break;
> +	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
> +	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
> +	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
> +	case V4L2_CID_PIXEL_RATE:
> +		json_object_object_add(v4l2_ext_control_obj, "value64", json_object_new_uint64(p->value64));
> +		break;
>  	default:
> -		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
> -		fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
> +		if (p->size) {
> +			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
> +			fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
> +		} else {
> +			json_object_object_add(v4l2_ext_control_obj, "value", json_object_new_uint64(p->value));

Same here I'm thinking to change uint64 to just int?

> +		}
>  		break;
>  	}
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCHv2] v4l2-tracer: add support for most basic controls
  2022-11-14 18:44 [PATCH v3] utils: add v4l2-tracer utility Deborah Brouwer
  2022-11-15 11:36 ` [PATCH] v4l2-tracer: add support for most basic controls Hans Verkuil
@ 2022-11-16  7:59 ` Hans Verkuil
  1 sibling, 0 replies; 4+ messages in thread
From: Hans Verkuil @ 2022-11-16  7:59 UTC (permalink / raw)
  To: Deborah Brouwer
  Cc: daniel.almeida, nfraprado, nicolas.dufresne, deborahbrouwer3563,
	linux-media

The v4l2-tracer utility didn't support tracing simple controls without
a payload (i.e. the 'size' field is 0) and just a simple value.

This adds support for that, plus the four standard INTEGER64 controls
that use field value64.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
---
Changes since v1:

- Use int64 instead of uint64 for "value64" (copy-paste error).
- Use int instead of int64 for "value".
---
diff --git a/utils/v4l2-tracer/retrace.cpp b/utils/v4l2-tracer/retrace.cpp
index b736e835..efed96e8 100644
--- a/utils/v4l2-tracer/retrace.cpp
+++ b/utils/v4l2-tracer/retrace.cpp
@@ -734,7 +734,23 @@ struct v4l2_ext_control *retrace_v4l2_ext_control(json_object *parent_obj, int c
 		p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
 		                                          v4l2_stateless_hevc_start_code_val_def);
 		break;
+	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
+	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
+	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
+	case V4L2_CID_PIXEL_RATE: {
+		json_object *value64_obj;
+
+		if (json_object_object_get_ex(v4l2_ext_control_obj, "value64", &value64_obj))
+			p->value64 = json_object_get_int64(value64_obj);
+		break;
+	}
 	default:
+		if (!p->size) {
+			json_object *value_obj;
+
+			if (json_object_object_get_ex(v4l2_ext_control_obj, "value", &value_obj))
+				p->value = json_object_get_int(value_obj);
+		}
 		break;
 	}

diff --git a/utils/v4l2-tracer/trace.cpp b/utils/v4l2-tracer/trace.cpp
index f0bd7002..ce14538b 100644
--- a/utils/v4l2-tracer/trace.cpp
+++ b/utils/v4l2-tracer/trace.cpp
@@ -405,9 +405,19 @@ void trace_v4l2_ext_control(void *arg, json_object *parent_obj, std::string key_
 	case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
 		trace_v4l2_ctrl_mpeg2_quantisation_gen(p->p_mpeg2_quantisation, v4l2_ext_control_obj);
 		break;
+	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
+	case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
+	case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
+	case V4L2_CID_PIXEL_RATE:
+		json_object_object_add(v4l2_ext_control_obj, "value64", json_object_new_int64(p->value64));
+		break;
 	default:
-		fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
-		fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
+		if (p->size) {
+			fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__);
+			fprintf(stderr, "warning: cannot trace control: %s\n", val2s(p->id, control_val_def).c_str());
+		} else {
+			json_object_object_add(v4l2_ext_control_obj, "value", json_object_new_int(p->value));
+		}
 		break;
 	}


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-16  7:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-14 18:44 [PATCH v3] utils: add v4l2-tracer utility Deborah Brouwer
2022-11-15 11:36 ` [PATCH] v4l2-tracer: add support for most basic controls Hans Verkuil
2022-11-16  5:04   ` Deborah Brouwer
2022-11-16  7:59 ` [PATCHv2] " Hans Verkuil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).