File: crash_reporter_win32.cpp

package info (click to toggle)
clanlib 1.0~svn3827-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 24,696 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,742; ansic: 463; perl: 424; php: 247; sh: 53
file content (830 lines) | stat: -rw-r--r-- 27,179 bytes parent folder | download | duplicates (7)
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "Core/precomp.h"
#include "API/Core/System/crash_reporter.h"
#include "API/Core/System/log.h"
#include "crash_reporter_win32.h"

#ifdef _MSC_VER
#pragma warning (disable:4535)
#endif

/////////////////////////////////////////////////////////////////////////////
// CL_CrashReporter_Win32 Construction:

CL_CrashReporter_Win32::CL_CrashReporter_Win32()
{
#ifdef HAS_PDB_SUPPORT
	if (moduleImagehlp == 0)
	{
		moduleImagehlp = LoadLibrary( "imagehlp.dll" );
		if ( moduleImagehlp == 0 )
		{
			CL_Log::log("debug", "Unable to load imagehlp.dll. Crash reporter disabled.");
			return;
		}

		ptrSymCleanup = (TypeSymCleanup) GetProcAddress( moduleImagehlp, "SymCleanup" );
		ptrSymFunctionTableAccess = (TypeSymFunctionTableAccess) GetProcAddress( moduleImagehlp, "SymFunctionTableAccess" );
		ptrSymGetLineFromAddr = (TypeSymGetLineFromAddr) GetProcAddress( moduleImagehlp, "SymGetLineFromAddr" );
		ptrSymGetModuleBase = (TypeSymGetModuleBase) GetProcAddress( moduleImagehlp, "SymGetModuleBase" );
		ptrSymGetModuleInfo = (TypeSymGetModuleInfo) GetProcAddress( moduleImagehlp, "SymGetModuleInfo" );
		ptrSymGetOptions = (TypeSymGetOptions) GetProcAddress( moduleImagehlp, "SymGetOptions" );
		ptrSymGetSymFromAddr = (TypeSymGetSymFromAddr) GetProcAddress( moduleImagehlp, "SymGetSymFromAddr" );
		ptrSymInitialize = (TypeSymInitialize) GetProcAddress( moduleImagehlp, "SymInitialize" );
		ptrSymSetOptions = (TypeSymSetOptions) GetProcAddress( moduleImagehlp, "SymSetOptions" );
		ptrStackWalk = (TypeStackWalk) GetProcAddress( moduleImagehlp, "StackWalk" );
		ptrUnDecorateSymbolName = (TypeUnDecorateSymbolName) GetProcAddress( moduleImagehlp, "UnDecorateSymbolName" );
		ptrSymLoadModule = (TypeSymLoadModule) GetProcAddress( moduleImagehlp, "SymLoadModule" );

		if (
			ptrSymCleanup == 0 ||
			ptrSymFunctionTableAccess == 0 ||
			ptrSymGetLineFromAddr == 0 ||
			ptrSymGetModuleBase == 0 ||
			ptrSymGetModuleInfo == 0 ||
			ptrSymGetOptions == 0 ||
			ptrSymGetSymFromAddr == 0 ||
			ptrSymInitialize == 0 ||
			ptrSymSetOptions == 0 ||
			ptrStackWalk == 0 ||
			ptrUnDecorateSymbolName == 0 ||
			ptrSymLoadModule == 0)
		{
			CL_Log::log("debug", "Unable to find all symbols in imagehlp.dll. Crash reporter disabled.");
			FreeLibrary( moduleImagehlp );
			moduleImagehlp = 0;
			return;
		}

		// Setup search path for pdb (symbol information) files:

		#define TTBUFLEN 65536 // for a temp buffer
		char tt[TTBUFLEN];

		if ( GetCurrentDirectoryA( TTBUFLEN, tt ) )
			userSearchPath += tt + std::string( ";" );
		// dir with executable
		if ( GetModuleFileNameA( 0, tt, TTBUFLEN ) )
		{
			char *p;
			for ( p = tt + strlen( tt ) - 1; p >= tt; -- p )
			{
				// locate the rightmost path separator
				if ( *p == '\\' || *p == '/' || *p == ':' )
					break;
			}
			// if we found one, p is pointing at it; if not, tt only contains
			// an exe name (no path), and p points before its first byte
			if ( p != tt ) // path sep found?
			{
				if ( *p == ':' ) // we leave colons in place
					++ p;
				*p = '\0'; // eliminate the exe name and last path sep
				userSearchPath += tt + std::string( ";" );
			}
		}
		// environment variable _NT_SYMBOL_PATH
		if ( GetEnvironmentVariableA( "_NT_SYMBOL_PATH", tt, TTBUFLEN ) )
			userSearchPath += tt + std::string( ";" );
		// environment variable _NT_ALTERNATE_SYMBOL_PATH
		if ( GetEnvironmentVariableA( "_NT_ALTERNATE_SYMBOL_PATH", tt, TTBUFLEN ) )
			userSearchPath += tt + std::string( ";" );
		// environment variable SYSTEMROOT
		if ( GetEnvironmentVariableA( "SYSTEMROOT", tt, TTBUFLEN ) )
			userSearchPath += tt + std::string( ";" );

		if ( userSearchPath.size() > 0 ) // if we added anything, we have a trailing semicolon
			userSearchPath = userSearchPath.substr( 0, userSearchPath.size() - 1 );
	}
	
	if (moduleDbghlp == 0)
	{
		moduleDbghlp = LoadLibrary( "dbghelp.dll" );
		if ( moduleDbghlp == 0 )
		{
			CL_Log::log("debug", "Unable to load dbghlp.dll. Crash reporter disabled.");
			return;
		}

		ptrMiniDumpWriteDump = (TypeMiniDumpWriteDump) GetProcAddress( moduleDbghlp, "MiniDumpWriteDump" );

		if (ptrMiniDumpWriteDump == 0)
		{
			CL_Log::log("debug", "Unable to find all symbols in dbghlp.dll. Crash reporter disabled.");
			FreeLibrary( moduleDbghlp );
			moduleDbghlp = 0;
			return;
		}
	}

#ifdef HAS_SE_TRANSLATOR
	ptrOldFilter = _set_se_translator(&CL_CrashReporter_Win32::unhandledExceptionFilter);
#else
	ptrOldFilter = SetUnhandledExceptionFilter(&CL_CrashReporter_Win32::unhandledExceptionFilter);
#endif

#endif
}

