|
| 1 | +# Debugging Rust Code in Xcode |
| 2 | + |
| 3 | +This guide explains how to debug Rust code using Xcode's LLDB debugger, which is particularly useful when you need to debug how Rust code executes in an iOS application. |
| 4 | + |
| 5 | +## Initial Setup |
| 6 | + |
| 7 | +1. Create a `.lldbinit` file in your home directory with the following content: |
| 8 | + |
| 9 | + ``` |
| 10 | + command script import <rust-toolchain-dir>/lib/rustlib/etc/lldb_lookup.py |
| 11 | + command source -s true <rust-toolchain-dir>/lib/rustlib/etc/lldb_commands |
| 12 | + ``` |
| 13 | +
|
| 14 | +2. Replace `<rust-toolchain-dir>` with your Rust toolchain path, typically: |
| 15 | + `$HOME/.rustup/toolchains/<active-toolchain-name>` |
| 16 | +
|
| 17 | + Example: `/Users/macuser/.rustup/toolchains/stable-aarch64-apple-darwin` |
| 18 | +
|
| 19 | +<details> |
| 20 | +<summary>About the LLDB Configuration</summary> |
| 21 | +
|
| 22 | +The LLDB configuration is taken from [this file in the Rust source code repository](https://github.com/rust-lang/rust/blob/master/src/etc/rust-lldb). After installing Rust, you should have a `rust-lldb` command at `$HOME/.cargo/bin/rust-lldb`, which essentially calls the `lldb` command with the same configuration above. Ideally, we could configure Xcode to invoke `rust-lldb` instead of the default LLDB, so that we don't have to keep the `.lldbinit` content in sync with how `rust-lldb` runs the LLDB command. However, this isn't possible at the moment. |
| 23 | +</details> |
| 24 | +
|
| 25 | +## Basic Debugging Steps |
| 26 | +
|
| 27 | +1. Set a breakpoint in your Rust code: |
| 28 | +
|
| 29 | + ``` |
| 30 | + breakpoint set --file <rust-file> --line <line-no> |
| 31 | + # Example: |
| 32 | + # breakpoint set --file wp_api/src/api_client.rs --line 133 |
| 33 | + ``` |
| 34 | +> [!Note] |
| 35 | +> You _do not_ need to set the same breakpoints repeatedly because Xcode remembers breakpoints between sessions. |
| 36 | +
|
| 37 | +2. Run your application until it hits the breakpoint |
| 38 | +
|
| 39 | +3. Once paused, you can: |
| 40 | + - View variables in the current frame from the left side of the debugger console |
| 41 | + - Inspect struct fields as defined in the source code |
| 42 | + - Step through code using Xcode's debug controls |
| 43 | +
|
| 44 | +> [!Note] |
| 45 | +> LLDB does not support Rust expressions, so you cannot run commands like `expression --language Rust -- api_root_url.as_str()` to execute functions and print the result. |
| 46 | +
|
| 47 | +## Debugging from an app |
| 48 | +
|
| 49 | +Your app should integrate this library via its releases, which is slightly different from developing the library within this repository. When debugging Rust code from your app, follow these additional steps: |
| 50 | +
|
| 51 | +1. Ensure your local `wordpress-rs` working copy matches the app's version: |
| 52 | + - Check the version used in your app |
| 53 | + - Go to your wordpress-rs repository and run `git checkout <version>` to switch to the correct version |
| 54 | +
|
| 55 | +2. After hitting your breakpoint, you'll see an error message: |
| 56 | + ``` |
| 57 | + [...]. The file path does not exist on the file system: /opt/ci/builds/builder/automattic/wordpress-rs/wp_api/src/api_client.rs |
| 58 | + ``` |
| 59 | +
|
| 60 | +3. This occurs because the app's binary is built in CI with different source paths. To fix this, run: |
| 61 | + ``` |
| 62 | + settings set target.source-map /opt/ci/builds/builder/automattic/wordpress-rs /path/to/wordpress-rs |
| 63 | + ``` |
| 64 | +
|
| 65 | +> [!Note] |
| 66 | +> Add the source map command to your `.lldbinit` file to avoid running it in every debugging session. |
0 commit comments