view agent/src/share/classes/sun/jvm/hotspot/debugger/proc/ProcDebuggerLocal.java @ 10905:f57189b7648d

8257192: Integrate AArch64 JIT port into 8u 7009641: Don't fail VM when CodeCache is full 8073108: [AArch64] Use x86 and SPARC CPU instructions for GHASH acceleration 8130309: Need to bailout cleanly if creation of stubs fails when codecache is out of space (AArch64 changes) 8131779: AARCH64: add Montgomery multiply intrinsic 8132875: AArch64: Fix error introduced into AArch64 CodeCache by commit for 8130309 8135018: AARCH64: Missing memory barriers for CMS collector 8145320: Create unsafe_arraycopy and generic_arraycopy for AArch64 8148328: aarch64: redundant lsr instructions in stub code. 8148783: aarch64: SEGV running SpecJBB2013 8148948: aarch64: generate_copy_longs calls align() incorrectly 8149080: AArch64: Recognise disjoint array copy in stub code 8149365: aarch64: memory copy does not prefetch on backwards copy 8149907: aarch64: use load/store pair instructions in call_stub 8150038: aarch64: make use of CBZ and CBNZ when comparing narrow pointer with zero 8150045: arraycopy causes segfaults in SATB during garbage collection 8150082: aarch64: optimise small array copy 8150229: aarch64: pipeline class for several instructions is not set correctly 8150313: aarch64: optimise array copy using SIMD instructions 8150394: aarch64: add support for 8.1 LSE CAS instructions 8150652: Remove unused code in AArch64 back end 8151340: aarch64: prefetch the destination word for write prior to ldxr/stxr loops. 8151502: optimize pd_disjoint_words and pd_conjoint_words 8151775: aarch64: add support for 8.1 LSE atomic operations 8152537: aarch64: Make use of CBZ and CBNZ when comparing unsigned values with zero. 8152840: aarch64: improve _unsafe_arraycopy stub routine 8153172: aarch64: hotspot crashes after the 8.1 LSE patch is merged 8153713: aarch64: improve short array clearing using store pair 8153797: aarch64: Add Arrays.fill stub code 8154413: AArch64: Better byte behaviour 8154537: AArch64: some integer rotate instructions are never emitted 8154739: AArch64: TemplateTable::fast_xaccess loads in wrong mode 8155015: Aarch64: bad assert in spill generation code 8155100: AArch64: Relax alignment requirement for byte_map_base 8155612: Aarch64: vector nodes need to support misaligned offset 8155617: aarch64: ClearArray does not use DC ZVA 8155627: Enable SA on AArch64 8155653: TestVectorUnalignedOffset.java not pushed with 8155612 8156731: aarch64: java/util/Arrays/Correct.java fails due to _generic_arraycopy stub routine 8157841: aarch64: prefetch ignores cache line size 8157906: aarch64: some more integer rotate instructions are never emitted 8158913: aarch64: SEGV running Spark terasort 8159052: aarch64: optimise unaligned copies in pd_disjoint_words and pd_conjoint_words 8159063: aarch64: optimise unaligned array copy long 8160748: [AArch64] Inconsistent types for ideal_reg 8161072: AArch64: jtreg compiler/uncommontrap/TestDeoptOOM failure 8161190: AArch64: Fix overflow in immediate cmp instruction 8164113: AArch64: follow-up the fix for 8161598 8165673: AArch64: Fix JNI floating point argument handling 8167200: AArch64: Broken stack pointer adjustment in interpreter 8167421: AArch64: in one core system, fatal error: Illegal threadstate encountered 8167595: AArch64: SEGV in stub code cipherBlockChaining_decryptAESCrypt 8168699: Validate special case invocations [AArch64 support] 8168888: Port 8160591: Improve internal array handling to AArch64. 8170100: AArch64: Crash in C1-compiled code accessing References 8170188: jtreg test compiler/types/TestMeetIncompatibleInterfaceArrays.java causes JVM crash 8170873: PPC64/aarch64: Poor StrictMath performance due to non-optimized compilation 8171537: aarch64: compiler/c1/Test6849574.java generates guarantee failure in C1 8172881: AArch64: assertion failure: the int pressure is incorrect 8173472: AArch64: C1 comparisons with null only use 32-bit instructions 8176100: [AArch64] [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles 8177661: Correct ad rule output register types from iRegX to iRegXNoSp 8179954: AArch64: C1 and C2 volatile accesses are not sequentially consistent 8182581: aarch64: fix for crash caused by earlyret of compiled method 8183925: [AArch64] Decouple crash protection from watcher thread 8186325: AArch64: jtreg test hotspot/test/gc/g1/TestJNIWeakG1/TestJNIWeakG1.java SEGV 8187224: aarch64: some inconsistency between aarch64_ad.m4 and aarch64.ad 8189170: [AArch64] Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM 8193133: Assertion failure because 0xDEADDEAD can be in-heap 8195685: AArch64 port of 8174962: Better interface invocations 8195859: AArch64: vtableStubs gtest fails after 8174962 8196136: AArch64: Correct register use in patch for JDK-8194686 8196221: AArch64: Mistake in committed patch for JDK-8195859 8199712: [AArch64] Flight Recorder 8203481: Incorrect constraint for unextended_sp in frame:safe_for_sender 8203699: java/lang/invoke/SpecialInterfaceCall fails with SIGILL on aarch64 8205421: AARCH64: StubCodeMark should be placed after alignment 8206163: AArch64: incorrect code generation for StoreCM 8207345: Trampoline generation code reads from uninitialized memory 8207838: AArch64: Float registers incorrectly restored in JNI call 8209413: AArch64: NPE in clhsdb jstack command 8209414: [AArch64] method handle invocation does not respect JVMTI interp_only mode 8209415: Fix JVMTI test failure HS202 8209420: Track membars for volatile accesses so they can be properly optimized 8209835: Aarch64: elide barriers on all volatile operations 8210425: [AArch64] sharedRuntimeTrig/sharedRuntimeTrans compiled without optimization 8211064: [AArch64] Interpreter and c1 don't correctly handle jboolean results in native calls 8211233: MemBarNode::trailing_membar() and MemBarNode::leading_membar() need to handle dying subgraphs better 8213134: AArch64: vector shift failed with MaxVectorSize=8 8213419: [AArch64] C2 may hang in MulLNode::Ideal()/MulINode::Ideal() with gcc 8.2.1 8214857: "bad trailing membar" assert failure at memnode.cpp:3220 8215951: AArch64: jtreg test vmTestbase/nsk/jvmti/PopFrame/popframe005 segfaults 8215961: jdk/jfr/event/os/TestCPUInformation.java fails on AArch64 8216350: AArch64: monitor unlock fast path not called 8216989: CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier() does not check for zero length on AARCH64 8217368: AArch64: C2 recursive stack locking optimisation not triggered 8218185: aarch64: missing LoadStore barrier in TemplateTable::putfield_or_static 8219011: Implement MacroAssembler::warn method on AArch64 8219635: aarch64: missing LoadStore barrier in TemplateTable::fast_storefield 8221220: AArch64: Add StoreStore membar explicitly for Volatile Writes in TemplateTable 8221658: aarch64: add necessary predicate for ubfx patterns 8224671: AArch64: mauve System.arraycopy test failure 8224828: aarch64: rflags is not correct after safepoint poll 8224851: AArch64: fix warnings and errors with Clang and GCC 8.3 8224880: AArch64: java/javac error with AllocatePrefetchDistance 8228400: Remove built-in AArch64 simulator 8228406: Superfluous change in chaitin.hpp 8228593: Revert explicit JDK 7 support additions 8228716: Revert InstanceKlass::print_on debug additions 8228718: Revert incorrect backport of JDK-8129757 to 8-aarch64 8228725: AArch64: Purge method call format support 8228747: Revert "unused" attribute from test_arraycopy_func 8228767: Revert ResourceMark additions 8228770: Revert development hsdis changes 8229123: Revert build fixes for aarch64/zero 8229124: Revert disassembler.cpp changes 8229145: Revert TemplateTable::bytecode() visibility change 8233839: aarch64: missing memory barrier in NewObjectArrayStub and NewTypeArrayStub 8237512: AArch64: aarch64TestHook leaks a BufferBlob 8246482: Build failures with +JFR -PCH 8247979: aarch64: missing side effect of killing flags for clearArray_reg_reg 8248219: aarch64: missing memory barrier in fast_storefield and fast_accessfield Reviewed-by: shade, aph
author andrew
date Mon, 01 Feb 2021 03:48:36 +0000
parents 8e47bac5643a
children
line wrap: on
line source

