| 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/misc/error.cpp $ |
| 21 | * $Revision: 26 $ |
| 22 | * $Date: 10/09/01 5:40p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Error-handling functions |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/misc/error.cpp $ |
| 28 | * |
| 29 | * 26 10/09/01 5:40p Matt |
| 30 | * Made -nocrashbox work in release build. |
| 31 | * |
| 32 | * 25 9/15/01 1:46p Kevin |
| 33 | * Added -nocrashbox to suppress crash dialog box |
| 34 | * |
| 35 | * 24 4/19/00 5:23p Matt |
| 36 | * From Duane for 1.4 |
| 37 | * Changed to Mac error exit message |
| 38 | * |
| 39 | * 23 5/05/99 5:22p Samir |
| 40 | * exit(0) instead of exit(1) and other stuff to see why error isn't |
| 41 | * exiting in release? |
| 42 | * |
| 43 | * 22 4/14/99 1:35a Jeff |
| 44 | * fixed case mismatched #includes |
| 45 | * |
| 46 | * 21 4/01/99 4:50p Matt |
| 47 | * Took out Warning() function, chaning those calls to mprintf() |
| 48 | * |
| 49 | * 20 3/02/99 5:50p Kevin |
| 50 | * Ouch. Duplicate structures existed and were conflicting. |
| 51 | * |
| 52 | * 19 2/15/99 1:22p Kevin |
| 53 | * Changes for GameGauge |
| 54 | * |
| 55 | * 18 1/10/99 6:47a Jeff |
| 56 | * Changes made to get linux version to compile |
| 57 | * |
| 58 | * 17 11/01/98 1:58a Jeff |
| 59 | * converted the vsprintf calls to use the Pvsprintf, which is a safe |
| 60 | * vsprintf, no buffer overflows allowed |
| 61 | * |
| 62 | * 16 10/18/98 8:52p Matt |
| 63 | * Revamped debug/error system. |
| 64 | * |
| 65 | */ |
| 66 | |
| 67 | #include <cstdarg> |
| 68 | #include <cstdio> |
| 69 | |
| 70 | #include <stdlib.h> |
| 71 | #include <string.h> |
| 72 | |
| 73 | #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 74 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 75 | #include "debug.h"cxx . . $ `<file>/ddebug/debug.h`/ |
| 76 | #include "application.h"cxx . . $ `<file>/lib/application.h`/ |
| 77 | |
| 78 | #define MAX_MSG_LENcxx . . $ `misc/error.cpp:78:9`! 2000 |
| 79 | |
| 80 | // Debug break chain handlers |
| 81 | void (*DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.)() = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 82 | void (*DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.)() = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 83 | |
| 84 | // library initialized flag |
| 85 | static bool Error_initializedcxx . . $ Error_initialized. = false; |
| 86 | |
| 87 | // message that is printed out when error occurs |
| 88 | static char Exit_messagecxx . . $ Exit_message.[MAX_MSG_LENcxx . . $ `misc/error.cpp:78:9`!]; |
| 89 | static char App_titlecxx . . $ App_title.[32]; |
| 90 | static char Exit_title_strcxx . . $ Exit_title_str.[64]; |
| 91 | |
| 92 | // error message output function |
| 93 | void error_Spewcxx . . $ error_Spew(49f6e7a06ebc5aa8).(); |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | // initializes error handler. |
| 98 | bool error_Initcxx . . $ error_Init(c4501528ba8b3ce4).(bool debugger, const char *app_title) { |
| 99 | Debug_Initcxx . . $ Debug_Init(efaadfb7dc14930a).(debugger); |
| 100 | |
| 101 | Error_initializedcxx . . $ Error_initialized. = true; |
| 102 | Exit_messagecxx . . $ Exit_message.[0] = 0; |
| 103 | |
| 104 | strncpy(App_titlecxx . . $ App_title., app_title, sizeof(App_titlecxx . . $ App_title.) - 1); |
| 105 | App_titlecxx . . $ App_title.[sizeof(App_titlecxx . . $ App_title.) - 1] = 0; |
| 106 | |
| 107 | strncpy(Exit_title_strcxx . . $ Exit_title_str., app_title, sizeof(Exit_title_strcxx . . $ Exit_title_str.) - 1); |
| 108 | Exit_title_strcxx . . $ Exit_title_str.[sizeof(Exit_title_strcxx . . $ Exit_title_str.) - 1] = 0; |
| 109 | |
| 110 | atexitcxx . . $ atexit(cc8c495369588262).(error_Spewcxx . . $ error_Spew(49f6e7a06ebc5aa8).); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // exits the application and prints out a standard error message |
| 116 | void Errorcxx . . $ Error(326aeb464d16410c).(const char *fmt, ...) { |
| 117 | stdcxx . . $ std/::va_listcxx . . $ std/va_list# arglist; |
| 118 | int exit_msg_len; |
| 119 | |
| 120 | strcpy(Exit_messagecxx . . $ Exit_message., "Error: "); |
| 121 | |
| 122 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(arglist, fmt); |
| 123 | exit_msg_len = strlen(Exit_messagecxx . . $ Exit_message.); |
| 124 | stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(Exit_messagecxx . . $ Exit_message. + exit_msg_len, MAX_MSG_LENcxx . . $ `misc/error.cpp:78:9`! - exit_msg_len, fmt, arglist); |
| 125 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(arglist); |
| 126 | |
| 127 | snprintf(Exit_title_strcxx . . $ Exit_title_str., sizeof(Exit_title_strcxx . . $ Exit_title_str.), "%s Error", App_titlecxx . . $ App_title.); |
| 128 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "%s\n", Exit_message); |
| 129 | |
| 130 | #ifdef _DEBUGcxx . . $ `<built-in>:479:9`! |
| 131 | int answer = IDOKcxx . . $ `ddebug/debug.h:115:9`!; |
| 132 | |
| 133 | if (DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.) |
| 134 | (*DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.)(); |
| 135 | |
| 136 | if (Debug_breakcxx . . $ Debug_break.) |
| 137 | answer = Debug_ErrorBoxcxx . . $ Debug_ErrorBox(d78f249273f0ce78).(OSMBOX_ABORTRETRYIGNOREcxx . . $ OSMBOX_ABORTRETRYIGNORE., Exit_title_strcxx . . $ Exit_title_str., Exit_messagecxx . . $ Exit_message., "Press RETRY to debug."); |
| 138 | else |
| 139 | answer = Debug_ErrorBoxcxx . . $ Debug_ErrorBox(d78f249273f0ce78).(OSMBOX_OKCANCELcxx . . $ OSMBOX_OKCANCEL., Exit_title_strcxx . . $ Exit_title_str., Exit_messagecxx . . $ Exit_message., |
| 140 | "Press OK to exit, CANCEL to ignore this error and continue."); |
| 141 | |
| 142 | switch (answer) { |
| 143 | case IDRETRYcxx . . $ `ddebug/debug.h:124:9`!: |
| 144 | debug_break(); // Step Out of this function to see where Error() was called |
| 145 | // fall into ignore/cancel case |
| 146 | case IDIGNOREcxx . . $ `ddebug/debug.h:127:9`!: |
| 147 | case IDCANCELcxx . . $ `ddebug/debug.h:118:9`!: |
| 148 | if (DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.) |
| 149 | (*DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.)(); |
| 150 | return; |
| 151 | case IDOKcxx . . $ `ddebug/debug.h:115:9`!: |
| 152 | case IDABORTcxx . . $ `ddebug/debug.h:121:9`!: |
| 153 | break; // do nothing, and exit below |
| 154 | } |
| 155 | |
| 156 | // Clear the message, since we don't need to see it again when we exit |
| 157 | Exit_messagecxx . . $ Exit_message.[0] = 0; |
| 158 | |
| 159 | #else |
| 160 | if (!Error_initialized) |
| 161 | error_Spew(); |
| 162 | #endif |
| 163 | |
| 164 | // Clear the DEBUG_BREAK() callbacks |
| 165 | SetDebugBreakHandlerscxx . . $ SetDebugBreakHandlers(af7ac88fdfa0d6bb).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 166 | |
| 167 | // Leave the program |
| 168 | exitcxx . . $ exit(44ed87f96534fad7).(0); |
| 169 | } |
| 170 | |
| 171 | // Brings up an error message for an int3 |
| 172 | void Int3MessageBoxcxx . . $ Int3MessageBox(34d26762b42c433a).(const char *file, int line) { |
| 173 | #ifndef RELEASE |
| 174 | char title[128], message[500]; |
| 175 | int answer; |
| 176 | |
| 177 | snprintf(title, sizeof(title), "%s Debug Break", App_titlecxx . . $ App_title.); |
| 178 | snprintf(message, sizeof(message), "Int3 in %s at line %d.", file, line); |
| 179 | |
| 180 | if (DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.) |
| 181 | (*DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.)(); |
| 182 | |
| 183 | answer = Debug_ErrorBoxcxx . . $ Debug_ErrorBox(d78f249273f0ce78).(OSMBOX_YESNOcxx . . $ OSMBOX_YESNO., title, message, "It's probably safe to continue. Continue?"); |
| 184 | |
| 185 | if (answer == IDNOcxx . . $ `ddebug/debug.h:133:9`!) { |
| 186 | SetDebugBreakHandlerscxx . . $ SetDebugBreakHandlers(af7ac88fdfa0d6bb).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 187 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 188 | } |
| 189 | |
| 190 | if (DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.) |
| 191 | (*DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.)(); |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | // prints out an assertion error |
| 196 | void AssertionFailedcxx . . $ AssertionFailed(5e891a9f77bb0eff).(const char *expstr, const char *file, int line) { |
| 197 | #ifndef RELEASE |
| 198 | char title[128], message[500]; |
| 199 | int answer; |
| 200 | |
| 201 | snprintf(title, sizeof(title), "%s Assertion Failed", App_titlecxx . . $ App_title.); |
| 202 | snprintf(message, sizeof(message), "Assertion failed: (%s)\n\nFile %s at line %d.", expstr, file, line); |
| 203 | |
| 204 | if (DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.) |
| 205 | (*DebugBreak_callback_stopcxx . . $ DebugBreak_callback_stop.)(); |
| 206 | |
| 207 | answer = Debug_ErrorBoxcxx . . $ Debug_ErrorBox(d78f249273f0ce78).(OSMBOX_YESNOcxx . . $ OSMBOX_YESNO., title, message, "Continue?"); |
| 208 | |
| 209 | if (answer == IDNOcxx . . $ `ddebug/debug.h:133:9`!) { |
| 210 | SetDebugBreakHandlerscxx . . $ SetDebugBreakHandlers(af7ac88fdfa0d6bb).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 211 | exitcxx . . $ exit(44ed87f96534fad7).(1); |
| 212 | } |
| 213 | |
| 214 | if (DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.) |
| 215 | (*DebugBreak_callback_resumecxx . . $ DebugBreak_callback_resume.)(); |
| 216 | #endif // #ifndef RELEASE |
| 217 | } |
| 218 | |
| 219 | // error message output function |
| 220 | void error_Spewcxx . . $ error_Spew(49f6e7a06ebc5aa8).() { |
| 221 | if (Exit_messagecxx . . $ Exit_message.[0]) |
| 222 | Debug_MessageBoxcxx . . $ Debug_MessageBox(9ee5302023219b2d).(OSMBOX_OKcxx . . $ OSMBOX_OK., Exit_title_strcxx . . $ Exit_title_str., Exit_messagecxx . . $ Exit_message.); |
| 223 | } |
| 224 | |
| 225 | ////////////////////////////////////////////////////////////////////////////// |
| 226 | // MESSAGE BOX SYSTEM |
| 227 | |
| 228 | char Messagebox_titlecxx . . $ Messagebox_title.[80] = "Message"; |
| 229 | |
| 230 | // Sets the title for future OutrageMessageBox() dialogs |
| 231 | void SetMessageBoxTitlecxx . . $ SetMessageBoxTitle(85c52e162fed56f9).(const char *title) { strncpy(Messagebox_titlecxx . . $ Messagebox_title., title, sizeof(Messagebox_titlecxx . . $ Messagebox_title.)); } |
| 232 | |
| 233 | #define BUF_LENcxx . . $ `misc/error.cpp:233:9`! 1024 |
| 234 | |
| 235 | // Pops up a dialog box to display a message |
| 236 | void OutrageMessageBoxcxx . . $ OutrageMessageBox(326aeb464d16410c).(const char *str, ...) { |
| 237 | char buf[BUF_LENcxx . . $ `misc/error.cpp:233:9`!]; |
| 238 | stdcxx . . $ std/::va_listcxx . . $ std/va_list# arglist; |
| 239 | int nchars; |
| 240 | |
| 241 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(arglist, str); |
| 242 | nchars = stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(buf, BUF_LENcxx . . $ `misc/error.cpp:233:9`!, str, arglist); |
| 243 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(arglist); |
| 244 | |
| 245 | if (nchars >= BUF_LENcxx . . $ `misc/error.cpp:233:9`!) |
| 246 | Debug_MessageBoxcxx . . $ Debug_MessageBox(9ee5302023219b2d).(OSMBOX_OKcxx . . $ OSMBOX_OK., Messagebox_titlecxx . . $ Messagebox_title., |
| 247 | "The dialog that follows this one overflowed its text buffer. The program may crash."); |
| 248 | |
| 249 | Debug_MessageBoxcxx . . $ Debug_MessageBox(9ee5302023219b2d).(OSMBOX_OKcxx . . $ OSMBOX_OK., Messagebox_titlecxx . . $ Messagebox_title., buf); |
| 250 | } |
| 251 | |
| 252 | int OutrageMessageBoxcxx . . $ OutrageMessageBox(811d273381043e7b).(int type, const char *str, ...) { |
| 253 | char buf[BUF_LENcxx . . $ `misc/error.cpp:233:9`!]; |
| 254 | stdcxx . . $ std/::va_listcxx . . $ std/va_list# arglist; |
| 255 | int os_flags = 0; |
| 256 | int nchars; |
| 257 | |
| 258 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(arglist, str); |
| 259 | nchars = stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(buf, BUF_LENcxx . . $ `misc/error.cpp:233:9`!, str, arglist); |
| 260 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(arglist); |
| 261 | |
| 262 | if (type == MBOX_OKcxx . . $ `misc/pserror.h:164:9`!) |
| 263 | os_flags = OSMBOX_OKcxx . . $ OSMBOX_OK.; |
| 264 | else if (type == MBOX_YESNOcxx . . $ `misc/pserror.h:165:9`!) |
| 265 | os_flags = OSMBOX_YESNOcxx . . $ OSMBOX_YESNO.; |
| 266 | else if (type == MBOX_YESNOCANCELcxx . . $ `misc/pserror.h:166:9`!) |
| 267 | os_flags = OSMBOX_YESNOCANCELcxx . . $ OSMBOX_YESNOCANCEL.; |
| 268 | else if (type == MBOX_ABORTRETRYIGNOREcxx . . $ `misc/pserror.h:167:9`!) |
| 269 | os_flags = OSMBOX_ABORTRETRYIGNOREcxx . . $ OSMBOX_ABORTRETRYIGNORE.; |
| 270 | else |
| 271 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 272 | |
| 273 | if (nchars >= BUF_LENcxx . . $ `misc/error.cpp:233:9`!) |
| 274 | Debug_MessageBoxcxx . . $ Debug_MessageBox(9ee5302023219b2d).(OSMBOX_OKcxx . . $ OSMBOX_OK., Messagebox_titlecxx . . $ Messagebox_title., |
| 275 | "The dialog that follows this one overflowed its text buffer. The program may crash."); |
| 276 | |
| 277 | return Debug_MessageBoxcxx . . $ Debug_MessageBox(9ee5302023219b2d).(os_flags, Messagebox_titlecxx . . $ Messagebox_title., buf); |
| 278 | } |