microsoft/mu_tiano_platforms
Publicmirrored fromhttps://github.com/microsoft/mu_tiano_platformsAvailable
UefiDbgExt/dbgexts.cpp
353lines · modecode
| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft Corporation |
| 4 | |
| 5 | SPDX-License-Identifier: BSD-2-Clause-Patent |
| 6 | |
| 7 | Module Name: |
| 8 | |
| 9 | dbgexts.cpp |
| 10 | |
| 11 | Abstract: |
| 12 | |
| 13 | This file contains the generic routines and initialization code |
| 14 | for the debugger extensions dll. |
| 15 | |
| 16 | --*/ |
| 17 | |
| 18 | #include "dbgexts.h" |
| 19 | #include <strsafe.h> |
| 20 | |
| 21 | PDEBUG_CLIENT4 g_ExtClient; |
| 22 | PDEBUG_CONTROL g_ExtControl; |
| 23 | PDEBUG_SYMBOLS2 g_ExtSymbols; |
| 24 | |
| 25 | WINDBG_EXTENSION_APIS ExtensionApis; |
| 26 | |
| 27 | ULONG TargetMachine; |
| 28 | BOOL Connected; |
| 29 | |
| 30 | // Queries for all debugger interfaces. |
| 31 | extern "C" HRESULT |
| 32 | ExtQuery(PDEBUG_CLIENT4 Client) |
| 33 | { |
| 34 | HRESULT Status; |
| 35 | |
| 36 | if ((Status = Client->QueryInterface(__uuidof(IDebugControl), |
| 37 | (void **)&g_ExtControl)) != S_OK) |
| 38 | { |
| 39 | goto Fail; |
| 40 | } |
| 41 | if ((Status = Client->QueryInterface(__uuidof(IDebugSymbols2), |
| 42 | (void **)&g_ExtSymbols)) != S_OK) |
| 43 | { |
| 44 | goto Fail; |
| 45 | } |
| 46 | g_ExtClient = Client; |
| 47 | |
| 48 | return S_OK; |
| 49 | |
| 50 | Fail: |
| 51 | ExtRelease(); |
| 52 | return Status; |
| 53 | } |
| 54 | |
| 55 | // Cleans up all debugger interfaces. |
| 56 | void |
| 57 | ExtRelease(void) |
| 58 | { |
| 59 | g_ExtClient = NULL; |
| 60 | EXT_RELEASE(g_ExtControl); |
| 61 | EXT_RELEASE(g_ExtSymbols); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // Normal output. |
| 66 | void __cdecl |
| 67 | ExtOut(PCSTR Format, ...) |
| 68 | { |
| 69 | va_list Args; |
| 70 | |
| 71 | va_start(Args, Format); |
| 72 | g_ExtControl->OutputVaList(DEBUG_OUTPUT_NORMAL, Format, Args); |
| 73 | va_end(Args); |
| 74 | } |
| 75 | |
| 76 | // Error output. |
| 77 | void __cdecl |
| 78 | ExtErr(PCSTR Format, ...) |
| 79 | { |
| 80 | va_list Args; |
| 81 | |
| 82 | va_start(Args, Format); |
| 83 | g_ExtControl->OutputVaList(DEBUG_OUTPUT_ERROR, Format, Args); |
| 84 | va_end(Args); |
| 85 | } |
| 86 | |
| 87 | // Warning output. |
| 88 | void __cdecl |
| 89 | ExtWarn(PCSTR Format, ...) |
| 90 | { |
| 91 | va_list Args; |
| 92 | |
| 93 | va_start(Args, Format); |
| 94 | g_ExtControl->OutputVaList(DEBUG_OUTPUT_WARNING, Format, Args); |
| 95 | va_end(Args); |
| 96 | } |
| 97 | |
| 98 | extern "C" |
| 99 | HRESULT |
| 100 | CALLBACK |
| 101 | DebugExtensionInitialize(PULONG Version, PULONG Flags) |
| 102 | { |
| 103 | IDebugClient *DebugClient; |
| 104 | PDEBUG_CONTROL DebugControl; |
| 105 | HRESULT Hr; |
| 106 | |
| 107 | *Version = DEBUG_EXTENSION_VERSION(1, 0); |
| 108 | *Flags = 0; |
| 109 | Hr = S_OK; |
| 110 | |
| 111 | if ((Hr = DebugCreate(__uuidof(IDebugClient), |
| 112 | (void **)&DebugClient)) != S_OK) |
| 113 | { |
| 114 | return Hr; |
| 115 | } |
| 116 | |
| 117 | if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl), |
| 118 | (void **)&DebugControl)) == S_OK) |
| 119 | { |
| 120 | |
| 121 | // |
| 122 | // Get the windbg-style extension APIS |
| 123 | // |
| 124 | ExtensionApis.nSize = sizeof (ExtensionApis); |
| 125 | Hr = DebugControl->GetWindbgExtensionApis64(&ExtensionApis); |
| 126 | |
| 127 | DebugControl->Release(); |
| 128 | |
| 129 | } |
| 130 | DebugClient->Release(); |
| 131 | return Hr; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | extern "C" |
| 136 | void |
| 137 | CALLBACK |
| 138 | DebugExtensionNotify(ULONG Notify, ULONG64 Argument) |
| 139 | { |
| 140 | UNREFERENCED_PARAMETER(Argument); |
| 141 | |
| 142 | // |
| 143 | // The first time we actually connect to a target |
| 144 | // |
| 145 | |
| 146 | if ((Notify == DEBUG_NOTIFY_SESSION_ACCESSIBLE) && (!Connected)) |
| 147 | { |
| 148 | IDebugClient *DebugClient; |
| 149 | HRESULT Hr; |
| 150 | PDEBUG_CONTROL DebugControl; |
| 151 | |
| 152 | if ((Hr = DebugCreate(__uuidof(IDebugClient), |
| 153 | (void **)&DebugClient)) == S_OK) |
| 154 | { |
| 155 | // |
| 156 | // Get the architecture type. |
| 157 | // |
| 158 | |
| 159 | if ((Hr = DebugClient->QueryInterface(__uuidof(IDebugControl), |
| 160 | (void **)&DebugControl)) == S_OK) |
| 161 | { |
| 162 | if ((Hr = DebugControl->GetActualProcessorType( |
| 163 | &TargetMachine)) == S_OK) |
| 164 | { |
| 165 | Connected = TRUE; |
| 166 | } |
| 167 | |
| 168 | NotifyOnTargetAccessible(DebugControl); |
| 169 | |
| 170 | DebugControl->Release(); |
| 171 | } |
| 172 | |
| 173 | DebugClient->Release(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | if (Notify == DEBUG_NOTIFY_SESSION_INACTIVE) |
| 179 | { |
| 180 | Connected = FALSE; |
| 181 | TargetMachine = 0; |
| 182 | } |
| 183 | |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | extern "C" |
| 188 | void |
| 189 | CALLBACK |
| 190 | DebugExtensionUninitialize(void) |
| 191 | { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | extern "C" |
| 196 | HRESULT |
| 197 | CALLBACK |
| 198 | KnownStructOutput( |
| 199 | _In_ ULONG Flag, |
| 200 | _In_ ULONG64 Address, |
| 201 | _In_ PSTR StructName, |
| 202 | _Out_writes_opt_(*BufferSize) PSTR Buffer, |
| 203 | _Inout_opt_ _When_(Flag == DEBUG_KNOWN_STRUCT_GET_NAMES, _Notnull_) _When_(Flag == DEBUG_KNOWN_STRUCT_GET_SINGLE_LINE_OUTPUT, _Notnull_) PULONG BufferSize |
| 204 | ) |
| 205 | { |
| 206 | const char* KnownStructs[] = {"_LARGE_INTEGER", "_SYSTEMTIME", NULL}; |
| 207 | HRESULT Hr = S_OK; |
| 208 | |
| 209 | if ((Flag == DEBUG_KNOWN_STRUCT_GET_NAMES) && (Buffer != NULL) && (BufferSize != NULL)) |
| 210 | { |
| 211 | // |
| 212 | // Return names of known structs in multi string |
| 213 | // |
| 214 | ULONG SizeRemaining = *BufferSize, SizeNeeded = 0, Length; |
| 215 | PCHAR CopyAt = Buffer; |
| 216 | |
| 217 | for (ULONG i=0; KnownStructs[i] != NULL; i++) |
| 218 | { |
| 219 | if (SizeRemaining > (Length = (ULONG)strlen(KnownStructs[i]) + 1) && |
| 220 | Hr == S_OK) |
| 221 | { |
| 222 | Hr = StringCbCopy(CopyAt, SizeRemaining, KnownStructs[i]); |
| 223 | |
| 224 | SizeRemaining -= Length; |
| 225 | CopyAt += Length; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | Hr = S_FALSE; |
| 230 | } |
| 231 | SizeNeeded += Length; |
| 232 | } |
| 233 | if (SizeRemaining > 0 && Hr == S_OK) |
| 234 | { |
| 235 | // Terminate multistring and return size copied |
| 236 | *CopyAt = 0; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | Hr = S_FALSE; |
| 241 | } |
| 242 | // return size copied |
| 243 | *BufferSize = SizeNeeded+1; |
| 244 | } |
| 245 | else if ((Flag == DEBUG_KNOWN_STRUCT_GET_SINGLE_LINE_OUTPUT) && (Buffer != NULL) && (BufferSize != NULL)) |
| 246 | { |
| 247 | if (!strcmp(StructName, KnownStructs[0])) |
| 248 | { |
| 249 | ULONG64 Data; |
| 250 | ULONG ret; |
| 251 | |
| 252 | if (ReadMemory(Address, &Data, sizeof(Data), &ret)) |
| 253 | { |
| 254 | Hr = StringCbPrintf(Buffer, *BufferSize, " { %lx`%lx }", (ULONG) (Data >> 32), (ULONG) Data); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | Hr = E_INVALIDARG; |
| 259 | } |
| 260 | } |
| 261 | else if (!strcmp(StructName, KnownStructs[1])) |
| 262 | { |
| 263 | SYSTEMTIME Data; |
| 264 | ULONG ret; |
| 265 | |
| 266 | if (ReadMemory(Address, &Data, sizeof(Data), &ret)) |
| 267 | { |
| 268 | Hr = StringCbPrintf(Buffer, *BufferSize, " { %02lu:%02lu:%02lu %02lu/%02lu/%04lu }", |
| 269 | Data.wHour, |
| 270 | Data.wMinute, |
| 271 | Data.wSecond, |
| 272 | Data.wMonth, |
| 273 | Data.wDay, |
| 274 | Data.wYear); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | Hr = E_INVALIDARG; |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | Hr = E_INVALIDARG; |
| 284 | } |
| 285 | } |
| 286 | else if (Flag == DEBUG_KNOWN_STRUCT_SUPPRESS_TYPE_NAME) |
| 287 | { |
| 288 | if (!strcmp(StructName, KnownStructs[0])) |
| 289 | { |
| 290 | // Do not print type name for KnownStructs[0] |
| 291 | Hr = S_OK; |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | // Print the type name |
| 296 | Hr = S_FALSE; |
| 297 | } |
| 298 | |
| 299 | if (Buffer) |
| 300 | { |
| 301 | Buffer[0] = 0; |
| 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | Hr = E_INVALIDARG; |
| 307 | } |
| 308 | return Hr; |
| 309 | } |
| 310 | |
| 311 | extern "C" |
| 312 | HRESULT |
| 313 | _EFN_Analyze( |
| 314 | _In_ PDEBUG_CLIENT4 Client, |
| 315 | _In_ FA_EXTENSION_PLUGIN_PHASE CallPhase, |
| 316 | _In_ PDEBUG_FAILURE_ANALYSIS2 pAnalysis |
| 317 | ) |
| 318 | { |
| 319 | INIT_API(); |
| 320 | |
| 321 | // Analysis tags |
| 322 | #define FA_TAG_SAMPLE_PLUGIN_DEBUG_TEXT 0xA0000000 |
| 323 | |
| 324 | ExtOut("DbgExts Analysis Phase: %lx\n", CallPhase); |
| 325 | switch (CallPhase) |
| 326 | { |
| 327 | case FA_PLUGIN_STACK_ANALYSIS: |
| 328 | pAnalysis->SetString((FA_TAG) FA_TAG_SAMPLE_PLUGIN_DEBUG_TEXT, |
| 329 | "Sample custom analyzer was run for this analysis.\n"); |
| 330 | break; |
| 331 | case FA_PLUGIN_POST_BUCKETING: |
| 332 | PFA_ENTRY Entry; |
| 333 | |
| 334 | // |
| 335 | // Set default bucket if folowup module in dbgeng |
| 336 | // |
| 337 | if ((Entry = pAnalysis->Get(DEBUG_FLR_MODULE_NAME)) != NULL && |
| 338 | !strcmp(FA_ENTRY_DATA(PSTR, Entry), "dbgeng")) |
| 339 | { |
| 340 | pAnalysis->SetString(DEBUG_FLR_DEFAULT_BUCKET_ID, "AV_IN_DEBUGGER"); |
| 341 | } |
| 342 | break; |
| 343 | default: |
| 344 | // do nothing |
| 345 | EXIT_API(); |
| 346 | return S_OK; |
| 347 | } |
| 348 | UNREFERENCED_PARAMETER(pAnalysis); |
| 349 | |
| 350 | EXIT_API(); |
| 351 | return S_OK; |
| 352 | } |
| 353 | |
| 354 | |