aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/runtime/VM.java
blob: c6ab99073e72f878e49de9d899cda8dda8ee5499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.runtime;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import sun.jvm.hotspot.code.*;
import sun.jvm.hotspot.c1.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.interpreter.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.types.*;
import sun.jvm.hotspot.utilities.*;
import sun.jvm.hotspot.runtime.*;

/** <P> This class encapsulates the global state of the VM; the
    universe, object heap, interpreter, etc. It is a Singleton and
    must be initialized with a call to initialize() before calling
    getVM(). </P>

    <P> Many auxiliary classes (i.e., most of the VMObjects) keep
    needed field offsets in the form of static Field objects. In a
    debugging system, the VM might be shutdown and re-initialized (on
    a differently-configured build, i.e., 32- vs. 64-bit), and all old
    cached state (including fields and field offsets) must be
    flushed. </P>

    <P> An Observer pattern is used to implement the initialization of
    such classes. Each such class, in its static initializer,
    registers an Observer with the VM class via
    VM.registerVMInitializedObserver(). This Observer is guaranteed to
    be notified whenever the VM is initialized (or re-initialized). To
    implement the first-time initialization, the observer is also
    notified when it registers itself with the VM. (For bootstrapping
    reasons, this implies that the constructor of VM can not
    instantiate any such objects, since VM.soleInstance will not have
    been set yet. This is a bootstrapping issue which may have to be
    revisited later.) </P>
*/

public class VM {
  private static VM    soleInstance;
  private static List  vmInitializedObservers = new ArrayList();
  private List         vmResumedObservers   = new ArrayList();
  private List         vmSuspendedObservers = new ArrayList();
  private TypeDataBase db;
  private boolean      isBigEndian;
  /** This is only present if in a debugging system */
  private JVMDebugger  debugger;
  private long         stackBias;
  private long         logAddressSize;
  private Universe     universe;
  private ObjectHeap   heap;
  private SymbolTable  symbols;
  private StringTable  strings;
  private SystemDictionary dict;
  private Threads      threads;
  private ObjectSynchronizer synchronizer;
  private JNIHandles   handles;
  private Interpreter  interpreter;
  private StubRoutines stubRoutines;
  private Bytes        bytes;
  /** Flags indicating whether we are attached to a core, C1, or C2 build */
  private boolean      usingClientCompiler;
  private boolean      usingServerCompiler;
  /** Flag indicating whether UseTLAB is turned on */
  private boolean      useTLAB;
  /** alignment constants */
  private boolean      isLP64;
  private int          bytesPerLong;
  private int          minObjAlignmentInBytes;
  private int          logMinObjAlignmentInBytes;
  private int          heapWordSize;
  private int          heapOopSize;
  private int          oopSize;
  /** This is only present in a non-core build */
  private CodeCache    codeCache;
  /** This is only present in a C1 build */
  private Runtime1     runtime1;
  /** These constants come from globalDefinitions.hpp */
  private int          invocationEntryBCI;
  private int          invalidOSREntryBCI;
  private ReversePtrs  revPtrs;
  private VMRegImpl    vmregImpl;

  // System.getProperties from debuggee VM
  private Properties   sysProps;

  // VM version strings come from Abstract_VM_Version class
  private String       vmRelease;
  private String       vmInternalInfo;

  private Flag[] commandLineFlags;
  private Map flagsMap;

  private static Type intxType;
  private static Type uintxType;
  private static CIntegerType boolType;
  private Boolean sharingEnabled;
  private Boolean compressedOopsEnabled;

  // command line flags supplied to VM - see struct Flag in globals.hpp
  public static final class Flag {
     private String type;
     private String name;
     private Address addr;
     private String kind;

     private Flag(String type, String name, Address addr, String kind) {
        this.type = type;
        this.name = name;
        this.addr = addr;
        this.kind = kind;
     }

     public String getType() {
        return type;
     }

     public String getName() {
        return name;
     }

     public Address getAddress() {
        return addr;
     }

     public String getKind() {
        return kind;
     }

     public boolean isBool() {
        return type.equals("bool");
     }

     public boolean getBool() {
        if (Assert.ASSERTS_ENABLED) {
           Assert.that(isBool(), "not a bool flag!");
        }
        return addr.getCIntegerAt(0, boolType.getSize(), boolType.isUnsigned())
               != 0;
     }

