◐ Shell
clean mode source ↗

gh-135953: Profile a module or script with sampling profiler by lkollar · Pull Request #136777 · python/cpython

@bedevere-app Bot mentioned this pull request

Jul 19, 2025

@lkollar lkollar marked this pull request as ready for review

July 19, 2025 14:59

pablogsal

lkollar

@AA-Turner AA-Turner changed the title [gh-135953] Profile a module or script with sampling profiler gh-135953: Profile a module or script with sampling profiler

Aug 4, 2025

AA-Turner

pablogsal

Add `-m` and `filename` arguments to the sampling profiler to launch the
specified Python program in a subprocess and start profiling it.
Previously only a PID was accepted, this can now be done by passing
`-p PID`.
These args are already mutually exclusive, but we need to check if at
least on module argument has been passed.
In this case the subprocess will go into zombie state until we can poll
it. We can simply assume this is the case if it's still detected as
running when we get a ValueError.
Improve the return value check to be able to raise a ProcessLookupError
when the remote process is not available.

Mach uses composite error values where higher error values indicate
specific subsystems. We can use the err_get_code function to mask the
higher bits to make our error checking more robust in case the subsystem
bits are set. For example, in some situations if the process is in zombie
state, we can get KERN_NO_SPACE (0x3) but the actual return value is
0x10000003 which indicates a specific subsystem, thus we need to
use err_get_code to extract the error value.

This also improves how KERN_INVALID_ARGUMENT is handled to check whether
we got a generic invalid argument error, or if the process is no longer
accessible.

@pablogsal

@pablogsal

@pablogsal

pablogsal

maurycy added a commit to maurycy/cpython that referenced this pull request

Aug 12, 2025

Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request

Aug 19, 2025