1

I've found that some programs print Memory Maps and other diagnostic information to the terminal when they crash, even when I redirect file descriptors 1 and 2 (standard out and error, respectively) to files. How can I redirect the diagnostics to a file?

For example, this c++ program:

#include <iostream>
#include <assert.h>
#include <string>

int main(){
    using namespace std;
    cout<<"foo"<<endl;
    cerr<<"bar"<<endl;
    string s;
    s = "asdf";
    delete &s;
}

After compilation, it can be run, redirecting "all" output (g++ foo.cpp && ./a.out > /dev/null 2>&1) but still print the following to the terminal:

======*** Error in `./a.out': double free or corruption (out): 0x00007ffe389313d0 ***
======= Backtrace: =========
/usr/lib/libc.so.6(+0x722ab)[0x7fc7c053f2ab]
/usr/lib/libc.so.6(+0x7890e)[0x7fc7c054590e]
/usr/lib/libc.so.6(+0x7911e)[0x7fc7c054611e]
./a.out[0x400b41]
/usr/lib/libc.so.6(__libc_start_main+0xf1)[0x7fc7c04ed511]
./a.out[0x4009fa]
= Memory map: ========
00400000-00401000 r-xp 00000000 00:27 531373                             /tmp/a.out
00601000-00602000 r--p 00001000 00:27 531373                             /tmp/a.out
00602000-00603000 rw-p 00002000 00:27 531373                             /tmp/a.out
01f1d000-01f4f000 rw-p 00000000 00:00 0                                  [heap]
7fc7bc000000-7fc7bc021000 rw-p 00000000 00:00 0 
7fc7bc021000-7fc7c0000000 ---p 00000000 00:00 0 
7fc7c04cd000-7fc7c0668000 r-xp 00000000 08:09 142916                     /usr/lib/libc-2.25.so
7fc7c0668000-7fc7c0867000 ---p 0019b000 08:09 142916                     /usr/lib/libc-2.25.so
7fc7c0867000-7fc7c086b000 r--p 0019a000 08:09 142916                     /usr/lib/libc-2.25.so
7fc7c086b000-7fc7c086d000 rw-p 0019e000 08:09 142916                     /usr/lib/libc-2.25.so
7fc7c086d000-7fc7c0871000 rw-p 00000000 00:00 0 
7fc7c0871000-7fc7c0887000 r-xp 00000000 08:09 144514                     /usr/lib/libgcc_s.so.1
7fc7c0887000-7fc7c0a86000 ---p 00016000 08:09 144514                     /usr/lib/libgcc_s.so.1
7fc7c0a86000-7fc7c0a87000 r--p 00015000 08:09 144514                     /usr/lib/libgcc_s.so.1
7fc7c0a87000-7fc7c0a88000 rw-p 00016000 08:09 144514                     /usr/lib/libgcc_s.so.1
7fc7c0a88000-7fc7c0b9a000 r-xp 00000000 08:09 131998                     /usr/lib/libm-2.25.so
7fc7c0b9a000-7fc7c0d99000 ---p 00112000 08:09 131998                     /usr/lib/libm-2.25.so
7fc7c0d99000-7fc7c0d9a000 r--p 00111000 08:09 131998                     /usr/lib/libm-2.25.so
7fc7c0d9a000-7fc7c0d9b000 rw-p 00112000 08:09 131998                     /usr/lib/libm-2.25.so
7fc7c0d9b000-7fc7c0f13000 r-xp 00000000 08:09 141919                     /usr/lib/libstdc++.so.6.0.22
7fc7c0f13000-7fc7c1113000 ---p 00178000 08:09 141919                     /usr/lib/libstdc++.so.6.0.22
7fc7c1113000-7fc7c111d000 r--p 00178000 08:09 141919                     /usr/lib/libstdc++.so.6.0.22
7fc7c111d000-7fc7c111f000 rw-p 00182000 08:09 141919                     /usr/lib/libstdc++.so.6.0.22
7fc7c111f000-7fc7c1123000 rw-p 00000000 00:00 0 
7fc7c1123000-7fc7c1146000 r-xp 00000000 08:09 142957                     /usr/lib/ld-2.25.so
7fc7c1318000-7fc7c131e000 rw-p 00000000 00:00 0 
7fc7c1344000-7fc7c1345000 rw-p 00000000 00:00 0 
7fc7c1345000-7fc7c1346000 r--p 00022000 08:09 142957                     /usr/lib/ld-2.25.so
7fc7c1346000-7fc7c1347000 rw-p 00023000 08:09 142957                     /usr/lib/ld-2.25.so
7fc7c1347000-7fc7c1348000 rw-p 00000000 00:00 0 
7ffe38911000-7ffe38932000 rw-p 00000000 00:00 0                          [stack]
7ffe38945000-7ffe38947000 r--p 00000000 00:00 0                          [vvar]
7ffe38947000-7ffe38949000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)
Einhaender
  • 99
  • 7

1 Answers1

1

To redirrect output from /dev/tty to a file you can do:

script -q -c 'g++ foo.cpp && ./a.out' /dev/null > /tmp/bla.txt

Taken from here - How to redirect a program that writes to tty?

Community
  • 1
  • 1
Robert Seaman
  • 2,432
  • 15
  • 18
  • The `g++ foo.cpp` part of the command does not generate output, and is not relevant to my main question. ie. even the command you suggest, `./a.out >/dev/null 2>&1` generates output to the terminal. I'm guessing that g++ does some equivalent of `echo error! crashed! >/dev/tty` to bypass all redirection; I want to know if it is possible to get that output logged to a file. – Einhaender Apr 25 '17 at 21:06
  • Good point. I'm not sure you can redirect the `/dev/tty` output - but I'm looking into it. – Robert Seaman Apr 25 '17 at 21:13
  • Hi @Einhaender - hopefully this edit answers your question. This works for me on a quick test. – Robert Seaman Apr 25 '17 at 21:19
  • 1
    Thanks! And for people from the future, if you want error output and /dev/tty output to one file, and standard output to another file, this command should help: `script -qc "/bin/sh -c \"/tmp/a.out > /tmp/out.txt\"" /dev/null > /tmp/err.txt` – Einhaender Apr 25 '17 at 22:04