0

I'm using lldb to debug a C++ program which has the following code

// Brute force to inner points
  for (int i = 0; i < y_strip.size(); i++) {
    for (int j = i + 1; j < y_strip.size(); j++) {      
      // If the vertical distance between the points is greater
      // than delta, break the loop
      if (abs(y_strip[i].y - y_strip[j].y) > delta) {
        break;
      } else {
        mid_min_distance = minimal_distance(y_strip[i], y_strip[j]);
        mid_min = min(mid_min_distance, mid_min);
      }
    }
  }

and I want to come up with a way of stopping the program if the difference between j and i is greater than 10. How can I do this?

Compiling the program with clang++ -Wall -g closest.cpp -o closest

mcansado
  • 2,026
  • 4
  • 25
  • 39

1 Answers1

0
(lldb) break set -p "If the vertical distance between" -c "abs(i - j) > 10"

will do the trick. The -c option here is the breakpoint condition; if that expression evaluates to true, the program will stop at the breakpoint, otherwise the program will keep running. Note that you are still stopping to check the condition every time round the two loops, and this condition runs a function, so if the code you are checking is going to get run a lot, evaluating the condition might get slow. In that case it might be worth rewriting the condition to something like:

i - j > 10 || j - i > 10

since a simple expression with no function calls can be emulated in the debugger w/o having to call code in the debugee to check the condition.

And if this is code that you can modify, and it is going to get called a whole lot so that stopping each time around the loop to check the condition would be a big performance hit, then you can just put the check in your code:

if (abs(i - j) > 10)
    printf ("Set a breakpoint here.\n");

rebuild it, and then just do:

(lldb) break set -p "Set a breakpoint here"

Note, the -p breakpoint option sets a breakpoint on the line whose source matches the pattern given in the option argument. You can also use file & line breakpoints:

(lldb) break set -f closest.cpp -l <whatever>

I like the pattern one because it moves with the intended location as the code is edited and it means I don't have to count lines.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • Tried adapting this to just watch a variable and see if it's ever == 0 so I did `(lldb) break set -p "Watch delta" -c "delta == 0"` but it returns `Breakpoint 2: no locations (pending).` `WARNING: Unable to resolve breakpoint to any actual locations.` even though I'm compiling with `clang++ -Wall -g closest.cpp -o closest` like [here](http://stackoverflow.com/questions/31122871/lldb-error-unable-to-resolve-breakpoint-to-any-actual-locations). Any idea why this is? – mcansado Mar 21 '17 at 08:20
  • Without seeing the source file I can't really tell. Does a file & line breakpoint on the line the pattern is not matching work? Do that with `break set -f closest.cpp -l whatever` on the equivalent line work? – Jim Ingham Mar 21 '17 at 21:10