/*
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package sun.jvm.hotspot.debugger.proc;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.debugger.cdbg.*;
import sun.jvm.hotspot.debugger.proc.amd64.*;
import sun.jvm.hotspot.debugger.proc.aarch64.*;
import sun.jvm.hotspot.debugger.proc.sparc.*;
import sun.jvm.hotspot.debugger.proc.x86.*;
import sun.jvm.hotspot.debugger.amd64.*;
import sun.jvm.hotspot.debugger.aarch64.*;
import sun.jvm.hotspot.debugger.sparc.*;
import sun.jvm.hotspot.debugger.x86.*;
import sun.jvm.hotspot.utilities.*;

/** <P> An implementation of the JVMDebugger interface which sits on
 * top of proc and relies on the SA's proc import module for
 * communication with the debugger. </P>
 *
 * <P> <B>NOTE</B> that since we have the notion of fetching "Java
 * primitive types" from the remote process (which might have
 * different sizes than we expect) we have a bootstrapping
 * problem. We need to know the sizes of these types before we can
 * fetch them. The current implementation solves this problem by
 * requiring that it be configured with these type sizes before they
 * can be fetched. The readJ(Type) routines here will throw a
 * RuntimeException if they are called before the debugger is
 * configured with the Java primitive type sizes. </P>
 */

