J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of Sun Microsystems nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <stddef.h> |
| 36 | |
| 37 | #include "jni.h" |
| 38 | #include "jvmti.h" |
| 39 | |
| 40 | #include "agent_util.h" |
| 41 | |
| 42 | #include "Monitor.hpp" |
| 43 | #include "Thread.hpp" |
| 44 | #include "Agent.hpp" |
| 45 | |
| 46 | /* Implementation of the Agent class */ |
| 47 | |
| 48 | /* Given a jvmtiEnv* and jthread, find the Thread instance */ |
| 49 | Thread * |
| 50 | Agent::get_thread(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) |
| 51 | { |
| 52 | jvmtiError err; |
| 53 | Thread *t; |
| 54 | |
| 55 | /* This should always be in the Thread Local Storage */ |
| 56 | t = NULL; |
| 57 | err = jvmti->GetThreadLocalStorage(thread, (void**)&t); |
| 58 | check_jvmti_error(jvmti, err, "get thread local storage"); |
| 59 | if ( t == NULL ) { |
| 60 | /* This jthread has never been seen before? */ |
| 61 | stdout_message("WARNING: Never before seen jthread?\n"); |
| 62 | t = new Thread(jvmti, env, thread); |
| 63 | err = jvmti->SetThreadLocalStorage(thread, (const void*)t); |
| 64 | check_jvmti_error(jvmti, err, "set thread local storage"); |
| 65 | } |
| 66 | return t; |
| 67 | } |
| 68 | |
| 69 | /* Given a jvmtiEnv* and jobject, find the Monitor instance or create one */ |
| 70 | Monitor * |
| 71 | Agent::get_monitor(jvmtiEnv *jvmti, JNIEnv *env, jobject object) |
| 72 | { |
| 73 | jvmtiError err; |
| 74 | Monitor *m; |
| 75 | |
| 76 | /* We use tags to track these, the tag is the Monitor pointer */ |
| 77 | err = jvmti->RawMonitorEnter(lock); { |
| 78 | check_jvmti_error(jvmti, err, "raw monitor enter"); |
| 79 | |
| 80 | /* The raw monitor enter/exit protects us from creating two |
| 81 | * instances for the same object. |
| 82 | */ |
| 83 | jlong tag; |
| 84 | |
| 85 | m = NULL; |
| 86 | tag = (jlong)0; |
| 87 | err = jvmti->GetTag(object, &tag); |
| 88 | check_jvmti_error(jvmti, err, "get tag"); |
| 89 | /*LINTED*/ |
| 90 | m = (Monitor *)(void *)(ptrdiff_t)tag; |
| 91 | if ( m == NULL ) { |
| 92 | m = new Monitor(jvmti, env, object); |
| 93 | /*LINTED*/ |
| 94 | tag = (jlong)(ptrdiff_t)(void *)m; |
| 95 | err = jvmti->SetTag(object, tag); |
| 96 | check_jvmti_error(jvmti, err, "set tag"); |
| 97 | /* Save monitor on list */ |
| 98 | monitor_list = (Monitor**)realloc((void*)monitor_list, |
| 99 | (monitor_count+1)*(int)sizeof(Monitor*)); |
| 100 | monitor_list[monitor_count++] = m; |
| 101 | } |
| 102 | } err = jvmti->RawMonitorExit(lock); |
| 103 | check_jvmti_error(jvmti, err, "raw monitor exit"); |
| 104 | |
| 105 | return m; |
| 106 | } |
| 107 | |
| 108 | /* VM initialization and VM death calls to Agent */ |
| 109 | Agent::Agent(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) |
| 110 | { |
| 111 | jvmtiError err; |
| 112 | |
| 113 | stdout_message("Agent created..\n"); |
| 114 | stdout_message("VMInit...\n"); |
| 115 | /* Create a Monitor lock to use */ |
| 116 | err = jvmti->CreateRawMonitor("waiters Agent lock", &lock); |
| 117 | check_jvmti_error(jvmti, err, "create raw monitor"); |
| 118 | /* Start monitor list */ |
| 119 | monitor_count = 0; |
| 120 | monitor_list = (Monitor**)malloc((int)sizeof(Monitor*)); |
| 121 | } |
| 122 | |
| 123 | Agent::~Agent() |
| 124 | { |
| 125 | stdout_message("Agent reclaimed..\n"); |
| 126 | } |
| 127 | |
| 128 | void Agent::vm_death(jvmtiEnv *jvmti, JNIEnv *env) |
| 129 | { |
| 130 | jvmtiError err; |
| 131 | |
| 132 | /* Delete all Monitors we allocated */ |
| 133 | for ( int i = 0; i < (int)monitor_count; i++ ) { |
| 134 | delete monitor_list[i]; |
| 135 | } |
| 136 | free(monitor_list); |
| 137 | /* Destroy the Monitor lock to use */ |
| 138 | err = jvmti->DestroyRawMonitor(lock); |
| 139 | check_jvmti_error(jvmti, err, "destroy raw monitor"); |
| 140 | /* Print death message */ |
| 141 | stdout_message("VMDeath...\n"); |
| 142 | } |
| 143 | |
| 144 | /* Thread start event, setup a new thread */ |
| 145 | void Agent::thread_start(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) |
| 146 | { |
| 147 | jvmtiError err; |
| 148 | Thread *t; |
| 149 | |
| 150 | /* Allocate a new Thread instance, put it in the Thread Local |
| 151 | * Storage for easy access later. |
| 152 | */ |
| 153 | t = new Thread(jvmti, env, thread); |
| 154 | err = jvmti->SetThreadLocalStorage(thread, (const void*)t); |
| 155 | check_jvmti_error(jvmti, err, "set thread local storage"); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* Thread end event, we need to reclaim the space */ |
| 160 | void Agent::thread_end(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) |
| 161 | { |
| 162 | jvmtiError err; |
| 163 | Thread *t; |
| 164 | |
| 165 | /* Find the thread */ |
| 166 | t = get_thread(jvmti, env, thread); |
| 167 | |
| 168 | /* Clear out the Thread Local Storage */ |
| 169 | err = jvmti->SetThreadLocalStorage(thread, (const void*)NULL); |
| 170 | check_jvmti_error(jvmti, err, "set thread local storage"); |
| 171 | |
| 172 | /* Reclaim the C++ object space */ |
| 173 | delete t; |
| 174 | } |
| 175 | |
| 176 | /* Monitor contention begins for a thread. */ |
| 177 | void Agent::monitor_contended_enter(jvmtiEnv* jvmti, JNIEnv *env, |
| 178 | jthread thread, jobject object) |
| 179 | { |
| 180 | get_monitor(jvmti, env, object)->contended(); |
| 181 | get_thread(jvmti, env, thread)-> |
| 182 | monitor_contended_enter(jvmti, env, thread, object); |
| 183 | } |
| 184 | |
| 185 | /* Monitor contention ends for a thread. */ |
| 186 | void Agent::monitor_contended_entered(jvmtiEnv* jvmti, JNIEnv *env, |
| 187 | jthread thread, jobject object) |
| 188 | { |
| 189 | /* Do nothing for now */ |
| 190 | } |
| 191 | |
| 192 | /* Monitor wait begins for a thread. */ |
| 193 | void Agent::monitor_wait(jvmtiEnv* jvmti, JNIEnv *env, |
| 194 | jthread thread, jobject object, jlong timeout) |
| 195 | { |
| 196 | get_monitor(jvmti, env, object)->waited(); |
| 197 | get_thread(jvmti, env, thread)-> |
| 198 | monitor_wait(jvmti, env, thread, object, timeout); |
| 199 | } |
| 200 | |
| 201 | /* Monitor wait ends for a thread. */ |
| 202 | void Agent::monitor_waited(jvmtiEnv* jvmti, JNIEnv *env, |
| 203 | jthread thread, jobject object, jboolean timed_out) |
| 204 | { |
| 205 | if ( timed_out ) { |
| 206 | get_monitor(jvmti, env, object)->timeout(); |
| 207 | } |
| 208 | get_thread(jvmti, env, thread)-> |
| 209 | monitor_waited(jvmti, env, thread, object, timed_out); |
| 210 | } |
| 211 | |
| 212 | /* A tagged object has been freed */ |
| 213 | void Agent::object_free(jvmtiEnv* jvmti, jlong tag) |
| 214 | { |
| 215 | /* We just cast the tag to a C++ pointer and delete it. |
| 216 | * we know it can only be a Monitor *. |
| 217 | */ |
| 218 | Monitor *m; |
| 219 | /*LINTED*/ |
| 220 | m = (Monitor *)(ptrdiff_t)tag; |
| 221 | delete m; |
| 222 | } |