Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsService.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
| 23 | #include "config/ConfigManager.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 24 | #include "guardrail/MemoryLeakTrackUtil.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 26 | #include "storage/StorageManager.h" |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 27 | #include "subscriber/SubscriberReporter.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 32 | #include <dirent.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 33 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
| 35 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 36 | #include <utils/String16.h> |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 37 | #include <statslog.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 38 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 40 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 41 | #include <unistd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 42 | |
| 43 | using namespace android; |
| 44 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace os { |
| 47 | namespace statsd { |
| 48 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 49 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 50 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 51 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 52 | // ====================================================================== |
| 53 | /** |
| 54 | * Watches for the death of the stats companion (system process). |
| 55 | */ |
| 56 | class CompanionDeathRecipient : public IBinder::DeathRecipient { |
| 57 | public: |
| 58 | CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor); |
| 59 | virtual void binderDied(const wp<IBinder>& who); |
| 60 | |
| 61 | private: |
| 62 | const sp<AnomalyMonitor> mAnomalyMonitor; |
| 63 | }; |
| 64 | |
| 65 | CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor) |
| 66 | : mAnomalyMonitor(anomalyMonitor) { |
| 67 | } |
| 68 | |
| 69 | void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 70 | ALOGW("statscompanion service died"); |
| 71 | mAnomalyMonitor->setStatsCompanionService(nullptr); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 72 | SubscriberReporter::getInstance().setStatsCompanionService(nullptr); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // ====================================================================== |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 76 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
Bookatz | 1d0136d | 2017-12-01 11:13:32 -0800 | [diff] [blame] | 77 | : mAnomalyMonitor(new AnomalyMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS)) |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 78 | { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 79 | mUidMap = new UidMap(); |
| 80 | mConfigManager = new ConfigManager(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 81 | mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, time(nullptr), [this](const ConfigKey& key) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 82 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 83 | auto receiver = mConfigManager->GetConfigReceiver(key); |
| 84 | if (sc == nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 85 | VLOG("Could not find StatsCompanionService"); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame^] | 86 | } else if (receiver == nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 87 | VLOG("Statscompanion could not find a broadcast receiver for %s", |
| 88 | key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 89 | } else { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame^] | 90 | sc->sendDataBroadcast(receiver); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 91 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 92 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 93 | |
| 94 | mConfigManager->AddListener(mProcessor); |
| 95 | |
| 96 | init_system_properties(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 99 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 102 | void StatsService::init_system_properties() { |
| 103 | mEngBuild = false; |
| 104 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 105 | if (buildType != NULL) { |
| 106 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 107 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 110 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 111 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 112 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 113 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Implement our own because the default binder implementation isn't |
| 119 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 120 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 121 | status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 122 | uint32_t flags) { |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 123 | status_t err; |
| 124 | |
| 125 | switch (code) { |
| 126 | case SHELL_COMMAND_TRANSACTION: { |
| 127 | int in = data.readFileDescriptor(); |
| 128 | int out = data.readFileDescriptor(); |
| 129 | int err = data.readFileDescriptor(); |
| 130 | int argc = data.readInt32(); |
| 131 | Vector<String8> args; |
| 132 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 133 | args.add(String8(data.readString16())); |
| 134 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 135 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 136 | sp<IResultReceiver> resultReceiver = |
| 137 | IResultReceiver::asInterface(data.readStrongBinder()); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 138 | |
| 139 | FILE* fin = fdopen(in, "r"); |
| 140 | FILE* fout = fdopen(out, "w"); |
| 141 | FILE* ferr = fdopen(err, "w"); |
| 142 | |
| 143 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 144 | resultReceiver->send(NO_MEMORY); |
| 145 | } else { |
| 146 | err = command(fin, fout, ferr, args); |
| 147 | resultReceiver->send(err); |
| 148 | } |
| 149 | |
| 150 | if (fin != NULL) { |
| 151 | fflush(fin); |
| 152 | fclose(fin); |
| 153 | } |
| 154 | if (fout != NULL) { |
| 155 | fflush(fout); |
| 156 | fclose(fout); |
| 157 | } |
| 158 | if (fout != NULL) { |
| 159 | fflush(ferr); |
| 160 | fclose(ferr); |
| 161 | } |
| 162 | |
| 163 | return NO_ERROR; |
| 164 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 165 | default: { return BnStatsManager::onTransact(code, data, reply, flags); } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Write debugging data about statsd. |
| 171 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 172 | status_t StatsService::dump(int fd, const Vector<String16>& args) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 173 | FILE* out = fdopen(fd, "w"); |
| 174 | if (out == NULL) { |
| 175 | return NO_MEMORY; // the fd is already open |
| 176 | } |
| 177 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 178 | bool verbose = false; |
| 179 | if (args.size() > 0 && !args[0].compare(String16("-v"))) { |
| 180 | verbose = true; |
| 181 | } |
| 182 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 183 | // TODO: Proto format for incident reports |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 184 | dump_impl(out, verbose); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 185 | |
| 186 | fclose(out); |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Write debugging data about statsd in text format. |
| 192 | */ |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 193 | void StatsService::dump_impl(FILE* out, bool verbose) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 194 | StatsdStats::getInstance().dumpStats(out); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 195 | mProcessor->dumpStates(out, verbose); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Implementation of the adb shell cmd stats command. |
| 200 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 201 | status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 202 | // TODO: Permission check |
| 203 | |
| 204 | const int argCount = args.size(); |
| 205 | if (argCount >= 1) { |
| 206 | // adb shell cmd stats config ... |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 207 | if (!args[0].compare(String8("config"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 208 | return cmd_config(in, out, err, args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 209 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 210 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 211 | if (!args[0].compare(String8("print-uid-map"))) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 212 | return cmd_print_uid_map(out, args); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 213 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 214 | |
| 215 | if (!args[0].compare(String8("dump-report"))) { |
| 216 | return cmd_dump_report(out, err, args); |
| 217 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 218 | |
| 219 | if (!args[0].compare(String8("pull-source")) && args.size() > 1) { |
| 220 | return cmd_print_pulled_metrics(out, args); |
| 221 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 222 | |
| 223 | if (!args[0].compare(String8("send-broadcast"))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 224 | return cmd_trigger_broadcast(out, args); |
| 225 | } |
| 226 | |
| 227 | if (!args[0].compare(String8("print-stats"))) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 228 | return cmd_print_stats(out, args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 229 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 230 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 231 | if (!args[0].compare(String8("meminfo"))) { |
| 232 | return cmd_dump_memory_info(out); |
| 233 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 234 | |
| 235 | if (!args[0].compare(String8("write-to-disk"))) { |
| 236 | return cmd_write_data_to_disk(out); |
| 237 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 238 | |
| 239 | if (!args[0].compare(String8("log-app-hook"))) { |
| 240 | return cmd_log_app_hook(out, args); |
| 241 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 242 | |
| 243 | if (!args[0].compare(String8("clear-puller-cache"))) { |
| 244 | return cmd_clear_puller_cache(out); |
| 245 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 246 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 247 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 248 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 249 | return NO_ERROR; |
| 250 | } |
| 251 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 252 | void StatsService::print_cmd_help(FILE* out) { |
| 253 | fprintf(out, |
| 254 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 255 | "[timestamp_nsec_optional]\n"); |
| 256 | fprintf(out, "\n"); |
| 257 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 258 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 259 | fprintf(out, "\n"); |
| 260 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 261 | fprintf(out, " # adb shell stop\n"); |
| 262 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 263 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 264 | fprintf(out, " # adb shell start\n"); |
| 265 | fprintf(out, "\n"); |
| 266 | fprintf(out, "\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 267 | fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 268 | fprintf(out, "\n"); |
| 269 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 270 | fprintf(out, " PKG Optional package name to print the uids of the package\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 271 | fprintf(out, "\n"); |
| 272 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 273 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 274 | fprintf(out, "\n"); |
| 275 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 276 | fprintf(out, "\n"); |
| 277 | fprintf(out, "\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 278 | fprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 279 | fprintf(out, "\n"); |
| 280 | fprintf(out, " Flushes all data on memory to disk.\n"); |
| 281 | fprintf(out, "\n"); |
| 282 | fprintf(out, "\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 283 | fprintf(out, "usage: adb shell cmd stats log-app-hook [UID] LABEL STATE\n"); |
| 284 | fprintf(out, " Writes an AppHook event to the statslog buffer.\n"); |
| 285 | fprintf(out, " UID The uid to use. It is only possible to pass a UID\n"); |
| 286 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 287 | fprintf(out, " uid is used.\n"); |
| 288 | fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n"); |
| 289 | fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n"); |
| 290 | fprintf(out, "\n"); |
| 291 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 292 | fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 293 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 294 | fprintf(out, "\n"); |
| 295 | fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 296 | fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 297 | fprintf(out, " provided, then all configs will be removed from memory and disk.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 298 | fprintf(out, "\n"); |
| 299 | fprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 300 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 301 | fprintf(out, " uid is used.\n"); |
| 302 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 303 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 304 | fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 305 | fprintf(out, "\n be removed from memory and disk!\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 306 | fprintf(out, "\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 307 | fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 308 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 309 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 310 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 311 | fprintf(out, " calling uid is used.\n"); |
| 312 | fprintf(out, " NAME The name of the configuration\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 313 | fprintf(out, " --proto Print proto binary.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 314 | fprintf(out, "\n"); |
| 315 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 316 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 317 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 318 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 319 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 320 | fprintf(out, " calling uid is used.\n"); |
| 321 | fprintf(out, " NAME The name of the configuration\n"); |
| 322 | fprintf(out, "\n"); |
| 323 | fprintf(out, "\n"); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 324 | fprintf(out, "usage: adb shell cmd stats print-stats\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 325 | fprintf(out, " Prints some basic stats.\n"); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 326 | fprintf(out, "\n"); |
| 327 | fprintf(out, "\n"); |
| 328 | fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n"); |
| 329 | fprintf(out, " Clear cached puller data.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 330 | } |
| 331 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 332 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 333 | string name; |
| 334 | bool good = false; |
| 335 | int uid; |
| 336 | const int argCount = args.size(); |
| 337 | if (argCount == 2) { |
| 338 | // Automatically pick the UID |
| 339 | uid = IPCThreadState::self()->getCallingUid(); |
| 340 | // TODO: What if this isn't a binder call? Should we fail? |
| 341 | name.assign(args[1].c_str(), args[1].size()); |
| 342 | good = true; |
| 343 | } else if (argCount == 3) { |
| 344 | // If it's a userdebug or eng build, then the shell user can |
| 345 | // impersonate other uids. |
| 346 | if (mEngBuild) { |
| 347 | const char* s = args[1].c_str(); |
| 348 | if (*s != '\0') { |
| 349 | char* end = NULL; |
| 350 | uid = strtol(s, &end, 0); |
| 351 | if (*end == '\0') { |
| 352 | name.assign(args[2].c_str(), args[2].size()); |
| 353 | good = true; |
| 354 | } |
| 355 | } |
| 356 | } else { |
| 357 | fprintf(out, |
| 358 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 359 | "builds.\n"); |
| 360 | } |
| 361 | } |
| 362 | if (!good) { |
| 363 | print_cmd_help(out); |
| 364 | return UNKNOWN_ERROR; |
| 365 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 366 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name))); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 367 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame^] | 368 | if (sc == nullptr) { |
| 369 | VLOG("Could not access statsCompanion"); |
| 370 | } else if (receiver == nullptr) { |
| 371 | VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str()) |
| 372 | } else { |
| 373 | sc->sendDataBroadcast(receiver); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 374 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 375 | args[2].c_str()); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 376 | } |
| 377 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 378 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 382 | const int argCount = args.size(); |
| 383 | if (argCount >= 2) { |
| 384 | if (args[1] == "update" || args[1] == "remove") { |
| 385 | bool good = false; |
| 386 | int uid = -1; |
| 387 | string name; |
| 388 | |
| 389 | if (argCount == 3) { |
| 390 | // Automatically pick the UID |
| 391 | uid = IPCThreadState::self()->getCallingUid(); |
| 392 | // TODO: What if this isn't a binder call? Should we fail? |
| 393 | name.assign(args[2].c_str(), args[2].size()); |
| 394 | good = true; |
| 395 | } else if (argCount == 4) { |
| 396 | // If it's a userdebug or eng build, then the shell user can |
| 397 | // impersonate other uids. |
| 398 | if (mEngBuild) { |
| 399 | const char* s = args[2].c_str(); |
| 400 | if (*s != '\0') { |
| 401 | char* end = NULL; |
| 402 | uid = strtol(s, &end, 0); |
| 403 | if (*end == '\0') { |
| 404 | name.assign(args[3].c_str(), args[3].size()); |
| 405 | good = true; |
| 406 | } |
| 407 | } |
| 408 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 409 | fprintf(err, |
| 410 | "The config can only be set for other UIDs on eng or userdebug " |
| 411 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 412 | } |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 413 | } else if (argCount == 2 && args[1] == "remove") { |
| 414 | good = true; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (!good) { |
| 418 | // If arg parsing failed, print the help text and return an error. |
| 419 | print_cmd_help(out); |
| 420 | return UNKNOWN_ERROR; |
| 421 | } |
| 422 | |
| 423 | if (args[1] == "update") { |
| 424 | // Read stream into buffer. |
| 425 | string buffer; |
| 426 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 427 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 428 | return UNKNOWN_ERROR; |
| 429 | } |
| 430 | |
| 431 | // Parse buffer. |
| 432 | StatsdConfig config; |
| 433 | if (!config.ParseFromString(buffer)) { |
| 434 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 435 | return UNKNOWN_ERROR; |
| 436 | } |
| 437 | |
| 438 | // Add / update the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 439 | mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 440 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 441 | if (argCount == 2) { |
| 442 | cmd_remove_all_configs(out); |
| 443 | } else { |
| 444 | // Remove the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 445 | mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name))); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 446 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return NO_ERROR; |
| 450 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 451 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 452 | print_cmd_help(out); |
| 453 | return UNKNOWN_ERROR; |
| 454 | } |
| 455 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 456 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 457 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 458 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 459 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 460 | bool proto = false; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 461 | int uid; |
| 462 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 463 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 464 | proto = true; |
| 465 | argCount -= 1; |
| 466 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 467 | if (argCount == 2) { |
| 468 | // Automatically pick the UID |
| 469 | uid = IPCThreadState::self()->getCallingUid(); |
| 470 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 471 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 472 | good = true; |
| 473 | } else if (argCount == 3) { |
| 474 | // If it's a userdebug or eng build, then the shell user can |
| 475 | // impersonate other uids. |
| 476 | if (mEngBuild) { |
| 477 | const char* s = args[1].c_str(); |
| 478 | if (*s != '\0') { |
| 479 | char* end = NULL; |
| 480 | uid = strtol(s, &end, 0); |
| 481 | if (*end == '\0') { |
| 482 | name.assign(args[2].c_str(), args[2].size()); |
| 483 | good = true; |
| 484 | } |
| 485 | } |
| 486 | } else { |
| 487 | fprintf(out, |
| 488 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 489 | "builds.\n"); |
| 490 | } |
| 491 | } |
| 492 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 493 | vector<uint8_t> data; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 494 | mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 495 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 496 | if (proto) { |
| 497 | for (size_t i = 0; i < data.size(); i ++) { |
| 498 | fprintf(out, "%c", data[i]); |
| 499 | } |
| 500 | } else { |
| 501 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 502 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 503 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 504 | return android::OK; |
| 505 | } else { |
| 506 | // If arg parsing failed, print the help text and return an error. |
| 507 | print_cmd_help(out); |
| 508 | return UNKNOWN_ERROR; |
| 509 | } |
| 510 | } else { |
| 511 | fprintf(out, "Log processor does not exist...\n"); |
| 512 | return UNKNOWN_ERROR; |
| 513 | } |
| 514 | } |
| 515 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 516 | status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 517 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 518 | for (const ConfigKey& key : configs) { |
| 519 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 520 | mProcessor->GetMetricsSize(key)); |
| 521 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 522 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 523 | statsdStats.dumpStats(out); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 524 | return NO_ERROR; |
| 525 | } |
| 526 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 527 | status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) { |
| 528 | if (args.size() > 1) { |
| 529 | string pkg; |
| 530 | pkg.assign(args[1].c_str(), args[1].size()); |
| 531 | auto uids = mUidMap->getAppUid(pkg); |
| 532 | fprintf(out, "%s -> [ ", pkg.c_str()); |
| 533 | for (const auto& uid : uids) { |
| 534 | fprintf(out, "%d ", uid); |
| 535 | } |
| 536 | fprintf(out, "]\n"); |
| 537 | } else { |
| 538 | mUidMap->printUidMap(out); |
| 539 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 540 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 541 | } |
| 542 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 543 | status_t StatsService::cmd_write_data_to_disk(FILE* out) { |
| 544 | fprintf(out, "Writing data to disk\n"); |
| 545 | mProcessor->WriteDataToDisk(); |
| 546 | return NO_ERROR; |
| 547 | } |
| 548 | |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 549 | status_t StatsService::cmd_log_app_hook(FILE* out, const Vector<String8>& args) { |
| 550 | bool good = false; |
| 551 | int32_t uid; |
| 552 | int32_t label; |
| 553 | int32_t state; |
| 554 | const int argCount = args.size(); |
| 555 | if (argCount == 3) { |
| 556 | // Automatically pick the UID |
| 557 | uid = IPCThreadState::self()->getCallingUid(); |
| 558 | label = atoi(args[1].c_str()); |
| 559 | state = atoi(args[2].c_str()); |
| 560 | good = true; |
| 561 | } else if (argCount == 4) { |
| 562 | uid = atoi(args[1].c_str()); |
| 563 | // If it's a userdebug or eng build, then the shell user can impersonate other uids. |
| 564 | // Otherwise, the uid must match the actual caller's uid. |
| 565 | if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) { |
| 566 | label = atoi(args[2].c_str()); |
| 567 | state = atoi(args[3].c_str()); |
| 568 | good = true; |
| 569 | } else { |
| 570 | fprintf(out, |
| 571 | "Selecting a UID for writing AppHook can only be dumped for other UIDs on eng" |
| 572 | " or userdebug builds.\n"); |
| 573 | } |
| 574 | } |
| 575 | if (good) { |
| 576 | fprintf(out, "Logging AppHook(%d, %d, %d) to statslog.\n", uid, label, state); |
| 577 | android::util::stats_write(android::util::APP_HOOK, uid, label, state); |
| 578 | } else { |
| 579 | print_cmd_help(out); |
| 580 | return UNKNOWN_ERROR; |
| 581 | } |
| 582 | return NO_ERROR; |
| 583 | } |
| 584 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 585 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 586 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 587 | vector<shared_ptr<LogEvent> > stats; |
| 588 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 589 | for (const auto& it : stats) { |
| 590 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 591 | } |
| 592 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 593 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 594 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 595 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 596 | } |
| 597 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 598 | status_t StatsService::cmd_remove_all_configs(FILE* out) { |
| 599 | fprintf(out, "Removing all configs...\n"); |
| 600 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 601 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 602 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 603 | return NO_ERROR; |
| 604 | } |
| 605 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 606 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
| 607 | std::string s = dumpMemInfo(100); |
| 608 | fprintf(out, "Memory Info\n"); |
| 609 | fprintf(out, "%s", s.c_str()); |
| 610 | return NO_ERROR; |
| 611 | } |
| 612 | |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 613 | status_t StatsService::cmd_clear_puller_cache(FILE* out) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 614 | IPCThreadState* ipc = IPCThreadState::self(); |
| 615 | VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
| 616 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 617 | int cleared = mStatsPullerManager.ForceClearPullerCache(); |
| 618 | fprintf(out, "Puller removed %d cached data!\n", cleared); |
| 619 | return NO_ERROR; |
| 620 | } else { |
| 621 | return PERMISSION_DENIED; |
| 622 | } |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 625 | Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version, |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 626 | const vector<String16>& app) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 627 | VLOG("StatsService::informAllUidData was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 628 | |
| 629 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 630 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 631 | "Only system uid can call informAllUidData"); |
| 632 | } |
| 633 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 634 | mUidMap->updateMap(uid, version, app); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 635 | VLOG("StatsService::informAllUidData succeeded"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 636 | |
| 637 | return Status::ok(); |
| 638 | } |
| 639 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 640 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 641 | VLOG("StatsService::informOnePackage was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 642 | |
| 643 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 644 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 645 | "Only system uid can call informOnePackage"); |
| 646 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 647 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 648 | return Status::ok(); |
| 649 | } |
| 650 | |
| 651 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 652 | VLOG("StatsService::informOnePackageRemoved was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 653 | |
| 654 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 655 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 656 | "Only system uid can call informOnePackageRemoved"); |
| 657 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 658 | mUidMap->removeApp(app, uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 659 | return Status::ok(); |
| 660 | } |
| 661 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 662 | Status StatsService::informAnomalyAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 663 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 664 | |
| 665 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 666 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 667 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 668 | } |
| 669 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 670 | VLOG("StatsService::informAnomalyAlarmFired succeeded"); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 671 | uint64_t currentTimeSec = time(nullptr); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 672 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet = |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 673 | mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 674 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 675 | return Status::ok(); |
| 676 | } |
| 677 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 678 | Status StatsService::informPollAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 679 | VLOG("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 680 | |
| 681 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 682 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 683 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 686 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 687 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 688 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 689 | |
| 690 | return Status::ok(); |
| 691 | } |
| 692 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 693 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 694 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 695 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 696 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 700 | VLOG("StatsService::systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 701 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 702 | sayHiToStatsCompanion(); |
| 703 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 704 | return Status::ok(); |
| 705 | } |
| 706 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 707 | Status StatsService::writeDataToDisk() { |
| 708 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 709 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 710 | "Only system uid can call systemRunning"); |
| 711 | } |
| 712 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 713 | VLOG("StatsService::writeDataToDisk"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 714 | |
| 715 | mProcessor->WriteDataToDisk(); |
| 716 | |
| 717 | return Status::ok(); |
| 718 | } |
| 719 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 720 | void StatsService::sayHiToStatsCompanion() { |
| 721 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 722 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 723 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 724 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 725 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 726 | statsCompanion->statsdReady(); |
| 727 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 728 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 732 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 733 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 734 | // Get statscompanion service from service manager |
| 735 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 736 | if (sm != nullptr) { |
| 737 | const String16 name("statscompanion"); |
| 738 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 739 | if (statsCompanion == nullptr) { |
| 740 | ALOGW("statscompanion service unavailable!"); |
| 741 | return nullptr; |
| 742 | } |
| 743 | } |
| 744 | return statsCompanion; |
| 745 | } |
| 746 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 747 | Status StatsService::statsCompanionReady() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 748 | VLOG("StatsService::statsCompanionReady was called"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 749 | |
| 750 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 751 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 752 | "Only system uid can call statsCompanionReady"); |
| 753 | } |
| 754 | |
| 755 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 756 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 757 | return Status::fromExceptionCode( |
| 758 | Status::EX_NULL_POINTER, |
| 759 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 760 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 761 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 762 | IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor)); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 763 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 764 | SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 765 | |
| 766 | return Status::ok(); |
| 767 | } |
| 768 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 769 | void StatsService::Startup() { |
| 770 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 773 | void StatsService::OnLogEvent(LogEvent* event) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 774 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 777 | Status StatsService::getData(int64_t key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 778 | IPCThreadState* ipc = IPCThreadState::self(); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 779 | VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 780 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 781 | ConfigKey configKey(ipc->getCallingUid(), key); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 782 | mProcessor->onDumpReport(configKey, output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 783 | return Status::ok(); |
| 784 | } else { |
| 785 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 786 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 787 | } |
| 788 | |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 789 | Status StatsService::getMetadata(vector<uint8_t>* output) { |
| 790 | IPCThreadState* ipc = IPCThreadState::self(); |
| 791 | VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(), |
| 792 | ipc->getCallingUid()); |
| 793 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 794 | StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters. |
| 795 | return Status::ok(); |
| 796 | } else { |
| 797 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 798 | } |
| 799 | } |
| 800 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 801 | Status StatsService::addConfiguration(int64_t key, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 802 | const vector <uint8_t>& config, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 803 | bool* success) { |
| 804 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 805 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 806 | ConfigKey configKey(ipc->getCallingUid(), key); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 807 | StatsdConfig cfg; |
Yangster-mac | bbd056a | 2018-01-22 13:37:02 -0800 | [diff] [blame] | 808 | if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) { |
David Chen | 7d8aa4d | 2017-12-27 13:37:01 -0800 | [diff] [blame] | 809 | *success = false; |
| 810 | return Status::ok(); |
| 811 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 812 | mConfigManager->UpdateConfig(configKey, cfg); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame^] | 813 | *success = true; |
| 814 | return Status::ok(); |
| 815 | } else { |
| 816 | *success = false; |
| 817 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | Status StatsService::removeDataFetchOperation(int64_t key, bool* success) { |
| 822 | IPCThreadState* ipc = IPCThreadState::self(); |
| 823 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 824 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 825 | mConfigManager->RemoveConfigReceiver(configKey); |
| 826 | *success = true; |
| 827 | return Status::ok(); |
| 828 | } else { |
| 829 | *success = false; |
| 830 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender, |
| 835 | bool* success) { |
| 836 | IPCThreadState* ipc = IPCThreadState::self(); |
| 837 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 838 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 839 | mConfigManager->SetConfigReceiver(configKey, intentSender); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 840 | *success = true; |
| 841 | return Status::ok(); |
| 842 | } else { |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 843 | *success = false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 844 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 845 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 848 | Status StatsService::removeConfiguration(int64_t key, bool* success) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 849 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 850 | if (checkCallingPermission(String16(kPermissionDump))) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 851 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 852 | mConfigManager->RemoveConfig(configKey); |
| 853 | SubscriberReporter::getInstance().removeConfig(configKey); |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 854 | *success = true; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 855 | return Status::ok(); |
| 856 | } else { |
| 857 | *success = false; |
| 858 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 859 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 862 | Status StatsService::setBroadcastSubscriber(int64_t configId, |
| 863 | int64_t subscriberId, |
| 864 | const sp<android::IBinder>& intentSender, |
| 865 | bool* success) { |
| 866 | VLOG("StatsService::setBroadcastSubscriber called."); |
| 867 | IPCThreadState* ipc = IPCThreadState::self(); |
| 868 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 869 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 870 | SubscriberReporter::getInstance() |
| 871 | .setBroadcastSubscriber(configKey, subscriberId, intentSender); |
| 872 | *success = true; |
| 873 | return Status::ok(); |
| 874 | } else { |
| 875 | *success = false; |
| 876 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | Status StatsService::unsetBroadcastSubscriber(int64_t configId, |
| 881 | int64_t subscriberId, |
| 882 | bool* success) { |
| 883 | VLOG("StatsService::unsetBroadcastSubscriber called."); |
| 884 | IPCThreadState* ipc = IPCThreadState::self(); |
| 885 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 886 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 887 | SubscriberReporter::getInstance() |
| 888 | .unsetBroadcastSubscriber(configKey, subscriberId); |
| 889 | *success = true; |
| 890 | return Status::ok(); |
| 891 | } else { |
| 892 | *success = false; |
| 893 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 898 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 901 | } // namespace statsd |
| 902 | } // namespace os |
| 903 | } // namespace android |