     public boolean isIntx() {
        return type.equals("intx");
     }

     public long getIntx() {
        if (Assert.ASSERTS_ENABLED) {
           Assert.that(isIntx(), "not a intx flag!");
        }
        return addr.getCIntegerAt(0, intxType.getSize(), false);
     }

     public boolean isUIntx() {
        return type.equals("uintx");
     }

     public long getUIntx() {
        if (Assert.ASSERTS_ENABLED) {
           Assert.that(isUIntx(), "not a uintx flag!");
        }
        return addr.getCIntegerAt(0, uintxType.getSize(), true);
     }

     public String getValue() {
        if (isBool()) {
           return new Boolean(getBool()).toString();
        } else if (isIntx()) {
           return new Long(getIntx()).toString();
        } else if (isUIntx()) {
           return new Long(getUIntx()).toString();
        } else {
           return null;
        }
     }
  };

  private static void checkVMVersion(String vmRelease) {
     if (System.getProperty("sun.jvm.hotspot.runtime.VM.disableVersionCheck") == null) {
        // read sa build version.
        String versionProp = "sun.jvm.hotspot.runtime.VM.saBuildVersion";
        String saVersion = saProps.getProperty(versionProp);
        if (saVersion == null)
           throw new RuntimeException("Missing property " + versionProp);

        // Strip nonproduct VM version substring (note: saVersion doesn't have it).
        String vmVersion = vmRelease.replaceAll("(-fastdebug)|(-debug)|(-jvmg)|(-optimized)|(-profiled)","");

        if (saVersion.equals(vmVersion)) {
           // Exact match
           return;
        }
        if (saVersion.indexOf('-') == saVersion.lastIndexOf('-') &&
            vmVersion.indexOf('-') == vmVersion.lastIndexOf('-')) {
           // Throw exception if different release versions:
           // <major>.<minor>-b<n>
           throw new VMVersionMismatchException(saVersion, vmRelease);
        } else {
           // Otherwise print warning to allow mismatch not release versions
           // during development.
           System.err.println("WARNING: Hotspot VM version " + vmRelease +
                              " does not match with SA version " + saVersion +
                              "." + " You may see unexpected results. ");
        }
     } else {
        System.err.println("WARNING: You have disabled SA and VM version check. You may be "  +
                           "using incompatible version of SA and you may see unexpected " +
                           "results.");
     }
  }

  private static final boolean disableDerivedPrinterTableCheck;
  private static final Properties saProps;

  static {
     saProps = new Properties();
     URL url = null;
     try {
       url = VM.class.getClassLoader().getResource("sa.properties");
       saProps.load(new BufferedInputStream(url.openStream()));
     } catch (Exception e) {
       throw new RuntimeException("Unable to load properties  " +
                                  (url == null ? "null" : url.toString()) +
                                  ": " + e.getMessage());
     }

     disableDerivedPrinterTableCheck = System.getProperty("sun.jvm.hotspot.runtime.VM.disableDerivedPointerTableCheck") != null;
  }

