Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve permission error messages in pdb and asyncio.tools#134290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ivonastojanovic wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromivonastojanovic:permission_error_message
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletionsLib/asyncio/tools.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
from collections import defaultdict
from itertools import count
from enum import Enum
import platform
import sys
from _remote_debugging import get_all_awaited_by

Expand DownExpand Up@@ -184,6 +185,83 @@ def _print_cycle_exception(exception: CycleFoundException):
print(f"cycle: {inames}", file=sys.stderr)


LINUX_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

This could be because the tracer lacks the required capability
(CAP_SYS_PTRACE), or because the process is already being traced. Additionally,
security restrictions may prevent attaching to processes you cannot signal, or
those running with set-user-ID/set-group-ID.

If you are trying to attach to a process you own, you can try the following:

* Re-run the command with elevated privileges, for example: 'sudo -E !!'

* Temporarily relax ptrace restrictions for the current session (until reboot)
by running:
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Note: Disabling ptrace scope reduces system hardening and should only be done
in trusted environments.
"""

MACOS_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

Try re-running the command with elevated privileges (e.g., using 'sudo'):

sudo python -m pdb -p <PID>

Note: Security restrictions may block debugging even for processes you own
unless run with root privileges.
"""

WINDOWS_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

Try running the command prompt or terminal as Administrator and re-run the
command.

Note: Some processes may still be inaccessible without special privileges such
as 'SeDebugPrivilege', even when running as Administrator.

To adjust file or folder permissions:

1. Right-click the file or folder and select "Properties".
2. Go to the "Security" tab. At the top, you'll see a list of users and groups
with current access.
3. Click "Edit" to change permissions.
4. In the "Group or user names" section, select your user account.
5. In the "Permissions" section below, check "Read" or "Full control" as needed.
6. Click "Apply", then "OK" to confirm changes.
"""


def exit_with_permission_help_text():
"""
Prints platform-specific permission help text and exits the program.

This function is called when a PermissionError is encountered while trying
to attach to a process.
"""
system = platform.system()
Copy link
Contributor

Choose a reason for hiding this comment

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

sys is already imported, so you can usesys.platform

if system == "Linux":
print(LINUX_PERMISSION_HELP_TEXT)
elif system == "Darwin":
print(MACOS_PERMISSION_HELP_TEXT)
elif system == "Windows":
print(WINDOWS_PERMISSION_HELP_TEXT)
else:
print(
"Permission denied when trying to attach to the process. "
"Make sure you have sufficient privileges."
)
sys.exit(1)


def _get_awaited_by_tasks(pid: int) -> list:
try:
return get_all_awaited_by(pid)
Expand All@@ -192,6 +270,8 @@ def _get_awaited_by_tasks(pid: int) -> list:
e = e.__context__
print(f"Error retrieving tasks: {e}")
sys.exit(1)
except PermissionError as e:
exit_with_permission_help_text()


def display_awaited_by_tasks_table(pid: int) -> None:
Expand Down
80 changes: 79 additions & 1 deletionLib/pdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,6 +67,7 @@

import os
import io
import platform
import re
import sys
import cmd
Expand DownExpand Up@@ -3499,6 +3500,80 @@ def help():
To let the script run up to a given line X in the debugged file, use
"-c 'until X'"."""

LINUX_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

This could be because the tracer lacks the required capability
(CAP_SYS_PTRACE), or because the process is already being traced. Additionally,
security restrictions may prevent attaching to processes you cannot signal, or
those running with set-user-ID/set-group-ID.

If you are trying to attach to a process you own, you can try the following:

* Re-run the command with elevated privileges, for example: 'sudo -E !!'

* Temporarily relax ptrace restrictions for the current session (until reboot)
by running:
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Note: Disabling ptrace scope reduces system hardening and should only be done
in trusted environments.
"""

MACOS_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

Try re-running the command with elevated privileges (e.g., using 'sudo'):

sudo python -m pdb -p <PID>

Note: Security restrictions may block debugging even for processes you own
unless run with root privileges.
"""

WINDOWS_PERMISSION_HELP_TEXT = """
Error: The specified process cannot be attached to due to insufficient
permissions.

Try running the command prompt or terminal as Administrator and re-run the
command.

Note: Some processes may still be inaccessible without special privileges such
as 'SeDebugPrivilege', even when running as Administrator.

To adjust file or folder permissions:

1. Right-click the file or folder and select "Properties".
2. Go to the "Security" tab. At the top, you'll see a list of users and groups
with current access.
3. Click "Edit" to change permissions.
4. In the "Group or user names" section, select your user account.
5. In the "Permissions" section below, check "Read" or "Full control" as needed.
6. Click "Apply", then "OK" to confirm changes.
"""

def exit_with_permission_help_text():
"""
Prints platform-specific permission help text and exits the program.

This function is called when a PermissionError is encountered while trying
to attach to a process.
"""
system = platform.system()
if system == "Linux":
print(LINUX_PERMISSION_HELP_TEXT)
elif system == "Darwin":
print(MACOS_PERMISSION_HELP_TEXT)
elif system == "Windows":
print(WINDOWS_PERMISSION_HELP_TEXT)
else:
print(
"Permission denied when trying to attach to the process. "
"Make sure you have sufficient privileges."
)
sys.exit(1)

def main():
import argparse
Expand DownExpand Up@@ -3533,7 +3608,10 @@ def main():
opts = parser.parse_args()
if opts.module:
parser.error("argument -m: not allowed with argument --pid")
attach(opts.pid, opts.commands)
try:
attach(opts.pid, opts.commands)
except PermissionError as e:
exit_with_permission_help_text()
return
elif opts.module:
# If a module is being debugged, we consider the arguments after "-m module" to
Expand Down
3 changes: 3 additions & 0 deletionsLib/test/test_remote_pdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1531,6 +1531,9 @@ def do_integration_test(self, client_stdin):
redirect_stdout(client_stdout),
redirect_stderr(client_stderr),
unittest.mock.patch("sys.argv", ["pdb", "-p", str(process.pid)]),
unittest.mock.patch(
"pdb.exit_with_permission_help_text", side_effect=PermissionError
),
):
try:
pdb.main()
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp