Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization failures logging #75097

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/coreclr/binder/assemblybindercommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,11 @@ HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pMa
/* in */ AssemblyBinder *pBinder,
/* out */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;

_ASSERTE(pManagedAssemblyLoadContextToBindWithin != NULL);

// RuntimeInvokeHostAssemblyResolver will perform steps 2-4 of CustomAssemblyBinder::BindAssemblyByName.
BINDER_SPACE::Assembly *pLoadedAssembly = NULL;
hr = RuntimeInvokeHostAssemblyResolver(pManagedAssemblyLoadContextToBindWithin,
HRESULT hr = RuntimeInvokeHostAssemblyResolver(pManagedAssemblyLoadContextToBindWithin,
pAssemblyName, pDefaultBinder, pBinder, &pLoadedAssembly);
if (SUCCEEDED(hr))
{
Expand All @@ -1183,7 +1181,7 @@ HRESULT AssemblyBinderCommon::BindUsingPEImage(/* in */ AssemblyBinder* pBinder
/* in */ bool excludeAppPaths,
/* [retval] [out] */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;
HRESULT hr;

LONG kContextVersion = 0;
BindResult bindResult;
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/binder/customassemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ HRESULT CustomAssemblyBinder::SetupContext(DefaultAssemblyBinder *pDefaultBinder
UINT_PTR ptrAssemblyLoadContext,
CustomAssemblyBinder **ppBindContext)
{
HRESULT hr = E_FAIL;
HRESULT hr;
EX_TRY
{
if(ppBindContext != NULL)
Expand Down Expand Up @@ -198,6 +198,10 @@ HRESULT CustomAssemblyBinder::SetupContext(DefaultAssemblyBinder *pDefaultBinder
*ppBindContext = pBinder.Extract();
}
}
else
{
hr = LogHR(E_FAIL);
}
}
EX_CATCH_HRESULT(hr);

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/binder/inc/bindertypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct AssemblyNameData

#define IF_FALSE_GO(expr) \
if (!(expr)) { \
hr = E_FAIL; \
hr = LogHR(E_FAIL); \
goto Exit; \
}

Expand Down
43 changes: 40 additions & 3 deletions src/coreclr/dlls/mscoree/exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif // FEATURE_GDBJIT
#include "bundle.h"
#include "pinvokeoverride.h"
#include "minipal.h"

#define ASSERTE_ALL_BUILDS(expr) _ASSERTE_ALL_BUILDS((expr))

Expand Down Expand Up @@ -158,11 +159,30 @@ static void ConvertConfigPropertiesToUnicode(
*propertyValuesWRef = propertyValuesW;
}

coreclr_error_writer_callback_fn g_errorWriter = nullptr;

//
// Set callback for writing error logging
//
// Parameters:
// errorWriter - callback that will be called for each line of the error info
// - passing in NULL removes a callback that was previously set
//
// Returns:
// S_OK
//
extern "C"
DLLEXPORT
int coreclr_set_error_writer(coreclr_error_writer_callback_fn error_writer)
{
g_errorWriter = error_writer;
return S_OK;
}

#ifdef FEATURE_GDBJIT
GetInfoForMethodDelegate getInfoForMethodDelegate = NULL;
extern "C" int coreclr_create_delegate(void*, unsigned int, const char*, const char*, const char*, void**);
#endif //FEATURE_GDBJIT

//
// Initialize the CoreCLR. Creates and starts CoreCLR host and creates an app domain
//
Expand Down Expand Up @@ -246,10 +266,10 @@ int coreclr_initialize(
InitializeStartupFlags(&startupFlags);

hr = host->SetStartupFlags(startupFlags);
IfFailRet(hr);
IfFailGo(hr);

hr = host->Start();
IfFailRet(hr);
IfFailGo(hr);

hr = host->CreateAppDomainWithManager(
appDomainFriendlyNameW,
Expand Down Expand Up @@ -285,6 +305,23 @@ int coreclr_initialize(

#endif
}

ErrExit:
if (FAILED(hr) && g_errorWriter)
{
const char hresultFormatString[] = "HRESULT 0x%08x at ";
char line[cchMaxAssertStackLevelStringLen + sizeof(hresultFormatString)];
int count = GetFailedHRLogEntryCount();;
for (int i = 0; i < count; i++)
{
FailedHRLogEntry* pEntry = GetFailedHRLogEntry(i);
int length = sprintf_s(line, sizeof(line), hresultFormatString, pEntry->hr);
VMToOSInterface::GetSymbolFromAddress(pEntry->address, line + length, sizeof(line) - length);
g_errorWriter(line);
}

}

return hr;
}

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/dlls/mscoree/mscorwks_ntdef.src
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ EXPORTS
; Unix hosting API
coreclr_create_delegate
coreclr_execute_assembly
coreclr_set_error_writer
coreclr_initialize
coreclr_shutdown
coreclr_shutdown_2
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/dlls/mscoree/mscorwks_unixexports.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; Unix hosting API
coreclr_create_delegate
coreclr_execute_assembly
coreclr_set_error_writer
coreclr_initialize
coreclr_shutdown
coreclr_shutdown_2
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/dlls/mscorpe/ceefilegenwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ HRESULT CeeFileGenWriter::computeOffset(_In_ char *ptr,
s++;
}

return E_FAIL;
return LogHR(E_FAIL);
} // HRESULT CeeFileGenWriter::computeOffset()

HRESULT CeeFileGenWriter::getCorHeader(IMAGE_COR20_HEADER **ppHeader)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/gc/env/gcenv.ee.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class GCToEEInterface
static uint32_t GetCurrentProcessCpuCount();

static void DiagAddNewRegion(int generation, uint8_t* rangeStart, uint8_t* rangeEnd, uint8_t* rangeEndReserved);

static uint32_t LogHR(uint32_t hr, void* address);
static uint32_t LogHR(uint32_t hr);
};

#endif // __GCENV_EE_H__
24 changes: 12 additions & 12 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13333,23 +13333,23 @@ HRESULT gc_heap::initialize_gc (size_t soh_segment_size,
gc_log = CreateLogFile(GCConfig::GetLogFile(), false);

if (gc_log == NULL)
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);

// GCLogFileSize in MBs.
gc_log_file_size = static_cast<size_t>(GCConfig::GetLogFileSize());

if (gc_log_file_size <= 0 || gc_log_file_size > 500)
{
fclose (gc_log);
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}

gc_log_lock.Initialize();
gc_log_buffer = new (nothrow) uint8_t [gc_log_buffer_size];
if (!gc_log_buffer)
{
fclose(gc_log);
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}

memset (gc_log_buffer, '*', gc_log_buffer_size);
Expand All @@ -13364,13 +13364,13 @@ HRESULT gc_heap::initialize_gc (size_t soh_segment_size,
gc_config_log = CreateLogFile(GCConfig::GetConfigLogFile(), true);

if (gc_config_log == NULL)
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);

gc_config_log_buffer = new (nothrow) uint8_t [gc_config_log_buffer_size];
if (!gc_config_log_buffer)
{
fclose(gc_config_log);
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}

compact_ratio = static_cast<int>(GCConfig::GetCompactRatio());
Expand Down Expand Up @@ -13477,7 +13477,7 @@ HRESULT gc_heap::initialize_gc (size_t soh_segment_size,
else
{
assert (!"cannot use regions without specifying the range!!!");
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}
#else //USE_REGIONS
bool separated_poh_p = use_large_pages_p &&
Expand Down Expand Up @@ -13590,7 +13590,7 @@ HRESULT gc_heap::initialize_gc (size_t soh_segment_size,

if (!init_semi_shared())
{
hres = E_FAIL;
hres = GCToEEInterface::LogHR(E_FAIL);
}

return hres;
Expand Down Expand Up @@ -45008,7 +45008,7 @@ HRESULT GCHeap::Initialize()

if (!WaitForGCEvent->CreateManualEventNoThrow(TRUE))
{
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}

#ifndef FEATURE_NATIVEAOT // Redhawk forces relocation a different way
Expand Down Expand Up @@ -45089,9 +45089,9 @@ HRESULT GCHeap::Initialize()
int hb_info_size_per_node = hb_info_size_per_proc * procs_per_numa_node;
uint8_t* numa_mem = (uint8_t*)GCToOSInterface::VirtualReserve (hb_info_size_per_node, 0, 0, numa_node_index);
if (!numa_mem)
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
if (!GCToOSInterface::VirtualCommit (numa_mem, hb_info_size_per_node, numa_node_index))
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);

heap_balance_info_proc* hb_info_procs = (heap_balance_info_proc*)numa_mem;
hb_info_numa_nodes[numa_node_index].hb_info_procs = hb_info_procs;
Expand Down Expand Up @@ -47462,7 +47462,7 @@ size_t GCHeap::ApproxFreeBytes()
HRESULT GCHeap::GetGcCounters(int gen, gc_counters* counters)
{
if ((gen < 0) || (gen > max_generation))
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
#ifdef MULTIPLE_HEAPS
counters->current_size = 0;
counters->promoted_size = 0;
Expand Down Expand Up @@ -48591,7 +48591,7 @@ HRESULT GCHeap::WaitUntilConcurrentGCCompleteAsync(int millisecondsTimeout)
else if (dwRet == WAIT_TIMEOUT)
return HRESULT_FROM_WIN32(ERROR_TIMEOUT);
else
return E_FAIL; // It is not clear if what the last error would be if the wait failed,
return GCToEEInterface::LogHR(E_FAIL); // It is not clear if what the last error would be if the wait failed,
// as there are too many layers in between. The best we can do is to return E_FAIL;
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/gc/gcenv.ee.standalone.inl
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,16 @@ inline void GCToEEInterface::DiagAddNewRegion(int generation, uint8_t* rangeStar
g_theGCToCLR->DiagAddNewRegion(generation, rangeStart, rangeEnd, rangeEndReserved);
}

inline uint32_t GCToEEInterface::LogHR(uint32_t hr, void* address)
{
return g_theGCToCLR->LogHR(hr, address);
}

// This function is marked as NOINLINE so that it can get the caller's address
NOINLINE
inline uint32_t GCToEEInterface::LogHR(uint32_t hr)
{
return LogHR(hr, nullptr);
}

#endif // __GCTOENV_EE_STANDALONE_INL__
3 changes: 3 additions & 0 deletions src/coreclr/gc/gcinterface.ee.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ class IGCToCLR {

virtual
void DiagAddNewRegion(int generation, uint8_t* rangeStart, uint8_t* rangeEnd, uint8_t* rangeEndReserved) = 0;

virtual
uint32_t LogHR(uint32_t hr, void* address) = 0;
};

#endif // _GCINTERFACE_EE_H_
3 changes: 2 additions & 1 deletion src/coreclr/gc/gcload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace WKS
{
extern void PopulateDacVars(GcDacVars* dacVars);
uint32_t LogHR(uint32_t hr);
}

namespace SVR
Expand Down Expand Up @@ -81,7 +82,7 @@ GC_Initialize(
#ifndef FEATURE_NATIVEAOT // GCToOSInterface is initialized directly
if (!GCToOSInterface::Initialize())
{
return E_FAIL;
return GCToEEInterface::LogHR(E_FAIL);
}
#endif

Expand Down
16 changes: 16 additions & 0 deletions src/coreclr/gc/sample/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "common.h"

#include "windows.h"
#include <intrin.h>

#include "gcenv.h"
#include "gc.h"
Expand Down Expand Up @@ -358,3 +359,18 @@ uint32_t GCToEEInterface::GetCurrentProcessCpuCount()
void GCToEEInterface::DiagAddNewRegion(int generation, uint8_t* rangeStart, uint8_t* rangeEnd, uint8_t* rangeEndReserved)
{
}

uint32_t GCToEEInterface::LogHR(uint32_t hr, void* address)
{
return hr;
}

#pragma intrinsic(_ReturnAddress)

// This function is marked as NOINLINE so that it can get the caller's address
NOINLINE
uint32_t GCToEEInterface::LogHR(uint32_t hr)
{
return LogHR(hr, _ReturnAddress());
}

Loading