Tencent/matrix · error

Failed to create stack.data

Error message

Failed to create stack.data

What it means

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.

Solutions

  1. Run the tool from a writable directory such as /data/local/tmp
  2. Free disk space or fix permissions reported by perror
  3. Remove any directory/file named stack.data that blocks creation
  4. Redirect output path in the tool to a user-writable location

Example fix

// before
fp(fopen(file_name.c_str(), "w+"), &fclose);
// after
std::string out = "/data/local/tmp/" + file_name;
fp(fopen(out.c_str(), "w+"), &fclose);
Defensive patterns

Strategy: validation

Validate before calling

if (access(".", W_OK) != 0) { fprintf(stderr, "cwd not writable for stack.data\n"); return 1; }

Try / catch

FILE *fp = fopen(file_name.c_str(), "w+");
if (!fp) { perror("Failed to create stack.data"); fp = fopen((fallback_dir + "/stack.data").c_str(), "w+"); }
if (!fp) return false;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/4abdb1987d61dd44. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-backtrace/src/main/cpp/external/libunwindstack/tools/unwind_for_offline.cpp:112

    } else {
      file_name = "stack.data";
    }

    // Do this first, so if it fails, we don't create the file.
    uint64_t sp_start = stacks[i].first;
    uint64_t sp_end = stacks[i].second;
    std::vector<uint8_t> buffer(sp_end - sp_start);
    auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
    if (!process_memory->Read(sp_start, buffer.data(), buffer.size())) {
      printf("Unable to read stack data.\n");
      return false;
    }

    printf("Saving the stack 0x%" PRIx64 "-0x%" PRIx64 "\n", sp_start, sp_end);

    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file_name.c_str(), "w+"), &fclose);
    if (fp == nullptr) {
      perror("Failed to create stack.data");
      return false;
    }

    size_t bytes = fwrite(&sp_start, 1, sizeof(sp_start), fp.get());
    if (bytes != sizeof(sp_start)) {
      printf("Failed to write sp_start data: sizeof(sp_start) %zu, written %zu\n", sizeof(sp_start),
             bytes);
      return false;
    }

    bytes = fwrite(buffer.data(), 1, buffer.size(), fp.get());
    if (bytes != buffer.size()) {
      printf("Failed to write all stack data: stack size %zu, written %zu\n", buffer.size(), bytes);
      return false;
    }
  }

  return true;

View on GitHub (pinned to 3b8293bd65)