  private VM(TypeDataBase db, JVMDebugger debugger, boolean isBigEndian) {
    this.db          = db;
    this.debugger    = debugger;
    this.isBigEndian = isBigEndian;

    // Note that we don't construct universe, heap, threads,
    // interpreter, or stubRoutines here (any more).  The current
    // initialization mechanisms require that the VM be completely set
    // up (i.e., out of its constructor, with soleInstance assigned)
    // before their static initializers are run.

    if (db.getAddressSize() == 4) {
      logAddressSize = 2;
    } else if (db.getAddressSize() == 8) {
      logAddressSize = 3;
    } else {
      throw new RuntimeException("Address size " + db.getAddressSize() + " not yet supported");
    }

    // read VM version info
    try {
       Type vmVersion = db.lookupType("Abstract_VM_Version");
       Address releaseAddr = vmVersion.getAddressField("_s_vm_release").getValue();
       vmRelease = CStringUtilities.getString(releaseAddr);
       Address vmInternalInfoAddr = vmVersion.getAddressField("_s_internal_vm_info_string").getValue();
       vmInternalInfo = CStringUtilities.getString(vmInternalInfoAddr);
    } catch (Exception exp) {
       throw new RuntimeException("can't determine target's VM version : " + exp.getMessage());
    }

    checkVMVersion(vmRelease);

    stackBias    = db.lookupIntConstant("STACK_BIAS").intValue();
    invocationEntryBCI = db.lookupIntConstant("InvocationEntryBci").intValue();
    invalidOSREntryBCI = db.lookupIntConstant("InvalidOSREntryBci").intValue();

    // We infer the presence of C1 or C2 from a couple of fields we
    // already have present in the type database
    {
      Type type = db.lookupType("methodOopDesc");
      if (type.getField("_from_compiled_entry", false, false) == null) {
        // Neither C1 nor C2 is present
        usingClientCompiler = false;
        usingServerCompiler = false;
      } else {
        // Determine whether C2 is present
        if (type.getField("_interpreter_invocation_count", false, false) != null) {
          usingServerCompiler = true;
        } else {
          usingClientCompiler = true;
        }
      }
    }

    useTLAB = (db.lookupIntConstant("UseTLAB").intValue() != 0);

    if (debugger != null) {
      isLP64 = debugger.getMachineDescription().isLP64();
    }
    bytesPerLong = db.lookupIntConstant("BytesPerLong").intValue();
    minObjAlignmentInBytes = db.lookupIntConstant("MinObjAlignmentInBytes").intValue();
    // minObjAlignment = db.lookupIntConstant("MinObjAlignment").intValue();
    logMinObjAlignmentInBytes = db.lookupIntConstant("LogMinObjAlignmentInBytes").intValue();
    heapWordSize = db.lookupIntConstant("HeapWordSize").intValue();
    oopSize  = db.lookupIntConstant("oopSize").intValue();
    heapOopSize  = db.lookupIntConstant("heapOopSize").intValue();

    intxType = db.lookupType("intx");
    uintxType = db.lookupType("uintx");
    boolType = (CIntegerType) db.lookupType("bool");
  }

  /** This could be used by a reflective runtime system */
  public static void initialize(TypeDataBase db, boolean isBigEndian) {
    if (soleInstance != null) {
      throw new RuntimeException("Attempt to initialize VM twice");
    }
    soleInstance = new VM(db, null, isBigEndian);
    for (Iterator iter = vmInitializedObservers.iterator(); iter.hasNext(); ) {
      ((Observer) iter.next()).update(null, null);
    }
  }

  /** This is used by the debugging system */
  public static void initialize(TypeDataBase db, JVMDebugger debugger) {
    if (soleInstance != null) {
      throw new RuntimeException("Attempt to initialize VM twice");
    }
    soleInstance = new VM(db, debugger, debugger.getMachineDescription().isBigEndian());
    debugger.putHeapConst(Universe.getHeapBase(), soleInstance.getHeapOopSize(),
                          soleInstance.logMinObjAlignmentInBytes);
    for (Iterator iter = vmInitializedObservers.iterator(); iter.hasNext(); ) {
      ((Observer) iter.next()).update(null, null);
    }
  }

  /** This is used by the debugging system */
  public static void shutdown() {
    soleInstance = null;
  }

  /** This is used by both the debugger and any runtime system. It is
      the basic mechanism by which classes which mimic underlying VM
      functionality cause themselves to be initialized. The given
      observer will be notified (with arguments (null, null)) when the
      VM is re-initialized, as well as when it registers itself with
      the VM. */
  public static void registerVMInitializedObserver(Observer o) {
    vmInitializedObservers.add(o);
    o.update(null, null);
  }

  /** This is the primary accessor used by both the debugger and any
      potential runtime system */
  public static VM getVM() {
    if (soleInstance == null) {
      throw new RuntimeException("VM.initialize() was not yet called");
    }
    return soleInstance;
  }

  /** This is only used by the debugging system. The given observer
      will be notified if the underlying VM resumes execution. NOTE
      that the given observer is not triggered if the VM is currently
      running and therefore differs in behavior from {@link
      #registerVMInitializedObserver} (because of the possibility of
      race conditions if the observer is added while the VM is being
      suspended or resumed).  */
  public void registerVMResumedObserver(Observer o) {
    vmResumedObservers.add(o);
  }

