-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (127 loc) · 3.89 KB
/
Copy pathmain.cpp
File metadata and controls
151 lines (127 loc) · 3.89 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
#include <string>
#include <vector>
#include <winerror.h>
#if defined(__linux__)
#include "sys/personality.h"
#include <sys/ptrace.h>
#include <unistd.h>
#include <sched.h>
#include "debugger/debugger.h"
#endif
#if defined(_WIN32)
#include <Windows.h>
#include <winuser.h>
#include <debugapi.h>
#include <minwinbase.h>
#include <processthreadsapi.h>
#include <errhandlingapi.h>
#include <handleapi.h>
#include <winbase.h>
#include <winnt.h>
#include <memoryapi.h>
#include <minwindef.h>
#include <oleidl.h>
#include "src/windows/include/debugger/windows_debugger.h"
#include <iostream>
#endif
#if defined(_WIN32)
int main(int argc, char* argv[]) {
STARTUPINFO si = {0};
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0};
if (CreateProcess(TEXT("C:\\windows\\notepade.exe"),
NULL,
NULL,
NULL,
FALSE,
DEBUG_ONLY_THIS_PROCESS |
CREATE_NEW_CONSOLE, // 只调试他这个进程,不调试他的子进程,并且子进程新开一个窗口,否则输出会乱
NULL,
NULL,
&si,
&pi) == FALSE) {
std::cout << "CreateProcess failed " << GetLastError() << std::endl;
return -1;
}
bool wait_event = true;
DEBUG_EVENT debug_event{}; // 调试事件
debugger::WindowsDebugger debugger{
pi.hProcess, pi.hThread, pi.dwProcessId, pi.dwThreadId};
while (true) {
std::vector<std::string> command{};
debugger.get_command(command);
if (command[0] == "q") {
break;
} else if (command[0] == "g") { // 开始调试
debugger.continue();
}
}
// 循环
while (wait_event == true && WaitForDebugEvent(&debug_event, INFINITE)) {
switch (debug_event.dwDebugEventCode) {
case CREATE_PROCESS_DEBUG_EVENT:
debugger.OnProcessCreated(&debug_event.u.CreateProcessInfo);
break;
case CREATE_THREAD_DEBUG_EVENT:
debugger.OnThreadCreated(&debug_event.u.CreateThread);
break;
case EXCEPTION_DEBUG_EVENT:
debugger.OnException(&debug_event.u.Exception);
break;
case EXIT_PROCESS_DEBUG_EVENT:
debugger.OnProcessExited(debug_event.u.ExitProcess);
wait_event = false;
break;
case LOAD_DLL_DEBUG_EVENT:
debugger.OnDllLoaded(debug_event.u.LoadDll);
break;
case UNLOAD_DLL_DEBUG_EVENT:
debugger.OnDllUnloaded(debug_event.u.UnloadDll);
break;
case OUTPUT_DEBUG_STRING_EVENT:
debugger.on_output_debug_string(&debug_event.u.DebugString);
break;
case RIP_EVENT:
debugger.OnRipEvent(debug_event.u.RipInfo);
break;
default:
std::cout << "UnKnown debug event." << std::endl;
break;
}
if (wait_event == true) {
ContinueDebugEvent(debug_event.dwProcessId,
debug_event.dwThreadId,
debugger.g_continue_status);
} else {
break;
}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
std::cout << "yes" << std::endl;
}
#endif
#if defined(__linux__)
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "错误" << std::endl;
return -1;
}
std::string program_name{argv[1]};
pid_t pid = fork();
// std::cout << argv[1] << std::endl;
// childen
if (pid == 0) {
personality(ADDR_NO_RANDOMIZE);
// extern long int ptrace (enum __ptrace_request __request, ...);
// 设置跟踪状态并且执行
ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
execl(program_name.c_str(), program_name.c_str(), nullptr);
}
if (pid > 0) {
std::cout << "开始Debug" << std::endl;
my_gdb::Debugger gdb{program_name, pid};
gdb.run();
}
}
#endif