public class ProcDebuggerLocal extends DebuggerBase implements ProcDebugger {
    protected static final int cacheSize = 16 * 1024 * 1024; // 16 MB

    //------------------------------------------------------------------------
    // Implementation of Debugger interface
    //

    /** <P> machDesc may be null if it couldn't be determined yet; i.e.,
     * if we're on SPARC, we need to ask the remote process whether
     * we're in 32- or 64-bit mode. </P>
     *
     * <P> useCache should be set to true if debugging is being done
     * locally, and to false if the debugger is being created for the
     * purpose of supporting remote debugging. </P> */
    public ProcDebuggerLocal(MachineDescription machDesc, boolean useCache) {
        this.machDesc = machDesc;
        int cacheNumPages;
        int cachePageSize;

        final String cpu = PlatformInfo.getCPU();
        if (cpu.equals("sparc")) {
            threadFactory = new ProcSPARCThreadFactory(this);
            pcRegIndex = SPARCThreadContext.R_PC;
            fpRegIndex = SPARCThreadContext.R_I6;
        } else if (cpu.equals("x86")) {
            threadFactory = new ProcX86ThreadFactory(this);
            pcRegIndex = X86ThreadContext.EIP;
            fpRegIndex = X86ThreadContext.EBP;
            unalignedAccessesOkay = true;
        } else if (cpu.equals("amd64") || cpu.equals("x86_64")) {
            threadFactory = new ProcAMD64ThreadFactory(this);
            pcRegIndex = AMD64ThreadContext.RIP;
            fpRegIndex = AMD64ThreadContext.RBP;
        } else if (cpu.equals("aarch64")) {
            threadFactory = new ProcAARCH64ThreadFactory(this);
            pcRegIndex = AARCH64ThreadContext.PC;
            fpRegIndex = AARCH64ThreadContext.FP;
        } else {
          try {
            Class tfc = Class.forName("sun.jvm.hotspot.debugger.proc." +
               cpu.toLowerCase() + ".Proc" + cpu.toUpperCase() +
               "ThreadFactory");
            Constructor[] ctfc = tfc.getConstructors();
            threadFactory = (ProcThreadFactory)ctfc[0].newInstance(this);
          } catch (Exception e) {
            throw new RuntimeException("Thread access for CPU architecture " + PlatformInfo.getCPU() + " not yet supported");
            // Note: pcRegIndex and fpRegIndex do not appear to be referenced
          }
        }
        if (useCache) {
            // Cache portion of the remote process's address space.
            // For now, this cache works best if it covers the entire
            // heap of the remote process. FIXME: at least should make this
            // tunable from the outside, i.e., via the UI. This is a 16 MB
            // cache divided on SPARC into 2048 8K pages and on x86 into
            // 4096 4K pages; the page size must be adjusted to be the OS's
            // page size.

            cachePageSize = getPageSize();
            cacheNumPages = parseCacheNumPagesProperty(cacheSize / cachePageSize);
            initCache(cachePageSize, cacheNumPages);
        }

        resetNativePointers();
        clearCacheFields();
    }

