Watching Static Variables

Debugging static variables, especially ‘static locals’ is sometimes challenging. Especially ‘static local’ debugging depends on the compiler capability how they are encoded in to the object file. I have found out that at least with CodeWarrior for MCU and ARM/Kinetis this works straight forward. Only ‘Watch Expressions’ need special attention.

I’m using following piece of source code to illustrate this:

static int static_global = 0;

static portTASK_FUNCTION(Task1, pvParameters) {
  static int static_local = 0;
  (void)pvParameters; /* parameter not used */
  for(;;) {
    static_global+=2;
    staticLocal++;
    LED1_Neg();
    FRTOS1_vTaskDelay(100/portTICK_RATE_MS);
  }
}

This code has a global static variable ‘static_global‘ and a static local variable named ‘static_local‘. ‘Static locals’ are special: they are like global variables, but with function scope.

Variables View

By default, the debugger will show me the global variables of the currently debugged source file. How to see my variables from another module? The answer is in Debugging Global Variables: I use the context menu ‘Add Global Variables…‘ in the Variables view, and I get a dialog where I can search and add my global variables:

Add Globals Dialog

Add Globals Dialog

This adds my two static variables to the view:

Variables View with added globals

Variables View with added globals

Expressions View

Eclipse has an Expressions view which allows me to enter an expression and to ‘watch’ it. However, if I try this it with my static variables, this does not work?

Static Global in Expression View

Static Global in Expression View

But there is a way to find out: I use the ‘Watch‘ context menu in the Variables view:

Watch Context Menu in Variables View

Watch Context Menu in Variables View

This will add it to the Expressions view:

Expressions View with new Expression

Expressions View with new Expression

Ahhhh! And here is the secret: I need to qualify my static variable to watch it in the Expressions view: FileName”::variableName”!

Now it is easy as well to use a bit more complex expression as taking the address of a static local variable:

Expressions View with Static Variables

Expressions View with Static Variables

Happy Watching 🙂

Advertisements

One thought on “Watching Static Variables

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s