I'm just doing some experiments using GDB and playing around with the registers, but I encounter a problem when using the syscall gettimeofday() and a watchpoint on a register.
first let me expose a little example of what I am doing.
ok, here is the code which I am using (very simple):
#include <stdio.h>
main()
{
int num;
getchar();
num=190320;
printf("value: %d\n", num);
}
well, what I am doing is just run the program (which stop at the getchar() funtion until I press enter) and then attach the program to a gdb session in other shell:
gdb -p <pid>
now I just add a conditional watchpoint on the "rdi" register so I can check the status of the program when the variable "num" is assigned :
(gdb) watch $rdi == 190320
Watchpoint 1: $rdi == 190320
and now continue the program execution on gdb and push enter on the other shell where I am running the program, and as you can see gdb stop the program in the watchpoint just like I wanna.
(gdb) c
Continuing.
Watchpoint 1: $rdi == 190320
well, this is the version that works like I just expect, a simple application that runs ok and a watchpoint that stop in the right moment.
Ok, now go to the problem itself.
this is the same program I used before but with the difference that I use a gettimeofday() before the variable assignation:
#include <stdio.h>
main()
{
int num;
struct timeval tim;
getchar();
gettimeofday(&tim, NULL); /* <---- Here is !!!*/
num=190320;
printf("value: %d\n", num);
}
and now repeat the same steps I did before:
-run the program in a shell
-attach the program to a gdb session in another shell
-set the conditional watchpoint on "rdi" register
but now when I continue the execution in gdb and push enter in the shell where the program is running, the program just get stuck at the gettimeofday() function.
if I press "Ctrl+C" on gdb I can check that the program is stuck in this function
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00007ffc88b85e3c in gettimeofday ()
now if I disable the watchpoint and try to continue the execution again, all goes fine, and the program ends with no problem (obviously the watchpoint is disable and gdb doesn't stop the program in the moment that I want to).
(gdb) info breakpoint
Num Type Disp Enb Address What
1 watchpoint keep y $rdi == 190320
(gdb) disable 1
(gdb) c
Continuing.
[Inferior 1 (process 4151) exited with code 016]
so I can verify that the cause of the program get stuck is the watchpoint set in the register...
So the question is, can someone explain why is this happening? and, is there any way to solve this issue and do the program doesn't get stuck in the gettimeofday() function and reach the watchpoint?
PD: I know that I can stop the program in the variable assignation using other methods but this is just an experiment and I just want the explanation of why is this happening
PD2: Sorry for my bad english, it's not my mattern language.
via
Chebli Mohamed