< prev index next >

src/share/vm/utilities/debug.hpp

Print this page




  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_UTILITIES_DEBUG_HPP
  26 #define SHARE_VM_UTILITIES_DEBUG_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "prims/jvm.h"
  30 
  31 #include <stdarg.h>
  32 
  33 // Simple class to format the ctor arguments into a fixed-sized buffer.
  34 class FormatBufferBase {
  35  protected:
  36   char* _buf;
  37   inline FormatBufferBase(char* buf) : _buf(buf) {}
  38  public:

  39   operator const char *() const { return _buf; }
  40 };
  41 
  42 // Use resource area for buffer
  43 #define BUFSZ 256
  44 class FormatBufferResource : public FormatBufferBase {
  45  public:
  46   FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  47 };
  48 
  49 // Use stack for buffer
  50 template <size_t bufsz = BUFSZ>
  51 class FormatBuffer : public FormatBufferBase {
  52  public:
  53   inline FormatBuffer(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  54   inline void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  55   inline void print(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  56   inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
  57 
  58   char* buffer() { return _buf; }
  59   int size() { return bufsz; }
  60 
  61  private:
  62   FormatBuffer(const FormatBuffer &); // prevent copies
  63   char _buffer[bufsz];
  64 
  65  protected:
  66   inline FormatBuffer();
  67 };
  68 
  69 template <size_t bufsz>
  70 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
  71   va_list argp;
  72   va_start(argp, format);
  73   jio_vsnprintf(_buf, bufsz, format, argp);


  88 }
  89 
  90 template <size_t bufsz>
  91 void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
  92   jio_vsnprintf(_buf, bufsz, format, argp);
  93 }
  94 
  95 template <size_t bufsz>
  96 void FormatBuffer<bufsz>::append(const char* format, ...) {
  97   // Given that the constructor does a vsnprintf we can assume that
  98   // _buf is already initialized.
  99   size_t len = strlen(_buf);
 100   char* buf_end = _buf + len;
 101 
 102   va_list argp;
 103   va_start(argp, format);
 104   jio_vsnprintf(buf_end, bufsz - len, format, argp);
 105   va_end(argp);
 106 }
 107 
 108 // Used to format messages
 109 typedef FormatBuffer<> err_msg;
 110 
 111 // assertions
 112 #ifndef ASSERT
 113 #define vmassert(p, ...)
 114 #else
 115 // Note: message says "assert" rather than "vmassert" for backward
 116 // compatibility with tools that parse/match the message text.
 117 #define vmassert(p, ...)                                                       \
 118 do {                                                                           \
 119   if (!(p)) {                                                                  \
 120     report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \
 121     BREAKPOINT;                                                                \
 122   }                                                                            \
 123 } while (0)
 124 
 125 #endif
 126 
 127 // For backward compatibility.
 128 #define assert(p, ...) vmassert(p, __VA_ARGS__)




  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_UTILITIES_DEBUG_HPP
  26 #define SHARE_VM_UTILITIES_DEBUG_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "prims/jvm.h"
  30 
  31 #include <stdarg.h>
  32 
  33 // Simple class to format the ctor arguments into a fixed-sized buffer.
  34 class FormatBufferBase {
  35  protected:
  36   char* _buf;
  37   inline FormatBufferBase(char* buf) : _buf(buf) {}
  38  public:
  39   static const int BufferSize = 256;
  40   operator const char *() const { return _buf; }
  41 };
  42 
  43 // Use resource area for buffer

  44 class FormatBufferResource : public FormatBufferBase {
  45  public:
  46   FormatBufferResource(const char * format, ...) ATTRIBUTE_PRINTF(2, 3);
  47 };
  48 
  49 // Use stack for buffer
  50 template <size_t bufsz = FormatBufferBase::BufferSize>
  51 class FormatBuffer : public FormatBufferBase {
  52  public:
  53   inline FormatBuffer(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
  54   inline void append(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  55   inline void print(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);
  56   inline void printv(const char* format, va_list ap) ATTRIBUTE_PRINTF(2, 0);
  57 
  58   char* buffer() { return _buf; }
  59   int size() { return bufsz; }
  60 
  61  private:
  62   FormatBuffer(const FormatBuffer &); // prevent copies
  63   char _buffer[bufsz];
  64 
  65  protected:
  66   inline FormatBuffer();
  67 };
  68 
  69 template <size_t bufsz>
  70 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) {
  71   va_list argp;
  72   va_start(argp, format);
  73   jio_vsnprintf(_buf, bufsz, format, argp);


  88 }
  89 
  90 template <size_t bufsz>
  91 void FormatBuffer<bufsz>::printv(const char * format, va_list argp) {
  92   jio_vsnprintf(_buf, bufsz, format, argp);
  93 }
  94 
  95 template <size_t bufsz>
  96 void FormatBuffer<bufsz>::append(const char* format, ...) {
  97   // Given that the constructor does a vsnprintf we can assume that
  98   // _buf is already initialized.
  99   size_t len = strlen(_buf);
 100   char* buf_end = _buf + len;
 101 
 102   va_list argp;
 103   va_start(argp, format);
 104   jio_vsnprintf(buf_end, bufsz - len, format, argp);
 105   va_end(argp);
 106 }
 107 
 108 // Used to format messages.
 109 typedef FormatBuffer<> err_msg;
 110 
 111 // assertions
 112 #ifndef ASSERT
 113 #define vmassert(p, ...)
 114 #else
 115 // Note: message says "assert" rather than "vmassert" for backward
 116 // compatibility with tools that parse/match the message text.
 117 #define vmassert(p, ...)                                                       \
 118 do {                                                                           \
 119   if (!(p)) {                                                                  \
 120     report_vm_error(__FILE__, __LINE__, "assert(" #p ") failed", __VA_ARGS__); \
 121     BREAKPOINT;                                                                \
 122   }                                                                            \
 123 } while (0)
 124 
 125 #endif
 126 
 127 // For backward compatibility.
 128 #define assert(p, ...) vmassert(p, __VA_ARGS__)


< prev index next >