Skip to content

fix(trace-exec): reject invalid command line arguments and add support builtin testing - #102

Open
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:test/add-mock-trace-exec-BUILTIN
Open

fix(trace-exec): reject invalid command line arguments and add support builtin testing#102
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:test/add-mock-trace-exec-BUILTIN

Conversation

@yuKing123-king

@yuKing123-king yuKing123-king commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

对本次trace-exec工具修改的内容

  1. 补齐 BUILTIN 测试入口,改为通过返回值处理参数解析结果
  2. 增加工具启动后的运行提示
  3. 增添用户传入异常参数时会报错反馈的功能,原来的代码如下:
void parse_args(int argc, char **argv)
{
	int opt, opt_idx;
	optind = 1;
	std::string sopts = long_opt2short_opt(lopts); // Convert long options to
												   // short options
	while ((opt = getopt_long(argc, argv, sopts.c_str(), lopts, &opt_idx)) > 0)
	{
		switch (opt)
		{
		case 'u': // UID option
			rule.uid = strtol(optarg, NULL, 10);
			break;
		case 'd': // Depth option
			rule.depth = strtol(optarg, NULL, 10);
			break;
		case 'h': // Help option
			Usage(argv[0]);
			exit(0);
			break;
		case 't': // Target path option
			strncpy(rule.target_path, optarg, PATH_MAX);
			rule.target_path[PATH_MAX - 1] = 0;
			break;
		default: // Invalid option
			Usage(argv[0]);
			exit(-1);
			break;
		}
	}
}

当用户传入-d -u的参数是负数或字母或极大值时,不会报错反馈给用户,导致工具状态异常,但是用户侧得不到反馈。现已增加参数异常报错反馈

@yuKing123-king
yuKing123-king force-pushed the test/add-mock-trace-exec-BUILTIN branch from e4ead48 to 129eca4 Compare July 21, 2026 02:37

@xu-lang xu-lang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加这些为了做什么,解决什么问题

Comment thread observe/trace-exec.cpp Outdated
@yuKing123-king
yuKing123-king force-pushed the test/add-mock-trace-exec-BUILTIN branch from 129eca4 to 86353b9 Compare July 21, 2026 07:52
@yuKing123-king

Copy link
Copy Markdown
Contributor Author

加这些为了做什么,解决什么问题

  • 加 BUILTIN 模式,本质上是在给这些 eBPF 工具补一个“测试驱动入口”,让工具逻辑和真实内核/BPF 环境解耦,因为正常运行时,这些工具依赖: 真正的 BPF skeleton open/load/attach、真正的 ring buffer、真正的内核事件、真实 stdout/文件输出、信号、线程、阻塞轮询,但是这些东西在单测里都不稳定,很多还根本没法精确构造。

  • 所以BUILTIN 模式把“参数解析、规则装载、事件过滤、日志格式化、输出行为”这些核心逻辑保留下来,同时把“事件来源”替换成测试线程可注入的 mock 事件。把原本依赖真实内核事件的 eBPF 工具,改造成“主流程仍然真实、事件来源可控替换”的测试模式,加了 BUILTIN 后,可以让测试走的是xxx_main() 主路径,与正常工具不同的,只是事件不是来自内核,而是测试自己定义事件,然后主动投递。这样才能真正验证:帮助输出、非法参数处理、默认规则、自定义规则写入 filter map、事件是否按规则被过滤、日志是否最终被输出到 stdout 或 outfile等,依据不同工具的功能去测试不同的方面。

@yuKing123-king
yuKing123-king force-pushed the test/add-mock-trace-exec-BUILTIN branch 2 times, most recently from f1d78c1 to bfe1d80 Compare July 31, 2026 03:15
@yuKing123-king yuKing123-king changed the title test: add support builtin testing and improve runtime feedback fix(trace-exec): reject invalid command line arguments and add support builtin testing Jul 31, 2026
Signed-off-by: Wang Yu <wangyu6@uniontech.com>
@yuKing123-king
yuKing123-king force-pushed the test/add-mock-trace-exec-BUILTIN branch from bfe1d80 to e54df87 Compare July 31, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants