linux-test-project / lcov

LCOV
GNU General Public License v2.0
866 stars 234 forks source link

gcov error:std::bad_alloc #295

Open Jeling-W opened 1 month ago

Jeling-W commented 1 month ago

I have used different versions of gcov and lcov to perform coverage analysis on two .cpp files. Both gcda and gcno files are generated correctly, and the file paths are accurate with no damage to the files. When generating the coverage of one file, it is normal, but when attempting to generate coverage for another file, I always encounter an error:

terminate called after throwing an instance of 'std::bad_alloc'  
        what(): std::bad_alloc

I have checked my system's memory, and it is sufficient. The file is not large

henry2cox commented 1 month ago

You didn't say what compiler and version(s) you used, your platform, or even the source language of your example. If it fails with the newest GCC - maybe you can try LLVM (either gcov or profdata path). You can execute gcov with different flags to see what makes it unhappy. Similarly, you can modify your source code (e.g., by stubbing our some functions) - to possibly narrow down the issue.

Sounds a lot like out of memory. How do you know that your system's memory is sufficient? Could also be a bug in gcov - trying to allocate an unreasonably large buffer.

In any event: you have not provided enough information for anyone to be able to help you.

Jeling-W commented 1 month ago

When using gcov version 8.3.0 paired with lcov 1.13 or gcov 9.4.0 paired with lcov 2.0 to process main.cpp, an error is encountered:

terminate called after throwing an instance of 'std::bad_alloc'  
        what(): std::bad_alloc

When using gcov version 4.8.0 to process main.cpp, a different error is encountered: main.gcno no functions found

After using gcov 4.8.0 to execute: gcov main.cpp, there will be some prompts for main. cpp: no lines for, but the main. cpp.gcov file can still be generated normally,

using gcov8.3 or gcov9.4 to execute: gcov main.cpp, the error is encountered:

terminate called after throwing an instance of 'std::bad_alloc'  
        what(): std::bad_alloc
Aborted (core dumped)

The same compilation process and generation coverage process are fine in the func.cpp file

henry2cox commented 1 month ago

Current gcc version is 13. gcc/4.8 is about 10 years old and 8.3 is more than 5. I suggest to upgrade.

This isn't an lcov bug - but the same observation applies. Current 'release' version is 2.1. lcov/1.13 predates move to github - and is really, really old. Pretty sure that lcov/2.1 and 2.0 both support gcc/4.X (but not older than 4) - so you may be able to use that, if you really cannot upgrade. Sounds like the gcov-extracted data is not completely correct, though. You could also just exclude the offending file from processing (losing that compilation unit from the coverage report).

Jeling-W commented 1 month ago

Is it possible that there is an error in the content of my gcda and gcno?

henry2cox commented 1 month ago

Anything is possible. An error in the compile-time data (gcno) would be a compiler bug. An error in the runtime data (gcda) would either be a compiler or C/C++ runtime bug. Or this could be a bug in gcov. If you really want to diagnose it: then you will need to do some experiments - and probably need the source code for the toolchain components so you can see what they are doing. Or you could upgrade and hope it goes away. Or you could upgrade - discover that there is still an issue - and then debug more recent toolchain code.

Not sure your affiliation or interests - but if you happen to be a computer science or software engineering student somewhere, then this could be a really excellent learning experience: track down a fix a real bug in a nontrivial framework. I guarantee you that you would learn a lot. (I guarantee you that I would learn a lot.)

Jeling-W commented 1 month ago

I upgraded the version and the current environment is gcc12.2, lcov2.2-beta, Recompiled gcda and gcno, but there are still errors: Execution: lcov -c -d . -o aa.info --demangle-cpp --function-coverage --branch-coverage Error:

geninfo: ERROR: (gcov) GCOV failed for aa.gcda!
(use "geninfo --ignore-errors gcov ...:" to bypass this error)
Message summary:
1 error message:
gcov:1

Execution:gcov aa.gcno Error:Segmentation fault (core dumped)

henry2cox commented 1 month ago

Looks like gcov has a bug (or the compiler has a bug and is producing something that crashes gcov). Crashes are always easier to debug than wrong results - so you case is somewhat lucky.

I seriously doubt that anyone is going to be able to identify or fix the issue without a testcase. If you want to debug it yourself: you might want to start by looking at the stack trace in your core dump. Then you might want to build a debug version of gcov and run there to see what went wrong.

It is also probably worth trying your example with llvm to see if it fails with an entirely different toolchain.

The other thing you can do is suggested by the lcov error message above: ignore the gcov error and generate data for the rest of your project. This particular compilation unit won't have any data, but you might not care.