[Lldb-commits] [lldb] [lldb][windows] allow longer paths than MAX_PATH in GetFileNameByLoadAddress (PR #200225)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Fri May 29 04:32:44 PDT 2026


https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/200225

>From f0bce07aec8f1681548745a7dee4963a7d146899 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 28 May 2026 18:06:45 +0100
Subject: [PATCH 1/6] [lldb][windows] allow longer paths than MAX_PATH in
 GetFileNameByLoadAddress

---
 .../Plugins/Process/Windows/Common/DebuggerThread.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index c4bfa11e9635d..660bd7bd75498 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -555,11 +555,14 @@ static std::optional<std::string> GetFileNameByLoadAddress(HANDLE hProcess,
   }
 
   // Fallback: ask the kernel for the file backing the mapping at this address.
-  std::array<wchar_t, MAX_PATH + 1> mapped_filename;
-  if (!::GetMappedFileNameW(hProcess, base_addr, mapped_filename.data(),
-                            mapped_filename.size()))
+  std::vector<wchar_t> mapped_filename(PATHCCH_MAX_CCH);
+  DWORD mapped_len = ::GetMappedFileNameW(
+      hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
+  if (!mapped_len)
     return std::nullopt;
-  return ConvertNtDevicePathToDosPath(mapped_filename);
+  std::optional<std::string> dos_path = ConvertNtDevicePathToDosPath(
+      llvm::ArrayRef<wchar_t>(mapped_filename.data(), mapped_len + 1));
+  return dos_path;
 }
 
 // Resolve the LOAD_DLL_DEBUG_INFO::lpImageName field.

>From 2cd8c0fcc9b4a417b557539b15cdb9612f6f511c Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 28 May 2026 18:18:34 +0100
Subject: [PATCH 2/6] fixup! [lldb][windows] allow longer paths than MAX_PATH
 in GetFileNameByLoadAddress

---
 lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index 660bd7bd75498..11e18a09b9d39 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -32,6 +32,7 @@
 #include "llvm/Support/raw_ostream.h"
 
 #include <optional>
+#include <pathcch.h>
 #include <psapi.h>
 
 #ifndef STATUS_WX86_BREAKPOINT

>From 8b3a9b3ad6416fff5345979cf6e51439758a1c88 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 29 May 2026 11:31:16 +0100
Subject: [PATCH 3/6] grow path buffer

---
 .../Process/Windows/Common/DebuggerThread.cpp   | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index 11e18a09b9d39..af080ceb1055f 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -556,11 +556,18 @@ static std::optional<std::string> GetFileNameByLoadAddress(HANDLE hProcess,
   }
 
   // Fallback: ask the kernel for the file backing the mapping at this address.
-  std::vector<wchar_t> mapped_filename(PATHCCH_MAX_CCH);
-  DWORD mapped_len = ::GetMappedFileNameW(
-      hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
-  if (!mapped_len)
-    return std::nullopt;
+  std::vector<wchar_t> mapped_filename(MAX_PATH + 1);
+  DWORD mapped_len = 0;
+  while (true) {
+    mapped_len = ::GetMappedFileNameW(
+        hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
+    if (SUCCESS(mapped_len))
+      break;
+    if (::GetLastError() != ERROR_MORE_DATA ||
+        mapped_filename.size() >= PATHCCH_MAX_CCH)
+      return std::nullopt;
+    mapped_filename.resize(mapped_filename.size() * 2);
+  }
   std::optional<std::string> dos_path = ConvertNtDevicePathToDosPath(
       llvm::ArrayRef<wchar_t>(mapped_filename.data(), mapped_len + 1));
   return dos_path;

>From ead406c0cfd77c02de67a5a62bdadc1a114a0577 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 29 May 2026 11:39:25 +0100
Subject: [PATCH 4/6] fixup! grow path buffer

---
 lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index af080ceb1055f..a0b2f54cb6995 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -563,7 +563,7 @@ static std::optional<std::string> GetFileNameByLoadAddress(HANDLE hProcess,
         hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
     if (SUCCESS(mapped_len))
       break;
-    if (::GetLastError() != ERROR_MORE_DATA ||
+    if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
         mapped_filename.size() >= PATHCCH_MAX_CCH)
       return std::nullopt;
     mapped_filename.resize(mapped_filename.size() * 2);

>From f10478777a019f73def44142964439a52a8155c6 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 29 May 2026 11:57:32 +0100
Subject: [PATCH 5/6] fix incorrect check

---
 lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index a0b2f54cb6995..b130acb09b804 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -561,7 +561,7 @@ static std::optional<std::string> GetFileNameByLoadAddress(HANDLE hProcess,
   while (true) {
     mapped_len = ::GetMappedFileNameW(
         hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
-    if (SUCCESS(mapped_len))
+    if (mapped_len > 0)
       break;
     if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
         mapped_filename.size() >= PATHCCH_MAX_CCH)

>From 54a27c5b5ba70f75bd555c9221ddad63848b9f99 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 29 May 2026 12:32:29 +0100
Subject: [PATCH 6/6] reverse the condition

---
 .../Plugins/Process/Windows/Common/DebuggerThread.cpp      | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
index b130acb09b804..140a70d5017c4 100644
--- a/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/DebuggerThread.cpp
@@ -558,13 +558,12 @@ static std::optional<std::string> GetFileNameByLoadAddress(HANDLE hProcess,
   // Fallback: ask the kernel for the file backing the mapping at this address.
   std::vector<wchar_t> mapped_filename(MAX_PATH + 1);
   DWORD mapped_len = 0;
-  while (true) {
+  while (mapped_filename.size() <= PATHCCH_MAX_CCH) {
     mapped_len = ::GetMappedFileNameW(
         hProcess, base_addr, mapped_filename.data(), mapped_filename.size());
-    if (mapped_len > 0)
+    if (mapped_len < mapped_filename.size())
       break;
-    if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
-        mapped_filename.size() >= PATHCCH_MAX_CCH)
+    if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
       return std::nullopt;
     mapped_filename.resize(mapped_filename.size() * 2);
   }



More information about the lldb-commits mailing list