Logging error during execution in C - 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.

Related

R session crashes when using .so file that uses OpenBLAS

I tried to write some simple matrix calculation code in c++ with OpenBLAS and then import my program into R. I did not use any Rcpp related things.
When compiled alone, the c++ program works perfectly. When I commented the few lines with OpenBLAS functions, used R CMD SHLIB to compile it and used dyn.load to import the .so file into my R program, R works fine.
However, if I uncommented the OpenBLAS-involved part, R crashes every time. When run from terminal, the following error message was reported:
R(90544,0x16bd7f000) malloc: Heap corruption detected, free list is damaged at 0x6000033d0620
*** Incorrect guard value: 0
R(90544,0x16bd7f000) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort R
Also, when I run only with a single line of OpenBLAS command, there was the following error:
*** caught segfault ***
address 0x60012e797320, cause 'invalid permissions'
Do I miss something? I do need some help since I struggled with this for a long time. Thank you guys.
I found a way round. I directly use the LAPACK used by R and followed the procedure stated here medium.com/#shiansu/calling-blas-in-r-the-hard-way-da90e0206d99. With some slight adaption, my code could finally work.

VisualStudio - Fatal error LNK1168: cannot open myfile.exe for writing

Sometime, while I try to compile my project, VisualStudio show me this error:
1>LINK : fatal error LNK1168: cannot open C:\Path\myfile.exe for writing
and It not allowes me to compile the project.
It seems that myfile.exe is open and running in background yet, although I closed it. I opened Win TaskManager but the file is not shown in the list of process running.
This error persists and the only solution I found is reboot the system, but it's a tedious way that give me crazy because this error recurs often.
Some ideas to resolve it? Someone had the same error before?
I had this error already many times. Though your myfile.exe is not shown in TaskManager, It is present in background yet. If you open the cmd as administator and type:
tasklist | FIND "myfile.exe"
You can see your process running.
So at this point type the command:
taskkill /IM myfile.exe /F
in order to force the process kill.
Now, if you try to compile your project again, you'll see the compiler is ok and the error is resolved.
Surely it's possible that this error will occur again, so I suggest you to create a .bat file that run the kill command, so you can resolve the problem quickly.
regarding: LNK1168
Microsoft has a web page dedicated to this error:
LNK1168
The first thing on the linked page is:
The linker can’t write to filename.
The rest of the page discusses the details of why the linker cannot write to the file.

core dumped message is not captured in STDERR

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)

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

Error: not.exe has stopped working - When trying to build LLVM

I am trying to build LLVM on windows, and everytime I do so I get to a certain point and then recieve an error that says "not.exe has stopped working" It pops up on the desktop about 15 times.
I am not sure what is going on, but when I check the error in Visual Studio I receive this:
Error 4 error : Couldn't execute program 'C:/Users/Cyborg/Documents/Developer'The process cannot access the file because it is being used by another process. C:\Users\Joe\Documents\Dev\llvm\test\CUSTOMBUILD check-llvm
I receive this error 5-7 times depending on how fast I click the error that pops up on the desktop. So clearly this issue is happening because of that.
The fault module is: MSVCR100.dll
Does anyone know why this is happening?
I don't know why you're getting the error since I don't build or use LLVM on Windows. Since no one else has chimed in, I'll try to give a hint or two.
"not.exe" (or "not" under Linux) is a little LLVM helper program used when running the lit based regressions tests. It is used to execute a program and return a good exit status if the program fails and a bad exit status if the program doesn't fail.
I'm guessing that "CUSTOMBUILD check-llvm" (what ever that is) is attempting to run the regression tests and your getting your mysterious error as a result whenevr "not" is used.
I hope that helps.