Skip to content

[lldb] Fix FindProcessImpl() for iOS simulators #139174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

royitaqi
Copy link
Contributor

@royitaqi royitaqi commented May 8, 2025

Benefit

This patch fixes:

  1. After platform select ios-simulator, platform process list will now print processes which are running in the iOS simulator. Previously, no process will be listed.
  2. After platform select ios-simulator, platform attach --name <name> will succeed. Previously, it will error out saying no process is found.

Changes

  1. During the process listing, add aarch64 to the list of CPU types for which iOS simulators are checked for.
  2. Fix a bug in the code which checks for simulators, where for a process running on simulator, the original code will find the desired environment variable and set the OS to iOS, but then the immediate next environment variable will set it back to macOS.
  3. For processes running on simulator, set the triple's Environment to Simulator, so that such processes can pass the filtering in this line. The original code leave it as the default UnknownEnvironment.

Manual test

With this patch:

royshi-mac-home ~/public_llvm/build % bin/lldb
(lldb) platform select ios-simulator

(lldb) platform process list
240 matching processes were found on "ios-simulator"

PID    PARENT USER       TRIPLE                         NAME
====== ====== ========== ============================== ============================
40511  28844  royshi     arm64-apple-ios-simulator      FocusPlayground // my toy iOS app running on simulator
... // omit
28844  1      royshi     arm64-apple-ios-simulator      launchd_sim

(lldb) process attach --name FocusPlayground
Process 40511 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x0000000104e3cb70 libsystem_kernel.dylib`mach_msg2_trap + 8
libsystem_kernel.dylib`mach_msg2_trap:
->  0x104e3cb70 <+8>: ret
... // omit

Without this patch:

royshi-mac-home ~/public_llvm/build % bin/lldb
(lldb) platform select ios-simulator

(lldb) platform process list
error: no processes were found on the "ios-simulator" platform

(lldb) process attach --name FocusPlayground
error: attach failed: could not find a process named FocusPlayground

Unittest

I did a couple of code searches (1, 2) and didn't seem to find any existing tests.

Any suggestion about how to test this?

@royitaqi royitaqi requested a review from JDevlieghere as a code owner May 8, 2025 23:47
@llvmbot llvmbot added the lldb label May 8, 2025
@royitaqi royitaqi requested review from clayborg and jeffreytan81 May 8, 2025 23:48
@llvmbot
Copy link
Member

llvmbot commented May 8, 2025

@llvm/pr-subscribers-lldb

Author: None (royitaqi)

Changes

Benefit

This patch fixes:

  1. After platform select ios-simulator, platform process list will now print processes which are running in the iOS simulator. Previously, no process will be listed.
  2. After platform select ios-simulator, platform attach --name &lt;name&gt; will succeed. Previously, it will error out saying no process is found.

Changes

  1. During the process listing, add aarch64 to the list of CPU types for which iOS simulators are checked for.
  2. Fix a bug in the code which checks for simulators, where for a process running on simulator, the original code will find the desired environment variable and set the OS to iOS, but then the immediate next environment variable will set it back to macOS.
  3. For processes running on simulator, set the triple's Environment to Simulator, so that such processes can pass the filtering in this line. The original code leave it as the default UnknownEnvironment.

Manual test

With this patch:

royshi-mac-home ~/public_llvm/build % bin/lldb
(lldb) platform select ios-simulator

(lldb) platform process list
240 matching processes were found on "ios-simulator"

PID    PARENT USER       TRIPLE                         NAME
====== ====== ========== ============================== ============================
40511  28844  royshi     arm64-apple-ios-simulator      FocusPlayground // my toy iOS app running on simulator
... // omit
28844  1      royshi     arm64-apple-ios-simulator      launchd_sim

