I am using GDBs machine interface (mi) to write some simple tracers/loggers.

Now I ran into the problem of telling apart GDB output from text the target-program itself wrote to stdout.

If I look into the documentation here: https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_22.html

it says

target-stream-output is the output produced by the target program. All the target output is prefixed by `@'.

This is not what I observe, however. Consider this example session of running ls like so:

gdb -i mi ls

which gives this output:

~"[Thread debugging using libthread_db enabled]\n"
file1.jpg  file2.jpg  file3.jpg  dir1  dir2   <---(!!!)
~"[Inferior 1 (process 22233) exited normally]\n"

As you can see, the output of the ls command was just randomly dumped into the console. Great.

How can I parse this?


gdb version is 8.1

1 Answer 1


According to this documentation page you can redirect your program stdout where-ever you want by using run>out_file_name . This will not solve the parsing problem, but should avoid it. Using other tty then gdb's is also described there.

In addition there is a simpler way to do it, with output redirection (see the first line of gdb output below). I checked it with simple program that prints to different strings to stderr and stdout :

#include <stdio.h>

int main(int argc, char ** argv)
    printf ("\ntest\n");
    fprintf (stderr, "stderr\n");
    return 0;

and here is my small debugging session results:

(gdb) set args 1>/home/ubuntu/out.txt 2> /home/ubuntu/err.txt
(gdb) b main
Breakpoint 1 at 0x4005b5: file ./gt.c, line 5.
(gdb) run
Starting program: /home/ubuntu/unittest/gt 1>/home/ubuntu/out.txt 2> /home/ubuntu/err.txt

Breakpoint 1, main (argc=1, argv=0x7fffffffe1f8) at ./gt.c:5
5       printf ("\ntest\n");
(gdb) c
[Inferior 1 (process 8659) exited normally]
(gdb) shell cat /home/ubuntu/out.txt

(gdb) shell cat /home/ubuntu/err.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.