1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_VMSTRUCTS_HPP
  26 #define SHARE_VM_RUNTIME_VMSTRUCTS_HPP
  27 
  28 #include "utilities/debug.hpp"
  29 #ifdef COMPILER1
  30 #include "c1/c1_Runtime1.hpp"
  31 #endif
  32 
  33 // This table encapsulates the debugging information required by the
  34 // serviceability agent in order to run. Specifically, we need to
  35 // understand the layout of certain C data structures (offsets, in
  36 // bytes, of their fields.)
  37 //
  38 // There are alternatives for the design of this mechanism, including
  39 // parsing platform-specific debugging symbols from a debug build into
  40 // a program database. While this current mechanism can be considered
  41 // to be a workaround for the inability to debug arbitrary C and C++
  42 // programs at the present time, it does have certain advantages.
  43 // First, it is platform-independent, which will vastly simplify the
  44 // initial bringup of the system both now and on future platforms.
  45 // Second, it is embedded within the VM, as opposed to being in a
  46 // separate program database; experience has shown that whenever
  47 // portions of a system are decoupled, version skew is problematic.
  48 // Third, generating a program database, for example for a product
  49 // build, would probably require two builds to be done: the desired
  50 // product build as well as an intermediary build with the PRODUCT
  51 // flag turned on but also compiled with -g, leading to a doubling of
  52 // the time required to get a serviceability agent-debuggable product
  53 // build. Fourth, and very significantly, this table probably
  54 // preserves more information about field types than stabs do; for
  55 // example, it preserves the fact that a field is a "jlong" rather
  56 // than transforming the type according to the typedef in jni_md.h,
  57 // which allows the Java-side code to identify "Java-sized" fields in
  58 // C++ data structures. If the symbol parsing mechanism was redone
  59 // using stabs, it might still be necessary to have a table somewhere
  60 // containing this information.
  61 //
  62 // Do not change the sizes or signedness of the integer values in
  63 // these data structures; they are fixed over in the serviceability
  64 // agent's Java code (for bootstrapping).
  65 
  66 typedef struct {
  67   const char* typeName;            // The type name containing the given field (example: "Klass")
  68   const char* fieldName;           // The field name within the type           (example: "_name")
  69   const char* typeString;          // Quoted name of the type of this field (example: "Symbol*";
  70                                    // parsed in Java to ensure type correctness
  71   int32_t  isStatic;               // Indicates whether following field is an offset or an address
  72   uint64_t offset;                 // Offset of field within structure; only used for nonstatic fields
  73   void* address;                   // Address of field; only used for static fields
  74                                    // ("offset" can not be reused because of apparent SparcWorks compiler bug
  75                                    // in generation of initializer data)
  76 } VMStructEntry;
  77 
  78 typedef struct {
  79   const char* typeName;            // Type name (example: "Method")
  80   const char* superclassName;      // Superclass name, or null if none (example: "oopDesc")
  81   int32_t isOopType;               // Does this type represent an oop typedef? (i.e., "Method*" or
  82                                    // "Klass*", but NOT "Method")
  83   int32_t isIntegerType;           // Does this type represent an integer type (of arbitrary size)?
  84   int32_t isUnsigned;              // If so, is it unsigned?
  85   uint64_t size;                   // Size, in bytes, of the type
  86 } VMTypeEntry;
  87 
  88 typedef struct {
  89   const char* name;                // Name of constant (example: "_thread_in_native")
  90   int32_t value;                   // Value of constant
  91 } VMIntConstantEntry;
  92 
  93 typedef struct {
  94   const char* name;                // Name of constant (example: "_thread_in_native")
  95   uint64_t value;                  // Value of constant
  96 } VMLongConstantEntry;
  97 
  98 typedef struct {
  99   const char* name;                // Name of address (example: "SharedRuntime::register_finalizer")
 100   void* value;                     // Value of address
 101 } VMAddressEntry;
 102 
 103 // This class is a friend of most classes, to be able to access
 104 // private fields
 105 class VMStructs {
 106 public:
 107   // The last entry is identified over in the serviceability agent by
 108   // the fact that it has a NULL fieldName
 109   static VMStructEntry localHotSpotVMStructs[];
 110   // The function to get localHotSpotVMStructs length
 111   static size_t localHotSpotVMStructsLength();
 112 
 113   // The last entry is identified over in the serviceability agent by
 114   // the fact that it has a NULL typeName
 115   static VMTypeEntry   localHotSpotVMTypes[];
 116   // The function to get localHotSpotVMTypes length
 117   static size_t localHotSpotVMTypesLength();
 118 
 119   // Table of integer constants required by the serviceability agent.
 120   // The last entry is identified over in the serviceability agent by
 121   // the fact that it has a NULL typeName
 122   static VMIntConstantEntry localHotSpotVMIntConstants[];
 123   // The function to get localHotSpotVMIntConstants length
 124   static size_t localHotSpotVMIntConstantsLength();
 125 
 126   // Table of long constants required by the serviceability agent.
 127   // The last entry is identified over in the serviceability agent by
 128   // the fact that it has a NULL typeName
 129   static VMLongConstantEntry localHotSpotVMLongConstants[];
 130   // The function to get localHotSpotVMIntConstants length
 131   static size_t localHotSpotVMLongConstantsLength();
 132 
 133   /**
 134    * Table of addresses.
 135    */
 136   static VMAddressEntry localHotSpotVMAddresses[];
 137 
 138   // This is used to run any checking code necessary for validation of
 139   // the data structure (debug build only)
 140   static void init();
 141 
 142 #ifndef PRODUCT
 143   // Execute unit tests
 144   static void test();
 145 #endif
 146 
 147 private:
 148   // Look up a type in localHotSpotVMTypes using strcmp() (debug build only).
 149   // Returns 1 if found, 0 if not.
 150   //  debug_only(static int findType(const char* typeName);)
 151   static int findType(const char* typeName);
 152 };
 153 
 154 // This utility macro quotes the passed string
 155 #define QUOTE(x) #x
 156 
 157 //--------------------------------------------------------------------------------
 158 // VMStructEntry macros
 159 //
 160 
 161 // This macro generates a VMStructEntry line for a nonstatic field
 162 #define GENERATE_NONSTATIC_VM_STRUCT_ENTRY(typeName, fieldName, type)              \
 163  { QUOTE(typeName), QUOTE(fieldName), QUOTE(type), 0, offset_of(typeName, fieldName), NULL },
 164 
 165 // This macro generates a VMStructEntry line for a static field
 166 #define GENERATE_STATIC_VM_STRUCT_ENTRY(typeName, fieldName, type)                 \
 167  { QUOTE(typeName), QUOTE(fieldName), QUOTE(type), 1, 0, &typeName::fieldName },
 168 
 169 // This macro generates a VMStructEntry line for a static pointer volatile field,
 170 // e.g.: "static ObjectMonitor * volatile gBlockList;"
 171 #define GENERATE_STATIC_PTR_VOLATILE_VM_STRUCT_ENTRY(typeName, fieldName, type)    \
 172  { QUOTE(typeName), QUOTE(fieldName), QUOTE(type), 1, 0, (void *)&typeName::fieldName },
 173 
 174 // This macro generates a VMStructEntry line for an unchecked
 175 // nonstatic field, in which the size of the type is also specified.
 176 // The type string is given as NULL, indicating an "opaque" type.
 177 #define GENERATE_UNCHECKED_NONSTATIC_VM_STRUCT_ENTRY(typeName, fieldName, size)    \
 178   { QUOTE(typeName), QUOTE(fieldName), NULL, 0, offset_of(typeName, fieldName), NULL },
 179 
 180 // This macro generates a VMStructEntry line for an unchecked
 181 // static field, in which the size of the type is also specified.
 182 // The type string is given as NULL, indicating an "opaque" type.
 183 #define GENERATE_UNCHECKED_STATIC_VM_STRUCT_ENTRY(typeName, fieldName, size)       \
 184  { QUOTE(typeName), QUOTE(fieldName), NULL, 1, 0, (void*) &typeName::fieldName },
 185 
 186 // This macro generates the sentinel value indicating the end of the list
 187 #define GENERATE_VM_STRUCT_LAST_ENTRY() \
 188  { NULL, NULL, NULL, 0, 0, NULL }
 189 
 190 // This macro checks the type of a VMStructEntry by comparing pointer types
 191 #define CHECK_NONSTATIC_VM_STRUCT_ENTRY(typeName, fieldName, type)                 \
 192  {typeName *dummyObj = NULL; type* dummy = &dummyObj->fieldName;                   \
 193   assert(offset_of(typeName, fieldName) < sizeof(typeName), "Illegal nonstatic struct entry, field offset too large"); }
 194 
 195 // This macro checks the type of a volatile VMStructEntry by comparing pointer types
 196 #define CHECK_VOLATILE_NONSTATIC_VM_STRUCT_ENTRY(typeName, fieldName, type)        \
 197  {typedef type dummyvtype; typeName *dummyObj = NULL; volatile dummyvtype* dummy = &dummyObj->fieldName; }
 198 
 199 // This macro checks the type of a static VMStructEntry by comparing pointer types
 200 #define CHECK_STATIC_VM_STRUCT_ENTRY(typeName, fieldName, type)                    \
 201  {type* dummy = &typeName::fieldName; }
 202 
 203 // This macro checks the type of a static pointer volatile VMStructEntry by comparing pointer types,
 204 // e.g.: "static ObjectMonitor * volatile gBlockList;"
 205 #define CHECK_STATIC_PTR_VOLATILE_VM_STRUCT_ENTRY(typeName, fieldName, type)       \
 206  {type volatile * dummy = &typeName::fieldName; }
 207 
 208 // This macro ensures the type of a field and its containing type are
 209 // present in the type table. The assertion string is shorter than
 210 // preferable because (incredibly) of a bug in Solstice NFS client
 211 // which seems to prevent very long lines from compiling. This assertion
 212 // means that an entry in VMStructs::localHotSpotVMStructs[] was not
 213 // found in VMStructs::localHotSpotVMTypes[].
 214 #define ENSURE_FIELD_TYPE_PRESENT(typeName, fieldName, type)                       \
 215  { assert(findType(QUOTE(typeName)) != 0, "type \"" QUOTE(typeName) "\" not found in type table"); \
 216    assert(findType(QUOTE(type)) != 0, "type \"" QUOTE(type) "\" not found in type table"); }
 217 
 218 // This is a no-op macro for unchecked fields
 219 #define CHECK_NO_OP(a, b, c)
 220 
 221 
 222 //--------------------------------------------------------------------------------
 223 // VMTypeEntry macros
 224 //
 225 
 226 #define GENERATE_VM_TYPE_ENTRY(type, superclass) \
 227  { QUOTE(type), QUOTE(superclass), 0, 0, 0, sizeof(type) },
 228 
 229 #define GENERATE_TOPLEVEL_VM_TYPE_ENTRY(type) \
 230  { QUOTE(type), NULL,              0, 0, 0, sizeof(type) },
 231 
 232 #define GENERATE_OOP_VM_TYPE_ENTRY(type) \
 233  { QUOTE(type), NULL,              1, 0, 0, sizeof(type) },
 234 
 235 #define GENERATE_INTEGER_VM_TYPE_ENTRY(type) \
 236  { QUOTE(type), NULL,              0, 1, 0, sizeof(type) },
 237 
 238 #define GENERATE_UNSIGNED_INTEGER_VM_TYPE_ENTRY(type) \
 239  { QUOTE(type), NULL,              0, 1, 1, sizeof(type) },
 240 
 241 #define GENERATE_VM_TYPE_LAST_ENTRY() \
 242  { NULL, NULL, 0, 0, 0, 0 }
 243 
 244 #define CHECK_VM_TYPE_ENTRY(type, superclass) \
 245  { type* dummyObj = NULL; superclass* dummySuperObj = dummyObj; }
 246 
 247 #define CHECK_VM_TYPE_NO_OP(a)
 248 #define CHECK_SINGLE_ARG_VM_TYPE_NO_OP(a)
 249 
 250 
 251 //--------------------------------------------------------------------------------
 252 // VMIntConstantEntry macros
 253 //
 254 
 255 #define GENERATE_VM_INT_CONSTANT_ENTRY(name) \
 256  { QUOTE(name), (int32_t) name },
 257 
 258 #define GENERATE_VM_INT_CONSTANT_WITH_VALUE_ENTRY(name, value) \
 259  { (name), (int32_t)(value) },
 260 
 261 #define GENERATE_PREPROCESSOR_VM_INT_CONSTANT_ENTRY(name, value) \
 262  { name, (int32_t) value },
 263 
 264 // This macro generates the sentinel value indicating the end of the list
 265 #define GENERATE_VM_INT_CONSTANT_LAST_ENTRY() \
 266  { NULL, 0 }
 267 
 268 
 269 //--------------------------------------------------------------------------------
 270 // VMLongConstantEntry macros
 271 //
 272 
 273 #define GENERATE_VM_LONG_CONSTANT_ENTRY(name) \
 274   { QUOTE(name), name },
 275 
 276 #define GENERATE_PREPROCESSOR_VM_LONG_CONSTANT_ENTRY(name, value) \
 277   { name, value },
 278 
 279 // This macro generates the sentinel value indicating the end of the list
 280 #define GENERATE_VM_LONG_CONSTANT_LAST_ENTRY() \
 281  { NULL, 0 }
 282 
 283 
 284 //--------------------------------------------------------------------------------
 285 // VMAddressEntry macros
 286 //
 287 
 288 #define GENERATE_VM_ADDRESS_ENTRY(name) \
 289   { QUOTE(name), (void*) (name) },
 290 
 291 #define GENERATE_PREPROCESSOR_VM_ADDRESS_ENTRY(name, value) \
 292   { name, (void*) (value) },
 293 
 294 #define GENERATE_VM_FUNCTION_ENTRY(name) \
 295   { QUOTE(name), CAST_FROM_FN_PTR(void*, &(name)) },
 296 
 297 // This macro generates the sentinel value indicating the end of the list
 298 #define GENERATE_VM_ADDRESS_LAST_ENTRY() \
 299  { NULL, NULL }
 300 
 301 #endif // SHARE_VM_RUNTIME_VMSTRUCTS_HPP