HyperDbg Debugger
Loading...
Searching...
No Matches
hw_clk.cpp File Reference

!hw_clk command More...

#include "pch.h"

Functions

VOID CommandHwClkHelp ()
 help of the !hw_clk command
 
VOID CommandHwClk (vector< string > SplitCommand, string Command)
 !hw_clk command handler
 

Variables

HWDBG_INSTANCE_INFORMATION g_HwdbgInstanceInfo
 Instance information of the current hwdbg debuggee.
 
BOOLEAN g_HwdbgInstanceInfoIsValid
 Shows whether the instance info is valid (received) or not.
 
std::vector< UINT32g_HwdbgPortConfiguration
 Ports configuration of hwdbg.
 
const char * HwdbgActionEnumNames []
 

Detailed Description

!hw_clk command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.9
Date
2024-05-29

Function Documentation

◆ CommandHwClk()

VOID CommandHwClk ( vector< string > SplitCommand,
string Command )

!hw_clk command handler

Parameters
SplitCommand
Command
Returns
VOID
47{
49 PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
50 PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
51 PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
52 UINT32 EventLength;
53 UINT64 SpecialTarget = 0;
54 UINT32 ActionBreakToDebuggerLength = 0;
55 UINT32 ActionCustomCodeLength = 0;
56 UINT32 ActionScriptLength = 0;
57 UINT32 NumberOfStagesForScript = 0;
58 UINT32 NumberOfOperandsForScript = 0;
59 size_t NewCompressedBufferSize = 0;
60 size_t NumberOfNeededFlipFlopsInTargetDevice = 0;
61 size_t NumberOfBytesPerChunk = 0;
62 vector<string> SplitCommandCaseSensitive {Split(Command, ' ')};
63 DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
64 HWDBG_SHORT_SYMBOL * NewScriptBuffer = NULL;
65
66 if (SplitCommand.size() >= 2 && !SplitCommand.at(1).compare("test"))
67 {
68 TCHAR TestFilePath[MAX_PATH] = {0};
69 const SIZE_T BufferSize = 256; // Adjust based on the number of memory entries of the file
70 UINT32 PortNum = 0;
71 UINT32 MemoryBuffer[BufferSize];
72
73 if (SetupPathForFileName(HWDBG_TEST_READ_INSTANCE_INFO_PATH, TestFilePath, sizeof(TestFilePath), TRUE) &&
74 HwdbgInterpreterFillMemoryFromFile(TestFilePath, MemoryBuffer, BufferSize))
75 {
76 //
77 // Print the content of MemoryBuffer for verification
78 //
79 for (SIZE_T I = 0; I < BufferSize; ++I)
80 {
81 ShowMessages("%08x ", MemoryBuffer[I]);
82 ShowMessages("\n");
83 }
84 }
85
86 //
87 // Interpret packet
88 //
89 if (HwdbgInterpretPacket(MemoryBuffer, BufferSize))
90 {
91 ShowMessages("instance info interpreted successfully\n");
92
93 ShowMessages("Debuggee Version: 0x%x\n", g_HwdbgInstanceInfo.version);
94 ShowMessages("Debuggee Maximum Number Of Stages: 0x%x\n", g_HwdbgInstanceInfo.maximumNumberOfStages);
95 ShowMessages("Debuggee Script Variable Length: 0x%x\n", g_HwdbgInstanceInfo.scriptVariableLength);
96 ShowMessages("Debuggee Number of Supported Local (and global) Variables: 0x%x\n", g_HwdbgInstanceInfo.numberOfSupportedLocalAndGlobalVariables);
97 ShowMessages("Debuggee Number of Supported Temporary Variables: 0x%x\n", g_HwdbgInstanceInfo.numberOfSupportedTemporaryVariables);
98 ShowMessages("Debuggee Maximum Number Of Supported GET Script Operators: 0x%x\n", g_HwdbgInstanceInfo.maximumNumberOfSupportedGetScriptOperators);
99 ShowMessages("Debuggee Maximum Number Of Supported SET Script Operators: 0x%x\n", g_HwdbgInstanceInfo.maximumNumberOfSupportedSetScriptOperators);
100 ShowMessages("Debuggee Shared Memory Size: 0x%x\n", g_HwdbgInstanceInfo.sharedMemorySize);
101 ShowMessages("Debuggee Debugger Area Offset: 0x%x\n", g_HwdbgInstanceInfo.debuggerAreaOffset);
102 ShowMessages("Debuggee Debuggee Area Offset: 0x%x\n", g_HwdbgInstanceInfo.debuggeeAreaOffset);
103 ShowMessages("Debuggee Script Capabilities Mask: 0x%llx\n", g_HwdbgInstanceInfo.scriptCapabilities);
104
105 //
106 // Show script capabilities
107 //
109
110 ShowMessages("Debuggee Number Of Pins: 0x%x\n", g_HwdbgInstanceInfo.numberOfPins);
111 ShowMessages("Debuggee Number Of Ports: 0x%x\n", g_HwdbgInstanceInfo.numberOfPorts);
112
113 ShowMessages("Debuggee BRAM Address Width: 0x%x\n", g_HwdbgInstanceInfo.bramAddrWidth);
114 ShowMessages("Debuggee BRAM Data Width: 0x%x (%d bit)\n", g_HwdbgInstanceInfo.bramDataWidth, g_HwdbgInstanceInfo.bramDataWidth);
115
116 for (auto item : g_HwdbgPortConfiguration)
117 {
118 ShowMessages("Port number %d ($hw_port%d): 0x%x\n", PortNum, PortNum, item);
119 PortNum++;
120 }
121 }
122 else
123 {
124 ShowMessages("err, unable to interpret instance info packet of the debuggee");
125 return;
126 }
127
128 //
129 // Interpret and fill the general event and action fields
130 //
131 //
133 &SplitCommand,
134 &SplitCommandCaseSensitive,
135 (VMM_EVENT_TYPE_ENUM)NULL, // not an event
136 &Event,
137 &EventLength,
138 &ActionBreakToDebugger,
139 &ActionBreakToDebuggerLength,
140 &ActionCustomCode,
141 &ActionCustomCodeLength,
142 &ActionScript,
143 &ActionScriptLength,
144 &EventParsingErrorCause))
145 {
146 return;
147 }
148
149 //
150 // Print the actual script
151 //
152 ShowMessages("\nHyperDbg (general) script buffer (size=%d, flip-flops (just script)=%d):\n\n",
153 ActionScript->ScriptBufferSize,
154 ActionScript->ScriptBufferSize * 8 // Converted to bits
155 );
156
157 CHAR * ScriptBuffer = (CHAR *)((UINT64)ActionScript + sizeof(DEBUGGER_GENERAL_ACTION));
158
159 for (size_t i = 0; i < ActionScript->ScriptBufferSize; i++)
160 {
161 ShowMessages("%02X ", (UINT8)ScriptBuffer[i]);
162 }
163
164 ShowMessages("\n");
165
166 //
167 // Check the script capabilities with the generated script
168 //
170 ScriptBuffer,
171 ActionScript->ScriptBufferSize / sizeof(SYMBOL),
172 &NumberOfStagesForScript,
173 &NumberOfOperandsForScript))
174 {
175 ShowMessages("\n[+] target script is supported by this instance of hwdbg!\n");
176
177 //
178 // Now, converting the script based on supported script variable length
179 //
181 {
182 //
183 // Conversion needed
184 //
186 {
187 //
188 // The script variable length is valid (at least 8 bit (1 byte)
189 //
190
191 //
192 // Compress script buffer
193 //
195 (SYMBOL *)ScriptBuffer,
196 ActionScript->ScriptBufferSize,
197 NumberOfStagesForScript,
198 &NewScriptBuffer,
199 &NewCompressedBufferSize) == TRUE &&
200
201 //
202 // we put bram data width size here instead of script variable length (g_HwdbgInstanceInfo.scriptVariableLength)
203 // since we want it to read one symbol filed at a time
204 //
205 HwdbgInterpreterCompressBuffer((UINT64 *)NewScriptBuffer,
206 NewCompressedBufferSize,
209 &NewCompressedBufferSize,
210 &NumberOfBytesPerChunk) == TRUE)
211 {
212 ShowMessages("\n---------------------------------------------------------\n");
213
215
216 //
217 // Calculate the number of flip-flops needed in the target device
218 // + operator symbol itself which only contains value (type is always equal to SYMBOL_SEMANTIC_RULE_TYPE)
219 // so, it is not counted as a flip-flop
220 //
221 NumberOfNeededFlipFlopsInTargetDevice = (NumberOfStagesForScript * (g_HwdbgInstanceInfo.maximumNumberOfSupportedGetScriptOperators + g_HwdbgInstanceInfo.maximumNumberOfSupportedSetScriptOperators) * g_HwdbgInstanceInfo.scriptVariableLength * sizeof(HWDBG_SHORT_SYMBOL) / sizeof(UINT64)) + // size of operator (GET and SET)
222 (NumberOfStagesForScript * g_HwdbgInstanceInfo.scriptVariableLength * (sizeof(HWDBG_SHORT_SYMBOL) / sizeof(UINT64)) / 2) + // size of main operator (/ 2 is becasue Type is not inffered)
223 (NumberOfStagesForScript * g_HwdbgInstanceInfo.numberOfSupportedLocalAndGlobalVariables * g_HwdbgInstanceInfo.scriptVariableLength) + // size of local (and global) variables
224 (NumberOfStagesForScript * g_HwdbgInstanceInfo.numberOfSupportedTemporaryVariables * g_HwdbgInstanceInfo.scriptVariableLength) + // size of temporary variables
226 (NumberOfStagesForScript) + // stage enable flip-flop
227 (NumberOfStagesForScript * g_HwdbgInstanceInfo.numberOfPins); // input => output flip-flop
228
229 ShowMessages("hwdbg script buffer (buffer size=%d, stages=%d, operands needed: %d - operands used: %d (%.2f%%), total used flip-flops=%d, number of bytes per chunk: %d):\n\n",
230 NewCompressedBufferSize,
231 NumberOfStagesForScript,
232 NumberOfOperandsImplemented,
233 NumberOfOperandsForScript,
234 ((float)NumberOfOperandsForScript / (float)NumberOfOperandsImplemented) * 100,
235 NumberOfNeededFlipFlopsInTargetDevice,
236 NumberOfBytesPerChunk);
237
238 for (size_t i = 0; i < NewCompressedBufferSize; i++)
239 {
240 ShowMessages("%02X ", (UINT8)((CHAR *)NewScriptBuffer)[i]);
241 }
242
243 ShowMessages("\n\nwriting script configuration packet into the file\n");
244
245 //
246 // *** Write script configuration packet into a file ***
247 //
248 if (SetupPathForFileName(HWDBG_TEST_WRITE_SCRIPT_BUFFER_PATH, TestFilePath, sizeof(TestFilePath), FALSE) &&
250 TestFilePath,
251 NumberOfStagesForScript + NumberOfOperandsImplemented - 1, // Number of symbols = Number of stages + Number of operands - 1
252 NewScriptBuffer,
253 (UINT32)NewCompressedBufferSize))
254 {
255 ShowMessages("\n[*] script buffer successfully written into file: %s\n", TestFilePath);
256 }
257 else
258 {
259 ShowMessages("err, unable to write script buffer\n");
260 }
261
262 //
263 // *** Write test instance info request into a file ***
264 //
265 if (SetupPathForFileName(HWDBG_TEST_WRITE_INSTANCE_INFO_PATH, TestFilePath, sizeof(TestFilePath), FALSE) &&
268 TestFilePath,
271 NULL,
272 NULL_ZERO))
273 {
274 ShowMessages("[*] instance info successfully written into file: %s\n", TestFilePath);
275 }
276 }
277 }
278 else
279 {
280 //
281 // The script variable length is not valid (at least 8 bit (1 byte)
282 //
283 ShowMessages("err, the script variable length should be at least 8 bits (1 byte)\n");
284 }
285 }
286 }
287 else
288 {
289 ShowMessages("\n[-] target script is NOT supported by this instance of hwdbg!\n");
290 }
291
292 //
293 // Free the allocated memory
294 //
295 if (NewScriptBuffer != NULL)
296 {
297 free(NewScriptBuffer);
298 }
299 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
300 }
301 else
302 {
303 ShowMessages("incorrect use of the '%s'\n\n", SplitCommand.at(0).c_str());
305 return;
306 }
307}
#define NULL_ZERO
Definition BasicTypes.h:51
unsigned char BYTE
Definition BasicTypes.h:24
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned char UINT8
Definition BasicTypes.h:46
unsigned int UINT32
Definition BasicTypes.h:48
char CHAR
Definition BasicTypes.h:31
@ DEBUGGER_REMOTE_PACKET_TYPE_DEBUGGER_TO_DEBUGGEE_HARDWARE_LEVEL
Definition Connection.h:169
@ hwdbgActionSendInstanceInfo
Definition HardwareDebugger.h:42
struct HWDBG_SHORT_SYMBOL HWDBG_SHORT_SYMBOL
const vector< string > Split(const string &s, const char &c)
general split command
Definition common.cpp:117
UINT32 Log2Ceil(UINT32 n)
Function to compute log2Ceil.
Definition common.cpp:1216
VOID FreeEventsAndActionsMemory(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, PDEBUGGER_GENERAL_ACTION ActionCustomCode, PDEBUGGER_GENERAL_ACTION ActionScript)
Deallocate buffers relating to events and actions.
Definition debugger.cpp:2292
BOOLEAN InterpretGeneralEventAndActionsFields(vector< string > *SplitCommand, vector< string > *SplitCommandCaseSensitive, VMM_EVENT_TYPE_ENUM EventType, PDEBUGGER_GENERAL_EVENT_DETAIL *EventDetailsToFill, PUINT32 EventBufferLength, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillBreakToDebugger, PUINT32 ActionBufferLengthBreakToDebugger, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillCustomCode, PUINT32 ActionBufferLengthCustomCode, PDEBUGGER_GENERAL_ACTION *ActionDetailsToFillScript, PUINT32 ActionBufferLengthScript, PDEBUGGER_EVENT_PARSING_ERROR_CAUSE ReasonForErrorInParsing)
Interpret general event fields.
Definition debugger.cpp:2342
VOID CommandHwClkHelp()
help of the !hw_clk command
Definition hw_clk.cpp:28
std::vector< UINT32 > g_HwdbgPortConfiguration
Ports configuration of hwdbg.
Definition globals.h:693
HWDBG_INSTANCE_INFORMATION g_HwdbgInstanceInfo
Instance information of the current hwdbg debuggee.
Definition globals.h:681
BOOLEAN g_HwdbgInstanceInfoIsValid
Shows whether the instance info is valid (received) or not.
Definition globals.h:687
BOOLEAN HwdbgInterpreterFillMemoryFromFile(const TCHAR *FileName, UINT32 *MemoryBuffer, size_t BufferSize)
Function to read the file and fill the memory buffer.
Definition hwdbg-interpreter.cpp:577
BOOLEAN HwdbgInterpreterConvertSymbolToHwdbgShortSymbolBuffer(HWDBG_INSTANCE_INFORMATION *InstanceInfo, SYMBOL *SymbolBuffer, size_t SymbolBufferLength, UINT32 NumberOfStages, HWDBG_SHORT_SYMBOL **NewShortSymbolBuffer, size_t *NewBufferSize)
Function to compress the buffer.
Definition hwdbg-interpreter.cpp:806
BOOLEAN HwdbgInterpreterSendScriptPacket(HWDBG_INSTANCE_INFORMATION *InstanceInfo, const TCHAR *FileName, UINT32 NumberOfSymbols, HWDBG_SHORT_SYMBOL *Buffer, UINT32 BufferLength)
Sends a HyperDbg script packet to the hwdbg.
Definition hwdbg-interpreter.cpp:1131
VOID HwdbgInterpreterShowScriptCapabilities(HWDBG_INSTANCE_INFORMATION *InstanceInfo)
Shows the script capablities of the target debuggee.
Definition hwdbg-interpreter.cpp:179
BOOLEAN HwdbgInterpreterCheckScriptBufferWithScriptCapabilities(HWDBG_INSTANCE_INFORMATION *InstanceInfo, PVOID ScriptBuffer, UINT32 CountOfScriptSymbolChunks, UINT32 *NumberOfStages, UINT32 *NumberOfOperands)
Check the script capablities with the target script buffer.
Definition hwdbg-interpreter.cpp:238
BOOLEAN HwdbgInterpreterCompressBuffer(UINT64 *Buffer, size_t BufferLength, UINT32 ScriptVariableLength, UINT32 BramDataWidth, size_t *NewBufferSize, size_t *NumberOfBytesPerChunk)
Function to compress the buffer.
Definition hwdbg-interpreter.cpp:724
BOOLEAN HwdbgInterpretPacket(PVOID BufferReceived, UINT32 LengthReceived)
Interpret packets of hwdbg.
Definition hwdbg-interpreter.cpp:30
BOOLEAN HwdbgInterpreterSendPacketAndBufferToHwdbg(HWDBG_INSTANCE_INFORMATION *InstanceInfo, const TCHAR *FileName, DEBUGGER_REMOTE_PACKET_TYPE PacketType, HWDBG_ACTION_ENUMS RequestedAction, CHAR *Buffer, UINT32 BufferLength)
Sends a HyperDbg packet + a buffer to the hwdbg.
Definition hwdbg-interpreter.cpp:994
#define HWDBG_TEST_WRITE_SCRIPT_BUFFER_PATH
Path to write the sample of the script buffer.
Definition hwdbg-interpreter.h:28
#define HWDBG_TEST_READ_INSTANCE_INFO_PATH
Path to read the sample of the instance info.
Definition hwdbg-interpreter.h:22
#define HWDBG_TEST_WRITE_INSTANCE_INFO_PATH
Path to write the sample of the instance info requests.
Definition hwdbg-interpreter.h:34
enum _VMM_EVENT_TYPE_ENUM VMM_EVENT_TYPE_ENUM
enum to show type of all HyperDbg events
struct _DEBUGGER_GENERAL_ACTION DEBUGGER_GENERAL_ACTION
Each event can have multiple actions.
BOOLEAN SetupPathForFileName(const CHAR *FileName, _Inout_updates_bytes_all_(BufferLength) PCHAR FileLocation, ULONG BufferLength, BOOLEAN CheckFileExists)
Setup file name.
Definition install.cpp:440
enum _DEBUGGER_EVENT_PARSING_ERROR_CAUSE DEBUGGER_EVENT_PARSING_ERROR_CAUSE
Reason for error in parsing commands.
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
NULL()
Definition test-case-generator.py:530
Each event can have multiple actions.
Definition Events.h:406
UINT32 ScriptBufferSize
Definition Events.h:413
Each command is like the following struct, it also used for tracing works in user mode and sending it...
Definition Events.h:350
UINT32 version
Definition HardwareDebugger.h:95
UINT32 sharedMemorySize
Definition HardwareDebugger.h:102
UINT32 maximumNumberOfSupportedGetScriptOperators
Definition HardwareDebugger.h:100
UINT32 debuggeeAreaOffset
Definition HardwareDebugger.h:104
UINT32 numberOfSupportedLocalAndGlobalVariables
Definition HardwareDebugger.h:98
UINT32 debuggerAreaOffset
Definition HardwareDebugger.h:103
struct _HWDBG_INSTANCE_INFORMATION::_HWDBG_SCRIPT_CAPABILITIES scriptCapabilities
UINT32 maximumNumberOfStages
Definition HardwareDebugger.h:96
UINT32 numberOfSupportedTemporaryVariables
Definition HardwareDebugger.h:99
UINT32 numberOfPorts
Definition HardwareDebugger.h:106
UINT32 bramAddrWidth
Definition HardwareDebugger.h:152
UINT32 maximumNumberOfSupportedSetScriptOperators
Definition HardwareDebugger.h:101
UINT32 bramDataWidth
Definition HardwareDebugger.h:153
UINT32 numberOfPins
Definition HardwareDebugger.h:105
UINT32 scriptVariableLength
Definition HardwareDebugger.h:97
Definition ScriptEngineCommonDefinitions.h:15
Definition ScriptEngineCommonDefinitions.h:6

◆ CommandHwClkHelp()

VOID CommandHwClkHelp ( )

help of the !hw_clk command

Returns
VOID
29{
30 ShowMessages("!hw_clk : performs actions related to hwdbg hardware debugging events for each clock cycle.\n\n");
31
32 ShowMessages("syntax : \t!hw_clk [script { Script (string) }]\n");
33
34 ShowMessages("\n");
35 ShowMessages("\t\te.g : !hw_clk script { @hw_pin1 = 0; }\n");
36}

Variable Documentation

◆ g_HwdbgInstanceInfo

HWDBG_INSTANCE_INFORMATION g_HwdbgInstanceInfo
extern

Instance information of the current hwdbg debuggee.

◆ g_HwdbgInstanceInfoIsValid

BOOLEAN g_HwdbgInstanceInfoIsValid
extern

Shows whether the instance info is valid (received) or not.

◆ g_HwdbgPortConfiguration

std::vector<UINT32> g_HwdbgPortConfiguration
extern

Ports configuration of hwdbg.

◆ HwdbgActionEnumNames

const char* HwdbgActionEnumNames[]
extern