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