HyperDbg Debugger
Loading...
Searching...
No Matches
Thread.h File Reference

Header for kernel debugger functions for threads. More...

Go to the source code of this file.

Functions

BOOLEAN ThreadInterpretThread (PROCESSOR_DEBUGGING_STATE *DbgState, PDEBUGGEE_DETAILS_AND_SWITCH_THREAD_PACKET TidRequest)
 change the current thread
VOID ThreadEnableOrDisableThreadChangeMonitor (PROCESSOR_DEBUGGING_STATE *DbgState, BOOLEAN Enable, BOOLEAN IsSwitchByClockIntrrupt)
 Enable or disable the thread change monitoring detection on the running core.
BOOLEAN ThreadHandleThreadChange (PROCESSOR_DEBUGGING_STATE *DbgState)
 handle thread changes
BOOLEAN ThreadQueryCount (PDEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS DebuggerUsermodeProcessOrThreadQueryRequest)
 Query thread details (count).
BOOLEAN ThreadQueryList (PDEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS DebuggerUsermodeProcessOrThreadQueryRequest, PVOID AddressToSaveDetail, UINT32 BufferSize)
 Query thread details (list).
BOOLEAN ThreadQueryDetails (PDEBUGGEE_DETAILS_AND_SWITCH_THREAD_PACKET GetInformationThreadRequest)
 Query thread details.
BOOLEAN ThreadQueryDebugRegisterInterceptionStateByCoreId (UINT32 CoreId)
 Query for deubg register interception state.

Detailed Description

Header for kernel debugger functions for threads.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2021-11-23

Function Documentation

◆ ThreadEnableOrDisableThreadChangeMonitor()

VOID ThreadEnableOrDisableThreadChangeMonitor ( PROCESSOR_DEBUGGING_STATE * DbgState,
BOOLEAN Enable,
BOOLEAN IsSwitchByClockIntrrupt )

Enable or disable the thread change monitoring detection on the running core.

should be called on vmx root

Parameters
DbgStateThe state of the debugger on the current core
Enable
IsSwitchByClockIntrrupt
Returns
VOID
617{
618 if (Enable)
619 {
621 DbgState->ThreadOrProcessTracingDetails.InitialSetByClockInterrupt = IsSwitchByClockIntrrupt;
622 }
623 else
624 {
625 //
626 // Avoid future sets/unsets
627 //
630 }
631
632 //
633 // Check if it's a HW breakpoint on gs:[188] or a clock interception
634 //
635 if (!IsSwitchByClockIntrrupt)
636 {
638 }
639 else
640 {
642 }
643}
VOID ThreadDetectChangeByDebugRegisterOnGs(PROCESSOR_DEBUGGING_STATE *DbgState, BOOLEAN Enable)
Enable or disable the thread change monitoring detection on the running core based on putting a HW br...
Definition Thread.c:424
VOID ThreadDetectChangeByInterceptingClockInterrupts(PROCESSOR_DEBUGGING_STATE *DbgState, BOOLEAN Enable)
Enable or disable the thread change monitoring detection on the running core based on intercepting cl...
Definition Thread.c:573
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
BOOLEAN InitialSetByClockInterrupt
Definition State.h:50
BOOLEAN InitialSetThreadChangeEvent
Definition State.h:48
DEBUGGEE_PROCESS_OR_THREAD_TRACING_DETAILS ThreadOrProcessTracingDetails
Definition State.h:179

◆ ThreadHandleThreadChange()

BOOLEAN ThreadHandleThreadChange ( PROCESSOR_DEBUGGING_STATE * DbgState)

handle thread changes

Parameters
DbgStateThe state of the debugger on the current core
Returns
BOOLEAN
23{
24 //
25 // Check if we reached to the target thread or not
26 //
27 if ((g_ThreadSwitch.ThreadId != NULL_ZERO && g_ThreadSwitch.ThreadId == HANDLE_TO_UINT32(PsGetCurrentThreadId())) ||
28 (g_ThreadSwitch.Thread != NULL64_ZERO && g_ThreadSwitch.Thread == PsGetCurrentThread()))
29 {
30 //
31 // Halt the debuggee, we have found the target thread
32 //
34
35 //
36 // Found
37 //
38 return TRUE;
39 }
40
41 //
42 // Not found
43 //
44 return FALSE;
45}
_Use_decl_annotations_ VOID KdHandleBreakpointAndDebugBreakpoints(PROCESSOR_DEBUGGING_STATE *DbgState, DEBUGGEE_PAUSING_REASON Reason, PDEBUGGER_TRIGGERED_EVENT_DETAILS EventDetails)
Handle DBs and BPs for kernel debugger.
Definition Kd.c:1291
#define HANDLE_TO_UINT32(_var)
Definition MetaMacros.h:39
#define NULL_ZERO
Definition BasicTypes.h:110
#define NULL64_ZERO
Definition BasicTypes.h:111
@ DEBUGGEE_PAUSING_REASON_DEBUGGEE_THREAD_SWITCHED
Definition Connection.h:33
DEBUGGEE_REQUEST_TO_CHANGE_THREAD g_ThreadSwitch
Thread switch to ETHREAD or Thread ID.
Definition Global.h:121

◆ ThreadInterpretThread()

BOOLEAN ThreadInterpretThread ( PROCESSOR_DEBUGGING_STATE * DbgState,
PDEBUGGEE_DETAILS_AND_SWITCH_THREAD_PACKET TidRequest )

change the current thread

Parameters
DbgStateThe state of the debugger on the current core
TidRequest
Returns
BOOLEAN
327{
328 switch (TidRequest->ActionType)
329 {
331
332 //
333 // Debugger wants to know current tid, nt!_ETHREAD and process name, etc.
334 //
335 TidRequest->ProcessId = HANDLE_TO_UINT32(PsGetCurrentProcessId());
336 TidRequest->ThreadId = HANDLE_TO_UINT32(PsGetCurrentThreadId());
337 TidRequest->Process = (UINT64)PsGetCurrentProcess();
338 TidRequest->Thread = (UINT64)PsGetCurrentThread();
339 MemoryMapperReadMemorySafe((UINT64)CommonGetProcessNameFromProcessControlBlock(PsGetCurrentProcess()), &TidRequest->ProcessName, 16);
340
341 //
342 // Operation was successful
343 //
345
346 break;
347
349
350 //
351 // Perform the thread switch
352 //
353 if (!ThreadSwitch(DbgState,
354 TidRequest->ThreadId,
355 (PETHREAD)TidRequest->Thread,
356 TidRequest->CheckByClockInterrupt))
357 {
359 break;
360 }
361
362 //
363 // Operation was successful
364 //
366
367 break;
368
370
371 //
372 // Show the threads list
373 //
374 if (!ThreadShowList(&TidRequest->ThreadListSymDetails,
376 NULL,
377 NULL,
378 (UINT64)NULL))
379 {
381 break;
382 }
383
384 //
385 // Operation was successful
386 //
388
389 break;
390
391 default:
392
393 //
394 // Invalid type of action
395 //
397
398 break;
399 }
400
401 //
402 // Check if the above operation contains error
403 //
404 if (TidRequest->Result == DEBUGGER_OPERATION_WAS_SUCCESSFUL)
405 {
406 return TRUE;
407 }
408 else
409 {
410 return FALSE;
411 }
412}
BOOLEAN ThreadShowList(PDEBUGGEE_THREAD_LIST_NEEDED_DETAILS ThreadListSymbolInfo, DEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS_ACTIONS QueryAction, UINT32 *CountOfThreads, PVOID ListSaveBuffer, UINT64 ListSaveBuffSize)
shows the threads list
Definition Thread.c:122
BOOLEAN ThreadSwitch(PROCESSOR_DEBUGGING_STATE *DbgState, UINT32 ThreadId, PETHREAD EThread, BOOLEAN CheckByClockInterrupt)
make evnvironment ready to change the thread
Definition Thread.c:58
#define DEBUGGER_ERROR_DETAILS_OR_SWITCH_THREAD_INVALID_PARAMETER
error, for thread switch or thread details, invalid parameter
Definition ErrorCodes.h:239
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
@ DEBUGGEE_DETAILS_AND_SWITCH_THREAD_GET_THREAD_LIST
Definition RequestStructures.h:1009
@ DEBUGGEE_DETAILS_AND_SWITCH_THREAD_PERFORM_SWITCH
Definition RequestStructures.h:1007
@ DEBUGGEE_DETAILS_AND_SWITCH_THREAD_GET_THREAD_DETAILS
Definition RequestStructures.h:1008
@ DEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS_ACTION_SHOW_INSTANTLY
Definition RequestStructures.h:724
IMPORT_EXPORT_VMM BOOLEAN MemoryMapperReadMemorySafe(_In_ UINT64 VaAddressToRead, _Inout_ PVOID BufferToSaveMemory, _In_ SIZE_T SizeToRead)
PCHAR CommonGetProcessNameFromProcessControlBlock(PEPROCESS Eprocess)
Get process name by eprocess.
Definition Common.c:48
UCHAR ProcessName[16]
Definition RequestStructures.h:1025
DEBUGGEE_DETAILS_AND_SWITCH_THREAD_TYPE ActionType
Definition RequestStructures.h:1019
UINT64 Thread
Definition RequestStructures.h:1022
DEBUGGEE_THREAD_LIST_NEEDED_DETAILS ThreadListSymDetails
Definition RequestStructures.h:1026
UINT64 Process
Definition RequestStructures.h:1023
UINT32 ProcessId
Definition RequestStructures.h:1021
UINT32 ThreadId
Definition RequestStructures.h:1020
BOOLEAN CheckByClockInterrupt
Definition RequestStructures.h:1024
UINT32 Result
Definition RequestStructures.h:1027

