Skip to content

Instantly share code, notes, and snippets.

@IsuraManchanayake
Created June 7, 2019 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IsuraManchanayake/9386d6df050c5feba51366ef56784e6b to your computer and use it in GitHub Desktop.
Save IsuraManchanayake/9386d6df050c5feba51366ef56784e6b to your computer and use it in GitHub Desktop.
Demangle the mangled names in C++ flamegraphs (perf)
import cxxfilt
def toxml(s):
s = str.replace(str(s), '&', '&')
s = str.replace(str(s), '<', '&lt;')
return str.replace(str(s), '>', '&gt;')
def tostr(s):
s = str.replace(str(s), '&gt;', '>')
s = str.replace(str(s), '&lt;', '<')
return str.replace(str(s), '&amp;', '&')
# import re
fname = '/home/smartfm/isura/flamegraphs/flamegraphs/2019-06-04-10:55:20perf-kernel.svg'
def main():
# doc = minidom.parse(fname)
# path_strings = doc.getElementsByTagName('title')
# doc.unlink()
# for ps in path_strings[:10]:
# if ps.firstChild:
# print(repr(ps.firstChild.nodeValue.strip()))
fstr = open(fname).read()
outs = ''
invalids = 0
dc = 0
f = True
i = 0
demangled = ''
while i < len(fstr):
if dc % 100 == 0 and dc != 0 and f:
f = False
print(dc)
if fstr[i:i+7] == '<title>':
for j in range(i + 7, len(fstr)):
if fstr[j:j + 2] == ' (':
mangled = fstr[i + 7:j]
try:
demangled = cxxfilt.demangle(mangled)
except cxxfilt.InvalidName:
demangled = mangled
dc += 1
f = True
demangled = toxml(demangled)
outs += '<title>' + demangled
i = j
break
if fstr[i:i + 5] == '<text':
i += 5
outs += '<text '
if fstr[i + 1] != ' ':
i += 1
continue
while fstr[i] != '>':
i += 1
outs += fstr[i]
for j in range(i + 1, len(fstr)):
if fstr[j:j + 7] == '</text>':
lentext = j - i - 1
newtext = fstr[i + 1:j]
if fstr[i + 1:j] != demangled and lentext != 0:
newtext = toxml(tostr(demangled)[:lentext - 2] + '..')
outs += newtext + '</text>'
i = j + 7 - 1
break
else:
outs += fstr[i]
i += 1
print(str.replace(fname, '.svg', '-demangled.svg'))
open(str.replace(fname, '.svg', '-demangled.svg'), 'w').write(outs)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment