1 // Copyright 2020 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "tools/v8windbg/base/dbgext.h" 6 7 #include <crtdbg.h> 8 #include <wrl/module.h> 9 10 #include "tools/v8windbg/base/utilities.h" 11 12 // See 13 // https://docs.microsoft.com/en-us/visualstudio/debugger/crt-debugging-techniques 14 // for the memory leak and debugger reporting macros used from <crtdbg.h> 15 _CrtMemState mem_old, mem_new, mem_diff; 16 int original_crt_dbg_flag = 0; 17 18 WRL::ComPtr<IDataModelManager> sp_data_model_manager; 19 WRL::ComPtr<IDebugHost> sp_debug_host; 20 WRL::ComPtr<IDebugControl5> sp_debug_control; 21 WRL::ComPtr<IDebugHostMemory2> sp_debug_host_memory; 22 WRL::ComPtr<IDebugHostSymbols> sp_debug_host_symbols; 23 WRL::ComPtr<IDebugHostExtensibility> sp_debug_host_extensibility; 24 25 extern "C" { 26 27 HRESULT DebugExtensionInitialize(PULONG,PULONG)28__stdcall DebugExtensionInitialize(PULONG /*pVersion*/, PULONG /*pFlags*/) { 29 original_crt_dbg_flag = _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF); 30 _CrtMemCheckpoint(&mem_old); 31 32 WRL::ComPtr<IDebugClient> sp_debug_client; 33 WRL::ComPtr<IHostDataModelAccess> sp_data_model_access; 34 35 RETURN_IF_FAIL(DebugCreate(__uuidof(IDebugClient), &sp_debug_client)); 36 37 RETURN_IF_FAIL(sp_debug_client.As(&sp_data_model_access)); 38 RETURN_IF_FAIL(sp_debug_client.As(&sp_debug_control)); 39 40 RETURN_IF_FAIL(sp_data_model_access->GetDataModel(&sp_data_model_manager, 41 &sp_debug_host)); 42 43 RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_memory)); 44 RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_symbols)); 45 RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_extensibility)); 46 47 return CreateExtension(); 48 } 49 DebugExtensionUninitialize()50void __stdcall DebugExtensionUninitialize() { 51 DestroyExtension(); 52 sp_debug_host = nullptr; 53 sp_data_model_manager = nullptr; 54 sp_debug_host_memory = nullptr; 55 sp_debug_host_symbols = nullptr; 56 sp_debug_host_extensibility = nullptr; 57 58 _CrtMemCheckpoint(&mem_new); 59 if (_CrtMemDifference(&mem_diff, &mem_old, &mem_new)) { 60 _CrtMemDumpStatistics(&mem_diff); 61 } 62 _CrtSetDbgFlag(original_crt_dbg_flag); 63 } 64 DebugExtensionCanUnload(void)65HRESULT __stdcall DebugExtensionCanUnload(void) { 66 if (!WRL::Module<WRL::InProc>::GetModule().Terminate()) { 67 _RPTF0(_CRT_WARN, "Failed to unload WRL\n"); 68 return S_FALSE; 69 } 70 return S_OK; 71 } 72 DebugExtensionUnload()73void __stdcall DebugExtensionUnload() { return; } 74 75 } // extern "C" 76