Skip to content

Commit 82e719c

Browse files
Improve permission error messages in pdb with platform-specific guidance
Add help texts for Linux, macOS, and Windows to guide users in resolving permission errors when attaching to a process using the -p option.
1 parent 8421b03 commit 82e719c

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

Lib/pdb.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
import os
6969
import io
70+
import platform
7071
import re
7172
import sys
7273
import cmd
@@ -3499,7 +3500,81 @@ def help():
34993500
To let the script run up to a given line X in the debugged file, use
35003501
"-c 'until X'"."""
35013502

3503+
LINUX_PERMISSION_HELP_TEXT = """
3504+
Error: The specified process cannot be attached to due to insufficient
3505+
permissions.
35023506
3507+
This could be because the tracer lacks the required capability
3508+
(CAP_SYS_PTRACE), or because the process is already being traced. Additionally,
3509+
security restrictions may prevent attaching to processes you cannot signal, or
3510+
those running with set-user-ID/set-group-ID.
3511+
3512+
If you are trying to attach to a process you own, you can try the following:
3513+
3514+
* Re-run the command with elevated privileges, for example: 'sudo -E !!'
3515+
3516+
* Temporarily relax ptrace restrictions for the current session (until reboot)
3517+
by running:
3518+
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
3519+
3520+
Note: Disabling ptrace scope reduces system hardening and should only be done
3521+
in trusted environments.
3522+
"""
3523+
3524+
MACOS_PERMISSION_HELP_TEXT = """
3525+
Error: The specified process cannot be attached to due to insufficient
3526+
permissions.
3527+
3528+
Try re-running the command with elevated privileges (e.g., using 'sudo'):
3529+
3530+
sudo python -m pdb -p <PID>
3531+
3532+
Note: Security restrictions may block debugging even for processes you own
3533+
unless run with root privileges.
3534+
"""
3535+
3536+
WINDOWS_PERMISSION_HELP_TEXT = """
3537+
Error: The specified process cannot be attached to due to insufficient
3538+
permissions.
3539+
3540+
Try running the command prompt or terminal as Administrator and re-run the
3541+
command.
3542+
3543+
Note: Some processes may still be inaccessible without special privileges such
3544+
as 'SeDebugPrivilege', even when running as Administrator.
3545+
3546+
To adjust file or folder permissions:
3547+
3548+
1. Right-click the file or folder and select "Properties".
3549+
2. Go to the "Security" tab. At the top, you'll see a list of users and groups
3550+
with current access.
3551+
3. Click "Edit" to change permissions.
3552+
4. In the "Group or user names" section, select your user account.
3553+
5. In the "Permissions" section below, check "Read" or "Full control" as needed.
3554+
6. Click "Apply", then "OK" to confirm changes.
3555+
"""
3556+
3557+
def exit_with_permission_help_text():
3558+
"""
3559+
Prints platform-specific permission help text and exits the program.
3560+
3561+
This function is called when a PermissionError is encountered while trying
3562+
to attach to a process.
3563+
"""
3564+
system = platform.system()
3565+
if system == "Linux":
3566+
print(LINUX_PERMISSION_HELP_TEXT)
3567+
elif system == "Darwin":
3568+
print(MACOS_PERMISSION_HELP_TEXT)
3569+
elif system == "Windows":
3570+
print(WINDOWS_PERMISSION_HELP_TEXT)
3571+
else:
3572+
print(
3573+
"Permission denied when trying to attach to the process. "
3574+
"Make sure you have sufficient privileges."
3575+
)
3576+
sys.exit(1)
3577+
35033578
def main():
35043579
import argparse
35053580

@@ -3533,7 +3608,10 @@ def main():
35333608
opts = parser.parse_args()
35343609
if opts.module:
35353610
parser.error("argument -m: not allowed with argument --pid")
3536-
attach(opts.pid, opts.commands)
3611+
try:
3612+
attach(opts.pid, opts.commands)
3613+
except PermissionError as e:
3614+
exit_with_permission_help_text()
35373615
return
35383616
elif opts.module:
35393617
# If a module is being debugged, we consider the arguments after "-m module" to

Lib/test/test_remote_pdb.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,9 @@ def do_integration_test(self, client_stdin):
15311531
redirect_stdout(client_stdout),
15321532
redirect_stderr(client_stderr),
15331533
unittest.mock.patch("sys.argv", ["pdb", "-p", str(process.pid)]),
1534+
unittest.mock.patch(
1535+
"pdb.exit_with_permission_help_text", side_effect=PermissionError
1536+
),
15341537
):
15351538
try:
15361539
pdb.main()

0 commit comments

Comments
 (0)