◆ ThreadQueryCount()

BOOLEAN ThreadQueryCount ( PDEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS DebuggerUsermodeProcessOrThreadQueryRequest)

Query thread details (count).

Parameters
DebuggerUsermodeProcessOrThreadQueryRequest
Returns
BOOLEAN
654{
655 BOOLEAN Result = FALSE;
656
657 //
658 // Getting the count results
659 //
660 Result = ThreadShowList(&DebuggerUsermodeProcessOrThreadQueryRequest->ThreadListNeededDetails,
662 &DebuggerUsermodeProcessOrThreadQueryRequest->Count,
663 NULL,
664 (UINT64)NULL);
665
666 if (Result && DebuggerUsermodeProcessOrThreadQueryRequest->Count != 0)
667 {
668 DebuggerUsermodeProcessOrThreadQueryRequest->Result = DEBUGGER_OPERATION_WAS_SUCCESSFUL;
669 return TRUE;
670 }
671
672 DebuggerUsermodeProcessOrThreadQueryRequest->Result = DEBUGGER_ERROR_UNABLE_TO_QUERY_COUNT_OF_PROCESSES_OR_THREADS;
673 return FALSE;
674}
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define DEBUGGER_ERROR_UNABLE_TO_QUERY_COUNT_OF_PROCESSES_OR_THREADS
error, unable to query count of processes or threads
Definition ErrorCodes.h:386
@ DEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS_ACTION_QUERY_COUNT
Definition RequestStructures.h:725
UINT32 Count
Definition RequestStructures.h:799
DEBUGGEE_THREAD_LIST_NEEDED_DETAILS ThreadListNeededDetails
Definition RequestStructures.h:796
UINT64 Result
Definition RequestStructures.h:800

◆ ThreadQueryDebugRegisterInterceptionStateByCoreId()

BOOLEAN ThreadQueryDebugRegisterInterceptionStateByCoreId ( UINT32 CoreId)

Query for deubg register interception state.

Parameters
CoreId
Returns
BOOLEAN whether it's activated or not
554{
555 BOOLEAN Result = FALSE;
556 PROCESSOR_DEBUGGING_STATE * DbgState = &g_DbgState[CoreId];
557
559
560 return Result;
561}
PROCESSOR_DEBUGGING_STATE * g_DbgState
Save the state and variables related to debugging on each to logical core.
Definition Global.h:17
struct _PROCESSOR_DEBUGGING_STATE PROCESSOR_DEBUGGING_STATE
Saves the debugger state.
BOOLEAN DebugRegisterInterceptionState
Definition State.h:56

◆ ThreadQueryDetails()

BOOLEAN ThreadQueryDetails ( PDEBUGGEE_DETAILS_AND_SWITCH_THREAD_PACKET GetInformationThreadRequest)

Query thread details.

Parameters
GetInformationThreadRequest
Returns
BOOLEAN
713{
714 GetInformationThreadRequest->ProcessId = HANDLE_TO_UINT32(PsGetCurrentProcessId());
715 GetInformationThreadRequest->Process = (UINT64)PsGetCurrentProcess();
716 GetInformationThreadRequest->Thread = (UINT64)PsGetCurrentThread();
717 GetInformationThreadRequest->ThreadId = HANDLE_TO_UINT32(PsGetCurrentThreadId());
718
719 RtlCopyMemory(&GetInformationThreadRequest->ProcessName,
720 CommonGetProcessNameFromProcessControlBlock(PsGetCurrentProcess()),
721 15);
722
723 GetInformationThreadRequest->Result = DEBUGGER_OPERATION_WAS_SUCCESSFUL;
724
725 return TRUE;
726}

◆ ThreadQueryList()

BOOLEAN ThreadQueryList ( PDEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS DebuggerUsermodeProcessOrThreadQueryRequest,
PVOID AddressToSaveDetail,
UINT32 BufferSize )

Query thread details (list).

Parameters
DebuggerUsermodeProcessOrThreadQueryRequest
AddressToSaveDetail
BufferSize
Returns
BOOLEAN
689{
690 BOOLEAN Result = FALSE;
691
692 //
693 // Getting the list of threads
694 //
695 Result = ThreadShowList(&DebuggerUsermodeProcessOrThreadQueryRequest->ThreadListNeededDetails,
697 NULL,
698 AddressToSaveDetail,
699 BufferSize);
700
701 return Result;
702}
@ DEBUGGER_QUERY_ACTIVE_PROCESSES_OR_THREADS_ACTION_QUERY_SAVE_DETAILS
Definition RequestStructures.h:726