]> granicus.if.org Git - strace/blobdiff - v4l2.c
Remove XLAT_END
[strace] / v4l2.c
diff --git a/v4l2.c b/v4l2.c
index 097b13ac8c2241ce64cc0f063e6d87052468f226..afa5d443412f67cb547ffd09dbcb84ecc47bac72 100644 (file)
--- a/v4l2.c
+++ b/v4l2.c
@@ -2,37 +2,24 @@
  * Copyright (c) 2014 Philippe De Muyter <phdm@macqel.be>
  * Copyright (c) 2014 William Manley <will@williammanley.net>
  * Copyright (c) 2011 Peter Zotov <whitequark@whitequark.org>
- * Copyright (c) 2014-2017 The strace developers.
+ * Copyright (c) 2014-2019 The strace developers.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
  */
 
 #include "defs.h"
 
+#include <stdint.h>
+#include <linux/ioctl.h>
+#include <linux/types.h>
+#include <linux/videodev2.h>
+
 #include DEF_MPERS_TYPE(struct_v4l2_buffer)
 #include DEF_MPERS_TYPE(struct_v4l2_clip)
-#include DEF_MPERS_TYPE(struct_v4l2_create_buffers)
+#ifdef VIDIOC_CREATE_BUFS
+# include DEF_MPERS_TYPE(struct_v4l2_create_buffers)
+#endif
 #include DEF_MPERS_TYPE(struct_v4l2_ext_control)
 #include DEF_MPERS_TYPE(struct_v4l2_ext_controls)
 #include DEF_MPERS_TYPE(struct_v4l2_format)
 #include DEF_MPERS_TYPE(struct_v4l2_input)
 #include DEF_MPERS_TYPE(struct_v4l2_standard)
 
-#include <stdint.h>
-#include <linux/ioctl.h>
-#include <linux/types.h>
-#include <linux/videodev2.h>
-
 typedef struct v4l2_buffer struct_v4l2_buffer;
 typedef struct v4l2_clip struct_v4l2_clip;
+#ifdef VIDIOC_CREATE_BUFS
 typedef struct v4l2_create_buffers struct_v4l2_create_buffers;
+#endif
 typedef struct v4l2_ext_control struct_v4l2_ext_control;
 typedef struct v4l2_ext_controls struct_v4l2_ext_controls;
 typedef struct v4l2_format struct_v4l2_format;
@@ -58,16 +42,12 @@ typedef struct v4l2_standard struct_v4l2_standard;
 #include MPERS_DEFS
 
 #include "print_fields.h"
+#include "print_utils.h"
+#include "xstring.h"
 
-/* some historical constants */
-#ifndef V4L2_CID_HCENTER
-#define V4L2_CID_HCENTER (V4L2_CID_BASE+22)
-#endif
-#ifndef V4L2_CID_VCENTER
-#define V4L2_CID_VCENTER (V4L2_CID_BASE+23)
-#endif
-#ifndef V4L2_CID_BAND_STOP_FILTER
-#define V4L2_CID_BAND_STOP_FILTER (V4L2_CID_BASE+33)
+/* v4l2_fourcc_be was added by Linux commit v3.18-rc1~101^2^2~127 */
+#ifndef v4l2_fourcc_be
+# define v4l2_fourcc_be(a, b, c, d) (v4l2_fourcc(a, b, c, d) | (1U << 31))
 #endif
 
 #define FMT_FRACT "%u/%u"
@@ -76,29 +56,24 @@ typedef struct v4l2_standard struct_v4l2_standard;
 #define FMT_RECT "{left=%d, top=%d, width=%u, height=%u}"
 #define ARGS_RECT(x) (x).left, (x).top, (x).width, (x).height
 
+#include "xlat/v4l2_pix_fmts.h"
+#include "xlat/v4l2_sdr_fmts.h"
+
 static void
