Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/byakugan.cpp
Views: 11766
#include "byakugan.h"1#include "jutsu.h"234PDEBUG_CLIENT4 g_ExtClient;5PDEBUG_CONTROL g_ExtControl;6PDEBUG_SYMBOLS3 g_ExtSymbols;7PDEBUG_SYSTEM_OBJECTS2 g_ExtSystem;8PDEBUG_DATA_SPACES g_ExtData;910WINDBG_EXTENSION_APIS ExtensionApis;1112ULONG TargetMachine;13BOOL Connected;1415// Queries for all debugger interfaces.16extern "C" HRESULT17ExtQuery(PDEBUG_CLIENT4 Client)18{19HRESULT Status;2021if ((Status = Client->QueryInterface(__uuidof(IDebugControl),22(void **)&g_ExtControl)) != S_OK)23{24goto Fail;25}26if ((Status = Client->QueryInterface(__uuidof(IDebugSymbols3),27(void **)&g_ExtSymbols)) != S_OK)28{29goto Fail;30}31if ((Status = Client->QueryInterface(__uuidof(IDebugSystemObjects2),32(void **)&g_ExtSystem)) != S_OK)33{34goto Fail;35}3637if ((Status = Client->QueryInterface(__uuidof(IDebugDataSpaces),38(void **)&g_ExtData)) != S_OK){39goto Fail;40}4142g_ExtClient = Client;4344return S_OK;4546Fail:47dprintf("Fuck...");48ExtRelease();49return Status;50}5152// Cleans up all debugger interfaces.53void54ExtRelease(void)55{56g_ExtClient = NULL;57EXT_RELEASE(g_ExtControl);58EXT_RELEASE(g_ExtSymbols);59}606162// Normal output.63void __cdecl64ExtOut(PCSTR Format, ...)65{66va_list Args;6768va_start(Args, Format);69g_ExtControl->OutputVaList(DEBUG_OUTPUT_NORMAL, Format, Args);70va_end(Args);71}7273// Error output.74void __cdecl75ExtErr(PCSTR Format, ...)76{77va_list Args;7879va_start(Args, Format);80g_ExtControl->OutputVaList(DEBUG_OUTPUT_ERROR, Format, Args);81va_end(Args);82}8384// Warning output.85void __cdecl86ExtWarn(PCSTR Format, ...)87{88va_list Args;8990va_start(Args, Format);91g_ExtControl->OutputVaList(DEBUG_OUTPUT_WARNING, Format, Args);92va_end(Args);93}9495extern "C"96//jc: this in the init routine. Runs on load.97HRESULT98CALLBACK99DebugExtensionInitialize(PULONG Version, PULONG Flags)100{101IDebugClient *DebugClient;102PDEBUG_CONTROL DebugControl;103HRESULT Hr;104105*Version = DEBUG_EXTENSION_VERSION(1, 0);106*Flags = 0;107Hr = S_OK;108109110111if ((Hr = DebugCreate(__uuidof(IDebugClient),112(void **)&DebugClient)) != S_OK)113{114return Hr;115}116117if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl),118(void **)&DebugControl)) == S_OK)119{120121//122// Get the windbg-style extension APIS123//124ExtensionApis.nSize = sizeof (ExtensionApis);125Hr = DebugControl->GetWindbgExtensionApis64(&ExtensionApis);126127DebugControl->Release();128129}130131dprintf("[Byakugan] Successfully loaded!\n");132DebugClient->Release();133134135return (Hr);136}137138139//jc: this runs when the debugger is connected to a target.140extern "C"141void142CALLBACK143DebugExtensionNotify(ULONG Notify, ULONG64 Argument)144{145UNREFERENCED_PARAMETER(Argument);146147//148// The first time we actually connect to a target149//150/*151*New debugger extensions get new debugger interfaces by calling152*DebugCreate(__uuidof (IDebugClient), &DebugClient))153*DebugClient->QueryInterface(_uuidof(Interface_you_want)154*/155if ((Notify == DEBUG_NOTIFY_SESSION_ACCESSIBLE) && (!Connected))156{157IDebugClient *DebugClient;158HRESULT Hr;159PDEBUG_CONTROL DebugControl;160161if ((Hr = DebugCreate(__uuidof(IDebugClient),162(void **)&DebugClient)) == S_OK)163{164//165// Get the architecture type.166//167168if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl),169(void **)&DebugControl)) == S_OK)170{171//jc:QueryInterface must fill in DebugControl172if ((Hr = DebugControl->GetActualProcessorType(173&TargetMachine)) == S_OK)174{175Connected = TRUE;176}177178179DebugControl->Release();180}181182DebugClient->Release();183}184}185186187if (Notify == DEBUG_NOTIFY_SESSION_INACTIVE)188{189Connected = FALSE;190TargetMachine = 0;191}192193return;194}195196extern "C"197void198CALLBACK199DebugExtensionUninitialize(void)200{201return;202}203204extern "C"205HRESULT206CALLBACK207KnownStructOutput(208__in ULONG Flag,209__in ULONG64 Address,210__in PSTR StructName,211__out_ecount(BufferSize) PSTR Buffer,212__in PULONG BufferSize213)214{215const char* KnownStructs[] = {"_LARGE_INTEGER", "_SYSTEMTIME", NULL};216HRESULT Hr;217218219Hr = S_OK;220221if (Flag == DEBUG_KNOWN_STRUCT_GET_NAMES)222{223//224// Return names of known structs in multi string225//226ULONG SizeRemaining = *BufferSize, SizeNeeded = 0, Length;227PCHAR CopyAt = Buffer;228229for (ULONG i=0; KnownStructs[i] != NULL; i++)230{231if (SizeRemaining > (Length = (ULONG)strlen(KnownStructs[i]) + 1) &&232Hr == S_OK)233{234Hr = StringCbCopy(CopyAt, SizeRemaining, KnownStructs[i]);235236SizeRemaining -= Length;237CopyAt += Length;238} else239{240Hr = S_FALSE;241}242SizeNeeded += Length;243}244// Terminate multistring and return size copied245*CopyAt = 0;246*BufferSize = SizeNeeded+1;247} else if (Flag == DEBUG_KNOWN_STRUCT_GET_SINGLE_LINE_OUTPUT)248{249if (!strcmp(StructName, KnownStructs[0]))250{251ULONG64 Data;252ULONG ret;253254if (ReadMemory(Address, &Data, sizeof(Data), &ret))255{256Hr = StringCbPrintf(Buffer, *BufferSize, " { %lx`%lx }", (ULONG) (Data >> 32), (ULONG) Data);257} else258{259Hr = E_INVALIDARG;260}261} else if (!strcmp(StructName, KnownStructs[1]))262{263SYSTEMTIME Data;264ULONG ret;265266if (ReadMemory(Address, &Data, sizeof(Data), &ret))267{268Hr = StringCbPrintf(Buffer, *BufferSize, " { %02ld:%02ld:%02ld %02ld/%02ld/%04ld }",269Data.wHour,270Data.wMinute,271Data.wSecond,272Data.wMonth,273Data.wDay,274Data.wYear);275} else276{277Hr = E_INVALIDARG;278}279} else280{281Hr = E_INVALIDARG;282}283} else if (Flag == DEBUG_KNOWN_STRUCT_SUPPRESS_TYPE_NAME)284{285if (!strcmp(StructName, KnownStructs[0]))286{287// Do not print type name for KnownStructs[0]288Hr = S_OK;289}290else291{292// Print the type name293Hr = S_FALSE;294}295} else296{297Hr = E_INVALIDARG;298}299return Hr;300}301302extern "C"303HRESULT304_EFN_Analyze(305__in PDEBUG_CLIENT4 Client,306__in FA_EXTENSION_PLUGIN_PHASE CallPhase,307__in PDEBUG_FAILURE_ANALYSIS2 pAnalysis308)309{310INIT_API();311312// Analysis tags313#define FA_TAG_SAMPLE_PLUGIN_DEBUG_TEXT 0xA0000000314315ExtOut("DbgExts Analysis Phase: %lx\n", CallPhase);316switch (CallPhase)317{318case FA_PLUGIN_STACK_ANALYSIS:319pAnalysis->SetString((FA_TAG) FA_TAG_SAMPLE_PLUGIN_DEBUG_TEXT,320"Sample custom analyzer was run for this analysis.\n");321break;322case FA_PLUGIN_POST_BUCKETING:323PFA_ENTRY Entry;324325//326// Set default bucket if folowup module in dbgeng327//328if ((Entry = pAnalysis->Get(DEBUG_FLR_MODULE_NAME)) != NULL &&329!strcmp(FA_ENTRY_DATA(PSTR, Entry), "dbgeng"))330{331pAnalysis->SetString(DEBUG_FLR_DEFAULT_BUCKET_ID, "AV_IN_DEBUGGER");332}333break;334default:335// do nothing336EXIT_API();337return S_OK;338}339UNREFERENCED_PARAMETER(pAnalysis);340341EXIT_API();342return S_OK;343}344345346347