blob: ce3a4b96b2613cc7f66a0b5ac5b66e87024a254b [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
20#include <sstream>
Yangster-mac20877162017-12-22 17:19:39 -080021
Yangster-mac20877162017-12-22 17:19:39 -080022#include "stats_log_util.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070023
24namespace android {
25namespace os {
26namespace statsd {
27
yro24809bd2017-10-31 23:06:53 -070028using namespace android::util;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070029using std::ostringstream;
David Chen1481fe12017-10-16 13:16:34 -070030using std::string;
Yao Chen5110bed2017-10-23 12:50:02 -070031using android::util::ProtoOutputStream;
Joe Onoratoc4dfae52017-10-17 23:38:21 -070032
Yao Chen80235402017-11-13 20:42:25 -080033LogEvent::LogEvent(log_msg& msg) {
Chenjie Yu3ca36832018-01-22 15:10:54 -080034 mContext =
Yao Chen80235402017-11-13 20:42:25 -080035 create_android_log_parser(msg.msg() + sizeof(uint32_t), msg.len() - sizeof(uint32_t));
36 mTimestampNs = msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec;
Yao Chend10f7b12017-12-18 12:53:50 -080037 mLogUid = msg.entry_v4.uid;
Chenjie Yu3ca36832018-01-22 15:10:54 -080038 init(mContext);
39 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080040 // android_log_destroy will set mContext to NULL
Chenjie Yu3ca36832018-01-22 15:10:54 -080041 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080042 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070043}
44
Yao Chen80235402017-11-13 20:42:25 -080045LogEvent::LogEvent(int32_t tagId, uint64_t timestampNs) {
46 mTimestampNs = timestampNs;
47 mTagId = tagId;
Yangster-mac20877162017-12-22 17:19:39 -080048 mLogUid = 0;
Yao Chen80235402017-11-13 20:42:25 -080049 mContext = create_android_logger(1937006964); // the event tag shared by all stats logs
50 if (mContext) {
51 android_log_write_int32(mContext, tagId);
52 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -070053}
54
David Chen1481fe12017-10-16 13:16:34 -070055void LogEvent::init() {
Yao Chen80235402017-11-13 20:42:25 -080056 if (mContext) {
57 const char* buffer;
58 size_t len = android_log_write_list_buffer(mContext, &buffer);
59 // turns to reader mode
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080060 android_log_context contextForRead = create_android_log_parser(buffer, len);
61 if (contextForRead) {
62 init(contextForRead);
63 // destroy the context to save memory.
Yao Chen48d75182018-01-23 09:40:48 -080064 // android_log_destroy will set mContext to NULL
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080065 android_log_destroy(&contextForRead);
Yao Chen48d75182018-01-23 09:40:48 -080066 }
Chenjie Yuc1fe6f42018-02-01 23:14:18 -080067 android_log_destroy(&mContext);
Yangster-mac20877162017-12-22 17:19:39 -080068 }
69}
70
71LogEvent::~LogEvent() {
72 if (mContext) {
Yao Chen48d75182018-01-23 09:40:48 -080073 // This is for the case when LogEvent is created using the test interface
74 // but init() isn't called.
Yangster-mac20877162017-12-22 17:19:39 -080075 android_log_destroy(&mContext);
Yao Chen80235402017-11-13 20:42:25 -080076 }
77}
78
79bool LogEvent::write(int32_t value) {
80 if (mContext) {
81 return android_log_write_int32(mContext, value) >= 0;
82 }
83 return false;
84}
85
86bool LogEvent::write(uint32_t value) {
87 if (mContext) {
88 return android_log_write_int32(mContext, value) >= 0;
89 }
90 return false;
91}
92
Chenjie Yud9dfda72017-12-11 17:41:20 -080093bool LogEvent::write(int64_t value) {
94 if (mContext) {
95 return android_log_write_int64(mContext, value) >= 0;
96 }
97 return false;
98}
99
Yao Chen80235402017-11-13 20:42:25 -0800100bool LogEvent::write(uint64_t value) {
101 if (mContext) {
102 return android_log_write_int64(mContext, value) >= 0;
103 }
104 return false;
105}
106
107bool LogEvent::write(const string& value) {
108 if (mContext) {
109 return android_log_write_string8_len(mContext, value.c_str(), value.length()) >= 0;
110 }
111 return false;
112}
113
114bool LogEvent::write(float value) {
115 if (mContext) {
116 return android_log_write_float32(mContext, value) >= 0;
117 }
118 return false;
119}
120
Yangster-mac20877162017-12-22 17:19:39 -0800121bool LogEvent::write(const std::vector<AttributionNode>& nodes) {
Yao Chen80235402017-11-13 20:42:25 -0800122 if (mContext) {
Yangster-mac20877162017-12-22 17:19:39 -0800123 if (android_log_write_list_begin(mContext) < 0) {
124 return false;
125 }
126 for (size_t i = 0; i < nodes.size(); ++i) {
127 if (!write(nodes[i])) {
128 return false;
129 }
130 }
131 if (android_log_write_list_end(mContext) < 0) {
132 return false;
133 }
134 return true;
135 }
136 return false;
137}
138
139bool LogEvent::write(const AttributionNode& node) {
140 if (mContext) {
141 if (android_log_write_list_begin(mContext) < 0) {
142 return false;
143 }
144 if (android_log_write_int32(mContext, node.uid()) < 0) {
145 return false;
146 }
147 if (android_log_write_string8(mContext, node.tag().c_str()) < 0) {
148 return false;
149 }
Yangster-mac20877162017-12-22 17:19:39 -0800150 if (android_log_write_list_end(mContext) < 0) {
151 return false;
152 }
153 return true;
154 }
155 return false;
156}
157
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700158/**
159 * The elements of each log event are stored as a vector of android_log_list_elements.
160 * The goal is to do as little preprocessing as possible, because we read a tiny fraction
161 * of the elements that are written to the log.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800162 *
163 * The idea here is to read through the log items once, we get as much information we need for
164 * matching as possible. Because this log will be matched against lots of matchers.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700165 */
Yao Chen80235402017-11-13 20:42:25 -0800166void LogEvent::init(android_log_context context) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700167 android_log_list_element elem;
Yao Chen80235402017-11-13 20:42:25 -0800168 int i = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800169
170 int seenListStart = 0;
171
Yao Chen8a8d16c2018-02-08 14:50:40 -0800172 int32_t field = 0;
173 int depth = -1;
174 int pos[] = {1, 1, 1};
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700175 do {
Yao Chen80235402017-11-13 20:42:25 -0800176 elem = android_log_read_next(context);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700177 switch ((int)elem.type) {
178 case EVENT_TYPE_INT:
Yangster-mac20877162017-12-22 17:19:39 -0800179 // elem at [0] is EVENT_TYPE_LIST, [1] is the tag id.
Yao Chen80235402017-11-13 20:42:25 -0800180 if (i == 1) {
181 mTagId = elem.data.int32;
Yangster-mac20877162017-12-22 17:19:39 -0800182 } else {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800183 if (depth < 0 || depth > 2) {
Yangster-mac20877162017-12-22 17:19:39 -0800184 return;
185 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800186
187 mValues.push_back(
188 FieldValue(Field(mTagId, pos, depth), Value((int32_t)elem.data.int32)));
189
190 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800191 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700192 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800193 case EVENT_TYPE_FLOAT: {
194 if (depth < 0 || depth > 2) {
195 ALOGE("Depth > 2. Not supported!");
196 return;
197 }
198
199 mValues.push_back(FieldValue(Field(mTagId, pos, depth), Value(elem.data.float32)));
200
201 pos[depth]++;
202
203 } break;
204 case EVENT_TYPE_STRING: {
205 if (depth < 0 || depth > 2) {
206 ALOGE("Depth > 2. Not supported!");
207 return;
208 }
209
210 mValues.push_back(FieldValue(Field(mTagId, pos, depth),
211 Value(string(elem.data.string, elem.len))));
212
213 pos[depth]++;
214
215 } break;
216 case EVENT_TYPE_LONG: {
217 if (depth < 0 || depth > 2) {
218 ALOGE("Depth > 2. Not supported!");
219 return;
220 }
221 mValues.push_back(
222 FieldValue(Field(mTagId, pos, depth), Value((int64_t)elem.data.int64)));
223
224 pos[depth]++;
225
226 } break;
227 case EVENT_TYPE_LIST:
228 depth++;
229 if (depth > 2) {
230 ALOGE("Depth > 2. Not supported!");
231 return;
232 }
233 pos[depth] = 1;
234
235 break;
236 case EVENT_TYPE_LIST_STOP: {
237 int prevDepth = depth;
238 depth--;
239 if (depth >= 0 && depth < 2) {
240 // Now go back to decorate the previous items that are last at prevDepth.
241 // So that we can later easily match them with Position=Last matchers.
242 pos[prevDepth]--;
243 int path = getEncodedField(pos, prevDepth, false);
Yao Chendb43afc2018-02-13 09:37:27 -0800244 for (auto it = mValues.rbegin(); it != mValues.rend(); ++it) {
245 if (it->mField.getDepth() >= prevDepth &&
246 it->mField.getPath(prevDepth) == path) {
247 it->mField.decorateLastPos(prevDepth);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800248 } else {
249 // Safe to break, because the items are in DFS order.
250 break;
251 }
Yangster-mac20877162017-12-22 17:19:39 -0800252 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800253 pos[depth]++;
Yangster-mac20877162017-12-22 17:19:39 -0800254 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700255 break;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800256 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700257 case EVENT_TYPE_UNKNOWN:
258 break;
259 default:
260 break;
261 }
Yao Chen80235402017-11-13 20:42:25 -0800262 i++;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700263 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
264}
265
266int64_t LogEvent::GetLong(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800267 // TODO: encapsulate the magical operations all in Field struct as a static function.
268 int field = getSimpleField(key);
269 for (const auto& value : mValues) {
270 if (value.mField.getField() == field) {
271 if (value.mValue.getType() == INT) {
272 return value.mValue.int_value;
273 } else {
274 *err = BAD_TYPE;
275 return 0;
276 }
277 }
278 if ((size_t)value.mField.getPosAtDepth(0) > key) {
279 break;
Yangster-mac20877162017-12-22 17:19:39 -0800280 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700281 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800282
283 *err = BAD_INDEX;
284 return 0;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700285}
286
Chenjie Yu80f91122018-01-31 20:24:50 -0800287int LogEvent::GetInt(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800288 int field = getSimpleField(key);
289 for (const auto& value : mValues) {
290 if (value.mField.getField() == field) {
291 if (value.mValue.getType() == INT) {
292 return value.mValue.int_value;
293 } else {
294 *err = BAD_TYPE;
295 return 0;
296 }
297 }
298 if ((size_t)value.mField.getPosAtDepth(0) > key) {
299 break;
300 }
301 }
302
Chenjie Yu80f91122018-01-31 20:24:50 -0800303 *err = BAD_INDEX;
304 return 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800305}
306
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700307const char* LogEvent::GetString(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800308 int field = getSimpleField(key);
309 for (const auto& value : mValues) {
310 if (value.mField.getField() == field) {
311 if (value.mValue.getType() == STRING) {
312 return value.mValue.str_value.c_str();
313 } else {
314 *err = BAD_TYPE;
315 return 0;
316 }
317 }
318 if ((size_t)value.mField.getPosAtDepth(0) > key) {
319 break;
Yangster-mac20877162017-12-22 17:19:39 -0800320 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700321 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800322
323 *err = BAD_INDEX;
324 return NULL;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700325}
326
327bool LogEvent::GetBool(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800328 int field = getSimpleField(key);
329 for (const auto& value : mValues) {
330 if (value.mField.getField() == field) {
331 if (value.mValue.getType() == INT) {
332 return value.mValue.int_value != 0;
333 } else if (value.mValue.getType() == LONG) {
334 return value.mValue.long_value != 0;
335 } else {
336 *err = BAD_TYPE;
337 return false;
338 }
339 }
340 if ((size_t)value.mField.getPosAtDepth(0) > key) {
341 break;
Yangster-mac20877162017-12-22 17:19:39 -0800342 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700343 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800344
345 *err = BAD_INDEX;
346 return false;
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700347}
348
349float LogEvent::GetFloat(size_t key, status_t* err) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800350 int field = getSimpleField(key);
351 for (const auto& value : mValues) {
352 if (value.mField.getField() == field) {
353 if (value.mValue.getType() == FLOAT) {
354 return value.mValue.float_value;
355 } else {
356 *err = BAD_TYPE;
357 return 0.0;
358 }
359 }
360 if ((size_t)value.mField.getPosAtDepth(0) > key) {
361 break;
Yangster-mac20877162017-12-22 17:19:39 -0800362 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700363 }
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700364
Yao Chen8a8d16c2018-02-08 14:50:40 -0800365 *err = BAD_INDEX;
366 return 0.0;
Yangster-macd40053e2018-01-09 16:29:22 -0800367}
368
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700369string LogEvent::ToString() const {
370 ostringstream result;
371 result << "{ " << mTimestampNs << " (" << mTagId << ")";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800372 for (const auto& value : mValues) {
373 result << StringPrintf("%#x", value.mField.getField());
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700374 result << "->";
Yao Chen8a8d16c2018-02-08 14:50:40 -0800375 result << value.mValue.toString();
Yangster-mac20877162017-12-22 17:19:39 -0800376 result << " ";
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700377 }
378 result << " }";
379 return result.str();
380}
381
Yangster-mac20877162017-12-22 17:19:39 -0800382void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800383 writeFieldValueTreeToStream(mTagId, getValues(), &protoOutput);
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700384}
385
386} // namespace statsd
387} // namespace os
388} // namespace android