aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/native/jvmdi/sa.cpp
blob: e93521a7f5b453ebac690440ca803fa289ad5865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
 * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <vector>
#include "sa.hpp"
#include "jni.h"
#include "jvmdi.h"

#ifndef WIN32
 #include <inttypes.h>
#else
 typedef int int32_t;
#endif

#ifdef WIN32
 #include <windows.h>
 #define YIELD() Sleep(0)
 #define SLEEP() Sleep(10)
 #define vsnprintf _vsnprintf
#else
 Error: please port YIELD() and SLEEP() macros to your platform
#endif

using namespace std;

//////////////////////////////////////////////////////////////////////
//                                                                  //
// Exported "interface" for Java language-level interaction between //
// the SA and the VM. Note that the SA knows about the layout of    //
// certain VM data structures and that knowledge is taken advantage //
// of in this code, although this interfaces with the VM via JVMDI. //
//                                                                  //
//////////////////////////////////////////////////////////////////////

extern "C" {
  /////////////////////////////////////
  //                                 //
  // Events sent by the VM to the SA //
  //                                 //
  /////////////////////////////////////

  // Set by the SA when it attaches. Indicates that events should be
  // posted via these exported variables, and that the VM should wait
  // for those events to be acknowledged by the SA (via its setting
  // saEventPending to 0).
  JNIEXPORT volatile int32_t saAttached     = 0;

  // Set to nonzero value by the VM when an event has been posted; set
  // back to 0 by the SA when it has processed that event.
  JNIEXPORT volatile int32_t saEventPending = 0;

  // Kind of the event (from jvmdi.h)
  JNIEXPORT volatile int32_t saEventKind    = 0;

  //
  // Exception events
  //
  JNIEXPORT jthread   saExceptionThread;
  JNIEXPORT jclass    saExceptionClass;
  JNIEXPORT jmethodID saExceptionMethod;
  JNIEXPORT int32_t   saExceptionLocation;
  JNIEXPORT jobject   saExceptionException;
  JNIEXPORT jclass    saExceptionCatchClass;
  JNIEXPORT jmethodID saExceptionCatchMethod;
  JNIEXPORT int32_t   saExceptionCatchLocation;

  //
  // Breakpoint events
  //
  JNIEXPORT jthread   saBreakpointThread;
  JNIEXPORT jclass    saBreakpointClass;
  JNIEXPORT jmethodID saBreakpointMethod;
  JNIEXPORT jlocation saBreakpointLocation;

  ///////////////////////////////////////
  //                                   //
  // Commands sent by the SA to the VM //
  //                                   //
  ///////////////////////////////////////

  extern JNIEXPORT const int32_t SA_CMD_SUSPEND_ALL       = 0;
  extern JNIEXPORT const int32_t SA_CMD_RESUME_ALL        = 1;
  extern JNIEXPORT const int32_t SA_CMD_TOGGLE_BREAKPOINT = 2;
  extern JNIEXPORT const int32_t SA_CMD_BUF_SIZE          = 1024;

  // SA sets this to a nonzero value when it is requesting a command
  // to be processed; VM sets it back to 0 when the command has been
  // executed
  JNIEXPORT volatile int32_t saCmdPending   = 0;

  // SA sets this to one of the manifest constants above to indicate
  // the kind of command to be executed
  JNIEXPORT volatile int32_t saCmdType      = 0;

  // VM sets this to 0 if the last command succeeded or a nonzero
  // value if it failed
  JNIEXPORT volatile int32_t saCmdResult    = 0;

  // If last command failed, this buffer will contain a descriptive
  // error message
  JNIEXPORT char             saCmdResultErrMsg[SA_CMD_BUF_SIZE];

  //
  // Toggling of breakpoint command arguments.
  //
  // Originally there were separate set/clear breakpoint commands
  // taking a class name, method name and signature, and the iteration
  // through the debug information was done in the SA. It turns out
  // that doing this work in the target VM is significantly faster,
  // and since interactivity when setting and clearing breakpoints is
  // important, the solution which resulted in more C/C++ code was used.
  //

  // Source file name
  JNIEXPORT char    saCmdBkptSrcFileName[SA_CMD_BUF_SIZE];

  // Package name ('/' as separator instead of '.')
  JNIEXPORT char    saCmdBkptPkgName[SA_CMD_BUF_SIZE];

  // Line number
  JNIEXPORT int32_t saCmdBkptLineNumber;

  // Output back to SA: indicator whether the last failure of a
  // breakpoint toggle command was really an error or just a lack of
  // debug information covering the requested line. 0 if not error.
  // Valid only if saCmdResult != 0.
  JNIEXPORT int32_t saCmdBkptResWasError;

  // Output back to SA: resulting line number at which the breakpoint
  // was set or cleared (valid only if saCmdResult == 0)
  JNIEXPORT int32_t saCmdBkptResLineNumber;

  // Output back to SA: resulting byte code index at which the
  // breakpoint was set or cleared (valid only if saCmdResult == 0)
  JNIEXPORT int32_t saCmdBkptResBCI;

  // Output back to SA: indicator whether the breakpoint operation
  // resulted in a set or cleared breakpoint; nonzero if set, zero if
  // cleared (valid only if saCmdResult == 0)
  JNIEXPORT int32_t saCmdBkptResWasSet;

  // Output back to SA: method name the breakpoint was set in (valid
  // only if saCmdResult == 0)
  JNIEXPORT char    saCmdBkptResMethodName[SA_CMD_BUF_SIZE];

  // Output back to SA: method signature (JNI style) the breakpoint
  // was set in (valid only if saCmdResult == 0)
  JNIEXPORT char    saCmdBkptResMethodSig[SA_CMD_BUF_SIZE];
}

