blob: 7eca5aa212016986eed376528fb28011d0dc5b9f [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07003 *
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
yro74fed972017-11-27 14:42:42 -080017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
20#include "StatsService.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080021#include "android-base/stringprintf.h"
David Chenadaf8b32017-11-03 15:42:08 -070022#include "config/ConfigKey.h"
23#include "config/ConfigManager.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080024#include "guardrail/MemoryLeakTrackUtil.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "guardrail/StatsdStats.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include "storage/DropboxReader.h"
yro947fbce2017-11-15 22:50:23 -080027#include "storage/StorageManager.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028
David Chen0656b7a2017-09-13 15:53:39 -070029#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
yro87d983c2017-11-14 21:31:43 -080032#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070033#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034#include <private/android_filesystem_config.h>
35#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070036#include <utils/String16.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070037#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070038#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070039#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070040#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070041
42using namespace android;
43
Bookatz906a35c2017-09-20 15:26:44 -070044namespace android {
45namespace os {
46namespace statsd {
47
David Chenadaf8b32017-11-03 15:42:08 -070048constexpr const char* kPermissionDump = "android.permission.DUMP";
yro87d983c2017-11-14 21:31:43 -080049#define STATS_SERVICE_DIR "/data/system/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070050
Joe Onorato9fc9edf2017-10-15 20:08:52 -070051// ======================================================================
52/**
53 * Watches for the death of the stats companion (system process).
54 */
55class CompanionDeathRecipient : public IBinder::DeathRecipient {
56public:
57 CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor);
58 virtual void binderDied(const wp<IBinder>& who);
59
60private:
61 const sp<AnomalyMonitor> mAnomalyMonitor;
62};
63
64CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor)
65 : mAnomalyMonitor(anomalyMonitor) {
66}
67
68void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
69 ALOGW("statscompanion service died");
70 mAnomalyMonitor->setStatsCompanionService(nullptr);
71}
72
73// ======================================================================
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070074StatsService::StatsService(const sp<Looper>& handlerLooper)
David Chen1481fe12017-10-16 13:16:34 -070075 : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Put this comment somewhere better
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070076{
Joe Onorato9fc9edf2017-10-15 20:08:52 -070077 mUidMap = new UidMap();
78 mConfigManager = new ConfigManager();
Yangster-mace2cd6d52017-11-09 20:38:30 -080079 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, [this](const ConfigKey& key) {
yro947fbce2017-11-15 22:50:23 -080080 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen1d7b0cd2017-11-15 14:20:04 -080081 auto receiver = mConfigManager->GetConfigReceiver(key);
82 if (sc == nullptr) {
yro74fed972017-11-27 14:42:42 -080083 VLOG("Could not find StatsCompanionService");
David Chen1d7b0cd2017-11-15 14:20:04 -080084 } else if (receiver.first.size() == 0) {
yro74fed972017-11-27 14:42:42 -080085 VLOG("Statscompanion could not find a broadcast receiver for %s",
86 key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -080087 } else {
88 sc->sendBroadcast(String16(receiver.first.c_str()),
89 String16(receiver.second.c_str()));
90 }
yro31eb67b2017-10-24 13:33:21 -070091 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070092
93 mConfigManager->AddListener(mProcessor);
94
95 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070096}
97
Yao Chenef99c4f2017-09-22 16:26:54 -070098StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070099}
100
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700101void StatsService::init_system_properties() {
102 mEngBuild = false;
103 const prop_info* buildType = __system_property_find("ro.build.type");
104 if (buildType != NULL) {
105 __system_property_read_callback(buildType, init_build_type_callback, this);
106 }
David Chen0656b7a2017-09-13 15:53:39 -0700107}
108
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700109void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
110 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700111 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700112 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
113 }
114}
115
116/**
117 * Implement our own because the default binder implementation isn't
118 * properly handling SHELL_COMMAND_TRANSACTION.
119 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700120status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
121 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700122 status_t err;
123
124 switch (code) {
125 case SHELL_COMMAND_TRANSACTION: {
126 int in = data.readFileDescriptor();
127 int out = data.readFileDescriptor();
128 int err = data.readFileDescriptor();
129 int argc = data.readInt32();
130 Vector<String8> args;
131 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
132 args.add(String8(data.readString16()));
133 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700134 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
135 sp<IResultReceiver> resultReceiver =
136 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700137
138 FILE* fin = fdopen(in, "r");
139 FILE* fout = fdopen(out, "w");
140 FILE* ferr = fdopen(err, "w");
141
142 if (fin == NULL || fout == NULL || ferr == NULL) {
143 resultReceiver->send(NO_MEMORY);
144 } else {
145 err = command(fin, fout, ferr, args);
146 resultReceiver->send(err);
147 }
148
149 if (fin != NULL) {
150 fflush(fin);
151 fclose(fin);
152 }
153 if (fout != NULL) {
154 fflush(fout);
155 fclose(fout);
156 }
157 if (fout != NULL) {
158 fflush(ferr);
159 fclose(ferr);
160 }
161
162 return NO_ERROR;
163 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700164 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700165 }
166}
167
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700168/**
169 * Write debugging data about statsd.
170 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700171status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700172 FILE* out = fdopen(fd, "w");
173 if (out == NULL) {
174 return NO_MEMORY; // the fd is already open
175 }
176
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700177 // TODO: Proto format for incident reports
178 dump_impl(out);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700179
180 fclose(out);
181 return NO_ERROR;
182}
183
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700184/**
185 * Write debugging data about statsd in text format.
186 */
187void StatsService::dump_impl(FILE* out) {
188 mConfigManager->Dump(out);
189}
190
191/**
192 * Implementation of the adb shell cmd stats command.
193 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700194status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700195 // TODO: Permission check
196
197 const int argCount = args.size();
198 if (argCount >= 1) {
199 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700200 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700202 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700203
204 // adb shell cmd stats print-stats-log
205 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
206 return cmd_print_stats_log(out, args);
207 }
208
David Chende701692017-10-05 13:16:02 -0700209 if (!args[0].compare(String8("print-uid-map"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700210 return cmd_print_uid_map(out);
David Chende701692017-10-05 13:16:02 -0700211 }
Yao Chen729093d2017-10-16 10:33:26 -0700212
213 if (!args[0].compare(String8("dump-report"))) {
214 return cmd_dump_report(out, err, args);
215 }
David Chen1481fe12017-10-16 13:16:34 -0700216
217 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
218 return cmd_print_pulled_metrics(out, args);
219 }
David Chenadaf8b32017-11-03 15:42:08 -0700220
221 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800222 return cmd_trigger_broadcast(out, args);
223 }
224
225 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800226 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700227 }
yro87d983c2017-11-14 21:31:43 -0800228
Yao Chen8d9989b2017-11-18 18:54:50 -0800229 if (!args[0].compare(String8("meminfo"))) {
230 return cmd_dump_memory_info(out);
231 }
yro947fbce2017-11-15 22:50:23 -0800232
233 if (!args[0].compare(String8("write-to-disk"))) {
234 return cmd_write_data_to_disk(out);
235 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700236 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700237
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700238 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700239 return NO_ERROR;
240}
241
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700242void StatsService::print_cmd_help(FILE* out) {
243 fprintf(out,
244 "usage: adb shell cmd stats print-stats-log [tag_required] "
245 "[timestamp_nsec_optional]\n");
246 fprintf(out, "\n");
247 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800248 fprintf(out, "usage: adb shell cmd stats meminfo\n");
249 fprintf(out, "\n");
250 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
251 fprintf(out, " # adb shell stop\n");
252 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
253 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
254 fprintf(out, " # adb shell start\n");
255 fprintf(out, "\n");
256 fprintf(out, "\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700257 fprintf(out, "usage: adb shell cmd stats print-uid-map \n");
258 fprintf(out, "\n");
259 fprintf(out, " Prints the UID, app name, version mapping.\n");
260 fprintf(out, "\n");
261 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800262 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700263 fprintf(out, "\n");
264 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
265 fprintf(out, "\n");
266 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800267 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
268 fprintf(out, "\n");
269 fprintf(out, " Flushes all data on memory to disk.\n");
270 fprintf(out, "\n");
271 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800272 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700273 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
274 fprintf(out, "\n");
275 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800276 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
277 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700278 fprintf(out, "\n");
279 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800280 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700281 fprintf(out, " uid is used.\n");
282 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a372017-10-30 22:57:06 -0700283 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800284 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
285 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a372017-10-30 22:57:06 -0700286 fprintf(out, "\n");
287 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n");
288 fprintf(out, " Dump all metric data for a configuration.\n");
289 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
290 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
291 fprintf(out, " calling uid is used.\n");
292 fprintf(out, " NAME The name of the configuration\n");
David Chenadaf8b32017-11-03 15:42:08 -0700293 fprintf(out, "\n");
294 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800295 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
296 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
297 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
298 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
299 fprintf(out, " calling uid is used.\n");
300 fprintf(out, " NAME The name of the configuration\n");
301 fprintf(out, "\n");
302 fprintf(out, "\n");
Yao Chenb3561512017-11-21 18:07:17 -0800303 fprintf(out, "usage: adb shell cmd stats print-stats [reset]\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800304 fprintf(out, " Prints some basic stats.\n");
Yao Chenb3561512017-11-21 18:07:17 -0800305 fprintf(out, " reset: 1 to reset the statsd stats. default=0.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700306}
307
David Chen1d7b0cd2017-11-15 14:20:04 -0800308status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
309 string name;
310 bool good = false;
311 int uid;
312 const int argCount = args.size();
313 if (argCount == 2) {
314 // Automatically pick the UID
315 uid = IPCThreadState::self()->getCallingUid();
316 // TODO: What if this isn't a binder call? Should we fail?
317 name.assign(args[1].c_str(), args[1].size());
318 good = true;
319 } else if (argCount == 3) {
320 // If it's a userdebug or eng build, then the shell user can
321 // impersonate other uids.
322 if (mEngBuild) {
323 const char* s = args[1].c_str();
324 if (*s != '\0') {
325 char* end = NULL;
326 uid = strtol(s, &end, 0);
327 if (*end == '\0') {
328 name.assign(args[2].c_str(), args[2].size());
329 good = true;
330 }
331 }
332 } else {
333 fprintf(out,
334 "The metrics can only be dumped for other UIDs on eng or userdebug "
335 "builds.\n");
336 }
337 }
338 if (!good) {
339 print_cmd_help(out);
340 return UNKNOWN_ERROR;
341 }
342 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name));
yro4d889e62017-11-17 15:44:48 -0800343 sp<IStatsCompanionService> sc = getStatsCompanionService();
344 if (sc != nullptr) {
345 sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str()));
yro74fed972017-11-27 14:42:42 -0800346 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
347 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800348 } else {
yro74fed972017-11-27 14:42:42 -0800349 VLOG("Could not access statsCompanion");
yro4d889e62017-11-17 15:44:48 -0800350 }
351
David Chenadaf8b32017-11-03 15:42:08 -0700352 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700353}
354
355status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
356 const int argCount = args.size();
357 if (argCount >= 2) {
358 if (args[1] == "update" || args[1] == "remove") {
359 bool good = false;
360 int uid = -1;
361 string name;
362
363 if (argCount == 3) {
364 // Automatically pick the UID
365 uid = IPCThreadState::self()->getCallingUid();
366 // TODO: What if this isn't a binder call? Should we fail?
367 name.assign(args[2].c_str(), args[2].size());
368 good = true;
369 } else if (argCount == 4) {
370 // If it's a userdebug or eng build, then the shell user can
371 // impersonate other uids.
372 if (mEngBuild) {
373 const char* s = args[2].c_str();
374 if (*s != '\0') {
375 char* end = NULL;
376 uid = strtol(s, &end, 0);
377 if (*end == '\0') {
378 name.assign(args[3].c_str(), args[3].size());
379 good = true;
380 }
381 }
382 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700383 fprintf(err,
384 "The config can only be set for other UIDs on eng or userdebug "
385 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700386 }
387 }
388
389 if (!good) {
390 // If arg parsing failed, print the help text and return an error.
391 print_cmd_help(out);
392 return UNKNOWN_ERROR;
393 }
394
395 if (args[1] == "update") {
396 // Read stream into buffer.
397 string buffer;
398 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
399 fprintf(err, "Error reading stream for StatsConfig.\n");
400 return UNKNOWN_ERROR;
401 }
402
403 // Parse buffer.
404 StatsdConfig config;
405 if (!config.ParseFromString(buffer)) {
406 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
407 return UNKNOWN_ERROR;
408 }
409
410 // Add / update the config.
411 mConfigManager->UpdateConfig(ConfigKey(uid, name), config);
412 } else {
yro74fed972017-11-27 14:42:42 -0800413 if (argCount == 2) {
414 cmd_remove_all_configs(out);
415 } else {
416 // Remove the config.
417 mConfigManager->RemoveConfig(ConfigKey(uid, name));
418 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700419 }
420
421 return NO_ERROR;
422 }
David Chen0656b7a2017-09-13 15:53:39 -0700423 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700424 print_cmd_help(out);
425 return UNKNOWN_ERROR;
426}
427
Yao Chen729093d2017-10-16 10:33:26 -0700428status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
429 if (mProcessor != nullptr) {
430 const int argCount = args.size();
431 bool good = false;
432 int uid;
433 string name;
434 if (argCount == 2) {
435 // Automatically pick the UID
436 uid = IPCThreadState::self()->getCallingUid();
437 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a372017-10-30 22:57:06 -0700438 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700439 good = true;
440 } else if (argCount == 3) {
441 // If it's a userdebug or eng build, then the shell user can
442 // impersonate other uids.
443 if (mEngBuild) {
444 const char* s = args[1].c_str();
445 if (*s != '\0') {
446 char* end = NULL;
447 uid = strtol(s, &end, 0);
448 if (*end == '\0') {
449 name.assign(args[2].c_str(), args[2].size());
450 good = true;
451 }
452 }
453 } else {
454 fprintf(out,
455 "The metrics can only be dumped for other UIDs on eng or userdebug "
456 "builds.\n");
457 }
458 }
459 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800460 vector<uint8_t> data;
461 mProcessor->onDumpReport(ConfigKey(uid, name), &data);
Yao Chen729093d2017-10-16 10:33:26 -0700462 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
463 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
464 fprintf(out, "See the StatsLogReport in logcat...\n");
465 return android::OK;
466 } else {
467 // If arg parsing failed, print the help text and return an error.
468 print_cmd_help(out);
469 return UNKNOWN_ERROR;
470 }
471 } else {
472 fprintf(out, "Log processor does not exist...\n");
473 return UNKNOWN_ERROR;
474 }
475}
476
Yao Chenb3561512017-11-21 18:07:17 -0800477status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800478 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
479 for (const ConfigKey& key : configs) {
480 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
481 mProcessor->GetMetricsSize(key));
482 }
David Chenc136f452017-11-27 11:52:26 -0800483 fprintf(out, "Detailed statsd stats in logcat...\n");
Yao Chenb3561512017-11-21 18:07:17 -0800484 StatsdStats& statsdStats = StatsdStats::getInstance();
485 bool reset = false;
486 if (args.size() > 1) {
487 reset = strtol(args[1].string(), NULL, 10);
488 }
Yao Chen69f1baf2017-11-27 17:25:36 -0800489 vector<uint8_t> output;
Yao Chenb3561512017-11-21 18:07:17 -0800490 statsdStats.dumpStats(&output, reset);
David Chen1d7b0cd2017-11-15 14:20:04 -0800491 return NO_ERROR;
492}
493
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700494status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
495 long msec = 0;
496
497 if (args.size() > 2) {
498 msec = strtol(args[2].string(), NULL, 10);
David Chen0656b7a2017-09-13 15:53:39 -0700499 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700500 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
501}
502
503status_t StatsService::cmd_print_uid_map(FILE* out) {
504 mUidMap->printUidMap(out);
505 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700506}
507
yro947fbce2017-11-15 22:50:23 -0800508status_t StatsService::cmd_write_data_to_disk(FILE* out) {
509 fprintf(out, "Writing data to disk\n");
510 mProcessor->WriteDataToDisk();
511 return NO_ERROR;
512}
513
David Chen1481fe12017-10-16 13:16:34 -0700514status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
515 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700516 vector<shared_ptr<LogEvent> > stats;
517 if (mStatsPullerManager.Pull(s, &stats)) {
518 for (const auto& it : stats) {
519 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
520 }
521 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
522 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700523 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700524 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700525}
526
yro74fed972017-11-27 14:42:42 -0800527status_t StatsService::cmd_remove_all_configs(FILE* out) {
528 fprintf(out, "Removing all configs...\n");
529 VLOG("StatsService::cmd_remove_all_configs was called");
530 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800531 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800532 return NO_ERROR;
533}
534
Yao Chen8d9989b2017-11-18 18:54:50 -0800535status_t StatsService::cmd_dump_memory_info(FILE* out) {
536 std::string s = dumpMemInfo(100);
537 fprintf(out, "Memory Info\n");
538 fprintf(out, "%s", s.c_str());
539 return NO_ERROR;
540}
541
David Chende701692017-10-05 13:16:02 -0700542Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
543 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800544 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700545
546 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
547 return Status::fromExceptionCode(Status::EX_SECURITY,
548 "Only system uid can call informAllUidData");
549 }
550
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700551 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800552 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700553
554 return Status::ok();
555}
556
557Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
yro74fed972017-11-27 14:42:42 -0800558 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700559
560 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
561 return Status::fromExceptionCode(Status::EX_SECURITY,
562 "Only system uid can call informOnePackage");
563 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700564 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700565 return Status::ok();
566}
567
568Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800569 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700570
571 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
572 return Status::fromExceptionCode(Status::EX_SECURITY,
573 "Only system uid can call informOnePackageRemoved");
574 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700575 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700576 return Status::ok();
577}
578
Yao Chenef99c4f2017-09-22 16:26:54 -0700579Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800580 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700581
582 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
583 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700584 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700585 }
586
yro74fed972017-11-27 14:42:42 -0800587 VLOG("StatsService::informAnomalyAlarmFired succeeded");
Bookatzcc5adef2017-11-21 14:36:23 -0800588 uint64_t currentTimeSec = time(nullptr);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800589 std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
Bookatzcc5adef2017-11-21 14:36:23 -0800590 mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
591 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet);
Bookatz1b0b1142017-09-08 11:58:42 -0700592 return Status::ok();
593}
594
Yao Chenef99c4f2017-09-22 16:26:54 -0700595Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800596 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700597
598 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
599 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700600 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700601 }
602
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700603 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700604
yro74fed972017-11-27 14:42:42 -0800605 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700606
607 return Status::ok();
608}
609
Yao Chenef99c4f2017-09-22 16:26:54 -0700610Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700611 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
612 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700613 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700614 }
615
616 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800617 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700618
Bookatzb487b552017-09-18 11:26:01 -0700619 sayHiToStatsCompanion();
620
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700621 return Status::ok();
622}
623
yro947fbce2017-11-15 22:50:23 -0800624Status StatsService::writeDataToDisk() {
625 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
626 return Status::fromExceptionCode(Status::EX_SECURITY,
627 "Only system uid can call systemRunning");
628 }
629
yro74fed972017-11-27 14:42:42 -0800630 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800631
632 mProcessor->WriteDataToDisk();
633
634 return Status::ok();
635}
636
Yao Chenef99c4f2017-09-22 16:26:54 -0700637void StatsService::sayHiToStatsCompanion() {
638 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
639 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700640 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
641 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800642 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700643 statsCompanion->statsdReady();
644 } else {
yro74fed972017-11-27 14:42:42 -0800645 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700646 }
647}
648
Yao Chenef99c4f2017-09-22 16:26:54 -0700649sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700650 sp<IStatsCompanionService> statsCompanion = nullptr;
651 // Get statscompanion service from service manager
652 const sp<IServiceManager> sm(defaultServiceManager());
653 if (sm != nullptr) {
654 const String16 name("statscompanion");
655 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
656 if (statsCompanion == nullptr) {
657 ALOGW("statscompanion service unavailable!");
658 return nullptr;
659 }
660 }
661 return statsCompanion;
662}
663
Yao Chenef99c4f2017-09-22 16:26:54 -0700664Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800665 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700666
667 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
668 return Status::fromExceptionCode(Status::EX_SECURITY,
669 "Only system uid can call statsCompanionReady");
670 }
671
672 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
673 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700674 return Status::fromExceptionCode(
675 Status::EX_NULL_POINTER,
676 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700677 }
yro74fed972017-11-27 14:42:42 -0800678 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700679 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700680 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
681
682 return Status::ok();
683}
684
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700685void StatsService::Startup() {
686 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700687}
688
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700689void StatsService::OnLogEvent(const LogEvent& event) {
690 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700691}
692
Yangster7c334a12017-11-22 14:24:24 -0800693Status StatsService::getData(const String16& key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700694 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800695 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800696 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800697 string keyStr = string(String8(key).string());
698 ConfigKey configKey(ipc->getCallingUid(), keyStr);
699 mProcessor->onDumpReport(configKey, output);
David Chenadaf8b32017-11-03 15:42:08 -0700700 return Status::ok();
701 } else {
702 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
703 }
yro31eb67b2017-10-24 13:33:21 -0700704}
705
David Chenadaf8b32017-11-03 15:42:08 -0700706Status StatsService::addConfiguration(const String16& key,
707 const vector <uint8_t>& config,
708 const String16& package, const String16& cls,
709 bool* success) {
710 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800711 if (checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700712 string keyString = string(String8(key).string());
David Chen1d7b0cd2017-11-15 14:20:04 -0800713 ConfigKey configKey(ipc->getCallingUid(), keyString);
David Chenadaf8b32017-11-03 15:42:08 -0700714 StatsdConfig cfg;
715 cfg.ParseFromArray(&config[0], config.size());
716 mConfigManager->UpdateConfig(configKey, cfg);
717 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
718 string(String8(cls).string()));
719 *success = true;
720 return Status::ok();
721 } else {
722 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700723 }
yro31eb67b2017-10-24 13:33:21 -0700724}
725
David Chenadaf8b32017-11-03 15:42:08 -0700726Status StatsService::removeConfiguration(const String16& key, bool* success) {
727 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800728 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800729 string keyStr = string(String8(key).string());
730 mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr));
David Chenadaf8b32017-11-03 15:42:08 -0700731 return Status::ok();
732 } else {
733 *success = false;
734 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700735 }
yro31eb67b2017-10-24 13:33:21 -0700736}
737
David Chen1d7b0cd2017-11-15 14:20:04 -0800738void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700739}
740
Yao Chenef99c4f2017-09-22 16:26:54 -0700741} // namespace statsd
742} // namespace os
743} // namespace android