2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
15 # The Original Code is mozilla.org code.
17 # The Initial Developer of the Original Code is The Mozilla Foundation
18 # Portions created by the Initial Developer are Copyright (C) 2009
19 # the Initial Developer. All Rights Reserved.
22 # Serge Gautherie <sgautherie.bz@free.fr>
23 # Ted Mielczarek <ted.mielczarek@gmail.com>
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
37 # ***** END LICENSE BLOCK ***** */
39 import glob
, logging
, os
, shutil
, subprocess
, sys
51 # Map of debugging programs to information about them, like default arguments
52 # and whether or not they are interactive.
54 # gdb requires that you supply the '--args' flag in order to pass arguments
55 # after the executable name to the executable.
61 # valgrind doesn't explain much about leaks unless you set the
62 # '--leak-check=full' flag.
65 "args": "--leak-check=full"
69 log
= logging
.getLogger()
71 def addCommonOptions(parser
, defaults
={}):
72 parser
.add_option("--xre-path",
73 action
= "store", type = "string", dest
= "xrePath",
74 # individual scripts will set a sane default
76 help = "absolute path to directory containing XRE (probably xulrunner)")
77 if 'SYMBOLS_PATH' not in defaults
:
78 defaults
['SYMBOLS_PATH'] = None
79 parser
.add_option("--symbols-path",
80 action
= "store", type = "string", dest
= "symbolsPath",
81 default
= defaults
['SYMBOLS_PATH'],
82 help = "absolute path to directory containing breakpad symbols")
83 parser
.add_option("--debugger",
84 action
= "store", dest
= "debugger",
85 help = "use the given debugger to launch the application")
86 parser
.add_option("--debugger-args",
87 action
= "store", dest
= "debuggerArgs",
88 help = "pass the given args to the debugger _before_ "
89 "the application on the command line")
90 parser
.add_option("--debugger-interactive",
91 action
= "store_true", dest
= "debuggerInteractive",
92 help = "prevents the test harness from redirecting "
93 "stdout and stderr for interactive debuggers")
95 def checkForCrashes(dumpDir
, symbolsPath
, testName
=None):
96 stackwalkPath
= os
.environ
.get('MINIDUMP_STACKWALK', None)
97 # try to get the caller's filename if no test name is given
100 testName
= os
.path
.basename(sys
._getframe
(1).f_code
.co_filename
)
105 dumps
= glob
.glob(os
.path
.join(dumpDir
, '*.dmp'))
107 log
.info("PROCESS-CRASH | %s | application crashed (minidump found)", testName
)
108 if symbolsPath
and stackwalkPath
and os
.path
.exists(stackwalkPath
):
109 nullfd
= open(os
.devnull
, 'w')
110 # eat minidump_stackwalk errors
111 subprocess
.call([stackwalkPath
, d
, symbolsPath
], stderr
=nullfd
)
115 print "No symbols path given, can't process dump."
116 if not stackwalkPath
:
117 print "MINIDUMP_STACKWALK not set, can't process dump."
119 if not os
.path
.exists(stackwalkPath
):
120 print "MINIDUMP_STACKWALK binary not found: %s" % stackwalkPath
121 dumpSavePath
= os
.environ
.get('MINIDUMP_SAVE_PATH', None)
123 shutil
.move(d
, dumpSavePath
)
124 print "Saved dump as %s" % os
.path
.join(dumpSavePath
,
128 extra
= os
.path
.splitext(d
)[0] + ".extra"
129 if os
.path
.exists(extra
):
135 def getFullPath(directory
, path
):
136 "Get an absolute path relative to 'directory'."
137 return os
.path
.normpath(os
.path
.join(directory
, os
.path
.expanduser(path
)))
139 def searchPath(directory
, path
):
140 "Go one step beyond getFullPath and try the various folders in PATH"
141 # Try looking in the current working directory first.
142 newpath
= getFullPath(directory
, path
)
143 if os
.path
.isfile(newpath
):
146 # At this point we have to fail if a directory was given (to prevent cases
147 # like './gdb' from matching '/usr/bin/./gdb').
148 if not os
.path
.dirname(path
):
149 for dir in os
.environ
['PATH'].split(os
.pathsep
):
150 newpath
= os
.path
.join(dir, path
)
151 if os
.path
.isfile(newpath
):
155 def getDebuggerInfo(directory
, debugger
, debuggerArgs
, debuggerInteractive
= False):
160 debuggerPath
= searchPath(directory
, debugger
)
162 print "Error: Path %s doesn't exist." % debugger
165 debuggerName
= os
.path
.basename(debuggerPath
).lower()
167 def getDebuggerInfo(type, default
):
168 if debuggerName
in DEBUGGER_INFO
and type in DEBUGGER_INFO
[debuggerName
]:
169 return DEBUGGER_INFO
[debuggerName
][type]
173 "path": debuggerPath
,
174 "interactive" : getDebuggerInfo("interactive", False),
175 "args": getDebuggerInfo("args", "").split()
179 debuggerInfo
["args"] = debuggerArgs
.split()
180 if debuggerInteractive
:
181 debuggerInfo
["interactive"] = debuggerInteractive
186 def dumpLeakLog(leakLogFile
, filter = False):
187 """Process the leak log, without parsing it.
189 Use this function if you want the raw log only.
190 Use it preferably with the |XPCOM_MEM_LEAK_LOG| environment variable.
193 # Don't warn (nor "info") if the log file is not there.
194 if not os
.path
.exists(leakLogFile
):
197 leaks
= open(leakLogFile
, "r")
198 leakReport
= leaks
.read()
201 # Only |XPCOM_MEM_LEAK_LOG| reports can be actually filtered out.
202 # Only check whether an actual leak was reported.
203 if filter and not "0 TOTAL " in leakReport
:
206 # Simply copy the log.
207 log
.info(leakReport
.rstrip("\n"))
209 def processSingleLeakFile(leakLogFileName
, PID
, processType
, leakThreshold
):
210 """Process a single leak log, corresponding to the specified
211 process PID and type.
214 # Per-Inst Leaked Total Rem ...
215 # 0 TOTAL 17 192 419115886 2 ...
216 # 833 nsTimerImpl 60 120 24726 2 ...
217 lineRe
= re
.compile(r
"^\s*\d+\s+(?P<name>\S+)\s+"
218 r
"(?P<size>-?\d+)\s+(?P<bytesLeaked>-?\d+)\s+"
219 r
"-?\d+\s+(?P<numLeaked>-?\d+)")
222 if PID
and processType
:
223 processString
= "| %s process %s " % (processType
, PID
)
224 leaks
= open(leakLogFileName
, "r")
226 matches
= lineRe
.match(line
)
228 int(matches
.group("numLeaked")) == 0 and
229 matches
.group("name") != "TOTAL"):
231 log
.info(line
.rstrip())
234 leaks
= open(leakLogFileName
, "r")
236 crashedOnPurpose
= False
239 if line
.find("purposefully crash") > -1:
240 crashedOnPurpose
= True
241 matches
= lineRe
.match(line
)
244 name
= matches
.group("name")
245 size
= int(matches
.group("size"))
246 bytesLeaked
= int(matches
.group("bytesLeaked"))
247 numLeaked
= int(matches
.group("numLeaked"))
248 if size
< 0 or bytesLeaked
< 0 or numLeaked
< 0:
249 log
.info("TEST-UNEXPECTED-FAIL %s| automationutils.processLeakLog() | negative leaks caught!" %
253 elif name
== "TOTAL":
256 if bytesLeaked
< 0 or bytesLeaked
> leakThreshold
:
257 prefix
= "TEST-UNEXPECTED-FAIL"
258 leakLog
= "TEST-UNEXPECTED-FAIL %s| automationutils.processLeakLog() | leaked" \
259 " %d bytes during test execution" % (processString
, bytesLeaked
)
260 elif bytesLeaked
> 0:
261 leakLog
= "TEST-PASS %s| automationutils.processLeakLog() | WARNING leaked" \
262 " %d bytes during test execution" % (processString
, bytesLeaked
)
264 leakLog
= "TEST-PASS %s| automationutils.processLeakLog() | no leaks detected!" \
266 # Remind the threshold if it is not 0, which is the default/goal.
267 if leakThreshold
!= 0:
268 leakLog
+= " (threshold set at %d bytes)" % leakThreshold
269 # Log the information.
274 instance
= "instances"
275 rest
= " each (%s bytes total)" % matches
.group("bytesLeaked")
277 instance
= "instance"
279 log
.info("%(prefix)s %(process)s| automationutils.processLeakLog() | leaked %(numLeaked)d %(instance)s of %(name)s "
280 "with size %(size)s bytes%(rest)s" %
282 "process": processString
,
283 "numLeaked": numLeaked
,
284 "instance": instance
,
286 "size": matches
.group("size"),
290 log
.info("INFO | automationutils.processLeakLog() | process %s was " \
291 "deliberately crashed and thus has no leak log" % PID
)
293 log
.info("TEST-UNEXPECTED-FAIL %s| automationutils.processLeakLog() | missing output line for total leaks!" %
298 def processLeakLog(leakLogFile
, leakThreshold
= 0):
299 """Process the leak log, including separate leak logs created
302 Use this function if you want an additional PASS/FAIL summary.
303 It must be used with the |XPCOM_MEM_BLOAT_LOG| environment variable.
306 if not os
.path
.exists(leakLogFile
):
307 log
.info("WARNING | automationutils.processLeakLog() | refcount logging is off, so leaks can't be detected!")
310 (leakLogFileDir
, leakFileBase
) = os
.path
.split(leakLogFile
)
311 pidRegExp
= re
.compile(r
".*?_([a-z]*)_pid(\d*)$")
312 if leakFileBase
[-4:] == ".log":
313 leakFileBase
= leakFileBase
[:-4]
314 pidRegExp
= re
.compile(r
".*?_([a-z]*)_pid(\d*).log$")
316 for fileName
in os
.listdir(leakLogFileDir
):
317 if fileName
.find(leakFileBase
) != -1:
318 thisFile
= os
.path
.join(leakLogFileDir
, fileName
)
321 m
= pidRegExp
.search(fileName
)
323 processType
= m
.group(1)
324 processPID
= m
.group(2)
325 processSingleLeakFile(thisFile
, processPID
, processType
, leakThreshold
)