{"record":{"id":"387dd568e0d5be21","repo":"Tencent/matrix","slug":"failed-to-create-file-regs-txt","errorCode":null,"errorMessage":"Failed to create file regs.txt","messagePattern":"Failed to create file regs\\.txt","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-backtrace/src/main/cpp/external/libunwindstack/tools/unwind_for_offline.cpp","lineNumber":79,"sourceCode":"    return false;\n  }\n\n  // Allow at least 1 second to attach properly.\n  for (size_t i = 0; i < 1000; i++) {\n    siginfo_t si;\n    if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {\n      return true;\n    }\n    usleep(1000);\n  }\n  printf(\"%d: Failed to stop.\\n\", pid);\n  return false;\n}\n\nbool SaveRegs(unwindstack::Regs* regs) {\n  std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(\"regs.txt\", \"w+\"), &fclose);\n  if (fp == nullptr) {\n    perror(\"Failed to create file regs.txt\");\n    return false;\n  }\n  regs->IterateRegisters([&fp](const char* name, uint64_t value) {\n    fprintf(fp.get(), \"%s: %\" PRIx64 \"\\n\", name, value);\n  });\n\n  return true;\n}\n\nbool SaveStack(pid_t pid, const std::vector<std::pair<uint64_t, uint64_t>>& stacks) {\n  for (size_t i = 0; i < stacks.size(); i++) {\n    std::string file_name;\n    if (stacks.size() != 1) {\n      file_name = \"stack\" + std::to_string(i) + \".data\";\n    } else {\n      file_name = \"stack.data\";\n    }\n","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-backtrace/src/main/cpp/external/libunwindstack/tools/unwind_for_offline.cpp#L61-L97","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nfp(fopen(\"regs.txt\", \"w+\"), &fclose);\n// after (run from a writable dir, or pass an output path)\nchdir(\"/data/local/tmp\");\nfp(fopen(\"regs.txt\", \"w+\"), &fclose);","handlingStrategy":"validation","validationCode":"if (access(\".\", W_OK) != 0 || access(\"regs.txt\", F_OK) == 0 && access(\"regs.txt\", W_OK) != 0) {\n  fprintf(stderr, \"cwd not writable for regs.txt\\n\"); return 1;\n}","typeGuard":null,"tryCatchPattern":"FILE *fp = fopen(\"regs.txt\", \"w+\");\nif (!fp) { perror(\"Failed to create file regs.txt\"); /* fallback dir */ fp = fopen(\"/data/local/tmp/regs.txt\", \"w+\"); }\nif (!fp) return false;","preventionTips":["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"],"tags":["file-io","android","unwind","debug-tools","fopen"],"backgroundTag":"file-open-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}