CL_CrashReporter_Win32::~CL_CrashReporter_Win32()
{
#ifdef HAS_PDB_SUPPORT
#ifdef HAS_SE_TRANSLATOR
	_set_se_translator(ptrOldFilter);
#else
	// Dont do this since exception filter seem to be on process level and not thread level:
	// ptrOldFilter = SetUnhandledExceptionFilter(ptrOldFilter);
#endif
#endif

//	if (moduleImagehlp == 0) return;
//	FreeLibrary( moduleImagehlp );
//	moduleImagehlp = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CrashReporter Implementation:

#ifdef HAS_PDB_SUPPORT

struct DumpParams
{
	HANDLE hProcess;
	HANDLE hThread;
	int threadId;
	PEXCEPTION_POINTERS exceptionInfo;
	unsigned int exceptionCode;
};

#ifdef HAS_SE_TRANSLATOR
void CL_CrashReporter_Win32::unhandledExceptionFilter(unsigned int exceptionCode, PEXCEPTION_POINTERS exceptionInfo)
#else
LONG CL_CrashReporter_Win32::unhandledExceptionFilter(PEXCEPTION_POINTERS exceptionInfo)
#endif
{
#ifndef HAS_SE_TRANSLATOR
	unsigned int exceptionCode = GetExceptionCode();
#endif

	// Ignore those bloody breakpoints!
	if (exceptionCode == EXCEPTION_BREAKPOINT) return;

	DumpParams dumpParams;
	dumpParams.hProcess = GetCurrentProcess();
	dumpParams.hThread = GetCurrentThread();
	dumpParams.threadId = GetCurrentThreadId();
	dumpParams.exceptionInfo = exceptionInfo;
	dumpParams.exceptionCode = exceptionCode;

	DWORD threadId;
	HANDLE hThread = CreateThread(0, 0, &CL_CrashReporter_Win32::dumpStack, &dumpParams, 0, &threadId);
	WaitForSingleObject(hThread, INFINITE);

//	if (ptrOldFilter) ptrOldFilter(exceptionCode, exceptionInfo);
	abort();
#ifndef HAS_SE_TRANSLATOR
	return EXCEPTION_CONTINUE_SEARCH;
#endif
}

DWORD WINAPI CL_CrashReporter_Win32::dumpStack(LPVOID lpThreadParameter)
{
	DumpParams *dumpParams = (DumpParams *) lpThreadParameter;
	HANDLE hProcess = dumpParams->hProcess;
	HANDLE hThread = dumpParams->hThread;
	PEXCEPTION_POINTERS exceptionInfo = dumpParams->exceptionInfo;

	// Get path to executable:
	TCHAR szDllName[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	TCHAR szFilename[256];
	TCHAR szExt[256];

	GetModuleFileName(0, szDllName, _MAX_PATH);
	_splitpath(szDllName, szDrive, szDir, szFilename, szExt);

	TCHAR fileName[1024];

	// Produce minidump file:
	_snprintf(fileName, 1024, "%s%scoredump-%d.dmp", szDrive, szDir, dumpParams->threadId);
	HANDLE hFile = CreateFile(
		fileName,
		GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

	MINIDUMP_EXCEPTION_INFORMATION exInfo;

	exInfo.ThreadId = dumpParams->threadId;
	exInfo.ExceptionPointers = exceptionInfo;
	exInfo.ClientPointers = 0;

	ptrMiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &exInfo, 0, 0);

	CloseHandle(hFile);

	// why oh why does SymInitialize() want a writeable string?
	char tt[TTBUFLEN];
	strncpy( tt, userSearchPath.c_str(), TTBUFLEN );
	tt[TTBUFLEN - 1] = '\0'; // if strncpy() overruns, it doesn't add the null terminator

	ptrSymInitialize(hProcess, tt, FALSE);

	DWORD symOptions;
	symOptions = ptrSymGetOptions();
	symOptions |= SYMOPT_LOAD_LINES;
	symOptions &= ~SYMOPT_UNDNAME;
	ptrSymSetOptions( symOptions );

	#define MAXNAMELEN 1024 // max name length for found symbols
	#define IMGSYMLEN ( sizeof IMAGEHLP_SYMBOL )

	IMAGEHLP_SYMBOL *pSym = (IMAGEHLP_SYMBOL *) malloc( IMGSYMLEN + MAXNAMELEN );
	char undName[MAXNAMELEN]; // undecorated name
	char undFullName[MAXNAMELEN]; // undecorated name with all shenanigans
	IMAGEHLP_MODULE Module;
	IMAGEHLP_LINE Line;

	memset( pSym, '\0', IMGSYMLEN + MAXNAMELEN );
	pSym->SizeOfStruct = IMGSYMLEN;
	pSym->MaxNameLength = MAXNAMELEN;

	memset( &Line, '\0', sizeof Line );
	Line.SizeOfStruct = sizeof Line;

	memset( &Module, '\0', sizeof Module );
	Module.SizeOfStruct = sizeof Module;

	DWORD offsetFromSymbol; // tells us how far from the symbol we were
	offsetFromSymbol = 0;

	// Enumerate modules and tell imagehlp.dll about them.
	enumAndLoadModuleSymbols( hProcess, GetCurrentProcessId() );

	STACKFRAME stackframe; // in/out stackframe
	memset( &stackframe, 0, sizeof(stackframe) );
	stackframe.AddrPC.Offset = exceptionInfo->ContextRecord->Eip;
	stackframe.AddrPC.Mode = AddrModeFlat;
	stackframe.AddrFrame.Offset = exceptionInfo->ContextRecord->Ebp;
	stackframe.AddrFrame.Mode = AddrModeFlat;

	// normally, call ImageNtHeader() and use machine info from PE header
	DWORD imageType = IMAGE_FILE_MACHINE_I386;

	_snprintf(fileName, 1024, "%s%scoredump-%d.txt", szDrive, szDir, dumpParams->threadId);

	hFile = CreateFile(
		fileName,
		GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

	char stringBuffer[1024];
	DWORD bytesWritten;

	char *exceptionStr = "Unknown exception.";
	switch (dumpParams->exceptionCode)
	{
	case EXCEPTION_ACCESS_VIOLATION:
		exceptionStr = "EXCEPTION_ACCESS_VIOLATION, The thread attempted to read from or write to a virtual address for which it does not have the appropriate access.";
		break;

	case EXCEPTION_BREAKPOINT:
		exceptionStr = "EXCEPTION_BREAKPOINT, A breakpoint was encountered.";
		break;

	case EXCEPTION_DATATYPE_MISALIGNMENT:
		exceptionStr = "EXCEPTION_DATATYPE_MISALIGNMENT, The thread attempted to read or write data that is misaligned on hardware that does not provide alignment.";
		break;

	case EXCEPTION_SINGLE_STEP:
		exceptionStr = "EXCEPTION_SINGLE_STEP, A trace trap or other single-instruction mechanism signaled that one instruction has been executed.";
		break;

	case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
		exceptionStr = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED, The thread attempted to access an array element that is out of bounds, and the underlying hardware supports bounds checking.";
		break;

	case EXCEPTION_FLT_DENORMAL_OPERAND:
		exceptionStr = "EXCEPTION_FLT_DENORMAL_OPERAND, One of the operands in a floating-point operation is denormal.";
		break;

	case EXCEPTION_FLT_DIVIDE_BY_ZERO:
		exceptionStr = "EXCEPTION_FLT_DIVIDE_BY_ZERO, The thread attempted to divide a floating-point value by a floating-point divisor of zero.";
		break;

	case EXCEPTION_FLT_INEXACT_RESULT:
		exceptionStr = "EXCEPTION_FLT_INEXACT_RESULT, The result of a floating-point operation cannot be represented exactly as a decimal fraction.";
		break;

	case EXCEPTION_FLT_INVALID_OPERATION:
		exceptionStr = "EXCEPTION_FLT_INVALID_OPERATION, This exception represents any floating-point exception not included in this list.";
		break;

	case EXCEPTION_FLT_OVERFLOW:
		exceptionStr = "EXCEPTION_FLT_OVERFLOW, The exponent of a floating-point operation is greater than the magnitude allowed by the corresponding type.";
		break;

	case EXCEPTION_FLT_STACK_CHECK:
		exceptionStr = "EXCEPTION_FLT_STACK_CHECK, The stack overflowed or underflowed as the result of a floating-point operation.";
		break;

	case EXCEPTION_FLT_UNDERFLOW:
		exceptionStr = "EXCEPTION_FLT_UNDERFLOW, The exponent of a floating-point operation is less than the magnitude allowed by the corresponding type.";
		break;

	case EXCEPTION_INT_DIVIDE_BY_ZERO:
		exceptionStr = "EXCEPTION_INT_DIVIDE_BY_ZERO, The thread attempted to divide an integer value by an integer divisor of zero.";
		break;

	case EXCEPTION_INT_OVERFLOW:
		exceptionStr = "EXCEPTION_INT_OVERFLOW, The result of an integer operation caused a carry out of the most significant bit of the result.";
		break;

	case EXCEPTION_PRIV_INSTRUCTION:
		exceptionStr = "EXCEPTION_PRIV_INSTRUCTION, The thread attempted to execute an instruction whose operation is not allowed in the current machine mode.";
		break;

	case EXCEPTION_NONCONTINUABLE_EXCEPTION:
		exceptionStr = "EXCEPTION_NONCONTINUABLE_EXCEPTION, The thread attempted to continue execution after a noncontinuable exception occurred.";
		break;
	}

	_snprintf(stringBuffer, 1024, "Exception in thread %d: %s\r\n", dumpParams->threadId, exceptionStr);
	WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);

	_snprintf(stringBuffer, 1024, "-- Begin stack trace --\r\n");
	WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);

	_snprintf(stringBuffer, 1024, " Nr Flags PC       Return   Frame    Stack\r\n");
	WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);

	int frameNum = 0;
	while (true)
	{
		// if this returns ERROR_INVALID_ADDRESS (487) or ERROR_NOACCESS (998), you can
		// assume that either you are done, or that the stack is so hosed that the next
		// deeper frame could not be found.

		if (!ptrStackWalk(
				imageType,
				hProcess,
				hThread,
				&stackframe,
				exceptionInfo->ContextRecord,
				0,
				ptrSymFunctionTableAccess,
				ptrSymGetModuleBase,
				0 ) )
			break;

		_snprintf(
			stringBuffer,
			1024,
			"%3d %c%c    %08lx %08lx %08lx %08lx ",
			frameNum,
			stackframe.Far? 'F': '.',
			stackframe.Virtual? 'V': '.',
			stackframe.AddrPC.Offset,
			stackframe.AddrReturn.Offset,
			stackframe.AddrFrame.Offset,
			stackframe.AddrStack.Offset );

		WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);

		if ( stackframe.AddrPC.Offset == 0 )
		{
			_snprintf(stringBuffer, 1024, "(-nosymbols- PC == 0)\r\n" );
			WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
		}
		else
		{
			// we seem to have a valid PC

			// show procedure info (SymGetSymFromAddr())
			if ( ! ptrSymGetSymFromAddr( hProcess, stackframe.AddrPC.Offset, &offsetFromSymbol, pSym ) )
			{
				if ( GetLastError() != 487 )
				{
					_snprintf(stringBuffer, 1024, "SymGetSymFromAddr(): gle = %lu\r\n", GetLastError() );
					WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				}
			}
			else
			{
				// UnDecorateSymbolName()
				ptrUnDecorateSymbolName( pSym->Name, undName, MAXNAMELEN, UNDNAME_NAME_ONLY );
				ptrUnDecorateSymbolName( pSym->Name, undFullName, MAXNAMELEN, UNDNAME_COMPLETE );
				_snprintf(stringBuffer, 1024, "%s", undName );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				if ( offsetFromSymbol != 0 )
				{
					_snprintf(stringBuffer, 1024, " %+ld bytes", (long) offsetFromSymbol );
					WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				}
				WriteFile(hFile, "\r\n", 2, &bytesWritten, 0 );
				_snprintf(stringBuffer, 1024, "    Sig:  %s\r\n", pSym->Name );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				_snprintf(stringBuffer, 1024, "    Decl: %s\r\n", undFullName );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
			}

			// show line number info, NT5.0-method (SymGetLineFromAddr())
			if ( ptrSymGetLineFromAddr != 0 )
			{ // yes, we have SymGetLineFromAddr()
				if ( ! ptrSymGetLineFromAddr( hProcess, stackframe.AddrPC.Offset, &offsetFromSymbol, &Line ) )
				{
					if ( GetLastError() != 487 )
					{
						_snprintf(stringBuffer, 1024, "SymGetLineFromAddr(): gle = %lu\r\n", GetLastError() );
						WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
					}
				}
				else
				{
					_snprintf(stringBuffer, 1024, "    Line: %s(%lu) %+ld bytes\r\n",
						Line.FileName, Line.LineNumber, offsetFromSymbol );
					WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				}
			}

			// show module info (SymGetModuleInfo())
			if ( ! ptrSymGetModuleInfo( hProcess, stackframe.AddrPC.Offset, &Module ) )
			{
				_snprintf(stringBuffer, 1024, "SymGetModuleInfo): gle = %lu\r\n", GetLastError() );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
			}
			else
			{ // got module info OK
				char ty[80];
				switch ( Module.SymType )
				{
				case SymNone:
					strcpy( ty, "-nosymbols-" );
					break;
				case SymCoff:
					strcpy( ty, "COFF" );
					break;
				case SymCv:
					strcpy( ty, "CV" );
					break;
				case SymPdb:
					strcpy( ty, "PDB" );
					break;
				case SymExport:
					strcpy( ty, "-exported-" );
					break;
				case SymDeferred:
					strcpy( ty, "-deferred-" );
					break;
				case SymSym:
					strcpy( ty, "SYM" );
					break;
				default:
					_snprintf( ty, sizeof ty, "symtype=%ld", (long) Module.SymType );
					break;
				}

				_snprintf(stringBuffer, 1024, "    Mod:  %s[%s], base: %08lxh\r\n",
					Module.ModuleName, Module.ImageName, Module.BaseOfImage );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
				_snprintf(stringBuffer, 1024, "    Sym:  type: %s, file: %s\r\n",
					ty, Module.LoadedImageName );
				WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);
			}
		}

		// no return address means no deeper stackframe
		if ( stackframe.AddrReturn.Offset == 0 ) break;

		frameNum++;
	}

	_snprintf(stringBuffer, 1024, "-- End stack trace --\r\n");
	WriteFile(hFile, stringBuffer, strlen(stringBuffer), &bytesWritten, 0);

	CloseHandle(hFile);

	ptrSymCleanup(hProcess);

	// todo: signal CL_CrashReporter::sig_crash here