-print_pixelformat(uint32_t fourcc)
+print_pixelformat(uint32_t fourcc, const struct xlat *xlat)
 {
-       const union {
-               uint32_t pixelformat;
-               unsigned char cc[sizeof(uint32_t)];
-       } u = {
-#if WORDS_BIGENDIAN
-               .cc = {
-                       (unsigned char) (fourcc >> 24),
-                       (unsigned char) (fourcc >> 16),
-                       (unsigned char) (fourcc >> 8),
-                       (unsigned char) fourcc
-               }
-#else
-               .pixelformat = fourcc
-#endif
+       unsigned char a[] = {
+               (unsigned char) fourcc,
+               (unsigned char) (fourcc >> 8),
+               (unsigned char) (fourcc >> 16),
+               (unsigned char) (fourcc >> 24),
        };
        unsigned int i;
 
        tprints("v4l2_fourcc(");
-       for (i = 0; i < sizeof(u.cc); ++i) {
-               unsigned char c = u.cc[i];
+       /* Generic char array printing routine.  */
+       for (i = 0; i < ARRAY_SIZE(a); ++i) {
+               unsigned char c = a[i];
 
                if (i)
                        tprints(", ");
@@ -111,7 +86,7 @@ print_pixelformat(uint32_t fourcc)
                                '\0'
                        };
                        tprints(sym);
-               } else if (c >= ' ' && c <= 0x7e) {
+               } else if (is_print(c)) {
                        char sym[] = {
                                '\'',
                                c,
@@ -121,18 +96,20 @@ print_pixelformat(uint32_t fourcc)
                        tprints(sym);
                } else {
                        char hex[] = {
-                               '\'',
-                               '\\',
-                               'x',
-                               "0123456789abcdef"[c >> 4],
-                               "0123456789abcdef"[c & 0xf],
-                               '\'',
+                               BYTE_HEX_CHARS_PRINTF_QUOTED(c),
                                '\0'
                        };
                        tprints(hex);
                }
        }
        tprints(")");
+
+       if (xlat) {
+               const char *pixfmt_name = xlookup(xlat, fourcc);
+
+               if (pixfmt_name)
+                       tprints_comment(pixfmt_name);
+       }
 }
 
 #include "xlat/v4l2_device_capabilities_flags.h"
@@ -156,7 +133,7 @@ print_v4l2_capability(struct tcb *const tcp, const kernel_ulong_t arg)
                caps.version & 0xFF);
        printflags(v4l2_device_capabilities_flags, caps.capabilities,
                   "V4L2_CAP_???");
-#ifdef V4L2_CAP_DEVICE_CAPS
+#ifdef HAVE_STRUCT_V4L2_CAPABILITY_DEVICE_CAPS
        tprints(", device_caps=");
        printflags(v4l2_device_capabilities_flags, caps.device_caps,
                   "V4L2_CAP_???");
@@ -188,7 +165,7 @@ print_v4l2_fmtdesc(struct tcb *const tcp, const kernel_ulong_t arg)
                           "V4L2_FMT_FLAG_???");
                PRINT_FIELD_CSTRING(", ", f, description);
                tprints(", pixelformat=");
-               print_pixelformat(f.pixelformat);
+               print_pixelformat(f.pixelformat, v4l2_pix_fmts);
        }
        tprints("}");
        return RVAL_IOCTL_DECODED;
@@ -218,7 +195,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                tprints(prefix);
                tprintf("fmt.pix={width=%u, height=%u, pixelformat=",
                        f->fmt.pix.width, f->fmt.pix.height);
-               print_pixelformat(f->fmt.pix.pixelformat);
+               print_pixelformat(f->fmt.pix.pixelformat, v4l2_pix_fmts);
                tprints(", field=");
                printxval(v4l2_fields, f->fmt.pix.field, "V4L2_FIELD_???");
                tprintf(", bytesperline=%u, sizeimage=%u, colorspace=",
@@ -227,7 +204,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                          "V4L2_COLORSPACE_???");
                tprints("}");
                break;
-#if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
+#if HAVE_STRUCT_V4L2_FORMAT_FMT_PIX_MP
        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: {
                unsigned int i, max;
@@ -235,7 +212,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                tprints(prefix);
                tprintf("fmt.pix_mp={width=%u, height=%u, pixelformat=",
                        f->fmt.pix_mp.width, f->fmt.pix_mp.height);
-               print_pixelformat(f->fmt.pix_mp.pixelformat);
+               print_pixelformat(f->fmt.pix_mp.pixelformat, v4l2_pix_fmts);
                tprints(", field=");
                printxval(v4l2_fields, f->fmt.pix_mp.field, "V4L2_FIELD_???");
                tprints(", colorspace=");
@@ -258,9 +235,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
        }
 #endif
        /* OUTPUT_OVERLAY since Linux v2.6.22-rc1~1118^2~179 */
-#if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
        case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
-#endif
        case V4L2_BUF_TYPE_VIDEO_OVERLAY: {
                struct_v4l2_clip clip;
                tprints(prefix);
@@ -270,7 +245,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                tprintf(", chromakey=%#x, clips=", f->fmt.win.chromakey);
                ret = print_array(tcp, ptr_to_kulong(f->fmt.win.clips),
                                  f->fmt.win.clipcount, &clip, sizeof(clip),
-                                 umoven_or_printaddr, print_v4l2_clip, 0);
+                                 tfetch_mem, print_v4l2_clip, 0);
                tprintf(", clipcount=%u, bitmap=", f->fmt.win.clipcount);
                printaddr(ptr_to_kulong(f->fmt.win.bitmap));
 #ifdef HAVE_STRUCT_V4L2_WINDOW_GLOBAL_ALPHA
