https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108658

            Bug ID: 108658
           Summary: [GCOV] Function entry is not recorded in a function
                    containing an infinite loop depending on the
                    optimization level
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sebastian.hu...@embedded-brains.de
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

Consider the following test code:

idle.c

void *idle(void *ignored)
{
  while (1) {
    /* Do nothing */
  }

  return 0;
}

main.c

#include <unistd.h>

void *idle(void *ignored);

int main(void)
{
  pthread_t th;
  pthread_create(&th, NULL, idle, NULL);
  sleep(1);
  return 0;
}

This sequence of commands shows that the idle() function entry is not recorded
for -O2 and -Og:

gcc-12 -O2 --coverage -c main.c
rm -f *.gc??
gcc-12 -O2 --coverage -c idle.c
gcc-12 -pthread --coverage main.o idle.o
./a.out
gcov-12 idle.c
File 'idle.c'
Lines executed:0.00% of 1
Creating 'idle.c.gcov'

Lines executed:0.00% of 1
cat idle.c.gcov
        -:    0:Source:idle.c
        -:    0:Graph:idle.gcno
        -:    0:Data:idle.gcda
        -:    0:Runs:1
    #####:    1:void *idle(void *ignored)
        -:    2:{
        -:    3:  while (1) {
        -:    4:    /* Do nothing */
        -:    5:  }
        -:    6:
        -:    7:  return 0;
        -:    8:}
rm -f *.gc??
gcc-12 -Og --coverage -c idle.c
gcc-12 -pthread --coverage main.o idle.o
./a.out
gcov-12 idle.c
File 'idle.c'
Lines executed:50.00% of 2
Creating 'idle.c.gcov'

Lines executed:50.00% of 2
cat idle.c.gcov
        -:    0:Source:idle.c
        -:    0:Graph:idle.gcno
        -:    0:Data:idle.gcda
        -:    0:Runs:1
    #####:    1:void *idle(void *ignored)
        -:    2:{
472195650:    3:  while (1) {
        -:    4:    /* Do nothing */
        -:    5:  }
        -:    6:
        -:    7:  return 0;
        -:    8:}
rm -f *.gc??
gcc-12 -O0 --coverage -c idle.c
gcc-12 -pthread --coverage main.o idle.o
./a.out
gcov-12 idle.c
File 'idle.c'
Lines executed:100.00% of 2
Creating 'idle.c.gcov'

Lines executed:100.00% of 2
cat idle.c.gcov
        -:    0:Source:idle.c
        -:    0:Graph:idle.gcno
        -:    0:Data:idle.gcda
        -:    0:Runs:1
472440920:    1:void *idle(void *ignored)
        -:    2:{
472440920:    3:  while (1) {
        -:    4:    /* Do nothing */
        -:    5:  }
        -:    6:
        -:    7:  return 0;
        -:    8:}

For -O0 the line count is also wrong from my point of view. Line 1 should have
a count of 1.

Reply via email to