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

182 lines
4.9 KiB
C++
Raw Permalink Normal View History

/**
* @file k.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
* @brief k command
* @details
* @version 0.1
* @date 2021-12-13
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2022-03-06 17:39:28 +03:30
//
// Global Variables
//
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
extern BOOLEAN g_IsRunningInstruction32Bit;
/**
2023-07-13 16:05:42 +09:00
* @brief help of the k command
*
* @return VOID
*/
VOID
CommandKHelp()
{
2022-03-05 15:46:03 +03:30
ShowMessages(
2022-03-06 17:39:28 +03:30
"k, kd, kq : shows the callstack of the thread.\n\n");
2022-03-05 15:46:03 +03:30
ShowMessages("syntax : \tk\n");
2022-03-06 17:39:28 +03:30
ShowMessages("syntax : \tkd\n");
ShowMessages("syntax : \tkq\n");
ShowMessages("syntax : \tk [base StackAddress (hex)] [l Length (hex)]\n");
ShowMessages("syntax : \tkd [base StackAddress (hex)] [l Length (hex)]\n");
ShowMessages("syntax : \tkq [base StackAddress (hex)] [l Length (hex)]\n");
ShowMessages("\n");
2022-03-05 15:46:03 +03:30
ShowMessages("\t\te.g : k\n");
2022-03-07 01:59:41 +03:30
ShowMessages("\t\te.g : k l 100\n");
ShowMessages("\t\te.g : kd base 0x77356f010\n");
ShowMessages("\t\te.g : kq base fffff8077356f010\n");
ShowMessages("\t\te.g : kq base @rbx-10\n");
ShowMessages("\t\te.g : kq base fffff8077356f010 l 100\n");
}
/**
* @brief k command handler
*
2024-07-30 13:17:50 +09:00
* @param CommandTokens
* @param Command
2024-07-30 13:17:50 +09:00
*
* @return VOID
*/
VOID
CommandK(vector<CommandToken> CommandTokens, string Command)
{
UINT64 BaseAddress = NULL; // Null base address means current RSP register
UINT32 Length = PAGE_SIZE; // Default length
2024-07-30 13:17:50 +09:00
BOOLEAN IsFirstCommand = TRUE;
BOOLEAN IsNextBase = FALSE;
BOOLEAN IsNextLength = FALSE;
2022-03-06 17:39:28 +03:30
2024-07-30 13:17:50 +09:00
if (CommandTokens.size() >= 6)
2022-03-06 17:39:28 +03:30
{
2024-07-30 13:17:50 +09:00
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2022-03-06 17:39:28 +03:30
CommandKHelp();
return;
}
if (!g_IsSerialConnectedToRemoteDebuggee)
{
ShowMessages("err, tracing callstack is not possible when you're not "
"connected to a debuggee\n");
return;
}
//
// Set the default length
//
if (g_IsRunningInstruction32Bit)
{
Length = PAGE_SIZE;
}
else
{
Length = PAGE_SIZE * 2;
}
2024-07-30 13:17:50 +09:00
for (auto Section : CommandTokens)
2022-03-06 17:39:28 +03:30
{
if (IsFirstCommand)
{
IsFirstCommand = FALSE;
continue;
}
if (IsNextBase == TRUE)
{
2024-07-30 13:17:50 +09:00
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(Section),
2022-03-06 17:39:28 +03:30
&BaseAddress))
{
//
2024-03-17 01:01:22 +09:00
// Couldn't resolve or unknown parameter
2022-03-06 17:39:28 +03:30
//
ShowMessages("err, couldn't resolve error at '%s'\n",
2024-07-30 13:17:50 +09:00
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2022-03-06 17:39:28 +03:30
return;
}
IsNextBase = FALSE;
continue;
}
if (IsNextLength == TRUE)
{
2024-07-30 13:17:50 +09:00
if (!ConvertTokenToUInt32(Section, &Length))
2022-03-06 17:39:28 +03:30
{
ShowMessages("err, you should enter a valid length\n\n");
return;
}
IsNextLength = FALSE;
continue;
}
2024-07-30 13:17:50 +09:00
if (CompareLowerCaseStrings(Section, "l"))
2022-03-06 17:39:28 +03:30
{
IsNextLength = TRUE;
continue;
}
2024-07-30 13:17:50 +09:00
if (CompareLowerCaseStrings(Section, "base"))
2022-03-06 17:39:28 +03:30
{
IsNextBase = TRUE;
continue;
}
//
// User inserts unexpected input
//
2023-07-13 16:05:42 +09:00
ShowMessages("err, incorrect use of the '%s' command\n\n",
2024-07-30 13:17:50 +09:00
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2022-03-06 17:39:28 +03:30
CommandKHelp();
return;
}
if (IsNextLength || IsNextBase)
{
2024-07-30 13:17:50 +09:00
ShowMessages("incorrect use of the '%s' command\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2022-03-06 17:39:28 +03:30
CommandKHelp();
return;
}
//
// Send callstack request
//
2024-07-30 13:17:50 +09:00
if (CompareLowerCaseStrings(CommandTokens.at(0), "k"))
2022-03-06 17:39:28 +03:30
{
KdSendCallStackPacketToDebuggee(BaseAddress,
Length,
2022-03-06 17:39:28 +03:30
DEBUGGER_CALLSTACK_DISPLAY_METHOD_WITHOUT_PARAMS,
g_IsRunningInstruction32Bit);
}
2024-07-30 13:17:50 +09:00
else if (CompareLowerCaseStrings(CommandTokens.at(0), "kq"))
2022-03-06 17:39:28 +03:30
{
KdSendCallStackPacketToDebuggee(BaseAddress,
Length,
DEBUGGER_CALLSTACK_DISPLAY_METHOD_WITH_PARAMS,
FALSE);
}
2024-07-30 13:17:50 +09:00
else if (CompareLowerCaseStrings(CommandTokens.at(0), "kd"))
2022-03-06 17:39:28 +03:30
{
KdSendCallStackPacketToDebuggee(BaseAddress,
Length,
2022-03-06 17:39:28 +03:30
DEBUGGER_CALLSTACK_DISPLAY_METHOD_WITH_PARAMS,
TRUE);
}
}