aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/ErrorHandling.h
blob: 3b35695f20bb281b4bba8fb128980f96eacd47ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#ifndef _ERRORHANDLING_H_
#define _ERRORHANDLING_H_

#include <vector>
#include <string>

#include "EmCPU68K.h"			// ExceptionNumber
#include "EmDlg.h"				// EmCommonDialogFlags
#include "EmRect.h"				// ExceptionNumber


class SessionFile;
struct SystemCallContext;


// ParamList contains a sequence of key/value pairs.  The even-numbered
// strings are the keys, and the odd-numbered strings are the values.
// The key/value pairs are used in string template substitution.  When
// substitution needs to take place, the keys and values are pulled
// out of the list one pair at a time.  The template string is then
// traversed, with each key being replaced by its value.
//
// Keys are typically of the form "%text" (with the "%" showing up in
// both the key text and the string template text), but could actually
// be in any form that is unique by convention.
//
// To create a parameter list, do something like the following:
//
//		ParamList	paramList;
//		paramList.push_back (string ("%foo"));
//		paramList.push_back (string ("bar"));
//		... repeat as necessary ...

typedef vector<string>	ParamList;


// Values passed to ReportInvalidPC.

enum
{
	kUnmappedAddress,
	kNotInCodeSegment,
	kOddAddress
};

class Errors
{
	public:
		static void				Initialize			(void);
		static void				Reset				(void);
		static void				Save				(SessionFile&);
		static void				Load				(SessionFile&);
		static void				Dispose				(void);


		// These three functions check for the indicated error condition.  If
		// there is an error, they display a "Could not foo because bar." message
		// in a dialog with an OK button.  If an optional recovery string is
		// provided, the message is "Could no foo because bar. Do this." message.
		// If "throwAfter" is true, Errors::Scram is called after the dialog
		// is dismissed.
		//
		// All of these functions bottleneck through DoDialog.
		//
		// "operation" and "recovery" are "kPart_Foo" values.
		// "error" is Mac or Windows error number, or a kError_Foo value.
		// "err" is a Palm OS error number.
		// "p" is a pointer to be tested.

		static void				ReportIfError		(StrCode operation, ErrCode error, StrCode recovery = 0, Bool throwAfter = true);
		static void				ReportIfPalmError	(StrCode operation, Err err, StrCode recovery = 0, Bool throwAfter = true);
		static void				ReportIfNULL		(StrCode operation, void* p, StrCode recovery = 0, Bool throwAfter = true);


		// Report that the indicated error condition occurred.  Messages are generally
		// of the form "<Application> <version> just tried to <foo>. <More explanatory
		// text>. Report this to the program author."

			// Hardware Exceptions

		static void				ReportErrBusError			(emuptr address, long size, Bool forRead);
		static void				ReportErrAddressError		(emuptr address, long size, Bool forRead);
		static void				ReportErrIllegalInstruction	(uint16 opcode);
		static void				ReportErrDivideByZero		(void);
		static void				ReportErrCHKInstruction		(void);
		static void				ReportErrTRAPVInstruction	(void);
		static void				ReportErrPrivilegeViolation	(uint16 opcode);
		static void				ReportErrTrace				(void);
		static void				ReportErrATrap				(uint16 opcode);
		static void				ReportErrFTrap				(uint16 opcode);
		static void				ReportErrTRAPx				(int exceptionNumber);

			// Special cases to hardware exceptions

		static void				ReportErrStorageHeap		(emuptr address, long size, Bool forRead);
		static void				ReportErrNoDrawWindow		(emuptr address, long size, Bool forRead);
		static void				ReportErrNoGlobals			(emuptr address, long size, Bool forRead);
		static void				ReportErrSANE				(void);
		static void				ReportErrTRAP0				(void);
		static void				ReportErrTRAP8				(void);

			// Fatal Poser-detected errors

