Resolving variables with same name in C-SPY

Technical Note 45813

Targets:
Arm, RL78

Component:
Debugger

Updated:
10/20/2015 12:39 PM

Introduction

In an application where some variables have the same name, it might be difficult to set a breakpoint for those variables.

Discussion

Setting a breakpoint for a variable, using a C-SPY macro or in the Watch window, might fail. One possible reason for this is that C-SPY finds multiple variables with the same name. This technical note gives advice for this situation.

Solution

Use a specified reference that includes information about the location of the symbol. The syntax for a symbol location is:

module\function::symbol

Here follow five examples.

Example A

If the files main.c and test.c contain a static global (file global) variable called testvar, the syntax to use in the Watch window is:

main\testvar
test\testvar

Example B

If the file main.c contains a function called testfunc with a local variable called testvar, the syntax to add this variable to the Watch window is:

main\testfunc::testvar

As long as function names are unique, main\ should be redundant. When debugging with extra image, debug information function names might not be unique. In such cases, it makes sense to use unique module names.

Example C – usage in C-SPY macros

When using C-SPY macros (like __setSimBreak), the backslash ( \ ) needs to be protected with an extra backslash. For example:

__setSimBreak("main\\testvar", "R", "Do_the_testvar_Read_action()");

Example D – How it looks in C-SPY

In the archive, there are four pictures that illustrate how C-SPY handles the situation:

  • There are the main.c, f1.c, and f2.c source files.
  • In each file there is a static int it variable.
  • In the Watch window, the it variable is referenced as it, and as main\it, f1\it, and f2\it, where the it reference is a non-specified reference.
  • The execution steps into each function (and back to the main function) to show how the non-specified reference of it changes both in value and location in memory.

Example E – Syntax error

When names are identical, there is a risk for a syntax error. If there are:

  • A module named t0 (file name t0.c)
  • A typedef named t0
  • A variable named t

The reference t0\t will produce:

[syntax error, unexpected BACKSLASH, expecting COLON2]

Some alternatives (with different limitations/drawbacks) are:

  • Use the Statics window.
  • Right-click on the variable in the editor window to add it to a Watch window.
  • Accept that the variable is ambiguous/undefined when not in scope.
  • Rename either the module or the typedef.

Conclusion

By informing C-SPY about Module, Function and Symbol names, it becomes possible to reference one out of several variables using the same name.

In the Debugging guide there are more examples under the heading C-SPY EXPRESSIONS.

All product names are trademarks or registered trademarks of their respective owners.

Related Tech Notes

© IAR Systems 1995-2020 - All rights reserved.

We use cookies on this website to provide you with a better experience. You need to accept cookies to continue using this site. Cookies