views:

496

answers:

4

Can I set the breakpoint/watchpoint/smth else in gdb for register value?

I want to break when $eax will have value 0x0000ffaa.

Is it possible with gdb or dbx or any other unix debugger?

+1  A: 

I was unable to get it to watch eax directly, so I inserted a few asm instructions to store the required value to a junk variable and watch that. I was unable to convince gcc to use eax, so this code "watches" ebx instead.

#include <stdio.h>
int tmp;
int main(void)
{
  int i;
  printf("tmp is originally %d\n",tmp);
  for(i=0;i<20;i++)
  {
    asm (
    "cmpl $10,%ebx\n"
    "jne dont\n"
    "movl %ebx,tmp\n"
    "dont:\n"
        );
    printf("%d\n",i);
  printf("\nnow tmp is %d\n",tmp);
  return 0;
}

Now you can "watch tmp"

Arthur Kalliokoski
Hmm. I can't change code so lot. What is the problem with watching eax?
osgx
If your code is using eax, fine, my little test program insisted on using ebx.
Arthur Kalliokoski
+7  A: 

Yes in gdb you would set a watchpoint for like so:

watch $eax == 0x0000ffaa

But it is dependent on watchpoint support for the target. You should note that this may slow down execution significantly.

If you would like to break in a certain location you can do so, by setting a conditional breakpoint:

break test.c:120 if $eax == 0x0000ffaa
Marc
A: 

If you are on a 64 bit machine you have to watch $rax, not $eax...

Dan
I'm on machine, which does not have neither eax nor rax. :)
osgx
A: 
Dan
Dan. If you are running x86_64 then please, don't spam in my question with your sexual problems with x86 vs x86_86 and others long modes.I need not the conditional breakpoint, but watchpoint on register value.Please, feel free to delete both your answers here.
osgx