Vowpal Wabbit
vw_exception.cc
Go to the documentation of this file.
1 #include "vw_exception.h"
2 
3 #ifdef _WIN32
4 #define NOMINMAX
5 #include <Windows.h>
6 #endif
7 
8 namespace VW
9 {
10 vw_exception::vw_exception(const char* pfile, int plineNumber, std::string const& pmessage) noexcept
11  : file(pfile), message(pmessage), lineNumber(plineNumber)
12 {
13 }
14 
16  : file(ex.file), message(ex.message), lineNumber(ex.lineNumber)
17 {
18 }
19 
21 {
22  // check for self-assignment
23  if (&other == this)
24  return *this;
25 
26  file = other.file;
27  message = other.message;
28  lineNumber = other.lineNumber;
29 
30  return *this;
31 }
32 
33 vw_exception::~vw_exception() noexcept = default;
34 
35 const char* vw_exception::what() const noexcept { return message.c_str(); }
36 
37 const char* vw_exception::Filename() const { return file; }
38 
39 int vw_exception::LineNumber() const { return lineNumber; }
40 
41 #ifdef _WIN32
42 
43 void vw_trace(const char* filename, int linenumber, const char* fmt, ...)
44 {
45  char buffer[4 * 1024];
46  int offset = sprintf_s(buffer, sizeof(buffer), "%s:%d (%d): ", filename, linenumber, GetCurrentThreadId());
47 
48  va_list argptr;
49  va_start(argptr, fmt);
50  offset += vsprintf_s(buffer + offset, sizeof(buffer) - offset, fmt, argptr);
51  va_end(argptr);
52 
53  sprintf_s(buffer + offset, sizeof(buffer) - offset, "\n");
54 
55  OutputDebugStringA(buffer);
56 }
57 
58 struct StopWatchData
59 {
60  LARGE_INTEGER frequency_;
61  LARGE_INTEGER startTime_;
62 };
63 
64 StopWatch::StopWatch() : data(new StopWatchData())
65 {
66  if (!::QueryPerformanceFrequency(&data->frequency_))
67  THROW("Error with QueryPerformanceFrequency");
68  ::QueryPerformanceCounter(&data->startTime_);
69 }
70 
71 StopWatch::~StopWatch() { delete data; }
72 
73 double StopWatch::MilliSeconds() const
74 {
75  LARGE_INTEGER now;
76  ::QueryPerformanceCounter(&now);
77 
78  return double(now.QuadPart - data->startTime_.QuadPart) / (double(data->frequency_.QuadPart) / 1000);
79 }
80 
81 bool launchDebugger()
82 {
83  // Get System directory, typically c:\windows\system32
84  std::wstring systemDir(MAX_PATH + 1, '\0');
85  UINT nChars = GetSystemDirectoryW(&systemDir[0], (UINT)systemDir.length());
86  if (nChars == 0)
87  return false; // failed to get system directory
88  systemDir.resize(nChars);
89 
90  // Get process ID and create the command line
91  DWORD pid = GetCurrentProcessId();
92  std::wostringstream s;
93  s << systemDir << L"\\vsjitdebugger.exe -p " << pid;
94  std::wstring cmdLine = s.str();
95 
96  // Start debugger process
97  STARTUPINFOW si;
98  ZeroMemory(&si, sizeof(si));
99  si.cb = sizeof(si);
100 
101  PROCESS_INFORMATION pi;
102  ZeroMemory(&pi, sizeof(pi));
103 
104  if (!CreateProcessW(NULL, &cmdLine[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
105  return false;
106 
107  // Close debugger process handles to eliminate resource leak
108  CloseHandle(pi.hThread);
109  CloseHandle(pi.hProcess);
110 
111  // Wait for the debugger to attach
112  while (!IsDebuggerPresent()) Sleep(100);
113 
114  // Stop execution so the debugger can take over
115  DebugBreak();
116  return true;
117 }
118 #endif
119 } // namespace VW
#define sprintf_s
const char * Filename() const
Definition: vw_exception.cc:37
const char * what() const noexcept override
Definition: vw_exception.cc:35
vw_exception & operator=(const vw_exception &other) noexcept
Definition: vw_exception.cc:20
std::string message
Definition: vw_exception.h:28
~vw_exception() noexcept override
const char * file
Definition: vw_exception.h:26
#define vsprintf_s
vw_exception(const char *file, int lineNumber, std::string const &message) noexcept
Definition: vw_exception.cc:10
Definition: autolink.cc:11
int LineNumber() const
Definition: vw_exception.cc:39
#define THROW(args)
Definition: vw_exception.h:181