File: eventlog_helper.h

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (272 lines) | stat: -rw-r--r-- 10,096 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
#define FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_

#include <memory>

#include <fcntl.h>

#include <android-base/macros.h>
#include <log/log_event_list.h>

#include <log/log.h>

#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedLocalRef.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include "core_jni_helpers.h"
#include "jni.h"

namespace android {

template <log_id_t LogID, const char* EventClassDescriptor>
class EventLogHelper {
public:
    static void Init(JNIEnv* env) {
        struct { const char *name; jclass *clazz; } gClasses[] = {
                { EventClassDescriptor, &gEventClass },
                { "java/lang/Integer", &gIntegerClass },
                { "java/lang/Long", &gLongClass },
                { "java/lang/Float", &gFloatClass },
                { "java/lang/String", &gStringClass },
                { "java/util/Collection", &gCollectionClass },
        };
        struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
                { &gIntegerClass, "value", "I", &gIntegerValueID },
                { &gLongClass, "value", "J", &gLongValueID },
                { &gFloatClass, "value", "F", &gFloatValueID },
        };
        struct { jclass *c; const char *name, *mt; jmethodID *id; } gMethods[] = {
                { &gEventClass, "<init>", "([B)V", &gEventInitID },
                { &gCollectionClass, "add", "(Ljava/lang/Object;)Z", &gCollectionAddID },
        };

        for (size_t i = 0; i < NELEM(gClasses); ++i) {
            ScopedLocalRef<jclass> clazz(env, FindClassOrDie(env, gClasses[i].name));
            *gClasses[i].clazz = MakeGlobalRefOrDie(env, clazz.get());
        }
        for (size_t i = 0; i < NELEM(gFields); ++i) {
            *gFields[i].id = GetFieldIDOrDie(env,
                    *gFields[i].c, gFields[i].name, gFields[i].ft);
        }

        for (size_t i = 0; i < NELEM(gMethods); ++i) {
            *gMethods[i].id = GetMethodIDOrDie(env,
                    *gMethods[i].c, gMethods[i].name, gMethods[i].mt);
        }
    }

    static jint writeEventInteger(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
            jint tag, jint value) {
        android_log_event_list ctx(tag);
        ctx << (int32_t)value;
        return ctx.write(LogID);
    }
    static jint writeEventLong(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
            jint tag, jlong value) {
        android_log_event_list ctx(tag);
        ctx << (int64_t)value;
        return ctx.write(LogID);
    }
    static jint writeEventFloat(JNIEnv* env ATTRIBUTE_UNUSED, jobject clazz ATTRIBUTE_UNUSED,
            jint tag, jfloat value) {
        android_log_event_list ctx(tag);
        ctx << (float)value;
        return ctx.write(LogID);
    }
    static jint writeEventString(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag,
            jstring value) {
        android_log_event_list ctx(tag);
        // Don't throw NPE -- I feel like it's sort of mean for a logging function
        // to be all crashy if you pass in NULL -- but make the NULL value explicit.
        ctx << (value != nullptr ? ScopedUtfChars(env, value).c_str() : "NULL");
        return ctx.write(LogID);
    }
    static jint writeEventArray(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag,
            jobjectArray value) {
        android_log_event_list ctx(tag);

        if (value == nullptr) {
            ctx << "[NULL]";
            return ctx.write(LogID);
        }

        jsize copied = 0, num = env->GetArrayLength(value);
        for (; copied < num && copied < 255; ++copied) {
            if (ctx.status()) break;
            ScopedLocalRef<jobject> item(env, env->GetObjectArrayElement(value, copied));
            if (item == nullptr) {
                ctx << "NULL";
            } else if (env->IsInstanceOf(item.get(), gStringClass)) {
                ctx << ScopedUtfChars(env, (jstring) item.get()).c_str();
            } else if (env->IsInstanceOf(item.get(), gIntegerClass)) {
                ctx << (int32_t)env->GetIntField(item.get(), gIntegerValueID);
            } else if (env->IsInstanceOf(item.get(), gLongClass)) {
                ctx << (int64_t)env->GetLongField(item.get(), gLongValueID);
            } else if (env->IsInstanceOf(item.get(), gFloatClass)) {
                ctx << (float)env->GetFloatField(item.get(), gFloatValueID);
            } else {
                jniThrowException(env,
                        "java/lang/IllegalArgumentException",
                        "Invalid payload item type");
                return -1;
            }
        }
        return ctx.write(LogID);
    }

