在LLDB中遇到断点时,如何使用pyplot显示数据

时间:2016-12-23 05:46:06

标签: python lldb

当遇到断点时,我成功运行了一个python脚本。正如here所解释的,我创建了一个python模块,用这个签名实现我的函数:

breakpoint_function (frame, bp_loc, dict)

然后我通过执行以下操作将模块带入lldb:

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

然后我创建一个断点并将我的函数添加到它中:

(lldb) br com a -F MyModule.breakpoint_function

我的模块看起来像这样

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

仅使用plt.plot(x)和plt.show()会导致lldbdb崩溃,错误日志的开头如下所示:

  

2016-12-22 21:26:51.192 lldb [32192:2025199] *** + [NSUndoManager _endTopLevelGroupings]中的断言失败,/ Library / Cache / com.apple.xbs / Sources / Foundation / Foundation1256.1 / Misc.subproj / NSUndoManager.m:359   2016-12-22 21:26:51.192 lldb [32192:2025199] + [NSUndoManager(NSInternal)_endTopLevelGroupings]只能在主线程上调用。   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基础0x00007fff9145c88c - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198       4基础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

当我先在plt.plot(x)之前调用plt.ion()然后没有任何显示,我可以继续单步执行lldb。然后,当我退出lldb时,这些情节实际上显示了一瞬间。

我尝试在matplotlibrc中更改后端而没有运气 还尝试了plt.show(block = True)(导致错误日志崩溃) 任何提示都会受到欢迎。

1 个答案:

答案 0 :(得分:1)

我无法使plt.show()工作(在lldb断点中)。但以下解决方法适用于我并在lldb断点中显示matplotlib图像(使用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)