Macok
7th February 2009, 19:51
I'm writting an application which will be doing something with memory of other process.
These actions may take some time, so I decided to place them in another thread.
Here's the simplified class of this thread:
Thread : public QThread{
public:
int pid;
void run();
};and run() function:
void Thread::run(){
ptrace(PTRACE_ATTACH, pid);
sleep(60);
ptrace(PTRACE_DETACH, pid);
}Of course i put those sleep for example only, in my program there are another functions, but it doesn't matter.
User can start or stop this thread by clicking on buttons - Start, and Cancel.
I have no problems with starting this thread, here's the simplified code:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startThread()));
(...)
void MainWindow::startThread(){
thread.pid=getPid();
thread.start();
}But problem appears, when user clicks on Cancel button.
Before doing anything with memory of other process it's needed to attach this process.
As you see, I'm doing it at the beggining of run() function.
But when process is attached it's impossible to use it - it must be detached first.
That's why i added the last line to run():
ptrace(PTRACE_DETACH, pid);But when user clicks Cancel process also need to be detached, so I overloaded terminate() function:
void Thread::terminate(){
ptrace(PTRACE_DETACH, pid);
QThread::terminate();
}
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(stopThread()));
(...)
void MainWindow::stopThread(){
thread.terminate();
}I think that should work fine, but it doesn't.
When user doesn't click Cancel, 60 seconds passess and process is detaching.
But when user click Cancel detaching fails.
How is it possible?
The same function, called in exactly the same way, and first works but second doesn't.
Thanks in advance and sorry for bad english!
These actions may take some time, so I decided to place them in another thread.
Here's the simplified class of this thread:
Thread : public QThread{
public:
int pid;
void run();
};and run() function:
void Thread::run(){
ptrace(PTRACE_ATTACH, pid);
sleep(60);
ptrace(PTRACE_DETACH, pid);
}Of course i put those sleep for example only, in my program there are another functions, but it doesn't matter.
User can start or stop this thread by clicking on buttons - Start, and Cancel.
I have no problems with starting this thread, here's the simplified code:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startThread()));
(...)
void MainWindow::startThread(){
thread.pid=getPid();
thread.start();
}But problem appears, when user clicks on Cancel button.
Before doing anything with memory of other process it's needed to attach this process.
As you see, I'm doing it at the beggining of run() function.
But when process is attached it's impossible to use it - it must be detached first.
That's why i added the last line to run():
ptrace(PTRACE_DETACH, pid);But when user clicks Cancel process also need to be detached, so I overloaded terminate() function:
void Thread::terminate(){
ptrace(PTRACE_DETACH, pid);
QThread::terminate();
}
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(stopThread()));
(...)
void MainWindow::stopThread(){
thread.terminate();
}I think that should work fine, but it doesn't.
When user doesn't click Cancel, 60 seconds passess and process is detaching.
But when user click Cancel detaching fails.
How is it possible?
The same function, called in exactly the same way, and first works but second doesn't.
Thanks in advance and sorry for bad english!