[PATCH] D138334: Improve llvm-symbolizer search logic for symlink

jeffrey tan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 18 15:15:38 PST 2022


yinghuitan created this revision.
yinghuitan added reviewers: clayborg, labath, jingham, kusmour.
Herald added a subscriber: hiraditya.
Herald added a project: All.
yinghuitan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

I found our internal usage of LLDB failed symbolicate on Linux even though 
llvm-symbolizer is next to it. Turns out that our lldb is distributed as 
as symbolic link so printSymbolizedStackTrace() will try to find the 
llvm-symbolizer next to the symlink instead of the real executable parent
directory.

This patch fixes this issue by adding an extra search path after resolving 
symlink.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138334

Files:
  llvm/lib/Support/Signals.cpp


Index: llvm/lib/Support/Signals.cpp
===================================================================
--- llvm/lib/Support/Signals.cpp
+++ llvm/lib/Support/Signals.cpp
@@ -136,6 +136,20 @@
   return format_hex((uint64_t)PC, PtrWidth);
 }
 
+/// Resolve \param File to its real path if it is a symlink
+/// and return its parent path.
+static StringRef resolveSymlinkParent(StringRef File) {
+  StringRef ResolvedParent;
+  if (!llvm::sys::fs::is_symlink_file(File))
+    return ResolvedParent;
+
+  llvm::SmallString<128> Resolved;
+  if (llvm::sys::fs::real_path(File, Resolved))
+    return ResolvedParent;
+
+  return llvm::sys::path::parent_path(Resolved);
+}
+
 /// Helper that launches llvm-symbolizer and symbolizes a backtrace.
 LLVM_ATTRIBUTE_USED
 static bool printSymbolizedStackTrace(StringRef Argv0, void **StackTrace,
@@ -156,7 +170,13 @@
     LLVMSymbolizerPathOrErr = sys::findProgramByName(Path);
   } else if (!Argv0.empty()) {
     StringRef Parent = llvm::sys::path::parent_path(Argv0);
+    std::vector<StringRef> paths;
+    if (!Parent.empty())
+      paths.push_back(Parent);
+    Parent = resolveSymlinkParent(Argv0);
     if (!Parent.empty())
+      paths.push_back(Parent);
+    if (!paths.empty())
       LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent);
   }
   if (!LLVMSymbolizerPathOrErr)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138334.476613.patch
Type: text/x-patch
Size: 1350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221118/3f28f021/attachment.bin>


More information about the llvm-commits mailing list