jdk/src/windows/native/sun/windows/awt_Debug.cpp
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7772 bf119e738928
child 14883 524cf4063ab3
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7772
diff changeset
     2
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include "awt.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "awt_Toolkit.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "debug_mem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
extern void DumpJavaStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#if defined(DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
////////////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
// avoid pulling in our redefinition of 'new'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
// since we actually need to implement it here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#if defined(new)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#undef new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
void * operator new(size_t size, const char * filename, int linenumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    void * ptr = DMem_AllocateBlock(size, filename, linenumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    if (ptr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        throw std::bad_alloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    return ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
7772
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    50
void * operator new[](size_t size, const char * filename, int linenumber) {
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    51
    void * ptr = DMem_AllocateBlock(size, filename, linenumber);
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    52
    if (ptr == NULL) {
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    53
        throw std::bad_alloc();
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    54
    }
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    55
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    56
    return ptr;
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    57
}
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#if _MSC_VER >= 1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
void operator delete(void *ptr, const char*, int) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    DASSERTMSG(FALSE, "This version of 'delete' should never get called!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#endif
7772
bf119e738928 6999766: Changes to correct c/c++ language issues for use of parfait
dav
parents: 5506
diff changeset
    64
void operator delete(void *ptr) throw() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    DMem_FreeBlock(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
////////////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static void DumpRegion(HRGN rgn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    DWORD size = ::GetRegionData(rgn, 0, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    char* buffer = (char *)safe_Malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    memset(buffer, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    LPRGNDATA rgndata = (LPRGNDATA)buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    rgndata->rdh.iType = RDH_RECTANGLES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    VERIFY(::GetRegionData(rgn, size, rgndata));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    RECT* r = (RECT*)(buffer + rgndata->rdh.dwSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    for (DWORD i=0; i<rgndata->rdh.nCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if ( !::IsRectEmpty(r) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            DTrace_PrintImpl("\trect %d %d %d %d\n", r->left, r->top, r->right, r->bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * DTRACE print callback to dump HDC clip region bounding rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void DumpClipRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    const char *msg = va_arg(arglist, const char *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    HDC         hdc = va_arg(arglist, HDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    RECT        r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    DASSERT(argc == 2 && hdc != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    DASSERT(msg != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    ::GetClipBox(hdc, &r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    DTrace_PrintImpl("%s: hdc=%x, %d %d %d %d\n", msg, hdc, r.left, r.top, r.right, r.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * DTRACE print callback to dump window's update region bounding rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
void DumpUpdateRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    const char *msg = va_arg(arglist, const char *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    HWND        hwnd = va_arg(arglist, HWND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    RECT        r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    DASSERT(argc == 2 && ::IsWindow(hwnd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    DASSERT(msg != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    ::GetUpdateRect(hwnd, &r, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    HRGN rgn = ::CreateRectRgn(0,0,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int updated = ::GetUpdateRgn(hwnd, rgn, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    DTrace_PrintImpl("%s: hwnd=%x, %d %d %d %d\n", msg, hwnd, r.left, r.top, r.right, r.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    DumpRegion(rgn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    DeleteObject(rgn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
// Declare a static object to init/fini the debug code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
// specify that this static object will get constructed before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
// any other static objects (except CRT objects) so the debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
// code can be used anywhere during the lifetime of the AWT dll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
#pragma warning( disable:4073 ) // disable warning about using init_seg(lib) in non-3rd party library code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#pragma init_seg( lib )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
static volatile AwtDebugSupport DebugSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
static int report_leaks = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
AwtDebugSupport::AwtDebugSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    DMem_Initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    DTrace_Initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    DAssert_SetCallback(AssertCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
AwtDebugSupport::~AwtDebugSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (report_leaks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        DMem_ReportLeaks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    DMem_Shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    DTrace_Shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
void AwtDebugSupport::AssertCallback(const char * expr, const char * file, int line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static const int ASSERT_MSG_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static const char * AssertFmt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            "%s\r\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            "File '%s', at line %d\r\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            "GetLastError() is %x : %s\r\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            "Do you want to break into the debugger?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    static char assertMsg[ASSERT_MSG_SIZE+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    DWORD   lastError = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    LPSTR       msgBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int     ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                  FORMAT_MESSAGE_FROM_SYSTEM |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                  FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                  NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                  lastError,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                  (LPSTR)&msgBuffer, // it's an output parameter when allocate buffer is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                  0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if (msgBuffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        msgBuffer = "<Could not get GetLastError() message text>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // format the assertion message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    _snprintf(assertMsg, ASSERT_MSG_SIZE, AssertFmt, expr, file, line, lastError, msgBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    LocalFree(msgBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // tell the user the bad news
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    fprintf(stderr, "*********************\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    fprintf(stderr, "AWT Assertion Failure\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    fprintf(stderr, "*********************\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    fprintf(stderr, "%s\n", assertMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    fprintf(stderr, "*********************\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    ret = MessageBoxA(NULL, assertMsg, "AWT Assertion Failure",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        MB_YESNO|MB_ICONSTOP|MB_TASKMODAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // if clicked Yes, break into the debugger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if ( ret == IDYES ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        # if defined(_M_IX86)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            _asm { int 3 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        # else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            DebugBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        # endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // otherwise, try to continue execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
void AwtDebugSupport::GenerateLeaksReport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    report_leaks = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
#endif // DEBUG