<s> import argparse <EOL> import commands <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> def get_objdump ( func ) : <EOL> for l in open ( "<STR_LIT>" ) : <EOL> addr , this_func = l . split ( ) <EOL> if this_func == func : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + func , "<STR_LIT>" % addr ] , stdout = subprocess . PIPE ) <EOL> r = p . communicate ( ) [ <NUM_LIT:0> ] <EOL> assert p . wait ( ) == <NUM_LIT:0> <EOL> return r <EOL> raise Exception ( "<STR_LIT>" % func ) <EOL> _symbols = None <EOL> def lookupAsSymbol ( n ) : <EOL> global _symbols <EOL> if _symbols is None : <EOL> _symbols = { } <EOL> nm_output = commands . getoutput ( "<STR_LIT>" ) <EOL> for l in nm_output . split ( '<STR_LIT:\n>' ) : <EOL> addr = l [ : <NUM_LIT:16> ] <EOL> if addr . isalnum ( ) : <EOL> _symbols [ int ( addr , <NUM_LIT:16> ) ] = l [ <NUM_LIT> : ] <EOL> sym = _symbols . get ( n , None ) <EOL> if not sym : <EOL> return sym <EOL> demangled = None <EOL> if sym . startswith ( '<STR_LIT:_>' ) and os . path . exists ( "<STR_LIT>" ) : <EOL> demangled = commands . getoutput ( "<STR_LIT>" % sym ) <EOL> if demangled != "<STR_LIT>" : <EOL> return demangled <EOL> return sym + "<STR_LIT>" <EOL> _heap_proc = None <EOL> heapmap_args = None <EOL> def lookupAsHeapAddr ( n ) : <EOL> global _heap_proc <EOL> if _heap_proc is None : <EOL> if heapmap_args is None : <EOL> return None <EOL> _heap_proc = subprocess . Popen ( heapmap_args , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> while True : <EOL> l = _heap_proc . stdout . readline ( ) <EOL> if l . startswith ( "<STR_LIT>" ) : <EOL> break <EOL> _heap_proc . stdin . write ( "<STR_LIT>" % n ) <EOL> lines = [ ] <EOL> while True : <EOL> l = _heap_proc . stdout . readline ( ) <EOL> if l == '<STR_LIT>' : <EOL> break <EOL> lines . append ( l ) <EOL> s = '<STR_LIT>' . join ( lines [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> if "<STR_LIT>" in s : <EOL> return "<STR_LIT:None>" <EOL> if "<STR_LIT>" in s : <EOL> return "<STR_LIT>" <EOL> if "<STR_LIT>" in s : <EOL> return "<STR_LIT>" <EOL> if "<STR_LIT>" in s : <EOL> m = re . search ( "<STR_LIT>" , s ) <EOL> return "<STR_LIT>" % m . group ( <NUM_LIT:1> ) <EOL> if "<STR_LIT>" in s : <EOL> m = re . search ( "<STR_LIT>" , s ) <EOL> return "<STR_LIT>" % m . group ( <NUM_LIT:1> ) <EOL> print s <EOL> def lookupConstant ( n ) : <EOL> sym = lookupAsSymbol ( n ) <EOL> if sym : <EOL> return "<STR_LIT>" + sym <EOL> heap = lookupAsHeapAddr ( n ) <EOL> if heap : <EOL> return "<STR_LIT>" + heap <EOL> return "<STR_LIT>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = argparse . ArgumentParser ( formatter_class = argparse . RawTextHelpFormatter ) <EOL> parser . add_argument ( "<STR_LIT>" , metavar = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = True ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , nargs = '<STR_LIT:+>' , help = """<STR_LIT>""" . strip ( ) ) <EOL> parser . add_argument ( "<STR_LIT>" , help = """<STR_LIT>""" . strip ( ) ) <EOL> parser . add_argument ( "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> func = args . func_name <EOL> if args . heap_map_args : <EOL> heapmap_args = args . heap_map_args <EOL> elif args . heap_map_target : <EOL> heapmap_args = [ "<STR_LIT>" , "<STR_LIT>" , args . heap_map_target ] <EOL> objdump = get_objdump ( func ) <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , args . perf_data , "<STR_LIT>" , func ] , stdout = subprocess . PIPE , stderr = open ( "<STR_LIT>" , "<STR_LIT:w>" ) ) <EOL> annotate = p . communicate ( ) [ <NUM_LIT:0> ] <EOL> assert p . wait ( ) == <NUM_LIT:0> <EOL> counts = { } <EOL> for l in annotate . split ( '<STR_LIT:\n>' ) : <EOL> if '<STR_LIT::>' not in l : <EOL> continue <EOL> addr , count = l . split ( '<STR_LIT::>' ) <EOL> addr = addr . strip ( ) <EOL> if addr == "<STR_LIT>" : <EOL> continue <EOL> counts [ addr ] = int ( count ) <EOL> nops = None <EOL> for l in objdump . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:7> : ] : <EOL> addr = l . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> count = counts . pop ( addr . strip ( ) , <NUM_LIT:0> ) <EOL> extra = "<STR_LIT>" <EOL> m = re . search ( "<STR_LIT>" , l ) <EOL> if m : <EOL> n = int ( m . group ( <NUM_LIT:1> ) , <NUM_LIT:16> ) <EOL> extra = lookupConstant ( n ) <EOL> m = re . search ( "<STR_LIT>" , l ) <EOL> if m : <EOL> n = int ( m . group ( <NUM_LIT:1> ) , <NUM_LIT:16> ) <EOL> extra = lookupConstant ( n ) <EOL> if args . collapse_nops and l . endswith ( "<STR_LIT>" ) : <EOL> addr = l . split ( ) [ <NUM_LIT:0> ] [ : - <NUM_LIT:1> ] <EOL> if not nops : <EOL> nops = ( count , <NUM_LIT:1> , addr , addr ) <EOL> else : <EOL> nops = ( nops [ <NUM_LIT:0> ] + count , nops [ <NUM_LIT:1> ] + <NUM_LIT:1> , nops [ <NUM_LIT:2> ] , addr ) <EOL> else : <EOL> if nops : <EOL> print str ( nops [ <NUM_LIT:0> ] ) . rjust ( <NUM_LIT:8> ) , ( "<STR_LIT>" % ( nops [ <NUM_LIT:2> ] , nops [ <NUM_LIT:3> ] , nops [ <NUM_LIT:1> ] ) ) . ljust ( <NUM_LIT> ) <EOL> nops = None <EOL> print str ( count ) . rjust ( <NUM_LIT:8> ) , l . ljust ( <NUM_LIT> ) , extra <EOL> assert not counts , counts </s>
