1 /*
   2  * Copyright (c) 1999, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #include "awt.h"
  26 #include "awt_Toolkit.h"
  27 #include "debug_mem.h"
  28 
  29 extern void DumpJavaStack();
  30 
  31 #if defined(DEBUG)
  32 
  33 ////////////////////////////////////////////////////////////////////////////////////
  34 // avoid pulling in our redefinition of 'new'
  35 // since we actually need to implement it here
  36 #if defined(new)
  37 #undef new
  38 #endif
  39 //
  40 
  41 void * operator new(size_t size, const char * filename, int linenumber) {
  42     void * ptr = DMem_AllocateBlock(size, filename, linenumber);
  43     if (ptr == NULL) {
  44         throw std::bad_alloc();
  45     }
  46 
  47     return ptr;
  48 }
  49 
  50 void * operator new[](size_t size, const char * filename, int linenumber) {
  51     void * ptr = DMem_AllocateBlock(size, filename, linenumber);
  52     if (ptr == NULL) {
  53         throw std::bad_alloc();
  54     }
  55 
  56     return ptr;
  57 }
  58 
  59 void operator delete(void *ptr, const char*, int) {
  60     DASSERTMSG(FALSE, "This version of 'delete' should never get called!!!");
  61 }
  62 
  63 void operator delete[](void *ptr, const char*, int) {
  64     DASSERTMSG(FALSE, "This version of 'delete' should never get called!!!");
  65 }
  66 
  67 void operator delete(void *ptr) throw() {
  68     DMem_FreeBlock(ptr);
  69 }
  70 
  71 ////////////////////////////////////////////////////////////////////////////////////
  72 
  73 static void DumpRegion(HRGN rgn) {
  74     DWORD size = ::GetRegionData(rgn, 0, NULL);
  75     char* buffer = (char *)safe_Malloc(size);
  76     memset(buffer, 0, size);
  77     LPRGNDATA rgndata = (LPRGNDATA)buffer;
  78     rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
  79     rgndata->rdh.iType = RDH_RECTANGLES;
  80     VERIFY(::GetRegionData(rgn, size, rgndata));
  81 
  82     RECT* r = (RECT*)(buffer + rgndata->rdh.dwSize);
  83     for (DWORD i=0; i<rgndata->rdh.nCount; i++) {
  84         if ( !::IsRectEmpty(r) ) {
  85             DTrace_PrintImpl("\trect %d %d %d %d\n", r->left, r->top, r->right, r->bottom);
  86         }
  87         r++;
  88     }
  89 
  90     free(buffer);
  91 }
  92 
  93 /*
  94  * DTRACE print callback to dump HDC clip region bounding rectangle
  95  */
  96 void DumpClipRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) {
  97     const char *msg = va_arg(arglist, const char *);
  98     HDC         hdc = va_arg(arglist, HDC);
  99     RECT        r;
 100 
 101     DASSERT(argc == 2 && hdc != NULL);
 102     DASSERT(msg != NULL);
 103 
 104     ::GetClipBox(hdc, &r);
 105     DTrace_PrintImpl("%s: hdc=%x, %d %d %d %d\n", msg, hdc, r.left, r.top, r.right, r.bottom);
 106 }
 107 
 108 /*
 109  * DTRACE print callback to dump window's update region bounding rectangle
 110  */
 111 void DumpUpdateRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) {
 112     const char *msg = va_arg(arglist, const char *);
 113     HWND        hwnd = va_arg(arglist, HWND);
 114     RECT        r;
 115 
 116     DASSERT(argc == 2 && ::IsWindow(hwnd));
 117     DASSERT(msg != NULL);
 118 
 119     ::GetUpdateRect(hwnd, &r, FALSE);
 120     HRGN rgn = ::CreateRectRgn(0,0,1,1);
 121     int updated = ::GetUpdateRgn(hwnd, rgn, FALSE);
 122     DTrace_PrintImpl("%s: hwnd=%x, %d %d %d %d\n", msg, hwnd, r.left, r.top, r.right, r.bottom);
 123     DumpRegion(rgn);
 124     DeleteObject(rgn);
 125 }
 126 
 127 //
 128 // Declare a static object to init/fini the debug code
 129 //
 130 // specify that this static object will get constructed before
 131 // any other static objects (except CRT objects) so the debug
 132 // code can be used anywhere during the lifetime of the AWT dll
 133 #pragma warning( disable:4073 ) // disable warning about using init_seg(lib) in non-3rd party library code
 134 #pragma init_seg( lib )
 135 
 136 static volatile AwtDebugSupport DebugSupport;
 137 static int report_leaks = 0;
 138 
 139 AwtDebugSupport::AwtDebugSupport() {
 140     DMem_Initialize();
 141     DTrace_Initialize();
 142     DAssert_SetCallback(AssertCallback);
 143 }
 144 
 145 AwtDebugSupport::~AwtDebugSupport() {
 146     if (report_leaks) {
 147         DMem_ReportLeaks();
 148     }
 149     DMem_Shutdown();
 150     DTrace_Shutdown();
 151 }
 152 
 153 static jboolean isHeadless() {
 154     jmethodID headlessFn;
 155     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 156     jclass graphicsEnvClass = env->FindClass(
 157         "java/awt/GraphicsEnvironment");
 158 
 159     if (graphicsEnvClass != NULL) {
 160         headlessFn = env->GetStaticMethodID(
 161             graphicsEnvClass, "isHeadless", "()Z");
 162         if (headlessFn != NULL) {
 163             return env->CallStaticBooleanMethod(graphicsEnvClass,
 164                                                 headlessFn);
 165         }
 166     }
 167     return true;
 168 }
 169 
 170 
 171 void AwtDebugSupport::AssertCallback(const char * expr, const char * file, int line) {
 172     static const int ASSERT_MSG_SIZE = 1024;
 173     static const char * AssertFmt =
 174             "%s\r\n"
 175             "File '%s', at line %d\r\n"
 176             "GetLastError() is %x : %s\r\n"
 177             "Do you want to break into the debugger?";
 178 
 179     static char assertMsg[ASSERT_MSG_SIZE+1];
 180     DWORD   lastError = GetLastError();
 181     LPSTR       msgBuffer = NULL;
 182     int     ret = IDNO;
 183     static jboolean headless = isHeadless();
 184 
 185     DWORD fret= FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 186                   FORMAT_MESSAGE_FROM_SYSTEM |
 187                   FORMAT_MESSAGE_IGNORE_INSERTS,
 188                   NULL,
 189                   lastError,
 190                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 191                   (LPSTR)&msgBuffer, // it's an output parameter when allocate buffer is used
 192                   0,
 193                   NULL);
 194 
 195     if (msgBuffer == NULL) {
 196         msgBuffer = "<Could not get GetLastError() message text>";
 197     }
 198     // format the assertion message
 199     _snprintf(assertMsg, ASSERT_MSG_SIZE, AssertFmt, expr, file, line, lastError, msgBuffer);
 200     if (fret != 0) {
 201         LocalFree(msgBuffer);
 202     }
 203 
 204     // tell the user the bad news
 205     fprintf(stderr, "*********************\n");
 206     fprintf(stderr, "AWT Assertion Failure\n");
 207     fprintf(stderr, "*********************\n");
 208     fprintf(stderr, "%s\n", assertMsg);
 209     fprintf(stderr, "*********************\n");
 210 
 211     if (!headless) {
 212         ret = MessageBoxA(NULL, assertMsg, "AWT Assertion Failure",
 213                           MB_YESNO|MB_ICONSTOP|MB_TASKMODAL);
 214     }
 215 
 216     // if clicked Yes, break into the debugger
 217     if ( ret == IDYES ) {
 218         # if defined(_M_IX86)
 219             _asm { int 3 };
 220         # else
 221             DebugBreak();
 222         # endif
 223     }
 224     // otherwise, try to continue execution
 225 }
 226 
 227 void AwtDebugSupport::GenerateLeaksReport() {
 228     report_leaks = 1;
 229 }
 230 
 231 #endif // DEBUG