Skip to content

Conversation

@medismailben
Copy link
Member

This patch adds the ability to open URLs from lldb on macOS. This can be a website URL or a deep link to a particular application.

rdar://143327616

This patch adds the ability to open URLs from lldb on macOS. This can be a
website URL or a deep link to a particular application.

rdar://143327616

Signed-off-by: Med Ismail Bennani <[email protected]>
@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-lldb

Author: Med Ismail Bennani (medismailben)

Changes

This patch adds the ability to open URLs from lldb on macOS. This can be a website URL or a deep link to a particular application.

rdar://143327616


Full diff: https://github.com/llvm/llvm-project/pull/125893.diff

2 Files Affected:

  • (modified) lldb/include/lldb/Host/Host.h (+2)
  • (modified) lldb/source/Host/macosx/objcxx/Host.mm (+32)
diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h
index d8113a5fceeada..4e19d15b06743e 100644
--- a/lldb/include/lldb/Host/Host.h
+++ b/lldb/include/lldb/Host/Host.h
@@ -259,6 +259,8 @@ class Host {
                                               const FileSpec &file_spec,
                                               uint32_t line_no);
 
+  static llvm::Error OpenURL(llvm::StringRef url);
+
   /// Check if we're running in an interactive graphical session.
   ///
   /// \return
diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm
index a99ffbc3a3faca..5b3d04a6b587b4 100644
--- a/lldb/source/Host/macosx/objcxx/Host.mm
+++ b/lldb/source/Host/macosx/objcxx/Host.mm
@@ -452,6 +452,38 @@ repeat with the_window in (get windows)\n\
 #endif // TARGET_OS_OSX
 }
 
+llvm::Error Host::OpenURL(llvm::StringRef url) {
+#if !TARGET_OS_OSX
+  return llvm::errorCodeToError(
+      std::error_code(ENOTSUP, std::system_category()));
+#else  // !TARGET_OS_OSX
+  if (url.empty())
+    return llvm::createStringError("Cannot open empty URL.");
+
+  LLDB_LOG(GetLog(LLDBLog::Host), "Opening URL: {0}", url);
+
+  CFCString url_cfstr(url.data(), kCFStringEncodingUTF8);
+  CFCReleaser<CFURLRef> cfurl = ::CFURLCreateWithString(
+      /*allocator=*/NULL,
+      /*URLString*/ url_cfstr.get(),
+      /*baseURL=*/NULL);
+
+  if (!cfurl.get())
+    return llvm::createStringError(
+        llvm::formatv("could not create CFURL from URL \"{0}\"", url));
+
+  OSStatus error = ::LSOpenCFURLRef(
+      /*inURL=*/cfurl.get(),
+      /*outLaunchedURL=*/NULL);
+
+  if (error != noErr)
+    return llvm::createStringError(
+        llvm::formatv("LSOpenCFURLRef failed: error {0:x}", error));
+
+  return llvm::Error::success();
+#endif // TARGET_OS_OSX
+}
+
 bool Host::IsInteractiveGraphicSession() {
 #if !TARGET_OS_OSX
   return false;

@medismailben medismailben merged commit 41d5d2b into llvm:main Feb 5, 2025
7 of 8 checks passed
medismailben added a commit to medismailben/llvm-project that referenced this pull request Feb 5, 2025
This patch adds the ability to open URLs from lldb on macOS. This can be
a website URL or a deep link to a particular application.

rdar://143327616

Signed-off-by: Med Ismail Bennani <[email protected]>
(cherry picked from commit 41d5d2b)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Feb 5, 2025
[lldb/Host] Add ability to open URLs on macOS (llvm#125893)
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
This patch adds the ability to open URLs from lldb on macOS. This can be
a website URL or a deep link to a particular application.

rdar://143327616

Signed-off-by: Med Ismail Bennani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants