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