@@ -286,7 +261,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                        "samples_per_line=%u, sample_format=",
                        f->fmt.vbi.sampling_rate, f->fmt.vbi.offset,
                        f->fmt.vbi.samples_per_line);
-               print_pixelformat(f->fmt.vbi.sample_format);
+               print_pixelformat(f->fmt.vbi.sample_format, v4l2_pix_fmts);
                tprintf(", start=[%u, %u], count=[%u, %u], ",
                        f->fmt.vbi.start[0], f->fmt.vbi.start[1],
                        f->fmt.vbi.count[0], f->fmt.vbi.count[1]);
@@ -295,7 +270,7 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                tprints("}");
                break;
        /* both since Linux v2.6.14-rc2~64 */
-#if HAVE_DECL_V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
+#if HAVE_STRUCT_V4L2_FORMAT_FMT_SLICED
        case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
        case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: {
                unsigned int i, j;
@@ -324,20 +299,18 @@ print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
                break;
        }
 #endif
+#if HAVE_STRUCT_V4L2_FORMAT_FMT_SDR
        /* since Linux v4.4-rc1~118^2~14 */
-#if HAVE_DECL_V4L2_BUF_TYPE_SDR_OUTPUT
        case V4L2_BUF_TYPE_SDR_OUTPUT:
-#endif
        /* since Linux v3.15-rc1~85^2~213 */
-#if HAVE_DECL_V4L2_BUF_TYPE_SDR_CAPTURE
        case V4L2_BUF_TYPE_SDR_CAPTURE:
                tprints(prefix);
                tprints("fmt.sdr={pixelformat=");
-               print_pixelformat(f->fmt.sdr.pixelformat);
-#ifdef HAVE_STRUCT_V4L2_SDR_FORMAT_BUFFERSIZE
+               print_pixelformat(f->fmt.sdr.pixelformat, v4l2_sdr_fmts);
+# ifdef HAVE_STRUCT_V4L2_SDR_FORMAT_BUFFERSIZE
                tprintf(", buffersize=%u",
                        f->fmt.sdr.buffersize);
-#endif
+# endif
                tprints("}");
                break;
 #endif
@@ -613,8 +586,42 @@ print_v4l2_input(struct tcb *const tcp, const kernel_ulong_t arg)
        return RVAL_IOCTL_DECODED;
 }
 
+/*
+ * We include it here and not before print_v4l2_ext_controls as we need
+ * V4L2_CTRL_CLASS_* definitions for V4L2_CID_*_BASE ones.
+ */
+#include "xlat/v4l2_control_classes.h"
+#include "xlat/v4l2_control_id_bases.h"
 #include "xlat/v4l2_control_ids.h"
 
+static void
+print_v4l2_cid(const uint32_t cid)
+{
+       const char *id_name = xlookup(v4l2_control_ids, cid);
+
+       if (id_name) {
+               print_xlat_ex(cid, id_name, XLAT_STYLE_DEFAULT);
+               return;
+       }
+
+       uint64_t class_id = cid;
+       const char *class_str = xlookup_le(v4l2_control_classes, &class_id);
+
+       if (!class_str || (cid - class_id) >= 0x10000) {
+               print_xlat_ex(cid, "V4L2_CID_???", PXF_DEFAULT_STR);
+               return;
+       }
+
+       char *tmp_str;
+
+       if (asprintf(&tmp_str, "%s+%#" PRIx64,
+                    class_str, cid - class_id) < 0)
+               tmp_str = NULL;
+
+       print_xlat_ex(cid, tmp_str, XLAT_STYLE_DEFAULT);
+       free(tmp_str);
+}
+
 static int
 print_v4l2_control(struct tcb *const tcp, const kernel_ulong_t arg,
                   const bool is_get)
