Skip to content

Commit

Permalink
Move event pipe utf8/utf16 string conversion functions to shared even…
Browse files Browse the repository at this point in the history
…t pipe code (dotnet#90774)
  • Loading branch information
elinor-fung authored Aug 28, 2023
1 parent 1286133 commit e0b8c24
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 310 deletions.
8 changes: 4 additions & 4 deletions src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ ds_rt_aot_transport_get_default_name (
const ep_char8_t *suffix)
{
STATIC_CONTRACT_NOTHROW;

#ifdef TARGET_UNIX

EP_ASSERT (name != NULL);
Expand All @@ -202,7 +202,7 @@ ds_rt_aot_transport_get_default_name (
// also try to use 0 as the value.
if (!aot_ipc_get_process_id_disambiguation_key (id, &disambiguation_key))
EP_ASSERT (disambiguation_key == 0);

// Get a temp file location
format_result = ep_rt_temp_path_get (format_buffer, name_len);
if (format_result == 0) {
Expand Down Expand Up @@ -238,8 +238,8 @@ uint32_t
ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value)
{
#ifdef TARGET_UNIX
ep_char8_t *nameNarrow = ep_rt_utf16le_to_utf8_string (name, ep_rt_utf16_string_len (name));
ep_char8_t *valueNarrow = ep_rt_utf16le_to_utf8_string (value, ep_rt_utf16_string_len (value));
ep_char8_t *nameNarrow = ep_rt_utf16le_to_utf8_string (name);
ep_char8_t *valueNarrow = ep_rt_utf16le_to_utf8_string (value);
int32_t ret_value = setenv(nameNarrow, valueNarrow, 1);
free(nameNarrow);
free(valueNarrow);
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ uint32_t
ds_rt_config_value_get_default_port_suspend (void)
{
STATIC_CONTRACT_NOTHROW;

uint64_t value;
if (RhConfig::Environment::TryGetIntegerValue("DefaultDiagnosticPortSuspend", &value))
{
Expand Down Expand Up @@ -192,7 +192,7 @@ ds_rt_generate_core_dump (
}
const ep_char16_t *dumpName = ds_generate_core_dump_command_payload_get_dump_name (payload);
int32_t dumpType = static_cast<int32_t>(ds_generate_core_dump_command_payload_get_dump_type (payload));
ep_char8_t *dumpNameUtf8 = ep_rt_utf16le_to_utf8_string (dumpName, ep_rt_utf16_string_len (dumpName));
ep_char8_t *dumpNameUtf8 = ep_rt_utf16le_to_utf8_string (dumpName);
extern bool PalGenerateCoreDump(const char* dumpName, int dumpType, uint32_t flags, char* errorMessageBuffer, int cbErrorMessageBuffer);
if (PalGenerateCoreDump(dumpNameUtf8, dumpType, flags, errorMessageBuffer, cbErrorMessageBuffer))
{
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ep_rt_aot_entrypoint_assembly_name_get_utf8 (void)
if (extension != NULL) {
len = extension - process_name_const;
}
entrypoint_assembly_name_local = ep_rt_utf16_to_utf8_string(reinterpret_cast<const ep_char16_t *>(process_name_const), len);
entrypoint_assembly_name_local = ep_rt_utf16_to_utf8_string_n(reinterpret_cast<const ep_char16_t *>(process_name_const), len);
#else
const ep_char8_t* process_name_const = strrchr(wszModuleFileName, DIRECTORY_SEPARATOR_CHAR);
if (process_name_const != NULL) {
Expand Down Expand Up @@ -132,7 +132,7 @@ ep_rt_aot_diagnostics_command_line_get (void)
// TODO: revisit commandline for AOT
#ifdef TARGET_WINDOWS
const ep_char16_t* command_line = reinterpret_cast<const ep_char16_t *>(::GetCommandLineW());
return ep_rt_utf16_to_utf8_string(command_line, -1);
return ep_rt_utf16_to_utf8_string(command_line);
#elif TARGET_LINUX
FILE *cmdline_file = ::fopen("/proc/self/cmdline", "r");
if (cmdline_file == nullptr)
Expand Down Expand Up @@ -438,7 +438,7 @@ ep_rt_aot_file_open_write (const ep_char8_t *path)
return INVALID_HANDLE_VALUE;

#ifdef TARGET_WINDOWS
ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16le_string (path, -1);
ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16le_string (path);
if (!path_utf16)
return INVALID_HANDLE_VALUE;

Expand Down Expand Up @@ -768,7 +768,7 @@ void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array)
#else
ep_char8_t **next = NULL;
for (next = environ; *next != NULL; ++next)
dn_vector_ptr_push_back (env_array, ep_rt_utf8_to_utf16le_string (*next, -1));
dn_vector_ptr_push_back (env_array, ep_rt_utf8_to_utf16le_string (*next));
#endif
}

Expand Down
117 changes: 11 additions & 106 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <eventpipe/ep-types.h>
#include <eventpipe/ep-provider.h>
#include <eventpipe/ep-session-provider.h>
#include <eventpipe/ep-string.h>

#include "rhassert.h"
#include <RhConfig.h>
Expand Down Expand Up @@ -1208,24 +1209,6 @@ ep_rt_utf8_string_compare_ignore_case (
return _stricmp (reinterpret_cast<const char *>(str1), reinterpret_cast<const char *>(str2));
}

static
inline
bool
ep_rt_utf8_string_is_null_or_empty (const ep_char8_t *str)
{
STATIC_CONTRACT_NOTHROW;

if (str == NULL)
return true;

while (*str) {
if (!isspace (*str))
return false;
str++;
}
return true;
}

static
inline
ep_char8_t *
Expand Down Expand Up @@ -1314,49 +1297,6 @@ ep_rt_utf8_string_replace (
return false;
}


static
ep_char16_t *
ep_rt_utf8_to_utf16le_string (
const ep_char8_t *str,
size_t len)
{
STATIC_CONTRACT_NOTHROW;

if (!str)
return NULL;

if (len == (size_t) -1) {
len = strlen(str);
}

if (len == 0) {
// Return an empty string if the length is 0
CHAR16_T * lpDestEmptyStr = reinterpret_cast<CHAR16_T *>(malloc(1 * sizeof(CHAR16_T)));
if(lpDestEmptyStr==NULL) {
return NULL;
}
*lpDestEmptyStr = '\0';
return reinterpret_cast<ep_char16_t*>(lpDestEmptyStr);
}

int32_t flags = MINIPAL_MB_NO_REPLACE_INVALID_CHARS | MINIPAL_TREAT_AS_LITTLE_ENDIAN;

size_t ret = minipal_get_length_utf8_to_utf16 (str, len, flags);

if (ret <= 0)
return NULL;

CHAR16_T * lpDestStr = reinterpret_cast<CHAR16_T *>(malloc((ret + 1) * sizeof(CHAR16_T)));
if(lpDestStr==NULL) {
return NULL;
}
ret = minipal_convert_utf8_to_utf16 (str, len, lpDestStr, ret, flags);
lpDestStr[ret] = '\0';

return reinterpret_cast<ep_char16_t*>(lpDestStr);
}

static
inline
ep_char16_t *
Expand All @@ -1374,6 +1314,13 @@ ep_rt_utf16_string_dup (const ep_char16_t *str)
return str_dup;
}

static
ep_char8_t *
ep_rt_utf8_string_alloc (size_t len)
{
return reinterpret_cast<ep_char8_t *>(malloc(len));
}

static
inline
void
Expand All @@ -1397,52 +1344,10 @@ ep_rt_utf16_string_len (const ep_char16_t *str)
}

static
ep_char8_t *
ep_rt_utf16_to_utf8_string (
const ep_char16_t *str,
size_t len)
{
STATIC_CONTRACT_NOTHROW;
if (!str)
return NULL;

if (len == (size_t) -1) {
len = ep_rt_utf16_string_len (str);
}

if (len == 0) {
// Return an empty string if the length is 0
char * lpDestEmptyStr = reinterpret_cast<char *>(malloc(1 * sizeof(char)));
if(lpDestEmptyStr==NULL) {
return NULL;
}
*lpDestEmptyStr = '\0';
return reinterpret_cast<ep_char8_t*>(lpDestEmptyStr);
}

size_t ret = minipal_get_length_utf16_to_utf8 (reinterpret_cast<const CHAR16_T *>(str), len, 0);

if (ret <= 0)
return NULL;

char* lpDestStr = reinterpret_cast<char *>(malloc((ret + 1) * sizeof(char)));
if(lpDestStr==NULL) {
return NULL;
}
ret = minipal_convert_utf16_to_utf8 (reinterpret_cast<const CHAR16_T*>(str), len, lpDestStr, ret, 0);
lpDestStr[ret] = '\0';

return reinterpret_cast<ep_char8_t*>(lpDestStr);
}

static
inline
ep_char8_t *
ep_rt_utf16le_to_utf8_string (
const ep_char16_t *str,
size_t len)
ep_char16_t *
ep_rt_utf16_string_alloc (size_t len)
{
return ep_rt_utf16_to_utf8_string (str, len);
return reinterpret_cast<ep_char16_t *>(malloc(len * sizeof(ep_char16_t)));
}

static
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/eventpipeadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EventPipeAdapter final
public:
static inline EventPipeProvider * CreateProvider(const WCHAR* providerName, EventPipeCallback callback, void* pCallbackContext = nullptr)
{
ep_char8_t *providerNameUTF8 = ep_rt_utf16_to_utf8_string(reinterpret_cast<const ep_char16_t *>(providerName), -1);
ep_char8_t *providerNameUTF8 = ep_rt_utf16_to_utf8_string(reinterpret_cast<const ep_char16_t *>(providerName));
EventPipeProvider * provider = ep_create_provider (providerNameUTF8, callback, pCallbackContext);
ep_rt_utf8_string_free (providerNameUTF8);
return provider;
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/nativeaot/Runtime/eventpipeinternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ EXTERN_C NATIVEAOT_API uint64_t __cdecl RhEventPipeInternal_Enable(
for (uint32_t i = 0; i < numProviders; ++i) {
ep_provider_config_init (
&configProviders[i],
ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(nativeProviders[i].pProviderName), -1),
ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(nativeProviders[i].pProviderName)),
nativeProviders[i].keywords,
static_cast<EventPipeEventLevel>(nativeProviders[i].loggingLevel),
ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(nativeProviders[i].pFilterData), -1));
ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(nativeProviders[i].pFilterData)));
}
}

