summaryrefslogblamecommitdiff
path: root/src/plugins/dbghelpplug.c
blob: 108922bdd0c56d2674bdbae58df526324c034e7a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
                                                           
                       


                                                                                                                     




                           




                                   
                                      



                        

  


                                                   

                                                                     
                













                                                                     
                                 



                    
                         


























                     
                      







































































































































































                                                                     
                                                                  























































































                                                                         
                                                                    




























































































                                                                                                                                                                       
                                                                         



































































                                                                                                                            
                 






































































































































































































































































































































































































































































































































                                                                                                                              
                                                                                                                  












































































































































































































































































                                                                                                                                            
                               
         
                                                                           







































































































































































































































































































































































































































































































                                                                                                                        
                                                                     
         



                                                   




                                                                            


                                                                                    




                                   

                                                    
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams

// Ported from eAthena Dev Team's version @ http://eathena-project.googlecode.com/svn/trunk/src/plugins/dbghelpplug.c

#include <stdio.h>
#include <string.h>
#include "../common/HPMi.h"

#include "../common/HPMDataCheck.h"

/**
 * Plugin basic information
 **/
HPExport struct hplugin_info pinfo = {
	"Debug Help",
	SERVER_TYPE_ALL,
	"0.4",
	HPM_VERSION,
};

#ifndef WIN32
#error This plugin is only compatible with windows!
#endif

/////////////////////////////////////////////////////////////////////
// Include files
//
#include <assert.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define _NO_CVCONST_H
#include <dbghelp.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/////////////////////////////////////////////////////////////////////
// Types from Cvconst.h (DIA SDK)
//

#ifdef _NO_CVCONST_H

typedef enum _BasicType {
   btNoType   = 0,
   btVoid     = 1,
   btChar     = 2,
   btWChar    = 3,
   btInt      = 6,
   btUInt     = 7,
   btFloat    = 8,
   btBCD      = 9,
   btBool     = 10,
   btLong     = 13,
   btULong    = 14,
   btCurrency = 25,
   btDate     = 26,
   btVariant  = 27,
   btComplex  = 28,
   btBit      = 29,
   btBSTR     = 30,
   btHresult  = 31
} BasicType;

typedef enum _UdtKind
{
    UdtStruct,
    UdtClass,
    UdtUnion
} UdtKind;
/*
typedef enum _SymTag {
   SymTagNull             = 0,
   SymTagExe              = 1,
   SymTagCompiland        = 2,
   SymTagCompilandDetails = 3,
   SymTagCompilandEnv     = 4,
   SymTagFunction         = 5,
   SymTagBlock            = 6,
   SymTagData             = 7,
   SymTagAnnotation       = 8,
   SymTagLabel            = 9,
   SymTagPublicSymbol     = 10,
   SymTagUDT              = 11,
   SymTagEnum             = 12,
   SymTagFunctionType     = 13,
   SymTagPointerType      = 14,
   SymTagArrayType        = 15,
   SymTagBaseType         = 16,
   SymTagTypedef          = 17,
   SymTagBaseClass        = 18,
   SymTagFriend           = 19,
   SymTagFunctionArgType  = 20,
   SymTagFuncDebugStart   = 21,
   SymTagFuncDebugEnd     = 22,
   SymTagUsingNamespace   = 23,
   SymTagVTableShape      = 24,
   SymTagVTable           = 25,
   SymTagCustom           = 26,
   SymTagThunk            = 27,
   SymTagCustomType       = 28,
   SymTagManagedType      = 29,
   SymTagDimension        = 30
} SymTag;
*/
#endif /* _NO_CVCONST_H */


/////////////////////////////////////////////////////////////////////
// dbghelp function prototypes
//

typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(
	HANDLE hProcess,
	DWORD ProcessId,
	HANDLE hFile,
	MINIDUMP_TYPE DumpType,
	CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
	CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
	CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
);
typedef BOOL (WINAPI *SYMINITIALIZE)(
	HANDLE  hProcess,
	PSTR    UserSearchPath,
	BOOL    fInvadeProcess
);
typedef DWORD (WINAPI *SYMSETOPTIONS)(
	DWORD   SymOptions
);
typedef DWORD (WINAPI *SYMGETOPTIONS)(
	VOID
);
typedef BOOL (WINAPI *SYMCLEANUP)(
	HANDLE  hProcess
);
typedef BOOL (WINAPI *SYMGETTYPEINFO)(
	HANDLE                      hProcess,
	DWORD64                     ModBase,
	ULONG                       TypeId,
	IMAGEHLP_SYMBOL_TYPE_INFO   GetType,
	PVOID                       pInfo
);
typedef BOOL (WINAPI *SYMGETLINEFROMADDR)(
	HANDLE          hProcess,
	DWORD           dwAddr,
	PDWORD          pdwDisplacement,
	PIMAGEHLP_LINE  Line
);
typedef BOOL (WINAPI *SYMENUMSYMBOLS)(
	HANDLE                          hProcess,
	ULONG64                         BaseOfDll,
	PCSTR                           Mask,
	PSYM_ENUMERATESYMBOLS_CALLBACK  EnumSymbolsCallback,
	PVOID                           UserContext
);
typedef BOOL (WINAPI *SYMSETCONTEXT)(
	HANDLE                  hProcess,
	PIMAGEHLP_STACK_FRAME   StackFrame,
	PIMAGEHLP_CONTEXT       Context
);
typedef BOOL (WINAPI *SYMFROMADDR)(
	HANDLE          hProcess,
	DWORD64         Address,
	PDWORD64        Displacement,
	PSYMBOL_INFO    Symbol
);
typedef BOOL (WINAPI *STACKWALK)(
	DWORD                           MachineType,
	HANDLE                          hProcess,
	HANDLE                          hThread,
	LPSTACKFRAME                    StackFrame,
	PVOID                           ContextRecord,
	PREAD_PROCESS_MEMORY_ROUTINE    ReadMemoryRoutine,
	PFUNCTION_TABLE_ACCESS_ROUTINE  FunctionTableAccessRoutine,
	PGET_MODULE_BASE_ROUTINE        GetModuleBaseRoutine,
	PTRANSLATE_ADDRESS_ROUTINE      TranslateAddress
);
typedef PVOID (WINAPI *SYMFUNCTIONTABLEACCESS)(
	HANDLE  hProcess,
	DWORD   AddrBase
);
typedef DWORD (WINAPI *SYMGETMODULEBASE)(
	HANDLE  hProcess,
	DWORD   dwAddr
);

/////////////////////////////////////////////////////////////////////
// Custom info

/// Internal structure used to pass some data around
typedef struct _InternalData {
	// PrintStacktrace
	FILE* log_file;
	STACKFRAME* pStackframe;
	HANDLE hProcess;
	DWORD nr_of_frame;

	// PrintFunctionDetail
	BOOL as_arg_list;
	BOOL log_params;
	BOOL log_locals;
	BOOL log_globals;
	DWORD nr_of_var;

	// PrintDataInfo
	ULONG64 modBase;
} InterData;

/// dbghelp dll filename
#define DBGHELP_DLL "dbghelp.dll"

// Default report filename, used when the module path is unavailable
#define DBG_DEFAULT_FILENAME "hercules"

// Extended information printed in the console
#define DBG_EXTENDED_INFORMATION \
		"Please report the crash in the bug tracker:\n" \
		"http://hercules.ws/board/tracker/\n"

/////////////////////////////////////////////////////////////////////
// Global variables

