splice(2) 在读取 ftrace 原始文件时返回"Invalid argument"



我想尝试splice syscall,trace cmd使用它来零复制off-ftrace的原始文件。

以下是拼接无法读取的ftrace原始文件的部分列表:

/sys/kernel/debug/tracking/per_cpu/trace_pipe_raw

/sys/kernel/debug/tracing/per_cpu/cpo0/snapshot_raw

/sys/kernel/debug/tracing/per_cpu/cpo1/trace_pipe_raw

/sys/kernel/debug/tracing/per_cpu/cpo1/snapshot_raw

这里还有一些其他文件(拼接处理得很好(:

/sys/kernel/debug/tracing/per_cpu/cpo0/trace_pipe

/sys/kernel/debug/tracing/per_cpu/cpo0/snapshot

/sys/kernel/debug/tracing/per_cpu/cpo1/trace_pipe

/sys/kernel/debug/tracing/per_cpu/cpo1/snapshot

什么有效:

  • 使用read((系统调用可以很好地读取原始的ftrace文件
  • 使用cat((系统调用将显示原始的ftrace文件
  • 使用trace cmd工具,该工具是ftrace的CLI前端

这是我的代码:

static void unit_test_x(void)
{  
int buffer_pipe[2];
pipe(buffer_pipe);
std::string source_path = "/sys/kernel/debug/tracing/per_cpu/cpu1/trace_pipe_raw";
int trace_fd = open(source_path.c_str(), O_RDONLY);
std::string destination_path = "foo";
int dest_fd = open (destination_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
int actually_read = splice(trace_fd,
NULL,
buffer_pipe[1],
NULL,
1000,
SPLICE_F_MORE | SPLICE_F_MOVE );
if (0 > actually_read )
{   
printf("Oh dear, something went wrong %sn", s  trerror(errno));
throw std::runtime_error("writing from source to pipe failed");
}
actually_read = splice(buffer_pipe[0],
NULL,
dest_fd,
NULL,
actually_read,
SPLICE_F_MORE | SPLICE_F_MOVE);
}

注意:

所有对/sys/kernel/debug/跟踪的访问都是用sudo 完成的

经过大量的故障排除,我已经解决了这个问题。拼接不会从一个"小"字中读取小于一个页面大小(4096字节(的数据;生的";文件,该文件是二进制数据流。提供小于页面大小将导致"页面大小";无效的参数"errno。

最新更新