Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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