1*27b03b36SApple OSS Distributions#!/usr/bin/env python 2*27b03b36SApple OSS Distributionsfrom subprocess import Popen, PIPE, call 3*27b03b36SApple OSS Distributionsimport re 4*27b03b36SApple OSS Distributionsimport sys 5*27b03b36SApple OSS Distributionsimport os 6*27b03b36SApple OSS Distributions 7*27b03b36SApple OSS DistributionsSLIDE = 0 8*27b03b36SApple OSS Distributions 9*27b03b36SApple OSS DistributionsNM_FORMAT = "([0-9a-f]+) ([UuAaTtDdBbCcSsIi]) (.*)" 10*27b03b36SApple OSS Distributions 11*27b03b36SApple OSS Distributionsnm_re = re.compile(NM_FORMAT) 12*27b03b36SApple OSS Distributions 13*27b03b36SApple OSS Distributionsdef parse_nm_output(str): 14*27b03b36SApple OSS Distributions "returns (start, type, name)" 15*27b03b36SApple OSS Distributions m = nm_re.match(str) 16*27b03b36SApple OSS Distributions if m: 17*27b03b36SApple OSS Distributions start = int(m.group(1), 16) 18*27b03b36SApple OSS Distributions return (start, m.group(2), m.group(3)) 19*27b03b36SApple OSS Distributions else: 20*27b03b36SApple OSS Distributions return None 21*27b03b36SApple OSS Distributions 22*27b03b36SApple OSS Distributionsdef nm(file): 23*27b03b36SApple OSS Distributions cmd = "nm %s" % file 24*27b03b36SApple OSS Distributions p = Popen(cmd, shell=True, stdout=PIPE) 25*27b03b36SApple OSS Distributions return p.stdout 26*27b03b36SApple OSS Distributions 27*27b03b36SApple OSS Distributionsclass SymbolLookup: 28*27b03b36SApple OSS Distributions def __init__(self, file, min_width=16): 29*27b03b36SApple OSS Distributions self.min_width = min_width 30*27b03b36SApple OSS Distributions self.symbols = [parse_nm_output(l) for l in nm(file)] 31*27b03b36SApple OSS Distributions self.symbols.sort(key=lambda x: x[0]) 32*27b03b36SApple OSS Distributions 33*27b03b36SApple OSS Distributions def padded(self, str): 34*27b03b36SApple OSS Distributions return ("%%%ds" % self.min_width) % str 35*27b03b36SApple OSS Distributions 36*27b03b36SApple OSS Distributions def __call__(self, saddr): 37*27b03b36SApple OSS Distributions addr = int(saddr.group(0), 16) 38*27b03b36SApple OSS Distributions last = (0, ' ', '<start of file>') 39*27b03b36SApple OSS Distributions if( addr > SLIDE ): 40*27b03b36SApple OSS Distributions addr -= SLIDE 41*27b03b36SApple OSS Distributions # stupid linear search... feel free to improve 42*27b03b36SApple OSS Distributions for s in self.symbols: 43*27b03b36SApple OSS Distributions if s[0] == addr: 44*27b03b36SApple OSS Distributions return self.padded(s[2]) 45*27b03b36SApple OSS Distributions elif s[0] > addr: 46*27b03b36SApple OSS Distributions if last[2] == "_last_kernel_symbol": 47*27b03b36SApple OSS Distributions return saddr.group(0) 48*27b03b36SApple OSS Distributions return self.padded("<%s>+%x" % (last[2], addr - last[0])) 49*27b03b36SApple OSS Distributions else: 50*27b03b36SApple OSS Distributions last = s 51*27b03b36SApple OSS Distributions if last[2] == "_last_kernel_symbol": 52*27b03b36SApple OSS Distributions return saddr.group(0) 53*27b03b36SApple OSS Distributions return self.padded("<%s>+%x" % (last[2], addr - last[0])) 54*27b03b36SApple OSS Distributions 55*27b03b36SApple OSS Distributionsdef symbolify(objfile, input, *args, **kargs): 56*27b03b36SApple OSS Distributions replacer = SymbolLookup(objfile, *args, **kargs) 57*27b03b36SApple OSS Distributions for l in input: 58*27b03b36SApple OSS Distributions print re.sub("(0x)?[0-9a-f]{6,16}", replacer, l), 59*27b03b36SApple OSS Distributions 60*27b03b36SApple OSS Distributions 61*27b03b36SApple OSS Distributionsdef usage(): 62*27b03b36SApple OSS Distributions 63*27b03b36SApple OSS Distributions print "usage: %s [filename] [slide]" % sys.argv[0] 64*27b03b36SApple OSS Distributions print "\tor speficy a filename in your SYMBOLIFY_KERNEL environment variable" 65*27b03b36SApple OSS Distributions 66*27b03b36SApple OSS Distributions # die now 67*27b03b36SApple OSS Distributions sys.exit(1) 68*27b03b36SApple OSS Distributions 69*27b03b36SApple OSS DistributionsKERNEL_FILE = None 70*27b03b36SApple OSS Distributions 71*27b03b36SApple OSS Distributionsif( len(sys.argv) > 3 ): 72*27b03b36SApple OSS Distributions usage() 73*27b03b36SApple OSS Distributions 74*27b03b36SApple OSS Distributionsif( len(sys.argv) == 3 ): 75*27b03b36SApple OSS Distributions SLIDE = int(sys.argv[2], 16) 76*27b03b36SApple OSS Distributions 77*27b03b36SApple OSS Distributionsif( len(sys.argv) >= 2 ): 78*27b03b36SApple OSS Distributions KERNEL_FILE = sys.argv[1] 79*27b03b36SApple OSS Distributions 80*27b03b36SApple OSS Distributionsif( KERNEL_FILE is None ): 81*27b03b36SApple OSS Distributions KERNEL_FILE = os.environ.get("SYMBOLIFY_KERNEL") 82*27b03b36SApple OSS Distributions 83*27b03b36SApple OSS Distributionsif( KERNEL_FILE is None ): 84*27b03b36SApple OSS Distributions usage() 85*27b03b36SApple OSS Distributions 86*27b03b36SApple OSS Distributionsprint "using kernel file '%s', slide 0x%x" % (KERNEL_FILE, SLIDE) 87*27b03b36SApple OSS Distributions 88*27b03b36SApple OSS Distributionssymbolify(KERNEL_FILE, sys.stdin, min_width=40) 89*27b03b36SApple OSS Distributions 90