Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a breakpoint in the middle of a line with multiple statements

I want to have a single line of code with an if and the result on the same line. Example

    if(_count > 0) return;

If I add a breakpoint on the line, it is set on the if(_count > 0):

Breakpoint on the if statement

but what I want is to have the breakpoint on the return; like so:

Breakpoint on return

Is this doable?

NOTE: The closest question I could find on SO was this (which is not the same thing).

like image 456
David Avatar asked Mar 22 '16 22:03

David


People also ask

How do you set a breakpoint in line?

To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main. cpp, you would type "break main.

What is a conditional breakpoint?

Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.

What is another way to set a breakpoint?

Set breakpoints in source code To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do you set a breakpoint within your code?

It's easy to set a breakpoint in Python code to i.e. inspect the contents of variables at a given line. Add import pdb; pdb. set_trace() at the corresponding line in the Python code and execute it. The execution will stop at the breakpoint.


2 Answers

Just click on a part of the line and press F9. This will set breakpoint in the middle of the line.

like image 155
mareko Avatar answered Sep 29 '22 08:09

mareko


EDIT

mareko posted a 3rd option that seems to work just fine and is much easier than messing around with all this line-and-character stuff. I'm going to leave this answer for completeness, but mareko's is much more practical in general.


Yes, but unfortunately, there is no drag-and-drop or anything like that. You have 2 options:

OPTION 1

Move your return to a new line:

enter image description here

obviously, you can leave it like that, but if you want to keep the return on the same line as the if, you can simply delete the new line and the whitespace between the if and the return -- the breakpoint should "stick" to the return as you move it around.

This is probably the easier way to do it unless you are currently debugging code that does not have edit-and-continue for whatever reason. In that case, you'll need option 2...


OPTION 2

Alternately, you can place the cursor just before the r in 'return' and then look in your status bar to see which character ("Ch") you are on. In my case, I'm on 20

enter image description here

now right click on the breakpoint and choose "Location..."

enter image description here

In the dialog box that pops up, set Character: to whatever the status bar was (20 in our example).

enter image description here

like image 45
David Avatar answered Sep 29 '22 08:09

David