forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugView++.cpp
More file actions
228 lines (190 loc) · 5.17 KB
/
Copy pathDebugView++.cpp
File metadata and controls
228 lines (190 loc) · 5.17 KB
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
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "MainFrame.h"
#include <boost/algorithm/string.hpp>
#include "CobaltFusion/scope_guard.h"
#include "CobaltFusion/hstream.h"
#include "CobaltFusion/fusionassert.h"
#include "DebugViewppLib/DBWinWriter.h"
#include "Win32/Com.h"
#include "atleverything.h"
#include <filesystem>
#include <cstdlib> // for std::getenv
// #define ENABLE_CRASHPAD
#ifdef ENABLE_CRASHPAD
#include "crashpad.h"
#endif
CAppModule _Module;
namespace fusion {
namespace debugviewpp {
class CAppModuleInitialization
{
public:
CAppModuleInitialization(CAppModule& module, HINSTANCE hInstance) :
m_module(module)
{
HRESULT hr = m_module.Init(nullptr, hInstance);
if (FAILED(hr))
{
Win32::ThrowWin32Error(hr, "CAppModule::Init");
}
}
~CAppModuleInitialization()
{
m_module.Term();
}
private:
CAppModule& m_module;
};
class MessageLoop
{
public:
explicit MessageLoop(CAppModule& module) :
m_module(module)
{
module.AddMessageLoop(&m_loop);
}
~MessageLoop()
{
m_module.RemoveMessageLoop();
}
int Run()
{
return m_loop.Run();
}
private:
CAppModule& m_module;
CMessageLoop m_loop;
};
int ForwardMessagesFromPipe(HANDLE hPipe)
{
DBWinWriter dbwin;
DWORD pid = Win32::GetParentProcessId();
hstream pipe(hPipe);
std::string line;
while (std::getline(pipe, line))
{
line += "\n";
dbwin.Write(pid, line);
}
return 0;
}
class DebugConsole
{
public:
DebugConsole()
{
::AllocConsole();
::freopen_s(&standardOut, "CONOUT$", "wb", stdout);
std::cout.clear();
}
~DebugConsole()
{
fclose(standardOut);
fclose(stdout);
FreeConsole();
}
private:
FILE* standardOut = nullptr;
};
int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine*/, int cmdShow)
{
Win32::SetPrivilege(SE_DEBUG_NAME, true);
Win32::SetPrivilege(SE_CREATE_GLOBAL_NAME, true);
Win32::SetPrivilege(SE_LOAD_DRIVER_NAME, true);
Win32::ComInitialization com;
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(nullptr, 0, 0, 0L);
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
std::unique_ptr<DebugConsole> debugConsole;
if (std::getenv("DEBUGVIEWPP_CONSOLE_DEBUG") != nullptr)
{
debugConsole = std::make_unique<DebugConsole>();
}
CAppModuleInitialization moduleInit(_Module, hInstance);
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hFile = nullptr;
HANDLE hPipe = nullptr;
switch (GetFileType(hStdIn))
{
case FILE_TYPE_DISK: hFile = hStdIn; break;
case FILE_TYPE_PIPE: hPipe = hStdIn; break;
default: break;
}
if ((hPipe != nullptr) && IsDBWinViewerActive())
{
return ForwardMessagesFromPipe(hPipe);
}
CMainFrame wndMain;
MessageLoop theLoop(_Module);
auto args = Win32::GetCommandLineArguments();
std::wstring fileName;
std::wstring selectTabName;
for (size_t i = 1; i < args.size(); ++i)
{
if (boost::iequals(args[i], L"/min"))
{
cmdShow = SW_MINIMIZE;
}
else if (boost::iequals(args[i], L"/log"))
{
// wndMain.SetLogging(); // todo: implement: FileWriter needs to concurrently access m_logfile, it now causes a crash if DbgMsgSrc -1 is run
// this should be replaced by the new streaming-to-disk feature we discussed.
}
else if (boost::icontains(args[i], L"/tab:"))
{
selectTabName = args[i].substr(5);
}
else if (args[i][0] != '/')
{
if (!fileName.empty())
{
throw std::runtime_error("multiple filenames specified on commandline");
}
fileName = args[i];
}
}
if (wndMain.CreateEx() == nullptr)
{
Win32::ThrowLastError(L"Main window creation failed!");
}
wndMain.ShowWindow(cmdShow == SW_SHOWDEFAULT ? wndMain.GetShowCommand() : cmdShow);
if (boost::algorithm::iends_with(fileName, ".dbconf"))
{
wndMain.LoadConfiguration(fileName);
}
else if (!fileName.empty())
{
wndMain.Load(fileName, false);
}
else if (hFile != nullptr)
{
wndMain.Load(hFile);
}
else if (hPipe != nullptr)
{
wndMain.CapturePipe(hPipe);
}
if (!selectTabName.empty())
{
wndMain.SetSelectTabByName(selectTabName);
}
return theLoop.Run();
}
} // namespace debugviewpp
} // namespace fusion
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpstrCmdLine, int nCmdShow)
try
{
#ifdef ENABLE_CRASHPAD
initializeCrashPad();
#endif
return fusion::debugviewpp::Main(hInstance, hPrevInstance, lpstrCmdLine, nCmdShow);
}
catch (std::exception& ex)
{
fusion::errormessage(ex.what(), "Debugview++ Error");
return EXIT_FAILURE;
}