src/jdk.hotspot.agent/windows/native/libsaproc/sawindbg.cpp
author cushon
Wed, 09 Oct 2019 10:02:54 -0700
changeset 58521 55d2e10ea134
parent 53042 56fbb14251ca
permissions -rw-r--r--
8231990: Remove unnecessary else-if branch in Types.union Reviewed-by: mcimadamore Contributed-by: Brad Corso <bcorso@google.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
49274
9f3ce373370a 8199682: Clean up building the saproc library
ihse
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
28162
cf13d6e8e46f 8028773: warnings from b116 for hotspot.agent.src.share.native: JNI exception pending
dsamersoff
parents: 26924
diff changeset
    25
// Disable CRT security warning against strcpy/strcat
cf13d6e8e46f 8028773: warnings from b116 for hotspot.agent.src.share.native: JNI exception pending
dsamersoff
parents: 26924
diff changeset
    26
#pragma warning(disable: 4996)
cf13d6e8e46f 8028773: warnings from b116 for hotspot.agent.src.share.native: JNI exception pending
dsamersoff
parents: 26924
diff changeset
    27
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// this is source code windbg based SA debugger agent to debug
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Dr. Watson dump files and process snapshots.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include "sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
15475
73896d91270c 6518907: cleanup IA64 specific code in Hotspot
morris
parents: 5547
diff changeset
    33