HANDLE dbghelp_dll = INVALID_HANDLE_VALUE;
MINIDUMPWRITEDUMP MiniDumpWriteDump_ = NULL;
SYMINITIALIZE SymInitialize_ = NULL;
SYMSETOPTIONS SymSetOptions_ = NULL;
SYMGETOPTIONS SymGetOptions_ = NULL;
SYMCLEANUP SymCleanup_ = NULL;
SYMGETTYPEINFO SymGetTypeInfo_ = NULL;
SYMGETLINEFROMADDR SymGetLineFromAddr_ = NULL;
SYMENUMSYMBOLS SymEnumSymbols_ = NULL;
SYMSETCONTEXT SymSetContext_ = NULL;
SYMFROMADDR SymFromAddr_ = NULL;
STACKWALK StackWalk_ = NULL;
SYMFUNCTIONTABLEACCESS SymFunctionTableAccess_ = NULL;
SYMGETMODULEBASE SymGetModuleBase_ = NULL;

/////////////////////////////////////////////////////////////////////
// Code

/// Writes the minidump to file. The callback function will at the
/// same time write the list of modules to the log file.
///
/// @param file Filename of the minidump
/// @param ptrs Exception info
/// @param module_callback Callback for MiniDumpWriteDump
/// @param log_file Log file
static VOID
Dhp__WriteMinidumpFile(
	const char*                 file,
	PEXCEPTION_POINTERS         ptrs,
	MINIDUMP_CALLBACK_ROUTINE   module_callback,
	FILE*                       log_file)
{
	// open minidump file
	HANDLE minidump_file = CreateFileA(
		file,
		GENERIC_WRITE,
		0,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);

	if( minidump_file != INVALID_HANDLE_VALUE )
	{
		MINIDUMP_EXCEPTION_INFORMATION expt_info;
		MINIDUMP_CALLBACK_INFORMATION dump_cb_info;

		expt_info.ThreadId = GetCurrentThreadId();
		expt_info.ExceptionPointers = ptrs;
		expt_info.ClientPointers = FALSE;

		dump_cb_info.CallbackRoutine = module_callback;
		dump_cb_info.CallbackParam = (void*)log_file;

		if( module_callback != NULL && log_file != NULL )
			fprintf(log_file, "\n\nLoaded modules:\n");

		MiniDumpWriteDump_(
			GetCurrentProcess(),
			GetCurrentProcessId(),
			minidump_file,
			MiniDumpNormal,
			ptrs ? &expt_info : NULL,
			NULL,
			&dump_cb_info
		);

		CloseHandle(minidump_file);
	}
}

/// Prints module information to the log file.
/// Used as a callback to MiniDumpWriteDump.
///
/// @param data Log file
/// @param callback_input
/// @param callback_output
/// @return
static BOOL CALLBACK
Dhp__PrintModuleInfoCallback(
	void*                           data,
	CONST PMINIDUMP_CALLBACK_INPUT  callback_input,
	PMINIDUMP_CALLBACK_OUTPUT       callback_output)
{
	if( data != NULL &&
		callback_input != NULL &&
		callback_input->CallbackType == ModuleCallback)
	{
		FILE* log_file = (FILE*)data;
		MINIDUMP_MODULE_CALLBACK module = callback_input->Module;

		fprintf(log_file, "0x%p", module.BaseOfImage);

		fprintf(log_file, "  %ws", module.FullPath, log_file);

		fprintf(log_file, "  (%d.%d.%d.%d, %d bytes)\n",
			HIWORD(module.VersionInfo.dwFileVersionMS),
			LOWORD(module.VersionInfo.dwFileVersionMS),
			HIWORD(module.VersionInfo.dwFileVersionLS),
			LOWORD(module.VersionInfo.dwFileVersionLS),
			module.SizeOfImage);
	}

	return TRUE;
}

