main.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. // Windows only sample to catch unhandled exceptions and email a minidump.
  11. // The minidump can be opened in visual studio and will show you where the crash occurred and give you the local variable values.
  12. //
  13. // How to use the minidump:
  14. //
  15. // Put the minidump on your harddrive and double click it. It will open Visual Studio. It will look for the exe that caused the crash in the directory
  16. // that the program that crashed was running at. If it can't find this exe, or if it is different, it will look in the current
  17. // directory for that exe. If it still can't find it, or if it is different, it will load Visual Studio and indicate that it can't find
  18. // the executable module. No source code will be shown at that point. However, you can specify modpath=<pathToExeDirectory> in the
  19. // project properties window for "Command Arguments".
  20. // The best solution is copy the .dmp to a directory containing a copy of the exe that crashed.
  21. //
  22. // On load, Visual Studio will look for the .pdb, which it uses to find the source code files and other information. This is fine as long as the source
  23. // code files on your harddrive match those that were used to create the exe. If they don't, you will see source code but it will be the wrong code.
  24. // There are three ways to deal with this.
  25. //
  26. // The first way is to change the path to your source code so it won't find the wrong code automatically.
  27. // This will cause the debugger to not find the source code pointed to in the .pdb . You will be prompted for the location of the correct source code.
  28. //
  29. // The second way is to build the exe on a different path than what you normally program with. For example, when you program you use c:/Working/Mygame
  30. // When you release builds, you do at c:/Version2.2/Mygame . After a build, you keep the source files, the exe, and the pdb
  31. // on a harddrive at that location. When you get a crash .dmp, copy it to the same directory as the exe, ( c:/Version2.2/Mygame/bin )
  32. // This way the .pdb will point to the correct sources to begin wtih.
  33. //
  34. // The third way is save build labels or branches in source control and get that version (you only need source code + .exe + .pdb) before debugging.
  35. // After debugging, restore your previous work.
  36. #include "SocketLayer.h"
  37. #include "CrashReporter.h" // This is the only required file for the crash reporter. You must link in Dbghelp.lib
  38. #include <stdio.h> // Printf, for the sample code
  39. #include "Kbhit.h" // getch, for the sample code
  40. #include <DbgHelp.h>
  41. #include "Gets.h"
  42. void function1(int a)
  43. {
  44. int *crashPtr=0;
  45. // Keep crashPtr from getting compiled out
  46. printf("Now crashing!!!! %p\n", crashPtr);
  47. // If it crashes here in your debugger that is because you didn't define _DEBUG_CRASH_REPORTER to catch it.
  48. // The normal mode of the crash handler is to only catch when you are NOT debugging (started through ctrl-f5)
  49. *crashPtr=10;
  50. }
  51. void RunGame(void)
  52. {
  53. int a=10;
  54. int b=20;
  55. function1(a);
  56. printf("%i", b);
  57. }
  58. //#define _DEBUG_CRASH_REPORTER
  59. // If you don't plan to debug the crash reporter itself, you can remove _DEBUG_CRASH_REPORTER
  60. #ifdef _DEBUG_CRASH_REPORTER
  61. #include "WindowsIncludes.h"
  62. extern void DumpMiniDump(PEXCEPTION_POINTERS excpInfo);
  63. #endif
  64. void main(void)
  65. {
  66. printf("Demonstrates the crash reporter.\n");
  67. printf("This program will prompt you for a variety of actions to take on crash.\n");
  68. printf("If so desired, it will generate a minidump which can be opened in visual studio\n");
  69. printf("to debug the crash.\n\n");
  70. RakNet::CrashReportControls controls;
  71. controls.actionToTake=0;
  72. printf("Send an email? (y/n)\n");
  73. if (getch()=='y')
  74. {
  75. printf("Attach the mini-dump to the email? (y/n)\n");
  76. if (getch()=='y')
  77. controls.actionToTake|=RakNet::AOC_EMAIL_WITH_ATTACHMENT;
  78. else
  79. controls.actionToTake|=RakNet::AOC_EMAIL_NO_ATTACHMENT;
  80. }
  81. printf("Write mini-dump to disk? (y/n)\n");
  82. if (getch()=='y')
  83. controls.actionToTake|=RakNet::AOC_WRITE_TO_DISK;
  84. printf("Handle crashes in silent mode (no prompts)? (y/n)\n");
  85. if (getch()=='y')
  86. controls.actionToTake|=RakNet::AOC_SILENT_MODE;
  87. if ((controls.actionToTake & RakNet::AOC_EMAIL_WITH_ATTACHMENT) || (controls.actionToTake & RakNet::AOC_EMAIL_NO_ATTACHMENT))
  88. {
  89. if (controls.actionToTake & RakNet::AOC_SILENT_MODE)
  90. {
  91. printf("Enter SMTP Server: ");
  92. Gets(controls.SMTPServer,sizeof(controls.SMTPServer));
  93. if (controls.SMTPServer[0]==0)
  94. return;
  95. printf("Enter SMTP account name: ");
  96. Gets(controls.SMTPAccountName,sizeof(controls.SMTPAccountName));
  97. if (controls.SMTPAccountName[0]==0)
  98. return;
  99. printf("Enter sender email address: ");
  100. Gets(controls.emailSender,sizeof(controls.emailSender));
  101. }
  102. printf("Enter email recipient email address: ");
  103. Gets(controls.emailRecipient,sizeof(controls.emailRecipient));
  104. if (controls.emailRecipient[0]==0)
  105. return;
  106. printf("Enter subject prefix, if any: ");
  107. Gets(controls.emailSubjectPrefix,sizeof(controls.emailSubjectPrefix));
  108. if ((controls.actionToTake & RakNet::AOC_SILENT_MODE)==0)
  109. {
  110. printf("Enter text to write in email body: ");
  111. Gets(controls.emailBody,sizeof(controls.emailBody));
  112. }
  113. }
  114. if (controls.actionToTake & RakNet::AOC_WRITE_TO_DISK)
  115. {
  116. printf("Enter disk path to write to (ENTER for current directory): ");
  117. Gets(controls.pathToMinidump,sizeof(controls.pathToMinidump));
  118. }
  119. printf("Enter application name: ");
  120. Gets(controls.appName,sizeof(controls.appName));
  121. printf("Enter application version: ");
  122. Gets(controls.appVersion,sizeof(controls.appVersion));
  123. // MiniDumpNormal will not give you SocketLayer::I correctly but is small (like 15K)
  124. // MiniDumpWithDataSegs is much bigger (391K) but does give you SocketLayer::I correctly.
  125. controls.minidumpType=MiniDumpWithDataSegs;
  126. // You must call Start before any crashes will be reported.
  127. RakNet::CrashReporter::Start(&controls);
  128. printf("Crash reporter started.\n");
  129. // If you don't plan to debug the crash reporter itself, you can remove the __try within _DEBUG_CRASH_REPORTER
  130. #ifdef _DEBUG_CRASH_REPORTER
  131. __try
  132. #endif
  133. {
  134. RunGame();
  135. }
  136. // If you don't plan to debug the crash reporter itself, you can remove the DumpMiniDump code within _DEBUG_CRASH_REPORTER
  137. #ifdef _DEBUG_CRASH_REPORTER
  138. __except(DumpMiniDump(GetExceptionInformation()),EXCEPTION_EXECUTE_HANDLER)
  139. {
  140. }
  141. #endif
  142. }
粤ICP备19079148号