Debian Codegraph @

ddebug/windebug.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/win32/windebug.cpp $
21 * $Revision: 33 $
22 * $Date: 9/15/01 1:46p $
23 * $Author: Kevin $
24 *
25 * Win32 Debugging 'kernal'
26 *
27 * $Log: /DescentIII/Main/win32/windebug.cpp $
28 *
29 * 33 9/15/01 1:46p Kevin
30 * Added -nocrashbox to suppress crash dialog box
31 *
32 * 32 5/19/99 2:43a Jeff
33 * handle some possible crashes. The UCT->Localtime doesn't seem to work,
34 * although it is setup correctly.
35 *
36 * 31 5/18/99 9:48p Kevin
37 * adjust time to time zone for Jeff
38 *
39 * 30 5/07/99 5:46p Jason
40 * changes for gamegauge release
41 *
42 * 29 4/18/99 1:18p Kevin
43 * Demo2 build changes
44 *
45 * 28 4/12/99 7:14p Samir
46 * added multiple pages per mono window.
47 *
48 * 27 4/01/99 11:07a Kevin
49 * Added some more usefull debugging info
50 *
51 * 26 4/01/99 9:40a Matt
52 * Fixed dump_text_to_clipboard. It used to allocate a fixed number of
53 * bytes to allow space for adding CRs to strings, but now I actually
54 * count the number needed and allocate that much.
55 *
56 * 25 3/29/99 6:50p Kevin
57 * Added some parameters to the log file
58 *
59 * 24 2/16/99 11:25a Kevin
60 * Took out testing code.
61 *
62 * 23 2/16/99 12:36a Kevin
63 * Fixes for release builds of OEM V3 and KAtmai
64 *
65 * 22 2/15/99 1:22p Kevin
66 * Changes for GameGauge
67 *
68 * 21 2/13/99 1:56p Kevin
69 *
70 * 20 1/05/99 4:28p Kevin
71 * Moved exception handling code to windebug.cpp
72 *
73 * 19 1/05/99 3:55p Kevin
74 * Added structured exception handling
75 *
76 * 18 10/18/98 8:52p Matt
77 * Revamped debug/error system.
78 *
79 * 17 10/12/98 10:20a Samir
80 * added parameter to debug init.
81 *
82 * 16 10/08/98 4:24p Kevin
83 * Changed code to comply with memory library usage. Always use mem_malloc
84 * , mem_free and mem_strdup
85 *
86 * 15 6/23/98 2:40p Matt
87 * Added Yes/No/Cancel type to OutrageMessageBox() and
88 * Debug_MesssageBox(), and changed return value from a bool to an
89 * integer.
90 *
91 * 14 4/21/98 2:30p Matt
92 * dump_text_to_clipboard() wasn't putting terminating null on text string
93 *
94 * 13 4/08/98 7:19p Samir
95 * Added runtime debugging option.
96 *
97 * 12 4/07/98 9:21p Samir
98 * Added volition's call stack and memory dump code. This will only
99 * happen if you run the game without the -debug argument.
100 *
101 * 11 4/03/98 5:15p Samir
102 * Implemented simple debug message filtering.
103 *
104 * 10 3/20/98 2:43p Samir
105 * Some better Int3 support.
106 *
107 * 9 1/29/98 12:24p Samir
108 * Added logfile support.
109 *
110 * 8 1/28/98 4:45p Samir
111 * Mono check stuff.
112 *
113 * 7 1/28/98 11:56a Samir
114 * Code to check for monochrome debug.
115 *
116 * 6 1/28/98 11:17a Samir
117 * Added debugbreak stuff.
118 *
119 * 5 10/15/97 3:05p Samir
120 * NT mono output works.
121 *
122 * 4 10/13/97 2:41p Samir
123 * Debug breaks now are macros to work depending on the operating system.
124 *
125 * 3 9/04/97 12:00p Matt
126 * Changed Debug_MessageBox() to return bool instead of int, since it was
127 * already being used that way.
128 *
129 * 2 7/28/97 3:46p Samir
130 * NT mono stubbed out.
131 *
132 * 18 6/11/97 1:10p Samir
133 * Fixed up function names.
134 *
135 * 17 3/31/97 3:50p Samir
136 * MessageBox should be task modal.
137 *
138 * 16 3/18/97 2:14p Samir
139 * DebugBreak called instead of Int3
140 *
141 * 15 3/17/97 8:10p Matt
142 * Don't print to monochrome screen unless D3_MONO evironment var is set
143 *
144 * 14 3/14/97 4:15p Matt
145 * Changed title of OutrageMessageBox() dialogs
146 *
147 * 13 3/10/97 12:29p Samir
148 * Moved mono code and altered lowlevel console interface to machine
149 * library.
150 *
151 * 12 3/10/97 11:13a Samir
152 * Changes made to reflect deletion of OSDebug class from system
153 *
154 * 11 3/04/97 7:15p Samir
155 * Fixed bug in message_box, caused an Int3.
156 *
157 * 10 3/04/97 1:00p Samir
158 * Fixed return type prototype errors.
159 *
160 * 9 3/04/97 12:55p Samir
161 * Added types of messageboxes.
162 *
163 * 8 3/03/97 5:07p Samir
164 * stupid
165 *
166 * 7 3/03/97 4:36p Samir
167 * Removed instances of pserror.h code. this is a lower level lib than
168 * that!
169 *
170 * 6 2/27/97 7:00p Samir
171 * Removed windows.h from oewin_os.h and moved to library.
172 *
173 * 5 2/26/97 7:04p Samir
174 * Added Win32 DebugBreak
175 *
176 * 4 2/18/97 7:00p Samir
177 * Now print out last Win32 error too.
178 *
179 * 3 1/31/97 3:31p Samir
180 * forgot to use OSWINDBG_MAX_STRINGS constant in array init.
181 *
182 * 2 1/30/97 6:49p Samir
183 * Added some winDebug code for interface with error handler
184 *
185 * 1 1/28/97 2:32p Samir
186 * Standard debugging functions
187 *
188 * $NoKeywords: $
189 */
190
191 #include <windows.h>
192 #include <DbgHelp.h>
193 #include <cstring>
194 #include <filesystem>
195
196 #include "debug.h"
197 #include "mem.h"
198 #include "pserror.h"
199
200 ///////////////////////////////////////////////////////////////////////////////
201
202 bool Debug_break = false;
203
204 ///////////////////////////////////////////////////////////////////////////////
205
206 bool Debug_Init(bool debugger) {
207 // initialization of debugging consoles.
208
209 #ifndef RELEASE
210
211 Debug_break = debugger;
212
213 if (Debug_break) {
214 mprintf(0, "Debug Break enabled.\n");
215 }
216
217 #endif // ifndef RELEASE
218
219 return true;
220 }
221
222 /* Message Box functions */
223
224 // Does a messagebox with a stack dump
225 // Messagebox shows topstring, then stack dump, then bottomstring
226 // Return types are as the same the Windows return values
227 int Debug_ErrorBox(int type, const char *title, const char *topstring, const char *bottomstring) {
228 DWORD flags = 0;
229 if (type == OSMBOX_OK)
230 flags = MB_OK;
231 else if (type == OSMBOX_YESNO)
232 flags = MB_YESNO;
233 else if (type == OSMBOX_YESNOCANCEL)
234 flags = MB_YESNOCANCEL;
235 else if (type == OSMBOX_ABORTRETRYIGNORE)
236 flags = MB_ABORTRETRYIGNORE;
237 else if (type == OSMBOX_OKCANCEL)
238 flags = MB_OKCANCEL;
239 else
240 debug_break();
241
242 char *tmpbuf = (char *)mem_malloc(strlen(topstring) + strlen(bottomstring) + 5);
243 wsprintf(tmpbuf, "%s\r\n\r\n%s", topstring, bottomstring);
244
245 ShowCursor(TRUE);
246 int answer = MessageBox(nullptr, tmpbuf, title, flags | MB_TASKMODAL | MB_SETFOREGROUND);
247 ShowCursor(FALSE);
248
249 mem_free(tmpbuf);
250
251 return answer;
252 }
253
254 // Return types are the same the Windows return values
255 int Debug_MessageBox(int type, const char *title, const char *str) {
256 DWORD flags = 0;
257 if (type == OSMBOX_OK)
258 flags = MB_OK;
259 else if (type == OSMBOX_YESNO)
260 flags = MB_YESNO;
261 else if (type == OSMBOX_YESNOCANCEL)
262 flags = MB_YESNOCANCEL;
263 else if (type == OSMBOX_ABORTRETRYIGNORE)
264 flags = MB_ABORTRETRYIGNORE;
265 else
266 DebugBreak();
267
268 ShowCursor(TRUE);
269 int answer = MessageBox(nullptr, str, title, flags | MB_TASKMODAL | MB_SETFOREGROUND);
270 ShowCursor(FALSE);
271
272 return answer;
273 }
274
275 ///////////////////////////////////////////////////////////////////////////////
276
277 void dump_text_to_clipboard(char *text);
278
279 ///////////////////////////////////////////////////////////////////////////////
280
281 void DumpTextToClipboard(char *text) {
282 char *ptr;
283
284 // Count number of carriage returns to be added
285 char last_char = 0;
286 int extra = 0;
287 ptr = text;
288 while (*ptr) {
289 if ((*ptr == '\n') && (last_char != '\r')) {
290 extra++;
291 }
292 last_char = *ptr++;
293 }
294
295 // Length of string with CRs added
296 size_t len = strlen(text) + extra + 1;
297
298 HGLOBAL h_text = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, len);
299 if (!h_text)
300 return;
301 ptr = (char *)GlobalLock(h_text);
302 if (!ptr)
303 return;
304
305 // copy then, if you find any \n's without \r's, then add in the \r.
306 last_char = 0;
307 while (*text) {
308 if ((*text == '\n') && (last_char != '\r')) {
309 *ptr++ = '\r';
310 }
311 last_char = *text;
312 *ptr++ = last_char;
313 text++;
314 }
315 *ptr = 0; // terminate string
316 GlobalUnlock(h_text);
317 OpenClipboard(NULL);
318 EmptyClipboard();
319 SetClipboardData(CF_TEXT, h_text);
320 CloseClipboard();
321 }
322
323 void dump_text_to_clipboard(char *text) { DumpTextToClipboard(text); }
324
325 ///////////////////////////////////////////////////////////////////////////////
326
327 #pragma comment(lib, "DbgHelp.lib")
328 long WINAPI RecordExceptionInfo(PEXCEPTION_POINTERS data) {
329 static bool BeenHere = false;
330 if (BeenHere) // Going recursive! That must mean this routine crashed!
331 return EXCEPTION_CONTINUE_SEARCH;
332 BeenHere = true;
333
334 SetMessageBoxTitle("Unexpected error");
335
336 std::error_code ec;
337 const std::filesystem::path dumFilePath = std::filesystem::temp_directory_path(ec) / "d3-crash.dmp";
338 if (ec) {
339 OutrageMessageBox("Could not find temporary directory to write crash dump file into: %s", ec.message().c_str());
340 exit(1);
341 }
342
343 HANDLE hDumpFile = CreateFile(dumFilePath.u8string().c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, 0);
344 if (hDumpFile == INVALID_HANDLE_VALUE) {
345 OutrageMessageBox("Could not create crash dump file, error code: %x", GetLastError());
346 exit(1);
347 }
348
349 MINIDUMP_EXCEPTION_INFORMATION exceptInfo;
350 exceptInfo.ThreadId = GetCurrentThreadId();
351 exceptInfo.ExceptionPointers = data;
352 exceptInfo.ClientPointers = TRUE;
353
354 if (MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &exceptInfo, nullptr, nullptr)) {
355 OutrageMessageBox("Crash dump file written to: %s\nYou can attach it to a bug report.", dumFilePath.u8string().c_str());
356 } else {
357 OutrageMessageBox("Could not write crash dump file, error code: %x", GetLastError());
358 exit(1);
359 }
360
361 BeenHere = false;
362
363 return Debug_break ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER;
364 }