Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to /proc/$pid/mem when process_vm_readv() is unavailabe #4

Merged
merged 2 commits into from
Jan 30, 2021

Conversation

sebageek
Copy link

@sebageek sebageek commented Jan 24, 2021

If the Linux kernel was built without support for process_vm_readv()
(CONFIG_CROSS_MEMORY_ATTACH is unset in kernel config) a call to this
function will fail with Function not implemented (os error 38) aka
ENOSYS. To still support reading process memory on this platform we
fallback to reading /proc/$pid/mem.


This is intended as a fix for benfred/py-spy/issues/22. To reproduce this I recompiled my current kernel with CONFIG_CROSS_MEMORY_ATTACH disabled.

$ uname -r
5.10.4no-proc-mem-attach
$ grep CONFIG_CROSS_MEMORY_ATTACH /boot/config-$(uname -r)
# CONFIG_CROSS_MEMORY_ATTACH is not set

This yielded the Error: Function not implemented (os error 38). The patch in this PR subsequently fixed this error.

I'm still a bit unhappy with the fallback, as each time memory is read an attempt to use process_vm_readv() is made, which is one extra syscall per read. Additionally the filehandle to procmem could be cached per pid, saving two additional syscalls (open, close). This might lead to an API change though, so I wanted to discuss this before wildly implementing something. In any case, the code in this PR shouldn't change the performance for current users of this library. It will only be slower in cases where it would have errored out beforehand.

Shoutout to @eqv for rust support!

If the Linux kernel was built without support for process_vm_readv()
(CONFIG_CROSS_MEMORY_ATTACH is unset in kernel config) a call to this
function will fail with Function not implemented (os error 38) aka
ENOSYS. To still support reading process memory on this platform we
fallback to reading /proc/$pid/mem.
@sebageek sebageek force-pushed the read-proc-pid-mem-on-os-error-38 branch from 98110bf to 27863c7 Compare January 24, 2021 18:16
@hartwork
Copy link

@sebageek CI is green now on master if you feel like a rebase 🎉

@benfred benfred merged commit 1a0f49d into benfred:master Jan 30, 2021
@benfred
Copy link
Owner

benfred commented Jan 30, 2021

Thanks for this! This helps a lot - I'm surprised how many people hit this problem with py-spy

@sebageek sebageek deleted the read-proc-pid-mem-on-os-error-38 branch January 30, 2021 23:36
@nehaljwani
Copy link

nehaljwani commented Feb 19, 2021

@benfred Could you please cut a release with this patch? The 0.2.0 crate doesn't have this change. Also, the tags on the GH repo are not in sync :)

@benfred
Copy link
Owner

benfred commented Mar 22, 2021

@nehaljwani I've pushed out 0.2.1 to crates.io with this change (and am in the process of getting this change into dependant packages like remoteprocess/py-spy).

benfred added a commit to benfred/rbspy that referenced this pull request Mar 22, 2021
The latest remoteprocess code includes a workaround for the case where the linux kernel
was compiled without process_vm_readv support (by falling back to /proc/PID/mem:
benfred/read-process-memory#4).

Closes rbspy#196
benfred pushed a commit that referenced this pull request Oct 27, 2021
If the Linux kernel was built without support for process_vm_readv()
(CONFIG_CROSS_MEMORY_ATTACH is unset in kernel config) a call to this
function will fail with Function not implemented (os error 38) aka
ENOSYS. To still support reading process memory on this platform we
fallback to reading /proc/$pid/mem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants