Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Problem with breakpoint on plain old IF ( cond ) (Read 5291 times)
Evan
Junior Member
**
Offline


Posts: 27
Location: Northeast U.S.A.
Joined: Oct 25th, 2012
Problem with breakpoint on plain old IF ( cond )
Nov 5th, 2012 at 9:10pm
Print Post  
This is possibly related to the earlier post about problems with breakpoints in case statements.

If a breakpoint/tracepoint is set on an IF statement, the break/trace will only occur when the "if" condition is true.  Consider the code fragment

     if ( true ) {          // breakpoint set on this line
           x = 42 ;
     } // if ( true )

     if ( false ) {          // breakpoint set on this line, too
           y = 42 ;
     } // if ( false )

Identical breakpoints are set on the two source lines.  When execution passes through, the first breakpoint will fire but not the second.  Given that the condition in a typical if statement will sometimes be true and sometimes not, this is going to look like inconsistent behavior.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Problem with breakpoint on plain old IF ( cond )
Reply #1 - Nov 5th, 2012 at 10:50pm
Print Post  
Hello, is this just a simple example or the real code? 

If it is the real code then I would not expect the 2nd breakpoint to fire because it is always false so the IF will evaluate as not to be executed

This IF code will always evaluate FALSE because it is simply "false"

Code
Select All
  if ( false ) {          // breakpoint set on this line, too
           y = 42 ;
     } // if ( false )
 



This would work

Code
Select All
  if ( false==false ) {          // breakpoint set on this line, too
           y = 42 ;
     } // if ( false )
 



Am I missing the point?
  
Back to top
IP Logged
 
Evan
Junior Member
**
Offline


Posts: 27
Location: Northeast U.S.A.
Joined: Oct 25th, 2012
Re: Problem with breakpoint on plain old IF ( cond )
Reply #2 - Nov 6th, 2012 at 12:42am
Print Post  
Well, you're missing *my* point which may or may not be a correct interpretation of the "if" construct.

In my (admittedly old fashioned) view, an "if" statement consists of two parts:  A conditional test and a body.  The outcome of the conditional test determines whether the body of the "if" gets executed.  I therefore consider the conditional test and the body to be two separate entities, and I'd like to be able to set a breakpoint on one or the other.

As for your question about "real code", the fragment I supplied was what I added to my app upon becoming suspicious about why I wasn't getting any hits.  The original code looked more like

    if ( count > 50 ) {
        call_some_function( ) ;
    }

I had the breakpoint set on the line containing the "if ( count > 50 ) {".  As long as count was less than 50, which was most of the time, the breakpoint was not firing and I spent a couple hours trying to figure out why.

Perhaps the problem is that breakpoints are set on a "line", which leaves open the question of whether an "if" statement is one line or several.  I'm an old assembler programmer so I would expect the compiler generated code for the above to look something like

    mov reg, count
    sub reg, 50
    blt label
    callsub call_some_function
label:

I thought I was putting the breakpoint on the 'instruction' at the beginning of the test, i.e. "mov reg, count", not on the call to call_some_function.

Now, another legal expression of the "if" construct is 

    if ( cond ) statement ;

or even

   if ( cond ) { statement1 ; statement2 ; ... }

In these cases, it's far less obvious whether a breakpoint set on the source code line is for the conditional test or the statement 'block'.  Moreover, it may be impossible for your debug code injector to deal with this situation in the way I would like.

Is it?

I haven't tried this in real world C, at least not recently, but I will and I'll be very much surprised if hitting a VS breakpoint set on the "if" depends on the outcome of the conditional test.  I don't accept existing C/C++/C# implementations to be a binding precedent, but they will set expectations.

Or maybe not.  As you have pointed out, MCU applications are fundamentally different than sorting mailing lists for "Teen Beat" magazine.  There are at least some, and possibly many cases in which we simply have to explain what VM/DT does and why.

Anyway, I'll try it and report back.

  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Problem with breakpoint on plain old IF ( cond )
Reply #3 - Nov 6th, 2012 at 1:05am
Print Post  
Ahaha. I see. I didn't realize that it wasn't clear that this is a difference between this "soft debugger" and a hardware debugger (at the moment)... 

Yes vm puts the injected code "after" the end of the code that executes for a line.

If the code is "abc=5;" then vm changes it to "abc=5;Serial.println("some debug notification")"

I'm open for a better way to work but when navigating function names, comments and other stuff it seemed easier to add the injected code to the end of a line. 

However there was one important consideration for many users, when we stop at a breakpoint we might want to see the value of a variable such as "abc". If the line of code is "abc=random(100);" then when the debugger breaks users will expect to see the current value of variable "abc". Keeping in mind that we can not step each line of code with this debugger (we can only step between breakpoints). So do you place the breakpoint "on" or "after" the code that changes the value of "abc"? 

We also have to consider what happens if users add a breakpoint to a function () name. This was another big consideration. If you add a breakpoint to "void loop()" where would you expect the injected code to be placed? Certainly would not be before the void loop() because that would be invalid.

Summary

This beta version works one way, the same way in all instances. So far it's worked okay but your questions are both useful and valid.

Maybe vm should become more intelligent and work a different way depending on location of the breakpoint? It would be useful to have yourself and others give this some thought and help to find the "best possible" solution.

It is certainly true that an injected debug system has different considerations to a hardware debugger but this area is open to discussion and its appreciated!


One example of what vm does in an attempt to make things work

Vm does some work to detect two line if statements that do not have braces {}. It actually inserts the braces and then ensures the injected breakpoint code is after the opening brace. 

NB (for anyone else reading this): All this happens in the temp copy during debug compile so the real source is never affected
« Last Edit: Nov 6th, 2012 at 1:15am by Tim@Visual Micro »  
Back to top
IP Logged
 
Evan
Junior Member
**
Offline


Posts: 27
Location: Northeast U.S.A.
Joined: Oct 25th, 2012
Re: Problem with breakpoint on plain old IF ( cond )
Reply #4 - Nov 6th, 2012 at 2:40pm
Print Post  
I agree that my problem with breakpoints on an "if" statement is a failure to consider the architectural limitations of injected code.  This issue is closed with RTFM.

I have a half-baked idea for a pre-pend option on the injected breakpoints that I'll post in a different thread.

Sorry for the confused bother.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint