1 /* Copyright (C) 2018 The Android Open Source Project
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This file implements interfaces from the file jvmti.h. This implementation
5  * is licensed under the same terms as the file jvmti.h.  The
6  * copyright and license information for the file jvmti.h follows.
7  *
8  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10  *
11  * This code is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 only, as
13  * published by the Free Software Foundation.  Oracle designates this
14  * particular file as subject to the "Classpath" exception as provided
15  * by Oracle in the LICENSE file that accompanied this code.
16  *
17  * This code is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  * version 2 for more details (a copy is included in the LICENSE file that
21  * accompanied this code).
22  *
23  * You should have received a copy of the GNU General Public License version
24  * 2 along with this work; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26  *
27  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28  * or visit www.oracle.com if you need additional information or have any
29  * questions.
30  */
31 
32 #include "ti_logging.h"
33 
34 #include "art_jvmti.h"
35 
36 #include "base/logging.h"
37 #include "base/mutex.h"
38 #include "base/strlcpy.h"
39 #include "cmdline_types.h"
40 #include "jvmti.h"
41 #include "thread-current-inl.h"
42 
43 namespace openjdkjvmti {
44 
GetLastError(jvmtiEnv * env,char ** data)45 jvmtiError LogUtil::GetLastError(jvmtiEnv* env, char** data) {
46   if (env == nullptr || data == nullptr) {
47     return ERR(INVALID_ENVIRONMENT);
48   }
49   ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env);
50   art::MutexLock mu(art::Thread::Current(), tienv->last_error_mutex_);
51   if (tienv->last_error_.empty()) {
52     return ERR(ABSENT_INFORMATION);
53   }
54   const size_t size = tienv->last_error_.size() + 1;
55   char* out;
56   jvmtiError err = tienv->Allocate(size, reinterpret_cast<unsigned char**>(&out));
57   if (err != OK) {
58     return err;
59   }
60   strlcpy(out, tienv->last_error_.c_str(), size);
61   *data = out;
62   return OK;
63 }
64 
ClearLastError(jvmtiEnv * env)65 jvmtiError LogUtil::ClearLastError(jvmtiEnv* env) {
66   if (env == nullptr) {
67     return ERR(INVALID_ENVIRONMENT);
68   }
69   ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env);
70   art::MutexLock mu(art::Thread::Current(), tienv->last_error_mutex_);
71   tienv->last_error_.clear();
72   return OK;
73 }
74 
SetVerboseFlagExt(jvmtiEnv * env,const char * data,jboolean enable)75 jvmtiError LogUtil::SetVerboseFlagExt(jvmtiEnv* env, const char* data, jboolean enable) {
76   if (env == nullptr) {
77     return ERR(INVALID_ENVIRONMENT);
78   } else if (data == nullptr) {
79     return ERR(NULL_POINTER);
80   }
81   bool new_value = (enable == JNI_TRUE) ? true : false;
82   art::CmdlineType<art::LogVerbosity> cmdline_parser;
83   std::string parse_data(data);
84   art::CmdlineType<art::LogVerbosity>::Result result = cmdline_parser.Parse(parse_data);
85   if (result.IsError()) {
86     JVMTI_LOG(INFO, env) << "Invalid verbose argument: '" << parse_data << "'. Error was "
87                          << result.GetMessage();
88     return ERR(ILLEGAL_ARGUMENT);
89   }
90 
91   const art::LogVerbosity& input_verbosity = result.GetValue();
92   const bool* verbosity_arr = reinterpret_cast<const bool*>(&input_verbosity);
93   bool* g_log_verbosity_arr = reinterpret_cast<bool*>(&art::gLogVerbosity);
94   // Copy/invert the verbosity byte-by-byte (sizeof(bool) == 1).
95   for (size_t i = 0; i < sizeof(art::LogVerbosity); i++) {
96     if (verbosity_arr[i]) {
97       g_log_verbosity_arr[i] = new_value;
98     }
99   }
100   return OK;
101 }
102 
SetVerboseFlag(jvmtiEnv * env ATTRIBUTE_UNUSED,jvmtiVerboseFlag flag,jboolean value)103 jvmtiError LogUtil::SetVerboseFlag(jvmtiEnv* env ATTRIBUTE_UNUSED,
104                                    jvmtiVerboseFlag flag,
105                                    jboolean value) {
106   if (flag == jvmtiVerboseFlag::JVMTI_VERBOSE_OTHER) {
107     // OTHER is special, as it's 0, so can't do a bit check.
108     bool val = (value == JNI_TRUE) ? true : false;
109 
110     art::gLogVerbosity.collector = val;
111     art::gLogVerbosity.compiler = val;
112     art::gLogVerbosity.deopt = val;
113     art::gLogVerbosity.heap = val;
114     art::gLogVerbosity.interpreter = val;
115     art::gLogVerbosity.jdwp = val;
116     art::gLogVerbosity.jit = val;
117     art::gLogVerbosity.monitor = val;
118     art::gLogVerbosity.oat = val;
119     art::gLogVerbosity.profiler = val;
120     art::gLogVerbosity.signals = val;
121     art::gLogVerbosity.simulator = val;
122     art::gLogVerbosity.startup = val;
123     art::gLogVerbosity.third_party_jni = val;
124     art::gLogVerbosity.threads = val;
125     art::gLogVerbosity.verifier = val;
126     // Do not set verifier-debug.
127     art::gLogVerbosity.image = val;
128     art::gLogVerbosity.plugin = val;
129 
130     // Note: can't switch systrace_lock_logging. That requires changing entrypoints.
131 
132     art::gLogVerbosity.agents = val;
133   } else {
134     // Spec isn't clear whether "flag" is a mask or supposed to be single. We implement the mask
135     // semantics.
136     constexpr std::underlying_type<jvmtiVerboseFlag>::type kMask =
137         jvmtiVerboseFlag::JVMTI_VERBOSE_GC |
138         jvmtiVerboseFlag::JVMTI_VERBOSE_CLASS |
139         jvmtiVerboseFlag::JVMTI_VERBOSE_JNI;
140     if ((flag & ~kMask) != 0) {
141       return ERR(ILLEGAL_ARGUMENT);
142     }
143 
144     bool val = (value == JNI_TRUE) ? true : false;
145 
146     if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_GC) != 0) {
147       art::gLogVerbosity.gc = val;
148     }
149 
150     if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_CLASS) != 0) {
151       art::gLogVerbosity.class_linker = val;
152     }
153 
154     if ((flag & jvmtiVerboseFlag::JVMTI_VERBOSE_JNI) != 0) {
155       art::gLogVerbosity.jni = val;
156     }
157   }
158 
159   return ERR(NONE);
160 }
161 
162 }  // namespace openjdkjvmti
163