Results 1 to 7 of 7

Thread: Reading from a QProcess

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Reading from a QProcess

    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


    Qt Code:
    1. #include <QApplication>
    2. #include <QProcess>
    3.  
    4. #include <signal.h>
    5.  
    6.  
    7. void segv_handler(int)
    8. {
    9. qDebug("Caught SEGV");
    10. QProcess pro;
    11. pro.setProcessChannelMode(QProcess::MergedChannels);
    12. //pro.setProcessChannelMode(QProcess::ForwardedChannels);
    13. qDebug("about to start gdb");
    14. pro.start("gdb", QStringList() << qApp->arguments().at(0) << qPrintable(QString::number(getpid())));
    15. qDebug("waiting for start");
    16. if (!pro.waitForStarted()) {
    17. qDebug("Could not start gdb");
    18. exit(1);
    19. }
    20. char buf[1024];
    21. int len;
    22. while(true) {
    23. pro.waitForReadyRead();
    24. do {
    25. len = pro.readLine(buf, sizeof(buf));
    26. if (len>0)
    27. qDebug("%s", buf);
    28. } while (len>0);
    29. }
    30. }
    31.  
    32.  
    33. void cause_segv(int x)
    34. {
    35. *(int *)0 = x;
    36. }
    37.  
    38.  
    39. int main(int argc, char *argv[])
    40. {
    41. QApplication app(argc, argv);
    42.  
    43. signal(SIGSEGV, segv_handler);
    44.  
    45. cause_segv(25);
    46.  
    47. app.exec();
    48. return 0;
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reading from a QProcess

    How about using this instead?

    http://www.thorsen-consulting.dk/freebies.html

    By the way. When the application segfaults, you musn't call any Qt methods as the application is in an unpredictable state.

  3. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading from a QProcess

    I tried the backtrace from thorsen consulting, it gave me a trace looking like this:
    0: prog [0x8049989]
    1: prog(__gxx_personality_v0+0x253) [0x8049427]
    2: [0xffffe420]
    3: prog(__gxx_personality_v0+0x16b) [0x804933f]
    4: prog(__gxx_personality_v0+0x1c0) [0x8049394]
    5: /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0) [0xb6e62050]
    6: prog(__gxx_personality_v0+0xfd) [0x80492d1]


    It looks the same whether the executable has debuginfo in it or not.

    Some further testing show that the problems I had reading from gdb was not caused by calling Qt functions after a SEGV, it did not work better when calling segv_handler as an ordinary function either. With some more command line options, gdb becomes easier to control from another process:


    Qt Code:
    1. #include <QApplication>
    2. #include <QProcess>
    3.  
    4. #include <signal.h>
    5.  
    6. void segv_handler(int)
    7. {
    8. QProcess pro;
    9. pro.setProcessChannelMode(QProcess::MergedChannels);
    10. qDebug("Caught SEGV, trying to get a backtrace from gdb...");
    11. pro.start("gdb", QStringList()
    12. << "-batch"
    13. << "-x" << "/home/drhex/src/foto/backtrace" // a file that just contains the word "backtrace"
    14. << qApp->arguments().at(0) << qPrintable(QString::number(getpid()))
    15. ,QIODevice::ReadOnly);
    16. if (!pro.waitForStarted()) {
    17. qDebug("Could not start gdb");
    18. exit(1);
    19. }
    20. char buf[1024];
    21. int len;
    22. bool doprint = false;
    23. do {
    24. pro.waitForReadyRead();
    25. do {
    26. len = pro.readLine(buf, sizeof(buf));
    27. if (len>0) {
    28. QString s = buf;
    29. if (s.indexOf("signal handler called") >-1) doprint = true;
    30. s.chop(1); // skip newline
    31. if (doprint) qDebug("%s", qPrintable(s));
    32. }
    33. } while (len>0);
    34. } while(pro.state() == QProcess::Running);
    35. exit(1);
    36. }
    37.  
    38.  
    39. void cause_segv(int x)
    40. {
    41. *(int *)0 = x;
    42. }
    43.  
    44.  
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication app(argc, argv);
    48.  
    49. signal(SIGSEGV, segv_handler);
    50.  
    51. cause_segv(26);
    52.  
    53. app.exec();
    54. return 0;
    55. }
    To copy to clipboard, switch view to plain text mode 


    With the above program, I get the following output:

    Caught SEGV, trying to get a backtrace from gdb...
    #6 <signal handler called>
    #7 0x0804983f in cause_segv (x=26) at prog.cc:42
    #8 0x08049894 in main (argc=Cannot access memory at address 0x0
    ) at prog.cc:52


    Line numbers, arguments, function names and file names, just like I wanted
    Last edited by drhex; 1st March 2008 at 21:46.

  4. #4
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading from a QProcess

    Btw, I'd like to edit the first message in the thread again to change the subject line since it turned out to be more about debuggning than reading from QProcess, but it seems that it is not possible to edit old messages when new ones have been posted?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reading from a QProcess

    It is impossible to edit a post after some time (4 hours?) passes. You have to ask a moderator or an administrator to change the title for you.

  6. #6
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading from a QProcess

    If you start your program through gdb with a command like gdb myprogram, you'll have gdb monitoring your program throughout its execution. Since gdb catches signals, you can just let your program run and let gdb catch it when it SEGFAULTs.

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reading from a QProcess

    Why not just start your program (and gdb) in a shell?

    Something like
    Qt Code:
    1. ProgramThatMightCrash &
    2. ITSPID=$!
    3. gdb ProgramThatMightCrash $ITSPID
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  3. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  4. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.