本文介绍了醒目的libc错误信息,从/ dev / tty的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想赶上当它检测到的错误情况的libc生成错误消息。例如,我的测试code:

I am trying to catch error messages that libc generates when it detects error conditions. For example, my test code:

#include <stdlib.h>

int main()
{
   char* p = (char*)malloc(10);
   free(p);
   free(p);
}

生成的输出

$ ./main
*** Error in `./main': double free or corruption (fasttop): 0x000000000124b010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7d1fd)[0x7f8c121291fd]
./main[0x400b86]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f8c120cdaf5]
./main[0x400a79]
... <snip>

然而,它没有被写入标准错误或标准输出,但为/ dev / tty的(我用strace的发现)

However, it is not being written to stderr or stdout, but /dev/tty (which I discovered using strace)

open("/dev/tty", O_RDWR|O_NOCTTY|O_NONBLOCK) = 3
writev(3, [{"*** Error in `", 14}, {"./main", 6}, {"': ", 3}, {"double free or corruption (fastt"..., 35}, {": 0x", 4}, {"00000000011bf010", 16}, {" ***\n", 5}], 7*** Error in `./main': double free or corruption (fasttop): 0x00000000011bf010 ***
) = 83
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0f2f7ac000
write(3, "======= Backtrace: =========\n", 29======= Backtrace: =========
) = 29
writev(3, [{"/lib64/libc.so.6", 16}, {"(", 1}, {"+0x", 3}, {"7d1fd", 5}, {")", 1}, {"[0x", 3}, {"7f0f2ea2a1fd", 12}, {"]\n", 2}], 8/lib64/libc.so.6(+0x7d1fd)[0x7f0f2ea2a1fd]
) = 43
writev(3, [{"./main", 6}, {"[0x", 3}, {"400b86", 6}, {"]\n", 2}], 4./main[0x400b86]
) = 17
writev(3, [{"/lib64/libc.so.6", 16}, {"(", 1}, {"__libc_start_main", 17}, {"+0x", 3}, {"f5", 2}, {")", 1}, {"[0x", 3}, {"7f0f2e9ceaf5", 12}, {"]\n", 2}], 9/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f0f2e9ceaf5]
) = 57
writev(3, [{"./main", 6}, {"[0x", 3}, {"400a79", 6}, {"]\n", 2}], 4./main[0x400a79]
) = 17

我如何重定向到这个文件?标准输出和错误重定向不起作用。我需要赶上这一个systemd服务,而现在,输出消失在乙醚。

How do I redirect this to a file? The standard stdout and stderr redirects don't work. I need to catch this for a systemd service, and right now, the output is disappearing into the ether.

推荐答案

我发现了一个未公开的(glibc的文档不!)答案通过检查<一个href=\"https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/libc_fatal.c;h=2d99c46cf8e2ccfc2c09847d864b45b0d95efb91;hb=HEAD\"相对=nofollow> libc的源$ C ​​$ C,libc_fatal.c 。

I found an UNDOCUMENTED (not in the glibc documentation!) answer by inspecting the libc source code, libc_fatal.c.

设置以下环境变量什么

LIBC_FATAL_STDERR_=1

造成的libc写标准错误而不是的/ dev / tty的

这篇关于醒目的libc错误信息,从/ dev / tty的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:08