| 123 | |
| 124 | /* |
| 125 | * Keep inaccessible memory as zero bytes. |
| 126 | * This preserves the original virtual offsets. |
| 127 | */ |
| 128 | memset(buffer.data(), 0, chunkSize); |
| 129 | |
| 130 | memIO.Read(current, buffer.data(), chunkSize, MemMode::SkipInaccessiblePages); |
| 131 | |
| 132 | /* |
| 133 | * Always write the full chunk. |
| 134 | */ |
| 135 | size_t written = destIO.write(buffer.data(), chunkSize); |
| 136 | |
| 137 | if (written != chunkSize) |
| 138 | { |
| 139 | KITTY_LOGE("dumpMemRange: Write failed at %p (%zu/%zu).", (void *)current, written, chunkSize); |
| 140 | destIO.close(); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | current += chunkSize; |
| 145 | } |
| 146 | |
| 147 | destIO.close(); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool KittyMemoryMgr::dumpMemFileMappings(const std::string &memFile, const std::string &destination) const |
| 152 | { |
| 153 | if (!isMemValid() || memFile.empty() || destination.empty()) |
| 154 | return false; |
| 155 | |
| 156 | auto fileMappings = KittyMemoryEx::getFileMappings(_pid, memFile); |
| 157 | if (fileMappings.empty()) |
| 158 | return false; |
| 159 | |
| 160 | std::string outPath = destination; |
| 161 | kt_stat64_t st{}; |
| 162 | |
| 163 | if (kt_stat64(destination.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) |
| 164 | { |
| 165 | // Destination is an existing directory. |
| 166 | if (access(destination.c_str(), W_OK) != 0) |