Skip to content

Add lldb attach-to-PID option for debugpy on Linux - #2052

Open
pdepetro wants to merge 1 commit into
microsoft:mainfrom
pdepetro:main
Open

Add lldb attach-to-PID option for debugpy on Linux#2052
pdepetro wants to merge 1 commit into
microsoft:mainfrom
pdepetro:main

Conversation

@pdepetro

Copy link
Copy Markdown
Contributor

Add an option to prefer lldb (falling back to gdb) when attaching to a process by PID on Linux.

debugpy's attach-by-PID flow injects the debug server into a target process. On Linux it has always shelled out to gdb; on macOS it uses lldb. In some environments lldb is the available/preferred injector on Linux. This adds an opt-in preference, surfaced as a VS Code launch configuration option "linuxAttachPreferLldb".

The selector flows end to end: adapter/clients.py reads linuxAttachPreferLldb from the attach request and forwards --linux-attach-prefer-lldb to the injector subprocess; server/cli.py parses that switch and sets the PYDEVD_ATTACH_PREFER_LLDB environment variable; add_code_to_python_process.py reads it at call time in the new run_python_code_linux dispatcher, which uses lldb when it is preferred and present on PATH, and otherwise transparently falls back to gdb.

Implementation notes:

  • run_python_code_linux_lldb mirrors the existing run_python_code_mac lldb invocation but resolves the Linux .so via get_target_filename() and reuses the shared linux_and_mac/lldb_prepare.py driver. No native code changes are needed: both paths call the same exported DoAttach entry point.
  • The PEP 768 sys.remote_exec fast path (Python 3.14+) runs first and is unaffected; the lldb preference only applies on the gdb/lldb fallback path.

Add an option to prefer lldb (falling back to gdb) when attaching to a
process by PID on Linux.

debugpy's attach-by-PID flow injects the debug server into a target
process. On Linux it has always shelled out to gdb; on macOS it uses
lldb. In some environments lldb is the available/preferred injector on
Linux. This adds an opt-in preference, surfaced as a VS Code launch
configuration option "linuxAttachPreferLldb".

The selector flows end to end: adapter/clients.py reads
linuxAttachPreferLldb from the attach request and forwards
--linux-attach-prefer-lldb to the injector subprocess; server/cli.py
parses that switch and sets the PYDEVD_ATTACH_PREFER_LLDB environment
variable; add_code_to_python_process.py reads it at call time in the new
run_python_code_linux dispatcher, which uses lldb when it is preferred
and present on PATH, and otherwise transparently falls back to gdb.

Implementation notes:
- run_python_code_linux_lldb mirrors the existing run_python_code_mac
  lldb invocation but resolves the Linux .so via get_target_filename()
  and reuses the shared linux_and_mac/lldb_prepare.py driver. No native
  code changes are needed: both paths call the same exported DoAttach
  entry point.
- The PEP 768 sys.remote_exec fast path (Python 3.14+) runs first and is
  unaffected; the lldb preference only applies on the gdb/lldb fallback
  path.
@pdepetro
pdepetro requested a review from a team as a code owner July 31, 2026 21:02
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@rchiodo

rchiodo commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

🔒 Automated review in progress — @rchiodo is auto-reviewing this PR.

@rchiodo

rchiodo commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

GitHub cannot anchor PR review comments to unchanged lines in the diff. Falling back to a general PR comment for tests/debugpy/server/test_cli.py:L390.

Warning · Non-blocking recommendation

This assertion hard-codes show_debug_info=0, but production reads DEBUGPY_ATTACH_BY_PID_DEBUG_INFO. The test isolates PYDEVD_ATTACH_PREFER_LLDB but not that variable, so environments that set it will fail this test spuriously. Clear it in the patched environment or assert show_debug_info=some.int.

env.pop("PYTHONIOENCODING", None)
env.pop("PYTHONPATH", None)
print("Running: %s" % (" ".join(cmd)))
subprocess.check_call(" ".join(cmd), shell=True, env=env)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Non-blocking recommendation

run_python_code_linux_lldb duplicates the macOS lldb injector, including command construction, quoting, environment scrubbing, and subprocess execution. This creates two sources of truth for lldb fixes. Please extract the shared lldb flow into a helper, or explicitly document why the implementations must remain mirrored.

@rchiodo rchiodo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@rchiodo rchiodo added the review-auto:approved Automated review: no blocking findings (approval posted). label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants