Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to watch variables at runtime?

I know the basics of debugging, and I know I can add watches to a variable that stop the program's execution on a given condition. But I didn't want to stop the program every time I need to see the value of a variable. Neither I want to log the value of every relevant variable into logcat... I only wanted to see their values like I do at breakpoints, only in runtime.

I'm programming Android, in Android Studio.

Thanks for the help!

like image 354
Ortiz Avatar asked Oct 16 '15 14:10

Ortiz


People also ask

How can you examine a variable while executing code?

Hover over a variable to see its value. The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.

How do I view local variables in Visual Studio?

To open the Locals window, while debugging, select Debug > Windows > Locals, or press Alt+4. This topic applies to Visual Studio on Windows.


2 Answers

When your program has stopped on a breakpoint click the icon at the far right of the debugger menu (see image below). You can type in methods or variable names into this window and see what they would be.

enter image description here

You can type any expression you like (as long as it is within the scope of where you broke your code) and input any hard-coded values or objects all without re-running your project.

enter image description here

To add a variable to your watch list

Start by putting a break point in the class where you'd want to watch a specific variable. Run the code and once it hits your breakpoint from the Variables window frame you should see all of the variables that are accessible. Simply choose the one you'd want to watch and then right click and choose "Add to watches" from the drop-down.

enter image description here

Keep debugging and you should see the variable from the Watches window frame update when appropriate based on your code.

enter image description here

like image 187
vguzzi Avatar answered Oct 08 '22 19:10

vguzzi


YES you can!

According to the Android Dev Summit '19, you can easily do that by disabling the Suspended flag in your breakpoint.

Then you can evaluate a log message to the console every time it gets the breakpoint, without suspending!

enter image description here

As you can see, my app fires a log to the console every time it gets to my breakpoint.

In other words, you can view variable changes at run time!

like image 39
genericUser Avatar answered Oct 08 '22 19:10

genericUser