c++ - 如何以图形方式显示 .map 文件中的内存布局?

标签 c++ c linker

我的 gcc 构建工具链生成一个 .map 文件。如何以图形方式显示内存映射?

最佳答案

这里是 Python 脚本的开端。它将 map 文件加载到部分和符号列表(前半部分)中。然后它使用 HTML 渲染 map (或使用 sectionssymbols 列表做任何你想做的事情)。

您可以通过修改这些行来控制脚本:

with open('t.map') as f:
colors = ['9C9F84', 'A97D5D', 'F7DCB4', '5C755E']
total_height = 32.0

map2html.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>"

这是它输出的 HTML 的错误渲染:

Map

关于c++ - 如何以图形方式显示 .map 文件中的内存布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48426/

相关文章:

c++ - 在构造函数中分配 std::string_view 类型是个好主意吗?

c - C 中用位表示一组整数

c++ - C++ 中的非成员静态模板方法定义?

c - 强弱符号 -> double 转 char 数组

c++ - 没有enable_if 的表达式SFINAE?

c++ - 声明嵌套类模板的静态对象

c++ - 我可以为并非所有非类型参数部分指定模板吗

c - 使用 ASCII 中的十进制索引获取字符

c - 使用结构体和变量作为函数的输入参数之间的速度差异

c++ - 可移植编程 - Win32 链接失败但与 linux 链接失败