src/share/vm/prims/jvmtiExport.cpp

Print this page




2174   JvmtiCurrentBreakpoints::gc_epilogue();
2175 }
2176 
2177 // Onload raw monitor transition.
2178 void JvmtiExport::transition_pending_onload_raw_monitors() {
2179   JvmtiPendingMonitors::transition_raw_monitors();
2180 }
2181 
2182 ////////////////////////////////////////////////////////////////////////////////////////////////
2183 
2184 // type for the Agent_OnAttach entry point
2185 extern "C" {
2186   typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
2187 }
2188 
2189 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
2190   char ebuf[1024];
2191   char buffer[JVM_MAXPATHLEN];
2192   void* library = NULL;
2193   jint result = JNI_ERR;


2194 
2195   // get agent name and options
2196   const char* agent = op->arg(0);
2197   const char* absParam = op->arg(1);
2198   const char* options = op->arg(2);
2199 
2200   // The abs paramter should be "true" or "false"
2201   bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
2202 


2203 
2204   // If the path is absolute we attempt to load the library. Otherwise we try to
2205   // load it from the standard dll directory.

2206 

2207   if (is_absolute_path) {
2208     library = os::dll_load(agent, ebuf, sizeof ebuf);
2209   } else {
2210     // Try to load the agent from the standard dll directory
2211     if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
2212                            agent)) {
2213       library = os::dll_load(buffer, ebuf, sizeof ebuf);
2214     }
2215     if (library == NULL) {
2216       // not found - try local path
2217       char ns[1] = {0};
2218       if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) {
2219         library = os::dll_load(buffer, ebuf, sizeof ebuf);
2220       }
2221     }
2222   }
2223 




2224   // If the library was loaded then we attempt to invoke the Agent_OnAttach
2225   // function
2226   if (library != NULL) {
2227 
2228     // Lookup the Agent_OnAttach function
2229     OnAttachEntry_t on_attach_entry = NULL;
2230     const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
2231     for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_attach_symbols); symbol_index++) {
2232       on_attach_entry =
2233         CAST_TO_FN_PTR(OnAttachEntry_t, os::dll_lookup(library, on_attach_symbols[symbol_index]));
2234       if (on_attach_entry != NULL) break;
2235     }
2236 
2237     if (on_attach_entry == NULL) {
2238       // Agent_OnAttach missing - unload library

2239       os::dll_unload(library);


2240     } else {
2241       // Invoke the Agent_OnAttach function
2242       JavaThread* THREAD = JavaThread::current();
2243       {
2244         extern struct JavaVM_ main_vm;
2245         JvmtiThreadEventMark jem(THREAD);
2246         JvmtiJavaThreadEventTransition jet(THREAD);
2247 
2248         result = (*on_attach_entry)(&main_vm, (char*)options, NULL);
2249       }
2250 
2251       // Agent_OnAttach may have used JNI
2252       if (HAS_PENDING_EXCEPTION) {
2253         CLEAR_PENDING_EXCEPTION;
2254       }
2255 
2256       // If OnAttach returns JNI_OK then we add it to the list of
2257       // agent libraries so that we can call Agent_OnUnload later.
2258       if (result == JNI_OK) {
2259         Arguments::add_loaded_agent(agent, (char*)options, is_absolute_path, library);


2260       }
2261 
2262       // Agent_OnAttach executed so completion status is JNI_OK
2263       st->print_cr("%d", result);
2264       result = JNI_OK;
2265     }
2266   }
2267   return result;
2268 }
2269 
2270 ////////////////////////////////////////////////////////////////////////////////////////////////
2271 
2272 // Setup current current thread for event collection.
2273 void JvmtiEventCollector::setup_jvmti_thread_state() {
2274   // set this event collector to be the current one.
2275   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2276   // state can only be NULL if the current thread is exiting which
2277   // should not happen since we're trying to configure for event collection
2278   guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2279   if (is_vm_object_alloc_event()) {




2174   JvmtiCurrentBreakpoints::gc_epilogue();
2175 }
2176 
2177 // Onload raw monitor transition.
2178 void JvmtiExport::transition_pending_onload_raw_monitors() {
2179   JvmtiPendingMonitors::transition_raw_monitors();
2180 }
2181 
2182 ////////////////////////////////////////////////////////////////////////////////////////////////
2183 
2184 // type for the Agent_OnAttach entry point
2185 extern "C" {
2186   typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
2187 }
2188 
2189 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
2190   char ebuf[1024];
2191   char buffer[JVM_MAXPATHLEN];
2192   void* library = NULL;
2193   jint result = JNI_ERR;
2194   const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
2195   size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols);
2196 
2197   // get agent name and options
2198   const char* agent = op->arg(0);
2199   const char* absParam = op->arg(1);
2200   const char* options = op->arg(2);
2201 
2202   // The abs paramter should be "true" or "false"
2203   bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
2204 
2205   // Initially marked as invalid. It will be set to valid if we can find the agent
2206   AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL);
2207 
2208   // Check for statically linked in agent. If not found then if the path is
2209   // absolute we attempt to load the library. Otherwise we try to load it
2210   // from the standard dll directory.
2211 
2212   if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) {
2213     if (is_absolute_path) {
2214       library = os::dll_load(agent, ebuf, sizeof ebuf);
2215     } else {
2216       // Try to load the agent from the standard dll directory
2217       if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
2218                              agent)) {
2219         library = os::dll_load(buffer, ebuf, sizeof ebuf);
2220       }
2221       if (library == NULL) {
2222         // not found - try local path
2223         char ns[1] = {0};
2224         if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) {
2225           library = os::dll_load(buffer, ebuf, sizeof ebuf);
2226         }
2227       }
2228     }
2229     if (library != NULL) {
2230       agent_lib->set_os_lib(library);
2231       agent_lib->set_valid();
2232     }
2233   }
2234   // If the library was loaded then we attempt to invoke the Agent_OnAttach
2235   // function
2236   if (agent_lib->valid()) {

2237     // Lookup the Agent_OnAttach function
2238     OnAttachEntry_t on_attach_entry = NULL;
2239     on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t,
2240        os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries));





