core dumped message is not captured in STDERR - c++

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?

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)

Related

How can I write C++ crash errors in a file? [duplicate]

I run the command./a.out < in &> output.txt
I want the errors also to be placed in output.txt.
The exit status of the command was 139 and on terminal its output was:
Segmentation fault (core dumped)
and the file output.txt was empty.
The message Segmentation fault (core dumped) is not coming from your program.
It's produced by shell as result of a signal received by it. It's not a part of stderr or stdout of your program.
So shell's message can be captured as:
{ ./a.out; } 2> out_err
If you want both the error messages from a.out and the string
Segmentation fault (core dumped)
to be appended to output.txt, then you have to redirect the shell's stderr as well. E.g.,
exec 2>> output.txt && ./a.out < in 2>&1 >> output.txt &
This is because the segfault message is coming from the shell itself.

Pass Terminal ouput in GUI

I have created a GUI that runs a command in Linux Terminal.
EXAMPLE=> -n 20 -id 15 -domain square(10,20) -dim 2 -o execution -format geo,tess
This command is for the execution of a code for package. And when it executes a series of outputs undergo in the Linux terminal. The output is either successful and the output is generated or an error occurs due to a dump error or bad argument.
Examples of Terminal Outputs:
Error : matheval failed to process the expression. The expression
syntax is probably incorrect. This may also be caused by an unproper
matheval installation (expression = 1 -ori fibre(,,)). Aborted (core
dumped)
Another Error
Error : Bad arguments!
Aborted (core dumped)
What i am trying to do is return this error back into the GUI as either a MessageBox and/or update the bottom Status bar of the GUI with the error.
I am not familiar with the wx and subprocess modules, but my research so far has failed to find a solution.
I am running Python 2.7 and using Tkinter.
This seems to get the work done.
# Create a string and pass variables
neper_exec = "neper -T -n %s -id %s -format %s -o %s -dim %s -domain %s " % (n, id, output_form, o, dim, domain)
# Execute the subporcess using the variable
p = subprocess.Popen(neper_exec, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # when shell is dropped Linux gives and error
Thanks to #BrianMcFarland for guidance on the subprocess command.

Redirect execution errors to file c++

How can I redirect execution errors of a c++ executable in bash? I've found that 2> helps while trying identify compilation errors:
g++ example.cpp 2> compErr.txt
But running the executable with that command still sends the errors to stdout:
$ ./a.out 2> e.txt
Floating point exception (core dumped)
Actually, the error "Floating point exception (core dumped)" does not come from the executable but from the shell! The messages from bash won't be suppressed by output redirection but there is a flag to enable/disable these messages.
You can install signal handlers for some of the errors which would cause the program to exit and write something to a suitable destination there. Some signals can't be intercepted and some other are hard to handle. That's the approach you can do from inside your code.
If you want to go further you could fork() your program first thing and have the actual work done in the child process. The parent process would essentially just waitpid() for the child process and use the information in the result structure received to report errors to a file.
I found something that worked in my terminal, here: http://bytes.com/topic/c/answers/822874-runtime-error-stderr-gcc-ubuntu-bash
In summary, a participant explained:
In this particular case, the reason that the string "Floating point exception" is not >redirected is that it is not produced by the process that runs ./{file} or anything that it invokes. Instead,it is being produced by the command-interpreter itself.
You can see this by telling the command interpreter to run another command interpreter, redirecting this sub-interpreter's error output. However, a bit of a >trick is required:
$ bash -c './{file}; true' >out 2>err
$ cat out
$ cat err
bash: line 1: 28106 Floating point exception./test_fail

Logging error during execution in C

So I know that logging compilation errors can be done as follows in the terminal
gcc -o objectname filename.c 2>'compilation_error_log.txt'
I get some memory error while executing the code and want to log that as well. I tried the same approach
./objectname 2>'Execution_error_log.txt'
but it's not working. Can someone tell me where the memory errors get stored so I can log them?
My error and output looks somewhat like this
./objectname arg
Expected Output.
*** Error in `./objectname': double free or corruption (!prev): 0x089d1008 ***
Aborted (core dumped)
I wanna log the expected output and the error messages
By default glibc error messaging is being written to /dev/tty, which isn't redirected to anywhere.
You can request messages in stderr by setting environment variable LIBC_FATAL_STDERR_ to 1. After that, you can 2> log.file.
Default behaviour is safe workaround when your application already closed stderr (or file descriptor 2), and fatal error happened after that.
Executing ./objectname arg 1 > compilation_error_log.txt 2>&1 in ash should work. Basically you have to provide the redirection to a file and then redirect the error stream to the stdin file descriptor.

redirection of ./a.out is not capturing segmentation fault

I run the command./a.out < in &> output.txt
I want the errors also to be placed in output.txt.
The exit status of the command was 139 and on terminal its output was:
Segmentation fault (core dumped)
and the file output.txt was empty.
The message Segmentation fault (core dumped) is not coming from your program.
It's produced by shell as result of a signal received by it. It's not a part of stderr or stdout of your program.
So shell's message can be captured as:
{ ./a.out; } 2> out_err
If you want both the error messages from a.out and the string
Segmentation fault (core dumped)
to be appended to output.txt, then you have to redirect the shell's stderr as well. E.g.,
exec 2>> output.txt && ./a.out < in 2>&1 >> output.txt &
This is because the segfault message is coming from the shell itself.