NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Guidance debugging kernel video.c with OpenCV



Hello -- I'm attempting to port https://github.com/opencv/opencv (4.6.0 branch) to NetBSD 10. I've successfully built on both AMD64 and Aarch64 and can run the opencv samples/python/video.py, for example. I'm trying to solve a few issues I see running opencv with debug output [0] and a -DCMAKE_BUILT_TYPE=Debug build of opencv.

I have little experience debugging the kernel and that was years ago (ARM TS7200 days).

In the debug output, I see
[ INFO:0@1224.983] global /home/joelp/repos/opencv.4.6.0/modules/videoio/src/cap_v4l.cpp (1011) tryIoctl VIDEOIO(V4L2:/dev/video0): ioctl returns with errno=EBUSY [ WARN:0@1224.983] global /home/joelp/repos/opencv.4.6.0/modules/videoio/src/cap_v4l.cpp (838) requestBuffers VIDEOIO(V4L2:/dev/video0): failed VIDIOC_REQBUFS: errno=16 (Device busy)

Under gdb a set a breakpoint at cap_v4l.cpp:1011 and here is the frame:
#0  cv::CvCaptureCAM_V4L::tryIoctl (this=0x7757ae255000, ioctlCode=3222558216,
    parameter=0x7757ae2556f0, failIfBusy=true, attempts=10)
    at /home/joelp/repos/opencv.4.6.0/modules/videoio/src/cap_v4l.cpp:1011
        cv_temp_msglevel = 4293269023
        cv_temp_logtagptr = 0x7757af400980
        cv_temp_logstream = <incomplete type>                                                     err = 16 fds = {fds_bits = {2918895280, 30551, 4293268495, 32639, 4293268824, 32639,                   2718204559, 30551}} tv = {tv_sec = 140187730843552, tv_usec = -1576952894}
        result = -1
        isBusy = true param_v4l_select_timeout = 0 __func__ = "tryIoctl"

Here's the opencv source:

bool CvCaptureCAM_V4L::requestBuffers(unsigned int buffer_number)
{
    if (!isOpened())
        return false;

    req = v4l2_requestbuffers();
    req.count = buffer_number;
    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    req.memory = V4L2_MEMORY_MMAP;

    if (!tryIoctl(VIDIOC_REQBUFS, &req)) {
        int err = errno;
        if (EINVAL == err) {
            CV_LOG_WARNING(NULL, "VIDEOIO(V4L2:" << deviceName << "): no support for memory mapping"); } else
        {
            CV_LOG_WARNING(NULL, "VIDEOIO(V4L2:" << deviceName << "): failed VIDIOC_REQBUFS: errno=" << err << " (" << strerror(err) << ")");
        }
        return false;
} v4l_buffersRequested = true;
    return true;
}

Looking in src/sys/dev/video.c for that ioctl:
int
videoioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
...
    case VIDIOC_REQBUFS:
        reqbufs = data;
        return (video_request_bufs(sc, reqbufs));

And video_request_bufs:
static int
video_request_bufs(struct video_softc *sc,
           struct v4l2_requestbuffers *req)
{
    struct video_stream *vs = &sc->sc_stream_in;
    struct v4l2_buffer *buf;
    int i, err;

    if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
        return EINVAL;

    vs->vs_type = req->type;

    switch (req->memory) {
    case V4L2_MEMORY_MMAP:
        if (req->count < VIDEO_MIN_BUFS)
            req->count = VIDEO_MIN_BUFS;
        else if (req->count > VIDEO_MAX_BUFS)
            req->count = VIDEO_MAX_BUFS;

        err = video_stream_setup_bufs(vs,
                          VIDEO_STREAM_METHOD_MMAP,
                          req->count);
        if (err != 0)
            return err;

Then video_stream_setup_bufs:
static int
video_stream_setup_bufs(struct video_stream *vs,
            enum video_stream_method method,
            uint8_t nbufs)
{
    int i, err;

    mutex_enter(&vs->vs_lock);

    /* Ensure that all allocated buffers are queued and not under
     * userspace control. */
    for (i = 0; i < vs->vs_nbufs; ++i) {
        if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED)) {
            mutex_exit(&vs->vs_lock);
            return EBUSY;
        }
    }

I think this might be why the assert sees the EBUSY error?

Can anyone help me understand that error in this context?

I'm debugging this under AMD64 and can build a custom kernel if needed. Is there better kernel tracing I can add to a custom kernel for more info?

Thanks - Joel


[0]
export OPENCV_LOG_LEVEL=DEBUG
export OPENCV_VIDEOIO_DEBUG=1



Home | Main Index | Thread Index | Old Index