C++ 如何以图形方式显示 .map 文件中的内存布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could I graphically display the memory layout from a .map file?
提问by JeffV
My gcc build toolchain produces a .map file. How do I display the memory map graphically?
我的 gcc 构建工具链生成一个 .map 文件。如何以图形方式显示内存映射?
采纳答案by Frank Krueger
Here's the beginnings of a script in Python. It loads the map file into a list of Sections and Symbols (first half). It then renders the map using HTML (or do whatever you want with the sections
and symbols
lists).
这是 Python 脚本的开头部分。它将地图文件加载到部分和符号列表中(前半部分)。然后它使用 HTML 呈现地图(或使用sections
和symbols
列表执行您想要的任何操作)。
You can control the script by modifying these lines:
您可以通过修改这些行来控制脚本:
with open('t.map') as f:
colors = ['9C9F84', 'A97D5D', 'F7DCB4', '5C755E']
total_height = 32.0
map2html.py
地图2html.py
from __future__ import with_statement
import re
class Section:
def __init__(self, address, size, segment, section):
self.address = address
self.size = size
self.segment = segment
self.section = section
def __str__(self):
return self.section+""
class Symbol:
def __init__(self, address, size, file, name):
self.address = address
self.size = size
self.file = file
self.name = name
def __str__(self):
return self.name
#===============================
# Load the Sections and Symbols
#
sections = []
symbols = []
with open('t.map') as f:
in_sections = True
for line in f:
m = re.search('^([0-9A-Fx]+)\s+([0-9A-Fx]+)\s+((\[[ 0-9]+\])|\w+)\s+(.*?)\s*$', line)
if m:
if in_sections:
sections.append(Section(eval(m.group(1)), eval(m.group(2)), m.group(3), m.group(5)))
else:
symbols.append(Symbol(eval(m.group(1)), eval(m.group(2)), m.group(3), m.group(5)))
else:
if len(sections) > 0:
in_sections = False
#===============================
# Gererate the HTML File
#
colors = ['9C9F84', 'A97D5D', 'F7DCB4', '5C755E']
total_height = 32.0
segments = set()
for s in sections: segments.add(s.segment)
segment_colors = dict()
i = 0
for s in segments:
segment_colors[s] = colors[i % len(colors)]
i += 1
total_size = 0
for s in symbols:
total_size += s.size
sections.sort(lambda a,b: a.address - b.address)
symbols.sort(lambda a,b: a.address - b.address)
def section_from_address(addr):
for s in sections:
if addr >= s.address and addr < (s.address + s.size):
return s
return None
print "<html><head>"
print " <style>a { color: black; text-decoration: none; font-family:monospace }</style>"
print "<body>"
print "<table cellspacing='1px'>"
for sym in symbols:
section = section_from_address(sym.address)
height = (total_height/total_size) * sym.size
font_size = 1.0 if height > 1.0 else height
print "<tr style='background-color:#%s;height:%gem;line-height:%gem;font-size:%gem'><td style='overflow:hidden'>" % \
(segment_colors[section.segment], height, height, font_size)
print "<a href='#%s'>%s</a>" % (sym.name, sym.name)
print "</td></tr>"
print "</table>"
print "</body></html>"
And here's a bad rendering of the HTML it outputs:
这是它输出的 HTML 的糟糕呈现:
回答by Sredni
I've written a C# program to display the information in a Map file along with information not usually present in the map file (like static symbols provided you can use binutils
). The code is available here. In short it parses the map file and also uses BINUTILS
(if available) to gather more information. To run it you need to download the code and run the project under visual studio, browse to the map file path and click Analyze
.
我编写了一个 C# 程序来显示地图文件中的信息以及地图文件中通常不存在的信息(例如您可以使用的静态符号binutils
)。该代码可在此处获得。简而言之,它解析地图文件并使用BINUTILS
(如果可用)来收集更多信息。要运行它,您需要下载代码并在visual studio 下运行该项目,浏览到地图文件路径并单击Analyze
。
Note: Only works for GCC/LD
map files
注意:仅适用于GCC/LD
地图文件
Screenshot:
[
截屏: [