Skip to content

Commit

Permalink
android: Convert java_deobfuscate.py to Java
Browse files Browse the repository at this point in the history
This allows it to not buffer output lines, which is essential when
piping logcat through it.

BUG=713710

Review-Url: https://codereview.chromium.org/2956153003
Cr-Commit-Position: refs/heads/master@{#484281}
  • Loading branch information
agrieve authored and Commit Bot committed Jul 5, 2017
1 parent bd29c48 commit a350dbd
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 59 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ group("gn_all") {
"//base/android/linker:chromium_android_linker",
"//build/android/gyp/test:hello_world",
"//build/android/gyp/test:hello_world",
"//build/android/stacktrace:java_deobfuscate",
"//chrome/android/webapk/shell_apk:webapk",
"//chrome/test/vr/perf:motopho_latency_test",
"//components/invalidation/impl:components_invalidation_impl_junit_tests",
Expand Down
17 changes: 17 additions & 0 deletions build/android/stacktrace/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/rules.gni")

java_binary("java_deobfuscate") {
main_class = "org.chromium.build.FlushingReTrace"
java_files = [ "java/org/chromium/build/FlushingReTrace.java" ]
deps = [
"//third_party/proguard:retrace_java",
]
data = [
"$root_build_dir/lib.java/build/android/stacktrace/java_deobfuscate.jar",
"$root_build_dir/bin/java_deobfuscate",
]
}
18 changes: 18 additions & 0 deletions build/android/stacktrace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# java_deobfuscate

A wrapper around ProGuard's ReTrace tool, which:

1) Updates the regular expression used to identify stack lines, and
2) Streams its output.

The second point here is what allows you to run:

adb logcat | out/Default/bin/java_deobfuscate

And have it actually show output without logcat terminating.


# stackwalker.py

Extracts Breakpad microdumps from a log file and uses `stackwalker` to symbolize
them.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.build;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import proguard.retrace.ReTrace;

/**
* A wrapper around ReTrace that:
* 1. Hardcodes a more useful line regular expression
* 2. Disables output buffering
*/
public class FlushingReTrace {
// This regex is based on the one from:
// http://proguard.sourceforge.net/manual/retrace/usage.html.
// But with the "at" part changed to "(?::|\bat)", to account for lines like:
// 06-22 13:58:02.895 4674 4674 E THREAD_STATE: bLA.a(PG:173)
// Normal stack trace lines look like:
// java.lang.RuntimeException: Intentional Java Crash
// at org.chromium.chrome.browser.tab.Tab.handleJavaCrash(Tab.java:682)
// at org.chromium.chrome.browser.tab.Tab.loadUrl(Tab.java:644)
private static final String LINE_PARSE_REGEX =
"(?:.*?(?::|\\bat)\\s+%c\\.%m\\s*\\(%s(?::%l)?\\)\\s*)|(?:(?:.*?[:\"]\\s+)?%c(?::.*)?)";

public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: retrace Foo.apk.map < foo.log > bar.log");
System.exit(1);
}

File mappingFile = new File(args[0]);
try {
LineNumberReader reader = new LineNumberReader(
new BufferedReader(new InputStreamReader(System.in, "UTF-8")));

// Enabling autoFlush is the main difference from ReTrace.main().
boolean autoFlush = true;
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"), autoFlush);

boolean verbose = false;
new ReTrace(LINE_PARSE_REGEX, verbose, mappingFile).retrace(reader, writer);
} catch (IOException ex) {
// Print a verbose stack trace.
ex.printStackTrace();
System.exit(1);
}

System.exit(0);
}
}
53 changes: 0 additions & 53 deletions build/android/stacktrace/java_deobfuscate.py

This file was deleted.

6 changes: 5 additions & 1 deletion build/config/android/internal_rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,11 @@ if (enable_java_templates) {
}

group(target_name) {
forward_variables_from(invoker, [ "data_deps" ])
forward_variables_from(invoker,
[
"data",
"data_deps",
])
public_deps = [
":$_ijar_target_name",
":$_process_jar_target_name",
Expand Down
14 changes: 9 additions & 5 deletions docs/android_debugging_instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,23 @@ that crashed was built. When building locally, these are found in:

```shell
out/Default/apks/ChromePublic.apk.mapping
out/Default/apks/Chrome.apk.mapping
out/Default/apks/ChromeModernPublic.apk.mapping
etc.
```

To deobfuscate a stack trace from a file, run
Build the `java_deobfuscate` tool:

```shell
build/android/stacktrace/java_deobfuscate.py PROGUARD_MAPPING_FILE.mapping --stacktrace STACKTRACE_FILE
ninja -C out/Default java_deobfuscate
```

Deobfuscation also works from `stdin`:
Then run it via:

```shell
adb logcat -d | build/android/stacktrace/java_deobfuscate.py PROGUARD_MAPPING_FILE.mapping
# For a file:
out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping < FILE
# For logcat:
adb logcat | out/Default/bin/java_deobfuscate PROGUARD_MAPPING_FILE.mapping
```

## Get WebKit code to output to the adb log
Expand Down
22 changes: 22 additions & 0 deletions third_party/proguard/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/rules.gni")

java_prebuilt("proguard_java") {
jar_path = "lib/proguard.jar"
data = [
"$root_build_dir/lib.java/third_party/proguard/proguard.jar",
]
}

java_prebuilt("retrace_java") {
jar_path = "lib/retrace.jar"
deps = [
":proguard_java",
]
data = [
"$root_build_dir/lib.java/third_party/proguard/retrace.jar",
]
}
1 change: 1 addition & 0 deletions third_party/proguard/OWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
agrieve@chromium.org
torne@chromium.org
yfriedman@chromium.org

0 comments on commit a350dbd

Please sign in to comment.