    static void readEvents(JNIEnv* env, int loggerMode, jlong startTime, jobject out) {
        readEvents(env, loggerMode, nullptr, startTime, out);
    }

    static void readEvents(JNIEnv* env, int loggerMode, jintArray jTags, jlong startTime,
            jobject out) {
        std::unique_ptr<struct logger_list, decltype(&android_logger_list_close)> logger_list(
                nullptr, android_logger_list_close);
        if (startTime) {
            logger_list.reset(android_logger_list_alloc_time(loggerMode,
                    log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0));
        } else {
            logger_list.reset(android_logger_list_alloc(loggerMode, 0, 0));
        }
        if (!logger_list) {
            jniThrowIOException(env, errno);
            return;
        }

        if (!android_logger_open(logger_list.get(), LogID)) {
            jniThrowIOException(env, errno);
            return;
        }

        ScopedIntArrayRO tags(env);
        if (jTags != nullptr) {
            tags.reset(jTags);
        }

        while (1) {
            log_msg log_msg;
            int ret = android_logger_list_read(logger_list.get(), &log_msg);

            if (ret == 0) {
                return;
            }
            if (ret < 0) {
                if (ret == -EINTR) {
                    continue;
                }
                if (ret == -EINVAL) {
                    jniThrowException(env, "java/io/IOException", "Event too short");
                } else if (ret != -EAGAIN) {
                    jniThrowIOException(env, -ret);  // Will throw on return
                }
                return;
            }

            if (log_msg.id() != LogID) {
                continue;
            }

            int32_t tag = * (int32_t *) log_msg.msg();

            if (jTags != nullptr) {
                bool found = false;
                for (size_t i = 0; !found && i < tags.size(); ++i) {
                    found = (tag == tags[i]);
                }
                if (!found) {
                    continue;
                }
            }

            jsize len = ret;
            ScopedLocalRef<jbyteArray> array(env, env->NewByteArray(len));
            if (array == nullptr) {
                return;
            }

            {
                ScopedByteArrayRW bytes(env, array.get());
                memcpy(bytes.get(), log_msg.buf, len);
            }

            ScopedLocalRef<jobject> event(env,
                    env->NewObject(gEventClass, gEventInitID, array.get()));
            if (event == nullptr) {
                return;
            }

            env->CallBooleanMethod(out, gCollectionAddID, event.get());
            if (env->ExceptionCheck() == JNI_TRUE) {
                return;
            }
        }
    }

private:
    static jclass gCollectionClass;
    static jmethodID gCollectionAddID;

    static jclass gEventClass;
    static jmethodID gEventInitID;

    static jclass gIntegerClass;
    static jfieldID gIntegerValueID;

    static jclass gLongClass;
    static jfieldID gLongValueID;

    static jclass gFloatClass;
    static jfieldID gFloatValueID;

    static jclass gStringClass;
};

// Explicit instantiation declarations.
template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gCollectionClass;
template <log_id_t LogID, const char* EventClassDescriptor>
jmethodID EventLogHelper<LogID, EventClassDescriptor>::gCollectionAddID;

template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gEventClass;
template <log_id_t LogID, const char* EventClassDescriptor>
jmethodID EventLogHelper<LogID, EventClassDescriptor>::gEventInitID;

template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gIntegerClass;
template <log_id_t LogID, const char* EventClassDescriptor>
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gIntegerValueID;

template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gLongClass;
template <log_id_t LogID, const char* EventClassDescriptor>
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gLongValueID;

template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gFloatClass;
template <log_id_t LogID, const char* EventClassDescriptor>
jfieldID EventLogHelper<LogID, EventClassDescriptor>::gFloatValueID;

template <log_id_t LogID, const char* EventClassDescriptor>
jclass EventLogHelper<LogID, EventClassDescriptor>::gStringClass;

}  // namespace android

#endif  // FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_