State firewall at higher stack frames

In the MSVC ++ debugger, is it possible to create a breakpoint in one function, the state of which depends on local variables from other frames of the stack? I often find that I am creating a conditional breakpoint in one function, and when this breakpoint hits, I turn on another breakpoint (which I expect to start before exiting the current function call) and continue. This is time consuming and error prone.

One approach that I have used in the past is to write the variable in question globally and use this global as a condition. But this requires recompilation and does not work with multi-threaded code.

+6
source share
1 answer

Yes, it is technically possible. There is not much joy, mind you have to tell the debugger to dereference the pointer to get the value of a local variable in another frame of the stack.

A simple example:

#include "stdafx.h" #include <iostream> void foo() { for (int ix = 0; ix < 5; ++ix) { std::cout << ix << " "; // <=== Conditional breakpoint here } } void bar() { for (int jx = 0; jx < 5; ++jx) { std::cout << jx << ": "; // <=== Start with a breakpoint here foo(); std::cout << std::endl; } } int _tmain(int argc, _TCHAR* argv[]) { bar(); return 0; } 

First you need to get the address of the variable on which you want to set the condition. Set a breakpoint in the display line in line (). When it hits, evaluate &jx and copy the value.

Now set a conditional breakpoint using this value. I used:

  *(int*)0x0073fbc8 == 2 && ix == 3 

Where 0x0073fbc8 is the value of I obtained at the first breakpoint. Or you can make it relative from the base pointer register. Set an unconditional breakpoint, and when it hits, use the Debug + Windows + registers to view the EBP value. Subtract it from & jx. I used:

  *(int*)(ebp+0xd8) == 2 && ix == 3 

Both worked well. Note that you will want to disable ASLR for Debug builds to hope that these addresses will repeat from one run to another. Project + Properties, Linker, Advanced, Randomized Base Address = No.

+6
source

All Articles