  /** This is only used by the debugging system. The given observer
      will be notified if the underlying VM suspends execution. NOTE
      that the given observer is not triggered if the VM is currently
      suspended and therefore differs in behavior from {@link
      #registerVMInitializedObserver} (because of the possibility of
      race conditions if the observer is added while the VM is being
      suspended or resumed).  */
  public void registerVMSuspendedObserver(Observer o) {
    vmSuspendedObservers.add(o);
  }

  /** This is only used by the debugging system. Informs all
      registered resumption observers that the VM has been resumed.
      The application is responsible for actually having performed the
      resumption. No OopHandles must be used after this point, as they
      may move in the target address space due to garbage
      collection. */
  public void fireVMResumed() {
    for (Iterator iter = vmResumedObservers.iterator(); iter.hasNext(); ) {
      ((Observer) iter.next()).update(null, null);
    }
  }

  /** This is only used by the debugging system. Informs all
      registered suspension observers that the VM has been suspended.
      The application is responsible for actually having performed the
      suspension. Garbage collection must be forbidden at this point;
      for example, a JPDA-level suspension is not adequate since the
      VM thread may still be running. */
  public void fireVMSuspended() {
    for (Iterator iter = vmSuspendedObservers.iterator(); iter.hasNext(); ) {
      ((Observer) iter.next()).update(null, null);
    }
  }

  /** Returns the OS this VM is running on. Notice that by delegating
      to the debugger we can transparently support remote
      debugging. */
  public String getOS() {
    if (debugger != null) {
      return debugger.getOS();
    }
    return PlatformInfo.getOS();
  }

  /** Returns the CPU this VM is running on. Notice that by delegating
      to the debugger we can transparently support remote
      debugging. */
  public String getCPU() {
    if (debugger != null) {
      return debugger.getCPU();
    }
    return PlatformInfo.getCPU();
  }

  public Type lookupType(String cTypeName) {
    return db.lookupType(cTypeName);
  }

  public Integer lookupIntConstant(String name) {
    return db.lookupIntConstant(name);
  }

  public long getAddressSize() {
    return db.getAddressSize();
  }

  public long getOopSize() {
    return oopSize;
  }

  public long getLogAddressSize() {
    return logAddressSize;
  }

  public long getIntSize() {
    return db.getJIntType().getSize();
  }

  /** NOTE: this offset is in BYTES in this system! */
  public long getStackBias() {
    return stackBias;
  }

  /** Indicates whether the underlying machine supports the LP64 data
      model. This is needed for conditionalizing code in a few places */
  public boolean isLP64() {
    if (Assert.ASSERTS_ENABLED) {
      Assert.that(isDebugging(), "Debugging system only for now");
    }
    return isLP64;
  }

  /** Get bytes-per-long == long/double natural alignment. */
  public int getBytesPerLong() {
    return bytesPerLong;
  }

  /** Get minimum object alignment in bytes. */
  public int getMinObjAlignment() {
    return minObjAlignmentInBytes;
  }

  public int getMinObjAlignmentInBytes() {
    return minObjAlignmentInBytes;
  }
  public int getLogMinObjAlignmentInBytes() {
    return logMinObjAlignmentInBytes;
  }

  public int getHeapWordSize() {
    return heapWordSize;
  }

  public int getHeapOopSize() {
    return heapOopSize;
  }
  /** Utility routine for getting data structure alignment correct */
  public long alignUp(long size, long alignment) {
    return (size + alignment - 1) & ~(alignment - 1);
  }

  /** Utility routine for getting data structure alignment correct */
  public long alignDown(long size, long alignment) {
    return size & ~(alignment - 1);
  }

  /** Utility routine for building an int from two "unsigned" 16-bit
      shorts */
  public int buildIntFromShorts(short low, short high) {
    return (((int) high) << 16) | (((int) low) & 0xFFFF);
  }

  /** Utility routine for building a long from two "unsigned" 32-bit
      ints in <b>platform-dependent</b> order */
  public long buildLongFromIntsPD(int oneHalf, int otherHalf) {
    if (isBigEndian) {
      return (((long) otherHalf) << 32) | (((long) oneHalf) & 0x00000000FFFFFFFFL);
    } else{
      return (((long) oneHalf) << 32) | (((long) otherHalf) & 0x00000000FFFFFFFFL);
    }
  }

  /** Indicates whether Thread-Local Allocation Buffers are used */
  public boolean getUseTLAB() {
    return useTLAB;
  }

