author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 7772 | bf119e738928 |
child 14883 | 524cf4063ab3 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
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 | 59 |
#if _MSC_VER >= 1200 |
60 |
void operator delete(void *ptr, const char*, int) { |
|
61 |
DASSERTMSG(FALSE, "This version of 'delete' should never get called!!!"); |
|
62 |
} |
|
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 | 65 |
DMem_FreeBlock(ptr); |
66 |
} |
|
67 |
||
68 |
//////////////////////////////////////////////////////////////////////////////////// |
|
69 |
||
70 |
static void DumpRegion(HRGN rgn) { |
|
71 |
DWORD size = ::GetRegionData(rgn, 0, NULL); |
|
72 |
char* buffer = (char *)safe_Malloc(size); |
|
73 |
memset(buffer, 0, size); |
|
74 |
LPRGNDATA rgndata = (LPRGNDATA)buffer; |
|
75 |
rgndata->rdh.dwSize = sizeof(RGNDATAHEADER); |
|
76 |
rgndata->rdh.iType = RDH_RECTANGLES; |
|
77 |
VERIFY(::GetRegionData(rgn, size, rgndata)); |
|
78 |
||
79 |
RECT* r = (RECT*)(buffer + rgndata->rdh.dwSize); |
|
80 |
for (DWORD i=0; i<rgndata->rdh.nCount; i++) { |
|
81 |
if ( !::IsRectEmpty(r) ) { |
|
82 |
DTrace_PrintImpl("\trect %d %d %d %d\n", r->left, r->top, r->right, r->bottom); |
|
83 |
} |
|
84 |
r++; |
|
85 |
} |
|
86 |
||
87 |
free(buffer); |
|
88 |
} |
|
89 |
||
90 |
/* |
|
91 |
* DTRACE print callback to dump HDC clip region bounding rectangle |
|
92 |
*/ |
|
93 |
void DumpClipRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) { |
|
94 |
const char *msg = va_arg(arglist, const char *); |
|
95 |
HDC hdc = va_arg(arglist, HDC); |
|
96 |
RECT r; |
|
97 |
||
98 |
DASSERT(argc == 2 && hdc != NULL); |
|
99 |
DASSERT(msg != NULL); |
|
100 |
||
101 |
::GetClipBox(hdc, &r); |
|
102 |
DTrace_PrintImpl("%s: hdc=%x, %d %d %d %d\n", msg, hdc, r.left, r.top, r.right, r.bottom); |
|
103 |
} |
|
104 |
||
105 |
/* |
|
106 |
* DTRACE print callback to dump window's update region bounding rectangle |
|
107 |
*/ |
|
108 |
void DumpUpdateRectangle(const char * file, int line, int argc, const char * fmt, va_list arglist) { |
|
109 |
const char *msg = va_arg(arglist, const char *); |
|
110 |
HWND hwnd = va_arg(arglist, HWND); |
|
111 |
RECT r; |
|
112 |
||
113 |
DASSERT(argc == 2 && ::IsWindow(hwnd)); |
|
114 |
DASSERT(msg != NULL); |
|
115 |
||
116 |
::GetUpdateRect(hwnd, &r, FALSE); |
|
117 |
HRGN rgn = ::CreateRectRgn(0,0,1,1); |
|
118 |
int updated = ::GetUpdateRgn(hwnd, rgn, FALSE); |
|
119 |
DTrace_PrintImpl("%s: hwnd=%x, %d %d %d %d\n", msg, hwnd, r.left, r.top, r.right, r.bottom); |
|
120 |
DumpRegion(rgn); |
|
121 |
DeleteObject(rgn); |
|
122 |
} |
|
123 |
||
124 |
// |
|
125 |
// Declare a static object to init/fini the debug code |
|
126 |
// |
|
127 |
// specify that this static object will get constructed before |
|
128 |
// any other static objects (except CRT objects) so the debug |
|
129 |
// code can be used anywhere during the lifetime of the AWT dll |
|
130 |
#pragma warning( disable:4073 ) // disable warning about using init_seg(lib) in non-3rd party library code |
|
131 |
#pragma init_seg( lib ) |
|
132 |
||
133 |
static volatile AwtDebugSupport DebugSupport; |
|
134 |
static int report_leaks = 0; |
|
135 |
||
136 |
AwtDebugSupport::AwtDebugSupport() { |
|
137 |
DMem_Initialize(); |
|
138 |
DTrace_Initialize(); |
|
139 |
DAssert_SetCallback(AssertCallback); |
|
140 |
} |
|
141 |
||
142 |
AwtDebugSupport::~AwtDebugSupport() { |
|
143 |
if (report_leaks) { |
|
144 |
DMem_ReportLeaks(); |
|
145 |
} |
|
146 |
DMem_Shutdown(); |
|
147 |
DTrace_Shutdown(); |
|
148 |
} |
|
149 |
||
150 |
void AwtDebugSupport::AssertCallback(const char * expr, const char * file, int line) { |
|
151 |
static const int ASSERT_MSG_SIZE = 1024; |
|
152 |
static const char * AssertFmt = |
|
153 |
"%s\r\n" |
|
154 |
"File '%s', at line %d\r\n" |
|
155 |
"GetLastError() is %x : %s\r\n" |
|
156 |
"Do you want to break into the debugger?"; |
|
157 |
||
158 |
static char assertMsg[ASSERT_MSG_SIZE+1]; |
|
159 |
DWORD lastError = GetLastError(); |
|
160 |
LPSTR msgBuffer = NULL; |
|
161 |
int ret; |
|
162 |
||
163 |
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
164 |
FORMAT_MESSAGE_FROM_SYSTEM | |
|
165 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
166 |
NULL, |
|
167 |
lastError, |
|
168 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
169 |
(LPSTR)&msgBuffer, // it's an output parameter when allocate buffer is used |
|
170 |
0, |
|
171 |
NULL); |
|
172 |
||
173 |
if (msgBuffer == NULL) { |
|
174 |
msgBuffer = "<Could not get GetLastError() message text>"; |
|
175 |
} |
|
176 |
// format the assertion message |
|
177 |
_snprintf(assertMsg, ASSERT_MSG_SIZE, AssertFmt, expr, file, line, lastError, msgBuffer); |
|
178 |
LocalFree(msgBuffer); |
|
179 |
||
180 |
// tell the user the bad news |
|
181 |
fprintf(stderr, "*********************\n"); |
|
182 |
fprintf(stderr, "AWT Assertion Failure\n"); |
|
183 |
fprintf(stderr, "*********************\n"); |
|
184 |
fprintf(stderr, "%s\n", assertMsg); |
|
185 |
fprintf(stderr, "*********************\n"); |
|
186 |
ret = MessageBoxA(NULL, assertMsg, "AWT Assertion Failure", |
|
187 |
MB_YESNO|MB_ICONSTOP|MB_TASKMODAL); |
|
188 |
||
189 |
// if clicked Yes, break into the debugger |
|
190 |
if ( ret == IDYES ) { |
|
191 |
# if defined(_M_IX86) |
|
192 |
_asm { int 3 }; |
|
193 |
# else |
|
194 |
DebugBreak(); |
|
195 |
# endif |
|
196 |
} |
|
197 |
// otherwise, try to continue execution |
|
198 |
} |
|
199 |
||
200 |
void AwtDebugSupport::GenerateLeaksReport() { |
|
201 |
report_leaks = 1; |
|
202 |
} |
|
203 |
||
204 |
#endif // DEBUG |