HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/bp.cpp

235 lines
5.9 KiB
C++
Raw Normal View History

2021-03-10 18:52:49 +03:30
/**
* @file bp.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2021-03-10 18:52:49 +03:30
* @brief bp command
* @details
* @version 0.1
* @date 2021-10-03
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2021-03-10 18:52:49 +03:30
//
// Global Variables
//
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
/**
2023-07-13 16:05:42 +09:00
* @brief help of the bp command
2021-03-10 18:52:49 +03:30
*
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandBpHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("bp : puts a breakpoint (0xcc).\n");
2021-03-22 18:19:39 +04:30
ShowMessages(
"Note : 'bp' is not an event, if you want to use an event version "
"of breakpoints use !epthook or !epthook2 instead. See "
2024-03-17 01:01:22 +09:00
"documentation for more information.\n\n");
ShowMessages("syntax : \tbp [Address (hex)] [pid ProcessId (hex)] [tid ThreadId (hex)] [core CoreId (hex)]\n");
ShowMessages("\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag+5\n");
ShowMessages("\t\te.g : bp nt!ExAllocatePoolWithTag+@rcx+rbx\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : bp fffff8077356f010\n");
ShowMessages("\t\te.g : bp fffff8077356f010 pid 0x4\n");
ShowMessages("\t\te.g : bp fffff8077356f010 tid 0x1000\n");
ShowMessages("\t\te.g : bp fffff8077356f010 pid 0x4 core 2\n");
2021-03-10 18:52:49 +03:30
}
/**
* @brief request breakpoint
*
* @param Address Address
* @param Pid Process Id
* @param Tid Thread Id
* @param CoreNumer Core Number
*
* @return VOID
*/
VOID
CommandBpRequest(UINT64 Address, UINT32 Pid, UINT32 Tid, UINT32 CoreNumer)
{
DEBUGGEE_BP_PACKET BpPacket = {0};
//
// Set the details for the remote packet
//
BpPacket.Address = Address;
BpPacket.Core = CoreNumer;
BpPacket.Pid = Pid;
BpPacket.Tid = Tid;
//
// Send the bp packet
//
KdSendBpPacketToDebuggee(&BpPacket);
}
2021-03-10 18:52:49 +03:30
/**
* @brief bp command handler
*
2024-07-29 18:46:12 +09:00
* @param CommandTokens
* @param Command
2024-07-29 18:46:12 +09:00
*
2021-03-10 18:52:49 +03:30
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandBp(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
BOOL IsNextCoreId = FALSE;
BOOL IsNextPid = FALSE;
BOOL IsNextTid = FALSE;
BOOLEAN SetCoreId = FALSE;
BOOLEAN SetPid = FALSE;
BOOLEAN SetTid = FALSE;
BOOLEAN SetAddress = FALSE;
2024-07-29 18:46:12 +09:00
UINT32 Tid = DEBUGGEE_BP_APPLY_TO_ALL_THREADS;
UINT32 Pid = DEBUGGEE_BP_APPLY_TO_ALL_PROCESSES;
UINT32 CoreNumer = DEBUGGEE_BP_APPLY_TO_ALL_CORES;
UINT64 Address = NULL;
BOOLEAN IsFirstCommand = TRUE;
2021-03-22 18:19:39 +04:30
2024-07-29 18:46:12 +09:00
if (CommandTokens.size() >= 9)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandBpHelp();
return;
}
2021-03-10 18:52:49 +03:30
2024-07-29 18:46:12 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
//
// Ignore the first argument as it's the command string itself (bp)
//
2023-07-26 15:15:16 +09:00
if (IsFirstCommand == TRUE)
2021-03-22 18:19:39 +04:30
{
2023-07-26 15:15:16 +09:00
IsFirstCommand = FALSE;
2021-03-22 18:19:39 +04:30
continue;
}
if (IsNextCoreId)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &CoreNumer))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for core id\n\n");
CommandBpHelp();
return;
}
IsNextCoreId = FALSE;
continue;
}
if (IsNextPid)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &Pid))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for process id\n\n");
CommandBpHelp();
return;
}
IsNextPid = FALSE;
continue;
}
if (IsNextTid)
{
2024-07-29 18:46:12 +09:00
if (!ConvertTokenToUInt32(Section, &Tid))
2021-03-22 18:19:39 +04:30
{
ShowMessages("please specify a correct hex value for thread id\n\n");
CommandBpHelp();
return;
}
IsNextTid = FALSE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "pid"))
2021-03-22 18:19:39 +04:30
{
IsNextPid = TRUE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "tid"))
2021-03-22 18:19:39 +04:30
{
IsNextTid = TRUE;
continue;
}
2024-07-29 18:46:12 +09:00
if (CompareLowerCaseStrings(Section, "core"))
2021-03-22 18:19:39 +04:30
{
IsNextCoreId = TRUE;
continue;
}
if (!SetAddress)
{
2024-07-29 18:46:12 +09:00
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(Section), &Address))
2021-03-22 18:19:39 +04:30
{
//
2024-03-17 01:01:22 +09:00
// Couldn't resolve or unknown parameter
//
ShowMessages("err, couldn't resolve error at '%s'\n\n",
2024-07-29 18:46:12 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandBpHelp();
return;
}
else
{
//
// Means that address is received
//
SetAddress = TRUE;
continue;
}
}
}
2021-03-10 18:52:49 +03:30
//
2021-03-22 18:19:39 +04:30
// Check if address is set or not
2021-03-10 18:52:49 +03:30
//
2021-03-22 18:19:39 +04:30
if (!SetAddress)
{
ShowMessages(
"please specify a correct hex value as the breakpoint address\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextPid)
{
2021-03-10 18:52:49 +03:30
ShowMessages("please specify a correct hex value for process id\n\n");
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextCoreId)
{
ShowMessages("please specify a correct hex value for core\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
}
2021-03-22 18:19:39 +04:30
if (IsNextTid)
{
ShowMessages("please specify a correct hex value for thread id\n\n");
2021-03-10 18:52:49 +03:30
CommandBpHelp();
return;
2021-03-22 18:19:39 +04:30
}
2021-03-10 18:52:49 +03:30
2021-03-22 18:19:39 +04:30
if (!g_IsSerialConnectedToRemoteDebuggee)
{
ShowMessages("err, setting breakpoints is not possible when you're not "
"connected to a debuggee\n");
return;
2021-03-10 18:52:49 +03:30
}
2021-03-22 18:19:39 +04:30
//
// Request breakpoint the bp packet
2021-03-22 18:19:39 +04:30
//
CommandBpRequest(Address, Pid, Tid, CoreNumer);
2021-03-10 18:52:49 +03:30
}