/*
	hFile = CreateFile(
		fileName,
		GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);

	int pos = 0;
	DWORD bytesRead;
	ReadFile(hFile, stringBuffer, 1024, &bytesRead, 0);
	stringBuffer[bytesRead] = 0;
	while (bytesRead > 0)
	{
		char *ptrNewline = strstr(stringBuffer, "\r\n");
		if (ptrNewline)
		{
			ptrNewline[0] = 0;
			CL_Log::log("debug", stringBuffer);
			pos += ptrNewline-stringBuffer+2;
			SetFilePointer(hFile, pos, 0, FILE_BEGIN);
		}

		ReadFile(hFile, stringBuffer, 1024, &bytesRead, 0);
		stringBuffer[bytesRead] = 0;
	}

	CloseHandle(hFile);
*/

	return 0;
}

void CL_CrashReporter_Win32::enumAndLoadModuleSymbols( HANDLE hProcess, DWORD pid )
{
	ModuleList modules;
	ModuleListIter it;
	char *img, *mod;

	// fill in module list
	fillModuleList( modules, pid, hProcess );

	for ( it = modules.begin(); it != modules.end(); ++ it )
	{
		// unfortunately, SymLoadModule() wants writeable strings
		img = new char[(*it).imageName.size() + 1];
		strcpy( img, (*it).imageName.c_str() );
		mod = new char[(*it).moduleName.size() + 1];
		strcpy( mod, (*it).moduleName.c_str() );

		ptrSymLoadModule( hProcess, 0, img, mod, (*it).baseAddress, (*it).size );
/*		if ( ptrSymLoadModule( hProcess, 0, img, mod, (*it).baseAddress, (*it).size ) == 0 )
			printf( "Error %lu loading symbols for \"%s\"\n",
				GetLastError(), (*it).moduleName.c_str() );
		else
			printf( "Symbols loaded: \"%s\"\n", (*it).moduleName.c_str() );
*/
		delete [] img;
		delete [] mod;
	}
}



