Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core dumped message is not captured in STDERR

Tags:

c++

linux

bash

I used to call my program with this line in bash shell to capture all stdout and stderr messages in the log file

./a.out input.txt 2>&1 | tee log

The log file shows no error but by examining the log, it's obvious that there's a problem and program terminates abruptly in the middle of execution.

I have also tried these but the result is the same:

./a.out input.txt > log 2>&1

./a.out input.txt |& tee log

When I run it without any redirection like this:

./a.out input.txt

I see the error message in my terminal window at the end:

*** Error in `./a.out': free(): invalid pointer: 0x000000000169b268 ***
Aborted (core dumped)

So, why I cannot capture the "core dumped" message in my log? What should I do to do so?

like image 253
Convergent Avatar asked Oct 26 '15 07:10

Convergent


People also ask

Where are core dumps stored?

By default, all core dumps are stored in /var/lib/systemd/coredump (due to Storage=external ) and they are compressed with zstd (due to Compress=yes ). Additionally, various size limits for the storage can be configured. Note: The default value for kernel.

What causes aborted core dumped?

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump. It is an error indicating memory corruption.

How do I read a core dump file?

In a terminal, run sleep 30 to start a process sleeping for 30 seconds. While it is running, press Ctrl + \ to force a core dump. You'll now see a core file in the directory you are in.

Where does ubuntu store core dumps?

By default, systemd-coredump will log the core dump to the journal, including a backtrace if possible, and store the core dump (an image of the memory contents of the process) itself in an external file in /var/lib/systemd/coredump.


1 Answers

There are 2 error messages here:

*** Error in `./a.out': free(): invalid pointer: 0x000000000169b268 ***

This comes from glibc, and is printed on the current tty, if it exists.

If you want it printed to stderr (wherever stderr is redirected), you must set the LIBC_FATAL_STDERR_ prior to starting the program. e.g. in bash do:

export LIBC_FATAL_STDERR_=1

The other message

Aborted (core dumped)

This comes from the shell that started your program, by the shell examining the status of wait(). If the program isn't run by a shell, or e.g. is run by a shell that have terminated, you'll not be able to capture that message. Even if the shell havn't terminated, the stderr of that shell isn't redirected to your log file.

You might get around that by doing:

 { ./a.out input.txt ; } >log 2>&1

See also redirection of ./a.out is not capturing segmentation fault)

like image 151
nos Avatar answered Sep 30 '22 12:09

nos