0

I want to add watch or print a specific global variable defined and declared in a particular file of a large code-base. So, it is possible that multiple variables of same name with different Linkageexists in the code-base. I know how to add watch or print a local and global variable in a single file project using GDB.

I am using gdb on my Ubuntu machine. I tried google but couldn't get an answer.

1 Answer 1

2

The answer is documented on this page: https://sourceware.org/gdb/current/onlinedocs/gdb/Variables.html#Variables

In summary you need to use the following syntax to select a particular variable by file:

(gdb) print 'file.c'::variable

And this can also be used for watchpoints too:

(gdb) watch -l 'file.c'::variable
Sign up to request clarification or add additional context in comments.

2 Comments

What to do if it says No symbol "file.c" in current context.? Also, there can be several files named file.c in different directories, how to choose specific one? (In my use case it's actually file.go, but my issue can be language-agnostic)
For golang correct syntax was print 'my_app/my_lib.my_var' in my case(it was difficult to guess and impossible to google)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.