簡體   English   中英

在 LLDB 中遇到斷點時如何使用 pyplot 顯示數據

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

當遇到斷點時,我可以成功運行 python 腳本。 正如這里所解釋的我使用這個簽名制作了一個實現我的功能的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/Caches/com.apple.xbs/Sources/Foundation/Foundation-1256.subjpro/ /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提高:格式:參數:] + 106 3基金會0x00007fff9145c88c - [NSAssertionHandler handleFailureInMethod:對象:文件:LINENUMBER:描述:] + 198 4基金會0x00007fff913e24c1 + [NSUndoManager(NSPrivate)_endTopLevelGroupings] + 170 5了AppKit 0x00007fff9bfd306a - [運行的NSApplication] + 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; 它創建了一個新進程。 例如“繪制 x/2048fw 緩沖區”

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM