Author |
Message |
Drow #1 / 11
|
 Memory watchpoints
Is there any way of adding memory watchpoints (i.e. have a debug event - or something similar - when a memory region is read from or written to) in Win32? We're developing a number of memory intensive algorithms, and we're interested in characterising the memory access counts/patterns. By way of example, consider a simple array add algorithm (implemented here in C++): void AddArray( const int *pIn1, const int *pIn2, int *pOut, size_t size ) { for( size_t idx = 0; idx < size; ++idx ) { pOut[ idx ] = pIn1[ idx ] + pIn2[ idx ]; } Quote: }
Before invocation of AddArray we'll know the start address, and ranges of each of pIn1, pIn2 and pOut, and would be able to put memory watchpoints on these regions. As each watchpoint triggers our 'de{*filter*}' will be able to catch these events. I've shown a C++ example, but it's likely that our algorithm's will be written in assembler. Thanks in advance, Ralph
|
Tue, 24 May 2005 17:38:15 GMT |
|
 |
Jochen Kalmbac #2 / 11
|
 Memory watchpoints
Quote:
> Is there any way of adding memory watchpoints (i.e. have a debug event > - or something similar - when a memory region is read from or written > to) in Win32?
Breakpoint | New | Data Greetings Jochen
|
Wed, 25 May 2005 01:24:26 GMT |
|
 |
Drow #3 / 11
|
 Memory watchpoints
Quote:
> > Is there any way of adding memory watchpoints (i.e. have a debug event > > - or something similar - when a memory region is read from or written > > to) in Win32? > Breakpoint | New | Data > Greetings > Jochen
Thanks for your help, but if this is the 'Advanced breakpoint' functionality in Visual Studio, then unfortunately it won't work. The advanced breakpoint can only tell us when a variable (or expression) changes at the breakpoint. It doesn't tell us when a variable is read, or when it's written to, but it's value doesn't change. We're interested in every read and write access to a memory region, even if it doesn't change the contents of the memory. Ralph
|
Fri, 27 May 2005 16:57:41 GMT |
|
 |
Jochen Kalmbac #4 / 11
|
 Memory watchpoints
Quote:
> Thanks for your help, but if this is the 'Advanced breakpoint' > functionality in Visual Studio, then unfortunately it won't work. The > advanced breakpoint can only tell us when a variable (or expression) > changes at the breakpoint. It doesn't tell us when a variable is read, > or when it's written to, but it's value doesn't change. > We're interested in every read and write access to a memory region, > even if it doesn't change the contents of the memory.
If you do not specify an condition, then it should break if the value is accessed.... -- Greetings Jochen Do you need a memory-leak finder ? http://www.codeproject.com/useritems/leakfinder.asp
|
Fri, 27 May 2005 18:14:11 GMT |
|
 |
Drow #5 / 11
|
 Memory watchpoints
Quote:
> > Thanks for your help, but if this is the 'Advanced breakpoint' > > functionality in Visual Studio, then unfortunately it won't work. The > > advanced breakpoint can only tell us when a variable (or expression) > > changes at the breakpoint. It doesn't tell us when a variable is read, > > or when it's written to, but it's value doesn't change. > > We're interested in every read and write access to a memory region, > > even if it doesn't change the contents of the memory. > If you do not specify an condition, then it should break if the value is > accessed....
Which version of Visual Studio are you using? We're using V6(SP4), and I can get it to trigger when the value actually changes, but at no other time. I'm using the test harness below: ::ZeroMemory( in1, 1024 * 4 ); ::ZeroMemory( in2, 1024 * 4 ); ::ZeroMemory( out, 1024 * 4 ); in1[ 32 ] = 12; // <-- turn watchpoints on here... for( size_t idx = 0; idx < 1024; ++idx ) { out[ idx ] = in1[ idx ] + in2[ idx ]; } the watchpoints I've got enabled are: when 'in1' (length:64) changes when 'in2' (length:64) changes when 'out' (length:64) changes The only breakpoint I see triggered is out[ 32 ] changing. Am I missing something? Thanks in advance, Ralph
|
Sat, 28 May 2005 00:38:32 GMT |
|
 |
Jochen Kalmbac #6 / 11
|
 Memory watchpoints