    /** FIXME: implement this with a Runtime.exec() of ps followed by
     * parsing of its output */
    public boolean hasProcessList() throws DebuggerException {
        return false;
    }

    public List getProcessList() throws DebuggerException {
        throw new DebuggerException("Not yet supported");
    }


    /** From the Debugger interface via JVMDebugger */
    public synchronized void attach(int processID) throws DebuggerException {
        checkAttached();
        isCore = false;
        attach0(new Integer(processID).toString());
        attached = true;
        suspended = true;
    }

    /** From the Debugger interface via JVMDebugger */
    public synchronized void attach
    (String executableName, String coreFileName) throws DebuggerException {
        checkAttached();
        isCore = true;
        topFrameCache = new HashMap();
        attach0(executableName, coreFileName);
        attached = true;
        suspended = true;
    }

    /** From the Debugger interface via JVMDebugger */
    public synchronized boolean detach() {
        if (! attached) {
            return false;
        }

        try {
            if (p_ps_prochandle == 0L) {
                return false;
            }
            detach0();
            clearCache();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            resetNativePointers();
            clearCacheFields();
            suspended = false;
            attached = false;
        }
    }

    public synchronized void suspend() throws DebuggerException {
        requireAttach();
        if (suspended) {
            throw new DebuggerException("Process already suspended");
        }
        suspend0();
        suspended = true;
        enableCache();
        reresolveLoadObjects();
    }

    public synchronized void resume() throws DebuggerException {
        requireAttach();
        if (!suspended) {
            throw new DebuggerException("Process not suspended");
        }
        resume0();
        disableCache();
        suspended = false;
    }

    public synchronized boolean isSuspended() throws DebuggerException {
        requireAttach();
        return suspended;
    }

    /** From the Debugger interface via JVMDebugger */
    public Address parseAddress(String addressString) throws NumberFormatException {
        long addr = utils.scanAddress(addressString);
        if (addr == 0) {
            return null;
        }
        return new ProcAddress(this, addr);
    }

    /** From the Debugger interface via JVMDebugger */
    public String getOS() {
        return PlatformInfo.getOS();
    }

    /** From the Debugger interface via JVMDebugger */
    public String getCPU() {
        return PlatformInfo.getCPU();
    }

    public boolean hasConsole() throws DebuggerException {
        return false;
    }

    public String consoleExecuteCommand(String cmd) throws DebuggerException {
        throw new DebuggerException("Can't execute console commands");
    }

    public String getConsolePrompt() throws DebuggerException {
        return "";
    }

    public CDebugger getCDebugger() throws DebuggerException {
        if (cdbg == null) {
            cdbg = new ProcCDebugger(this);
        }
        return cdbg;
    }

    /** From the SymbolLookup interface via Debugger and JVMDebugger */
    public synchronized Address lookup(String objectName, String symbol) {
        requireAttach();
        long addr = lookupByName0(objectName, symbol);
        if (addr == 0) {
            return null;
        }
        return new ProcAddress(this, addr);
    }

    /** From the SymbolLookup interface via Debugger and JVMDebugger */
    public synchronized OopHandle lookupOop(String objectName, String symbol) {
        Address addr = lookup(objectName, symbol);
        if (addr == null) {
            return null;
        }
        return addr.addOffsetToAsOopHandle(0);
    }

    /** From the ProcDebugger interface */
    public MachineDescription getMachineDescription() {
        return machDesc;
    }

    /** Internal routine supporting lazy setting of MachineDescription,
     * since on SPARC we will need to query the remote process to ask
     * it what its data model is (32- or 64-bit).
     */

    public void setMachineDescription(MachineDescription machDesc) {
        this.machDesc = machDesc;
        setBigEndian(machDesc.isBigEndian());
        utils = new DebuggerUtilities(machDesc.getAddressSize(), machDesc.isBigEndian());
    }

    public synchronized int getRemoteProcessAddressSize()
    throws DebuggerException {
        requireAttach();
        return getRemoteProcessAddressSize0();
    }

    //--------------------------------------------------------------------------------
    // Implementation of ThreadAccess interface
    //

    /** From the ThreadAccess interface via Debugger and JVMDebugger */
    public ThreadProxy getThreadForIdentifierAddress(Address addr) {
        return threadFactory.createThreadWrapper(addr);
    }

    public ThreadProxy getThreadForThreadId(long id) {
        return threadFactory.createThreadWrapper(id);
    }

