There are two main uses of watch expressions:
- Evaluate an arbitrary expression in the current debug context (i.e. the line where you're suspended)
- Quickly view select variables
For simple variable expressions, you don't need to manually write these. If you head on over to the Variables view, you can right-click on a variable and select Watch. This will automatically build a watch expression, which is especially useful for deeply-nested fields (Tip: you can select multiple variables at once to "Watch").
Here's a bunch of expressions to show how they look, including some errors:
5 additional things you should know about watch expressions:
- The Eclipse framework will run these evaluations a lot. Every time you suspend, step, or click on a stack frame, they're run. This can hurt performance if you have too many expressions enabled.
- Watch expressions are only run when the Expressions view is visible. Don't want them to run right now? Either close or hide the view. You can also disable the expressions and leave the view open (right-click an expression and select Disable).
- Eclipse will sometimes invoke an expression twice - for example, when you first create an expression, I've seen it get run twice. Due to this, be very careful when using expressions that have side effects (like running a commit on a database). I don't recommend such expressions from this view, at least not until Eclipse stops executing them twice in certain conditions.
- If your expression resolves to a variable, you can change its value by right-clicking on it and selecting Change Value. This can also be done for child variables (e.g. if your expression resolved to a record, you can expand it to see the record's fields and can change their values).
- If your expression contains two statements then only the first will be run. For example, i += 10; writestdout(i); will only run the first part that adds 10 to a variable i - the print statement is not run. If you want both parts run, one trick is to put the statements inside an if statement: if (true) i += 10; writestdout(i); end. Why does this work? Because the if statement is technically a single statement. In most cases, you can just have two separate expressions.
In case you missed my previous blogs on debugging, see Viewing variables during a TUI converse and Conditional breakpoints.