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

!trace command More...

#include "pch.h"

Functions

VOID CommandTraceHelp ()
 help of the !trace command
VOID CommandTrace (vector< CommandToken > CommandTokens, string Command)
 !trace command handler

Detailed Description

!trace command

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.7
Date
2023-11-02

Function Documentation

◆ CommandTrace()

VOID CommandTrace ( vector< CommandToken > CommandTokens,
string Command )

!trace command handler

Parameters
CommandTokens
Command
Returns
VOID
52{
54 PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
55 PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
56 PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
57 UINT32 EventLength;
58 UINT32 ActionBreakToDebuggerLength = 0;
59 UINT32 ActionCustomCodeLength = 0;
60 UINT32 ActionScriptLength = 0;
61 DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
62 BOOLEAN SetTraceType = FALSE;
64
65 //
66 // Interpret and fill the general event and action fields
67 //
68 //
70 &CommandTokens,
72 &Event,
73 &EventLength,
74 &ActionBreakToDebugger,
75 &ActionBreakToDebuggerLength,
76 &ActionCustomCode,
77 &ActionCustomCodeLength,
78 &ActionScript,
79 &ActionScriptLength,
80 &EventParsingErrorCause))
81 {
82 return;
83 }
84
85 //
86 // Check for size
87 //
88 if (CommandTokens.size() > 2)
89 {
90 ShowMessages("incorrect use of the '%s'\n\n",
91 GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
93
94 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
95 return;
96 }
97
98 //
99 // Interpret command specific details (if any)
100 //
101 for (auto Section : CommandTokens)
102 {
103 if (CompareLowerCaseStrings(Section, "!trace"))
104 {
105 continue;
106 }
107 else if ((CompareLowerCaseStrings(Section, "step-in") || CompareLowerCaseStrings(Section, "stepin") || CompareLowerCaseStrings(Section, "step")) && !SetTraceType)
108 {
110 SetTraceType = TRUE;
111 }
112 else if ((CompareLowerCaseStrings(Section, "step-out") || CompareLowerCaseStrings(Section, "stepout")) && !SetTraceType)
113 {
115 SetTraceType = TRUE;
116 }
117 else if ((CompareLowerCaseStrings(Section, "step-instrument") || CompareLowerCaseStrings(Section, "instrument-step") ||
118 CompareLowerCaseStrings(Section, "instrumentstep") ||
119 CompareLowerCaseStrings(Section, "instrument-step-in")) &&
120 !SetTraceType)
121 {
123 SetTraceType = TRUE;
124 }
125 else
126 {
127 //
128 // Couldn't resolve or unknown parameter
129 //
130 ShowMessages("err, couldn't resolve error at '%s'\n\n",
133
134 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
135 }
136 }
137
138 //
139 // Check if user specified the execution mode or not
140 //
141 if (!SetTraceType)
142 {
143 ShowMessages("please specify the trace type\n");
144
145 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
146 return;
147 }
148
149 //
150 // Set the first parameter to the required trace type
151 //
152 Event->Options.OptionalParam1 = (UINT64)SetTraceType;
153
154 //
155 // Send the ioctl to the kernel for event registration
156 //
157 if (!SendEventToKernel(Event, EventLength))
158 {
159 //
160 // There was an error, probably the handle was not initialized
161 // we have to free the Action before exit, it is because, we
162 // already freed the Event and string buffers
163 //
164
165 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
166 return;
167 }
168
169 //
170 // Add the event to the kernel
171 //
172 if (!RegisterActionToEvent(Event,
173 ActionBreakToDebugger,
174 ActionBreakToDebuggerLength,
175 ActionCustomCode,
176 ActionCustomCodeLength,
177 ActionScript,
178 ActionScriptLength))
179 {
180 //
181 // There was an error
182 //
183
184 FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
185 return;
186 }
187}
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
@ DEBUGGER_EVENT_TRACE_TYPE_INVALID
Definition Events.h:221
@ DEBUGGER_EVENT_TRACE_TYPE_INSTRUMENTATION_STEP_IN
Definition Events.h:224
@ DEBUGGER_EVENT_TRACE_TYPE_STEP_IN
Definition Events.h:222
@ DEBUGGER_EVENT_TRACE_TYPE_STEP_OUT
Definition Events.h:223
@ TRAP_EXECUTION_INSTRUCTION_TRACE
Definition Events.h:170
enum _DEBUGGER_EVENT_TRACE_TYPE DEBUGGER_EVENT_TRACE_TYPE
Type of tracing events.
struct _DEBUGGER_GENERAL_EVENT_DETAIL * PDEBUGGER_GENERAL_EVENT_DETAIL
struct _DEBUGGER_GENERAL_ACTION * PDEBUGGER_GENERAL_ACTION
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
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:1688
BOOLEAN InterpretGeneralEventAndActionsFields(vector< CommandToken > *CommandTokens, 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:1736
BOOLEAN SendEventToKernel(PDEBUGGER_GENERAL_EVENT_DETAIL Event, UINT32 EventBufferLength)
Register the event to the kernel.
Definition debugger.cpp:1367
BOOLEAN RegisterActionToEvent(PDEBUGGER_GENERAL_EVENT_DETAIL Event, PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger, UINT32 ActionBreakToDebuggerLength, PDEBUGGER_GENERAL_ACTION ActionCustomCode, UINT32 ActionCustomCodeLength, PDEBUGGER_GENERAL_ACTION ActionScript, UINT32 ActionScriptLength)
Register the action to the event.
Definition debugger.cpp:1483
enum _DEBUGGER_EVENT_PARSING_ERROR_CAUSE DEBUGGER_EVENT_PARSING_ERROR_CAUSE
Reason for error in parsing commands.
NULL()
Definition test-case-generator.py:530
UINT64 OptionalParam1
Definition Events.h:277
DEBUGGER_EVENT_OPTIONS Options
Definition Events.h:396
VOID CommandTraceHelp()
help of the !trace command
Definition trace.cpp:20

◆ CommandTraceHelp()

VOID CommandTraceHelp ( )

help of the !trace command

Returns
VOID
21{
22 ShowMessages("!trace : traces the execution of user-mode/kernel-mode instructions.\n\n");
23
24 ShowMessages("syntax : \t!trace [TraceType (string)] [pid ProcessId (hex)] [core CoreId (hex)] [imm IsImmediate (yesno)] "
25 "[sc EnableShortCircuiting (onoff)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
26 "[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
27
28 ShowMessages("\n");
29 ShowMessages("\t\te.g : !trace step-out\n");
30 ShowMessages("\t\te.g : !trace step-in pid 1c0\n");
31 ShowMessages("\t\te.g : !trace instrument-step core 2 pid 400\n");
32 ShowMessages("\t\te.g : !trace instrument-step asm code { nop; nop; nop }\n");
33
34 ShowMessages("\n");
35 ShowMessages("valid trace types: \n");
36
37 ShowMessages("\tstep-in : single step-in (regular)\n");
38 ShowMessages("\tstep-out : single step-out (regular)\n");
39 ShowMessages("\tinstrument-step : single step-in (instrumentation)\n");
40}