-
Notifications
You must be signed in to change notification settings - Fork 178
add memory trace parser to mbed client #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thinkberg
wants to merge
1
commit into
ARMmbed:master
Choose a base branch
from
ubirch:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# 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 MbedTransform(Transform): | ||
mem = {} | ||
allocated = 0 | ||
reset = None | ||
buffer = "" | ||
|
||
def __init__(self, reset_str=None): | ||
self.reset = reset_str | ||
if self.reset is not None: | ||
sys.stderr.write("memory tracing enabled: resetting on '{}'\n".format(self.reset)) | ||
|
||
self.r_malloc = re.compile("([^#]*)#m:0x([0-9a-f]+);0x([0-9a-f]+)-(\\d+)") | ||
self.r_calloc = re.compile("([^#]*)#c:0x([0-9a-f]+);0x([0-9a-f]+)-(\\d+);(\\d+)") | ||
self.r_realloc = re.compile("([^#]*)#r:0x([0-9a-f]+);0x([0-9a-f]+)-0x([0-9a-f]+);(\\d+)") | ||
self.r_free = re.compile("([^#]*)#f:0x([0-9a-f]+);0x([0-9a-f]+)-0x([0-9a-f]+)") | ||
|
||
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) | ||
else: | ||
out += "\n" | ||
self.buffer = "" | ||
continue | ||
else: | ||
self.buffer += c | ||
continue | ||
return out | ||
|
||
def trace_line(self, line): | ||
# strip newline and carriage return | ||
line = line.rstrip('\n').rstrip('\r') | ||
|
||
# if we detect the reset keyword, rest the map and memory counter | ||
if self.reset is not None and self.reset in line: | ||
self.mem = {} | ||
self.allocated = 0 | ||
line += "\n\n\033[91m>> RESET HEAP COUNTERS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m\n" | ||
line += "\033[4m (NUM) TOTAL [ ADDRESS] CHANGE (COMMENT)\033[0m" | ||
return line | ||
|
||
# match malloc, realloc and free | ||
m = self.r_malloc.search(line) | ||
if m: | ||
out = "" | ||
if len(m.group(1)): | ||
out += m.group(1) | ||
if m.group(2) == "0x0": | ||
out += "\033[1m!! (%03d) \033[31mmalloc failed\033[0m (%s)\n" % (len(self.mem), line) | ||
else: | ||
self.mem[m.group(2)] = int(m.group(4)) | ||
self.allocated += int(m.group(4)) | ||
out += "\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[31m+%-6d\033[0m (%s)" % \ | ||
(len(self.mem), self.allocated, int(m.group(2), 16), int(m.group(4)), line.replace(m.group(1), "")) | ||
return out | ||
|
||
m = self.r_calloc.search(line) | ||
if m: | ||
out = "" | ||
if len(m.group(1)): | ||
out += m.group(1) | ||
if m.group(2) == "0x0": | ||
out += "\033[1m!! (%03d) \033[31mcalloc failed\033[0m (%s)\n" % (len(self.mem), line) | ||
else: | ||
total = int(m.group(4)) * int(m.group(5)) | ||
self.mem[m.group(2)] = total | ||
self.allocated += total | ||
out += "\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[31m+%-6d\033[0m (%s)" % \ | ||
(len(self.mem), self.allocated, int(m.group(2), 16), total, line.replace(m.group(1), "")) | ||
return out | ||
|
||
m = self.r_realloc.search(line) | ||
if m: | ||
out = "" | ||
if len(m.group(1)): | ||
out += m.group(1) | ||
diff = 0 | ||
if self.mem.has_key(m.group(2)): | ||
diff = int(m.group(5)) - self.mem[m.group(2)] | ||
self.mem[m.group(2)] = int(m.group(5)) | ||
else: | ||
out += "\033[33m!! (%03d) WARN: realloc() without previous allocation\033[0m (%s)\n" % (len(self.mem), line) | ||
self.allocated += diff | ||
out += "\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[35m+%-6d\033[0m (%s)" % \ | ||
(len(self.mem), self.allocated, int(m.group(2), 16), diff, line.replace(m.group(1), "")) | ||
return out | ||
|
||
m = self.r_free.search(line) | ||
if m: | ||
out = "" | ||
if len(m.group(1)): | ||
out += m.group(1) | ||
freed = 0 | ||
if self.mem.has_key(m.group(4)): | ||
freed = self.mem[m.group(4)] | ||
self.allocated -= freed | ||
del self.mem[m.group(4)] | ||
else: | ||
out += "\033[33m!! (%03d) WARN: free(0x%s)\033[0m\n" % (len(self.mem), m.group(4)) | ||
out += "\033[1m== (%03d) \033[34m%8d\033[0m [%8x] \033[92m-%-6d\033[0m (%s)" % \ | ||
(len(self.mem), self.allocated, int(m.group(4), 16), freed, line.replace(m.group(1), "")) | ||
return out | ||
|
||
# print all other lines as is, so we can still use the log functionality | ||
return line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the place where it runs the data through the trace matcher. The transformer class collects data until it sees either
\r
or\n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the reference