2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Variables with library scope.
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.
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.
29 #ifndef _DALVIK_GLOBALS
30 #define _DALVIK_GLOBALS
35 #define MAX_BREAKPOINTS 20 /* used for a debugger optimization */
38 typedef struct GcHeap GcHeap; /* heap internal structure */
41 * One of these for each -ea/-da/-esa/-dsa on the command line.
43 typedef 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 "..."? */
51 * Execution mode, e.g. interpreter vs. JIT.
53 typedef enum ExecutionMode {
54 kExecutionModeUnknown = 0,
55 kExecutionModeInterpPortable,
56 kExecutionModeInterpFast,
60 * All fields are initialized to zero.
62 * Storage allocated here must be freed by a subsystem shutdown function or
63 * from within freeGlobals().
67 * Some options from the command line or environment.
69 char* bootClassPathStr;
72 unsigned int heapSizeStart;
73 unsigned int heapSizeMax;
74 unsigned int stackSize;
80 bool jdwpAllowed; // debugging allowed for this process?
81 bool jdwpConfigured; // has debugging info been provided?
88 int (*vfprintfHook)(FILE*, const char*, va_list);
89 void (*exitHook)(int);
90 void (*abortHook)(void);
92 int jniGrefLimit; // 0 means no limit
95 bool verifyDexChecksum;
96 char* stackTraceFile; // for SIGQUIT-inspired output
100 DexOptimizerMode dexOptMode;
101 DexClassVerifyMode classVerifyMode;
102 bool generateRegisterMaps;
104 int assertionCtrlCount;
105 AssertionControl* assertionCtrl;
107 ExecutionMode executionMode;
110 * VM init management.
113 int initExceptionCount;
117 * java.lang.System properties set from the command line.
124 * Where the VM goes to find system classes.
126 ClassPathEntry* bootClassPath;
127 /* used by the DEX optimizer to load classes from an unfinished DEX */
128 DvmDex* bootClassPathOptExtra;
129 bool optimizingBootstrapClass;
132 * Loaded classes, hashed by class name. Each entry is a ClassObject*,
133 * allocated in GC space.
135 HashTable* loadedClasses;
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.
144 volatile int classSerialNumber;
147 * Classes with a low classSerialNumber are probably in the zygote, and
148 * their InitiatingLoaderList is not used, to promote sharing. The list is
151 InitiatingLoaderList* initiatingLoaderList;
156 HashTable* internedStrings;
159 * Quick lookups for popular classes used internally.
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;
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;
202 /* method offsets - Object */
203 int voffJavaLangObject_equals;
204 int voffJavaLangObject_hashCode;
205 int voffJavaLangObject_toString;
206 int voffJavaLangObject_finalize;
208 /* field offsets - Class */
209 int offJavaLangClass_pd;
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;
218 /* field offsets - Thread */
219 int offJavaLangThread_vmThread;
220 int offJavaLangThread_group;
221 int offJavaLangThread_daemon;
222 int offJavaLangThread_name;
223 int offJavaLangThread_priority;
225 /* method offsets - Thread */
226 int voffJavaLangThread_run;
228 /* field offsets - VMThread */
229 int offJavaLangVMThread_thread;
230 int offJavaLangVMThread_vmData;
232 /* method offsets - ThreadGroup */
233 int voffJavaLangThreadGroup_removeThread;
235 /* field offsets - Throwable */
236 int offJavaLangThrowable_stackState;
237 int offJavaLangThrowable_message;
238 int offJavaLangThrowable_cause;
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;
249 /* field offsets - java.lang.ref.Reference */
250 int offJavaLangRefReference_referent;
251 int offJavaLangRefReference_queue;
252 int offJavaLangRefReference_queueNext;
253 int offJavaLangRefReference_vmData;
255 #if FANCY_REFERENCE_SUBCLASS
256 /* method offsets - java.lang.ref.Reference */
257 int voffJavaLangRefReference_clear;
258 int voffJavaLangRefReference_enqueue;
260 /* method pointers - java.lang.ref.Reference */
261 Method* methJavaLangRefReference_enqueueInternal;
264 /* field offsets - java.nio.Buffer and java.nio.DirectByteBufferImpl */
265 //int offJavaNioBuffer_capacity;
266 //int offJavaNioDirectByteBufferImpl_pointer;
268 /* method pointers - java.security.AccessController */
269 volatile bool javaSecurityAccessControllerReady;
270 Method* methJavaSecurityAccessController_doPrivileged[4];
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;
280 /* static method pointers - android.lang.annotation.* */
282 methOrgApacheHarmonyLangAnnotationAnnotationFactory_createAnnotation;
284 /* direct method pointers - java.lang.reflect.Proxy */
285 Method* methJavaLangReflectProxy_constructorPrototype;
287 /* field offsets - java.lang.reflect.Proxy */
288 int offJavaLangReflectProxy_h;
290 /* fake native entry point method */
291 Method* methFakeNativeEntry;
294 * VM-synthesized primitive classes, for arrays.
296 ClassObject* volatile primitiveClass[PRIM_MAX];
299 * A placeholder ClassObject used during ClassObject
302 ClassObject unlinkedJavaLangClassObject;
305 * Thread list. This always has at least one element in it (main),
306 * and main is always the first entry.
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
318 pthread_mutex_t threadListLock;
320 pthread_cond_t threadStartCond;
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.
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.
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.
339 pthread_mutex_t _threadSuspendLock;
342 * Guards Thread->suspendCount for all threads, and provides the lock
343 * for the condition variable that all suspended threads sleep on
344 * (threadSuspendCountCond).
346 * This has to be separate from threadListLock because of the way
347 * threads put themselves to sleep.
349 pthread_mutex_t threadSuspendCountLock;
352 * Suspended threads sleep on this. They should sleep on the condition
353 * variable until their "suspend count" is zero.
355 * Paired with "threadSuspendCountLock".
357 pthread_cond_t threadSuspendCountCond;
360 * MUTEX ORDERING: when locking multiple mutexes, always grab them in
361 * this order to avoid deadlock:
363 * (1) _threadSuspendLock (use lockThreadSuspend())
364 * (2) threadListLock (use dvmLockThreadList())
365 * (3) threadSuspendCountLock (use lockThreadSuspendCount())
370 * Thread ID bitmap. We want threads to have small integer IDs so
371 * we can use them in "thin locks".
373 BitVector* threadIdMap;
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.
380 int nonDaemonThreadCount; /* must hold threadListLock to access */
381 //pthread_mutex_t vmExitLock;
382 pthread_cond_t vmExitCond;
385 * The set of DEX files loaded by custom class loaders.
387 HashTable* userDexFiles;
390 * JNI global reference table.
392 ReferenceTable jniGlobalRefTable;
393 pthread_mutex_t jniGlobalRefLock;
394 int jniGlobalRefHiMark;
395 int jniGlobalRefLoMark;
398 * Native shared library table.
400 HashTable* nativeLibs;
403 * GC heap lock. Functions like gcMalloc() acquire this before making
404 * any changes to the heap. It is held throughout garbage collection.
406 pthread_mutex_t gcHeapLock;
408 /* Opaque pointer representing the heap. */
412 * Pre-allocated object for out-of-memory errors.
414 Object* outOfMemoryObj;
416 /* pre-allocated general failure exception */
417 Object* internalErrorObj;
419 /* Monitor list, so we can free them */
420 /*volatile*/ Monitor* monitorList;
422 /* Monitor for Thread.sleep() implementation */
423 Monitor* threadSleepMon;
425 /* set when we create a second heap inside the zygote */
426 bool newZygoteHeapAllocated;
431 pthread_key_t pthreadKeySelf; /* Thread*, for dvmThreadSelf */
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.
440 * Cache results of "A instanceof B".
442 AtomicCache* instanceofCache;
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;
452 * Bootstrap class loader linear allocator.
454 LinearAllocHdr* pBootLoaderAlloc;
458 * Heap worker thread.
460 bool heapWorkerInitialized;
461 bool heapWorkerReady;
463 pthread_t heapWorkerHandle;
464 pthread_mutex_t heapWorkerLock;
465 pthread_cond_t heapWorkerCond;
466 pthread_cond_t heapWorkerIdleCond;
467 pthread_mutex_t heapWorkerListLock;
470 * Compute some stats on loaded classes.
472 int numLoadedClasses;
473 int numDeclaredMethods;
474 int numDeclaredInstFields;
475 int numDeclaredStaticFields;
477 /* when using a native debugger, set this to suppress watchdog timers */
478 bool nativeDebuggerActive;
481 * JDWP debugger support.
483 bool debuggerConnected; /* debugger or DDMS is connected */
484 bool debuggerActive; /* debugger is making requests */
485 JdwpState* jdwpState;
488 * Registry of objects known to the debugger.
490 HashTable* dbgRegistry;
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.
498 const u2* debugBreakAddr[MAX_BREAKPOINTS];
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.
505 StepControl stepControl;
508 * DDM features embedded in the VM.
510 bool ddmThreadNotification;
513 * Zygote (partially-started process) support
518 * Used for tracking allocations that we report to DDMS. When the feature
519 * is enabled (through a DDMS request) the "allocRecords" pointer becomes
522 pthread_mutex_t allocTrackerLock;
523 AllocRecord* allocRecords;
524 int allocRecordHead; /* most-recently-added entry */
525 int allocRecordCount; /* #of valid entries */
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 */
534 #ifdef WITH_DEADLOCK_PREDICTION
535 /* global lock on history tree accesses */
536 pthread_mutex_t deadlockHistoryLock;
538 enum { kDPOff=0, kDPWarn, kDPErr, kDPAbort } deadlockPredictMode;
543 * When a profiler is enabled, this is incremented. Distinct profilers
544 * include "dmtrace" method tracing, emulator method tracing, and
545 * possibly instruction counting.
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.
550 volatile int activeProfilers;
553 * State for method-trace profiling.
555 MethodTraceState methodTrace;
558 * State for emulator tracing.
560 void* emulatorTracePage;
561 int emulatorTraceEnableCount;
564 * Global state for memory allocation profiling.
566 AllocProfState allocProf;
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.
573 Method** inlinedMethods;
576 * Dalvik instruction counts (256 entries).
578 int* executedInstrCounts;
579 bool instructionCountEnableCount;
583 * Signal catcher thread (for SIGQUIT).
585 pthread_t signalCatcherHandle;
586 bool haltSignalCatcher;
589 * Stdout/stderr conversion thread.
591 bool haltStdioConverter;
592 bool stdioConverterReady;
593 pthread_t stdioConverterHandle;
594 pthread_mutex_t stdioConverterLock;
595 pthread_cond_t stdioConverterCond;
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.
601 pid_t systemServerPid;
603 //#define COUNT_PRECISE_METHODS
604 #ifdef COUNT_PRECISE_METHODS
605 PointerSet* preciseMethods;
609 extern struct DvmGlobals gDvm;
611 #endif /*_DALVIK_GLOBALS*/