Globals.h revision 2c98747b403970ef4b3352e271633f93935b9825
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 */ 16 17/* 18 * Variables with library scope. 19 * 20 * Prefer this over scattered static and global variables -- it's easier to 21 * view the state in a debugger, it makes clean shutdown simpler, we can 22 * trivially dump the state into a crash log, and it dodges most naming 23 * collisions that will arise when we are embedded in a larger program. 24 * 25 * If we want multiple VMs per process, this can get stuffed into TLS (or 26 * accessed through a Thread field). May need to pass it around for some 27 * of the early initialization functions. 28 */ 29#ifndef _DALVIK_GLOBALS 30#define _DALVIK_GLOBALS 31 32#include <stdarg.h> 33#include <pthread.h> 34 35#define MAX_BREAKPOINTS 20 /* used for a debugger optimization */ 36 37// fwd 38typedef struct GcHeap GcHeap; /* heap internal structure */ 39 40/* 41 * One of these for each -ea/-da/-esa/-dsa on the command line. 42 */ 43typedef struct AssertionControl { 44 char* pkgOrClass; /* package/class string, or NULL for esa/dsa */ 45 int pkgOrClassLen; /* string length, for quick compare */ 46 bool enable; /* enable or disable */ 47 bool isPackage; /* string ended with "..."? */ 48} AssertionControl; 49 50/* 51 * Execution mode, e.g. interpreter vs. JIT. 52 */ 53typedef enum ExecutionMode { 54 kExecutionModeUnknown = 0, 55 kExecutionModeInterpPortable, 56 kExecutionModeInterpFast, 57} ExecutionMode; 58 59/* 60 * All fields are initialized to zero. 61 * 62 * Storage allocated here must be freed by a subsystem shutdown function or 63 * from within freeGlobals(). 64 */ 65struct DvmGlobals { 66 /* 67 * Some options from the command line or environment. 68 */ 69 char* bootClassPathStr; 70 char* classPathStr; 71 72 unsigned int heapSizeStart; 73 unsigned int heapSizeMax; 74 unsigned int stackSize; 75 76 bool verboseGc; 77 bool verboseJni; 78 bool verboseClass; 79 80 bool jdwpAllowed; // debugging allowed for this process? 81 bool jdwpConfigured; // has debugging info been provided? 82 int jdwpTransport; 83 bool jdwpServer; 84 char* jdwpHost; 85 int jdwpPort; 86 bool jdwpSuspend; 87 88 int (*vfprintfHook)(FILE*, const char*, va_list); 89 void (*exitHook)(int); 90 void (*abortHook)(void); 91 92 int jniGrefLimit; // 0 means no limit 93 bool reduceSignals; 94 bool noQuitHandler; 95 bool verifyDexChecksum; 96 char* stackTraceFile; // for SIGQUIT-inspired output 97 98 bool logStdio; 99 100 DexOptimizerMode dexOptMode; 101 DexClassVerifyMode classVerifyMode; 102 bool generateRegisterMaps; 103 104 int assertionCtrlCount; 105 AssertionControl* assertionCtrl; 106 107 ExecutionMode executionMode; 108 109 /* 110 * VM init management. 111 */ 112 bool initializing; 113 int initExceptionCount; 114 bool optimizing; 115 116 /* 117 * java.lang.System properties set from the command line. 118 */ 119 int numProps; 120 int maxProps; 121 char** propList; 122 123 /* 124 * Where the VM goes to find system classes. 125 */ 126 ClassPathEntry* bootClassPath; 127 /* used by the DEX optimizer to load classes from an unfinished DEX */ 128 DvmDex* bootClassPathOptExtra; 129 bool optimizingBootstrapClass; 130 131 /* 132 * Loaded classes, hashed by class name. Each entry is a ClassObject*, 133 * allocated in GC space. 134 */ 135 HashTable* loadedClasses; 136 137 /* 138 * Value for the next class serial number to be assigned. This is 139 * incremented as we load classes. Failed loads and races may result 140 * in some numbers being skipped, and the serial number is not 141 * guaranteed to start at 1, so the current value should not be used 142 * as a count of loaded classes. 143 */ 144 volatile int classSerialNumber; 145 146 /* 147 * Classes with a low classSerialNumber are probably in the zygote, and 148 * their InitiatingLoaderList is not used, to promote sharing. The list is 149 * kept here instead. 150 */ 151 InitiatingLoaderList* initiatingLoaderList; 152 153 /* 154 * Interned strings. 155 */ 156 HashTable* internedStrings; 157 158 /* 159 * Quick lookups for popular classes used internally. 160 */ 161 ClassObject* unlinkedJavaLangClass; // see unlinkedJavaLangClassObject 162 ClassObject* classJavaLangClass; 163 ClassObject* classJavaLangClassArray; 164 ClassObject* classJavaLangError; 165 ClassObject* classJavaLangObject; 166 ClassObject* classJavaLangObjectArray; 167 ClassObject* classJavaLangRuntimeException; 168 ClassObject* classJavaLangString; 169 ClassObject* classJavaLangThread; 170 ClassObject* classJavaLangVMThread; 171 ClassObject* classJavaLangThreadGroup; 172 ClassObject* classJavaLangThrowable; 173 ClassObject* classJavaLangStackTraceElement; 174 ClassObject* classJavaLangStackTraceElementArray; 175 ClassObject* classJavaLangAnnotationAnnotationArray; 176 ClassObject* classJavaLangAnnotationAnnotationArrayArray; 177 ClassObject* classJavaLangReflectAccessibleObject; 178 ClassObject* classJavaLangReflectConstructor; 179 ClassObject* classJavaLangReflectConstructorArray; 180 ClassObject* classJavaLangReflectField; 181 ClassObject* classJavaLangReflectFieldArray; 182 ClassObject* classJavaLangReflectMethod; 183 ClassObject* classJavaLangReflectMethodArray; 184 ClassObject* classJavaLangReflectProxy; 185 ClassObject* classJavaLangExceptionInInitializerError; 186 ClassObject* classJavaLangRefReference; 187 ClassObject* classJavaSecurityAccessController; 188 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationFactory; 189 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationMember; 190 ClassObject* classOrgApacheHarmonyLangAnnotationAnnotationMemberArray; 191 192 /* synthetic classes for arrays of primitives */ 193 ClassObject* classArrayBoolean; 194 ClassObject* classArrayChar; 195 ClassObject* classArrayFloat; 196 ClassObject* classArrayDouble; 197 ClassObject* classArrayByte; 198 ClassObject* classArrayShort; 199 ClassObject* classArrayInt; 200 ClassObject* classArrayLong; 201 202 /* method offsets - Object */ 203 int voffJavaLangObject_equals; 204 int voffJavaLangObject_hashCode; 205 int voffJavaLangObject_toString; 206 int voffJavaLangObject_finalize; 207 208 /* field offsets - Class */ 209 int offJavaLangClass_pd; 210 211 /* field offsets - String */ 212 volatile int javaLangStringReady; /* 0=not init, 1=ready, -1=initing */ 213 int offJavaLangString_value; 214 int offJavaLangString_count; 215 int offJavaLangString_offset; 216 int offJavaLangString_hashCode; 217 218 /* field offsets - Thread */ 219 int offJavaLangThread_vmThread; 220 int offJavaLangThread_group; 221 int offJavaLangThread_daemon; 222 int offJavaLangThread_name; 223 int offJavaLangThread_priority; 224 225 /* method offsets - Thread */ 226 int voffJavaLangThread_run; 227 228 /* field offsets - VMThread */ 229 int offJavaLangVMThread_thread; 230 int offJavaLangVMThread_vmData; 231 232 /* method offsets - ThreadGroup */ 233 int voffJavaLangThreadGroup_removeThread; 234 235 /* field offsets - Throwable */ 236 int offJavaLangThrowable_stackState; 237 int offJavaLangThrowable_message; 238 int offJavaLangThrowable_cause; 239 240 /* field offsets - java.lang.reflect.* */ 241 int offJavaLangReflectAccessibleObject_flag; 242 int offJavaLangReflectConstructor_slot; 243 int offJavaLangReflectConstructor_declClass; 244 int offJavaLangReflectField_slot; 245 int offJavaLangReflectField_declClass; 246 int offJavaLangReflectMethod_slot; 247 int offJavaLangReflectMethod_declClass; 248 249 /* field offsets - java.lang.ref.Reference */ 250 int offJavaLangRefReference_referent; 251 int offJavaLangRefReference_queue; 252 int offJavaLangRefReference_queueNext; 253 int offJavaLangRefReference_vmData; 254 255#if FANCY_REFERENCE_SUBCLASS 256 /* method offsets - java.lang.ref.Reference */ 257 int voffJavaLangRefReference_clear; 258 int voffJavaLangRefReference_enqueue; 259#else 260 /* method pointers - java.lang.ref.Reference */ 261 Method* methJavaLangRefReference_enqueueInternal; 262#endif 263 264 /* field offsets - java.nio.Buffer and java.nio.DirectByteBufferImpl */ 265 //int offJavaNioBuffer_capacity; 266 //int offJavaNioDirectByteBufferImpl_pointer; 267 268 /* method pointers - java.security.AccessController */ 269 volatile bool javaSecurityAccessControllerReady; 270 Method* methJavaSecurityAccessController_doPrivileged[4]; 271 272 /* constructor method pointers; no vtable involved, so use Method* */ 273 Method* methJavaLangStackTraceElement_init; 274 Method* methJavaLangExceptionInInitializerError_init; 275 Method* methJavaLangReflectConstructor_init; 276 Method* methJavaLangReflectField_init; 277 Method* methJavaLangReflectMethod_init; 278 Method* methOrgApacheHarmonyLangAnnotationAnnotationMember_init; 279 280 /* static method pointers - android.lang.annotation.* */ 281 Method* 282 methOrgApacheHarmonyLangAnnotationAnnotationFactory_createAnnotation; 283 284 /* direct method pointers - java.lang.reflect.Proxy */ 285 Method* methJavaLangReflectProxy_constructorPrototype; 286 287 /* field offsets - java.lang.reflect.Proxy */ 288 int offJavaLangReflectProxy_h; 289 290 /* fake native entry point method */ 291 Method* methFakeNativeEntry; 292 293 /* 294 * VM-synthesized primitive classes, for arrays. 295 */ 296 ClassObject* volatile primitiveClass[PRIM_MAX]; 297 298 /* 299 * A placeholder ClassObject used during ClassObject 300 * construction. 301 */ 302 ClassObject unlinkedJavaLangClassObject; 303 304 /* 305 * Thread list. This always has at least one element in it (main), 306 * and main is always the first entry. 307 * 308 * The threadListLock is used for several things, including the thread 309 * start condition variable. Generally speaking, you must hold the 310 * threadListLock when: 311 * - adding/removing items from the list 312 * - waiting on or signaling threadStartCond 313 * - examining the Thread struct for another thread (this is to avoid 314 * one thread freeing the Thread struct while another thread is 315 * perusing it) 316 */ 317 Thread* threadList; 318 pthread_mutex_t threadListLock; 319 320 pthread_cond_t threadStartCond; 321 322 /* 323 * The thread code grabs this before suspending all threads. There 324 * are four things that can cause a "suspend all": 325 * (1) the GC is starting; 326 * (2) the debugger has sent a "suspend all" request; 327 * (3) a thread has hit a breakpoint or exception that the debugger 328 * has marked as a "suspend all" event; 329 * (4) the SignalCatcher caught a signal that requires suspension. 330 * 331 * Because we use "safe point" self-suspension, it is never safe to 332 * do a blocking "lock" call on this mutex -- if it has been acquired, 333 * somebody is probably trying to put you to sleep. The leading '_' is 334 * intended as a reminder that this lock is special. 335 * 336 * This lock is also held while attaching an externally-created thread 337 * through JNI. That way we can correctly set the initial suspend state. 338 */ 339 pthread_mutex_t _threadSuspendLock; 340 341 /* 342 * Guards Thread->suspendCount for all threads, and provides the lock 343 * for the condition variable that all suspended threads sleep on 344 * (threadSuspendCountCond). 345 * 346 * This has to be separate from threadListLock because of the way 347 * threads put themselves to sleep. 348 */ 349 pthread_mutex_t threadSuspendCountLock; 350 351 /* 352 * Suspended threads sleep on this. They should sleep on the condition 353 * variable until their "suspend count" is zero. 354 * 355 * Paired with "threadSuspendCountLock". 356 */ 357 pthread_cond_t threadSuspendCountCond; 358 359 /* 360 * MUTEX ORDERING: when locking multiple mutexes, always grab them in 361 * this order to avoid deadlock: 362 * 363 * (1) _threadSuspendLock (use lockThreadSuspend()) 364 * (2) threadListLock (use dvmLockThreadList()) 365 * (3) threadSuspendCountLock (use lockThreadSuspendCount()) 366 */ 367 368 369 /* 370 * Thread ID bitmap. We want threads to have small integer IDs so 371 * we can use them in "thin locks". 372 */ 373 BitVector* threadIdMap; 374 375 /* 376 * Manage exit conditions. The VM exits when all non-daemon threads 377 * have exited. If the main thread returns early, we need to sleep 378 * on a condition variable. 379 */ 380 int nonDaemonThreadCount; /* must hold threadListLock to access */ 381 //pthread_mutex_t vmExitLock; 382 pthread_cond_t vmExitCond; 383 384 /* 385 * The set of DEX files loaded by custom class loaders. 386 */ 387 HashTable* userDexFiles; 388 389 /* 390 * JNI global reference table. 391 */ 392 ReferenceTable jniGlobalRefTable; 393 pthread_mutex_t jniGlobalRefLock; 394 int jniGlobalRefHiMark; 395 int jniGlobalRefLoMark; 396 397 /* 398 * Native shared library table. 399 */ 400 HashTable* nativeLibs; 401 402 /* 403 * GC heap lock. Functions like gcMalloc() acquire this before making 404 * any changes to the heap. It is held throughout garbage collection. 405 */ 406 pthread_mutex_t gcHeapLock; 407 408 /* Opaque pointer representing the heap. */ 409 GcHeap* gcHeap; 410 411 /* 412 * Pre-allocated object for out-of-memory errors. 413 */ 414 Object* outOfMemoryObj; 415 416 /* pre-allocated general failure exception */ 417 Object* internalErrorObj; 418 419 /* Monitor list, so we can free them */ 420 /*volatile*/ Monitor* monitorList; 421 422 /* Monitor for Thread.sleep() implementation */ 423 Monitor* threadSleepMon; 424 425 /* set when we create a second heap inside the zygote */ 426 bool newZygoteHeapAllocated; 427 428 /* 429 * TLS keys. 430 */ 431 pthread_key_t pthreadKeySelf; /* Thread*, for dvmThreadSelf */ 432 433 /* 434 * JNI allows you to have multiple VMs, but we limit ourselves to 1, 435 * so "vmList" is really just a pointer to the one and only VM. 436 */ 437 JavaVM* vmList; 438 439 /* 440 * Cache results of "A instanceof B". 441 */ 442 AtomicCache* instanceofCache; 443 444 /* instruction width table, used for optimization and verification */ 445 InstructionWidth* instrWidth; 446 /* instruction flags table, used for verification */ 447 InstructionFlags* instrFlags; 448 /* instruction format table, used for verification */ 449 InstructionFormat* instrFormat; 450 451 /* 452 * Bootstrap class loader linear allocator. 453 */ 454 LinearAllocHdr* pBootLoaderAlloc; 455 456 457 /* 458 * Heap worker thread. 459 */ 460 bool heapWorkerInitialized; 461 bool heapWorkerReady; 462 bool haltHeapWorker; 463 pthread_t heapWorkerHandle; 464 pthread_mutex_t heapWorkerLock; 465 pthread_cond_t heapWorkerCond; 466 pthread_cond_t heapWorkerIdleCond; 467 pthread_mutex_t heapWorkerListLock; 468 469 /* 470 * Compute some stats on loaded classes. 471 */ 472 int numLoadedClasses; 473 int numDeclaredMethods; 474 int numDeclaredInstFields; 475 int numDeclaredStaticFields; 476 477 /* when using a native debugger, set this to suppress watchdog timers */ 478 bool nativeDebuggerActive; 479 480 /* 481 * JDWP debugger support. 482 */ 483 bool debuggerConnected; /* debugger or DDMS is connected */ 484 bool debuggerActive; /* debugger is making requests */ 485 JdwpState* jdwpState; 486 487 /* 488 * Registry of objects known to the debugger. 489 */ 490 HashTable* dbgRegistry; 491 492 /* 493 * Breakpoint optimization table. This is global and NOT explicitly 494 * synchronized, but all operations that modify the table are made 495 * from relatively-synchronized functions. False-positives are 496 * possible, false-negatives (i.e. missing a breakpoint) should not be. 497 */ 498 const u2* debugBreakAddr[MAX_BREAKPOINTS]; 499 500 /* 501 * Single-step control struct. We currently only allow one thread to 502 * be single-stepping at a time, which is all that really makes sense, 503 * but it's possible we may need to expand this to be per-thread. 504 */ 505 StepControl stepControl; 506 507 /* 508 * DDM features embedded in the VM. 509 */ 510 bool ddmThreadNotification; 511 512 /* 513 * Zygote (partially-started process) support 514 */ 515 bool zygote; 516 517 /* 518 * Used for tracking allocations that we report to DDMS. When the feature 519 * is enabled (through a DDMS request) the "allocRecords" pointer becomes 520 * non-NULL. 521 */ 522 pthread_mutex_t allocTrackerLock; 523 AllocRecord* allocRecords; 524 int allocRecordHead; /* most-recently-added entry */ 525 int allocRecordCount; /* #of valid entries */ 526 527#ifdef WITH_ALLOC_LIMITS 528 /* set on first use of an alloc limit, never cleared */ 529 bool checkAllocLimits; 530 /* allocation limit, for setGlobalAllocationLimit() regression testing */ 531 int allocationLimit; 532#endif 533 534#ifdef WITH_DEADLOCK_PREDICTION 535 /* global lock on history tree accesses */ 536 pthread_mutex_t deadlockHistoryLock; 537 538 enum { kDPOff=0, kDPWarn, kDPErr, kDPAbort } deadlockPredictMode; 539#endif 540 541#ifdef WITH_PROFILER 542 /* 543 * When a profiler is enabled, this is incremented. Distinct profilers 544 * include "dmtrace" method tracing, emulator method tracing, and 545 * possibly instruction counting. 546 * 547 * The purpose of this is to have a single value that the interpreter 548 * can check to see if any profiling activity is enabled. 549 */ 550 volatile int activeProfilers; 551 552 /* 553 * State for method-trace profiling. 554 */ 555 MethodTraceState methodTrace; 556 557 /* 558 * State for emulator tracing. 559 */ 560 void* emulatorTracePage; 561 int emulatorTraceEnableCount; 562 563 /* 564 * Global state for memory allocation profiling. 565 */ 566 AllocProfState allocProf; 567 568 /* 569 * Pointers to the original methods for things that have been inlined. 570 * This makes it easy for us to output method entry/exit records for 571 * the method calls we're not actually making. 572 */ 573 Method** inlinedMethods; 574 575 /* 576 * Dalvik instruction counts (256 entries). 577 */ 578 int* executedInstrCounts; 579 bool instructionCountEnableCount; 580#endif 581 582 /* 583 * Signal catcher thread (for SIGQUIT). 584 */ 585 pthread_t signalCatcherHandle; 586 bool haltSignalCatcher; 587 588 /* 589 * Stdout/stderr conversion thread. 590 */ 591 bool haltStdioConverter; 592 bool stdioConverterReady; 593 pthread_t stdioConverterHandle; 594 pthread_mutex_t stdioConverterLock; 595 pthread_cond_t stdioConverterCond; 596 597 /* 598 * pid of the system_server process. We track it so that when system server 599 * crashes the Zygote process will be killed and restarted. 600 */ 601 pid_t systemServerPid; 602 603//#define COUNT_PRECISE_METHODS 604#ifdef COUNT_PRECISE_METHODS 605 PointerSet* preciseMethods; 606#endif 607}; 608 609extern struct DvmGlobals gDvm; 610 611#endif /*_DALVIK_GLOBALS*/ 612