nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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