mirror of https://github.com/ARMmbed/mbed-os.git
Fixed pylint issues
parent
07c3a108ef
commit
aafd19992b
146
tools/memap.py
146
tools/memap.py
|
@ -1,16 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
# pylint: disable=too-many-arguments, too-many-locals, too-many-branches, too-many-lines, line-too-long, too-many-nested-blocks, too-many-public-methods, too-many-instance-attributes
|
||||
# pylint: disable=invalid-name, missing-docstring
|
||||
|
||||
# Memory Map File Analyser for ARM mbed OS
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import string
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import time
|
||||
import string
|
||||
import StringIO
|
||||
import argparse
|
||||
from prettytable import PrettyTable
|
||||
|
||||
debug = False
|
||||
|
@ -27,14 +26,15 @@ class MemmapParser(object):
|
|||
|
||||
self.misc_flash_sections = ('.interrupts', '.flash_config')
|
||||
|
||||
self.other_sections = ('.interrupts_ram', '.init', '.ARM.extab', '.ARM.exidx', '.ARM.attributes', \
|
||||
'.eh_frame', '.init_array', '.fini_array', '.jcr', '.stab', '.stabstr', \
|
||||
'.ARM.exidx','.ARM' )
|
||||
self.other_sections = ('.interrupts_ram', '.init', '.ARM.extab', \
|
||||
'.ARM.exidx', '.ARM.attributes', '.eh_frame', \
|
||||
'.init_array', '.fini_array', '.jcr', '.stab', \
|
||||
'.stabstr', '.ARM.exidx', '.ARM')
|
||||
|
||||
# sections to print info (generic for all toolchains)
|
||||
self.sections = ('.text', '.data', '.bss', '.heap', '.stack',)
|
||||
self.sections = ('.text', '.data', '.bss', '.heap', '.stack')
|
||||
|
||||
# need to have sections merged in this order ()
|
||||
# sections must be defined in this order to take irrelevant out
|
||||
self.all_sections = self.sections + self.other_sections + \
|
||||
self.misc_flash_sections + ('unknown', 'OUTPUT')
|
||||
|
||||
|
@ -44,17 +44,15 @@ class MemmapParser(object):
|
|||
self.object_to_module = dict()
|
||||
|
||||
|
||||
def generate_output(self, file, json_mode):
|
||||
def generate_output(self, file_desc, json_mode):
|
||||
"""
|
||||
Generates summary of memory map data
|
||||
|
||||
Parameters
|
||||
file: descriptor (either stdout or file)
|
||||
file_desc: descriptor (either stdout or file)
|
||||
json_mode: generates output in json formal (True/False)
|
||||
"""
|
||||
|
||||
buf = StringIO.StringIO()
|
||||
|
||||
# Calculate misc flash sections
|
||||
misc_flash_mem = 0
|
||||
for i in self.modules:
|
||||
|
@ -86,7 +84,9 @@ class MemmapParser(object):
|
|||
for k in self.print_sections:
|
||||
row.append(self.modules[i][k])
|
||||
|
||||
json_obj.append({ "module":i, "size":{k:self.modules[i][k] for k in self.print_sections}})
|
||||
json_obj.append({"module":i, "size":{\
|
||||
k:self.modules[i][k] for k in self.print_sections}})
|
||||
|
||||
table.add_row(row)
|
||||
|
||||
subtotal_row = ['Subtotals']
|
||||
|
@ -96,22 +96,24 @@ class MemmapParser(object):
|
|||
table.add_row(subtotal_row)
|
||||
|
||||
if json_mode:
|
||||
json_obj.append({ "summary":{'static_ram':(subtotal['.data']+subtotal['.bss']),
|
||||
'heap':(subtotal['.heap']),
|
||||
'stack':(subtotal['.stack']),
|
||||
'total_ram':(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack']),
|
||||
json_obj.append({\
|
||||
'summary':{\
|
||||
'static_ram':(subtotal['.data']+subtotal['.bss']),\
|
||||
'heap':(subtotal['.heap']),\
|
||||
'stack':(subtotal['.stack']),\
|
||||
'total_ram':(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack']),\
|
||||
'total_flash':(subtotal['.text']+subtotal['.data']+misc_flash_mem),}})
|
||||
|
||||
file.write(json.dumps(json_obj, indent=4))
|
||||
file.write('\n')
|
||||
file_desc.write(json.dumps(json_obj, indent=4))
|
||||
file_desc.write('\n')
|
||||
else:
|
||||
file.write(table.get_string())
|
||||
file.write('\n')
|
||||
file.write("Static RAM memory (data + bss): %s\n" % (str(subtotal['.data']+subtotal['.bss'])))
|
||||
file.write("Heap: %s\n" % str(subtotal['.heap']))
|
||||
file.write("Stack: %s\n" % str(subtotal['.stack']))
|
||||
file.write("Total RAM memory (data + bss + heap + stack): %s\n" % (str(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack'])))
|
||||
file.write("Total Flash memory (text + data + misc): %s\n" % (str(subtotal['.text']+subtotal['.data']+misc_flash_mem)))
|
||||
file_desc.write(table.get_string())
|
||||
file_desc.write('\n')
|
||||
file_desc.write("Static RAM memory (data + bss): %s\n" % (str(subtotal['.data']+subtotal['.bss'])))
|
||||
file_desc.write("Heap: %s\n" % str(subtotal['.heap']))
|
||||
file_desc.write("Stack: %s\n" % str(subtotal['.stack']))
|
||||
file_desc.write("Total RAM memory (data + bss + heap + stack): %s\n" % (str(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack'])))
|
||||
file_desc.write("Total Flash memory (text + data + misc): %s\n" % (str(subtotal['.text']+subtotal['.data']+misc_flash_mem)))
|
||||
return
|
||||
|
||||
def module_add(self, module_name, size, section):
|
||||
|
@ -123,38 +125,11 @@ class MemmapParser(object):
|
|||
self.modules[module_name][section] += size
|
||||
else:
|
||||
temp_dic = dict()
|
||||
for x in self.all_sections:
|
||||
temp_dic[x] = 0
|
||||
for section_idx in self.all_sections:
|
||||
temp_dic[section_idx] = 0
|
||||
temp_dic[section] = size
|
||||
self.modules[module_name] = temp_dic
|
||||
|
||||
def find_start_gcc(self,line):
|
||||
"""
|
||||
Checks location of gcc map file to start parsing map file
|
||||
"""
|
||||
if line.startswith('Linker script and memory map'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def find_start_armcc(self,line):
|
||||
"""
|
||||
Checks location of armcc map file to start parsing map file
|
||||
"""
|
||||
if line.startswith(' Base Addr Size'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def find_start_iar(self,line):
|
||||
"""
|
||||
Checks location of armcc map file to start parsing map file
|
||||
"""
|
||||
if line.startswith(' Section '):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def check_new_section_gcc(self, line):
|
||||
"""
|
||||
Check whether a new section in a map file has been detected (only applies to gcc)
|
||||
|
@ -214,7 +189,7 @@ class MemmapParser(object):
|
|||
m_size = int(test_address_len_name.group(2), 16)
|
||||
return [m_name, m_size]
|
||||
|
||||
else: # special cortner case for *fill* sections
|
||||
else: # special corner case for *fill* sections
|
||||
# example
|
||||
# *fill* 0x0000abe4 0x4
|
||||
rex_address_len = r'^\s+\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$'
|
||||
|
@ -230,18 +205,18 @@ class MemmapParser(object):
|
|||
else:
|
||||
return ["", 0] # no valid entry
|
||||
|
||||
def parse_map_file_gcc(self, file):
|
||||
def parse_map_file_gcc(self, file_desc):
|
||||
"""
|
||||
Main logic to decode gcc map files
|
||||
"""
|
||||
|
||||
current_section = 'unknown'
|
||||
|
||||
with file as infile:
|
||||
with file_desc as infile:
|
||||
|
||||
# Search area to parse
|
||||
for line in infile:
|
||||
if self.find_start_gcc(line) == True:
|
||||
if line.startswith('Linker script and memory map'):
|
||||
current_section = "unknown"
|
||||
break
|
||||
|
||||
|
@ -331,7 +306,6 @@ class MemmapParser(object):
|
|||
if test_rex_iar.group(2) == 'const' or test_rex_iar.group(2) == 'ro code':
|
||||
section = '.text'
|
||||
elif test_rex_iar.group(2) == 'zero' or test_rex_iar.group(2) == 'uninit':
|
||||
|
||||
if test_rex_iar.group(1)[0:4] == 'HEAP':
|
||||
section = '.heap'
|
||||
elif test_rex_iar.group(1)[0:6] == 'CSTACK':
|
||||
|
@ -357,16 +331,16 @@ class MemmapParser(object):
|
|||
else:
|
||||
return ["", 0, ""] # no valid entry
|
||||
|
||||
def parse_map_file_armcc(self, file):
|
||||
def parse_map_file_armcc(self, file_desc):
|
||||
"""
|
||||
Main logic to decode armcc map files
|
||||
"""
|
||||
|
||||
with file as infile:
|
||||
with file_desc as infile:
|
||||
|
||||
# Search area to parse
|
||||
for line in infile:
|
||||
if self.find_start_armcc(line) == True:
|
||||
if line.startswith(' Base Addr Size'):
|
||||
break
|
||||
|
||||
# Start decoding the map file
|
||||
|
@ -379,16 +353,16 @@ class MemmapParser(object):
|
|||
else:
|
||||
self.module_add(name, size, section)
|
||||
|
||||
def parse_map_file_iar(self, file):
|
||||
def parse_map_file_iar(self, file_desc):
|
||||
"""
|
||||
Main logic to decode armcc map files
|
||||
"""
|
||||
|
||||
with file as infile:
|
||||
with file_desc as infile:
|
||||
|
||||
# Search area to parse
|
||||
for line in infile:
|
||||
if self.find_start_iar(line) == True:
|
||||
if line.startswith(' Section '):
|
||||
break
|
||||
|
||||
# Start decoding the map file
|
||||
|
@ -421,10 +395,10 @@ class MemmapParser(object):
|
|||
print "Warning: specified toolchain doesn't match with path to the memory map file."
|
||||
return
|
||||
|
||||
for root, dirs, files in os.walk(search_path):
|
||||
for file in files:
|
||||
if file.endswith(".o"):
|
||||
module_name, object_name = self.path_object_to_module_name(os.path.join(root, file))
|
||||
for root, obj_files in os.walk(search_path):
|
||||
for obj_file in obj_files:
|
||||
if obj_file.endswith(".o"):
|
||||
module_name, object_name = self.path_object_to_module_name(os.path.join(root, obj_file))
|
||||
|
||||
if object_name in self.object_to_module:
|
||||
print "WARNING: multiple usages of object file: %s" % object_name
|
||||
|
@ -437,7 +411,7 @@ class MemmapParser(object):
|
|||
|
||||
def main():
|
||||
|
||||
version = '0.3.7'
|
||||
version = '0.3.8'
|
||||
time_start = time.clock()
|
||||
|
||||
# Parser handling
|
||||
|
@ -445,12 +419,12 @@ def main():
|
|||
|
||||
parser.add_argument('file', help='memory map file')
|
||||
|
||||
parser.add_argument('-t','--toolchain', dest='toolchain', help='select a toolchain that corresponds to the memory map file (ARM, GCC_ARM, IAR)',
|
||||
parser.add_argument('-t', '--toolchain', dest='toolchain', help='select a toolchain that corresponds to the memory map file (ARM, GCC_ARM, IAR)',\
|
||||
required=True)
|
||||
|
||||
parser.add_argument('-o', '--output', help='output file name', required=False)
|
||||
|
||||
parser.add_argument('-j', '--json', dest='json', required=False, action="store_true",
|
||||
parser.add_argument('-j', '--json', dest='json', required=False, action="store_true",\
|
||||
help='output in JSON formatted list')
|
||||
|
||||
parser.add_argument('-v', '--version', action='version', version=version)
|
||||
|
@ -464,24 +438,24 @@ def main():
|
|||
|
||||
try:
|
||||
file_input = open(args.file, 'rt')
|
||||
except IOError as e:
|
||||
print "I/O error({0}): {1}".format(e.errno, e.strerror)
|
||||
except IOError as error:
|
||||
print "I/O error({0}): {1}".format(error.errno, error.strerror)
|
||||
sys.exit(0)
|
||||
|
||||
# Creates parser object
|
||||
t = MemmapParser()
|
||||
memap = MemmapParser()
|
||||
|
||||
# Decode map file depending on the toolchain
|
||||
if args.toolchain == "ARM":
|
||||
t.search_objects(os.path.abspath(args.file),args.toolchain)
|
||||
t.parse_map_file_armcc(file_input)
|
||||
memap.search_objects(os.path.abspath(args.file), args.toolchain)
|
||||
memap.parse_map_file_armcc(file_input)
|
||||
elif args.toolchain == "GCC_ARM":
|
||||
t.parse_map_file_gcc(file_input)
|
||||
memap.parse_map_file_gcc(file_input)
|
||||
elif args.toolchain == "IAR":
|
||||
print "WARNING: IAR Compiler not fully supported (yet)"
|
||||
print " "
|
||||
t.search_objects(os.path.abspath(args.file),args.toolchain)
|
||||
t.parse_map_file_iar(file_input)
|
||||
memap.search_objects(os.path.abspath(args.file), args.toolchain)
|
||||
memap.parse_map_file_iar(file_input)
|
||||
else:
|
||||
print "Invalid toolchain. Options are: ARM, GCC_ARM, IAR"
|
||||
sys.exit(0)
|
||||
|
@ -490,13 +464,13 @@ def main():
|
|||
if args.output != None:
|
||||
try:
|
||||
file_output = open(args.output, 'w')
|
||||
t.generate_output(file_output,args.json)
|
||||
memap.generate_output(file_output, args.json)
|
||||
file_output.close()
|
||||
except IOError as e:
|
||||
print "I/O error({0}): {1}".format(e.errno, e.strerror)
|
||||
except IOError as error:
|
||||
print "I/O error({0}): {1}".format(error.errno, error.strerror)
|
||||
sys.exit(0)
|
||||
else: # Write output in screen
|
||||
t.generate_output(sys.stdout,args.json)
|
||||
memap.generate_output(sys.stdout, args.json)
|
||||
|
||||
file_input.close()
|
||||
|
||||
|
|
Loading…
Reference in New Issue