2241     if (on_attach_entry == NULL) {
2242       // Agent_OnAttach missing - unload library
2243       if (!agent_lib->is_static_lib()) {
2244         os::dll_unload(library);
2245       }
2246       delete agent_lib;
2247     } else {
2248       // Invoke the Agent_OnAttach function
2249       JavaThread* THREAD = JavaThread::current();
2250       {
2251         extern struct JavaVM_ main_vm;
2252         JvmtiThreadEventMark jem(THREAD);
2253         JvmtiJavaThreadEventTransition jet(THREAD);
2254 
2255         result = (*on_attach_entry)(&main_vm, (char*)options, NULL);
2256       }
2257 
2258       // Agent_OnAttach may have used JNI
2259       if (HAS_PENDING_EXCEPTION) {
2260         CLEAR_PENDING_EXCEPTION;
2261       }
2262 
2263       // If OnAttach returns JNI_OK then we add it to the list of
2264       // agent libraries so that we can call Agent_OnUnload later.
2265       if (result == JNI_OK) {
2266         Arguments::add_loaded_agent(agent_lib);
2267       } else {
2268         delete agent_lib;
2269       }
2270 
2271       // Agent_OnAttach executed so completion status is JNI_OK
2272       st->print_cr("%d", result);
2273       result = JNI_OK;
2274     }
2275   }
2276   return result;
2277 }
2278 
2279 ////////////////////////////////////////////////////////////////////////////////////////////////
2280 
2281 // Setup current current thread for event collection.
2282 void JvmtiEventCollector::setup_jvmti_thread_state() {
2283   // set this event collector to be the current one.
2284   JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2285   // state can only be NULL if the current thread is exiting which
2286   // should not happen since we're trying to configure for event collection
2287   guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2288   if (is_vm_object_alloc_event()) {