bool CL_CrashReporter_Win32::fillModuleList( ModuleList& modules, DWORD pid, HANDLE hProcess )
{
	// try toolhelp32 first
	if ( fillModuleListTH32( modules, pid ) )
		return true;
	// nope? try psapi, then
	return fillModuleListPSAPI( modules, pid, hProcess );
}



// miscellaneous toolhelp32 declarations; we cannot #include the header
// because not all systems may have it
#define MAX_MODULE_NAME32 255
#define TH32CS_SNAPMODULE   0x00000008
#pragma pack( push, 8 )
typedef struct tagMODULEENTRY32
{
    DWORD   dwSize;
    DWORD   th32ModuleID;       // This module
    DWORD   th32ProcessID;      // owning process
    DWORD   GlblcntUsage;       // Global usage count on the module
    DWORD   ProccntUsage;       // Module usage count in th32ProcessID's context
    BYTE  * modBaseAddr;        // Base address of module in th32ProcessID's context
    DWORD   modBaseSize;        // Size in bytes of module starting at modBaseAddr
    HMODULE hModule;            // The hModule of this module in th32ProcessID's context
    char    szModule[MAX_MODULE_NAME32 + 1];
    char    szExePath[MAX_PATH];
} MODULEENTRY32;
typedef MODULEENTRY32 *  PMODULEENTRY32;
typedef MODULEENTRY32 *  LPMODULEENTRY32;
#pragma pack( pop )



