Search code examples
c++exceptionclanglldbcgal

How to display CGAL exceptions in command line lldb


I usually debug my code by hand, but I am trying to use lldb to debug a CGAL project. Therefore, this is a newbie lldb question. The following code causes an exception (that is the expected behavior). When I compile and run the following code in Xcode (using os-x's built-in clang compiler)

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Boolean_set_operations_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_2<K> Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;

int main(){
    Polygon_2 p,q;
    Polygon_with_holes_2 r;

p.push_back(Point(0,0));
p.push_back(Point(1,0));
p.push_back(Point(1,1));

q.push_back(Point(0,0));
q.push_back(Point(0,1));
q.push_back(Point(1,1));

CGAL::join(p,q,r);

return 0;
}

I get the following information:

CGAL warning: check violation!
Expression : valid_orientation
File       : /opt/local/include/CGAL/Boolean_set_operations_2/Gps_polygon_validation.h
Line       : 310
Explanation: The polygon has a wrong orientation.
Refer to the bug-reporting instructions at http://www.cgal.org/bug_report.html
CGAL error: precondition violation!
Expression : is_valid_unknown_polygon(p, t)
File       : /opt/local/include/CGAL/General_polygon_set_on_surface_2.h
Line       : 45
Explanation: 
Refer to the bug-reporting instructions at http://www.cgal.org/bug_report.html
libc++abi.dylib: terminating with uncaught exception of type CGAL::Precondition_exception: CGAL ERROR: precondition violation!
Expr: is_valid_unknown_polygon(p, t)
File: /opt/local/include/CGAL/General_polygon_set_on_surface_2.h
Line: 45
(lldb) 

I would like to know how to get the same information from lldb on the command line.


Solution

  • I don't know quite what you want to get. You can get lldb to stop at the point where the exception is going to be thrown by setting a breakpoint on the exception throw:

    (lldb) break set -n __cxa_throw
    

    Then you will stop in the debugger at the point where the the exception is about to be delivered. It is often helpful to see the context for the exception. Note that if the library you are working with or your own code uses exceptions a lot it might get tedious to wade through all the unrelated hits... If you just want to see the backtrace, you can put a command on the breakpoint that prints the backtrace and continues. The last one printed will be the interesting one... Do this like:

    (lldb) br comm add
    Enter your debugger command(s).  Type 'DONE' to end.
    > bt 
    > continue 
    > DONE
    

    This adds a command to the last set breakpoint (specify the breakpoint number as an argument to the command if it wasn't the last one.)

    Note, it's most likely the error text is just consed up and printed out by the checker code before throwing the error. The debugger has no way of knowing this is a special thing, the best it can do is print it to the console along with all other text going to stderr...