Repository navigation
Expand file tree
/
Copy pathtracer.cpp
More file actions
176 lines (145 loc) · 5.82 KB
/
Copy pathtracer.cpp
File metadata and controls
176 lines (145 loc) · 5.82 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
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
* Copyright (c) 2026 Gauravsingh Sisodia.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Gauravsingh Sisodia - initial implementation
******************************************************************************/
/*
* JVMTI agent for generating dynamic call graphs
*
* Build:
* g++ -O3 -fPIC -shared -o tracer.so \
* -I/usr/lib/jvm/java-openjdk/include \
* -I/usr/lib/jvm/java-openjdk/include/linux \
* tracer.cpp
*
* Usage:
* java -agentpath:./tracer.so=dynamic_call_graph.log \
* -jar dacapo-23.11-MR2-chopin.jar <benchmark> -s small
*
* The agent records method invocation events and writes them to the
* specified log file. The resulting trace can be converted to GXL
* using Trace2GXL.java from the TamiFlex benchmarks repository:
*
* https://github.com/secure-software-engineering/tamiflex.benchmarks/tree/master/jvmti-tracer
*/
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_set>
#include <mutex>
#include <jvmti.h>
static std::ofstream out_file;
// For unique edges
static std::unordered_set<std::string> seen_edges;
static std::mutex edge_mutex;
static void check_jvmti_error(jvmtiEnv *jvmti, jvmtiError error, const char *msg) {
if (error != JVMTI_ERROR_NONE) {
char *name = nullptr;
jvmti->GetErrorName(error, &name);
std::cerr << "ERROR: JVMTI: " << error << "(" << (name == nullptr ? "Unknown" : name) << "): " << (msg == nullptr ? "" : msg) << std::endl;
if (name != nullptr) {
jvmti->Deallocate((unsigned char*)name);
}
}
}
static const int CALL_CHAIN_LENGTH = 2;
void JNICALL method_entry_callback(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jmethodID mid) {
jvmtiError error;
int depth;
char *method_names[CALL_CHAIN_LENGTH] = {nullptr, nullptr};
char *method_signatures[CALL_CHAIN_LENGTH] = {nullptr, nullptr};
char *class_signatures[CALL_CHAIN_LENGTH] = {nullptr, nullptr};
int frames_found = 0;
for (depth = 0; depth < CALL_CHAIN_LENGTH; depth++) {
jmethodID method;
jlocation location;
jclass declaring_class;
if (jvmti->GetFrameLocation(thread, depth, &method, &location) == JVMTI_ERROR_NO_MORE_FRAMES) {
break;
}
jvmti->GetMethodDeclaringClass(method, &declaring_class);
jvmti->GetClassSignature(declaring_class, &class_signatures[depth], nullptr);
jni->DeleteLocalRef(declaring_class);
jvmti->GetMethodName(method, &method_names[depth], &method_signatures[depth], nullptr);
frames_found++;
}
if (frames_found > 0) {
std::string edge_str = "CALL\t";
for (depth = 0; depth < CALL_CHAIN_LENGTH; depth++) {
if (depth < frames_found) {
edge_str += class_signatures[depth];
edge_str += '\t';
edge_str += method_names[depth];
edge_str += '\t';
edge_str += method_signatures[depth];
} else {
edge_str += "ROOT";
}
if (depth != CALL_CHAIN_LENGTH - 1) {
edge_str += '\t';
}
}
{
std::lock_guard<std::mutex> lock(edge_mutex);
if (seen_edges.insert(edge_str).second) {
out_file << edge_str << "\n";
}
}
}
for (depth = 0; depth < frames_found; depth++) {
jvmti->Deallocate((unsigned char*) method_names[depth]);
jvmti->Deallocate((unsigned char*) method_signatures[depth]);
jvmti->Deallocate((unsigned char*) class_signatures[depth]);
}
}
void JNICALL vm_death_callback(jvmtiEnv *jvmti, JNIEnv* jni) {
// Wait briefly to allow threads to finish writing their final edges
}
static const jvmtiEventCallbacks callbacks = {
.VMDeath = &vm_death_callback,
.MethodEntry = &method_entry_callback,
};
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
jvmtiEnv *jvmti = nullptr;
jvmtiCapabilities capabilities;
jvmtiError error;
if (options == nullptr) {
std::cerr << "This agent requires the following option: <file>." << std::endl;
return JNI_ERR;
}
out_file.open(options, std::ios::out | std::ios::trunc);
if (!out_file.is_open()) {
std::cerr << "Couldn't open file " << options << " for writing" << std::endl;
return JNI_ERR;
}
if (jvm->GetEnv((void **) &jvmti, JVMTI_VERSION) != JNI_OK) {
std::cerr << "Couldn't get JVMTI environment" << std::endl;
return JNI_ERR;
}
error = jvmti->GetCapabilities(&capabilities);
check_jvmti_error(jvmti, error, "Couldn't get capabilities");
capabilities.can_generate_method_entry_events = 1;
error = jvmti->AddCapabilities(&capabilities);
check_jvmti_error(jvmti, error, "Couldn't add capabilities");
jvmtiEventCallbacks cb = {0};
cb.MethodEntry = &method_entry_callback;
cb.VMDeath = &vm_death_callback;
error = jvmti->SetEventCallbacks(&cb, sizeof(cb));
check_jvmti_error(jvmti, error, "Couldn't set event callbacks");
error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, nullptr);
check_jvmti_error(jvmti, error, "Couldn't enable notification on method entry");
error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, nullptr);
check_jvmti_error(jvmti, error, "Couldn't enable notification on VM death");
return JNI_OK;
}
JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm) {
if (out_file.is_open()) {
out_file.flush();
out_file.close();
}
}