/// Prints details about the current process, platform and exception
/// information to the log file.
///
/// @param exception Exception info
/// @param context Exception context
/// @param log_file Log file
static VOID
Dhp__PrintProcessInfo(
	EXCEPTION_RECORD*   exception,
	CONTEXT*            context,
	FILE*               log_file)
{
	OSVERSIONINFOA oi;
	LPSTR cmd_line;

	fprintf(log_file,
		"\nProcess info:\n");

	// print the command line
	cmd_line = GetCommandLineA();
	if( cmd_line )
	fprintf(log_file,
		"Cmd line: %s\n",
		cmd_line);

	// print information about the OS
	oi.dwOSVersionInfoSize = sizeof(oi);
	GetVersionExA(&oi);
	fprintf(log_file,
		"Platform: Windows OS version %d.%d build %d %s\n",
		oi.dwMajorVersion, oi.dwMinorVersion, oi.dwBuildNumber, oi.szCSDVersion);

	// print the exception code
	if( exception )
	{
		fprintf(log_file,
			"\nException:\n"
			"0x%x",
			exception->ExceptionCode);
		switch( exception->ExceptionCode )
		{
#define PRINT(x) case x: fprintf(log_file, " "#x); break
		PRINT(EXCEPTION_ACCESS_VIOLATION);
		PRINT(EXCEPTION_DATATYPE_MISALIGNMENT);
		PRINT(EXCEPTION_BREAKPOINT);
		PRINT(EXCEPTION_SINGLE_STEP);
		PRINT(EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
		PRINT(EXCEPTION_FLT_DENORMAL_OPERAND);
		PRINT(EXCEPTION_FLT_DIVIDE_BY_ZERO);
		PRINT(EXCEPTION_FLT_INEXACT_RESULT);
		PRINT(EXCEPTION_FLT_INVALID_OPERATION);
		PRINT(EXCEPTION_FLT_OVERFLOW);
		PRINT(EXCEPTION_FLT_STACK_CHECK);
		PRINT(EXCEPTION_FLT_UNDERFLOW);
		PRINT(EXCEPTION_INT_DIVIDE_BY_ZERO);
		PRINT(EXCEPTION_INT_OVERFLOW);
		PRINT(EXCEPTION_PRIV_INSTRUCTION);
		PRINT(EXCEPTION_IN_PAGE_ERROR);
		PRINT(EXCEPTION_ILLEGAL_INSTRUCTION);
		PRINT(EXCEPTION_NONCONTINUABLE_EXCEPTION);
		PRINT(EXCEPTION_STACK_OVERFLOW);
		PRINT(EXCEPTION_INVALID_DISPOSITION);
		PRINT(EXCEPTION_GUARD_PAGE);
		PRINT(EXCEPTION_INVALID_HANDLE);
#undef PRINT
		}

		// print where the fault occured
		fprintf(log_file, " at location 0x%p", exception->ExceptionAddress);

		// if the exception was an access violation, print additional information
		if( exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && exception->NumberParameters >= 2 )
			fprintf(log_file, " %s location 0x%p", exception->ExceptionInformation[0] ? "writing to" : "reading from", exception->ExceptionInformation[1]);

		fprintf(log_file, "\n");
	}

	// print the register info
	if( context )
	{
#if defined(_M_IX86)
		fprintf(log_file,
			"\nRegisters:\n");
		if( context->ContextFlags & CONTEXT_INTEGER )
		{
			fprintf(log_file,
				"eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
				context->Eax, context->Ebx, context->Ecx,
				context->Edx, context->Esi, context->Edi);
		}
		if( context->ContextFlags & CONTEXT_CONTROL )
		{
			fprintf(log_file,
				"eip=%08x esp=%08x ebp=%08x iopl=%1x %s %s %s %s %s %s %s %s %s %s\n",
				context->Eip, context->Esp, context->Ebp,
				(context->EFlags >> 12) & 3,	//  IOPL level value
				context->EFlags & 0x00100000 ? "vip" : "   ",	//  VIP (virtual interrupt pending)
				context->EFlags & 0x00080000 ? "vif" : "   ",	//  VIF (virtual interrupt flag)
				context->EFlags & 0x00000800 ? "ov"  : "nv",	//  VIF (virtual interrupt flag)
				context->EFlags & 0x00000400 ? "dn"  : "up",	//  OF (overflow flag)
				context->EFlags & 0x00000200 ? "ei"  : "di",	//  IF (interrupt enable flag)
				context->EFlags & 0x00000080 ? "ng"  : "pl",	//  SF (sign flag)
				context->EFlags & 0x00000040 ? "zr"  : "nz",	//  ZF (zero flag)
				context->EFlags & 0x00000010 ? "ac"  : "na",	//  AF (aux carry flag)
				context->EFlags & 0x00000004 ? "po"  : "pe",	//  PF (parity flag)
				context->EFlags & 0x00000001 ? "cy"  : "nc");	//  CF (carry flag)
		}
		if( context->ContextFlags & CONTEXT_SEGMENTS )
		{
			fprintf(log_file,
				"cs=%04x  ss=%04x  ds=%04x  es=%04x  fs=%04x  gs=%04x",
				context->SegCs,
				context->SegSs,
				context->SegDs,
				context->SegEs,
				context->SegFs,
				context->SegGs,
				context->EFlags);
			if( context->ContextFlags & CONTEXT_CONTROL )
				fprintf(log_file,
					"             efl=%08x",
					context->EFlags);
			fprintf(log_file, "\n");
		}
		else if( context->ContextFlags & CONTEXT_CONTROL )
			fprintf(log_file,
					"                                                                       efl=%08x\n",
					context->EFlags);
#else /* defined(_M_IX86) */
		//TODO add more processors
#endif
	}
}

/// Prints the typename of the symbol.
///
/// @param typeIndex Type index of the symbol
/// @param symtag Symbol tag
/// @param withParens If brackets are printed around the typename
/// @param interData Inter data
static VOID
Dhp__PrintTypeName(
	DWORD       typeIndex,
	DWORD       symtag,
	BOOL        withParens,
	InterData*  interData)
{
	// inter data
	FILE* log_file;
	HANDLE hProcess;
	ULONG64 modBase;
	//
	assert( interData != NULL );
	log_file = interData->log_file;
	hProcess = interData->hProcess;
	modBase  = interData->modBase;

	if( withParens )
		fprintf(log_file, "(");

	switch( symtag )
	{
	case SymTagEnum:
		{
			WCHAR* pwszTypeName;

			if( SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_SYMNAME, &pwszTypeName) )
			{
				fprintf(log_file, "enum %ls", pwszTypeName);
				LocalFree(pwszTypeName);
			}
			else
				fprintf(log_file, "enum <symname not found>");
		}
		break;
	case SymTagBaseType:
		{
			DWORD basetype;
			ULONG64 length = 0;

			SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_LENGTH, &length);
			if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_BASETYPE, &basetype) )
			{
				fprintf(log_file, "<basetype not found>");
				break;
			}
			switch( basetype )
			{
			case btVoid: fprintf(log_file, "void"); break;
			case btChar: fprintf(log_file, "char"); break;
			case btWChar: fprintf(log_file, "wchar"); break;
			case btULong: fprintf(log_file, "unsigned ");	// next
			case btLong: fprintf(log_file, "long"); break;
			case btUInt: fprintf(log_file, "unsigned ");	// next
			case btInt:
				if( length == sizeof(char) ) fprintf(log_file, "char");
				else if( length == sizeof(short) ) fprintf(log_file, "short");
				else if( length == sizeof(int) ) fprintf(log_file, "int");
				else if( length == sizeof(long long) ) fprintf(log_file, "long long");
				else fprintf(log_file, "<int%d>", length*8);
				break;
			case btFloat:
				if( length == sizeof(float) ) fprintf(log_file, "float");
				else if( length == sizeof(double) ) fprintf(log_file, "double");
				else if( length == sizeof(long double) ) fprintf(log_file, "long double");
				else fprintf(log_file, "<float%d>", length*8);
				break;
			default: fprintf(log_file, "<TODO name of basetype %d %d>", basetype, length); break;
			}
		}
		break;
	case SymTagPointerType:
		{
			DWORD subtype;
			DWORD subtag;

			if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_TYPE, &subtype) )
				fprintf(log_file, "<type not found>*");
			else if( !SymGetTypeInfo_(hProcess, modBase, subtype, TI_GET_SYMTAG, &subtag) )
				fprintf(log_file, "<symtag not found>*");
			else
			{
				Dhp__PrintTypeName(subtype, subtag, FALSE, interData);
				fprintf(log_file, "*");
			}
		}
		break;
	case SymTagArrayType:
		{
			DWORD childTypeIndex;
			DWORD childSymtag;

			// print root type
			childTypeIndex = typeIndex;
			childSymtag = symtag;
			for( ; ; )
			{
				if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_TYPE, &childTypeIndex) )
				{
					fprintf(log_file, "<child type not found>");
					break;
				}
				if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_SYMTAG, &childSymtag) )
				{
					fprintf(log_file, "<child symtag not found>");
					break;
				}
				if( childSymtag != SymTagArrayType )
				{
					Dhp__PrintTypeName(childTypeIndex, childSymtag, FALSE, interData);
					break;
				}
				// next dimension
			}
			// print dimensions
			childTypeIndex = typeIndex;
			childSymtag = symtag;
			for( ; childSymtag == SymTagArrayType ; )
			{
				DWORD childCount;
				if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_COUNT, &childCount) )
					fprintf(log_file, "[<count not found>]");
				else
					fprintf(log_file, "[%u]", childCount);
				if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_TYPE, &childTypeIndex) )
				{
					fprintf(log_file, "<child type not found>");
					break;
				}
				if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_SYMTAG, &childSymtag) )
				{
					fprintf(log_file, "<child symtag not found>");
					break;
				}
				// next dimension
			}
		}
		break;
	default:
		{
			WCHAR* pSymname;
			DWORD udtkind;

			if( SymGetTypeInfo_( hProcess, modBase, typeIndex, TI_GET_UDTKIND, &udtkind) )
			{
				switch( (UdtKind)udtkind )
				{
				case UdtStruct: fprintf(log_file, "struct "); break;
				case UdtClass: fprintf(log_file, "class "); break;
				case UdtUnion: fprintf(log_file, "union "); break;
				default: fprintf(log_file, "<unknown udtkind %d> ", udtkind); break;
				}
			}
			if( SymGetTypeInfo_( hProcess, modBase, typeIndex, TI_GET_SYMNAME, &pSymname ) )
			{
				fprintf(log_file, "%ls", pSymname);
				LocalFree( pSymname );
			}
			else
				fprintf(log_file, "<TODO typename of symtag %d>", symtag); break;
		}
		break;
	}

	if( withParens )
		fprintf(log_file, ")");
}

/// Prints the bytes in the target location.
///
/// @param log_file Log file
/// @param p Pointer to the data
/// @param length Length of the data in bytes
static VOID
Dhp__PrintValueBytes(
	FILE*   log_file,
	BYTE*   p,
	ULONG64 length)
{
	ULONG64 i;

	fprintf(log_file, "<bytes:");
	for( i = 0; i < length; ++i )
	{
		fprintf(log_file, "%02X", p[i]);
	}
	fprintf(log_file, ">");
}

