Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 #!/usr/bin/env python
marcozecchini 0:9fca2b23d0ba 2
marcozecchini 0:9fca2b23d0ba 3 """Memory Map File Analyser for ARM mbed"""
marcozecchini 0:9fca2b23d0ba 4
marcozecchini 0:9fca2b23d0ba 5 import sys
marcozecchini 0:9fca2b23d0ba 6 import os
marcozecchini 0:9fca2b23d0ba 7 import re
marcozecchini 0:9fca2b23d0ba 8 import csv
marcozecchini 0:9fca2b23d0ba 9 import json
marcozecchini 0:9fca2b23d0ba 10 import argparse
marcozecchini 0:9fca2b23d0ba 11 from copy import deepcopy
marcozecchini 0:9fca2b23d0ba 12 from prettytable import PrettyTable
marcozecchini 0:9fca2b23d0ba 13
marcozecchini 0:9fca2b23d0ba 14 from utils import argparse_filestring_type, \
marcozecchini 0:9fca2b23d0ba 15 argparse_lowercase_hyphen_type, argparse_uppercase_type
marcozecchini 0:9fca2b23d0ba 16
marcozecchini 0:9fca2b23d0ba 17 RE_ARMCC = re.compile(
marcozecchini 0:9fca2b23d0ba 18 r'^\s+0x(\w{8})\s+0x(\w{8})\s+(\w+)\s+(\w+)\s+(\d+)\s+[*]?.+\s+(.+)$')
marcozecchini 0:9fca2b23d0ba 19 RE_IAR = re.compile(
marcozecchini 0:9fca2b23d0ba 20 r'^\s+(.+)\s+(zero|const|ro code|inited|uninit)\s'
marcozecchini 0:9fca2b23d0ba 21 r'+0x(\w{8})\s+0x(\w+)\s+(.+)\s.+$')
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 RE_CMDLINE_FILE_IAR = re.compile(r'^#\s+(.+\.o)')
marcozecchini 0:9fca2b23d0ba 24 RE_LIBRARY_IAR = re.compile(r'^(.+\.a)\:.+$')
marcozecchini 0:9fca2b23d0ba 25 RE_OBJECT_LIBRARY_IAR = re.compile(r'^\s+(.+\.o)\s.*')
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 RE_OBJECT_FILE_GCC = re.compile(r'^(.+\/.+\.o)$')
marcozecchini 0:9fca2b23d0ba 28 RE_LIBRARY_OBJECT_GCC = re.compile(r'^.+\/lib(.+\.a)\((.+\.o)\)$')
marcozecchini 0:9fca2b23d0ba 29 RE_STD_SECTION_GCC = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$')
marcozecchini 0:9fca2b23d0ba 30 RE_FILL_SECTION_GCC = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$')
marcozecchini 0:9fca2b23d0ba 31
marcozecchini 0:9fca2b23d0ba 32 RE_OBJECT_ARMCC = re.compile(r'(.+\.(l|ar))\((.+\.o)\)')
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34
marcozecchini 0:9fca2b23d0ba 35 class MemapParser(object):
marcozecchini 0:9fca2b23d0ba 36 """An object that represents parsed results, parses the memory map files,
marcozecchini 0:9fca2b23d0ba 37 and writes out different file types of memory results
marcozecchini 0:9fca2b23d0ba 38 """
marcozecchini 0:9fca2b23d0ba 39
marcozecchini 0:9fca2b23d0ba 40 print_sections = ('.text', '.data', '.bss')
marcozecchini 0:9fca2b23d0ba 41
marcozecchini 0:9fca2b23d0ba 42 misc_flash_sections = ('.interrupts', '.flash_config')
marcozecchini 0:9fca2b23d0ba 43
marcozecchini 0:9fca2b23d0ba 44 other_sections = ('.interrupts_ram', '.init', '.ARM.extab',
marcozecchini 0:9fca2b23d0ba 45 '.ARM.exidx', '.ARM.attributes', '.eh_frame',
marcozecchini 0:9fca2b23d0ba 46 '.init_array', '.fini_array', '.jcr', '.stab',
marcozecchini 0:9fca2b23d0ba 47 '.stabstr', '.ARM.exidx', '.ARM')
marcozecchini 0:9fca2b23d0ba 48
marcozecchini 0:9fca2b23d0ba 49 # sections to print info (generic for all toolchains)
marcozecchini 0:9fca2b23d0ba 50 sections = ('.text', '.data', '.bss', '.heap', '.stack')
marcozecchini 0:9fca2b23d0ba 51
marcozecchini 0:9fca2b23d0ba 52 def __init__(self):
marcozecchini 0:9fca2b23d0ba 53 """ General initialization
marcozecchini 0:9fca2b23d0ba 54 """
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 # list of all modules and their sections
marcozecchini 0:9fca2b23d0ba 57 self.modules = dict() # full list - doesn't change with depth
marcozecchini 0:9fca2b23d0ba 58 self.short_modules = dict() # short version with specific depth
marcozecchini 0:9fca2b23d0ba 59
marcozecchini 0:9fca2b23d0ba 60 # sections must be defined in this order to take irrelevant out
marcozecchini 0:9fca2b23d0ba 61 self.all_sections = self.sections + self.other_sections + \
marcozecchini 0:9fca2b23d0ba 62 self.misc_flash_sections + ('unknown', 'OUTPUT')
marcozecchini 0:9fca2b23d0ba 63
marcozecchini 0:9fca2b23d0ba 64 # Memory report (sections + summary)
marcozecchini 0:9fca2b23d0ba 65 self.mem_report = []
marcozecchini 0:9fca2b23d0ba 66
marcozecchini 0:9fca2b23d0ba 67 # Just the memory summary section
marcozecchini 0:9fca2b23d0ba 68 self.mem_summary = dict()
marcozecchini 0:9fca2b23d0ba 69
marcozecchini 0:9fca2b23d0ba 70 self.subtotal = dict()
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 self.misc_flash_mem = 0
marcozecchini 0:9fca2b23d0ba 73
marcozecchini 0:9fca2b23d0ba 74 # Modules passed to the linker on the command line
marcozecchini 0:9fca2b23d0ba 75 # this is a dict because modules are looked up by their basename
marcozecchini 0:9fca2b23d0ba 76 self.cmd_modules = {}
marcozecchini 0:9fca2b23d0ba 77
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 def module_add(self, object_name, size, section):
marcozecchini 0:9fca2b23d0ba 80 """ Adds a module / section to the list
marcozecchini 0:9fca2b23d0ba 81
marcozecchini 0:9fca2b23d0ba 82 Positional arguments:
marcozecchini 0:9fca2b23d0ba 83 object_name - name of the entry to add
marcozecchini 0:9fca2b23d0ba 84 size - the size of the module being added
marcozecchini 0:9fca2b23d0ba 85 section - the section the module contributes to
marcozecchini 0:9fca2b23d0ba 86 """
marcozecchini 0:9fca2b23d0ba 87
marcozecchini 0:9fca2b23d0ba 88 if not object_name or not size or not section:
marcozecchini 0:9fca2b23d0ba 89 return
marcozecchini 0:9fca2b23d0ba 90
marcozecchini 0:9fca2b23d0ba 91 if object_name in self.modules:
marcozecchini 0:9fca2b23d0ba 92 self.modules[object_name].setdefault(section, 0)
marcozecchini 0:9fca2b23d0ba 93 self.modules[object_name][section] += size
marcozecchini 0:9fca2b23d0ba 94 return
marcozecchini 0:9fca2b23d0ba 95
marcozecchini 0:9fca2b23d0ba 96 obj_split = os.sep + os.path.basename(object_name)
marcozecchini 0:9fca2b23d0ba 97 for module_path, contents in self.modules.items():
marcozecchini 0:9fca2b23d0ba 98 if module_path.endswith(obj_split) or module_path == object_name:
marcozecchini 0:9fca2b23d0ba 99 contents.setdefault(section, 0)
marcozecchini 0:9fca2b23d0ba 100 contents[section] += size
marcozecchini 0:9fca2b23d0ba 101 return
marcozecchini 0:9fca2b23d0ba 102
marcozecchini 0:9fca2b23d0ba 103 new_module = {section: size}
marcozecchini 0:9fca2b23d0ba 104 self.modules[object_name] = new_module
marcozecchini 0:9fca2b23d0ba 105
marcozecchini 0:9fca2b23d0ba 106 def module_replace(self, old_object, new_object):
marcozecchini 0:9fca2b23d0ba 107 """ Replaces an object name with a new one
marcozecchini 0:9fca2b23d0ba 108 """
marcozecchini 0:9fca2b23d0ba 109 if old_object in self.modules:
marcozecchini 0:9fca2b23d0ba 110 self.modules[new_object] = self.modules[old_object]
marcozecchini 0:9fca2b23d0ba 111 del self.modules[old_object]
marcozecchini 0:9fca2b23d0ba 112
marcozecchini 0:9fca2b23d0ba 113 def check_new_section_gcc(self, line):
marcozecchini 0:9fca2b23d0ba 114 """ Check whether a new section in a map file has been detected (only
marcozecchini 0:9fca2b23d0ba 115 applies to gcc)
marcozecchini 0:9fca2b23d0ba 116
marcozecchini 0:9fca2b23d0ba 117 Positional arguments:
marcozecchini 0:9fca2b23d0ba 118 line - the line to check for a new section
marcozecchini 0:9fca2b23d0ba 119 """
marcozecchini 0:9fca2b23d0ba 120
marcozecchini 0:9fca2b23d0ba 121 for i in self.all_sections:
marcozecchini 0:9fca2b23d0ba 122 if line.startswith(i):
marcozecchini 0:9fca2b23d0ba 123 # should name of the section (assuming it's a known one)
marcozecchini 0:9fca2b23d0ba 124 return i
marcozecchini 0:9fca2b23d0ba 125
marcozecchini 0:9fca2b23d0ba 126 if line.startswith('.'):
marcozecchini 0:9fca2b23d0ba 127 return 'unknown' # all others are classified are unknown
marcozecchini 0:9fca2b23d0ba 128 else:
marcozecchini 0:9fca2b23d0ba 129 return False # everything else, means no change in section
marcozecchini 0:9fca2b23d0ba 130
marcozecchini 0:9fca2b23d0ba 131
marcozecchini 0:9fca2b23d0ba 132 def parse_object_name_gcc(self, line):
marcozecchini 0:9fca2b23d0ba 133 """ Parse a path to object file
marcozecchini 0:9fca2b23d0ba 134
marcozecchini 0:9fca2b23d0ba 135 Positional arguments:
marcozecchini 0:9fca2b23d0ba 136 txt - the path to parse the object and module name from
marcozecchini 0:9fca2b23d0ba 137 """
marcozecchini 0:9fca2b23d0ba 138
marcozecchini 0:9fca2b23d0ba 139 line = line.replace('\\', '/')
marcozecchini 0:9fca2b23d0ba 140 test_re_mbed_os_name = re.match(RE_OBJECT_FILE_GCC, line)
marcozecchini 0:9fca2b23d0ba 141
marcozecchini 0:9fca2b23d0ba 142 if test_re_mbed_os_name:
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144 object_name = test_re_mbed_os_name.group(1)
marcozecchini 0:9fca2b23d0ba 145
marcozecchini 0:9fca2b23d0ba 146 # corner case: certain objects are provided by the GCC toolchain
marcozecchini 0:9fca2b23d0ba 147 if 'arm-none-eabi' in line:
marcozecchini 0:9fca2b23d0ba 148 return '[lib]/misc/' + object_name
marcozecchini 0:9fca2b23d0ba 149 return object_name
marcozecchini 0:9fca2b23d0ba 150
marcozecchini 0:9fca2b23d0ba 151 else:
marcozecchini 0:9fca2b23d0ba 152
marcozecchini 0:9fca2b23d0ba 153 test_re_obj_name = re.match(RE_LIBRARY_OBJECT_GCC, line)
marcozecchini 0:9fca2b23d0ba 154
marcozecchini 0:9fca2b23d0ba 155 if test_re_obj_name:
marcozecchini 0:9fca2b23d0ba 156 object_name = test_re_obj_name.group(1) + '/' + \
marcozecchini 0:9fca2b23d0ba 157 test_re_obj_name.group(2)
marcozecchini 0:9fca2b23d0ba 158
marcozecchini 0:9fca2b23d0ba 159 return '[lib]/' + object_name
marcozecchini 0:9fca2b23d0ba 160
marcozecchini 0:9fca2b23d0ba 161 else:
marcozecchini 0:9fca2b23d0ba 162 print "Unknown object name found in GCC map file: %s" % line
marcozecchini 0:9fca2b23d0ba 163 return '[misc]'
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 def parse_section_gcc(self, line):
marcozecchini 0:9fca2b23d0ba 166 """ Parse data from a section of gcc map file
marcozecchini 0:9fca2b23d0ba 167
marcozecchini 0:9fca2b23d0ba 168 examples:
marcozecchini 0:9fca2b23d0ba 169 0x00004308 0x7c ./BUILD/K64F/GCC_ARM/mbed-os/hal/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/spi_api.o
marcozecchini 0:9fca2b23d0ba 170 .text 0x00000608 0x198 ./BUILD/K64F/GCC_ARM/mbed-os/core/mbed-rtos/rtx/TARGET_CORTEX_M/TARGET_RTOS_M4_M7/TOOLCHAIN_GCC/HAL_CM4.o
marcozecchini 0:9fca2b23d0ba 171
marcozecchini 0:9fca2b23d0ba 172 Positional arguments:
marcozecchini 0:9fca2b23d0ba 173 line - the line to parse a section from
marcozecchini 0:9fca2b23d0ba 174 """
marcozecchini 0:9fca2b23d0ba 175
marcozecchini 0:9fca2b23d0ba 176 is_fill = re.match(RE_FILL_SECTION_GCC, line)
marcozecchini 0:9fca2b23d0ba 177 if is_fill:
marcozecchini 0:9fca2b23d0ba 178 o_name = '[fill]'
marcozecchini 0:9fca2b23d0ba 179 o_size = int(is_fill.group(2), 16)
marcozecchini 0:9fca2b23d0ba 180 return [o_name, o_size]
marcozecchini 0:9fca2b23d0ba 181
marcozecchini 0:9fca2b23d0ba 182 is_section = re.match(RE_STD_SECTION_GCC, line)
marcozecchini 0:9fca2b23d0ba 183 if is_section:
marcozecchini 0:9fca2b23d0ba 184 o_size = int(is_section.group(2), 16)
marcozecchini 0:9fca2b23d0ba 185 if o_size:
marcozecchini 0:9fca2b23d0ba 186 o_name = self.parse_object_name_gcc(is_section.group(3))
marcozecchini 0:9fca2b23d0ba 187 return [o_name, o_size]
marcozecchini 0:9fca2b23d0ba 188
marcozecchini 0:9fca2b23d0ba 189 return ["", 0]
marcozecchini 0:9fca2b23d0ba 190
marcozecchini 0:9fca2b23d0ba 191 def parse_map_file_gcc(self, file_desc):
marcozecchini 0:9fca2b23d0ba 192 """ Main logic to decode gcc map files
marcozecchini 0:9fca2b23d0ba 193
marcozecchini 0:9fca2b23d0ba 194 Positional arguments:
marcozecchini 0:9fca2b23d0ba 195 file_desc - a stream object to parse as a gcc map file
marcozecchini 0:9fca2b23d0ba 196 """
marcozecchini 0:9fca2b23d0ba 197
marcozecchini 0:9fca2b23d0ba 198 current_section = 'unknown'
marcozecchini 0:9fca2b23d0ba 199
marcozecchini 0:9fca2b23d0ba 200 with file_desc as infile:
marcozecchini 0:9fca2b23d0ba 201 for line in infile:
marcozecchini 0:9fca2b23d0ba 202 if line.startswith('Linker script and memory map'):
marcozecchini 0:9fca2b23d0ba 203 current_section = "unknown"
marcozecchini 0:9fca2b23d0ba 204 break
marcozecchini 0:9fca2b23d0ba 205
marcozecchini 0:9fca2b23d0ba 206 for line in infile:
marcozecchini 0:9fca2b23d0ba 207 next_section = self.check_new_section_gcc(line)
marcozecchini 0:9fca2b23d0ba 208
marcozecchini 0:9fca2b23d0ba 209 if next_section == "OUTPUT":
marcozecchini 0:9fca2b23d0ba 210 break
marcozecchini 0:9fca2b23d0ba 211 elif next_section:
marcozecchini 0:9fca2b23d0ba 212 current_section = next_section
marcozecchini 0:9fca2b23d0ba 213
marcozecchini 0:9fca2b23d0ba 214 object_name, object_size = self.parse_section_gcc(line)
marcozecchini 0:9fca2b23d0ba 215
marcozecchini 0:9fca2b23d0ba 216 self.module_add(object_name, object_size, current_section)
marcozecchini 0:9fca2b23d0ba 217
marcozecchini 0:9fca2b23d0ba 218 common_prefix = os.path.dirname(os.path.commonprefix([
marcozecchini 0:9fca2b23d0ba 219 o for o in self.modules.keys() if (o.endswith(".o") and not o.startswith("[lib]"))]))
marcozecchini 0:9fca2b23d0ba 220 new_modules = {}
marcozecchini 0:9fca2b23d0ba 221 for name, stats in self.modules.items():
marcozecchini 0:9fca2b23d0ba 222 if name.startswith("[lib]"):
marcozecchini 0:9fca2b23d0ba 223 new_modules[name] = stats
marcozecchini 0:9fca2b23d0ba 224 elif name.endswith(".o"):
marcozecchini 0:9fca2b23d0ba 225 new_modules[os.path.relpath(name, common_prefix)] = stats
marcozecchini 0:9fca2b23d0ba 226 else:
marcozecchini 0:9fca2b23d0ba 227 new_modules[name] = stats
marcozecchini 0:9fca2b23d0ba 228 self.modules = new_modules
marcozecchini 0:9fca2b23d0ba 229
marcozecchini 0:9fca2b23d0ba 230 def parse_object_name_armcc(self, line):
marcozecchini 0:9fca2b23d0ba 231 """ Parse object file
marcozecchini 0:9fca2b23d0ba 232
marcozecchini 0:9fca2b23d0ba 233 Positional arguments:
marcozecchini 0:9fca2b23d0ba 234 line - the line containing the object or library
marcozecchini 0:9fca2b23d0ba 235 """
marcozecchini 0:9fca2b23d0ba 236
marcozecchini 0:9fca2b23d0ba 237 # simple object (not library)
marcozecchini 0:9fca2b23d0ba 238 if line[-2] == '.' and line[-1] == 'o':
marcozecchini 0:9fca2b23d0ba 239 return line
marcozecchini 0:9fca2b23d0ba 240
marcozecchini 0:9fca2b23d0ba 241 else:
marcozecchini 0:9fca2b23d0ba 242 is_obj = re.match(RE_OBJECT_ARMCC, line)
marcozecchini 0:9fca2b23d0ba 243 if is_obj:
marcozecchini 0:9fca2b23d0ba 244 object_name = os.path.basename(is_obj.group(1)) + '/' + is_obj.group(3)
marcozecchini 0:9fca2b23d0ba 245 return '[lib]/' + object_name
marcozecchini 0:9fca2b23d0ba 246 else:
marcozecchini 0:9fca2b23d0ba 247 print "Malformed input found when parsing ARMCC map: %s" % line
marcozecchini 0:9fca2b23d0ba 248 return '[misc]'
marcozecchini 0:9fca2b23d0ba 249
marcozecchini 0:9fca2b23d0ba 250
marcozecchini 0:9fca2b23d0ba 251
marcozecchini 0:9fca2b23d0ba 252 def parse_section_armcc(self, line):
marcozecchini 0:9fca2b23d0ba 253 """ Parse data from an armcc map file
marcozecchini 0:9fca2b23d0ba 254
marcozecchini 0:9fca2b23d0ba 255 Examples of armcc map file:
marcozecchini 0:9fca2b23d0ba 256 Base_Addr Size Type Attr Idx E Section Name Object
marcozecchini 0:9fca2b23d0ba 257 0x00000000 0x00000400 Data RO 11222 RESET startup_MK64F12.o
marcozecchini 0:9fca2b23d0ba 258 0x00000410 0x00000008 Code RO 49364 * !!!main c_w.l(__main.o)
marcozecchini 0:9fca2b23d0ba 259
marcozecchini 0:9fca2b23d0ba 260 Positional arguments:
marcozecchini 0:9fca2b23d0ba 261 line - the line to parse the section data from
marcozecchini 0:9fca2b23d0ba 262 """
marcozecchini 0:9fca2b23d0ba 263
marcozecchini 0:9fca2b23d0ba 264 test_re_armcc = re.match(RE_ARMCC, line)
marcozecchini 0:9fca2b23d0ba 265
marcozecchini 0:9fca2b23d0ba 266 if test_re_armcc:
marcozecchini 0:9fca2b23d0ba 267
marcozecchini 0:9fca2b23d0ba 268 size = int(test_re_armcc.group(2), 16)
marcozecchini 0:9fca2b23d0ba 269
marcozecchini 0:9fca2b23d0ba 270 if test_re_armcc.group(4) == 'RO':
marcozecchini 0:9fca2b23d0ba 271 section = '.text'
marcozecchini 0:9fca2b23d0ba 272 else:
marcozecchini 0:9fca2b23d0ba 273 if test_re_armcc.group(3) == 'Data':
marcozecchini 0:9fca2b23d0ba 274 section = '.data'
marcozecchini 0:9fca2b23d0ba 275 elif test_re_armcc.group(3) == 'Zero':
marcozecchini 0:9fca2b23d0ba 276 section = '.bss'
marcozecchini 0:9fca2b23d0ba 277 else:
marcozecchini 0:9fca2b23d0ba 278 print "Malformed input found when parsing armcc map: %s" %\
marcozecchini 0:9fca2b23d0ba 279 line
marcozecchini 0:9fca2b23d0ba 280
marcozecchini 0:9fca2b23d0ba 281 # check name of object or library
marcozecchini 0:9fca2b23d0ba 282 object_name = self.parse_object_name_armcc(\
marcozecchini 0:9fca2b23d0ba 283 test_re_armcc.group(6))
marcozecchini 0:9fca2b23d0ba 284
marcozecchini 0:9fca2b23d0ba 285 return [object_name, size, section]
marcozecchini 0:9fca2b23d0ba 286
marcozecchini 0:9fca2b23d0ba 287 else:
marcozecchini 0:9fca2b23d0ba 288 return ["", 0, ""]
marcozecchini 0:9fca2b23d0ba 289
marcozecchini 0:9fca2b23d0ba 290 def parse_object_name_iar(self, object_name):
marcozecchini 0:9fca2b23d0ba 291 """ Parse object file
marcozecchini 0:9fca2b23d0ba 292
marcozecchini 0:9fca2b23d0ba 293 Positional arguments:
marcozecchini 0:9fca2b23d0ba 294 line - the line containing the object or library
marcozecchini 0:9fca2b23d0ba 295 """
marcozecchini 0:9fca2b23d0ba 296
marcozecchini 0:9fca2b23d0ba 297 # simple object (not library)
marcozecchini 0:9fca2b23d0ba 298 if object_name.endswith(".o"):
marcozecchini 0:9fca2b23d0ba 299 try:
marcozecchini 0:9fca2b23d0ba 300 return self.cmd_modules[object_name]
marcozecchini 0:9fca2b23d0ba 301 except KeyError:
marcozecchini 0:9fca2b23d0ba 302 return object_name
marcozecchini 0:9fca2b23d0ba 303 else:
marcozecchini 0:9fca2b23d0ba 304 return '[misc]'
marcozecchini 0:9fca2b23d0ba 305
marcozecchini 0:9fca2b23d0ba 306
marcozecchini 0:9fca2b23d0ba 307 def parse_section_iar(self, line):
marcozecchini 0:9fca2b23d0ba 308 """ Parse data from an IAR map file
marcozecchini 0:9fca2b23d0ba 309
marcozecchini 0:9fca2b23d0ba 310 Examples of IAR map file:
marcozecchini 0:9fca2b23d0ba 311 Section Kind Address Size Object
marcozecchini 0:9fca2b23d0ba 312 .intvec ro code 0x00000000 0x198 startup_MK64F12.o [15]
marcozecchini 0:9fca2b23d0ba 313 .rodata const 0x00000198 0x0 zero_init3.o [133]
marcozecchini 0:9fca2b23d0ba 314 .iar.init_table const 0x00008384 0x2c - Linker created -
marcozecchini 0:9fca2b23d0ba 315 Initializer bytes const 0x00000198 0xb2 <for P3 s0>
marcozecchini 0:9fca2b23d0ba 316 .data inited 0x20000000 0xd4 driverAtmelRFInterface.o [70]
marcozecchini 0:9fca2b23d0ba 317 .bss zero 0x20000598 0x318 RTX_Conf_CM.o [4]
marcozecchini 0:9fca2b23d0ba 318 .iar.dynexit uninit 0x20001448 0x204 <Block tail>
marcozecchini 0:9fca2b23d0ba 319 HEAP uninit 0x20001650 0x10000 <Block tail>
marcozecchini 0:9fca2b23d0ba 320
marcozecchini 0:9fca2b23d0ba 321 Positional_arguments:
marcozecchini 0:9fca2b23d0ba 322 line - the line to parse section data from
marcozecchini 0:9fca2b23d0ba 323 """
marcozecchini 0:9fca2b23d0ba 324
marcozecchini 0:9fca2b23d0ba 325 test_re_iar = re.match(RE_IAR, line)
marcozecchini 0:9fca2b23d0ba 326
marcozecchini 0:9fca2b23d0ba 327 if test_re_iar:
marcozecchini 0:9fca2b23d0ba 328
marcozecchini 0:9fca2b23d0ba 329 size = int(test_re_iar.group(4), 16)
marcozecchini 0:9fca2b23d0ba 330
marcozecchini 0:9fca2b23d0ba 331 if (test_re_iar.group(2) == 'const' or
marcozecchini 0:9fca2b23d0ba 332 test_re_iar.group(2) == 'ro code'):
marcozecchini 0:9fca2b23d0ba 333 section = '.text'
marcozecchini 0:9fca2b23d0ba 334 elif (test_re_iar.group(2) == 'zero' or
marcozecchini 0:9fca2b23d0ba 335 test_re_iar.group(2) == 'uninit'):
marcozecchini 0:9fca2b23d0ba 336 if test_re_iar.group(1)[0:4] == 'HEAP':
marcozecchini 0:9fca2b23d0ba 337 section = '.heap'
marcozecchini 0:9fca2b23d0ba 338 elif test_re_iar.group(1)[0:6] == 'CSTACK':
marcozecchini 0:9fca2b23d0ba 339 section = '.stack'
marcozecchini 0:9fca2b23d0ba 340 else:
marcozecchini 0:9fca2b23d0ba 341 section = '.bss' # default section
marcozecchini 0:9fca2b23d0ba 342
marcozecchini 0:9fca2b23d0ba 343 elif test_re_iar.group(2) == 'inited':
marcozecchini 0:9fca2b23d0ba 344 section = '.data'
marcozecchini 0:9fca2b23d0ba 345 else:
marcozecchini 0:9fca2b23d0ba 346 print "Malformed input found when parsing IAR map: %s" % line
marcozecchini 0:9fca2b23d0ba 347
marcozecchini 0:9fca2b23d0ba 348 # lookup object in dictionary and return module name
marcozecchini 0:9fca2b23d0ba 349 object_name = self.parse_object_name_iar(test_re_iar.group(5))
marcozecchini 0:9fca2b23d0ba 350
marcozecchini 0:9fca2b23d0ba 351 return [object_name, size, section]
marcozecchini 0:9fca2b23d0ba 352
marcozecchini 0:9fca2b23d0ba 353 else:
marcozecchini 0:9fca2b23d0ba 354 return ["", 0, ""] # no valid entry
marcozecchini 0:9fca2b23d0ba 355
marcozecchini 0:9fca2b23d0ba 356 def parse_map_file_armcc(self, file_desc):
marcozecchini 0:9fca2b23d0ba 357 """ Main logic to decode armc5 map files
marcozecchini 0:9fca2b23d0ba 358
marcozecchini 0:9fca2b23d0ba 359 Positional arguments:
marcozecchini 0:9fca2b23d0ba 360 file_desc - a file like object to parse as an armc5 map file
marcozecchini 0:9fca2b23d0ba 361 """
marcozecchini 0:9fca2b23d0ba 362
marcozecchini 0:9fca2b23d0ba 363 with file_desc as infile:
marcozecchini 0:9fca2b23d0ba 364
marcozecchini 0:9fca2b23d0ba 365 # Search area to parse
marcozecchini 0:9fca2b23d0ba 366 for line in infile:
marcozecchini 0:9fca2b23d0ba 367 if line.startswith(' Base Addr Size'):
marcozecchini 0:9fca2b23d0ba 368 break
marcozecchini 0:9fca2b23d0ba 369
marcozecchini 0:9fca2b23d0ba 370 # Start decoding the map file
marcozecchini 0:9fca2b23d0ba 371 for line in infile:
marcozecchini 0:9fca2b23d0ba 372 self.module_add(*self.parse_section_armcc(line))
marcozecchini 0:9fca2b23d0ba 373
marcozecchini 0:9fca2b23d0ba 374 common_prefix = os.path.dirname(os.path.commonprefix([
marcozecchini 0:9fca2b23d0ba 375 o for o in self.modules.keys() if (o.endswith(".o") and o != "anon$$obj.o" and not o.startswith("[lib]"))]))
marcozecchini 0:9fca2b23d0ba 376 new_modules = {}
marcozecchini 0:9fca2b23d0ba 377 for name, stats in self.modules.items():
marcozecchini 0:9fca2b23d0ba 378 if name == "anon$$obj.o" or name.startswith("[lib]"):
marcozecchini 0:9fca2b23d0ba 379 new_modules[name] = stats
marcozecchini 0:9fca2b23d0ba 380 elif name.endswith(".o"):
marcozecchini 0:9fca2b23d0ba 381 new_modules[os.path.relpath(name, common_prefix)] = stats
marcozecchini 0:9fca2b23d0ba 382 else:
marcozecchini 0:9fca2b23d0ba 383 new_modules[name] = stats
marcozecchini 0:9fca2b23d0ba 384 self.modules = new_modules
marcozecchini 0:9fca2b23d0ba 385
marcozecchini 0:9fca2b23d0ba 386
marcozecchini 0:9fca2b23d0ba 387
marcozecchini 0:9fca2b23d0ba 388 def check_new_library_iar(self, line):
marcozecchini 0:9fca2b23d0ba 389 """
marcozecchini 0:9fca2b23d0ba 390 Searches for libraries and returns name. Example:
marcozecchini 0:9fca2b23d0ba 391 m7M_tls.a: [43]
marcozecchini 0:9fca2b23d0ba 392
marcozecchini 0:9fca2b23d0ba 393 """
marcozecchini 0:9fca2b23d0ba 394
marcozecchini 0:9fca2b23d0ba 395
marcozecchini 0:9fca2b23d0ba 396 test_address_line = re.match(RE_LIBRARY_IAR, line)
marcozecchini 0:9fca2b23d0ba 397
marcozecchini 0:9fca2b23d0ba 398 if test_address_line:
marcozecchini 0:9fca2b23d0ba 399 return test_address_line.group(1)
marcozecchini 0:9fca2b23d0ba 400 else:
marcozecchini 0:9fca2b23d0ba 401 return ""
marcozecchini 0:9fca2b23d0ba 402
marcozecchini 0:9fca2b23d0ba 403 def check_new_object_lib_iar(self, line):
marcozecchini 0:9fca2b23d0ba 404 """
marcozecchini 0:9fca2b23d0ba 405 Searches for objects within a library section and returns name. Example:
marcozecchini 0:9fca2b23d0ba 406 rt7M_tl.a: [44]
marcozecchini 0:9fca2b23d0ba 407 ABImemclr4.o 6
marcozecchini 0:9fca2b23d0ba 408 ABImemcpy_unaligned.o 118
marcozecchini 0:9fca2b23d0ba 409 ABImemset48.o 50
marcozecchini 0:9fca2b23d0ba 410 I64DivMod.o 238
marcozecchini 0:9fca2b23d0ba 411 I64DivZer.o 2
marcozecchini 0:9fca2b23d0ba 412
marcozecchini 0:9fca2b23d0ba 413 """
marcozecchini 0:9fca2b23d0ba 414
marcozecchini 0:9fca2b23d0ba 415 test_address_line = re.match(RE_OBJECT_LIBRARY_IAR, line)
marcozecchini 0:9fca2b23d0ba 416
marcozecchini 0:9fca2b23d0ba 417 if test_address_line:
marcozecchini 0:9fca2b23d0ba 418 return test_address_line.group(1)
marcozecchini 0:9fca2b23d0ba 419 else:
marcozecchini 0:9fca2b23d0ba 420 return ""
marcozecchini 0:9fca2b23d0ba 421
marcozecchini 0:9fca2b23d0ba 422 def parse_iar_command_line(self, lines):
marcozecchini 0:9fca2b23d0ba 423 """Parse the files passed on the command line to the iar linker
marcozecchini 0:9fca2b23d0ba 424
marcozecchini 0:9fca2b23d0ba 425 Positional arguments:
marcozecchini 0:9fca2b23d0ba 426 lines -- an iterator over the lines within a file
marcozecchini 0:9fca2b23d0ba 427 """
marcozecchini 0:9fca2b23d0ba 428 for line in lines:
marcozecchini 0:9fca2b23d0ba 429 if line.startswith("*"):
marcozecchini 0:9fca2b23d0ba 430 break
marcozecchini 0:9fca2b23d0ba 431 is_cmdline_file = RE_CMDLINE_FILE_IAR.match(line)
marcozecchini 0:9fca2b23d0ba 432 if is_cmdline_file:
marcozecchini 0:9fca2b23d0ba 433 full_path = is_cmdline_file.group(1)
marcozecchini 0:9fca2b23d0ba 434 self.cmd_modules[os.path.basename(full_path)] = full_path
marcozecchini 0:9fca2b23d0ba 435
marcozecchini 0:9fca2b23d0ba 436 common_prefix = os.path.dirname(os.path.commonprefix(self.cmd_modules.values()))
marcozecchini 0:9fca2b23d0ba 437 self.cmd_modules = {s: os.path.relpath(f, common_prefix)
marcozecchini 0:9fca2b23d0ba 438 for s, f in self.cmd_modules.items()}
marcozecchini 0:9fca2b23d0ba 439
marcozecchini 0:9fca2b23d0ba 440
marcozecchini 0:9fca2b23d0ba 441 def parse_map_file_iar(self, file_desc):
marcozecchini 0:9fca2b23d0ba 442 """ Main logic to decode IAR map files
marcozecchini 0:9fca2b23d0ba 443
marcozecchini 0:9fca2b23d0ba 444 Positional arguments:
marcozecchini 0:9fca2b23d0ba 445 file_desc - a file like object to parse as an IAR map file
marcozecchini 0:9fca2b23d0ba 446 """
marcozecchini 0:9fca2b23d0ba 447
marcozecchini 0:9fca2b23d0ba 448 with file_desc as infile:
marcozecchini 0:9fca2b23d0ba 449 self.parse_iar_command_line(infile)
marcozecchini 0:9fca2b23d0ba 450
marcozecchini 0:9fca2b23d0ba 451 for line in infile:
marcozecchini 0:9fca2b23d0ba 452 if line.startswith(' Section '):
marcozecchini 0:9fca2b23d0ba 453 break
marcozecchini 0:9fca2b23d0ba 454
marcozecchini 0:9fca2b23d0ba 455 for line in infile:
marcozecchini 0:9fca2b23d0ba 456 self.module_add(*self.parse_section_iar(line))
marcozecchini 0:9fca2b23d0ba 457
marcozecchini 0:9fca2b23d0ba 458 if line.startswith('*** MODULE SUMMARY'): # finish section
marcozecchini 0:9fca2b23d0ba 459 break
marcozecchini 0:9fca2b23d0ba 460
marcozecchini 0:9fca2b23d0ba 461 current_library = ""
marcozecchini 0:9fca2b23d0ba 462 for line in infile:
marcozecchini 0:9fca2b23d0ba 463
marcozecchini 0:9fca2b23d0ba 464 library = self.check_new_library_iar(line)
marcozecchini 0:9fca2b23d0ba 465
marcozecchini 0:9fca2b23d0ba 466 if library:
marcozecchini 0:9fca2b23d0ba 467 current_library = library
marcozecchini 0:9fca2b23d0ba 468
marcozecchini 0:9fca2b23d0ba 469 object_name = self.check_new_object_lib_iar(line)
marcozecchini 0:9fca2b23d0ba 470
marcozecchini 0:9fca2b23d0ba 471 if object_name and current_library:
marcozecchini 0:9fca2b23d0ba 472 temp = '[lib]' + '/'+ current_library + '/'+ object_name
marcozecchini 0:9fca2b23d0ba 473 self.module_replace(object_name, temp)
marcozecchini 0:9fca2b23d0ba 474
marcozecchini 0:9fca2b23d0ba 475
marcozecchini 0:9fca2b23d0ba 476 def reduce_depth(self, depth):
marcozecchini 0:9fca2b23d0ba 477 """
marcozecchini 0:9fca2b23d0ba 478 populates the short_modules attribute with a truncated module list
marcozecchini 0:9fca2b23d0ba 479
marcozecchini 0:9fca2b23d0ba 480 (1) depth = 1:
marcozecchini 0:9fca2b23d0ba 481 main.o
marcozecchini 0:9fca2b23d0ba 482 mbed-os
marcozecchini 0:9fca2b23d0ba 483
marcozecchini 0:9fca2b23d0ba 484 (2) depth = 2:
marcozecchini 0:9fca2b23d0ba 485 main.o
marcozecchini 0:9fca2b23d0ba 486 mbed-os/test.o
marcozecchini 0:9fca2b23d0ba 487 mbed-os/drivers
marcozecchini 0:9fca2b23d0ba 488
marcozecchini 0:9fca2b23d0ba 489 """
marcozecchini 0:9fca2b23d0ba 490 if depth == 0 or depth == None:
marcozecchini 0:9fca2b23d0ba 491 self.short_modules = deepcopy(self.modules)
marcozecchini 0:9fca2b23d0ba 492 else:
marcozecchini 0:9fca2b23d0ba 493 self.short_modules = dict()
marcozecchini 0:9fca2b23d0ba 494 for module_name, v in self.modules.items():
marcozecchini 0:9fca2b23d0ba 495 split_name = module_name.split('/')
marcozecchini 0:9fca2b23d0ba 496 if split_name[0] == '':
marcozecchini 0:9fca2b23d0ba 497 split_name = split_name[1:]
marcozecchini 0:9fca2b23d0ba 498 new_name = "/".join(split_name[:depth])
marcozecchini 0:9fca2b23d0ba 499 self.short_modules.setdefault(new_name, {})
marcozecchini 0:9fca2b23d0ba 500 for section_idx, value in v.items():
marcozecchini 0:9fca2b23d0ba 501 self.short_modules[new_name].setdefault(section_idx, 0)
marcozecchini 0:9fca2b23d0ba 502 self.short_modules[new_name][section_idx] += self.modules[module_name][section_idx]
marcozecchini 0:9fca2b23d0ba 503
marcozecchini 0:9fca2b23d0ba 504
marcozecchini 0:9fca2b23d0ba 505 export_formats = ["json", "csv-ci", "table"]
marcozecchini 0:9fca2b23d0ba 506
marcozecchini 0:9fca2b23d0ba 507 def generate_output(self, export_format, depth, file_output=None):
marcozecchini 0:9fca2b23d0ba 508 """ Generates summary of memory map data
marcozecchini 0:9fca2b23d0ba 509
marcozecchini 0:9fca2b23d0ba 510 Positional arguments:
marcozecchini 0:9fca2b23d0ba 511 export_format - the format to dump
marcozecchini 0:9fca2b23d0ba 512
marcozecchini 0:9fca2b23d0ba 513 Keyword arguments:
marcozecchini 0:9fca2b23d0ba 514 file_desc - descriptor (either stdout or file)
marcozecchini 0:9fca2b23d0ba 515 depth - directory depth on report
marcozecchini 0:9fca2b23d0ba 516
marcozecchini 0:9fca2b23d0ba 517 Returns: generated string for the 'table' format, otherwise None
marcozecchini 0:9fca2b23d0ba 518 """
marcozecchini 0:9fca2b23d0ba 519
marcozecchini 0:9fca2b23d0ba 520 self.reduce_depth(depth)
marcozecchini 0:9fca2b23d0ba 521 self.compute_report()
marcozecchini 0:9fca2b23d0ba 522
marcozecchini 0:9fca2b23d0ba 523 try:
marcozecchini 0:9fca2b23d0ba 524 if file_output:
marcozecchini 0:9fca2b23d0ba 525 file_desc = open(file_output, 'wb')
marcozecchini 0:9fca2b23d0ba 526 else:
marcozecchini 0:9fca2b23d0ba 527 file_desc = sys.stdout
marcozecchini 0:9fca2b23d0ba 528 except IOError as error:
marcozecchini 0:9fca2b23d0ba 529 print "I/O error({0}): {1}".format(error.errno, error.strerror)
marcozecchini 0:9fca2b23d0ba 530 return False
marcozecchini 0:9fca2b23d0ba 531
marcozecchini 0:9fca2b23d0ba 532 to_call = {'json': self.generate_json,
marcozecchini 0:9fca2b23d0ba 533 'csv-ci': self.generate_csv,
marcozecchini 0:9fca2b23d0ba 534 'table': self.generate_table}[export_format]
marcozecchini 0:9fca2b23d0ba 535 output = to_call(file_desc)
marcozecchini 0:9fca2b23d0ba 536
marcozecchini 0:9fca2b23d0ba 537 if file_desc is not sys.stdout:
marcozecchini 0:9fca2b23d0ba 538 file_desc.close()
marcozecchini 0:9fca2b23d0ba 539
marcozecchini 0:9fca2b23d0ba 540 return output
marcozecchini 0:9fca2b23d0ba 541
marcozecchini 0:9fca2b23d0ba 542 def generate_json(self, file_desc):
marcozecchini 0:9fca2b23d0ba 543 """Generate a json file from a memory map
marcozecchini 0:9fca2b23d0ba 544
marcozecchini 0:9fca2b23d0ba 545 Positional arguments:
marcozecchini 0:9fca2b23d0ba 546 file_desc - the file to write out the final report to
marcozecchini 0:9fca2b23d0ba 547 """
marcozecchini 0:9fca2b23d0ba 548 file_desc.write(json.dumps(self.mem_report, indent=4))
marcozecchini 0:9fca2b23d0ba 549 file_desc.write('\n')
marcozecchini 0:9fca2b23d0ba 550
marcozecchini 0:9fca2b23d0ba 551 return None
marcozecchini 0:9fca2b23d0ba 552
marcozecchini 0:9fca2b23d0ba 553 def generate_csv(self, file_desc):
marcozecchini 0:9fca2b23d0ba 554 """Generate a CSV file from a memoy map
marcozecchini 0:9fca2b23d0ba 555
marcozecchini 0:9fca2b23d0ba 556 Positional arguments:
marcozecchini 0:9fca2b23d0ba 557 file_desc - the file to write out the final report to
marcozecchini 0:9fca2b23d0ba 558 """
marcozecchini 0:9fca2b23d0ba 559 csv_writer = csv.writer(file_desc, delimiter=',',
marcozecchini 0:9fca2b23d0ba 560 quoting=csv.QUOTE_MINIMAL)
marcozecchini 0:9fca2b23d0ba 561
marcozecchini 0:9fca2b23d0ba 562 csv_module_section = []
marcozecchini 0:9fca2b23d0ba 563 csv_sizes = []
marcozecchini 0:9fca2b23d0ba 564 for i in sorted(self.short_modules):
marcozecchini 0:9fca2b23d0ba 565 for k in self.print_sections:
marcozecchini 0:9fca2b23d0ba 566 csv_module_section += [i+k]
marcozecchini 0:9fca2b23d0ba 567 csv_sizes += [self.short_modules[i][k]]
marcozecchini 0:9fca2b23d0ba 568
marcozecchini 0:9fca2b23d0ba 569 csv_module_section += ['static_ram']
marcozecchini 0:9fca2b23d0ba 570 csv_sizes += [self.mem_summary['static_ram']]
marcozecchini 0:9fca2b23d0ba 571
marcozecchini 0:9fca2b23d0ba 572 csv_module_section += ['total_flash']
marcozecchini 0:9fca2b23d0ba 573 csv_sizes += [self.mem_summary['total_flash']]
marcozecchini 0:9fca2b23d0ba 574
marcozecchini 0:9fca2b23d0ba 575 csv_writer.writerow(csv_module_section)
marcozecchini 0:9fca2b23d0ba 576 csv_writer.writerow(csv_sizes)
marcozecchini 0:9fca2b23d0ba 577
marcozecchini 0:9fca2b23d0ba 578 return None
marcozecchini 0:9fca2b23d0ba 579
marcozecchini 0:9fca2b23d0ba 580 def generate_table(self, file_desc):
marcozecchini 0:9fca2b23d0ba 581 """Generate a table from a memoy map
marcozecchini 0:9fca2b23d0ba 582
marcozecchini 0:9fca2b23d0ba 583 Returns: string of the generated table
marcozecchini 0:9fca2b23d0ba 584 """
marcozecchini 0:9fca2b23d0ba 585 # Create table
marcozecchini 0:9fca2b23d0ba 586 columns = ['Module']
marcozecchini 0:9fca2b23d0ba 587 columns.extend(self.print_sections)
marcozecchini 0:9fca2b23d0ba 588
marcozecchini 0:9fca2b23d0ba 589 table = PrettyTable(columns)
marcozecchini 0:9fca2b23d0ba 590 table.align["Module"] = "l"
marcozecchini 0:9fca2b23d0ba 591 for col in self.print_sections:
marcozecchini 0:9fca2b23d0ba 592 table.align[col] = 'r'
marcozecchini 0:9fca2b23d0ba 593
marcozecchini 0:9fca2b23d0ba 594 for i in list(self.print_sections):
marcozecchini 0:9fca2b23d0ba 595 table.align[i] = 'r'
marcozecchini 0:9fca2b23d0ba 596
marcozecchini 0:9fca2b23d0ba 597 for i in sorted(self.short_modules):
marcozecchini 0:9fca2b23d0ba 598 row = [i]
marcozecchini 0:9fca2b23d0ba 599
marcozecchini 0:9fca2b23d0ba 600 for k in self.print_sections:
marcozecchini 0:9fca2b23d0ba 601 row.append(self.short_modules[i][k])
marcozecchini 0:9fca2b23d0ba 602
marcozecchini 0:9fca2b23d0ba 603 table.add_row(row)
marcozecchini 0:9fca2b23d0ba 604
marcozecchini 0:9fca2b23d0ba 605 subtotal_row = ['Subtotals']
marcozecchini 0:9fca2b23d0ba 606 for k in self.print_sections:
marcozecchini 0:9fca2b23d0ba 607 subtotal_row.append(self.subtotal[k])
marcozecchini 0:9fca2b23d0ba 608
marcozecchini 0:9fca2b23d0ba 609 table.add_row(subtotal_row)
marcozecchini 0:9fca2b23d0ba 610
marcozecchini 0:9fca2b23d0ba 611 output = table.get_string()
marcozecchini 0:9fca2b23d0ba 612 output += '\n'
marcozecchini 0:9fca2b23d0ba 613
marcozecchini 0:9fca2b23d0ba 614 output += "Total Static RAM memory (data + bss): %s bytes\n" % \
marcozecchini 0:9fca2b23d0ba 615 str(self.mem_summary['static_ram'])
marcozecchini 0:9fca2b23d0ba 616 output += "Total Flash memory (text + data): %s bytes\n" % \
marcozecchini 0:9fca2b23d0ba 617 str(self.mem_summary['total_flash'])
marcozecchini 0:9fca2b23d0ba 618
marcozecchini 0:9fca2b23d0ba 619 return output
marcozecchini 0:9fca2b23d0ba 620
marcozecchini 0:9fca2b23d0ba 621 toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "GCC_CR", "IAR"]
marcozecchini 0:9fca2b23d0ba 622
marcozecchini 0:9fca2b23d0ba 623 def compute_report(self):
marcozecchini 0:9fca2b23d0ba 624 """ Generates summary of memory usage for main areas
marcozecchini 0:9fca2b23d0ba 625 """
marcozecchini 0:9fca2b23d0ba 626 for k in self.sections:
marcozecchini 0:9fca2b23d0ba 627 self.subtotal[k] = 0
marcozecchini 0:9fca2b23d0ba 628
marcozecchini 0:9fca2b23d0ba 629 for i in self.short_modules:
marcozecchini 0:9fca2b23d0ba 630 for k in self.sections:
marcozecchini 0:9fca2b23d0ba 631 self.short_modules[i].setdefault(k, 0)
marcozecchini 0:9fca2b23d0ba 632 self.subtotal[k] += self.short_modules[i][k]
marcozecchini 0:9fca2b23d0ba 633
marcozecchini 0:9fca2b23d0ba 634 self.mem_summary = {
marcozecchini 0:9fca2b23d0ba 635 'static_ram': (self.subtotal['.data'] + self.subtotal['.bss']),
marcozecchini 0:9fca2b23d0ba 636 'total_flash': (self.subtotal['.text'] + self.subtotal['.data']),
marcozecchini 0:9fca2b23d0ba 637 }
marcozecchini 0:9fca2b23d0ba 638
marcozecchini 0:9fca2b23d0ba 639 self.mem_report = []
marcozecchini 0:9fca2b23d0ba 640 for i in sorted(self.short_modules):
marcozecchini 0:9fca2b23d0ba 641 self.mem_report.append({
marcozecchini 0:9fca2b23d0ba 642 "module":i,
marcozecchini 0:9fca2b23d0ba 643 "size":{
marcozecchini 0:9fca2b23d0ba 644 k: self.short_modules[i][k] for k in self.print_sections
marcozecchini 0:9fca2b23d0ba 645 }
marcozecchini 0:9fca2b23d0ba 646 })
marcozecchini 0:9fca2b23d0ba 647
marcozecchini 0:9fca2b23d0ba 648 self.mem_report.append({
marcozecchini 0:9fca2b23d0ba 649 'summary': self.mem_summary
marcozecchini 0:9fca2b23d0ba 650 })
marcozecchini 0:9fca2b23d0ba 651
marcozecchini 0:9fca2b23d0ba 652 def parse(self, mapfile, toolchain):
marcozecchini 0:9fca2b23d0ba 653 """ Parse and decode map file depending on the toolchain
marcozecchini 0:9fca2b23d0ba 654
marcozecchini 0:9fca2b23d0ba 655 Positional arguments:
marcozecchini 0:9fca2b23d0ba 656 mapfile - the file name of the memory map file
marcozecchini 0:9fca2b23d0ba 657 toolchain - the toolchain used to create the file
marcozecchini 0:9fca2b23d0ba 658 """
marcozecchini 0:9fca2b23d0ba 659
marcozecchini 0:9fca2b23d0ba 660 result = True
marcozecchini 0:9fca2b23d0ba 661 try:
marcozecchini 0:9fca2b23d0ba 662 with open(mapfile, 'r') as file_input:
marcozecchini 0:9fca2b23d0ba 663 if toolchain in ("ARM", "ARM_STD", "ARM_MICRO", "ARMC6"):
marcozecchini 0:9fca2b23d0ba 664 self.parse_map_file_armcc(file_input)
marcozecchini 0:9fca2b23d0ba 665 elif toolchain == "GCC_ARM" or toolchain == "GCC_CR":
marcozecchini 0:9fca2b23d0ba 666 self.parse_map_file_gcc(file_input)
marcozecchini 0:9fca2b23d0ba 667 elif toolchain == "IAR":
marcozecchini 0:9fca2b23d0ba 668 self.parse_map_file_iar(file_input)
marcozecchini 0:9fca2b23d0ba 669 else:
marcozecchini 0:9fca2b23d0ba 670 result = False
marcozecchini 0:9fca2b23d0ba 671
marcozecchini 0:9fca2b23d0ba 672 except IOError as error:
marcozecchini 0:9fca2b23d0ba 673 print "I/O error({0}): {1}".format(error.errno, error.strerror)
marcozecchini 0:9fca2b23d0ba 674 result = False
marcozecchini 0:9fca2b23d0ba 675 return result
marcozecchini 0:9fca2b23d0ba 676
marcozecchini 0:9fca2b23d0ba 677 def main():
marcozecchini 0:9fca2b23d0ba 678 """Entry Point"""
marcozecchini 0:9fca2b23d0ba 679
marcozecchini 0:9fca2b23d0ba 680 version = '0.4.0'
marcozecchini 0:9fca2b23d0ba 681
marcozecchini 0:9fca2b23d0ba 682 # Parser handling
marcozecchini 0:9fca2b23d0ba 683 parser = argparse.ArgumentParser(
marcozecchini 0:9fca2b23d0ba 684 description="Memory Map File Analyser for ARM mbed\nversion %s" %
marcozecchini 0:9fca2b23d0ba 685 version)
marcozecchini 0:9fca2b23d0ba 686
marcozecchini 0:9fca2b23d0ba 687 parser.add_argument(
marcozecchini 0:9fca2b23d0ba 688 'file', type=argparse_filestring_type, help='memory map file')
marcozecchini 0:9fca2b23d0ba 689
marcozecchini 0:9fca2b23d0ba 690 parser.add_argument(
marcozecchini 0:9fca2b23d0ba 691 '-t', '--toolchain', dest='toolchain',
marcozecchini 0:9fca2b23d0ba 692 help='select a toolchain used to build the memory map file (%s)' %
marcozecchini 0:9fca2b23d0ba 693 ", ".join(MemapParser.toolchains),
marcozecchini 0:9fca2b23d0ba 694 required=True,
marcozecchini 0:9fca2b23d0ba 695 type=argparse_uppercase_type(MemapParser.toolchains, "toolchain"))
marcozecchini 0:9fca2b23d0ba 696
marcozecchini 0:9fca2b23d0ba 697 parser.add_argument(
marcozecchini 0:9fca2b23d0ba 698 '-d', '--depth', dest='depth', type=int,
marcozecchini 0:9fca2b23d0ba 699 help='specify directory depth level to display report', required=False)
marcozecchini 0:9fca2b23d0ba 700
marcozecchini 0:9fca2b23d0ba 701 parser.add_argument(
marcozecchini 0:9fca2b23d0ba 702 '-o', '--output', help='output file name', required=False)
marcozecchini 0:9fca2b23d0ba 703
marcozecchini 0:9fca2b23d0ba 704 parser.add_argument(
marcozecchini 0:9fca2b23d0ba 705 '-e', '--export', dest='export', required=False, default='table',
marcozecchini 0:9fca2b23d0ba 706 type=argparse_lowercase_hyphen_type(MemapParser.export_formats,
marcozecchini 0:9fca2b23d0ba 707 'export format'),
marcozecchini 0:9fca2b23d0ba 708 help="export format (examples: %s: default)" %
marcozecchini 0:9fca2b23d0ba 709 ", ".join(MemapParser.export_formats))
marcozecchini 0:9fca2b23d0ba 710
marcozecchini 0:9fca2b23d0ba 711 parser.add_argument('-v', '--version', action='version', version=version)
marcozecchini 0:9fca2b23d0ba 712
marcozecchini 0:9fca2b23d0ba 713 # Parse/run command
marcozecchini 0:9fca2b23d0ba 714 if len(sys.argv) <= 1:
marcozecchini 0:9fca2b23d0ba 715 parser.print_help()
marcozecchini 0:9fca2b23d0ba 716 sys.exit(1)
marcozecchini 0:9fca2b23d0ba 717
marcozecchini 0:9fca2b23d0ba 718 args = parser.parse_args()
marcozecchini 0:9fca2b23d0ba 719
marcozecchini 0:9fca2b23d0ba 720 # Create memap object
marcozecchini 0:9fca2b23d0ba 721 memap = MemapParser()
marcozecchini 0:9fca2b23d0ba 722
marcozecchini 0:9fca2b23d0ba 723 # Parse and decode a map file
marcozecchini 0:9fca2b23d0ba 724 if args.file and args.toolchain:
marcozecchini 0:9fca2b23d0ba 725 if memap.parse(args.file, args.toolchain) is False:
marcozecchini 0:9fca2b23d0ba 726 sys.exit(0)
marcozecchini 0:9fca2b23d0ba 727
marcozecchini 0:9fca2b23d0ba 728 if args.depth is None:
marcozecchini 0:9fca2b23d0ba 729 depth = 2 # default depth level
marcozecchini 0:9fca2b23d0ba 730 else:
marcozecchini 0:9fca2b23d0ba 731 depth = args.depth
marcozecchini 0:9fca2b23d0ba 732
marcozecchini 0:9fca2b23d0ba 733 returned_string = None
marcozecchini 0:9fca2b23d0ba 734 # Write output in file
marcozecchini 0:9fca2b23d0ba 735 if args.output != None:
marcozecchini 0:9fca2b23d0ba 736 returned_string = memap.generate_output(args.export, \
marcozecchini 0:9fca2b23d0ba 737 depth, args.output)
marcozecchini 0:9fca2b23d0ba 738 else: # Write output in screen
marcozecchini 0:9fca2b23d0ba 739 returned_string = memap.generate_output(args.export, depth)
marcozecchini 0:9fca2b23d0ba 740
marcozecchini 0:9fca2b23d0ba 741 if args.export == 'table' and returned_string:
marcozecchini 0:9fca2b23d0ba 742 print returned_string
marcozecchini 0:9fca2b23d0ba 743
marcozecchini 0:9fca2b23d0ba 744 sys.exit(0)
marcozecchini 0:9fca2b23d0ba 745
marcozecchini 0:9fca2b23d0ba 746 if __name__ == "__main__":
marcozecchini 0:9fca2b23d0ba 747 main()