Search code examples
chttp-redirectstdoutpopenstderr

How to redirect info like "Aborted (core dumped)" when execute "./a.out"


There is a .out file, which will cause Aborted (core dumped) infomation when running.

I want to put this information Aborted (core dumped) into a file.

I want to put it into my program actually, but I have redirect problem. Once this problem solved, I can put it in my program by redirect and popen().

I try ./a.out 2>file, but it doesn't work.


Solution

  • Redirecting standard error for a.out won't do anything since a.out doesn't actually write the Aborted (core dumped) message. This message is written by the shell itself in response to an abnormal status returned when it calls waitpid or similar on your process.

    In order to obtain the same information programmatically, you'll need to check the exit code which will be present in $?. Signal 11 (SIGSEGV) will result in exit code 128+11=139; SIGABRT (signal 6) should result in exit code 134.