Skip to content

Commit 5293830

Browse files
authored
Further improve the debugger scripts (#111)
1 parent 23c5d69 commit 5293830

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

Example/.bazelrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
common --ios_simulator_device="iPhone 16 Pro"
2-
common --ios_simulator_version="18.4"
2+
common --ios_simulator_version="18.6"
3+
4+
# rules_swift flags migration
5+
# --swiftcopt and --host_swiftcopt are deprecated
6+
common --flag_alias=swiftcopt=@build_bazel_rules_swift//swift:copt
7+
common --flag_alias=host_swiftcopt=@build_bazel_rules_swift//swift:exec_copt
8+
common --swiftcopt=-whole-module-optimization
9+
common --host_swiftcopt=-whole-module-optimization
310

411
# All of the following are Debug/Index setup configs inspired by the default rules_xcodeproj template
512

Example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ After performing these steps, you should already be able to see the basic indexi
2121

2222
- To run a regular build: Cmd+Shift+B -> `Build HelloWorld`
2323
- To run a debug build, launch the simulator and attach `lldb`: Cmd+Shift+D -> `Debug HelloWorld (Example)`
24-
- This requires a **iOS 18.4 iPhone 16 Pro** simulator installed as this is what the example project is currently configured for. Make sure you have one available, as otherwise the build will fail.
24+
- This requires a **iOS 18.6 iPhone 16 Pro** simulator installed as this is what the example project is currently configured for. Make sure you have one available, as otherwise the build will fail.
2525
- To test: Cmd+Shift+P -> `Run Test Task` -> `Test HelloWorldTests`
2626

2727
## Considerations

Example/scripts/lldb_attach.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
2-
from lldb_common import load_launch_info, load_output_base, run_command
1+
from lldb_common import load_launch_info, load_bazel_info, run_command
2+
import os
33

44
launch_info = load_launch_info()
5-
output_base = load_output_base()
5+
bazel_info = load_bazel_info()
6+
output_base = bazel_info["output_base"]
7+
execution_root = bazel_info["execution_root"]
8+
workspace_root = os.getcwd()
69
device_platform = launch_info["platform"]
710
device_udid = launch_info["udid"]
811
debug_pid = str(launch_info["pid"])
@@ -12,21 +15,25 @@
1215
# This is needed because we use the `oso_prefix_is_pwd` feature, which makes the
1316
# paths to archives relative to the exec root.
1417
run_command(
15-
f'platform settings -w "{output_base}/execroot/_main"',
18+
f'platform settings -w "{execution_root}"',
1619
)
1720

1821
# Adjust the source map.
1922
#
20-
# The source map will be initialized to the workspace. Need to resolve external
21-
# paths to the stable external directory. Then need to resolve generated files
22-
# to the execution root. We set it for `./` instead of `./bazel-out/` to allow
23-
# the convenience symlink to be used if it exists.
23+
# By default, breakpoints will move the IDE to the file within the execution root.
24+
# We need to map them to their real location in the workspace root to fix that.
25+
# The only exception is the the `external` folder, which only exists within the execution root.
26+
run_command(
27+
'settings append target.source-map "." "{}"'.format(workspace_root),
28+
)
2429
run_command(
2530
'settings insert-before target.source-map 0 "./external/" '
2631
f'"{output_base}/external/"',
2732
)
33+
34+
# Helps lldb to not timeout on large projects
2835
run_command(
29-
f'settings append target.source-map "./" "{output_base}/execroot/_main/"',
36+
'settings set plugin.process.gdb-remote.packet-timeout 300',
3037
)
3138

3239
if device_platform == "device":

Example/scripts/lldb_common.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,27 @@
1111

1212
# These files are generated by the lldb_launch_and_debug.sh script.
1313
LAUNCH_INFO_PATH = pathlib.Path(".bsp/skbsp_generated/lldb.json")
14-
OUTPUT_BASE_PATH = pathlib.Path(".bsp/skbsp_generated/output_base.txt")
14+
BAZEL_INFO_PATH = pathlib.Path(".bsp/skbsp_generated/bazel_info.json")
1515

1616

17-
def load_launch_info() -> dict[str, Any]:
17+
def _load_file(path: pathlib.Path) -> str:
1818
# We exit cleanly as a way to not throw a bunch of
1919
# errors when the `preLaunchTask` fails. Ideally we wouldn't get here, but
2020
# `lldb-dap` has a bug where background tasks don't prevent debugging from
2121
# starting.
22-
if not LAUNCH_INFO_PATH.exists():
23-
print("Launch info file not found. Exiting.", file=sys.stderr, flush=True)
22+
if not path.exists():
23+
print(f"File not found: {path}. Exiting.", file=sys.stderr, flush=True)
2424
lldb.SBDebugger.Terminate()
2525
os._exit(0)
26+
return path.read_text()
2627

27-
return json.loads(LAUNCH_INFO_PATH.read_text())
2828

29+
def load_launch_info() -> dict[str, Any]:
30+
return json.loads(_load_file(LAUNCH_INFO_PATH))
2931

30-
def load_output_base() -> str:
31-
# We exit cleanly as a way to not throw a bunch of
32-
# errors when the `preLaunchTask` fails. Ideally we wouldn't get here, but
33-
# `lldb-dap` has a bug where background tasks don't prevent debugging from
34-
# starting.
35-
if not OUTPUT_BASE_PATH.exists():
36-
print("Output base file not found. Exiting.", file=sys.stderr, flush=True)
37-
lldb.SBDebugger.Terminate()
38-
os._exit(0)
3932

40-
return OUTPUT_BASE_PATH.read_text().strip()
33+
def load_bazel_info() -> dict[str, Any]:
34+
return json.loads(_load_file(BAZEL_INFO_PATH))
4135

4236

4337
def run_command(command: str) -> None:

Example/scripts/lldb_launch_and_debug.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,39 @@ WORKSPACE_ROOT=$(pwd)
99
# When asking Bazel to launch a simulator, we need to intercept
1010
# the launched process' PID to be able to debug it later on.
1111
# We do this by asking it to write this info to a file.
12-
# These two paths are also hardcoded in the lldb_attach.py and lldb_kill_app.py scripts.
13-
INFO_JSON=${WORKSPACE_ROOT}/.bsp/skbsp_generated/lldb.json
14-
rm -f ${INFO_JSON} || true
12+
# These paths are also hardcoded in the lldb_attach.py and lldb_kill_app.py scripts.
13+
LAUNCH_INFO_JSON=${WORKSPACE_ROOT}/.bsp/skbsp_generated/lldb.json
14+
rm -f ${LAUNCH_INFO_JSON} || true
1515
OUTPUT_BASE=$(bazelisk info output_base)
16-
OUTPUT_BASE_INFO_PATH=${WORKSPACE_ROOT}/.bsp/skbsp_generated/output_base.txt
17-
rm -f ${OUTPUT_BASE_INFO_PATH} || true
16+
EXECUTION_ROOT=$(bazelisk info execution_root)
17+
BAZEL_INFO_JSON=${WORKSPACE_ROOT}/.bsp/skbsp_generated/bazel_info.json
18+
rm -f ${BAZEL_INFO_JSON} || true
1819
mkdir -p ${WORKSPACE_ROOT}/.bsp/skbsp_generated
19-
echo "${OUTPUT_BASE}" > ${OUTPUT_BASE_INFO_PATH}
20+
cat > ${BAZEL_INFO_JSON} <<EOF
21+
{
22+
"output_base": "${OUTPUT_BASE}",
23+
"execution_root": "${EXECUTION_ROOT}"
24+
}
25+
EOF
26+
27+
ADDITIONAL_FLAGS=()
28+
ADDITIONAL_FLAGS+=("--keep_going")
29+
ADDITIONAL_FLAGS+=("--color=yes")
30+
# We need --remote_download_regex because the files that lldb needs won't usually be downloaded by Bazel
31+
# when using flags like --remote_download_toplevel and the such.
32+
ADDITIONAL_FLAGS+=("--remote_download_regex=.*\.indexstore/.*|.*\.(a|cfg|c|C|cc|cl|cpp|cu|cxx|c++|def|h|H|hh|hpp|hxx|h++|hmap|ilc|inc|inl|ipp|tcc|tlh|tli|tpp|m|modulemap|mm|pch|swift|swiftdoc|swiftmodule|swiftsourceinfo|yaml)$")
2033

21-
# FIXME: Could not figure out how to make the output show up on the dedicated
22-
# debug console. So we need to keep this script alive and display it here in the meantime.
2334
BAZEL_APPLE_PREFER_PERSISTENT_SIMS=1 \
24-
BAZEL_APPLE_LAUNCH_INFO_PATH=${INFO_JSON} \
35+
BAZEL_APPLE_LAUNCH_INFO_PATH=${LAUNCH_INFO_JSON} \
2536
BAZEL_SIMCTL_LAUNCH_FLAGS="--wait-for-debugger --stdout=$(tty) --stderr=$(tty)" \
26-
bazelisk run "${BAZEL_LABEL_TO_RUN}"
37+
bazelisk run "${BAZEL_LABEL_TO_RUN}" "${ADDITIONAL_FLAGS[@]}"
2738

28-
PID=$(jq -r '.pid' "${INFO_JSON}")
39+
PID=$(jq -r '.pid' "${LAUNCH_INFO_JSON}")
2940

3041
echo "Launched with PID: ${PID}"
3142

3243
# Keep the terminal alive until the app is killed.
33-
# FIXME: Only necessary because of the stdout-related FIXME mentioned above
44+
# FIXME: This is only necessary because I couldn't figure out how to make the app's output
45+
# show up on the dedicated debug console. So we need to keep this script alive and
46+
# display it here in the meantime.
3447
lsof -p ${PID} +r 1 &>/dev/null

0 commit comments

Comments
 (0)