dhewm3/neo/tools/debugger/DebuggerClient.cpp

664 lines
14 KiB
C++
Raw Normal View History

2011-11-22 21:28:15 +00:00
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
2011-11-22 21:28:15 +00:00
2011-12-06 16:14:59 +00:00
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
2011-11-22 21:28:15 +00:00
Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "tools/edit_gui_common.h"
2011-11-22 21:28:15 +00:00
#include "DebuggerApp.h"
/*
================
rvDebuggerClient::rvDebuggerClient
================
*/
rvDebuggerClient::rvDebuggerClient ( )
{
mConnected = false;
mWaitFor = DBMSG_UNKNOWN;
}
/*
================
rvDebuggerClient::~rvDebuggerClient
================
*/
rvDebuggerClient::~rvDebuggerClient ( )
{
ClearBreakpoints ( );
2011-11-22 21:28:15 +00:00
ClearCallstack ( );
ClearThreads ( );
}
/*
================
rvDebuggerClient::Initialize
Initialize the debugger client
================
*/
bool rvDebuggerClient::Initialize ( void )
{
// Nothing else can run with the debugger
com_editors = EDITOR_DEBUGGER;
// Initialize the network connection
if ( !mPort.InitForPort ( 27981 ) )
{
return false;
}
// Server must be running on the local host on port 28980
2021-05-11 20:45:24 +00:00
Sys_StringToNetAdr ( "localhost", &mServerAdr, true );
2011-11-22 21:28:15 +00:00
mServerAdr.port = 27980;
2011-11-22 21:28:15 +00:00
// Attempt to let the server know we are here. The server may not be running so this
// message will just get ignored.
SendMessage ( DBMSG_CONNECT );
return true;
}
2011-11-22 21:28:15 +00:00
/*
================
rvDebuggerClient::Shutdown
Shutdown the debugger client and let the debugger server
know we are shutting down
================
*/
void rvDebuggerClient::Shutdown ( void )
{
if ( mConnected )
{
SendMessage ( DBMSG_DISCONNECT );
mConnected = false;
}
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::ProcessMessages
Process all incomding messages from the debugger server
================
*/
bool rvDebuggerClient::ProcessMessages ( void )
{
netadr_t adrFrom;
2021-05-11 20:45:24 +00:00
idBitMsg msg;
2011-11-22 21:28:15 +00:00
byte buffer[MAX_MSGLEN];
2021-05-11 20:45:24 +00:00
msg.SetSize(MAX_MSGLEN);
msg.BeginReading();
2011-11-22 21:28:15 +00:00
2021-05-11 20:45:24 +00:00
int msgSize;
2011-11-22 21:28:15 +00:00
// Check for pending udp packets on the debugger port
2021-05-11 20:45:24 +00:00
while ( mPort.GetPacket ( adrFrom, buffer,msgSize, MAX_MSGLEN) )
2011-11-22 21:28:15 +00:00
{
2021-05-11 20:45:24 +00:00
short command;
msg.Init(buffer, sizeof(buffer));
msg.SetSize(msgSize);
msg.BeginReading();
2011-11-22 21:28:15 +00:00
// Only accept packets from the debugger server for security reasons
if ( !Sys_CompareNetAdrBase ( adrFrom, mServerAdr ) )
{
continue;
}
2021-05-11 20:45:24 +00:00
command = msg.ReadShort ( );
2011-11-22 21:28:15 +00:00
2021-05-11 20:45:24 +00:00
// Is this what we are waiting for?
2011-11-22 21:28:15 +00:00
if ( command == mWaitFor )
{
mWaitFor = DBMSG_UNKNOWN;
}
2011-11-22 21:28:15 +00:00
switch ( command )
{
case DBMSG_CONNECT:
mConnected = true;
SendMessage ( DBMSG_CONNECTED );
SendBreakpoints ( );
break;
2011-11-22 21:28:15 +00:00
case DBMSG_CONNECTED:
mConnected = true;
SendBreakpoints ( );
break;
2011-11-22 21:28:15 +00:00
case DBMSG_DISCONNECT:
mConnected = false;
break;
2011-11-22 21:28:15 +00:00
case DBMSG_BREAK:
HandleBreak ( &msg );
2011-11-22 21:28:15 +00:00
break;
2011-11-22 21:28:15 +00:00
// Callstack being send to the client
case DBMSG_INSPECTCALLSTACK:
HandleInspectCallstack ( &msg );
break;
2011-11-22 21:28:15 +00:00
// Thread list is being sent to the client
case DBMSG_INSPECTTHREADS:
HandleInspectThreads ( &msg );
break;
2011-11-22 21:28:15 +00:00
case DBMSG_INSPECTVARIABLE:
HandleInspectVariable ( &msg );
break;
2021-05-11 20:45:24 +00:00
case DBMSG_REMOVEBREAKPOINT:
HandleRemoveBreakpoint( &msg );
break;
case DBMSG_INSPECTSCRIPTS:
HandleInspectScripts( &msg );
break;
}
2011-11-22 21:28:15 +00:00
// Give the window a chance to process the message
2021-05-11 20:45:24 +00:00
msg.SetReadCount(0);
msg.SetReadBit(0);
2011-11-22 21:28:15 +00:00
gDebuggerApp.GetWindow().ProcessNetMessage ( &msg );
}
return true;
}
2021-05-11 20:45:24 +00:00
void rvDebuggerClient::HandleRemoveBreakpoint(idBitMsg* msg)
{
long lineNumber;
char filename[MAX_PATH];
// Read the breakpoint info
lineNumber = msg->ReadInt();
msg->ReadString(filename, MAX_PATH);
rvDebuggerBreakpoint* bp = FindBreakpoint(filename, lineNumber);
if(bp)
RemoveBreakpoint(bp->GetID());
}
2011-11-22 21:28:15 +00:00
/*
================
rvDebuggerClient::HandleBreak
Handle the DBMSG_BREAK message send from the server. This message is handled
by caching the file and linenumber where the break occured.
2011-11-22 21:28:15 +00:00
================
*/
2021-05-11 20:45:24 +00:00
void rvDebuggerClient::HandleBreak ( idBitMsg* msg )
2011-11-22 21:28:15 +00:00
{
char filename[MAX_PATH];
mBreak = true;
// Line number
2021-05-11 20:45:24 +00:00
mBreakLineNumber = msg->ReadInt ( );
2011-11-22 21:28:15 +00:00
// Filename
2021-05-11 20:45:24 +00:00
msg->ReadString ( filename, MAX_PATH );
2011-11-22 21:28:15 +00:00
mBreakFilename = filename;
//int64_t ptr64b = msg->ReadInt64();
//mBreakProgram = (idProgram*)ptr64b;
2021-05-11 20:45:24 +00:00
2011-11-22 21:28:15 +00:00
// Clear the variables
mVariables.Clear ( );
// Request the callstack and threads
SendMessage ( DBMSG_INSPECTCALLSTACK );
WaitFor ( DBMSG_INSPECTCALLSTACK, 2000 );
2011-11-22 21:28:15 +00:00
SendMessage ( DBMSG_INSPECTTHREADS );
WaitFor ( DBMSG_INSPECTTHREADS, 2000 );
}
2021-05-11 20:45:24 +00:00
/*
================
rvDebuggerClient::InspectScripts
Instructs the client to inspect the loaded scripts
================
*/
void rvDebuggerClient::InspectScripts ( void )
{
idBitMsg msg;
byte buffer[MAX_MSGLEN];
msg.Init(buffer, sizeof(buffer));
msg.BeginWriting();
msg.WriteShort((short)DBMSG_INSPECTSCRIPTS);
SendPacket(msg.GetData(), msg.GetSize());
}
2011-11-22 21:28:15 +00:00
/*
================
rvDebuggerClient::InspectVariable
Instructs the client to inspect the given variable at the given callstack depth. The
variable is inspected by sending a DBMSG_INSPECTVARIABLE message to the server which
will in turn respond back to the client with the variable value
================
*/
void rvDebuggerClient::InspectVariable ( const char* name, int callstackDepth )
{
2021-05-11 20:45:24 +00:00
idBitMsg msg;
byte buffer[MAX_MSGLEN];
2021-05-11 20:45:24 +00:00
msg.Init( buffer, sizeof( buffer ) );
msg.BeginWriting();
msg.WriteShort ( (short)DBMSG_INSPECTVARIABLE );
msg.WriteShort ( (short)(mCallstack.Num()-callstackDepth) );
msg.WriteString ( name );
SendPacket ( msg.GetData(), msg.GetSize());
}
/*
================
rvDebuggerClient::HandleInspectScripts
2021-05-11 20:45:24 +00:00
Handle the message DBMSG_INSPECTSCRIPTS being sent from the server. This message
is handled by adding the script entries to a list for later lookup.
================
*/
void rvDebuggerClient::HandleInspectScripts( idBitMsg* msg )
{
int totalScripts;
mServerScripts.Clear();
// Read all of the callstack entries specfied in the message
for (totalScripts = msg->ReadInt(); totalScripts > 0; totalScripts--)
{
char temp[1024];
// Script Name
msg->ReadString(temp, 1024);
mServerScripts.Append(temp);
}
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::HandleInspectCallstack
Handle the message DBMSG_INSPECTCALLSTACK being sent from the server. This message
is handled by adding the callstack entries to a list for later lookup.
================
*/
2021-05-11 20:45:24 +00:00
void rvDebuggerClient::HandleInspectCallstack ( idBitMsg* msg )
2011-11-22 21:28:15 +00:00
{
int depth;
2011-11-22 21:28:15 +00:00
ClearCallstack ( );
// Read all of the callstack entries specfied in the message
2021-05-11 20:45:24 +00:00
for ( depth = (short)msg->ReadShort ( ) ; depth > 0; depth -- )
2011-11-22 21:28:15 +00:00
{
rvDebuggerCallstack* entry = new rvDebuggerCallstack;
2011-11-22 21:28:15 +00:00
char temp[1024];
// Function name
2021-05-11 20:45:24 +00:00
msg->ReadString ( temp, 1024 );
entry->mFunction = idStr(temp);
2011-11-22 21:28:15 +00:00
// Filename
2021-05-11 20:45:24 +00:00
msg->ReadString ( temp, 1024 );
entry->mFilename = idStr(temp);
2011-11-22 21:28:15 +00:00
// Line Number
2021-05-11 20:45:24 +00:00
entry->mLineNumber = msg->ReadInt ( );
2011-11-22 21:28:15 +00:00
// Add to list
mCallstack.Append ( entry );
}
}
/*
================
rvDebuggerClient::HandleInspectThreads
Handle the message DBMSG_INSPECTTHREADS being sent from the server. This message
is handled by adding the list of threads to a list for later lookup.
================
*/
2021-05-11 20:45:24 +00:00
void rvDebuggerClient::HandleInspectThreads ( idBitMsg* msg )
2011-11-22 21:28:15 +00:00
{
int count;
ClearThreads ( );
// Loop over the number of threads in the message
2021-05-11 20:45:24 +00:00
for ( count = (short)msg->ReadShort ( ) ; count > 0; count -- )
2011-11-22 21:28:15 +00:00
{
rvDebuggerThread* entry = new rvDebuggerThread;
2011-11-22 21:28:15 +00:00
char temp[1024];
// Thread name
2021-05-11 20:45:24 +00:00
msg->ReadString ( temp, 1024 );
2011-11-22 21:28:15 +00:00
entry->mName = temp;
// Thread ID
2021-05-11 20:45:24 +00:00
entry->mID = msg->ReadInt ( );
2011-11-22 21:28:15 +00:00
// Thread state
2021-05-11 20:45:24 +00:00
entry->mCurrent = msg->ReadBits ( 1 ) ? true : false;
entry->mDoneProcessing = msg->ReadBits ( 1 ) ? true : false;
entry->mWaiting = msg->ReadBits ( 1 ) ? true : false;
entry->mDying = msg->ReadBits ( 1 ) ? true : false;
2011-11-22 21:28:15 +00:00
// Add thread to list
mThreads.Append ( entry );
}
}
/*
================
rvDebuggerClient::HandleInspectVariable
Handle the message DBMSG_INSPECTVARIABLE being sent from the server. This message
is handled by adding the inspected variable to a dictionary for later lookup
================
*/
2021-05-11 20:45:24 +00:00
void rvDebuggerClient::HandleInspectVariable ( idBitMsg* msg )
2011-11-22 21:28:15 +00:00
{
char var[1024];
char value[1024];
int callDepth;
2021-05-11 20:45:24 +00:00
callDepth = (short)msg->ReadShort ( );
msg->ReadString ( var, 1024 );
msg->ReadString ( value, 1024 );
mVariables.Set ( va("%d:%s", mCallstack.Num()-callDepth, var), value );
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::WaitFor
Waits the given amount of time for the specified message to be received by the
2011-11-22 21:28:15 +00:00
debugger client.
================
*/
bool rvDebuggerClient::WaitFor ( EDebuggerMessage msg, int time )
{
int start;
2011-11-22 21:28:15 +00:00
// Cant wait if not connected
if ( !mConnected )
{
return false;
}
2011-11-22 21:28:15 +00:00
start = Sys_Milliseconds ( );
mWaitFor = msg;
2011-11-22 21:28:15 +00:00
while ( mWaitFor != DBMSG_UNKNOWN && Sys_Milliseconds()-start < time )
{
ProcessMessages ( );
Sleep ( 0 );
}
2011-11-22 21:28:15 +00:00
if ( mWaitFor != DBMSG_UNKNOWN )
{
mWaitFor = DBMSG_UNKNOWN;
return false;
}
2011-11-22 21:28:15 +00:00
return true;
}
/*
================
rvDebuggerClient::FindBreakpoint
Searches for a breakpoint that maches the given filename and linenumber
================
*/
rvDebuggerBreakpoint* rvDebuggerClient::FindBreakpoint ( const char* filename, int linenumber )
{
int i;
2011-11-22 21:28:15 +00:00
for ( i = 0; i < mBreakpoints.Num(); i ++ )
{
rvDebuggerBreakpoint* bp = mBreakpoints[i];
2011-11-22 21:28:15 +00:00
if ( linenumber == bp->GetLineNumber ( ) && !idStr::Icmp ( bp->GetFilename ( ), filename ) )
{
return bp;
}
}
2011-11-22 21:28:15 +00:00
return NULL;
}
/*
================
rvDebuggerClient::ClearBreakpoints
Removes all breakpoints from the client and server
================
*/
void rvDebuggerClient::ClearBreakpoints ( void )
{
int i;
2011-11-22 21:28:15 +00:00
for ( i = 0; i < GetBreakpointCount(); i ++ )
{
rvDebuggerBreakpoint* bp = mBreakpoints[i];
assert ( bp );
2011-11-22 21:28:15 +00:00
SendRemoveBreakpoint ( *bp );
delete bp;
2011-11-22 21:28:15 +00:00
}
2011-11-22 21:28:15 +00:00
mBreakpoints.Clear ( );
}
/*
================
rvDebuggerClient::AddBreakpoint
Adds a breakpoint to the client and server with the give nfilename and linenumber
================
*/
int rvDebuggerClient::AddBreakpoint ( const char* filename, int lineNumber, bool onceOnly )
{
int index = mBreakpoints.Append ( new rvDebuggerBreakpoint ( filename, lineNumber ) );
2011-11-22 21:28:15 +00:00
SendAddBreakpoint ( *mBreakpoints[index] );
2011-11-22 21:28:15 +00:00
return index;
}
/*
================
rvDebuggerClient::RemoveBreakpoint
Removes the breakpoint with the given ID from the client and server
================
*/
bool rvDebuggerClient::RemoveBreakpoint ( int bpID )
{
int index;
2011-11-22 21:28:15 +00:00
for ( index = 0; index < GetBreakpointCount(); index ++ )
{
2011-11-22 21:28:15 +00:00
if ( mBreakpoints[index]->GetID ( ) == bpID )
{
SendRemoveBreakpoint ( *mBreakpoints[index] );
delete mBreakpoints[index];
mBreakpoints.RemoveIndex ( index );
return true;
}
}
2011-11-22 21:28:15 +00:00
return false;
}
/*
================
rvDebuggerClient::SendMessage
Send a message with no data to the debugger server
================
*/
void rvDebuggerClient::SendMessage ( EDebuggerMessage dbmsg )
{
2021-05-11 20:45:24 +00:00
idBitMsg msg;
2011-11-22 21:28:15 +00:00
byte buffer[MAX_MSGLEN];
2021-05-11 20:45:24 +00:00
msg.Init ( buffer, sizeof( buffer ) );
msg.BeginWriting ( );
msg.WriteShort ( (short)dbmsg );
2021-05-11 20:45:24 +00:00
SendPacket ( msg.GetData(), msg.GetSize() );
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::SendBreakpoints
Send all breakpoints to the debugger server
================
*/
void rvDebuggerClient::SendBreakpoints ( void )
{
int i;
if ( !mConnected )
{
return;
}
2011-11-22 21:28:15 +00:00
// Send all the breakpoints to the server
for ( i = 0; i < mBreakpoints.Num(); i ++ )
{
SendAddBreakpoint ( *mBreakpoints[i] );
}
}
/*
================
rvDebuggerClient::SendAddBreakpoint
Send an individual breakpoint over to the debugger server
================
*/
void rvDebuggerClient::SendAddBreakpoint ( rvDebuggerBreakpoint& bp, bool onceOnly )
{
2021-05-11 20:45:24 +00:00
idBitMsg msg;
2011-11-22 21:28:15 +00:00
byte buffer[MAX_MSGLEN];
2011-11-22 21:28:15 +00:00
if ( !mConnected )
{
return;
}
2021-05-11 20:45:24 +00:00
msg.Init( buffer, sizeof( buffer ) );
msg.BeginWriting();
msg.WriteShort ( (short)DBMSG_ADDBREAKPOINT );
msg.WriteBits ( onceOnly?1:0, 1 );
msg.WriteInt ( (unsigned long) bp.GetLineNumber ( ) );
msg.WriteInt ( bp.GetID ( ) );
msg.WriteString ( bp.GetFilename() );
2021-05-11 20:45:24 +00:00
SendPacket ( msg.GetData(), msg.GetSize() );
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::SendRemoveBreakpoint
Sends a remove breakpoint message to the debugger server
================
*/
void rvDebuggerClient::SendRemoveBreakpoint ( rvDebuggerBreakpoint& bp )
{
2021-05-11 20:45:24 +00:00
idBitMsg msg;
2011-11-22 21:28:15 +00:00
byte buffer[MAX_MSGLEN];
if ( !mConnected )
{
return;
}
2021-05-11 20:45:24 +00:00
msg.Init ( buffer, sizeof( buffer ) );
msg.BeginWriting( );
msg.WriteShort ( (short)DBMSG_REMOVEBREAKPOINT );
msg.WriteInt ( bp.GetID() );
2021-05-11 20:45:24 +00:00
SendPacket ( msg.GetData(), msg.GetSize() );
2011-11-22 21:28:15 +00:00
}
/*
================
rvDebuggerClient::ClearCallstack
Clear all callstack entries
================
*/
void rvDebuggerClient::ClearCallstack ( void )
{
int depth;
2011-11-22 21:28:15 +00:00
for ( depth = 0; depth < mCallstack.Num(); depth ++ )
{
delete mCallstack[depth];
}
mCallstack.Clear ( );
}
/*
================
rvDebuggerClient::ClearThreads
Clear all thread entries
================
*/
void rvDebuggerClient::ClearThreads ( void )
{
int i;
2011-11-22 21:28:15 +00:00
for ( i = 0; i < mThreads.Num(); i ++ )
{
delete mThreads[i];
}
2011-11-22 21:28:15 +00:00
mThreads.Clear ( );
}