gpt4 book ai didi

python - 在 LLDB 中命中断点时如何使用 pyplot 显示数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:04 26 4
gpt4 key购买 nike

当遇到断点时,我可以成功运行 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)

关于python - 在 LLDB 中命中断点时如何使用 pyplot 显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41295819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com