Quote:
>> If you do not specify an condition, then it should break if the value >> is accessed.... > Which version of Visual Studio are you using? We're using V6(SP4), and > I can get it to trigger when the value actually changes, but at no > other time. I'm using the test harness below: > ::ZeroMemory( in1, 1024 * 4 ); > ::ZeroMemory( in2, 1024 * 4 ); > ::ZeroMemory( out, 1024 * 4 ); > in1[ 32 ] = 12; // <-- turn watchpoints on here... > for( size_t idx = 0; idx < 1024; ++idx ) > { > out[ idx ] = in1[ idx ] + in2[ idx ]; > } > the watchpoints I've got enabled are: > when 'in1' (length:64) changes > when 'in2' (length:64) changes > when 'out' (length:64) changes > The only breakpoint I see triggered is out[ 32 ] changing. > Am I missing something?
No I missed something.... I tried it with the VS-De{*filter*} but I was also unable to set the breakpoint "on access". The only solution I found is to use the Microsoft WinDbg. With this de{*filter*} you can set data breakpoints with the BA command: <help> BA (Break on Access) Sets a data breakpoint, which will be triggered when the specified memory is accessed. </help> And you can specify which access should be monitored: <help> Access The type of access which will satisfy the breakpoint: Option Action e (execute) Breaks into the de{*filter*} when the CPU fetches an instruction from the specified address. r (read/write) Breaks into the de{*filter*} when the CPU reads or writes at the specified address. w (write) Breaks into the de{*filter*} when the CPU writes at the specified address. i (i/o) (Windows XP and Windows .NET Server only, kernel-mode only, x86 only) Breaks into the de{*filter*} when the I/O port at the specified Address is accessed. </help> You can download the debugging tools (including the de{*filter*}) at: http://www.*-*-*.com/ -- Greetings Jochen Do you need a memory-leak finder ? http://www.*-*-*.com/
|
Sat, 28 May 2005 14:26:12 GMT |
|
 |
Drow #7 / 11
|
 Memory watchpoints
<snip> Quote: > The only solution I found is to use the Microsoft WinDbg. > With this de{*filter*} you can set data breakpoints with the BA command: > <help> > BA (Break on Access) > Sets a data breakpoint, which will be triggered when the specified memory > is accessed. > </help> > And you can specify which access should be monitored: > <help> > Access > The type of access which will satisfy the breakpoint: Option Action > e (execute) Breaks into the de{*filter*} when the CPU fetches an instruction > from the specified address. > r (read/write) Breaks into the de{*filter*} when the CPU reads or writes at > the specified address. > w (write) Breaks into the de{*filter*} when the CPU writes at the specified > address. > i (i/o) (Windows XP and Windows .NET Server only, kernel-mode only, x86 > only) Breaks into the de{*filter*} when the I/O port at the specified Address > is accessed. > </help> > You can download the debugging tools (including the de{*filter*}) at: > http://www.*-*-*.com/
We've already looked at this - it only appears to support a maximum of four data breakpoints, and (on IA-32) each watchpoint can only support upto four bytes. As our algorithms are likely to take quite large buffers, it could take some considerable time to iterate through each dataset. Thanks for you help tho' - it's been much appreciated. Ralph
|
Sat, 28 May 2005 20:51:26 GMT |
|
 |
Jochen Kalmbac #8 / 11
|
 Memory watchpoints
Quote:
> <snip> >> The only solution I found is to use the Microsoft WinDbg. >> With this de{*filter*} you can set data breakpoints with the BA command: >> <help> >> BA (Break on Access) >> Sets a data breakpoint, which will be triggered when the specified >> memory is accessed. >> </help> >> And you can specify which access should be monitored: >> <help> >> Access >> The type of access which will satisfy the breakpoint: Option Action >> e (execute) Breaks into the de{*filter*} when the CPU fetches an >> instruction from the specified address. >> r (read/write) Breaks into the de{*filter*} when the CPU reads or writes >> at the specified address. >> w (write) Breaks into the de{*filter*} when the CPU writes at the >> specified address. >> i (i/o) (Windows XP and Windows .NET Server only, kernel-mode only, >> x86 only) Breaks into the de{*filter*} when the I/O port at the specified >> Address is accessed. >> </help> >> You can download the debugging tools (including the de{*filter*}) at: >> http://www.*-*-*.com/ > We've already looked at this - it only appears to support a maximum of > four data breakpoints, and (on IA-32) each watchpoint can only support > upto four bytes.
Yes, this is true. The data breakpoints are implemented using the DR0 to DR3 register. And these registers does have the restriction you said... And the "breakpoint" is generated by the processor if the specified operation occurs. So there is not time delay in watching the memory. By the way: What do you want to do ? Maybe there are other solutions... -- Greetings Jochen Do you need a memory-leak finder ? http://www.*-*-*.com/
|
Sat, 28 May 2005 20:59:41 GMT |
|
 |
