set_record.py: Fixed input stream for Windows, omit null characters in string receive

This commit fixes two bugs, the first when recording
on Windows where the user is unable to stop the capture cleanly
and one regarding the VCD output being malformed due to null characters in strings.

Reading from the stdin buffer is generally better behaved on both platforms,
while not as performant. The user can now cancel the capture by terminating the source
program or issuing a keyboard interrupt.

The issue with string capture having trailing null characters is fixed,
which in the best case confuses GTKwave, or in the worst case segfaults it.
This commit is contained in:
Dominic Höglinger 2025-05-14 20:36:49 +02:00
parent 5c3473ff1f
commit a8c6119b56

View File

@ -261,7 +261,6 @@ class Filter:
if tagcode not in self.TAGCODE_LUT: if tagcode not in self.TAGCODE_LUT:
self.packets_dropped += 1 self.packets_dropped += 1
print("LUT ERR", tagcode)
return return
tag = self.TAGCODE_LUT[tagcode] tag = self.TAGCODE_LUT[tagcode]
@ -413,11 +412,13 @@ class VcdSink:
timestamp = self.timestamp timestamp = self.timestamp
# unpack # unpack
for i in range(0,4): for i in range(0,4):
char = chr(value >> (i*8) & 0xFF) char = value >> (i*8) & 0xFF
self.strings[tag][1] += char if char != 0:
self.strings[tag][1] += chr(char)
# sub of 1 indicates end of string # sub of 1 indicates end of string
if sub == 1: if sub == 1:
try: try:
string = self.strings[tag][1]
#print(f"### {timestamp:012X} : {self.varnames[tag]} <= \"{self.strings[tag][1]}\"", flush=True) #print(f"### {timestamp:012X} : {self.varnames[tag]} <= \"{self.strings[tag][1]}\"", flush=True)
self.writer.change(self.strings[tag][0], timestamp, self.strings[tag][1]) self.writer.change(self.strings[tag][0], timestamp, self.strings[tag][1])
except ValueError: except ValueError:
@ -515,9 +516,9 @@ def main():
print(" === BEGIN NOISE ===") print(" === BEGIN NOISE ===")
try: try:
while True: for bstr in sys.stdin.buffer:
for b in sys.stdin.buffer.read(1): for b in bstr:
packet_filter.process(b) petf.process(b)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass