blob: 8b6a864641559d5694ef049a38dbdebb080ca0e7 [file] [log] [blame]
Joe Onoratoc4dfae52017-10-17 23:38:21 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Joe Onoratoc4dfae52017-10-17 23:38:21 -070018#include "logd/LogEvent.h"
19
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -070020#include <android-base/stringprintf.h>
21#include <android/binder_ibinder.h>
22#include <private/android_filesystem_config.h>
23
Ruchir Rastogi13296512020-03-24 10:59:49 -070024#include "annotations.h"
Yangster-mac20877162017-12-22 17:19:39 -080025#include "stats_log_util.h"
Jeffrey Huang74fc4352020-03-06 15:18:33 -080026#include "statslog_statsd.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070027
28namespace android {
29namespace os {
30namespace statsd {
31
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040032// for TrainInfo experiment id serialization
33const int FIELD_ID_EXPERIMENT_ID = 1;
34
yro24809bd2017-10-31 23:06:53 -070035using namespace android::util;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080036using android::base::StringPrintf;
Yao Chen9c1debe2018-02-19 14:39:19 -080037using android::util::ProtoOutputStream;
David Chen1481fe12017-10-16 13:16:34 -070038using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080039using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070040
Tej Singhb26d0442020-01-31 16:18:21 -080041// stats_event.h socket types. Keep in sync.
42/* ERRORS */
43#define ERROR_NO_TIMESTAMP 0x1
44#define ERROR_NO_ATOM_ID 0x2
45#define ERROR_OVERFLOW 0x4
46#define ERROR_ATTRIBUTION_CHAIN_TOO_LONG 0x8
47#define ERROR_TOO_MANY_KEY_VALUE_PAIRS 0x10
48#define ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD 0x20
49#define ERROR_INVALID_ANNOTATION_ID 0x40
50#define ERROR_ANNOTATION_ID_TOO_LARGE 0x80
51#define ERROR_TOO_MANY_ANNOTATIONS 0x100
52#define ERROR_TOO_MANY_FIELDS 0x200
53#define ERROR_INVALID_VALUE_TYPE 0x400
54#define ERROR_STRING_NOT_NULL_TERMINATED 0x800
55
56/* TYPE IDS */
57#define INT32_TYPE 0x00
58#define INT64_TYPE 0x01
59#define STRING_TYPE 0x02
60#define LIST_TYPE 0x03
61#define FLOAT_TYPE 0x04
62#define BOOL_TYPE 0x05
63#define BYTE_ARRAY_TYPE 0x06
64#define OBJECT_TYPE 0x07
65#define KEY_VALUE_PAIRS_TYPE 0x08
66#define ATTRIBUTION_CHAIN_TYPE 0x09
67#define ERROR_TYPE 0x0F
68
Chenjie Yu0bd73db2018-12-16 07:37:04 -080069LogEvent::LogEvent(const LogEvent& event) {
70 mTagId = event.mTagId;
71 mLogUid = event.mLogUid;
Ruchir Rastogiab71ef02020-01-30 01:24:50 -080072 mLogPid = event.mLogPid;
Chenjie Yu0bd73db2018-12-16 07:37:04 -080073 mElapsedTimestampNs = event.mElapsedTimestampNs;
74 mLogdTimestampNs = event.mLogdTimestampNs;
75 mValues = event.mValues;
76}
77
Ruchir Rastogidfd63d42020-02-20 17:54:13 -080078LogEvent::LogEvent(int32_t uid, int32_t pid)
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -070079 : mLogdTimestampNs(time(nullptr)), mLogUid(uid), mLogPid(pid) {
Ruchir Rastogidfd63d42020-02-20 17:54:13 -080080}
81
Chenjie Yu6b1667c2019-01-18 10:09:33 -080082LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
83 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
84 const std::vector<uint8_t>& experimentIds, int32_t userId) {
85 mLogdTimestampNs = getWallClockNs();
86 mElapsedTimestampNs = getElapsedRealtimeNs();
Jeffrey Huang74fc4352020-03-06 15:18:33 -080087 mTagId = util::BINARY_PUSH_STATE_CHANGED;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080088 mLogUid = AIBinder_getCallingUid();
89 mLogPid = AIBinder_getCallingPid();
Chenjie Yu6b1667c2019-01-18 10:09:33 -080090
91 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
92 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
93 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value((int)requiresStaging)));
94 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value((int)rollbackEnabled)));
95 mValues.push_back(
96 FieldValue(Field(mTagId, getSimpleField(5)), Value((int)requiresLowLatencyMonitor)));
97 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(6)), Value(state)));
98 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(7)), Value(experimentIds)));
99 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(8)), Value(userId)));
100}
101
Howard Roa46b6582018-09-18 16:45:02 -0700102LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Chenjie Yu97dbb202019-02-13 16:42:04 -0800103 const InstallTrainInfo& trainInfo) {
104 mLogdTimestampNs = wallClockTimestampNs;
105 mElapsedTimestampNs = elapsedTimestampNs;
Jeffrey Huang74fc4352020-03-06 15:18:33 -0800106 mTagId = util::TRAIN_INFO;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800107
Chenjie Yu97dbb202019-02-13 16:42:04 -0800108 mValues.push_back(
109 FieldValue(Field(mTagId, getSimpleField(1)), Value(trainInfo.trainVersionCode)));
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400110 std::vector<uint8_t> experimentIdsProto;
111 writeExperimentIdsToProto(trainInfo.experimentIds, &experimentIdsProto);
112 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(experimentIdsProto)));
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800113 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value(trainInfo.trainName)));
114 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value(trainInfo.status)));
Chenjie Yu97dbb202019-02-13 16:42:04 -0800115}
116
Yangster-mac20877162017-12-22 17:19:39 -0800117LogEvent::~LogEvent() {
118 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -0800119 // This is for the case when LogEvent is created using the test interface
120 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -0800121 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -0800122 }
123}
124
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700125void LogEvent::parseInt32(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800126 int32_t value = readNextValue<int32_t>();
127 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700128 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800129}
130
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700131void LogEvent::parseInt64(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800132 int64_t value = readNextValue<int64_t>();
133 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700134 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800135}
136
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700137void LogEvent::parseString(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800138 int32_t numBytes = readNextValue<int32_t>();
139 if ((uint32_t)numBytes > mRemainingLen) {
140 mValid = false;
141 return;
142 }
143
144 string value = string((char*)mBuf, numBytes);
145 mBuf += numBytes;
146 mRemainingLen -= numBytes;
147 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700148 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800149}
150
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700151void LogEvent::parseFloat(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800152 float value = readNextValue<float>();
153 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700154 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800155}
156
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700157void LogEvent::parseBool(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800158 // cast to int32_t because FieldValue does not support bools
159 int32_t value = (int32_t)readNextValue<uint8_t>();
160 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700161 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800162}
163
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700164void LogEvent::parseByteArray(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800165 int32_t numBytes = readNextValue<int32_t>();
166 if ((uint32_t)numBytes > mRemainingLen) {
167 mValid = false;
168 return;
169 }
170
171 vector<uint8_t> value(mBuf, mBuf + numBytes);
172 mBuf += numBytes;
173 mRemainingLen -= numBytes;
174 addToValues(pos, depth, value, last);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700175 parseAnnotations(numAnnotations);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800176}
177
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700178void LogEvent::parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800179 int32_t numPairs = readNextValue<uint8_t>();
180
181 for (pos[1] = 1; pos[1] <= numPairs; pos[1]++) {
182 last[1] = (pos[1] == numPairs);
183
184 // parse key
185 pos[2] = 1;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700186 parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800187
188 // parse value
189 last[2] = true;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700190
191 uint8_t typeInfo = readNextValue<uint8_t>();
192 switch (getTypeId(typeInfo)) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800193 case INT32_TYPE:
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700194 pos[2] = 2; // pos[2] determined by index of type in KeyValuePair in atoms.proto
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700195 parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800196 break;
197 case INT64_TYPE:
198 pos[2] = 3;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700199 parseInt64(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800200 break;
201 case STRING_TYPE:
202 pos[2] = 4;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700203 parseString(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800204 break;
205 case FLOAT_TYPE:
206 pos[2] = 5;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700207 parseFloat(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800208 break;
209 default:
210 mValid = false;
211 }
212 }
213
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700214 parseAnnotations(numAnnotations);
215
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800216 pos[1] = pos[2] = 1;
217 last[1] = last[2] = false;
218}
219
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700220void LogEvent::parseAttributionChain(int32_t* pos, int32_t depth, bool* last,
221 uint8_t numAnnotations) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700222 int firstUidInChainIndex = mValues.size();
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800223 int32_t numNodes = readNextValue<uint8_t>();
224 for (pos[1] = 1; pos[1] <= numNodes; pos[1]++) {
225 last[1] = (pos[1] == numNodes);
226
227 // parse uid
228 pos[2] = 1;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700229 parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800230
231 // parse tag
232 pos[2] = 2;
233 last[2] = true;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700234 parseString(pos, /*depth=*/2, last, /*numAnnotations=*/0);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800235 }
236
Ruchir Rastogi13296512020-03-24 10:59:49 -0700237 parseAnnotations(numAnnotations, firstUidInChainIndex);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700238
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800239 pos[1] = pos[2] = 1;
240 last[1] = last[2] = false;
241}
242
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700243// Assumes that mValues is not empty
244bool LogEvent::checkPreviousValueType(Type expected) {
245 return mValues[mValues.size() - 1].mValue.getType() == expected;
246}
247
Ruchir Rastogi13296512020-03-24 10:59:49 -0700248void LogEvent::parseIsUidAnnotation(uint8_t annotationType) {
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700249 if (mValues.empty() || !checkPreviousValueType(INT) || annotationType != BOOL_TYPE) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700250 mValid = false;
251 return;
252 }
253
254 bool isUid = readNextValue<uint8_t>();
255 if (isUid) mUidFieldIndex = mValues.size() - 1;
Ruchir Rastogiffa34f02020-04-04 15:30:11 -0700256 mValues[mValues.size() - 1].mAnnotations.setUidField(isUid);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700257}
258
259void LogEvent::parseTruncateTimestampAnnotation(uint8_t annotationType) {
260 if (!mValues.empty() || annotationType != BOOL_TYPE) {
261 mValid = false;
262 return;
263 }
264
265 mTruncateTimestamp = readNextValue<uint8_t>();
266}
267
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700268void LogEvent::parsePrimaryFieldAnnotation(uint8_t annotationType) {
269 if (mValues.empty() || annotationType != BOOL_TYPE) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700270 mValid = false;
271 return;
272 }
273
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700274 const bool primaryField = readNextValue<uint8_t>();
275 mValues[mValues.size() - 1].mAnnotations.setPrimaryField(primaryField);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700276}
277
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700278void LogEvent::parsePrimaryFieldFirstUidAnnotation(uint8_t annotationType,
279 int firstUidInChainIndex) {
280 if (mValues.empty() || annotationType != BOOL_TYPE || -1 == firstUidInChainIndex) {
281 mValid = false;
282 return;
283 }
284
285 const bool primaryField = readNextValue<uint8_t>();
286 mValues[firstUidInChainIndex].mAnnotations.setPrimaryField(primaryField);
287}
288
289void LogEvent::parseExclusiveStateAnnotation(uint8_t annotationType) {
290 if (mValues.empty() || annotationType != BOOL_TYPE) {
291 mValid = false;
292 return;
293 }
294
295 const bool exclusiveState = readNextValue<uint8_t>();
296 mValues[mValues.size() - 1].mAnnotations.setExclusiveState(exclusiveState);
297}
298
299void LogEvent::parseTriggerStateResetAnnotation(uint8_t annotationType) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700300 if (mValues.empty() || annotationType != INT32_TYPE) {
301 mValid = false;
302 return;
303 }
304
305 int32_t resetState = readNextValue<int32_t>();
306 mValues[mValues.size() - 1].mAnnotations.setResetState(resetState);
307}
308
309void LogEvent::parseStateNestedAnnotation(uint8_t annotationType) {
310 if (mValues.empty() || annotationType != BOOL_TYPE) {
311 mValid = false;
312 return;
313 }
314
315 bool nested = readNextValue<uint8_t>();
316 mValues[mValues.size() - 1].mAnnotations.setNested(nested);
317}
318
319// firstUidInChainIndex is a default parameter that is only needed when parsing
320// annotations for attribution chains.
321void LogEvent::parseAnnotations(uint8_t numAnnotations, int firstUidInChainIndex) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700322 for (uint8_t i = 0; i < numAnnotations; i++) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700323 uint8_t annotationId = readNextValue<uint8_t>();
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700324 uint8_t annotationType = readNextValue<uint8_t>();
Ruchir Rastogi13296512020-03-24 10:59:49 -0700325
326 switch (annotationId) {
327 case ANNOTATION_ID_IS_UID:
328 parseIsUidAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700329 break;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700330 case ANNOTATION_ID_TRUNCATE_TIMESTAMP:
331 parseTruncateTimestampAnnotation(annotationType);
332 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700333 case ANNOTATION_ID_PRIMARY_FIELD:
334 parsePrimaryFieldAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700335 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700336 case ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID:
337 parsePrimaryFieldFirstUidAnnotation(annotationType, firstUidInChainIndex);
338 break;
339 case ANNOTATION_ID_EXCLUSIVE_STATE:
340 parseExclusiveStateAnnotation(annotationType);
341 break;
342 case ANNOTATION_ID_TRIGGER_STATE_RESET:
343 parseTriggerStateResetAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700344 break;
345 case ANNOTATION_ID_STATE_NESTED:
346 parseStateNestedAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700347 break;
348 default:
349 mValid = false;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700350 return;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700351 }
352 }
353}
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800354
355// This parsing logic is tied to the encoding scheme used in StatsEvent.java and
356// stats_event.c
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800357bool LogEvent::parseBuffer(uint8_t* buf, size_t len) {
358 mBuf = buf;
359 mRemainingLen = (uint32_t)len;
360
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800361 int32_t pos[] = {1, 1, 1};
362 bool last[] = {false, false, false};
363
364 // Beginning of buffer is OBJECT_TYPE | NUM_FIELDS | TIMESTAMP | ATOM_ID
365 uint8_t typeInfo = readNextValue<uint8_t>();
366 if (getTypeId(typeInfo) != OBJECT_TYPE) mValid = false;
367
368 uint8_t numElements = readNextValue<uint8_t>();
369 if (numElements < 2 || numElements > 127) mValid = false;
370
371 typeInfo = readNextValue<uint8_t>();
372 if (getTypeId(typeInfo) != INT64_TYPE) mValid = false;
373 mElapsedTimestampNs = readNextValue<int64_t>();
374 numElements--;
375
376 typeInfo = readNextValue<uint8_t>();
377 if (getTypeId(typeInfo) != INT32_TYPE) mValid = false;
378 mTagId = readNextValue<int32_t>();
379 numElements--;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700380 parseAnnotations(getNumAnnotations(typeInfo)); // atom-level annotations
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800381
382 for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700383 last[0] = (pos[0] == numElements);
384
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800385 typeInfo = readNextValue<uint8_t>();
386 uint8_t typeId = getTypeId(typeInfo);
387
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800388 // TODO(b/144373276): handle errors passed to the socket
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700389 switch (typeId) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800390 case BOOL_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700391 parseBool(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800392 break;
393 case INT32_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700394 parseInt32(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800395 break;
396 case INT64_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700397 parseInt64(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800398 break;
399 case FLOAT_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700400 parseFloat(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800401 break;
402 case BYTE_ARRAY_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700403 parseByteArray(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800404 break;
405 case STRING_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700406 parseString(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800407 break;
408 case KEY_VALUE_PAIRS_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700409 parseKeyValuePairs(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800410 break;
411 case ATTRIBUTION_CHAIN_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700412 parseAttributionChain(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi13296512020-03-24 10:59:49 -0700413 if (mAttributionChainIndex == -1) mAttributionChainIndex = pos[0];
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800414 break;
415 default:
416 mValid = false;
417 }
418 }
419
420 if (mRemainingLen != 0) mValid = false;
421 mBuf = nullptr;
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800422 return mValid;
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800423}
424
425uint8_t LogEvent::getTypeId(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700426 return typeInfo & 0x0F; // type id in lower 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800427}
428
429uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700430 return (typeInfo >> 4) & 0x0F; // num annotations in upper 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800431}
432
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700433int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700434 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800435 int field = getSimpleField(key);
436 for (const auto& value : mValues) {
437 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800438 if (value.mValue.getType() == LONG) {
439 return value.mValue.long_value;
440 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800441 return value.mValue.int_value;
442 } else {
443 *err = BAD_TYPE;
444 return 0;
445 }
446 }
447 if ((size_t)value.mField.getPosAtDepth(0) > key) {
448 break;
Yangster-mac20877162017-12-22 17:19:39 -0800449 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700450 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800451
452 *err = BAD_INDEX;
453 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700454}
455
Chenjie Yu80f91122018-01-31 20:24:50 -0800456int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800457 int field = getSimpleField(key);
458 for (const auto& value : mValues) {
459 if (value.mField.getField() == field) {
460 if (value.mValue.getType() == INT) {
461 return value.mValue.int_value;
462 } else {
463 *err = BAD_TYPE;
464 return 0;
465 }
466 }
467 if ((size_t)value.mField.getPosAtDepth(0) > key) {
468 break;
469 }
470 }
471
Chenjie Yu80f91122018-01-31 20:24:50 -0800472 *err = BAD_INDEX;
473 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800474}
475
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700476const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800477 int field = getSimpleField(key);
478 for (const auto& value : mValues) {
479 if (value.mField.getField() == field) {
480 if (value.mValue.getType() == STRING) {
481 return value.mValue.str_value.c_str();
482 } else {
483 *err = BAD_TYPE;
484 return 0;
485 }
486 }
487 if ((size_t)value.mField.getPosAtDepth(0) > key) {
488 break;
Yangster-mac20877162017-12-22 17:19:39 -0800489 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700490 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800491
492 *err = BAD_INDEX;
493 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700494}
495
496bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800497 int field = getSimpleField(key);
498 for (const auto& value : mValues) {
499 if (value.mField.getField() == field) {
500 if (value.mValue.getType() == INT) {
501 return value.mValue.int_value != 0;
502 } else if (value.mValue.getType() == LONG) {
503 return value.mValue.long_value != 0;
504 } else {
505 *err = BAD_TYPE;
506 return false;
507 }
508 }
509 if ((size_t)value.mField.getPosAtDepth(0) > key) {
510 break;
Yangster-mac20877162017-12-22 17:19:39 -0800511 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700512 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800513
514 *err = BAD_INDEX;
515 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700516}
517
518float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800519 int field = getSimpleField(key);
520 for (const auto& value : mValues) {
521 if (value.mField.getField() == field) {
522 if (value.mValue.getType() == FLOAT) {
523 return value.mValue.float_value;
524 } else {
525 *err = BAD_TYPE;
526 return 0.0;
527 }
528 }
529 if ((size_t)value.mField.getPosAtDepth(0) > key) {
530 break;
Yangster-mac20877162017-12-22 17:19:39 -0800531 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700532 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700533
Yao Chen8a8d16c2018-02-08 14:50:40 -0800534 *err = BAD_INDEX;
535 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800536}
537
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800538std::vector<uint8_t> LogEvent::GetStorage(size_t key, status_t* err) const {
539 int field = getSimpleField(key);
540 for (const auto& value : mValues) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700541 if (value.mField.getField() == field) {
542 if (value.mValue.getType() == STORAGE) {
543 return value.mValue.storage_value;
544 } else {
545 *err = BAD_TYPE;
546 return vector<uint8_t>();
547 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800548 }
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700549 if ((size_t)value.mField.getPosAtDepth(0) > key) {
550 break;
551 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800552 }
553
554 *err = BAD_INDEX;
555 return vector<uint8_t>();
556}
557
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700558string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800559 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800560 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800561 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800562 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800563 result +=
564 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700565 }
Yao Chen20e9e622018-02-28 11:18:51 -0800566 result += " }";
567 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700568}
569
Yangster-mac20877162017-12-22 17:19:39 -0800570void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800571 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700572}
573
Tej Singh9b4a5ec2019-04-25 12:43:35 -0700574void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds,
575 std::vector<uint8_t>* protoOut) {
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400576 ProtoOutputStream proto;
577 for (const auto& expId : experimentIds) {
578 proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
579 (long long)expId);
580 }
581
582 protoOut->resize(proto.size());
583 size_t pos = 0;
584 sp<ProtoReader> reader = proto.data();
585 while (reader->readBuffer() != NULL) {
586 size_t toRead = reader->currentToRead();
587 std::memcpy(protoOut->data() + pos, reader->readBuffer(), toRead);
588 pos += toRead;
589 reader->move(toRead);
590 }
591}
592
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700593} // namespace statsd
594} // namespace os
595} // namespace android