Tencent/matrix · error
Failed to create file regs.txt
Error message
Failed to create file regs.txt
What it means
In libunwindstack's unwind_for_offline tool, SaveRegs() opens 'regs.txt' in the current working directory with fopen("regs.txt","w+") to dump the captured register values. If the file cannot be created (fopen returns nullptr), the tool reports 'Failed to create file regs.txt' via perror and aborts the save. perror appends the errno reason (e.g. Permission denied, Read-only file system) to the message.
Solutions
- cd to a writable directory (e.g. /data/local/tmp) before running unwind_for_offline
- Check and fix errno details printed by perror: fix permissions (chmod/chown) or free disk space
- Remove or rename an existing regs.txt that is a directory or otherwise unopenable
- Run with a user/SELinux context permitted to write in the target directory
Example fix
// before
fp(fopen("regs.txt", "w+"), &fclose);
// after (run from a writable dir, or pass an output path)
chdir("/data/local/tmp");
fp(fopen("regs.txt", "w+"), &fclose); Defensive patterns
Strategy: validation
Validate before calling
if (access(".", W_OK) != 0 || access("regs.txt", F_OK) == 0 && access("regs.txt", W_OK) != 0) {
fprintf(stderr, "cwd not writable for regs.txt\n"); return 1;
} Try / catch
FILE *fp = fopen("regs.txt", "w+");
if (!fp) { perror("Failed to create file regs.txt"); /* fallback dir */ fp = fopen("/data/local/tmp/regs.txt", "w+"); }
if (!fp) return false; Prevention
- Always run unwind tools from a writable directory like /data/local/tmp
- Check disk space before captures
- Verify SELinux context allows writes for your shell user
- Avoid running under read-only mounts
When it happens
Trigger: Running the unwind_for_offline tool with a current working directory that is not writable, a read-only filesystem, disk full, or regs.txt existing as a directory/locked file; SaveData calls SaveRegs right after collecting registers from a core dump or live process.
Common situations: Running the tool from a root-owned or read-only directory on a device/emulator shell, SELinux blocking writes in /data or /system paths, or a full tmpfs when capturing offline unwind data for backtrace debugging.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
- Failed to create stack.data
- Failed to create maps.txt
- Matrix init, Matrix should not be null.
- you must init Matrix sdk first
- matrix init, application is null
AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08).
Data as JSON: /api/errors/387dd568e0d5be21.
Report an issue: GitHub.
Appendix: source
Thrown at matrix/matrix-android/matrix-backtrace/src/main/cpp/external/libunwindstack/tools/unwind_for_offline.cpp:79
return false;
}
// Allow at least 1 second to attach properly.
for (size_t i = 0; i < 1000; i++) {
siginfo_t si;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
return true;
}
usleep(1000);
}
printf("%d: Failed to stop.\n", pid);
return false;
}
bool SaveRegs(unwindstack::Regs* regs) {
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("regs.txt", "w+"), &fclose);
if (fp == nullptr) {
perror("Failed to create file regs.txt");
return false;
}
regs->IterateRegisters([&fp](const char* name, uint64_t value) {
fprintf(fp.get(), "%s: %" PRIx64 "\n", name, value);
});
return true;
}
bool SaveStack(pid_t pid, const std::vector<std::pair<uint64_t, uint64_t>>& stacks) {
for (size_t i = 0; i < stacks.size(); i++) {
std::string file_name;
if (stacks.size() != 1) {
file_name = "stack" + std::to_string(i) + ".data";
} else {
file_name = "stack.data";
}
View on GitHub (pinned to 3b8293bd65)