blob: 61cd01728ab1d6c019eebb3d8bd283fbacfb7287 [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 Rastogiffa34f052020-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 Rastogiffa34f052020-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 Rastogiffa34f052020-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>();
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700296 mExclusiveStateFieldIndex = mValues.size() - 1;
297 mValues[getExclusiveStateFieldIndex()].mAnnotations.setExclusiveState(exclusiveState);
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700298}
299
300void LogEvent::parseTriggerStateResetAnnotation(uint8_t annotationType) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700301 if (mValues.empty() || annotationType != INT32_TYPE) {
302 mValid = false;
303 return;
304 }
305
306 int32_t resetState = readNextValue<int32_t>();
307 mValues[mValues.size() - 1].mAnnotations.setResetState(resetState);
308}
309
310void LogEvent::parseStateNestedAnnotation(uint8_t annotationType) {
311 if (mValues.empty() || annotationType != BOOL_TYPE) {
312 mValid = false;
313 return;
314 }
315
316 bool nested = readNextValue<uint8_t>();
317 mValues[mValues.size() - 1].mAnnotations.setNested(nested);
318}
319
320// firstUidInChainIndex is a default parameter that is only needed when parsing
321// annotations for attribution chains.
322void LogEvent::parseAnnotations(uint8_t numAnnotations, int firstUidInChainIndex) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700323 for (uint8_t i = 0; i < numAnnotations; i++) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700324 uint8_t annotationId = readNextValue<uint8_t>();
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700325 uint8_t annotationType = readNextValue<uint8_t>();
Ruchir Rastogi13296512020-03-24 10:59:49 -0700326
327 switch (annotationId) {
328 case ANNOTATION_ID_IS_UID:
329 parseIsUidAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700330 break;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700331 case ANNOTATION_ID_TRUNCATE_TIMESTAMP:
332 parseTruncateTimestampAnnotation(annotationType);
333 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700334 case ANNOTATION_ID_PRIMARY_FIELD:
335 parsePrimaryFieldAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700336 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700337 case ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID:
338 parsePrimaryFieldFirstUidAnnotation(annotationType, firstUidInChainIndex);
339 break;
340 case ANNOTATION_ID_EXCLUSIVE_STATE:
341 parseExclusiveStateAnnotation(annotationType);
342 break;
343 case ANNOTATION_ID_TRIGGER_STATE_RESET:
344 parseTriggerStateResetAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700345 break;
346 case ANNOTATION_ID_STATE_NESTED:
347 parseStateNestedAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700348 break;
349 default:
350 mValid = false;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700351 return;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700352 }
353 }
354}
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800355
356// This parsing logic is tied to the encoding scheme used in StatsEvent.java and
357// stats_event.c
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800358bool LogEvent::parseBuffer(uint8_t* buf, size_t len) {
359 mBuf = buf;
360 mRemainingLen = (uint32_t)len;
361
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800362 int32_t pos[] = {1, 1, 1};
363 bool last[] = {false, false, false};
364
365 // Beginning of buffer is OBJECT_TYPE | NUM_FIELDS | TIMESTAMP | ATOM_ID
366 uint8_t typeInfo = readNextValue<uint8_t>();
367 if (getTypeId(typeInfo) != OBJECT_TYPE) mValid = false;
368
369 uint8_t numElements = readNextValue<uint8_t>();
370 if (numElements < 2 || numElements > 127) mValid = false;
371
372 typeInfo = readNextValue<uint8_t>();
373 if (getTypeId(typeInfo) != INT64_TYPE) mValid = false;
374 mElapsedTimestampNs = readNextValue<int64_t>();
375 numElements--;
376
377 typeInfo = readNextValue<uint8_t>();
378 if (getTypeId(typeInfo) != INT32_TYPE) mValid = false;
379 mTagId = readNextValue<int32_t>();
380 numElements--;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700381 parseAnnotations(getNumAnnotations(typeInfo)); // atom-level annotations
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800382
383 for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700384 last[0] = (pos[0] == numElements);
385
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800386 typeInfo = readNextValue<uint8_t>();
387 uint8_t typeId = getTypeId(typeInfo);
388
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800389 // TODO(b/144373276): handle errors passed to the socket
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700390 switch (typeId) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800391 case BOOL_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700392 parseBool(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800393 break;
394 case INT32_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700395 parseInt32(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800396 break;
397 case INT64_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700398 parseInt64(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800399 break;
400 case FLOAT_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700401 parseFloat(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800402 break;
403 case BYTE_ARRAY_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700404 parseByteArray(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800405 break;
406 case STRING_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700407 parseString(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800408 break;
409 case KEY_VALUE_PAIRS_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700410 parseKeyValuePairs(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800411 break;
412 case ATTRIBUTION_CHAIN_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700413 parseAttributionChain(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi13296512020-03-24 10:59:49 -0700414 if (mAttributionChainIndex == -1) mAttributionChainIndex = pos[0];
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800415 break;
416 default:
417 mValid = false;
418 }
419 }
420
421 if (mRemainingLen != 0) mValid = false;
422 mBuf = nullptr;
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800423 return mValid;
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800424}
425
426uint8_t LogEvent::getTypeId(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700427 return typeInfo & 0x0F; // type id in lower 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800428}
429
430uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700431 return (typeInfo >> 4) & 0x0F; // num annotations in upper 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800432}
433
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700434int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700435 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800436 int field = getSimpleField(key);
437 for (const auto& value : mValues) {
438 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800439 if (value.mValue.getType() == LONG) {
440 return value.mValue.long_value;
441 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800442 return value.mValue.int_value;
443 } else {
444 *err = BAD_TYPE;
445 return 0;
446 }
447 }
448 if ((size_t)value.mField.getPosAtDepth(0) > key) {
449 break;
Yangster-mac20877162017-12-22 17:19:39 -0800450 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700451 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800452
453 *err = BAD_INDEX;
454 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700455}
456
Chenjie Yu80f91122018-01-31 20:24:50 -0800457int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800458 int field = getSimpleField(key);
459 for (const auto& value : mValues) {
460 if (value.mField.getField() == field) {
461 if (value.mValue.getType() == INT) {
462 return value.mValue.int_value;
463 } else {
464 *err = BAD_TYPE;
465 return 0;
466 }
467 }
468 if ((size_t)value.mField.getPosAtDepth(0) > key) {
469 break;
470 }
471 }
472
Chenjie Yu80f91122018-01-31 20:24:50 -0800473 *err = BAD_INDEX;
474 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800475}
476
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700477const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800478 int field = getSimpleField(key);
479 for (const auto& value : mValues) {
480 if (value.mField.getField() == field) {
481 if (value.mValue.getType() == STRING) {
482 return value.mValue.str_value.c_str();
483 } else {
484 *err = BAD_TYPE;
485 return 0;
486 }
487 }
488 if ((size_t)value.mField.getPosAtDepth(0) > key) {
489 break;
Yangster-mac20877162017-12-22 17:19:39 -0800490 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700491 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800492
493 *err = BAD_INDEX;
494 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700495}
496
497bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800498 int field = getSimpleField(key);
499 for (const auto& value : mValues) {
500 if (value.mField.getField() == field) {
501 if (value.mValue.getType() == INT) {
502 return value.mValue.int_value != 0;
503 } else if (value.mValue.getType() == LONG) {
504 return value.mValue.long_value != 0;
505 } else {
506 *err = BAD_TYPE;
507 return false;
508 }
509 }
510 if ((size_t)value.mField.getPosAtDepth(0) > key) {
511 break;
Yangster-mac20877162017-12-22 17:19:39 -0800512 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700513 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800514
515 *err = BAD_INDEX;
516 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700517}
518
519float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800520 int field = getSimpleField(key);
521 for (const auto& value : mValues) {
522 if (value.mField.getField() == field) {
523 if (value.mValue.getType() == FLOAT) {
524 return value.mValue.float_value;
525 } else {
526 *err = BAD_TYPE;
527 return 0.0;
528 }
529 }
530 if ((size_t)value.mField.getPosAtDepth(0) > key) {
531 break;
Yangster-mac20877162017-12-22 17:19:39 -0800532 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700533 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700534
Yao Chen8a8d16c2018-02-08 14:50:40 -0800535 *err = BAD_INDEX;
536 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800537}
538
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800539std::vector<uint8_t> LogEvent::GetStorage(size_t key, status_t* err) const {
540 int field = getSimpleField(key);
541 for (const auto& value : mValues) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700542 if (value.mField.getField() == field) {
543 if (value.mValue.getType() == STORAGE) {
544 return value.mValue.storage_value;
545 } else {
546 *err = BAD_TYPE;
547 return vector<uint8_t>();
548 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800549 }
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700550 if ((size_t)value.mField.getPosAtDepth(0) > key) {
551 break;
552 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800553 }
554
555 *err = BAD_INDEX;
556 return vector<uint8_t>();
557}
558
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700559string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800560 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800561 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800562 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800563 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800564 result +=
565 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700566 }
Yao Chen20e9e622018-02-28 11:18:51 -0800567 result += " }";
568 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700569}
570
Yangster-mac20877162017-12-22 17:19:39 -0800571void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800572 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700573}
574
Tej Singh9b4a5ec2019-04-25 12:43:35 -0700575void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds,
576 std::vector<uint8_t>* protoOut) {
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400577 ProtoOutputStream proto;
578 for (const auto& expId : experimentIds) {
579 proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
580 (long long)expId);
581 }
582
583 protoOut->resize(proto.size());
584 size_t pos = 0;
585 sp<ProtoReader> reader = proto.data();
586 while (reader->readBuffer() != NULL) {
587 size_t toRead = reader->currentToRead();
588 std::memcpy(protoOut->data() + pos, reader->readBuffer(), toRead);
589 pos += toRead;
590 reader->move(toRead);
591 }
592}
593
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700594} // namespace statsd
595} // namespace os
596} // namespace android