gcc does not produce debugging symbols for local variable-length arrays (or gdb doesn't find them)

1

Apparently gdb cannot find the symbols associated with local variable-length arrays.
Is it a gcc problem or a gdb problem? (Or maybe it's just my problem...).
Take the following program "main.c"

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv ) {
    int n;
    n = random() & 0xf;
    int array[n];
    int *array_pt = array;
    int i;
    for (i=0; i<n; i++) {
        array[i] = random()&0xffff;
    }
    return(0);
}

Compile as:
gcc -c -O0 -g -ggdb -Wall main.c -o main.o
gcc main.o -o main

If I now run it under gdb, I find that symbol "array_pt" is known, but "array" isn't.

(gdb) p array
No symbol "array" in current context.
(gdb) p array_pt 
$1 = (int *) 0x7fff5fbff6f0

I circumvent the problem by using a pointer, as in the example, but... is this normal?


After @Michael's suggestion I'm including relevant output of dwarfdump

0x000000d5:         TAG_variable [5]  
         AT_name( "array.1" )
         AT_decl_file( "/.../main.c" )
         AT_decl_line( 7 )
         AT_type( {0x00000152} ( int[]* ) )
         AT_location( fbreg -56 )

0x00000104:             TAG_variable [5]  
             AT_name( "array_pt" )
             AT_decl_file( "/.../main.c" )
             AT_decl_line( 8 )
             AT_type( {0x0000015a} ( int* ) )
             AT_location( fbreg -72 )
gcc
asked on Stack Overflow Aug 6, 2014 by brian0 • edited Aug 6, 2014 by brian0

2 Answers

1

Ooooooopppsssssss....
I just found out that on my machine (os x 10.7.5) gcc is not gcc!
It's just a link /usr/bin/gcc@ -> llvm-gcc-4.2

I did the same experiment on a machine with a genuine gcc and everything works as is should! The symbol for "array" is... lo and behold... "array".

So it's a llvm thing. Sorry guys. As far as gcc is concerned, case closed. If I compile with clang it's even worse. No symbol at all is produced for "array".

I shuold repost the question under the llvm-ggc tag with a different title.

answered on Stack Overflow Aug 6, 2014 by brian0
0

gdb 7.8 should now support this:

Tue, 29 Jul 2014

GDB 7.8 brings new targets, features and improvements, including:

...

  • ISO C99 variable length automatic arrays support.

http://lwn.net/Articles/607108/

and indeed on ubuntu 14.10 utopic it works for me:

# gcc -g3 test.c -O0
# gdb ./a.out
GNU gdb (Ubuntu 7.8-0ubuntu1) 7.8
(gdb) break main
Breakpoint 1 at 0x4005be: file test.c, line 4.
(gdb) r
Starting program: /a.out 

Breakpoint 1, main (argc=1, argv=0x7fffffffe2d8) at test.c:4
4   int main( int argc, char **argv ) { 
(gdb) n
6       n = random() & 0xf;
(gdb) 
7       int array[n];
(gdb) 
8       int *array_pt = array;
(gdb) p array
$1 = {-136403536, 32767, -134225464, 1804289383, 3, 0, 4195800}
(gdb) 
answered on Stack Overflow Aug 6, 2014 by jtaylor • edited Aug 6, 2014 by jtaylor

User contributions licensed under CC BY-SA 3.0