mtrace在ArchLinux无法生成数据文件但Debian中正常工作
无法生成mtrace数据文件
/tmp/t的排查与解决 问题描述
按照ArchLinux手册中的mtrace示例操作,始终无法生成指定的mtrace数据文件/tmp/t。
环境信息
- mtrace版本:
mtrace (GNU libc) 2.36 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Written by Ulrich Drepper <drepper@gnu.org>
- glibc版本:
local/glibc 2.36-6 GNU C Library
- gcc版本:
gcc (GCC) 12.2.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- 系统版本:
Linux ArchLinux 6.0.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 15 Oct 2022 14:00:49 +0000 x86_64 GNU/Linux
排查步骤与解决方法
- 确认编译选项正确性
mtrace依赖程序编译时启用内存跟踪支持,必须添加-g调试选项并链接libmcheck库。编译命令需确保为:
gcc -g -o test test.c -lmcheck
仅用-g未链接libmcheck会导致mtrace无法生成跟踪文件。
- 检查环境变量与执行流程
运行程序前需设置MALLOC_TRACE指向目标文件,且确保当前用户对/tmp有读写权限:
export MALLOC_TRACE=/tmp/t
执行程序后,需等待程序正常退出(异常终止会导致数据未写入),再执行mtrace解析命令:
./test mtrace ./test /tmp/t
- 验证文件权限与系统限制
- 检查
/tmp目录权限,确保为1777(粘滞位开启,允许所有用户读写):ls -ld /tmp - 排查系统是否启用
tmpfs或systemd-tmpfiles规则,导致/tmp文件被自动清理或写入受限。
- 测试最小化示例代码
用最简代码验证mtrace功能,排除原代码逻辑问题:
#include <stdlib.h> #include <mcheck.h> int main() { mtrace(); char *ptr = malloc(10); // 故意不释放,用于测试跟踪效果 return 0; }
编译运行流程:
gcc -g -o mtrace_test mtrace_test.c -lmcheck export MALLOC_TRACE=/tmp/t ./mtrace_test mtrace ./mtrace_test /tmp/t
若此示例能生成/tmp/t,说明原代码存在问题;若仍失败,可尝试重新安装glibc修复可能的损坏:
sudo pacman -S glibc
内容的提问来源于stack exchange,提问作者Johan Chane
相关产品推荐
相关产品推荐

