LLDB and environment variables in Xcode

Hey!

I am writing type formatting scripts in Python for lldb, as described in https://lldb.llvm.org/use/variable.html#python-scripting. I'm trying to pass an environment variable to lldb from Xcode to determine the path to the scripts in the project root, so the project is not bound to some predetermined path. I am having trouble doing this in Xcode.

What I have tried:

  1. lldbinit file
    1. Create a .lldbinit file in $(SRCROOT) with the following content: platform shell echo $SRCROOT.
    2. Set the path to the .lldbinit file in Edit Scheme... > Info > LLDB Init File ($(SRCROOT)/.lldbinit).
    3. Set the SRCROOT environment variable to $(SRCROOT) in Edit Scheme... > Arguments > Environment Variables.
  2. entry-point breakpoint
    1. Add a breakpoint to the entry-point of the application.
    2. Set the Automatically continue after evaluating actions option.
    3. Add the action platform shell $(SRCROOT) or platform shell $SRCROOT.
    4. Set the SRCROOT environment variable to $(SRCROOT) in Edit Scheme... > Arguments > Environment Variables.

What I expect to happen: The first line of the debug console is a path pointing to $(SRCROOT) What actually happens: No path is output

Is this functionality available in Xcode Version 26.0.1 (17A400)?

Thanks in advance, Barnabas

Answered by Developer Tools Engineer in 866513022

Xcode doesn't pass these environment variables to lldb when it runs it, nor does it preprocess the .lldbinit files to insert these variables.

We don't pass them to lldb as there's too much chance one of them really wouldn't be appropriate for lldb and cause hard to diagnose problems. And since .lldbinit files can contain arbitrary user expressions, preprocessing can also cause problems.

Instead, the suggested way to do this is to make a target specific lldbinit file - as you have done - and in that file bring in all the .py files you need using

command script import --relative-to-command-file ./my_python_files/my_python_file.py

That allows you to give the paths to all your python files relative to the target-specific .lldbinit file without having to know where any of them actually are.

Note, the same option exists for sourcing other files of lldb commands using command source.

Accepted Answer

Xcode doesn't pass these environment variables to lldb when it runs it, nor does it preprocess the .lldbinit files to insert these variables.

We don't pass them to lldb as there's too much chance one of them really wouldn't be appropriate for lldb and cause hard to diagnose problems. And since .lldbinit files can contain arbitrary user expressions, preprocessing can also cause problems.

Instead, the suggested way to do this is to make a target specific lldbinit file - as you have done - and in that file bring in all the .py files you need using

command script import --relative-to-command-file ./my_python_files/my_python_file.py

That allows you to give the paths to all your python files relative to the target-specific .lldbinit file without having to know where any of them actually are.

Note, the same option exists for sourcing other files of lldb commands using command source.

I didn't know about script import's --relative-to-command-file flag. This is exactly what I was looking for. Thank you!

I tried the solution today and would like to post a correction:

Using the --relative-to-command-file or -c flag will then expect a Python module name, which needs to be located directly next to the command file. Demonstrating with examples:

  • $(SRCROOT)/.lldbinit with the following content: command script import --relative-to-command-file ./lldb/type_formatters.py
    • Will result in an error: error: module importing failed: Python does not allow dots in module names: ./lldb/type_formatters
  • $(SRCROOT)/lldb/.lldbinit with the following content: command script import --relative-to-command-file type_formatters
    • Will result in a successful import
  • $(SRCROOT)/.lldbinit with the following content: command script import lldb
    • Will succeed if $(SRCROOT)/lldb is a Python module, denoted with a __init__.py file at $(SRCROOT)/lldb/__init__.py
    • Further python modules then can be imported with command script import <module_name> which live under $(SRCROOT)/lldb
LLDB and environment variables in Xcode
 
 
Q