Clone of official tools

Committer:
screamer
Date:
Thu Jul 14 20:21:19 2016 +0100
Revision:
13:ab47a20b66f0
Parent:
9:2d27d77ada5c
Child:
22:9e85236d8716
Apply latest tools

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 1 #!/usr/bin/env python
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 2 # pylint: disable=too-many-arguments, too-many-locals, too-many-branches, too-many-lines, line-too-long, too-many-nested-blocks, too-many-public-methods, too-many-instance-attributes
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 3 # pylint: disable=invalid-name, missing-docstring
screamer 8:a8ac6ed29081 4
screamer 13:ab47a20b66f0 5 # Memory Map File Analyser for ARM mbed
screamer 8:a8ac6ed29081 6
screamer 8:a8ac6ed29081 7 import sys
screamer 8:a8ac6ed29081 8 import os
screamer 8:a8ac6ed29081 9 import re
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 10 import csv
screamer 8:a8ac6ed29081 11 import json
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 12 import argparse
screamer 8:a8ac6ed29081 13 from prettytable import PrettyTable
screamer 8:a8ac6ed29081 14
screamer 8:a8ac6ed29081 15 debug = False
screamer 8:a8ac6ed29081 16
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 17 class MemapParser(object):
screamer 8:a8ac6ed29081 18
screamer 8:a8ac6ed29081 19 def __init__(self):
screamer 8:a8ac6ed29081 20 """
screamer 8:a8ac6ed29081 21 General initialization
screamer 8:a8ac6ed29081 22 """
screamer 8:a8ac6ed29081 23
screamer 8:a8ac6ed29081 24 # list of all modules and their sections
screamer 8:a8ac6ed29081 25 self.modules = dict()
screamer 8:a8ac6ed29081 26
screamer 8:a8ac6ed29081 27 self.misc_flash_sections = ('.interrupts', '.flash_config')
screamer 8:a8ac6ed29081 28
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 29 self.other_sections = ('.interrupts_ram', '.init', '.ARM.extab', \
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 30 '.ARM.exidx', '.ARM.attributes', '.eh_frame', \
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 31 '.init_array', '.fini_array', '.jcr', '.stab', \
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 32 '.stabstr', '.ARM.exidx', '.ARM')
screamer 8:a8ac6ed29081 33
screamer 8:a8ac6ed29081 34 # sections to print info (generic for all toolchains)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 35 self.sections = ('.text', '.data', '.bss', '.heap', '.stack')
screamer 8:a8ac6ed29081 36
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 37 # sections must be defined in this order to take irrelevant out
screamer 8:a8ac6ed29081 38 self.all_sections = self.sections + self.other_sections + \
screamer 8:a8ac6ed29081 39 self.misc_flash_sections + ('unknown', 'OUTPUT')
screamer 8:a8ac6ed29081 40
screamer 8:a8ac6ed29081 41 self.print_sections = ('.text', '.data', '.bss')
screamer 8:a8ac6ed29081 42
screamer 8:a8ac6ed29081 43 # list of all object files and mappting to module names
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 44 self.object_to_module = dict()
screamer 8:a8ac6ed29081 45
screamer 8:a8ac6ed29081 46 def module_add(self, module_name, size, section):
screamer 8:a8ac6ed29081 47 """
screamer 8:a8ac6ed29081 48 Adds a module / section to the list
screamer 8:a8ac6ed29081 49 """
screamer 8:a8ac6ed29081 50
screamer 8:a8ac6ed29081 51 if module_name in self.modules:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 52 self.modules[module_name][section] += size
screamer 8:a8ac6ed29081 53 else:
screamer 8:a8ac6ed29081 54 temp_dic = dict()
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 55 for section_idx in self.all_sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 56 temp_dic[section_idx] = 0
screamer 8:a8ac6ed29081 57 temp_dic[section] = size
screamer 8:a8ac6ed29081 58 self.modules[module_name] = temp_dic
screamer 8:a8ac6ed29081 59
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 60 def check_new_section_gcc(self, line):
screamer 8:a8ac6ed29081 61 """
screamer 8:a8ac6ed29081 62 Check whether a new section in a map file has been detected (only applies to gcc)
screamer 8:a8ac6ed29081 63 """
screamer 8:a8ac6ed29081 64
screamer 8:a8ac6ed29081 65 for i in self.all_sections:
screamer 8:a8ac6ed29081 66 if line.startswith(i):
screamer 8:a8ac6ed29081 67 return i # should name of the section (assuming it's a known one)
screamer 8:a8ac6ed29081 68
screamer 8:a8ac6ed29081 69 if line.startswith('.'):
screamer 8:a8ac6ed29081 70 return 'unknown' # all others are clasified are unknown
screamer 8:a8ac6ed29081 71 else:
screamer 8:a8ac6ed29081 72 return False # everything else, means no change in section
screamer 8:a8ac6ed29081 73
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 74 def path_object_to_module_name(self, txt):
screamer 8:a8ac6ed29081 75 """
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 76 Parses path to object file and extracts module / object data
screamer 8:a8ac6ed29081 77 """
screamer 8:a8ac6ed29081 78
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 79 txt = txt.replace('\\', '/')
screamer 8:a8ac6ed29081 80 rex_mbed_os_name = r'^.+mbed-os\/(.+)\/(.+\.o)$'
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 81 test_rex_mbed_os_name = re.match(rex_mbed_os_name, txt)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 82
screamer 8:a8ac6ed29081 83 if test_rex_mbed_os_name:
screamer 8:a8ac6ed29081 84
screamer 8:a8ac6ed29081 85 object_name = test_rex_mbed_os_name.group(2)
screamer 8:a8ac6ed29081 86 data = test_rex_mbed_os_name.group(1).split('/')
screamer 8:a8ac6ed29081 87 ndata = len(data)
screamer 8:a8ac6ed29081 88
screamer 8:a8ac6ed29081 89 if ndata == 1:
screamer 8:a8ac6ed29081 90 module_name = data[0]
screamer 8:a8ac6ed29081 91 else:
screamer 8:a8ac6ed29081 92 module_name = data[0] + '/' + data[1]
screamer 8:a8ac6ed29081 93
screamer 8:a8ac6ed29081 94 return [module_name, object_name]
screamer 8:a8ac6ed29081 95 else:
screamer 8:a8ac6ed29081 96 return ['Misc', ""]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 97
screamer 8:a8ac6ed29081 98
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 99 def parse_section_gcc(self, line):
screamer 8:a8ac6ed29081 100 """
screamer 8:a8ac6ed29081 101 Parse data from a section of gcc map file
screamer 8:a8ac6ed29081 102 """
screamer 8:a8ac6ed29081 103 # examples
screamer 8:a8ac6ed29081 104 # 0x00004308 0x7c ./.build/K64F/GCC_ARM/mbed-os/hal/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/spi_api.o
screamer 8:a8ac6ed29081 105 # .text 0x00000608 0x198 ./.build/K64F/GCC_ARM/mbed-os/core/mbed-rtos/rtx/TARGET_CORTEX_M/TARGET_RTOS_M4_M7/TOOLCHAIN_GCC/HAL_CM4.o
screamer 8:a8ac6ed29081 106 rex_address_len_name = r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$'
screamer 8:a8ac6ed29081 107
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 108 test_address_len_name = re.match(rex_address_len_name, line)
screamer 8:a8ac6ed29081 109
screamer 8:a8ac6ed29081 110 if test_address_len_name:
screamer 8:a8ac6ed29081 111
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 112 if int(test_address_len_name.group(2), 16) == 0: # size == 0
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 113 return ["", 0] # no valid entry
screamer 8:a8ac6ed29081 114 else:
screamer 8:a8ac6ed29081 115 m_name, m_object = self.path_object_to_module_name(test_address_len_name.group(3))
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 116 m_size = int(test_address_len_name.group(2), 16)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 117 return [m_name, m_size]
screamer 8:a8ac6ed29081 118
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 119 else: # special corner case for *fill* sections
screamer 8:a8ac6ed29081 120 # example
screamer 8:a8ac6ed29081 121 # *fill* 0x0000abe4 0x4
screamer 8:a8ac6ed29081 122 rex_address_len = r'^\s+\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$'
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 123 test_address_len = re.match(rex_address_len, line)
screamer 8:a8ac6ed29081 124
screamer 8:a8ac6ed29081 125 if test_address_len:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 126 if int(test_address_len.group(2), 16) == 0: # size == 0
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 127 return ["", 0] # no valid entry
screamer 8:a8ac6ed29081 128 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 129 m_name = 'Fill'
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 130 m_size = int(test_address_len.group(2), 16)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 131 return [m_name, m_size]
screamer 8:a8ac6ed29081 132 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 133 return ["", 0] # no valid entry
screamer 8:a8ac6ed29081 134
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 135 def parse_map_file_gcc(self, file_desc):
screamer 8:a8ac6ed29081 136 """
screamer 8:a8ac6ed29081 137 Main logic to decode gcc map files
screamer 8:a8ac6ed29081 138 """
screamer 8:a8ac6ed29081 139
screamer 8:a8ac6ed29081 140 current_section = 'unknown'
screamer 8:a8ac6ed29081 141
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 142 with file_desc as infile:
screamer 8:a8ac6ed29081 143
screamer 8:a8ac6ed29081 144 # Search area to parse
screamer 8:a8ac6ed29081 145 for line in infile:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 146 if line.startswith('Linker script and memory map'):
screamer 8:a8ac6ed29081 147 current_section = "unknown"
screamer 8:a8ac6ed29081 148 break
screamer 8:a8ac6ed29081 149
screamer 8:a8ac6ed29081 150 # Start decoding the map file
screamer 8:a8ac6ed29081 151 for line in infile:
screamer 8:a8ac6ed29081 152
screamer 8:a8ac6ed29081 153 change_section = self.check_new_section_gcc(line)
screamer 8:a8ac6ed29081 154
screamer 8:a8ac6ed29081 155 if change_section == "OUTPUT": # finish parsing file: exit
screamer 8:a8ac6ed29081 156 break
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 157 elif change_section != False:
screamer 8:a8ac6ed29081 158 current_section = change_section
screamer 8:a8ac6ed29081 159
screamer 8:a8ac6ed29081 160 [module_name, module_size] = self.parse_section_gcc(line)
screamer 8:a8ac6ed29081 161
screamer 8:a8ac6ed29081 162 if module_size == 0 or module_name == "":
screamer 8:a8ac6ed29081 163 pass
screamer 8:a8ac6ed29081 164 else:
screamer 8:a8ac6ed29081 165 self.module_add(module_name, module_size, current_section)
screamer 8:a8ac6ed29081 166
screamer 8:a8ac6ed29081 167 if debug:
screamer 8:a8ac6ed29081 168 print "Line: %s" % line,
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 169 print "Module: %s\tSection: %s\tSize: %s" % (module_name, current_section, module_size)
screamer 8:a8ac6ed29081 170 raw_input("----------")
screamer 8:a8ac6ed29081 171
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 172 def parse_section_armcc(self, line):
screamer 8:a8ac6ed29081 173 """
screamer 8:a8ac6ed29081 174 Parse data from an armcc map file
screamer 8:a8ac6ed29081 175 """
screamer 8:a8ac6ed29081 176 # Examples of armcc map file:
screamer 8:a8ac6ed29081 177 # Base_Addr Size Type Attr Idx E Section Name Object
screamer 8:a8ac6ed29081 178 # 0x00000000 0x00000400 Data RO 11222 RESET startup_MK64F12.o
screamer 8:a8ac6ed29081 179 # 0x00000410 0x00000008 Code RO 49364 * !!!main c_w.l(__main.o)
screamer 8:a8ac6ed29081 180 rex_armcc = r'^\s+0x(\w{8})\s+0x(\w{8})\s+(\w+)\s+(\w+)\s+(\d+)\s+[*]?.+\s+(.+)$'
screamer 8:a8ac6ed29081 181
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 182 test_rex_armcc = re.match(rex_armcc, line)
screamer 8:a8ac6ed29081 183
screamer 8:a8ac6ed29081 184 if test_rex_armcc:
screamer 8:a8ac6ed29081 185
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 186 size = int(test_rex_armcc.group(2), 16)
screamer 8:a8ac6ed29081 187
screamer 8:a8ac6ed29081 188 if test_rex_armcc.group(4) == 'RO':
screamer 8:a8ac6ed29081 189 section = '.text'
screamer 8:a8ac6ed29081 190 else:
screamer 8:a8ac6ed29081 191 if test_rex_armcc.group(3) == 'Data':
screamer 8:a8ac6ed29081 192 section = '.data'
screamer 8:a8ac6ed29081 193 elif test_rex_armcc.group(3) == 'Zero':
screamer 8:a8ac6ed29081 194 section = '.bss'
screamer 8:a8ac6ed29081 195 else:
screamer 8:a8ac6ed29081 196 print "BUG armcc map parser"
screamer 8:a8ac6ed29081 197 raw_input()
screamer 8:a8ac6ed29081 198
screamer 8:a8ac6ed29081 199 # lookup object in dictionary and return module name
screamer 8:a8ac6ed29081 200 object_name = test_rex_armcc.group(6)
screamer 8:a8ac6ed29081 201 if object_name in self.object_to_module:
screamer 8:a8ac6ed29081 202 module_name = self.object_to_module[object_name]
screamer 8:a8ac6ed29081 203 else:
screamer 8:a8ac6ed29081 204 module_name = 'Misc'
screamer 8:a8ac6ed29081 205
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 206 return [module_name, size, section]
screamer 8:a8ac6ed29081 207
screamer 8:a8ac6ed29081 208 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 209 return ["", 0, ""] # no valid entry
screamer 8:a8ac6ed29081 210
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 211 def parse_section_iar(self, line):
screamer 8:a8ac6ed29081 212 """
screamer 8:a8ac6ed29081 213 Parse data from an IAR map file
screamer 8:a8ac6ed29081 214 """
screamer 8:a8ac6ed29081 215 # Examples of IAR map file:
screamer 8:a8ac6ed29081 216 # Section Kind Address Size Object
screamer 8:a8ac6ed29081 217 # .intvec ro code 0x00000000 0x198 startup_MK64F12.o [15]
screamer 8:a8ac6ed29081 218 # .rodata const 0x00000198 0x0 zero_init3.o [133]
screamer 8:a8ac6ed29081 219 # .iar.init_table const 0x00008384 0x2c - Linker created -
screamer 8:a8ac6ed29081 220 # Initializer bytes const 0x00000198 0xb2 <for P3 s0>
screamer 8:a8ac6ed29081 221 # .data inited 0x20000000 0xd4 driverAtmelRFInterface.o [70]
screamer 8:a8ac6ed29081 222 # .bss zero 0x20000598 0x318 RTX_Conf_CM.o [4]
screamer 8:a8ac6ed29081 223 # .iar.dynexit uninit 0x20001448 0x204 <Block tail>
screamer 8:a8ac6ed29081 224 # HEAP uninit 0x20001650 0x10000 <Block tail>
screamer 8:a8ac6ed29081 225 rex_iar = r'^\s+(.+)\s+(zero|const|ro code|inited|uninit)\s+0x(\w{8})\s+0x(\w+)\s+(.+)\s.+$'
screamer 8:a8ac6ed29081 226
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 227 test_rex_iar = re.match(rex_iar, line)
screamer 8:a8ac6ed29081 228
screamer 8:a8ac6ed29081 229 if test_rex_iar:
screamer 8:a8ac6ed29081 230
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 231 size = int(test_rex_iar.group(4), 16)
screamer 8:a8ac6ed29081 232
screamer 8:a8ac6ed29081 233 if test_rex_iar.group(2) == 'const' or test_rex_iar.group(2) == 'ro code':
screamer 8:a8ac6ed29081 234 section = '.text'
screamer 8:a8ac6ed29081 235 elif test_rex_iar.group(2) == 'zero' or test_rex_iar.group(2) == 'uninit':
screamer 8:a8ac6ed29081 236 if test_rex_iar.group(1)[0:4] == 'HEAP':
screamer 8:a8ac6ed29081 237 section = '.heap'
screamer 8:a8ac6ed29081 238 elif test_rex_iar.group(1)[0:6] == 'CSTACK':
screamer 8:a8ac6ed29081 239 section = '.stack'
screamer 8:a8ac6ed29081 240 else:
screamer 8:a8ac6ed29081 241 section = '.bss' # default section
screamer 8:a8ac6ed29081 242
screamer 8:a8ac6ed29081 243 elif test_rex_iar.group(2) == 'inited':
screamer 8:a8ac6ed29081 244 section = '.data'
screamer 8:a8ac6ed29081 245 else:
screamer 8:a8ac6ed29081 246 print "BUG IAR map parser"
screamer 8:a8ac6ed29081 247 raw_input()
screamer 8:a8ac6ed29081 248
screamer 8:a8ac6ed29081 249 # lookup object in dictionary and return module name
screamer 8:a8ac6ed29081 250 object_name = test_rex_iar.group(5)
screamer 8:a8ac6ed29081 251 if object_name in self.object_to_module:
screamer 8:a8ac6ed29081 252 module_name = self.object_to_module[object_name]
screamer 8:a8ac6ed29081 253 else:
screamer 8:a8ac6ed29081 254 module_name = 'Misc'
screamer 8:a8ac6ed29081 255
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 256 return [module_name, size, section]
screamer 8:a8ac6ed29081 257
screamer 8:a8ac6ed29081 258 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 259 return ["", 0, ""] # no valid entry
screamer 8:a8ac6ed29081 260
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 261 def parse_map_file_armcc(self, file_desc):
screamer 8:a8ac6ed29081 262 """
screamer 8:a8ac6ed29081 263 Main logic to decode armcc map files
screamer 8:a8ac6ed29081 264 """
screamer 8:a8ac6ed29081 265
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 266 with file_desc as infile:
screamer 8:a8ac6ed29081 267
screamer 8:a8ac6ed29081 268 # Search area to parse
screamer 8:a8ac6ed29081 269 for line in infile:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 270 if line.startswith(' Base Addr Size'):
screamer 8:a8ac6ed29081 271 break
screamer 8:a8ac6ed29081 272
screamer 8:a8ac6ed29081 273 # Start decoding the map file
screamer 8:a8ac6ed29081 274 for line in infile:
screamer 8:a8ac6ed29081 275
screamer 8:a8ac6ed29081 276 [name, size, section] = self.parse_section_armcc(line)
screamer 8:a8ac6ed29081 277
screamer 8:a8ac6ed29081 278 if size == 0 or name == "" or section == "":
screamer 8:a8ac6ed29081 279 pass
screamer 8:a8ac6ed29081 280 else:
screamer 8:a8ac6ed29081 281 self.module_add(name, size, section)
screamer 8:a8ac6ed29081 282
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 283 def parse_map_file_iar(self, file_desc):
screamer 8:a8ac6ed29081 284 """
screamer 8:a8ac6ed29081 285 Main logic to decode armcc map files
screamer 8:a8ac6ed29081 286 """
screamer 8:a8ac6ed29081 287
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 288 with file_desc as infile:
screamer 8:a8ac6ed29081 289
screamer 8:a8ac6ed29081 290 # Search area to parse
screamer 8:a8ac6ed29081 291 for line in infile:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 292 if line.startswith(' Section '):
screamer 8:a8ac6ed29081 293 break
screamer 8:a8ac6ed29081 294
screamer 8:a8ac6ed29081 295 # Start decoding the map file
screamer 8:a8ac6ed29081 296 for line in infile:
screamer 8:a8ac6ed29081 297
screamer 8:a8ac6ed29081 298 [name, size, section] = self.parse_section_iar(line)
screamer 8:a8ac6ed29081 299
screamer 8:a8ac6ed29081 300 if size == 0 or name == "" or section == "":
screamer 8:a8ac6ed29081 301 pass
screamer 8:a8ac6ed29081 302 else:
screamer 8:a8ac6ed29081 303 self.module_add(name, size, section)
screamer 8:a8ac6ed29081 304
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 305 def search_objects(self, path, toolchain):
screamer 8:a8ac6ed29081 306 """
screamer 8:a8ac6ed29081 307 Check whether the specified map file matches with the toolchain.
screamer 8:a8ac6ed29081 308 Searches for object files and creates mapping: object --> module
screamer 8:a8ac6ed29081 309 """
screamer 8:a8ac6ed29081 310
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 311 path = path.replace('\\', '/')
screamer 8:a8ac6ed29081 312
screamer 8:a8ac6ed29081 313 # check location of map file
screamer 8:a8ac6ed29081 314 rex = r'^(.+\/)' + re.escape(toolchain) + r'\/(.+\.map)$'
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 315 test_rex = re.match(rex, path)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 316
screamer 8:a8ac6ed29081 317 if test_rex:
screamer 8:a8ac6ed29081 318 search_path = test_rex.group(1) + toolchain + '/mbed-os/'
screamer 8:a8ac6ed29081 319 else:
screamer 13:ab47a20b66f0 320 # It looks this is not an mbed project
screamer 8:a8ac6ed29081 321 # object-to-module mapping cannot be generated
screamer 8:a8ac6ed29081 322 print "Warning: specified toolchain doesn't match with path to the memory map file."
screamer 8:a8ac6ed29081 323 return
screamer 8:a8ac6ed29081 324
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 325 for root, dir, obj_files in os.walk(search_path):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 326 for obj_file in obj_files:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 327 if obj_file.endswith(".o"):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 328 module_name, object_name = self.path_object_to_module_name(os.path.join(root, obj_file))
screamer 8:a8ac6ed29081 329
screamer 8:a8ac6ed29081 330 if object_name in self.object_to_module:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 331 if debug:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 332 print "WARNING: multiple usages of object file: %s" % object_name
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 333 print " Current: %s" % self.object_to_module[object_name]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 334 print " New: %s" % module_name
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 335 print " "
screamer 8:a8ac6ed29081 336 else:
screamer 8:a8ac6ed29081 337 self.object_to_module.update({object_name:module_name})
screamer 8:a8ac6ed29081 338
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 339 def generate_output(self, export_format, file_output=None):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 340 """
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 341 Generates summary of memory map data
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 342
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 343 Parameters
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 344 json_mode: generates output in json formal (True/False)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 345 file_desc: descriptor (either stdout or file)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 346 """
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 347
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 348 try:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 349 if file_output:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 350 file_desc = open(file_output, 'wb')
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 351 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 352 file_desc = sys.stdout
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 353 except IOError as error:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 354 print "I/O error({0}): {1}".format(error.errno, error.strerror)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 355 return False
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 356
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 357 # Calculate misc flash sections
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 358 misc_flash_mem = 0
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 359 for i in self.modules:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 360 for k in self.misc_flash_sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 361 if self.modules[i][k]:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 362 misc_flash_mem += self.modules[i][k]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 363
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 364 # Create table
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 365 columns = ['Module']
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 366 for i in list(self.print_sections):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 367 columns.append(i)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 368
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 369 table = PrettyTable(columns)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 370 table.align["Module"] = "l"
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 371
screamer 13:ab47a20b66f0 372 for i in list(self.print_sections):
screamer 13:ab47a20b66f0 373 table.align[i] = 'r'
screamer 13:ab47a20b66f0 374
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 375 subtotal = dict()
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 376 for k in self.sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 377 subtotal[k] = 0
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 378
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 379 json_obj = []
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 380 for i in sorted(self.modules):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 381
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 382 row = []
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 383 row.append(i)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 384
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 385 for k in self.sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 386 subtotal[k] += self.modules[i][k]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 387
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 388 for k in self.print_sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 389 row.append(self.modules[i][k])
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 390
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 391 json_obj.append({"module":i, "size":{\
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 392 k:self.modules[i][k] for k in self.print_sections}})
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 393
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 394 table.add_row(row)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 395
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 396 subtotal_row = ['Subtotals']
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 397 for k in self.print_sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 398 subtotal_row.append(subtotal[k])
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 399
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 400 table.add_row(subtotal_row)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 401
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 402 if export_format == 'json':
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 403 json_obj.append({\
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 404 'summary':{\
screamer 13:ab47a20b66f0 405 'total_static_ram':(subtotal['.data']+subtotal['.bss']),\
screamer 13:ab47a20b66f0 406 'allocated_heap':(subtotal['.heap']),\
screamer 13:ab47a20b66f0 407 'allocated_stack':(subtotal['.stack']),\
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 408 'total_ram':(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack']),\
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 409 'total_flash':(subtotal['.text']+subtotal['.data']+misc_flash_mem),}})
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 410
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 411 file_desc.write(json.dumps(json_obj, indent=4))
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 412 file_desc.write('\n')
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 413
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 414 elif export_format == 'csv-ci': # CSV format for the CI system
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 415
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 416 csv_writer = csv.writer(file_desc, delimiter=',', quoting=csv.QUOTE_NONE)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 417
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 418 csv_module_section = []
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 419 csv_sizes = []
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 420 for i in sorted(self.modules):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 421 for k in self.print_sections:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 422 csv_module_section += [i+k]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 423 csv_sizes += [self.modules[i][k]]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 424
screamer 13:ab47a20b66f0 425 csv_module_section += ['total_static_ram']
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 426 csv_sizes += [subtotal['.data']+subtotal['.bss']]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 427
screamer 13:ab47a20b66f0 428 csv_module_section += ['allocated_heap']
screamer 13:ab47a20b66f0 429 if subtotal['.heap'] == 0:
screamer 13:ab47a20b66f0 430 csv_sizes += ['unknown']
screamer 13:ab47a20b66f0 431 else:
screamer 13:ab47a20b66f0 432 csv_sizes += [subtotal['.heap']]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 433
screamer 13:ab47a20b66f0 434 csv_module_section += ['allocated_stack']
screamer 13:ab47a20b66f0 435 if subtotal['.stack'] == 0:
screamer 13:ab47a20b66f0 436 csv_sizes += ['unknown']
screamer 13:ab47a20b66f0 437 else:
screamer 13:ab47a20b66f0 438 csv_sizes += [subtotal['.stack']]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 439
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 440 csv_module_section += ['total_ram']
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 441 csv_sizes += [subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack']]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 442
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 443 csv_module_section += ['total_flash']
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 444 csv_sizes += [subtotal['.text']+subtotal['.data']+misc_flash_mem]
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 445
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 446 csv_writer.writerow(csv_module_section)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 447 csv_writer.writerow(csv_sizes)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 448
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 449 else: # default format is 'table'
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 450 file_desc.write(table.get_string())
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 451 file_desc.write('\n')
screamer 13:ab47a20b66f0 452
screamer 13:ab47a20b66f0 453 if subtotal['.heap'] == 0:
screamer 13:ab47a20b66f0 454 file_desc.write("Allocated Heap: unknown\n")
screamer 13:ab47a20b66f0 455 else:
screamer 13:ab47a20b66f0 456 file_desc.write("Allocated Heap: %s bytes\n" % str(subtotal['.heap']))
screamer 13:ab47a20b66f0 457
screamer 13:ab47a20b66f0 458 if subtotal['.stack'] == 0:
screamer 13:ab47a20b66f0 459 file_desc.write("Allocated Stack: unknown\n")
screamer 13:ab47a20b66f0 460 else:
screamer 13:ab47a20b66f0 461 file_desc.write("Allocated Stack: %s bytes\n" % str(subtotal['.stack']))
screamer 13:ab47a20b66f0 462
screamer 13:ab47a20b66f0 463 file_desc.write("Total Static RAM memory (data + bss): %s bytes\n" % (str(subtotal['.data']+subtotal['.bss'])))
screamer 13:ab47a20b66f0 464 file_desc.write("Total RAM memory (data + bss + heap + stack): %s bytes\n" % (str(subtotal['.data']+subtotal['.bss']+subtotal['.heap']+subtotal['.stack'])))
screamer 13:ab47a20b66f0 465 file_desc.write("Total Flash memory (text + data + misc): %s bytes\n" % (str(subtotal['.text']+subtotal['.data']+misc_flash_mem)))
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 466
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 467 if file_desc is not sys.stdout:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 468 file_desc.close()
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 469
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 470 return True
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 471
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 472 def parse(self, mapfile, toolchain):
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 473 """
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 474 Parse and decode map file depending on the toolchain
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 475 """
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 476
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 477 try:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 478 file_input = open(mapfile, 'rt')
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 479 except IOError as error:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 480 print "I/O error({0}): {1}".format(error.errno, error.strerror)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 481 return False
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 482
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 483 if toolchain == "ARM" or toolchain == "ARM_STD" or toolchain == "ARM_MICRO":
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 484 self.search_objects(os.path.abspath(mapfile), "ARM")
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 485 self.parse_map_file_armcc(file_input)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 486 elif toolchain == "GCC_ARM":
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 487 self.parse_map_file_gcc(file_input)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 488 elif toolchain == "IAR":
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 489 self.search_objects(os.path.abspath(mapfile), toolchain)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 490 self.parse_map_file_iar(file_input)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 491 else:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 492 return False
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 493
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 494 file_input.close()
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 495
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 496 return True
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 497
screamer 8:a8ac6ed29081 498 def main():
screamer 8:a8ac6ed29081 499
screamer 13:ab47a20b66f0 500 version = '0.3.11'
screamer 8:a8ac6ed29081 501
screamer 8:a8ac6ed29081 502 # Parser handling
screamer 13:ab47a20b66f0 503 parser = argparse.ArgumentParser(description="Memory Map File Analyser for ARM mbed\nversion %s" % version)
screamer 8:a8ac6ed29081 504
screamer 8:a8ac6ed29081 505 parser.add_argument('file', help='memory map file')
screamer 8:a8ac6ed29081 506
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 507 parser.add_argument('-t', '--toolchain', dest='toolchain', help='select a toolchain used to build the memory map file (ARM, GCC_ARM, IAR)',\
screamer 8:a8ac6ed29081 508 required=True)
screamer 8:a8ac6ed29081 509
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 510 parser.add_argument('-o', '--output', help='output file name', required=False)
screamer 8:a8ac6ed29081 511
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 512 parser.add_argument('-e', '--export', dest='export', required=False,\
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 513 help="export format (examples: 'json', 'csv-ci', 'table': default)")
screamer 8:a8ac6ed29081 514
screamer 8:a8ac6ed29081 515 parser.add_argument('-v', '--version', action='version', version=version)
screamer 8:a8ac6ed29081 516
screamer 8:a8ac6ed29081 517 # Parse/run command
screamer 8:a8ac6ed29081 518 if len(sys.argv) <= 1:
screamer 8:a8ac6ed29081 519 parser.print_help()
screamer 8:a8ac6ed29081 520 sys.exit(1)
screamer 8:a8ac6ed29081 521
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 522
screamer 8:a8ac6ed29081 523 args, remainder = parser.parse_known_args()
screamer 8:a8ac6ed29081 524
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 525 # Create memap object
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 526 memap = MemapParser()
screamer 8:a8ac6ed29081 527
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 528 # Parse and decode a map file
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 529 if args.file and args.toolchain:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 530 if memap.parse(args.file, args.toolchain) is False:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 531 print "Unknown toolchain for memory statistics %s" % args.toolchain
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 532 sys.exit(0)
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 533
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 534 # default export format is table
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 535 if not args.export:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 536 args.export = 'table'
screamer 8:a8ac6ed29081 537
screamer 8:a8ac6ed29081 538 # Write output in file
screamer 8:a8ac6ed29081 539 if args.output != None:
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 540 memap.generate_output(args.export, args.output)
screamer 8:a8ac6ed29081 541 else: # Write output in screen
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 542 memap.generate_output(args.export)
screamer 8:a8ac6ed29081 543
screamer 8:a8ac6ed29081 544 sys.exit(0)
screamer 8:a8ac6ed29081 545
screamer 8:a8ac6ed29081 546 if __name__ == "__main__":
Screamer@Y5070-M.virtuoso 9:2d27d77ada5c 547 main()