Search code examples
pythonlldb

how do I use pyplot to display data when a breakpoint is hit in LLDB


I successfully can run a python script when a breakpoint is hit. As explained here I make a python module implementing my function with this signature:

breakpoint_function (frame, bp_loc, dict)

then I bring the module into lldb by doing:

(lldb) command script import "path to my .py file"

then I make a breakpoint and add my function to it like this:

(lldb) br com a -F MyModule.breakpoint_function

my module looks like this

import matplotlib.pyplot as plt                                                                                                                                                                                                
import numpy as np  


def bp(frame, bp_loc, dict):

    a       = frame.FindVariable ("myFloatArray")

    for i in range(128):
        x[i]=  float(a.GetChildAtIndex(i,1,1).GetValue())


    # plt.ion()
    # plt.show()                                                                                                                                                                                                                
    plt.plot(x)
    plt.show()                                                                                                                                                                                                                
    #plt.pause(10.001)


return 0

using just plt.plot(x) and plt.show() causes lldbdb to crash with the begining of the error log looking like this:

2016-12-22 21:26:51.192 lldb[32192:2025199] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1256.1/Misc.subproj/NSUndoManager.m:359 2016-12-22 21:26:51.192 lldb[32192:2025199] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread. 2016-12-22 21:26:51.272 lldb[32192:2025199] ( 0 CoreFoundation 0x00007fff8da54ae2 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff90f7173c objc_exception_throw + 48 2 CoreFoundation 0x00007fff8da548ba +[NSException raise:format:arguments:] + 106 3 Foundation 0x00007fff9145c88c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 Foundation 0x00007fff913e24c1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170 5 AppKit 0x00007fff9bfd306a -[NSApplication run] + 844 6 _macosx.so 0x00000001256c931e init_macosx + 32153 7 Python 0x000000010e75aa90 PyEval_EvalFrameEx + 13533 8 Python 0x000000010e7573c1 PyEval_EvalCodeEx + 1583 9 Python 0x000000010e75d4ae _PyEval_SliceIndex + 342 10 Python 0x000000010e75a30c PyEval_EvalFrameEx + 11609

When I call plt.ion() before plt.plot(x) first then nothing displays and i can continue stepping through lldb. Then when I quit lldb the plots actually show for a split second.

I tried changing backend in matplotlibrc with no luck Also tried plt.show(block = True) (cause crashe with error log) Any hint would be welcome.


Solution

  • I couldn't get plt.show() to work either (in a lldb breakpoint). But the following workaround works for me and displays matplotlib images in a lldb breakpoint (with Xcode 7, lldb-340.4.70):

    def bp1(frame, bp_loc, dict):
        """Use matplotlib in an Xcode breakpoint. 
        Add with: br com add -F cmd.bp1
        """
        import matplotlib.pyplot as plt, numpy as np, sys
        # The following addition to the path may not be required in your case
        sys.path.append("/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages")
        from PIL import Image
    
        print ("hit bp1")
        # Some example plot (sine curve)
        fig = plt.figure()
        Fs = 8000
        f = 5
        sample = 8000
        x = np.arange(sample)
        y = np.sin(2 * np.pi * f * x / Fs)
        fig.gca().plot(x, y)
    
        # Save figure to image
        fileName = "/Users/<username>/tempwork/lldb_pic.png"
        fig.savefig(fileName)
    
        # Open image from filesystem and show with PIL
        img = Image.open(fileName)
        img.show()
        return True  # False = continue execution, True = stop execution in debugger (lldb prompt)