{"record":{"id":"4abdb1987d61dd44","repo":"Tencent/matrix","slug":"failed-to-create-stack-data","errorCode":null,"errorMessage":"Failed to create stack.data","messagePattern":"Failed to create stack\\.data","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":112,"sourceCode":"    } else {\n      file_name = \"stack.data\";\n    }\n\n    // Do this first, so if it fails, we don't create the file.\n    uint64_t sp_start = stacks[i].first;\n    uint64_t sp_end = stacks[i].second;\n    std::vector<uint8_t> buffer(sp_end - sp_start);\n    auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);\n    if (!process_memory->Read(sp_start, buffer.data(), buffer.size())) {\n      printf(\"Unable to read stack data.\\n\");\n      return false;\n    }\n\n    printf(\"Saving the stack 0x%\" PRIx64 \"-0x%\" PRIx64 \"\\n\", sp_start, sp_end);\n\n    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file_name.c_str(), \"w+\"), &fclose);\n    if (fp == nullptr) {\n      perror(\"Failed to create stack.data\");\n      return false;\n    }\n\n    size_t bytes = fwrite(&sp_start, 1, sizeof(sp_start), fp.get());\n    if (bytes != sizeof(sp_start)) {\n      printf(\"Failed to write sp_start data: sizeof(sp_start) %zu, written %zu\\n\", sizeof(sp_start),\n             bytes);\n      return false;\n    }\n\n    bytes = fwrite(buffer.data(), 1, buffer.size(), fp.get());\n    if (bytes != buffer.size()) {\n      printf(\"Failed to write all stack data: stack size %zu, written %zu\\n\", buffer.size(), bytes);\n      return false;\n    }\n  }\n\n  return true;","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-backtrace/src/main/cpp/external/libunwindstack/tools/unwind_for_offline.cpp#L94-L130","documentation":"SaveStack() in unwind_for_offline writes the stack memory range (starting with sp_start) to 'stack.data' via fopen(...,\"w+\"). If the file cannot be created, it reports 'Failed to create stack.data' through perror and returns false, aborting the offline-unwind data capture. This typically fails for the same environmental reasons as the sibling regs.txt/maps.txt saves.","triggerScenarios":"fopen of stack.data returns nullptr while saving the stack range 0x..-0x.. during SaveData: unwritable cwd, read-only filesystem, disk full, or a directory named stack.data in the way.","commonSituations":"Capturing offline stack data on an Android device from a protected directory, running under SELinux restrictions, or with a full filesystem when debugging native crashes with Matrix backtrace tooling.","solutions":["Run the tool from a writable directory such as /data/local/tmp","Free disk space or fix permissions reported by perror","Remove any directory/file named stack.data that blocks creation","Redirect output path in the tool to a user-writable location"],"exampleFix":"// before\nfp(fopen(file_name.c_str(), \"w+\"), &fclose);\n// after\nstd::string out = \"/data/local/tmp/\" + file_name;\nfp(fopen(out.c_str(), \"w+\"), &fclose);","handlingStrategy":"validation","validationCode":"if (access(\".\", W_OK) != 0) { fprintf(stderr, \"cwd not writable for stack.data\\n\"); return 1; }","typeGuard":null,"tryCatchPattern":"FILE *fp = fopen(file_name.c_str(), \"w+\");\nif (!fp) { perror(\"Failed to create stack.data\"); fp = fopen((fallback_dir + \"/stack.data\").c_str(), \"w+\"); }\nif (!fp) return false;","preventionTips":["cd to a writable directory before invoking the tool","Free disk space ahead of large stack captures","Pre-check errno path on failure with perror and retry with an alternate directory","Don't pre-create files/dirs named stack.data in the working path"],"tags":["file-io","android","unwind","debug-tools","stack-capture"],"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"}