I intend to simplify debuggning by having my program start gdb to automatically print a backtrace whenever the program crashes. The first stumbling block seems to be reading from a QProcess.
The program below deliberately causes a segfault and starts gdb from the signal handler. For now, I just want to read whatever gdb outputs and pass selected lines to the console. When I run this program, 9 times out of 10 it hangs after printing "about to start gdb". About every 10th time it manages to read and echo the first 10 or so lines of output from gdb.
I don't think getting gdb to start is a problem, as it always seems to start if I set the channel mode to ForwardedChannels (so that gdb prints directly to the console).
So my questions are:
1) Why doesn't this program do the same thing every time?
2) Why do I only get the first few lines of output? (the rest of the lines show up after I explicitly kill the gdb process)
QT 4.3.4, Linux Ubuntu 7.10
#include <QApplication>
#include <QProcess>
#include <signal.h>
void segv_handler(int)
{
qDebug("Caught SEGV");
pro.
setProcessChannelMode(QProcess::MergedChannels);
//pro.setProcessChannelMode(QProcess::ForwardedChannels);
qDebug("about to start gdb");
pro.
start("gdb",
QStringList() << qApp
->arguments
().
at(0) << qPrintable
(QString::number(getpid
())));
qDebug("waiting for start");
if (!pro.waitForStarted()) {
qDebug("Could not start gdb");
exit(1);
}
char buf[1024];
int len;
while(true) {
pro.waitForReadyRead();
do {
len = pro.readLine(buf, sizeof(buf));
if (len>0)
qDebug("%s", buf);
} while (len>0);
}
}
void cause_segv(int x)
{
*(int *)0 = x;
}
int main(int argc, char *argv[])
{
signal(SIGSEGV, segv_handler);
cause_segv(25);
app.exec();
return 0;
}
#include <QApplication>
#include <QProcess>
#include <signal.h>
void segv_handler(int)
{
qDebug("Caught SEGV");
QProcess pro;
pro.setProcessChannelMode(QProcess::MergedChannels);
//pro.setProcessChannelMode(QProcess::ForwardedChannels);
qDebug("about to start gdb");
pro.start("gdb", QStringList() << qApp->arguments().at(0) << qPrintable(QString::number(getpid())));
qDebug("waiting for start");
if (!pro.waitForStarted()) {
qDebug("Could not start gdb");
exit(1);
}
char buf[1024];
int len;
while(true) {
pro.waitForReadyRead();
do {
len = pro.readLine(buf, sizeof(buf));
if (len>0)
qDebug("%s", buf);
} while (len>0);
}
}
void cause_segv(int x)
{
*(int *)0 = x;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
signal(SIGSEGV, segv_handler);
cause_segv(25);
app.exec();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks