# Resolving variables with same name in C-SPY

Arkitekturer:

Arm, RL78

Komponent:

debugger

2020-11-25 15:55

## 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

### 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\testvartest\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.

#### Det här innehållet finns tyvärr inte på svenska.

Vår webbplats finns främst på vårt koncernspråk engelska, förutom det innehåll för investerare som vi är lagstadgade att kommunicera på svenska. Vi rekommenderar att du besöker vår globala webbplats på engelska för att få en bättre upplevelse.

Vi stöder inte längre Internet Explorer. För att få bästa möjliga upplevelse av iar.com rekommenderar vi att du uppgraderar till en modern webbläsare som Chrome eller Edge.