		static void				ReportErrStackOverflow		(void);
		static void				ReportErrUnimplementedTrap	(const SystemCallContext&);
		static void				ReportErrInvalidRefNum		(const SystemCallContext&);
		static void				ReportErrCorruptedHeap		(ErrCode, emuptr);
		static void				ReportErrInvalidPC			(emuptr address, int reason);

			// Non-fatal Poser-detected errors

		static void				ReportErrLowMemory			(emuptr address, long size, Bool forRead);
		static void				ReportErrSystemGlobals		(emuptr address, long size, Bool forRead);
		static void				ReportErrScreen				(emuptr address, long size, Bool forRead);
		static void				ReportErrHardwareRegisters	(emuptr address, long size, Bool forRead);
		static void				ReportErrROM				(emuptr address, long size, Bool forRead);
		static void				ReportErrMemMgrStructures	(emuptr address, long size, Bool forRead);
		static void				ReportErrMemMgrLeaks		(int leaks);
		static void				ReportErrMemMgrSemaphore	(void);
		static void				ReportErrFreeChunk			(emuptr address, long size, Bool forRead);
		static void				ReportErrUnlockedChunk		(emuptr address, long size, Bool forRead);
		static void				ReportErrLowStack			(emuptr stackLow, emuptr stackPointer, emuptr stackHigh, emuptr address, long size, Bool forRead);
		static void				ReportErrStackFull			(void);
		static void				ReportErrSizelessObject		(uint16 id, const EmRect&);
		static void				ReportErrOffscreenObject	(uint16 id, const EmRect&);
		static void				ReportErrFormAccess			(emuptr formAddress, emuptr address, long size, Bool forRead);
		static void				ReportErrFormObjectAccess	(emuptr objectAddress, emuptr formAddress, emuptr address, long size, Bool forRead);
		static void				ReportErrWindowAccess		(emuptr windowAddress, emuptr address, long size, Bool forRead);
		static void				ReportErrBitmapAccess		(emuptr bitmapAddress, emuptr address, long size, Bool forRead);
		static void				ReportErrProscribedFunction	(const SystemCallContext&);
		static void				ReportErrStepSpy			(emuptr writeAddress, int writeBytes, emuptr ssAddress, uint32 ssValue, uint32 newValue);
		static void				ReportErrWatchpoint			(emuptr writeAddress, int writeBytes, emuptr watchAddress, uint32 watchBytes);

			// Palm OS-detected errors

		static void				ReportErrSysFatalAlert		(const char* appMsg);
		static void				ReportErrDbgMessage			(const char* appMsg);

			// Helper functions for handling reporting of similar errors.

		static void				ReportErrAccessCommon		(StrCode, ExceptionNumber, int flags, emuptr address, long size, Bool forRead);
		static void				ReportErrObjectCommon		(StrCode, ExceptionNumber, int flags, uint16 id, const EmRect&);
		static void				ReportErrOpcodeCommon		(StrCode, ExceptionNumber, int flags, uint16 opcode);
		static void				ReportErrStackCommon		(StrCode, ExceptionNumber, int flags);
		static void				ReportErrCommon				(StrCode, ExceptionNumber, int flags);
		static EmDlgItemID		ReportErrCommPort			(string);
		static EmDlgItemID		ReportErrSockets			(string);

		enum
		{
			kFatal = 0x01,
			kEnterDebuggerFirst = 0x02
		};

		enum EAccessType
		{
			kOKAccess,

			kLowMemAccess,
			kGlobalVarAccess,
			kScreenAccess,
			kMemMgrAccess,
			kLowStackAccess,
			kFreeChunkAccess,
			kUnlockedChunkAccess,

			kUnknownAccess
		};


		// Add a user-supplied substitution rule.  Parameters are deleted after they
		// are used (by making a call to ReplaceParameters), so they need to be
		// re-established after every call to that function.

		static void				SetParameter		(const string& key, const string& value);
		static void				SetParameter		(const string& key, const char* value);
		static void				SetParameter		(const string& key, const unsigned char* value);
		static void				SetParameter		(const string& key, long);
		static void				ClearParameter		(const string& key);
		static void				ClearAllParameters	(void);
		static Bool				SetErrorParameters	(StrCode operation, ErrCode error, StrCode recovery);
		static void				SetStandardParameters	(void);