bool CL_CrashReporter_Win32::fillModuleListTH32( ModuleList& modules, DWORD pid )
{
	// CreateToolhelp32Snapshot()
	typedef HANDLE (__stdcall *tCT32S)( DWORD dwFlags, DWORD th32ProcessID );
	// Module32First()
	typedef BOOL (__stdcall *tM32F)( HANDLE hSnapshot, LPMODULEENTRY32 lpme );
	// Module32Next()
	typedef BOOL (__stdcall *tM32N)( HANDLE hSnapshot, LPMODULEENTRY32 lpme );

	// I think the DLL is called tlhelp32.dll on Win9X, so we try both
	const char *dllname[] = { "kernel32.dll", "tlhelp32.dll" };
	HINSTANCE hToolhelp;
	tCT32S pCT32S;
	tM32F pM32F;
	tM32N pM32N;

	HANDLE hSnap;
	MODULEENTRY32 me = { sizeof me };
	bool keepGoing;
	ModuleEntry e;
	int i;

	#define lenof(a) (sizeof(a) / sizeof((a)[0]))
	for ( i = 0; i < lenof( dllname ); ++ i )
	{
		hToolhelp = LoadLibraryA( dllname[i] );
		if ( hToolhelp == 0 )
			continue;
		pCT32S = (tCT32S) GetProcAddress( hToolhelp, "CreateToolhelp32Snapshot" );
		pM32F = (tM32F) GetProcAddress( hToolhelp, "Module32First" );
		pM32N = (tM32N) GetProcAddress( hToolhelp, "Module32Next" );
		if ( pCT32S != 0 && pM32F != 0 && pM32N != 0 )
			break; // found the functions!
		FreeLibrary( hToolhelp );
		hToolhelp = 0;
	}

	if ( hToolhelp == 0 ) // nothing found?
		return false;

	hSnap = pCT32S( TH32CS_SNAPMODULE, pid );
	if ( hSnap == (HANDLE) -1 )
		return false;

	keepGoing = !!pM32F( hSnap, &me );
	while ( keepGoing )
	{
		// here, we have a filled-in MODULEENTRY32
//		printf( "%08lXh %6lu %-15.15s %s\n", me.modBaseAddr, me.modBaseSize, me.szModule, me.szExePath );
		e.imageName = me.szExePath;
		e.moduleName = me.szModule;
		e.baseAddress = (DWORD) me.modBaseAddr;
		e.size = me.modBaseSize;
		modules.push_back( e );
		keepGoing = !!pM32N( hSnap, &me );
	}

	CloseHandle( hSnap );

	FreeLibrary( hToolhelp );

	return modules.size() != 0;
}



