如何获取C++ std::ofstream写入设备的更多调试信息?

How to get more debug info for C++ std::ofstream writing to device?

提问人:qmastery 提问时间:11/29/2021 更新时间:12/2/2021 访问量:237

问:

美好的一天,我正在尝试调试与XDMA设备交互的C++代码:

#include <fstream>
#include <iostream>
#include <unistd.h>

int main()
{
    std::ofstream output_;
    const char* output_name = "/dev/xdma/card0/h2c0";

    output_.exceptions(std::ios::failbit | std::ios::badbit);
    output_.rdbuf()->pubsetbuf(nullptr, 0);
    output_.open(output_name, std::ios::binary | std::ios::out);

    std::streamoff offset = 0x1e00000;
    output_.seekp(offset, std::ios::beg);

    const char buf[1] = "";
    std::streamsize size = 1;
    auto pos = output_.tellp();

    output_.write(buf, size); // <--- IOSTREAM ERROR

    output_.seekp(pos + size, std::ios::beg);
    return 0;
}

但是这个程序失败了 - 有一个相当模糊的错误消息:output_.write(buf, size);

terminate called after throwing an instance of 'std::__ios_failure'
  what():  basic_ios::clear: iostream error
Aborted (core dumped)

而且,如果我把它包装在一个try&catch块中:output_.write(buf, size);

    try {
        output_.write(buf, size);
    }
    catch (std::system_error& e) {
        std::cerr << e.code().message() << "(" << e.code().value() << ")\n";
        return 1;
    }

它更改为 .这并不能说明失败的原因......我确信我可以写入0x1e00000偏移量,因为替代 C 代码可以完美地工作。所以错误出在C++代码或库中。如何获取更多调试信息?iostream error(1)

C++ 文件 std iostream ofstream

评论

0赞 KamilCuk 11/29/2021
那么,为什么不呢?和std:cerr << e.what()std:cerr << e.code();
0赞 qmastery 11/29/2021
@KamilCuk这些变体也没有提供任何有用的东西......

答:

0赞 qmastery 12/2/2021 #1

虽然我没有成功获得更多的调试信息,但解决方案是将提供设备的XDMA驱动程序从旧版本降级到旧版本(需要自定义补丁才能在新操作系统上运行)。不幸的是,这些新驱动程序真的很有问题....../dev/xdma/card0/h2c0v2017.1.47v2017.0.45