Leo Chen [MSFT #9 / 11
|
 Memory watchpoints
Hi Ralph, Maybe VirtualProtect/VirtualAllocEx is what you want? You can use it to guard the memory, and get exception when the memory is accessed. Hope this helps. Best Regards, Leo Chen This posting is provided "AS IS" with no warranties, and confers no rights. --------------------
| Newsgroups: microsoft.public.vc.de{*filter*} | Subject: Re: Memory watchpoints | Date: 10 Dec 2002 04:51:26 -0800 | Organization: http://www.*-*-*.com/ | Lines: 38
| NNTP-Posting-Host: 193.122.23.66 | Content-Type: text/plain; charset=ISO-8859-1 | Content-Transfer-Encoding: 8bit | X-Trace: posting.google.com 1039524686 17620 127.0.0.1 (10 Dec 2002 12:51:26 GMT)
| NNTP-Posting-Date: 10 Dec 2002 12:51:26 GMT | Path: cpmsftngxa06!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de!news-spur1.m axwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-08!sn-xit-09! supernews.com!postnews1.google.com!not-for-mail | Xref: cpmsftngxa06 microsoft.public.vc.de{*filter*}:14736 | X-Tomcat-NG: microsoft.public.vc.de{*filter*} | | <snip> | > The only solution I found is to use the Microsoft WinDbg. | > With this de{*filter*} you can set data breakpoints with the BA command: | > | > <help> | > BA (Break on Access) | > Sets a data breakpoint, which will be triggered when the specified memory | > is accessed. | > </help> | > | > And you can specify which access should be monitored: | > <help> | > Access | > The type of access which will satisfy the breakpoint: Option Action | > e (execute) Breaks into the de{*filter*} when the CPU fetches an instruction | > from the specified address. | > r (read/write) Breaks into the de{*filter*} when the CPU reads or writes at | > the specified address. | > w (write) Breaks into the de{*filter*} when the CPU writes at the specified | > address. | > i (i/o) (Windows XP and Windows .NET Server only, kernel-mode only, x86 | > only) Breaks into the de{*filter*} when the I/O port at the specified Address | > is accessed. | > </help> | > | > You can download the debugging tools (including the de{*filter*}) at: | > | > http://www.*-*-*.com/ | | We've already looked at this - it only appears to support a maximum of | four data breakpoints, and (on IA-32) each watchpoint can only support | upto four bytes. As our algorithms are likely to take quite large | buffers, it could take some considerable time to iterate through each | dataset. | | Thanks for you help tho' - it's been much appreciated. | | Ralph |
|
Sun, 29 May 2005 09:25:19 GMT |
|
 |
Drow #10 / 11
|
 Memory watchpoints
<snip> Quote: > By the way: > What do you want to do ? > Maybe there are other solutions...
To fit in our system, the functional signature of an algorithm, and its memory requirements (e.g. scratch and persistent buffers) have to be declared up front. Additionally, the algorithm isn't allowed to allocate any dynamic memory of its own (its obviously allowed to grow the stack though). Because of these restrictions, we know before executing the algorithm all the memory regions it will use. We're trying to characterise the access counts/patterns to these memory regions. The results from the characterisations are employed in a simulator we're developing. One approach we've considered is using smart arrays, and implementing the [] operator to count the memory accesses. Unfortunately, while the algorithm must adhere to a C++ calling convention, it doesn't have to be implemented in C++ - Win32 is just one of our target platforms. On our other target platforms, it's likely that we'll be characterising algorithms developed in optimised assembler. These platforms have simulators which will support the application of watchpoints to memory regions. I look forward to any suggestions you might have. Ralph
|
Sun, 29 May 2005 17:32:32 GMT |
|
 |
Jochen Kalmbac #11 / 11
|
 Memory watchpoints
Quote:
>> What do you want to do ? > To fit in our system, the functional signature of an algorithm, and > its memory requirements (e.g. scratch and persistent buffers) have to > be declared up front. Additionally, the algorithm isn't allowed to > allocate any dynamic memory of its own (its obviously allowed to grow > the stack though). Because of these restrictions, we know before > executing the algorithm all the memory regions it will use. We're > trying to characterise the access counts/patterns to these memory > regions. The results from the characterisations are employed in a > simulator we're developing. > One approach we've considered is using smart arrays, and implementing > the [] operator to count the memory accesses.
There is no general (fast) solution to your problem... The best solution is the one you did ("smart" class with [] operator). But this works only for C++ (as you said)... The only generally solution I am able to thing of, is a "virtual processor". You have to executue the code line by line (or better assembler code by assembler code) and check if someone tried to access you region. To find out if someone has accessed your region you can use the VirtualProtect-function (thanx to Leo Chen). This approch will work for every language (bases on Win, IA32). But the excution might be very slow... For more info see: http://msdn.microsoft.com/library/en-us/dndebug/html/msdn_debugeh.asp -- Greetings Jochen Do you need a memory-leak finder ? http://www.codeproject.com/useritems/leakfinder.asp
|
Sun, 29 May 2005 18:01:40 GMT |
|
|
|