// miscellaneous psapi declarations; we cannot #include the header
// because not all systems may have it
typedef struct _MODULEINFO {
    LPVOID lpBaseOfDll;
    DWORD SizeOfImage;
    LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;



bool CL_CrashReporter_Win32::fillModuleListPSAPI( ModuleList& modules, DWORD pid, HANDLE hProcess )
{
	// EnumProcessModules()
	typedef BOOL (__stdcall *tEPM)( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded );
	// GetModuleFileNameEx()
	typedef DWORD (__stdcall *tGMFNE)( HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
	// GetModuleBaseName() -- redundant, as GMFNE() has the same prototype, but who cares?
	typedef DWORD (__stdcall *tGMBN)( HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
	// GetModuleInformation()
	typedef BOOL (__stdcall *tGMI)( HANDLE hProcess, HMODULE hModule, LPMODULEINFO pmi, DWORD nSize );

	HINSTANCE hPsapi;
	tEPM pEPM;
	tGMFNE pGMFNE;
	tGMBN pGMBN;
	tGMI pGMI;

	unsigned int i;
	ModuleEntry e;
	DWORD cbNeeded;
	MODULEINFO mi;
	HMODULE *hMods = 0;
	char *tt = 0;

	hPsapi = LoadLibrary( "psapi.dll" );
	if ( hPsapi == 0 )
		return false;

	modules.clear();

	pEPM = (tEPM) GetProcAddress( hPsapi, "EnumProcessModules" );
	pGMFNE = (tGMFNE) GetProcAddress( hPsapi, "GetModuleFileNameExA" );
	pGMBN = (tGMFNE) GetProcAddress( hPsapi, "GetModuleBaseNameA" );
	pGMI = (tGMI) GetProcAddress( hPsapi, "GetModuleInformation" );
	if ( pEPM == 0 || pGMFNE == 0 || pGMBN == 0 || pGMI == 0 )
	{
		// yuck. Some API is missing.
		FreeLibrary( hPsapi );
		return false;
	}

	hMods = new HMODULE[TTBUFLEN / sizeof HMODULE];
	tt = new char[TTBUFLEN];
	// not that this is a sample. Which means I can get away with
	// not checking for errors, but you cannot. :)

	if ( ! pEPM( hProcess, hMods, TTBUFLEN, &cbNeeded ) )
	{
//		printf( "EPM failed, gle = %lu\n", GetLastError() );
		goto cleanup;
	}

	if ( cbNeeded > TTBUFLEN )
	{
//		printf( "More than %lu module handles. Huh?\n", lenof( hMods ) );
		goto cleanup;
	}

	for ( i = 0; i < cbNeeded / sizeof hMods[0]; ++ i )
	{
		// for each module, get:
		// base address, size
		pGMI( hProcess, hMods[i], &mi, sizeof mi );
		e.baseAddress = (DWORD) mi.lpBaseOfDll;
		e.size = mi.SizeOfImage;
		// image file name
		tt[0] = '\0';
		pGMFNE( hProcess, hMods[i], tt, TTBUFLEN );
		e.imageName = tt;
		// module name
		tt[0] = '\0';
		pGMBN( hProcess, hMods[i], tt, TTBUFLEN );
		e.moduleName = tt;
//		printf( "%08lXh %6lu %-15.15s %s\n", e.baseAddress,
//			e.size, e.moduleName.c_str(), e.imageName.c_str() );

		modules.push_back( e );
	}

cleanup:
	if ( hPsapi )
		FreeLibrary( hPsapi );
	delete [] tt;
	delete [] hMods;

	return modules.size() != 0;
}

HINSTANCE CL_CrashReporter_Win32::moduleImagehlp = 0;

HINSTANCE CL_CrashReporter_Win32::moduleDbghlp = 0;

CL_CrashReporter_Win32::TypeSymCleanup CL_CrashReporter_Win32::ptrSymCleanup = 0;

CL_CrashReporter_Win32::TypeSymFunctionTableAccess CL_CrashReporter_Win32::ptrSymFunctionTableAccess = 0;

CL_CrashReporter_Win32::TypeSymGetLineFromAddr CL_CrashReporter_Win32::ptrSymGetLineFromAddr = 0;

CL_CrashReporter_Win32::TypeSymGetModuleBase CL_CrashReporter_Win32::ptrSymGetModuleBase = 0;

CL_CrashReporter_Win32::TypeSymGetModuleInfo CL_CrashReporter_Win32::ptrSymGetModuleInfo = 0;

CL_CrashReporter_Win32::TypeSymGetOptions CL_CrashReporter_Win32::ptrSymGetOptions = 0;

CL_CrashReporter_Win32::TypeSymGetSymFromAddr CL_CrashReporter_Win32::ptrSymGetSymFromAddr = 0;

CL_CrashReporter_Win32::TypeSymInitialize CL_CrashReporter_Win32::ptrSymInitialize = 0;

CL_CrashReporter_Win32::TypeSymLoadModule CL_CrashReporter_Win32::ptrSymLoadModule = 0;

CL_CrashReporter_Win32::TypeSymSetOptions CL_CrashReporter_Win32::ptrSymSetOptions = 0;

CL_CrashReporter_Win32::TypeStackWalk CL_CrashReporter_Win32::ptrStackWalk = 0;

CL_CrashReporter_Win32::TypeUnDecorateSymbolName CL_CrashReporter_Win32::ptrUnDecorateSymbolName = 0;

CL_CrashReporter_Win32::TypeMiniDumpWriteDump CL_CrashReporter_Win32::ptrMiniDumpWriteDump = 0;

_se_translator_function CL_CrashReporter_Win32::ptrOldFilter = 0;

std::string CL_CrashReporter_Win32::userSearchPath;

#endif