From 17fe8de6b35ede03493b9af4afd2353dfbbc957f Mon Sep 17 00:00:00 2001 From: Bookatz Date: Thu, 13 Dec 2018 10:31:13 -0800 Subject: [PATCH] Allow statsd_testdrive to work in root LocalDrive needs to handle the situation whereby the caller does 'adb root' in between calls, and therefore runs everything as shell. TestDrive is a single-call, so it can just run as the caller. That's what it used to do, but a recent refactoring made some of its calls via shell, causes an error. This fixes that. Test: manual confirmation that it worked for both shell and root Change-Id: I5e31cdd59d61290a480cb6fae107170616daabc0 --- .../src/com/android/statsd/shelltools/Utils.java | 15 +++++++++++---- .../statsd/shelltools/localdrive/LocalDrive.java | 6 +++--- .../statsd/shelltools/testdrive/TestDrive.java | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/Utils.java b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/Utils.java index 597377e34ac3a..8464b8df03d03 100644 --- a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/Utils.java +++ b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/Utils.java @@ -70,9 +70,16 @@ public class Utils { /** * Dumps the report from the device and converts it to a ConfigMetricsReportList. * Erases the data if clearData is true. + * @param configId id of the config + * @param clearData whether to erase the report data from statsd after getting the report. + * @param useShellUid Pulls data for the {@link SHELL_UID} instead of the caller's uid. + * @param logger Logger to log error messages + * @return + * @throws IOException + * @throws InterruptedException */ public static ConfigMetricsReportList getReportList(long configId, boolean clearData, - Logger logger) throws IOException, InterruptedException { + boolean useShellUid, Logger logger) throws IOException, InterruptedException { try { File outputFile = File.createTempFile("statsdret", ".bin"); outputFile.deleteOnExit(); @@ -82,7 +89,7 @@ public class Utils { "adb", "shell", CMD_DUMP_REPORT, - SHELL_UID, + useShellUid ? SHELL_UID : "", String.valueOf(configId), clearData ? "" : "--keep_data", "--include_current_bucket", @@ -93,8 +100,8 @@ public class Utils { } catch (com.google.protobuf.InvalidProtocolBufferException e) { logger.severe("Failed to fetch and parse the statsd output report. " + "Perhaps there is not a valid statsd config for the requested " - + "uid=" + SHELL_UID - + ", configId=" + configId + + (useShellUid ? ("uid=" + SHELL_UID + ", ") : "") + + "configId=" + configId + "."); throw (e); } diff --git a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/localdrive/LocalDrive.java b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/localdrive/LocalDrive.java index 08074ede9d31c..67fb906c25707 100644 --- a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/localdrive/LocalDrive.java +++ b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/localdrive/LocalDrive.java @@ -165,7 +165,7 @@ public class LocalDrive { try { Utils.runCommand(null, sLogger, "adb", "shell", Utils.CMD_REMOVE_CONFIG, Utils.SHELL_UID, String.valueOf(configId)); - Utils.getReportList(configId, true /* clearData */, sLogger); + Utils.getReportList(configId, true /* clearData */, true /* SHELL_UID */, sLogger); } catch (InterruptedException | IOException e) { sLogger.severe("Failed to remove config: " + e.getMessage()); return false; @@ -234,7 +234,7 @@ public class LocalDrive { // Even if the args request no modifications, we still parse it to make sure it's valid. ConfigMetricsReportList reportList; try { - reportList = Utils.getReportList(configId, clearData, sLogger); + reportList = Utils.getReportList(configId, clearData, true /* SHELL_UID */, sLogger); } catch (IOException | InterruptedException e) { sLogger.severe("Failed to get report list: " + e.getMessage()); return false; @@ -278,7 +278,7 @@ public class LocalDrive { sLogger.fine(String.format("cmdClear with %d", configId)); try { - Utils.getReportList(configId, true /* clearData */, sLogger); + Utils.getReportList(configId, true /* clearData */, true /* SHELL_UID */, sLogger); } catch (IOException | InterruptedException e) { sLogger.severe("Failed to get report list: " + e.getMessage()); return false; diff --git a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/testdrive/TestDrive.java b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/testdrive/TestDrive.java index f7bd44aeab62c..e3fe928a13094 100644 --- a/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/testdrive/TestDrive.java +++ b/cmds/statsd/tools/localtools/src/com/android/statsd/shelltools/testdrive/TestDrive.java @@ -165,7 +165,7 @@ public class TestDrive { } private void dumpMetrics() throws Exception { - ConfigMetricsReportList reportList = Utils.getReportList(CONFIG_ID, true, logger); + ConfigMetricsReportList reportList = Utils.getReportList(CONFIG_ID, true, false, logger); // We may get multiple reports. Take the last one. ConfigMetricsReport report = reportList.getReports(reportList.getReportsCount() - 1); // Really should be only one metric.