  public TypeDataBase getTypeDataBase() {
    return db;
  }

  public Universe    getUniverse() {
    if (universe == null) {
      universe = new Universe();
    }
    return universe;
  }

  public ObjectHeap  getObjectHeap() {
    if (heap == null) {
      heap = new ObjectHeap(db);
    }
    return heap;
  }

  public SymbolTable getSymbolTable() {
    if (symbols == null) {
      symbols = SymbolTable.getTheTable();
    }
    return symbols;
  }

  public StringTable getStringTable() {
    if (strings == null) {
      strings = StringTable.getTheTable();
    }
    return strings;
  }

  public SystemDictionary getSystemDictionary() {
    if (dict == null) {
      dict = new SystemDictionary();
    }
    return dict;
  }

  public Threads     getThreads() {
    if (threads == null) {
      threads = new Threads();
    }
    return threads;
  }

  public ObjectSynchronizer getObjectSynchronizer() {
    if (synchronizer == null) {
      synchronizer = new ObjectSynchronizer();
    }
    return synchronizer;
  }

  public JNIHandles getJNIHandles() {
    if (handles == null) {
      handles = new JNIHandles();
    }
    return handles;
  }

  public Interpreter getInterpreter() {
    if (interpreter == null) {
      interpreter = new Interpreter();
    }
    return interpreter;
  }

  public StubRoutines getStubRoutines() {
    if (stubRoutines == null) {
      stubRoutines = new StubRoutines();
    }
    return stubRoutines;
  }

  public VMRegImpl getVMRegImplInfo() {
    if (vmregImpl == null) {
      vmregImpl = new VMRegImpl();
    }
    return vmregImpl;
  }

  public Bytes getBytes() {
    if (bytes == null) {
      bytes = new Bytes(debugger.getMachineDescription());
    }
    return bytes;
  }

  /** Returns true if this is a "core" build, false if either C1 or C2
      is present */
  public boolean isCore() {
    return (!(usingClientCompiler || usingServerCompiler));
  }

  /** Returns true if this is a C1 build, false otherwise */
  public boolean isClientCompiler() {
    return usingClientCompiler;
  }

  /** Returns true if this is a C2 build, false otherwise */
  public boolean isServerCompiler() {
    return usingServerCompiler;
  }

  /** Returns true if C2 derived pointer table should be used, false otherwise */
  public boolean useDerivedPointerTable() {
    return !disableDerivedPrinterTableCheck;
  }

  /** Returns the code cache; should not be used if is core build */
  public CodeCache getCodeCache() {
    if (Assert.ASSERTS_ENABLED) {
      Assert.that(!isCore(), "noncore builds only");
    }
    if (codeCache == null) {
      codeCache = new CodeCache();
    }
    return codeCache;
  }

  /** Should only be called for C1 builds */
  public Runtime1 getRuntime1() {
    if (Assert.ASSERTS_ENABLED) {
      Assert.that(isClientCompiler(), "C1 builds only");
    }
    if (runtime1 == null) {
      runtime1 = new Runtime1();
    }
    return runtime1;
  }

  /** Test to see whether we're in debugging mode (NOTE: this really
      should not be tested by this code; currently only used in
      StackFrameStream) */
  public boolean isDebugging() {
    return (debugger != null);
  }

  /** This is only used by the debugging (i.e., non-runtime) system */
  public JVMDebugger getDebugger() {
    if (debugger == null) {
      throw new RuntimeException("Attempt to use debugger in runtime system");
    }
    return debugger;
  }

  /** Indicates whether a given program counter is in Java code. This
      includes but is not spanned by the interpreter and code cache.
      Only used in the debugging system, for implementing
      JavaThread.currentFrameGuess() on x86. */
  public boolean isJavaPCDbg(Address addr) {
    // FIXME: this is not a complete enough set: must include areas
    // like vtable stubs
    return (getInterpreter().contains(addr) ||
            getCodeCache().contains(addr));
  }

  /** FIXME: figure out where to stick this */
  public int getInvocationEntryBCI() {
    return invocationEntryBCI;
  }

  /** FIXME: figure out where to stick this */
  public int getInvalidOSREntryBCI() {
    return invalidOSREntryBCI;
  }

  // FIXME: figure out where to stick this
  public boolean wizardMode() {
    return true;
  }

