-
Notifications
You must be signed in to change notification settings - Fork 0
/
memtrace_calliope_trns.py
157 lines (134 loc) · 5.28 KB
/
memtrace_calliope_trns.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# mbed debug trace transformer, handling memory trace information
# Author: Matthias L. Jugel (@thinkberg)
#
# Copyright (c) 2018 ubirch GmbH, All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied.
import re
import sys
from serial.tools.miniterm import Transform
def debug(s):
sys.stderr.write("D "+repr(s)+"\n")
class CalliopeDebugTransform(Transform):
mem = {}
max = 0
allocated = 0
reset = None
buffer = ""
heap_start = 0
heap_end = 0
def __init__(self):
self.r_malloc = re.compile("(?:microbit_)?malloc:\s+(?:NATIVE\s+)?(?:ALLOCATED:)\s+(\d+)\s+\[(0x[0-9a-f]+)\]")
self.r_free = re.compile("(?:microbit_)?free:\\s+((0x)?[0-9a-f]+)")
self.r_heap_start = re.compile("heap_start\s+:\s+(0x[0-9a-f]+)")
self.r_heap_end = re.compile("heap_end\s+:\s+(0x[0-9a-f]+)")
self.r_heap_size = re.compile("heap_size\s+:\s+(\d+)")
self.r_oom = re.compile("malloc:\s+OUT OF MEMORY\s+\[(\d+)\]")
def rx(self, rx_input):
# collect lines
out = ""
for c in rx_input:
if c == '\r' or c == '\n':
if len(self.buffer):
out += self.trace_line(self.buffer) + c
else:
out += c
self.buffer = ""
continue
else:
self.buffer += c
continue
return out
def colorize(self, free):
if free > self.max/2: return "4"
elif free > self.max/3: return "5"
return "1"
def trace_line(self, line):
# strip newline and carriage return
# line = line.rstrip('\n').rstrip('\r')
m = self.r_heap_size.search(line)
if m:
self.max = int(m.group(1))
self.mem = {}
self.allocated = 0
line += "\n\n\033[91m>> RESET HEAP COUNTERS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m\n"
line += "\033[4m (NUM) FREE [ ADDRESS] CHANGE (COMMENT)\033[0m\n"
return line
m = self.r_heap_start.search(line)
if m:
self.heap_start = int(m.group(1), 16)
return line + "\n"
m = self.r_heap_end.search(line)
if m:
self.heap_end = int(m.group(1), 16)
return line + "\n"
# match malloc, realloc and free
m = self.r_malloc.search(line)
if m:
out = ""
if m.group(2) == "0":
out += "\033[1m!! (%03d) \033[31mmalloc failed\033[0m (%s)\n" % (len(self.mem), line)
else:
addr = int(m.group(2), 16)
self.mem[addr] = int(m.group(1))
self.allocated += int(m.group(1))
free = self.max - self.allocated
out += "\033[1m== (%03d) \033[3%sm%8d\033[0m [%8x] \033[31m+%-6d\033[0m (%s)" % \
(len(self.mem), self.colorize(free), free, addr, int(m.group(1)), line)
return out
m = self.r_free.search(line)
if m:
out = ""
freed = 0
addr = int(m.group(1), 16)
if addr in self.mem:
freed = self.mem[addr]
self.allocated -= freed
del self.mem[addr]
else:
out += "\033[33m!! (%03d) WARN: free(0x%x)\033[0m\n" % (len(self.mem), addr)
free = self.max - self.allocated
out += "\033[1m== (%03d) \033[3%sm%8d\033[0m [%8x] \033[92m-%-6d\033[0m (%s)" % \
(len(self.mem), self.colorize(free), free, addr, freed, line)
return out
m = self.r_oom.search(line)
if m:
out = ""
wanted = int(m.group(1))
out += "\033[31m!! (%03d) : malloc(): no free block of size %d\033[0m\n" % (len(self.mem), wanted)
mem = ""
start = 0
end = 0
for addr in range(self.heap_start, self.heap_end):
if addr+4 in self.mem:
start = addr+4
end = addr+4 + self.mem[addr+4]
if addr == end:
end = 0
if end:
if addr < start: mem += "%"
else: mem += "*"
else:
mem += "."
out += "HEAP ALLOCATION (% header | * data | . free)\n"
for a in range(0, self.max, 64):
out += "%06d %08x %s\n" % (a, self.heap_start + a, mem[a:a+64])
addr = 0
out += "\033[31m== Free blocks: ==\033[0m\n"
blocks = re.findall("[%*]+|[.]+", mem)
for b in blocks:
if len(b) > 4 and b[0] == '.':
out += "%08x %d bytes\n" % (self.heap_start + addr + 4, len(b)-4)
addr += len(b)
return out
# print all other lines as is, so we can still use the log functionality
return line