// Internal state
static JavaVM* jvm = NULL;
static JVMDI_Interface_1* jvmdi = NULL;
static jthread debugThreadObj = NULL;
static bool suspended = false;
static vector<jthread> suspendedThreads;
static JVMDI_RawMonitor eventLock = NULL;

class MonitorLocker {
private:
  JVMDI_RawMonitor lock;
public:
  MonitorLocker(JVMDI_RawMonitor lock) {
    this->lock = lock;
    if (lock != NULL) {
      jvmdi->RawMonitorEnter(lock);
    }
  }
  ~MonitorLocker() {
    if (lock != NULL) {
      jvmdi->RawMonitorExit(lock);
    }
  }
};

class JvmdiDeallocator {
private:
  void* ptr;
public:
  JvmdiDeallocator(void* ptr) {
    this->ptr = ptr;
  }
  ~JvmdiDeallocator() {
    jvmdi->Deallocate((jbyte*) ptr);
  }
};

class JvmdiRefListDeallocator {
private:
  JNIEnv* env;
  jobject* refList;
  jint refCount;
public:
  JvmdiRefListDeallocator(JNIEnv* env, jobject* refList, jint refCount) {
    this->env = env;
    this->refList = refList;
    this->refCount = refCount;
  }
  ~JvmdiRefListDeallocator() {
    for (int i = 0; i < refCount; i++) {
      env->DeleteGlobalRef(refList[i]);
    }
    jvmdi->Deallocate((jbyte*) refList);
  }
};

static void
stop(char* msg) {
  fprintf(stderr, "%s", msg);
  fprintf(stderr, "\n");
  exit(1);
}

// This fills in the command result error message, sets the command
// result to -1, and clears the pending command flag
static void
reportErrorToSA(const char* str, ...) {
  va_list varargs;
  va_start(varargs, str);
  vsnprintf(saCmdResultErrMsg, sizeof(saCmdResultErrMsg), str, varargs);
  va_end(varargs);
  saCmdResult = -1;
  saCmdPending = 0;
}

