hotspot/src/share/vm/prims/jvmtiExport.cpp
changeset 19553 9bbd930be684
parent 18940 d39d4765e6cb
child 22897 77e1024a4a14
equal deleted inserted replaced
19530:0c39c59e1624 19553:9bbd930be684
  2189 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
  2189 jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
  2190   char ebuf[1024];
  2190   char ebuf[1024];
  2191   char buffer[JVM_MAXPATHLEN];
  2191   char buffer[JVM_MAXPATHLEN];
  2192   void* library = NULL;
  2192   void* library = NULL;
  2193   jint result = JNI_ERR;
  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);
  2194 
  2196 
  2195   // get agent name and options
  2197   // get agent name and options
  2196   const char* agent = op->arg(0);
  2198   const char* agent = op->arg(0);
  2197   const char* absParam = op->arg(1);
  2199   const char* absParam = op->arg(1);
  2198   const char* options = op->arg(2);
  2200   const char* options = op->arg(2);
  2199 
  2201 
  2200   // The abs paramter should be "true" or "false"
  2202   // The abs paramter should be "true" or "false"
  2201   bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
  2203   bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
  2202 
  2204 
  2203 
  2205   // Initially marked as invalid. It will be set to valid if we can find the agent
  2204   // If the path is absolute we attempt to load the library. Otherwise we try to
  2206   AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL);
  2205   // load it from the standard dll directory.
  2207 
  2206 
  2208   // Check for statically linked in agent. If not found then if the path is
  2207   if (is_absolute_path) {
  2209   // absolute we attempt to load the library. Otherwise we try to load it
  2208     library = os::dll_load(agent, ebuf, sizeof ebuf);
  2210   // from the standard dll directory.
  2209   } else {
  2211 
  2210     // Try to load the agent from the standard dll directory
  2212   if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) {
  2211     if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
  2213     if (is_absolute_path) {
  2212                            agent)) {
  2214       library = os::dll_load(agent, ebuf, sizeof ebuf);
  2213       library = os::dll_load(buffer, ebuf, sizeof ebuf);
  2215     } else {
  2214     }
  2216       // Try to load the agent from the standard dll directory
  2215     if (library == NULL) {
  2217       if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
  2216       // not found - try local path
  2218                              agent)) {
  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);
  2219         library = os::dll_load(buffer, ebuf, sizeof ebuf);
  2220       }
  2220       }
  2221     }
  2221       if (library == NULL) {
  2222   }
  2222         // not found - try local path
  2223 
  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   }
  2224   // If the library was loaded then we attempt to invoke the Agent_OnAttach
  2234   // If the library was loaded then we attempt to invoke the Agent_OnAttach
  2225   // function
  2235   // function
  2226   if (library != NULL) {
  2236   if (agent_lib->valid()) {
  2227 
       
  2228     // Lookup the Agent_OnAttach function
  2237     // Lookup the Agent_OnAttach function
  2229     OnAttachEntry_t on_attach_entry = NULL;
  2238     OnAttachEntry_t on_attach_entry = NULL;
  2230     const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
  2239     on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t,
  2231     for (uint symbol_index = 0; symbol_index < ARRAY_SIZE(on_attach_symbols); symbol_index++) {
  2240        os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries));
  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) {
  2241     if (on_attach_entry == NULL) {
  2238       // Agent_OnAttach missing - unload library
  2242       // Agent_OnAttach missing - unload library
  2239       os::dll_unload(library);
  2243       if (!agent_lib->is_static_lib()) {
       
  2244         os::dll_unload(library);
       
  2245       }
       
  2246       delete agent_lib;
  2240     } else {
  2247     } else {
  2241       // Invoke the Agent_OnAttach function
  2248       // Invoke the Agent_OnAttach function
  2242       JavaThread* THREAD = JavaThread::current();
  2249       JavaThread* THREAD = JavaThread::current();
  2243       {
  2250       {
  2244         extern struct JavaVM_ main_vm;
  2251         extern struct JavaVM_ main_vm;
  2254       }
  2261       }
  2255 
  2262 
  2256       // If OnAttach returns JNI_OK then we add it to the list of
  2263       // If OnAttach returns JNI_OK then we add it to the list of
  2257       // agent libraries so that we can call Agent_OnUnload later.
  2264       // agent libraries so that we can call Agent_OnUnload later.
  2258       if (result == JNI_OK) {
  2265       if (result == JNI_OK) {
  2259         Arguments::add_loaded_agent(agent, (char*)options, is_absolute_path, library);
  2266         Arguments::add_loaded_agent(agent_lib);
       
  2267       } else {
       
  2268         delete agent_lib;
  2260       }
  2269       }
  2261 
  2270 
  2262       // Agent_OnAttach executed so completion status is JNI_OK
  2271       // Agent_OnAttach executed so completion status is JNI_OK
  2263       st->print_cr("%d", result);
  2272       st->print_cr("%d", result);
  2264       result = JNI_OK;
  2273       result = JNI_OK;