(lldb) process attach --name FocusPlayground
Process 40511 stopped
* thread #<!-- -->1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #<!-- -->0: 0x0000000104e3cb70 libsystem_kernel.dylib`mach_msg2_trap + 8
libsystem_kernel.dylib`mach_msg2_trap:
-&gt;  0x104e3cb70 &lt;+8&gt;: ret
... // omit

Without this patch:

royshi-mac-home ~/public_llvm/build % bin/lldb
(lldb) platform select ios-simulator

(lldb) platform process list
error: no processes were found on the "ios-simulator" platform

(lldb) process attach --name FocusPlayground
error: attach failed: could not find a process named FocusPlayground

Unittest

I did a couple of code searches (1, 2) and didn't seem to find any existing tests.

Any suggestion about how to test this?


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

1 Files Affected:

  • (modified) lldb/source/Host/macosx/objcxx/Host.mm (+15-10)
diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm
index e187bf98188ae..e8a1c597eea53 100644
--- a/lldb/source/Host/macosx/objcxx/Host.mm
+++ b/lldb/source/Host/macosx/objcxx/Host.mm
@@ -595,7 +595,9 @@ DataExtractor data(arg_data.GetBytes(), arg_data_size,
       const llvm::Triple::ArchType triple_arch = triple.getArch();
       const bool check_for_ios_simulator =
           (triple_arch == llvm::Triple::x86 ||
-           triple_arch == llvm::Triple::x86_64);
+           triple_arch == llvm::Triple::x86_64 ||
+           triple_arch == llvm::Triple::aarch64);
+
       const char *cstr = data.GetCStr(&offset);
       if (cstr) {
         process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
@@ -621,22 +623,25 @@ DataExtractor data(arg_data.GetBytes(), arg_data_size,
           }
 
           Environment &proc_env = process_info.GetEnvironment();
+          bool is_simulator = false;
           while ((cstr = data.GetCStr(&offset))) {
             if (cstr[0] == '\0')
               break;
 
-            if (check_for_ios_simulator) {
-              if (strncmp(cstr, "SIMULATOR_UDID=", strlen("SIMULATOR_UDID=")) ==
-                  0)
-                process_info.GetArchitecture().GetTriple().setOS(
-                    llvm::Triple::IOS);
-              else
-                process_info.GetArchitecture().GetTriple().setOS(
-                    llvm::Triple::MacOSX);
-            }
+            if (check_for_ios_simulator &&
+                strncmp(cstr, "SIMULATOR_UDID=", strlen("SIMULATOR_UDID=")) ==
+                    0)
+              is_simulator = true;
 
             proc_env.insert(cstr);
           }
+          llvm::Triple &triple = process_info.GetArchitecture().GetTriple();
+          if (is_simulator) {
+            triple.setOS(llvm::Triple::IOS);
+            triple.setEnvironment(llvm::Triple::Simulator);
+          } else {
+            triple.setOS(llvm::Triple::MacOSX);
+          }
           return true;
         }
       }

@JDevlieghere
Copy link
Member

Could we test this in TestSimulatorPlatform.py?

@royitaqi
Copy link
Contributor Author

royitaqi commented May 9, 2025

Could we test this in TestSimulatorPlatform.py?

@JDevlieghere Thanks for pointer. It seems the tests in that file are all skipped because of this bug number: rdar://76995109.

E.g.

UNSUPPORTED: LLDB (/Users/royshi/public_llvm/build/bin/clang-arm64) :: test_ios (TestSimulatorPlatform.TestSimulatorPlatformLaunching) (skipping unconditionally [rdar://76995109])

Did a bit internet search and couldn't find how to find more info about this bug or why these tests are all skipped. Not sure if I should un-skip them.

What's your advice on my next steps?

@JDevlieghere
Copy link
Member

Could we test this in TestSimulatorPlatform.py?

@JDevlieghere Thanks for pointer. It seems the tests in that file are all skipped because of this bug number: rdar://76995109.

E.g.

UNSUPPORTED: LLDB (/Users/royshi/public_llvm/build/bin/clang-arm64) :: test_ios (TestSimulatorPlatform.TestSimulatorPlatformLaunching) (skipping unconditionally [rdar://76995109])

Did a bit internet search and couldn't find how to find more info about this bug or why these tests are all skipped. Not sure if I should un-skip them.

Ha, that's my radar, and it's no longer relevant. I bisected an issue with the test suite to that particular test, but the last comment says that it wasn't the culprit after all, so there's no reason it should still be disabled.

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.

3 participants