@@ -14,60 +14,60 @@ using namespace std;
1414Agent Agent::instance;
1515
1616void Agent::LogMessage (string level, string msg) {
17- if (debug_mode) {
18- cout << " StackImpact Agent: " << level << " : " << msg << endl;
19- }
17+ if (debug_mode) {
18+ cout << " StackImpact Agent: " << level << " : " << msg << endl;
19+ }
2020}
2121
2222
2323void Agent::LogInfo (string msg) {
24- LogMessage (" INFO" , msg);
24+ LogMessage (" INFO" , msg);
2525}
2626
2727
2828void Agent::LogError (string msg) {
29- LogMessage (" ERROR" , msg);
29+ LogMessage (" ERROR" , msg);
3030}
3131
3232
3333bool Agent::CheckJVMTIError (jvmtiEnv *jvmti, jvmtiError error, string msg) {
34- if (error != JVMTI_ERROR_NONE) {
35- if (debug_mode) {
36- char *error_str;
37- error_str = NULL ;
38- jvmti->GetErrorName (error, &error_str);
39- ostringstream error_message;
40- error_message << " JVMTI: "
41- << (error_str == NULL ? " Unknown" : error_str)
42- << " (" << error << " ): "
43- << msg;
44-
45- LogError (error_message.str ());
46-
47- return false ;
48- }
49- }
34+ if (error != JVMTI_ERROR_NONE) {
35+ if (debug_mode) {
36+ char *error_str;
37+ error_str = NULL ;
38+ jvmti->GetErrorName (error, &error_str);
39+ ostringstream error_message;
40+ error_message << " JVMTI: "
41+ << (error_str == NULL ? " Unknown" : error_str)
42+ << " (" << error << " ): "
43+ << msg;
44+
45+ LogError (error_message.str ());
46+
47+ return false ;
48+ }
49+ }
5050
51- return true ;
51+ return true ;
5252}
5353
5454
5555void Agent::Init (JavaVM* vm) {
56- Agent* agent = &Agent::instance;
56+ Agent* agent = &Agent::instance;
5757
58- jvm = vm;
58+ jvm = vm;
5959
60- int rc = jvm->GetEnv ((void **)&agent->jni , JNI_VERSION_1_6);
61- if (rc != JNI_OK && rc != JNI_EDETACHED) {
62- agent->LogError (" GetEnv, JNI_VERSION_1_6" );
63- return ;
64- }
60+ int rc = jvm->GetEnv ((void **)&agent->jni , JNI_VERSION_1_6);
61+ if (rc != JNI_OK && rc != JNI_EDETACHED) {
62+ agent->LogError (" GetEnv, JNI_VERSION_1_6" );
63+ return ;
64+ }
6565
66- rc = jvm->GetEnv ((void **)&agent->jvmti , JVMTI_VERSION_1_2);
67- if (rc != JNI_OK) {
68- agent->LogError (" GetEnv, JVMTI_VERSION_1_2" );
69- return ;
70- }
66+ rc = jvm->GetEnv ((void **)&agent->jvmti , JVMTI_VERSION_1_2);
67+ if (rc != JNI_OK) {
68+ agent->LogError (" GetEnv, JVMTI_VERSION_1_2" );
69+ return ;
70+ }
7171}
7272
7373
@@ -93,139 +93,139 @@ void LoadAllMethodIDs(jvmtiEnv* jvmti) {
9393
9494
9595void JNICALL ClassLoad (jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
96- jclass klass) {
96+ jclass klass) {
9797}
9898
9999
100100void JNICALL ClassPrepare (jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
101- jclass klass) {
102- LoadMethodIDs (jvmti_env, klass);
101+ jclass klass) {
102+ LoadMethodIDs (jvmti_env, klass);
103103}
104104
105105
106106void JNICALL CompiledMethodLoad (jvmtiEnv* jvmti, jmethodID method,
107- jint code_size, const void * code_addr,
108- jint map_length, const jvmtiAddrLocationMap* map,
109- const void * compile_info) {
107+ jint code_size, const void * code_addr,
108+ jint map_length, const jvmtiAddrLocationMap* map,
109+ const void * compile_info) {
110110}
111111
112112
113113bool Agent::Attach () {
114- if (is_attached) {
115- return true ;
116- }
117-
118- cpu_profiler = new CPUProfiler ();
119- lock_profiler = new LockProfiler ();
120-
121- if (dlsym (RTLD_DEFAULT, " AsyncGetCallTrace" ) == NULL ) {
122- LogError (" AsyncGetCallTrace not available." );
123- return false ;
124- }
125-
126- jvmtiError error;
127-
128- jvmtiCapabilities potentialCapabilities = {0 };
129- jvmti->GetPotentialCapabilities (&potentialCapabilities);
130-
131- jvmtiCapabilities capabilities = {0 };
132- jvmtiEventCallbacks callbacks = {0 };
133-
134- callbacks.ClassLoad = ClassLoad;
135- callbacks.ClassPrepare = ClassPrepare;
136-
137- if (potentialCapabilities.can_generate_compiled_method_load_events ) {
138- capabilities.can_generate_compiled_method_load_events = 1 ;
139- callbacks.CompiledMethodLoad = CompiledMethodLoad;
140- }
141- else {
142- LogInfo (" Missing capability: can_generate_compiled_method_load_events" );
143- }
144-
145- if (potentialCapabilities.can_get_source_file_name ) {
146- capabilities.can_get_source_file_name = 1 ;
147- }
148- else {
149- LogInfo (" Missing capability: can_get_source_file_name" );
150- }
151-
152- if (potentialCapabilities.can_get_line_numbers ) {
153- capabilities.can_get_line_numbers = 1 ;
154- }
155- else {
156- LogInfo (" Missing capability: can_get_line_numbers" );
157- }
158-
159- if (potentialCapabilities.can_generate_monitor_events ) {
160- capabilities.can_generate_monitor_events = 1 ;
161- callbacks.MonitorContendedEnter = LockProfiler::MonitorContendedEnter;
162- callbacks.MonitorContendedEntered = LockProfiler::MonitorContendedEntered;
163- }
164- else {
165- LogInfo (" Missing capability: can_generate_monitor_events" );
166- }
167-
168- if (potentialCapabilities.can_tag_objects ) {
169- capabilities.can_tag_objects = 1 ;
170- }
171- else {
172- LogInfo (" Missing capability: can_tag_objects" );
173- }
174-
175- error = jvmti->AddCapabilities (&capabilities);
176- CheckJVMTIError (jvmti, error, " AddCapabilities" );
177-
178- error = jvmti->SetEventCallbacks (&callbacks, sizeof (callbacks));
179- CheckJVMTIError (jvmti, error, " SetEventCallbacks" );
180-
181- jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL );
182- jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL );
183- jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL );
184- // error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);
185- CheckJVMTIError (jvmti, error, " SetEventNotificationMode" );
186-
187- // Initialize methodIDs by calling GetClassMethods, needed for AsyncGetCallTrace to function.
188- LoadAllMethodIDs (jvmti);
189- jvmti->GenerateEvents (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
114+ if (is_attached) {
115+ return true ;
116+ }
117+
118+ cpu_profiler = new CPUProfiler ();
119+ lock_profiler = new LockProfiler ();
120+
121+ if (dlsym (RTLD_DEFAULT, " AsyncGetCallTrace" ) == NULL ) {
122+ LogError (" AsyncGetCallTrace not available." );
123+ return false ;
124+ }
125+
126+ jvmtiError error;
127+
128+ jvmtiCapabilities potentialCapabilities = {0 };
129+ jvmti->GetPotentialCapabilities (&potentialCapabilities);
130+
131+ jvmtiCapabilities capabilities = {0 };
132+ jvmtiEventCallbacks callbacks = {0 };
133+
134+ callbacks.ClassLoad = ClassLoad;
135+ callbacks.ClassPrepare = ClassPrepare;
136+
137+ if (potentialCapabilities.can_generate_compiled_method_load_events ) {
138+ capabilities.can_generate_compiled_method_load_events = 1 ;
139+ callbacks.CompiledMethodLoad = CompiledMethodLoad;
140+ }
141+ else {
142+ LogInfo (" Missing capability: can_generate_compiled_method_load_events" );
143+ }
144+
145+ if (potentialCapabilities.can_get_source_file_name ) {
146+ capabilities.can_get_source_file_name = 1 ;
147+ }
148+ else {
149+ LogInfo (" Missing capability: can_get_source_file_name" );
150+ }
151+
152+ if (potentialCapabilities.can_get_line_numbers ) {
153+ capabilities.can_get_line_numbers = 1 ;
154+ }
155+ else {
156+ LogInfo (" Missing capability: can_get_line_numbers" );
157+ }
158+
159+ if (potentialCapabilities.can_generate_monitor_events ) {
160+ capabilities.can_generate_monitor_events = 1 ;
161+ callbacks.MonitorContendedEnter = LockProfiler::MonitorContendedEnter;
162+ callbacks.MonitorContendedEntered = LockProfiler::MonitorContendedEntered;
163+ }
164+ else {
165+ LogInfo (" Missing capability: can_generate_monitor_events" );
166+ }
167+
168+ if (potentialCapabilities.can_tag_objects ) {
169+ capabilities.can_tag_objects = 1 ;
170+ }
171+ else {
172+ LogInfo (" Missing capability: can_tag_objects" );
173+ }
174+
175+ error = jvmti->AddCapabilities (&capabilities);
176+ CheckJVMTIError (jvmti, error, " AddCapabilities" );
177+
178+ error = jvmti->SetEventCallbacks (&callbacks, sizeof (callbacks));
179+ CheckJVMTIError (jvmti, error, " SetEventCallbacks" );
180+
181+ jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL );
182+ jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL );
183+ jvmti->SetEventNotificationMode (JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL );
184+ // error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);
185+ CheckJVMTIError (jvmti, error, " SetEventNotificationMode" );
186+
187+ // Initialize methodIDs by calling GetClassMethods, needed for AsyncGetCallTrace to function.
188+ LoadAllMethodIDs (jvmti);
189+ jvmti->GenerateEvents (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
190190 jvmti->GenerateEvents (JVMTI_EVENT_COMPILED_METHOD_LOAD);
191191
192- is_attached = true ;
193- LogInfo (" The JVMTI agent has been attached successfuly." );
192+ is_attached = true ;
193+ LogInfo (" The JVMTI agent has been attached successfuly." );
194194
195- return true ;
195+ return true ;
196196}
197197
198198
199199extern " C" JNIEXPORT jint JNICALL Agent_OnLoad (JavaVM *jvm, char *options, void *reserved) {
200- // the agent is not loaded via agentpath, it's loaded from java agent
201- return JNI_ERR;
200+ // the agent is not loaded via agentpath, it's loaded from java agent
201+ return JNI_ERR;
202202}
203203
204204
205205extern " C" JNIEXPORT jint JNICALL Agent_OnAttach (JavaVM *jvm, char *options, void *reserved) {
206- return JNI_ERR;
206+ return JNI_ERR;
207207}
208208
209209
210210extern " C" JNIEXPORT void JNICALL Agent_OnUnload (JavaVM *vm) {
211- Agent::instance.LogInfo (" Agent_OnUnload" );
211+ Agent::instance.LogInfo (" Agent_OnUnload" );
212212}
213213
214214
215215extern " C" JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM* jvm, void * reserved) {
216- Agent* agent = &Agent::instance;
217-
218- agent->Init (jvm);
216+ Agent* agent = &Agent::instance;
217+
218+ agent->Init (jvm);
219219
220- return JNI_VERSION_1_6;
220+ return JNI_VERSION_1_6;
221221}
222222
223223
224224extern " C" JNIEXPORT jboolean JNICALL Java_com_stackimpact_agent_Agent_attach (JNIEnv* env, jobject unused, jboolean debug_mode) {
225- Agent* agent = &Agent::instance;
226-
227- agent->jni = env;
228- agent->debug_mode = (bool )debug_mode;
225+ Agent* agent = &Agent::instance;
226+
227+ agent->jni = env;
228+ agent->debug_mode = (bool )debug_mode;
229229
230- return agent->Attach ();
230+ return agent->Attach ();
231231}
0 commit comments