		// Displays a dialog box using DoDialog, and handles the buttons
		// in a standard fashion.

		static void				HandleDialog		(StrCode messageID,
													 ExceptionNumber excNum,
													 EmCommonDialogFlags flags,
													 Bool enterDebuggerFirst);

		// Displays a dialog box with the given message and according to
		// the given flags.  Returns which button was clicked.

		static EmDlgItemID		DoDialog			(StrCode messageID, EmCommonDialogFlags);
		static EmDlgItemID		DoDialog			(const char* msg, EmCommonDialogFlags, StrCode messageID = -1);


		// Creates and returns a string based on the string template and
		// parameter list.

		static string			ReplaceParameters	(StrCode templateID);
		static string			ReplaceParameters	(StrCode templateID, ParamList& params);
		static string			ReplaceParameters	(const string&, ParamList& params);


		// Convert error numbers into string IDs that describe the error.

		static int				GetIDForError		(ErrCode error);
		static int				GetIDForRecovery	(ErrCode error);


		// Get the name and version of the current application.

		static void				GetAppName			(string& appNameUC,
													 string& appNameLC);
		static void				GetAppVersion		(string& appVersion);

		static Bool				LooksLikeA5Access	(emuptr address, long size, Bool forRead);


		// If an error condition is detected, throws an error number.
		// No message is reported.

		static void				Throw				(ErrCode error);
		static void				ThrowIfError		(ErrCode error);
		static void				ThrowIfPalmError	(Err error);
		static void				ThrowIfStdCError	(int error);
		static void				ThrowIfNULL			(void*);

		// Call this function as a "silent failure" bottleneck.
		// An exception will be thrown that will unwind the stack
		// to some top-level, but nothing will be reported.

		static void				Scram				(void);
};

class EmDeferredErr
{
	public:
								EmDeferredErr					(void);
		virtual					~EmDeferredErr					(void);

		virtual void			Do								(void) = 0;
};

class EmDeferredErrAccessCommon : public EmDeferredErr
{
	public:
								EmDeferredErrAccessCommon		(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrAccessCommon		(void);

	protected:
		emuptr					fAddress;
		long					fSize;
		Bool					fForRead;
};

class EmDeferredErrLowMemory : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrLowMemory			(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrLowMemory			(void);

		virtual void			Do								(void);
};

class EmDeferredErrSystemGlobals : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrSystemGlobals		(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrSystemGlobals		(void);

		virtual void			Do								(void);
};

class EmDeferredErrScreen : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrScreen				(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrScreen			(void);

		virtual void			Do								(void);
};

class EmDeferredErrHardwareRegisters : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrHardwareRegisters	(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrHardwareRegisters	(void);

		virtual void			Do								(void);
};

class EmDeferredErrROM : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrROM				(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrROM				(void);

		virtual void			Do								(void);
};

class EmDeferredErrMemMgrStructures : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrMemMgrStructures	(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrMemMgrStructures	(void);

		virtual void			Do								(void);
};

class EmDeferredErrMemMgrSemaphore : public EmDeferredErr
{
	public:
								EmDeferredErrMemMgrSemaphore	(void);
		virtual					~EmDeferredErrMemMgrSemaphore	(void);

		virtual void			Do								(void);
};

class EmDeferredErrFreeChunk : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrFreeChunk			(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrFreeChunk			(void);

		virtual void			Do								(void);
};

class EmDeferredErrUnlockedChunk : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrUnlockedChunk		(emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrUnlockedChunk		(void);

		virtual void			Do								(void);
};

class EmDeferredErrLowStack : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrLowStack			(emuptr stackLow, emuptr stackPointer, emuptr stackHigh, emuptr address, long size, Bool forRead);
		virtual					~EmDeferredErrLowStack			(void);

