2

I'm running C++ on Cortex M4 and I want to start doing automated unit tests and coverage.

gcov writes its output files - .gcno, .gcda - on the target which is a no-go because I don't have a filesystem.

QUESTION

Is it possible to "intercept and redirect" these writes to the PC using gdb?

I want to automate these tests via a Python script:

  1. Get unit test from data base
  2. Compile
  3. Transfer to target board
  4. Run
  5. Redirect results to PC
Bob
  • 4,576
  • 7
  • 39
  • 107
  • So you have implement filesystem abstraction yourself. Implement `write`, `open`, `close` etc. I/O posix system calls, or whatever "gcov" utility system calls uses. What compiler, compiler options are you using? How are you compiling to the destination ar\chitecture. Without any further info, this is too broad - "is it possible to intercept _these_ writes" is vague - which writes? What have you tried? I mean, a 5 min google search results in ex. [this](https://mcuoneclipse.com/2014/12/26/code-coverage-for-embedded-target-with-eclipse-gcc-and-gcov/) – KamilCuk Jun 24 '19 at 20:30
  • @KamilCuk I already knew how to do it that way. I hoped `gdb` could make it easier. – Bob Jun 24 '19 at 20:57
  • So how did you do it? You can implement the embedded I/O using semihosting and gdb should trap the calls and create the files. Looks like [this](https://mcuoneclipse.com/2017/06/19/gnu-code-coverage-on-embedded-target-with-eclipse-neon-and-arm-gcc-5/). – KamilCuk Jun 24 '19 at 21:16
  • @KamilCuk I haven't done it yet. I know about semihosting but I was hoping for a uniform solution across my Linux projects and bare-metal. I guess there isn't one. I'll do semihosting. – Bob Jun 24 '19 at 21:18
  • 1
    visit [10.6 Profiling and Test Coverage in Freestanding Environments](https://gcc.gnu.org/onlinedocs/gcc/Freestanding-Environments.html#Freestanding-Environments) – HZ1 Nov 01 '22 at 06:37
  • Looks like some people are doing just that (using gcov on baremetal) https://dzone.com/articles/gnu-code-coverage-on-embedded-targets http://allsoftwaresucks.blogspot.com/2015/05/gcov-is-amazing-yet-undocumented.html – nicolasb565 Jun 25 '19 at 20:24
  • @HZ1 this is the answer. Please add it as an answer. – Bob Nov 01 '22 at 17:36

1 Answers1

0

So, I didn't know gcov, thank you.

A quick look at man gcov showed that I had it on my system and that the following option exists:

-t, --stdout Output to stdout instead of a file

I haven't done python in a while, but you should be able to create a memory-only file, and, supposing the opened file handle is 8, use:

gcov -t [whatever you are doing] >&8

The -t argument tells gcov to print to the standard output instead of writing to a file, and >&8 overwrites stdout (as seen by the executed program) to be file handle 8 instead of 1 (on most unix/posix/whateveryoucallit, anyway).

That is of course in bash, which you can either invoke from python, or transcode to however you achieve this in python.

I haven't got to try it, so if it helps (or if it doesn't) make sure to leave a mark to help future readers.

Alceste_
  • 592
  • 4
  • 13
  • This doesn’t work on bare metal because there is no stdout – Bob Jun 25 '19 at 03:02
  • No default one, but shouldn't it still allow for a user-defined file to be passed to gcov as its output file? (How does gcov react to `-t`?) – Alceste_ Jun 25 '19 at 03:03
  • You run `gcov` on the output data files which have to be produced by the program running on bare metal – Bob Jun 26 '19 at 14:36