================
@@ -797,20 +822,75 @@ void MinidumpFileBuilder::AddLinuxFileStreams(
   }
 }
 
-Status MinidumpFileBuilder::Dump(lldb::FileUP &core_file) const {
-  constexpr size_t header_size = sizeof(llvm::minidump::Header);
-  constexpr size_t directory_size = sizeof(llvm::minidump::Directory);
+Status MinidumpFileBuilder::AddMemory(const ProcessSP &process_sp,
+                                      SaveCoreStyle core_style) {
+  Status error;
+
+  Process::CoreFileMemoryRanges ranges_for_memory_list;
+  error = process_sp->CalculateCoreFileSaveRanges(
+      SaveCoreStyle::eSaveCoreStackOnly, ranges_for_memory_list);
+  if (error.Fail()) {
+    return error;
+  }
+
+  std::set<addr_t> stack_ranges;
+  for (const auto &core_range : ranges_for_memory_list) {
+    stack_ranges.insert(core_range.range.start());
+  }
+  // We leave a little padding for dictionary and any other metadata we would
+  // want. Also so that we can put the header of the memory list 64 in 32b 
land,
+  // because the directory requires a 32b RVA.
+  Process::CoreFileMemoryRanges ranges;
+  error = process_sp->CalculateCoreFileSaveRanges(core_style, ranges);
+  if (error.Fail()) {
+    return error;
+  }
+
+  uint64_t total_size =
+      256 + (ranges.size() * sizeof(llvm::minidump::MemoryDescriptor_64));
+  // Take all the memory that will fit in the 32b range.
+  for (int i = ranges.size() - 1; i >= 0; i--) {
----------------
clayborg wrote:

Why are we iterating over the ranges in reverse order? It might be easier to 
iterate over the ranges in the normal order and then adding the right ranges to 
either the `ranges_32` or `ranges_64` list as needed? So this could be just:
```
for (const auto &range: all_ranges) {
  lldb::offset_t range_32_size = range.range.size() + 
sizeof(llvm::minidump::MemoryDescriptor);
  if (stack_start_addresses.count(ranges[i].range.start()) > 0)
    continue; // Stack range that was already added to range_32
  if (total_size + range_32_size < UINT32_MAX) {
    ranges_32.push_back(range);
    total_size += range_32_size;
  } else {
    ranges_64.push_back(range);
  }
}
```
    

https://github.com/llvm/llvm-project/pull/95312
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to