hotspot/agent/src/os/win32/windbg/sawindbg.cpp
author xdono
Thu, 16 Apr 2009 19:10:32 -0700
changeset 2543 825f6628794a
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// this is source code windbg based SA debugger agent to debug
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// Dr. Watson dump files and process snapshots.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include "sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#ifdef _M_IA64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  #include "sun_jvm_hotspot_debugger_ia64_IA64ThreadContext.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  #define NPRGREG sun_jvm_hotspot_debugger_ia64_IA64ThreadContext_NPRGREG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#elif _M_IX86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  #include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#elif _M_AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  #error "SA windbg back-end is not supported for your cpu!"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#include <limits.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#include <windows.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
#ifndef STDMETHODV
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
#define STDMETHODV(method) virtual HRESULT STDMETHODVCALLTYPE method
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
#define DEBUG_NO_IMPLEMENTATION
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
#include <dbgeng.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
#include <dbghelp.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// simple template to manage array delete across early (error) returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
template <class T>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
class AutoArrayPtr {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
      T* m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      AutoArrayPtr(T* ptr) : m_ptr(ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
      ~AutoArrayPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
         delete [] m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      T* asPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
         return m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
class AutoJavaString {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      JNIEnv* m_env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
      jstring m_str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      const char* m_buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      AutoJavaString(JNIEnv* env, jstring str, const char* buf)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        : m_env(env), m_str(str), m_buf(buf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      ~AutoJavaString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
         m_env->ReleaseStringUTFChars(m_str, m_buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      operator const char* () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
         return m_buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
// field and method IDs we want here
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
static jfieldID imagePath_ID                    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
static jfieldID symbolPath_ID                   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
static jfieldID ptrIDebugClient_ID              = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
static jfieldID ptrIDebugControl_ID             = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
static jfieldID ptrIDebugDataSpaces_ID          = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
static jfieldID ptrIDebugOutputCallbacks_ID     = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
static jfieldID ptrIDebugAdvanced_ID            = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
static jfieldID ptrIDebugSymbols_ID             = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
static jfieldID ptrIDebugSystemObjects_ID       = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
static jmethodID addLoadObject_ID               = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
static jmethodID addThread_ID                   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
static jmethodID createClosestSymbol_ID         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
static jmethodID setThreadIntegerRegisterSet_ID = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
#define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
#define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                          throwNewDebuggerException(env, str); return value; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
 return;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  env->ThrowNew(env->FindClass("sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 * Method:    initIDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
 * Signature: ()V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_initIDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  (JNIEnv *env, jclass clazz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  imagePath_ID = env->GetStaticFieldID(clazz, "imagePath", "Ljava/lang/String;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  symbolPath_ID = env->GetStaticFieldID(clazz, "symbolPath", "Ljava/lang/String;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  ptrIDebugClient_ID = env->GetFieldID(clazz, "ptrIDebugClient", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  ptrIDebugControl_ID = env->GetFieldID(clazz, "ptrIDebugControl", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  ptrIDebugDataSpaces_ID = env->GetFieldID(clazz, "ptrIDebugDataSpaces", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  ptrIDebugOutputCallbacks_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
                                            "ptrIDebugOutputCallbacks", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  ptrIDebugAdvanced_ID = env->GetFieldID(clazz, "ptrIDebugAdvanced", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  ptrIDebugSymbols_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                                         "ptrIDebugSymbols", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  ptrIDebugSystemObjects_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
                                         "ptrIDebugSystemObjects", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  addLoadObject_ID = env->GetMethodID(clazz, "addLoadObject",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
                                         "(Ljava/lang/String;JJ)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  addThread_ID = env->GetMethodID(clazz, "addThread", "(J)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  createClosestSymbol_ID = env->GetMethodID(clazz, "createClosestSymbol",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  setThreadIntegerRegisterSet_ID = env->GetMethodID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
                                         "setThreadIntegerRegisterSet", "(J[J)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
// class for IDebugOutputCallbacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
class SAOutputCallbacks : public IDebugOutputCallbacks {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
   LONG  m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
   char* m_msgBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
      SAOutputCallbacks() : m_refCount(0), m_msgBuffer(0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      ~SAOutputCallbacks() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
         clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      const char* getBuffer() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
         return m_msgBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
      void clearBuffer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
         if (m_msgBuffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
            free(m_msgBuffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
            m_msgBuffer = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      STDMETHOD_(ULONG, AddRef)(THIS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      STDMETHOD_(ULONG, Release)(THIS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      STDMETHOD(QueryInterface)(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
                                IN REFIID interfaceId,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
                                OUT PVOID* ppInterface);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
      STDMETHOD(Output)(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
                        IN ULONG mask,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
                        IN PCSTR msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
STDMETHODIMP_(ULONG) SAOutputCallbacks::AddRef(THIS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
   InterlockedIncrement(&m_refCount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
   return m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
STDMETHODIMP_(ULONG) SAOutputCallbacks::Release(THIS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
   LONG retVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
   InterlockedDecrement(&m_refCount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
   retVal = m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
   if (retVal == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      delete this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
   return retVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
STDMETHODIMP SAOutputCallbacks::QueryInterface(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
                                          IN REFIID interfaceId,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
                                          OUT PVOID* ppInterface) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
   *ppInterface = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
   HRESULT res = E_NOINTERFACE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
   if (TRUE == IsEqualIID(interfaceId, __uuidof(IUnknown)) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
       TRUE == IsEqualIID(interfaceId, __uuidof(IDebugOutputCallbacks))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      *ppInterface = (IDebugOutputCallbacks*) this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      AddRef();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
      res = S_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
   return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
STDMETHODIMP SAOutputCallbacks::Output(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
                                       IN ULONG mask,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
                                       IN PCSTR msg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
   int len = (int) (strlen(msg) + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
   if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      m_msgBuffer = (char*) malloc(len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
         fprintf(stderr, "out of memory debugger output!\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
         return S_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      strcpy(m_msgBuffer, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      m_msgBuffer = (char*) realloc(m_msgBuffer, len + strlen(m_msgBuffer));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
         fprintf(stderr, "out of memory debugger output!\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
         return S_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      strcat(m_msgBuffer, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
   return S_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
static bool getWindbgInterfaces(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  // get windbg interfaces ..
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  IDebugClient* ptrIDebugClient = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  if (DebugCreate(__uuidof(IDebugClient), (PVOID*) &ptrIDebugClient) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to create IDebugClient object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  env->SetLongField(obj, ptrIDebugClient_ID, (jlong) ptrIDebugClient);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  IDebugControl* ptrIDebugControl = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugControl), (PVOID*) &ptrIDebugControl)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugControl", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  env->SetLongField(obj, ptrIDebugControl_ID, (jlong) ptrIDebugControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  IDebugDataSpaces* ptrIDebugDataSpaces = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugDataSpaces), (PVOID*) &ptrIDebugDataSpaces)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugDataSpaces object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  env->SetLongField(obj, ptrIDebugDataSpaces_ID, (jlong) ptrIDebugDataSpaces);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  SAOutputCallbacks* ptrIDebugOutputCallbacks = new SAOutputCallbacks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  ptrIDebugOutputCallbacks->AddRef();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  env->SetLongField(obj, ptrIDebugOutputCallbacks_ID, (jlong) ptrIDebugOutputCallbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  IDebugAdvanced* ptrIDebugAdvanced = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugAdvanced), (PVOID*) &ptrIDebugAdvanced)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugAdvanced object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  env->SetLongField(obj, ptrIDebugAdvanced_ID, (jlong) ptrIDebugAdvanced);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  IDebugSymbols* ptrIDebugSymbols = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugSymbols), (PVOID*) &ptrIDebugSymbols)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugSymbols object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  env->SetLongField(obj, ptrIDebugSymbols_ID, (jlong) ptrIDebugSymbols);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  IDebugSystemObjects* ptrIDebugSystemObjects = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugSystemObjects), (PVOID*) &ptrIDebugSystemObjects)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugSystemObjects object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  env->SetLongField(obj, ptrIDebugSystemObjects_ID, (jlong) ptrIDebugSystemObjects);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
static bool setImageAndSymbolPath(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  jclass clazz = env->GetObjectClass(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  jstring path;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  const char* buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  path = (jstring) env->GetStaticObjectField(clazz, imagePath_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  buf = env->GetStringUTFChars(path, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  AutoJavaString imagePath(env, path, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  path = (jstring) env->GetStaticObjectField(clazz, symbolPath_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  buf = env->GetStringUTFChars(path, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  AutoJavaString symbolPath(env, path, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  ptrIDebugSymbols->SetImagePath(imagePath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  ptrIDebugSymbols->SetSymbolPath(symbolPath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
static bool openDumpFile(JNIEnv* env, jobject obj, jstring coreFileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  // open the dump file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  const char* buf = env->GetStringUTFChars(coreFileName, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  AutoJavaString coreFile(env, coreFileName, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  if (setImageAndSymbolPath(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  if (ptrIDebugClient->OpenDumpFile(coreFile) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: OpenDumpFile failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  if (ptrIDebugControl->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: WaitForEvent failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
static bool attachToProcess(JNIEnv* env, jobject obj, jint pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  if (setImageAndSymbolPath(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  /***********************************************************************************
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
     We are attaching to a process in 'read-only' mode. i.e., we do not want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
     put breakpoints, suspend/resume threads etc. For read-only JDI and HSDB kind of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
     usage this should suffice. We are not intending to use this for full-fledged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
     ProcessControl implementation to be used with BugSpotAgent.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
     Please refer to DEBUG_ATTACH_NONINVASIVE mode source comments from dbgeng.h.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
     In this mode, debug engine does not call DebugActiveProrcess. i.e., we are not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
     actually debugging at all. We can safely 'detach' from the process anytime
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
     we want and debuggee process is left as is on all Windows variants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
     This also makes JDI-on-SA installation/usage simpler because with this we would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
     not need a tool like ServiceInstaller from http://www.kcmultimedia.com/smaster.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  ***********************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  if (ptrIDebugClient->AttachProcess(0, pid, DEBUG_ATTACH_NONINVASIVE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: AttachProcess failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  if (ptrIDebugControl->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: WaitForEvent failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
static bool addLoadObjects(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  ULONG loaded = 0, unloaded = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  if (ptrIDebugSymbols->GetNumberModules(&loaded, &unloaded) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetNumberModules failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  AutoArrayPtr<DEBUG_MODULE_PARAMETERS> params(new DEBUG_MODULE_PARAMETERS[loaded]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  if (params.asPtr() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
      THROW_NEW_DEBUGGER_EXCEPTION_("out of memory to allocate debug module params!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  if (ptrIDebugSymbols->GetModuleParameters(loaded, 0, NULL, params.asPtr()) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetModuleParameters failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  for (int u = 0; u < (int)loaded; u++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
     TCHAR imageName[MAX_PATH];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
     if (ptrIDebugSymbols->GetModuleNames(DEBUG_ANY_ID, params.asPtr()[u].Base,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
                                      imageName, MAX_PATH, NULL, NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
                                      0, NULL, NULL, 0, NULL) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetModuleNames failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
     jstring strName = env->NewStringUTF(imageName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
     env->CallVoidMethod(obj, addLoadObject_ID, strName, (jlong) params.asPtr()[u].Size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
                               (jlong) params.asPtr()[u].Base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
static bool addThreads(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
                                                      ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  ULONG numThreads = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  if (ptrIDebugSystemObjects->GetNumberThreads(&numThreads) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetNumberThreads failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  AutoArrayPtr<ULONG> ptrSysThreadIds = new ULONG[numThreads];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  if (ptrSysThreadIds.asPtr() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
     THROW_NEW_DEBUGGER_EXCEPTION_("out of memory to allocate thread ids!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  AutoArrayPtr<ULONG> ptrThreadIds = new ULONG[numThreads];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  if (ptrThreadIds.asPtr() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
     THROW_NEW_DEBUGGER_EXCEPTION_("out of memory to allocate thread ids!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  if (ptrIDebugSystemObjects->GetThreadIdsByIndex(0, numThreads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
                                      ptrThreadIds.asPtr(), ptrSysThreadIds.asPtr()) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetThreadIdsByIndex failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  IDebugAdvanced* ptrIDebugAdvanced = (IDebugAdvanced*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
                                                      ptrIDebugAdvanced_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  // for each thread, get register context and save it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  for (ULONG t = 0; t < numThreads; t++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
     if (ptrIDebugSystemObjects->SetCurrentThreadId(ptrThreadIds.asPtr()[t]) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: SetCurrentThread failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
     jlongArray regs = env->NewLongArray(NPRGREG);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
     jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
     jlong* ptrRegs = env->GetLongArrayElements(regs, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
     // copy register values from the CONTEXT struct
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
     CONTEXT context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
     memset(&context, 0, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
#undef REG_INDEX
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
#ifdef _M_IA64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
     #define REG_INDEX(x) sun_jvm_hotspot_debugger_ia64_IA64ThreadContext_##x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
     context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
     ptrIDebugAdvanced->GetThreadContext(&context, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
     ptrRegs[REG_INDEX(GR0)]  = 0; // always 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
     ptrRegs[REG_INDEX(GR1)]  = context.IntGp;  // r1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
     ptrRegs[REG_INDEX(GR2)]  = context.IntT0;  // r2-r3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
     ptrRegs[REG_INDEX(GR3)]  = context.IntT1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
     ptrRegs[REG_INDEX(GR4)]  = context.IntS0;  // r4-r7
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
     ptrRegs[REG_INDEX(GR5)]  = context.IntS1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
     ptrRegs[REG_INDEX(GR6)]  = context.IntS2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
     ptrRegs[REG_INDEX(GR7)]  = context.IntS3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
     ptrRegs[REG_INDEX(GR8)]  = context.IntV0;  // r8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
     ptrRegs[REG_INDEX(GR9)]  = context.IntT2;  // r9-r11
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
     ptrRegs[REG_INDEX(GR10)] = context.IntT3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
     ptrRegs[REG_INDEX(GR11)] = context.IntT4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
     ptrRegs[REG_INDEX(GR12)] = context.IntSp;  // r12 stack pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
     ptrRegs[REG_INDEX(GR13)] = context.IntTeb; // r13 teb
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
     ptrRegs[REG_INDEX(GR14)] = context.IntT5;  // r14-r31
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
     ptrRegs[REG_INDEX(GR15)] = context.IntT6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
     ptrRegs[REG_INDEX(GR16)] = context.IntT7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
     ptrRegs[REG_INDEX(GR17)] = context.IntT8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
     ptrRegs[REG_INDEX(GR18)] = context.IntT9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
     ptrRegs[REG_INDEX(GR19)] = context.IntT10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
     ptrRegs[REG_INDEX(GR20)] = context.IntT11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
     ptrRegs[REG_INDEX(GR21)] = context.IntT12;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
     ptrRegs[REG_INDEX(GR22)] = context.IntT13;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
     ptrRegs[REG_INDEX(GR23)] = context.IntT14;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
     ptrRegs[REG_INDEX(GR24)] = context.IntT15;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
     ptrRegs[REG_INDEX(GR25)] = context.IntT16;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
     ptrRegs[REG_INDEX(GR26)] = context.IntT17;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
     ptrRegs[REG_INDEX(GR27)] = context.IntT18;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
     ptrRegs[REG_INDEX(GR28)] = context.IntT19;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
     ptrRegs[REG_INDEX(GR29)] = context.IntT20;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
     ptrRegs[REG_INDEX(GR30)] = context.IntT21;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
     ptrRegs[REG_INDEX(GR31)] = context.IntT22;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
     ptrRegs[REG_INDEX(INT_NATS)] = context.IntNats;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
     ptrRegs[REG_INDEX(PREDS)]    = context.Preds;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
     ptrRegs[REG_INDEX(BR_RP)] = context.BrRp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
     ptrRegs[REG_INDEX(BR1)]   = context.BrS0;  // b1-b5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
     ptrRegs[REG_INDEX(BR2)]   = context.BrS1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
     ptrRegs[REG_INDEX(BR3)]   = context.BrS2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
     ptrRegs[REG_INDEX(BR4)]   = context.BrS3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
     ptrRegs[REG_INDEX(BR5)]   = context.BrS4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
     ptrRegs[REG_INDEX(BR6)]   = context.BrT0;  // b6-b7
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
     ptrRegs[REG_INDEX(BR7)]   = context.BrT1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
     ptrRegs[REG_INDEX(AP_UNAT)] = context.ApUNAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
     ptrRegs[REG_INDEX(AP_LC)]   = context.ApLC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
     ptrRegs[REG_INDEX(AP_EC)]   = context.ApEC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
     ptrRegs[REG_INDEX(AP_CCV)]  = context.ApCCV;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
     ptrRegs[REG_INDEX(AP_DCR)]  = context.ApDCR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
     ptrRegs[REG_INDEX(RS_PFS)]      = context.RsPFS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
     ptrRegs[REG_INDEX(RS_BSP)]      = context.RsBSP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
     ptrRegs[REG_INDEX(RS_BSPSTORE)] = context.RsBSPSTORE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
     ptrRegs[REG_INDEX(RS_RSC)]      = context.RsRSC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
     ptrRegs[REG_INDEX(RS_RNAT)]     = context.RsRNAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
     ptrRegs[REG_INDEX(ST_IPSR)] = context.StIPSR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
     ptrRegs[REG_INDEX(ST_IIP)]  = context.StIIP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
     ptrRegs[REG_INDEX(ST_IFS)]  = context.StIFS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
     ptrRegs[REG_INDEX(DB_I0)] = context.DbI0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
     ptrRegs[REG_INDEX(DB_I1)] = context.DbI1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
     ptrRegs[REG_INDEX(DB_I2)] = context.DbI2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
     ptrRegs[REG_INDEX(DB_I3)] = context.DbI3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
     ptrRegs[REG_INDEX(DB_I4)] = context.DbI4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
     ptrRegs[REG_INDEX(DB_I5)] = context.DbI5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
     ptrRegs[REG_INDEX(DB_I6)] = context.DbI6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
     ptrRegs[REG_INDEX(DB_I7)] = context.DbI7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
     ptrRegs[REG_INDEX(DB_D0)] = context.DbD0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
     ptrRegs[REG_INDEX(DB_D1)] = context.DbD1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
     ptrRegs[REG_INDEX(DB_D2)] = context.DbD2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
     ptrRegs[REG_INDEX(DB_D3)] = context.DbD3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
     ptrRegs[REG_INDEX(DB_D4)] = context.DbD4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
     ptrRegs[REG_INDEX(DB_D5)] = context.DbD5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
     ptrRegs[REG_INDEX(DB_D6)] = context.DbD6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
     ptrRegs[REG_INDEX(DB_D7)] = context.DbD7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
#elif _M_IX86
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
     #define REG_INDEX(x) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
     context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
     ptrIDebugAdvanced->GetThreadContext(&context, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
     ptrRegs[REG_INDEX(GS)]  = context.SegGs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
     ptrRegs[REG_INDEX(FS)]  = context.SegFs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
     ptrRegs[REG_INDEX(ES)]  = context.SegEs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
     ptrRegs[REG_INDEX(DS)]  = context.SegDs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
     ptrRegs[REG_INDEX(EDI)] = context.Edi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
     ptrRegs[REG_INDEX(ESI)] = context.Esi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
     ptrRegs[REG_INDEX(EBX)] = context.Ebx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
     ptrRegs[REG_INDEX(EDX)] = context.Edx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
     ptrRegs[REG_INDEX(ECX)] = context.Ecx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
     ptrRegs[REG_INDEX(EAX)] = context.Eax;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
     ptrRegs[REG_INDEX(FP)] = context.Ebp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
     ptrRegs[REG_INDEX(PC)] = context.Eip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
     ptrRegs[REG_INDEX(CS)]  = context.SegCs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
     ptrRegs[REG_INDEX(EFL)] = context.EFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
     ptrRegs[REG_INDEX(SP)] = context.Esp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
     ptrRegs[REG_INDEX(SS)]  = context.SegSs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
     ptrRegs[REG_INDEX(DR0)] = context.Dr0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
     ptrRegs[REG_INDEX(DR1)] = context.Dr1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
     ptrRegs[REG_INDEX(DR2)] = context.Dr2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
     ptrRegs[REG_INDEX(DR3)] = context.Dr3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
     ptrRegs[REG_INDEX(DR6)] = context.Dr6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
     ptrRegs[REG_INDEX(DR7)] = context.Dr7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
#elif _M_AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
     #define REG_INDEX(x) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
     context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
     ptrIDebugAdvanced->GetThreadContext(&context, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
     // Segment Registers and processor flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
     ptrRegs[REG_INDEX(CS)]  = context.SegCs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
     ptrRegs[REG_INDEX(DS)]  = context.SegDs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
     ptrRegs[REG_INDEX(ES)]  = context.SegEs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
     ptrRegs[REG_INDEX(FS)]  = context.SegFs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
     ptrRegs[REG_INDEX(GS)]  = context.SegGs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
     ptrRegs[REG_INDEX(SS)]  = context.SegSs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
     ptrRegs[REG_INDEX(RFL)] = context.EFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
     // Integer registers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
     ptrRegs[REG_INDEX(RDI)] = context.Rdi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
     ptrRegs[REG_INDEX(RSI)] = context.Rsi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
     ptrRegs[REG_INDEX(RAX)] = context.Rax;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
     ptrRegs[REG_INDEX(RCX)] = context.Rcx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
     ptrRegs[REG_INDEX(RDX)] = context.Rdx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
     ptrRegs[REG_INDEX(RBX)] = context.Rbx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
     ptrRegs[REG_INDEX(RBP)] = context.Rbp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
     ptrRegs[REG_INDEX(RSP)] = context.Rsp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
     ptrRegs[REG_INDEX(R8)]  = context.R8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
     ptrRegs[REG_INDEX(R9)]  = context.R9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
     ptrRegs[REG_INDEX(R10)] = context.R10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
     ptrRegs[REG_INDEX(R11)] = context.R11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
     ptrRegs[REG_INDEX(R12)] = context.R12;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
     ptrRegs[REG_INDEX(R13)] = context.R13;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
     ptrRegs[REG_INDEX(R14)] = context.R14;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
     ptrRegs[REG_INDEX(R15)] = context.R15;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
     // Program counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
     ptrRegs[REG_INDEX(RIP)] = context.Rip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
     env->ReleaseLongArrayElements(regs, ptrRegs, JNI_COMMIT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
     env->CallVoidMethod(obj, setThreadIntegerRegisterSet_ID,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
                        (jlong) ptrThreadIds.asPtr()[t], regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
     ULONG sysId;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
     if (ptrIDebugSystemObjects->GetCurrentThreadSystemId(&sysId) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetCurrentThreadSystemId failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
     env->CallVoidMethod(obj, addThread_ID, (jlong) sysId);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
 * Method:    attach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
  (JNIEnv *env, jobject obj, jstring execName, jstring coreFileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
  if (getWindbgInterfaces(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  if (openDumpFile(env, obj, coreFileName) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
  if (addLoadObjects(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
  if (addThreads(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
 * Method:    attach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
 * Signature: (I)V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_attach0__I
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
  (JNIEnv *env, jobject obj, jint pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  if (getWindbgInterfaces(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  if (attachToProcess(env, obj, pid) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  if (addLoadObjects(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
  if (addThreads(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
static bool releaseWindbgInterfaces(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  IDebugDataSpaces* ptrIDebugDataSpaces = (IDebugDataSpaces*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
                                                      ptrIDebugDataSpaces_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  if (ptrIDebugDataSpaces != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
     ptrIDebugDataSpaces->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  IDebugOutputCallbacks* ptrIDebugOutputCallbacks = (IDebugOutputCallbacks*)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
                          env->GetLongField(obj, ptrIDebugOutputCallbacks_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  if (ptrIDebugOutputCallbacks != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
     ptrIDebugOutputCallbacks->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  IDebugAdvanced* ptrIDebugAdvanced = (IDebugAdvanced*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
                                                      ptrIDebugAdvanced_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  if (ptrIDebugAdvanced != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
     ptrIDebugAdvanced->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
  if (ptrIDebugSymbols != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
     ptrIDebugSymbols->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
                                                      ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
  if (ptrIDebugSystemObjects != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
     ptrIDebugSystemObjects->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
  if (ptrIDebugControl != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
     ptrIDebugControl->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  if (ptrIDebugClient != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
     ptrIDebugClient->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
 * Method:    detach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
 * Signature: ()V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_detach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  (JNIEnv *env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
  ptrIDebugClient->DetachProcesses();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
  releaseWindbgInterfaces(env, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
 * Method:    readBytesFromProcess0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
 * Signature: (JJ)[B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_readBytesFromProcess0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
  (JNIEnv *env, jobject obj, jlong address, jlong numBytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  jbyteArray byteArray = env->NewByteArray((long) numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
  jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
  jbyte* bytePtr = env->GetByteArrayElements(byteArray, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  IDebugDataSpaces* ptrIDebugDataSpaces = (IDebugDataSpaces*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
                                                       ptrIDebugDataSpaces_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  ULONG bytesRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
  if (ptrIDebugDataSpaces->ReadVirtual((ULONG64) address, (PVOID) bytePtr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
                                  (ULONG)numBytes, &bytesRead) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: ReadVirtual failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  if (bytesRead != numBytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
     return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
  env->ReleaseByteArrayElements(byteArray, bytePtr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  return byteArray;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
 * Method:    getThreadIdFromSysId0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
 * Signature: (J)J
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_getThreadIdFromSysId0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
  (JNIEnv *env, jobject obj, jlong sysId) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
                                                    ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
  ULONG id = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  if (ptrIDebugSystemObjects->GetThreadIdBySystemId((ULONG)sysId, &id) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetThreadIdBySystemId failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  return (jlong) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
// manage COM 'auto' pointers (to avoid multiple Release
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
// calls at every early (exception) returns). Similar to AutoArrayPtr.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
template <class T>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
class AutoCOMPtr {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
      T* m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
      AutoCOMPtr(T* ptr) : m_ptr(ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
      ~AutoCOMPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
         if (m_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
            m_ptr->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
      T* operator->() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
         return m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
 * Method:    consoleExecuteCommand0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_consoleExecuteCommand0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  (JNIEnv *env, jobject obj, jstring cmd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
  const char* buf = env->GetStringUTFChars(cmd, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  AutoJavaString command(env, cmd, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj, ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
  IDebugClient*  tmpClientPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  if (ptrIDebugClient->CreateClient(&tmpClientPtr) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: CreateClient failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
  AutoCOMPtr<IDebugClient> tmpClient(tmpClientPtr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  IDebugControl* tmpControlPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
  if (tmpClient->QueryInterface(__uuidof(IDebugControl), (PVOID*) &tmpControlPtr) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: QueryInterface (IDebugControl) failed", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
  AutoCOMPtr<IDebugControl> tmpControl(tmpControlPtr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
  SAOutputCallbacks* saOutputCallbacks = (SAOutputCallbacks*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
                                                                   ptrIDebugOutputCallbacks_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
  saOutputCallbacks->clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
  if (tmpClient->SetOutputCallbacks(saOutputCallbacks) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: SetOutputCallbacks failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
  tmpControl->Execute(DEBUG_OUTPUT_VERBOSE, command, DEBUG_EXECUTE_DEFAULT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
  const char* output = saOutputCallbacks->getBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
  if (output == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
     output = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
  jstring res = env->NewStringUTF(output);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
  saOutputCallbacks->clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
 * Method:    lookupByName0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_lookupByName0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
(JNIEnv *env, jobject obj, jstring objName, jstring sym) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
  const char* buf = env->GetStringUTFChars(sym, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  AutoJavaString name(env, sym, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
  ULONG64 offset = 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
  if (strstr(name, "::") != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
    ptrIDebugSymbols->AddSymbolOptions(SYMOPT_UNDNAME);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
    ptrIDebugSymbols->RemoveSymbolOptions(SYMOPT_UNDNAME);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
  if (ptrIDebugSymbols->GetOffsetByName(name, &offset) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
    return (jlong) 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
  return (jlong) offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
#define SYMBOL_BUFSIZE 512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
 * Method:    lookupByAddress0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_lookupByAddress0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
(JNIEnv *env, jobject obj, jlong address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  ULONG64 disp = 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
  char buf[SYMBOL_BUFSIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
  memset(buf, 0, sizeof(buf));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
  if (ptrIDebugSymbols->GetNameByOffset(address, buf, sizeof(buf),0,&disp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
      != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
  jstring sym = env->NewStringUTF(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
  jobject res = env->CallObjectMethod(obj, createClosestSymbol_ID, sym, disp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
}