I'm working on one OpenJDK project, where I need to get the call stack of running aplication (Jav program) inside OpenJDK source code.
Explanation: For example: When I run the following java program with my OpenJDK build
class Test{
public static void main(String[] args){
System.out.println("Testing Call Stack Program");
}
}
Here when the control enters println
statement, internally in the OpenJDK it hits
writeBytes()
.
void
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
jint off, jint len, jboolean append, jfieldID fid)
{
**//RETRIEVE THE CALL STACK OF APPLICATION and NOT THE CURRENT CALL STACK OF JDK**
jint n;
char stackBuf[BUF_SIZE];
char *buf = NULL;
FD fd;
if (IS_NULL(bytes)) {
JNU_ThrowNullPointerException(env, NULL);
return;
}
if (outOfBounds(env, off, len, bytes)) {
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
return;
}
if (len == 0) {
return;
} else if (len > BUF_SIZE) {
buf = malloc(len);
if (buf == NULL) {
JNU_ThrowOutOfMemoryError(env, NULL);
return;
}
} else {
buf = stackBuf;
}
(*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
if (!(*env)->ExceptionOccurred(env)) {
off = 0;
while (len > 0) {
fd = GET_FD(this, fid);
if (fd == -1) {
JNU_ThrowIOException(env, "Stream Closed");
break;
}
if (append == JNI_TRUE) {
n = IO_Append(fd, buf+off, len);
} else {
n = IO_Write(fd, buf+off, len);
}
if (n == -1) {
JNU_ThrowIOExceptionWithLastError(env, "Write error");
break;
}
off += n;
len -= n;
}
}
if (buf != stackBuf) {
free(buf);
}
}
Here I need to get the application level call stack (that is I need to get the call stack of System.out.println()
which is main()
and then println()
). How can we get the call stack from within the OpenJDK source?. Is that even possible to get the user level call stack from JDK source?.