  public ReversePtrs getRevPtrs() {
    return revPtrs;
  }

  public void setRevPtrs(ReversePtrs rp) {
    revPtrs = rp;
  }

  // returns null, if not available.
  public String getVMRelease() {
    return vmRelease;
  }

  // returns null, if not available.
  public String getVMInternalInfo() {
    return vmInternalInfo;
  }

  public boolean isSharingEnabled() {
    if (sharingEnabled == null) {
      Flag flag = getCommandLineFlag("UseSharedSpaces");
      sharingEnabled = (flag == null)? Boolean.FALSE :
          (flag.getBool()? Boolean.TRUE: Boolean.FALSE);
    }
    return sharingEnabled.booleanValue();
  }

  public boolean isCompressedOopsEnabled() {
    if (compressedOopsEnabled == null) {
        Flag flag = getCommandLineFlag("UseCompressedOops");
        compressedOopsEnabled = (flag == null) ? Boolean.FALSE:
             (flag.getBool()? Boolean.TRUE: Boolean.FALSE);
    }
    return compressedOopsEnabled.booleanValue();
  }

  // returns null, if not available.
  public Flag[] getCommandLineFlags() {
    if (commandLineFlags == null) {
       readCommandLineFlags();
    }

    return commandLineFlags;
  }

  public Flag getCommandLineFlag(String name) {
    if (flagsMap == null) {
      flagsMap = new HashMap();
      Flag[] flags = getCommandLineFlags();
      for (int i = 0; i < flags.length; i++) {
        flagsMap.put(flags[i].getName(), flags[i]);
      }
    }
    return (Flag) flagsMap.get(name);
  }

  private void readCommandLineFlags() {
    // get command line flags
    TypeDataBase db = getTypeDataBase();
    try {
       Type flagType = db.lookupType("Flag");
       int numFlags = (int) flagType.getCIntegerField("numFlags").getValue();
       // NOTE: last flag contains null values.
       commandLineFlags = new Flag[numFlags - 1];

       Address flagAddr = flagType.getAddressField("flags").getValue();

       AddressField typeFld = flagType.getAddressField("type");
       AddressField nameFld = flagType.getAddressField("name");
       AddressField addrFld = flagType.getAddressField("addr");
       AddressField kindFld = flagType.getAddressField("kind");

       long flagSize = flagType.getSize(); // sizeof(Flag)

       // NOTE: last flag contains null values.
       for (int f = 0; f < numFlags - 1; f++) {
          String type = CStringUtilities.getString(typeFld.getValue(flagAddr));
          String name = CStringUtilities.getString(nameFld.getValue(flagAddr));
          Address addr = addrFld.getValue(flagAddr);
          String kind = CStringUtilities.getString(kindFld.getValue(flagAddr));
          commandLineFlags[f] = new Flag(type, name, addr, kind);
          flagAddr = flagAddr.addOffsetTo(flagSize);
       }

       // sort flags by name
       Arrays.sort(commandLineFlags, new Comparator() {
                                        public int compare(Object o1, Object o2) {
                                           Flag f1 = (Flag) o1;
                                           Flag f2 = (Flag) o2;
                                           return f1.getName().compareTo(f2.getName());
                                        }
                                     });
    } catch (Exception exp) {
       // ignore. may be older version. command line flags not available.
    }
  }

  public String getSystemProperty(String key) {
    Properties props = getSystemProperties();
    return (props != null)? props.getProperty(key) : null;
  }

  public Properties getSystemProperties() {
    if (sysProps == null) {
       readSystemProperties();
    }
    return sysProps;
  }

  private void readSystemProperties() {
     InstanceKlass systemKls = getSystemDictionary().getSystemKlass();
     systemKls.iterate(new DefaultOopVisitor() {
                               ObjectReader objReader = new ObjectReader();
                               public void doOop(sun.jvm.hotspot.oops.OopField field, boolean isVMField) {
                                  if (field.getID().getName().equals("props")) {
                                     try {
                                        sysProps = (Properties) objReader.readObject(field.getValue(getObj()));
                                     } catch (Exception e) {
                                        if (Assert.ASSERTS_ENABLED) {
                                           e.printStackTrace();
                                        }
                                     }
                                  }
                               }
                        }, false);
  }
}