]>
git.saurik.com Git - apple/xnu.git/blob - tools/symbolify.py
dde29a7326d0fab1467a835c9f6792242b8b9b7a
2 from subprocess
import Popen
, PIPE
, call
7 NM_FORMAT
= "([0-9a-f]+) ([UuAaTtDdBbCcSsIi]) (.*)"
9 nm_re
= re
.compile(NM_FORMAT
)
11 def parse_nm_output(str):
12 "returns (start, type, name)"
15 start
= int(m
.group(1), 16)
16 return (start
, m
.group(2), m
.group(3))
22 p
= Popen(cmd
, shell
=True, stdout
=PIPE
)
26 def __init__(self
, file, min_width
=16):
27 self
.min_width
= min_width
28 self
.symbols
= [parse_nm_output(l
) for l
in nm(file)]
29 self
.symbols
.sort(key
=lambda x
: x
[0])
31 def padded(self
, str):
32 return ("%%%ds" % self
.min_width
) % str
34 def __call__(self
, saddr
):
35 addr
= int(saddr
.group(0), 16)
36 last
= (0, ' ', '<start of file>')
37 # stupid linear search... feel free to improve
38 for s
in self
.symbols
:
40 return self
.padded(s
[2])
42 if last
[2] == "_last_kernel_symbol":
44 return self
.padded("<%s>+%x" % (last
[2], addr
- last
[0]))
47 if last
[2] == "_last_kernel_symbol":
49 return self
.padded("<%s>+%x" % (last
[2], addr
- last
[0]))
51 def symbolify(objfile
, input, *args
, **kargs
):
52 replacer
= SymbolLookup(objfile
, *args
, **kargs
)
54 print re
.sub("(0x)?[0-9a-f]{6,16}", replacer
, l
),
59 print "usage: %s [filename]" % sys
.argv
[0]
60 print "\tor speficy a filename in your SYMBOLIFY_KERNEL environment variable"
67 if( len(sys
.argv
) > 2 ):
70 if( len(sys
.argv
) == 2 ):
71 KERNEL_FILE
= sys
.argv
[1]
73 if( KERNEL_FILE
is None ):
74 KERNEL_FILE
= os
.environ
.get("SYMBOLIFY_KERNEL")
76 if( KERNEL_FILE
is None ):
79 print "using kernel file '%s'" % KERNEL_FILE
81 symbolify(KERNEL_FILE
, sys
.stdin
, min_width
=40)