Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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