    //----------------------------------------------------------------------
    // Overridden from DebuggerBase because we need to relax alignment
    // constraints on x86

    public long readJLong(long address)
    throws UnmappedAddressException, UnalignedAddressException {
        checkJavaConfigured();
        // FIXME: allow this to be configurable. Undesirable to add a
        // dependency on the runtime package here, though, since this
        // package should be strictly underneath it.
        if (unalignedAccessesOkay) {
            utils.checkAlignment(address, jintSize);
        } else {
            utils.checkAlignment(address, jlongSize);
        }
        byte[] data = readBytes(address, jlongSize);
        return utils.dataToJLong(data, jlongSize);
    }

    //--------------------------------------------------------------------------------
    // Internal routines (for implementation of ProcAddress).
    // These must not be called until the MachineDescription has been set up.
    //

    /** From the ProcDebugger interface */
    public String addressValueToString(long address) {
        return utils.addressValueToString(address);
    }

    /** Need to override this to relax alignment checks on Solaris/x86. */
    public long readCInteger(long address, long numBytes, boolean isUnsigned)
    throws UnmappedAddressException, UnalignedAddressException {
        checkConfigured();
        if (!unalignedAccessesOkay) {
            utils.checkAlignment(address, numBytes);
        } else {
            // Only slightly relaxed semantics -- this is a hack, but is
            // necessary on Solaris/x86 where it seems the compiler is
            // putting some global 64-bit data on 32-bit boundaries
            if (numBytes == 8) {
                utils.checkAlignment(address, 4);
            } else {
                utils.checkAlignment(address, numBytes);
            }
        }
        byte[] data = readBytes(address, numBytes);
        return utils.dataToCInteger(data, isUnsigned);
    }

    /** From the ProcDebugger interface */
    public ProcAddress readAddress(long address)
    throws UnmappedAddressException, UnalignedAddressException {
        long value = readAddressValue(address);
        return (value == 0 ? null : new ProcAddress(this, value));
    }

    public ProcAddress readCompOopAddress(long address)
    throws UnmappedAddressException, UnalignedAddressException {
        long value = readCompOopAddressValue(address);
        return (value == 0 ? null : new ProcAddress(this, value));
    }

    public ProcAddress readCompKlassAddress(long address)
    throws UnmappedAddressException, UnalignedAddressException {
        long value = readCompKlassAddressValue(address);
        return (value == 0 ? null : new ProcAddress(this, value));
    }

    /** From the ProcDebugger interface */
    public ProcOopHandle readOopHandle(long address)
    throws UnmappedAddressException, UnalignedAddressException, NotInHeapException {
        long   value = readAddressValue(address);
        return (value == 0 ? null : new ProcOopHandle(this, value));
    }

    public ProcOopHandle readCompOopHandle(long address) {
        long value = readCompOopAddressValue(address);
        return (value == 0 ? null : new ProcOopHandle(this, value));
    }

    public void writeBytesToProcess(long address, long numBytes, byte[] data)
    throws UnmappedAddressException, DebuggerException {
        if (isCore) {
            throw new DebuggerException("Attached to a core file!");
        }
        writeBytesToProcess0(address, numBytes, data);
    }

    public synchronized ReadResult readBytesFromProcess(long address, long numBytes)
    throws DebuggerException {
        requireAttach();
        byte[] res = readBytesFromProcess0(address, numBytes);
        if(res != null)
            return new ReadResult(res);
        else
            return new ReadResult(address);
    }

    protected int getPageSize() {
        int pagesize = getPageSize0();
        if (pagesize == -1) {
            // return the hard coded default value.
            if (PlatformInfo.getCPU().equals("sparc") ||
                PlatformInfo.getCPU().equals("amd64") )
               pagesize = 8196;
            else
               pagesize = 4096;
        }
        return pagesize;
    }

    //--------------------------------------------------------------------------------
    // Thread context access. Can not be package private, but should
    // only be accessed by the architecture-specific subpackages.

    /** From the ProcDebugger interface. May have to redefine this later. */
    public synchronized long[] getThreadIntegerRegisterSet(int tid) {
        requireAttach();
        return getThreadIntegerRegisterSet0(tid);
    }

    //--------------------------------------------------------------------------------
    // Address access. Can not be package private, but should only be
    // accessed by the architecture-specific subpackages.

