|
67 | 67 |
|
68 | 68 | importos
|
69 | 69 | importio
|
| 70 | +importplatform |
70 | 71 | importre
|
71 | 72 | importsys
|
72 | 73 | importcmd
|
@@ -3499,7 +3500,81 @@ def help():
|
3499 | 3500 | To let the script run up to a given line X in the debugged file, use
|
3500 | 3501 | "-c 'until X'"."""
|
3501 | 3502 |
|
| 3503 | +LINUX_PERMISSION_HELP_TEXT=""" |
| 3504 | +Error: The specified process cannot be attached to due to insufficient |
| 3505 | +permissions. |
3502 | 3506 |
|
| 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 | +defexit_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 | +ifsystem=="Linux": |
| 3566 | +print(LINUX_PERMISSION_HELP_TEXT) |
| 3567 | +elifsystem=="Darwin": |
| 3568 | +print(MACOS_PERMISSION_HELP_TEXT) |
| 3569 | +elifsystem=="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 | + |
3503 | 3578 | defmain():
|
3504 | 3579 | importargparse
|
3505 | 3580 |
|
@@ -3533,7 +3608,10 @@ def main():
|
3533 | 3608 | opts=parser.parse_args()
|
3534 | 3609 | ifopts.module:
|
3535 | 3610 | parser.error("argument -m: not allowed with argument --pid")
|
3536 |
| -attach(opts.pid,opts.commands) |
| 3611 | +try: |
| 3612 | +attach(opts.pid,opts.commands) |
| 3613 | +exceptPermissionErrorase: |
| 3614 | +exit_with_permission_help_text() |
3537 | 3615 | return
|
3538 | 3616 | elifopts.module:
|
3539 | 3617 | # If a module is being debugged, we consider the arguments after "-m module" to
|
|