ep_char8_t *outputPathUTF8 = NULL;
if (outputFile)
outputPathUTF8 = ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(outputFile), -1);
outputPathUTF8 = ep_rt_utf16_to_utf8_string (reinterpret_cast<const ep_char16_t *>(outputFile));
EventPipeSessionID result = ep_enable (
outputPathUTF8,
circularBufferSizeInMB,
Expand Down Expand Up @@ -133,7 +133,7 @@ EXTERN_C NATIVEAOT_API intptr_t __cdecl RhEventPipeInternal_GetProvider(const WC
EventPipeProvider * provider = NULL;
if (providerName)
{
ep_char8_t *providerNameUTF8 = ep_rt_utf16_to_utf8_string(reinterpret_cast<const ep_char16_t *>(providerName), -1);
ep_char8_t *providerNameUTF8 = ep_rt_utf16_to_utf8_string(reinterpret_cast<const ep_char16_t *>(providerName));
provider = ep_get_provider (providerNameUTF8);
ep_rt_utf8_string_free(providerNameUTF8);
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/eventing/eventpipe/ds-rt-coreclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ ds_rt_config_value_get_ports (void)
STATIC_CONTRACT_NOTHROW;

CLRConfigStringHolder value(CLRConfig::GetConfigValue (CLRConfig::EXTERNAL_DOTNET_DiagnosticPorts));
return ep_rt_utf16_to_utf8_string (reinterpret_cast<ep_char16_t *>(value.GetValue ()), -1);
return ep_rt_utf16_to_utf8_string (reinterpret_cast<ep_char16_t *>(value.GetValue ()));
}

static
Expand Down
Loading

0 comments on commit e0b8c24

Please sign in to comment.