/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
#include "stdlib.h"
#include "mtrace.h"
#include "java_crw_demo.h"
/* ------------------------------------------------------------------- */
/* Some constant maximum sizes */
* We assume the Java class whose static methods we will be calling
* looks like:
*
* public class Mtrace {
* private static int engaged;
* private static native void _method_entry(Object thr, int cnum, int mnum);
* public static void method_entry(int cnum, int mnum)
* {
* if ( engaged != 0 ) {
* _method_entry(Thread.currentThread(), cnum, mnum);
* }
* }
* private static native void _method_exit(Object thr, int cnum, int mnum);
* public static void method_exit(int cnum, int mnum)
* {
* if ( engaged != 0 ) {
* _method_exit(Thread.currentThread(), cnum, mnum);
* }
* }
* }
*
* The engaged field allows us to inject all classes (even system classes)
* and delay the actual calls to the native code until the VM has reached
* a safe time to call native methods (Past the JVMTI VM_START event).
*
*/
/* C macros to create strings from tokens */
#define _STRING(s) #s
/* ------------------------------------------------------------------- */
/* Data structure to hold method and class information in agent */
typedef struct MethodInfo {
} MethodInfo;
typedef struct ClassInfo {
} ClassInfo;
/* Global agent data structure */
typedef struct {
/* JVMTI Environment */
/* Data access Lock */
/* Options */
char *include;
char *exclude;
int max_count;
/* ClassInfo Table */
/* Enter a critical section by doing a JVMTI Raw Monitor Enter */
static void
{
}
/* Exit a critical section by doing a JVMTI Raw Monitor Exit */
static void
{
}
/* Get a name for a jthread */
static void
{
/* Make sure the stack variables are garbage free */
/* Assume the name is unknown for now */
/* Get the thread information, which includes the name */
/* The thread might not have a name, be careful here. */
int len;
/* Copy the thread name into tname if it will fit */
}
/* Every string allocated by JVMTI needs to be freed */
}
}
/* Qsort class compare routine */
static int
{
return 0;
}
/* Qsort method compare routine */
static int
{
return 0;
}
/* Callback from java_crw_demo() that gives us mnum mappings */
static void
{
int mnum;
fatal_error("ERROR: Class number out of range\n");
}
if ( mcount == 0 ) {
return;
}
fatal_error("ERROR: Out of malloc memory\n");
}
fatal_error("ERROR: Out of malloc memory\n");
}
fatal_error("ERROR: Out of malloc memory\n");
}
}
}
/* Java Native Method for entry */
static void
{
/* It's possible we get here right after VmDeath event, be careful */
if ( !gdata->vm_is_dead ) {
fatal_error("ERROR: Class number out of range\n");
}
fatal_error("ERROR: Method number out of range\n");
}
}
}
}
/* Java Native Method for exit */
static void
{
/* It's possible we get here right after VmDeath event, be careful */
if ( !gdata->vm_is_dead ) {
fatal_error("ERROR: Class number out of range\n");
}
fatal_error("ERROR: Method number out of range\n");
}
}
}
}
/* Callback for JVMTI_EVENT_VM_START */
static void JNICALL
{
int rc;
/* Java Native Methods for class */
(void*)&MTRACE_native_entry},
(void*)&MTRACE_native_exit}
};
/* The VM has started. */
stdout_message("VMStart\n");
/* Register Natives for class whose methods we use */
fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
}
if ( rc != 0 ) {
fatal_error("ERROR: JNI: Cannot register native methods for %s\n",
}
/* Engage calls. */
fatal_error("ERROR: JNI: Cannot get field from %s\n",
}
/* Indicate VM has started */
}
/* Callback for JVMTI_EVENT_VM_INIT */
static void JNICALL
{
int i;
/* The VM has started. */
/* The VM is now initialized, at this time we make our requests
* for additional events.
*/
for( i=0; i < (int)(sizeof(events)/sizeof(jvmtiEvent)); i++) {
/* Setup event notification modes */
}
}
/* Callback for JVMTI_EVENT_VM_DEATH */
static void JNICALL
{
/* The VM has died. */
stdout_message("VMDeath\n");
/* Disengage calls in MTRACE_class. */
fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
}
fatal_error("ERROR: JNI: Cannot get field from %s\n",
}
/* The critical section here is important to hold back the VM death
* until all other callbacks have completed.
*/
/* Since this critical section could be holding up other threads
* in other event callbacks, we need to indicate that the VM is
* dead so that the other callbacks can short circuit their work.
* We don't expect any further events after VmDeath but we do need
* to be careful that existing threads might be in our own agent
* callback code.
*/
/* Dump out stats */
stdout_message("Begin Class Stats\n");
int cnum;
/* Sort table (in place) by number of method calls into class. */
/* Note: Do not use this table after this qsort! */
&class_compar);
/* Dump out gdata->max_count most called classes */
cnum-- ) {
int mnum;
/* Sort method table (in place) by number of method calls. */
/* Note: Do not use this table after this qsort! */
stdout_message("\tMethod %s %s %d calls %d returns\n",
}
}
}
stdout_message("End Class Stats\n");
}
/* Callback for JVMTI_EVENT_THREAD_START */
static void JNICALL
{
/* It's possible we get here right after VmDeath event, be careful */
if ( !gdata->vm_is_dead ) {
}
}
/* Callback for JVMTI_EVENT_THREAD_END */
static void JNICALL
{
/* It's possible we get here right after VmDeath event, be careful */
if ( !gdata->vm_is_dead ) {
}
}
/* Callback for JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
static void JNICALL
{
/* It's possible we get here right after VmDeath event, be careful */
if ( !gdata->vm_is_dead ) {
const char *classname;
/* Name could be NULL */
NULL);
fatal_error("ERROR: No classname inside classfile\n");
}
} else {
fatal_error("ERROR: Out of malloc memory\n");
}
}
*new_class_data_len = 0;
*new_class_data = NULL;
/* The tracker class itself? */
int system_class;
unsigned char *new_image;
long new_length;
/* Get unique number for every class file image loaded */
/* Save away class information */
} else {
}
fatal_error("ERROR: Out of malloc memory\n");
}
fatal_error("ERROR: Out of malloc memory\n");
}
/* Is it a system class? If the class load is before VmStart
* then we will consider it a system class that should
* be treated carefully. (See java_crw_demo)
*/
system_class = 0;
if ( !gdata->vm_is_started ) {
system_class = 1;
}
new_length = 0;
NULL,
/* If we got back a new class image, return it back as "the"
* new class image. This must be JVMTI Allocate space.
*/
if ( new_length > 0 ) {
unsigned char *jvmti_space;
}
/* Always free up the space we get from java_crw_demo() */
}
}
}
}
/* Parse the options for this mtrace agent */
static void
{
char *next;
/* Parse options and set flags in gdata */
return;
}
/* Get the first token from the options string. */
/* While not at the end of the options string, process this option. */
stdout_message("The mtrace JVMTI demo agent\n");
stdout_message("\n");
stdout_message(" java -agent:mtrace[=options] ...\n");
stdout_message("\n");
stdout_message("The options are comma separated:\n");
stdout_message("\t help\t\t\t Print help information\n");
stdout_message("\t max=n\t\t Only list top n classes\n");
stdout_message("\t include=item\t\t Only these classes/methods\n");
stdout_message("\t exclude=item\t\t Exclude these classes/methods\n");
stdout_message("\n");
stdout_message("item\t Qualified class and/or method names\n");
stdout_message("\t\t e.g. (*.<init>;Foobar.method;sun.*)\n");
stdout_message("\n");
exit(0);
/* Get the numeric option */
/* Check for token scan error */
fatal_error("ERROR: max=n option error\n");
}
/* Save numeric value */
int used;
int maxlen;
used = 0;
} else {
gdata->include = (char*)
}
fatal_error("ERROR: Out of malloc memory\n");
}
/* Add this item to the list */
/* Check for token scan error */
fatal_error("ERROR: include option error\n");
}
int used;
int maxlen;
used = 0;
} else {
}
fatal_error("ERROR: Out of malloc memory\n");
}
/* Add this item to the list */
/* Check for token scan error */
fatal_error("ERROR: exclude option error\n");
}
} else if ( token[0]!=0 ) {
/* We got a non-empty token and we don't know what it is. */
}
/* Get the next token (returns NULL if there are no more) */
}
}
/* Agent_OnLoad: This is called immediately after the shared library is
* loaded. This is the first code executed.
*/
{
/* Setup initial global agent data area
* We need to make sure that we are able to cleanup after ourselves
* so anything allocated in this library needs to be freed in
* the Agent_OnUnload() function.
*/
/* First thing we need to do is get the jvmtiEnv* or JVMTI environment */
/* This means that the VM was unable to obtain this version of the
* JVMTI interface, this is a fatal error.
*/
fatal_error("ERROR: Unable to access JVMTI Version 1 (0x%x),"
" is your JDK a 5.0 or newer version?"
" JNIEnv's GetEnv() returned %d\n",
}
/* Here we save the jvmtiEnv* for Agent_OnUnload(). */
/* Parse any options supplied on java command line */
/* Immediately after getting the jvmtiEnv* we need to ask for the
* capabilities this agent will need. In this case we need to make
* sure that we can get all class load hooks.
*/
/* Next we need to provide the pointers to the callback functions to
* to this jvmtiEnv*
*/
/* JVMTI_EVENT_VM_START */
/* JVMTI_EVENT_VM_INIT */
/* JVMTI_EVENT_VM_DEATH */
/* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
/* JVMTI_EVENT_THREAD_START */
/* JVMTI_EVENT_THREAD_END */
/* At first the only initial events we are interested in are VM
* initialization, VM death, and Class File Loads.
* Once the VM is initialized we will request more events.
*/
/* Here we create a raw monitor for our use in this agent to
* protect critical sections of code.
*/
/* Add demo jar file to boot classpath */
/* We return JNI_OK to signify success */
return JNI_OK;
}
/* Agent_OnUnload: This is called immediately before the shared library is
* unloaded. This is the last code executed.
*/
{
}
}
int cnum;
int mnum;
}
}
}
}
}