    /** From the ProcDebugger interface */
    public long getAddressValue(Address addr) {
        if (addr == null) return 0;
        return ((ProcAddress) addr).getValue();
    }

    /** From the ProcDebugger interface */
    public Address newAddress(long value) {
        if (value == 0) return null;
        return new ProcAddress(this, value);
    }

    /** From the ProcDebugger interface */
    public synchronized List getThreadList() throws DebuggerException {
        requireAttach();
        List res = null;
        if (isCore && (threadListCache != null)) {
            res = threadListCache;
        } else {
            res = new ArrayList();
            fillThreadList0(res);
            if (isCore) {
                threadListCache = res;
            }
        }
        return res;
    }

    /** From the ProcDebugger interface */
    public synchronized List getLoadObjectList() throws DebuggerException {
        requireAttach();
        if (!suspended) {
            throw new DebuggerException("Process not suspended");
        }

        if (loadObjectCache == null) {
            updateLoadObjectCache();
        }
        return loadObjectCache;
    }

    /** From the ProcDebugger interface */
    public synchronized CFrame topFrameForThread(ThreadProxy thread)
    throws DebuggerException {
        requireAttach();
        CFrame res = null;
        if (isCore && ((res = (CFrame) topFrameCache.get(thread)) != null)) {
            return res;
        } else {
            ThreadContext context = thread.getContext();
            int numRegs = context.getNumRegisters();
            long[] regs = new long[numRegs];
            for (int i = 0; i < numRegs; i++) {
                regs[i] = context.getRegister(i);
            }
            res = fillCFrameList0(regs);
            if (isCore) {
                topFrameCache.put(thread, res);
            }
            return res;
        }
    }

    /** From the ProcDebugger interface */
    public synchronized ClosestSymbol lookup(long address) {
        requireAttach();
        return lookupByAddress0(address);
    }

    /** From the ProcDebugger interface */
    public String demangle(String name) {
        return demangle0(name);
    }

    //------------- Internals only below this point --------------------
    //
    //

    private void updateLoadObjectCache() {
        List res = new ArrayList();
        nameToDsoMap = new HashMap();
        fillLoadObjectList0(res);
        loadObjectCache = sortLoadObjects(res);
    }

    // sort load objects by base address
    private static List sortLoadObjects(List in) {
        // sort the list by base address
        Object[] arr = in.toArray();
        Arrays.sort(arr, loadObjectComparator);
        return Arrays.asList(arr);
    }

    private long lookupByName(String objectName, String symbolName)
    throws DebuggerException {
        // NOTE: this assumes that process is suspended (which is probably
        // necessary assumption given that DSOs can be loaded/unloaded as
        // process runs). Should update documentation.
        if (nameToDsoMap == null) {
            getLoadObjectList();
        }
        SharedObject dso = (SharedObject) nameToDsoMap.get(objectName);
        // The DSO can be null because we use this to search through known
        // DSOs in HotSpotTypeDataBase (for example)
        if (dso != null) {
            ProcAddress addr = (ProcAddress) dso.lookupSymbol(symbolName);
            if (addr != null) {
                return addr.getValue();
            }
        }
        return 0;
    }

    private SharedObject findDSOByName(String fullPathName) {
        if (loadObjectCache == null)
            return null;
        for (Iterator iter = loadObjectCache.iterator(); iter.hasNext(); ) {
            SharedObject dso = (SharedObject) iter.next();
            if (dso.getName().equals(fullPathName)) {
                return dso;
            }
        }
        return null;
    }

    private void reresolveLoadObjects() throws DebuggerException {
        if (loadObjectCache == null) {
            return;
        }
        updateLoadObjectCache();
    }


    private void checkAttached() {
        if (attached) {
            if (isCore) {
                throw new DebuggerException("already attached to a core file!");
            } else {
                throw new DebuggerException("already attached to a process!");
            }
        }
    }

    private void requireAttach() {
        if (! attached) {
            throw new RuntimeException("not attached to a process or core file!");
        }
    }

    private void clearCacheFields() {
        loadObjectCache = null;
        nameToDsoMap    = null;
        threadListCache = null;
        topFrameCache   = null;
    }

