← Back to Pull requests

[lldb/docs] Add tutorials for dSYM scripts #242959

Open
medismailben opened this pull request with 700 lines diff across 13 files
View on GitHub
medismailben medismailben commented on Nov 16, 2026 04:13 UTC
Author

LLDB's documentation doesn't say that a binary can ship a Python script that LLDB loads with it, or how to write and package one.

This patch adds two tutorials: one on writing the script and how lldb calls it, and one on packaging, signing and loading it from a dSYM. It also links them from the existing documentation.

Assisted-by: Claude

Diff: 700 lines (+668 / -32) across 13 files Reviewer: jimingham Review duration: 25057m
All Vouch review confidence checks passed
Residual risk 0.09 = Change Risk (0.65) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.65 · 700 lines diff across 13 files
0.65
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 5 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
86%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.392
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
20 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 16, 2026 04:13 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Med Ismail Bennani (medismailben)

<details>
<summary>Changes</summary>

LLDB's documentation doesn't say that a binary can ship a Python script that LLDB loads with it, or how to write and package one.

This patch adds two tutorials: one on writing the script and how lldb calls it, and one on packaging, signing and loading it from a dSYM. It also links them from the existing documentation.

Assisted-by: Claude

---

Patch is 50.20 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/242959.diff

22 Files Affected:

  • (modified) lldb/docs/use/python-reference.md (+2)

  • (modified) lldb/docs/use/symbols.md (+4-1)

  • (added) lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md (+173)

  • (modified) lldb/docs/use/tutorials/extending-target-stop-hooks.md (+8-1)

  • (added) lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md (+185)

  • (modified) lldb/docs/use/tutorials/writing-custom-commands.md (+4-3)

  • (modified) lldb/include/lldb/Target/Target.h (+21)

  • (modified) lldb/source/Commands/CommandObjectTarget.cpp (+15)

  • (modified) lldb/source/Commands/Options.td (+12-5)

  • (modified) lldb/source/Core/ModuleList.cpp (+4)

  • (modified) lldb/source/Target/Target.cpp (+54-3)

  • (modified) lldb/source/Target/TargetList.cpp (+5-3)

  • (modified) lldb/test/API/commands/target/stop-hooks/TestStopHookScripted.py (+31-2)

  • (modified) lldb/test/API/commands/target/stop-hooks/TestStopHooks.py (+29)

  • (added) lldb/test/API/macosx/dsym_modules/TestdSYMModuleHooks.py (+93)

  • (added) lldb/test/API/macosx/dsym_modules/module_hooks.py (+15)

  • (added) lldb/test/API/macosx/dsym_modules/read_header_hook.py (+38)

  • (added) lldb/test/API/macosx/scripted_process_dsym_scripts/Makefile (+8)

  • (added) lldb/test/API/macosx/scripted_process_dsym_scripts/TestScriptedProcessdSYMScripts.py (+85)

  • (added) lldb/test/API/macosx/scripted_process_dsym_scripts/image_scripted_process.py (+68)

  • (added) lldb/test/API/macosx/scripted_process_dsym_scripts/payload.c (+1)

  • (added) lldb/test/API/macosx/scripted_process_dsym_scripts/payload.py (+39)
  • ``````

    diff --git a/lldb/docs/use/python-reference.md b/lldb/docs/use/python-reference.md
    index 940b53a88e2742..6f9ca2bb629363 100644
    --- a/lldb/docs/use/python-reference.md
    +++ b/lldb/docs/use/python-reference.md
    @@ -26,4 +26,6 @@ tutorials/writing-custom-commands
     tutorials/implementing-standalone-scripts
     tutorials/custom-frame-recognizers
     tutorials/extending-target-stop-hooks
    +tutorials/shipping-debugging-support-with-a-binary
    +tutorials/auto-loading-scripts-from-dsyms
     

    diff --git a/lldb/docs/use/symbols.md b/lldb/docs/use/symbols.md
    index 80edc5fba3d29d..a69fd35c08649c 100644
    --- a/lldb/docs/use/symbols.md
    +++ b/lldb/docs/use/symbols.md
    @@ -2,7 +2,10 @@

    On macOS, debug symbols are often in stand alone bundles called dSYM files.
    These are bundles that contain DWARF debug information and other resources
    -related to builds and debug info.
    +related to builds and debug info. A dSYM can also carry a Python script that
    +LLDB loads with its module, as
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](tutorials/auto-loading-scripts-from-dsyms.md)
    +explains.

    The DebugSymbols.framework framework helps locate dSYM files when given a UUID.
    It can locate the symbols using a variety of methods:
    diff --git a/lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md b/lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md
    new file mode 100644
    index 00000000000000..186e0fb965c323
    --- /dev/null
    +++ b/lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in
    Contents/Resources/Python,
    +named after the module:
    +
    +
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +

    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces
    ., spaces and - with _, replaces + with x, and adds
    +a leading
    _ to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named
    a.out, LLDB looks for a_out.py, then for
    +
    a.py.
    +
    +A framework works the same way, through its dSYM:
    +
    MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py is loaded with
    +
    MyKit.framework/MyKit.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let
    dsymutil
    +do it when it creates the bundle.
    --embed-resource <source>=<destination>
    +copies a file or a directory into the bundle, at a destination relative to
    +
    Contents/Resources:
    +
    +
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +

    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +

    +
    +The option can be repeated, and doesn't work with
    --flat, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:
    +
    +- The SDK that matches the target,
    $SDKROOT/usr/share/lldb, on Darwin.
    +- The directories LLDB was configured with, through the
    +
    LLDB_SAFE_AUTO_LOAD_PATHS CMake option.
    +
    +In each location, the scripts for a module go in a directory named after it,
    +without its extension:
    $SDKROOT/usr/share/lldb/MyApp/MyApp.py. LLDB uses the
    +first location that has a directory for the module, even if the script it
    +expects isn't there, and the settings below decide whether it loads the
    +script, as they do for a dSYM.
    +
    +## When LLDB loads the script on its own
    +
    +
    target.load-script-from-symbol-file decides what LLDB does with the scripts
    +it finds:
    +
    +| Value | What LLDB does |
    +|-------|----------------|
    +|
    trusted (the default) | Loads the script if the dSYM bundle has a trusted code signature, and warns about the script otherwise. |
    +|
    true | Loads every script it finds. |
    +|
    warn | Never loads scripts, and warns about each one it finds. |
    +|
    false | Never loads scripts, and says nothing. |
    +
    +
    target.auto-load-scripts-for-modules overrides that setting for the modules
    +you name, with the same values:
    +
    +
    +(lldb) settings set target.auto-load-scripts-for-modules MyApp=true
    +

    +
    +Use the module's file name without its extension. A per-module entry is the
    +narrowest way to allow a script that isn't signed: it allows one module's
    +scripts instead of every script in every dSYM.
    +
    +Set both before the target loads the module, for example in
    ~/.lldbinit. LLDB
    +decides when the module loads, and changing the settings later doesn't load the
    +scripts of modules that are already in the target.
    +
    +## What makes a dSYM bundle trusted
    +
    +A bundle is trusted when its code signature is valid and chains to a
    +certificate authority that the system trusts. In code signing terms, it
    +satisfies the
    anchor trusted requirement. You can check a bundle the same way
    +LLDB does:
    +
    +
    +$ codesign --verify --verbose -R="anchor trusted" MyApp.dSYM
    +MyApp.dSYM: valid on disk
    +MyApp.dSYM: satisfies its Designated Requirement
    +MyApp.dSYM: explicit requirement satisfied
    +

    +
    +An ad hoc signature,
    codesign --sign -, never satisfies it, because it doesn't
    +chain to any authority.
    +
    +## Signing a dSYM bundle
    +
    +Sign the bundle with an identity whose certificate chains to a trusted
    +authority, such as an Apple Development or Developer ID certificate:
    +
    +
    +$ codesign --force --sign "Apple Development: Jane Appleseed (ABCDE12345)" MyApp.dSYM
    +

    +
    +The signature seals the whole bundle, the script included. Editing the script
    +or the DWARF afterwards breaks the signature, and LLDB goes back to warning
    +about the script. Sign the bundle as the last step of the build, after
    +
    dsymutil and after copying the script in, and sign it again after every
    +change.
    +
    +A signature only helps where its certificate is trusted. If you sign with a
    +certificate from a private authority, such as a company's internal signing
    +service, a machine that doesn't trust that authority, for example one that
    +downloaded the dSYM from a bug report, treats the bundle as untrusted. On those
    +machines, allow the module with
    target.auto-load-scripts-for-modules.
    +
    +## When LLDB doesn't load the script
    +
    +When the settings don't let LLDB load a script on its own, LLDB asks you
    +instead. In an interactive session, it lists those scripts in a picker once the
    +process stops. Trusted scripts are checked, and LLDB loads the ones you leave
    +checked, for this debug session:
    +
    +![asciicast](https://asciinema.org/a/vEHGCf50bSbSwvPZ)
    +
    +When the picker isn't available, for example when you run
    lldb --batch, LLDB
    +falls back to printing a warning for each script, with the command that loads
    +it:
    +
    +
    +warning: 'MyApp' contains an untrusted debug script. To run this script in this debug session:
    +
    +    command script import "/path/to/MyApp.dSYM/Contents/Resources/Python/MyApp.py"
    +
    +To run all discovered debug scripts in this session:
    +
    +    settings set target.load-script-from-symbol-file true
    +

    +
    +Importing the script by hand doesn't call
    __lldb_module_added_to_target, as
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains. If your script relies on it, allow the module with
    +
    target.auto-load-scripts-for-modules and create the target again.
    +
    +To see what LLDB found and why it did or didn't load a script, enable the
    +module log before creating the target:
    +
    +
    +(lldb) log enable lldb module
    +

    diff --git a/lldb/docs/use/tutorials/extending-target-stop-hooks.md b/lldb/docs/use/tutorials/extending-target-stop-hooks.md
    index 232187d0dcf114..5fcae2f4e348cc 100644
    --- a/lldb/docs/use/tutorials/extending-target-stop-hooks.md
    +++ b/lldb/docs/use/tutorials/extending-target-stop-hooks.md
    @@ -22,4 +22,11 @@ To use this class in lldb, run the command:
    (lldb) target stop-hook add -P MyModule.MyStopHook -k first -v 1 -k second -v 2
     
    -where `MyModule.py` is the file containing the class definition `MyStopHook`.
    \ No newline at end of file
    +where `MyModule.py` is the file containing the class definition `MyStopHook`.
    +
    +To run a stop hook only once, add it with `--one-shot true`. LLDB deletes it the
    +first time it runs, for the first thread it applies to. A one-shot stop hook is
    +how a script can do something at the next stop and then get out of the way:
    +once it's deleted, it no longer votes on whether the process stops.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md) shows a script that uses one to read the process once it
    +stops.
    \ No newline at end of file
    diff --git a/lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md b/lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md
    new file mode 100644
    index 00000000000000..07f3b8ae7aa505
    --- /dev/null
    +++ b/lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md
    @@ -0,0 +1,185 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +
    python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    + # Commands and formatters work in every target.
    + debugger.HandleCommand(
    + "type summary add --summary-string '${var.name}' MyApp::Widget"
    + )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    + # Breakpoints and stop hooks belong to the target that loaded the module.
    + target.BreakpointCreateByName("my_app_assertion_failed")
    +
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +
    python3
    +if "configured_targets" not in globals():
    + configured_targets = set()
    +
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +
    python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    + directory = os.path.dirname(__file__)
    + for name in TOOLS:
    + path = os.path.join(directory, name + ".py")
    + debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    + for name in TOOLS:
    + hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    + if hook:
    + hook(target, internal_dict)
    +
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook from `__lldb_module_added_to_target`. LLDB runs it the next
    +time the process stops, then deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +
    python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    + def __init__(self, target, extra_args, internal_dict):
    + pass
    +
    + def handle_stop(self, exe_ctx, stream):
    + target = exe_ctx.GetTarget()
    + module = target.module[MODULE_NAME]
    + header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    + if header == lldb.LLDB_INVALID_ADDRESS:
    + # Too early: the module isn't loaded yet, for example when a
    + # launch stops at entry, before the loader has run. Try again at
    + # the next stop.
    + add_header_hook(target)
    + return True
    +
    + error = lldb.SBError()
    + data = exe_ctx.GetProcess().ReadMemory(header, 32, error)
    + if error.Fail():
    + stream.Print(f"{MODULE_NAME}: could not read the Mach-O header: {error}\n")
    + return True
    +
    + _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack("<8I", data)
    + stream.Print(f"{MODULE_NAME}: {ncmds} load commands, {sizeofcmds} bytes\n")
    + return True
    +
    +
    +def add_header_hook(target):
    + result = lldb.SBCommandReturnObject()
    + target.GetDebugger().GetCommandInterpreter().HandleCommand(
    + f"target stop-hook add --one-shot true -P {__name__}.ReadMachOHeader",
    + lldb.SBExecutionContext(target),
    + result,
    + )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    + add_header_hook(target)
    +
    +
    +A few things in this example matter:
    +
    +- `HandleCommand` gets an `SBExecutionContext` for the target that
    +  `__lldb_module_added_to_target` was given, so the stop hook goes on that
    +  target, not on whichever target is selected.
    +- The stop hook runs the first time the process stops after it's added: when a
    +  launched process stops at a breakpoint, when an attach completes, or when a
    +  core file has finished loading. A launch that stops at entry stops before the
    +  loader has loaded the process's libraries, so the hook checks that the module
    +  has a load address, and adds itself again for the next stop if it doesn't.
    +- `handle_stop` returns `True`: the hook only reads, and the process should
    +  stay stopped. Returning `False` would ask LLDB to continue.
    +- Because the hook is one-shot, LLDB deletes it after it runs, so it doesn't
    +  run at later stops and doesn't vote on whether they continue. It runs for one
    +  process: if you relaunch and need it again, add it again.
    +
    +Read whatever you need from the stop hook, but don't resume, step, detach or
    +kill the process from it: let the user or a command decide what the process
    +does next.
    diff --git a/lldb/docs/use/tutorials/writing-custom-commands.md b/lldb/docs/use/tutorials/writing-custom-commands.md
    index 965d01eb17713e..64c323f7cfab9a 100644
    --- a/lldb/docs/use/tutorials/writing-custom-commands.md
    +++ b/lldb/docs/use/tutorials/writing-custom-commands.md
    @@ -295,9 +295,10 @@ def __lldb_init_module(debugger, internal_dict):
     
     where debugger and internal_dict are as above, that function will get run when
     the module is loaded allowing you to add whatever commands you want into the
    -current debugger. Note that this function will only be run when using the LLDB
    -command `command script import`, it will not get run if anyone imports your
    -module from another module.
    +current debugger. Note that LLDB only runs this function when it loads your
    +module itself: with `command script import`, or when it loads your module along
    +with a binary, as [Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md) explains. It doesn't run when another Python
    +module imports yours.
     
     Another way to load custom commands in lldb is to use the
     `@lldb.command(command_name=None, doc=None)` decorator.
    diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h
    index 781683caf2db63..dcd0b4459be288 100644
    --- a/lldb/include/lldb/Target/Target.h
    +++ b/lldb/include/lldb/Target/Target.h
    @@ -12,6 +12,7 @@
     #include <list>
     #include <map>
     #include <memory>
    +#include <mutex>
     #include <optional>
     #include <string>
     #include <type_traits>
    @@ -46,7 +47,9 @@
     #include "lldb/Utility/Stream.h"
     #include "lldb/Utility/StructuredData.h"
     #include "lldb/Utility/Timeout.h"
    +#include "lldb/Utility/UUID.h"
     #include "lldb/lldb-public.h"
    +#include "llvm/ADT/DenseSet.h"
     #include "llvm/ADT/MapVector.h"
     #include "llvm/ADT/StringRef.h"
     #include "llvm/Support/Casting.h"
    @@ -1190,6 +1193,13 @@ class Target : public std::enable_shared_from_this<Target>,
     
       void SymbolsDidLoad(ModuleList &module_list);
     
    +  /// Record that we're auto-loading \p module's \p script in this target, so
    +  /// we only load it once. We don't record modules without a UUID.
    +  ///
    +  /// \return
    +  ///     False if we already loaded \p module's script in this target.
    +  bool RecordScriptingResource(Module &module, const FileSpec &script);
    +
       void ClearModules(bool delete_locations);
     
       /// Called as the last function in Process::DidExec().
    @@ -1669,6 +1679,10 @@ class Target : public std::enable_shared_from_this<Target>,
     
         bool GetRunAtInitialStop() const { return m_at_initial_stop; }
     
    +    void SetOneShot(bool one_shot) { m_one_shot = one_shot; }
    +
    +    bool IsOneShot() const { return m_one_shot; }
    +
         void SetSuppressOutput(bool suppress_output) {
           m_suppress_output = suppress_output;
         }
    @@ -1686,6 +1700,7 @@ class Target : ...
    [truncated]
    
    
    ``````

    </details>

    medismailben medismailben commented on Nov 17, 2026 09:05 UTC
    Author

    Depends on #242939 and #243394

    jimingham jimingham commented on code on Nov 18, 2026 22:46 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.

    This is an awkward sentence:

    1) It makes it sound like this happens when LLDB loads a Module, but lldb doesn't really do that, either it finds Modules by looking at what is loaded in the target or process it is managing, or users load them by hand.
    2) it's not clear what the : is explaining. Better to separate that and say something like:

    These python scripts can add ...

    jimingham jimingham commented on code on Nov 18, 2026 22:47 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds

    When you have lists of lists, it's easier to read if you use ; to separate the lists:

    LLDB replaces ., spaces and - with _; replaces + with x; and adds

    jimingham jimingham commented on code on Nov 18, 2026 22:50 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't

    I'd make that two sentences. The two facts are not related to one another. I'd say:

    The option can be repeated to specify multiple resources. However, it doesn't work with the --flat option because explicitly instructs dsymutil NOT to create a bundle.

    jimingham jimingham commented on code on Nov 18, 2026 22:52 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or

    So I would put a : after safe auto-load locations because what follows is a definition of the term.

    jimingham jimingham commented on code on Nov 18, 2026 22:54 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:

    But the colon seems strained here since it's now a second level explanation. And anyway, it would be better to phrase this as "the option that everyone uses" followed by "and BTW, on Darwin there's a special directory".

    jimingham jimingham commented on code on Nov 18, 2026 22:55 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:
    +
    +- The SDK that matches the target, `$SDKROOT/usr/share/lldb`, on Darwin.
    +- The directories LLDB was configured with, through the
    +  `LLDB_SAFE_AUTO_LOAD_PATHS` CMake option.
    +
    +In each location, the scripts for a module go in a directory named after it,
    +without its extension: `$SDKROOT/usr/share/lldb/MyApp/MyApp.py`. LLDB uses the
    +first location that has a directory for the module, even if the script it

    You haven't explicitly said what the search order is - maybe that's why you listed them in that order but I'm just guessing. So I can't guess which one it would "find first".

    jimingham jimingham commented on code on Nov 18, 2026 22:58 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:
    +
    +- The SDK that matches the target, `$SDKROOT/usr/share/lldb`, on Darwin.
    +- The directories LLDB was configured with, through the
    +  `LLDB_SAFE_AUTO_LOAD_PATHS` CMake option.
    +
    +In each location, the scripts for a module go in a directory named after it,
    +without its extension: `$SDKROOT/usr/share/lldb/MyApp/MyApp.py`. LLDB uses the
    +first location that has a directory for the module, even if the script it
    +expects isn't there, and the settings below decide whether it loads the
    +script, as they do for a dSYM.
    +
    +## When LLDB loads the script on its own
    +
    +`target.load-script-from-symbol-file` decides what LLDB does with the scripts
    +it finds:
    +
    +| Value | What LLDB does |
    +|-------|----------------|
    +| `trusted` (the default) | Loads the script if the dSYM bundle has a trusted code signature, and warns about the script otherwise. |
    +| `true` | Loads every script it finds. |
    +| `warn` | Never loads scripts, and warns about each one it finds. |
    +| `false` | Never loads scripts, and says nothing. |
    +
    +`target.auto-load-scripts-for-modules` overrides that setting for the modules
    +you name, with the same values:
    +
    +```
    +(lldb) settings set target.auto-load-scripts-for-modules MyApp=true
    +```
    +
    +Use the module's file name without its extension. A per-module entry is the
    +narrowest way to allow a script that isn't signed: it allows one module's
    +scripts instead of every script in every dSYM.
    +
    +Set both before the target loads the module, for example in `~/.lldbinit`. LLDB
    +decides when the module loads, and changing the settings later doesn't load the
    +scripts of modules that are already in the target.

    I thought setting load-script-from-symbol-file true would rescan for scripts and load them?

    jimingham jimingham commented on code on Nov 18, 2026 23:00 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:
    +
    +- The SDK that matches the target, `$SDKROOT/usr/share/lldb`, on Darwin.
    +- The directories LLDB was configured with, through the
    +  `LLDB_SAFE_AUTO_LOAD_PATHS` CMake option.
    +
    +In each location, the scripts for a module go in a directory named after it,
    +without its extension: `$SDKROOT/usr/share/lldb/MyApp/MyApp.py`. LLDB uses the
    +first location that has a directory for the module, even if the script it
    +expects isn't there, and the settings below decide whether it loads the
    +script, as they do for a dSYM.
    +
    +## When LLDB loads the script on its own
    +
    +`target.load-script-from-symbol-file` decides what LLDB does with the scripts
    +it finds:
    +
    +| Value | What LLDB does |
    +|-------|----------------|
    +| `trusted` (the default) | Loads the script if the dSYM bundle has a trusted code signature, and warns about the script otherwise. |
    +| `true` | Loads every script it finds. |
    +| `warn` | Never loads scripts, and warns about each one it finds. |
    +| `false` | Never loads scripts, and says nothing. |
    +
    +`target.auto-load-scripts-for-modules` overrides that setting for the modules
    +you name, with the same values:
    +
    +```
    +(lldb) settings set target.auto-load-scripts-for-modules MyApp=true
    +```
    +
    +Use the module's file name without its extension. A per-module entry is the
    +narrowest way to allow a script that isn't signed: it allows one module's
    +scripts instead of every script in every dSYM.
    +
    +Set both before the target loads the module, for example in `~/.lldbinit`. LLDB
    +decides when the module loads, and changing the settings later doesn't load the
    +scripts of modules that are already in the target.
    +
    +## What makes a dSYM bundle trusted
    +
    +A bundle is trusted when its code signature is valid and chains to a
    +certificate authority that the system trusts. In code signing terms, it
    +satisfies the `anchor trusted` requirement. You can check a bundle the same way
    +LLDB does:
    +
    +```
    +$ codesign --verify --verbose -R="anchor trusted" MyApp.dSYM
    +MyApp.dSYM: valid on disk
    +MyApp.dSYM: satisfies its Designated Requirement
    +MyApp.dSYM: explicit requirement satisfied
    +```
    +
    +An ad hoc signature, `codesign --sign -`, never satisfies it, because it doesn't
    +chain to any authority.
    +
    +## Signing a dSYM bundle
    +
    +Sign the bundle with an identity whose certificate chains to a trusted
    +authority, such as an Apple Development or Developer ID certificate:
    +
    +```
    +$ codesign --force --sign "Apple Development: Jane Appleseed (ABCDE12345)" MyApp.dSYM
    +```
    +
    +The signature seals the whole bundle, the script included. Editing the script
    +or the DWARF afterwards breaks the signature, and LLDB goes back to warning
    +about the script. Sign the bundle as the last step of the build, after
    +`dsymutil` and after copying the script in, and sign it again after every
    +change.
    +
    +A signature only helps where its certificate is trusted. If you sign with a
    +certificate from a private authority, such as a company's internal signing
    +service, a machine that doesn't trust that authority, for example one that
    +downloaded the dSYM from a bug report, treats the bundle as untrusted. On those
    +machines, allow the module with `target.auto-load-scripts-for-modules`.
    +
    +## When LLDB doesn't load the script
    +
    +When the settings don't let LLDB load a script on its own, LLDB asks you
    +instead. In an interactive session, it lists those scripts in a picker once the
    +process stops. Trusted scripts are checked, and LLDB loads the ones you leave
    +checked, for this debug session:
    +
    +[![asciicast](https://asciinema.org/a/vEHGCf50bSbSwvPZ.svg)](https://asciinema.org/a/vEHGCf50bSbSwvPZ)
    +
    +When the picker isn't available, for example when you run `lldb --batch`, LLDB
    +falls back to printing a warning for each script, with the command that loads
    +it:
    +
    +```
    +warning: 'MyApp' contains an untrusted debug script. To run this script in this debug session:
    +
    +    command script import "/path/to/MyApp.dSYM/Contents/Resources/Python/MyApp.py"
    +
    +To run all discovered debug scripts in this session:

    This is what I thought was true but contradicts what you said above.

    jimingham jimingham commented on code on Nov 18, 2026 23:02 UTC
    Code Review
    lldb/docs/use/tutorials/auto-loading-scripts-from-dsyms.md:1
    @@ -0,0 +1,173 @@
    +# Auto-Loading Scripts from Code-Signed dSYM Bundles
    +
    +A dSYM bundle can carry a Python script that LLDB loads with the module it
    +describes: data formatters for the module's types, commands to inspect it, or
    +setup for the targets that load it.
    +[Shipping Debugging Support with a Binary](shipping-debugging-support-with-a-binary.md)
    +explains how to write the script. This tutorial explains where to put the
    +script, when LLDB loads it on its own, and how to sign the bundle so that it
    +does.
    +
    +## Where the script goes
    +
    +LLDB looks for the script in the dSYM bundle, in `Contents/Resources/Python`,
    +named after the module:
    +
    +```
    +MyApp.dSYM/
    +  Contents/
    +    Info.plist
    +    Resources/
    +      DWARF/
    +        MyApp
    +      Python/
    +        MyApp.py
    +```
    +
    +The script's name is the module's file name, changed to a valid Python module
    +name: LLDB replaces `.`, spaces and `-` with `_`, replaces `+` with `x`, and adds
    +a leading `_` to a name that is a Python keyword. If there is no script with
    +that name, LLDB strips the last extension from the file name and tries again.
    +For example, for a module named `a.out`, LLDB looks for `a_out.py`, then for
    +`a.py`.
    +
    +A framework works the same way, through its dSYM:
    +`MyKit.framework.dSYM/Contents/Resources/Python/MyKit.py` is loaded with
    +`MyKit.framework/MyKit`.
    +
    +## Packaging the script with dsymutil
    +
    +Instead of copying the script into the dSYM after the build, let `dsymutil`
    +do it when it creates the bundle. `--embed-resource <source>=<destination>`
    +copies a file or a directory into the bundle, at a destination relative to
    +`Contents/Resources`:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts/MyApp.py=Python/MyApp.py
    +```
    +
    +A directory is copied with its contents, so a script that imports helper
    +modules can ship them next to it:
    +
    +```
    +$ dsymutil MyApp -o MyApp.dSYM --embed-resource scripts=Python
    +```
    +
    +The option can be repeated, and doesn't work with `--flat`, which doesn't
    +create a bundle.
    +
    +## The safe auto-load locations
    +
    +When the dSYM has no script for a module, or there is no dSYM, LLDB looks in
    +its safe auto-load locations, a list of directories that ship with LLDB or
    +with the SDK rather than with the module:
    +
    +- The SDK that matches the target, `$SDKROOT/usr/share/lldb`, on Darwin.
    +- The directories LLDB was configured with, through the
    +  `LLDB_SAFE_AUTO_LOAD_PATHS` CMake option.
    +
    +In each location, the scripts for a module go in a directory named after it,
    +without its extension: `$SDKROOT/usr/share/lldb/MyApp/MyApp.py`. LLDB uses the
    +first location that has a directory for the module, even if the script it
    +expects isn't there, and the settings below decide whether it loads the
    +script, as they do for a dSYM.
    +
    +## When LLDB loads the script on its own
    +
    +`target.load-script-from-symbol-file` decides what LLDB does with the scripts
    +it finds:
    +
    +| Value | What LLDB does |
    +|-------|----------------|
    +| `trusted` (the default) | Loads the script if the dSYM bundle has a trusted code signature, and warns about the script otherwise. |
    +| `true` | Loads every script it finds. |
    +| `warn` | Never loads scripts, and warns about each one it finds. |
    +| `false` | Never loads scripts, and says nothing. |
    +
    +`target.auto-load-scripts-for-modules` overrides that setting for the modules
    +you name, with the same values:
    +
    +```
    +(lldb) settings set target.auto-load-scripts-for-modules MyApp=true
    +```
    +
    +Use the module's file name without its extension. A per-module entry is the
    +narrowest way to allow a script that isn't signed: it allows one module's
    +scripts instead of every script in every dSYM.
    +
    +Set both before the target loads the module, for example in `~/.lldbinit`. LLDB
    +decides when the module loads, and changing the settings later doesn't load the
    +scripts of modules that are already in the target.
    +
    +## What makes a dSYM bundle trusted
    +
    +A bundle is trusted when its code signature is valid and chains to a
    +certificate authority that the system trusts. In code signing terms, it
    +satisfies the `anchor trusted` requirement. You can check a bundle the same way
    +LLDB does:
    +
    +```
    +$ codesign --verify --verbose -R="anchor trusted" MyApp.dSYM
    +MyApp.dSYM: valid on disk
    +MyApp.dSYM: satisfies its Designated Requirement
    +MyApp.dSYM: explicit requirement satisfied
    +```
    +
    +An ad hoc signature, `codesign --sign -`, never satisfies it, because it doesn't
    +chain to any authority.
    +
    +## Signing a dSYM bundle
    +
    +Sign the bundle with an identity whose certificate chains to a trusted
    +authority, such as an Apple Development or Developer ID certificate:
    +
    +```
    +$ codesign --force --sign "Apple Development: Jane Appleseed (ABCDE12345)" MyApp.dSYM
    +```
    +
    +The signature seals the whole bundle, the script included. Editing the script
    +or the DWARF afterwards breaks the signature, and LLDB goes back to warning
    +about the script. Sign the bundle as the last step of the build, after
    +`dsymutil` and after copying the script in, and sign it again after every
    +change.
    +
    +A signature only helps where its certificate is trusted. If you sign with a
    +certificate from a private authority, such as a company's internal signing
    +service, a machine that doesn't trust that authority, for example one that
    +downloaded the dSYM from a bug report, treats the bundle as untrusted. On those
    +machines, allow the module with `target.auto-load-scripts-for-modules`.
    +
    +## When LLDB doesn't load the script
    +
    +When the settings don't let LLDB load a script on its own, LLDB asks you
    +instead. In an interactive session, it lists those scripts in a picker once the
    +process stops. Trusted scripts are checked, and LLDB loads the ones you leave
    +checked, for this debug session:
    +
    +[![asciicast](https://asciinema.org/a/vEHGCf50bSbSwvPZ.svg)](https://asciinema.org/a/vEHGCf50bSbSwvPZ)
    +
    +When the picker isn't available, for example when you run `lldb --batch`, LLDB
    +falls back to printing a warning for each script, with the command that loads
    +it:
    +
    +```
    +warning: 'MyApp' contains an untrusted debug script. To run this script in this debug session:
    +
    +    command script import "/path/to/MyApp.dSYM/Contents/Resources/Python/MyApp.py"
    +
    +To run all discovered debug scripts in this session:
    +
    +    settings set target.load-script-from-symbol-file true
    +```
    +
    +Importing the script by hand doesn't call `__lldb_module_added_to_target`, as

    Is this the right behavior? Is there any reason why we don't also scan for __lldb_module_added_to_target when scanning for Python modules and call that for you when you set load-script-from-symbol-file true?

    jimingham jimingham commented on code on Nov 18, 2026 23:05 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |

    Didn't you recently change that so we only call it once per Debugger, not once per target? That makes more sense since it's awkward to add Debugger wide affordances once per target.

    jimingham jimingham commented on code on Nov 18, 2026 23:07 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python

    This is also outdated, right? With your recent changes, we only call __lldb_init_module the FIRST time we load that module in a given SBDebugger.

    jimingham jimingham commented on code on Nov 18, 2026 23:14 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so

    This explanation describes symptoms before causes, which makes it confusing. I'd say:

    There are two common cases for when Modules are loaded into an lldb Target:

    1) lldb MyApp.app

    In that case the scripts from MyApp.app will get read in when the target is created, before a process can exist.

    2) lldb -p 12345

    In that case, lldb looks for a process with PID 12345, and attaches to it. Since it has to first attach to the PID before it can know what Modules are in the process, there will be a process when it attaches.

    So there's no guarantee that there is or is not a process extant in the target when __lldb_module_added_to_target is run, and consequently this is not the right place to do work on the process. To do something once...

    jimingham jimingham commented on code on Nov 18, 2026 23:18 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook with `--at-initial-stop true` from
    +`__lldb_module_added_to_target`. LLDB runs it at the first stop it can, then
    +deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +```python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    +    def __init__(self, target, extra_args, internal_dict):
    +        self.done = False
    +
    +    def handle_stop(self, exe_ctx, stream):
    +        # LLDB runs the hook for every thread that stopped, and we only need
    +        # to read the header once.
    +        if self.done:
    +            return True
    +        self.done = True
    +
    +        target = exe_ctx.GetTarget()
    +        module = target.module[MODULE_NAME]
    +        header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    +        if header == lldb.LLDB_INVALID_ADDRESS:

    How would this happen? You know you have a running process because you are in a stop hook. And you know the module is registered in the target because it was in the module list. The only way I can think of where this might happen is if I did target modules add but then didn't do target modules load, which is kind of a corner case.

    jimingham jimingham commented on code on Nov 18, 2026 23:20 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook with `--at-initial-stop true` from
    +`__lldb_module_added_to_target`. LLDB runs it at the first stop it can, then
    +deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +```python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    +    def __init__(self, target, extra_args, internal_dict):
    +        self.done = False
    +
    +    def handle_stop(self, exe_ctx, stream):
    +        # LLDB runs the hook for every thread that stopped, and we only need
    +        # to read the header once.
    +        if self.done:
    +            return True
    +        self.done = True
    +
    +        target = exe_ctx.GetTarget()
    +        module = target.module[MODULE_NAME]
    +        header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    +        if header == lldb.LLDB_INVALID_ADDRESS:
    +            # Too early: the module isn't loaded yet. Try again at the next
    +            # stop.
    +            add_header_hook(target)
    +            return True
    +
    +        error = lldb.SBError()
    +        data = exe_ctx.GetProcess().ReadMemory(header, 32, error)
    +        if error.Fail():
    +            stream.Print(f"{MODULE_NAME}: could not read the Mach-O header: {error}\n")
    +            return True
    +
    +        _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack("<8I", data)
    +        stream.Print(f"{MODULE_NAME}: {ncmds} load commands, {sizeofcmds} bytes\n")
    +        return True
    +
    +
    +def add_header_hook(target):
    +    result = lldb.SBCommandReturnObject()
    +    target.GetDebugger().GetCommandInterpreter().HandleCommand(
    +        f"target stop-hook add --one-shot true --at-initial-stop true "
    +        f"-P {__name__}.ReadMachOHeader",
    +        lldb.SBExecutionContext(target),
    +        result,
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    add_header_hook(target)
    +```
    +

    This example seems a little over-complicated to make the point you are making. If I didn't know anything about Mach-O this would be confusing. Can you come up with something more artificial but where just the point of how to register and manage the stop hook were foreground?

    jimingham jimingham commented on code on Nov 18, 2026 23:21 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook with `--at-initial-stop true` from
    +`__lldb_module_added_to_target`. LLDB runs it at the first stop it can, then
    +deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +```python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    +    def __init__(self, target, extra_args, internal_dict):
    +        self.done = False
    +
    +    def handle_stop(self, exe_ctx, stream):
    +        # LLDB runs the hook for every thread that stopped, and we only need
    +        # to read the header once.
    +        if self.done:
    +            return True
    +        self.done = True
    +
    +        target = exe_ctx.GetTarget()
    +        module = target.module[MODULE_NAME]
    +        header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    +        if header == lldb.LLDB_INVALID_ADDRESS:
    +            # Too early: the module isn't loaded yet. Try again at the next
    +            # stop.
    +            add_header_hook(target)
    +            return True
    +
    +        error = lldb.SBError()
    +        data = exe_ctx.GetProcess().ReadMemory(header, 32, error)
    +        if error.Fail():
    +            stream.Print(f"{MODULE_NAME}: could not read the Mach-O header: {error}\n")
    +            return True
    +
    +        _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack("<8I", data)
    +        stream.Print(f"{MODULE_NAME}: {ncmds} load commands, {sizeofcmds} bytes\n")
    +        return True
    +
    +
    +def add_header_hook(target):
    +    result = lldb.SBCommandReturnObject()
    +    target.GetDebugger().GetCommandInterpreter().HandleCommand(
    +        f"target stop-hook add --one-shot true --at-initial-stop true "
    +        f"-P {__name__}.ReadMachOHeader",
    +        lldb.SBExecutionContext(target),
    +        result,
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    add_header_hook(target)
    +```
    +
    +A few things in this example matter:
    +
    +- `HandleCommand` gets an `SBExecutionContext` for the target that
    +  `__lldb_module_added_to_target` was given, so the stop hook goes on that

    "was given" is confusing, who gave it? The point is the script writer actively passed the execution context to the HandleCommand call, just like you the reader will have to do. So phrase this actively.

    jimingham jimingham commented on code on Nov 18, 2026 23:22 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook with `--at-initial-stop true` from
    +`__lldb_module_added_to_target`. LLDB runs it at the first stop it can, then
    +deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +```python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    +    def __init__(self, target, extra_args, internal_dict):
    +        self.done = False
    +
    +    def handle_stop(self, exe_ctx, stream):
    +        # LLDB runs the hook for every thread that stopped, and we only need
    +        # to read the header once.
    +        if self.done:
    +            return True
    +        self.done = True
    +
    +        target = exe_ctx.GetTarget()
    +        module = target.module[MODULE_NAME]
    +        header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    +        if header == lldb.LLDB_INVALID_ADDRESS:
    +            # Too early: the module isn't loaded yet. Try again at the next
    +            # stop.
    +            add_header_hook(target)
    +            return True
    +
    +        error = lldb.SBError()
    +        data = exe_ctx.GetProcess().ReadMemory(header, 32, error)
    +        if error.Fail():
    +            stream.Print(f"{MODULE_NAME}: could not read the Mach-O header: {error}\n")
    +            return True
    +
    +        _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack("<8I", data)
    +        stream.Print(f"{MODULE_NAME}: {ncmds} load commands, {sizeofcmds} bytes\n")
    +        return True
    +
    +
    +def add_header_hook(target):
    +    result = lldb.SBCommandReturnObject()
    +    target.GetDebugger().GetCommandInterpreter().HandleCommand(
    +        f"target stop-hook add --one-shot true --at-initial-stop true "
    +        f"-P {__name__}.ReadMachOHeader",
    +        lldb.SBExecutionContext(target),
    +        result,
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    add_header_hook(target)
    +```
    +
    +A few things in this example matter:
    +
    +- `HandleCommand` gets an `SBExecutionContext` for the target that
    +  `__lldb_module_added_to_target` was given, so the stop hook goes on that
    +  target, not on whichever target is selected.
    +- `--at-initial-stop true` lets the stop hook run at the stop LLDB is handling
    +  when it loads the script: when an attach completes, when a core file or a
    +  scripted process has finished loading, or, if the process already stopped,

    if the process had already stopped

    You use the verb in the other clauses, so it's awkward to omit it here.

    jimingham jimingham commented on code on Nov 18, 2026 23:30 UTC
    Code Review
    lldb/docs/use/tutorials/shipping-debugging-support-with-a-binary.md:1
    @@ -0,0 +1,202 @@
    +# Shipping Debugging Support with a Binary
    +
    +A binary can bring its own debugging support: data formatters for its types,
    +commands to inspect it, or the breakpoints and stop hooks that every debug
    +session of it needs. You put that support in a Python script that LLDB loads
    +along with the binary, for each target that loads it. The script usually ships
    +in the binary's dSYM bundle:
    +[Auto-Loading Scripts from Code-Signed dSYM Bundles](auto-loading-scripts-from-dsyms.md)
    +explains where it goes and when LLDB loads it.
    +
    +This tutorial explains the functions the script can define, when LLDB calls
    +each one, and how to write it so that it works when LLDB loads it more than
    +once. LLDB calls a binary a module, which is why these functions have module in
    +their names.
    +
    +## The functions LLDB calls
    +
    +| Function | Arguments | When LLDB calls it | Use it for |
    +|----------|-----------|--------------------|------------|
    +| `__lldb_init_module` | `debugger: lldb.SBDebugger` `internal_dict: dict` | Each time the script is loaded: once for each target that loads the module, and each time you import it with `command script import`. | Setup that belongs to the debugger: commands, data formatters, settings that apply to every target. |
    +| `__lldb_module_added_to_target` | `target: lldb.SBTarget` `internal_dict: dict` | Once for each target, when the module is added to it. The module may not have a load address yet, and there may be no process. | Setup that belongs to the target you were given: breakpoints, stop hooks, frame providers. |
    +
    +Both are optional. A script that only adds commands and formatters needs just
    +`__lldb_init_module`. A script that uses both looks like this:
    +
    +```python3
    +import lldb
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    # Commands and formatters work in every target.
    +    debugger.HandleCommand(
    +        "type summary add --summary-string '${var.name}' MyApp::Widget"
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    # Breakpoints and stop hooks belong to the target that loaded the module.
    +    target.BreakpointCreateByName("my_app_assertion_failed")
    +```
    +
    +## When LLDB calls them
    +
    +Each time LLDB loads the script for a target, it imports the script as a Python
    +module, or reloads it if it was already imported, which runs its top-level code
    +again. Then it calls `__lldb_init_module`, and then
    +`__lldb_module_added_to_target` with that target. If `__lldb_init_module`
    +raises an exception, LLDB reports it and doesn't call
    +`__lldb_module_added_to_target`.
    +
    +LLDB does this once for each target that loads the module, as soon as the
    +module is added to the target. That's usually before the module has a load
    +address, and often before there's a process: "Waiting for the process to stop"
    +below explains how to do work that needs the process.
    +
    +`command script import` also runs `__lldb_init_module`, but there's no target to
    +pass, so LLDB doesn't call `__lldb_module_added_to_target`. A script that relies
    +on it has to be loaded with its binary.
    +
    +If you edit the script, a new target picks up the change, but a target that
    +already loaded it doesn't. To reload it there, import it with
    +`command script import`.
    +
    +## Keeping state across loads
    +
    +When a second target loads the same module, LLDB loads the script again, which
    +reloads the Python module and runs its top-level code again. A variable that the
    +module sets at top level starts over each time. If it has to survive, for
    +example a set of the targets you already set up, create it only when it doesn't
    +exist yet:
    +
    +```python3
    +if "configured_targets" not in globals():
    +    configured_targets = set()
    +```
    +
    +## Modules that your script imports
    +
    +LLDB only calls these functions on the script it loaded with the binary. If that script
    +imports other modules, with `import` or `command script import`, LLDB doesn't
    +call their `__lldb_module_added_to_target`. It doesn't know that they belong to
    +the module, or in which order they need to run.
    +
    +If your script is a small loader that pulls in the rest of your tools, forward
    +the call yourself, in the order your modules need:
    +
    +```python3
    +import os
    +import sys
    +
    +TOOLS = ["memory_tools", "thread_tools"]
    +
    +
    +def __lldb_init_module(debugger, internal_dict):
    +    directory = os.path.dirname(__file__)
    +    for name in TOOLS:
    +        path = os.path.join(directory, name + ".py")
    +        debugger.HandleCommand(f"command script import {path}")
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    for name in TOOLS:
    +        hook = getattr(sys.modules[name], "__lldb_module_added_to_target", None)
    +        if hook:
    +            hook(target, internal_dict)
    +```
    +
    +## Waiting for the process to stop
    +
    +LLDB calls `__lldb_module_added_to_target` when the module joins the target, so
    +the script usually can't read the process from there: the module has no load
    +address yet, there's no process, or, for a core file or an attach, the process
    +is still being loaded. To do something once the process has stopped, add a
    +one-shot stop hook with `--at-initial-stop true` from
    +`__lldb_module_added_to_target`. LLDB runs it at the first stop it can, then
    +deletes it.
    +
    +For example, this script reads its module's Mach-O header from memory, because
    +the information it needs is only there and not in the binary on disk:
    +
    +```python3
    +import struct
    +
    +import lldb
    +
    +MODULE_NAME = "MyApp"
    +
    +
    +class ReadMachOHeader:
    +    def __init__(self, target, extra_args, internal_dict):
    +        self.done = False
    +
    +    def handle_stop(self, exe_ctx, stream):
    +        # LLDB runs the hook for every thread that stopped, and we only need
    +        # to read the header once.
    +        if self.done:
    +            return True
    +        self.done = True
    +
    +        target = exe_ctx.GetTarget()
    +        module = target.module[MODULE_NAME]
    +        header = module.GetObjectFileHeaderAddress().GetLoadAddress(target)
    +        if header == lldb.LLDB_INVALID_ADDRESS:
    +            # Too early: the module isn't loaded yet. Try again at the next
    +            # stop.
    +            add_header_hook(target)
    +            return True
    +
    +        error = lldb.SBError()
    +        data = exe_ctx.GetProcess().ReadMemory(header, 32, error)
    +        if error.Fail():
    +            stream.Print(f"{MODULE_NAME}: could not read the Mach-O header: {error}\n")
    +            return True
    +
    +        _, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack("<8I", data)
    +        stream.Print(f"{MODULE_NAME}: {ncmds} load commands, {sizeofcmds} bytes\n")
    +        return True
    +
    +
    +def add_header_hook(target):
    +    result = lldb.SBCommandReturnObject()
    +    target.GetDebugger().GetCommandInterpreter().HandleCommand(
    +        f"target stop-hook add --one-shot true --at-initial-stop true "
    +        f"-P {__name__}.ReadMachOHeader",
    +        lldb.SBExecutionContext(target),
    +        result,
    +    )
    +
    +
    +def __lldb_module_added_to_target(target, internal_dict):
    +    add_header_hook(target)
    +```
    +
    +A few things in this example matter:
    +
    +- `HandleCommand` gets an `SBExecutionContext` for the target that
    +  `__lldb_module_added_to_target` was given, so the stop hook goes on that
    +  target, not on whichever target is selected.
    +- `--at-initial-stop true` lets the stop hook run at the stop LLDB is handling
    +  when it loads the script: when an attach completes, when a core file or a
    +  scripted process has finished loading, or, if the process already stopped,
    +  as soon as `__lldb_module_added_to_target` returns. Without it, the hook
    +  waits for the process to stop again, which a core file never does.
    +- Only the stop hooks that `__lldb_module_added_to_target` adds run as soon as
    +  it returns. If the process already stopped, a stop hook that
    +  `__lldb_init_module` or any other script adds first runs at the next stop,
    +  even with `--at-initial-stop true`.
    +- When LLDB loads the script with the target, before there's a process, the
    +  hook runs at the process's first stop. For a launch, that stop comes before
    +  the loader has run, so the module doesn't have a load address yet, and the
    +  hook adds itself again for the next stop.
    +- `handle_stop` returns `True`: the hook only reads, and the process should
    +  stay stopped. Returning `False` would ask LLDB to continue.
    +- At the stop where it runs, LLDB calls `handle_stop` for every thread that
    +  stopped, so the hook remembers that it already read the header.
    +- Because the hook is one-shot, LLDB deletes it after that stop, so it doesn't
    +  run at later stops and doesn't vote on whether they continue. It runs for one
    +  process: if you relaunch and need it again, add it again.

    I think that only running __lldb_module_added_to_target once in the Target's lifecycle makes sense for most of the target-specific work you want to do, but having to recreate the stop hooks on each rerun seems like it would be a PITN. I wonder if it would be better to make the combination --one-shot true --at-initial-stop true make a stop hook that is disabled but not deleted after the first time it is hit, and then re-enabled when the process dies.

    That would have the same positive benefits, but would not require me to re-add them on each subsequent run.

    Reviewers
    Assignees
    medismailben medismailben
    Labels
    risk: low confidence: 86.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 64.8% (MEDIUM) · Higher = larger blast radius
    64.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 86.8% · Lower = rushed or shallow review
    86.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 8.6% · Safe to merge
    8.6%