Skip to content

Instantly share code, notes, and snippets.

@apangin
Created February 6, 2016 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apangin/d3204345f3b08e6d0138 to your computer and use it in GitHub Desktop.
Save apangin/d3204345f3b08e6d0138 to your computer and use it in GitHub Desktop.
Dump all Java strings using JVMTI
#include <jvmti.h>
#include <stdio.h>
#include <wchar.h>
static jvmtiEnv* jvmti;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
(*vm)->GetEnv(vm, (void**)&jvmti, JVMTI_VERSION_1_0);
jvmtiCapabilities capabilities = {0};
capabilities.can_tag_objects = 1;
jvmtiError err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
return JNI_VERSION_1_4;
}
jint JNICALL stringCallback(jlong class_tag, jlong size, jlong* tag_ptr,
const jchar* value, jint value_length, void* user_data) {
wprintf(L"%.*s\n", value_length, value);
return 0;
}
JNIEXPORT void JNICALL Java_HeapIterator_printStrings(JNIEnv* env, jclass cls) {
jvmtiHeapCallbacks callbacks = {NULL, NULL, NULL, NULL, stringCallback};
(*jvmti)->IterateThroughHeap(jvmti, 0, NULL, &callbacks, NULL);
}
public class HeapIterator {
static {
System.loadLibrary("heapIterator");
}
private static native void printStrings();
public static void main(String[] args) throws Exception {
printStrings();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment