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