2

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?

6
  • This is perhaps an X-Y problem. You should perhaps post your macro file code and have that fixed rather than ask for a "from-scratch" solution. Commented Jan 26, 2021 at 18:12
  • Not really an X-Y problem nor a solution "from-scratch", are you familiar with the C-SPY macros? I would have preferred referring to the Debugging Guide document provided by the IAR but the problem is I am not sure whether it is ok or not (from the copyright/license point of view) I could even provide an example of myself, however, then I would be explaining what works which is different from what I want. (Im not get into that right now without an example maybe it’s not straight forward but in short __setCodeBreak() returns an id and then __clearBreak() uses that id to remove the breakpoint ). Commented Jan 27, 2021 at 12:49
  • I would expect not more than one line for it, however, I am starting to think it’s not possible at all considering how these functions are used in the Debugging Guide document, so I might delete the question or put an answer myself that it’s not possible and put he example which does the job differently using the mentioned functions. Do you know whether I can refer to this document or not? the doc cannot be found on their website. Commented Jan 27, 2021 at 12:49
  • I did draft an answer, but on re-reading your requirements, it seemed not to address them in the manner you want, and may be a duplicate of what you already have (which is why it would be a good idea to post that). I will undelete it so you can see it, but don't be surprised if it is not what you are after (or down-vote it - I am aware of its probable deficiencies but you might comment on it as to whether there is anything useful in there). Commented Jan 27, 2021 at 13:14
  • I think fair-use rules apply. I doubt IAR would object to free publicity and the manuals are available from their site in any case. Attribution is a good idea. As you can see from my answer I have used an image from their site - no one has sued me yet; but I am not a lawyer. Commented Jan 27, 2021 at 13:19

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank for your time, this is almost the same way that I mentioned in the question (I provided an example), the problem with that is it is dependent to another macro so it increases the number of breakpoints that are needed, secondly the line number should be hard coded in that.
@Kepler indeed - that is why I originally deleted it, it clearly did not resolve your problem. What it really needs is 1) the macro language to support static variable semantics, and 2) a means of assigning a user defined name or handle to a breakpoint that you could refer to in __clearBreak(). Without that the first method has some merit - means placing code at the point you want to break, but it is small, and if you don't set the breakpoint if has no impact on the code. Note that the BKPT instruction method is best guarded by a test to see if the debugger is attached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.