blob: 6652db609f2fd71f8aa04fb1bd53726c6ebd7ca5 [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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // 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"
Yangster-mac330af582018-02-08 15:24:38 -080021#include "stats_log_util.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080022#include "android-base/stringprintf.h"
David Chenadaf8b32017-11-03 15:42:08 -070023#include "config/ConfigKey.h"
24#include "config/ConfigManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "guardrail/StatsdStats.h"
yro947fbce2017-11-15 22:50:23 -080026#include "storage/StorageManager.h"
Bookatzc6977972018-01-16 16:55:05 -080027#include "subscriber/SubscriberReporter.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>
Bookatzb223c4e2018-02-01 15:35:04 -080037#include <statslog.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070039#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070040#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070041#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070042
43using namespace android;
44
Bookatz906a35c2017-09-20 15:26:44 -070045namespace android {
46namespace os {
47namespace statsd {
48
David Chenadaf8b32017-11-03 15:42:08 -070049constexpr const char* kPermissionDump = "android.permission.DUMP";
yro03faf092017-12-12 00:17:50 -080050#define STATS_SERVICE_DIR "/data/misc/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070051
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070052StatsService::StatsService(const sp<Looper>& handlerLooper)
Yangster-mac932ecec2018-02-01 10:23:52 -080053 : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
54 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
55 if (sc != nullptr) {
56 sc->setAnomalyAlarm(timeMillis);
57 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
58 }
59 },
60 [](const sp<IStatsCompanionService>& sc) {
61 if (sc != nullptr) {
62 sc->cancelAnomalyAlarm();
63 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
64 }
65 })),
66 mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
67 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
68 if (sc != nullptr) {
69 sc->setAlarmForSubscriberTriggering(timeMillis);
70 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
71 }
72 },
73 [](const sp<IStatsCompanionService>& sc) {
74 if (sc != nullptr) {
75 sc->cancelAlarmForSubscriberTriggering();
76 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
77 }
78
79 })) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070080 mUidMap = new UidMap();
Chenjie Yu80f91122018-01-31 20:24:50 -080081 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070082 mConfigManager = new ConfigManager();
Yangster-mac932ecec2018-02-01 10:23:52 -080083 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070084 getElapsedRealtimeNs(), [this](const ConfigKey& key) {
Yangster-mac932ecec2018-02-01 10:23:52 -080085 sp<IStatsCompanionService> sc = getStatsCompanionService();
86 auto receiver = mConfigManager->GetConfigReceiver(key);
87 if (sc == nullptr) {
88 VLOG("Could not find StatsCompanionService");
89 } else if (receiver == nullptr) {
90 VLOG("Statscompanion could not find a broadcast receiver for %s",
91 key.ToString().c_str());
92 } else {
93 sc->sendDataBroadcast(receiver);
David Chen1d7b0cd2017-11-15 14:20:04 -080094 }
Yangster-mac932ecec2018-02-01 10:23:52 -080095 }
Yangster-mac330af582018-02-08 15:24:38 -080096 );
Joe Onorato9fc9edf2017-10-15 20:08:52 -070097
98 mConfigManager->AddListener(mProcessor);
99
100 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700101}
102
Yao Chenef99c4f2017-09-22 16:26:54 -0700103StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700104}
105
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700106void StatsService::init_system_properties() {
107 mEngBuild = false;
108 const prop_info* buildType = __system_property_find("ro.build.type");
109 if (buildType != NULL) {
110 __system_property_read_callback(buildType, init_build_type_callback, this);
111 }
David Chen0656b7a2017-09-13 15:53:39 -0700112}
113
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
115 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700116 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
118 }
119}
120
121/**
122 * Implement our own because the default binder implementation isn't
123 * properly handling SHELL_COMMAND_TRANSACTION.
124 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700125status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
126 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700127 switch (code) {
128 case SHELL_COMMAND_TRANSACTION: {
129 int in = data.readFileDescriptor();
130 int out = data.readFileDescriptor();
131 int err = data.readFileDescriptor();
132 int argc = data.readInt32();
133 Vector<String8> args;
134 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
135 args.add(String8(data.readString16()));
136 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700137 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
138 sp<IResultReceiver> resultReceiver =
139 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700140
141 FILE* fin = fdopen(in, "r");
142 FILE* fout = fdopen(out, "w");
143 FILE* ferr = fdopen(err, "w");
144
145 if (fin == NULL || fout == NULL || ferr == NULL) {
146 resultReceiver->send(NO_MEMORY);
147 } else {
148 err = command(fin, fout, ferr, args);
149 resultReceiver->send(err);
150 }
151
152 if (fin != NULL) {
153 fflush(fin);
154 fclose(fin);
155 }
156 if (fout != NULL) {
157 fflush(fout);
158 fclose(fout);
159 }
160 if (fout != NULL) {
161 fflush(ferr);
162 fclose(ferr);
163 }
164
165 return NO_ERROR;
166 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700167 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700168 }
169}
170
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700171/**
172 * Write debugging data about statsd.
173 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700174status_t StatsService::dump(int fd, const Vector<String16>& args) {
Tej Singhdd83d702018-04-10 17:24:50 -0700175 if (!checkCallingPermission(String16(kPermissionDump))) {
176 return PERMISSION_DENIED;
177 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700178 FILE* out = fdopen(fd, "w");
179 if (out == NULL) {
180 return NO_MEMORY; // the fd is already open
181 }
182
Yao Chen884c8c12018-01-26 10:36:25 -0800183 bool verbose = false;
Tej Singh41b3f9a2018-04-03 17:06:35 -0700184 bool proto = false;
Yao Chen884c8c12018-01-26 10:36:25 -0800185 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
186 verbose = true;
187 }
Tej Singh41b3f9a2018-04-03 17:06:35 -0700188 if (args.size() > 0 && !args[args.size()-1].compare(String16("--proto"))) {
189 proto = true;
190 }
Yao Chen884c8c12018-01-26 10:36:25 -0800191
Tej Singh41b3f9a2018-04-03 17:06:35 -0700192 dump_impl(out, verbose, proto);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700193
194 fclose(out);
195 return NO_ERROR;
196}
197
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700198/**
Tej Singh41b3f9a2018-04-03 17:06:35 -0700199 * Write debugging data about statsd in text or proto format.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700200 */
Tej Singh41b3f9a2018-04-03 17:06:35 -0700201void StatsService::dump_impl(FILE* out, bool verbose, bool proto) {
202 if (proto) {
203 vector<uint8_t> data;
204 StatsdStats::getInstance().dumpStats(&data, false); // does not reset statsdStats.
205 for (size_t i = 0; i < data.size(); i ++) {
206 fprintf(out, "%c", data[i]);
207 }
208 } else {
209 StatsdStats::getInstance().dumpStats(out);
210 mProcessor->dumpStates(out, verbose);
211 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700212}
213
214/**
215 * Implementation of the adb shell cmd stats command.
216 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700217status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700218 // TODO: Permission check
219
220 const int argCount = args.size();
221 if (argCount >= 1) {
222 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700223 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700224 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700225 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700226
David Chende701692017-10-05 13:16:02 -0700227 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800228 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700229 }
Yao Chen729093d2017-10-16 10:33:26 -0700230
231 if (!args[0].compare(String8("dump-report"))) {
232 return cmd_dump_report(out, err, args);
233 }
David Chen1481fe12017-10-16 13:16:34 -0700234
235 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
236 return cmd_print_pulled_metrics(out, args);
237 }
David Chenadaf8b32017-11-03 15:42:08 -0700238
239 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800240 return cmd_trigger_broadcast(out, args);
241 }
242
243 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800244 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700245 }
yro87d983c2017-11-14 21:31:43 -0800246
Yao Chen8d9989b2017-11-18 18:54:50 -0800247 if (!args[0].compare(String8("meminfo"))) {
248 return cmd_dump_memory_info(out);
249 }
yro947fbce2017-11-15 22:50:23 -0800250
251 if (!args[0].compare(String8("write-to-disk"))) {
252 return cmd_write_data_to_disk(out);
253 }
Bookatzb223c4e2018-02-01 15:35:04 -0800254
David Chen0b5c90c2018-01-25 16:51:49 -0800255 if (!args[0].compare(String8("log-app-breadcrumb"))) {
256 return cmd_log_app_breadcrumb(out, args);
Bookatzb223c4e2018-02-01 15:35:04 -0800257 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800258
259 if (!args[0].compare(String8("clear-puller-cache"))) {
260 return cmd_clear_puller_cache(out);
261 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700262 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700263
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700264 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700265 return NO_ERROR;
266}
267
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700268void StatsService::print_cmd_help(FILE* out) {
269 fprintf(out,
270 "usage: adb shell cmd stats print-stats-log [tag_required] "
271 "[timestamp_nsec_optional]\n");
272 fprintf(out, "\n");
273 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800274 fprintf(out, "usage: adb shell cmd stats meminfo\n");
275 fprintf(out, "\n");
276 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
277 fprintf(out, " # adb shell stop\n");
278 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
279 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
280 fprintf(out, " # adb shell start\n");
281 fprintf(out, "\n");
282 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800283 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700284 fprintf(out, "\n");
285 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800286 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700287 fprintf(out, "\n");
288 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800289 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700290 fprintf(out, "\n");
291 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
292 fprintf(out, "\n");
293 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800294 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
295 fprintf(out, "\n");
296 fprintf(out, " Flushes all data on memory to disk.\n");
297 fprintf(out, "\n");
298 fprintf(out, "\n");
David Chen0b5c90c2018-01-25 16:51:49 -0800299 fprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
300 fprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800301 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
302 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
303 fprintf(out, " uid is used.\n");
304 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
305 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
306 fprintf(out, "\n");
307 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800308 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700309 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
310 fprintf(out, "\n");
311 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800312 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
313 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700314 fprintf(out, "\n");
315 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800316 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700317 fprintf(out, " uid is used.\n");
318 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700319 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800320 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
321 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700322 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800323 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700324 fprintf(out, " Dump all metric data for a configuration.\n");
325 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
326 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
327 fprintf(out, " calling uid is used.\n");
328 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800329 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700330 fprintf(out, "\n");
331 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800332 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
333 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
334 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
335 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
336 fprintf(out, " calling uid is used.\n");
337 fprintf(out, " NAME The name of the configuration\n");
338 fprintf(out, "\n");
339 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800340 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800341 fprintf(out, " Prints some basic stats.\n");
Tej Singh41b3f9a2018-04-03 17:06:35 -0700342 fprintf(out, " --proto Print proto binary instead of string format.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800343 fprintf(out, "\n");
344 fprintf(out, "\n");
345 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
346 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700347}
348
David Chen1d7b0cd2017-11-15 14:20:04 -0800349status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
350 string name;
351 bool good = false;
352 int uid;
353 const int argCount = args.size();
354 if (argCount == 2) {
355 // Automatically pick the UID
356 uid = IPCThreadState::self()->getCallingUid();
357 // TODO: What if this isn't a binder call? Should we fail?
358 name.assign(args[1].c_str(), args[1].size());
359 good = true;
360 } else if (argCount == 3) {
361 // If it's a userdebug or eng build, then the shell user can
362 // impersonate other uids.
363 if (mEngBuild) {
364 const char* s = args[1].c_str();
365 if (*s != '\0') {
366 char* end = NULL;
367 uid = strtol(s, &end, 0);
368 if (*end == '\0') {
369 name.assign(args[2].c_str(), args[2].size());
370 good = true;
371 }
372 }
373 } else {
374 fprintf(out,
375 "The metrics can only be dumped for other UIDs on eng or userdebug "
376 "builds.\n");
377 }
378 }
379 if (!good) {
380 print_cmd_help(out);
381 return UNKNOWN_ERROR;
382 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800383 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800384 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800385 if (sc == nullptr) {
386 VLOG("Could not access statsCompanion");
387 } else if (receiver == nullptr) {
388 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
389 } else {
390 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800391 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
392 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800393 }
394
David Chenadaf8b32017-11-03 15:42:08 -0700395 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700396}
397
398status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
399 const int argCount = args.size();
400 if (argCount >= 2) {
401 if (args[1] == "update" || args[1] == "remove") {
402 bool good = false;
403 int uid = -1;
404 string name;
405
406 if (argCount == 3) {
407 // Automatically pick the UID
408 uid = IPCThreadState::self()->getCallingUid();
409 // TODO: What if this isn't a binder call? Should we fail?
410 name.assign(args[2].c_str(), args[2].size());
411 good = true;
412 } else if (argCount == 4) {
413 // If it's a userdebug or eng build, then the shell user can
414 // impersonate other uids.
415 if (mEngBuild) {
416 const char* s = args[2].c_str();
417 if (*s != '\0') {
418 char* end = NULL;
419 uid = strtol(s, &end, 0);
420 if (*end == '\0') {
421 name.assign(args[3].c_str(), args[3].size());
422 good = true;
423 }
424 }
425 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700426 fprintf(err,
427 "The config can only be set for other UIDs on eng or userdebug "
428 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700429 }
yroe5f82922018-01-22 18:37:27 -0800430 } else if (argCount == 2 && args[1] == "remove") {
431 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700432 }
433
434 if (!good) {
435 // If arg parsing failed, print the help text and return an error.
436 print_cmd_help(out);
437 return UNKNOWN_ERROR;
438 }
439
440 if (args[1] == "update") {
yro255f72e2018-02-26 15:15:17 -0800441 char* endp;
442 int64_t configID = strtoll(name.c_str(), &endp, 10);
443 if (endp == name.c_str() || *endp != '\0') {
444 fprintf(err, "Error parsing config ID.\n");
445 return UNKNOWN_ERROR;
446 }
447
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700448 // Read stream into buffer.
449 string buffer;
450 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
451 fprintf(err, "Error reading stream for StatsConfig.\n");
452 return UNKNOWN_ERROR;
453 }
454
455 // Parse buffer.
456 StatsdConfig config;
457 if (!config.ParseFromString(buffer)) {
458 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
459 return UNKNOWN_ERROR;
460 }
461
462 // Add / update the config.
yro255f72e2018-02-26 15:15:17 -0800463 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700464 } else {
yro74fed972017-11-27 14:42:42 -0800465 if (argCount == 2) {
466 cmd_remove_all_configs(out);
467 } else {
468 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800469 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800470 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700471 }
472
473 return NO_ERROR;
474 }
David Chen0656b7a2017-09-13 15:53:39 -0700475 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700476 print_cmd_help(out);
477 return UNKNOWN_ERROR;
478}
479
Yao Chen729093d2017-10-16 10:33:26 -0700480status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
481 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800482 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700483 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800484 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700485 int uid;
486 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800487 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
488 proto = true;
489 argCount -= 1;
490 }
Yao Chen729093d2017-10-16 10:33:26 -0700491 if (argCount == 2) {
492 // Automatically pick the UID
493 uid = IPCThreadState::self()->getCallingUid();
494 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700495 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700496 good = true;
497 } else if (argCount == 3) {
498 // If it's a userdebug or eng build, then the shell user can
499 // impersonate other uids.
500 if (mEngBuild) {
501 const char* s = args[1].c_str();
502 if (*s != '\0') {
503 char* end = NULL;
504 uid = strtol(s, &end, 0);
505 if (*end == '\0') {
506 name.assign(args[2].c_str(), args[2].size());
507 good = true;
508 }
509 }
510 } else {
511 fprintf(out,
512 "The metrics can only be dumped for other UIDs on eng or userdebug "
513 "builds.\n");
514 }
515 }
516 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800517 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800518 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
Yangster-mace68f3a52018-04-04 00:01:43 -0700519 false /* include_current_bucket*/, &data);
Yao Chen729093d2017-10-16 10:33:26 -0700520 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800521 if (proto) {
522 for (size_t i = 0; i < data.size(); i ++) {
523 fprintf(out, "%c", data[i]);
524 }
525 } else {
526 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
527 fprintf(out, "See the StatsLogReport in logcat...\n");
528 }
Yao Chen729093d2017-10-16 10:33:26 -0700529 return android::OK;
530 } else {
531 // If arg parsing failed, print the help text and return an error.
532 print_cmd_help(out);
533 return UNKNOWN_ERROR;
534 }
535 } else {
536 fprintf(out, "Log processor does not exist...\n");
537 return UNKNOWN_ERROR;
538 }
539}
540
Yao Chenb3561512017-11-21 18:07:17 -0800541status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
Tej Singh41b3f9a2018-04-03 17:06:35 -0700542 int argCount = args.size();
543 bool proto = false;
544 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
545 proto = true;
546 argCount -= 1;
David Chen1d7b0cd2017-11-15 14:20:04 -0800547 }
Yao Chenb3561512017-11-21 18:07:17 -0800548 StatsdStats& statsdStats = StatsdStats::getInstance();
Tej Singh41b3f9a2018-04-03 17:06:35 -0700549 if (proto) {
550 vector<uint8_t> data;
551 statsdStats.dumpStats(&data, false); // does not reset statsdStats.
552 for (size_t i = 0; i < data.size(); i ++) {
553 fprintf(out, "%c", data[i]);
554 }
555
556 } else {
557 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
558 for (const ConfigKey& key : configs) {
559 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
560 mProcessor->GetMetricsSize(key));
561 }
562 statsdStats.dumpStats(out);
563 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800564 return NO_ERROR;
565}
566
Yao Chend10f7b12017-12-18 12:53:50 -0800567status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
568 if (args.size() > 1) {
569 string pkg;
570 pkg.assign(args[1].c_str(), args[1].size());
571 auto uids = mUidMap->getAppUid(pkg);
572 fprintf(out, "%s -> [ ", pkg.c_str());
573 for (const auto& uid : uids) {
574 fprintf(out, "%d ", uid);
575 }
576 fprintf(out, "]\n");
577 } else {
578 mUidMap->printUidMap(out);
579 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700580 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700581}
582
yro947fbce2017-11-15 22:50:23 -0800583status_t StatsService::cmd_write_data_to_disk(FILE* out) {
584 fprintf(out, "Writing data to disk\n");
585 mProcessor->WriteDataToDisk();
586 return NO_ERROR;
587}
588
David Chen0b5c90c2018-01-25 16:51:49 -0800589status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800590 bool good = false;
591 int32_t uid;
592 int32_t label;
593 int32_t state;
594 const int argCount = args.size();
595 if (argCount == 3) {
596 // Automatically pick the UID
597 uid = IPCThreadState::self()->getCallingUid();
598 label = atoi(args[1].c_str());
599 state = atoi(args[2].c_str());
600 good = true;
601 } else if (argCount == 4) {
602 uid = atoi(args[1].c_str());
603 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
604 // Otherwise, the uid must match the actual caller's uid.
605 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
606 label = atoi(args[2].c_str());
607 state = atoi(args[3].c_str());
608 good = true;
609 } else {
610 fprintf(out,
David Chenb639d142018-02-14 17:29:54 -0800611 "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs "
612 "on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800613 }
614 }
615 if (good) {
David Chen0b5c90c2018-01-25 16:51:49 -0800616 fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
617 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800618 } else {
619 print_cmd_help(out);
620 return UNKNOWN_ERROR;
621 }
622 return NO_ERROR;
623}
624
David Chen1481fe12017-10-16 13:16:34 -0700625status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
626 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700627 vector<shared_ptr<LogEvent> > stats;
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700628 if (mStatsPullerManager.Pull(s, getElapsedRealtimeNs(), &stats)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700629 for (const auto& it : stats) {
630 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
631 }
632 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
633 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700634 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700635 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700636}
637
yro74fed972017-11-27 14:42:42 -0800638status_t StatsService::cmd_remove_all_configs(FILE* out) {
639 fprintf(out, "Removing all configs...\n");
640 VLOG("StatsService::cmd_remove_all_configs was called");
641 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800642 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800643 return NO_ERROR;
644}
645
Yao Chen8d9989b2017-11-18 18:54:50 -0800646status_t StatsService::cmd_dump_memory_info(FILE* out) {
Yao Chen20e9e622018-02-28 11:18:51 -0800647 fprintf(out, "meminfo not available.\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800648 return NO_ERROR;
649}
650
Chenjie Yue72252b2018-02-01 13:19:35 -0800651status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800652 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800653 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
654 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800655 if (checkCallingPermission(String16(kPermissionDump))) {
656 int cleared = mStatsPullerManager.ForceClearPullerCache();
657 fprintf(out, "Puller removed %d cached data!\n", cleared);
658 return NO_ERROR;
659 } else {
660 return PERMISSION_DENIED;
661 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800662}
663
Dianne Hackborn3accca02013-09-20 09:32:11 -0700664Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700665 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800666 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700667
668 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
669 return Status::fromExceptionCode(Status::EX_SECURITY,
670 "Only system uid can call informAllUidData");
671 }
672
David Chenbd125272018-04-04 19:02:50 -0700673 mUidMap->updateMap(getElapsedRealtimeNs(), uid, version, app);
yro74fed972017-11-27 14:42:42 -0800674 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700675
676 return Status::ok();
677}
678
Dianne Hackborn3accca02013-09-20 09:32:11 -0700679Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800680 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700681
682 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
683 return Status::fromExceptionCode(Status::EX_SECURITY,
684 "Only system uid can call informOnePackage");
685 }
David Chenbd125272018-04-04 19:02:50 -0700686 mUidMap->updateApp(getElapsedRealtimeNs(), app, uid, version);
David Chende701692017-10-05 13:16:02 -0700687 return Status::ok();
688}
689
690Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800691 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700692
693 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
694 return Status::fromExceptionCode(Status::EX_SECURITY,
695 "Only system uid can call informOnePackageRemoved");
696 }
David Chenbd125272018-04-04 19:02:50 -0700697 mUidMap->removeApp(getElapsedRealtimeNs(), app, uid);
yro01924022018-02-20 18:20:49 -0800698 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700699 return Status::ok();
700}
701
Yao Chenef99c4f2017-09-22 16:26:54 -0700702Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800703 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700704
705 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
706 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700707 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700708 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800709
Yangster-macb142cc82018-03-30 15:22:08 -0700710 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800711 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
712 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
713 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800714 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800715 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800716 } else {
717 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
718 }
Bookatz1b0b1142017-09-08 11:58:42 -0700719 return Status::ok();
720}
721
Yangster-mac932ecec2018-02-01 10:23:52 -0800722Status StatsService::informAlarmForSubscriberTriggeringFired() {
723 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
724
725 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
726 return Status::fromExceptionCode(
727 Status::EX_SECURITY,
728 "Only system uid can call informAlarmForSubscriberTriggeringFired");
729 }
730
Yangster-macb142cc82018-03-30 15:22:08 -0700731 int64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800732 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
733 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
734 if (alarmSet.size() > 0) {
735 VLOG("Found periodic alarm fired.");
736 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
737 } else {
738 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
739 }
740 return Status::ok();
741}
742
Yao Chenef99c4f2017-09-22 16:26:54 -0700743Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800744 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700745
746 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
747 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700748 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700749 }
750
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700751 mProcessor->informPullAlarmFired(getElapsedRealtimeNs());
Chenjie Yub3dda412017-10-24 13:41:59 -0700752
yro74fed972017-11-27 14:42:42 -0800753 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700754
755 return Status::ok();
756}
757
Yao Chenef99c4f2017-09-22 16:26:54 -0700758Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700759 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
760 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700761 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700762 }
763
764 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800765 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700766
Bookatzb487b552017-09-18 11:26:01 -0700767 sayHiToStatsCompanion();
768
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700769 return Status::ok();
770}
771
yro947fbce2017-11-15 22:50:23 -0800772Status StatsService::writeDataToDisk() {
773 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
774 return Status::fromExceptionCode(Status::EX_SECURITY,
775 "Only system uid can call systemRunning");
776 }
777
yro74fed972017-11-27 14:42:42 -0800778 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800779
780 mProcessor->WriteDataToDisk();
781
782 return Status::ok();
783}
784
Yao Chenef99c4f2017-09-22 16:26:54 -0700785void StatsService::sayHiToStatsCompanion() {
Bookatzb487b552017-09-18 11:26:01 -0700786 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
787 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800788 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700789 statsCompanion->statsdReady();
790 } else {
yro74fed972017-11-27 14:42:42 -0800791 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700792 }
793}
794
Yao Chenef99c4f2017-09-22 16:26:54 -0700795Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800796 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700797
798 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
799 return Status::fromExceptionCode(Status::EX_SECURITY,
800 "Only system uid can call statsCompanionReady");
801 }
802
803 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
804 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700805 return Status::fromExceptionCode(
806 Status::EX_NULL_POINTER,
807 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700808 }
yro74fed972017-11-27 14:42:42 -0800809 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700810 IInterface::asBinder(statsCompanion)->linkToDeath(this);
811 mStatsPullerManager.SetStatsCompanionService(statsCompanion);
Yangster-mac932ecec2018-02-01 10:23:52 -0800812 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
813 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800814 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700815 return Status::ok();
816}
817
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700818void StatsService::Startup() {
819 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700820}
821
Yao Chen163d2602018-04-10 10:39:53 -0700822void StatsService::OnLogEvent(LogEvent* event, bool reconnectionStarts) {
823 mProcessor->OnLogEvent(event, reconnectionStarts);
Bookatz906a35c2017-09-20 15:26:44 -0700824}
825
Yangster-mac94e197c2018-01-02 16:03:03 -0800826Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700827 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800828 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -0700829 if (!checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700830 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
831 }
Bookatz4f716292018-04-10 17:15:12 -0700832 ConfigKey configKey(ipc->getCallingUid(), key);
833 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(),
834 false /* include_current_bucket*/, output);
835 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -0700836}
837
David Chen2e8f3802017-11-22 10:56:48 -0800838Status StatsService::getMetadata(vector<uint8_t>* output) {
839 IPCThreadState* ipc = IPCThreadState::self();
840 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
841 ipc->getCallingUid());
Bookatz4f716292018-04-10 17:15:12 -0700842 if (!checkCallingPermission(String16(kPermissionDump))) {
David Chen2e8f3802017-11-22 10:56:48 -0800843 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
844 }
Bookatz4f716292018-04-10 17:15:12 -0700845 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
846 return Status::ok();
David Chen2e8f3802017-11-22 10:56:48 -0800847}
848
Bookatz4f716292018-04-10 17:15:12 -0700849Status StatsService::addConfiguration(int64_t key, const vector <uint8_t>& config) {
850 if (!checkCallingPermission(String16(kPermissionDump))) {
851 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
852 }
David Chenadaf8b32017-11-03 15:42:08 -0700853 IPCThreadState* ipc = IPCThreadState::self();
Bookatz4f716292018-04-10 17:15:12 -0700854 if (addConfigurationChecked(ipc->getCallingUid(), key, config)) {
David Chen661f7912018-01-22 17:46:24 -0800855 return Status::ok();
856 } else {
Bookatz4f716292018-04-10 17:15:12 -0700857 ALOGE("Could not parse malformatted StatsdConfig");
858 return Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT,
859 "config does not correspond to a StatsdConfig proto");
David Chen661f7912018-01-22 17:46:24 -0800860 }
861}
862
David Chen9fdd4032018-03-20 14:38:56 -0700863bool StatsService::addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config) {
864 ConfigKey configKey(uid, key);
865 StatsdConfig cfg;
866 if (config.size() > 0) { // If the config is empty, skip parsing.
867 if (!cfg.ParseFromArray(&config[0], config.size())) {
868 return false;
869 }
870 }
871 mConfigManager->UpdateConfig(configKey, cfg);
872 return true;
873}
874
Bookatz4f716292018-04-10 17:15:12 -0700875Status StatsService::removeDataFetchOperation(int64_t key) {
876 if (!checkCallingPermission(String16(kPermissionDump))) {
David Chen661f7912018-01-22 17:46:24 -0800877 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
878 }
Bookatz4f716292018-04-10 17:15:12 -0700879 IPCThreadState* ipc = IPCThreadState::self();
880 ConfigKey configKey(ipc->getCallingUid(), key);
881 mConfigManager->RemoveConfigReceiver(configKey);
882 return Status::ok();
David Chen661f7912018-01-22 17:46:24 -0800883}
884
Bookatz4f716292018-04-10 17:15:12 -0700885Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender) {
886 if (!checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700887 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700888 }
Bookatz4f716292018-04-10 17:15:12 -0700889 IPCThreadState* ipc = IPCThreadState::self();
890 ConfigKey configKey(ipc->getCallingUid(), key);
891 mConfigManager->SetConfigReceiver(configKey, intentSender);
892 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -0700893}
894
Bookatz4f716292018-04-10 17:15:12 -0700895Status StatsService::removeConfiguration(int64_t key) {
896 if (!checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700897 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700898 }
Bookatz4f716292018-04-10 17:15:12 -0700899 IPCThreadState* ipc = IPCThreadState::self();
900 ConfigKey configKey(ipc->getCallingUid(), key);
901 mConfigManager->RemoveConfig(configKey);
902 SubscriberReporter::getInstance().removeConfig(configKey);
903 return Status::ok();
yro31eb67b2017-10-24 13:33:21 -0700904}
905
Bookatzc6977972018-01-16 16:55:05 -0800906Status StatsService::setBroadcastSubscriber(int64_t configId,
907 int64_t subscriberId,
Bookatz4f716292018-04-10 17:15:12 -0700908 const sp<android::IBinder>& intentSender) {
Bookatzc6977972018-01-16 16:55:05 -0800909 VLOG("StatsService::setBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -0700910 if (!checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800911 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
912 }
Bookatz4f716292018-04-10 17:15:12 -0700913 IPCThreadState* ipc = IPCThreadState::self();
914 ConfigKey configKey(ipc->getCallingUid(), configId);
915 SubscriberReporter::getInstance()
916 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
917 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -0800918}
919
920Status StatsService::unsetBroadcastSubscriber(int64_t configId,
Bookatz4f716292018-04-10 17:15:12 -0700921 int64_t subscriberId) {
Bookatzc6977972018-01-16 16:55:05 -0800922 VLOG("StatsService::unsetBroadcastSubscriber called.");
Bookatz4f716292018-04-10 17:15:12 -0700923 if (!checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800924 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
925 }
Bookatz4f716292018-04-10 17:15:12 -0700926 IPCThreadState* ipc = IPCThreadState::self();
927 ConfigKey configKey(ipc->getCallingUid(), configId);
928 SubscriberReporter::getInstance()
929 .unsetBroadcastSubscriber(configKey, subscriberId);
930 return Status::ok();
Bookatzc6977972018-01-16 16:55:05 -0800931}
932
933
David Chen1d7b0cd2017-11-15 14:20:04 -0800934void StatsService::binderDied(const wp <IBinder>& who) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700935 ALOGW("statscompanion service died");
936 mProcessor->WriteDataToDisk();
937 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
938 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
939 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
940 mStatsPullerManager.SetStatsCompanionService(nullptr);
yro31eb67b2017-10-24 13:33:21 -0700941}
942
Yao Chenef99c4f2017-09-22 16:26:54 -0700943} // namespace statsd
944} // namespace os
945} // namespace android