GCC doesn't produce line number information even with -g option

15,329

Solution 1

Your gdb is too old -- you need a more recent gdb (I use 7.6) to understand the debugging info generated by gcc 4.8.1

Solution 2

Usually GCC uses dwarf as its main debugging file format, you need to enable dwarf support when building gcc with the flag --with-dwarf2.

While building your compiled object you can use -ggdb instead of -g which is a more specific solution but just for gdb.

Share:
15,329
Calmarius
Author by

Calmarius

Languages I use often: C, PHP, JavaScript, HTML, CSS, MySQL. Languages I use less often: x86 assembly, C++, C# Technologies, formats I know well: x86 architecture, PE file format, ELF file format, OpenGL, OpenAL, SDL. (there may be items I forgot to put on the list.)

Updated on June 08, 2022

Comments

  • Calmarius
    Calmarius almost 2 years

    I have built and installed GCC 4.8.1 from source:

    $ gcc -v
    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper
    Target: x86_64-unknown-linux-gnu
    Configured with: ./configure --disable-multilib
    Thread model: posix
    gcc version 4.8.1 (GCC) 
    

    And I've written a simple useless program:

    $ cat hw.c
    #include <stdio.h>
    
    void foo()
    {
        int a;
        scanf("%d", &a); /* So I can press ctrl+c here. */
        printf("Hello world!\n");
    }
    
    int main()
    {
        foo();   
    }
    

    Now I compile this:

    $ gcc -g -O0 hw.c -o hw
    

    Then started debugging it with GDB:

    $ gdb hw
    GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
    Copyright (C) 2012 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/calmarius/workdir/crucible/hw/hw...done.
    (gdb) 
    

    Run it and Ctrl+C it immediately:

    (gdb) run
    Starting program: /home/dcsirmaz/workdir/crucible/hw/hw 
    ^C
    Program received signal SIGINT, Interrupt.
    0x00007ffff7b018b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
    82  ../sysdeps/unix/syscall-template.S: Nincs ilyen fájl vagy könyvtár.
    

    I got function names in the backtrace but no line numbers in my code:

    (gdb) bt
    #0  0x00007ffff7b018b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
    #1  0x00007ffff7a95ff8 in _IO_new_file_underflow (fp=0x7ffff7dd4340) at fileops.c:619
    #2  0x00007ffff7a9703e in _IO_default_uflow (fp=0x7ffff7dd4340) at genops.c:440
    #3  0x00007ffff7a74fb6 in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=0x7fffffffe018, errp=0x0) at vfscanf.c:620
    #4  0x00007ffff7a790bd in __isoc99_scanf (format=<optimized out>) at isoc99_scanf.c:37
    #5  0x000000000040054e in foo ()
    #6  0x0000000000400568 in main ()
    

    What's gone wrong? Maybe is it something with the configuration?

  • Chris Dodd
    Chris Dodd over 10 years
    Actually, dwarf is the problem -- he's using Ubuntu's old version of gdb that doesn't understand debug info generated by the latest version of gcc.
  • Employed Russian
    Employed Russian over 10 years
    More precisely, GCC-4.8 defaults to using dwarf4 (gcc.gnu.org/gcc-4.8/changes.html), but your GDB is too old to understand that. Build with -gdwarf-2, and you'll get your line numbers back. Or update GDB to a newer from source build.
  • Calmarius
    Calmarius over 10 years
    So gdb 7.4 is too old.