How to automatically disable/delete a breakpoint in IAR after it gets hit?

Viewed 335

Is is possible to set a breakpoint in IAR embedded workbench for ARM IDE, in a way that after it hits once it gets deleted/disbled automatically?

I know already a way to do that using C-SPY macros and __setCodeBreak() and then __clearBreak() functions. The problem though is I have to set my desired breakpoint using another breakpoint and a C-SPY macro in which __setCodeBreak() sets my desired breakpoint after that I can use a C-SPY macro for it, at the end of which __clearBreak() removes the breakpoint. However, what I want is to be able to set a breakpoint directly in the IAR IDE and link it to a C-SPY macro and then the breakpoint gets deleted/disbled when is hit (I cannot make __clearBreak() work in this second way, standalone)

Here is an example of what I explained above:

__var bp2;

// This macro sets a new breakpoint (bp2), and is linked to another breakpoint (let's say bp1 which is set in the IDE)
Set_bp2()  
{
bp2 = __setCodeBreak("{C:\\file.c}.80.1", 0, "ActionMacro()", "TRUE", "");
}

//This macro gets executed when the new breakpoint (bp2) is hit and then removes this breakpoint (bp2)
ActionMacro()  
{
__message "bp2 is hit ...";
__clearBreak(bp2);
}

So, the __clearBreak() manages to clear the breakpoint using the return code of the __setCodeBreak(), however, I would like to have a macro which independently of another macro is able to clear the breakpoint which is linked to, something like below:

ActionAndClear()  // This macro is linked to a breakpoint which is set in the IDE
{
__message "breakpointX is hit ...";
__clearBreak(<UNKNOWN>);
}

Is there any value for the UNKNOWN, which does the trick?

1 Answers

A number if solutions are possible:

A crude solution is to use a condition break point and a "helper" variable:

bool break_here = true ;

...
break_here = false ; // Place condition break on this line

If you set the condition break to break when break_here == true, the it will break the first time, set the flag to false and never break again. However condition breaks take time to stop and evaluate the condition, so this may not be suitable.

Also crude but without the performance issue, make breakpoint conditional directly in the code:

static bool break_here = true ;
if( break_here )
{
    __breakpoint(0) ;
    break_here = false ;
}

The above methods will work with any debuggers. C-Spy allows you to invoke macros on under specific conditions such as debugger start, processor reset or breakpoints. Be aware that I do not use IAR EWB or C-Spy, the macro solution described here may need some tweaking, I am working from documentation. Feel free to fix it if you spot any errors, but the take-home is "user a C-Spy macro" one way or another.

So you might have a macro file:

__var break_count ;
__var break_handle ;

execUserReset()
{
    break_count = 1 ;
    break_handle = __setCodeBreak( "{myfile.c}.82.1", 0, "onBreakOnce()", "TRUE", "" ) ;
}

onBreakOnce()
{
    break_count-- ;
    if( break_count == 0 )
    {
        __clearBreak( break_handle ) ;
    }
}

That will set up a breakpoint every time you reset, and delete the breakpoint when it is first encountered.

Change break_count if you want to break more times before deletion. This has the opposite effect of the "skip-count" argument. A skip count can be set in the __setCodeBreak() call so you could skip a number of passes, then break for a number of passes then delete the break-point ... if your really wanted to!

You set up the macro file in EWB as follows:

enter image description here

Image attribution https://www.iar.com/support/resources/articles/introduction-to-debugging-with-c-spy-macros/

Related