1 /*
2 * Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "jfr/jni/jfrJavaSupport.hpp"
26 #include "jfr/utilities/jfrJavaLog.hpp"
27 #include "jfr/utilities/jfrLogTagSets.hpp"
28 #include "logging/log.hpp"
29 #include "logging/logConfiguration.hpp"
30 #include "logging/logMessage.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/objArrayOop.inline.hpp"
33 #include "oops/oopCast.inline.hpp"
34 #include "runtime/javaThread.hpp"
35
36 #define JFR_LOG_TAGS_CONCATED(T0, T1, T2, T3, T4, T5, ...) \
37 T0 ## _ ## T1 ## _ ## T2 ## _ ## T3 ## _ ## T4 ## _ ## T5
38
39 enum JfrLogTagSetType {
40 #define JFR_LOG_TAG(...) \
41 EXPAND_VARARGS(JFR_LOG_TAGS_CONCATED(__VA_ARGS__, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG)),
42
43 JFR_LOG_TAG_SET_LIST
44
45 #undef JFR_LOG_TAG
46 JFR_LOG_TAG_SET_COUNT
47 };
48
49 struct jfrLogSubscriber
50 {
51 jobject log_tag_enum_ref;
52 LogTagSet* log_tag_set;
53 };
54
55 static jfrLogSubscriber log_tag_sets[JFR_LOG_TAG_SET_COUNT];
56
57 static void log_cfg_update(LogLevelType llt, JfrLogTagSetType jflt, TRAPS) {
58 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
59 if (log_tag_sets[jflt].log_tag_enum_ref == nullptr) {
60 return;
61 }
62 jobject lt = log_tag_sets[jflt].log_tag_enum_ref;
63 // set field tagSetLevel to llt value
64 JavaValue result(T_VOID);
65 JfrJavaArguments args(&result);
66 args.set_klass(JfrJavaSupport::klass(lt));
67 args.set_name("tagSetLevel");
68 args.set_signature("I");
69 args.set_receiver(JfrJavaSupport::resolve_non_null(lt));
70 args.push_int(llt);
71 JfrJavaSupport::set_field(&args, THREAD);
72 }
73
74 static LogLevelType highest_level(const LogTagSet& lts) {
75 for (size_t i = 0; i < LogLevel::Count; i++) {
76 if (lts.is_level((LogLevelType)i)) {
77 return (LogLevelType)i;
78 }
79 }
80 return LogLevel::Off;
81 }
82
83 static void log_config_change_internal(bool init, TRAPS) {
84 LogLevelType llt;
85 LogTagSet* lts;
86
87 #define JFR_LOG_TAG(...) \
88 lts = &LogTagSetMapping<LOG_TAGS(__VA_ARGS__)>::tagset(); \
89 if (init) { \
90 JfrLogTagSetType tagSetType = \
91 EXPAND_VARARGS(JFR_LOG_TAGS_CONCATED(__VA_ARGS__, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG)); \
92 assert(nullptr == log_tag_sets[tagSetType].log_tag_set, "Init JFR LogTagSets twice"); \
93 log_tag_sets[tagSetType].log_tag_set = lts; \
94 } \
95 llt = highest_level(*lts); \
96 log_cfg_update(llt, \
97 EXPAND_VARARGS(JFR_LOG_TAGS_CONCATED(__VA_ARGS__, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG, _NO_TAG)), THREAD);
98 JFR_LOG_TAG_SET_LIST
99 #undef JFR_LOG_TAG
100 }
101
102 static void log_config_change() {
103 JavaThread* t = JavaThread::current();
104 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
105 log_config_change_internal(false, t);
106 }
107
108 void JfrJavaLog::subscribe_log_level(jobject log_tag, jint id, TRAPS) {
109 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
110 static bool subscribed_updates = true;
111 assert(id < JFR_LOG_TAG_SET_COUNT,
112 "LogTag id, java and native not in synch, %d < %d", id, JFR_LOG_TAG_SET_COUNT);
113 assert(nullptr == log_tag_sets[id].log_tag_enum_ref, "Subscribing twice");
114 log_tag_sets[id].log_tag_enum_ref = JfrJavaSupport::global_jni_handle(log_tag, THREAD);
115 if (subscribed_updates) {
116 LogConfiguration::register_update_listener(&log_config_change);
117 log_config_change_internal(true, THREAD);
118 subscribed_updates = false;
119 } else {
120 log_config_change_internal(false, THREAD);
121 }
122 }
123
124 void JfrJavaLog::log_event(JNIEnv* env, jint level, jobjectArray lines, bool system, TRAPS) {
125 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
126 if (lines == nullptr) {
127 return;
128 }
129 if (level < (jint)LogLevel::First || level > (jint)LogLevel::Last) {
130 JfrJavaSupport::throw_illegal_argument_exception("LogLevel passed is outside valid range", THREAD);
131 return;
132 }
133
134 refArrayOop the_lines = oop_cast<refArrayOop>(JfrJavaSupport::resolve_non_null(lines));
135 const int length = the_lines->length();
136
137 ResourceMark rm(THREAD);
138 LogMessage(jfr, event) jfr_event;
139 LogMessage(jfr, system, event) jfr_event_system;
140 for (int i = 0; i < length; ++i) {
141 const char* text = JfrJavaSupport::c_str(the_lines->obj_at(i), THREAD);
142 if (text == nullptr) {
143 // An oome has been thrown and is pending.
144 return;
145 }
146 if (system) {
147 jfr_event_system.write((LogLevelType)level, "%s", text);
148 } else {
149 jfr_event.write((LogLevelType)level, "%s", text);
150 }
151 }
152 }
153
154 void JfrJavaLog::log(jint tag_set, jint level, jstring message, TRAPS) {
155 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
156 if (message == nullptr) {
157 return;
158 }
159 if (level < (jint)LogLevel::First || level > (jint)LogLevel::Last) {
160 JfrJavaSupport::throw_illegal_argument_exception("LogLevel passed is outside valid range", THREAD);
161 return;
162 }
163 if (tag_set < 0 || tag_set >= (jint)JFR_LOG_TAG_SET_COUNT) {
164 JfrJavaSupport::throw_illegal_argument_exception("LogTagSet id is outside valid range", THREAD);
165 return;
166 }
167 ResourceMark rm(THREAD);
168 const char* const s = JfrJavaSupport::c_str(message, CHECK);
169 assert(s != nullptr, "invariant");
170 assert(log_tag_sets[tag_set].log_tag_set != nullptr, "LogTagSet is not init");
171 log_tag_sets[tag_set].log_tag_set->log((LogLevelType)level, s);
172 }