This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCSv5.1 & MSP430.DLLv3: Stepping points to wrong line in code

Other Parts Discussed in Thread: MSP430F5418A, TEST2

So I was quite dazzled as I tried a new function in my code. While stepping through it always pointed to the same return statement, but the actual value returned was that from a different return statement.

The code is working fine, I tested it with the CCSv4.2 and the old v2-dll - there the stepping is showing to the correct line all the time. So I guess there is a bug in either the MSP430.DLLv3 or CCSv5.1.0.09000 - right now I can't tell which of those it is.

I've tried to create a minimal example to recreate the problem:

#include <msp430f5418a.h>

typedef signed int sFix0_15;

sFix0_15 add(sFix0_15 a, sFix0_15 b);

void main(void) {
       WDTCTL = WDTPW | WDTHOLD;

       volatile sFix0_15 test1 = add(0x2000, 0x2000);
       volatile sFix0_15 test2 = add(0xC000, 0xC000);
       volatile sFix0_15 test3 = add(0x4000, 0x4000);
       volatile sFix0_15 test4 = add(0x8000, 0x8000);
}

sFix0_15 add(sFix0_15 a, sFix0_15 b)
{
       sFix0_15 result = a + b;
       volatile int SR = _get_SR_register();
       if ((SR & V))
       {
             if (SR & N)
             {
                    return 0x7FFF;
             }
             else
             {
                    return 0x8000;
             }
       }
       return result;
}

Don't be to concerned about the typedef, I'm working with fixed point numbers, and the function is basically an add function with saturation behavior. Now the results are computed fine (also with CCSv5.1). The first two statements give no saturation, results being 0x4000 and 0x8000, the last two give positive and negative saturation so the results are 0x7FFF and 0x8000. If I step through that code with CCSv5.1 the marker jumps always to "return 0x7FFF;". I have switched the optimization level to the empty field, so I hopefully have not a problem due to optimization here.

Trying the same code with CCSv4.2, everything was fine, the marker went to the right return statements.

  • Bernhard,

    I can reproduce the behavior you are reporting. As you probably noticed, the MSP430 compiler tools versions changed between CCS 4.2 and CCS 5.1 (the latest version in CCS 5.1 is code generation tools v4.0.0). The difference seems to be coming from the compiler. I can simply change the compiler version in CCS 5.1 to an older version (I used 3.3.3) and observe the different/expected behavior.

    To narrow it down, I set the project options to no optimization and enabled the option to keep assembly source file and generate C source interlisted assembly and compared the assembly outputs(.asm) in the two cases.

    It seems that with compiler 4.0.0, it always enables a level 0 optimization (even when no optimization is selected in project settings), and with that optimization, the C source line correlation is shifted. Notice in the assembly code snippet below that the source lines 21 and 23 do not have any assembly code associated with it, instead it is all associated with line 25, which is why you always end up at line 25 in the debugger. However the code itself executes correctly. This, in itself, is not a bug as it is known that optimization can affect debugging ability. However, I do think there is a bug in the 4.0.0 compiler in that it is generating code for -o0 even though no optimization is selected. I will file a bug for this and post the bug # here.

    ;----------------------------------------------------------------------
    ;  20 | volatile int SR = _get_SR_register();                                 
    ;  21 | if ((SR & V))                                                         
    ;  23 |       if (SR & N)                                                     
    ;----------------------------------------------------------------------
            MOV.W     SR,0(SP)              ; [] |20|
     .dwpsn file "main.c",line 25,column 21,is_stmt
    ;----------------------------------------------------------------------
    ;  25 | return 0x7FFF;                                                        
    ;  27 | else                                                                  
    ;  29 | return 0x8000;                                                        
    ;  32 | return result;                                                        
    ;----------------------------------------------------------------------
            BIT.W     #256,0(SP)            ; [] |25|
            JEQ       $C$L2                 ; [] |25|
                                              ; [] |25|
    ;* --------------------------------------------------------------------------*
            BIT.W     #4,0(SP)              ; [] |25|
            JEQ       $C$L1                 ; [] |25|
                                              ; [] |25|
    ;* --------------------------------------------------------------------------*
            MOV.W     #32767,r12            ; [] |25|
            JMP       $C$L2                 ; [] |25|
                                              ; [] |25|
    ;* --------------------------------------------------------------------------*
    $C$L1:   
            MOV.W     #32768,r12            ; [] |25|
    ;* --------------------------------------------------------------------------*

  • The bug tracking # is SDSCM00042383. You may track the status of this bug using the SDOWP link in my signature.

  • Thank you very much for your fast analysis and detailed description of what's happening, this could also explain a problem a colleague is experiencing with CCSv5.

    I'm aware that the assembler -> code line associations get messed up if optimization is selected as some statements are just optimized away or turned around for efficiency. I also noticed that -o0 is now the standard for a new project even for the debug build, which made me aware that there could be trouble coming from that direction, so I turned it off, but well I thought that would really turn it off.

    Thanks again.

  • Okay I see the bug you posted was declined stating that

    This level of optimization (equivalent to -o0) should not interfere with debug and provides performance more in line with expectations when not optimizing.

    Now, I'm not sure if like in my example this optimization is not interfering with debugging, I mean stepping through the code gave not the expected results.

    Well at least I now know how to turn it off completely using the --no_high_level_opt (Disable all high level optimizations under Advanced Options, Advanced Optimization), and indeed then it behaves like it should.

  • The key words in the bug decline statement is 'should not'. It does not read 'does not'.
    If things always were as they should be, we wouldn't need a debugger at all. Or a bug report tracker. :)

    Maybe re-posting the bug will help, together with a note that despite of a previously comment on the same bug the optimization DOES reproduceably interfere with the debugger.
    Including a demo code.

    I often see bug reports being closed with a "it works for me so there is no bug" comment. Not (only) here, but for online games or open source projects too. Being persistent sometimes helps.