6 #include "../../GDBServerFoundation/GDBServer.h"
7 #include "../../GDBServerFoundation/GDBStub.h"
8 #include "../../GDBServerFoundation/GlobalSessionMonitor.h"
12 using namespace BazisLib;
13 using namespace GDBServerFoundation;
14 using namespace VBoxGDB;
18 virtual StubResponse HandleRequest(
const BazisLib::TempStringA &requestType,
char splitterChar,
const BazisLib::TempStringA &requestData)
20 printf(
"%s%c%s\n", DynamicStringA(requestType).c_str(), splitterChar, DynamicStringA(requestData).c_str());
21 return __super::HandleRequest(requestType, splitterChar, requestData);
24 virtual void OnProtocolError(
const TCHAR *errorDescription)
26 _tprintf(_T(
"Protocol error: %s\n"), errorDescription);
34 virtual StubResponse HandleRequest(
const BazisLib::TempStringA &requestType,
char splitterChar,
const BazisLib::TempStringA &requestData)
36 printf(
">> %s%c%s\n", DynamicStringA(requestType).c_str(), splitterChar, DynamicStringA(requestData).c_str());
37 fprintf(pLogFile,
">> %s%c%s\n", DynamicStringA(requestType).c_str(), splitterChar, DynamicStringA(requestData).c_str());
38 StubResponse response = __super::HandleRequest(requestType, splitterChar, requestData);
39 printf(
"<< %s\n", std::string(response.GetData(), response.GetSize()).c_str());
40 fprintf(pLogFile,
"<< %s\n", std::string(response.GetData(), response.GetSize()).c_str());
47 : GDBStub(pTarget, true)
49 pLogFile = fopen(
"gdbserver.log",
"w");
71 virtual IGDBStub *CreateStub(GDBServer *pServer)
76 printf(
"GDB connected.\n");
77 pServer->StopListening();
80 return new GDBStub(pTarget);
83 virtual void OnProtocolError(
const TCHAR *errorDescription)
85 _tprintf(_T(
"Protocol error: %s\n"), errorDescription);
89 #pragma region The code that reproduces the VirtualBox hanging bug
94 if (dwType == CTRL_C_EVENT)
104 VBoxClient client(BazisLib::Network::DNS::GetHostByName(pHost).SetPort(port));
124 char sz[32], szCmd[128];
126 sprintf(szCmd,
"db %08I64x", val);
130 strcpy(szCmd,
"showvars");
144 printf(
"Usage: VBoxGDB <VirtualBox debug port or host:port> [--gdbport=<GDBServer port>]\n");
152 if (dwType == CTRL_C_EVENT)
154 printf(
"Stopping target... If you intended to close this program, press Ctrl+Break.\n");
159 if (dwType == CTRL_BREAK_EVENT|| dwType == CTRL_CLOSE_EVENT)
169 int main(
int argc,
char* argv[])
171 printf(
"VBoxGDB v0.1 [http://sysprogs.com/VBoxGDB]\n");
172 printf(
"This pre-release version has the following limitations:\n\
173 \t*Only 64-bit VirtualBox 4.1.x is supported\n\
174 \t*Only 32-bit Linux guests are supported\n\
175 \t*Only single-CPU virtual machines are supported\n\
176 ---------------------------------------------------------------------\n");
184 int vboxPort, gdbPort = 2000;
185 std::string vboxHost =
"localhost";
187 char *p = strchr(argv[1],
':');
189 vboxPort = atoi(argv[1]);
192 vboxHost = std::string(argv[1], p - argv[1]);
193 vboxPort = atoi(p + 1);
196 for (
int i = 2; i < argc; i++)
198 if (memicmp(argv[i],
"--gdbport=", 10))
199 gdbPort = atoi(argv[i] + 10);
200 else if (!strcmp(argv[i],
"--tryhang"))
204 printf(
"Trying to connect to VirtualBox debugger at %s:%d...\n", vboxHost.c_str(), vboxPort);
205 VBoxClient client(BazisLib::Network::DNS::GetHostByName(vboxHost.c_str()).SetPort(vboxPort));
206 if (!client.Initialize())
208 printf(
"Cannot connect to VirtualBox debugger.\n");
214 printf(
"Connected to VirtualBox debugger\n", vboxPort);
218 printf(
"Listening for GDB connections on port %d\n", gdbPort);
219 printf(
"To start debugging, run \"gdb vmlinux\" and execute \"target remote :%d\"\n", gdbPort, gdbPort);
221 srv.WaitForTermination();