optimised away during debugging

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

 

Why is that some values are optmised away during debugging? what does that mean?

 

 

This topic has a solution.
Last Edited: Tue. Aug 8, 2017 - 05:59 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nothing is optimised during debugging.

 

Optimisation occurs during compiling (and some as part of linking).

 

This has nothing to do with AVR - it applies to compilers in general.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Aug 8, 2017 - 02:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote this tutorial to explain what's going on and how to work around it:

 

https://www.avrfreaks.net/forum/t...

 

Note that since I wrote that the compiler has added a -Og option that could prove useful.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It means that the variables are not used anywhere or not necessary. Set, perhaps, but not used. 

 

For example:

uint8_t x;
x = 5;
uint8_t y = x;

If "X" is not used elsewhere, this will probably be optimized into

 

uint8_t y = 5;

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
this will probably be optimized into
It's worse than that. Even if 'y' is now used later (and for something writing to a volatile target) then y itself may not exist as a variable in RAM (and watchable) but the compiler may just choose to hold that 5 value in one of the R0..R31 registers which the debugger may not be able to show. So you may still see that "y optimised away" if you try to view it.

 

Making it "volatile" means it will have to exist even if that makes inefficient code. The other option is to reduce the optimisation level. As I say -Og may help.

 

Do NOT be tempted to "fix" this by using -O0 though!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To easily switch for between some settings you could add a macro like:

#if DEBUG
#define DBG volatile
#else
#define DBG     /* Define into empty statement. */
#endif

DBG uint8_t Asdf;

 

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That was intended as a case example, not an exhaustive catalog. But, the subsequent comments should be instructive to the OP and others.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net