Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio: set a data breakpoint at a memory ACCESS (i.e. when data is READ)

I really need to figure out when my Fortran project is reading an element of a vector. I use data breakpoint on a daily basis but I could not find a way to set a data breakpoint when my code access (i.e. read) a memory address, while I always set it for breaking when the address is modified. Is there a way to do this on Visual Studio 2010? (I use intel visual fortran compose XE 2011 as compiler). Or maybe updating to a more recent visual studio? Just as a note, I saw here that gdb does that Can I set a breakpoint on 'memory access' in GDB? thanks A.

Ps: I emaild the guys from GDB and they said its not possible to do it with it.See their answer below:

Hello, Currently the type of created watchpoint is hardcoded to "write". This is because Visual Studio has no support for other types of watchpoints (in GUI and infrastructure). Perhaps it would be possible to enable read watchpoints in the GDB console, however it also would require a hack, as the console actually works "through" Visual Studio (it does not pass commands directly to GDB). I am also not sure whether this feature really works in GDB. GDB has a lot of commands which have very limited target scope, e.g. they only work for single threaded programs, or just for Linux and not when using gdbserver, etc. A read watchpoint looks like a mechanism that is very platform dependent. Please check if GDB your platform supports read watchpoints. Also let us know if this feature is critical for you.

Best regards

like image 782
Millemila Avatar asked May 14 '13 16:05

Millemila


People also ask

How do I set a data breakpoint in Visual Studio?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do I add a conditional breakpoint in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

Why we use breakpoints in debugging?

A breakpoint helps to speed up the debugging process in a large program by allowing the execution to continue up to a desired point before debugging begins. This is more efficient than stepping through the code on a line-by-line basis.

How do I view breakpoints in Visual Studio?

Visual Studio provides a straightforward way to manage all your breakpoints under a single toolbox window, which you can find by navigating to Debug | Windows | Breakpoints (keyboard shortcut: Ctrl + D + B).


2 Answers

Some workarounds (except using WinDBG):

  1. Inject a NaN value if it is about a floating point element. And enable trapping of operations with NaN. This will catch not a read, but a first arithmetic operation with the value. Thus copying of the element will be missed, but an attempt to perform an operation will throw an FP exception exposing the place it occurs.

  2. Unmap a memory page with the value. It's very unexact and will react for access to 4Kb of data around the value... But it still may work for some cases. Check description of MapUserPhysicalPages() for Windows, and munmap() for Linux.

like image 134
Alexander Kobotov Avatar answered Oct 04 '22 02:10

Alexander Kobotov


You can use Mike Morearty's Hardware Breakpoints.

I have not tested them on Visual Studio 2010, but I have succesfully used it in VS 2008, 2015 and 2017.

int x = 0;
HardwareBreakpoint hb;
hb.Set(&x, sizeof(x), HardwareBreakpoint::Read);
// Some random code.
int y = x; // The breakpoint pauses the execution on this line.

Note that HardwareBreakpoint object must be in the scope while you want that breakpoint to be alive. When it goes out of the scope, the breakpoint will stop functioning.

like image 40
Nikola Malešević Avatar answered Oct 04 '22 04:10

Nikola Malešević