[Lldb-commits] [lldb] [LLDB/Coredump] Only take the Pthread from stack start to the stackpointer + red_zone (PR #92002)

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Thu May 16 10:45:25 PDT 2024


================
@@ -6335,16 +6335,51 @@ static void AddRegion(const MemoryRegionInfo &region, bool try_dirty_pages,
   ranges.push_back(CreateCoreFileMemoryRange(region));
 }
 
+static void
+SaveOffRegionsWithStackPointers(Process &process,
+                               const MemoryRegionInfos &regions,
+                               Process::CoreFileMemoryRanges &ranges,
+                               std::set<addr_t> &stack_ends) {
+  const bool try_dirty_pages = true;
+
+  // Before we take any dump, we want to save off the used portions of the stacks
+  // and mark those memory regions as saved. This prevents us from saving the unused portion
+  // of the stack below the stack pointer. Saving space on the dump.
+  for (lldb::ThreadSP thread_sp : process.GetThreadList().Threads()) {
+    if (!thread_sp)
+      continue;
+    StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
+    if (!frame_sp)
+      continue;
+    RegisterContextSP reg_ctx_sp = frame_sp->GetRegisterContext();
+    if (!reg_ctx_sp)
+      continue;
+    const addr_t sp = reg_ctx_sp->GetSP();
+    const size_t red_zone = process.GetABI()->GetRedZoneSize();
+    lldb_private::MemoryRegionInfo sp_region;
+    if (process.GetMemoryRegionInfo(sp, sp_region).Success()) {
+        const size_t stack_head = (sp - red_zone);
+        const size_t stack_size = sp_region.GetRange().GetRangeEnd() - stack_head;
+        sp_region.GetRange().SetRangeBase(stack_head);
+        sp_region.GetRange().SetByteSize(stack_size);
+        stack_ends.insert(sp_region.GetRange().GetRangeEnd());
+        AddRegion(sp_region, try_dirty_pages, ranges);
+    }
----------------
clayborg wrote:

We should check if the stack head makes sense before adusting it:
We should make sure stack_head is >= sp_region.GetRange().GetRangeBase():
```
// Adjust start of head of stack if possible 
const size_t stack_head = (sp - red_zone);
if (stack_head > sp_region.GetRange().GetRangeBase()) {
  const size_t stack_size = sp_region.GetRange().GetRangeEnd() - stack_head;
  sp_region.GetRange().SetRangeBase(stack_head);
  sp_region.GetRange().SetByteSize(stack_size);
}
stack_ends.insert(sp_region.GetRange().GetRangeEnd());
AddRegion(sp_region, try_dirty_pages, ranges);
```

https://github.com/llvm/llvm-project/pull/92002


More information about the lldb-commits mailing list