0

I need in a debug session, see and manipulate some global variables of a C program. I already tried some options of gcc, but no success until now to see (print) at least my global variables.

GDB always say: No symbol "???" in current context.

What flags shall I use in compilation with gcc to do that ?

MY SAMPLE:

#include <stdio.h>

/* Some vars to see in debug */
static long gvar = 0L;
int othervar=3;
char* onepointer=NULL;

int main() {
    long lvar=gvar;

    gvar = 1L;
    lvar = gvar;
    printf("gvar = %ld\n", gvar);

    gvar = 5L;
    lvar = gvar;
    printf("gvar = %ld\n", gvar);
}

GDB TUI SESSION:

enter image description here

THE ENVIRONMENT:

  • OS: AIX 7
  • GCC: 4.8.3
  • GDB: 7.8.1

TRIED GCC FLAGS (in some combinations):

-g, -g3, -ggdb, -ggdb3 -O0, and others

Luciano
  • 2,695
  • 6
  • 38
  • 53
  • 1
    In addition to the `-g` flag (to build with debug info), try turning optimisations off... the compiler might be removing `gvar` and just using a value of 0 or 5 in its place. – Dmitri Jul 11 '16 at 20:51
  • 1
    Be sure you're using `-O0` [and _not_ `-o0`]. In addition to `-g`, you could try `-gdwarf-2`. Also, be sure that the build doesn't do `strip` and that when linking there is _no_ `-s` option – Craig Estey Jul 11 '16 at 20:58
  • 1
    What versions of gcc and gdb are you using? They are not native on AIX, and are known to have problems on AIX. +1: Try to qualify the variable-name: `print 'foo.c'::gvar` – Zsigmond Lőrinczy Jul 12 '16 at 03:48
  • @CraigEstey: Yes it's -O0 (-o0 was a type mistake in my question). But,`-gdwarf-2` isn't supported in this environment: `error: target system does not support the "dwarf-2" debug format` – Luciano Jul 12 '16 at 13:58
  • @ZsigmondLőrinczy: I added more info about the environment to the question, that are: OS: AIX 7; GCC: 4.8.3; GDB: 7.8.1. Qualifying the variable changed the error: `p 'x.c'::gvar: No symbol "gvar" in specified context`; `p gvar: No symbol "gvar" in current context` – Luciano Jul 12 '16 at 14:03

2 Answers2

1

Your compiler optimized out the gvar variable. It's allowed to do so, as it can prove this won't affect program flow, i.e. no external uses of symbol and its address is not taken.

If you need it for debugging, you can remove the static temporarily or mark it volatile or one of the other methods proposed here.


Generally, static variables (unless they are optmized out) are limited in scope to the file they're defined in. So, if you aren't stepping through that particular file, gdb's p gvar won't find it because it's out of scope.

To access static variables like gvar, regardless of context use 'x.c'::gvar to refer to it instead.

Community
  • 1
  • 1
a3f
  • 8,517
  • 1
  • 41
  • 46
  • Different messages: `p 'x.c'::gvar`: No symbol "gvar" in specified context; `p gvar`: No symbol "gvar" in current context – Luciano Jul 12 '16 at 14:24
  • @Luciano Seems your optimization settings are to blame. Check the revised answer. – a3f Jul 12 '16 at 15:18
  • Removing static from declaration worked ! But, but are there flags that could be used to avoid it ? (-O0 didn't worked) – Luciano Jul 12 '16 at 23:32
  • 1
    @Luciano Well, `-O0`. If it doesn't do like it should, maybe raise a bug report with your toolchain vendor. Till then, some hackery in a header, like `#if !__OPTIMIZE__\n#define static volatile static\n#endif` (Replace `\n`), might ease your debugging a bit. – a3f Jul 13 '16 at 09:06
0

All seems to work for me (on a linux box). -O0 and -g. Do a br on main and look at locals/globals.

evaitl@evbb ~/se $ cat foo.c
#include <stdio.h>

/* Some vars to see in debug */
static long gvar = 0L;
int othervar=3;
char* onepointer=NULL;

int main() {
    long lvar=gvar;

    gvar = 1L;
    lvar = gvar;
    printf("gvar = %ld\n", gvar);

    gvar = 5L;
    lvar = gvar;
    printf("gvar = %ld\n", gvar);
    return 0;
}
evaitl@evbb ~/se $ cat Makefile 

foo: foo.c
    gcc -Wall -g -O0 -o $@ $<
evaitl@evbb ~/se $ rm foo
evaitl@evbb ~/se $ make
gcc -Wall -g -O0 -o foo foo.c
foo.c: In function ‘main’:
foo.c:9:10: warning: variable ‘lvar’ set but not used [-Wunused-but-set-variable]
     long lvar=gvar;
          ^
evaitl@evbb ~/se $ gdb foo
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from foo...done.
(gdb) br main
Breakpoint 1 at 0x400535: file foo.c, line 9.
(gdb) run
Starting program: /home/evaitl/se/foo 

Breakpoint 1, main () at foo.c:9
9       long lvar=gvar;
(gdb) list
4   static long gvar = 0L;
5   int othervar=3;
6   char* onepointer=NULL;
7   
8   int main() {
9       long lvar=gvar;
10  
11      gvar = 1L;
12      lvar = gvar;
13      printf("gvar = %ld\n", gvar);
(gdb) p gvar
$1 = 0
(gdb) p lvar
$2 = 0
(gdb) p onepointer
$3 = 0x0
(gdb) p othervar
$4 = 3
(gdb) quit
A debugging session is active.

    Inferior 1 [process 13890] will be killed.

Quit anyway? (y or n) y
evaitl
  • 1,365
  • 8
  • 16