Re: Problem with manual watchpoints

Aravinda
2009-12-24T21:42:03Z

Thanks Jan,

replied inline.

On Thu, Dec 24, 2009 at 4:08 PM, Jan Kratochvil
<jan.kratochvil@redhat.com> wrote:
> On Thu, 24 Dec 2009 21:47:06 +0100, Aravinda wrote:
>> a = malloc(20);
>> __add_watchpoint(getpid(), &a[20]);
>>
>> for (i = 0; i < 25; i ++) {
>>       /* getc(stdin); ----> without this, no SIGTRAP is getting generated */
>>       printf("Accessing now %x\n", &a[i]);
>>       a[i]++;
>> }
>
> What is the type of "a"?
a is char*.
>After "a = malloc(20);" you can access elements
> a[0]...a[19] but a[20] is already after the allocated array size.
>
> Also the loop is till "i < 25" but you have allocated only 20 elements.  In
> fact you may have allocated only 5 elements if "*a" is "int" etc.
Yes, thats why I have a watchpoint added at &a[20]. So on accessing
the first element out of the allocated array, I want the program to
receive a SIGTRAP.
>
>
>> However, if I just add a 'getc(stdin)' before accessing every element, it
>> does get the exception on accessing a[20].
>
> You have data corruption in your program so it behaves very unpredictably.
But I have &a[20] in DR0 with necessary DR7 bits enabled, isnt the
program supposed to get a Trap/Breakpoint exception on accessing it
(the very first element out of the allocated buffer) ? Im trying to
avoid the memory corruption by handling SIGTRAP and aborting the
program.
Infact it gets this signal when the getc(stdin) is included, is it
something to do with the program should be in single step mode to get
SIGTRAPs or is the loop way too simple that it executes in no time
before the SIGTRAP is even raised ?

Thanks,
Aravinda