12

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?

4
  • grab the value for someInt .... conceptual problem. What exactly is someInt?? Nov 12, 2014 at 7:59
  • 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 Nov 12, 2014 at 8:04
  • 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
    Nov 12, 2014 at 8:42
  • 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 Nov 12, 2014 at 9:00

2 Answers 2

13

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

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.

7
  • 1
    Reclarified the question; I'm looking how to print out value at #define address using gdb (and in conjunction with with x), not modifying the code Nov 12, 2014 at 7:54
  • @KurtWagner and how do you exactly know value at #define address is accessible? What you're upto? Nov 12, 2014 at 7:57
  • I'm following along in an exploitation book to better understand how people do things like stack/heap overflows and getting shell access. It's one of the problems so it should be feasible, I'm just not getting a gdb concept that is critical for this bit.... Nov 12, 2014 at 8:03
  • 1
    @KurtWagner: Object-like macros don't have any address. These are simply gone after preprocessing phase. Either recompile you program an use proper syntax from GDB manual or deduce it from program's source (or disasm if symbols are not available). Nov 12, 2014 at 8:29
  • Ah OK, I'm thinking maybe I should trying using gdb on one of the registers then? With Intel architecture is there a place where the first/second arg's go in an if statement? Nov 12, 2014 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.