Feature/add memap cstack usage ports (#661)

* Added memap, avstack, and checkstackusage tools to STM32F4xx Makefile and CMake builds to calculate CSTACK depth and RAM usage

* Added memap, cstack, and ram-usage recipes to stm32f10x port Makefile.  Added Cmake build.

* Removed local dlmstp.c module from stm32f10x port, and used the common datalink dlmstp.c module with MS/TP extended frames and zero-config support.

* Added .nm and .su to .gitignore to skip the analysis file residue.
This commit is contained in:
Steve Karg
2024-05-31 14:39:25 -05:00
committed by GitHub
parent cf7eb7d98d
commit 4a7b7763c2
32 changed files with 3855 additions and 1974 deletions
+251
View File
@@ -0,0 +1,251 @@
#!/usr/bin/perl -w
# avstack.pl: GCC C stack checker
# Copyright (C) 2013 Daniel Beer <dlbeer@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# Usage
# -----
#
# This script requires that you compile your code with -fstack-usage.
# This results in GCC generating a .su file for each .o file. Once you
# have these, do:
#
# ./avstack.pl <object files>
#
# This will disassemble .o files to construct a call graph, and read
# frame size information from .su. The call graph is traced to find, for
# each function:
#
# - Call height: the maximum call height of any callee, plus 1
# (defined to be 1 for any function which has no callees).
#
# - Inherited frame: the maximum *inherited* frame of any callee, plus
# the GCC-calculated frame size of the function in question.
#
# Using these two pieces of information, we calculate a cost (estimated
# peak stack usage) for calling the function. Functions are then listed
# on stdout in decreasing order of cost.
#
# Functions which are recursive are marked with an 'R' to the left of
# them. Their cost is calculated for a single level of recursion.
#
# The peak stack usage of your entire program can usually be estimated
# as the stack cost of "main", plus the maximum stack cost of any
# interrupt handler which might execute.
use strict;
# Configuration: set these as appropriate for your architecture/project.
my $objdump = "arm-none-eabi-objdump";
my $call_cost = 4;
# First, we need to read all object and corresponding .su files. We're
# gathering a mapping of functions to callees and functions to frame
# sizes. We're just parsing at this stage -- callee name resolution
# comes later.
my %frame_size; # "func@file" -> size
my %call_graph; # "func@file" -> {callees}
my %addresses; # "addr@file" -> "func@file"
my %global_name; # "func" -> "func@file"
my %ambiguous; # "func" -> 1
foreach (@ARGV) {
# Disassemble this object file to obtain a callees. Sources in the
# call graph are named "func@file". Targets in the call graph are
# named either "offset@file" or "funcname". We also keep a list of
# the addresses and names of each function we encounter.
my $objfile = $_;
my $source;
open(DISASSEMBLY, "$objdump -dr $objfile|") ||
die "Can't disassemble $objfile";
while (<DISASSEMBLY>) {
chomp;
if (/^([0-9a-fA-F]+) <(.*)>:/) {
my $a = $1;
my $name = $2;
$source = "$name\@$objfile";
$call_graph{$source} = {};
$ambiguous{$name} = 1 if defined($global_name{$name});
$global_name{$name} = "$name\@$objfile";
$a =~ s/^0*//;
$addresses{"$a\@$objfile"} = "$name\@$objfile";
}
if (/: R_[A-Za-z0-9_]+_CALL[ \t]+(.*)/) {
my $t = $1;
if ($t eq ".text") {
$t = "\@$objfile";
} elsif ($t =~ /^\.text\+0x(.*)$/) {
$t = "$1\@$objfile";
}
$call_graph{$source}->{$t} = 1;
}
}
close(DISASSEMBLY);
# Extract frame sizes from the corresponding .su file.
if ($objfile =~ /^(.*).o$/) {
my $sufile = "$1.su";
open(SUFILE, "<$sufile") || die "Can't open $sufile";
while (<SUFILE>) {
$frame_size{"$1\@$objfile"} = $2 + $call_cost
if /^.*:([^\t ]+)[ \t]+([0-9]+)/;
}
close(SUFILE);
}
}
# In this step, we enumerate each list of callees in the call graph and
# try to resolve the symbols. We omit ones we can't resolve, but keep a
# set of them anyway.
my %unresolved;
foreach (keys %call_graph) {
my $from = $_;
my $callees = $call_graph{$from};
my %resolved;
foreach (keys %$callees) {
my $t = $_;
if (defined($addresses{$t})) {
$resolved{$addresses{$t}} = 1;
} elsif (defined($global_name{$t})) {
$resolved{$global_name{$t}} = 1;
warn "Ambiguous resolution: $t" if defined ($ambiguous{$t});
} elsif (defined($call_graph{$t})) {
$resolved{$t} = 1;
} else {
$unresolved{$t} = 1;
}
}
$call_graph{$from} = \%resolved;
}
# Create fake edges and nodes to account for dynamic behaviour.
$call_graph{"INTERRUPT"} = {};
foreach (keys %call_graph) {
$call_graph{"INTERRUPT"}->{$_} = 1 if /^__vector_/;
}
# Trace the call graph and calculate, for each function:
#
# - inherited frames: maximum inherited frame of callees, plus own
# frame size.
# - height: maximum height of callees, plus one.
# - recursion: is the function called recursively (including indirect
# recursion)?
my %has_caller;
my %visited;
my %total_cost;
my %call_depth;
sub trace {
my $f = shift;
if ($visited{$f}) {
$visited{$f} = "R" if $visited{$f} eq "?";
return;
}
$visited{$f} = "?";
my $max_depth = 0;
my $max_frame = 0;
my $targets = $call_graph{$f} || die "Unknown function: $f";
if (defined($targets)) {
foreach (keys %$targets) {
my $t = $_;
$has_caller{$t} = 1;
trace($t);
my $is = $total_cost{$t};
my $d = $call_depth{$t};
$max_frame = $is if $is > $max_frame;
$max_depth = $d if $d > $max_depth;
}
}
$call_depth{$f} = $max_depth + 1;
$total_cost{$f} = $max_frame + ($frame_size{$f} || 0);
$visited{$f} = " " if $visited{$f} eq "?";
}
foreach (keys %call_graph) { trace $_; }
# Now, print results in a nice table.
printf " %-40s %8s %8s %8s\n",
"Function Name", "Cost", "Frame", "Height";
print "------------------------------------";
print "------------------------------------\n";
my $max_iv = 0;
my $main = 0;
foreach (sort { $total_cost{$b} <=> $total_cost{$a} } keys %visited) {
my $name = $_;
if (/^(.*)@(.*)$/) {
$name = $1 unless $ambiguous{$name};
}
my $tag = $visited{$_};
my $cost = $total_cost{$_};
$name = $_ if $ambiguous{$name};
$tag = ">" unless $has_caller{$_};
if (/^__vector_/) {
$max_iv = $cost if $cost > $max_iv;
} elsif (/^main@/) {
$main = $cost;
}
if ($ambiguous{$name}) { $name = $_; }
printf "%s %-40s %8d %8d %8d\n", $tag, $name, $cost,
$frame_size{$_} || 0, $call_depth{$_};
}
print "\n";
print "Peak execution estimate (main + worst-case IV):\n";
printf " main = %d, worst IV = %d, total = %d\n",
$total_cost{$global_name{"main"}},
$total_cost{"INTERRUPT"},
$total_cost{$global_name{"main"}} + $total_cost{"INTERRUPT"};
print "\n";
print "The following functions were not resolved:\n";
foreach (keys %unresolved) { print " $_\n"; }
+120
View File
@@ -0,0 +1,120 @@
# avstack.pl
dlbeer@gmail.com
31 May 2013 (updated 24 Aug 2015)
When developing for memory constrained systems, it's useful to know at compile-time that the available memory is sufficient for your application firmware. Dynamic allocation is usually avoided, and the size of statically allocated memory in the `.data` and `.bss` sections can be easily inspected with GNU `size` or a similary tool. This leaves one problem: stack use. The stack grows and shrinks dynamically at runtime, but under some circumstances, the peak stack use can be statically analyzed.
* [avstack.pl](avstack.pl)
The script linked above is a static stack checker, intended for use with a recent-ish version of [AVR-GCC](http://gcc.gnu.org/wiki/avr-gcc). In order to use it, you must ensure that your object files are compiled with `-fstack-usage`. This causes GCC to generate a `.su` file for every `.o` file. [These files](http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Static-Stack-Usage-Analysis.html) contain information on the size of the stack frame for each function compiled in the `.o` file. For example:
sha1.c:43:13:mix_w 0 static
sha1.c:54:13:rot_left 0 static
sha1.c:69:13:rot_right 0 static
sha1.c:178:6:sha1_init 0 static
sha1.c:183:6:sha1_next 65 static
sha1.c:192:6:sha1_final 6 static
Once you have these files, invoke `avstack.pl`, passing as arguments the names of all `.o` files that will be linked into your binary. The `.su` files are assumed to be located in the same directories as their corresponding `.o` files.
The script reads all `.su` files, and disassembles all `.o` files, including relocation data. The disassemblies are parsed and used to construct a call graph. Multiple functions in different translation units with the same name don't cause problems, provided there are no global namespace collisions. Information will appear on any unresolvable or ambiguous references.
Next, the call graph is traced to find the peak stack usage of all functions. This is calculated for each function as the maximum stack usage of any of its callees, plus its own stack frame, plus some call-cost constant (not included in GCC's analysis).
Here's an example of the output produced:
$ ./avstack.pl */*.o
Func Cost Frame Height
------------------------------------------------------------------------
> main 235 90 4
prng_next 145 72 3
sha1_final 83 10 3
sha1_next 73 69 2
__vector_16 28 20 3
> INTERRUPT 28 0 4
clock_signal_tick 8 4 2
clock_iterate 8 4 2
clock_init 4 4 1
led_init 4 4 1
led_set 4 4 1
> led_get 4 4 1
rot_right 4 4 1
mix_w 4 4 1
prng_init 4 4 1
clock_signal_pps 4 4 1
clock_get_raw 4 4 1
clock_get 4 4 1
prng_stir 4 4 1
rot_left 4 4 1
sha1_init 4 4 1
led_slot_clock_tick 4 4 1
The following functions were not resolved:
memcpy_P
The columns are:
* **Cost**: peak stack usage during a call to the function.
* **Frame**: stack frame size, obtained from the `.su` file, plus the call-cost constant.
* **Height**: height in call graph -- calculated as maximum height of any callee, plus one.
Indicators to the left of the function name indicate features of the call graph. A `>` indicates that the function has no callee. This could be because it's an entry point (like `main` or an interrupt vector), because the function is called dynamically through a function pointer, or simply that it's unused. An `R` indicates that the function is recursive, and the cost estimate is for a single level of recursion. Multiple functions with the same name are distinguished in the listing by appending a suffix of the form `@filename.o`.
You can customize this script by altering two variables near the beginning:
my $objdump = "avr-objdump";
my $call_cost = 4;
Note that making sense of the output of the stack analysis still requires you to know something about how your program runs. For example, in many programs, the actual peak stack use would be the cost of `main`, plus the maximum cost of any interrupt handler that might execute. You will also need to take into account dynamically invoked functions, if you have any.
To make things easier, there is a processing section in which fake nodes and edges can be added to the call graph to account for dynamic behaviour. For example, the script currently contains in this section, to represent interrupt execution:
$call_graph{"INTERRUPT"} = {};
foreach (keys %call_graph) {
$call_graph{"INTERRUPT"}->{$_} = 1 if /^__vector_/;
}
## Use with C++
Updated: 24 Aug 2015
Unfortunately, GCC uses "printable" names in the output for `-fstack-usage`. For example, the function:
namespace test {
uint32_t crc32_ieee802_3(const uint8_t *data, size_t len,
uint32_t crc = 0)
{
...
}
}
Gets listed as:
foo.cpp:6:10:uint32_t test::crc32_ieee802_3(const uint8_t*, size_t, uint32_t) 32 static
This is difficult to match up with the disassembler output. Using `c++filt` doesn't help, because it doesn't know anything about typedefs (`uint8_t` becomes `unsigned char`, for example).
I've prepared the following patches for two versions of GCC. They're independent of language and target:
* [gcc-4.7.2-mangled-stack-usage.patch](../downloads/gcc-4.7.2-mangled-stack-usage.patch)
* [gcc-4.9.2-mangled-stack-usage.patch](../downloads/gcc-4.9.2-mangled-stack-usage.patch)
These patches cause GCC to use names in the output for `-fstack-usage` which match the disassembler output:
foo.cpp:6:10:_ZN4test15crc32_ieee802_3EPKhyj 32 static
Nothing changes for C code. With these patches, `avstack.pl` works fine for C++ (although you probably want to run the final output through `c++filt`).
## Copyright
Copyright © 2013 Daniel Beer dlbeer@gmail.com
>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+3
View File
@@ -0,0 +1,3 @@
.analysed
.setup
.venv/
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright © 2021 Thanassis Tsiodras
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+73
View File
@@ -0,0 +1,73 @@
TARGET:= checkStackUsage.py
PYTHON:=python3
VENV:=.venv
all: .setup .analysed test ## Check everything
.analysed: ${TARGET}
$(MAKE) flake8
$(MAKE) pylint
$(MAKE) mypy
@touch $@
flake8: dev-install ## PEP8 compliance checks with Flake8
@echo "============================================"
@echo " Running flake8..."
@echo "============================================"
${VENV}/bin/flake8 ${TARGET}
pylint: dev-install ## Static analysis with Pylint
@echo "============================================"
@echo " Running pylint..."
@echo "============================================"
${VENV}/bin/pylint --disable=I --rcfile=pylint.cfg ${TARGET}
mypy: dev-install ## Type checking with mypy
@echo "============================================"
@echo " Running mypy..."
@echo "============================================"
${VENV}/bin/mypy --ignore-missing-imports ${TARGET}
dev-install: .setup | prereq
prereq:
@${PYTHON} -c 'import sys; sys.exit(1 if (sys.version_info.major<3 or sys.version_info.minor<5) else 0)' || { \
echo "=============================================" ; \
echo "[x] You need at least Python 3.5 to run this." ; \
echo "=============================================" ; \
exit 1 ; \
}
.setup: requirements.txt
@if [ ! -d ${VENV} ] ; then \
echo "[-] Installing VirtualEnv environment..." ; \
${PYTHON} -m venv ${VENV} || exit 1 ; \
fi
echo "[-] Installing packages inside environment..." ; \
. ${VENV}/bin/activate || exit 1 ; \
${PYTHON} -m pip install -r requirements.txt || exit 1
touch $@
test: ## Test proper functioning
$(MAKE) -C tests/
clean: ## Cleanup artifacts
rm -rf .cache/ .mypy_cache/ .analysed .setup __pycache__ \
tests/__pycache__ .pytest_cache/ .processed .coverage
$(MAKE) -C tests/ clean
help: ## Display this help section
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-18s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help
define newline # a literal \n
endef
# Makefile debugging trick:
# call print-VARIABLE to see the runtime value of any variable
# (hardened a bit against some special characters appearing in the output)
print-%:
@echo '$*=$(subst ','\'',$(subst $(newline),\n,$($*)))'
.PHONY: flake8 pylint mypy clean dev-install prereq
+173
View File
@@ -0,0 +1,173 @@
*(Detailed blog post [here](https://www.thanassis.space/stackusage.html) )*
# Introduction
This is a utility that computes the stack usage per function. It is
particularly useful in embedded systems programming, where memory is at a
premium - and also in safety-critical SW *(blowing up from a stack overflow
while you operate medical equipment or fly in space is not exactly optimal)*.
# In detail
*(Detailed blog post [here](https://www.thanassis.space/stackusage.html) )*
You can read many details about how this script works in the blog post
linked above; but the executive summary is this:
- We expect the source code compilation to use GCC's `-fstack-usage`.
This generates `.su` files with the stack usage of each function
**(in isolation)** stored per compilation unit. Simply put, `file.c`
compiled with `-fstack-usage` will create `file.o` *and* `file.su`.
- The script can then be launched like so:
checkStackUsage.py binaryELF folderTreeContainingSUfiles
For example, if after the build we have a tree like this:
bin/
someBinary
src/
file1.c
file1.o
file1.su
lib1_src/
lib1.c
lib1.o
lib1.su
lib2_src/
lib2.c
lib2.o
lib2.su
...we run this:
checkStackUsage.py bin/someBinary src/
The script will scan all .su files in the `src` folder *(recursively,
at any depth)* and collect the standalone use of stack for each function.
It will then launch the appropriate `objdump` with option `-d` - to
disassemble the code, and create the call graph. Simplistically, it
detects patterns like this:
<foo>:
....
call <func>
...and proceeds from there to create the entire call graph.
It can then accumulate the use of all subordinate calls from each function,
and therefore compute it's total stack usage.
Output looks like this:
176: foo (foo(16),func(160))
288: func (func(288))
304: bar (bar(16),func(288))
320: main (main(16),bar(16),func(288))
...which means that function `foo` uses 176 bytes of stack; 16 because of
itself, and 160 because it calls `func`. `main` uses 320 bytes, etc.
Notice that `bar` also uses `func` - but reports a larger stack size for it
in that call chain. Read section "Repeated functions" below, to see why;
suffice to say, this is one of the few stack checkers that can cope with
symbols defined more than once.
# Platforms
The script needs to spawn the right `objdump`. It uses
`file` to detect the ELF signature, and uses appropriate regexes
to match disassembly `call` forms for:
- SPARC/LEONs (used in the European Space Agency missions)
- x86/amd64 (both 32 and 64 bits)
- 32-bit ARM
Adding additional platforms is very easy - just tell the script what
`objdump` flavor to use, and what regex to scan for to locate the
call sequences; [relevant code is here](checkStackUsage.py#L198).
# Repeated functions
Each function can only have a specific stack usage - right?
Sadly, no :-(
Feast yourself on moronic - yet perfectly valid - C code like this:
// a.c
static int func() { ...}
void foo() { func(); }
// b.c
static int func() { ...}
void bar() { func(); }
"Houston, we have a problem". While scanning the `.su` files for `a.c`
and `b.c`, we find `func` twice - and due to the `static`, we want
to use the right value on each call (from `foo`/`bar`) based on
filescope. In effect, the .su files' content need to be read
*prioritizing local static calls* when computing stack usage.
# Hidden calls
The scanning of `objdump` output for call sequences is the best we can do;
but it's not perfect. For example, any calls made via function pointer
indirections are "invisible" to this process.
And since fp-based calls can do all sorts of shenanigans - e.g.
reading the call endpoint from an array of functions via some
algorithm - statically deducing which functions are actually called
is tantamount to the halting problem.
I am open to suggestions on this.
# Static Analysis
The script is written in Python - `make all` will check it with:
- `flake8` (PEP8 compliance)
- `pylint` (Static Analysis)
- ...and `mypy` (static type checking).
All dependencies to perform these checks will be automatically
installed via `pip` in a local virtual environment (i.e.
under folder `.venv`) the first time you invoke `make all`.
# Test example
The scenario of repeated functions is tested via `make test`;
in my 64-bit Arch Linux I see this output:
$ make test
...
make[1]: Entering directory '/home/ttsiod/Github/checkStackUsage/tests'
==============================================
176: foo (foo(16),func(160))
288: func (func(288))
304: bar (bar(16),func(288))
320: main (main(16),bar(16),func(288))
==============================================
1. The output shown above must contain 4 lines
2. 'foo' and 'bar' must both be calling 'func'
*but with different stack sizes in each*.
3. 'main' must be using the largest 'func'
(i.e. be going through 'bar')
4. The reported sizes must properly accumulate
==============================================
make[1]: Leaving directory '/home/ttsiod/Github/checkStackUsage/tests'
Given the content of [a.c](tests/a.c), [b.c](tests/b.c) and
[main.c](tests/main.c), the output looks good. Notice that for
`func` we report the maximum of the two (the one reported by
GCC inside `b.c`, when it is called by `bar`).
# Feedback
If you see something wrong in the script that isn't documented above,
questions (and much better, pull requests) are most welcome.
Thanassis Tsiodras, Dr.-Ing.
ttsiodras_at-no-spam_thanks-gmail_dot-com
+445
View File
@@ -0,0 +1,445 @@
#!/usr/bin/env python3
"""
Utility to detect recursive calls and calculate total stack usage per function
(via following the call graph).
Published under the GPL, (C) 2011 Thanassis Tsiodras
Suggestions/comments: ttsiodras@gmail.com
Updated to use GCC-based .su files (-fstack-usage) in 2021.
"""
import os
import re
import sys
import operator
from typing import Dict, Set, Optional, List, Tuple
FunctionName = str
FunctionNameToInt = Dict[FunctionName, int]
# For each function, what is the set of functions it calls?
CallGraph = Dict[FunctionName, Optional[Set[FunctionName]]]
# .su files data
Filename = str
SuData = Dict[FunctionName, List[Tuple[Filename, int]]]
# findStackUsage will return a tuple:
# - The total stack size used
# - A list of pairs, of the functions/stacksizes, adding up to the final use.
UsageResult = Tuple[int, List[Tuple[FunctionName, int]]]
class Matcher:
"""regexp helper"""
def __init__(self, pattern, flags=0):
self._pattern = re.compile(pattern, flags)
self._hit = None
def match(self, line):
self._hit = re.match(self._pattern, line)
return self._hit
def search(self, line):
self._hit = re.search(self._pattern, line)
return self._hit
def group(self, idx):
return self._hit.group(idx)
def lookupStackSizeOfFunction(
fn: FunctionName,
fns: List[Tuple[FunctionName, int]],
suData: SuData,
stackUsagePerFunction: FunctionNameToInt) -> int:
"""
What do you do when you have moronic C code like this?
===
a.c
===
static int func() { ...}
void foo() { func(); }
===
b.c
===
static int func() { ...}
void bar() { func(); }
You have a problem. There is only one entry in stackUsagePerFunction,
since we assign as we scan the su files - but you basically want to
use the right value, based on filescope (due to the static).
In effect, the .su files need to be read *prioritizing local calls*
when computing stack usage.
So what we do is this: we have suData - a dictionary that stores
per each function name, WHERE we found it (at which .su filenames)
and what size it had in each of them. We scan that list, looking for
the last filename in our call chain so far (i.e. the last of the 'fns').
And we then report the associated stack use.
"""
# If the call chain so far is empty, or our function is not in the
# .su files (e.g. it comes from a library we linked with), then we
# use the 'su-agnostic' information we got during our symbol scan.
if not fns or fn not in suData:
return stackUsagePerFunction[fn]
# Otherwise, we first get the last function in the call chain...
suFilename = None
lastCallData = fns[-1]
functionName = lastCallData[0]
# ...and use the .su files information to get the file it lives in
# Remember, SuData is a Dict[FunctionName, List[Tuple[Filename, int]]]
suList = suData.get(functionName, [])
if suList:
suFilename = suList[0][0] # Get the filename part of the first tuple
if not suFilename:
return stackUsagePerFunction[fn]
# Now that we have the filename of our last caller in the chain
# that calls us, scan our OWN existence in the .su data, to find
# the one that matches - thus prioritizing local calls!
for elem in suData.get(fn, []):
xFilename, xStackUsage = elem
if xFilename == suFilename:
return xStackUsage
# ...otherwise (no match in the .su data) we use the 'su-agnostic'
# information we got during our symbol scan.
return stackUsagePerFunction[fn]
def findStackUsage(
fn: FunctionName,
fns: List[Tuple[FunctionName, int]],
suData: SuData,
stackUsagePerFunction: FunctionNameToInt,
callGraph: CallGraph,
badSymbols: Set[FunctionName]) -> UsageResult:
"""
Calculate the total stack usage of the input function,
taking into account who it calls.
"""
if fn in [x[0] for x in fns]:
# So, what to do with recursive functions?
# We just reached this point with fns looking like this:
# [a, b, c, d, a]
# A "baseline" answer is better than no answer;
# so we let the parent series of calls accumulate to
# as + bs + cs + ds
# ...ie. the stack sizes of all others.
# We therefore return 0 for this "last step"
# and stop the recursion here.
totalStackUsage = sum(x[1] for x in fns)
if fn not in badSymbols:
# Report recursive functions.
badSymbols.add(fn)
print("[x] Recursion detected:", fn, 'is called from',
fns[-1][0], 'in this chain:', fns)
return (totalStackUsage, fns[:])
if fn not in stackUsagePerFunction:
# Unknown function, what else to do? Count it as 0 stack usage...
totalStackUsage = sum(x[1] for x in fns)
return (totalStackUsage, fns[:] + [(fn, 0)])
thisFunctionStackSize = lookupStackSizeOfFunction(
fn, fns, suData, stackUsagePerFunction)
calledFunctions = callGraph.get(fn, set())
# If we call noone else, stack usage is just our own stack usage
if fn not in callGraph or not calledFunctions:
totalStackUsage = sum(x[1] for x in fns) + thisFunctionStackSize
res = (totalStackUsage, fns[:] + [(fn, thisFunctionStackSize)])
return res
# Otherwise, we check the stack usage for each function we call
totalStackUsage = 0
maxStackPath = []
for x in sorted(calledFunctions):
total, path = findStackUsage(
x,
fns[:] + [(fn, thisFunctionStackSize)],
suData,
stackUsagePerFunction,
callGraph,
badSymbols)
if total > totalStackUsage:
totalStackUsage = total
maxStackPath = path
return (totalStackUsage, maxStackPath[:])
def ParseCmdLineArgs(cross_prefix: str) -> Tuple[
str, str, Matcher, Matcher, Matcher]:
if cross_prefix:
objdump = cross_prefix + 'objdump'
nm = cross_prefix + 'nm'
functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9\._]+?)>:')
callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9\._]+)>')
stackUsagePattern = Matcher(
r'^.*save.*%sp, (-[0-9]{1,}), %sp')
else:
binarySignature = os.popen(f"file \"{sys.argv[-2]}\"").readlines()[0]
x86 = Matcher(r'ELF 32-bit LSB.*80.86')
x64 = Matcher(r'ELF 64-bit LSB.*x86-64')
leon = Matcher(r'ELF 32-bit MSB.*SPARC')
arm = Matcher(r'ELF 32-bit LSB.*ARM')
if x86.search(binarySignature):
objdump = 'objdump'
nm = 'nm'
functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:')
callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9_]+)>')
stackUsagePattern = Matcher(r'^.*[add|sub]\s+\$(0x\S+),%esp')
elif x64.search(binarySignature):
objdump = 'objdump'
nm = 'nm'
functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:')
callPattern = Matcher(r'^.*callq?\s+\S+\s+<([a-zA-Z0-9_]+)>')
stackUsagePattern = Matcher(r'^.*[add|sub]\s+\$(0x\S+),%rsp')
elif leon.search(binarySignature):
objdump = 'sparc-rtems5-objdump'
nm = 'sparc-rtems5-nm'
functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:')
callPattern = Matcher(r'^.*call\s+\S+\s+<([a-zA-Z0-9_]+)>')
stackUsagePattern = Matcher(
r'^.*save.*%sp, (-([0-9]{2}|[3-9])[0-9]{2}), %sp')
elif arm.search(binarySignature):
objdump = 'arm-none-eabi-objdump'
nm = 'arm-none-eabi-nm'
functionNamePattern = Matcher(r'^(\S+) <([a-zA-Z0-9_]+?)>:')
callPattern = Matcher(r'^.*bl\s+\S+\s+<([a-zA-Z0-9_]+)>')
stackUsagePattern = Matcher(
r'^.*sub.*sp, (#[0-9][0-9]*)')
else:
print("Unknown signature:", binarySignature, "please use -cross")
sys.exit(1)
return objdump, nm, functionNamePattern, callPattern, stackUsagePattern
def GetSizeOfSymbols(nm: str, elf_binary: str) -> Tuple[
FunctionNameToInt, FunctionNameToInt]:
# Store .text symbol offsets and sizes (use nm)
offsetOfSymbol = {} # type: FunctionNameToInt
for line in os.popen(
nm + " \"" + elf_binary + "\" | grep ' [Tt] '").readlines():
offsetData, unused, symbolData = line.split()
offsetOfSymbol[symbolData] = int(offsetData, 16)
sizeOfSymbol = {}
lastOffset = 0
lastSymbol = ""
sortedSymbols = sorted(
offsetOfSymbol.items(), key=operator.itemgetter(1))
for symbolStr, offsetInt in sortedSymbols:
if lastSymbol != "":
sizeOfSymbol[lastSymbol] = offsetInt - lastOffset
lastSymbol = symbolStr
lastOffset = offsetInt
sizeOfSymbol[lastSymbol] = 2**31 # allow last .text symbol to roam free
return sizeOfSymbol, offsetOfSymbol
def GetCallGraph(
objdump: str,
offsetOfSymbol: FunctionNameToInt, sizeOfSymbol: FunctionNameToInt,
functionNamePattern: Matcher, stackUsagePattern: Matcher,
callPattern: Matcher) -> Tuple[CallGraph, FunctionNameToInt]:
# Parse disassembly to create callgraph (use objdump -d)
functionName = ""
stackUsagePerFunction = {} # type: FunctionNameToInt
callGraph = {} # type: CallGraph
insideFunctionBody = False
offsetPattern = Matcher(r'^([0-9A-Za-z]+):')
for line in os.popen(objdump + " -d \"" + sys.argv[-2] + "\"").readlines():
# Have we matched a function name yet?
if functionName != "":
# Yes, update "insideFunctionBody" boolean by checking
# the current offset against the length of this symbol,
# stored in sizeOfSymbol[functionName]
offset = offsetPattern.match(line)
if offset:
offset = int(offset.group(1), 16)
if functionName in offsetOfSymbol:
startOffset = offsetOfSymbol[functionName]
insideFunctionBody = \
insideFunctionBody and \
(offset - startOffset) < sizeOfSymbol[functionName]
# Check to see if we see a new function:
# 08048be8 <_functionName>:
fn = functionNamePattern.match(line)
if fn:
offset = int(fn.group(1), 16)
functionName = fn.group(2)
callGraph.setdefault(functionName, set())
# make sure this is the function we found with nm
# UPDATE: no, can't do - if a symbol is of local file scope
# (i.e. if it was declared with 'static')
# then it may appear in multiple offsets!...
#
# if functionName in offsetOfSymbol:
# if offsetOfSymbol[functionName] != offset:
# print "Weird,", functionName, \
# "is not at offset reported by", nm
# print hex(offsetOfSymbol[functionName]), hex(offset)
insideFunctionBody = True
foundFirstCall = False
stackUsagePerFunction[functionName] = 0
# If we're inside a function body
# (i.e. offset is not out of symbol size range)
if insideFunctionBody:
# Check to see if we have a call
# 8048c0a: e8 a1 03 00 00 call 8048fb0 <frame_dummy>
call = callPattern.match(line)
if functionName != "" and call:
foundFirstCall = True
calledFunction = call.group(1)
calledFunctions = callGraph[functionName]
if calledFunctions is not None:
calledFunctions.add(calledFunction)
# Check to see if we have a stack reduction opcode
# 8048bec: 83 ec 04 sub $0x46,%esp
if functionName != "" and not foundFirstCall:
stackMatch = stackUsagePattern.match(line)
if stackMatch:
value = stackMatch.group(1)
if value.startswith("0x"):
# sub $0x46,%esp
value = int(stackMatch.group(1), 16)
if value > 2147483647:
# unfortunately, GCC may also write:
# add $0xFFFFFF86,%esp
value = 4294967296 - value
elif value.startswith("#"):
# sub sp, sp, #1024
value = int(value[1:])
else:
# save %sp, -104, %sp
value = -int(value)
assert(
stackUsagePerFunction[functionName] is not None)
stackUsagePerFunction[functionName] += value
# for fn,v in stackUsagePerFunction.items():
# print fn,v
# print "CALLS:", callGraph[fn]
return callGraph, stackUsagePerFunction
def ReadSU(fullPathToSuFile: str) -> Tuple[FunctionNameToInt, SuData]:
stackUsagePerFunction = {} # type: FunctionNameToInt
suData = {} # type: SuData
# pylint: disable=R1732
for line in open(fullPathToSuFile, encoding='utf-8'):
data = line.strip().split()
if len(data) == 3 and data[2] == 'static':
try:
functionName = data[0].split(':')[-1]
functionStackUsage = int(data[1])
except ValueError:
continue
stackUsagePerFunction[functionName] = functionStackUsage
suData.setdefault(functionName, []).append(
(fullPathToSuFile, functionStackUsage))
return stackUsagePerFunction, suData
def GetSizesFromSUfiles(root_path) -> Tuple[FunctionNameToInt, SuData]:
stackUsagePerFunction = {} # type: FunctionNameToInt
suData = {} # type: SuData
for root, unused_dirs, files in os.walk(root_path):
for f in files:
if f.endswith('.su'):
supf, sud = ReadSU(root + os.sep + f)
for functionName, v1 in supf.items():
stackUsagePerFunction[functionName] = max(
v1, stackUsagePerFunction.get(functionName, 0))
# We need to augment the list of .su if there's
# a symbol that appears in two or more .su files.
# SuData = Dict[FunctionName, List[Tuple[Filename, int]]]
for functionName, v2 in sud.items():
suData.setdefault(functionName, []).extend(v2)
return stackUsagePerFunction, suData
def main() -> None:
cross_prefix = ''
try:
idx = sys.argv.index("-cross")
except ValueError:
idx = -1
if idx != -1:
cross_prefix = sys.argv[idx + 1]
del sys.argv[idx]
del sys.argv[idx]
chosenFunctionNames = []
if len(sys.argv) >= 4:
chosenFunctionNames = sys.argv[3:]
sys.argv = sys.argv[:3]
if len(sys.argv) < 3 or not os.path.exists(sys.argv[-2]) \
or not os.path.isdir(sys.argv[-1]):
print(f"Usage: {sys.argv[0]} [-cross PREFIX]"
" ELFbinary root_path_for_su_files [functions...]")
print("\nwhere the default prefix is:\n")
print("\tarm-none-eabi- for ARM binaries")
print("\tsparc-rtems5- for SPARC binaries")
print("\t(no prefix) for x86/amd64 binaries")
print("\nNote that if you use '-cross', SPARC opcodes are assumed.\n")
sys.exit(1)
objdump, nm, functionNamePattern, callPattern, stackUsagePattern = \
ParseCmdLineArgs(cross_prefix)
sizeOfSymbol, offsetOfSymbol = GetSizeOfSymbols(nm, sys.argv[-2])
callGraph, stackUsagePerFunction = GetCallGraph(
objdump,
offsetOfSymbol, sizeOfSymbol,
functionNamePattern, stackUsagePattern, callPattern)
supf, suData = GetSizesFromSUfiles(sys.argv[-1])
alreadySeen = set() # type: Set[Filename]
badSymbols = set() # type: Set[Filename]
for k, v in supf.items():
if k in alreadySeen:
badSymbols.add(k)
alreadySeen.add(k)
stackUsagePerFunction[k] = max(
v, stackUsagePerFunction.get(k, 0))
# Then, navigate the graph to calculate stack needs per function
results = []
for fn, value in stackUsagePerFunction.items():
if chosenFunctionNames and fn not in chosenFunctionNames:
continue
if value is not None:
results.append(
(fn,
findStackUsage(
fn, [], suData, stackUsagePerFunction, callGraph,
badSymbols)))
for fn, data in sorted(results, key=lambda x: x[1][0]):
# pylint: disable=C0209
print(
"%10s: %s (%s)" % (
data[0], fn, ",".join(
x[0] + "(" + str(x[1]) + ")"
for x in data[1])))
if __name__ == "__main__":
main()
+381
View File
@@ -0,0 +1,381 @@
[MASTER]
# Specify a configuration file.
#rcfile=
# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
#init-hook=
# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=CVS
# Pickle collected data for later comparisons.
persistent=yes
# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.
load-plugins=
# Use multiple processes to speed up Pylint.
jobs=1
# Allow loading of arbitrary C extensions. Extensions are imported into the
# active Python interpreter and may run arbitrary code.
unsafe-load-any-extension=no
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=lxml,clang
# Allow optimization of some AST trees. This will activate a peephole AST
# optimizer, which will apply various small optimizations. For instance, it can
# be used to obtain the result of joining multiple strings with the addition
# operator. Joining a lot of strings can lead to a maximum recursion error in
# Pylint and this flag can prevent that. It has one side effect, the resulting
# AST will be different than the one from reality.
optimize-ast=no
[MESSAGES CONTROL]
# Only show warnings with the listed confidence levels. Leave empty to show
# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED
confidence=
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once). See also the "--disable" option for examples.
#enable=
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once).You can also use "--disable=all" to
# disable everything first and then reenable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=coerce-method,nonzero-method,buffer-builtin,unichr-builtin,reload-builtin,using-cmp-argument,reduce-builtin,filter-builtin-not-iterating,zip-builtin-not-iterating,raising-string,long-builtin,backtick,long-suffix,delslice-method,suppressed-message,cmp-method,old-octal-literal,basestring-builtin,metaclass-assignment,print-statement,execfile-builtin,round-builtin,oct-method,standarderror-builtin,hex-method,import-star-module-level,indexing-exception,map-builtin-not-iterating,old-ne-operator,setslice-method,input-builtin,apply-builtin,range-builtin-not-iterating,xrange-builtin,parameter-unpacking,no-absolute-import,old-raise-syntax,dict-iter-method,unicode-builtin,unpacking-in-except,old-division,file-builtin,next-method-called,useless-suppression,raw_input-builtin,intern-builtin,getslice-method,dict-view-method,cmp-builtin,coerce-builtin,line-too-long,missing-docstring,protected-access,global-statement,too-many-arguments,too-many-branches,too-many-locals,bare-except,invalid-name,too-many-statements,broad-except,too-many-instance-attributes,too-many-public-methods,too-few-public-methods,similarities,no-else-return,fixme,relative-beyond-top-level,import-outside-toplevel,too-many-nested-blocks
never-returning-functions=dmt.commonPy.utility.panic,sys.exit
[REPORTS]
# Set the output format. Available formats are text, parseable, colorized, msvs
# (visual studio) and html. You can also give a reporter class, eg
# mypackage.mymodule.MyReporterClass.
output-format=text
# Put messages in a separate file for each module / package specified on the
# command line instead of printing them on stdout. Reports (if any) will be
# written in a file name "pylint_global.[txt|html]".
files-output=no
# Tells whether to display a full report or only the messages
reports=yes
# Python expression which should return a note less than 10 (10 is the highest
# note). You have access to the variables errors warning, statement which
# respectively contain the number of errors / warnings messages and the total
# number of statements analyzed. This is used by the global evaluation report
# (RP0004).
evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)
# Template used to display messages. This is a python new-style format string
# used to format the message information. See doc for all details
#msg-template=
[BASIC]
# List of builtins function names that should not be used, separated by a comma
bad-functions=map,filter
# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,ex,Run,_
# Bad variable names which should always be refused, separated by a comma
bad-names=foo,bar,baz,toto,tutu,tata
# Colon-delimited sets of names that determine each other's naming style when
# the name regexes allow several styles.
name-group=
# Include a hint for the correct naming format with invalid-name
include-naming-hint=no
# Regular expression matching correct module names
module-rgx=(([a-z_][A-Za-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
# Naming hint for module names
module-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
# Regular expression matching correct constant names
const-rgx=(([a-zA-Z_][a-zA-Z0-9_]*)|(__.*__))$
# Naming hint for constant names
const-name-hint=(([a-zA-Z_][A-Z0-9_]*)|(__.*__))$
# Regular expression matching correct class names
class-rgx=[A-Z_][a-zA-Z0-9]+$
# Naming hint for class names
class-name-hint=[A-Z_][a-zA-Z0-9]+$
# Regular expression matching correct method names
method-rgx=[a-zA-Z_][a-zA-Z0-9_]{2,30}$
# Naming hint for method names
method-name-hint=[a-z_][a-z0-9_]{2,30}$
# Regular expression matching correct argument names
argument-rgx=[a-z_][A-Za-z0-9_]{2,30}$
# Naming hint for argument names
argument-name-hint=[a-z_][A-Za-z0-9_]{2,30}$
# Regular expression matching correct class attribute names
class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$
# Naming hint for class attribute names
class-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$
# Regular expression matching correct inline iteration names
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
# Naming hint for inline iteration names
inlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$
# Regular expression matching correct function names
function-rgx=[A-Za-z_][A-Za-z0-9_]{2,30}$
# Naming hint for function names
function-name-hint=[a-z_][a-z0-9_]{2,30}$
# Regular expression matching correct attribute names
attr-rgx=[a-z_][A-Za-z0-9_]{2,30}$
# Naming hint for attribute names
attr-name-hint=[a-z_][a-z0-9_]{2,30}$
# Regular expression matching correct variable names
variable-rgx=[a-z_][A-Za-z0-9_]{2,30}$
# Naming hint for variable names
variable-name-hint=[a-z_][a-z0-9_]{2,30}$
# Regular expression which should only match function or class names that do
# not require a docstring.
no-docstring-rgx=^_
# Minimum line length for functions/classes that require docstrings, shorter
# ones are exempt.
docstring-min-length=-1
[ELIF]
# Maximum number of nested blocks for function / method body
max-nested-blocks=5
[SIMILARITIES]
# Minimum lines number of a similarity.
min-similarity-lines=4
# Ignore comments when computing similarities.
ignore-comments=yes
# Ignore docstrings when computing similarities.
ignore-docstrings=yes
# Ignore imports when computing similarities.
ignore-imports=no
[FORMAT]
# Maximum number of characters on a single line.
max-line-length=140
# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$
# Allow the body of an if to be on the same line as the test if there is no
# else.
single-line-if-stmt=no
# List of optional constructs for which whitespace checking is disabled. `dict-
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
# `empty-line` allows space-only lines.
no-space-check=trailing-comma,dict-separator
# Maximum number of lines in a module
max-module-lines=1000
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
# tab).
indent-string=' '
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren=4
# Expected format of line ending, e.g. empty (any line ending), LF or CRLF.
expected-line-ending-format=
[VARIABLES]
# Tells whether we should check for unused import in __init__ files.
init-import=no
# A regular expression matching the name of dummy variables (i.e. expectedly
# not used).
dummy-variables-rgx=_$|dummy|unused.*$|__*
# List of additional names supposed to be defined in builtins. Remember that
# you should avoid to define new builtins when possible.
additional-builtins=
# List of strings which can identify a callback function by name. A callback
# name must start or end with one of those strings.
callbacks=cb_,_cb
[MISCELLANEOUS]
# List of note tags to take in consideration, separated by a comma.
notes=FIXME,XXX,TODO
[SPELLING]
# Spelling dictionary name. Available dictionaries: none. To make it working
# install python-enchant package.
spelling-dict=
# List of comma separated words that should not be checked.
spelling-ignore-words=
# A path to a file that contains private dictionary; one word per line.
spelling-private-dict-file=
# Tells whether to store unknown words to indicated private dictionary in
# --spelling-private-dict-file option instead of raising a message.
spelling-store-unknown-words=no
[TYPECHECK]
# Tells whether missing members accessed in mixin class should be ignored. A
# mixin class is detected if its name ends with "mixin" (case insensitive).
ignore-mixin-members=yes
# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=
# List of classes names for which member attributes should not be checked
# (useful for classes with attributes dynamically set). This supports can work
# with qualified names.
ignored-classes=
TokenKind
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=
[LOGGING]
# Logging modules to check that the string format arguments are in logging
# function parameter format
logging-modules=logging
[CLASSES]
# List of method names used to declare (i.e. assign) instance attributes.
defining-attr-methods=__init__,__new__,setUp
# List of valid names for the first argument in a class method.
valid-classmethod-first-arg=cls
# List of valid names for the first argument in a metaclass class method.
valid-metaclass-classmethod-first-arg=mcs
# List of member names, which should be excluded from the protected access
# warning.
exclude-protected=_asdict,_fields,_replace,_source,_make
[IMPORTS]
# Deprecated modules which should not be used, separated by a comma
deprecated-modules=optparse
# Create a graph of every (i.e. internal and external) dependencies in the
# given file (report RP0402 must not be disabled)
import-graph=
# Create a graph of external dependencies in the given file (report RP0402 must
# not be disabled)
ext-import-graph=
# Create a graph of internal dependencies in the given file (report RP0402 must
# not be disabled)
int-import-graph=
[DESIGN]
# Maximum number of arguments for function / method
max-args=5
# Argument names that match this expression will be ignored. Default to name
# with leading underscore
ignored-argument-names=_.*|unused.*|dummy.*
# Maximum number of locals for function / method body
max-locals=15
# Maximum number of return / yield for function / method body
max-returns=6
# Maximum number of branch for function / method body
max-branches=12
# Maximum number of statements in function / method body
max-statements=50
# Maximum number of parents for a class (see R0901).
max-parents=7
# Maximum number of attributes for a class (see R0902).
max-attributes=7
# Minimum number of public methods for a class (see R0903).
min-public-methods=2
# Maximum number of public methods for a class (see R0904).
max-public-methods=20
# Maximum number of boolean expressions in a if statement
max-bool-expr=5
[EXCEPTIONS]
# Exceptions that will emit a warning when being caught. Defaults to
# "Exception"
overgeneral-exceptions=Exception
+3
View File
@@ -0,0 +1,3 @@
flake8>=3.9.0
pylint>=2.6.2
mypy>=0.812
+3
View File
@@ -0,0 +1,3 @@
*.o
*.su
main
+23
View File
@@ -0,0 +1,23 @@
OBJ:=$(patsubst %.c, %.o, $(wildcard *.c))
CFLAGS:=-fstack-usage
TARGET:=main
CC?=gcc
all: ${TARGET}
@echo "=============================================="
@../checkStackUsage.py $< . 2>&1 | \
grep -E '(func|foo|bar|main) '
@echo "=============================================="
@echo "1. The output shown above must contain 4 lines"
@echo "2. 'foo' and 'bar' must both be calling 'func'"
@echo " *but with different stack sizes in each*."
@echo "3. 'main' must be using the largest 'func'"
@echo " (i.e. be going through 'bar')"
@echo "4. The reported sizes must properly accumulate"
@echo "=============================================="
${TARGET}: ${OBJ}
${CC} -o $@ ${CFLAGS} $^
clean:
rm -f ${OBJ} ${TARGET} *.su
+12
View File
@@ -0,0 +1,12 @@
#include <string.h>
static void func()
{
char a[128];
memset(a, ' ', sizeof(a));
}
void foo()
{
func();
}
+12
View File
@@ -0,0 +1,12 @@
#include <string.h>
static void func()
{
char b[256];
memset(b, ' ', sizeof(b));
}
void bar()
{
func();
}
+8
View File
@@ -0,0 +1,8 @@
extern void foo();
extern void bar();
int main()
{
foo();
bar();
}
+979
View File
@@ -0,0 +1,979 @@
#!/usr/bin/env python
"""
Copyright (c) 2016-2019 ARM Limited. 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.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function, division, absolute_import
from abc import abstractmethod, ABCMeta
from sys import stdout, exit, argv, path
from os import sep
from os.path import (basename, dirname, join, relpath, abspath, commonprefix,
splitext)
import re
import csv
import json
from argparse import ArgumentParser
from copy import deepcopy
from collections import defaultdict
from prettytable import PrettyTable, HEADER
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment
from future.utils import with_metaclass
# Be sure that the tools directory is in the search path
ROOT = abspath(join(dirname(__file__), ".."))
path.insert(0, ROOT)
from utils import (
argparse_filestring_type,
argparse_lowercase_hyphen_type,
argparse_uppercase_type
) # noqa: E402
class _Parser(with_metaclass(ABCMeta, object)):
"""Internal interface for parsing"""
SECTIONS = ('.text', '.data', '.bss', '.heap', '.stack')
MISC_FLASH_SECTIONS = ('.interrupts', '.flash_config')
OTHER_SECTIONS = ('.interrupts_ram', '.init', '.ARM.extab',
'.ARM.exidx', '.ARM.attributes', '.eh_frame',
'.init_array', '.fini_array', '.jcr', '.stab',
'.stabstr', '.ARM.exidx', '.ARM')
def __init__(self):
self.modules = dict()
def module_add(self, object_name, size, section):
""" Adds a module or section to the list
Positional arguments:
object_name - name of the entry to add
size - the size of the module being added
section - the section the module contributes to
"""
if not object_name or not size or not section:
return
if object_name in self.modules:
self.modules[object_name].setdefault(section, 0)
self.modules[object_name][section] += size
return
obj_split = sep + basename(object_name)
for module_path, contents in self.modules.items():
if module_path.endswith(obj_split) or module_path == object_name:
contents.setdefault(section, 0)
contents[section] += size
return
new_module = defaultdict(int)
new_module[section] = size
self.modules[object_name] = new_module
def module_replace(self, old_object, new_object):
""" Replaces an object name with a new one
"""
if old_object in self.modules:
self.modules[new_object] = self.modules[old_object]
del self.modules[old_object]
@abstractmethod
def parse_mapfile(self, mapfile):
"""Parse a given file object pointing to a map file
Positional arguments:
mapfile - an open file object that reads a map file
return value - a dict mapping from object names to section dicts,
where a section dict maps from sections to sizes
"""
raise NotImplemented
class _GccParser(_Parser):
RE_OBJECT_FILE = re.compile(r'^(.+\/.+\.o(bj)?)$')
RE_LIBRARY_OBJECT = re.compile(
r'^.+' + r''.format(sep) + r'lib((.+\.a)\((.+\.o(bj)?)\))$'
)
RE_STD_SECTION = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$')
RE_FILL_SECTION = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$')
RE_TRANS_FILE = re.compile(r'^(.+\/|.+\.ltrans.o(bj)?)$')
OBJECT_EXTENSIONS = (".o", ".obj")
ALL_SECTIONS = (
_Parser.SECTIONS
+ _Parser.OTHER_SECTIONS
+ _Parser.MISC_FLASH_SECTIONS
+ ('unknown', 'OUTPUT')
)
def check_new_section(self, line):
""" Check whether a new section in a map file has been detected
Positional arguments:
line - the line to check for a new section
return value - A section name, if a new section was found, None
otherwise
"""
line_s = line.strip()
for i in self.ALL_SECTIONS:
if line_s.startswith(i):
return i
if line.startswith('.'):
return 'unknown'
else:
return None
def parse_object_name(self, line):
""" Parse a path to object file
Positional arguments:
line - the path to parse the object and module name from
return value - an object file name
"""
if re.match(self.RE_TRANS_FILE, line):
return '[misc]'
test_re_mbed_os_name = re.match(self.RE_OBJECT_FILE, line)
if test_re_mbed_os_name:
object_name = test_re_mbed_os_name.group(1)
# corner case: certain objects are provided by the GCC toolchain
if 'arm-none-eabi' in line:
return join('[lib]', 'misc', basename(object_name))
return object_name
else:
test_re_obj_name = re.match(self.RE_LIBRARY_OBJECT, line)
if test_re_obj_name:
return join('[lib]', test_re_obj_name.group(2),
test_re_obj_name.group(3))
else:
if (
not line.startswith("LONG") and
not line.startswith("linker stubs")
):
print("Unknown object name found in GCC map file: %s"
% line)
return '[misc]'
def parse_section(self, line):
""" Parse data from a section of gcc map file
examples:
0x00004308 0x7c ./BUILD/K64F/GCC_ARM/spi_api.o
.text 0x00000608 0x198 ./BUILD/K64F/HAL_CM4.o
Positional arguments:
line - the line to parse a section from
"""
is_fill = re.match(self.RE_FILL_SECTION, line)
if is_fill:
o_name = '[fill]'
o_size = int(is_fill.group(2), 16)
return [o_name, o_size]
is_section = re.match(self.RE_STD_SECTION, line)
if is_section:
o_size = int(is_section.group(2), 16)
if o_size:
o_name = self.parse_object_name(is_section.group(3))
return [o_name, o_size]
return ["", 0]
def parse_mapfile(self, file_desc):
""" Main logic to decode gcc map files
Positional arguments:
file_desc - a stream object to parse as a gcc map file
"""
current_section = 'unknown'
with file_desc as infile:
for line in infile:
if line.startswith('Linker script and memory map'):
current_section = "unknown"
break
for line in infile:
next_section = self.check_new_section(line)
if next_section == "OUTPUT":
break
elif next_section:
current_section = next_section
object_name, object_size = self.parse_section(line)
self.module_add(object_name, object_size, current_section)
common_prefix = dirname(commonprefix([
o for o in self.modules.keys()
if (
o.endswith(self.OBJECT_EXTENSIONS)
and not o.startswith("[lib]")
)]))
new_modules = {}
for name, stats in self.modules.items():
if name.startswith("[lib]"):
new_modules[name] = stats
elif name.endswith(self.OBJECT_EXTENSIONS):
new_modules[relpath(name, common_prefix)] = stats
else:
new_modules[name] = stats
return new_modules
class _ArmccParser(_Parser):
RE = re.compile(
r'^\s+0x(\w{8})\s+0x(\w{8})\s+(\w+)\s+(\w+)\s+(\d+)\s+[*]?.+\s+(.+)$')
RE_OBJECT = re.compile(r'(.+\.(l|a|ar))\((.+\.o(bj)?)\)')
OBJECT_EXTENSIONS = (".o", ".obj")
def parse_object_name(self, line):
""" Parse object file
Positional arguments:
line - the line containing the object or library
"""
if line.endswith(self.OBJECT_EXTENSIONS):
return line
else:
is_obj = re.match(self.RE_OBJECT, line)
if is_obj:
return join(
'[lib]', basename(is_obj.group(1)), is_obj.group(3)
)
else:
print(
"Malformed input found when parsing ARMCC map: %s" % line
)
return '[misc]'
def parse_section(self, line):
""" Parse data from an armcc map file
Examples of armcc map file:
Base_Addr Size Type Attr Idx E Section Name Object
0x00000000 0x00000400 Data RO 11222 self.RESET startup_MK64F12.o
0x00000410 0x00000008 Code RO 49364 * !!!main c_w.l(__main.o)
Positional arguments:
line - the line to parse the section data from
""" # noqa: E501
test_re = re.match(self.RE, line)
if (
test_re
and "ARM_LIB_HEAP" not in line
):
size = int(test_re.group(2), 16)
if test_re.group(4) == 'RO':
section = '.text'
else:
if test_re.group(3) == 'Data':
section = '.data'
elif test_re.group(3) == 'Zero':
section = '.bss'
elif test_re.group(3) == 'Code':
section = '.text'
else:
print(
"Malformed input found when parsing armcc map: %s, %r"
% (line, test_re.groups())
)
return ["", 0, ""]
# check name of object or library
object_name = self.parse_object_name(
test_re.group(6))
return [object_name, size, section]
else:
return ["", 0, ""]
def parse_mapfile(self, file_desc):
""" Main logic to decode armc5 map files
Positional arguments:
file_desc - a file like object to parse as an armc5 map file
"""
with file_desc as infile:
# Search area to parse
for line in infile:
if line.startswith(' Base Addr Size'):
break
# Start decoding the map file
for line in infile:
self.module_add(*self.parse_section(line))
common_prefix = dirname(commonprefix([
o for o in self.modules.keys()
if (
o.endswith(self.OBJECT_EXTENSIONS)
and o != "anon$$obj.o"
and o != "anon$$obj.obj"
and not o.startswith("[lib]")
)]))
new_modules = {}
for name, stats in self.modules.items():
if (
name == "anon$$obj.o"
or name == "anon$$obj.obj"
or name.startswith("[lib]")
):
new_modules[name] = stats
elif name.endswith(self.OBJECT_EXTENSIONS):
new_modules[relpath(name, common_prefix)] = stats
else:
new_modules[name] = stats
return new_modules
class _IarParser(_Parser):
RE = re.compile(
r'^\s+(.+)\s+(zero|const|ro code|inited|uninit)\s'
r'+0x([\'\w]+)\s+0x(\w+)\s+(.+)\s.+$')
RE_CMDLINE_FILE = re.compile(r'^#\s+(.+\.o(bj)?)')
RE_LIBRARY = re.compile(r'^(.+\.a)\:.+$')
RE_OBJECT_LIBRARY = re.compile(r'^\s+(.+\.o(bj)?)\s.*')
OBJECT_EXTENSIONS = (".o", ".obj")
def __init__(self):
_Parser.__init__(self)
# Modules passed to the linker on the command line
# this is a dict because modules are looked up by their basename
self.cmd_modules = {}
def parse_object_name(self, object_name):
""" Parse object file
Positional arguments:
line - the line containing the object or library
"""
if object_name.endswith(self.OBJECT_EXTENSIONS):
try:
return self.cmd_modules[object_name]
except KeyError:
return object_name
else:
return '[misc]'
def parse_section(self, line):
""" Parse data from an IAR map file
Examples of IAR map file:
Section Kind Address Size Object
.intvec ro code 0x00000000 0x198 startup_MK64F12.o [15]
.rodata const 0x00000198 0x0 zero_init3.o [133]
.iar.init_table const 0x00008384 0x2c - Linker created -
Initializer bytes const 0x00000198 0xb2 <for P3 s0>
.data inited 0x20000000 0xd4 driverAtmelRFInterface.o [70]
.bss zero 0x20000598 0x318 RTX_Conf_CM.o [4]
.iar.dynexit uninit 0x20001448 0x204 <Block tail>
HEAP uninit 0x20001650 0x10000 <Block tail>
Positional_arguments:
line - the line to parse section data from
""" # noqa: E501
test_re = re.match(self.RE, line)
if test_re:
if (
test_re.group(2) == 'const' or
test_re.group(2) == 'ro code'
):
section = '.text'
elif (test_re.group(2) == 'zero' or
test_re.group(2) == 'uninit'):
if test_re.group(1)[0:4] == 'HEAP':
section = '.heap'
elif test_re.group(1)[0:6] == 'CSTACK':
section = '.stack'
else:
section = '.bss' # default section
elif test_re.group(2) == 'inited':
section = '.data'
else:
print("Malformed input found when parsing IAR map: %s" % line)
return ["", 0, ""]
# lookup object in dictionary and return module name
object_name = self.parse_object_name(test_re.group(5))
size = int(test_re.group(4), 16)
return [object_name, size, section]
else:
return ["", 0, ""]
def check_new_library(self, line):
"""
Searches for libraries and returns name. Example:
m7M_tls.a: [43]
"""
test_address_line = re.match(self.RE_LIBRARY, line)
if test_address_line:
return test_address_line.group(1)
else:
return ""
def check_new_object_lib(self, line):
"""
Searches for objects within a library section and returns name.
Example:
rt7M_tl.a: [44]
ABImemclr4.o 6
ABImemcpy_unaligned.o 118
ABImemset48.o 50
I64DivMod.o 238
I64DivZer.o 2
"""
test_address_line = re.match(self.RE_OBJECT_LIBRARY, line)
if test_address_line:
return test_address_line.group(1)
else:
return ""
def parse_command_line(self, lines):
"""Parse the files passed on the command line to the iar linker
Positional arguments:
lines -- an iterator over the lines within a file
"""
for line in lines:
if line.startswith("*"):
break
for arg in line.split(" "):
arg = arg.rstrip(" \n")
if (
not arg.startswith("-")
and arg.endswith(self.OBJECT_EXTENSIONS)
):
self.cmd_modules[basename(arg)] = arg
common_prefix = dirname(commonprefix(list(self.cmd_modules.values())))
self.cmd_modules = {s: relpath(f, common_prefix)
for s, f in self.cmd_modules.items()}
def parse_mapfile(self, file_desc):
""" Main logic to decode IAR map files
Positional arguments:
file_desc - a file like object to parse as an IAR map file
"""
with file_desc as infile:
self.parse_command_line(infile)
for line in infile:
if line.startswith(' Section '):
break
for line in infile:
self.module_add(*self.parse_section(line))
if line.startswith('*** MODULE SUMMARY'): # finish section
break
current_library = ""
for line in infile:
library = self.check_new_library(line)
if library:
current_library = library
object_name = self.check_new_object_lib(line)
if object_name and current_library:
temp = join('[lib]', current_library, object_name)
self.module_replace(object_name, temp)
return self.modules
class MemapParser(object):
"""An object that represents parsed results, parses the memory map files,
and writes out different file types of memory results
"""
print_sections = ('.text', '.data', '.bss')
delta_sections = ('.text-delta', '.data-delta', '.bss-delta')
# sections to print info (generic for all toolchains)
sections = _Parser.SECTIONS
misc_flash_sections = _Parser.MISC_FLASH_SECTIONS
other_sections = _Parser.OTHER_SECTIONS
def __init__(self):
# list of all modules and their sections
# full list - doesn't change with depth
self.modules = dict()
self.old_modules = None
# short version with specific depth
self.short_modules = dict()
# Memory report (sections + summary)
self.mem_report = []
# Memory summary
self.mem_summary = dict()
# Totals of ".text", ".data" and ".bss"
self.subtotal = dict()
# Flash no associated with a module
self.misc_flash_mem = 0
# Name of the toolchain, for better headings
self.tc_name = None
def reduce_depth(self, depth):
"""
populates the short_modules attribute with a truncated module list
(1) depth = 1:
main.o
mbed-os
(2) depth = 2:
main.o
mbed-os/test.o
mbed-os/drivers
"""
if depth == 0 or depth is None:
self.short_modules = deepcopy(self.modules)
else:
self.short_modules = dict()
for module_name, v in self.modules.items():
split_name = module_name.split(sep)
if split_name[0] == '':
split_name = split_name[1:]
new_name = join(*split_name[:depth])
self.short_modules.setdefault(new_name, defaultdict(int))
for section_idx, value in v.items():
self.short_modules[new_name][section_idx] += value
delta_name = section_idx + '-delta'
self.short_modules[new_name][delta_name] += value
if self.old_modules:
for module_name, v in self.old_modules.items():
split_name = module_name.split(sep)
if split_name[0] == '':
split_name = split_name[1:]
new_name = join(*split_name[:depth])
self.short_modules.setdefault(new_name, defaultdict(int))
for section_idx, value in v.items():
delta_name = section_idx + '-delta'
self.short_modules[new_name][delta_name] -= value
export_formats = ["json", "csv-ci", "html", "table"]
def generate_output(self, export_format, depth, file_output=None):
""" Generates summary of memory map data
Positional arguments:
export_format - the format to dump
Keyword arguments:
file_desc - descriptor (either stdout or file)
depth - directory depth on report
Returns: generated string for the 'table' format, otherwise None
"""
if depth is None or depth > 0:
self.reduce_depth(depth)
self.compute_report()
try:
if file_output:
file_desc = open(file_output, 'w')
else:
file_desc = stdout
except IOError as error:
print("I/O error({0}): {1}".format(error.errno, error.strerror))
return False
to_call = {'json': self.generate_json,
'html': self.generate_html,
'csv-ci': self.generate_csv,
'table': self.generate_table}[export_format]
output = to_call(file_desc)
if file_desc is not stdout:
file_desc.close()
return output
@staticmethod
def _move_up_tree(tree, next_module):
tree.setdefault("children", [])
for child in tree["children"]:
if child["name"] == next_module:
return child
else:
new_module = {"name": next_module, "value": 0, "delta": 0}
tree["children"].append(new_module)
return new_module
def generate_html(self, file_desc):
"""Generate a json file from a memory map for D3
Positional arguments:
file_desc - the file to write out the final report to
"""
tree_text = {"name": ".text", "value": 0, "delta": 0}
tree_bss = {"name": ".bss", "value": 0, "delta": 0}
tree_data = {"name": ".data", "value": 0, "delta": 0}
for name, dct in self.modules.items():
cur_text = tree_text
cur_bss = tree_bss
cur_data = tree_data
modules = name.split(sep)
while True:
try:
cur_text["value"] += dct['.text']
cur_text["delta"] += dct['.text']
except KeyError:
pass
try:
cur_bss["value"] += dct['.bss']
cur_bss["delta"] += dct['.bss']
except KeyError:
pass
try:
cur_data["value"] += dct['.data']
cur_data["delta"] += dct['.data']
except KeyError:
pass
if not modules:
break
next_module = modules.pop(0)
cur_text = self._move_up_tree(cur_text, next_module)
cur_data = self._move_up_tree(cur_data, next_module)
cur_bss = self._move_up_tree(cur_bss, next_module)
if self.old_modules:
for name, dct in self.old_modules.items():
cur_text = tree_text
cur_bss = tree_bss
cur_data = tree_data
modules = name.split(sep)
while True:
try:
cur_text["delta"] -= dct['.text']
except KeyError:
pass
try:
cur_bss["delta"] -= dct['.bss']
except KeyError:
pass
try:
cur_data["delta"] -= dct['.data']
except KeyError:
pass
if not modules:
break
next_module = modules.pop(0)
if not any(
cld['name'] == next_module
for cld in cur_text['children']
):
break
cur_text = self._move_up_tree(cur_text, next_module)
cur_data = self._move_up_tree(cur_data, next_module)
cur_bss = self._move_up_tree(cur_bss, next_module)
tree_rom = {
"name": "ROM",
"value": tree_text["value"] + tree_data["value"],
"delta": tree_text["delta"] + tree_data["delta"],
"children": [tree_text, tree_data]
}
tree_ram = {
"name": "RAM",
"value": tree_bss["value"] + tree_data["value"],
"delta": tree_bss["delta"] + tree_data["delta"],
"children": [tree_bss, tree_data]
}
jinja_loader = FileSystemLoader(dirname(abspath(__file__)))
jinja_environment = Environment(loader=jinja_loader,
undefined=StrictUndefined)
template = jinja_environment.get_template("memap_flamegraph.html")
name, _ = splitext(basename(file_desc.name))
if name.endswith("_map"):
name = name[:-4]
if self.tc_name:
name = "%s %s" % (name, self.tc_name)
data = {
"name": name,
"rom": json.dumps(tree_rom),
"ram": json.dumps(tree_ram),
}
file_desc.write(template.render(data))
return None
def generate_json(self, file_desc):
"""Generate a json file from a memory map
Positional arguments:
file_desc - the file to write out the final report to
"""
file_desc.write(json.dumps(self.mem_report, indent=4))
file_desc.write('\n')
return None
RAM_FORMAT_STR = (
"Total Static RAM memory (data + bss): {}({:+}) bytes\n"
)
ROM_FORMAT_STR = (
"Total Flash memory (text + data): {}({:+}) bytes\n"
)
def generate_csv(self, file_desc):
"""Generate a CSV file from a memoy map
Positional arguments:
file_desc - the file to write out the final report to
"""
writer = csv.writer(file_desc, delimiter=',',
quoting=csv.QUOTE_MINIMAL)
module_section = []
sizes = []
for i in sorted(self.short_modules):
for k in self.print_sections + self.delta_sections:
module_section.append((i + k))
sizes += [self.short_modules[i][k]]
module_section.append('static_ram')
sizes.append(self.mem_summary['static_ram'])
module_section.append('total_flash')
sizes.append(self.mem_summary['total_flash'])
writer.writerow(module_section)
writer.writerow(sizes)
return None
def generate_table(self, file_desc):
"""Generate a table from a memoy map
Returns: string of the generated table
"""
# Create table
columns = ['Module']
columns.extend(self.print_sections)
table = PrettyTable(columns, junction_char="|", hrules=HEADER)
table.align["Module"] = "l"
for col in self.print_sections:
table.align[col] = 'r'
for i in list(self.print_sections):
table.align[i] = 'r'
for i in sorted(self.short_modules):
row = [i]
for k in self.print_sections:
row.append("{}({:+})".format(
self.short_modules[i][k],
self.short_modules[i][k + "-delta"]
))
table.add_row(row)
subtotal_row = ['Subtotals']
for k in self.print_sections:
subtotal_row.append("{}({:+})".format(
self.subtotal[k], self.subtotal[k + '-delta']))
table.add_row(subtotal_row)
output = table.get_string()
output += '\n'
output += self.RAM_FORMAT_STR.format(
self.mem_summary['static_ram'],
self.mem_summary['static_ram_delta']
)
output += self.ROM_FORMAT_STR.format(
self.mem_summary['total_flash'],
self.mem_summary['total_flash_delta']
)
return output
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
def compute_report(self):
""" Generates summary of memory usage for main areas
"""
self.subtotal = defaultdict(int)
for mod in self.modules.values():
for k in self.sections:
self.subtotal[k] += mod[k]
self.subtotal[k + '-delta'] += mod[k]
if self.old_modules:
for mod in self.old_modules.values():
for k in self.sections:
self.subtotal[k + '-delta'] -= mod[k]
self.mem_summary = {
'static_ram': self.subtotal['.data'] + self.subtotal['.bss'],
'static_ram_delta':
self.subtotal['.data-delta'] + self.subtotal['.bss-delta'],
'total_flash': (self.subtotal['.text'] + self.subtotal['.data']),
'total_flash_delta':
self.subtotal['.text-delta'] + self.subtotal['.data-delta'],
}
self.mem_report = []
if self.short_modules:
for name, sizes in sorted(self.short_modules.items()):
self.mem_report.append({
"module": name,
"size": {
k: sizes.get(k, 0) for k in (self.print_sections +
self.delta_sections)
}
})
self.mem_report.append({
'summary': self.mem_summary
})
def parse(self, mapfile, toolchain):
""" Parse and decode map file depending on the toolchain
Positional arguments:
mapfile - the file name of the memory map file
toolchain - the toolchain used to create the file
"""
self.tc_name = toolchain.title()
if toolchain in ("ARM", "ARM_STD", "ARM_MICRO", "ARMC6"):
parser = _ArmccParser
elif toolchain == "GCC_ARM":
parser = _GccParser
elif toolchain == "IAR":
parser = _IarParser
else:
return False
try:
with open(mapfile, 'r') as file_input:
self.modules = parser().parse_mapfile(file_input)
try:
with open("%s.old" % mapfile, 'r') as old_input:
self.old_modules = parser().parse_mapfile(old_input)
except IOError:
self.old_modules = None
return True
except IOError as error:
print("I/O error({0}): {1}".format(error.errno, error.strerror))
return False
def main():
"""Entry Point"""
version = '0.4.0'
# Parser handling
parser = ArgumentParser(
description="Memory Map File Analyser for ARM mbed\nversion %s" %
version)
parser.add_argument(
'file', type=argparse_filestring_type, help='memory map file')
parser.add_argument(
'-t', '--toolchain', dest='toolchain',
help='select a toolchain used to build the memory map file (%s)' %
", ".join(MemapParser.toolchains),
required=True,
type=argparse_uppercase_type(MemapParser.toolchains, "toolchain"))
parser.add_argument(
'-d', '--depth', dest='depth', type=int,
help='specify directory depth level to display report', required=False)
parser.add_argument(
'-o', '--output', help='output file name', required=False)
parser.add_argument(
'-e', '--export', dest='export', required=False, default='table',
type=argparse_lowercase_hyphen_type(MemapParser.export_formats,
'export format'),
help="export format (examples: %s: default)" %
", ".join(MemapParser.export_formats))
parser.add_argument('-v', '--version', action='version', version=version)
# Parse/run command
if len(argv) <= 1:
parser.print_help()
exit(1)
args = parser.parse_args()
# Create memap object
memap = MemapParser()
# Parse and decode a map file
if args.file and args.toolchain:
if memap.parse(args.file, args.toolchain) is False:
exit(0)
if args.depth is None:
depth = 2 # default depth level
else:
depth = args.depth
returned_string = None
# Write output in file
if args.output is not None:
returned_string = memap.generate_output(
args.export,
depth,
args.output
)
else: # Write output in screen
returned_string = memap.generate_output(args.export, depth)
if args.export == 'table' and returned_string:
print(returned_string)
exit(0)
if __name__ == "__main__":
main()
+85
View File
@@ -0,0 +1,85 @@
# memap - Static Memory Map Analysis
## Introduction
_memap_ is a simple utility that displays static memory information required by [mbed](https://github.com/mbedmicro/mbed) applications. This information is produced by analysing the memory map file previously generated by your toolchain.
**Note**: this tool shows static RAM usage and the total size of allocated heap and stack space defined at compile time, not the actual heap and stack usage (which may be different depending on your application).
## Table of contents
1. [Using memap](#using-memap)
2. [Information on memory sections](#info-mem-sections)
3. [Current support](#current-support)
4. [Known problems](#known-problems)
## Using memap
_memap_ is automatically invoked after an mbed build finishes successfully. Its also possible to manually run the program with different command line options, for example:
$> python memap.py
usage: memap.py [-h] -t TOOLCHAIN [-o OUTPUT] [-e EXPORT] [-v] file
Memory Map File Analyser for ARM mbed version 0.3.11
positional arguments:
file memory map file
optional arguments:
-h, --help show this help message and exit
-t TOOLCHAIN, --toolchain TOOLCHAIN
select a toolchain used to build the memory map file
(ARM, GCC_ARM, IAR)
-o OUTPUT, --output OUTPUT
output file name
-e EXPORT, --export EXPORT
export format (examples: 'json', 'csv-ci', 'table':
default)
-v, --version show program's version number and exit
Result example:
$> python memap.py GCC_ARM\myprog3.map -t GCC_ARM
+----------------------------+-------+-------+------+
| Module | .text | .data | .bss |
+----------------------------+-------+-------+------+
| Fill | 170 | 0 | 2294 |
| Misc | 36282 | 2220 | 2152 |
| core/hal | 15396 | 16 | 568 |
| core/rtos | 6751 | 24 | 2662 |
| features/FEATURE_IPV4 | 96 | 0 | 48 |
| frameworks/greentea-client | 912 | 28 | 44 |
| frameworks/utest | 3079 | 0 | 732 |
| Subtotals | 62686 | 2288 | 8500 |
+----------------------------+-------+-------+------+
Allocated Heap: 65540 bytes
Allocated Stack: 32768 bytes
Total Static RAM memory (data + bss): 10788 bytes
Total RAM memory (data + bss + heap + stack): 109096 bytes
Total Flash memory (text + data + misc): 66014 bytes
## Information on memory sections
The table above showed multiple memory sections.
* `.text`: is where the code application and constants are located in Flash.
* `.data`: non-zero initialized variables; allocated in both RAM and Flash memory (variables are copied from Flash to RAM at run time)
* `.bss`: uninitialized data allocated in RAM, or variables initialized to zero.
* `Heap`: dynamic allocations in the Heap area in RAM (for example, used by `malloc`). The maximum size value may be defined at build time.
* `Stack`: dynamic allocations in the Stack area in RAM (for example, used to store local data, temporary data when branching to a subroutine or context switch information). The maximum size value may be defined at build time.
There are other entries that require a bit of clarification:
* Fill: represents the bytes in multiple sections (RAM and Flash) that the toolchain has filled with zeros because it requires subsequent data or code to be aligned appropriately in memory.
* Misc: usually represents helper libraries introduced by the toolchain (like `libc`), but can also represent modules that are not part of mbed.
## Current support
_memap_ has been tested on Windows 7, Linux and Mac OS X and works with memory map files are generated by the GCC_ARM, ARM (ARM Compiler 5) and IAR toochains.
## Known issues and new features
This utility is considered alpha quality at the moment. The information generated by this utility may not be fully accurate and may vary from one toolchain to another.
If you are experiencing problems, or would like additional features, please raise a ticket on [GitHub](https://github.com/mbedmicro/mbed/issues) and use `[memap]` in the title.
+617
View File
@@ -0,0 +1,617 @@
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited
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.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function, division, absolute_import
import sys
import inspect
import os
import argparse
import math
from os import listdir, remove, makedirs
from shutil import copyfile
from os.path import isdir, join, exists, split, relpath, splitext, abspath
from os.path import commonprefix, normpath, dirname
from subprocess import Popen, PIPE, STDOUT, call
from math import ceil
import json
from collections import OrderedDict
import logging
from intelhex import IntelHex
import io
try:
unicode
except NameError:
unicode = str
def remove_if_in(lst, thing):
if thing in lst:
lst.remove(thing)
def compile_worker(job):
"""Standard task runner used for compiling
Positional argumets:
job - a dict containing a list of commands and the remaining arguments
to run_cmd
"""
results = []
for command in job['commands']:
try:
_, _stderr, _rc = run_cmd(command, work_dir=job['work_dir'],
chroot=job['chroot'])
except KeyboardInterrupt:
raise ToolException
results.append({
'code': _rc,
'output': _stderr,
'command': command
})
return {
'source': job['source'],
'object': job['object'],
'commands': job['commands'],
'results': results
}
def cmd(command, check=True, verbose=False, shell=False, cwd=None):
"""A wrapper to run a command as a blocking job"""
text = command if shell else ' '.join(command)
if verbose:
print(text)
return_code = call(command, shell=shell, cwd=cwd)
if check and return_code != 0:
raise Exception('ERROR %d: "%s"' % (return_code, text))
def run_cmd(command, work_dir=None, chroot=None, redirect=False):
"""Run a command in the foreground
Positional arguments:
command - the command to run
Keyword arguments:
work_dir - the working directory to run the command in
chroot - the chroot to run the command in
redirect - redirect the stderr to a pipe to be used later
"""
if chroot:
# Conventions managed by the web team for the mbed.org build system
chroot_cmd = [
'/usr/sbin/chroot', '--userspec=33:33', chroot
]
for element in command:
chroot_cmd += [element.replace(chroot, '')]
logging.debug("Running command %s", ' '.join(chroot_cmd))
command = chroot_cmd
work_dir = None
try:
process = Popen(command, stdout=PIPE,
stderr=STDOUT if redirect else PIPE, cwd=work_dir,
universal_newlines=True)
_stdout, _stderr = process.communicate()
except OSError:
print("[OS ERROR] Command: "+(' '.join(command)))
raise
return _stdout, _stderr, process.returncode
def run_cmd_ext(command):
""" A version of run command that checks if the command exists befor running
Positional arguments:
command - the command line you are trying to invoke
"""
assert is_cmd_valid(command[0])
process = Popen(command, stdout=PIPE, stderr=PIPE)
_stdout, _stderr = process.communicate()
return _stdout, _stderr, process.returncode
def is_cmd_valid(command):
""" Verify that a command exists and is executable
Positional arguments:
command - the command to check
"""
caller = get_caller_name()
cmd_path = find_cmd_abspath(command)
if not cmd_path:
error("%s: Command '%s' can't be found" % (caller, command))
if not is_exec(cmd_path):
error("%s: Command '%s' resolves to file '%s' which is not executable"
% (caller, command, cmd_path))
return True
def is_exec(path):
"""A simple check to verify that a path to an executable exists
Positional arguments:
path - the executable
"""
return os.access(path, os.X_OK) or os.access(path+'.exe', os.X_OK)
def find_cmd_abspath(command):
""" Returns the absolute path to a command.
None is returned if no absolute path was found.
Positional arguhments:
command - the command to find the path of
"""
if exists(command) or exists(command + '.exe'):
return os.path.abspath(command)
if not 'PATH' in os.environ:
raise Exception("Can't find command path for current platform ('%s')"
% sys.platform)
path_env = os.environ['PATH']
for path in path_env.split(os.pathsep):
cmd_path = '%s/%s' % (path, command)
if exists(cmd_path) or exists(cmd_path + '.exe'):
return cmd_path
def mkdir(path):
""" a wrapped makedirs that only tries to create a directory if it does not
exist already
Positional arguments:
path - the path to maybe create
"""
if not exists(path):
makedirs(path)
def write_json_to_file(json_data, file_name):
"""
Write json content in file
:param json_data:
:param file_name:
:return:
"""
# Create the target dir for file if necessary
test_spec_dir = os.path.dirname(file_name)
if test_spec_dir:
mkdir(test_spec_dir)
try:
with open(file_name, 'w') as f:
f.write(json.dumps(json_data, indent=2))
except IOError as e:
print("[ERROR] Error writing test spec to file")
print(e)
def copy_file(src, dst):
""" Implement the behaviour of "shutil.copy(src, dst)" without copying the
permissions (this was causing errors with directories mounted with samba)
Positional arguments:
src - the source of the copy operation
dst - the destination of the copy operation
"""
if isdir(dst):
_, base = split(src)
dst = join(dst, base)
copyfile(src, dst)
def copy_when_different(src, dst):
""" Only copy the file when it's different from its destination.
Positional arguments:
src - the source of the copy operation
dst - the destination of the copy operation
"""
if isdir(dst):
_, base = split(src)
dst = join(dst, base)
if exists(dst):
with open(src, 'rb') as srcfd, open(dst, 'rb') as dstfd:
if srcfd.read() == dstfd.read():
return
copyfile(src, dst)
def delete_dir_files(directory):
""" A function that does rm -rf
Positional arguments:
directory - the directory to remove
"""
if not exists(directory):
return
for element in listdir(directory):
to_remove = join(directory, element)
if not isdir(to_remove):
remove(to_remove)
def get_caller_name(steps=2):
"""
When called inside a function, it returns the name
of the caller of that function.
Keyword arguments:
steps - the number of steps up the stack the calling function is
"""
return inspect.stack()[steps][3]
def error(msg):
"""Fatal error, abort hard
Positional arguments:
msg - the message to print before crashing
"""
print("ERROR: %s" % msg)
sys.exit(1)
def rel_path(path, base, dot=False):
"""Relative path calculation that optionaly always starts with a dot
Positional arguments:
path - the path to make relative
base - what to make the path relative to
Keyword arguments:
dot - if True, the path will always start with a './'
"""
final_path = relpath(path, base)
if dot and not final_path.startswith('.'):
final_path = './' + final_path
return final_path
class ToolException(Exception):
"""A class representing an exception throw by the tools"""
pass
class NotSupportedException(Exception):
"""A class a toolchain not supporting a particular target"""
pass
class InvalidReleaseTargetException(Exception):
pass
class NoValidToolchainException(Exception):
"""A class representing no valid toolchain configurations found on
the system"""
pass
def split_path(path):
"""spilt a file name into it's directory name, base name, and extension
Positional arguments:
path - the file name to split
"""
base, has_ext = split(path)
name, ext = splitext(has_ext)
return base, name, ext
def get_path_depth(path):
""" Given a path, return the number of directory levels present.
This roughly translates to the number of path separators (os.sep) + 1.
Ex. Given "path/to/dir", this would return 3
Special cases: "." and "/" return 0
Positional arguments:
path - the path to calculate the depth of
"""
normalized_path = normpath(path)
path_depth = 0
head, tail = split(normalized_path)
while tail and tail != '.':
path_depth += 1
head, tail = split(head)
return path_depth
def args_error(parser, message):
"""Abort with an error that was generated by the arguments to a CLI program
Positional arguments:
parser - the ArgumentParser object that parsed the command line
message - what went wrong
"""
parser.exit(status=2, message=message+'\n')
def construct_enum(**enums):
""" Create your own pseudo-enums
Keyword arguments:
* - a member of the Enum you are creating and it's value
"""
return type('Enum', (), enums)
def check_required_modules(required_modules, verbose=True):
""" Function checks for Python modules which should be "importable"
before test suite can be used.
@return returns True if all modules are installed already
"""
import imp
not_installed_modules = []
for module_name in required_modules:
try:
imp.find_module(module_name)
except ImportError:
# We also test against a rare case: module is an egg file
try:
__import__(module_name)
except ImportError as exc:
not_installed_modules.append(module_name)
if verbose:
print("Error: %s" % exc)
if verbose:
if not_installed_modules:
print("Warning: Module(s) %s not installed. Please install "
"required module(s) before using this script."
% (', '.join(not_installed_modules)))
if not_installed_modules:
return False
else:
return True
def _ordered_dict_collapse_dups(pair_list):
to_ret = OrderedDict()
for key, value in pair_list:
if key in to_ret:
if isinstance(to_ret[key], dict):
to_ret[key].update(value)
elif isinstance(to_ret[key], list):
to_ret[key].extend(value)
else:
raise ValueError(
"Key %s found twice and is not mergeable" % key
)
else:
to_ret[key] = value
return to_ret
def json_file_to_dict(fname):
""" Read a JSON file and return its Python representation, transforming all
the strings from Unicode to ASCII. The order of keys in the JSON file is
preserved.
Positional arguments:
fname - the name of the file to parse
"""
try:
with io.open(fname, encoding='ascii',
errors='ignore') as file_obj:
return json.load(
file_obj, object_pairs_hook=_ordered_dict_collapse_dups
)
except (ValueError, IOError) as e:
sys.stderr.write("Error parsing '%s': %s\n" % (fname, e))
raise
# Wowza, double closure
def argparse_type(casedness, prefer_hyphen=False):
def middle(lst, type_name):
def parse_type(string):
""" validate that an argument passed in (as string) is a member of
the list of possible arguments. Offer a suggestion if the case of
the string, or the hyphens/underscores do not match the expected
style of the argument.
"""
if not isinstance(string, unicode):
string = string.decode()
if prefer_hyphen:
newstring = casedness(string).replace("_", "-")
else:
newstring = casedness(string).replace("-", "_")
if string in lst:
return string
elif string not in lst and newstring in lst:
raise argparse.ArgumentTypeError(
"{0} is not a supported {1}. Did you mean {2}?".format(
string, type_name, newstring))
else:
raise argparse.ArgumentTypeError(
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
format(string, type_name, columnate(lst)))
return parse_type
return middle
# short cuts for the argparse_type versions
argparse_uppercase_type = argparse_type(unicode.upper, False)
argparse_lowercase_type = argparse_type(unicode.lower, False)
argparse_uppercase_hyphen_type = argparse_type(unicode.upper, True)
argparse_lowercase_hyphen_type = argparse_type(unicode.lower, True)
def argparse_force_type(case):
""" validate that an argument passed in (as string) is a member of the list
of possible arguments after converting it's case.
"""
def middle(lst, type_name):
""" The parser type generator"""
if not isinstance(lst[0], unicode):
lst = [o.decode() for o in lst]
def parse_type(string):
""" The parser type"""
if not isinstance(string, unicode):
string = string.decode()
for option in lst:
if case(string) == case(option):
return option
raise argparse.ArgumentTypeError(
"{0} is not a supported {1}. Supported {1}s are:\n{2}".
format(string, type_name, columnate(lst)))
return parse_type
return middle
# these two types convert the case of their arguments _before_ validation
argparse_force_uppercase_type = argparse_force_type(unicode.upper)
argparse_force_lowercase_type = argparse_force_type(unicode.lower)
def argparse_many(func):
""" An argument parser combinator that takes in an argument parser and
creates a new parser that accepts a comma separated list of the same thing.
"""
def wrap(string):
""" The actual parser"""
return [func(s) for s in string.split(",")]
return wrap
def argparse_filestring_type(string):
""" An argument parser that verifies that a string passed in corresponds
to a file"""
if exists(string):
return string
else:
raise argparse.ArgumentTypeError(
"{0}"" does not exist in the filesystem.".format(string))
def argparse_profile_filestring_type(string):
""" An argument parser that verifies that a string passed in is either
absolute path or a file name (expanded to
mbed-os/tools/profiles/<fname>.json) of a existing file"""
fpath = join(dirname(__file__), "profiles/{}.json".format(string))
# default profiles are searched first, local ones next.
if exists(fpath):
return fpath
elif exists(string):
return string
else:
raise argparse.ArgumentTypeError(
"{0} does not exist in the filesystem.".format(string))
def columnate(strings, separator=", ", chars=80):
""" render a list of strings as a in a bunch of columns
Positional arguments:
strings - the strings to columnate
Keyword arguments;
separator - the separation between the columns
chars - the maximum with of a row
"""
col_width = max(len(s) for s in strings)
total_width = col_width + len(separator)
columns = math.floor(chars / total_width)
output = ""
for i, string in zip(range(len(strings)), strings):
append = string
if i != len(strings) - 1:
append += separator
if i % columns == columns - 1:
append += "\n"
else:
append = append.ljust(total_width)
output += append
return output
def argparse_dir_not_parent(other):
"""fail if argument provided is a parent of the specified directory"""
def parse_type(not_parent):
"""The parser type"""
abs_other = abspath(other)
abs_not_parent = abspath(not_parent)
if abs_not_parent == commonprefix([abs_not_parent, abs_other]):
raise argparse.ArgumentTypeError(
"{0} may not be a parent directory of {1}".format(
not_parent, other))
else:
return not_parent
return parse_type
def argparse_deprecate(replacement_message):
"""fail if argument is provided with deprecation warning"""
def parse_type(_):
"""The parser type"""
raise argparse.ArgumentTypeError("Deprecated." + replacement_message)
return parse_type
def print_large_string(large_string):
""" Breaks a string up into smaller pieces before print them
This is a limitation within Windows, as detailed here:
https://bugs.python.org/issue11395
Positional arguments:
large_string - the large string to print
"""
string_limit = 1000
large_string_len = len(large_string)
num_parts = int(ceil(float(large_string_len) / float(string_limit)))
for string_part in range(num_parts):
start_index = string_part * string_limit
if string_part == num_parts - 1:
sys.stdout.write(large_string[start_index:])
else:
sys.stdout.write(large_string[start_index:
start_index + string_limit])
sys.stdout.write("\n")
def intelhex_offset(filename, offset):
"""Load a hex or bin file at a particular offset"""
_, inteltype = splitext(filename)
ih = IntelHex()
if inteltype == ".bin":
ih.loadbin(filename, offset=offset)
elif inteltype == ".hex":
ih.loadhex(filename)
else:
raise ToolException("File %s does not have a known binary file type"
% filename)
return ih
def integer(maybe_string, base):
"""Make an integer of a number or a string"""
if isinstance(maybe_string, int):
return maybe_string
else:
return int(maybe_string, base)
def generate_update_filename(name, target):
return "%s_update.%s" % (
name,
getattr(target, "OUTPUT_EXT_UPDATE", "bin")
)
def print_end_warnings(end_warnings):
""" Print a formatted list of warnings
Positional arguments:
end_warnings - A list of warnings (strings) to print
"""
if end_warnings:
warning_separator = "-" * 60
print(warning_separator)
for end_warning in end_warnings:
print(end_warning)
print(warning_separator)