Skip to content

Commit ab54791

Browse files
committed
gh-138704: Add PermissionError messages to profiling.sampling
1 parent 074f3b2 commit ab54791

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

Lib/profiling/sampling/__main__.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,59 @@
11
"""Run the sampling profiler from the command line."""
22

3+
import sys
4+
5+
MACOS_PERMISSION_ERROR = """\
6+
🔒 Tachyon was unable to access process memory due to insufficient permissions on Mac OS.
7+
To profile processes, you may need to run Tachyon with sudo. Note that System Integrity Protection (SIP) can block access to Python binaries in protected locations.
8+
Using a virtual environment or installing Python outside protected directories may help.
9+
"""
10+
11+
LINUX_PERMISSION_ERROR = """
12+
🔒 Tachyon was unable to acess process memory. This could be because tachyon
13+
has insufficient privileges (the required capability is CAP_SYS_PTRACE).
14+
Unprivileged processes cannot trace processes that they cannot send signals
15+
to or those running set-user-ID/set-group-ID programs, for security reasons.
16+
17+
If your uid matches the uid of the target process you want to analyze, you
18+
can do one of the following to get 'ptrace' scope permissions:
19+
20+
* If you are running inside a Docker container, you need to make sure you
21+
start the container using the '--cap-add=SYS_PTRACE' or '--privileged'
22+
command line arguments. Notice that this may not be enough if you are not
23+
running as 'root' inside the Docker container as you may need to disable
24+
hardening (see next points).
25+
26+
* Try running again with elevated permissions by running 'sudo -E !!'.
27+
28+
* You can disable kernel hardening for the current session temporarily (until
29+
a reboot happens) by running 'echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope'.
30+
"""
31+
32+
WINDOWS_PERMISSION_ERROR = """
33+
🔒 Tachyon requires administrator rights to access process memory on Windows.
34+
Please run your command prompt as Administrator and try again.
35+
"""
36+
37+
GENERIC_PERMISSION_ERROR = """
38+
🔒 Tachyon was unable to access the target process due to operating system restrictions or missing privileges.
39+
"""
40+
341
from .sample import main
442

43+
def handle_permission_error():
44+
"""Handle PermissionError by displaying appropriate error message."""
45+
if sys.platform == "darwin":
46+
print(MACOS_PERMISSION_ERROR, file=sys.stderr)
47+
elif sys.platform.startswith("linux"):
48+
print(LINUX_PERMISSION_ERROR, file=sys.stderr)
49+
elif sys.platform.startswith("win"):
50+
print(WINDOWS_PERMISSION_ERROR, file=sys.stderr)
51+
else:
52+
print(GENERIC_PERMISSION_ERROR, file=sys.stderr)
53+
sys.exit(1)
54+
555
if __name__ == '__main__':
6-
main()
56+
try:
57+
main()
58+
except PermissionError:
59+
handle_permission_error()

0 commit comments

Comments
 (0)