    private void resetNativePointers() {
        p_ps_prochandle          = 0L;

        // reset thread_db pointers
        libthread_db_handle    = 0L;
        p_td_thragent_t        = 0L;
        p_td_init              = 0L;
        p_td_ta_new            = 0L;
        p_td_ta_delete         = 0L;
        p_td_ta_thr_iter       = 0L;
        p_td_thr_get_info      = 0L;
        p_td_ta_map_id2thr     = 0L;
        p_td_thr_getgregs      = 0L;

        // part of class sharing workaround
        classes_jsa_fd         = -1;
        p_file_map_header      = 0L;
    }

    // native methods and native helpers

    // attach, detach
    private native void attach0(String pid) throws DebuggerException;
    private native void attach0(String executableFile, String coreFileName) throws DebuggerException;
    private native void detach0() throws DebuggerException;

    // address size, page size
    private native int getRemoteProcessAddressSize0() throws DebuggerException;
    private native int getPageSize0() throws DebuggerException;

    // threads, stacks
    private native long[] getThreadIntegerRegisterSet0(long tid) throws DebuggerException;
    private native void   fillThreadList0(List l) throws DebuggerException;

    // fills stack frame list given reg set of the top frame and top frame
    private native ProcCFrame fillCFrameList0(long[] regs) throws DebuggerException;

    // helper called by fillCFrameList0
    private ProcCFrame createSenderFrame(ProcCFrame f, long pc, long fp) {
        ProcCFrame sender = new ProcCFrame(this, newAddress(pc), newAddress(fp));
        if (f != null) {
            f.setSender(sender);
        }
        return sender;
    }

    // shared objects
    private native void fillLoadObjectList0(List l) throws DebuggerException;

    // helper called by fillLoadObjectList0
    private LoadObject createLoadObject(String fileName, long textsize, long base) {
        File f = new File(fileName);
        Address baseAddr = newAddress(base);
        SharedObject res = findDSOByName(fileName);
        if (res != null) {
            // already in cache. just change the base, if needed
            Address oldBase = res.getBase();
            if (! baseAddr.equals(oldBase)) {
                res.setBase(baseAddr);
            }
        } else {
            // new shared object.
            res = new SharedObject(this, fileName, f.length(), baseAddr);
        }
        nameToDsoMap.put(f.getName(), res);
        return res;
    }

    // symbol-to-pc
    private native long lookupByName0(String objectName, String symbolName) throws DebuggerException;
    private native ClosestSymbol lookupByAddress0(long address) throws DebuggerException;

    // helper called by lookupByAddress0
    private ClosestSymbol createClosestSymbol(String name, long offset) {
        return new ClosestSymbol(name, offset);
    }

    // process read/write
    private native byte[] readBytesFromProcess0(long address, long numBytes) throws DebuggerException;
    private native void writeBytesToProcess0(long address, long numBytes, byte[] data) throws DebuggerException;

    // process control
    private native void suspend0() throws DebuggerException;
    private native void resume0() throws DebuggerException;

    // demangle a C++ name
    private native String demangle0(String name);

    // init JNI ids to fields, methods
    private native static void initIDs() throws DebuggerException;
    private static LoadObjectComparator loadObjectComparator;

    static {
        System.loadLibrary("saproc");
        initIDs();
        loadObjectComparator = new LoadObjectComparator();
    }

    private boolean unalignedAccessesOkay;
    private ProcThreadFactory threadFactory;

    // indices of PC and FP registers in gregset
    private int pcRegIndex;
    private int fpRegIndex;

    // Symbol lookup support
    // This is a map of library names to DSOs
    private Map nameToDsoMap;  // Map<String, SharedObject>

    // C/C++ debugging support
    private List/*<LoadObject>*/ loadObjects;
    private CDebugger cdbg;

    // ProcessControl support
    private boolean suspended;

    // libproc handle
    private long p_ps_prochandle;

    // libthread.so's dlopen handle, thread agent
    // and function pointers
    private long libthread_db_handle;
    private long p_td_thragent_t;
    private long p_td_init;
    private long p_td_ta_new;
    private long p_td_ta_delete;
    private long p_td_ta_thr_iter;
    private long p_td_thr_get_info;
    private long p_td_ta_map_id2thr;
    private long p_td_thr_getgregs;

    // part of class sharing workaround
    private int classes_jsa_fd;
    private long p_file_map_header;

    private boolean attached = false;
    private boolean isCore;

    // for core files, we cache load object list, thread list, top frames etc.
    // for processes we cache load object list and sync. it during suspend.
    private List threadListCache;
    private List loadObjectCache;
    private Map  topFrameCache;      // Map<ThreadProxy, CFrame>
}