/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*
*/
/** <P> An implementation of the JVMDebugger interface which talks to
windbg and symbol table management is done in Java. </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> */
private boolean attached;
private boolean isCore;
// Symbol lookup support
// This is a map of library names to DLLs
// C/C++ debugging support
// thread access
// windbg native interface pointers
private long ptrIDebugClient;
private long ptrIDebugControl;
private long ptrIDebugDataSpaces;
private long ptrIDebugOutputCallbacks;
private long ptrIDebugAdvanced;
private long ptrIDebugSymbols;
private long ptrIDebugSystemObjects;
//--------------------------------------------------------------------------------
// Implementation of Debugger interface
//
/** <P> machDesc may not be null. </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> */
boolean useCache) throws DebuggerException {
// Need to override default checkAlignment because we need to
throw new UnalignedAddressException(
"Trying to read at address: "
+ " with alignment: " + alignment,
address);
}
}
};
threadFactory = new WindbgX86ThreadFactory(this);
threadFactory = new WindbgAMD64ThreadFactory(this);
threadFactory = new WindbgIA64ThreadFactory(this);
}
if (useCache) {
// Cache portion of the remote process's address space.
// Fetching data over the socket connection to dbx is slow.
// Might be faster if we were using a binary protocol to talk to
// dbx, but would have to test. 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 cache of 4096 4K pages, or 16 MB. The page
// size must be adjusted to be the hardware's page size.
// (FIXME: should pick this up from the debugger.)
}
// FIXME: add instantiation of thread factory
}
/** From the Debugger interface via JVMDebugger */
return false;
}
/** From the Debugger interface via JVMDebugger */
return null;
}
/** From the Debugger interface via JVMDebugger */
attachInit();
attached = true;
isCore = false;
}
/** From the Debugger interface via JVMDebugger */
public synchronized void attach(String executableName, String coreFileName) throws DebuggerException {
attachInit();
attached = true;
isCore = true;
}
return loadObjects;
}
/** From the Debugger interface via JVMDebugger */
public synchronized boolean detach() {
if ( ! attached)
return false;
// Close all open DLLs
if (nameToDllMap != null) {
}
nameToDllMap = null;
loadObjects = null;
}
clearCache();
threadList = null;
try {
detach0();
} finally {
attached = false;
}
return true;
}
/** From the Debugger interface via JVMDebugger */
}
/** From the Debugger interface via JVMDebugger */
return PlatformInfo.getOS();
}
/** From the Debugger interface via JVMDebugger */
return PlatformInfo.getCPU();
}
return true;
}
if (! attached) {
throw new DebuggerException("debugger not yet attached to a Dr. Watson dump!");
}
return consoleExecuteCommand0(cmd);
}
return "(windbg)";
}
// FIXME: CDebugger is not yet supported for IA64 because
// of native stack walking issues.
cdbg = new WindbgCDebugger(this);
}
}
return cdbg;
}
/** From the SymbolLookup interface via Debugger and JVMDebugger */
}
/** From the SymbolLookup interface via Debugger and JVMDebugger */
return null;
}
}
return lookupByAddress0(address);
}
/** From the Debugger interface */
return machDesc;
}
//--------------------------------------------------------------------------------
// Implementation of ThreadAccess interface
//
/** From the ThreadAccess interface via Debugger and JVMDebugger */
}
// with windbg we can't make out using handle
throw new DebuggerException("Unimplemented!");
}
return getThreadIdFromSysId0(sysId);
}
//----------------------------------------------------------------------
// Overridden from DebuggerBase because we need to relax alignment
// constraints on x86
// 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.
// utils.checkAlignment(address, jlongSize);
}
//--------------------------------------------------------------------------------
// Internal routines (for implementation of WindbgAddress).
// These must not be called until the MachineDescription has been set up.
//
/** From the WindbgDebugger interface */
}
/** From the WindbgDebugger interface */
}
}
/** From the WindbgDebugger interface */
}
}
/** From the WindbgDebugger interface */
public int getAddressSize() {
return (int) machDesc.getAddressSize();
}
//--------------------------------------------------------------------------------
// Thread context access
//
long[] regs) {
}
}
throws DebuggerException {
}
return threadList;
}
if (f.exists()) {
return file;
} else {
// remove path part, if any.
while (st.hasMoreTokens()) {
if (f.exists()) {
return f.getPath();
}
}
}
return null;
}
if (useNativeLookup) {
return super.closestSymbolToPC(pcAsAddr);
} else {
return sym;
}
}
};
} else {
}
}
}
//--------------------------------------------------------------------------------
// Address access
//
/** From the Debugger interface */
}
/** From the WindbgDebugger interface */
return new WindbgAddress(this, value);
}
//--------------------------------------------------------------------------------
// Internals only below this point
//
private void checkAttached() {
if (attached) {
"already attached to a process!";
throw new DebuggerException(msg);
}
}
private void requireAttach() {
if (!attached) {
throw new RuntimeException("not attached to a process or Dr Watson dump");
}
}
private void attachInit() {
loadObjects = new ArrayList();
nameToDllMap = new HashMap();
threadIntegerRegisterSet = new HashMap();
threadList = new ArrayList();
}
private void resetNativePointers() {
ptrIDebugClient = 0L;
ptrIDebugControl = 0L;
ptrIDebugDataSpaces = 0L;
ptrIDebugOutputCallbacks = 0L;
ptrIDebugAdvanced = 0L;
ptrIDebugSymbols = 0L;
ptrIDebugSystemObjects = 0L;
}
long res = 0L;
if (useNativeLookup) {
if (res != 0L) {
return res;
} // else fallthru...
}
// The DLL can be null because we use this to search through known
// DLLs in HotSpotTypeDataBase (for example)
}
}
return 0L;
}
/** This reads bytes from the remote process. */
throws UnmappedAddressException, DebuggerException {
return new ReadResult(res);
else
return new ReadResult(address);
}
return dll;
}
}
return null;
}
throws UnmappedAddressException, DebuggerException {
// FIXME
throw new DebuggerException("Unimplemented");
}
private static boolean useNativeLookup;
static {
/*
* sawindbg.dll depends on dbgeng.dll which itself depends on
* dbghelp.dll. We have to make sure that the dbgeng.dll and
* dbghelp.dll that we load are compatible with each other. We
* load both of those libraries from the same directory based
* on the theory that co-located libraries are compatible.
*
* On Windows 2000 and earlier, dbgeng.dll and dbghelp.dll were
* not included as part of the standard system directory. On
* systems newer than Windows 2000, dbgeng.dll and dbghelp.dll
* are included in the standard system directory. However, the
* versions included in the standard system directory may not
* be able to handle symbol information for the newer compilers.
*
* We search for and explicitly load the libraries using the
* following directory search order:
*
* - dir named by DEBUGGINGTOOLSFORWINDOWS environment variable
* - various "Debugging Tools For Windows" program directories
*
* If SA is invoked with -Dsun.jvm.hotspot.loadLibrary.DEBUG=1,
* then debug messages about library loading are printed to
* System.err.
*/
boolean loadLibraryDEBUG =
{
// First place to search is co-located with sawindbg.dll in
"sawindbg.dll";
// second place to search is specified by an environment variable:
}
// The third place to search is the install directory for the
// "Debugging Tools For Windows" package; so far there are three
// name variations that we know of:
// The last place to search is the system directory:
}
if (loadLibraryDEBUG) {
"': directory does not exist.");
}
// this search directory doesn't exist so skip it
continue;
}
// both files exist so we have a match
break;
}
// At least one of the files does not exist; no warning if both
// don't exist. If just one doesn't exist then we don't check
// loadLibraryDEBUG because we have a mis-configured system.
dbgengPath + "'.");
dbghelpPath + "'.");
} else if (loadLibraryDEBUG) {
"': dbgeng.dll and dbghelp.dll were not found.");
}
dbgengPath = null;
dbghelpPath = null;
}
// at least one of the files wasn't found anywhere we searched
mesg = "dbgeng.dll and dbghelp.dll cannot be found. ";
} else if (dbgengPath == null) {
mesg = "dbgeng.dll cannot be found (dbghelp.dll was found). ";
} else {
mesg = "dbghelp.dll cannot be found (dbgeng.dll was found). ";
}
throw new UnsatisfiedLinkError(mesg +
"Please search microsoft.com for 'Debugging Tools For Windows', " +
"and either download it to the default location, or download it " +
"to a custom location and set environment variable " +
"'DEBUGGINGTOOLSFORWINDOWS' to the pathname of that location.");
}
// NOTE: The order of loads is important! If we load dbgeng.dll
// first, then the dependency - dbghelp.dll - will be loaded
// from usual DLL search thereby defeating the purpose!
if (loadLibraryDEBUG) {
}
if (loadLibraryDEBUG) {
}
// Now, load sawindbg.dll
if (loadLibraryDEBUG) {
}
// where do I find '.exe', '.dll' files?
}
// where do I find '.pdb', '.dbg' files?
// mostly, debug files would be find where .dll's, .exe's are found.
if (symbolPath == null) {
}
// should we parse DLL symbol table in Java code or use
// Windbg's native lookup facility? By default, we use
// native lookup so that we can take advantage of '.pdb'
// files, if available.
useNativeLookup = true;
useNativeLookup = false;
}
initIDs();
}
// native methods
private static native void initIDs();
private native void detach0();
// helper called lookupByAddress0
}
}