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

how do I use pyplot to display data when a breakpoint is hit in 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] *** 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

当我先在 plt.plot(x) 之前调用 plt.ion() 时,什么都不显示,我可以继续单步执行 lldb。然后,当我退出 lldb 时,绘图实际上会显示一瞬间。

我尝试在 matplotlibrc 中更改后端但没有成功 还尝试了 plt.show(block = True) (导致错误日志崩溃) 欢迎任何提示。

我也无法使 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)

这是另一个解决方法,可以让您使用 pylab 的 UI;它创建了一个新过程。例如"plot x/2048fw buffer"

import lldb
import subprocess
import re
import io

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add -f plot.plot plot')

def plot(debugger, command, result, internal_dict):
    res = lldb.SBCommandReturnObject()
    interpreter = lldb.debugger.GetCommandInterpreter()
    interpreter.HandleCommand(command, res)
    if not res.Succeeded():
        print(res.GetError())
        return
    output = res.GetOutput()

    lines = output.splitlines()
    lines = '\n'.join(map(lambda x: re.sub(r'^.*: ', '', x), lines))

    cmd="""
import sys;
import pylab
import numpy
data = numpy.loadtxt(sys.stdin, dtype=numpy.float)
pylab.figure()
pylab.title("Plot")
pylab.plot(data)
pylab.xlim(0, data.size)
pylab.show()
"""

    proc = subprocess.Popen(["python", "-c", cmd], stdin=subprocess.PIPE, text=True)
    try:
        proc.communicate(lines, timeout=1)
    except subprocess.TimeoutExpired:
        return