static bool
packageNameMatches(char* clazzName, char* pkg) {
  int pkgLen = strlen(pkg);
  int clazzNameLen = strlen(clazzName);

  if (pkgLen >= clazzNameLen + 1) {
    return false;
  }

  if (strncmp(clazzName, pkg, pkgLen)) {
    return false;
  }

  // Ensure that '/' is the next character if non-empty package name
  int l = pkgLen;
  if (l > 0) {
    if (clazzName[l] != '/') {
      return false;
    }
    l++;
  }
  // Ensure that there are no more trailing slashes
  while (l < clazzNameLen) {
    if (clazzName[l++] == '/') {
      return false;
    }
  }
  return true;
}

static void
executeOneCommand(JNIEnv* env) {
  switch (saCmdType) {
  case SA_CMD_SUSPEND_ALL: {
    if (suspended) {
      reportErrorToSA("Target process already suspended");
      return;
    }

    // We implement this by getting all of the threads and calling
    // SuspendThread on each one, except for the thread object
    // corresponding to this thread. Each thread for which the call
    // succeeded (i.e., did not return JVMDI_ERROR_INVALID_THREAD)
    // is added to a list which is remembered for later resumption.
    // Note that this currently has race conditions since a thread
    // might be started after we call GetAllThreads and since a
    // thread for which we got an error earlier might be resumed by
    // the VM while we are busy suspending other threads. We could
    // solve this by looping until there are no more threads we can
    // suspend, but a more robust and scalable solution is to add
    // this functionality to the JVMDI interface (i.e.,
    // "suspendAll"). Probably need to provide an exclude list for
    // such a routine.
    jint threadCount;
    jthread* threads;
    if (jvmdi->GetAllThreads(&threadCount, &threads) != JVMDI_ERROR_NONE) {
      reportErrorToSA("Error while getting thread list");
      return;
    }


    for (int i = 0; i < threadCount; i++) {
      jthread thr = threads[i];
      if (!env->IsSameObject(thr, debugThreadObj)) {
        jvmdiError err = jvmdi->SuspendThread(thr);
        if (err == JVMDI_ERROR_NONE) {
          // Remember this thread and do not free it
          suspendedThreads.push_back(thr);
          continue;
        } else {
          fprintf(stderr, " SA: Error %d while suspending thread\n", err);
          // FIXME: stop, resume all threads, report error
        }
      }
      env->DeleteGlobalRef(thr);
    }

    // Free up threads
    jvmdi->Deallocate((jbyte*) threads);

    // Suspension is complete
    suspended = true;
    break;
  }

  case SA_CMD_RESUME_ALL: {
    if (!suspended) {
      reportErrorToSA("Target process already suspended");
      return;
    }

    saCmdResult = 0;
    bool errorOccurred = false;
    jvmdiError firstError;
    for (int i = 0; i < suspendedThreads.size(); i++) {
      jthread thr = suspendedThreads[i];
      jvmdiError err = jvmdi->ResumeThread(thr);
      env->DeleteGlobalRef(thr);
      if (err != JVMDI_ERROR_NONE) {
        if (!errorOccurred) {
          errorOccurred = true;
          firstError = err;
        }
      }
    }
    suspendedThreads.clear();
    suspended = false;
    if (errorOccurred) {
      reportErrorToSA("Error %d while resuming threads", firstError);
      return;
    }
    break;
  }

  case SA_CMD_TOGGLE_BREAKPOINT: {
    saCmdBkptResWasError = 1;

    // Search line number info for all loaded classes
    jint classCount;
    jclass* classes;

    jvmdiError glcRes = jvmdi->GetLoadedClasses(&classCount, &classes);
    if (glcRes != JVMDI_ERROR_NONE) {
      reportErrorToSA("Error %d while getting loaded classes", glcRes);
      return;
    }
    JvmdiRefListDeallocator rld(env, (jobject*) classes, classCount);

    bool done = false;
    bool gotOne = false;
    jclass targetClass;
    jmethodID targetMethod;
    jlocation targetLocation;
    jint targetLineNumber;

    for (int i = 0; i < classCount && !done; i++) {
      fflush(stderr);
      jclass clazz = classes[i];
      char* srcName;
      jvmdiError sfnRes = jvmdi->GetSourceFileName(clazz, &srcName);
      if (sfnRes == JVMDI_ERROR_NONE) {
        JvmdiDeallocator de1(srcName);
        if (!strcmp(srcName, saCmdBkptSrcFileName)) {
          // Got a match. Now see whether the package name of the class also matches
          char* clazzName;
          jvmdiError sigRes = jvmdi->GetClassSignature(clazz, &clazzName);
          if (sigRes != JVMDI_ERROR_NONE) {
            reportErrorToSA("Error %d while getting a class's signature", sigRes);
            return;
          }
          JvmdiDeallocator de2(clazzName);
          if (packageNameMatches(clazzName + 1, saCmdBkptPkgName)) {
            // Iterate through all methods
            jint methodCount;
            jmethodID* methods;
            if (jvmdi->GetClassMethods(clazz, &methodCount, &methods) != JVMDI_ERROR_NONE) {
              reportErrorToSA("Error while getting methods of class %s", clazzName);
              return;
            }
            JvmdiDeallocator de3(methods);
            for (int j = 0; j < methodCount && !done; j++) {
              jmethodID m = methods[j];
              jint entryCount;
              JVMDI_line_number_entry* table;
              jvmdiError lnRes = jvmdi->GetLineNumberTable(clazz, m, &entryCount, &table);
              if (lnRes == JVMDI_ERROR_NONE) {
                JvmdiDeallocator de4(table);
                // Look for line number greater than or equal to requested line
                for (int k = 0; k < entryCount && !done; k++) {
                  JVMDI_line_number_entry& entry = table[k];
                  if (entry.line_number >= saCmdBkptLineNumber &&
                      (!gotOne || entry.line_number < targetLineNumber)) {
                    gotOne = true;
                    targetClass = clazz;
                    targetMethod = m;
                    targetLocation = entry.start_location;
                    targetLineNumber = entry.line_number;
                    done = (targetLineNumber == saCmdBkptLineNumber);
                  }
                }
              } else if (lnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
                reportErrorToSA("Unexpected error %d while fetching line number table", lnRes);
                return;
              }
            }
          }
        }
      } else if (sfnRes != JVMDI_ERROR_ABSENT_INFORMATION) {
        reportErrorToSA("Unexpected error %d while fetching source file name", sfnRes);
        return;
      }
    }

    bool wasSet = true;
    if (gotOne) {
      // Really toggle this breakpoint
      jvmdiError bpRes;
      bpRes = jvmdi->SetBreakpoint(targetClass, targetMethod, targetLocation);
      if (bpRes == JVMDI_ERROR_DUPLICATE) {
        bpRes = jvmdi->ClearBreakpoint(targetClass, targetMethod, targetLocation);
        wasSet = false;
      }
      if (bpRes != JVMDI_ERROR_NONE) {
        reportErrorToSA("Unexpected error %d while setting or clearing breakpoint at bci %d, line %d",
                        bpRes, targetLocation, targetLineNumber);
        return;
      }
    } else {
      saCmdBkptResWasError = 0;
      reportErrorToSA("No debug information found covering this line");
      return;
    }

    // Provide result
    saCmdBkptResLineNumber = targetLineNumber;
    saCmdBkptResBCI        = targetLocation;
    saCmdBkptResWasSet     = (wasSet ? 1 : 0);
    {
      char* methodName;
      char* methodSig;
      if (jvmdi->GetMethodName(targetClass, targetMethod, &methodName, &methodSig)
          == JVMDI_ERROR_NONE) {
        JvmdiDeallocator mnd(methodName);
        JvmdiDeallocator msd(methodSig);
        strncpy(saCmdBkptResMethodName, methodName, SA_CMD_BUF_SIZE);
        strncpy(saCmdBkptResMethodSig,  methodSig, SA_CMD_BUF_SIZE);
      } else {
        strncpy(saCmdBkptResMethodName, "<error>", SA_CMD_BUF_SIZE);
        strncpy(saCmdBkptResMethodSig,  "<error>", SA_CMD_BUF_SIZE);
      }
    }
    break;
  }

  default:
    reportErrorToSA("Command %d not yet supported", saCmdType);
    return;
  }

  // Successful command execution
  saCmdResult = 0;
  saCmdPending = 0;
}