/// Prints a wide string/char value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueWideChars(
	FILE*   log_file,
	WCHAR*  wstr,
	ULONG64 length,
	BOOL    isString)
{
	ULONG64 i;
	char* buf;
	char delim;

	length /= sizeof(WCHAR);
	delim = ( isString || length > 1 ? '\"' : '\'' );
	fprintf(log_file, "%c", delim);
	buf = (char *)LocalAlloc(LMEM_FIXED, MB_CUR_MAX+1);
	buf[MB_CUR_MAX] = '\0';
	for( i = 0; i < length; ++i )
	{
		int n;
		switch( wstr[i] )
		{
		case L'\"': fprintf(log_file, "\\\""); break;
		case L'\'': fprintf(log_file, "\\\'"); break;
		case L'\\': fprintf(log_file, "\\\\"); break;
		case L'\a': fprintf(log_file, "\\a"); break;
		case L'\b': fprintf(log_file, "\\b"); break;
		case L'\f': fprintf(log_file, "\\f"); break;
		case L'\n': fprintf(log_file, "\\n"); break;
		case L'\r': fprintf(log_file, "\\r"); break;
		case L'\t': fprintf(log_file, "\\t"); break;
		case L'\v': fprintf(log_file, "\\v"); break;
		default:
			if( iswprint(wstr[i]) && (n=wctomb(buf, wstr[i])) > 0 )
			{
				buf[n] = '\0';
				fprintf(log_file, "%s", buf);
			}
			else fprintf(log_file, "\\u%04X", wstr[i]);
			break;
		}
	}
	LocalFree(buf);
	fprintf(log_file, "%c", delim);
}

/// Prints a string/char value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueChars(
	FILE*   log_file,
	char*   str,
	ULONG64 length,
	BOOL    isString)
{
	ULONG64 i;
	char delim;

	length /= sizeof(char);
	delim = ( isString || length > 1 ? '\"' : '\'' );
	fprintf(log_file, "%c", delim);
	for( i = 0; i < length; ++i )
	{
		switch( str[i] )
		{
		case '\"': fprintf(log_file, "\\\""); break;
		case '\'': fprintf(log_file, "\\\'"); break;
		case '\\': fprintf(log_file, "\\\\"); break;
		case '\a': fprintf(log_file, "\\a"); break;
		case '\b': fprintf(log_file, "\\b"); break;
		case '\f': fprintf(log_file, "\\f"); break;
		case '\n': fprintf(log_file, "\\n"); break;
		case '\r': fprintf(log_file, "\\r"); break;
		case '\t': fprintf(log_file, "\\t"); break;
		case '\v': fprintf(log_file, "\\v"); break;
		default:
			if( isprint((unsigned char)str[i]) ) fprintf(log_file, "%c", str[i]);
			else fprintf(log_file, "\\x%02X", (unsigned char)str[i]);
			break;
		}
	}
	fprintf(log_file, "%c", delim);
}

/// Prints a float value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueFloat(
	FILE*   log_file,
	VOID*   p,
	ULONG64 length)
{
	if( length == sizeof(float) ) fprintf(log_file, "%f", *(float*)p);
	else if( length == sizeof(double) ) fprintf(log_file, "%lf", *(double*)p);
	else if( length == sizeof(long double) ) fprintf(log_file, "%Lf", *(long double*)p);
	else
	{
		fprintf(log_file, "<unexpected length %I64u>", length);
		Dhp__PrintValueBytes(log_file, (BYTE*)p, length);
	}
}

/// Prints a hex value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueHex(
	FILE*   log_file,
	VOID*   p,
	ULONG64 length)
{
	if( length == sizeof(UINT32) ) fprintf(log_file, "0x%I32X", *(UINT32*)p);
	else if( length == sizeof(UINT64) ) fprintf(log_file, "0x%I64X", *(UINT64*)p);
	else if( length == sizeof(char) ) fprintf(log_file, "0x%X", *(unsigned char*)p);
	else if( length == sizeof(short) ) fprintf(log_file, "0x%X", *(unsigned short*)p);
	else if( length == sizeof(int) ) fprintf(log_file, "0x%x", *(unsigned int*)p);
	else if( length == sizeof(long) ) fprintf(log_file, "0x%lX", *(unsigned long*)p);
	else if( length == sizeof(long long) ) fprintf(log_file, "0x%llX", *(unsigned long long*)p);
	else
	{
		fprintf(log_file, "<unexpected length %I64u>", length);
		Dhp__PrintValueBytes(log_file, (BYTE*)p, length);
	}
}

/// Prints an unsigned integer value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueUnsigned(
	FILE*   log_file,
	VOID*   p,
	ULONG64 length)
{
	if( length == sizeof(INT32) ) fprintf(log_file, "%I32u", *(INT32*)p);
	else if( length == sizeof(INT64) ) fprintf(log_file, "%I64u", *(INT64*)p);
	else if( length == sizeof(char) ) fprintf(log_file, "%u", *(unsigned char*)p);
	else if( length == sizeof(short) ) fprintf(log_file, "%u", *(unsigned short*)p);
	else if( length == sizeof(int) ) fprintf(log_file, "%u", *(unsigned int*)p);
	else if( length == sizeof(long) ) fprintf(log_file, "%lu", *(unsigned long*)p);
	else if( length == sizeof(long long) ) fprintf(log_file, "%llu", *(unsigned long long*)p);
	else
	{
		fprintf(log_file, "<unexpected length %I64u>", length);
		Dhp__PrintValueBytes(log_file, (BYTE*)p, length);
	}
}

/// Prints a signed integer value.
///
/// @param log_file Log file
/// @param p Pointer to the value
/// @param length Length of the value in bytes
static VOID
Dhp__PrintValueSigned(
	FILE*   log_file,
	VOID*   p,
	ULONG64 length)
{
	if( length == sizeof(INT32) ) fprintf(log_file, "%I32d", *(INT32*)p);
	else if( length == sizeof(INT64) ) fprintf(log_file, "%I64d", *(INT64*)p);
	else if( length == sizeof(char) ) fprintf(log_file, "%d", *(signed char*)p);
	else if( length == sizeof(short) ) fprintf(log_file, "%d", *(signed short*)p);
	else if( length == sizeof(int) ) fprintf(log_file, "%d", *(signed int*)p);
	else if( length == sizeof(long) ) fprintf(log_file, "%ld", *(signed long*)p);
	else if( length == sizeof(long long) ) fprintf(log_file, "%lld", *(signed long long*)p);
	else
	{
		fprintf(log_file, "<unexpected length %I64u>", length);
		Dhp__PrintValueBytes(log_file, (BYTE*)p, length);
	}
}

/// Prints a nul-terminated wide string value.
/// Checks if the memory can be read from.
///
/// @param log_file Log file
/// @param str Target string
static VOID
Dhp__PrintValueCWideString(
	FILE*   log_file,
	WCHAR*   str)
{
	ULONG64 length = 0;

	// check if memory is readable
	__try
	{
		while( str[length] != L'\0' )
			++length;
	}
	__except( EXCEPTION_EXECUTE_HANDLER )
	{
		if( length ) Dhp__PrintValueWideChars(log_file, str, length*sizeof(WCHAR), TRUE);	// print readable part
		fprintf(log_file, "<invalid memory>");
		return;
	}

	// print string
	Dhp__PrintValueWideChars(log_file, str, length*sizeof(WCHAR), TRUE);
}

