Skip to contentSkip to conversation

Using hardware watchpoint for applications debugging

DISCUSSION colin 5 participants 8 messages Mar 29, 2006 – Mar 30, 2006 MIPS

lore
Share & tools
lore

colin

author
#0 lore raw

Hi all,
Our applications encounter memory crash very often. Thus we would like to use hardware watchpoint on our platform, MIPS 4KEc.
After starting to design it, more and more issues emerge. They are listed below:
1. WatchLo Register only has 29 bits to indicate the VAddr. Therefore,
it will also trigger exceptions when accessing nearby addresses.
2. When an exception happens and we find that it's not touching the righ
address, we will discard it. However, exception will happen again because the former instruction will be re-executed when the exception is finished.

Is there any easy way to solve these problems?

Regards,
Colin

#1 lore raw

colin wrote:.

Quoted 5 lines

2. When an exception happens and we find that it's not touching the righ
address, we will discard it. However, exception will happen again because the former instruction will be re-executed when the exception is finished.

You'll need to single-step over the instruction which generated the unwanted watchpoint exception, with the watchpoint disabled. Then after handling the single step reenable the watchpoint and resume normal execution.

It would be best if you added watchpoint support to the kernel ptrace
code: since that would make the watchpoints usable by GDB also.

Nigel

#2 lore raw
Quoted 10 lines

colin wrote:.

Quoted 4 lines

2. When an exception happens and we find that it's not touching the righ
address, we will discard it. However, exception will happen again because the former instruction will be re-executed when the exception is finished.

You'll need to single-step over the instruction which generated the unwanted watchpoint exception, with the watchpoint disabled. Then after handling the single step reenable the watchpoint and resume normal execution.

There's actually a simpler and more efficient approach in Linux. The code already exists in the MIPS Linux kernel to "skip" the instruction responsible for the current exception, because the situation also arises for emulated
instructions. In do_watch(), in the cases where you want to ignore the
watchpoint, you should be able to just invoke compute_return_epc(regs) and return. There should be no need to handle single-step exceptions or disable/reenable the watchpoint.

Regards,

Kevin K.

author
#4 lore raw
In reply to Nigel Stephens

Hi Nigel,
We use the same way with you to handle the issue 2.

As to adding watchpoint to kernel, there will be another problem. ASID in kernel is variable. Therefore, we cannot indicate which thread we want to watch by ASID.
What we can do is setting G (global) bit to WatchHi Register and then all threads accessing that address will cause the exception.
In the exception, it will filter the threads by PID to find out the thread we are watching.

Regards,
Colin

----- Original Message -----
From: "Nigel Stephens" <[email protected]>
To: "colin" <[email protected]>
Cc: <[email protected]>
Sent: Wednesday, March 29, 2006 10:08 PM
Subject: Re: Using hardware watchpoint for applications debugging

Quoted 4 lines

colin wrote:.

Quoted 1 line

2. When an exception happens and we find that it's not touching the

righ

Quoted 1 line
Quoted 1 line

address, we will discard it. However, exception will happen again

because

Quoted 1 line
Quoted 1 line

the former instruction will be re-executed when the exception is

finished.

Quoted 13 lines
Quoted 2 lines

You'll need to single-step over the instruction which generated the unwanted watchpoint exception, with the watchpoint disabled. Then after handling the single step reenable the watchpoint and resume normal execution.

It would be best if you added watchpoint support to the kernel ptrace
code: since that would make the watchpoints usable by GDB also.

Nigel

author
#3 lore raw
In reply to Kevin D. Kissell

Hi Kevin,
After looking into function compute_return_epc(regs), we find that it can just skip an instruction.
But the instruction that cause exceptions should not be skipped.

Regards,
Colin

----- Original Message -----
From: "Kevin D. Kissell" <[email protected]>
To: "Nigel Stephens" <[email protected]>; "colin" <[email protected]>
Cc: <[email protected]>
Sent: Wednesday, March 29, 2006 10:32 PM
Subject: Re: Using hardware watchpoint for applications debugging

Quoted 2 lines
Quoted 2 lines

colin wrote:.

Quoted 1 line

2. When an exception happens and we find that it's not touching

the righ

Quoted 1 line
Quoted 1 line
Quoted 1 line

address, we will discard it. However, exception will happen again

because

Quoted 1 line
Quoted 1 line
Quoted 1 line

the former instruction will be re-executed when the exception is

finished.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

You'll need to single-step over the instruction which generated the unwanted watchpoint exception, with the watchpoint disabled. Then after handling the single step reenable the watchpoint and resume normal execution.

There's actually a simpler and more efficient approach in Linux. The code already exists in the MIPS Linux kernel to "skip" the instruction

responsible

Quoted 9 lines

for the current exception, because the situation also arises for emulated
instructions. In do_watch(), in the cases where you want to ignore the
watchpoint, you should be able to just invoke compute_return_epc(regs) and return. There should be no need to handle single-step exceptions or disable/reenable the watchpoint.

Regards,

Kevin K.

#5 lore raw
In reply to colin
Quoted 9 lines · colin

As to adding watchpoint to kernel, there will be another problem. ASID in kernel is variable. Therefore, we cannot indicate which thread we want to watch by ASID.
What we can do is setting G (global) bit to WatchHi Register and then all threads accessing that address will cause the exception.
In the exception, it will filter the threads by PID to find out the thread we are watching.

They're variable, but not very variable: the PID->ASID mapping will only change when the ASIDs roll over and the ASID gets reallocated to a different process, which will only happen after another 256 processes have been created. But in that case your watched process will have to be allocated a new ASID before it can run again. So you could, perhaps, modify the TLB management code to clear the Watch registers whenever an ASID belong to a process with watchpoints is recycled, and then reprogram the Watch registers when such a process is allocated a new ASID. Alternatively you could maintain pre-process copies of the Watch registers, and context switch them along with other per-process register state -- though that is adding context switch overhead to processes which don't use watchpoints, and might not be popular with the maintainer.

Nigel

#6 lore raw
Quoted 4 lines · Nigel Stephens

They're variable, but not very variable: the PID->ASID mapping will only change when the ASIDs roll over and the ASID gets reallocated to a different process, which will only happen after another 256 processes have been created. But in that case your watched process will have to be

Quoted 8 lines

allocated a new ASID before it can run again. So you could, perhaps, modify the TLB management code to clear the Watch registers whenever an ASID belong to a process with watchpoints is recycled, and then reprogram the Watch registers when such a process is allocated a new ASID. Alternatively you could maintain pre-process copies of the Watch registers, and context switch them along with other per-process register state -- though that is adding context switch overhead to processes which don't use watchpoints, and might not be popular with the maintainer.

That's not quite true; the kernel also uses the ASID as a way to flush a context from the TLB very quickly.

Ralf

#7 lore raw
In reply to Nigel Stephens
Quoted 4 lines · Nigel Stephens

They're variable, but not very variable: the PID->ASID mapping will only change when the ASIDs roll over and the ASID gets reallocated to a different process, which will only happen after another 256 processes have been created. But in that case your watched process will have to be

Quoted 8 lines

allocated a new ASID before it can run again. So you could, perhaps, modify the TLB management code to clear the Watch registers whenever an ASID belong to a process with watchpoints is recycled, and then reprogram the Watch registers when such a process is allocated a new ASID. Alternatively you could maintain pre-process copies of the Watch registers, and context switch them along with other per-process register state -- though that is adding context switch overhead to processes which don't use watchpoints, and might not be popular with the maintainer.

If you want GDB to use them, you almost certainly want them to be per-process. You can context switch them lazily, though. We've solved this problem before plenty of times...

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery