GCC not emitting all line numbers in debug info in -Og

2

I was trying to teach my colleagues to use GDB and created an example to show how to step through code with step and next. Compiling with -O0 went fine and the code is stepped through line by line, but when compiling with -Og (which I was going to recommend to my colleagues...) it only hit some of the lines (line 11, 13, 15, 20 and 25 below).

Looking through the debug-info in the elf-file it looks like only some of the line numbers are included. Looking through the disassembly, the code is nearly identical and it is not obvious that lines are optimized away when building with -Og.

Can anyone explain to me why the optimization optimized for debugging is failing at generating the line numbers?

Is there a reason why line info cannot be generated that I don't see? Or am I missing something when compiling? Or is this a known bug with GCC?

Code:

#include <stdio.h>

struct ABC{
  int a;
  unsigned int b;
  char c;
};

#define MY_CONST 3

int main(){ // line #11
  int var = 0x12345678;
  char str[] = "Hello World!"; //line #13
  char* ptr1 = &str[6];
  struct ABC abc = {-1, 1, 'a'}; // line #15
  struct ABC abc_array[] = {{-1, 1, 'A'}, {-2, 2, 'B'}, {-3, 3, 'C'}};

  void * ptr2 = &abc_array;

  printf("var = 0x%x\n", var); //line #20
  printf("str = %s\n", str);
  printf("ptr1 = %s\n", ptr1);
  printf("&abc = %p\n", &abc);
  printf("MY_CONST = %d\n", MY_CONST);
} //line #25

Compilation:

gcc -g -Og main.c -o exercise2.elf

Dwarfdump:

.debug_line: line number info for a single cu
Source lines (from CU-DIE at .debug_info offset 0x0000000b):

<pc>        [row,col] NS BB ET PE EB IS= DI= uri: "filepath"
NS new statement, BB new basic block, ET end of text sequence
PE prologue end, EB epilogue begin
IA=val ISA number, DI=val discriminator value
0x004005b6  [  11, 0] NS uri: "/home/developer/Courses/Debugging/Exercises/Exercise 2/main.c"
0x004005ba  [  11, 0] NS
0x004005ca  [  13, 0] NS
0x004005e6  [  15, 0] NS
0x004005fa  [ 104, 0] NS uri: "/usr/include/x86_64-linux-gnu/bits/stdio2.h"
0x00400675  [  25, 0] NS uri: "/home/developer/Courses/Debugging/Exercises/Exercise 2/main.c"
0x00400694  [  25, 0] NS ET 

Note that there is no line info for line 20 which GDB stopped at nonetheless!

c
linux
gcc
gdb
dwarf
asked on Stack Overflow Aug 8, 2016 by tombjo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0