3 # Copyright (C) 2009 The Android Open Source Project
5 # Licensed under the Apache License, Version 2.0 (the 'License');
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an 'AS IS' BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 ###############################################################################
23 # match "#00 pc 0003f52e /system/lib/libdvm.so" for example
24 ###############################################################################
25 trace_line = re.compile("(.*)(\#[0-9]+) (..) ([0-9a-f]{8}) ([^\r\n \t]*)")
27 # returns a list containing the function name and the file/lineno
28 def CallAddr2Line(lib, addr):
34 cmd = addr2line_cmd + \
35 " -f -e " + symbols_dir + lib + " 0x" + addr
36 stream = os.popen(cmd)
37 lines = stream.readlines()
38 list = map(string.strip, lines)
42 # Name like "move_forward_type<JavaVMOption>" causes troubles
43 mangled_name = re.sub('<', '\<', list[0]);
44 mangled_name = re.sub('>', '\>', mangled_name);
45 cmd = cppfilt_cmd + " " + mangled_name
46 stream = os.popen(cmd)
47 list[0] = stream.readline()
49 list = map(string.strip, list)
51 list = [ "(unknown)", "(unknown)" ]
55 ###############################################################################
56 # similar to CallAddr2Line, but using objdump to find out the name of the
57 # containing function of the specified address
58 ###############################################################################
59 def CallObjdump(lib, addr):
71 elif uname == "Linux":
74 next_addr = string.atoi(addr, 16) + 1
76 + " -C -d --start-address=0x" + addr + " --stop-address=" \
78 + " " + symbols_dir + lib
79 stream = os.popen(cmd)
80 lines = stream.readlines()
81 map(string.strip, lines)
88 # file format elf32-littlearm
90 # Disassembly of section .text:
92 # 0000833c <func+0x4>:
93 # 833c: 701a strb r2, [r3, #0]
95 # we want to extract the "func" part
96 num_lines = len(lines)
99 func_name = lines[num_lines-2]
100 func_regexp = re.compile("(^.*\<)(.*)(\+.*\>:$)")
101 components = func_regexp.match(func_name)
102 if components is None:
104 return components.group(2)
106 ###############################################################################
107 # determine the symbols directory in the local build
108 ###############################################################################
109 def FindSymbolsDir():
113 path = os.environ['ANDROID_PRODUCT_OUT'] + "/symbols"
115 cmd = "CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core " \
116 + "SRC_TARGET_DIR=build/target make -f build/core/envsetup.mk " \
117 + "dumpvar-abs-TARGET_OUT_UNSTRIPPED"
118 stream = os.popen(cmd)
123 if (not os.path.exists(path)):
124 print path + " not found!"
129 ###############################################################################
130 # determine the path of binutils
131 ###############################################################################
132 def SetupToolsPath():
138 uname = os.uname()[0]
139 if uname == "Darwin":
140 proc = os.uname()[-1]
145 elif uname == "Linux":
147 prefix = "./prebuilt/" + uname + "/toolchain/arm-eabi-4.4.0/bin/"
148 addr2line_cmd = prefix + "arm-eabi-addr2line"
150 if (not os.path.exists(addr2line_cmd)):
152 prefix = os.environ['ANDROID_BUILD_TOP'] + "/prebuilt/" + uname + \
153 "/toolchain/arm-eabi-4.4.0/bin/"
157 addr2line_cmd = prefix + "arm-eabi-addr2line"
158 if (not os.path.exists(addr2line_cmd)):
159 print addr2line_cmd + " not found!"
162 objdump_cmd = prefix + "arm-eabi-objdump"
163 cppfilt_cmd = prefix + "arm-eabi-c++filt"
165 ###############################################################################
166 # look up the function and file/line number for a raw stack trace line
168 # groups[1]: stack level
170 # groups[3]: code address
171 # groups[4]: library name
172 ###############################################################################
173 def SymbolTranslation(groups):
175 code_addr = groups[3]
176 caller = CallObjdump(lib_name, code_addr)
177 func_line_pair = CallAddr2Line(lib_name, code_addr)
179 # If a callee is inlined to the caller, objdump will see the caller's
180 # address but addr2line will report the callee's address. So the printed
181 # format is desgined to be "caller<-callee file:line"
182 if (func_line_pair[0] != caller):
183 print groups[0] + groups[1] + " " + caller + "<-" + \
184 ' '.join(func_line_pair[:]) + " "
186 print groups[0] + groups[1] + " " + ' '.join(func_line_pair[:]) + " "
188 ###############################################################################
190 if __name__ == '__main__':
191 # pass the options to adb
192 adb_cmd = "adb " + ' '.join(sys.argv[1:])
194 # setup addr2line_cmd and objdump_cmd
197 # setup the symbols directory
200 # invoke the adb command and filter its output
201 stream = os.popen(adb_cmd)
203 line = stream.readline()
209 # remove the trailing \n
212 # see if this is a stack trace line
213 match = trace_line.match(line)
215 groups = match.groups()
216 # translate raw address into symbols
217 SymbolTranslation(groups)