Debugging a QProcess on Linux/GDB hangs
-
wrote on 15 Feb 2023, 07:56 last edited by __mk__
I have a program where I create a child process using
QProcess
which I want to debug with the Qt Creator Debugger. I'm on Ubuntu 20.04.5 LTS and I'm using gdb version 9.2.The project is compiled in Debug mode. When I start the debugger, the program runs normally, but then hangs where I would expect the child process to finish.
The program works fine when I run it without the debugger. (both in debug mode and release mode)
Here's my program:
#include <QCoreApplication> #include <QDebug> #include <QProcess> QString getUname(void) { QProcess* myProcess = new QProcess(); QString uname; myProcess->start("uname", QStringList()<<"-a"); myProcess->waitForFinished(100); uname = myProcess->readAllStandardOutput().replace("\n", ""); delete myProcess; return uname; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "uname:"; qDebug() << getUname(); return a.exec(); }
When I run it in debugger, it prints "uname:" and then hangs. It does not print the output of the uname command, but the debugger does not finish either.
Here's the debugger log (I've read that this is important for troubleshooting) https://pastebin.com/QMWRfrdH
-
I have a program where I create a child process using
QProcess
which I want to debug with the Qt Creator Debugger. I'm on Ubuntu 20.04.5 LTS and I'm using gdb version 9.2.The project is compiled in Debug mode. When I start the debugger, the program runs normally, but then hangs where I would expect the child process to finish.
The program works fine when I run it without the debugger. (both in debug mode and release mode)
Here's my program:
#include <QCoreApplication> #include <QDebug> #include <QProcess> QString getUname(void) { QProcess* myProcess = new QProcess(); QString uname; myProcess->start("uname", QStringList()<<"-a"); myProcess->waitForFinished(100); uname = myProcess->readAllStandardOutput().replace("\n", ""); delete myProcess; return uname; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "uname:"; qDebug() << getUname(); return a.exec(); }
When I run it in debugger, it prints "uname:" and then hangs. It does not print the output of the uname command, but the debugger does not finish either.
Here's the debugger log (I've read that this is important for troubleshooting) https://pastebin.com/QMWRfrdH
@__mk__ said in Debugging a QProcess on Linux/GDB hangs:
and then hangs
Where exactly does it hang?
Note: there is no need to allocate QProcess on the heap in this case as you anyway wait for it to finish.
-
I have a program where I create a child process using
QProcess
which I want to debug with the Qt Creator Debugger. I'm on Ubuntu 20.04.5 LTS and I'm using gdb version 9.2.The project is compiled in Debug mode. When I start the debugger, the program runs normally, but then hangs where I would expect the child process to finish.
The program works fine when I run it without the debugger. (both in debug mode and release mode)
Here's my program:
#include <QCoreApplication> #include <QDebug> #include <QProcess> QString getUname(void) { QProcess* myProcess = new QProcess(); QString uname; myProcess->start("uname", QStringList()<<"-a"); myProcess->waitForFinished(100); uname = myProcess->readAllStandardOutput().replace("\n", ""); delete myProcess; return uname; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "uname:"; qDebug() << getUname(); return a.exec(); }
When I run it in debugger, it prints "uname:" and then hangs. It does not print the output of the uname command, but the debugger does not finish either.
Here's the debugger log (I've read that this is important for troubleshooting) https://pastebin.com/QMWRfrdH
wrote on 15 Feb 2023, 08:13 last edited by@__mk__
gdb behaviour can be controlled when the process spawns a sub-process ("forking"). Although the default behaviour is to allow the child to run unimpeded I suppose you might have changed that, you could read https://sourceware.org/gdb/onlinedocs/gdb/Forks.html for information.What happens if you debug your program by running
gdb
directly in a terminal/shell instead of from Qt Creator --- any different behaviour? -
@__mk__
gdb behaviour can be controlled when the process spawns a sub-process ("forking"). Although the default behaviour is to allow the child to run unimpeded I suppose you might have changed that, you could read https://sourceware.org/gdb/onlinedocs/gdb/Forks.html for information.What happens if you debug your program by running
gdb
directly in a terminal/shell instead of from Qt Creator --- any different behaviour?wrote on 15 Feb 2023, 09:24 last edited byManually debugging the program via command-line gdb works fine.
Starting program: /home/user/build-TestProject-Desktop_Qt_5_15_2_GCC_64bit-Debug/TestProject [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". uname: [Attaching after Thread 0x7ffff4e49780 (LWP 12448) fork to child process 12452] [New inferior 2 (process 12452)] [Detaching after fork from parent process 12448] [Inferior 1 (process 12448) detached] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". process 12452 is executing new program: /usr/bin/uname [Inferior 2 (process 12452) exited normally] "Linux u 5.15.0-60-generic #66~20.04.1-Ubuntu SMP Wed Jan 25 09:41:30 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
The Qt creator debugger seems to hang where I call
myProcess->start
. I set a breakpoint for that line and the lines afterwards. The debugger waits on the line where I callstart
and then I click continue. This is the point where it hangs. The line afterstart
never gets reached. -
Manually debugging the program via command-line gdb works fine.
Starting program: /home/user/build-TestProject-Desktop_Qt_5_15_2_GCC_64bit-Debug/TestProject [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". uname: [Attaching after Thread 0x7ffff4e49780 (LWP 12448) fork to child process 12452] [New inferior 2 (process 12452)] [Detaching after fork from parent process 12448] [Inferior 1 (process 12448) detached] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". process 12452 is executing new program: /usr/bin/uname [Inferior 2 (process 12452) exited normally] "Linux u 5.15.0-60-generic #66~20.04.1-Ubuntu SMP Wed Jan 25 09:41:30 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
The Qt creator debugger seems to hang where I call
myProcess->start
. I set a breakpoint for that line and the lines afterwards. The debugger waits on the line where I callstart
and then I click continue. This is the point where it hangs. The line afterstart
never gets reached.wrote on 15 Feb 2023, 09:30 last edited by JonB@__mk__
Well this output from standalone gdb seems to indicate that it actually attaches to the forked child process and detaches from the parent process, which I was not expecting. If it does the same from Qt Creator (doesn't that have some window pane to show the sort of output you are getting from gdb?) I imagine that could be problematic.Did you read the link I referenced? I think you should make sure just what your gdb "fork" settings are, you can print them out from standalone gdb I think.
-
wrote on 16 Feb 2023, 11:09 last edited by
The log is indeed helpful: You have non-default settings that are apparently not helpful in your case:
d/MultiInferior: true (default: false) ***
[...]
set detach-on-fork off
1/6