Print retraced stack-trace on test-error

Change-Id: Idfbf5d0a1d40039319b5217daf9fca29fef9e5c1
diff --git a/build.gradle b/build.gradle
index d2df8f8..8ff95c8 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1421,6 +1421,30 @@
     outputs.dir r8LibTestPath
 }
 
+def printStackTrace(TestResult result) {
+    if (project.hasProperty('r8lib') || project.hasProperty('r8lib_no_deps')) {
+        def out = new StringBuffer()
+        def err = new StringBuffer()
+        def command = "tools/retrace.py"
+        out.append("\n--------------------------------------\n")
+        out.append("RETRACED STACKTRACE\n")
+        out.append("--------------------------------------\n")
+        Process process = command.execute()
+        def processIn = new PrintStream(process.getOut())
+        process.consumeProcessOutput(out, err)
+        result.exception.printStackTrace(processIn)
+        processIn.flush()
+        processIn.close()
+        process.waitFor()
+        out.append("\n\n--------------------------------------\n")
+        out.append("OBFUSCATED STACKTRACE\n")
+        out.append("--------------------------------------\n")
+        result.exceptions.add(0, new Exception(out.toString()))
+    } else {
+        result.exception.printStackTrace()
+    }
+}
+
 test {
     if (project.hasProperty('generate_golden_files_to')) {
         systemProperty 'generate_golden_files_to', project.property('generate_golden_files_to')
@@ -1459,7 +1483,7 @@
         }
         afterTest { desc, result ->
             if (result.resultType == TestResult.ResultType.FAILURE) {
-                 result.exception.printStackTrace()
+                printStackTrace(result)
             }
             if (project.hasProperty('update_test_timestamp')) {
                 file(project.getProperty('update_test_timestamp')).text = new Date().getTime()
@@ -1469,7 +1493,7 @@
     } else {
         afterTest { desc, result ->
             if (result.resultType == TestResult.ResultType.FAILURE) {
-                 result.exception.printStackTrace()
+                printStackTrace(result)
             }
         }
     }
diff --git a/tools/retrace.py b/tools/retrace.py
new file mode 100755
index 0000000..3b8da1d
--- /dev/null
+++ b/tools/retrace.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+import subprocess
+import sys
+import utils
+
+def main():
+  # Run the retrace tool with standard r8lib arguments.
+  subprocess.call(['java', '-jar', utils.RETRACE_JAR, '-verbose', utils.R8LIB_JAR + '.map'])
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/tools/test_self_retrace.py b/tools/test_self_retrace.py
index a3515b1..6e48aa6 100755
--- a/tools/test_self_retrace.py
+++ b/tools/test_self_retrace.py
@@ -12,13 +12,6 @@
 import sys
 import utils
 
-RETRACE_JAR = os.path.join(
-  utils.THIRD_PARTY,
-  'proguard',
-  'proguard6.0.1',
-  'lib',
-  'retrace.jar')
-
 EXCEPTION_LINE = 'Intentional exception for testing retrace.'
 EXPECTED_LINES = [
   'com.android.tools.r8.utils.SelfRetraceTest.foo3(SelfRetraceTest.java:13)',
@@ -55,7 +48,7 @@
   assert('SelfRetraceTest' not in stacktrace)
 
   # Run the retrace tool.
-  cmd = ['java', '-jar', RETRACE_JAR, r8lib + ".map"]
+  cmd = ['java', '-jar', utils.RETRACE_JAR, r8lib + ".map"]
   utils.PrintCmd(cmd)
   p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
   retrace_stdout, _ = p.communicate(stacktrace)
diff --git a/tools/utils.py b/tools/utils.py
index e0ffa5c..bd54dbc 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -51,6 +51,7 @@
 GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE')
 RT_JAR = os.path.join(REPO_ROOT, 'third_party/openjdk/openjdk-rt-1.8/rt.jar')
 R8LIB_KEEP_RULES = os.path.join(REPO_ROOT, 'src/main/keep.txt')
+RETRACE_JAR = os.path.join(THIRD_PARTY, 'proguard', 'proguard6.0.1', 'lib', 'retrace.jar')
 
 def PrintCmd(s):
   if type(s) is list: