Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / android_platform / development / scripts / stack
blobc5a56c61e031fd1ba226ea958e3ce20cc9d9fba1
1 #!/usr/bin/env python
3 # Copyright (C) 2013 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.
17 """stack symbolizes native crash dumps."""
19 import getopt
20 import glob
21 import logging
22 import os
23 import sys
25 import stack_core
26 import subprocess
27 import symbol
28 import sys
30 DEFAULT_SYMROOT='/tmp/symbols'
32 def PrintUsage():
33 """Print usage and exit with error."""
34 # pylint: disable-msg=C6310
35 print
36 print " usage: " + sys.argv[0] + " [options] [FILE]"
37 print
38 print " --symbols-dir=path"
39 print " the path to a symbols dir, such as =/tmp/out/target/product/dream/symbols"
40 print
41 print " --chrome-symbols-dir=path"
42 print " the path to a Chrome symbols dir (can be absolute or relative"
43 print " to src), such as =out/Debug/lib"
44 print " If not specified, will look for the newest lib in out/Debug or"
45 print " out/Release"
46 print
47 print " --symbols-zip=path"
48 print " the path to a symbols zip file, such as =dream-symbols-12345.zip"
49 print
50 print " --more-info"
51 print " --less-info"
52 print " Change the level of detail in the output."
53 print " --more-info is slower and more verbose, but more functions will"
54 print " be fully qualified with namespace/classname and have full"
55 print " argument information. Also, the 'stack data' section will be"
56 print " printed."
57 print
58 print " --arch=arm|arm64|x64|x86|mips"
59 print " the target architecture"
60 print
61 print " --verbose"
62 print " enable extra logging, particularly for debugging failed symbolization"
63 print
64 print " FILE should contain a stack trace in it somewhere"
65 print " the tool will find that and re-print it with"
66 print " source files and line numbers. If you don't"
67 print " pass FILE, or if file is -, it reads from"
68 print " stdin."
69 print
70 # pylint: enable-msg=C6310
71 sys.exit(1)
73 def UnzipSymbols(symbolfile, symdir=None):
74 """Unzips a file to DEFAULT_SYMROOT and returns the unzipped location.
76 Args:
77 symbolfile: The .zip file to unzip
78 symdir: Optional temporary directory to use for extraction
80 Returns:
81 A tuple containing (the directory into which the zip file was unzipped,
82 the path to the "symbols" directory in the unzipped file). To clean
83 up, the caller can delete the first element of the tuple.
85 Raises:
86 SymbolDownloadException: When the unzip fails.
87 """
88 if not symdir:
89 symdir = "%s/%s" % (DEFAULT_SYMROOT, hash(symbolfile))
90 if not os.path.exists(symdir):
91 os.makedirs(symdir)
93 print "extracting %s..." % symbolfile
94 saveddir = os.getcwd()
95 os.chdir(symdir)
96 try:
97 unzipcode = subprocess.call(["unzip", "-qq", "-o", symbolfile])
98 if unzipcode > 0:
99 os.remove(symbolfile)
100 raise SymbolDownloadException("failed to extract symbol files (%s)."
101 % symbolfile)
102 finally:
103 os.chdir(saveddir)
105 android_symbols = glob.glob("%s/out/target/product/*/symbols" % symdir)
106 if android_symbols:
107 return (symdir, android_symbols[0])
108 else:
109 # This is a zip of Chrome symbols, so symbol.CHROME_SYMBOLS_DIR needs to be
110 # updated to point here.
111 symbol.CHROME_SYMBOLS_DIR = symdir
112 return (symdir, symdir)
115 def main(argv):
116 try:
117 options, arguments = getopt.getopt(argv, "",
118 ["more-info",
119 "less-info",
120 "chrome-symbols-dir=",
121 "symbols-dir=",
122 "symbols-zip=",
123 "arch=",
124 "verbose",
125 "help"])
126 except getopt.GetoptError, unused_error:
127 PrintUsage()
129 zip_arg = None
130 more_info = False
131 for option, value in options:
132 if option == "--help":
133 PrintUsage()
134 elif option == "--symbols-dir":
135 symbol.SYMBOLS_DIR = os.path.expanduser(value)
136 elif option == "--symbols-zip":
137 zip_arg = os.path.expanduser(value)
138 elif option == "--arch":
139 symbol.ARCH = value
140 elif option == "--chrome-symbols-dir":
141 symbol.CHROME_SYMBOLS_DIR = os.path.join(symbol.CHROME_SYMBOLS_DIR, value)
142 elif option == "--more-info":
143 more_info = True
144 elif option == "--less-info":
145 more_info = False
146 elif option == "--verbose":
147 logging.basicConfig(level=logging.DEBUG)
149 if len(arguments) > 1:
150 PrintUsage()
152 if not arguments or arguments[0] == "-":
153 print "Reading native crash info from stdin"
154 f = sys.stdin
155 else:
156 print "Searching for native crashes in %s" % arguments[0]
157 f = open(arguments[0], "r")
159 lines = f.readlines()
160 f.close()
162 rootdir = None
163 if zip_arg:
164 rootdir, symbol.SYMBOLS_DIR = UnzipSymbols(zip_arg)
166 print "Reading Android symbols from", symbol.SYMBOLS_DIR
167 print "Reading Chrome symbols from", symbol.CHROME_SYMBOLS_DIR
168 stack_core.ConvertTrace(lines, more_info)
170 if rootdir:
171 # be a good citizen and clean up...os.rmdir and os.removedirs() don't work
172 cmd = "rm -rf \"%s\"" % rootdir
173 print "\ncleaning up (%s)" % cmd
174 os.system(cmd)
176 if __name__ == "__main__":
177 sys.exit(main(sys.argv[1:]))
179 # vi: ts=2 sw=2