// Copyright 2020 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "tools/v8windbg/base/dbgext.h" #include #include #include "tools/v8windbg/base/utilities.h" // See // https://docs.microsoft.com/en-us/visualstudio/debugger/crt-debugging-techniques // for the memory leak and debugger reporting macros used from _CrtMemState mem_old, mem_new, mem_diff; int original_crt_dbg_flag = 0; WRL::ComPtr sp_data_model_manager; WRL::ComPtr sp_debug_host; WRL::ComPtr sp_debug_control; WRL::ComPtr sp_debug_host_memory; WRL::ComPtr sp_debug_host_symbols; WRL::ComPtr sp_debug_host_extensibility; extern "C" { HRESULT __stdcall DebugExtensionInitialize(PULONG /*pVersion*/, PULONG /*pFlags*/) { original_crt_dbg_flag = _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF); _CrtMemCheckpoint(&mem_old); WRL::ComPtr sp_debug_client; WRL::ComPtr sp_data_model_access; RETURN_IF_FAIL(DebugCreate(__uuidof(IDebugClient), &sp_debug_client)); RETURN_IF_FAIL(sp_debug_client.As(&sp_data_model_access)); RETURN_IF_FAIL(sp_debug_client.As(&sp_debug_control)); RETURN_IF_FAIL(sp_data_model_access->GetDataModel(&sp_data_model_manager, &sp_debug_host)); RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_memory)); RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_symbols)); RETURN_IF_FAIL(sp_debug_host.As(&sp_debug_host_extensibility)); return CreateExtension(); } void __stdcall DebugExtensionUninitialize() { DestroyExtension(); sp_debug_host = nullptr; sp_data_model_manager = nullptr; sp_debug_host_memory = nullptr; sp_debug_host_symbols = nullptr; sp_debug_host_extensibility = nullptr; _CrtMemCheckpoint(&mem_new); if (_CrtMemDifference(&mem_diff, &mem_old, &mem_new)) { _CrtMemDumpStatistics(&mem_diff); } _CrtSetDbgFlag(original_crt_dbg_flag); } HRESULT __stdcall DebugExtensionCanUnload(void) { if (!WRL::Module::GetModule().Terminate()) { _RPTF0(_CRT_WARN, "Failed to unload WRL\n"); return S_FALSE; } return S_OK; } void __stdcall DebugExtensionUnload() { return; } } // extern "C"