@@ -625,8 +632,9 @@ print_v4l2_control(struct tcb *const tcp, const kernel_ulong_t arg,
                tprints(", ");
                if (umove_or_printaddr(tcp, arg, &c))
                        return RVAL_IOCTL_DECODED;
+
                tprints("{id=");
-               printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
+               print_v4l2_cid(c.id);
                if (!is_get)
                        tprintf(", value=%d", c.value);
                return 0;
@@ -718,7 +726,8 @@ print_v4l2_queryctrl(struct tcb *const tcp, const kernel_ulong_t arg)
                const unsigned long next = c.id & V4L2_CTRL_FLAG_NEXT_CTRL;
                set_tcb_priv_ulong(tcp, next);
                if (next) {
-                       tprints("V4L2_CTRL_FLAG_NEXT_CTRL|");
+                       print_xlat(V4L2_CTRL_FLAG_NEXT_CTRL);
+                       tprints("|");
                        c.id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
                }
 #endif
@@ -800,7 +809,7 @@ print_v4l2_ext_control(struct tcb *tcp, void *elem_buf, size_t elem_size, void *
 
        tprints("{id=");
        printxval(v4l2_control_ids, p->id, "V4L2_CID_???");
-# if HAVE_DECL_V4L2_CTRL_TYPE_STRING
+# if HAVE_STRUCT_V4L2_EXT_CONTROL_STRING
        tprintf(", size=%u", p->size);
        if (p->size > 0) {
                tprints(", string=");
@@ -813,8 +822,6 @@ print_v4l2_ext_control(struct tcb *tcp, void *elem_buf, size_t elem_size, void *
        return true;
 }
 
-#include "xlat/v4l2_control_classes.h"
-
 static int
 print_v4l2_ext_controls(struct tcb *const tcp, const kernel_ulong_t arg,
                        const bool is_get)
@@ -848,7 +855,7 @@ print_v4l2_ext_controls(struct tcb *const tcp, const kernel_ulong_t arg,
        struct_v4l2_ext_control ctrl;
        bool fail = !print_array(tcp, ptr_to_kulong(c.controls), c.count,
                                 &ctrl, sizeof(ctrl),
-                                umoven_or_printaddr_ignore_syserror,
+                                tfetch_mem_ignore_syserror,
                                 print_v4l2_ext_control, 0);
 
        if (exiting(tcp) && syserror(tcp))
@@ -877,7 +884,7 @@ print_v4l2_frmsizeenum(struct tcb *const tcp, const kernel_ulong_t arg)
                if (umove_or_printaddr(tcp, arg, &s))
                        return RVAL_IOCTL_DECODED;
                tprintf("{index=%u, pixel_format=", s.index);
-               print_pixelformat(s.pixel_format);
+               print_pixelformat(s.pixel_format, v4l2_pix_fmts);
                return 0;
        }
 
@@ -917,7 +924,7 @@ print_v4l2_frmivalenum(struct tcb *const tcp, const kernel_ulong_t arg)
                if (umove_or_printaddr(tcp, arg, &f))
                        return RVAL_IOCTL_DECODED;
                tprintf("{index=%u, pixel_format=", f.index);
-               print_pixelformat(f.pixel_format);
+               print_pixelformat(f.pixel_format, v4l2_pix_fmts);
                tprintf(", width=%u, height=%u", f.width, f.height);
                return 0;
        }
@@ -975,7 +982,7 @@ print_v4l2_create_buffers(struct tcb *const tcp, const kernel_ulong_t arg)
        if (syserror(tcp) || umove(tcp, arg, &b))
                return RVAL_IOCTL_DECODED;
 
-       sprintf(outstr, fmt, b.index, b.count);
+       xsprintf(outstr, fmt, b.index, b.count);
        tcp->auxstr = outstr;
 
        return RVAL_IOCTL_DECODED | RVAL_STR;
@@ -1011,7 +1018,7 @@ MPERS_PRINTER_DECL(int, v4l2_ioctl, struct tcb *const tcp,
        case VIDIOC_G_FBUF: /* R */
                if (entering(tcp))
                        return 0;
-               /* fall through */
+               ATTRIBUTE_FALLTHROUGH;
        case VIDIOC_S_FBUF: /* W */
                return print_v4l2_framebuffer(tcp, arg);
 
@@ -1026,7 +1033,7 @@ MPERS_PRINTER_DECL(int, v4l2_ioctl, struct tcb *const tcp,
        case VIDIOC_G_STD: /* R */
                if (entering(tcp))
                        return 0;
-               /* fall through */
+               ATTRIBUTE_FALLTHROUGH;
        case VIDIOC_S_STD: /* W */
                tprints(", ");
                printnum_int64(tcp, arg, "%#" PRIx64);
@@ -1052,7 +1059,7 @@ MPERS_PRINTER_DECL(int, v4l2_ioctl, struct tcb *const tcp,
        case VIDIOC_G_INPUT: /* R */
                if (entering(tcp))
                        return 0;
-               /* fall through */
+               ATTRIBUTE_FALLTHROUGH;
        case VIDIOC_S_INPUT: /* RW */
                tprints(", ");
                printnum_int(tcp, arg, "%u");