@@ -13,44 +13,92 @@ Most platforms require elevated privileges to attach to another Python process.
1313Permission requirements
1414=======================
1515
16- Attaching to a running Python process for remote debugging requires elevated
17- privileges on most platforms. The specific requirements and troubleshooting
16+ Attaching to a running Python process for remote debugging requires special
17+ configuration on most platforms. The specific requirements and troubleshooting
1818steps depend on your operating system:
1919
2020.. rubric :: Linux
2121
22- The tracer process must have the `` CAP_SYS_PTRACE `` capability or equivalent
23- privileges. You can only trace processes you own and can signal. Tracing may
24- fail if the process is already being traced, or if it is running with
25- set-user-ID or set-group-ID. Security modules like Yama may further restrict
26- tracing.
22+ In general, you can debug your own processes, but there are several common
23+ configurations that may disable this. Some Linux distributions enable ** ptrace
24+ restrictions **, aka "Yama," as a form of system hardening. Recent versions of
25+ the `` setpriv `` command (util-linux 2.41, released June 2025) let you loosen
26+ ptrace restrictions on a per-process basis:
2727
28- To temporarily relax ptrace restrictions (until reboot), run:
28+ ``setpriv --ptracer any python3 ``
29+
30+ (This is configured on the process *being debugged *.) You can also turn off
31+ ptrace restrictions for all processes until reboot with:
2932
3033 ``echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope ``
3134
35+ This can also be configured persistently, usually in ``/etc/sysctl.d ``.
36+
3237.. note ::
3338
3439 Disabling ``ptrace_scope `` reduces system hardening and should only be done
35- in trusted environments.
40+ in low-security environments.
41+
42+ It is also possible that the ``ptrace `` system call is disabled because of a
43+ security filter. In particular, this was common with older versions of Docker.
44+ Docker 19.03 or higher (released 2019) running on Linux kernel 4.8 or higher
45+ (released 2016) allow usage of the ``ptrace `` system call inside containers.
3646
37- If running inside a container, use ``--cap-add=SYS_PTRACE `` or
38- ``--privileged ``, and run as root if needed.
47+ If you need to trace a process that you *do not * own, you will need superuser
48+ access or equivalent. This also applies to processes that have changed their
49+ security credentials, e.g., set-user-ID or set-group-ID processes (though this
50+ is unusual for Python). Try running the debugging command with ``sudo -E ``.
3951
40- Try re-running the command with elevated privileges :
52+ .. note : :
4153
42- ``sudo -E !! ``
54+ The ``CAP_SYS_PTRACE `` capability is equivalent to superuser access, in
55+ that it allows debugging *any * process, not just your own. You may see
56+ advice on the internet suggesting using it to work around ptrace
57+ restrictions or system call filters. This may work in practice, as would
58+ ``sudo ``, but this gives the debugging process much more access than it
59+ needs and should only be done in low-security environments.
4360
61+ Finally, note that a process can only have one tracer at a time. If you are
62+ have already attached to Python process under ``strace ``, ``gdb ``, etc., you
63+ won't be able to simultaneously use remote debugging. (Superuser access cannot
64+ get around this restriction.)
4465
4566.. rubric :: macOS
4667
47- To attach to another process, you typically need to run your debugging tool
48- with elevated privileges. This can be done by using ``sudo `` or running as
49- root.
68+ By default, macOS disables the ability to debug other processes.
69+
70+ You can modify your Python binary to opt in to being debugged by giving it an
71+ **ad-hoc code signature **. (An ad-hoc "signature" is just a configuration
72+ without any actual cryptographic signature or a need for a certificate or
73+ anything else such as an Apple developer program membership.)
74+
75+ Create an entitlement property list file with the following contents:
76+
77+ .. code-block :: xml
78+
79+ <?xml version =" 1.0" encoding =" UTF-8" ?>
80+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
81+ <plist version =" 1.0" >
82+ <dict >
83+ <key >com.apple.security.get-task-allow</key >
84+ <true />
85+ </dict >
86+ </plist >
87+
88+ Save it to a file e.g. ``get-task-allow.plist ``, and then run
89+
90+ ``codesign --sign - --entitlements get-task-allow.plist path/to/bin/python3 ``
91+
92+ (These instructions are for a non-framework build of Python. Framework builds
93+ may need to be configured differently.)
5094
51- Even when attaching to processes you own, macOS may block debugging unless
52- the debugger is run with root privileges due to system security restrictions .
95+ You should then be able to debug your own Python processes started with that
96+ binary .
5397
98+ Alternatively, much as with Linux, processes with superuser privileges e.g. ``sudo ``
99+ are not subject to this check and can debug any user's process on the system
100+ (though there are additional checks on specific binaries, such as OS-provided
101+ commands, due to System Integrity Protection).
54102
55103.. rubric :: Windows
56104
0 commit comments