-
Notifications
You must be signed in to change notification settings - Fork 0
Test resuming a process #9
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,6 @@ | |||||||||
| #include <sys/ptrace.h> | ||||||||||
| #include <sys/wait.h> | ||||||||||
| #include <unistd.h> | ||||||||||
| #include <utility> | ||||||||||
| #include <vector> | ||||||||||
|
|
||||||||||
| namespace sdb | ||||||||||
|
|
@@ -29,6 +28,31 @@ Process::Process(pid_t pid, bool cleanupOnExit, bool isBeingTraced) | |||||||||
| { | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Process::Process(Process&& other) | ||||||||||
| : d_pid(other.d_pid), d_state(other.d_state), | ||||||||||
| d_cleanupOnExit(other.d_cleanupOnExit), | ||||||||||
| d_isBeingTraced(other.d_isBeingTraced) | ||||||||||
| { | ||||||||||
| // Ensure the underlying process is not cleaned | ||||||||||
| // when the object moved out of is destructed | ||||||||||
| other.d_pid = 0; | ||||||||||
| other.d_cleanupOnExit = false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Process& Process::operator=(Process&& other) | ||||||||||
| { | ||||||||||
| d_pid = other.d_pid; | ||||||||||
| d_state = other.d_state; | ||||||||||
| d_cleanupOnExit = other.d_cleanupOnExit; | ||||||||||
| d_isBeingTraced = other.d_isBeingTraced; | ||||||||||
|
|
||||||||||
| // Ensure the underlying process is not cleaned | ||||||||||
| // when the object moved out of is destructed | ||||||||||
| other.d_pid = 0; | ||||||||||
| other.d_cleanupOnExit = false; | ||||||||||
| return *this; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Process::~Process() | ||||||||||
| { | ||||||||||
| if (d_pid == 0) | ||||||||||
|
|
@@ -41,22 +65,23 @@ Process::~Process() | |||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (d_state == ProcessState::e_RUNNING) | ||||||||||
| { | ||||||||||
| kill(d_pid, SIGSTOP); | ||||||||||
| waitOnSignal(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Detach | ||||||||||
| if (d_isBeingTraced) | ||||||||||
| { | ||||||||||
| if (d_state == ProcessState::e_RUNNING) | ||||||||||
| { | ||||||||||
| kill(d_pid, SIGSTOP); | ||||||||||
| waitOnSignal(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Detach | ||||||||||
| if (auto rc | ||||||||||
| = ptrace(PTRACE_DETACH, d_pid, /*addr=*/nullptr, /*data=*/nullptr); | ||||||||||
| rc < 0) | ||||||||||
| { | ||||||||||
| std::cout << "Unable to detach from process, rc=" << rc << std::endl; | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| kill(d_pid, SIGCONT); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -157,11 +182,14 @@ void Process::resume() | |||||||||
| case ProcessState::e_STOPPED: | ||||||||||
| if (int rc = ptrace(PTRACE_CONT, d_pid, nullptr, nullptr); rc < 0) | ||||||||||
| { | ||||||||||
| std::cerr << "Unable to continue, rc=" << rc << std::endl; | ||||||||||
| std::stringstream ss; | ||||||||||
| ss << "Unable to continue proc with pid=" << d_pid << ", rc=" << rc | ||||||||||
| << ". perror="; | ||||||||||
| perror(ss.str().c_str()); | ||||||||||
|
||||||||||
| perror(ss.str().c_str()); | |
| std::string message = "Unable to continue proc with pid=" + std::to_string(d_pid) + | |
| ", rc=" + std::to_string(rc) + ". perror="; | |
| perror(message.c_str()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include <chrono> | ||
| #include <thread> | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| int main() | ||
| { | ||
| std::this_thread::sleep_for(5s); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The move assignment operator doesn't handle self-assignment properly. If
this == &other, the object will invalidate itself by settingother.d_pid = 0andother.d_cleanupOnExit = false. Add a self-assignment check at the beginning of the function.