-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdefmt-rtt-gdb.py
83 lines (63 loc) · 1.98 KB
/
defmt-rtt-gdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! python
#
# Author: Gaute Hope ([email protected]) (c) 2021-11-16
#
# Requirements:
# * nc
# * defmt-print
#
# This command attached to JLinkGDBServer for RTT output and passes that through defmt-print.
#
# Source this file in gdb or .gdbinit and run "defmt-rtt", continue your program with 'c &'.
from threading import Thread
from subprocess import Popen, PIPE, DEVNULL
import os
class DefmtRtt(gdb.Command):
rtt_th = None
def __init__(self):
super().__init__("defmt-rtt", gdb.COMMAND_USER)
def get_exe(self):
return gdb.current_progspace().filename
def invoke(self, arg, from_tty):
self.dont_repeat()
exe = self.get_exe()
gdb.events.exited.connect(self.stop)
# gdb.events.stop.connect(self.stop)
gdb.events.cont.connect(self.cont)
self.rtt_th = DefmtPrinter(exe)
self.rtt_th.start()
def stop(self, event):
self.rtt_th.stop()
def cont(self, event):
if not self.rtt_th.is_alive():
self.rtt_th = DefmtPrinter(self.get_exe())
self.rtt_th.start()
class DefmtPrinter(Thread):
exe = None
prc1 = None
prc2 = None
def __init__(self, exe):
super().__init__()
self.exe = exe
def run(self):
print(f"Attaching to GDB server for RTT | defmt output (exe: {self.exe}..")
self.prc1 = Popen(["nc", "localhost", "19021"],
bufsize=0,
stdin=DEVNULL,
stdout=PIPE)
# read jlink header
for _ in range(0, 3):
self.prc1.stdout.readline()
self.prc2 = Popen(["defmt-print", "-e", self.exe],
stdin=self.prc1.stdout,
bufsize=0)
self.prc2.wait()
self.prc1.wait()
def stop(self):
# stop the defmt thread
self.prc2.kill()
self.prc1.kill()
self.prc2.wait()
self.prc1.wait()
# Instantiate so that gdb knows about us.
DefmtRtt()