blob: ec2650e72c0886616105f245165c4dc6859ae5b0 [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
Bookatzb487b552017-09-18 11:26:01 -070017#define DEBUG 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"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include "storage/DropboxReader.h"
yro947fbce2017-11-15 22:50:23 -080026#include "storage/StorageManager.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070027
David Chen0656b7a2017-09-13 15:53:39 -070028#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
yro87d983c2017-11-14 21:31:43 -080031#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070032#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070033#include <private/android_filesystem_config.h>
34#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070035#include <utils/String16.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070036#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070037#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070038#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070039#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070040
41using namespace android;
42
Bookatz906a35c2017-09-20 15:26:44 -070043namespace android {
44namespace os {
45namespace statsd {
46
David Chenadaf8b32017-11-03 15:42:08 -070047constexpr const char* kPermissionDump = "android.permission.DUMP";
yro87d983c2017-11-14 21:31:43 -080048#define STATS_SERVICE_DIR "/data/system/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070049
Joe Onorato9fc9edf2017-10-15 20:08:52 -070050// ======================================================================
51/**
52 * Watches for the death of the stats companion (system process).
53 */
54class CompanionDeathRecipient : public IBinder::DeathRecipient {
55public:
56 CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor);
57 virtual void binderDied(const wp<IBinder>& who);
58
59private:
60 const sp<AnomalyMonitor> mAnomalyMonitor;
61};
62
63CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor)
64 : mAnomalyMonitor(anomalyMonitor) {
65}
66
67void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
68 ALOGW("statscompanion service died");
69 mAnomalyMonitor->setStatsCompanionService(nullptr);
70}
71
72// ======================================================================
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070073StatsService::StatsService(const sp<Looper>& handlerLooper)
David Chen1481fe12017-10-16 13:16:34 -070074 : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Put this comment somewhere better
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070075{
Joe Onorato9fc9edf2017-10-15 20:08:52 -070076 mUidMap = new UidMap();
77 mConfigManager = new ConfigManager();
Yangster-mace2cd6d52017-11-09 20:38:30 -080078 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, [this](const ConfigKey& key) {
yro947fbce2017-11-15 22:50:23 -080079 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen1d7b0cd2017-11-15 14:20:04 -080080 auto receiver = mConfigManager->GetConfigReceiver(key);
81 if (sc == nullptr) {
82 ALOGD("Could not find StatsCompanionService");
83 } else if (receiver.first.size() == 0) {
84 ALOGD("Statscompanion could not find a broadcast receiver for %s",
85 key.ToString().c_str());
86 } else {
87 sc->sendBroadcast(String16(receiver.first.c_str()),
88 String16(receiver.second.c_str()));
89 }
yro31eb67b2017-10-24 13:33:21 -070090 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070091
92 mConfigManager->AddListener(mProcessor);
93
94 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070095}
96
Yao Chenef99c4f2017-09-22 16:26:54 -070097StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070098}
99
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700100void StatsService::init_system_properties() {
101 mEngBuild = false;
102 const prop_info* buildType = __system_property_find("ro.build.type");
103 if (buildType != NULL) {
104 __system_property_read_callback(buildType, init_build_type_callback, this);
105 }
David Chen0656b7a2017-09-13 15:53:39 -0700106}
107
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700108void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
109 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700110 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700111 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
112 }
113}
114
115/**
116 * Implement our own because the default binder implementation isn't
117 * properly handling SHELL_COMMAND_TRANSACTION.
118 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700119status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
120 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700121 status_t err;
122
123 switch (code) {
124 case SHELL_COMMAND_TRANSACTION: {
125 int in = data.readFileDescriptor();
126 int out = data.readFileDescriptor();
127 int err = data.readFileDescriptor();
128 int argc = data.readInt32();
129 Vector<String8> args;
130 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
131 args.add(String8(data.readString16()));
132 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700133 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
134 sp<IResultReceiver> resultReceiver =
135 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700136
137 FILE* fin = fdopen(in, "r");
138 FILE* fout = fdopen(out, "w");
139 FILE* ferr = fdopen(err, "w");
140
141 if (fin == NULL || fout == NULL || ferr == NULL) {
142 resultReceiver->send(NO_MEMORY);
143 } else {
144 err = command(fin, fout, ferr, args);
145 resultReceiver->send(err);
146 }
147
148 if (fin != NULL) {
149 fflush(fin);
150 fclose(fin);
151 }
152 if (fout != NULL) {
153 fflush(fout);
154 fclose(fout);
155 }
156 if (fout != NULL) {
157 fflush(ferr);
158 fclose(ferr);
159 }
160
161 return NO_ERROR;
162 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700163 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700164 }
165}
166
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700167/**
168 * Write debugging data about statsd.
169 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700170status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700171 FILE* out = fdopen(fd, "w");
172 if (out == NULL) {
173 return NO_MEMORY; // the fd is already open
174 }
175
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700176 // TODO: Proto format for incident reports
177 dump_impl(out);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700178
179 fclose(out);
180 return NO_ERROR;
181}
182
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700183/**
184 * Write debugging data about statsd in text format.
185 */
186void StatsService::dump_impl(FILE* out) {
187 mConfigManager->Dump(out);
188}
189
190/**
191 * Implementation of the adb shell cmd stats command.
192 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700193status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194 // TODO: Permission check
195
196 const int argCount = args.size();
197 if (argCount >= 1) {
198 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700199 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700200 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700201 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700202
203 // adb shell cmd stats print-stats-log
204 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
205 return cmd_print_stats_log(out, args);
206 }
207
David Chende701692017-10-05 13:16:02 -0700208 if (!args[0].compare(String8("print-uid-map"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700209 return cmd_print_uid_map(out);
David Chende701692017-10-05 13:16:02 -0700210 }
Yao Chen729093d2017-10-16 10:33:26 -0700211
212 if (!args[0].compare(String8("dump-report"))) {
213 return cmd_dump_report(out, err, args);
214 }
David Chen1481fe12017-10-16 13:16:34 -0700215
216 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
217 return cmd_print_pulled_metrics(out, args);
218 }
David Chenadaf8b32017-11-03 15:42:08 -0700219
220 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800221 return cmd_trigger_broadcast(out, args);
222 }
223
224 if (!args[0].compare(String8("print-stats"))) {
225 return cmd_print_stats(out);
David Chenadaf8b32017-11-03 15:42:08 -0700226 }
yro87d983c2017-11-14 21:31:43 -0800227
228 if (!args[0].compare(String8("clear-config"))) {
229 return cmd_remove_config_files(out);
230 }
Yao Chen8d9989b2017-11-18 18:54:50 -0800231
232 if (!args[0].compare(String8("meminfo"))) {
233 return cmd_dump_memory_info(out);
234 }
yro947fbce2017-11-15 22:50:23 -0800235
236 if (!args[0].compare(String8("write-to-disk"))) {
237 return cmd_write_data_to_disk(out);
238 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700239 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700240
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700241 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700242 return NO_ERROR;
243}
244
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700245void StatsService::print_cmd_help(FILE* out) {
246 fprintf(out,
247 "usage: adb shell cmd stats print-stats-log [tag_required] "
248 "[timestamp_nsec_optional]\n");
249 fprintf(out, "\n");
250 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800251 fprintf(out, "usage: adb shell cmd stats meminfo\n");
252 fprintf(out, "\n");
253 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
254 fprintf(out, " # adb shell stop\n");
255 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
256 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
257 fprintf(out, " # adb shell start\n");
258 fprintf(out, "\n");
259 fprintf(out, "\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700260 fprintf(out, "usage: adb shell cmd stats print-uid-map \n");
261 fprintf(out, "\n");
262 fprintf(out, " Prints the UID, app name, version mapping.\n");
263 fprintf(out, "\n");
264 fprintf(out, "\n");
yro87d983c2017-11-14 21:31:43 -0800265 fprintf(out, "usage: adb shell cmd stats clear-config \n");
266 fprintf(out, "\n");
267 fprintf(out, " Removes all configs from disk.\n");
268 fprintf(out, "\n");
269 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800270 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700271 fprintf(out, "\n");
272 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
273 fprintf(out, "\n");
274 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800275 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
276 fprintf(out, "\n");
277 fprintf(out, " Flushes all data on memory to disk.\n");
278 fprintf(out, "\n");
279 fprintf(out, "\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700280 fprintf(out, "usage: adb shell cmd stats config remove [UID] NAME\n");
281 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
282 fprintf(out, "\n");
283 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
284 fprintf(out, " wire-encoded protobuf format and passed via stdin.\n");
285 fprintf(out, "\n");
286 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
287 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
288 fprintf(out, " uid is used.\n");
289 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a372017-10-30 22:57:06 -0700290 fprintf(out, "\n");
291 fprintf(out, "\n");
292 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n");
293 fprintf(out, " Dump all metric data for a configuration.\n");
294 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
295 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
296 fprintf(out, " calling uid is used.\n");
297 fprintf(out, " NAME The name of the configuration\n");
David Chenadaf8b32017-11-03 15:42:08 -0700298 fprintf(out, "\n");
299 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800300 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
301 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
302 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
303 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
304 fprintf(out, " calling uid is used.\n");
305 fprintf(out, " NAME The name of the configuration\n");
306 fprintf(out, "\n");
307 fprintf(out, "\n");
308 fprintf(out, "usage: adb shell cmd stats print-stats\n");
309 fprintf(out, " Prints some basic stats.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700310}
311
David Chen1d7b0cd2017-11-15 14:20:04 -0800312status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
313 string name;
314 bool good = false;
315 int uid;
316 const int argCount = args.size();
317 if (argCount == 2) {
318 // Automatically pick the UID
319 uid = IPCThreadState::self()->getCallingUid();
320 // TODO: What if this isn't a binder call? Should we fail?
321 name.assign(args[1].c_str(), args[1].size());
322 good = true;
323 } else if (argCount == 3) {
324 // If it's a userdebug or eng build, then the shell user can
325 // impersonate other uids.
326 if (mEngBuild) {
327 const char* s = args[1].c_str();
328 if (*s != '\0') {
329 char* end = NULL;
330 uid = strtol(s, &end, 0);
331 if (*end == '\0') {
332 name.assign(args[2].c_str(), args[2].size());
333 good = true;
334 }
335 }
336 } else {
337 fprintf(out,
338 "The metrics can only be dumped for other UIDs on eng or userdebug "
339 "builds.\n");
340 }
341 }
342 if (!good) {
343 print_cmd_help(out);
344 return UNKNOWN_ERROR;
345 }
346 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name));
yro4d889e62017-11-17 15:44:48 -0800347 sp<IStatsCompanionService> sc = getStatsCompanionService();
348 if (sc != nullptr) {
349 sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str()));
350 ALOGD("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
351 args[2].c_str());
352 } else {
353 ALOGD("Could not access statsCompanion");
354 }
355
David Chenadaf8b32017-11-03 15:42:08 -0700356 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700357}
358
359status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
360 const int argCount = args.size();
361 if (argCount >= 2) {
362 if (args[1] == "update" || args[1] == "remove") {
363 bool good = false;
364 int uid = -1;
365 string name;
366
367 if (argCount == 3) {
368 // Automatically pick the UID
369 uid = IPCThreadState::self()->getCallingUid();
370 // TODO: What if this isn't a binder call? Should we fail?
371 name.assign(args[2].c_str(), args[2].size());
372 good = true;
373 } else if (argCount == 4) {
374 // If it's a userdebug or eng build, then the shell user can
375 // impersonate other uids.
376 if (mEngBuild) {
377 const char* s = args[2].c_str();
378 if (*s != '\0') {
379 char* end = NULL;
380 uid = strtol(s, &end, 0);
381 if (*end == '\0') {
382 name.assign(args[3].c_str(), args[3].size());
383 good = true;
384 }
385 }
386 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700387 fprintf(err,
388 "The config can only be set for other UIDs on eng or userdebug "
389 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700390 }
391 }
392
393 if (!good) {
394 // If arg parsing failed, print the help text and return an error.
395 print_cmd_help(out);
396 return UNKNOWN_ERROR;
397 }
398
399 if (args[1] == "update") {
400 // Read stream into buffer.
401 string buffer;
402 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
403 fprintf(err, "Error reading stream for StatsConfig.\n");
404 return UNKNOWN_ERROR;
405 }
406
407 // Parse buffer.
408 StatsdConfig config;
409 if (!config.ParseFromString(buffer)) {
410 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
411 return UNKNOWN_ERROR;
412 }
413
414 // Add / update the config.
415 mConfigManager->UpdateConfig(ConfigKey(uid, name), config);
416 } else {
417 // Remove the config.
418 mConfigManager->RemoveConfig(ConfigKey(uid, name));
419 }
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
David Chen1d7b0cd2017-11-15 14:20:04 -0800477status_t StatsService::cmd_print_stats(FILE* out) {
478 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 }
483 return NO_ERROR;
484}
485
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700486status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
487 long msec = 0;
488
489 if (args.size() > 2) {
490 msec = strtol(args[2].string(), NULL, 10);
David Chen0656b7a2017-09-13 15:53:39 -0700491 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700492 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
493}
494
495status_t StatsService::cmd_print_uid_map(FILE* out) {
496 mUidMap->printUidMap(out);
497 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700498}
499
yro947fbce2017-11-15 22:50:23 -0800500status_t StatsService::cmd_write_data_to_disk(FILE* out) {
501 fprintf(out, "Writing data to disk\n");
502 mProcessor->WriteDataToDisk();
503 return NO_ERROR;
504}
505
David Chen1481fe12017-10-16 13:16:34 -0700506status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
507 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700508 vector<shared_ptr<LogEvent> > stats;
509 if (mStatsPullerManager.Pull(s, &stats)) {
510 for (const auto& it : stats) {
511 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
512 }
513 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
514 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700515 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700516 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700517}
518
yro87d983c2017-11-14 21:31:43 -0800519status_t StatsService::cmd_remove_config_files(FILE* out) {
520 fprintf(out, "Trying to remove config files...\n");
yro947fbce2017-11-15 22:50:23 -0800521 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800522 return NO_ERROR;
523}
524
Yao Chen8d9989b2017-11-18 18:54:50 -0800525status_t StatsService::cmd_dump_memory_info(FILE* out) {
526 std::string s = dumpMemInfo(100);
527 fprintf(out, "Memory Info\n");
528 fprintf(out, "%s", s.c_str());
529 return NO_ERROR;
530}
531
David Chende701692017-10-05 13:16:02 -0700532Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
533 const vector<String16>& app) {
534 if (DEBUG) ALOGD("StatsService::informAllUidData was called");
535
536 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
537 return Status::fromExceptionCode(Status::EX_SECURITY,
538 "Only system uid can call informAllUidData");
539 }
540
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700541 mUidMap->updateMap(uid, version, app);
David Chende701692017-10-05 13:16:02 -0700542 if (DEBUG) ALOGD("StatsService::informAllUidData succeeded");
543
544 return Status::ok();
545}
546
547Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
548 if (DEBUG) ALOGD("StatsService::informOnePackage was called");
549
550 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
551 return Status::fromExceptionCode(Status::EX_SECURITY,
552 "Only system uid can call informOnePackage");
553 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700554 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700555 return Status::ok();
556}
557
558Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
559 if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called");
560
561 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
562 return Status::fromExceptionCode(Status::EX_SECURITY,
563 "Only system uid can call informOnePackageRemoved");
564 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700565 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700566 return Status::ok();
567}
568
Yao Chenef99c4f2017-09-22 16:26:54 -0700569Status StatsService::informAnomalyAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700570 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700571
572 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
573 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700574 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700575 }
576
Bookatzb487b552017-09-18 11:26:01 -0700577 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700578 // TODO: check through all counters/timers and see if an anomaly has indeed occurred.
Yangster-mace2cd6d52017-11-09 20:38:30 -0800579 uint64_t currentTimeNs = time(nullptr) * NS_PER_SEC;
580 std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
581 mAnomalyMonitor->onAlarmFired(currentTimeNs);
582 mProcessor->onAnomalyAlarmFired(currentTimeNs, anomalySet);
Bookatz1b0b1142017-09-08 11:58:42 -0700583 return Status::ok();
584}
585
Yao Chenef99c4f2017-09-22 16:26:54 -0700586Status StatsService::informPollAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700587 if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700588
589 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
590 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700591 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700592 }
593
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700594 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700595
Bookatzb487b552017-09-18 11:26:01 -0700596 if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700597
598 return Status::ok();
599}
600
Yao Chenef99c4f2017-09-22 16:26:54 -0700601Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700602 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
603 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700604 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700605 }
606
607 // When system_server is up and running, schedule the dropbox task to run.
608 ALOGD("StatsService::systemRunning");
609
Bookatzb487b552017-09-18 11:26:01 -0700610 sayHiToStatsCompanion();
611
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700612 return Status::ok();
613}
614
yro947fbce2017-11-15 22:50:23 -0800615Status StatsService::writeDataToDisk() {
616 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
617 return Status::fromExceptionCode(Status::EX_SECURITY,
618 "Only system uid can call systemRunning");
619 }
620
621 ALOGD("StatsService::writeDataToDisk");
622
623 mProcessor->WriteDataToDisk();
624
625 return Status::ok();
626}
627
Yao Chenef99c4f2017-09-22 16:26:54 -0700628void StatsService::sayHiToStatsCompanion() {
629 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
630 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700631 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
632 if (statsCompanion != nullptr) {
633 if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready");
634 statsCompanion->statsdReady();
635 } else {
636 if (DEBUG) ALOGD("Could not access statsCompanion");
637 }
638}
639
Yao Chenef99c4f2017-09-22 16:26:54 -0700640sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700641 sp<IStatsCompanionService> statsCompanion = nullptr;
642 // Get statscompanion service from service manager
643 const sp<IServiceManager> sm(defaultServiceManager());
644 if (sm != nullptr) {
645 const String16 name("statscompanion");
646 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
647 if (statsCompanion == nullptr) {
648 ALOGW("statscompanion service unavailable!");
649 return nullptr;
650 }
651 }
652 return statsCompanion;
653}
654
Yao Chenef99c4f2017-09-22 16:26:54 -0700655Status StatsService::statsCompanionReady() {
Bookatzb487b552017-09-18 11:26:01 -0700656 if (DEBUG) ALOGD("StatsService::statsCompanionReady was called");
657
658 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
659 return Status::fromExceptionCode(Status::EX_SECURITY,
660 "Only system uid can call statsCompanionReady");
661 }
662
663 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
664 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700665 return Status::fromExceptionCode(
666 Status::EX_NULL_POINTER,
667 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700668 }
669 if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700670 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700671 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
672
673 return Status::ok();
674}
675
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700676void StatsService::Startup() {
677 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700678}
679
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700680void StatsService::OnLogEvent(const LogEvent& event) {
681 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700682}
683
Yangster7c334a12017-11-22 14:24:24 -0800684Status StatsService::getData(const String16& key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700685 IPCThreadState* ipc = IPCThreadState::self();
David Chen1d7b0cd2017-11-15 14:20:04 -0800686 ALOGD("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(),
687 ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800688 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800689 string keyStr = string(String8(key).string());
690 ConfigKey configKey(ipc->getCallingUid(), keyStr);
691 mProcessor->onDumpReport(configKey, output);
David Chenadaf8b32017-11-03 15:42:08 -0700692 return Status::ok();
693 } else {
694 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
695 }
yro31eb67b2017-10-24 13:33:21 -0700696}
697
David Chenadaf8b32017-11-03 15:42:08 -0700698Status StatsService::addConfiguration(const String16& key,
699 const vector <uint8_t>& config,
700 const String16& package, const String16& cls,
701 bool* success) {
702 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800703 if (checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700704 string keyString = string(String8(key).string());
David Chen1d7b0cd2017-11-15 14:20:04 -0800705 ConfigKey configKey(ipc->getCallingUid(), keyString);
David Chenadaf8b32017-11-03 15:42:08 -0700706 StatsdConfig cfg;
707 cfg.ParseFromArray(&config[0], config.size());
708 mConfigManager->UpdateConfig(configKey, cfg);
709 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
710 string(String8(cls).string()));
711 *success = true;
712 return Status::ok();
713 } else {
714 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700715 }
yro31eb67b2017-10-24 13:33:21 -0700716}
717
David Chenadaf8b32017-11-03 15:42:08 -0700718Status StatsService::removeConfiguration(const String16& key, bool* success) {
719 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800720 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800721 string keyStr = string(String8(key).string());
722 mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr));
David Chenadaf8b32017-11-03 15:42:08 -0700723 return Status::ok();
724 } else {
725 *success = false;
726 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700727 }
yro31eb67b2017-10-24 13:33:21 -0700728}
729
David Chen1d7b0cd2017-11-15 14:20:04 -0800730void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700731}
732
Yao Chenef99c4f2017-09-22 16:26:54 -0700733} // namespace statsd
734} // namespace os
735} // namespace android