Debian Codegraph @

utils/v4l2-tracer/retrace.cpp

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright 2022 Collabora Ltd.
4 */
5
6 #include "retrace.h"
7
8 extern struct retrace_context ctx_retrace;
9
10 void retrace_mmap(json_object *mmap_obj, bool is_mmap64)
11 {
12 json_object *mmap_args_obj;
13 if (is_mmap64)
14 json_object_object_get_ex(mmap_obj, "mmap64", &mmap_args_obj);
15 else
16 json_object_object_get_ex(mmap_obj, "mmap", &mmap_args_obj);
17
18 json_object *len_obj;
19 json_object_object_get_ex(mmap_args_obj, "len", &len_obj);
20 size_t len = (size_t) json_object_get_int(len_obj);
21
22 json_object *prot_obj;
23 json_object_object_get_ex(mmap_args_obj, "prot", &prot_obj);
24 int prot = json_object_get_int(prot_obj);
25
26 json_object *flags_obj;
27 json_object_object_get_ex(mmap_args_obj, "flags", &flags_obj);
28 int flags = s2number(json_object_get_string(flags_obj));
29
30 json_object *fildes_obj;
31 json_object_object_get_ex(mmap_args_obj, "fildes", &fildes_obj);
32 int fd_trace = json_object_get_int(fildes_obj);
33
34 json_object *off_obj;
35 json_object_object_get_ex(mmap_args_obj, "off", &off_obj);
36 off_t off = (off_t) json_object_get_int64(off_obj);
37
38 int fd_retrace = get_fd_retrace_from_fd_trace(fd_trace);
39 if (fd_retrace < 0) {
40 line_info("\n\tBad or missing file descriptor.");
41 return;
42 }
43
44 /* Only retrace mmap calls that map a buffer. */
45 if (!buffer_in_retrace_context(fd_retrace, off))
46 return;
47
48 void *buf_address_retrace_pointer = nullptr;
49 if (is_mmap64)
50 buf_address_retrace_pointer = mmap64(0, len, prot, flags, fd_retrace, off);
51 else
52 buf_address_retrace_pointer = mmap(0, len, prot, flags, fd_retrace, off);
53
54 if (buf_address_retrace_pointer == MAP_FAILED) {
55 if (is_mmap64)
56 perror("mmap64");
57 else
58 perror("mmap");
59 debug_line_info();
60 print_context();
61 exit(EXIT_FAILURE);
62 }
63
64 /*
65 * Get and store the original trace address so that it can be matched
66 * with the munmap address later.
67 */
68 json_object *buffer_address_obj;
69 json_object_object_get_ex(mmap_obj, "buffer_address", &buffer_address_obj);
70 long buf_address_trace = json_object_get_int64(buffer_address_obj);
71 set_buffer_address_retrace(fd_retrace, off, buf_address_trace,
72 (long) buf_address_retrace_pointer);
73
74 if (is_verbose() || (errno != 0)) {
75 fprintf(stderr, "fd: %d, offset: %lld, ", fd_retrace, (long long)off);
76 if (is_mmap64)
77 perror("mmap64");
78 else
79 perror("mmap");
80
81 debug_line_info();
82 print_context();
83 }
84 }
85
86 void retrace_munmap(json_object *syscall_obj)
87 {
88 json_object *munmap_args_obj;
89 json_object_object_get_ex(syscall_obj, "munmap", &munmap_args_obj);
90
91 json_object *start_obj;
92 json_object_object_get_ex(munmap_args_obj, "start", &start_obj);
93 long start = json_object_get_int64(start_obj);
94
95 json_object *length_obj;
96 json_object_object_get_ex(munmap_args_obj, "length", &length_obj);
97 size_t length = (size_t) json_object_get_int(length_obj);
98
99 long buffer_address_retrace = get_retrace_address_from_trace_address(start);
100
101 if (buffer_address_retrace < 0)
102 return;
103
104 munmap((void *)buffer_address_retrace, length);
105
106 if (is_verbose() || (errno != 0)) {
107 fprintf(stderr, "unmapped: %ld, ", buffer_address_retrace);
108 perror("munmap");
109 }
110 }
111
112 void retrace_open(json_object *jobj, bool is_open64)
113 {
114 json_object *fd_trace_obj;
115 json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
116 int fd_trace = json_object_get_int(fd_trace_obj);
117
118 json_object *open_args_obj;
119 if (is_open64)
120 json_object_object_get_ex(jobj, "open64", &open_args_obj);
121 else
122 json_object_object_get_ex(jobj, "open", &open_args_obj);
123
124 json_object *path_obj;
125 std::string path_trace;
126 json_object_object_get_ex(open_args_obj, "path", &path_obj);
127 if (json_object_get_string(path_obj) != nullptr)
128 path_trace = json_object_get_string(path_obj);
129 std::string path_retrace = get_path_retrace_from_path_trace(path_trace, jobj);
130
131 /*
132 * Don't fail if a retrace path can't be found.
133 * Try using the same path as in the trace file.
134 */
135 if (path_retrace.empty()) {
136 line_info("\n\tWarning: can't find retrace device.\
137 \n\tAttempting to use: %s", path_trace.c_str());
138 path_retrace = path_trace;
139 }
140
141 json_object *oflag_obj;
142 json_object_object_get_ex(open_args_obj, "oflag", &oflag_obj);
143 int oflag = s2val(json_object_get_string(oflag_obj), open_val_def);
144
145 int mode = 0;
146 json_object *mode_obj;
147 if (json_object_object_get_ex(open_args_obj, "mode", &mode_obj))
148 mode = s2number(json_object_get_string(mode_obj));
149
150 int fd_retrace = 0;
151 if (is_open64)
152 fd_retrace = open64(path_retrace.c_str(), oflag, mode);
153 else
154 fd_retrace = open(path_retrace.c_str(), oflag, mode);
155
156 if (fd_retrace <= 0) {
157 line_info("\n\tCan't open: %s", path_retrace.c_str());
158 exit(fd_retrace);
159 }
160
161 add_fd(fd_trace, fd_retrace);
162
163 if (is_verbose() || errno != 0) {
164 fprintf(stderr, "path: %s ", path_retrace.c_str());
165 if (is_open64)
166 perror("open64");
167 else
168 perror("open");
169 debug_line_info();
170 print_context();
171 }
172 }
173
174 void retrace_close(json_object *jobj)
175 {
176 json_object *fd_trace_obj;
177 json_object_object_get_ex(jobj, "fd", &fd_trace_obj);
178 int fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
179
180 /* Only close devices that were opened in the retrace context. */
181 if (fd_retrace < 0)
182 return;
183
184 close(fd_retrace);
185 ctx_retrace.retrace_fds.erase(json_object_get_int(fd_trace_obj));
186
187 if (is_verbose() || (errno != 0)) {
188 fprintf(stderr, "fd: %d ", fd_retrace);
189 perror("close");
190 debug_line_info();
191 print_context();
192 }
193 }
194
195 void retrace_vidioc_reqbufs(int fd_retrace, json_object *ioctl_args)
196 {
197 struct v4l2_requestbuffers *ptr = retrace_v4l2_requestbuffers_gen(ioctl_args);
198 ioctl(fd_retrace, VIDIOC_REQBUFS, ptr);
199 free(ptr);
200 }
201
202 struct v4l2_plane *retrace_v4l2_plane(json_object *plane_obj, __u32 memory)
203 {
204 struct v4l2_plane *ptr = (struct v4l2_plane *) calloc(1, sizeof(v4l2_plane));
205
206 json_object *bytesused_obj;
207 json_object_object_get_ex(plane_obj, "bytesused", &bytesused_obj);
208 ptr->bytesused = (__u32) json_object_get_int64(bytesused_obj);
209
210 json_object *length_obj;
211 json_object_object_get_ex(plane_obj, "length", &length_obj);
212 ptr->length = (__u32) json_object_get_int64(length_obj);
213
214 json_object *m_obj;
215 json_object_object_get_ex(plane_obj, "m", &m_obj);
216 if (memory == V4L2_MEMORY_MMAP) {
217 json_object *mem_offset_obj;
218 json_object_object_get_ex(m_obj, "mem_offset", &mem_offset_obj);
219 ptr->m.mem_offset = (__u32) json_object_get_int64(mem_offset_obj);
220 }
221
222 json_object *data_offset_obj;
223 json_object_object_get_ex(plane_obj, "data_offset", &data_offset_obj);
224 ptr->data_offset = (__u32) json_object_get_int64(data_offset_obj);
225
226 return ptr;
227 }
228
229 struct v4l2_buffer *retrace_v4l2_buffer(json_object *ioctl_args)
230 {
231 struct v4l2_buffer *buf = (struct v4l2_buffer *) calloc(1, sizeof(struct v4l2_buffer));
232
233 json_object *buf_obj;
234 json_object_object_get_ex(ioctl_args, "v4l2_buffer", &buf_obj);
235
236 json_object *index_obj;
237 json_object_object_get_ex(buf_obj, "index", &index_obj);
238 buf->index = (__u32) json_object_get_uint64(index_obj);
239
240 json_object *type_obj;
241 json_object_object_get_ex(buf_obj, "type", &type_obj);
242 buf->type = s2val(json_object_get_string(type_obj), v4l2_buf_type_val_def);
243
244 json_object *bytesused_obj;
245 json_object_object_get_ex(buf_obj, "bytesused", &bytesused_obj);
246 buf->bytesused = (__u32) json_object_get_uint64(bytesused_obj);
247
248 json_object *flags_obj;
249 json_object_object_get_ex(buf_obj, "flags", &flags_obj);
250 buf->flags = (__u32) s2flags_buffer(json_object_get_string(flags_obj));
251
252 json_object *field_obj;
253 json_object_object_get_ex(buf_obj, "field", &field_obj);
254 buf->field = (__u32) s2val(json_object_get_string(field_obj), v4l2_field_val_def);
255
256 json_object *timestamp_obj;
257 json_object_object_get_ex(buf_obj, "timestamp", &timestamp_obj);
258
259 struct timeval tval = {};
260 json_object *tv_sec_obj;
261 json_object_object_get_ex(timestamp_obj, "tv_sec", &tv_sec_obj);
262 tval.tv_sec = json_object_get_int64(tv_sec_obj);
263 json_object *tv_usec_obj;
264 json_object_object_get_ex(timestamp_obj, "tv_usec", &tv_usec_obj);
265 tval.tv_usec = json_object_get_int64(tv_usec_obj);
266 buf->timestamp = tval;
267
268 json_object *sequence_obj;
269 json_object_object_get_ex(buf_obj, "sequence", &sequence_obj);
270 buf->sequence = (__u32) json_object_get_uint64(sequence_obj);
271
272 json_object *memory_obj;
273 json_object_object_get_ex(buf_obj, "memory", &memory_obj);
274 buf->memory = s2val(json_object_get_string(memory_obj), v4l2_memory_val_def);
275
276 json_object *length_obj;
277 json_object_object_get_ex(buf_obj, "length", &length_obj);
278 buf->length = (__u32) json_object_get_uint64(length_obj);
279
280 json_object *m_obj;
281 json_object_object_get_ex(buf_obj, "m", &m_obj);
282
283 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
284 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
285 json_object *planes_obj;
286 json_object_object_get_ex(m_obj, "planes", &planes_obj);
287 /* TODO add planes > 0 */
288 json_object *plane_obj = json_object_array_get_idx(planes_obj, 0);
289 buf->m.planes = retrace_v4l2_plane(plane_obj, buf->memory);
290 }
291
292 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
293 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
294 if (buf->memory == V4L2_MEMORY_MMAP) {
295 json_object *offset_obj;
296 json_object_object_get_ex(m_obj, "offset", &offset_obj);
297 buf->m.offset = (__u32) json_object_get_uint64(offset_obj);
298 }
299 }
300
301 if ((buf->flags & V4L2_BUF_FLAG_REQUEST_FD) != 0U) {
302 json_object *request_fd_obj;
303 json_object_object_get_ex(buf_obj, "request_fd", &request_fd_obj);
304 buf->request_fd = (__s32) get_fd_retrace_from_fd_trace(json_object_get_int(request_fd_obj));
305 if (buf->request_fd < 0)
306 line_info("\n\tBad or missing file descriptor.\n");
307 }
308
309 return buf;
310 }
311
312 void retrace_vidioc_querybuf(int fd_retrace, json_object *ioctl_args)
313 {
314 struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args);
315
316 ioctl(fd_retrace, VIDIOC_QUERYBUF, buf);
317
318 if (buf->memory == V4L2_MEMORY_MMAP) {
319 __u32 offset = 0;
320 if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
321 (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT))
322 offset = buf->m.offset;
323 if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) ||
324 (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
325 offset = buf->m.planes->m.mem_offset;
326 if (get_buffer_fd_retrace(buf->type, buf->index) == -1)
327 add_buffer_retrace(fd_retrace, buf->type, buf->index, offset);
328 }
329
330 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
331 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
332 if (buf->m.planes != nullptr) {
333 free(buf->m.planes);
334 }
335 }
336
337 if (is_verbose() || (errno != 0)) {
338 fprintf(stderr, "%s, index: %d, fd: %d, ",
339 val2s(buf->type, v4l2_buf_type_val_def).c_str(),
340 buf->index, fd_retrace);
341 perror("VIDIOC_QUERYBUF");
342 debug_line_info();
343 print_context();
344 }
345
346 free(buf);
347 }
348
349 void retrace_vidioc_qbuf(int fd_retrace, json_object *ioctl_args)
350 {
351 struct v4l2_buffer *ptr = retrace_v4l2_buffer(ioctl_args);
352
353 ioctl(fd_retrace, VIDIOC_QBUF, ptr);
354
355 if (ptr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
356 ptr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
357 if (ptr->m.planes != nullptr) {
358 free(ptr->m.planes);
359 }
360 }
361
362 if (is_verbose() || (errno != 0)) {
363 fprintf(stderr, "%s, index: %d, fd: %d, ",
364 val2s(ptr->type, v4l2_buf_type_val_def).c_str(),
365 ptr->index, fd_retrace);
366 perror("VIDIOC_QBUF");
367 debug_line_info();
368 print_context();
369 }
370
371 free(ptr);
372 }
373
374 void retrace_vidioc_dqbuf(int fd_retrace, json_object *ioctl_args)
375 {
376 struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args);
377
378 const int poll_timeout_ms = 5000;
379 struct pollfd *pfds = (struct pollfd *) calloc(1, sizeof(struct pollfd));
380 if (pfds == nullptr)
381 exit(EXIT_FAILURE);
382 pfds[0].fd = fd_retrace;
383 pfds[0].events = POLLIN;
384 int ret = poll(pfds, 1, poll_timeout_ms);
385 free(pfds);
386 if (ret == -1) {
387 line_info("\n\tPoll error.");
388 perror("");
389 exit(EXIT_FAILURE);
390 }
391 if (ret == 0) {
392 line_info("\n\tPoll timed out.");
393 exit(EXIT_FAILURE);
394 }
395
396 ioctl(fd_retrace, VIDIOC_DQBUF, buf);
397
398 if (is_verbose() || (errno != 0)) {
399 fprintf(stderr, "%s, index: %d, fd: %d, ",
400 val2s(buf->type, v4l2_buf_type_val_def).c_str(),
401 buf->index, fd_retrace);
402 perror("VIDIOC_DQBUF");
403 debug_line_info();
404 print_context();
405 }
406
407 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
408 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
409 free(buf->m.planes);
410
411 free(buf);
412 }
413
414 void retrace_vidioc_prepare_buf(int fd_retrace, json_object *ioctl_args)
415 {
416 struct v4l2_buffer *buf = retrace_v4l2_buffer(ioctl_args);
417
418 ioctl(fd_retrace, VIDIOC_PREPARE_BUF, buf);
419
420 if (is_verbose() || (errno != 0)) {
421 fprintf(stderr, "%s, index: %d, fd: %d, ",
422 val2s(buf->type, v4l2_buf_type_val_def).c_str(),
423 buf->index, fd_retrace);
424 perror("VIDIOC_PREPARE_BUF");
425 debug_line_info();
426 print_context();
427 }
428
429 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
430 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
431 free(buf->m.planes);
432
433 free(buf);
434 }
435
436 void retrace_vidioc_create_bufs(int fd_retrace, json_object *ioctl_args)
437 {
438 struct v4l2_create_buffers *ptr = retrace_v4l2_create_buffers_gen(ioctl_args);
439 ioctl(fd_retrace, VIDIOC_CREATE_BUFS, ptr);
440
441 if (is_verbose() || (errno != 0)) {
442 perror("VIDIOC_CREATE_BUFS");
443 debug_line_info();
444 print_context();
445 }
446
447 free(ptr);
448 }
449
450 void retrace_vidioc_expbuf(int fd_retrace, json_object *ioctl_args)
451 {
452 struct v4l2_exportbuffer *ptr = retrace_v4l2_exportbuffer_gen(ioctl_args);
453 ioctl(fd_retrace, VIDIOC_EXPBUF, ptr);
454
455 int buf_fd_retrace = ptr->fd;
456
457 /*
458 * If a buffer was previously added to the retrace context using the video device
459 * file descriptor, replace the video fd with the more specific buffer fd from EXPBUF.
460 */
461 int fd_found_in_retrace_context = get_buffer_fd_retrace(ptr->type, ptr->index);
462 if (fd_found_in_retrace_context != -1)
463 remove_buffer_retrace(ptr->type, ptr->index);
464
465 add_buffer_retrace(buf_fd_retrace, ptr->type, ptr->index);
466
467 /* Retrace again to associate the original fd with the current buffer fd. */
468 memset(ptr, 0, sizeof(v4l2_exportbuffer));
469 ptr = retrace_v4l2_exportbuffer_gen(ioctl_args);
470 int buf_fd_trace = ptr->fd;
471 add_fd(buf_fd_trace, buf_fd_retrace);
472
473 if (is_verbose() || (errno != 0))
474 perror("VIDIOC_EXPBUF");
475
476 free(ptr);
477 }
478
479 void retrace_vidioc_streamon(int fd_retrace, json_object *ioctl_args)
480 {
481 json_object *type_obj;
482 json_object_object_get_ex(ioctl_args, "type", &type_obj);
483 v4l2_buf_type buf_type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
484 v4l2_buf_type_val_def);
485
486 ioctl(fd_retrace, VIDIOC_STREAMON, &buf_type);
487
488 if (is_verbose() || (errno != 0)) {
489 fprintf(stderr, "%s, ", val2s(buf_type, v4l2_buf_type_val_def).c_str());
490 perror("VIDIOC_STREAMON");
491 }
492 }
493
494 void retrace_vidioc_streamoff(int fd_retrace, json_object *ioctl_args)
495 {
496 json_object *type_obj;
497 json_object_object_get_ex(ioctl_args, "type", &type_obj);
498 v4l2_buf_type buf_type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
499 v4l2_buf_type_val_def);
500
501 ioctl(fd_retrace, VIDIOC_STREAMOFF, &buf_type);
502
503 if (is_verbose() || (errno != 0)) {
504 fprintf(stderr, "%s, ", val2s(buf_type, v4l2_buf_type_val_def).c_str());
505 perror("VIDIOC_STREAMOFF");
506 }
507 }
508
509 void retrace_vidioc_try_fmt(int fd_retrace, json_object *ioctl_args)
510 {
511 struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args);
512 ioctl(fd_retrace, VIDIOC_TRY_FMT, ptr);
513
514 if (is_verbose() || (errno != 0))
515 perror("VIDIOC_TRY_FMT");
516
517 free(ptr);
518 }
519
520 void retrace_vidioc_g_fmt(int fd_retrace, json_object *ioctl_args)
521 {
522 struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args);
523 ioctl(fd_retrace, VIDIOC_G_FMT, ptr);
524
525 if (is_verbose() || (errno != 0))
526 perror("VIDIOC_G_FMT");
527
528 free(ptr);
529 }
530
531 void retrace_vidioc_s_fmt(int fd_retrace, json_object *ioctl_args)
532 {
533 struct v4l2_format *ptr = retrace_v4l2_format_gen(ioctl_args);
534
535 ioctl(fd_retrace, VIDIOC_S_FMT, ptr);
536
537 if (is_verbose() || (errno != 0)) {
538 perror("VIDIOC_S_FMT");
539 }
540
541 free(ptr);
542 }
543
544 struct v4l2_streamparm *retrace_v4l2_streamparm(json_object *parent_obj, std::string key_name = "")
545 {
546 struct v4l2_streamparm *ptr = (struct v4l2_streamparm *) calloc(1, sizeof(v4l2_streamparm));
547
548 json_object *v4l2_streamparm_obj;
549 json_object_object_get_ex(parent_obj, "v4l2_streamparm", &v4l2_streamparm_obj);
550
551 json_object *type_obj;
552 if (json_object_object_get_ex(v4l2_streamparm_obj, "type", &type_obj))
553 ptr->type = (__u32) s2val(json_object_get_string(type_obj), v4l2_buf_type_val_def);
554
555 if ((ptr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || (ptr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
556 ptr->parm.capture = *retrace_v4l2_captureparm_gen(v4l2_streamparm_obj);
557
558 if ((ptr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || (ptr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
559 ptr->parm.output = *retrace_v4l2_outputparm_gen(v4l2_streamparm_obj);
560
561 return ptr;
562 }
563
564 void retrace_vidioc_g_parm (int fd_retrace, json_object *ioctl_args)
565 {
566 struct v4l2_streamparm *ptr = retrace_v4l2_streamparm(ioctl_args);
567 ioctl(fd_retrace, VIDIOC_G_PARM, ptr);
568
569 if (is_verbose() || (errno != 0))
570 perror("VIDIOC_G_PARM");
571
572 free(ptr);
573 }
574
575 void retrace_vidioc_s_parm (int fd_retrace, json_object *ioctl_args)
576 {
577 struct v4l2_streamparm *ptr = retrace_v4l2_streamparm(ioctl_args);
578 ioctl(fd_retrace, VIDIOC_S_PARM, ptr);
579
580 if (is_verbose() || (errno != 0))
581 perror("VIDIOC_S_PARM");
582
583 free(ptr);
584 }
585
586 void retrace_vidioc_queryctrl(int fd_retrace, json_object *ioctl_args)
587 {
588 struct v4l2_queryctrl *ptr = retrace_v4l2_queryctrl_gen(ioctl_args);
589 ioctl(fd_retrace, VIDIOC_QUERYCTRL, ptr);
590
591 if (is_verbose() || (errno != 0))
592 perror("VIDIOC_QUERYCTRL");
593
594 free(ptr);
595 }
596
597 void retrace_vidioc_enuminput(int fd_retrace, json_object *ioctl_args)
598 {
599 struct v4l2_input *ptr = retrace_v4l2_input_gen(ioctl_args);
600 ioctl(fd_retrace, VIDIOC_ENUMINPUT, ptr);
601
602 if (is_verbose() || (errno != 0))
603 perror("VIDIOC_ENUMINPUT");
604
605 free(ptr);
606 }
607
608 void retrace_vidioc_g_control(int fd_retrace, json_object *ioctl_args)
609 {
610 struct v4l2_control *ptr = retrace_v4l2_control_gen(ioctl_args);
611 ioctl(fd_retrace, VIDIOC_G_CTRL, ptr);
612
613 if (is_verbose() || (errno != 0))
614 perror("VIDIOC_G_CTRL");
615
616 free(ptr);
617 }
618
619 void retrace_vidioc_s_control(int fd_retrace, json_object *ioctl_args)
620 {
621 struct v4l2_control *ptr = retrace_v4l2_control_gen(ioctl_args);
622 ioctl(fd_retrace, VIDIOC_S_CTRL, ptr);
623
624 if (is_verbose() || (errno != 0))
625 perror("VIDIOC_S_CTRL");
626
627 free(ptr);
628 }
629
630 void retrace_vidioc_g_tuner(int fd_retrace, json_object *ioctl_args)
631 {
632 struct v4l2_tuner *ptr = retrace_v4l2_tuner_gen(ioctl_args);
633 ioctl(fd_retrace, VIDIOC_G_TUNER, ptr);
634
635 if (is_verbose() || (errno != 0))
636 perror("VIDIOC_G_TUNER");
637
638 free(ptr);
639 }
640
641 void retrace_vidioc_s_tuner(int fd_retrace, json_object *ioctl_args)
642 {
643 struct v4l2_tuner *ptr = retrace_v4l2_tuner_gen(ioctl_args);
644 ioctl(fd_retrace, VIDIOC_S_TUNER, ptr);
645
646 if (is_verbose() || (errno != 0))
647 perror("VIDIOC_S_TUNER");
648
649 free(ptr);
650 }
651
652 void retrace_vidioc_g_input(int fd_retrace, json_object *ioctl_args)
653 {
654 int input = 0;
655 ioctl(fd_retrace, VIDIOC_G_INPUT, &input);
656
657 if (is_verbose() || (errno != 0))
658 perror("VIDIOC_G_INPUT");
659 }
660
661 void retrace_vidioc_s_input(int fd_retrace, json_object *ioctl_args)
662 {
663 int input = 0;
664 json_object *input_obj;
665 if (json_object_object_get_ex(ioctl_args, "input", &input_obj))
666 input = json_object_get_int(input_obj);
667
668 ioctl(fd_retrace, VIDIOC_S_INPUT, &input);
669
670 if (is_verbose() || (errno != 0))
671 perror("VIDIOC_S_INPUT");
672 }
673
674 void retrace_vidioc_g_output(int fd_retrace, json_object *ioctl_args)
675 {
676 int output = 0;
677 ioctl(fd_retrace, VIDIOC_G_OUTPUT, &output);
678
679 if (is_verbose() || (errno != 0))
680 perror("VIDIOC_G_OUTPUT");
681 }
682
683 void retrace_vidioc_s_output(int fd_retrace, json_object *ioctl_args)
684 {
685 int output = 0;
686 json_object *output_obj;
687 if (json_object_object_get_ex(ioctl_args, "output", &output_obj))
688 output = json_object_get_int(output_obj);
689
690 ioctl(fd_retrace, VIDIOC_S_OUTPUT, &output);
691
692 if (is_verbose() || (errno != 0))
693 perror("VIDIOC_S_OUTPUT");
694 }
695
696 void retrace_vidioc_enumoutput(int fd_retrace, json_object *ioctl_args)
697 {
698 struct v4l2_output *ptr = retrace_v4l2_output_gen(ioctl_args);
699 ioctl(fd_retrace, VIDIOC_ENUMOUTPUT, ptr);
700
701 if (is_verbose() || (errno != 0))
702 perror("VIDIOC_ENUMOUTPUT");
703
704 free(ptr);
705 }
706
707 void retrace_vidioc_g_crop(int fd_retrace, json_object *ioctl_args)
708 {
709 struct v4l2_crop *ptr = retrace_v4l2_crop_gen(ioctl_args);
710 ioctl(fd_retrace, VIDIOC_G_CROP, ptr);
711
712 if (is_verbose() || (errno != 0))
713 perror("VIDIOC_G_CROP");
714
715 free(ptr);
716 }
717
718 void retrace_vidioc_s_crop(int fd_retrace, json_object *ioctl_args)
719 {
720 struct v4l2_crop *ptr = retrace_v4l2_crop_gen(ioctl_args);
721 ioctl(fd_retrace, VIDIOC_S_CROP, ptr);
722
723 if (is_verbose() || (errno != 0))
724 perror("VIDIOC_S_CROP");
725
726 free(ptr);
727 }
728
729 int retrace_v4l2_ext_control_value(json_object *ctrl_obj, const val_def *def)
730 {
731 __s32 value = -1;
732
733 json_object *value_obj;
734 if (json_object_object_get_ex(ctrl_obj, "value", &value_obj))
735 value = (__s32) s2val(json_object_get_string(value_obj), def);
736
737 return value;
738 }
739
740 __u32 *retrace_v4l2_dynamic_array(json_object *v4l2_ext_control_obj)
741 {
742 __u32 elems = 0;
743 json_object *elems_obj;
744 if (json_object_object_get_ex(v4l2_ext_control_obj, "elems", &elems_obj))
745 elems = (__u32) json_object_get_int64(elems_obj);
746
747 __u32 *ptr = static_cast<__u32 *>(calloc(elems, sizeof(__u32)));
748 if (ptr == nullptr) {
749 line_info("\n\tMemory allocation failed.");
750 return ptr;
751 }
752
753 json_object *p_u32_obj;
754 if (json_object_object_get_ex(v4l2_ext_control_obj, "p_u32", &p_u32_obj)) {
755 for (size_t i = 0; i < elems; i++) {
756 if (json_object_array_get_idx(p_u32_obj, i))
757 ptr[i] = (__u32) json_object_get_int64(json_object_array_get_idx(p_u32_obj, i));
758 }
759 }
760
761 return ptr;
762 }
763
764 struct v4l2_ext_control *retrace_v4l2_ext_control(json_object *parent_obj, int ctrl_idx)
765 {
766 struct v4l2_ext_control *p = (struct v4l2_ext_control *) calloc(1, sizeof(v4l2_ext_control));
767
768 json_object *v4l2_ext_control_obj = json_object_array_get_idx(parent_obj, ctrl_idx);
769 if (!v4l2_ext_control_obj)
770 return p;
771
772 json_object *id_obj;
773 if (json_object_object_get_ex(v4l2_ext_control_obj, "id", &id_obj))
774 p->id = s2val(json_object_get_string(id_obj), control_val_def);
775
776 json_object *size_obj;
777 if (json_object_object_get_ex(v4l2_ext_control_obj, "size", &size_obj))
778 p->size = json_object_get_int64(size_obj);
779
780 switch (p->id) {
781 case V4L2_CID_STATELESS_H264_DECODE_MODE:
782 p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
783 v4l2_stateless_h264_decode_mode_val_def);
784 break;
785 case V4L2_CID_STATELESS_H264_START_CODE:
786 p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
787 v4l2_stateless_h264_start_code_val_def);
788 break;
789 case V4L2_CID_STATELESS_HEVC_DECODE_MODE:
790 p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
791 v4l2_stateless_hevc_decode_mode_val_def);
792 break;
793 case V4L2_CID_STATELESS_HEVC_START_CODE:
794 p->value = retrace_v4l2_ext_control_value(v4l2_ext_control_obj,
795 v4l2_stateless_hevc_start_code_val_def);
796 break;
797 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
798 case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
799 case V4L2_CID_MPEG_VIDEO_DEC_CONCEAL_COLOR:
800 case V4L2_CID_PIXEL_RATE: {
801 json_object *value64_obj;
802
803 if (json_object_object_get_ex(v4l2_ext_control_obj, "value64", &value64_obj))
804 p->value64 = json_object_get_int64(value64_obj);
805 break;
806 }
807 default:
808 if (!p->size) {
809 json_object *value_obj;
810
811 if (json_object_object_get_ex(v4l2_ext_control_obj, "value", &value_obj))
812 p->value = json_object_get_int(value_obj);
813 }
814 break;
815 }
816
817 /* Don't retrace pointers that were not traced because they were null. */
818 if (p->size == 0)
819 return p;
820
821 switch (p->id) {
822 case V4L2_CID_STATELESS_VP8_FRAME:
823 p->ptr = retrace_v4l2_ctrl_vp8_frame_gen(v4l2_ext_control_obj);
824 break;
825 case V4L2_CID_STATELESS_H264_SPS:
826 p->ptr = retrace_v4l2_ctrl_h264_sps_gen(v4l2_ext_control_obj);
827 break;
828 case V4L2_CID_STATELESS_H264_PPS:
829 p->ptr = retrace_v4l2_ctrl_h264_pps_gen(v4l2_ext_control_obj);
830 break;
831 case V4L2_CID_STATELESS_H264_SCALING_MATRIX:
832 p->ptr = retrace_v4l2_ctrl_h264_scaling_matrix_gen(v4l2_ext_control_obj);
833 break;
834 case V4L2_CID_STATELESS_H264_PRED_WEIGHTS:
835 p->ptr = retrace_v4l2_ctrl_h264_pred_weights_gen(v4l2_ext_control_obj);
836 break;
837 case V4L2_CID_STATELESS_H264_SLICE_PARAMS:
838 p->ptr = retrace_v4l2_ctrl_h264_slice_params_gen(v4l2_ext_control_obj);
839 break;
840 case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
841 p->ptr = retrace_v4l2_ctrl_h264_decode_params_gen(v4l2_ext_control_obj);
842 break;
843 case V4L2_CID_STATELESS_FWHT_PARAMS:
844 p->ptr = retrace_v4l2_ctrl_fwht_params_gen(v4l2_ext_control_obj);
845 break;
846 case V4L2_CID_STATELESS_VP9_FRAME:
847 p->ptr = retrace_v4l2_ctrl_vp9_frame_gen(v4l2_ext_control_obj);
848 break;
849 case V4L2_CID_STATELESS_VP9_COMPRESSED_HDR:
850 p->ptr = retrace_v4l2_ctrl_vp9_compressed_hdr_gen(v4l2_ext_control_obj);
851 break;
852 case V4L2_CID_STATELESS_HEVC_SPS:
853 p->ptr = retrace_v4l2_ctrl_hevc_sps_gen(v4l2_ext_control_obj);
854 break;
855 case V4L2_CID_STATELESS_HEVC_PPS:
856 p->ptr = retrace_v4l2_ctrl_hevc_pps_gen(v4l2_ext_control_obj);
857 break;
858 case V4L2_CID_STATELESS_HEVC_SLICE_PARAMS:
859 p->ptr = retrace_v4l2_ctrl_hevc_slice_params_gen(v4l2_ext_control_obj);
860 break;
861 case V4L2_CID_STATELESS_HEVC_SCALING_MATRIX:
862 p->ptr = retrace_v4l2_ctrl_hevc_scaling_matrix_gen(v4l2_ext_control_obj);
863 break;
864 case V4L2_CID_STATELESS_HEVC_DECODE_PARAMS:
865 p->ptr = retrace_v4l2_ctrl_hevc_decode_params_gen(v4l2_ext_control_obj);
866 break;
867 case V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS:
868 p->p_u32 = retrace_v4l2_dynamic_array(v4l2_ext_control_obj);
869 break;
870 case V4L2_CID_STATELESS_MPEG2_SEQUENCE:
871 p->ptr = retrace_v4l2_ctrl_mpeg2_sequence_gen(v4l2_ext_control_obj);
872 break;
873 case V4L2_CID_STATELESS_MPEG2_PICTURE:
874 p->ptr = retrace_v4l2_ctrl_mpeg2_picture_gen(v4l2_ext_control_obj);
875 break;
876 case V4L2_CID_STATELESS_MPEG2_QUANTISATION:
877 p->ptr = retrace_v4l2_ctrl_mpeg2_quantisation_gen(v4l2_ext_control_obj);
878 break;
879 case V4L2_CID_STATELESS_AV1_SEQUENCE:
880 p->ptr = retrace_v4l2_ctrl_av1_sequence_gen(v4l2_ext_control_obj);
881 break;
882 case V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY:
883 p->ptr = retrace_v4l2_ctrl_av1_tile_group_entry_gen(v4l2_ext_control_obj);
884 break;
885 case V4L2_CID_STATELESS_AV1_FRAME:
886 p->ptr = retrace_v4l2_ctrl_av1_frame_gen(v4l2_ext_control_obj);
887 break;
888 case V4L2_CID_STATELESS_AV1_FILM_GRAIN:
889 p->ptr = retrace_v4l2_ctrl_av1_film_grain_gen(v4l2_ext_control_obj);
890 break;
891 default:
892 line_info("\n\tWarning: cannot retrace control: %s",
893 val2s(p->id, control_val_def).c_str());
894 break;
895 }
896
897 return p;
898 }
899
900 struct v4l2_ext_controls *retrace_v4l2_ext_controls(json_object *parent_obj)
901 {
902 struct v4l2_ext_controls *ptr = (struct v4l2_ext_controls *) calloc(1, sizeof(v4l2_ext_controls));
903
904 json_object *v4l2_ext_controls_obj;
905 json_object_object_get_ex(parent_obj, "v4l2_ext_controls", &v4l2_ext_controls_obj);
906
907 json_object *which_obj;
908 if (json_object_object_get_ex(v4l2_ext_controls_obj, "which", &which_obj))
909 ptr->which = (__u32) s2val(json_object_get_string(which_obj), which_val_def);
910
911 json_object *count_obj;
912 if (json_object_object_get_ex(v4l2_ext_controls_obj, "count", &count_obj))
913 ptr->count = (__u32) json_object_get_int64(count_obj);
914
915 json_object *error_idx_obj;
916 if (json_object_object_get_ex(v4l2_ext_controls_obj, "error_idx", &error_idx_obj))
917 ptr->error_idx = (__u32) json_object_get_int64(error_idx_obj);
918
919 /* request_fd is only valid for V4L2_CTRL_WHICH_REQUEST_VAL */
920 if (ptr->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
921 json_object *request_fd_obj;
922 if (json_object_object_get_ex(v4l2_ext_controls_obj, "request_fd", &request_fd_obj)) {
923
924 int request_fd_trace = json_object_get_int(request_fd_obj);
925 int request_fd_retrace = get_fd_retrace_from_fd_trace(request_fd_trace);
926 if (request_fd_retrace < 0) {
927 line_info("\n\tBad file descriptor.");
928 return ptr;
929 }
930 ptr->request_fd = (__s32) request_fd_retrace;
931 }
932 }
933
934 json_object *controls_obj;
935 if (json_object_object_get_ex(v4l2_ext_controls_obj, "controls", &controls_obj)) {
936 ptr->controls = (struct v4l2_ext_control *) calloc(ptr->count, sizeof(v4l2_ext_control));
937 for (__u32 i = 0; i < ptr->count; i++) {
938 void *temp = retrace_v4l2_ext_control(controls_obj, i);
939 if (temp != nullptr) {
940 ptr->controls[i] = *(static_cast<struct v4l2_ext_control*>(temp));
941 free(temp);
942 }
943 }
944 }
945
946 return ptr;
947 }
948
949 void retrace_vidioc_try_ext_ctrls(int fd_retrace, json_object *ioctl_args)
950 {
951 struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
952 ioctl(fd_retrace, VIDIOC_TRY_EXT_CTRLS, ptr);
953
954 free(ptr);
955
956 if (is_verbose() || (errno != 0))
957 perror("VIDIOC_TRY_EXT_CTRLS");
958 }
959
960 void retrace_vidioc_g_ext_ctrls(int fd_retrace, json_object *ioctl_args)
961 {
962 struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
963 ioctl(fd_retrace, VIDIOC_G_EXT_CTRLS, ptr);
964
965 free(ptr);
966
967 if (is_verbose() || (errno != 0))
968 perror("VIDIOC_G_EXT_CTRLS");
969 }
970
971 void retrace_vidioc_s_ext_ctrls(int fd_retrace, json_object *ioctl_args)
972 {
973 struct v4l2_ext_controls *ptr = retrace_v4l2_ext_controls(ioctl_args);
974 ioctl(fd_retrace, VIDIOC_S_EXT_CTRLS, ptr);
975
976 free(ptr);
977
978 if (is_verbose() || (errno != 0)) {
979 perror("VIDIOC_S_EXT_CTRLS");
980 debug_line_info();
981 print_context();
982 }
983 }
984
985 void retrace_vidioc_enum_framesizes(int fd_retrace, json_object *ioctl_args)
986 {
987 struct v4l2_frmsizeenum *ptr = retrace_v4l2_frmsizeenum_gen(ioctl_args);
988
989 ioctl(fd_retrace, VIDIOC_ENUM_FRAMESIZES, ptr);
990
991 if (is_verbose() || (errno != 0))
992 perror("VIDIOC_ENUM_FRAMESIZES");
993
994 free(ptr);
995 }
996
997 void retrace_vidioc_enum_frameintervals(int fd_retrace, json_object *ioctl_args)
998 {
999 struct v4l2_frmivalenum *ptr = retrace_v4l2_frmivalenum_gen(ioctl_args);
1000
1001 ioctl(fd_retrace, VIDIOC_ENUM_FRAMEINTERVALS, ptr);
1002
1003 if (is_verbose() || (errno != 0))
1004 perror("VIDIOC_ENUM_FRAMEINTERVALS");
1005
1006 free(ptr);
1007 }
1008
1009
1010 void retrace_vidioc_try_encoder_cmd(int fd_retrace, json_object *ioctl_args)
1011 {
1012 struct v4l2_encoder_cmd *ptr = retrace_v4l2_encoder_cmd_gen(ioctl_args);
1013
1014 ioctl(fd_retrace, VIDIOC_TRY_ENCODER_CMD, ptr);
1015
1016 if (is_verbose() || (errno != 0))
1017 perror("VIDIOC_TRY_ENCODER_CMD");
1018
1019 free(ptr);
1020 }
1021
1022 void retrace_vidioc_encoder_cmd(int fd_retrace, json_object *ioctl_args)
1023 {
1024 struct v4l2_encoder_cmd *ptr = retrace_v4l2_encoder_cmd_gen(ioctl_args);
1025
1026 ioctl(fd_retrace, VIDIOC_ENCODER_CMD, ptr);
1027
1028 if (is_verbose() || (errno != 0))
1029 perror("VIDIOC_ENCODER_CMD");
1030
1031 free(ptr);
1032 }
1033
1034 void retrace_vidioc_g_selection(int fd_retrace, json_object *ioctl_args)
1035 {
1036 struct v4l2_selection *ptr = retrace_v4l2_selection_gen(ioctl_args);
1037
1038 ioctl(fd_retrace, VIDIOC_G_SELECTION, ptr);
1039
1040 if (is_verbose() || (errno != 0))
1041 perror("VIDIOC_G_SELECTION");
1042
1043 free(ptr);
1044
1045 }
1046
1047 void retrace_vidioc_s_selection(int fd_retrace, json_object *ioctl_args)
1048 {
1049 struct v4l2_selection *ptr = retrace_v4l2_selection_gen(ioctl_args);
1050
1051 ioctl(fd_retrace, VIDIOC_S_SELECTION, ptr);
1052
1053 if (is_verbose() || (errno != 0))
1054 perror("VIDIOC_S_SELECTION");
1055
1056 free(ptr);
1057 }
1058
1059 struct v4l2_decoder_cmd *retrace_v4l2_decoder_cmd(json_object *parent_obj)
1060 {
1061 struct v4l2_decoder_cmd *ptr = (struct v4l2_decoder_cmd *) calloc(1, sizeof(v4l2_decoder_cmd));
1062
1063 json_object *v4l2_decoder_cmd_obj;
1064 json_object_object_get_ex(parent_obj, "v4l2_decoder_cmd", &v4l2_decoder_cmd_obj);
1065
1066 json_object *cmd_obj;
1067 /* Since V4L2_DEC_CMD_START is 0, an empty key will be retraced as V4L2_DEC_CMD_START. */
1068 if (json_object_object_get_ex(v4l2_decoder_cmd_obj, "cmd", &cmd_obj))
1069 ptr->cmd = (__u32) s2val(json_object_get_string(cmd_obj), decoder_cmd_val_def);
1070
1071 std::string flags;
1072 json_object *flags_obj;
1073 if (json_object_object_get_ex(v4l2_decoder_cmd_obj, "flags", &flags_obj))
1074 if (json_object_get_string(flags_obj) != nullptr)
1075 flags = json_object_get_string(flags_obj);
1076
1077 switch (ptr->cmd) {
1078 case V4L2_DEC_CMD_START: {
1079 ptr->flags = s2flags(flags.c_str(), v4l2_decoder_cmd_start_flag_def);
1080
1081 json_object *start_obj;
1082 json_object_object_get_ex(v4l2_decoder_cmd_obj, "start", &start_obj);
1083
1084 json_object *speed_obj;
1085 if (json_object_object_get_ex(start_obj, "speed", &speed_obj))
1086 ptr->start.speed = json_object_get_int(speed_obj);
1087
1088 std::string format;
1089 json_object *format_obj;
1090 if (json_object_object_get_ex(start_obj, "format", &format_obj))
1091 if (json_object_get_string(format_obj) != nullptr)
1092 format = json_object_get_string(format_obj);
1093
1094 if (format == "V4L2_DEC_START_FMT_GOP")
1095 ptr->start.format = V4L2_DEC_START_FMT_GOP;
1096 else if (format == "V4L2_DEC_START_FMT_NONE")
1097 ptr->start.format = V4L2_DEC_START_FMT_NONE;
1098 break;
1099 }
1100 case V4L2_DEC_CMD_STOP: {
1101 ptr->flags = s2flags(flags.c_str(), v4l2_decoder_cmd_stop_flag_def);
1102
1103 json_object *stop_obj;
1104 json_object_object_get_ex(v4l2_decoder_cmd_obj, "stop", &stop_obj);
1105
1106 json_object *pts_obj;
1107 if (json_object_object_get_ex(stop_obj, "pts", &pts_obj))
1108 ptr->stop.pts = (__u64) json_object_get_uint64(pts_obj);
1109 break;
1110 }
1111 case V4L2_DEC_CMD_PAUSE: {
1112 ptr->flags = s2flags(flags.c_str(), v4l2_decoder_cmd_pause_flag_def);
1113 break;
1114 }
1115 default:
1116 break;
1117 }
1118
1119 return ptr;
1120 }
1121
1122 void retrace_vidioc_try_decoder_cmd(int fd_retrace, json_object *ioctl_args)
1123 {
1124 struct v4l2_decoder_cmd *ptr = retrace_v4l2_decoder_cmd(ioctl_args);
1125
1126 ioctl(fd_retrace, VIDIOC_TRY_DECODER_CMD, ptr);
1127
1128 if (is_verbose() || (errno != 0))
1129 perror("VIDIOC_TRY_DECODER_CMD");
1130
1131 free (ptr);
1132 }
1133
1134 void retrace_vidioc_dqevent(int fd_retrace)
1135 {
1136 const int poll_timeout_ms = 200;
1137 struct pollfd *pfds = (struct pollfd *) calloc(1, sizeof(struct pollfd));
1138 if (pfds == nullptr)
1139 exit(EXIT_FAILURE);
1140 pfds[0].fd = fd_retrace;
1141 pfds[0].events = POLLIN | POLLPRI;
1142 int ret = poll(pfds, 1, poll_timeout_ms);
1143
1144 if (ret == -1) {
1145 line_info("\n\tPoll error.");
1146 perror("");
1147 free(pfds);
1148 exit(EXIT_FAILURE);
1149 }
1150 if (ret == 0) {
1151 line_info("\n\tPoll timed out.");
1152 free(pfds);
1153 exit(EXIT_FAILURE);
1154 }
1155
1156 struct v4l2_event event = {};
1157 ioctl(fd_retrace, VIDIOC_DQEVENT, &event);
1158
1159 if (is_verbose() || (errno != 0))
1160 perror("VIDIOC_DQEVENT");
1161
1162 free(pfds);
1163 }
1164
1165 void retrace_vidioc_subscribe_event(int fd_retrace, json_object *ioctl_args)
1166 {
1167 struct v4l2_event_subscription *ptr = retrace_v4l2_event_subscription_gen(ioctl_args);
1168
1169 ioctl(fd_retrace, VIDIOC_SUBSCRIBE_EVENT, ptr);
1170
1171 if (is_verbose() || (errno != 0))
1172 perror("VIDIOC_SUBSCRIBE_EVENT");
1173
1174 free (ptr);
1175 }
1176
1177 void retrace_vidioc_unsubscribe(int fd_retrace, json_object *ioctl_args)
1178 {
1179 struct v4l2_event_subscription *ptr = retrace_v4l2_event_subscription_gen(ioctl_args);
1180
1181 ioctl(fd_retrace, VIDIOC_UNSUBSCRIBE_EVENT, ptr);
1182
1183 if (is_verbose() || (errno != 0))
1184 perror("VIDIOC_UNSUBSCRIBE_EVENT");
1185
1186 free (ptr);
1187 }
1188
1189 void retrace_vidioc_decoder_cmd(int fd_retrace, json_object *ioctl_args)
1190 {
1191 struct v4l2_decoder_cmd *ptr = retrace_v4l2_decoder_cmd(ioctl_args);
1192
1193 ioctl(fd_retrace, VIDIOC_DECODER_CMD, ptr);
1194
1195 if (is_verbose() || (errno != 0))
1196 perror("VIDIOC_DECODER_CMD");
1197
1198 free (ptr);
1199 }
1200
1201 void retrace_vidioc_query_ext_ctrl(int fd_retrace, json_object *ioctl_args)
1202 {
1203 struct v4l2_query_ext_ctrl *ptr = retrace_v4l2_query_ext_ctrl_gen(ioctl_args);
1204
1205 ioctl(fd_retrace, VIDIOC_QUERY_EXT_CTRL, ptr);
1206
1207 if (is_verbose())
1208 perror("VIDIOC_QUERY_EXT_CTRL");
1209
1210 free(ptr);
1211 }
1212
1213 void retrace_vidioc_enum_fmt(int fd_retrace, json_object *ioctl_args)
1214 {
1215 struct v4l2_fmtdesc *ptr = retrace_v4l2_fmtdesc_gen(ioctl_args);
1216
1217 ioctl(fd_retrace, VIDIOC_ENUM_FMT, ptr);
1218
1219 if (is_verbose())
1220 perror("VIDIOC_ENUM_FMT");
1221
1222 free(ptr);
1223 }
1224
1225 void retrace_vidioc_querycap(int fd_retrace, json_object *ioctl_args)
1226 {
1227 struct v4l2_capability *ptr = retrace_v4l2_capability_gen(ioctl_args);
1228
1229 ioctl(fd_retrace, VIDIOC_QUERYCAP, ptr);
1230
1231 if (is_verbose() || (errno != 0))
1232 perror("VIDIOC_QUERYCAP");
1233
1234 free(ptr);
1235 }
1236
1237 void retrace_media_ioc_request_alloc(int fd_retrace, json_object *ioctl_args)
1238 {
1239 /* Get the original request file descriptor from the original trace file. */
1240 json_object *request_fd_trace_obj;
1241 json_object_object_get_ex(ioctl_args, "request_fd", &request_fd_trace_obj);
1242 int request_fd_trace = json_object_get_int(request_fd_trace_obj);
1243
1244 /* Allocate a request in the retrace context. */
1245 __s32 request_fd_retrace = 0;
1246 ioctl(fd_retrace, MEDIA_IOC_REQUEST_ALLOC, &request_fd_retrace);
1247
1248 /* Associate the original request file descriptor with the current request file descriptor. */
1249 add_fd(request_fd_trace, request_fd_retrace);
1250 }
1251
1252 void retrace_ioctl(json_object *syscall_obj)
1253 {
1254 __s64 cmd = 0;
1255 int fd_retrace = 0;
1256
1257 json_object *fd_trace_obj;
1258 json_object_object_get_ex(syscall_obj, "fd", &fd_trace_obj);
1259 fd_retrace = get_fd_retrace_from_fd_trace(json_object_get_int(fd_trace_obj));
1260
1261 json_object *cmd_obj;
1262 json_object_object_get_ex(syscall_obj, "ioctl", &cmd_obj);
1263 cmd = s2val(json_object_get_string(cmd_obj), ioctl_val_def);
1264
1265 json_object *errno_obj;
1266 if (json_object_object_get_ex(syscall_obj, "errno", &errno_obj))
1267 return;
1268
1269 if (fd_retrace < 0) {
1270 line_info("\n\tBad file descriptor on %s\n", json_object_get_string(cmd_obj));
1271 return;
1272 }
1273
1274 /* If available, use the ioctl arguments from userspace, otherwise use the driver arguments. */
1275 json_object *ioctl_args;
1276 if (json_object_object_get_ex(syscall_obj, "from_userspace", &ioctl_args) == false)
1277 json_object_object_get_ex(syscall_obj, "from_driver", &ioctl_args);
1278
1279 switch (cmd) {
1280 case VIDIOC_QUERYCAP:
1281 retrace_vidioc_querycap(fd_retrace, ioctl_args);
1282 break;
1283 case VIDIOC_ENUM_FMT:
1284 retrace_vidioc_enum_fmt(fd_retrace, ioctl_args);
1285 break;
1286 case VIDIOC_TRY_FMT:
1287 retrace_vidioc_try_fmt(fd_retrace, ioctl_args);
1288 break;
1289 case VIDIOC_G_FMT:
1290 retrace_vidioc_g_fmt(fd_retrace, ioctl_args);
1291 break;
1292 case VIDIOC_S_FMT:
1293 retrace_vidioc_s_fmt(fd_retrace, ioctl_args);
1294 break;
1295 case VIDIOC_REQBUFS:
1296 retrace_vidioc_reqbufs(fd_retrace, ioctl_args);
1297 break;
1298 case VIDIOC_QUERYBUF:
1299 retrace_vidioc_querybuf(fd_retrace, ioctl_args);
1300 break;
1301 case VIDIOC_QBUF:
1302 retrace_vidioc_qbuf(fd_retrace, ioctl_args);
1303 break;
1304 case VIDIOC_EXPBUF:
1305 retrace_vidioc_expbuf(fd_retrace, ioctl_args);
1306 break;
1307 case VIDIOC_DQBUF:
1308 retrace_vidioc_dqbuf(fd_retrace, ioctl_args);
1309 break;
1310 case VIDIOC_STREAMON:
1311 retrace_vidioc_streamon(fd_retrace, ioctl_args);
1312 break;
1313 case VIDIOC_STREAMOFF:
1314 retrace_vidioc_streamoff(fd_retrace, ioctl_args);
1315 break;
1316 case VIDIOC_G_PARM:
1317 retrace_vidioc_g_parm(fd_retrace, ioctl_args);
1318 break;
1319 case VIDIOC_S_PARM:
1320 retrace_vidioc_s_parm(fd_retrace, ioctl_args);
1321 break;
1322 case VIDIOC_ENUMINPUT:
1323 retrace_vidioc_enuminput(fd_retrace, ioctl_args);
1324 break;
1325 case VIDIOC_G_CTRL:
1326 retrace_vidioc_g_control(fd_retrace, ioctl_args);
1327 break;
1328 case VIDIOC_S_CTRL:
1329 retrace_vidioc_s_control(fd_retrace, ioctl_args);
1330 break;
1331 case VIDIOC_G_TUNER:
1332 retrace_vidioc_g_tuner(fd_retrace, ioctl_args);
1333 break;
1334 case VIDIOC_S_TUNER:
1335 retrace_vidioc_s_tuner(fd_retrace, ioctl_args);
1336 break;
1337 case VIDIOC_QUERYCTRL:
1338 retrace_vidioc_queryctrl(fd_retrace, ioctl_args);
1339 break;
1340 case VIDIOC_G_INPUT:
1341 retrace_vidioc_g_input(fd_retrace, ioctl_args);
1342 break;
1343 case VIDIOC_S_INPUT:
1344 retrace_vidioc_s_input(fd_retrace, ioctl_args);
1345 break;
1346 case VIDIOC_G_OUTPUT:
1347 retrace_vidioc_g_output(fd_retrace, ioctl_args);
1348 break;
1349 case VIDIOC_S_OUTPUT:
1350 retrace_vidioc_s_output(fd_retrace, ioctl_args);
1351 break;
1352 case VIDIOC_ENUMOUTPUT:
1353 retrace_vidioc_enumoutput(fd_retrace, ioctl_args);
1354 break;
1355 case VIDIOC_G_CROP:
1356 retrace_vidioc_g_crop(fd_retrace, ioctl_args);
1357 break;
1358 case VIDIOC_S_CROP:
1359 retrace_vidioc_s_crop(fd_retrace, ioctl_args);
1360 break;
1361 case VIDIOC_G_EXT_CTRLS:
1362 retrace_vidioc_g_ext_ctrls(fd_retrace, ioctl_args);
1363 break;
1364 case VIDIOC_TRY_EXT_CTRLS:
1365 retrace_vidioc_try_ext_ctrls(fd_retrace, ioctl_args);
1366 break;
1367 case VIDIOC_S_EXT_CTRLS:
1368 retrace_vidioc_s_ext_ctrls(fd_retrace, ioctl_args);
1369 break;
1370 case VIDIOC_ENUM_FRAMESIZES:
1371 retrace_vidioc_enum_framesizes(fd_retrace, ioctl_args);
1372 break;
1373 case VIDIOC_ENUM_FRAMEINTERVALS:
1374 retrace_vidioc_enum_frameintervals(fd_retrace, ioctl_args);
1375 break;
1376 case VIDIOC_TRY_ENCODER_CMD:
1377 retrace_vidioc_try_encoder_cmd(fd_retrace, ioctl_args);
1378 break;
1379 case VIDIOC_ENCODER_CMD:
1380 retrace_vidioc_encoder_cmd(fd_retrace, ioctl_args);
1381 break;
1382 case VIDIOC_CREATE_BUFS:
1383 retrace_vidioc_create_bufs(fd_retrace, ioctl_args);
1384 break;
1385 case VIDIOC_G_SELECTION:
1386 retrace_vidioc_g_selection(fd_retrace, ioctl_args);
1387 break;
1388 case VIDIOC_S_SELECTION:
1389 retrace_vidioc_s_selection(fd_retrace, ioctl_args);
1390 break;
1391 case VIDIOC_PREPARE_BUF:
1392 retrace_vidioc_prepare_buf(fd_retrace, ioctl_args);
1393 break;
1394 case VIDIOC_TRY_DECODER_CMD:
1395 retrace_vidioc_try_decoder_cmd(fd_retrace, ioctl_args);
1396 break;
1397 case VIDIOC_DQEVENT:
1398 retrace_vidioc_dqevent(fd_retrace);
1399 break;
1400 case VIDIOC_SUBSCRIBE_EVENT:
1401 retrace_vidioc_subscribe_event(fd_retrace, ioctl_args);
1402 break;
1403 case VIDIOC_UNSUBSCRIBE_EVENT:
1404 retrace_vidioc_unsubscribe(fd_retrace, ioctl_args);
1405 break;
1406 case VIDIOC_DECODER_CMD:
1407 retrace_vidioc_decoder_cmd(fd_retrace, ioctl_args);
1408 break;
1409 case VIDIOC_QUERY_EXT_CTRL:
1410 retrace_vidioc_query_ext_ctrl(fd_retrace, ioctl_args);
1411 break;
1412 case MEDIA_IOC_REQUEST_ALLOC:
1413 retrace_media_ioc_request_alloc(fd_retrace, ioctl_args);
1414 break;
1415 case MEDIA_REQUEST_IOC_QUEUE:
1416 ioctl(fd_retrace, MEDIA_REQUEST_IOC_QUEUE);
1417 break;
1418 case MEDIA_REQUEST_IOC_REINIT:
1419 ioctl(fd_retrace, MEDIA_REQUEST_IOC_REINIT);
1420 break;
1421 default:
1422 if (json_object_get_string(cmd_obj) != nullptr)
1423 line_info("\n\tWarning: cannot retrace ioctl: \'%s\'\n",
1424 json_object_get_string(cmd_obj));
1425 else
1426 line_info("\n\tWarning: cannot retrace ioctl.");
1427 break;
1428 }
1429 }
1430
1431 void retrace_mem(json_object *mem_obj)
1432 {
1433 json_object *type_obj;
1434 json_object_object_get_ex(mem_obj, "mem_dump", &type_obj);
1435 v4l2_buf_type type = (v4l2_buf_type) s2val(json_object_get_string(type_obj),
1436 v4l2_buf_type_val_def);
1437 json_object *bytesused_obj;
1438 json_object_object_get_ex(mem_obj, "bytesused", &bytesused_obj);
1439 int bytesused = json_object_get_int64(bytesused_obj);
1440 if (bytesused == 0)
1441 return;
1442
1443 json_object *offset_obj;
1444 json_object_object_get_ex(mem_obj, "offset", &offset_obj);
1445 __u32 offset = json_object_get_int64(offset_obj);
1446
1447 json_object *address_obj;
1448 json_object_object_get_ex(mem_obj, "address", &address_obj);
1449 long buffer_address_trace = json_object_get_int64(address_obj);
1450
1451 long buffer_address_retrace = get_retrace_address_from_trace_address(buffer_address_trace);
1452
1453 unsigned char *buffer_pointer = (unsigned char *) buffer_address_retrace;
1454
1455 /* Get the encoded data from the json file and write it to output buffer memory. */
1456 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1457 write_to_output_buffer(buffer_pointer, bytesused, mem_obj);
1458
1459 debug_line_info("\n\t%s, bytesused: %d, offset: %d, addr: %ld",
1460 val2s(type, v4l2_buf_type_val_def).c_str(),
1461 bytesused, offset, buffer_address_retrace);
1462 print_context();
1463 }
1464
1465 void retrace_object(json_object *jobj)
1466 {
1467 errno = 0;
1468 json_object *temp_obj;
1469 if (json_object_object_get_ex(jobj, "ioctl", &temp_obj)) {
1470 retrace_ioctl(jobj);
1471 return;
1472 }
1473
1474 if (json_object_object_get_ex(jobj, "open", &temp_obj)) {
1475 retrace_open(jobj, false);
1476 return;
1477 }
1478
1479 if (json_object_object_get_ex(jobj, "open64", &temp_obj)) {
1480 retrace_open(jobj, true);
1481 return;
1482 }
1483
1484 if (json_object_object_get_ex(jobj, "close", &temp_obj)) {
1485 retrace_close(jobj);
1486 return;
1487 }
1488
1489 if (json_object_object_get_ex(jobj, "mmap", &temp_obj)) {
1490 retrace_mmap(jobj, false);
1491 return;
1492 }
1493
1494 if (json_object_object_get_ex(jobj, "mmap64", &temp_obj)) {
1495 retrace_mmap(jobj, true);
1496 return;
1497 }
1498
1499 if (json_object_object_get_ex(jobj, "munmap", &temp_obj)) {
1500 retrace_munmap(jobj);
1501 return;
1502 }
1503
1504 if (json_object_object_get_ex(jobj, "mem_dump", &temp_obj)) {
1505 retrace_mem(jobj);
1506 return;
1507 }
1508
1509 if (json_object_object_get_ex(jobj, "package_version", &temp_obj)) {
1510 compare_program_versions(jobj);
1511 return;
1512 }
1513
1514 if (json_object_object_get_ex(jobj, "Trace", &temp_obj)) {
1515 return;
1516 }
1517 if (json_object_object_get_ex(jobj, "write", &temp_obj)) {
1518 return;
1519 }
1520 line_info("\n\tWarning: unexpected JSON object in trace file.");
1521 }
1522
1523 void retrace_array(json_object *root_array_obj)
1524 {
1525 json_object *jobj;
1526 struct array_list *array_list_pointer = json_object_get_array(root_array_obj);
1527 size_t json_objects_in_file = array_list_length(array_list_pointer);
1528
1529 if (json_objects_in_file < 3)
1530 line_info("\n\tWarning: trace file may be empty.");
1531
1532 for (size_t i = 0; i < json_objects_in_file; i++) {
1533 jobj = (json_object *) array_list_get_idx(array_list_pointer, i);
1534 retrace_object(jobj);
1535 }
1536 }
1537
1538 int retrace(std::string trace_filename)
1539 {
1540 struct stat sb;
1541 if (stat(trace_filename.c_str(), &sb) == -1) {
1542 line_info("\n\tTrace file error: \'%s\'", trace_filename.c_str());
1543 return -EINVAL;
1544 }
1545
1546 fprintf(stderr, "Retracing: %s\n", trace_filename.c_str());
1547
1548 json_object *root_array_obj = json_object_from_file(trace_filename.c_str());
1549
1550 if (root_array_obj == nullptr) {
1551 line_info("\n\t%s\tCan't get JSON-object from file: %s",
1552 json_util_get_last_err(),
1553 trace_filename.c_str());
1554 return 1;
1555 }
1556
1557 retrace_array(root_array_obj);
1558 json_object_put(root_array_obj);
1559
1560 return 0;
1561 }