4

I have a program comparing two values and I would like to print them for debug :

   0x00000000004005cd <+73>:    mov    DWORD PTR [rbp-0x4],eax
   0x00000000004005d0 <+76>:    mov    eax,DWORD PTR [rbp-0x4]
=> 0x00000000004005d3 <+79>:    cmp    eax,0x1e240
   0x00000000004005d8 <+84>:    jne    0x4005e6 <main+98>

So i placed a breakpoint on main+79 and I would like to print the values being compared by the cmp call.

How can I achieve that with gdb?

Thanks for your help.

mimipc
  • 1,354
  • 2
  • 14
  • 28

2 Answers2

4

CMP is not a call - it's a single instruction. To see the current value of EAX, use the following command:

info registers eax

The other comparand is the hex value 0x1e240. It's not an address, it's an integer constant.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Or print $eax. Or display $eax (to print it after each step). – dbrank0 Jun 27 '13 at 13:15
  • 1
    As far as i can understand, my input is in eax and is compared with 123456 (0x1e240). But my eax register contains 41 when I input 123. Shouldn't it be 7B? – mimipc Jun 27 '13 at 13:17
  • Then something is probably wrong with your input processing. Please ask another question, with input handling code. Most likely, you don't do decimal-to-binary conversion (the stuff of `atoi`). But that's a topic for another question. – Seva Alekseyev Jun 27 '13 at 13:20
  • Here on StackOverflow, we say thanks by accepting the answer :) Just sayin'. – Seva Alekseyev Jun 27 '13 at 13:21
  • I don't know if it's bad to comment to thank someone, but I think it's just normal to thank people for the time they spend helping you with your problems. – mimipc Jun 27 '13 at 13:23
2

Using the print and/or x (examine) commands you can print the values being compared, you can also just print the registers in this case.

print $eax
info registers

The formats for the examine command are specified by entering help x:

Formats: o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string).
Size letters: b(byte), h(halfword), w(word), g(giant, 8 bytes).

I'll assume 0x1e240 is an address in this case (though its clearly not), and let's say I want to print 4 hex words starting at this base address. It would be:

x[count]{format}{size} 0x1e240
x/4xw 0x1e240

Also, keep in mind the print can also use the format specifiers:

print/{format} $eax
print/x $eax

Check out other interesting commands like display as well.

David Pullar
  • 706
  • 6
  • 18