static void
saCommandThread(void *arg) {
  JNIEnv* env = NULL;
  if (jvm->GetEnv((void **) &env, JNI_VERSION_1_2) != JNI_OK) {
    stop("Error while starting Serviceability Agent "
         "command thread: could not get JNI environment");
  }

  while (1) {
    // Wait for command
    while (!saCmdPending) {
      SLEEP();
    }

    executeOneCommand(env);
  }
}

static void
saEventHook(JNIEnv *env, JVMDI_Event *event)
{
  MonitorLocker ml(eventLock);

  saEventKind = event->kind;

  if (event->kind == JVMDI_EVENT_VM_INIT) {
    // Create event lock
    if (jvmdi->CreateRawMonitor("Serviceability Agent Event Lock", &eventLock)
        != JVMDI_ERROR_NONE) {
      stop("Unable to create Serviceability Agent's event lock");
    }
    // Start thread which receives commands from the SA.
    jclass threadClass = env->FindClass("java/lang/Thread");
    if (threadClass == NULL) stop("Unable to find class java/lang/Thread");
    jstring threadName = env->NewStringUTF("Serviceability Agent Command Thread");
    if (threadName == NULL) stop("Unable to allocate debug thread name");
    jmethodID ctor = env->GetMethodID(threadClass, "<init>", "(Ljava/lang/String;)V");
    if (ctor == NULL) stop("Unable to find appropriate constructor for java/lang/Thread");
    // Allocate thread object
    jthread thr = (jthread) env->NewObject(threadClass, ctor, threadName);
    if (thr == NULL) stop("Unable to allocate debug thread's java/lang/Thread instance");
    // Remember which thread this is
    debugThreadObj = env->NewGlobalRef(thr);
    if (debugThreadObj == NULL) stop("Unable to allocate global ref for debug thread object");
    // Start thread
    jvmdiError err;
    if ((err = jvmdi->RunDebugThread(thr, &saCommandThread, NULL, JVMDI_THREAD_NORM_PRIORITY))
        != JVMDI_ERROR_NONE) {
      char buf[256];
      sprintf(buf, "Error %d while starting debug thread", err);
      stop(buf);
    }
    // OK, initialization is done
    return;
  }

  if (!saAttached) {
    return;
  }

  switch (event->kind) {
  case JVMDI_EVENT_EXCEPTION: {
    fprintf(stderr, "SA: Exception thrown -- ignoring\n");
    saExceptionThread        = event->u.exception.thread;
    saExceptionClass         = event->u.exception.clazz;
    saExceptionMethod        = event->u.exception.method;
    saExceptionLocation      = event->u.exception.location;
    saExceptionException     = event->u.exception.exception;
    saExceptionCatchClass    = event->u.exception.catch_clazz;
    saExceptionCatchClass    = event->u.exception.catch_clazz;
    saExceptionCatchMethod   = event->u.exception.catch_method;
    saExceptionCatchLocation = event->u.exception.catch_location;
    //    saEventPending = 1;
    break;
  }

  case JVMDI_EVENT_BREAKPOINT: {
    saBreakpointThread       = event->u.breakpoint.thread;
    saBreakpointClass        = event->u.breakpoint.clazz;
    saBreakpointMethod       = event->u.breakpoint.method;
    saBreakpointLocation     = event->u.breakpoint.location;
    saEventPending = 1;
    break;
  }

  default:
    break;
  }

  while (saAttached && saEventPending) {
    SLEEP();
  }
}

extern "C" {
JNIEXPORT jint JNICALL
JVM_OnLoad(JavaVM *vm, char *options, void *reserved)
{
  jvm = vm;
  if (jvm->GetEnv((void**) &jvmdi, JVMDI_VERSION_1) != JNI_OK) {
    return -1;
  }
  if (jvmdi->SetEventHook(&saEventHook) != JVMDI_ERROR_NONE) {
    return -1;
  }
  return 0;
}
};