The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Native method resolution. |
| 19 | * |
| 20 | * Currently the "Dalvik native" methods are only used for internal methods. |
| 21 | * Someday we may want to export the interface as a faster but riskier |
| 22 | * alternative to JNI. |
| 23 | */ |
| 24 | #include "Dalvik.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <dlfcn.h> |
| 28 | |
| 29 | static void freeSharedLibEntry(void* ptr); |
| 30 | static void* lookupSharedLibMethod(const Method* method); |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * Initialize the native code loader. |
| 35 | */ |
| 36 | bool dvmNativeStartup(void) |
| 37 | { |
| 38 | gDvm.nativeLibs = dvmHashTableCreate(4, freeSharedLibEntry); |
| 39 | if (gDvm.nativeLibs == NULL) |
| 40 | return false; |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Free up our tables. |
| 47 | */ |
| 48 | void dvmNativeShutdown(void) |
| 49 | { |
| 50 | dvmHashTableFree(gDvm.nativeLibs); |
| 51 | gDvm.nativeLibs = NULL; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * Resolve a native method and invoke it. |
| 57 | * |
| 58 | * This is executed as if it were a native bridge or function. If the |
| 59 | * resolution succeeds, method->insns is replaced, and we don't go through |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 60 | * here again unless the method is unregistered. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | * |
| 62 | * Initializes method's class if necessary. |
| 63 | * |
| 64 | * An exception is thrown on resolution failure. |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 65 | * |
| 66 | * (This should not be taking "const Method*", because it modifies the |
| 67 | * structure, but the declaration needs to match the DalvikBridgeFunc |
| 68 | * type definition.) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | */ |
| 70 | void dvmResolveNativeMethod(const u4* args, JValue* pResult, |
| 71 | const Method* method, Thread* self) |
| 72 | { |
| 73 | ClassObject* clazz = method->clazz; |
| 74 | void* func; |
| 75 | |
| 76 | /* |
| 77 | * If this is a static method, it could be called before the class |
| 78 | * has been initialized. |
| 79 | */ |
| 80 | if (dvmIsStaticMethod(method)) { |
| 81 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 82 | assert(dvmCheckException(dvmThreadSelf())); |
| 83 | return; |
| 84 | } |
| 85 | } else { |
| 86 | assert(dvmIsClassInitialized(clazz) || |
| 87 | dvmIsClassInitializing(clazz)); |
| 88 | } |
| 89 | |
| 90 | /* start with our internal-native methods */ |
| 91 | func = dvmLookupInternalNativeMethod(method); |
| 92 | if (func != NULL) { |
| 93 | /* resolution always gets the same answer, so no race here */ |
| 94 | IF_LOGVV() { |
| 95 | char* desc = dexProtoCopyMethodDescriptor(&method->prototype); |
| 96 | LOGVV("+++ resolved native %s.%s %s, invoking\n", |
| 97 | clazz->descriptor, method->name, desc); |
| 98 | free(desc); |
| 99 | } |
| 100 | if (dvmIsSynchronizedMethod(method)) { |
| 101 | LOGE("ERROR: internal-native can't be declared 'synchronized'\n"); |
| 102 | LOGE("Failing on %s.%s\n", method->clazz->descriptor, method->name); |
| 103 | dvmAbort(); // harsh, but this is VM-internal problem |
| 104 | } |
| 105 | DalvikBridgeFunc dfunc = (DalvikBridgeFunc) func; |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 106 | dvmSetNativeFunc((Method*) method, dfunc, NULL); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 107 | dfunc(args, pResult, method, self); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | /* now scan any DLLs we have loaded for JNI signatures */ |
| 112 | func = lookupSharedLibMethod(method); |
| 113 | if (func != NULL) { |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 114 | /* found it, point it at the JNI bridge and then call it */ |
| 115 | dvmUseJNIBridge((Method*) method, func); |
Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame] | 116 | (*method->nativeFunc)(args, pResult, method, self); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | IF_LOGW() { |
| 121 | char* desc = dexProtoCopyMethodDescriptor(&method->prototype); |
| 122 | LOGW("No implementation found for native %s.%s %s\n", |
| 123 | clazz->descriptor, method->name, desc); |
| 124 | free(desc); |
| 125 | } |
| 126 | |
| 127 | dvmThrowException("Ljava/lang/UnsatisfiedLinkError;", method->name); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* |
| 132 | * =========================================================================== |
| 133 | * Native shared library support |
| 134 | * =========================================================================== |
| 135 | */ |
| 136 | |
| 137 | // TODO? if a ClassLoader is unloaded, we need to unload all DLLs that |
| 138 | // are associated with it. (Or not -- can't determine if native code |
| 139 | // is still using parts of it.) |
| 140 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 141 | typedef enum OnLoadState { |
| 142 | kOnLoadPending = 0, /* initial state, must be zero */ |
| 143 | kOnLoadFailed, |
| 144 | kOnLoadOkay, |
| 145 | } OnLoadState; |
| 146 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 147 | /* |
| 148 | * We add one of these to the hash table for every library we load. The |
| 149 | * hash is on the "pathName" field. |
| 150 | */ |
| 151 | typedef struct SharedLib { |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 152 | char* pathName; /* absolute path to library */ |
| 153 | void* handle; /* from dlopen */ |
| 154 | Object* classLoader; /* ClassLoader we are associated with */ |
| 155 | |
| 156 | pthread_mutex_t onLoadLock; /* guards remaining items */ |
| 157 | pthread_cond_t onLoadCond; /* wait for JNI_OnLoad in other thread */ |
| 158 | u4 onLoadThreadId; /* recursive invocation guard */ |
| 159 | OnLoadState onLoadResult; /* result of earlier JNI_OnLoad */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 160 | } SharedLib; |
| 161 | |
| 162 | /* |
| 163 | * (This is a dvmHashTableLookup callback.) |
| 164 | * |
| 165 | * Find an entry that matches the string. |
| 166 | */ |
| 167 | static int hashcmpNameStr(const void* ventry, const void* vname) |
| 168 | { |
| 169 | const SharedLib* pLib = (const SharedLib*) ventry; |
| 170 | const char* name = (const char*) vname; |
| 171 | |
| 172 | return strcmp(pLib->pathName, name); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * (This is a dvmHashTableLookup callback.) |
| 177 | * |
| 178 | * Find an entry that matches the new entry. |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 179 | * |
| 180 | * We don't compare the class loader here, because you're not allowed to |
| 181 | * have the same shared library associated with more than one CL. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 182 | */ |
| 183 | static int hashcmpSharedLib(const void* ventry, const void* vnewEntry) |
| 184 | { |
| 185 | const SharedLib* pLib = (const SharedLib*) ventry; |
| 186 | const SharedLib* pNewLib = (const SharedLib*) vnewEntry; |
| 187 | |
| 188 | LOGD("--- comparing %p '%s' %p '%s'\n", |
| 189 | pLib, pLib->pathName, pNewLib, pNewLib->pathName); |
| 190 | return strcmp(pLib->pathName, pNewLib->pathName); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Check to see if an entry with the same pathname already exists. |
| 195 | */ |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 196 | static SharedLib* findSharedLibEntry(const char* pathName) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 197 | { |
| 198 | u4 hash = dvmComputeUtf8Hash(pathName); |
| 199 | void* ent; |
| 200 | |
| 201 | ent = dvmHashTableLookup(gDvm.nativeLibs, hash, (void*)pathName, |
| 202 | hashcmpNameStr, false); |
| 203 | return ent; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Add the new entry to the table. |
| 208 | * |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 209 | * Returns the table entry, which will not be the same as "pLib" if the |
| 210 | * entry already exists. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 211 | */ |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 212 | static SharedLib* addSharedLibEntry(SharedLib* pLib) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 213 | { |
| 214 | u4 hash = dvmComputeUtf8Hash(pLib->pathName); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Do the lookup with the "add" flag set. If we add it, we will get |
| 218 | * our own pointer back. If somebody beat us to the punch, we'll get |
| 219 | * their pointer back instead. |
| 220 | */ |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 221 | return dvmHashTableLookup(gDvm.nativeLibs, hash, pLib, hashcmpSharedLib, |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | true); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Free up an entry. (This is a dvmHashTableFree callback.) |
| 227 | */ |
| 228 | static void freeSharedLibEntry(void* ptr) |
| 229 | { |
| 230 | SharedLib* pLib = (SharedLib*) ptr; |
| 231 | |
| 232 | /* |
| 233 | * Calling dlclose() here is somewhat dangerous, because it's possible |
| 234 | * that a thread outside the VM is still accessing the code we loaded. |
| 235 | */ |
| 236 | if (false) |
| 237 | dlclose(pLib->handle); |
| 238 | free(pLib->pathName); |
| 239 | free(pLib); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Convert library name to system-dependent form, e.g. "jpeg" becomes |
| 244 | * "libjpeg.so". |
| 245 | * |
| 246 | * (Should we have this take buffer+len and avoid the alloc? It gets |
| 247 | * called very rarely.) |
| 248 | */ |
| 249 | char* dvmCreateSystemLibraryName(char* libName) |
| 250 | { |
| 251 | char buf[256]; |
| 252 | int len; |
| 253 | |
| 254 | len = snprintf(buf, sizeof(buf), OS_SHARED_LIB_FORMAT_STR, libName); |
| 255 | if (len >= (int) sizeof(buf)) |
| 256 | return NULL; |
| 257 | else |
| 258 | return strdup(buf); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | #if 0 |
| 263 | /* |
| 264 | * Find a library, given the lib's system-dependent name (e.g. "libjpeg.so"). |
| 265 | * |
| 266 | * We need to search through the path defined by the java.library.path |
| 267 | * property. |
| 268 | * |
| 269 | * Returns NULL if the library was not found. |
| 270 | */ |
| 271 | static char* findLibrary(const char* libSysName) |
| 272 | { |
| 273 | char* javaLibraryPath = NULL; |
| 274 | char* testName = NULL; |
| 275 | char* start; |
| 276 | char* cp; |
| 277 | bool done; |
| 278 | |
| 279 | javaLibraryPath = dvmGetProperty("java.library.path"); |
| 280 | if (javaLibraryPath == NULL) |
| 281 | goto bail; |
| 282 | |
| 283 | LOGVV("+++ path is '%s'\n", javaLibraryPath); |
| 284 | |
| 285 | start = cp = javaLibraryPath; |
| 286 | while (cp != NULL) { |
| 287 | char pathBuf[256]; |
| 288 | int len; |
| 289 | |
| 290 | cp = strchr(start, ':'); |
| 291 | if (cp != NULL) |
| 292 | *cp = '\0'; |
| 293 | |
| 294 | len = snprintf(pathBuf, sizeof(pathBuf), "%s/%s", start, libSysName); |
| 295 | if (len >= (int) sizeof(pathBuf)) { |
| 296 | LOGW("Path overflowed %d bytes: '%s' / '%s'\n", |
| 297 | len, start, libSysName); |
| 298 | /* keep going, next one might fit */ |
| 299 | } else { |
| 300 | LOGVV("+++ trying '%s'\n", pathBuf); |
| 301 | if (access(pathBuf, R_OK) == 0) { |
| 302 | testName = strdup(pathBuf); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | start = cp +1; |
| 308 | } |
| 309 | |
| 310 | bail: |
| 311 | free(javaLibraryPath); |
| 312 | return testName; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Load a native shared library, given the system-independent piece of |
| 317 | * the library name. |
| 318 | * |
| 319 | * Throws an exception on failure. |
| 320 | */ |
| 321 | void dvmLoadNativeLibrary(StringObject* libNameObj, Object* classLoader) |
| 322 | { |
| 323 | char* libName = NULL; |
| 324 | char* libSysName = NULL; |
| 325 | char* libPath = NULL; |
| 326 | |
| 327 | /* |
| 328 | * If "classLoader" isn't NULL, call the class loader's "findLibrary" |
| 329 | * method with the lib name. If it returns a non-NULL result, we use |
| 330 | * that as the pathname. |
| 331 | */ |
| 332 | if (classLoader != NULL) { |
| 333 | Method* findLibrary; |
| 334 | Object* findLibResult; |
| 335 | |
| 336 | findLibrary = dvmFindVirtualMethodByDescriptor(classLoader->clazz, |
| 337 | "findLibrary", "(Ljava/lang/String;)Ljava/lang/String;"); |
| 338 | if (findLibrary == NULL) { |
| 339 | LOGW("Could not find findLibrary() in %s\n", |
| 340 | classLoader->clazz->name); |
| 341 | dvmThrowException("Ljava/lang/UnsatisfiedLinkError;", |
| 342 | "findLibrary"); |
| 343 | goto bail; |
| 344 | } |
| 345 | |
| 346 | findLibResult = (Object*)(u4) dvmCallMethod(findLibrary, classLoader, |
| 347 | libNameObj); |
| 348 | if (dvmCheckException()) { |
| 349 | LOGV("returning early on exception\n"); |
| 350 | goto bail; |
| 351 | } |
| 352 | if (findLibResult != NULL) { |
| 353 | /* success! */ |
| 354 | libPath = dvmCreateCstrFromString(libNameObj); |
| 355 | LOGI("Found library through CL: '%s'\n", libPath); |
| 356 | dvmLoadNativeCode(libPath, classLoader); |
| 357 | goto bail; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | libName = dvmCreateCstrFromString(libNameObj); |
| 362 | if (libName == NULL) |
| 363 | goto bail; |
| 364 | libSysName = dvmCreateSystemLibraryName(libName); |
| 365 | if (libSysName == NULL) |
| 366 | goto bail; |
| 367 | |
| 368 | libPath = findLibrary(libSysName); |
| 369 | if (libPath != NULL) { |
| 370 | LOGD("Found library through path: '%s'\n", libPath); |
| 371 | dvmLoadNativeCode(libPath, classLoader); |
| 372 | } else { |
| 373 | LOGW("Unable to locate shared lib matching '%s'\n", libSysName); |
| 374 | dvmThrowException("Ljava/lang/UnsatisfiedLinkError;", libName); |
| 375 | } |
| 376 | |
| 377 | bail: |
| 378 | free(libName); |
| 379 | free(libSysName); |
| 380 | free(libPath); |
| 381 | } |
| 382 | #endif |
| 383 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Check the result of an earlier call to JNI_OnLoad on this library. If |
| 387 | * the call has not yet finished in another thread, wait for it. |
| 388 | */ |
| 389 | static bool checkOnLoadResult(SharedLib* pEntry) |
| 390 | { |
| 391 | Thread* self = dvmThreadSelf(); |
| 392 | if (pEntry->onLoadThreadId == self->threadId) { |
| 393 | /* |
| 394 | * Check this so we don't end up waiting for ourselves. We need |
| 395 | * to return "true" so the caller can continue. |
| 396 | */ |
| 397 | LOGI("threadid=%d: recursive native library load attempt (%s)\n", |
| 398 | self->threadId, pEntry->pathName); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | LOGV("+++ retrieving %s OnLoad status\n", pEntry->pathName); |
| 403 | bool result; |
| 404 | |
| 405 | dvmLockMutex(&pEntry->onLoadLock); |
| 406 | while (pEntry->onLoadResult == kOnLoadPending) { |
| 407 | LOGD("threadid=%d: waiting for %s OnLoad status\n", |
| 408 | self->threadId, pEntry->pathName); |
Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 409 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 410 | pthread_cond_wait(&pEntry->onLoadCond, &pEntry->onLoadLock); |
| 411 | dvmChangeStatus(self, oldStatus); |
| 412 | } |
| 413 | if (pEntry->onLoadResult == kOnLoadOkay) { |
| 414 | LOGV("+++ earlier OnLoad(%s) okay\n", pEntry->pathName); |
| 415 | result = true; |
| 416 | } else { |
| 417 | LOGV("+++ earlier OnLoad(%s) failed\n", pEntry->pathName); |
| 418 | result = false; |
| 419 | } |
| 420 | dvmUnlockMutex(&pEntry->onLoadLock); |
| 421 | return result; |
| 422 | } |
| 423 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 424 | typedef int (*OnLoadFunc)(JavaVM*, void*); |
| 425 | |
| 426 | /* |
| 427 | * Load native code from the specified absolute pathname. Per the spec, |
| 428 | * if we've already loaded a library with the specified pathname, we |
| 429 | * return without doing anything. |
| 430 | * |
| 431 | * TODO? for better results we should absolutify the pathname. For fully |
| 432 | * correct results we should stat to get the inode and compare that. The |
| 433 | * existing implementation is fine so long as everybody is using |
| 434 | * System.loadLibrary. |
| 435 | * |
| 436 | * The library will be associated with the specified class loader. The JNI |
| 437 | * spec says we can't load the same library into more than one class loader. |
| 438 | * |
| 439 | * Returns "true" on success. |
| 440 | */ |
| 441 | bool dvmLoadNativeCode(const char* pathName, Object* classLoader) |
| 442 | { |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 443 | SharedLib* pEntry; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 444 | void* handle; |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 445 | bool verbose; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 446 | |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 447 | /* reduce noise by not chattering about system libraries */ |
Dima Zavin | b14f405 | 2010-09-23 22:38:45 -0700 | [diff] [blame^] | 448 | verbose = !!strncmp(pathName, "/system", sizeof("/system")-1); |
| 449 | verbose = verbose && !!strncmp(pathName, "/vendor", sizeof("/vendor")-1); |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 450 | |
| 451 | if (verbose) |
| 452 | LOGD("Trying to load lib %s %p\n", pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * See if we've already loaded it. If we have, and the class loader |
| 456 | * matches, return successfully without doing anything. |
| 457 | */ |
| 458 | pEntry = findSharedLibEntry(pathName); |
| 459 | if (pEntry != NULL) { |
| 460 | if (pEntry->classLoader != classLoader) { |
| 461 | LOGW("Shared lib '%s' already opened by CL %p; can't open in %p\n", |
| 462 | pathName, pEntry->classLoader, classLoader); |
| 463 | return false; |
| 464 | } |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 465 | if (verbose) { |
| 466 | LOGD("Shared lib '%s' already loaded in same CL %p\n", |
| 467 | pathName, classLoader); |
| 468 | } |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 469 | if (!checkOnLoadResult(pEntry)) |
| 470 | return false; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Open the shared library. Because we're using a full path, the system |
| 476 | * doesn't have to search through LD_LIBRARY_PATH. (It may do so to |
| 477 | * resolve this library's dependencies though.) |
| 478 | * |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 479 | * Failures here are expected when java.library.path has several entries |
| 480 | * and we have to hunt for the lib. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 481 | * |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 482 | * The current version of the dynamic linker prints detailed information |
| 483 | * about dlopen() failures. Some things to check if the message is |
| 484 | * cryptic: |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 485 | * - make sure the library exists on the device |
| 486 | * - verify that the right path is being opened (the debug log message |
| 487 | * above can help with that) |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 488 | * - check to see if the library is valid (e.g. not zero bytes long) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 489 | * - check config/prelink-linux-arm.map to ensure that the library |
| 490 | * is listed and is not being overrun by the previous entry (if |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 491 | * loading suddenly stops working on a prelinked library, this is |
| 492 | * a good one to check) |
| 493 | * - write a trivial app that calls sleep() then dlopen(), attach |
| 494 | * to it with "strace -p <pid>" while it sleeps, and watch for |
| 495 | * attempts to open nonexistent dependent shared libs |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 496 | * |
| 497 | * This can execute slowly for a large library on a busy system, so we |
| 498 | * want to switch from RUNNING to VMWAIT while it executes. This allows |
| 499 | * the GC to ignore us. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 500 | */ |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 501 | Thread* self = dvmThreadSelf(); |
Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 502 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 503 | handle = dlopen(pathName, RTLD_LAZY); |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 504 | dvmChangeStatus(self, oldStatus); |
| 505 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 506 | if (handle == NULL) { |
| 507 | LOGI("Unable to dlopen(%s): %s\n", pathName, dlerror()); |
| 508 | return false; |
| 509 | } |
| 510 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 511 | /* create a new entry */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 512 | SharedLib* pNewEntry; |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 513 | pNewEntry = (SharedLib*) calloc(1, sizeof(SharedLib)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 514 | pNewEntry->pathName = strdup(pathName); |
| 515 | pNewEntry->handle = handle; |
| 516 | pNewEntry->classLoader = classLoader; |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 517 | dvmInitMutex(&pNewEntry->onLoadLock); |
| 518 | pthread_cond_init(&pNewEntry->onLoadCond, NULL); |
| 519 | pNewEntry->onLoadThreadId = self->threadId; |
| 520 | |
| 521 | /* try to add it to the list */ |
| 522 | SharedLib* pActualEntry = addSharedLibEntry(pNewEntry); |
| 523 | |
| 524 | if (pNewEntry != pActualEntry) { |
| 525 | LOGI("WOW: we lost a race to add a shared lib (%s CL=%p)\n", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 526 | pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 527 | freeSharedLibEntry(pNewEntry); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 528 | return checkOnLoadResult(pActualEntry); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 529 | } else { |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 530 | if (verbose) |
| 531 | LOGD("Added shared lib %s %p\n", pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 532 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 533 | bool result = true; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 534 | void* vonLoad; |
| 535 | int version; |
| 536 | |
| 537 | vonLoad = dlsym(handle, "JNI_OnLoad"); |
| 538 | if (vonLoad == NULL) { |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 539 | LOGD("No JNI_OnLoad found in %s %p, skipping init\n", |
| 540 | pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 541 | } else { |
| 542 | /* |
| 543 | * Call JNI_OnLoad. We have to override the current class |
| 544 | * loader, which will always be "null" since the stuff at the |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 545 | * top of the stack is around Runtime.loadLibrary(). (See |
| 546 | * the comments in the JNI FindClass function.) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 547 | */ |
| 548 | OnLoadFunc func = vonLoad; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 549 | Object* prevOverride = self->classLoaderOverride; |
| 550 | |
| 551 | self->classLoaderOverride = classLoader; |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 552 | oldStatus = dvmChangeStatus(self, THREAD_NATIVE); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 553 | LOGV("+++ calling JNI_OnLoad(%s)\n", pathName); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 554 | version = (*func)(gDvm.vmList, NULL); |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 555 | dvmChangeStatus(self, oldStatus); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 556 | self->classLoaderOverride = prevOverride; |
| 557 | |
| 558 | if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && |
| 559 | version != JNI_VERSION_1_6) |
| 560 | { |
| 561 | LOGW("JNI_OnLoad returned bad version (%d) in %s %p\n", |
| 562 | version, pathName, classLoader); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 563 | /* |
| 564 | * It's unwise to call dlclose() here, but we can mark it |
| 565 | * as bad and ensure that future load attempts will fail. |
| 566 | * |
| 567 | * We don't know how far JNI_OnLoad got, so there could |
| 568 | * be some partially-initialized stuff accessible through |
| 569 | * newly-registered native method calls. We could try to |
| 570 | * unregister them, but that doesn't seem worthwhile. |
| 571 | */ |
| 572 | result = false; |
| 573 | } else { |
| 574 | LOGV("+++ finished JNI_OnLoad %s\n", pathName); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 577 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 578 | if (result) |
| 579 | pNewEntry->onLoadResult = kOnLoadOkay; |
| 580 | else |
| 581 | pNewEntry->onLoadResult = kOnLoadFailed; |
| 582 | |
| 583 | pNewEntry->onLoadThreadId = 0; |
| 584 | |
| 585 | /* |
| 586 | * Broadcast a wakeup to anybody sleeping on the condition variable. |
| 587 | */ |
| 588 | dvmLockMutex(&pNewEntry->onLoadLock); |
| 589 | pthread_cond_broadcast(&pNewEntry->onLoadCond); |
| 590 | dvmUnlockMutex(&pNewEntry->onLoadLock); |
| 591 | return result; |
| 592 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
| 596 | /* |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 597 | * Un-register JNI native methods. |
| 598 | * |
| 599 | * There are two relevant fields in struct Method, "nativeFunc" and |
| 600 | * "insns". The former holds a function pointer to a "bridge" function |
| 601 | * (or, for internal native, the actual implementation). The latter holds |
| 602 | * a pointer to the actual JNI method. |
| 603 | * |
| 604 | * The obvious approach is to reset both fields to their initial state |
| 605 | * (nativeFunc points at dvmResolveNativeMethod, insns holds NULL), but |
| 606 | * that creates some unpleasant race conditions. In particular, if another |
| 607 | * thread is executing inside the call bridge for the method in question, |
| 608 | * and we reset insns to NULL, the VM will crash. (See the comments above |
| 609 | * dvmSetNativeFunc() for additional commentary.) |
| 610 | * |
| 611 | * We can't rely on being able to update two 32-bit fields in one atomic |
| 612 | * operation (e.g. no 64-bit atomic ops on ARMv5TE), so we want to change |
| 613 | * only one field. It turns out we can simply reset nativeFunc to its |
| 614 | * initial state, leaving insns alone, because dvmResolveNativeMethod |
| 615 | * ignores "insns" entirely. |
| 616 | * |
| 617 | * When the method is re-registered, both fields will be updated, but |
| 618 | * dvmSetNativeFunc guarantees that "insns" is updated first. This means |
| 619 | * we shouldn't be in a situation where we have a "live" call bridge and |
| 620 | * a stale implementation pointer. |
| 621 | */ |
| 622 | static void unregisterJNINativeMethods(Method* methods, size_t count) |
| 623 | { |
| 624 | while (count != 0) { |
| 625 | count--; |
| 626 | |
| 627 | Method* meth = &methods[count]; |
| 628 | if (!dvmIsNativeMethod(meth)) |
| 629 | continue; |
| 630 | if (dvmIsAbstractMethod(meth)) /* avoid abstract method stubs */ |
| 631 | continue; |
| 632 | |
| 633 | /* |
| 634 | * Strictly speaking this ought to test the function pointer against |
| 635 | * the various JNI bridge functions to ensure that we only undo |
| 636 | * methods that were registered through JNI. In practice, any |
| 637 | * native method with a non-NULL "insns" is a registered JNI method. |
| 638 | * |
| 639 | * If we inadvertently unregister an internal-native, it'll get |
| 640 | * re-resolved on the next call; unregistering an unregistered |
| 641 | * JNI method is a no-op. So we don't really need to test for |
| 642 | * anything. |
| 643 | */ |
| 644 | |
| 645 | LOGD("Unregistering JNI method %s.%s:%s\n", |
| 646 | meth->clazz->descriptor, meth->name, meth->shorty); |
| 647 | dvmSetNativeFunc(meth, dvmResolveNativeMethod, NULL); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Un-register all JNI native methods from a class. |
| 653 | */ |
| 654 | void dvmUnregisterJNINativeMethods(ClassObject* clazz) |
| 655 | { |
| 656 | unregisterJNINativeMethods(clazz->directMethods, clazz->directMethodCount); |
| 657 | unregisterJNINativeMethods(clazz->virtualMethods, clazz->virtualMethodCount); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 662 | * =========================================================================== |
| 663 | * Signature-based method lookup |
| 664 | * =========================================================================== |
| 665 | */ |
| 666 | |
| 667 | /* |
| 668 | * Create the pre-mangled form of the class+method string. |
| 669 | * |
| 670 | * Returns a newly-allocated string, and sets "*pLen" to the length. |
| 671 | */ |
| 672 | static char* createJniNameString(const char* classDescriptor, |
| 673 | const char* methodName, int* pLen) |
| 674 | { |
| 675 | char* result; |
| 676 | size_t descriptorLength = strlen(classDescriptor); |
| 677 | |
| 678 | *pLen = 4 + descriptorLength + strlen(methodName); |
| 679 | |
| 680 | result = malloc(*pLen +1); |
| 681 | if (result == NULL) |
| 682 | return NULL; |
| 683 | |
| 684 | /* |
| 685 | * Add one to classDescriptor to skip the "L", and then replace |
| 686 | * the final ";" with a "/" after the sprintf() call. |
| 687 | */ |
| 688 | sprintf(result, "Java/%s%s", classDescriptor + 1, methodName); |
| 689 | result[5 + (descriptorLength - 2)] = '/'; |
| 690 | |
| 691 | return result; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Returns a newly-allocated, mangled copy of "str". |
| 696 | * |
| 697 | * "str" is a "modified UTF-8" string. We convert it to UTF-16 first to |
| 698 | * make life simpler. |
| 699 | */ |
| 700 | static char* mangleString(const char* str, int len) |
| 701 | { |
| 702 | u2* utf16 = NULL; |
| 703 | char* mangle = NULL; |
| 704 | int charLen; |
| 705 | |
| 706 | //LOGI("mangling '%s' %d\n", str, len); |
| 707 | |
| 708 | assert(str[len] == '\0'); |
| 709 | |
| 710 | charLen = dvmUtf8Len(str); |
| 711 | utf16 = (u2*) malloc(sizeof(u2) * charLen); |
| 712 | if (utf16 == NULL) |
| 713 | goto bail; |
| 714 | |
| 715 | dvmConvertUtf8ToUtf16(utf16, str); |
| 716 | |
| 717 | /* |
| 718 | * Compute the length of the mangled string. |
| 719 | */ |
| 720 | int i, mangleLen = 0; |
| 721 | |
| 722 | for (i = 0; i < charLen; i++) { |
| 723 | u2 ch = utf16[i]; |
| 724 | |
Brian McKenna | dfdaa87 | 2009-07-19 20:49:26 +1000 | [diff] [blame] | 725 | if (ch == '$' || ch > 127) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 726 | mangleLen += 6; |
| 727 | } else { |
| 728 | switch (ch) { |
| 729 | case '_': |
| 730 | case ';': |
| 731 | case '[': |
| 732 | mangleLen += 2; |
| 733 | break; |
| 734 | default: |
| 735 | mangleLen++; |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | char* cp; |
| 742 | |
| 743 | mangle = (char*) malloc(mangleLen +1); |
| 744 | if (mangle == NULL) |
| 745 | goto bail; |
| 746 | |
| 747 | for (i = 0, cp = mangle; i < charLen; i++) { |
| 748 | u2 ch = utf16[i]; |
| 749 | |
Brian McKenna | dfdaa87 | 2009-07-19 20:49:26 +1000 | [diff] [blame] | 750 | if (ch == '$' || ch > 127) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 751 | sprintf(cp, "_0%04x", ch); |
| 752 | cp += 6; |
| 753 | } else { |
| 754 | switch (ch) { |
| 755 | case '_': |
| 756 | *cp++ = '_'; |
| 757 | *cp++ = '1'; |
| 758 | break; |
| 759 | case ';': |
| 760 | *cp++ = '_'; |
| 761 | *cp++ = '2'; |
| 762 | break; |
| 763 | case '[': |
| 764 | *cp++ = '_'; |
| 765 | *cp++ = '3'; |
| 766 | break; |
| 767 | case '/': |
| 768 | *cp++ = '_'; |
| 769 | break; |
| 770 | default: |
| 771 | *cp++ = (char) ch; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | *cp = '\0'; |
| 778 | |
| 779 | bail: |
| 780 | free(utf16); |
| 781 | return mangle; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Create the mangled form of the parameter types. |
| 786 | */ |
| 787 | static char* createMangledSignature(const DexProto* proto) |
| 788 | { |
| 789 | DexStringCache sigCache; |
| 790 | const char* interim; |
| 791 | char* result; |
| 792 | |
| 793 | dexStringCacheInit(&sigCache); |
| 794 | interim = dexProtoGetParameterDescriptors(proto, &sigCache); |
| 795 | result = mangleString(interim, strlen(interim)); |
| 796 | dexStringCacheRelease(&sigCache); |
| 797 | |
| 798 | return result; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * (This is a dvmHashForeach callback.) |
| 803 | * |
| 804 | * Search for a matching method in this shared library. |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 805 | * |
| 806 | * TODO: we may want to skip libraries for which JNI_OnLoad failed. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 807 | */ |
| 808 | static int findMethodInLib(void* vlib, void* vmethod) |
| 809 | { |
| 810 | const SharedLib* pLib = (const SharedLib*) vlib; |
| 811 | const Method* meth = (const Method*) vmethod; |
| 812 | char* preMangleCM = NULL; |
| 813 | char* mangleCM = NULL; |
| 814 | char* mangleSig = NULL; |
| 815 | char* mangleCMSig = NULL; |
| 816 | void* func = NULL; |
| 817 | int len; |
| 818 | |
| 819 | if (meth->clazz->classLoader != pLib->classLoader) { |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 820 | LOGV("+++ not scanning '%s' for '%s' (wrong CL)\n", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 821 | pLib->pathName, meth->name); |
| 822 | return 0; |
| 823 | } else |
| 824 | LOGV("+++ scanning '%s' for '%s'\n", pLib->pathName, meth->name); |
| 825 | |
| 826 | /* |
| 827 | * First, we try it without the signature. |
| 828 | */ |
| 829 | preMangleCM = |
| 830 | createJniNameString(meth->clazz->descriptor, meth->name, &len); |
| 831 | if (preMangleCM == NULL) |
| 832 | goto bail; |
| 833 | |
| 834 | mangleCM = mangleString(preMangleCM, len); |
| 835 | if (mangleCM == NULL) |
| 836 | goto bail; |
| 837 | |
| 838 | LOGV("+++ calling dlsym(%s)\n", mangleCM); |
| 839 | func = dlsym(pLib->handle, mangleCM); |
| 840 | if (func == NULL) { |
| 841 | mangleSig = |
| 842 | createMangledSignature(&meth->prototype); |
| 843 | if (mangleSig == NULL) |
| 844 | goto bail; |
| 845 | |
| 846 | mangleCMSig = (char*) malloc(strlen(mangleCM) + strlen(mangleSig) +3); |
| 847 | if (mangleCMSig == NULL) |
| 848 | goto bail; |
| 849 | |
| 850 | sprintf(mangleCMSig, "%s__%s", mangleCM, mangleSig); |
| 851 | |
| 852 | LOGV("+++ calling dlsym(%s)\n", mangleCMSig); |
| 853 | func = dlsym(pLib->handle, mangleCMSig); |
| 854 | if (func != NULL) { |
| 855 | LOGV("Found '%s' with dlsym\n", mangleCMSig); |
| 856 | } |
| 857 | } else { |
| 858 | LOGV("Found '%s' with dlsym\n", mangleCM); |
| 859 | } |
| 860 | |
| 861 | bail: |
| 862 | free(preMangleCM); |
| 863 | free(mangleCM); |
| 864 | free(mangleSig); |
| 865 | free(mangleCMSig); |
| 866 | return (int) func; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * See if the requested method lives in any of the currently-loaded |
| 871 | * shared libraries. We do this by checking each of them for the expected |
| 872 | * method signature. |
| 873 | */ |
| 874 | static void* lookupSharedLibMethod(const Method* method) |
| 875 | { |
| 876 | if (gDvm.nativeLibs == NULL) { |
| 877 | LOGE("Unexpected init state: nativeLibs not ready\n"); |
| 878 | dvmAbort(); |
| 879 | } |
| 880 | return (void*) dvmHashForeach(gDvm.nativeLibs, findMethodInLib, |
| 881 | (void*) method); |
| 882 | } |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 883 | |
| 884 | |
| 885 | static void appendValue(char type, const JValue value, char* buf, size_t n, |
| 886 | bool appendComma) |
| 887 | { |
| 888 | size_t len = strlen(buf); |
| 889 | if (len >= n - 32) { // 32 should be longer than anything we could append. |
| 890 | buf[len - 1] = '.'; |
| 891 | buf[len - 2] = '.'; |
| 892 | buf[len - 3] = '.'; |
| 893 | return; |
| 894 | } |
| 895 | char* p = buf + len; |
| 896 | switch (type) { |
| 897 | case 'B': |
| 898 | if (value.b >= 0 && value.b < 10) { |
| 899 | sprintf(p, "%d", value.b); |
| 900 | } else { |
| 901 | sprintf(p, "0x%x (%d)", value.b, value.b); |
| 902 | } |
| 903 | break; |
| 904 | case 'C': |
| 905 | if (value.c < 0x7f && value.c >= ' ') { |
| 906 | sprintf(p, "U+%x ('%c')", value.c, value.c); |
| 907 | } else { |
| 908 | sprintf(p, "U+%x", value.c); |
| 909 | } |
| 910 | break; |
| 911 | case 'D': |
| 912 | sprintf(p, "%g", value.d); |
| 913 | break; |
| 914 | case 'F': |
| 915 | sprintf(p, "%g", value.f); |
| 916 | break; |
| 917 | case 'I': |
| 918 | sprintf(p, "%d", value.i); |
| 919 | break; |
| 920 | case 'L': |
| 921 | sprintf(p, "0x%x", value.i); |
| 922 | break; |
| 923 | case 'J': |
| 924 | sprintf(p, "%lld", value.j); |
| 925 | break; |
| 926 | case 'S': |
| 927 | sprintf(p, "%d", value.s); |
| 928 | break; |
| 929 | case 'V': |
| 930 | strcpy(p, "void"); |
| 931 | break; |
| 932 | case 'Z': |
| 933 | strcpy(p, value.z ? "true" : "false"); |
| 934 | break; |
| 935 | default: |
| 936 | sprintf(p, "unknown type '%c'", type); |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | if (appendComma) { |
| 941 | strcat(p, ", "); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | #define LOGI_NATIVE(...) LOG(LOG_INFO, LOG_TAG "-native", __VA_ARGS__) |
| 946 | |
Elliott Hughes | de66fcb | 2010-07-30 13:34:49 -0700 | [diff] [blame] | 947 | void dvmLogNativeMethodEntry(const Method* method, const u4* args) |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 948 | { |
| 949 | char thisString[32] = { 0 }; |
Elliott Hughes | de66fcb | 2010-07-30 13:34:49 -0700 | [diff] [blame] | 950 | const u4* sp = args; // &args[method->registersSize - method->insSize]; |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 951 | if (!dvmIsStaticMethod(method)) { |
| 952 | sprintf(thisString, "this=0x%08x ", *sp++); |
| 953 | } |
| 954 | |
| 955 | char argsString[128]= { 0 }; |
| 956 | const char* desc = &method->shorty[1]; |
| 957 | while (*desc != '\0') { |
| 958 | char argType = *desc++; |
| 959 | JValue value; |
| 960 | if (argType == 'D' || argType == 'J') { |
| 961 | value.j = dvmGetArgLong(sp, 0); |
| 962 | sp += 2; |
| 963 | } else { |
| 964 | value.i = *sp++; |
| 965 | } |
| 966 | appendValue(argType, value, argsString, sizeof(argsString), |
| 967 | *desc != '\0'); |
| 968 | } |
| 969 | |
| 970 | char* signature = dexProtoCopyMethodDescriptor(&method->prototype); |
| 971 | LOGI_NATIVE("-> %s.%s%s %s(%s)", method->clazz->descriptor, method->name, |
| 972 | signature, thisString, argsString); |
| 973 | free(signature); |
| 974 | } |
| 975 | |
| 976 | void dvmLogNativeMethodExit(const Method* method, Thread* self, |
| 977 | const JValue returnValue) |
| 978 | { |
| 979 | char* signature = dexProtoCopyMethodDescriptor(&method->prototype); |
| 980 | if (dvmCheckException(self)) { |
| 981 | Object* exception = dvmGetException(self); |
| 982 | LOGI_NATIVE("<- %s.%s%s threw %s", method->clazz->descriptor, |
| 983 | method->name, signature, exception->clazz->descriptor); |
| 984 | } else { |
| 985 | char returnValueString[128] = { 0 }; |
| 986 | char returnType = method->shorty[0]; |
| 987 | appendValue(returnType, returnValue, |
| 988 | returnValueString, sizeof(returnValueString), false); |
| 989 | LOGI_NATIVE("<- %s.%s%s returned %s", method->clazz->descriptor, |
| 990 | method->name, signature, returnValueString); |
| 991 | } |
| 992 | free(signature); |
| 993 | } |