本文分享通過在內核增加打印信息來調試V4L2的方法。
如何在WSL下修改內核代碼進行調試參考:
WSL2中配置支持UVC
查看如下位置,會根據UVC設備生成對應的video文件夾
qinyunti@qinyunti:~$ ls /sys/class/video4linuxvideo0 video1

每個設備一個videox文件夾,有如下文件
qinyunti@qinyunti:~$ ls /sys/class/video4linux/video0/
dev dev_debug device index name power subsystem ueven
其中dev_debug
'name' 屬性是 video_device 結構體的 'name' 字段。
'dev_debug' 屬性可用于啟用核心調試。
'index' 屬性是設備節點的索引:對于每次調用 video_register_device(),索引僅增加 1。您注冊的第一個視頻設備節點始終以索引 0 開始。
重點關注dev_debug
每一個bit對應相應的調試信息是否打開,只需要修改該文件的內容修改對應的bit即可使能或者關閉對應的打印。
掩碼 | 描述 |
0x01 | 記錄ioctl 名稱和錯誤代碼。僅當同時設置了位 0x08 時,才會記錄 VIDIOC_(D)QBUF ioctl。 |
0x02 | 記錄ioctl 名稱參數和錯誤代碼。僅當同時設置了位 0x08 時,才會記錄 VIDIOC_(D)QBUF ioctl。 |
0x04 | 記錄文件操作open、release、read、write、mmap 和 get_unmapped_area。僅當同時設置了位 0x08 時,才會記錄讀寫操作。 |
0x08 | 記錄讀寫文件操作以及VIDIOC_QBUF 和 VIDIOC_DQBUF ioctl。 |
0x10 | 記錄poll 文件操作。 |
0x20 | 記錄控制操作中的錯誤和消息。 |
相應的宏位于
include/media/v4l2-ioctl.h
/* v4l debugging and diagnostics *//* Device debug flags to be used with the video device debug attribute *//* Just log the ioctl name + error code *//* Log the ioctl name arguments + error code *//* Log the file operations open, release, mmap and get_unmapped_area *//* Log the read and write file operations and the VIDIOC_(D)QBUF ioctls *//* Log poll() *//* Log controls */
sudo suecho 0x3F > /sys/class/video4linux/video0/dev_debug
注意直接用sudo 0x3F > /sys/class/video4linux/video0/dev_debug會提示權限問題
此時dmesg可以看到更多打印信息

例如
[ 889.091494] videodev: v4l2_poll: video0: poll: 00000000 0000001a就是
drivers/media/v4l2-core/v4l2-dev.c中以下位置打印
static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll){struct video_device *vdev = video_devdata(filp);__poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;if (video_is_registered(vdev)) {if (!vdev->fops->poll)res = DEFAULT_POLLMASK;elseres = vdev->fops->poll(filp, poll);}if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)dprintk("%s: poll: %08x %08x\n",video_device_node_name(vdev), res,poll_requested_events(poll));return res;}
為了調試此時就可以去增加自己的打印信息。
本文分享如何通過增加調試信息來進行v4l2的調試。在WSL下可以方便替換內核,進行V4L2相關的學習,調試等。