HyperDbg Debugger
Loading...
Searching...
No Matches
TraceApi.c File Reference

Tracing routines for HyperTrace module. More...

#include "pch.h"

Functions

BOOLEAN HyperTraceInitCallback (HYPERTRACE_CALLBACKS *HyperTraceCallbacks, BOOLEAN RunningOnHypervisorEnvironment)
 Initialize the hypertrace module callbacks.
VOID HyperTraceUninit ()
 Uninitialize the hypertrace module.

Detailed Description

Tracing routines for HyperTrace module.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.19
Date
2026-04-25

Function Documentation

◆ HyperTraceInitCallback()

BOOLEAN HyperTraceInitCallback ( HYPERTRACE_CALLBACKS * HyperTraceCallbacks,
BOOLEAN RunningOnHypervisorEnvironment )

Initialize the hypertrace module callbacks.

This only for callback initialization, not for LBR, PT, etc. initialization

Parameters
HyperTraceCallbacksPointer to the HyperTrace callbacks structure to be registered
RunningOnHypervisorEnvironmentWhether the initialization is being done for hypervisor environment or not, it can be used to skip some of the initialization steps if it is not for hypervisor environment and behave differently based on that
Returns
BOOLEAN
26{
27 UINT32 ProcessorsCount = 0;
28
29 //
30 // Check if any of the required callbacks are NULL
31 //
32 for (UINT32 i = 0; i < sizeof(HYPERTRACE_CALLBACKS) / sizeof(UINT64); i++)
33 {
34 if (((PVOID *)HyperTraceCallbacks)[i] == NULL)
35 {
36 //
37 // The callback has null entry, so we cannot proceed
38 //
39 return FALSE;
40 }
41 }
42
43 //
44 // Save the callbacks
45 //
46 PlatformWriteMemory(&g_Callbacks, HyperTraceCallbacks, sizeof(HYPERTRACE_CALLBACKS));
47
48 //
49 // Query the number of processors in the system to initialize the global LBR state list accordingly
50 //
51 ProcessorsCount = PlatformCpuGetActiveProcessorCount();
52
53 //
54 // Initialize the global LBR state list to hold LBR states for each core
55 //
57
58 //
59 // Initialize the global PT per-CPU state list. Each entry starts in
60 // PT_STATE_DISABLED with no buffers allocated; PtStart() will lazily
61 // allocate ToPA / output / overflow buffers on first use per core.
62 //
64
65 if (g_PtStateList != NULL)
66 {
67 UINT32 i;
68 for (i = 0; i < ProcessorsCount; i++)
69 {
72 }
73 }
74
75 //
76 // Set the flag to indicate whether the initialization is being done for hypervisor environment or not
77 //
78 g_RunningOnHypervisorEnvironment = RunningOnHypervisorEnvironment;
79
80 //
81 // It is initialized, but LBR is disabled at this stage
82 //
84
85 //
86 // It is initialized, but Processor Trace is disabled at this stage
87 //
89
90 //
91 // Enable callbacks and set the initialized flag
92 //
94
95 return TRUE;
96}
ULONG PlatformCpuGetActiveProcessorCount(VOID)
Get the count of active logical processors.
Definition PlatformCpu.c:24
PVOID PlatformMemAllocateZeroedNonPagedPool(SIZE_T NumberOfBytes)
Allocates zeroed non-paged pool memory.
Definition PlatformMem.c:248
VOID PlatformWriteMemory(PVOID Address, PVOID Buffer, SIZE_T Size)
Writes data from a buffer to a memory address.
Definition PlatformMem.c:115
void * PVOID
Definition BasicTypes.h:56
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
struct _LBR_STACK_ENTRY LBR_STACK_ENTRY
The structure to hold the LBR stack for a single processor core, including the branch entries and the...
struct _PT_PER_CPU PT_PER_CPU
Per-CPU Intel PT state — one of these per logical processor.
@ PT_STATE_DISABLED
Definition PtDefinitions.h:218
struct _HYPERTRACE_CALLBACKS HYPERTRACE_CALLBACKS
Prototype of each function needed by hypertrace module.
HYPEREVADE_CALLBACKS g_Callbacks
List of callbacks.
Definition Transparency.h:23
BOOLEAN g_RunningOnHypervisorEnvironment
The flag indicating whether the initialization is being done for hypervisor environment or not.
Definition GlobalVariables.h:35
VOID PtEngineInitDefaultConfig(PT_TRACE_CONFIG *Config)
Initialize a PT_TRACE_CONFIG with sensible defaults. Trace user + kernel, branch + TSC packets,...
Definition Pt.c:342
PT_PER_CPU * g_PtStateList
Dynamically allocated array of per-CPU Intel PT state. Sized to KeQueryActiveProcessorCount(0) at hyp...
Definition GlobalVariables.h:84
LBR_STACK_ENTRY * g_LbrStateList
This will be a dynamically allocated array to hold LBR states for each core.
Definition GlobalVariables.h:54
BOOLEAN g_ProcessorTraceEnabled
The flag indicating whether the hypertrace Processor Trace is initialized or not.
Definition GlobalVariables.h:78
BOOLEAN g_HyperTraceCallbacksInitialized
The flag indicating whether the hypertrace module callbacks is initialized or not.
Definition GlobalVariables.h:29
BOOLEAN g_LastBranchRecordEnabled
The flag indicating whether the hypertrace LBR tracing is initialized or not.
Definition GlobalVariables.h:48

◆ HyperTraceUninit()

VOID HyperTraceUninit ( )

Uninitialize the hypertrace module.

Returns
VOID
105{
106 //
107 // Check if the callbacks are initialized, if not, we don't need to handle anymore
108 //
110 {
111 return;
112 }
113
114 //
115 // Disable LBR tracing if it is still enabled
116 //
118 {
120 }
121
122 //
123 // Unallocate the global LBR state list if it is allocated
124 //
125 if (g_LbrStateList != NULL)
126 {
129 }
130
131 //
132 // Disable Processor Trace if it is still enabled
133 //
135 {
137 }
138
139 //
140 // Free PT buffers (if any) and the per-CPU state list
141 //
142 if (g_PtStateList != NULL)
143 {
144 UINT32 ProcessorsCountLocal = KeQueryActiveProcessorCount(0);
145 UINT32 i;
146
147 for (i = 0; i < ProcessorsCountLocal; i++)
148 {
150 }
151
154 }
155
156 //
157 // Reset the environment flag to default value
158 //
160
161 //
162 // Set callbacks to not initialized
163 //
165}
BOOLEAN HyperTraceLbrDisable(HYPERTRACE_LBR_OPERATION_PACKETS *HyperTraceOperationRequest)
Disable LBR tracing for HyperTrace.
Definition LbrApi.c:278
PVOID PlatformMemFreePool(PVOID BufferAddress)
Frees a memory pool.
Definition PlatformMem.c:269
IMPORT_EXPORT_HYPERTRACE BOOLEAN HyperTracePtDisable(HYPERTRACE_PT_OPERATION_PACKETS *PtOperationRequest)
Disable PT tracing for HyperTrace.
Definition PtApi.c:157
VOID PtEngineFreeBuffers(PT_PER_CPU *Cpu)
Free all PT buffers belonging to one per-CPU context. Must not be called while State == PT_STATE_TRAC...
Definition Pt.c:482
NULL()
Definition test-case-generator.py:530