1
votes

I'm using GDB to return an address of a local static variable in my c code (pressureResult2 in this case), this is working fine for the ARM output: The .elf file.

However, if I use a build configuration for windows, creating a .exe, the variable I'm asking for can't be found.

What I'm using for returning the address of the variable:

sensorOffset::pressureResult2

Code:

static void sensorOffset (uint8_t axle)
{
  static int16_t pressureResult2 = 400;
  int16_t pressureResult = 0;
  if (axle == AXLE_FRONT)
  {
   /* Always copy the actual value first to the value with offset */
   CtisMach.front.tPressureSensorOffset = CtisMach.front.tPressureAct;
   .... and so on

Is someone known with this issue? Is the command different for a windows executable? Or am I just doing something wrong?

To get the most obvious ones out:

Can you read an global static?

Yes, no problem

Does GDB notice anything about debug symbol?

No, the usual "Reading symbol from file.exe .. done" appears.

Does it work with .elf?

Yes, it does.

To answer the comments:

The code is compiled with the following:

 cflags := \
    -O0 \
    -g3 \
    -Wall \
    -c \
    -MD \
    -fmessage-length=0 \
    -fpermissive \
    -I/mingw/include \
    -I/usr/include \
    -I/local/include \
    -D WINDOWS \
    $(CONFIGFLAGS) \
    $(INCLUDES)

 lnkflags := \
     -Wl,--enable-stdcall-fixup \
     -static-libgcc \
     -static-libstdc++ \
     $(CONFIGFLAGS) \
    $(EXT_LIBDIR)

 od_flags := \
    --dwarf

Since I already mentioned it doesn't complain about debug variables symbols and I can read the global statics as well this doesn't appear to be the issue, or am I wrong? It should complain about not having debug symbols without -g right? Edit: Andreas reproduced this situation, but I still can't seem to fix it.

To do anything useful with the variable:

static int16_t pressureResult2 = 0;

if (pressureResult2 < 100)
{
  pressureResult2++;
}
else
{
  pressureResult2 = 0;
} 

NOTE: This is just an example, same problem counts for all local statics in the code (that is too large to dump on SO).

GDB response on "Info variables", my variable "pressureResult2" is placed in the category Non-debugging symbols, might this be the issue?:

GDB response on "Info variables"

To see if the -g flag is actually doing something, without -g:

p& randomvar
$1 = (<data variable, no debug info> *) 0x4eade2 <randomvar>

with -g

p& randomvar    
$1 = (uint16_t *) 0x4eade2 <randomvar>

So it's active for sure, but its still not possible to return local statics. The only remarkable things so far is how the variable I'm looking for is categorized into Non-debugging symbols.

Compiling the code snipped of Andreas works including returning the address of the variable, my own code however, not much.

1
Love the downvotes, any explanations? - koldewb
Show the compilation command and how exactly you use pressureResult2. Look into the produced assembler code by passing -fverbose-asm -O -S to your GCC cross-compiler - Basile Starynkevitch
Try shrinking down the program - does the minimal code from my answer work? Can you see the local static variable when you follow those steps? - Andreas Fester

1 Answers

0
votes

Most likely, you need to add the -g flag to the compiler invocation to add debugging information, and remove optimization flags like -O2. Given the following .c source file, using a cygwin environment on MS Windows:

#include <stdio.h>

static int globalstatic = 512;

static void sensorOffset (uint8_t axle) {
  static int16_t pressureResult2 = 400;

  pressureResult2++;
  printf("%d %d\n", globalstatic, pressureResult2);
}

int main() {
   int i = 0;
   for (i = 0;  i < 10;  i++) {
       sensorOffset(42);
   }
   return 0;
}

When compiled to an .exe file with -O2, your observation is reproduceable - gdb recognizes the global static variable, but not the local one (even though -g was specified):

C:> gcc -g -O2 -Wall -pedantic -o static static.c

C:> gdb static.exe
(gdb) break main
(gdb) run
Breakpoint 1, main () at static.c:14
14      int main() {    Breakpoint 1, 0x0000000100401128 in main ()
(gdb) print globalstatic
$1 = 512
(gdb) print sensorOffset::pressureResult2
No symbol "sensorOffset" in current context.

When removing the -O2 flag, gdb does recognize the local static variable:

C:> gcc -g -Wall -pedantic -o static static.c

C:> gdb static.exe
(gdb) break main
(gdb) run
Breakpoint 1, main () at static.c:12
12         int i = 0;
(gdb) print sensorOffset::pressureResult2
$1 = 400