24

I am using Visual Studio Code and when I debug (I am debugging C++ code compiled with Clang) I see only local variables. I do not see any global variables list.

How can I see all variables?

enter image description here

In this case I am inside a loop and I see only all the variables defined inside the loop, not the one defined outside.

0

4 Answers 4

23

You will need to manually add global variables to a watch window.

  1. Set a breakpoint
  2. Start debugging (Debug -> Start Debugging or F5)
  3. Open a Watch window (Debug -> Windows -> Watch -> Watch 1)
  4. Type in the name of the variable manually
Sign up to request clarification or add additional context in comments.

3 Comments

But I can not change the value, if it's in the Watch window...
apparently we can now set value by right clicking it and choosing "Set Value"
Manually type isn't a long term solution.
4

In Visual Studio Code you can just go to the Watch pannel int the debug menu and click on + , then type the name of the variable you want to watch. Hope it helps !

Comments

2

After you follow the stepes in Andrew L's answer you can modify the variable's value by the help of either:

1. Debug Console

enter image description here

2. Address-Of Operator

enter image description here

By prefixing the variable we want to watch with (&) we can now expand it and then right click on the dereferenced variable to set a value.

Comments

1

As stated in previous answers, instead of the "VARIABLES" subview, use the "WATCH" subview, press the "+" button, and type the name of the global variable you want to watch. Ex. my_global_variable, or if there are any namespaces involved, my_top_level_namespace::my_inner_namespace::my_global_variable.

If there are any unnamed namespaces involved, you might need to find out the mangled name of the variable and use that instead (not sure what else you would/could do, actually). One way if you're using gcc to find it is to dig through nm -nC path/to/my_executable_filename | less.

Note that if the global variable temporarily shows up in the VARIABLES subview at any point, you could also right click it there and select "Add to watch".

In the latest version of the C/C++ extension, editing global variables from that subview should also be supported (right click the variable and click "Set Value"), though at some point in the past it was broken, which could be worked around by writing the expression for the address of that variable (Ex. &my_variable), and then expanding that item in the subview to get to the value of the variable, and then right click and do "Set Value" there.

Comments

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.