		virtual void			Do								(void);

	private:
		emuptr					fStackLow;
		emuptr					fStackPointer;
		emuptr					fStackHigh;
};

class EmDeferredErrStackFull : public EmDeferredErr
{
	public:
								EmDeferredErrStackFull			(void);
		virtual					~EmDeferredErrStackFull			(void);

		virtual void			Do								(void);
};

class EmDeferredErrObjectCommon : public EmDeferredErr
{
	public:
								EmDeferredErrObjectCommon		(uint16 id, const EmRect&);
		virtual					~EmDeferredErrObjectCommon		(void);

	protected:
		uint16					fID;
		EmRect					fRect;
};

class EmDeferredErrSizelessObject : public EmDeferredErrObjectCommon
{
	public:
								EmDeferredErrSizelessObject		(uint16 id, const EmRect&);
		virtual					~EmDeferredErrSizelessObject	(void);

		virtual void			Do								(void);
};

class EmDeferredErrOffscreenObject : public EmDeferredErrObjectCommon
{
	public:
								EmDeferredErrOffscreenObject	(uint16 id, const EmRect&);
		virtual					~EmDeferredErrOffscreenObject	(void);

		virtual void			Do								(void);
};

class EmDeferredErrFormAccess : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrFormAccess			(emuptr formAddress,
																 emuptr address,
																 long size,
																 Bool forRead);
		virtual					~EmDeferredErrFormAccess		(void);

		virtual void			Do								(void);

	protected:
		emuptr					fFormAddress;
};

class EmDeferredErrFormObjectAccess : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrFormObjectAccess	(emuptr objectAddress,
																 emuptr formAddress,
																 emuptr address,
																 long size,
																 Bool forRead);
		virtual					~EmDeferredErrFormObjectAccess	(void);

		virtual void			Do								(void);

	protected:
		emuptr					fObjectAddress;
		emuptr					fFormAddress;
};

class EmDeferredErrWindowAccess : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrWindowAccess		(emuptr windowAddress,
																 emuptr address,
																 long size,
																 Bool forRead);
		virtual					~EmDeferredErrWindowAccess		(void);

		virtual void			Do								(void);

		emuptr					fWindowAddress;
};

class EmDeferredErrBitmapAccess : public EmDeferredErrAccessCommon
{
	public:
								EmDeferredErrBitmapAccess		(emuptr bitmapAddress,
																 emuptr address,
																 long size,
																 Bool forRead);
		virtual					~EmDeferredErrBitmapAccess		(void);

		virtual void			Do								(void);

		emuptr					fBitmapAddress;
};

class EmDeferredErrProscribedFunction : public EmDeferredErr
{
	public:
								EmDeferredErrProscribedFunction	(const SystemCallContext&);
		virtual					~EmDeferredErrProscribedFunction(void);

		virtual void			Do								(void);

		SystemCallContext		fContext;
};

class EmDeferredErrStepSpy : public EmDeferredErr
{
	public:
								EmDeferredErrStepSpy			(emuptr writeAddress,
																 int writeBytes,
																 emuptr ssAddress,
																 uint32 ssValue,
																 uint32 newValue);
		virtual					~EmDeferredErrStepSpy			(void);

		virtual void			Do								(void);

	protected:
		emuptr					fWriteAddress;
		int						fWriteBytes;
		emuptr					fSSAddress;
		uint32					fSSValue;
		uint32					fNewValue;
};

class EmDeferredErrWatchpoint : public EmDeferredErr
{
	public:
								EmDeferredErrWatchpoint			(emuptr writeAddress,
																 int writeBytes,
																 emuptr watchAddress,
																 uint32 watchBytes);
		virtual					~EmDeferredErrWatchpoint		(void);

		virtual void			Do								(void);

	protected:
		emuptr					fWriteAddress;
		int						fWriteBytes;
		emuptr					fWatchAddress;
		uint32					fWatchBytes;
};

#endif /* _ERRORHANDLING_H_ */