#ifdef _M_IX86
1
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
#define DEBUG_NO_IMPLEMENTATION
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
#include <dbgeng.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
#include <dbghelp.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// simple template to manage array delete across early (error) returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
template <class T>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
class AutoArrayPtr {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
      T* m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
      AutoArrayPtr(T* ptr) : m_ptr(ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
      ~AutoArrayPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
         delete [] m_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
      T* asPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
         return 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
class AutoJavaString {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      JNIEnv* m_env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      jstring m_str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      const char* m_buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
      AutoJavaString(JNIEnv* env, jstring str, const char* buf)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        : m_env(env), m_str(str), m_buf(buf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      ~AutoJavaString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
         m_env->ReleaseStringUTFChars(m_str, m_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
      operator const char* () {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
         return 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// field and method IDs we want here
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
static jfieldID imagePath_ID                    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
static jfieldID symbolPath_ID                   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
static jfieldID ptrIDebugClient_ID              = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
static jfieldID ptrIDebugControl_ID             = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
static jfieldID ptrIDebugDataSpaces_ID          = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
static jfieldID ptrIDebugOutputCallbacks_ID     = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
static jfieldID ptrIDebugAdvanced_ID            = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
static jfieldID ptrIDebugSymbols_ID             = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
static jfieldID ptrIDebugSystemObjects_ID       = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
static jmethodID addLoadObject_ID               = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
static jmethodID addThread_ID                   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
static jmethodID createClosestSymbol_ID         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
static jmethodID setThreadIntegerRegisterSet_ID = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
#define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
#define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
                          throwNewDebuggerException(env, str); return value; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
 return;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) {
26560
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   114
  jclass clazz = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException");
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   115
  CHECK_EXCEPTION;
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   116
  env->ThrowNew(clazz, errMsg);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
 * Method:    initIDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
 * Signature: ()V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_initIDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  (JNIEnv *env, jclass clazz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  imagePath_ID = env->GetStaticFieldID(clazz, "imagePath", "Ljava/lang/String;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  symbolPath_ID = env->GetStaticFieldID(clazz, "symbolPath", "Ljava/lang/String;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  ptrIDebugClient_ID = env->GetFieldID(clazz, "ptrIDebugClient", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  ptrIDebugControl_ID = env->GetFieldID(clazz, "ptrIDebugControl", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  ptrIDebugDataSpaces_ID = env->GetFieldID(clazz, "ptrIDebugDataSpaces", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  ptrIDebugOutputCallbacks_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                                            "ptrIDebugOutputCallbacks", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  ptrIDebugAdvanced_ID = env->GetFieldID(clazz, "ptrIDebugAdvanced", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  ptrIDebugSymbols_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                                         "ptrIDebugSymbols", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  ptrIDebugSystemObjects_ID = env->GetFieldID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
                                         "ptrIDebugSystemObjects", "J");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  addLoadObject_ID = env->GetMethodID(clazz, "addLoadObject",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
                                         "(Ljava/lang/String;JJ)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  addThread_ID = env->GetMethodID(clazz, "addThread", "(J)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  createClosestSymbol_ID = env->GetMethodID(clazz, "createClosestSymbol",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  setThreadIntegerRegisterSet_ID = env->GetMethodID(clazz,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
                                         "setThreadIntegerRegisterSet", "(J[J)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
// class for IDebugOutputCallbacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
class SAOutputCallbacks : public IDebugOutputCallbacks {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
   LONG  m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
   char* m_msgBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
      SAOutputCallbacks() : m_refCount(0), m_msgBuffer(0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      ~SAOutputCallbacks() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
         clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      const char* getBuffer() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
         return m_msgBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      void clearBuffer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
         if (m_msgBuffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
            free(m_msgBuffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
            m_msgBuffer = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      STDMETHOD_(ULONG, AddRef)(THIS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      STDMETHOD_(ULONG, Release)(THIS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      STDMETHOD(QueryInterface)(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
                                IN REFIID interfaceId,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
                                OUT PVOID* ppInterface);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      STDMETHOD(Output)(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
                        IN ULONG mask,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
                        IN PCSTR msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
STDMETHODIMP_(ULONG) SAOutputCallbacks::AddRef(THIS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
   InterlockedIncrement(&m_refCount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
   return m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
STDMETHODIMP_(ULONG) SAOutputCallbacks::Release(THIS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
   LONG retVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
   InterlockedDecrement(&m_refCount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
   retVal = m_refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
   if (retVal == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      delete this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
   return retVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
STDMETHODIMP SAOutputCallbacks::QueryInterface(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
                                          IN REFIID interfaceId,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
                                          OUT PVOID* ppInterface) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
   *ppInterface = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
   HRESULT res = E_NOINTERFACE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
   if (TRUE == IsEqualIID(interfaceId, __uuidof(IUnknown)) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
       TRUE == IsEqualIID(interfaceId, __uuidof(IDebugOutputCallbacks))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      *ppInterface = (IDebugOutputCallbacks*) this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      AddRef();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      res = S_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
   return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
STDMETHODIMP SAOutputCallbacks::Output(THIS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
                                       IN ULONG mask,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
                                       IN PCSTR msg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
   int len = (int) (strlen(msg) + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
   if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      m_msgBuffer = (char*) malloc(len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
         fprintf(stderr, "out of memory debugger output!\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
         return S_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
      strcpy(m_msgBuffer, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      m_msgBuffer = (char*) realloc(m_msgBuffer, len + strlen(m_msgBuffer));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      if (m_msgBuffer == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
         fprintf(stderr, "out of memory debugger output!\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
         return S_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      strcat(m_msgBuffer, msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
   return S_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
static bool getWindbgInterfaces(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  // get windbg interfaces ..
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  IDebugClient* ptrIDebugClient = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  if (DebugCreate(__uuidof(IDebugClient), (PVOID*) &ptrIDebugClient) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to create IDebugClient object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  env->SetLongField(obj, ptrIDebugClient_ID, (jlong) ptrIDebugClient);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  IDebugControl* ptrIDebugControl = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugControl), (PVOID*) &ptrIDebugControl)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugControl", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  env->SetLongField(obj, ptrIDebugControl_ID, (jlong) ptrIDebugControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  IDebugDataSpaces* ptrIDebugDataSpaces = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugDataSpaces), (PVOID*) &ptrIDebugDataSpaces)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugDataSpaces object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  env->SetLongField(obj, ptrIDebugDataSpaces_ID, (jlong) ptrIDebugDataSpaces);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  SAOutputCallbacks* ptrIDebugOutputCallbacks = new SAOutputCallbacks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  ptrIDebugOutputCallbacks->AddRef();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  env->SetLongField(obj, ptrIDebugOutputCallbacks_ID, (jlong) ptrIDebugOutputCallbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  IDebugAdvanced* ptrIDebugAdvanced = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugAdvanced), (PVOID*) &ptrIDebugAdvanced)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugAdvanced object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  env->SetLongField(obj, ptrIDebugAdvanced_ID, (jlong) ptrIDebugAdvanced);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  IDebugSymbols* ptrIDebugSymbols = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugSymbols), (PVOID*) &ptrIDebugSymbols)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugSymbols object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  env->SetLongField(obj, ptrIDebugSymbols_ID, (jlong) ptrIDebugSymbols);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  IDebugSystemObjects* ptrIDebugSystemObjects = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  if (ptrIDebugClient->QueryInterface(__uuidof(IDebugSystemObjects), (PVOID*) &ptrIDebugSystemObjects)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
     != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get IDebugSystemObjects object!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  env->SetLongField(obj, ptrIDebugSystemObjects_ID, (jlong) ptrIDebugSystemObjects);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
static bool setImageAndSymbolPath(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  jclass clazz = env->GetObjectClass(obj);
26560
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   314
  CHECK_EXCEPTION_(false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  jstring path;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  const char* buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  path = (jstring) env->GetStaticObjectField(clazz, imagePath_ID);
26560
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   319
  CHECK_EXCEPTION_(false);
26924
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   320
  if (path == NULL) {
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   321
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get imagePath field ID!", false);
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   322
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  buf = env->GetStringUTFChars(path, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  AutoJavaString imagePath(env, path, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  path = (jstring) env->GetStaticObjectField(clazz, symbolPath_ID);
26560
ec2ebd701956 8035419: warning from b09 for hotspot.agent.src.os.win32.windbg.sawindbg.cpp: 'JNI exception pending'
dsamersoff
parents: 22234
diff changeset
   328
  CHECK_EXCEPTION_(false);
26924
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   329
  if (path == NULL) {
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   330
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: not able to get symbolPath field ID!", false);
8b20f1c5af93 8054194: jstack crash: assert(handle != NULL) failed: JNI handle should not be null
dsamersoff
parents: 26560
diff changeset
   331
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  buf = env->GetStringUTFChars(path, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  AutoJavaString symbolPath(env, path, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  ptrIDebugSymbols->SetImagePath(imagePath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  ptrIDebugSymbols->SetSymbolPath(symbolPath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
static bool openDumpFile(JNIEnv* env, jobject obj, jstring coreFileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  // open the dump file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  const char* buf = env->GetStringUTFChars(coreFileName, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  AutoJavaString coreFile(env, coreFileName, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  if (setImageAndSymbolPath(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
     return 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
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  if (ptrIDebugClient->OpenDumpFile(coreFile) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: OpenDumpFile 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
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  if (ptrIDebugControl->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: WaitForEvent failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
static bool attachToProcess(JNIEnv* env, jobject obj, jint pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  if (setImageAndSymbolPath(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  /***********************************************************************************
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
     We are attaching to a process in 'read-only' mode. i.e., we do not want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
     put breakpoints, suspend/resume threads etc. For read-only JDI and HSDB kind of
15798
cea39eefa98e 7165259: Remove BugSpot
sla
parents: 15475
diff changeset
   385
     usage this should suffice.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
     Please refer to DEBUG_ATTACH_NONINVASIVE mode source comments from dbgeng.h.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
     In this mode, debug engine does not call DebugActiveProrcess. i.e., we are not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
     actually debugging at all. We can safely 'detach' from the process anytime
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
     we want and debuggee process is left as is on all Windows variants.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
     This also makes JDI-on-SA installation/usage simpler because with this we would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
     not need a tool like ServiceInstaller from http://www.kcmultimedia.com/smaster.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  ***********************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  if (ptrIDebugClient->AttachProcess(0, pid, DEBUG_ATTACH_NONINVASIVE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: AttachProcess failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  if (ptrIDebugControl->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: WaitForEvent failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
static bool addLoadObjects(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  ULONG loaded = 0, unloaded = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  if (ptrIDebugSymbols->GetNumberModules(&loaded, &unloaded) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetNumberModules failed!", 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
  AutoArrayPtr<DEBUG_MODULE_PARAMETERS> params(new DEBUG_MODULE_PARAMETERS[loaded]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  if (params.asPtr() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
      THROW_NEW_DEBUGGER_EXCEPTION_("out of memory to allocate debug module params!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  if (ptrIDebugSymbols->GetModuleParameters(loaded, 0, NULL, params.asPtr()) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetModuleParameters failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  for (int u = 0; u < (int)loaded; u++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
     TCHAR imageName[MAX_PATH];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
     if (ptrIDebugSymbols->GetModuleNames(DEBUG_ANY_ID, params.asPtr()[u].Base,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
                                      imageName, MAX_PATH, NULL, NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
                                      0, NULL, NULL, 0, NULL) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetModuleNames failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
     jstring strName = env->NewStringUTF(imageName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
     env->CallVoidMethod(obj, addLoadObject_ID, strName, (jlong) params.asPtr()[u].Size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
                               (jlong) params.asPtr()[u].Base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
static bool addThreads(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
                                                      ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  ULONG numThreads = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  if (ptrIDebugSystemObjects->GetNumberThreads(&numThreads) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetNumberThreads failed!", 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> ptrSysThreadIds = new ULONG[numThreads];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  if (ptrSysThreadIds.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
  AutoArrayPtr<ULONG> ptrThreadIds = new ULONG[numThreads];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  if (ptrThreadIds.asPtr() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
     THROW_NEW_DEBUGGER_EXCEPTION_("out of memory to allocate thread ids!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  if (ptrIDebugSystemObjects->GetThreadIdsByIndex(0, numThreads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
                                      ptrThreadIds.asPtr(), ptrSysThreadIds.asPtr()) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetThreadIdsByIndex failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  IDebugAdvanced* ptrIDebugAdvanced = (IDebugAdvanced*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
                                                      ptrIDebugAdvanced_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  // for each thread, get register context and save it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  for (ULONG t = 0; t < numThreads; t++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
     if (ptrIDebugSystemObjects->SetCurrentThreadId(ptrThreadIds.asPtr()[t]) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: SetCurrentThread failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
     jlongArray regs = env->NewLongArray(NPRGREG);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
     jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
     jlong* ptrRegs = env->GetLongArrayElements(regs, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
     // copy register values from the CONTEXT struct
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
     CONTEXT context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
     memset(&context, 0, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
#undef REG_INDEX
15475
73896d91270c 6518907: cleanup IA64 specific code in Hotspot
morris
parents: 5547
diff changeset
   500
#ifdef _M_IX86
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
     #define REG_INDEX(x) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
     context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
     ptrIDebugAdvanced->GetThreadContext(&context, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
     ptrRegs[REG_INDEX(GS)]  = context.SegGs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
     ptrRegs[REG_INDEX(FS)]  = context.SegFs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
     ptrRegs[REG_INDEX(ES)]  = context.SegEs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
     ptrRegs[REG_INDEX(DS)]  = context.SegDs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
     ptrRegs[REG_INDEX(EDI)] = context.Edi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
     ptrRegs[REG_INDEX(ESI)] = context.Esi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
     ptrRegs[REG_INDEX(EBX)] = context.Ebx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
     ptrRegs[REG_INDEX(EDX)] = context.Edx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
     ptrRegs[REG_INDEX(ECX)] = context.Ecx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
     ptrRegs[REG_INDEX(EAX)] = context.Eax;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
     ptrRegs[REG_INDEX(FP)] = context.Ebp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
     ptrRegs[REG_INDEX(PC)] = context.Eip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
     ptrRegs[REG_INDEX(CS)]  = context.SegCs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
     ptrRegs[REG_INDEX(EFL)] = context.EFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
     ptrRegs[REG_INDEX(SP)] = context.Esp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
     ptrRegs[REG_INDEX(SS)]  = context.SegSs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
     ptrRegs[REG_INDEX(DR0)] = context.Dr0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
     ptrRegs[REG_INDEX(DR1)] = context.Dr1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
     ptrRegs[REG_INDEX(DR2)] = context.Dr2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
     ptrRegs[REG_INDEX(DR3)] = context.Dr3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
     ptrRegs[REG_INDEX(DR6)] = context.Dr6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
     ptrRegs[REG_INDEX(DR7)] = context.Dr7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
#elif _M_AMD64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
     #define REG_INDEX(x) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
     context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
     ptrIDebugAdvanced->GetThreadContext(&context, sizeof(CONTEXT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
     // Segment Registers and processor flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
     ptrRegs[REG_INDEX(CS)]  = context.SegCs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
     ptrRegs[REG_INDEX(DS)]  = context.SegDs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
     ptrRegs[REG_INDEX(ES)]  = context.SegEs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
     ptrRegs[REG_INDEX(FS)]  = context.SegFs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
     ptrRegs[REG_INDEX(GS)]  = context.SegGs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
     ptrRegs[REG_INDEX(SS)]  = context.SegSs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
     ptrRegs[REG_INDEX(RFL)] = context.EFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
     // Integer registers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
     ptrRegs[REG_INDEX(RDI)] = context.Rdi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
     ptrRegs[REG_INDEX(RSI)] = context.Rsi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
     ptrRegs[REG_INDEX(RAX)] = context.Rax;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
     ptrRegs[REG_INDEX(RCX)] = context.Rcx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
     ptrRegs[REG_INDEX(RDX)] = context.Rdx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
     ptrRegs[REG_INDEX(RBX)] = context.Rbx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
     ptrRegs[REG_INDEX(RBP)] = context.Rbp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
     ptrRegs[REG_INDEX(RSP)] = context.Rsp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
     ptrRegs[REG_INDEX(R8)]  = context.R8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
     ptrRegs[REG_INDEX(R9)]  = context.R9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
     ptrRegs[REG_INDEX(R10)] = context.R10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
     ptrRegs[REG_INDEX(R11)] = context.R11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
     ptrRegs[REG_INDEX(R12)] = context.R12;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
     ptrRegs[REG_INDEX(R13)] = context.R13;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
     ptrRegs[REG_INDEX(R14)] = context.R14;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
     ptrRegs[REG_INDEX(R15)] = context.R15;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
     // Program counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
     ptrRegs[REG_INDEX(RIP)] = context.Rip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
     env->ReleaseLongArrayElements(regs, ptrRegs, JNI_COMMIT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
     env->CallVoidMethod(obj, setThreadIntegerRegisterSet_ID,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
                        (jlong) ptrThreadIds.asPtr()[t], regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
     ULONG sysId;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
     if (ptrIDebugSystemObjects->GetCurrentThreadSystemId(&sysId) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
        THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetCurrentThreadSystemId failed!", false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
     env->CallVoidMethod(obj, addThread_ID, (jlong) sysId);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
     CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
 * Method:    attach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
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
   595
  (JNIEnv *env, jobject obj, jstring execName, jstring coreFileName) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
  if (getWindbgInterfaces(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  if (openDumpFile(env, obj, coreFileName) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
  if (addLoadObjects(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  if (addThreads(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
 * Method:    attach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
 * Signature: (I)V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_attach0__I
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
  (JNIEnv *env, jobject obj, jint pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  if (getWindbgInterfaces(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  if (attachToProcess(env, obj, pid) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  if (addLoadObjects(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  if (addThreads(env, obj) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
     return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
static bool releaseWindbgInterfaces(JNIEnv* env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
  IDebugDataSpaces* ptrIDebugDataSpaces = (IDebugDataSpaces*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
                                                      ptrIDebugDataSpaces_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  if (ptrIDebugDataSpaces != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
     ptrIDebugDataSpaces->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  IDebugOutputCallbacks* ptrIDebugOutputCallbacks = (IDebugOutputCallbacks*)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
                          env->GetLongField(obj, ptrIDebugOutputCallbacks_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  if (ptrIDebugOutputCallbacks != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
     ptrIDebugOutputCallbacks->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
  IDebugAdvanced* ptrIDebugAdvanced = (IDebugAdvanced*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
                                                      ptrIDebugAdvanced_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
  if (ptrIDebugAdvanced != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
     ptrIDebugAdvanced->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
  if (ptrIDebugSymbols != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
     ptrIDebugSymbols->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
                                                      ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
  if (ptrIDebugSystemObjects != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
     ptrIDebugSystemObjects->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
  IDebugControl* ptrIDebugControl = (IDebugControl*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
                                                     ptrIDebugControl_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  if (ptrIDebugControl != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
     ptrIDebugControl->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
  CHECK_EXCEPTION_(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  if (ptrIDebugClient != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
     ptrIDebugClient->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
 * Method:    detach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
 * Signature: ()V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_detach0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
  (JNIEnv *env, jobject obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
                                                      ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
  CHECK_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
  ptrIDebugClient->DetachProcesses();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  releaseWindbgInterfaces(env, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
 * Method:    readBytesFromProcess0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
 * Signature: (JJ)[B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_readBytesFromProcess0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  (JNIEnv *env, jobject obj, jlong address, jlong numBytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
  jbyteArray byteArray = env->NewByteArray((long) numBytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
  jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  jbyte* bytePtr = env->GetByteArrayElements(byteArray, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  IDebugDataSpaces* ptrIDebugDataSpaces = (IDebugDataSpaces*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
                                                       ptrIDebugDataSpaces_ID);
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   725
  if (env->ExceptionOccurred()) {
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   726
     env->ReleaseByteArrayElements(byteArray, bytePtr, JNI_ABORT);
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   727
     return 0;
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   728
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
  ULONG bytesRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  if (ptrIDebugDataSpaces->ReadVirtual((ULONG64) address, (PVOID) bytePtr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
                                  (ULONG)numBytes, &bytesRead) != S_OK) {
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   733
     env->ReleaseByteArrayElements(byteArray, bytePtr, JNI_ABORT);
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   734
     throwNewDebuggerException(env, "Windbg Error: ReadVirtual failed!");
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   735
     return 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  if (bytesRead != numBytes) {
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   739
     env->ReleaseByteArrayElements(byteArray, bytePtr, JNI_ABORT);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
     return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  }
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 49274
diff changeset
   742
  env->ReleaseByteArrayElements(byteArray, bytePtr, 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
  return byteArray;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
 * Method:    getThreadIdFromSysId0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
 * Signature: (J)J
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_getThreadIdFromSysId0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
  (JNIEnv *env, jobject obj, jlong sysId) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  IDebugSystemObjects* ptrIDebugSystemObjects = (IDebugSystemObjects*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
                                                    ptrIDebugSystemObjects_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
  ULONG id = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
  if (ptrIDebugSystemObjects->GetThreadIdBySystemId((ULONG)sysId, &id) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: GetThreadIdBySystemId failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
  return (jlong) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
// manage COM 'auto' pointers (to avoid multiple Release
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
// calls at every early (exception) returns). Similar to AutoArrayPtr.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
template <class T>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
class AutoCOMPtr {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      T* m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
      AutoCOMPtr(T* ptr) : m_ptr(ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
      ~AutoCOMPtr() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
         if (m_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
            m_ptr->Release();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      T* operator->() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
         return m_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
 * Method:    consoleExecuteCommand0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_consoleExecuteCommand0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
  (JNIEnv *env, jobject obj, jstring cmd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
  jboolean isCopy = JNI_FALSE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
  const char* buf = env->GetStringUTFChars(cmd, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
  AutoJavaString command(env, cmd, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  IDebugClient* ptrIDebugClient = (IDebugClient*) env->GetLongField(obj, ptrIDebugClient_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
  IDebugClient*  tmpClientPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  if (ptrIDebugClient->CreateClient(&tmpClientPtr) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: CreateClient failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  AutoCOMPtr<IDebugClient> tmpClient(tmpClientPtr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
  IDebugControl* tmpControlPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  if (tmpClient->QueryInterface(__uuidof(IDebugControl), (PVOID*) &tmpControlPtr) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: QueryInterface (IDebugControl) failed", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
  AutoCOMPtr<IDebugControl> tmpControl(tmpControlPtr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  SAOutputCallbacks* saOutputCallbacks = (SAOutputCallbacks*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
                                                                   ptrIDebugOutputCallbacks_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
  saOutputCallbacks->clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  if (tmpClient->SetOutputCallbacks(saOutputCallbacks) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
     THROW_NEW_DEBUGGER_EXCEPTION_("Windbg Error: SetOutputCallbacks failed!", 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
  tmpControl->Execute(DEBUG_OUTPUT_VERBOSE, command, DEBUG_EXECUTE_DEFAULT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  const char* output = saOutputCallbacks->getBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
  if (output == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
     output = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  jstring res = env->NewStringUTF(output);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
  saOutputCallbacks->clearBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
 * Method:    lookupByName0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_lookupByName0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
(JNIEnv *env, jobject obj, jstring objName, jstring sym) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
  jboolean isCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
  const char* buf = env->GetStringUTFChars(sym, &isCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
  AutoJavaString name(env, sym, buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
  ULONG64 offset = 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
  if (strstr(name, "::") != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
    ptrIDebugSymbols->AddSymbolOptions(SYMOPT_UNDNAME);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
    ptrIDebugSymbols->RemoveSymbolOptions(SYMOPT_UNDNAME);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
  if (ptrIDebugSymbols->GetOffsetByName(name, &offset) != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
    return (jlong) 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
  return (jlong) offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
#define SYMBOL_BUFSIZE 512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
 * Class:     sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
 * Method:    lookupByAddress0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_windbg_WindbgDebuggerLocal_lookupByAddress0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
(JNIEnv *env, jobject obj, jlong address) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
  IDebugSymbols* ptrIDebugSymbols = (IDebugSymbols*) env->GetLongField(obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
                                                      ptrIDebugSymbols_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  ULONG64 disp = 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
  char buf[SYMBOL_BUFSIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
  memset(buf, 0, sizeof(buf));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  if (ptrIDebugSymbols->GetNameByOffset(address, buf, sizeof(buf),0,&disp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
      != S_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
  jstring sym = env->NewStringUTF(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
  jobject res = env->CallObjectMethod(obj, createClosestSymbol_ID, sym, disp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  CHECK_EXCEPTION_(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
}