blob: 36f4623c4dcb8f799c5ff69429d7fe86795a77a2 [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
Yangster-mac20877162017-12-22 17:19:39 -080020#include "stats_log_util.h"
Yangster-mac48b3d622018-08-18 12:38:11 -070021#include "statslog.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070022
Chenjie Yu6b1667c2019-01-18 10:09:33 -080023#include <binder/IPCThreadState.h>
Tej Singh8928ed72019-02-22 19:06:22 -080024#include <private/android_filesystem_config.h>
Chenjie Yu6b1667c2019-01-18 10:09:33 -080025
Joe Onoratoc4dfae52017-10-17 23:38:21 -070026namespace android {
27namespace os {
28namespace statsd {
29
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -040030// for TrainInfo experiment id serialization
31const int FIELD_ID_EXPERIMENT_ID = 1;
32
yro24809bd2017-10-31 23:06:53 -070033using namespace android::util;
Yao Chen9c1debe2018-02-19 14:39:19 -080034using android::util::ProtoOutputStream;
David Chen1481fe12017-10-16 13:16:34 -070035using std::string;
Yao Chen9c1debe2018-02-19 14:39:19 -080036using std::vector;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070037
Ruchir Rastogi1736ba42019-11-04 14:37:13 -080038// Msg is expected to begin at the start of the serialized atom -- it should not
39// include the android_log_header_t or the StatsEventTag.
40LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid)
41 : mBuf(msg),
42 mRemainingLen(len),
43 mLogdTimestampNs(time(nullptr)),
44 mLogUid(uid)
45{
46#ifdef NEW_ENCODING_SCHEME
47 initNew();
48# else
49 mContext = create_android_log_parser((char*)msg, len);
Chenjie Yu3ca36832018-01-22 15:10:54 -080050 init(mContext);
Ruchir Rastogi1736ba42019-11-04 14:37:13 -080051 if (mContext) android_log_destroy(&mContext); // set mContext to NULL
52#endif
Joe Onoratoc4dfae52017-10-17 23:38:21 -070053}
54
Tej Singh89817632019-12-09 16:58:08 -080055LogEvent::LogEvent(uint8_t* msg, uint32_t len, uint32_t uid, bool useNewSchema)
56 : mBuf(msg), mRemainingLen(len), mLogdTimestampNs(time(nullptr)), mLogUid(uid) {
57 if (useNewSchema) {
58 initNew();
59 } else {
60 mContext = create_android_log_parser((char*)msg, len);
61 init(mContext);
62 if (mContext) android_log_destroy(&mContext); // set mContext to NULL
63 }
64}
65
Chenjie Yu0bd73db2018-12-16 07:37:04 -080066LogEvent::LogEvent(const LogEvent& event) {
67 mTagId = event.mTagId;
68 mLogUid = event.mLogUid;
69 mElapsedTimestampNs = event.mElapsedTimestampNs;
70 mLogdTimestampNs = event.mLogdTimestampNs;
71 mValues = event.mValues;
72}
73
Chenjie Yud7e3a222018-11-28 21:29:44 +000074LogEvent::LogEvent(const StatsLogEventWrapper& statsLogEventWrapper, int workChainIndex) {
Chenjie Yu12e5e672018-09-14 15:54:59 -070075 mTagId = statsLogEventWrapper.getTagId();
76 mLogdTimestampNs = statsLogEventWrapper.getWallClockTimeNs();
77 mElapsedTimestampNs = statsLogEventWrapper.getElapsedRealTimeNs();
78 mLogUid = 0;
Chenjie Yud7e3a222018-11-28 21:29:44 +000079 int workChainPosOffset = 0;
80 if (workChainIndex != -1) {
81 const WorkChain& wc = statsLogEventWrapper.getWorkChains()[workChainIndex];
82 // chains are at field 1, level 2
83 int depth = 2;
84 for (int i = 0; i < (int)wc.uids.size(); i++) {
85 int pos[] = {1, i + 1, 1};
86 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.uids[i])));
87 pos[2]++;
88 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(wc.tags[i])));
89 mValues.back().mField.decorateLastPos(2);
90 }
91 mValues.back().mField.decorateLastPos(1);
92 workChainPosOffset = 1;
93 }
Chenjie Yu12e5e672018-09-14 15:54:59 -070094 for (int i = 0; i < (int)statsLogEventWrapper.getElements().size(); i++) {
Chenjie Yud7e3a222018-11-28 21:29:44 +000095 Field field(statsLogEventWrapper.getTagId(), getSimpleField(i + 1 + workChainPosOffset));
Chenjie Yu12e5e672018-09-14 15:54:59 -070096 switch (statsLogEventWrapper.getElements()[i].type) {
97 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::INT:
98 mValues.push_back(
99 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].int_value)));
100 break;
101 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::LONG:
102 mValues.push_back(
103 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].long_value)));
104 break;
105 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::FLOAT:
106 mValues.push_back(FieldValue(
107 field, Value(statsLogEventWrapper.getElements()[i].float_value)));
108 break;
109 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::DOUBLE:
110 mValues.push_back(FieldValue(
111 field, Value(statsLogEventWrapper.getElements()[i].double_value)));
112 break;
113 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STRING:
114 mValues.push_back(
115 FieldValue(field, Value(statsLogEventWrapper.getElements()[i].str_value)));
116 break;
117 case android::os::StatsLogValue::STATS_LOG_VALUE_TYPE::STORAGE:
118 mValues.push_back(FieldValue(
119 field, Value(statsLogEventWrapper.getElements()[i].storage_value)));
120 break;
121 default:
122 break;
123 }
124 }
125}
126
Chenjie Yud7e3a222018-11-28 21:29:44 +0000127void LogEvent::createLogEvents(const StatsLogEventWrapper& statsLogEventWrapper,
128 std::vector<std::shared_ptr<LogEvent>>& logEvents) {
129 if (statsLogEventWrapper.getWorkChains().size() == 0) {
130 logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, -1));
131 } else {
132 for (size_t i = 0; i < statsLogEventWrapper.getWorkChains().size(); i++) {
133 logEvents.push_back(std::make_shared<LogEvent>(statsLogEventWrapper, i));
134 }
135 }
136}
137
Yangster-mac330af582018-02-08 15:24:38 -0800138LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs) {
139 mLogdTimestampNs = wallClockTimestampNs;
Yao Chen0f861862019-03-27 11:51:15 -0700140 mElapsedTimestampNs = elapsedTimestampNs;
Yao Chen80235402017-11-13 20:42:25 -0800141 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -0800142 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -0800143 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
144 if (mContext) {
Yangster-mac330af582018-02-08 15:24:38 -0800145 android_log_write_int64(mContext, elapsedTimestampNs);
146 android_log_write_int32(mContext, tagId);
147 }
148}
149
Yangster-mac48b3d622018-08-18 12:38:11 -0700150LogEvent::LogEvent(int32_t tagId, int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
151 int32_t uid,
Howard Ro4078dd42018-09-27 17:41:08 -0700152 const std::map<int32_t, int32_t>& int_map,
153 const std::map<int32_t, int64_t>& long_map,
Yangster-mac48b3d622018-08-18 12:38:11 -0700154 const std::map<int32_t, std::string>& string_map,
155 const std::map<int32_t, float>& float_map) {
156 mLogdTimestampNs = wallClockTimestampNs;
157 mElapsedTimestampNs = elapsedTimestampNs;
158 mTagId = android::util::KEY_VALUE_PAIRS_ATOM;
159 mLogUid = uid;
160
161 int pos[] = {1, 1, 1};
162
163 mValues.push_back(FieldValue(Field(mTagId, pos, 0 /* depth */), Value(uid)));
164 pos[0]++;
165 for (const auto&itr : int_map) {
166 pos[2] = 1;
167 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
168 pos[2] = 2;
169 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
170 mValues.back().mField.decorateLastPos(2);
171 pos[1]++;
172 }
173
Howard Ro4078dd42018-09-27 17:41:08 -0700174 for (const auto&itr : long_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700175 pos[2] = 1;
176 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
177 pos[2] = 3;
178 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
179 mValues.back().mField.decorateLastPos(2);
180 pos[1]++;
181 }
182
Howard Ro4078dd42018-09-27 17:41:08 -0700183 for (const auto&itr : string_map) {
Yangster-mac48b3d622018-08-18 12:38:11 -0700184 pos[2] = 1;
185 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
186 pos[2] = 4;
187 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
188 mValues.back().mField.decorateLastPos(2);
189 pos[1]++;
190 }
Howard Ro4078dd42018-09-27 17:41:08 -0700191
192 for (const auto&itr : float_map) {
193 pos[2] = 1;
194 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.first)));
195 pos[2] = 5;
196 mValues.push_back(FieldValue(Field(mTagId, pos, 2 /* depth */), Value(itr.second)));
197 mValues.back().mField.decorateLastPos(2);
198 pos[1]++;
199 }
Yangster-mac48b3d622018-08-18 12:38:11 -0700200 if (!mValues.empty()) {
201 mValues.back().mField.decorateLastPos(1);
202 mValues.at(mValues.size() - 2).mField.decorateLastPos(1);
203 }
204}
205
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800206LogEvent::LogEvent(const string& trainName, int64_t trainVersionCode, bool requiresStaging,
207 bool rollbackEnabled, bool requiresLowLatencyMonitor, int32_t state,
208 const std::vector<uint8_t>& experimentIds, int32_t userId) {
209 mLogdTimestampNs = getWallClockNs();
210 mElapsedTimestampNs = getElapsedRealtimeNs();
211 mTagId = android::util::BINARY_PUSH_STATE_CHANGED;
212 mLogUid = android::IPCThreadState::self()->getCallingUid();
213
214 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(1)), Value(trainName)));
215 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(trainVersionCode)));
216 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value((int)requiresStaging)));
217 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value((int)rollbackEnabled)));
218 mValues.push_back(
219 FieldValue(Field(mTagId, getSimpleField(5)), Value((int)requiresLowLatencyMonitor)));
220 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(6)), Value(state)));
221 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(7)), Value(experimentIds)));
222 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(8)), Value(userId)));
223}
224
Howard Roa46b6582018-09-18 16:45:02 -0700225LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
Maggie White58174da2019-01-18 15:23:35 -0800226 const VendorAtom& vendorAtom) {
227 mLogdTimestampNs = wallClockTimestampNs;
228 mElapsedTimestampNs = elapsedTimestampNs;
229 mTagId = vendorAtom.atomId;
Tej Singh8928ed72019-02-22 19:06:22 -0800230 mLogUid = AID_STATSD;
Maggie White58174da2019-01-18 15:23:35 -0800231
232 mValues.push_back(
233 FieldValue(Field(mTagId, getSimpleField(1)), Value(vendorAtom.reverseDomainName)));
234 for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
235 switch (vendorAtom.values[i].getDiscriminator()) {
236 case VendorAtom::Value::hidl_discriminator::intValue:
237 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
238 Value(vendorAtom.values[i].intValue())));
239 break;
240 case VendorAtom::Value::hidl_discriminator::longValue:
241 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
242 Value(vendorAtom.values[i].longValue())));
243 break;
244 case VendorAtom::Value::hidl_discriminator::floatValue:
245 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
246 Value(vendorAtom.values[i].floatValue())));
247 break;
248 case VendorAtom::Value::hidl_discriminator::stringValue:
249 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(i + 2)),
250 Value(vendorAtom.values[i].stringValue())));
251 break;
252 }
253 }
254}
255
Chenjie Yu97dbb202019-02-13 16:42:04 -0800256LogEvent::LogEvent(int64_t wallClockTimestampNs, int64_t elapsedTimestampNs,
257 const InstallTrainInfo& trainInfo) {
258 mLogdTimestampNs = wallClockTimestampNs;
259 mElapsedTimestampNs = elapsedTimestampNs;
260 mTagId = android::util::TRAIN_INFO;
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800261
Chenjie Yu97dbb202019-02-13 16:42:04 -0800262 mValues.push_back(
263 FieldValue(Field(mTagId, getSimpleField(1)), Value(trainInfo.trainVersionCode)));
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400264 std::vector<uint8_t> experimentIdsProto;
265 writeExperimentIdsToProto(trainInfo.experimentIds, &experimentIdsProto);
266 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(2)), Value(experimentIdsProto)));
Muhammad Qureshif4ca8242019-03-01 09:20:15 -0800267 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(3)), Value(trainInfo.trainName)));
268 mValues.push_back(FieldValue(Field(mTagId, getSimpleField(4)), Value(trainInfo.status)));
Chenjie Yu97dbb202019-02-13 16:42:04 -0800269}
270
Yao Chen0f861862019-03-27 11:51:15 -0700271LogEvent::LogEvent(int32_t tagId, int64_t timestampNs) : LogEvent(tagId, timestampNs, timestampNs) {
272}
Howard Ro1a2a3992018-10-22 22:51:57 -0700273
274LogEvent::LogEvent(int32_t tagId, int64_t timestampNs, int32_t uid) {
Yangster-mac330af582018-02-08 15:24:38 -0800275 mLogdTimestampNs = timestampNs;
276 mTagId = tagId;
Howard Ro1a2a3992018-10-22 22:51:57 -0700277 mLogUid = uid;
Yangster-mac330af582018-02-08 15:24:38 -0800278 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
279 if (mContext) {
280 android_log_write_int64(mContext, timestampNs);
Yao Chen80235402017-11-13 20:42:25 -0800281 android_log_write_int32(mContext, tagId);
282 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700283}
284
David Chen1481fe12017-10-16 13:16:34 -0700285void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -0800286 if (mContext) {
287 const char* buffer;
288 size_t len = android_log_write_list_buffer(mContext, &buffer);
289 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800290 android_log_context contextForRead = create_android_log_parser(buffer, len);
291 if (contextForRead) {
292 init(contextForRead);
293 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -0800294 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800295 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -0800296 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -0800297 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -0800298 }
299}
300
301LogEvent::~LogEvent() {
302 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -0800303 // This is for the case when LogEvent is created using the test interface
304 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -0800305 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -0800306 }
307}
308
309bool LogEvent::write(int32_t value) {
310 if (mContext) {
311 return android_log_write_int32(mContext, value) >= 0;
312 }
313 return false;
314}
315
316bool LogEvent::write(uint32_t value) {
317 if (mContext) {
318 return android_log_write_int32(mContext, value) >= 0;
319 }
320 return false;
321}
322
Chenjie Yud9dfda72017-12-11 17:41:20 -0800323bool LogEvent::write(int64_t value) {
324 if (mContext) {
325 return android_log_write_int64(mContext, value) >= 0;
326 }
327 return false;
328}
329
Yao Chen80235402017-11-13 20:42:25 -0800330bool LogEvent::write(uint64_t value) {
331 if (mContext) {
332 return android_log_write_int64(mContext, value) >= 0;
333 }
334 return false;
335}
336
337bool LogEvent::write(const string& value) {
338 if (mContext) {
339 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
340 }
341 return false;
342}
343
344bool LogEvent::write(float value) {
345 if (mContext) {
346 return android_log_write_float32(mContext, value) >= 0;
347 }
348 return false;
349}
350
Tej Singha02bfab2019-10-01 19:03:24 -0700351bool LogEvent::writeBytes(const string& value) {
352 if (mContext) {
353 return android_log_write_char_array(mContext, value.c_str(), value.length()) >= 0;
354 }
355 return false;
356}
357
Howard Ro1a2a3992018-10-22 22:51:57 -0700358bool LogEvent::writeKeyValuePairs(int32_t uid,
359 const std::map<int32_t, int32_t>& int_map,
Howard Ro4078dd42018-09-27 17:41:08 -0700360 const std::map<int32_t, int64_t>& long_map,
Yangster-mace124e422018-08-16 10:30:28 -0700361 const std::map<int32_t, std::string>& string_map,
362 const std::map<int32_t, float>& float_map) {
363 if (mContext) {
364 if (android_log_write_list_begin(mContext) < 0) {
365 return false;
366 }
Howard Ro1a2a3992018-10-22 22:51:57 -0700367 write(uid);
Yangster-mace124e422018-08-16 10:30:28 -0700368 for (const auto& itr : int_map) {
369 if (android_log_write_list_begin(mContext) < 0) {
370 return false;
371 }
372 write(itr.first);
373 write(itr.second);
374 if (android_log_write_list_end(mContext) < 0) {
375 return false;
376 }
377 }
378
Howard Ro4078dd42018-09-27 17:41:08 -0700379 for (const auto& itr : long_map) {
380 if (android_log_write_list_begin(mContext) < 0) {
381 return false;
382 }
383 write(itr.first);
384 write(itr.second);
385 if (android_log_write_list_end(mContext) < 0) {
386 return false;
387 }
388 }
389
Yangster-mace124e422018-08-16 10:30:28 -0700390 for (const auto& itr : string_map) {
391 if (android_log_write_list_begin(mContext) < 0) {
392 return false;
393 }
394 write(itr.first);
395 write(itr.second.c_str());
396 if (android_log_write_list_end(mContext) < 0) {
397 return false;
398 }
399 }
400
401 for (const auto& itr : float_map) {
402 if (android_log_write_list_begin(mContext) < 0) {
403 return false;
404 }
405 write(itr.first);
406 write(itr.second);
407 if (android_log_write_list_end(mContext) < 0) {
408 return false;
409 }
410 }
411
412 if (android_log_write_list_end(mContext) < 0) {
413 return false;
414 }
415 return true;
416 }
417 return false;
418}
419
Yao Chen9c1debe2018-02-19 14:39:19 -0800420bool LogEvent::write(const std::vector<AttributionNodeInternal>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800421 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800422 if (android_log_write_list_begin(mContext) < 0) {
423 return false;
424 }
425 for (size_t i = 0; i < nodes.size(); ++i) {
426 if (!write(nodes[i])) {
427 return false;
428 }
429 }
430 if (android_log_write_list_end(mContext) < 0) {
431 return false;
432 }
433 return true;
434 }
435 return false;
436}
437
Yao Chen9c1debe2018-02-19 14:39:19 -0800438bool LogEvent::write(const AttributionNodeInternal& node) {
Yangster-mac20877162017-12-22 17:19:39 -0800439 if (mContext) {
440 if (android_log_write_list_begin(mContext) < 0) {
441 return false;
442 }
443 if (android_log_write_int32(mContext, node.uid()) < 0) {
444 return false;
445 }
446 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
447 return false;
448 }
Yangster-mac20877162017-12-22 17:19:39 -0800449 if (android_log_write_list_end(mContext) < 0) {
450 return false;
451 }
452 return true;
453 }
454 return false;
455}
456
Ruchir Rastogi1736ba42019-11-04 14:37:13 -0800457void LogEvent::parseInt32(int32_t* pos, int32_t depth, bool* last) {
458 int32_t value = readNextValue<int32_t>();
459 addToValues(pos, depth, value, last);
460}
461
462void LogEvent::parseInt64(int32_t* pos, int32_t depth, bool* last) {
463 int64_t value = readNextValue<int64_t>();
464 addToValues(pos, depth, value, last);
465}
466
467void LogEvent::parseString(int32_t* pos, int32_t depth, bool* last) {
468 int32_t numBytes = readNextValue<int32_t>();
469 if ((uint32_t)numBytes > mRemainingLen) {
470 mValid = false;
471 return;
472 }
473
474 string value = string((char*)mBuf, numBytes);
475 mBuf += numBytes;
476 mRemainingLen -= numBytes;
477 addToValues(pos, depth, value, last);
478}
479
480void LogEvent::parseFloat(int32_t* pos, int32_t depth, bool* last) {
481 float value = readNextValue<float>();
482 addToValues(pos, depth, value, last);
483}
484
485void LogEvent::parseBool(int32_t* pos, int32_t depth, bool* last) {
486 // cast to int32_t because FieldValue does not support bools
487 int32_t value = (int32_t)readNextValue<uint8_t>();
488 addToValues(pos, depth, value, last);
489}
490
491void LogEvent::parseByteArray(int32_t* pos, int32_t depth, bool* last) {
492 int32_t numBytes = readNextValue<int32_t>();
493 if ((uint32_t)numBytes > mRemainingLen) {
494 mValid = false;
495 return;
496 }
497
498 vector<uint8_t> value(mBuf, mBuf + numBytes);
499 mBuf += numBytes;
500 mRemainingLen -= numBytes;
501 addToValues(pos, depth, value, last);
502}
503
504void LogEvent::parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last) {
505 int32_t numPairs = readNextValue<uint8_t>();
506
507 for (pos[1] = 1; pos[1] <= numPairs; pos[1]++) {
508 last[1] = (pos[1] == numPairs);
509
510 // parse key
511 pos[2] = 1;
512 parseInt32(pos, 2, last);
513
514 // parse value
515 last[2] = true;
516 uint8_t typeId = getTypeId(readNextValue<uint8_t>());
517 switch (typeId) {
518 case INT32_TYPE:
519 pos[2] = 2; // pos[2] determined by index of type in KeyValuePair in atoms.proto
520 parseInt32(pos, 2, last);
521 break;
522 case INT64_TYPE:
523 pos[2] = 3;
524 parseInt64(pos, 2, last);
525 break;
526 case STRING_TYPE:
527 pos[2] = 4;
528 parseString(pos, 2, last);
529 break;
530 case FLOAT_TYPE:
531 pos[2] = 5;
532 parseFloat(pos, 2, last);
533 break;
534 default:
535 mValid = false;
536 }
537 }
538
539 pos[1] = pos[2] = 1;
540 last[1] = last[2] = false;
541}
542
543void LogEvent::parseAttributionChain(int32_t* pos, int32_t depth, bool* last) {
544 int32_t numNodes = readNextValue<uint8_t>();
545 for (pos[1] = 1; pos[1] <= numNodes; pos[1]++) {
546 last[1] = (pos[1] == numNodes);
547
548 // parse uid
549 pos[2] = 1;
550 parseInt32(pos, 2, last);
551
552 // parse tag
553 pos[2] = 2;
554 last[2] = true;
555 parseString(pos, 2, last);
556 }
557
558 pos[1] = pos[2] = 1;
559 last[1] = last[2] = false;
560}
561
562
563// This parsing logic is tied to the encoding scheme used in StatsEvent.java and
564// stats_event.c
565void LogEvent::initNew() {
566 int32_t pos[] = {1, 1, 1};
567 bool last[] = {false, false, false};
568
569 // Beginning of buffer is OBJECT_TYPE | NUM_FIELDS | TIMESTAMP | ATOM_ID
570 uint8_t typeInfo = readNextValue<uint8_t>();
571 if (getTypeId(typeInfo) != OBJECT_TYPE) mValid = false;
572
573 uint8_t numElements = readNextValue<uint8_t>();
574 if (numElements < 2 || numElements > 127) mValid = false;
575
576 typeInfo = readNextValue<uint8_t>();
577 if (getTypeId(typeInfo) != INT64_TYPE) mValid = false;
578 mElapsedTimestampNs = readNextValue<int64_t>();
579 numElements--;
580
581 typeInfo = readNextValue<uint8_t>();
582 if (getTypeId(typeInfo) != INT32_TYPE) mValid = false;
583 mTagId = readNextValue<int32_t>();
584 numElements--;
585
586
587 for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) {
588 typeInfo = readNextValue<uint8_t>();
589 uint8_t typeId = getTypeId(typeInfo);
590
591 last[0] = (pos[0] == numElements);
592
593 // TODO(b/144373276): handle errors passed to the socket
594 // TODO(b/144373257): parse annotations
595 switch(typeId) {
596 case BOOL_TYPE:
597 parseBool(pos, 0, last);
598 break;
599 case INT32_TYPE:
600 parseInt32(pos, 0, last);
601 break;
602 case INT64_TYPE:
603 parseInt64(pos, 0, last);
604 break;
605 case FLOAT_TYPE:
606 parseFloat(pos, 0, last);
607 break;
608 case BYTE_ARRAY_TYPE:
609 parseByteArray(pos, 0, last);
610 break;
611 case STRING_TYPE:
612 parseString(pos, 0, last);
613 break;
614 case KEY_VALUE_PAIRS_TYPE:
615 parseKeyValuePairs(pos, 0, last);
616 break;
617 case ATTRIBUTION_CHAIN_TYPE:
618 parseAttributionChain(pos, 0, last);
619 break;
620 default:
621 mValid = false;
622 }
623 }
624
625 if (mRemainingLen != 0) mValid = false;
626 mBuf = nullptr;
627}
628
629uint8_t LogEvent::getTypeId(uint8_t typeInfo) {
630 return typeInfo & 0x0F; // type id in lower 4 bytes
631}
632
633uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) {
634 return (typeInfo >> 4) & 0x0F;
635}
636
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700637/**
638 * The elements of each log event are stored as a vector of android_log_list_elements.
639 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
640 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800641 *
642 * The idea here is to read through the log items once, we get as much information we need for
643 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700644 */
Yao Chen80235402017-11-13 20:42:25 -0800645void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700646 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800647 int i = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800648 int depth = -1;
649 int pos[] = {1, 1, 1};
Yangster-mace124e422018-08-16 10:30:28 -0700650 bool isKeyValuePairAtom = false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700651 do {
Yao Chen80235402017-11-13 20:42:25 -0800652 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700653 switch ((int)elem.type) {
654 case EVENT_TYPE_INT:
Yangster-mac330af582018-02-08 15:24:38 -0800655 // elem at [0] is EVENT_TYPE_LIST, [1] is the timestamp, [2] is tag id.
656 if (i == 2) {
Yao Chen80235402017-11-13 20:42:25 -0800657 mTagId = elem.data.int32;
Yangster-mace124e422018-08-16 10:30:28 -0700658 isKeyValuePairAtom = (mTagId == android::util::KEY_VALUE_PAIRS_ATOM);
Yangster-mac20877162017-12-22 17:19:39 -0800659 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800660 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800661 return;
662 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800663
664 mValues.push_back(
665 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
666
667 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800668 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700669 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800670 case EVENT_TYPE_FLOAT: {
671 if (depth < 0 || depth > 2) {
672 ALOGE("Depth > 2. Not supported!");
673 return;
674 }
675
Yangster-mace124e422018-08-16 10:30:28 -0700676 // Handles the oneof field in KeyValuePair atom.
677 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800678 pos[depth] = 5;
Yangster-mace124e422018-08-16 10:30:28 -0700679 }
680
Yao Chen8a8d16c2018-02-08 14:50:40 -0800681 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
682
683 pos[depth]++;
684
685 } break;
686 case EVENT_TYPE_STRING: {
687 if (depth < 0 || depth > 2) {
688 ALOGE("Depth > 2. Not supported!");
689 return;
690 }
691
Yangster-mace124e422018-08-16 10:30:28 -0700692 // Handles the oneof field in KeyValuePair atom.
693 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800694 pos[depth] = 4;
Yangster-mace124e422018-08-16 10:30:28 -0700695 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800696 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
697 Value(string(elem.data.string, elem.len))));
698
699 pos[depth]++;
700
701 } break;
702 case EVENT_TYPE_LONG: {
Yangster-mac330af582018-02-08 15:24:38 -0800703 if (i == 1) {
704 mElapsedTimestampNs = elem.data.int64;
705 } else {
706 if (depth < 0 || depth > 2) {
707 ALOGE("Depth > 2. Not supported!");
708 return;
709 }
Yangster-mace124e422018-08-16 10:30:28 -0700710 // Handles the oneof field in KeyValuePair atom.
711 if (isKeyValuePairAtom && depth == 2) {
Tej Singh2c96b5a2019-02-04 21:28:49 -0800712 pos[depth] = 3;
Yangster-mace124e422018-08-16 10:30:28 -0700713 }
Yangster-mac330af582018-02-08 15:24:38 -0800714 mValues.push_back(
715 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
716
717 pos[depth]++;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800718 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800719 } break;
720 case EVENT_TYPE_LIST:
721 depth++;
722 if (depth > 2) {
723 ALOGE("Depth > 2. Not supported!");
724 return;
725 }
726 pos[depth] = 1;
727
728 break;
729 case EVENT_TYPE_LIST_STOP: {
730 int prevDepth = depth;
731 depth--;
732 if (depth >= 0 && depth < 2) {
733 // Now go back to decorate the previous items that are last at prevDepth.
734 // So that we can later easily match them with Position=Last matchers.
735 pos[prevDepth]--;
736 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800737 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
738 if (it->mField.getDepth() >= prevDepth &&
739 it->mField.getPath(prevDepth) == path) {
740 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800741 } else {
742 // Safe to break, because the items are in DFS order.
743 break;
744 }
Yangster-mac20877162017-12-22 17:19:39 -0800745 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800746 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800747 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700748 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800749 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700750 case EVENT_TYPE_UNKNOWN:
751 break;
752 default:
753 break;
754 }
Yao Chen80235402017-11-13 20:42:25 -0800755 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700756 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
Howard Ro1a2a3992018-10-22 22:51:57 -0700757 if (isKeyValuePairAtom && mValues.size() > 0) {
758 mValues[0] = FieldValue(Field(android::util::KEY_VALUE_PAIRS_ATOM, getSimpleField(1)),
759 Value((int32_t)mLogUid));
760 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700761}
762
763int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen5bfffb52018-06-21 16:58:51 -0700764 // TODO(b/110561208): encapsulate the magical operations in Field struct as static functions
Yao Chen8a8d16c2018-02-08 14:50:40 -0800765 int field = getSimpleField(key);
766 for (const auto& value : mValues) {
767 if (value.mField.getField() == field) {
Yao Chenab92a1f2018-02-13 15:17:55 -0800768 if (value.mValue.getType() == LONG) {
769 return value.mValue.long_value;
770 } else if (value.mValue.getType() == INT) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800771 return value.mValue.int_value;
772 } else {
773 *err = BAD_TYPE;
774 return 0;
775 }
776 }
777 if ((size_t)value.mField.getPosAtDepth(0) > key) {
778 break;
Yangster-mac20877162017-12-22 17:19:39 -0800779 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700780 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800781
782 *err = BAD_INDEX;
783 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700784}
785
Chenjie Yu80f91122018-01-31 20:24:50 -0800786int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800787 int field = getSimpleField(key);
788 for (const auto& value : mValues) {
789 if (value.mField.getField() == field) {
790 if (value.mValue.getType() == INT) {
791 return value.mValue.int_value;
792 } else {
793 *err = BAD_TYPE;
794 return 0;
795 }
796 }
797 if ((size_t)value.mField.getPosAtDepth(0) > key) {
798 break;
799 }
800 }
801
Chenjie Yu80f91122018-01-31 20:24:50 -0800802 *err = BAD_INDEX;
803 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800804}
805
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700806const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800807 int field = getSimpleField(key);
808 for (const auto& value : mValues) {
809 if (value.mField.getField() == field) {
810 if (value.mValue.getType() == STRING) {
811 return value.mValue.str_value.c_str();
812 } else {
813 *err = BAD_TYPE;
814 return 0;
815 }
816 }
817 if ((size_t)value.mField.getPosAtDepth(0) > key) {
818 break;
Yangster-mac20877162017-12-22 17:19:39 -0800819 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700820 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800821
822 *err = BAD_INDEX;
823 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700824}
825
826bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800827 int field = getSimpleField(key);
828 for (const auto& value : mValues) {
829 if (value.mField.getField() == field) {
830 if (value.mValue.getType() == INT) {
831 return value.mValue.int_value != 0;
832 } else if (value.mValue.getType() == LONG) {
833 return value.mValue.long_value != 0;
834 } else {
835 *err = BAD_TYPE;
836 return false;
837 }
838 }
839 if ((size_t)value.mField.getPosAtDepth(0) > key) {
840 break;
Yangster-mac20877162017-12-22 17:19:39 -0800841 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700842 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800843
844 *err = BAD_INDEX;
845 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700846}
847
848float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800849 int field = getSimpleField(key);
850 for (const auto& value : mValues) {
851 if (value.mField.getField() == field) {
852 if (value.mValue.getType() == FLOAT) {
853 return value.mValue.float_value;
854 } else {
855 *err = BAD_TYPE;
856 return 0.0;
857 }
858 }
859 if ((size_t)value.mField.getPosAtDepth(0) > key) {
860 break;
Yangster-mac20877162017-12-22 17:19:39 -0800861 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700862 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700863
Yao Chen8a8d16c2018-02-08 14:50:40 -0800864 *err = BAD_INDEX;
865 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800866}
867
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700868string LogEvent::ToString() const {
Yao Chen20e9e622018-02-28 11:18:51 -0800869 string result;
Yao Chen8e6f9982018-11-29 09:39:45 -0800870 result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
Yao Chen20e9e622018-02-28 11:18:51 -0800871 (long long)mElapsedTimestampNs, mTagId);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800872 for (const auto& value : mValues) {
Yao Chen20e9e622018-02-28 11:18:51 -0800873 result +=
874 StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700875 }
Yao Chen20e9e622018-02-28 11:18:51 -0800876 result += " }";
877 return result;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700878}
879
Yangster-mac20877162017-12-22 17:19:39 -0800880void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800881 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700882}
883
Tej Singh9b4a5ec2019-04-25 12:43:35 -0700884void writeExperimentIdsToProto(const std::vector<int64_t>& experimentIds,
885 std::vector<uint8_t>* protoOut) {
Jeff Hamiltonfa2f91c2019-03-22 00:25:02 -0400886 ProtoOutputStream proto;
887 for (const auto& expId : experimentIds) {
888 proto.write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_EXPERIMENT_ID,
889 (long long)expId);
890 }
891
892 protoOut->resize(proto.size());
893 size_t pos = 0;
894 sp<ProtoReader> reader = proto.data();
895 while (reader->readBuffer() != NULL) {
896 size_t toRead = reader->currentToRead();
897 std::memcpy(protoOut->data() + pos, reader->readBuffer(), toRead);
898 pos += toRead;
899 reader->move(toRead);
900 }
901}
902
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700903} // namespace statsd
904} // namespace os
905} // namespace android