我很难重新创建错误,我希望在python GDB script
检测到错误时使用 a 来停止进程(我将在调试中运行它,直到错误重新生成)。参考从 snprintf 中看到的杂散字符。
我目前制作了一个脚本,可以创建一个break-point
并行读取serial
终端以检测字符串中的错误。这是脚本:
from __future__ import print_function
import serial
import pandas as pd
import argparse
import io
import time
import ast
import sys
import signal
import threading
class Serial_Port():
def __init__(self, port_number, baud_rate):
self.ser = serial.Serial(port_number, baud_rate, timeout=10)
self.ser_io = io.TextIOWrapper(io.BufferedRWPair(self.ser, self.ser, 1), newline = '\r', line_buffering = True)
self.data_count = 0
self.data_times = 0
self.data_succ = 0
self.ifPrint = False
def read(self):
raw_line = self.ser_io.readline()
raw_line = raw_line.strip("\x00")
raw_line = raw_line.strip("\r\n")
if self.ifPrint == True:
fptr.write(raw_line)
else:
self.data_times +=1
# Print the current status of string test
to_write = "Loop: %d Read Count: %d Successful: %d" % (self.data_times, self.data_count, self.data_succ)
fptr.write(to_write)
fptr.write("\n")
if "[[" in raw_line:
self.data_count += 1
self.parse(raw_line)
#Flush all the data before returning
fptr.flush()
def parse(self, gnss):
try:
#if we are able to convert literal and a DF made
# we'll assume the data received is valid
gnss = ast.literal_eval(gnss)
df = pd.DataFrame(gnss)
self.data_succ += 1
except KeyboardInterrupt:
self.ser.flush()
self.ser.close()
sys.exit(0)
except:
fptr.write(gnss)
fptr.write("\n")
fptr.flush()
def close(self, frame, andsomething):
self.ser.flush()
self.ser.close()
sys.exit(0)
class DebugPrintingBreakpoint(gdb.Breakpoint):
debugging_IDs = frozenset({37, 153, 420})
def stop(self):
top = gdb.newest_frame()
someVector = top.read_var('aVectorVar')
# Access the begin() & end() pointer of std::vector in GNU Standard C++ lib
first = someVector['_M_impl']['_M_start']
last = someVector['_M_impl']['_M_finish']
values = []
while first != last:
values.append(int(first.dereference()['intID']))
first = first + 1
if not set(values) & debugging_IDs:
return False # skip: none of the items we're looking for can be found by ID in the vector on the stack
print("Found other accompanying IDs: {}".format(values))
return True # drop to gdb's prompt
class MyThread_serial(threading.Thread):
def run(self):
while True:
time.sleep(1)#sleep for 1s
serialPort.read()
def main():
ser_thread = MyThread_serial(name = "Thread-serial")
ser_thread.start()
gdb.execute("continue")
if __name__ == "__main__":
fptr = open("gdbOPs.txt", "a")
#connect to serial port
strPort = "/dev/ttyUSB0"
serialPort = Serial_Port(strPort, 9600)
#set signal interrupt to exit
signal.signal(signal.SIGINT, serialPort.close)
print('Press Ctrl+C to exit')
main()
背景
我有一个硬件正在发送格式的字符串
[[12.12345678,12.12345678],[12.12345678,12.12345678],...]
但有时字符串可能会出现类似于
[[12.12345678,12.12345678],[ 55.01 [12.12345678,12.12345678],...]
我从来没有能够重现这个错误,所以我决定编写一个脚本,等待看到错误,然后是interrupts
GDB。转储堆栈和所有相关的变量。
请参考原始C问题以了解更多错误
问题
- 如何创建动态中断?即,当看到失败的字符串时,我应该发送
Ctrl+C
以停止进程并控制提示。 - 我如何找到错误?人们通常使用这些测试方法吗?你能把它们链接起来吗?
更新
该错误已通过将sprintf
函数替换为不处理 64 位的函数(在 32 位 ARM 环境中,请参阅此问题)直接解决。请转到原始问题以查找使用的新功能。