| 1 | #include "KittyTrace.hpp" |
| 2 | |
| 3 | bool KittyTraceMgr::attach(int options) |
| 4 | { |
| 5 | if (_pid <= 0) |
| 6 | return false; |
| 7 | |
| 8 | if (isAttached()) |
| 9 | { |
| 10 | _attached = true; |
| 11 | return true; |
| 12 | } |
| 13 | |
| 14 | errno = 0; |
| 15 | if (ptrace(PTRACE_ATTACH, _pid, nullptr, options) == -1L) |
| 16 | { |
| 17 | KITTY_LOGE("PTRACE_ATTACH failed for pid %d. \"%s\".", _pid, strerror(errno)); |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | _seized = false; |
| 22 | |
| 23 | int status; |
| 24 | if (waitpid(_pid, &status, 0) != _pid || !WIFSTOPPED(status)) |
| 25 | { |
| 26 | KITTY_LOGE("Error occurred while waiting for pid %d to stop. \"%s\".", _pid, strerror(errno)); |
| 27 | ptrace(PTRACE_DETACH, _pid, nullptr, nullptr); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | _attached = true; |
| 32 | |
| 33 | if (options != 0) |
| 34 | setOptions(options); |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool KittyTraceMgr::seize(int options) |
| 40 | { |