10
votes

So I'm doing some stack/heap digging with gdb and trying to grab the value for someInt, but have thrown my limited gdb knowledge to get at it w/o effect. I need to get the value of someInt using gdb, and it's only referenced at one location outside of the #define, line 20

#define someInt 0x11111111

void someFunc() {
   // ...
   int a = 0;
   if(a==someInt) {  //line 20
   //...
   }
}

After calling gdb on the compiled program I've tried gdb break 20 and then gdb x\dw $someInt I get No symbol 'someInt' in current context. If I try x/dw 0x11111111 I get 'Cannot access memory at address 0x11111111'. I can't recompile the code a la How do I print a #defined constant in GDB? and thus am lost as to how to print the value at that space.

How do I use gdb (most likely with x) to print out the value of someInt?

2
grab the value for someInt .... conceptual problem. What exactly is someInt?? - Sourav Ghosh
someInt is a value that if 'a' matches it, gives me (through the if statement) mock shell access to demonstrate a concept in a book - Kurt Wagner
So, you can see the define, but you would still like to know value of macro? Or do you need to print value of a? - dbrank0
Do yourself a favour, and make sure your macro's all have UPPER_CASE names. That way, you're far less likely to confuse them with variables as you seem to be doing. Macro's are for the preprocessor to sort, once compiled, they no longer exist as such - Elias Van Ootegem

2 Answers

12
votes

The answer is here: GCC -g vs -g3 GDB Flag: What is the Difference?

Compile with -O0 -ggdb3:

gcc -O0 -ggdb3 source.c

From doc

-ggdblevel - Request debugging information and also use level to specify how much information. The default level is 2.

Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

9               if(a == someInt)
(gdb) list
4
5       int main()
6       {
7               int a=0;
8
9               if(a == someInt)
10              {
11                      printf("!\n");
12              }
13      }
(gdb) p someInt
$1 = 1111
0
votes

yes. MACROS are usally exapanded and simply used as text replacement. That's why gdb reports , in your code, there is no someInt.

effecttively, after preprocessing, your code looks like

void someFunc() {
   // ...
   int a = 0;
   if(a==0x11111111) {  //line 20 //note the change
   //...
   }
}

so, in your binary, there is no existance of someInt.

Hint: Don't confuse someInt as a variable. Hope this helps.