blob: 96bf04f4f6d6103b479b00a25d2fc7969f48629b [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 Rastogi13296512020-03-24 10:59:49 -0700243void LogEvent::parseIsUidAnnotation(uint8_t annotationType) {
244 if (mValues.empty() || annotationType != BOOL_TYPE) {
245 mValid = false;
246 return;
247 }
248
249 bool isUid = readNextValue<uint8_t>();
250 if (isUid) mUidFieldIndex = mValues.size() - 1;
251}
252
253void LogEvent::parseTruncateTimestampAnnotation(uint8_t annotationType) {
254 if (!mValues.empty() || annotationType != BOOL_TYPE) {
255 mValid = false;
256 return;
257 }
258
259 mTruncateTimestamp = readNextValue<uint8_t>();
260}
261
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700262void LogEvent::parsePrimaryFieldAnnotation(uint8_t annotationType) {
263 if (mValues.empty() || annotationType != BOOL_TYPE) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700264 mValid = false;
265 return;
266 }
267
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700268 const bool primaryField = readNextValue<uint8_t>();
269 mValues[mValues.size() - 1].mAnnotations.setPrimaryField(primaryField);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700270}
271
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700272void LogEvent::parsePrimaryFieldFirstUidAnnotation(uint8_t annotationType,
273 int firstUidInChainIndex) {
274 if (mValues.empty() || annotationType != BOOL_TYPE || -1 == firstUidInChainIndex) {
275 mValid = false;
276 return;
277 }
278
279 const bool primaryField = readNextValue<uint8_t>();
280 mValues[firstUidInChainIndex].mAnnotations.setPrimaryField(primaryField);
281}
282
283void LogEvent::parseExclusiveStateAnnotation(uint8_t annotationType) {
284 if (mValues.empty() || annotationType != BOOL_TYPE) {
285 mValid = false;
286 return;
287 }
288
289 const bool exclusiveState = readNextValue<uint8_t>();
290 mValues[mValues.size() - 1].mAnnotations.setExclusiveState(exclusiveState);
291}
292
293void LogEvent::parseTriggerStateResetAnnotation(uint8_t annotationType) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700294 if (mValues.empty() || annotationType != INT32_TYPE) {
295 mValid = false;
296 return;
297 }
298
299 int32_t resetState = readNextValue<int32_t>();
300 mValues[mValues.size() - 1].mAnnotations.setResetState(resetState);
301}
302
303void LogEvent::parseStateNestedAnnotation(uint8_t annotationType) {
304 if (mValues.empty() || annotationType != BOOL_TYPE) {
305 mValid = false;
306 return;
307 }
308
309 bool nested = readNextValue<uint8_t>();
310 mValues[mValues.size() - 1].mAnnotations.setNested(nested);
311}
312
313// firstUidInChainIndex is a default parameter that is only needed when parsing
314// annotations for attribution chains.
315void LogEvent::parseAnnotations(uint8_t numAnnotations, int firstUidInChainIndex) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700316 for (uint8_t i = 0; i < numAnnotations; i++) {
Ruchir Rastogi13296512020-03-24 10:59:49 -0700317 uint8_t annotationId = readNextValue<uint8_t>();
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700318 uint8_t annotationType = readNextValue<uint8_t>();
Ruchir Rastogi13296512020-03-24 10:59:49 -0700319
320 switch (annotationId) {
321 case ANNOTATION_ID_IS_UID:
322 parseIsUidAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700323 break;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700324 case ANNOTATION_ID_TRUNCATE_TIMESTAMP:
325 parseTruncateTimestampAnnotation(annotationType);
326 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700327 case ANNOTATION_ID_PRIMARY_FIELD:
328 parsePrimaryFieldAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700329 break;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700330 case ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID:
331 parsePrimaryFieldFirstUidAnnotation(annotationType, firstUidInChainIndex);
332 break;
333 case ANNOTATION_ID_EXCLUSIVE_STATE:
334 parseExclusiveStateAnnotation(annotationType);
335 break;
336 case ANNOTATION_ID_TRIGGER_STATE_RESET:
337 parseTriggerStateResetAnnotation(annotationType);
Ruchir Rastogi13296512020-03-24 10:59:49 -0700338 break;
339 case ANNOTATION_ID_STATE_NESTED:
340 parseStateNestedAnnotation(annotationType);
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700341 break;
342 default:
343 mValid = false;
Ruchir Rastogi13296512020-03-24 10:59:49 -0700344 return;
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700345 }
346 }
347}
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800348
349// This parsing logic is tied to the encoding scheme used in StatsEvent.java and
350// stats_event.c
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800351bool LogEvent::parseBuffer(uint8_t* buf, size_t len) {
352 mBuf = buf;
353 mRemainingLen = (uint32_t)len;
354
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800355 int32_t pos[] = {1, 1, 1};
356 bool last[] = {false, false, false};
357
358 // Beginning of buffer is OBJECT_TYPE | NUM_FIELDS | TIMESTAMP | ATOM_ID
359 uint8_t typeInfo = readNextValue<uint8_t>();
360 if (getTypeId(typeInfo) != OBJECT_TYPE) mValid = false;
361
362 uint8_t numElements = readNextValue<uint8_t>();
363 if (numElements < 2 || numElements > 127) mValid = false;
364
365 typeInfo = readNextValue<uint8_t>();
366 if (getTypeId(typeInfo) != INT64_TYPE) mValid = false;
367 mElapsedTimestampNs = readNextValue<int64_t>();
368 numElements--;
369
370 typeInfo = readNextValue<uint8_t>();
371 if (getTypeId(typeInfo) != INT32_TYPE) mValid = false;
372 mTagId = readNextValue<int32_t>();
373 numElements--;
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700374 parseAnnotations(getNumAnnotations(typeInfo)); // atom-level annotations
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800375
376 for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) {
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700377 last[0] = (pos[0] == numElements);
378
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800379 typeInfo = readNextValue<uint8_t>();
380 uint8_t typeId = getTypeId(typeInfo);
381
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800382 // TODO(b/144373276): handle errors passed to the socket
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700383 switch (typeId) {
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800384 case BOOL_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700385 parseBool(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800386 break;
387 case INT32_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700388 parseInt32(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800389 break;
390 case INT64_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700391 parseInt64(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800392 break;
393 case FLOAT_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700394 parseFloat(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800395 break;
396 case BYTE_ARRAY_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700397 parseByteArray(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800398 break;
399 case STRING_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700400 parseString(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800401 break;
402 case KEY_VALUE_PAIRS_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700403 parseKeyValuePairs(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800404 break;
405 case ATTRIBUTION_CHAIN_TYPE:
Ruchir Rastogi84eb44b2020-03-12 18:48:48 -0700406 parseAttributionChain(pos, /*depth=*/0, last, getNumAnnotations(typeInfo));
Ruchir Rastogi13296512020-03-24 10:59:49 -0700407 if (mAttributionChainIndex == -1) mAttributionChainIndex = pos[0];
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800408 break;
409 default:
410 mValid = false;
411 }
412 }
413
414 if (mRemainingLen != 0) mValid = false;
415 mBuf = nullptr;
Ruchir Rastogidfd63d42020-02-20 17:54:13 -0800416 return mValid;
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800417}
418
419uint8_t LogEvent::getTypeId(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700420 return typeInfo & 0x0F; // type id in lower 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800421}
422
423uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700424 return (typeInfo >> 4) & 0x0F; // num annotations in upper 4 bytes
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800425}
426
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700427int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700428 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800429 int field = getSimpleField(key);
430 for (const auto& value : mValues) {
431 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800432 if (value.mValue.getType() == LONG) {
433 return value.mValue.long_value;
434 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800435 return value.mValue.int_value;
436 } else {
437 *err = BAD_TYPE;
438 return 0;
439 }
440 }
441 if ((size_t)value.mField.getPosAtDepth(0) > key) {
442 break;
Yangster-mac20877162017-12-22 17:19:39 -0800443 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700444 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800445
446 *err = BAD_INDEX;
447 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700448}
449
Chenjie Yu80f91122018-01-31 20:24:50 -0800450int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800451 int field = getSimpleField(key);
452 for (const auto& value : mValues) {
453 if (value.mField.getField() == field) {
454 if (value.mValue.getType() == INT) {
455 return value.mValue.int_value;
456 } else {
457 *err = BAD_TYPE;
458 return 0;
459 }
460 }
461 if ((size_t)value.mField.getPosAtDepth(0) > key) {
462 break;
463 }
464 }
465
Chenjie Yu80f91122018-01-31 20:24:50 -0800466 *err = BAD_INDEX;
467 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800468}
469
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700470const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800471 int field = getSimpleField(key);
472 for (const auto& value : mValues) {
473 if (value.mField.getField() == field) {
474 if (value.mValue.getType() == STRING) {
475 return value.mValue.str_value.c_str();
476 } else {
477 *err = BAD_TYPE;
478 return 0;
479 }
480 }
481 if ((size_t)value.mField.getPosAtDepth(0) > key) {
482 break;
Yangster-mac20877162017-12-22 17:19:39 -0800483 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700484 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800485
486 *err = BAD_INDEX;
487 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700488}
489
490bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800491 int field = getSimpleField(key);
492 for (const auto& value : mValues) {
493 if (value.mField.getField() == field) {
494 if (value.mValue.getType() == INT) {
495 return value.mValue.int_value != 0;
496 } else if (value.mValue.getType() == LONG) {
497 return value.mValue.long_value != 0;
498 } else {
499 *err = BAD_TYPE;
500 return false;
501 }
502 }
503 if ((size_t)value.mField.getPosAtDepth(0) > key) {
504 break;
Yangster-mac20877162017-12-22 17:19:39 -0800505 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700506 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800507
508 *err = BAD_INDEX;
509 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700510}
511
512float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800513 int field = getSimpleField(key);
514 for (const auto& value : mValues) {
515 if (value.mField.getField() == field) {
516 if (value.mValue.getType() == FLOAT) {
517 return value.mValue.float_value;
518 } else {
519 *err = BAD_TYPE;
520 return 0.0;
521 }
522 }
523 if ((size_t)value.mField.getPosAtDepth(0) > key) {
524 break;
Yangster-mac20877162017-12-22 17:19:39 -0800525 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700526 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700527
Yao Chen8a8d16c2018-02-08 14:50:40 -0800528 *err = BAD_INDEX;
529 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800530}
531
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800532std::vector<uint8_t> LogEvent::GetStorage(size_t key, status_t* err) const {
533 int field = getSimpleField(key);
534 for (const auto& value : mValues) {
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700535 if (value.mField.getField() == field) {
536 if (value.mValue.getType() == STORAGE) {
537 return value.mValue.storage_value;
538 } else {
539 *err = BAD_TYPE;
540 return vector<uint8_t>();
541 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800542 }
Muhammad Qureshi3f9c3302020-04-01 16:11:53 -0700543 if ((size_t)value.mField.getPosAtDepth(0) > key) {
544 break;
545 }
Jonathan Nguyena0e6de12020-01-28 18:33:55 -0800546 }
547
548 *err = BAD_INDEX;
549 return vector<uint8_t>();
550}
551
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700552string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800553 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800554 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800555 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800556 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800557 result +=
558 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700559 }
Yao Chen20e9e622018-02-28 11:18:51 -0800560 result += " }";
561 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700562}
563
Yangster-mac20877162017-12-22 17:19:39 -0800564void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800565 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700566}
567
Tej Singh9b4a5ec2019-04-25 12:43:35 -0700568void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds,
569 std::vector<uint8_t>* protoOut) {
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400570 ProtoOutputStream proto;
571 for (const auto& expId : experimentIds) {
572 proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
573 (long long)expId);
574 }
575
576 protoOut->resize(proto.size());
577 size_t pos = 0;
578 sp<ProtoReader> reader = proto.data();
579 while (reader->readBuffer() != NULL) {
580 size_t toRead = reader->currentToRead();
581 std::memcpy(protoOut->data() + pos, reader->readBuffer(), toRead);
582 pos += toRead;
583 reader->move(toRead);
584 }
585}
586
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700587} // namespace statsd
588} // namespace os
589} // namespace android