Search code examples
breakpointslocal-variablestrace32lauterbach

LauterBach debugging in trace32 : Is there a way to set a data breakpoint for a local variable before entering the context?


From the help document, to set a data access breakpoint, we can use

var.break <variable> /READWRITE

However, this can only work when we have entered the context for that variable.

Because I want to write a PRACTICE script to do automatic debugging, I want to do this before programs runs. Something like

....
var.break <foo>::<variable> /READWRITE
GO
WAIT !run()
...

Is there a way to do it?


Solution

  • In general you can set breakpoints only on local variables (before entering its declaring function), if the local variables have been declared static. (This is not a limitation by TRACE32. No debugger can set a breakpoint on a non-static variable before entering the declaring function.)

    Let's assume you have a function like that:

    int myFunc(int x) {
        int myVar;        
        static int myStaticVar;      
        /* ... */
    }
    

    For a static local variable you can set the breakpoint with

    `Var.Break.Set  myFunc\myStaticVar  /ReadWrite`
    

    Read/Write breakpoints work by using address comparators located on you target CPU. So these kind of breakpoints work only with static addresses. However not-static local variables do not have a fixed address. They are located in core registers or relative to the functions frame pointer on the call stack.

    Check the location of your local variable with the command Var.INFO myFunc\myVar

    For a local (non-static) variable located on the call stack you can set a read/write breakpoint before entering the function with the following trick:

    Var.NEWGLOBAL void * \temp    
    Break.Set myFunc /Program /CMD "Var.Break.Set myVar /ReadWrite" /CMD "Var.Set \temp=&myVar" /RESUME 
    Break.Set sYmbol.EXIT(myFunc) /Program /CMD "Break.Delete Var.VALUE(\temp)++(Var.SIZEOF(myVar)-1)" /RESUME 
    

    This will set a breakpoint which stops your CPU when entering your function. When the CPU stops at this breakpoint, the debugger sets the read/write breakpoint to the variable. The third command ensures that the read/write breakpoint gets deleted when exiting the function. (Otherwise the read/write breakpoint might trigger on a complete different variable in a complete different function)

    For a local (non-static) variable located in a core register you can normally not set a read/write breakpoint. However some CPUs support breakpoints on core register. In this case you can set a breakpoint like that:

    Var.Break.Set myFunc /VarReadWrite myFunc\myVar
    

    or

    Var.Break.Set myFunc /RegisterReadWrite R1
    

    The second case assume that you know that your variable is located in core register R1.