重定向崩溃输出

时间:2017-04-25 20:43:31

标签: bash io sh

我发现有些程序在崩溃时会将内存映射和其他诊断信息打印到终端,即使我将文件描述符1和2(分别标准输出和错误)重定向到文件。如何将诊断重定向到文件?

例如,这个c ++程序:

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

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

编译后,可以运行,重定向“全部”输出(g++ foo.cpp && ./a.out > /dev/null 2>&1),但仍然将以下内容打印到终端:

======*** 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)

1 个答案:

答案 0 :(得分:1)

要将/dev/tty的输出重新输出到您可以执行的文件:

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

从这里采取 - How to redirect a program that writes to tty?