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

[NativeAOT] Handle event pipe environment variable configuration and output to file #86656

Merged
merged 7 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 0 additions & 14 deletions src/coreclr/gc/env/gcenv.structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ typedef void * HANDLE;

#ifdef TARGET_UNIX

typedef char TCHAR;
#define _T(s) s

#else

#ifndef _INC_WINDOWS
typedef wchar_t TCHAR;
#define _T(s) L##s
#endif

#endif

#ifdef TARGET_UNIX

class EEThreadId
{
pthread_t m_id;
Expand Down
30 changes: 16 additions & 14 deletions src/coreclr/nativeaot/Runtime/PalRedhawk.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

#include <sal.h>
#include <stdarg.h>
#include "gcenv.structs.h"
#include "gcenv.structs.h" // CRITICAL_SECTION
#include "IntrinsicConstants.h"
#include "PalRedhawkCommon.h"

#ifndef PAL_REDHAWK_INCLUDED
#define PAL_REDHAWK_INCLUDED
Expand Down Expand Up @@ -50,7 +51,6 @@
#endif // !_MSC_VER

#ifndef _INC_WINDOWS
//#ifndef DACCESS_COMPILE

// There are some fairly primitive type definitions below but don't pull them into the rest of Redhawk unless
// we have to (in which case these definitions will move to CommonTypes.h).
Expand All @@ -65,14 +65,13 @@ typedef void * LPOVERLAPPED;

#ifdef TARGET_UNIX
#define __stdcall
typedef char TCHAR;
#define _T(s) s
#else
typedef wchar_t TCHAR;
#define _T(s) L##s
#endif

#ifndef __GCENV_BASE_INCLUDED__
#define CALLBACK __stdcall
#define WINAPI __stdcall
#define WINBASEAPI __declspec(dllimport)
#endif //!__GCENV_BASE_INCLUDED__

#ifdef TARGET_UNIX
#define DIRECTORY_SEPARATOR_CHAR '/'
#else // TARGET_UNIX
Expand Down Expand Up @@ -101,9 +100,6 @@ typedef struct _GUID {

#define DECLARE_HANDLE(_name) typedef HANDLE _name

// defined in gcrhenv.cpp
bool __SwitchToThread(uint32_t dwSleepMSec, uint32_t dwSwitchCount);

struct FILETIME
{
uint32_t dwLowDateTime;
Expand Down Expand Up @@ -502,18 +498,19 @@ typedef enum _EXCEPTION_DISPOSITION {
#define NULL_AREA_SIZE (64*1024)
#endif

//#endif // !DACCESS_COMPILE
#endif // !_INC_WINDOWS



#ifndef DACCESS_COMPILE
#ifndef _INC_WINDOWS

#ifndef __GCENV_BASE_INCLUDED__
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif // !__GCENV_BASE_INCLUDED__
#endif

#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)

Expand Down Expand Up @@ -750,6 +747,9 @@ REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalRegisterHijackCallback(_In_ PalH

#ifdef FEATURE_ETW
REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalEventEnabled(REGHANDLE regHandle, _In_ const EVENT_DESCRIPTOR* eventDescriptor);
REDHAWK_PALIMPORT uint32_t REDHAWK_PALAPI PalEventRegister(const GUID * arg1, void * arg2, void * arg3, REGHANDLE * arg4);
REDHAWK_PALIMPORT uint32_t REDHAWK_PALAPI PalEventUnregister(REGHANDLE arg1);
REDHAWK_PALIMPORT uint32_t REDHAWK_PALAPI PalEventWrite(REGHANDLE arg1, const EVENT_DESCRIPTOR * arg2, uint32_t arg3, EVENT_DATA_DESCRIPTOR * arg4);
#endif

REDHAWK_PALIMPORT _Ret_maybenull_ void* REDHAWK_PALAPI PalSetWerDataBuffer(_In_ void* pNewBuffer);
Expand All @@ -776,6 +776,8 @@ REDHAWK_PALIMPORT uint64_t PalQueryPerformanceFrequency();

REDHAWK_PALIMPORT void PalPrintFatalError(const char* message);

REDHAWK_PALIMPORT char* PalCopyTCharAsChar(const TCHAR* toCopy);

#ifdef TARGET_UNIX
REDHAWK_PALIMPORT int32_t __cdecl _stricmp(const char *string1, const char *string2);
#endif // TARGET_UNIX
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/nativeaot/Runtime/PalRedhawkFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ inline void PalEnterCriticalSection(CRITICAL_SECTION * arg1)
EnterCriticalSection(arg1);
}

extern "C" uint32_t __stdcall EventRegister(const GUID *, void *, void *, REGHANDLE *);
inline uint32_t PalEventRegister(const GUID * arg1, void * arg2, void * arg3, REGHANDLE * arg4)
{
return EventRegister(arg1, arg2, arg3, arg4);
}

extern "C" uint32_t __stdcall EventUnregister(REGHANDLE);
inline uint32_t PalEventUnregister(REGHANDLE arg1)
{
return EventUnregister(arg1);
}

extern "C" uint32_t __stdcall EventWrite(REGHANDLE, const EVENT_DESCRIPTOR *, uint32_t, EVENT_DATA_DESCRIPTOR *);
inline uint32_t PalEventWrite(REGHANDLE arg1, const EVENT_DESCRIPTOR * arg2, uint32_t arg3, EVENT_DATA_DESCRIPTOR * arg4)
{
return EventWrite(arg1, arg2, arg3, arg4);
}

extern "C" void __stdcall FlushProcessWriteBuffers();
inline void PalFlushProcessWriteBuffers()
{
Expand Down
140 changes: 100 additions & 40 deletions src/coreclr/nativeaot/Runtime/RhConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,120 @@

#include <string.h>

bool RhConfig::ReadConfigValue(_In_z_ const char *name, uint64_t* pValue, bool decimal)
#define DOTNET_PREFIX _T("DOTNET_")
#define DOTNET_PREFIX_LEN STRING_LENGTH(DOTNET_PREFIX)

namespace
{
void GetEnvironmentConfigName(const char* name, TCHAR* buffer, uint32_t bufferSize)
{
assert(DOTNET_PREFIX_LEN + strlen(name) < bufferSize);
memcpy(buffer, DOTNET_PREFIX, (DOTNET_PREFIX_LEN) * sizeof(TCHAR));
#ifdef TARGET_WINDOWS
size_t nameLen = strlen(name);
for (size_t i = 0; i < nameLen; i++)
{
buffer[DOTNET_PREFIX_LEN + i] = name[i];
}
buffer[DOTNET_PREFIX_LEN + nameLen] = '\0';
#else
strcat(buffer, name);
#endif
}
}

bool RhConfig::Environment::TryGetBooleanValue(const char* name, bool* value)
{
uint64_t intValue;
if (!TryGetIntegerValue(name, &intValue))
return false;

*value = intValue != 0;
return true;
}

bool RhConfig::Environment::TryGetIntegerValue(const char* name, uint64_t* value, bool decimal)
{
TCHAR variableName[64];
GetEnvironmentConfigName(name, variableName, ARRAY_SIZE(variableName));

TCHAR buffer[CONFIG_VAL_MAXLEN + 1]; // hex digits plus a nul terminator.
const uint32_t cchBuffer = ARRAY_SIZE(buffer);
uint32_t cchResult = PalGetEnvironmentVariable(variableName, buffer, cchBuffer);
if (cchResult == 0 || cchResult >= cchBuffer)
return false;

uint32_t cchResult = 0;
TCHAR variableName[64] = _T("DOTNET_");
assert(ARRAY_SIZE("DOTNET_") - 1 + strlen(name) < ARRAY_SIZE(variableName));
#ifdef TARGET_WINDOWS
for (size_t i = 0; i < strlen(name); i++)
// Environment variable was set. Convert it to an integer.
uint64_t uiResult = 0;
for (uint32_t i = 0; i < cchResult; i++)
{
variableName[ARRAY_SIZE("DOTNET_") - 1 + i] = name[i];
}
#else
strcat(variableName, name);
#endif
TCHAR ch = buffer[i];

cchResult = PalGetEnvironmentVariable(variableName, buffer, cchBuffer);
if (cchResult != 0 && cchResult < cchBuffer)
{
// Environment variable was set. Convert it to an integer.
uint64_t uiResult = 0;
for (uint32_t i = 0; i < cchResult; i++)
if (decimal)
{
TCHAR ch = buffer[i];
uiResult *= 10;

if (decimal)
{
uiResult *= 10;

if ((ch >= '0') && (ch <= '9'))
uiResult += ch - '0';
else
return false; // parse error
}
if ((ch >= '0') && (ch <= '9'))
uiResult += ch - '0';
else
{
uiResult *= 16;

if ((ch >= '0') && (ch <= '9'))
uiResult += ch - '0';
else if ((ch >= 'a') && (ch <= 'f'))
uiResult += (ch - 'a') + 10;
else if ((ch >= 'A') && (ch <= 'F'))
uiResult += (ch - 'A') + 10;
else
return false; // parse error
}
return false; // parse error
}
else
{
uiResult *= 16;

if ((ch >= '0') && (ch <= '9'))
uiResult += ch - '0';
else if ((ch >= 'a') && (ch <= 'f'))
uiResult += (ch - 'a') + 10;
else if ((ch >= 'A') && (ch <= 'F'))
uiResult += (ch - 'A') + 10;
else
return false; // parse error
}
}

*value = uiResult;
return true;
}

bool RhConfig::Environment::TryGetStringValue(const char* name, char** value)
{
TCHAR variableName[64];
GetEnvironmentConfigName(name, variableName, ARRAY_SIZE(variableName));

TCHAR buffer[260];
uint32_t bufferLen = ARRAY_SIZE(buffer);
uint32_t actualLen = PalGetEnvironmentVariable(variableName, buffer, bufferLen);
if (actualLen == 0)
return false;

*pValue = uiResult;
if (actualLen < bufferLen)
{
*value = PalCopyTCharAsChar(buffer);
return true;
}

// Expand the buffer to get the value
bufferLen = actualLen + 1;
NewArrayHolder<TCHAR> newBuffer {new (nothrow) TCHAR[bufferLen]};
actualLen = PalGetEnvironmentVariable(variableName, newBuffer, bufferLen);
if (actualLen >= bufferLen)
return false;

#ifdef TARGET_WINDOWS
*value = PalCopyTCharAsChar(newBuffer);
#else
*value = newBuffer.Extract();
#endif
return true;
}

bool RhConfig::ReadConfigValue(_In_z_ const char *name, uint64_t* pValue, bool decimal)
{
if (Environment::TryGetIntegerValue(name, pValue, decimal))
return true;

// Check the embedded configuration
const char *embeddedValue = nullptr;
if (GetEmbeddedVariable(name, &embeddedValue))
Expand Down
12 changes: 11 additions & 1 deletion src/coreclr/nativeaot/Runtime/RhConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

#ifndef DACCESS_COMPILE

#include <sal.h>

class RhConfig
{

#define CONFIG_INI_NOT_AVAIL (void*)0x1 //signal for ini file failed to load
#define CONFIG_KEY_MAXLEN 50 //arbitrary max length of config keys increase if needed
#define CONFIG_VAL_MAXLEN 16 //64 bit uint in hex

private:
struct ConfigPair
{
Expand All @@ -36,6 +37,15 @@ class RhConfig
void* volatile g_embeddedSettings = NULL;

public:
class Environment
{
public: // static
static bool TryGetBooleanValue(const char* name, bool* value);
static bool TryGetIntegerValue(const char* name, uint64_t* value, bool decimal = false);

// Get environment variable configuration as a string. On success, the caller owns the returned string value.
static bool TryGetStringValue(const char* name, char** value);
};

bool ReadConfigValue(_In_z_ const char* wszName, uint64_t* pValue, bool decimal = false);

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/nativeaot/Runtime/SpinLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifndef __SPINLOCK_H__
#define __SPINLOCK_H__

// defined in gcrhenv.cpp
bool __SwitchToThread(uint32_t dwSleepMSec, uint32_t dwSwitchCount);

// #SwitchToThreadSpinning
//
// If you call __SwitchToThread in a loop waiting for a condition to be met,
Expand Down
Loading