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