Author: Felipe de Azevedo Piovezan
Date: 2026-07-15T09:21:08+01:00
New Revision: 2af345af8dbffa975589b5f774ab006f372b4c00

URL: 
https://github.com/llvm/llvm-project/commit/2af345af8dbffa975589b5f774ab006f372b4c00
DIFF: 
https://github.com/llvm/llvm-project/commit/2af345af8dbffa975589b5f774ab006f372b4c00.diff

LOG: [lldb-server] Add type info for qMemoryinfo reponse (#209235)

Use the name of the memory region to report the known cases where an
address points to stack/heap: "[stack]" or "[heap"].

According to [1], the "[stack]" name is the main thread's stack region:
```
  [stack]
                     The initial process's (also known as the main
                     thread's) stack.
```

For other threads, we can't know their stack region because this field
got removed in newer kernels:

```
 [stack:tid] (from Linux 3.4 to Linux 4.4)
        A thread's stack (where the tid is a thread ID).  It
        corresponds to the /proc/pid/task/tid/ path.  This
        field was removed in Linux 4.5, since providing this
        information for a process with large numbers of
        threads is expensive.
```

So we can only set "isStack" to Yes in some cases, and to "No" when the
name is "[heap]". If there is no name, or the name is something else, we
keep the "don't know" answer.

[1]: https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html

Added: 
    

Modified: 
    lldb/include/lldb/Target/MemoryRegionInfo.h
    lldb/source/Plugins/Process/Utility/LinuxProcMaps.cpp
    lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
    lldb/unittests/Process/Utility/LinuxProcMapsTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/MemoryRegionInfo.h 
b/lldb/include/lldb/Target/MemoryRegionInfo.h
index 16faf6ed9d64b..e8a16a3ab9757 100644
--- a/lldb/include/lldb/Target/MemoryRegionInfo.h
+++ b/lldb/include/lldb/Target/MemoryRegionInfo.h
@@ -142,7 +142,10 @@ class MemoryRegionInfo {
 
   LazyBool IsStackMemory() const { return m_is_stack_memory; }
 
-  void SetIsStackMemory(LazyBool val) { m_is_stack_memory = val; }
+  MemoryRegionInfo &SetIsStackMemory(LazyBool val) {
+    m_is_stack_memory = val;
+    return *this;
+  }
 
   void SetPageSize(int pagesize) { m_pagesize = pagesize; }
 

diff  --git a/lldb/source/Plugins/Process/Utility/LinuxProcMaps.cpp 
b/lldb/source/Plugins/Process/Utility/LinuxProcMaps.cpp
index d952b7dbdb816..350e44d3547ff 100644
--- a/lldb/source/Plugins/Process/Utility/LinuxProcMaps.cpp
+++ b/lldb/source/Plugins/Process/Utility/LinuxProcMaps.cpp
@@ -114,8 +114,17 @@ ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef 
maps_line,
 
   line_extractor.SkipSpaces();
   const char *name = line_extractor.Peek();
-  if (name)
+  if (name) {
     region.SetName(name);
+    // /proc/maps labels only the main thread's stack; thread stacks are
+    // anonymous and the default MemoryRegionInfo::eDontKnow is kept.
+    if (llvm::StringRef(name) == "[stack]")
+      region.SetIsStackMemory(eLazyBoolYes);
+    else if (llvm::StringRef(name) == "[heap]")
+      region.SetIsStackMemory(eLazyBoolNo);
+    else
+      region.SetIsStackMemory(eLazyBoolDontKnow);
+  }
 
   return region;
 }

diff  --git 
a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp 
b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
index 769705588fbdf..4f11cf8c5475e 100644
--- 
a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
+++ 
b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
@@ -2971,6 +2971,10 @@ 
GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo(
 
     if (std::optional<unsigned> protection_key = 
region_info.GetProtectionKey())
       response.Printf("protection-key:%" PRIu32 ";", *protection_key);
+
+    LazyBool is_stack = region_info.IsStackMemory();
+    if (is_stack != eLazyBoolDontKnow)
+      response.Printf("type: %s", is_stack ? "stack" : "heap");
   }
 
   return SendPacketNoLock(response.GetString());

diff  --git a/lldb/unittests/Process/Utility/LinuxProcMapsTest.cpp 
b/lldb/unittests/Process/Utility/LinuxProcMapsTest.cpp
index fcfd76cc67960..84aa206b4e50a 100644
--- a/lldb/unittests/Process/Utility/LinuxProcMapsTest.cpp
+++ b/lldb/unittests/Process/Utility/LinuxProcMapsTest.cpp
@@ -99,7 +99,18 @@ INSTANTIATE_TEST_SUITE_P(
                 MemoryRegionInfo(make_range(0x55a4512f7000, 0x55a451b68000),
                                  eLazyBoolYes, eLazyBoolYes, eLazyBoolNo,
                                  eLazyBoolNo, eLazyBoolYes,
-                                 ConstString("[heap]")),
+                                 ConstString("[heap]"))
+                    .SetIsStackMemory(eLazyBoolNo),
+            },
+            ""),
+        std::make_tuple(
+            "7ffcad8f7000-7ffcad918000 rw-p 00000000 00:00 0    [stack]",
+            MemoryRegionInfos{
+                MemoryRegionInfo(make_range(0x7ffcad8f7000, 0x7ffcad918000),
+                                 eLazyBoolYes, eLazyBoolYes, eLazyBoolNo,
+                                 eLazyBoolNo, eLazyBoolYes,
+                                 ConstString("[stack]"))
+                    .SetIsStackMemory(eLazyBoolYes),
             },
             ""),
         // Multiple entries


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to