1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30  */
  31 
  32 /*
  33  * This source code is provided to illustrate the usage of a given feature
  34  * or technique and has been deliberately simplified. Additional steps
  35  * required for a production-quality application, such as security checks,
  36  * input validation and proper error handling, might not be present in
  37  * this sample code.
  38  */
  39 
  40 
  41 /* JVMTI tag definitions. */
  42 
  43 /*
  44  * JVMTI tags are jlongs (64 bits) and how the hprof information is
  45  *   turned into a tag and/or extracted from a tag is here.
  46  *
  47  * Currently a special TAG_CHECK is placed in the high order 32 bits of
  48  *    the tag as a check.
  49  *
  50  */
  51 
  52 #include "hprof.h"
  53 
  54 #define TAG_CHECK 0xfad4dead
  55 
  56 jlong
  57 tag_create(ObjectIndex object_index)
  58 {
  59     jlong               tag;
  60 
  61     HPROF_ASSERT(object_index != 0);
  62     tag = TAG_CHECK;
  63     tag = (tag << 32) | object_index;
  64     return tag;
  65 }
  66 
  67 ObjectIndex
  68 tag_extract(jlong tag)
  69 {
  70     HPROF_ASSERT(tag != (jlong)0);
  71     if ( ((tag >> 32) & 0xFFFFFFFF) != TAG_CHECK) {
  72         HPROF_ERROR(JNI_TRUE, "JVMTI tag value is not 0 and missing TAG_CHECK");
  73     }
  74     return  (ObjectIndex)(tag & 0xFFFFFFFF);
  75 }
  76 
  77 /* Tag a new jobject */
  78 void
  79 tag_new_object(jobject object, ObjectKind kind, SerialNumber thread_serial_num,
  80                 jint size, SiteIndex site_index)
  81 {
  82     ObjectIndex  object_index;
  83     jlong        tag;
  84 
  85     HPROF_ASSERT(site_index!=0);
  86     /* New object for this site. */
  87     object_index = object_new(site_index, size, kind, thread_serial_num);
  88     /* Create and set the tag. */
  89     tag = tag_create(object_index);
  90     setTag(object, tag);
  91     LOG3("tag_new_object", "tag", (int)tag);
  92 }
  93 
  94 /* Tag a jclass jobject if it hasn't been tagged. */
  95 void
  96 tag_class(JNIEnv *env, jclass klass, ClassIndex cnum,
  97                 SerialNumber thread_serial_num, SiteIndex site_index)
  98 {
  99     ObjectIndex object_index;
 100 
 101     /* If the ClassIndex has an ObjectIndex, then we have tagged it. */
 102     object_index = class_get_object_index(cnum);
 103 
 104     if ( object_index == 0 ) {
 105         jint        size;
 106         jlong        tag;
 107 
 108         HPROF_ASSERT(site_index!=0);
 109 
 110         /* If we don't know the size of a java.lang.Class object, get it */
 111         size =  gdata->system_class_size;
 112         if ( size == 0 ) {
 113             size  = (jint)getObjectSize(klass);
 114             gdata->system_class_size = size;
 115         }
 116 
 117         /* Tag this java.lang.Class object if it hasn't been already */
 118         tag = getTag(klass);
 119         if ( tag == (jlong)0 ) {
 120             /* New object for this site. */
 121             object_index = object_new(site_index, size, OBJECT_CLASS,
 122                                         thread_serial_num);
 123             /* Create and set the tag. */
 124             tag = tag_create(object_index);
 125             setTag(klass, tag);
 126         } else {
 127             /* Get the ObjectIndex from the tag. */
 128             object_index = tag_extract(tag);
 129         }
 130 
 131         /* Record this object index in the Class table */
 132         class_set_object_index(cnum, object_index);
 133     }
 134 }