/// Prints a nul-terminated string value.
/// Checks if the memory can be read from.
///
/// @param log_file Log file
/// @param str Target string
static VOID
Dhp__PrintValueCString(
	FILE*   log_file,
	char*   str)
{
	ULONG64 length = 0;

	assert( log_file != NULL );

	// check if memory is readable
	__try
	{
		while( str[length] != '\0' )
			++length;
	}
	__except( EXCEPTION_EXECUTE_HANDLER )
	{
		if( length ) Dhp__PrintValueChars(log_file, str, length*sizeof(char), TRUE);	// print readable part
		fprintf(log_file, "<invalid memory>");
		return;
	}

	// print string
	Dhp__PrintValueChars(log_file, str, length*sizeof(char), TRUE);
}

// forward declaration of Dhp__PrintDataContents
static VOID Dhp__PrintDataContents(DWORD typeIndex, PVOID pVariable, InterData*  interData);

/// Prints the value of the data symbol.
/// Checks if the memory can be read from.
///
/// @param typeIndex Type index of the symbol
/// @param symtag Symbol tag
/// @param pVariable Address to the symbol contents
/// @param pInterData Inter data
static VOID
Dhp__PrintDataValue(
	DWORD       typeIndex,
	DWORD       symtag,
	PVOID       pVariable,
	InterData*  pInterData)
{
	// inter data
	FILE* log_file;
	DWORD64 modBase;
	HANDLE hProcess;
	//
	ULONG64 length = 0;
	DWORD basetype;
	BOOL isValid = TRUE;

	assert( pInterData != NULL );
	log_file = pInterData->log_file;
	modBase  = pInterData->modBase;
	hProcess = pInterData->hProcess;

	if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_LENGTH, &length) )
	{
		fprintf(log_file, "<unknown data length>");
		return;
	}

	// check if memory is readable
	__try
	{
		BYTE* p = (BYTE*)pVariable;
		ULONG i;
		BYTE b = 0;
		for( i = 0; i < length; ++i )
			b += p[i];	// add to make sure it's not optimized out in release mode
	}
	__except( EXCEPTION_EXECUTE_HANDLER )
	{
		fprintf(log_file, "<invalid memory>");
		return;
	}

	switch( symtag )
	{
	case SymTagBaseType:
		{
			if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_BASETYPE, &basetype) )
			{
				fprintf(log_file, "<basetype not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}
			switch( basetype )
			{
			case btInt:
			case btLong:
				Dhp__PrintValueSigned(log_file, pVariable, length);
				break;
			case btUInt:
			case btULong:
				Dhp__PrintValueUnsigned(log_file, pVariable, length);
				break;
			case btFloat:
				Dhp__PrintValueFloat(log_file, pVariable, length);
				break;
			case btChar:
				{
					if( length == sizeof(char) ) fprintf(log_file, "%u ", *(unsigned char*)pVariable);
					Dhp__PrintValueChars(log_file, (char*)pVariable, length, FALSE);
				}
				break;
			case btWChar:
				{
					if( length == sizeof(WCHAR) ) fprintf(log_file, "%u ", *(WCHAR*)pVariable);
					Dhp__PrintValueWideChars(log_file, (WCHAR*)pVariable, length, FALSE);
				}
				break;
			case btVoid:
				if( length > 0 ) Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			default:
				fprintf(log_file, "<TODO value of basetype %d>", basetype);
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}
		}
		break;
	case SymTagEnum:
		Dhp__PrintValueHex(log_file, pVariable, length);
		break;
	case SymTagPointerType:
		{
			DWORD childTypeIndex;
			DWORD childSymtag;

			fprintf(log_file, "0x%p", *(void**)pVariable);
			if( SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_TYPE, &childTypeIndex) &&
				SymGetTypeInfo_(hProcess, modBase, childTypeIndex, TI_GET_SYMTAG, &childSymtag) &&
				childSymtag != SymTagPointerType )
			{
				DWORD childBasetype;

				// child isn't a pointer, print the contents
				fprintf(log_file, " ");
				if( childSymtag == SymTagBaseType &&
					SymGetTypeInfo_(hProcess, modBase, childTypeIndex, TI_GET_BASETYPE, &childBasetype) &&
					(childBasetype == btChar || childBasetype == btWChar) )
				{
					// string or wide string
					if( childBasetype == btChar ) Dhp__PrintValueCString(log_file, *(char**)pVariable);
					else if( childBasetype == btWChar ) Dhp__PrintValueCWideString(log_file, *(WCHAR**)pVariable);
					else fprintf(log_file, "<unexpected child basetype %d>", childBasetype);
					break;
				}
				Dhp__PrintDataValue(childTypeIndex, childSymtag, *(PVOID*)pVariable, pInterData);
			}
		}
		break;
	case SymTagArrayType:
		{
			DWORD childTypeIndex;
			DWORD childSymtag;
			DWORD count;
			DWORD i;

			if( !SymGetTypeInfo_( hProcess, modBase, typeIndex, TI_GET_TYPE, &childTypeIndex) )
			{
				fprintf(log_file, "<child type not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}
			if( !SymGetTypeInfo_( hProcess, modBase, childTypeIndex, TI_GET_SYMTAG, &childSymtag) )
			{
				fprintf(log_file, "<child symtag not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}
			if( !SymGetTypeInfo_( hProcess, modBase, typeIndex, TI_GET_COUNT, &count) )
			{
				fprintf(log_file, "<count not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}
			// print values
			fprintf(log_file, "{");
			for( i = 0; i < count; ++i )
			{
				BYTE* pData = pVariable;
				pData += i*(length/count);
				if( i > 0 ) fprintf(log_file, ",");
				Dhp__PrintDataValue(childTypeIndex, childSymtag, pData, pInterData);
			}
			fprintf(log_file, "}");
		}
		break;
	default:
#if 0
		{//## TODO show children of structs/unions
			TI_FINDCHILDREN_PARAMS* children;
			DWORD childCount;
			DWORD i;

			// count children
			if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_CHILDRENCOUNT, &childCount) )
			{
				fprintf(log_file, "<child count not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				break;
			}

			// Prepare to get an array of "TypeIds", representing each of the children.
			// SymGetTypeInfo(TI_FINDCHILDREN) expects more memory than just a
			// TI_FINDCHILDREN_PARAMS struct has.  Use derivation to accomplish this.
			children = (TI_FINDCHILDREN_PARAMS*)LocalAlloc(LMEM_FIXED, sizeof(TI_FINDCHILDREN_PARAMS)+childCount*sizeof(ULONG));
			children->Count = childCount;
			children->Start= 0;

			// Get the array of TypeIds, one for each child type
			if( !SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_FINDCHILDREN, &children) )
			{
				fprintf(log_file, "<children not found>");
				Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
				LocalFree(children);
				return;
			}

			// Iterate through each of the children
			fprintf(log_file, "{");
			for( i = 0; i < childCount; ++i )
			{
				DWORD childOffset;
				DWORD childTypeid;
				WCHAR* childName;
				DWORD_PTR pData;

				if( i > 0 ) fprintf(log_file, ",");

				// Get the offset of the child member, relative to its parent
				if( !SymGetTypeInfo_(hProcess, modBase, children->ChildId[i], TI_GET_OFFSET, &childOffset) )
				{
					fprintf(log_file, "<child offset not found>");
					continue;
				}

				// Get the real "TypeId" of the child.
				if( !SymGetTypeInfo_(hProcess, modBase, children->ChildId[i], TI_GET_TYPEID, &childTypeid) )
				{
					fprintf(log_file, "<child typeid not found>");
					continue;
				}

				// Calculate the address of the member
				pData = (DWORD_PTR)pVariable;
				pData += childOffset;

				// print name of the child
				if( !SymGetTypeInfo_(hProcess, modBase, childTypeid, TI_GET_SYMNAME, &childName) )
				{
					fprintf(log_file, "<child symname not found>");
					continue;
				}
				fprintf(log_file, "%ws=", childName);
				LocalFree(childName);

				// print contents of the child
				Dhp__PrintDataContents(childTypeid, (PVOID)pData, interData);
			}
			fprintf(log_file, "}");

			LocalFree(children);
		}
#endif
		Dhp__PrintValueBytes(log_file, (BYTE*)pVariable, length);
		break;
	}
}

/// Prints the contents of the data symbol. (type and value)
///
/// @param typeIndex Type index of the symbol
/// @param pVariable Address of the symbol contents
/// @param pInterData Inter data
static VOID
Dhp__PrintDataContents(
	DWORD typeIndex,
	PVOID pVariable,
	InterData*  pInterData)
{
	// inter data
	FILE* log_file;
	HANDLE hProcess;
	DWORD64 modBase;
	//
	DWORD symtag;

	assert( pInterData != NULL );
	log_file = pInterData->log_file;
	hProcess = pInterData->hProcess;
	modBase  = pInterData->modBase;

	if( SymGetTypeInfo_(hProcess, modBase, typeIndex, TI_GET_SYMTAG, &symtag) )
	{
		// print type
		Dhp__PrintTypeName(typeIndex, symtag, TRUE, pInterData);
		// print value
		Dhp__PrintDataValue(typeIndex, symtag, pVariable, pInterData);
	}
	else
		fprintf(log_file, "<symtag not found>");
}

/// Prints information about the data symbol.
///
/// @param pSymInfo Symbol info
/// @param pInterData Inter data
static VOID
Dhp__PrintDataInfo(
	PSYMBOL_INFO    pSymInfo,
	InterData*      pInterData)
{
	// inter data
	FILE* log_file;
	STACKFRAME* pStackframe;
	BOOL as_arg_list;
	BOOL log_params;
	BOOL log_locals;
	BOOL log_globals;
	int nr_of_var;
	// my data
	DWORD_PTR pVariable = 0;
	enum{ UNKNOWN, PARAM, LOCAL, GLOBAL } scope = UNKNOWN;

	assert( pSymInfo != NULL );
	assert( pInterData != NULL );
	assert( pSymInfo->Tag == SymTagData );
	log_file    = pInterData->log_file;
	pStackframe = pInterData->pStackframe;
	as_arg_list = pInterData->as_arg_list;
	log_params  = pInterData->log_params;
	log_locals  = pInterData->log_locals;
	log_globals = pInterData->log_globals;
	nr_of_var   = pInterData->nr_of_var;

	// Determine the scope and address of the variable
    if( pSymInfo->Flags & SYMFLAG_REGREL )
	{
		pVariable = pStackframe->AddrFrame.Offset;
		pVariable += (DWORD_PTR)pSymInfo->Address;
		if( pSymInfo->Flags & SYMFLAG_PARAMETER )
			scope = PARAM;	// parameter
		else if( pSymInfo->Flags & SYMFLAG_LOCAL )
		{
			scope = LOCAL;	// local
#if defined(_M_IX86)
			if( (LONG64)pSymInfo->Address  > 0) scope = PARAM;	// parameter as local (bug in DBGHELP 5.1)
#endif
		}
	}
	else if( pSymInfo->Flags & SYMFLAG_REGISTER )
	{
		scope = ( pSymInfo->Flags & SYMFLAG_PARAMETER ? PARAM : LOCAL );	// register, optimized out(?)
	}
	else
	{
		pVariable = (DWORD_PTR)pSymInfo->Address;
		scope = GLOBAL;	// It must be a global variable
	}

	// check if we should to log the variable
	if( (scope == PARAM && log_params) ||
		(scope == LOCAL && log_locals) ||
		(scope == GLOBAL && log_globals) )
	{
		// print prefix and name
		if( as_arg_list )
			fprintf(log_file, "%s%s=", (nr_of_var ? ", " : ""), pSymInfo->Name);
		else
			fprintf(log_file, "\t%s = ", pSymInfo->Name);

		// print value
		if( !(pSymInfo->Flags & SYMFLAG_REGREL) && (pSymInfo->Flags & SYMF_REGISTER) )
			fprintf(log_file, "<value optimized out>");
		else
		{
			pInterData->modBase = pSymInfo->ModBase;
			Dhp__PrintDataContents(pSymInfo->TypeIndex, (PVOID)pVariable, pInterData);
		}

		// print postfix
		if( !as_arg_list )
			fprintf(log_file, "\n");
		pInterData->nr_of_var = ++nr_of_var;
	}
}

/// Prints information about the symbol.
///
/// @param pSymInfo Symbol info
/// @param pInterData Inter data
static VOID
Dhp__PrintSymbolInfo(
	PSYMBOL_INFO    pSymInfo,
	InterData*      pInterData)
{
	assert( pSymInfo != NULL );
	assert( pInterData != NULL );

	switch( pSymInfo->Tag )
	{
	case SymTagData: Dhp__PrintDataInfo( pSymInfo, pInterData ); break;
	default: /*fprintf(pInterData->log_file, "<unsupported symtag %d>", pSymInfo->Tag);*/ break;
	}
}

/// Prints the details of one symbol to the log file.
/// Used as a callback for SymEnumSymbols.
///
/// @param pSymInfo Symbol info
/// @param symSize Size of the symbol info structure
/// @param pData Inter data
static BOOL WINAPI
Dhp__EnumSymbolsCallback(
	PSYMBOL_INFO    pSymInfo,
	ULONG           symSize,
	PVOID           pData)
{
	if( pSymInfo == NULL )
		return TRUE;	// try other symbols

	if( pData == NULL )
	{
		printf("Dhp__EnumSymbolsCallback: pData is NULL\n");
		return FALSE;
	}

	Dhp__PrintSymbolInfo(pSymInfo, (InterData*)pData);
	return TRUE;
}

/// Prints the source code of the target line.
/// Searches for the target file in the original path,
/// in the last src folder of the original path (relative)
/// and in the current directory.
///
/// @param filename Original source file
/// @param line Target line
/// @param log_file Log file
static VOID
Dhp__PrintSourceLine(
	FILE* log_file,
	char* filename,
	DWORD line)
{
	char path[MAX_PATH*3];
	char pathBuffer[MAX_PATH+1];
	char* p;

	assert( filename != NULL );
	assert( log_file != NULL );

	// generate search paths
	strcpy(path, filename);	// original path
	p = strrchr(path, '\\');
	if( p )
	{
		memcpy(p, ";\0", 2);
		p = strstr(filename, "\\src\\");
		if( p )
		{
			while( strstr(p+1, "\\src\\") )
				p = strstr(p+1, "\\src\\");
			strcat(path, p+1);	// last src folder path
			p = strrchr(path, '\\');
			memcpy(p, ";\0", 2);
		}
		filename = strrchr(filename, '\\')+1;
	}
	else
		*path = '\0';	// no path
	strcat(path, ".");	// current directoy

	// search for file and line
	if( SearchPathA(path, filename, NULL, MAX_PATH, pathBuffer, NULL) )
	{
		char code[1024+1];
		DWORD i = 1;
		FILE* fp;

		fp = fopen(pathBuffer, "rt");
		if( fp == NULL )
			return;

		code[1024] = '\0';
		while( fgets(code, 1024, fp) )
		{
			if( i == line )
			{// found line
				char* term = strchr(code, '\n');
				if( term && term != code && term[-1] == '\r' ) --term;
				if( term ) *term = '\0';
				fprintf(log_file, "%d\t%s\n", line, code);
				break;
			}
			if( strchr(code, '\n') )
				++i;
		}
		fclose(fp);
	}
}

/// Prints details of one function to the log file.
///
/// @param interData Inter data
static VOID
Dhp__PrintFunctionDetails(
	InterData*  pInterData)
{
	// inter data
	HANDLE hProcess;
	STACKFRAME* pStackframe;
	FILE* log_file;
	int nr_of_frame;
	//
	PSYMBOL_INFO pSymbolInfo;
	DWORD64 funcDisplacement=0;
	IMAGEHLP_STACK_FRAME imagehlpStackFrame;
	IMAGEHLP_LINE imagehlpLine;
	DWORD lineDisplacement=0;

	assert( pInterData != NULL );
	hProcess    = pInterData->hProcess;
	pStackframe = pInterData->pStackframe;
	log_file    = pInterData->log_file;
	nr_of_frame = pInterData->nr_of_frame;

	// frame info
	fprintf(log_file,
		"#%d  0x%p",
		nr_of_frame, (void*)(DWORD_PTR)pStackframe->AddrPC.Offset);

	// restrict symbol enumeration to this frame only
	ZeroMemory(&imagehlpStackFrame, sizeof(IMAGEHLP_STACK_FRAME));
	imagehlpStackFrame.InstructionOffset = pStackframe->AddrPC.Offset;
	SymSetContext_(hProcess, &imagehlpStackFrame, 0);

	// function name and displacement
	pSymbolInfo = (PSYMBOL_INFO)LocalAlloc(LMEM_FIXED, sizeof(SYMBOL_INFO)+1024);
	pSymbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
	pSymbolInfo->MaxNameLen = 1024;
	if( SymFromAddr_(hProcess, pStackframe->AddrPC.Offset, &funcDisplacement, pSymbolInfo) == TRUE )
	{
		fprintf(log_file,
			" in %.1024s+0x%I64X (",
			pSymbolInfo->Name, funcDisplacement);

		// log all function parameters
		pInterData->as_arg_list = TRUE;
		pInterData->log_params = TRUE;
		pInterData->log_locals = FALSE;
		pInterData->log_globals = FALSE;
		pInterData->nr_of_var = 0;
		SymEnumSymbols_(hProcess, 0, 0, Dhp__EnumSymbolsCallback, pInterData);

		fprintf(log_file,
			")");
	}
	else
		fprintf(log_file,
			"in <unknown function>");

	// find the source line for this function.
	imagehlpLine.SizeOfStruct = sizeof(IMAGEHLP_LINE);
	if( SymGetLineFromAddr_(hProcess, pStackframe->AddrPC.Offset, &lineDisplacement, &imagehlpLine) != 0 )
	{
		char* filename = imagehlpLine.FileName;
		DWORD line = imagehlpLine.LineNumber;

		fprintf(log_file,
			" at %s:%d\n",
			filename, line);

		Dhp__PrintSourceLine(log_file, filename, line);
	}
	else
		fprintf(log_file,
			"\n");

	// log all function local variables
	pInterData->as_arg_list = FALSE;
	pInterData->log_params = FALSE;
	pInterData->log_locals = TRUE;
	pInterData->log_globals = FALSE;
	pInterData->nr_of_var = 0;
	SymEnumSymbols_(hProcess, 0, 0, Dhp__EnumSymbolsCallback, pInterData);

	pInterData->nr_of_frame = ++nr_of_frame;
	LocalFree(pSymbolInfo);
}


/// Walks over the stack and prints all relevant information to the log file.
///
/// @param context Exception context
/// @param log_file Log file
static VOID
Dhp__PrintStacktrace(
	CONTEXT *context,
	FILE *log_file)
{
	HANDLE hProcess = GetCurrentProcess();
	STACKFRAME stackframe;
	DWORD machine;
	CONTEXT ctx;
	InterData interData;
	int skip = 0;
	int i;

	assert( log_file != NULL );

	fprintf(log_file,
		"\nStacktrace:\n");

	// Use thread information - if not supplied.
	if( context == NULL )
	{
		// If no context is supplied, skip 1 frame
		skip = 1;

		ctx.ContextFlags = CONTEXT_FULL;
		if( GetThreadContext(GetCurrentThread(), &ctx) )
			context = &ctx;
	}

	if( context == NULL )
		return;

	// Write the stack trace
	ZeroMemory(&stackframe, sizeof(STACKFRAME));
	stackframe.AddrPC.Mode = AddrModeFlat;
	stackframe.AddrStack.Mode = AddrModeFlat;
	stackframe.AddrFrame.Mode = AddrModeFlat;
#if defined(_M_IX86)
	machine = IMAGE_FILE_MACHINE_I386;
	stackframe.AddrPC.Offset = context->Eip;
	stackframe.AddrStack.Offset = context->Esp;
	stackframe.AddrFrame.Offset = context->Ebp;
#else /* defined(_M_IX86) */
#error FIXME add more processors
some compilers don't stop on #error, this line makes sure it errors out
#endif

	interData.hProcess = hProcess;
	interData.log_file = log_file;
	interData.pStackframe = &stackframe;
	interData.nr_of_frame = 0;
	for( i = 0; ; ++i )
	{
		if( !StackWalk_(machine, hProcess, GetCurrentThread(),
			&stackframe, context, NULL, SymFunctionTableAccess_,
			SymGetModuleBase_, NULL))
		{
			break;
		}

		if( i >= skip )
		{
			// Check that the address is not zero.
			// Sometimes StackWalk returns TRUE with a frame of zero.
			if( stackframe.AddrPC.Offset != 0 )
				Dhp__PrintFunctionDetails(&interData);
		}
	}
}

typedef BOOL (WINAPI *ISDEBUGGERPRESENT)(void);
/// Checks if a debugger is attached to this process
///
/// @return TRUE is a debugger is present
static BOOL
Dhp__IsDebuggerPresent()
{
	HANDLE kernel32_dll;
	ISDEBUGGERPRESENT IsDebuggerPresent_;
	BOOL result;

	kernel32_dll = LoadLibraryA("kernel32.dll");
	if( kernel32_dll == NULL )
		return FALSE;

	IsDebuggerPresent_ = (ISDEBUGGERPRESENT)GetProcAddress(kernel32_dll, "IsDebuggerPresent");
	if( IsDebuggerPresent_ )
		result = IsDebuggerPresent_();
	else
		result = FALSE;

	FreeLibrary(kernel32_dll);

	return result;
}

/// Loads the dbghelp.dll library.
///
/// @return TRUE is sucessfull
static BOOL
Dhp__LoadDbghelpDll()
{
	dbghelp_dll = LoadLibraryA(DBGHELP_DLL);
	if( dbghelp_dll != INVALID_HANDLE_VALUE )
	{
		DWORD opts;

		// load the functions
		MiniDumpWriteDump_      =      (MINIDUMPWRITEDUMP)GetProcAddress(dbghelp_dll, "MiniDumpWriteDump");
		SymInitialize_          =          (SYMINITIALIZE)GetProcAddress(dbghelp_dll, "SymInitialize");
		SymSetOptions_          =          (SYMSETOPTIONS)GetProcAddress(dbghelp_dll, "SymSetOptions");
		SymGetOptions_          =          (SYMGETOPTIONS)GetProcAddress(dbghelp_dll, "SymGetOptions");
		SymCleanup_             =             (SYMCLEANUP)GetProcAddress(dbghelp_dll, "SymCleanup");
		SymGetTypeInfo_         =         (SYMGETTYPEINFO)GetProcAddress(dbghelp_dll, "SymGetTypeInfo");
		SymGetLineFromAddr_     =     (SYMGETLINEFROMADDR)GetProcAddress(dbghelp_dll, "SymGetLineFromAddr");
		SymEnumSymbols_         =         (SYMENUMSYMBOLS)GetProcAddress(dbghelp_dll, "SymEnumSymbols");
		SymSetContext_          =          (SYMSETCONTEXT)GetProcAddress(dbghelp_dll, "SymSetContext");
		SymFromAddr_            =            (SYMFROMADDR)GetProcAddress(dbghelp_dll, "SymFromAddr");
		StackWalk_              =              (STACKWALK)GetProcAddress(dbghelp_dll, "StackWalk");
		SymFunctionTableAccess_ = (SYMFUNCTIONTABLEACCESS)GetProcAddress(dbghelp_dll, "SymFunctionTableAccess");
		SymGetModuleBase_       =       (SYMGETMODULEBASE)GetProcAddress(dbghelp_dll, "SymGetModuleBase");

		if( MiniDumpWriteDump_ &&
			SymInitialize_  && SymSetOptions_  && SymGetOptions_ &&
			SymCleanup_     && SymGetTypeInfo_ && SymGetLineFromAddr_ &&
			SymEnumSymbols_ && SymSetContext_  && SymFromAddr_ && StackWalk_ &&
			SymFunctionTableAccess_ && SymGetModuleBase_ )
		{
			// initialize the symbol loading code
			opts = SymGetOptions_();

			// Set the 'load lines' option to retrieve line number information.
			// Set the 'deferred loads' option to map the debug info in memory only when needed.
			SymSetOptions_(opts | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);

			// Initialize the dbghelp DLL with the default path and automatic
			// module enumeration (and loading of symbol tables) for this process.
			SymInitialize_(GetCurrentProcess(), NULL, TRUE);

			return TRUE;
		}
	}

	if( dbghelp_dll )
	{
		FreeLibrary(dbghelp_dll);
		dbghelp_dll = NULL;
	}

	return FALSE;
}

/// Unloads the dbghelp.dll library.
static VOID
Dhp__UnloadDbghlpDll()
{
	SymCleanup_(GetCurrentProcess());

	FreeLibrary(dbghelp_dll);
	dbghelp_dll = NULL;
}

/// Creates the report and minidump files.
/// Puts the resulting pathnames in the arguments.
/// The buffers must be at least MAX_PATH+1 in size.
///
/// @param out_lpszLogFileName Buffer for the report filename
/// @param out_lpszDmpFileName Buffer for the minidump filename
/// @return TRUE if the files were created
static BOOL
Dhp__CreateFiles(
	char*   out_logFileName,
	char*   out_dmpFileName)
{
#define LEN_TIMESTAMP 14	// "YYYYMMDDhhmmss"
#define LEN_EXT 4	// ".rpt" or ".dmp"
	char baseFileName[MAX_PATH+1];
	char timestamp[LEN_TIMESTAMP+1];
	FILE* fp;
	time_t now;

	// Generate base filename for the report/minidump
	ZeroMemory(baseFileName, sizeof(baseFileName));
	if( GetModuleFileName(NULL, baseFileName, MAX_PATH-LEN_TIMESTAMP-LEN_EXT) )
	{
		char* pTerm = strrchr(baseFileName, '\\');
		if( pTerm == NULL ) pTerm = baseFileName;
		pTerm = strrchr(pTerm, '.');
		if( pTerm ) *pTerm = '\0';	// remove extension
	}
	else if( GetTempPathA(MAX_PATH-6-LEN_TIMESTAMP-LEN_EXT, baseFileName) )
	{// in temp folder
		strcat(baseFileName, DBG_DEFAULT_FILENAME);
	}
	else
	{// in current folder
		strcpy(baseFileName, DBG_DEFAULT_FILENAME);
	}

	time(&now);
#if 0
	szTimestamp[0] = '\0';
#else
	strftime(timestamp, sizeof(timestamp), "%Y%m%d%H%M%S", localtime(&now));
#endif
	timestamp[LEN_TIMESTAMP] = '\0';
	
	sprintf(out_logFileName, "%s%s.rpt", baseFileName, timestamp);
	fp = fopen(out_logFileName, "w");
	if( fp == NULL )
		return FALSE;	// failed to create log file
	fclose(fp);

	sprintf(out_dmpFileName, "%s%s.dmp", baseFileName, timestamp);
	fp = fopen(out_dmpFileName, "w");
	if( fp == NULL)
		return FALSE;	// failed to create dump file
	fclose(fp);

	return TRUE;	// success
#undef LEN_EXT
#undef LEN_TIMESTAMP
}

/// Unhandled exception handler. Where the magic starts... ;D
///
/// @param ptrs Exception information
/// @return What to do with the exception
LONG WINAPI
Dhp__UnhandledExceptionFilter(PEXCEPTION_POINTERS ptrs)
{
	char szLogFileName[MAX_PATH+1];
	char szDmpFileName[MAX_PATH+1];
	FILE* log_file;

	// check if the crash handler was already loaded (crash while handling the crash)
	if( dbghelp_dll != INVALID_HANDLE_VALUE )
		return EXCEPTION_CONTINUE_SEARCH;

	// don't log anything if we're running inside a debugger ...
	if( Dhp__IsDebuggerPresent() == TRUE )
		return EXCEPTION_CONTINUE_SEARCH;

	// ... or if we can't load dbghelp.dll ...
	if( Dhp__LoadDbghelpDll() == FALSE )
		return EXCEPTION_CONTINUE_SEARCH;

	// ... or if we can't create the log files
	if( Dhp__CreateFiles(szLogFileName, szDmpFileName) == FALSE )
		return EXCEPTION_CONTINUE_SEARCH;

	// open log file
	log_file = fopen(szLogFileName, "wt");

	// print information about the process
	Dhp__PrintProcessInfo(
		ptrs ? ptrs->ExceptionRecord : NULL,
		ptrs ? ptrs->ContextRecord : NULL,
		log_file);

	// print the stacktrace
	Dhp__PrintStacktrace(
		ptrs ? ptrs->ContextRecord : NULL,
		log_file);

	// write the minidump file and use the callback to print the list of modules to the log file
	Dhp__WriteMinidumpFile(
		szDmpFileName,
		ptrs,
		Dhp__PrintModuleInfoCallback,
		log_file);

	fclose(log_file);

	Dhp__UnloadDbghlpDll();

	// inform the user
	fprintf(stderr,
		"\n"
		"This application has halted due to an unexpected error.\n"
		"A crash report and minidump file were saved to disk, you can find them here:\n"
		"%s\n"
		"%s\n"
		DBG_EXTENDED_INFORMATION
		"\n"
		"NOTE: The crash report and minidump files can contain sensitive information\n"
		"(filenames, partial file content, usernames and passwords etc.)\n",
		szLogFileName,
		szDmpFileName);

	// terminate the application
	return EXCEPTION_EXECUTE_HANDLER;
}

/////////////////////////////////////////////////////////////////////
// Plugin

/// Previous exception filter.
static LPTOP_LEVEL_EXCEPTION_FILTER previousFilter;

/**
 * Initializes plugin
 * Installs a new unhandled exception filter (Dhp__UnhandledExceptionFilter)
 **/
HPExport void plugin_init (void) {
	previousFilter = SetUnhandledExceptionFilter(Dhp__UnhandledExceptionFilter);
}

/**
 * Finalizes plugin
 * Uninstalls the handler
 **/
HPExport void plugin_final (void) {
	SetUnhandledExceptionFilter(previousFilter);
}