gpt4 book ai didi

c++ - 使用 Boost::asio posix stream_descriptor 读取 ftrace 管道

转载 作者:行者123 更新时间:2023-11-30 05:25:19 25 4
gpt4 key购买 nike

我正在尝试构建一个从 ftrace pipes 中读取的应用程序在调试 fs。

似乎在尝试从 trace_pipe 或异步读取时trace_pipe_raw 使用 boost::asio API,正在等待管道中的事件由 async_read 句柄处理并打印到屏幕,但程序启动后到达的新事件不会触发 async_read 句柄。

运行下面的示例代码,我打印了所有在队列中等待的事件,但我没有打印稍后到达的新事件。

如果我尝试使用 mkfifo 从手动创建的管道中读取数据但对 ftrace 管道不起作用,则同一示例可以完美运行。

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>

namespace asio = boost::asio;
#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef asio::posix::stream_descriptor stream_descriptor;
#endif

class PipeReader
{
typedef std::shared_ptr<PipeReader> PipeReaderPtr;
typedef std::weak_ptr<PipeReader> PipeReaderWeakPtr;
public:
static PipeReaderWeakPtr Create(asio::io_service& io_service, const std::string& path);

void HandleRead(PipeReaderPtr me, const boost::system::error_code &error);
private:
PipeReader(asio::io_service& io_service, const std::string& path);
stream_descriptor m_pipe;
char buf[4096];
};

PipeReader::PipeReaderWeakPtr PipeReader::Create(asio::io_service& io_service, const std::string& path)
{
PipeReaderPtr ptr(new PipeReader(io_service, path));

ptr->m_pipe.async_read_some(boost::asio::buffer(ptr->buf),
boost::bind(&PipeReader::HandleRead,
ptr.get(),
ptr,
asio::placeholders::error));
return ptr;
}

PipeReader::PipeReader(asio::io_service& io_service, const std::string& path)
: m_pipe(io_service)
{
int dev = open(path.c_str(), O_RDWR);
if (dev == -1) {
std::cout << "failed to open path - " << path << std::endl;
}
else
{
m_pipe.assign(dev);
}
}

void PipeReader::HandleRead(PipeReaderPtr me, const boost::system::error_code &error)
{
if (!error) {
std::string str(me->buf);

std::cout << "got message: " << str << std::endl;
m_pipe.async_read_some(boost::asio::buffer(me->buf),
boost::bind(&PipeReader::HandleRead,
this,
me,
asio::placeholders::error));
}
else
{
std::cout << "got error - " << error.message() << std::endl;
}
}


int main()
{
boost::asio::io_service io_service;
boost::asio::io_service::work dummy(io_service);

PipeReader::Create(io_service, "/sys/kernel/debug/tracing/trace_pipe");

io_service.run();
return 0;
}

最佳答案

我发现了问题。导致 epoll 挂起的是 ftrace 实现中的一个错误。该错误已在内核 3.16 中修复。

correspondence thread , commit in git hub

关于c++ - 使用 Boost::asio posix stream_descriptor 读取 ftrace 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com