Skip to content

Commit b799433

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

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

Lib/profiling/sampling/__main__.py

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

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

50+
def handle_permission_error():
51+
"""Handle PermissionError by displaying appropriate error message."""
52+
if sys.platform == "darwin":
53+
print(MACOS_PERMISSION_ERROR, file=sys.stderr)
54+
elif sys.platform.startswith("linux"):
55+
print(LINUX_PERMISSION_ERROR, file=sys.stderr)
56+
elif sys.platform.startswith("win"):
57+
print(WINDOWS_PERMISSION_ERROR, file=sys.stderr)
58+
else:
59+
print(GENERIC_PERMISSION_ERROR, file=sys.stderr)
60+
sys.exit(1)
61+
562
if __name__ == '__main__':
6-
main()
63+
try:
64+
main()
65+
except PermissionError:
66+
handle_permission_error()

0 commit comments

Comments
 (0)