hotspot/agent/src/share/native/jvmdi/sa.cpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1 489c9b5090e2
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2002, 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include <stdarg.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <vector>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include "sa.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include "jvmdi.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#ifndef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
 #include <inttypes.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
 typedef int int32_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
 #include <windows.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
 #define YIELD() Sleep(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 #define SLEEP() Sleep(10)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
 #define vsnprintf _vsnprintf
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 Error: please port YIELD() and SLEEP() macros to your platform
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
using namespace std;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//////////////////////////////////////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
//                                                                  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// Exported "interface" for Java language-level interaction between //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// the SA and the VM. Note that the SA knows about the layout of    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// certain VM data structures and that knowledge is taken advantage //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
// of in this code, although this interfaces with the VM via JVMDI. //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
//                                                                  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//////////////////////////////////////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
extern "C" {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  /////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  //                                 //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // Events sent by the VM to the SA //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  //                                 //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  /////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  // Set by the SA when it attaches. Indicates that events should be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // posted via these exported variables, and that the VM should wait
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // for those events to be acknowledged by the SA (via its setting
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // saEventPending to 0).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  JNIEXPORT volatile int32_t saAttached     = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // Set to nonzero value by the VM when an event has been posted; set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // back to 0 by the SA when it has processed that event.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  JNIEXPORT volatile int32_t saEventPending = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // Kind of the event (from jvmdi.h)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  JNIEXPORT volatile int32_t saEventKind    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // Exception events
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  JNIEXPORT jthread   saExceptionThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  JNIEXPORT jclass    saExceptionClass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  JNIEXPORT jmethodID saExceptionMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  JNIEXPORT int32_t   saExceptionLocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  JNIEXPORT jobject   saExceptionException;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  JNIEXPORT jclass    saExceptionCatchClass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  JNIEXPORT jmethodID saExceptionCatchMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  JNIEXPORT int32_t   saExceptionCatchLocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // Breakpoint events
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  JNIEXPORT jthread   saBreakpointThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  JNIEXPORT jclass    saBreakpointClass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  JNIEXPORT jmethodID saBreakpointMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  JNIEXPORT jlocation saBreakpointLocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  ///////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  //                                   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Commands sent by the SA to the VM //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  //                                   //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  ///////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  extern JNIEXPORT const int32_t SA_CMD_SUSPEND_ALL       = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  extern JNIEXPORT const int32_t SA_CMD_RESUME_ALL        = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  extern JNIEXPORT const int32_t SA_CMD_TOGGLE_BREAKPOINT = 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  extern JNIEXPORT const int32_t SA_CMD_BUF_SIZE          = 1024;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // SA sets this to a nonzero value when it is requesting a command
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // to be processed; VM sets it back to 0 when the command has been
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  // executed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  JNIEXPORT volatile int32_t saCmdPending   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // SA sets this to one of the manifest constants above to indicate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // the kind of command to be executed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  JNIEXPORT volatile int32_t saCmdType      = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // VM sets this to 0 if the last command succeeded or a nonzero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  // value if it failed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  JNIEXPORT volatile int32_t saCmdResult    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // If last command failed, this buffer will contain a descriptive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  // error message
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  JNIEXPORT char             saCmdResultErrMsg[SA_CMD_BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  // Toggling of breakpoint command arguments.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  // Originally there were separate set/clear breakpoint commands
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // taking a class name, method name and signature, and the iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // through the debug information was done in the SA. It turns out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  // that doing this work in the target VM is significantly faster,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  // and since interactivity when setting and clearing breakpoints is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // important, the solution which resulted in more C/C++ code was used.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // Source file name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  JNIEXPORT char    saCmdBkptSrcFileName[SA_CMD_BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Package name ('/' as separator instead of '.')
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  JNIEXPORT char    saCmdBkptPkgName[SA_CMD_BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  // Line number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  JNIEXPORT int32_t saCmdBkptLineNumber;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // Output back to SA: indicator whether the last failure of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  // breakpoint toggle command was really an error or just a lack of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // debug information covering the requested line. 0 if not error.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // Valid only if saCmdResult != 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  JNIEXPORT int32_t saCmdBkptResWasError;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // Output back to SA: resulting line number at which the breakpoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  // was set or cleared (valid only if saCmdResult == 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  JNIEXPORT int32_t saCmdBkptResLineNumber;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  // Output back to SA: resulting byte code index at which the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  // breakpoint was set or cleared (valid only if saCmdResult == 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  JNIEXPORT int32_t saCmdBkptResBCI;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  // Output back to SA: indicator whether the breakpoint operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  // resulted in a set or cleared breakpoint; nonzero if set, zero if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  // cleared (valid only if saCmdResult == 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  JNIEXPORT int32_t saCmdBkptResWasSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Output back to SA: method name the breakpoint was set in (valid
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // only if saCmdResult == 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  JNIEXPORT char    saCmdBkptResMethodName[SA_CMD_BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  // Output back to SA: method signature (JNI style) the breakpoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // was set in (valid only if saCmdResult == 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  JNIEXPORT char    saCmdBkptResMethodSig[SA_CMD_BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
// Internal state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
static JavaVM* jvm = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
static JVMDI_Interface_1* jvmdi = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
static jthread debugThreadObj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
static bool suspended = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
static vector<jthread> suspendedThreads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
static JVMDI_RawMonitor eventLock = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
class MonitorLocker {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  JVMDI_RawMonitor lock;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  MonitorLocker(JVMDI_RawMonitor lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    this->lock = lock;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    if (lock != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      jvmdi->RawMonitorEnter(lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  ~MonitorLocker() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    if (lock != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
      jvmdi->RawMonitorExit(lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
class JvmdiDeallocator {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  void* ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  JvmdiDeallocator(void* ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    this->ptr = ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  ~JvmdiDeallocator() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    jvmdi->Deallocate((jbyte*) ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
class JvmdiRefListDeallocator {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  JNIEnv* env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  jobject* refList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  jint refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  JvmdiRefListDeallocator(JNIEnv* env, jobject* refList, jint refCount) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    this->env = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    this->refList = refList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    this->refCount = refCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  ~JvmdiRefListDeallocator() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    for (int i = 0; i < refCount; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      env->DeleteGlobalRef(refList[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    jvmdi->Deallocate((jbyte*) refList);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
static void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
stop(char* msg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  fprintf(stderr, "%s", msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  fprintf(stderr, "\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  exit(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
// This fills in the command result error message, sets the command
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
// result to -1, and clears the pending command flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
static void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
reportErrorToSA(const char* str, ...) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  va_list varargs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  va_start(varargs, str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  vsnprintf(saCmdResultErrMsg, sizeof(saCmdResultErrMsg), str, varargs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  va_end(varargs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  saCmdResult = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  saCmdPending = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
static bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
packageNameMatches(char* clazzName, char* pkg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  int pkgLen = strlen(pkg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  int clazzNameLen = strlen(clazzName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  if (pkgLen >= clazzNameLen + 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  if (strncmp(clazzName, pkg, pkgLen)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  // Ensure that '/' is the next character if non-empty package name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  int l = pkgLen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  if (l > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    if (clazzName[l] != '/') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    l++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  // Ensure that there are no more trailing slashes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  while (l < clazzNameLen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    if (clazzName[l++] == '/') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
static void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
executeOneCommand(JNIEnv* env) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  switch (saCmdType) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  case SA_CMD_SUSPEND_ALL: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
    if (suspended) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
      reportErrorToSA("Target process already suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    // We implement this by getting all of the threads and calling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    // SuspendThread on each one, except for the thread object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    // corresponding to this thread. Each thread for which the call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    // succeeded (i.e., did not return JVMDI_ERROR_INVALID_THREAD)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    // is added to a list which is remembered for later resumption.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    // Note that this currently has race conditions since a thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    // might be started after we call GetAllThreads and since a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    // thread for which we got an error earlier might be resumed by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    // the VM while we are busy suspending other threads. We could
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    // solve this by looping until there are no more threads we can
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    // suspend, but a more robust and scalable solution is to add
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    // this functionality to the JVMDI interface (i.e.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    // "suspendAll"). Probably need to provide an exclude list for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    // such a routine.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    jint threadCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    jthread* threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    if (jvmdi->GetAllThreads(&threadCount, &threads) != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      reportErrorToSA("Error while getting thread list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    for (int i = 0; i < threadCount; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
      jthread thr = threads[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      if (!env->IsSameObject(thr, debugThreadObj)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
        jvmdiError err = jvmdi->SuspendThread(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
        if (err == JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
          // Remember this thread and do not free it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
          suspendedThreads.push_back(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
          continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
          fprintf(stderr, " SA: Error %d while suspending thread\n", err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
          // FIXME: stop, resume all threads, report error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      env->DeleteGlobalRef(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    // Free up threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    jvmdi->Deallocate((jbyte*) threads);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    // Suspension is complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    suspended = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  case SA_CMD_RESUME_ALL: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    if (!suspended) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
      reportErrorToSA("Target process already suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    saCmdResult = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    bool errorOccurred = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    jvmdiError firstError;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    for (int i = 0; i < suspendedThreads.size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      jthread thr = suspendedThreads[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
      jvmdiError err = jvmdi->ResumeThread(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
      env->DeleteGlobalRef(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
      if (err != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
        if (!errorOccurred) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
          errorOccurred = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
          firstError = err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    suspendedThreads.clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    suspended = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    if (errorOccurred) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
      reportErrorToSA("Error %d while resuming threads", firstError);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  case SA_CMD_TOGGLE_BREAKPOINT: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    saCmdBkptResWasError = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    // Search line number info for all loaded classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    jint classCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    jclass* classes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    jvmdiError glcRes = jvmdi->GetLoadedClasses(&classCount, &classes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    if (glcRes != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
      reportErrorToSA("Error %d while getting loaded classes", glcRes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    JvmdiRefListDeallocator rld(env, (jobject*) classes, classCount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    bool done = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    bool gotOne = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    jclass targetClass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    jmethodID targetMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    jlocation targetLocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    jint targetLineNumber;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    for (int i = 0; i < classCount && !done; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
      fflush(stderr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      jclass clazz = classes[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
      char* srcName;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
      jvmdiError sfnRes = jvmdi->GetSourceFileName(clazz, &srcName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
      if (sfnRes == JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
        JvmdiDeallocator de1(srcName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
        if (!strcmp(srcName, saCmdBkptSrcFileName)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
          // Got a match. Now see whether the package name of the class also matches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
          char* clazzName;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
          jvmdiError sigRes = jvmdi->GetClassSignature(clazz, &clazzName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
          if (sigRes != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
            reportErrorToSA("Error %d while getting a class's signature", sigRes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
            return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
          JvmdiDeallocator de2(clazzName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
          if (packageNameMatches(clazzName + 1, saCmdBkptPkgName)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
            // Iterate through all methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
            jint methodCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
            jmethodID* methods;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
            if (jvmdi->GetClassMethods(clazz, &methodCount, &methods) != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
              reportErrorToSA("Error while getting methods of class %s", clazzName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
              return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
            JvmdiDeallocator de3(methods);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
            for (int j = 0; j < methodCount && !done; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
              jmethodID m = methods[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
              jint entryCount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
              JVMDI_line_number_entry* table;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
              jvmdiError lnRes = jvmdi->GetLineNumberTable(clazz, m, &entryCount, &table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
              if (lnRes == JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
                JvmdiDeallocator de4(table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
                // Look for line number greater than or equal to requested line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
                for (int k = 0; k < entryCount && !done; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
                  JVMDI_line_number_entry& entry = table[k];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
                  if (entry.line_number >= saCmdBkptLineNumber &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
                      (!gotOne || entry.line_number < targetLineNumber)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
                    gotOne = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
                    targetClass = clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
                    targetMethod = m;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
                    targetLocation = entry.start_location;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
                    targetLineNumber = entry.line_number;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
                    done = (targetLineNumber == saCmdBkptLineNumber);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
                  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
              } else if (lnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
                reportErrorToSA("Unexpected error %d while fetching line number table", lnRes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
                return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
      } else if (sfnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
        reportErrorToSA("Unexpected error %d while fetching source file name", sfnRes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
    bool wasSet = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    if (gotOne) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      // Really toggle this breakpoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
      jvmdiError bpRes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
      bpRes = jvmdi->SetBreakpoint(targetClass, targetMethod, targetLocation);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
      if (bpRes == JVMDI_ERROR_DUPLICATE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
        bpRes = jvmdi->ClearBreakpoint(targetClass, targetMethod, targetLocation);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
        wasSet = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
      if (bpRes != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
        reportErrorToSA("Unexpected error %d while setting or clearing breakpoint at bci %d, line %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
                        bpRes, targetLocation, targetLineNumber);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
      saCmdBkptResWasError = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      reportErrorToSA("No debug information found covering this line");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
    // Provide result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    saCmdBkptResLineNumber = targetLineNumber;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    saCmdBkptResBCI        = targetLocation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    saCmdBkptResWasSet     = (wasSet ? 1 : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      char* methodName;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
      char* methodSig;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
      if (jvmdi->GetMethodName(targetClass, targetMethod, &methodName, &methodSig)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
          == JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
        JvmdiDeallocator mnd(methodName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
        JvmdiDeallocator msd(methodSig);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
        strncpy(saCmdBkptResMethodName, methodName, SA_CMD_BUF_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        strncpy(saCmdBkptResMethodSig,  methodSig, SA_CMD_BUF_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
        strncpy(saCmdBkptResMethodName, "<error>", SA_CMD_BUF_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
        strncpy(saCmdBkptResMethodSig,  "<error>", SA_CMD_BUF_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    reportErrorToSA("Command %d not yet supported", saCmdType);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  // Successful command execution
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  saCmdResult = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  saCmdPending = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
static void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
saCommandThread(void *arg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  JNIEnv* env = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  if (jvm->GetEnv((void **) &env, JNI_VERSION_1_2) != JNI_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
    stop("Error while starting Serviceability Agent "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
         "command thread: could not get JNI environment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  while (1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
    // Wait for command
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
    while (!saCmdPending) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
      SLEEP();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
    executeOneCommand(env);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
static void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
saEventHook(JNIEnv *env, JVMDI_Event *event)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  MonitorLocker ml(eventLock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  saEventKind = event->kind;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  if (event->kind == JVMDI_EVENT_VM_INIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    // Create event lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    if (jvmdi->CreateRawMonitor("Serviceability Agent Event Lock", &eventLock)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
        != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
      stop("Unable to create Serviceability Agent's event lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    // Start thread which receives commands from the SA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    jclass threadClass = env->FindClass("java/lang/Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    if (threadClass == NULL) stop("Unable to find class java/lang/Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    jstring threadName = env->NewStringUTF("Serviceability Agent Command Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    if (threadName == NULL) stop("Unable to allocate debug thread name");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    jmethodID ctor = env->GetMethodID(threadClass, "<init>", "(Ljava/lang/String;)V");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
    if (ctor == NULL) stop("Unable to find appropriate constructor for java/lang/Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
    // Allocate thread object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
    jthread thr = (jthread) env->NewObject(threadClass, ctor, threadName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    if (thr == NULL) stop("Unable to allocate debug thread's java/lang/Thread instance");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    // Remember which thread this is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    debugThreadObj = env->NewGlobalRef(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
    if (debugThreadObj == NULL) stop("Unable to allocate global ref for debug thread object");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    // Start thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    jvmdiError err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
    if ((err = jvmdi->RunDebugThread(thr, &saCommandThread, NULL, JVMDI_THREAD_NORM_PRIORITY))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
        != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
      char buf[256];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
      sprintf(buf, "Error %d while starting debug thread", err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
      stop(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    // OK, initialization is done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  if (!saAttached) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  switch (event->kind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  case JVMDI_EVENT_EXCEPTION: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
    fprintf(stderr, "SA: Exception thrown -- ignoring\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
    saExceptionThread        = event->u.exception.thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
    saExceptionClass         = event->u.exception.clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
    saExceptionMethod        = event->u.exception.method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
    saExceptionLocation      = event->u.exception.location;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    saExceptionException     = event->u.exception.exception;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
    saExceptionCatchClass    = event->u.exception.catch_clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
    saExceptionCatchClass    = event->u.exception.catch_clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
    saExceptionCatchMethod   = event->u.exception.catch_method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
    saExceptionCatchLocation = event->u.exception.catch_location;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
    //    saEventPending = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  case JVMDI_EVENT_BREAKPOINT: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
    saBreakpointThread       = event->u.breakpoint.thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
    saBreakpointClass        = event->u.breakpoint.clazz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
    saBreakpointMethod       = event->u.breakpoint.method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
    saBreakpointLocation     = event->u.breakpoint.location;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    saEventPending = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  while (saAttached && saEventPending) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
    SLEEP();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
extern "C" {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
JNIEXPORT jint JNICALL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
JVM_OnLoad(JavaVM *vm, char *options, void *reserved)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  jvm = vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  if (jvm->GetEnv((void**) &jvmdi, JVMDI_VERSION_1) != JNI_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  if (jvmdi->SetEventHook(&saEventHook) != JVMDI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
};