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

EventPipe support for ProcessInfo #87562

Merged
merged 10 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion eng/pipelines/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ extends:
extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml
extraStepsParameters:
creator: dotnet-bot
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'
testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/diagnosticport;tracing/eventpipe/reverse;tracing/eventpipe/processenvironment;tracing/eventpipe/simpleruntimeeventvalidation;tracing/eventpipe/processinfo2;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
liveLibrariesBuildConfig: Release
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)
extraVariablesTemplates:
Expand Down
87 changes: 77 additions & 10 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ thread_local EventPipeAotThreadHolderTLS EventPipeAotThreadHolderTLS::g_threadHo
ep_rt_lock_handle_t _ep_rt_aot_config_lock_handle;
CrstStatic _ep_rt_aot_config_lock;

ep_char8_t *volatile _ep_rt_aot_diagnostics_cmd_line;

#ifndef TARGET_UNIX
uint32_t *_ep_rt_aot_proc_group_offsets;
Expand Down Expand Up @@ -85,12 +84,66 @@ ep_rt_aot_sample_profiler_write_sampling_event_for_threads (
{
}

#if defined (__linux__)
extern char *__progname;
#endif

const ep_char8_t *
ep_rt_aot_entrypoint_assembly_name_get_utf8 (void)
{
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
// TODO: Implement EventPipe assembly name - return filename in nativeaot?
return reinterpret_cast<const ep_char8_t*>("");
// Although PalGetModuleFileName exists, non-windows implementation has issues and we use __progname for Linux.
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
// Cannot use __cpp_threadsafe_static_init feature since it will bring in the C++ runtime and need to use threadsafe way to initialize entrypoint_assembly_name
static const ep_char8_t * entrypoint_assembly_name = nullptr;
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
if (entrypoint_assembly_name == nullptr) {
const ep_char8_t * entrypoint_assembly_name_local;
#ifdef HOST_WINDOWS
const TCHAR * wszModuleFileName = NULL;
if(PalGetModuleFileName(&wszModuleFileName, nullptr) == 0)
entrypoint_assembly_name_local = reinterpret_cast<const ep_char8_t*>("");
else {
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
const wchar_t* process_name = wcsrchr(wszModuleFileName, '\\');
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
if (process_name != NULL) {
const wchar_t* extension = wcschr(process_name + 1, '.');
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
if (extension != NULL) {
// We don't want to include the first '\'
size_t len = extension - (process_name + 1);
wchar_t* process_name_wo_ext = reinterpret_cast<wchar_t *>(malloc(len + 1));
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
wcsncpy(process_name_wo_ext, process_name + 1, len);
process_name_wo_ext[len] = L'\0';
const ep_char16_t* process_name_wo_ext_l = reinterpret_cast<const ep_char16_t *>(process_name_wo_ext);
entrypoint_assembly_name_local = ep_rt_utf16_to_utf8_string(process_name_wo_ext_l, -1);
} else {
const ep_char16_t* process_name_l = reinterpret_cast<const ep_char16_t *>(process_name + 1);
entrypoint_assembly_name_local = ep_rt_utf16_to_utf8_string(process_name_l, -1);
}
}
else
entrypoint_assembly_name_local = reinterpret_cast<const ep_char8_t*>("");
}
#else
#if defined (__linux__)
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
size_t program_len = strlen(__progname);
ep_char8_t *process_name = reinterpret_cast<ep_char8_t *>(malloc(program_len + 1));
if (process_name == NULL)
entrypoint_assembly_name_local = reinterpret_cast<const ep_char8_t*>("");
else {
memcpy (process_name, __progname, program_len);
process_name[program_len] = '\0';
char *dot = strrchr(process_name, '.');
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
if (dot != NULL) {
*dot = '\0';
}
entrypoint_assembly_name_local = reinterpret_cast<const ep_char8_t*>(process_name);
}
#else
entrypoint_assembly_name_local = reinterpret_cast<const ep_char8_t*>("");
#endif // __linux__
#endif // HOST_WINDOWS

if (PalInterlockedCompareExchangePointer((void**)(&entrypoint_assembly_name), (void*)(entrypoint_assembly_name_local), nullptr) != nullptr)
delete[] entrypoint_assembly_name_local;
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
}
return reinterpret_cast<const char*>(entrypoint_assembly_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is always returning the process name (minus extension) what we want? So if someone renames the executable or if someone uses a nativeaot library, they would get the name of the process which would not match the assembly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the first iteration, using the process name without extension is fine. We can enhance if user feedback requests this.

LakshanF marked this conversation as resolved.
Show resolved Hide resolved
}

const ep_char8_t *
Expand All @@ -101,6 +154,20 @@ ep_rt_aot_diagnostics_command_line_get (void)
#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);
#elif TARGET_LINUX
FILE *cmdline_file = ::fopen("/proc/self/cmdline", "r");
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
if (cmdline_file == nullptr)
return "";

char *line = NULL;
size_t line_len = 0;
if (::getline (&line, &line_len, cmdline_file) == -1) {
::fclose (cmdline_file);
return "";
}

::fclose (cmdline_file);
return reinterpret_cast<const ep_char8_t*>(line);
#else
return "";
#endif
Expand Down Expand Up @@ -388,10 +455,10 @@ ep_rt_aot_file_close (ep_rt_file_handle_t file_handle)

bool
ep_rt_aot_file_write (
ep_rt_file_handle_t file_handle,
const uint8_t *buffer,
uint32_t bytes_to_write,
uint32_t *bytes_written)
ep_rt_file_handle_t file_handle,
const uint8_t *buffer,
uint32_t bytes_to_write,
uint32_t *bytes_written)
{
#ifdef TARGET_WINDOWS
return ::WriteFile (file_handle, buffer, bytes_to_write, reinterpret_cast<LPDWORD>(bytes_written), NULL) != FALSE;
Expand Down Expand Up @@ -722,12 +789,12 @@ void ep_rt_aot_lock_requires_lock_not_held (const ep_rt_lock_handle_t *lock)
void ep_rt_aot_spin_lock_requires_lock_held (const ep_rt_spin_lock_handle_t *spin_lock)
{
EP_ASSERT (ep_rt_spin_lock_is_valid (spin_lock));
EP_ASSERT (spin_lock->lock->OwnedByCurrentThread ());
EP_ASSERT (spin_lock->lock->OwnedByCurrentThread ());
}

void ep_rt_aot_spin_lock_requires_lock_not_held (const ep_rt_spin_lock_handle_t *spin_lock)
{
EP_ASSERT (spin_lock->lock == NULL || !spin_lock->lock->OwnedByCurrentThread ());
EP_ASSERT (spin_lock->lock == NULL || !spin_lock->lock->OwnedByCurrentThread ());
}

#endif /* EP_CHECKED_BUILD */
Expand Down
4 changes: 4 additions & 0 deletions src/tests/Common/CoreCLRTestLibrary/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public static bool IsWindowsIoTCore
public static bool IsMonoRuntime => Type.GetType("Mono.RuntimeStructs") != null;
public static bool IsNotMonoRuntime => !IsMonoRuntime;
public static bool IsNativeAot => IsNotMonoRuntime && !IsReflectionEmitSupported;

public static bool HasAssemblyFiles => !string.IsNullOrEmpty(typeof(Utilities).Assembly.Location);
public static bool IsSingleFile => !HasAssemblyFiles;

#if NETCOREAPP
public static bool IsReflectionEmitSupported => RuntimeFeature.IsDynamicCodeSupported;
public static bool IsNotReflectionEmitSupported => !IsReflectionEmitSupported;
Expand Down
8 changes: 3 additions & 5 deletions src/tests/tracing/eventpipe/diagnosticport/diagnosticport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,12 @@ public static async Task<bool> TEST_CanGetProcessInfo2WhileSuspended()
}
else if (TestLibrary.Utilities.IsNativeAot)
{
// shipping criteria: no EVENTPIPE-NATIVEAOT-TODO left in the codebase
// https://github.com/dotnet/runtime/issues/83051
// NativeAOT currently always returns empty string
Utils.Assert(processInfo2.ManagedEntrypointAssemblyName == string.Empty);
string expectedName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
Utils.Assert(expectedName.Equals(processInfo2.ManagedEntrypointAssemblyName),
$"ManagedEntrypointAssemblyName must match. Expected: {expectedName}, Received: {processInfo2.ManagedEntrypointAssemblyName}");
}
else
{
// Assembly has not been loaded yet, so the assembly file name is used
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
string expectedName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location);
Utils.Assert(expectedName.Equals(processInfo2.ManagedEntrypointAssemblyName),
$"ManagedEntrypointAssemblyName must match. Expected: {expectedName}, Received: {processInfo2.ManagedEntrypointAssemblyName}");
Expand Down
4 changes: 3 additions & 1 deletion src/tests/tracing/eventpipe/processinfo/processinfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public static int Main()
// /path/to/corerun /path/to/processinfo.dll
// or
// "C:\path\to\CoreRun.exe" C:\path\to\processinfo.dll
string currentProcessCommandLine = $"{currentProcess.MainModule.FileName} {System.Reflection.Assembly.GetExecutingAssembly().Location}";
string currentProcessCommandLine = TestLibrary.Utilities.IsSingleFile
? currentProcess.MainModule.FileName
: $"{currentProcess.MainModule.FileName} {System.Reflection.Assembly.GetExecutingAssembly().Location}";
string receivedCommandLine = NormalizeCommandLine(commandLine);
Utils.Assert(currentProcessCommandLine.Equals(receivedCommandLine, StringComparison.OrdinalIgnoreCase), $"CommandLine must match current process. Expected: {currentProcessCommandLine}, Received: {receivedCommandLine} (original: {commandLine})");
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/tracing/eventpipe/processinfo/processinfo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<ProjectReference Include="../common/common.csproj" />
<ProjectReference Include="$(TestSourceDir)Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion src/tests/tracing/eventpipe/processinfo2/processinfo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public static int Main()
// /path/to/corerun /path/to/processinfo.dll
// or
// "C:\path\to\CoreRun.exe" C:\path\to\processinfo.dll
string currentProcessCommandLine = $"{currentProcess.MainModule.FileName} {System.Reflection.Assembly.GetExecutingAssembly().Location}";
string currentProcessCommandLine = TestLibrary.Utilities.IsSingleFile
? currentProcess.MainModule.FileName
: $"{currentProcess.MainModule.FileName} {System.Reflection.Assembly.GetExecutingAssembly().Location}";
string receivedCommandLine = NormalizeCommandLine(commandLine);
Utils.Assert(currentProcessCommandLine.Equals(receivedCommandLine, StringComparison.OrdinalIgnoreCase), $"CommandLine must match current process. Expected: {currentProcessCommandLine}, Received: {receivedCommandLine} (original: {commandLine})");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<ProjectReference Include="../common/common.csproj" />
<ProjectReference Include="$(TestSourceDir)Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>
Loading