Skip to content

Commit

Permalink
Implement backoff loop when trying to copy the pthread id
Browse files Browse the repository at this point in the history
To get the pthread id we copy a buffer of 200 pointers from the
start of the pthread structure and try to match the pid inside. It is
possible that the process doesn't have that much space available in a
valid memory map so reading this much memory will fail.

To fix this, try to reduce the size of the buffer by half every time we
fail to copy from the process until we succeed or we reach 0.

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Matt Wozniski <godlygeek@gmail.com>
  • Loading branch information
pablogsal and godlygeek committed Nov 16, 2023
1 parent 6b6f221 commit 6f2af72
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pystack/_pystack/pythread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,20 @@ findPthreadTidOffset(
auto pthread_id_addr = manager->getField(current_thread, &py_thread_v::o_thread_id);

// Attempt to locate a field in the pthread struct that's equal to the pid.
uintptr_t buffer[200];
manager->copyObjectFromProcess(pthread_id_addr, &buffer);
for (int i = 0; i < 200; i++) {
uintptr_t buffer[100];
size_t buffer_size = sizeof(buffer);
while (buffer_size > 0) {
try {
LOG(DEBUG) << "Trying to copy a buffer of " << buffer_size << " bytes to get pthread ID";
manager->copyMemoryFromProcess(pthread_id_addr, buffer_size, &buffer);
break;
} catch (const RemoteMemCopyError& ex) {
LOG(DEBUG) << "Failed to copy buffer to get pthread ID";
buffer_size /= 2;
}
}
LOG(DEBUG) << "Copied a buffer of " << buffer_size << " bytes to get pthread ID";
for (int i = 0; i < buffer_size / sizeof(uintptr_t); i++) {
if (static_cast<pid_t>(buffer[i]) == manager->Pid()) {
off_t offset = sizeof(uintptr_t) * i;
LOG(DEBUG) << "Tid offset located by scanning at offset " << std::showbase << std::hex
Expand Down

0 comments on commit 6f2af72

Please sign in to comment.