-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCallbacks.cc
More file actions
164 lines (132 loc) · 5.05 KB
/
Callbacks.cc
File metadata and controls
164 lines (132 loc) · 5.05 KB
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
#include "Callbacks.h"
#include "InstrumentBytecode.h"
#include "ETProxy_class.h"
#include "InstrumentFlag_class.h"
#include "ObjectSize.h"
#include <cassert>
#include <cstring>
#include <iostream>
#include <fstream>
#include <jnif.hpp>
using namespace std;
void JNICALL onMethodEntry(jvmtiEnv *jvmti, JNIEnv *jni, jthread th, jmethodID method)
{
unsigned char *name;
// not safe, but I think no one has a 201 byte class name!
jvmtiError error = jvmti->Allocate(200, &name);
assert(!error);
error = jvmti->GetMethodName(method, (char**) &name, nullptr, nullptr);
assert(!error);
// do this until we get to main
if (string((char*) name) == "main") {
#ifdef DEBUG
cerr << "Finally we got to main" << endl;
#endif
try {
jclass etProxyClass = jni->FindClass("ETProxy");
jclass runnableClass = jni->FindClass("ETProxy.ShutdownRunnable");
jfieldID atMain = jni->GetStaticFieldID(etProxyClass, "atMain", "Z");
jni->SetStaticBooleanField(etProxyClass, atMain, (jboolean) true);
} catch (exception &e) {
cerr << "Unable to load ETProxy and related classes." << endl;
cerr << e.what() << endl;
assert(false);
}
// reset callbacks
jvmtiEventCallbacks callbacks;
(void) memset(&callbacks, 0, sizeof(callbacks));
callbacks.VMStart = &onVMStart;
callbacks.VMInit = &onVMInit;
callbacks.ClassFileLoadHook = &onClassFileLoad;
callbacks.VMDeath = &flushBuffers;
jvmtiError error = jvmti->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks));
assert(!error);
}
error = jvmti->Deallocate(name);
assert(!error);
}
void JNICALL onVMInit(jvmtiEnv *jvmti, JNIEnv *jni, jthread th)
{
#ifdef DEBUG
cerr << "JVM initialized" << endl;
#endif
isReady = true;
}
void JNICALL onVMStart(jvmtiEnv *jvmti, JNIEnv *jni)
{
#ifdef DEBUG
cerr << "JVM started" << endl;
#endif
jni->DefineClass("InstrumentFlag", NULL, (jbyte *) InstrumentFlag_class, (jsize) InstrumentFlag_class_len);
jclass etProxyClass = jni->DefineClass("ETProxy", NULL, (jbyte *) ETProxy_class, (jsize) ETProxy_class_len);
JNINativeMethod jniMethod;
jniMethod.name = (char *) "getObjectSize";
jniMethod.signature = (char *) "(Ljava/lang/Object;)I";
jniMethod.fnPtr = (void *) jniObjectSize;
jint suc = jni->RegisterNatives(etProxyClass, &jniMethod, 1);
assert(suc == 0);
}
void JNICALL onClassFileLoad(jvmtiEnv *jvmti,
JNIEnv *jni,
jclass class_being_redefined,
jobject loader,
const char *class_name,
jobject protection_domain,
jint class_data_len,
const unsigned char *class_data,
jint *new_class_data_len,
unsigned char **new_class_data)
{
#ifdef DEBUG
cerr << "Loading class: " << string(class_name) << endl;
cerr << "id " << classTable.mapOrFind(string(class_name)) << ": " << string(class_name) << endl;
#endif
if (!isReady || string(class_name).find("java/util") == 0) { return; }
if (class_name == nullptr) {
// maybe should throw exception instead; don't yet know what exactly to do
return;
}
if (string(class_name).find("sun") == 0) {
return;
}
try {
#ifdef DEBUG
cerr << "About to instrument: " << string(class_name) << endl;
#endif
instrumentClass(jvmti, jni, loader, (unsigned char *) class_data, class_data_len,
new_class_data_len, new_class_data);
#ifdef DEBUG
cerr << "Instrumentation of " << string(class_name) << " successful" << endl;
#endif
} catch (exception &e) {
// cerr << "Exception!: " << e.what() << endl;
throw;
}
}
static void flushMethodTable(MethodTable &methodTable, ofstream &methodDataF)
{
for (auto it = methodTable.begin(); it != methodTable.end(); ++it) {
long methodID = it->first;
string className = (it->second).first;
string methodName = (it->second).second;
methodDataF << methodID << ", " << className << ", " << methodName << endl;
}
}
void JNICALL flushBuffers(jvmtiEnv *jvmti, JNIEnv *jni)
{
// Flush the method call timestamp buffer first
jclass proxyClass = jni->FindClass("ETProxy");
jmethodID flushBuffer = jni->GetStaticMethodID(proxyClass, "flushBuffer", "()V");
jni->CallStaticVoidMethod(proxyClass, flushBuffer);
ofstream methodDataF, classDataF, fieldDataF;
methodDataF.open("methods.list");
classDataF.open("classes.list");
fieldDataF.open("fields.list");
// Then flush the rest of the timestamp table
flushMethodTable(methodTable, methodDataF);
classTable.dumpTable(classDataF);
fieldTable.dumpTable(fieldDataF);
methodDataF.close();
classDataF.close();
fieldDataF.close();
}