Skip to content

Commit 2f14594

Browse files
authored
Enable reading process environment variables in EventPipe (#87771)
* Process Environment support * using free to match malloc * Add test to CI * Fix FreeBSD build break * Fix typo in OSX environ def * FB
1 parent a3981dd commit 2f14594

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

eng/pipelines/runtime.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ extends:
248248
extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml
249249
extraStepsParameters:
250250
creator: dotnet-bot
251-
testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/diagnosticport;tracing/eventpipe/reverse;tracing/eventpipe/simpleruntimeeventvalidation;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
251+
testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/diagnosticport;tracing/eventpipe/reverse;tracing/eventpipe/processenvironment;tracing/eventpipe/simpleruntimeeventvalidation;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
252252
liveLibrariesBuildConfig: Release
253253
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)
254254
extraVariablesTemplates:

src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#ifdef TARGET_WINDOWS
5+
#include <windows.h>
6+
#else
7+
#include <stdlib.h>
8+
#endif
9+
410
#include <sys/types.h>
511

612
#ifdef __APPLE__
@@ -227,4 +233,19 @@ ds_rt_aot_transport_get_default_name (
227233
return true;
228234
#endif
229235
}
236+
237+
uint32_t
238+
ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value)
239+
{
240+
#ifdef TARGET_UNIX
241+
ep_char8_t *nameNarrow = ep_rt_utf16le_to_utf8_string (name, ep_rt_utf16_string_len (name));
242+
ep_char8_t *valueNarrow = ep_rt_utf16le_to_utf8_string (value, ep_rt_utf16_string_len (value));
243+
int32_t ret_value = setenv(nameNarrow, valueNarrow, 1);
244+
free(nameNarrow);
245+
free(valueNarrow);
246+
return ret_value;
247+
#else
248+
return SetEnvironmentVariableW(reinterpret_cast<LPCWSTR>(name), reinterpret_cast<LPCWSTR>(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
249+
#endif
250+
}
230251
#endif /* ENABLE_PERFTRACING */

src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ static
266266
uint32_t
267267
ds_rt_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value)
268268
{
269-
// return SetEnvironmentVariableW(reinterpret_cast<LPCWSTR>(name), reinterpret_cast<LPCWSTR>(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
270-
// PalDebugBreak();
271-
return 0xffff;
269+
extern uint32_t ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value);
270+
return ds_rt_aot_set_environment_variable(name, value);
272271
}
273272

274273
static

src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,44 @@ bool ep_rt_aot_spin_lock_release (ep_rt_spin_lock_handle_t *spin_lock)
669669
return false;
670670
}
671671

672+
#ifndef HOST_WIN32
673+
#if defined(__APPLE__)
674+
#if defined (HOST_OSX)
675+
extern "C" {char ***_NSGetEnviron(void);}
676+
#define environ (*_NSGetEnviron())
677+
#else
678+
static char *_ep_rt_aot_environ[1] = { NULL };
679+
#define environ _ep_rt_aot_environ
680+
#endif /* defined (HOST_OSX) */
681+
#else
682+
extern "C" {
683+
extern char **environ;
684+
}
685+
#endif /* defined (__APPLE__) */
686+
#endif /* !defined (HOST_WIN32) */
687+
688+
void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array)
689+
{
690+
STATIC_CONTRACT_NOTHROW;
691+
EP_ASSERT (env_array != NULL);
692+
693+
#ifdef HOST_WIN32
694+
ep_char16_t * envs = reinterpret_cast<ep_char16_t *>(GetEnvironmentStringsW ());
695+
if (envs) {
696+
const ep_char16_t * next = envs;
697+
while (*next) {
698+
dn_vector_ptr_push_back (env_array, ep_rt_utf16_string_dup (reinterpret_cast<const ep_char16_t *>(next)));
699+
next += ep_rt_utf16_string_len (reinterpret_cast<const ep_char16_t *>(next)) + 1;
700+
}
701+
FreeEnvironmentStringsW (reinterpret_cast<LPWSTR>(envs));
702+
}
703+
#else
704+
ep_char8_t **next = NULL;
705+
for (next = environ; *next != NULL; ++next)
706+
dn_vector_ptr_push_back (env_array, ep_rt_utf8_to_utf16le_string (*next, -1));
707+
#endif
708+
}
709+
672710
#ifdef EP_CHECKED_BUILD
673711

674712
void ep_rt_aot_lock_requires_lock_held (const ep_rt_lock_handle_t *lock)

src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,8 @@ static
11141114
void
11151115
ep_rt_os_environment_get_utf16 (dn_vector_ptr_t *env_array)
11161116
{
1117-
STATIC_CONTRACT_NOTHROW;
1118-
EP_ASSERT (env_array != NULL);
1119-
1120-
// PalDebugBreak();
1117+
extern void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array);
1118+
ep_rt_aot_os_environment_get_utf16(env_array);
11211119
}
11221120

11231121
/*

src/tests/tracing/eventpipe/processenvironment/processenvironment.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<GCStressIncompatible>true</GCStressIncompatible>
99
<JitOptimizationSensitive>true</JitOptimizationSensitive>
1010
<IlasmRoundTripIncompatible>true</IlasmRoundTripIncompatible>
11+
<EventSourceSupport Condition="'$(TestBuildMode)' == 'nativeaot'">true</EventSourceSupport>
1112
</PropertyGroup>
1213
<ItemGroup>
1314
<Compile Include="$(MSBuildProjectName).cs" />

0 commit comments

Comments
 (0)