Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,21 @@ ep_rt_utf8_string_dup (const ep_char8_t *str)
return _strdup (str);
}

static
inline
ep_char8_t *
ep_rt_utf8_string_dup_range (const ep_char8_t *str, const ep_char8_t *strEnd)
{
ptrdiff_t byte_len = strEnd - str;
ep_char8_t *buffer = reinterpret_cast<ep_char8_t *>(malloc(byte_len + 1));
if (buffer != NULL)
{
memcpy (buffer, str, byte_len);
buffer [byte_len] = '\0';
}
return buffer;
}

static
inline
ep_char8_t *
Expand All @@ -2472,6 +2487,38 @@ ep_rt_utf8_string_strtok (
format, ...) \
sprintf_s (reinterpret_cast<char *>(str), static_cast<size_t>(str_len), reinterpret_cast<const char *>(format), __VA_ARGS__)

static
inline
bool
ep_rt_utf8_string_replace (
ep_char8_t **str,
const ep_char8_t *strSearch,
const ep_char8_t *strReplacement
)
{
STATIC_CONTRACT_NOTHROW;
if ((*str) == NULL)
return false;

ep_char8_t* strFound = strstr(*str, strSearch);
if (strFound != NULL)
{
size_t strSearchLen = strlen(strSearch);
size_t newStrSize = strlen(*str) + strlen(strReplacement) - strSearchLen + 1;
ep_char8_t *newStr = reinterpret_cast<ep_char8_t *>(malloc(newStrSize));
if (newStr == NULL)
{
*str = NULL;
return false;
}
ep_rt_utf8_string_snprintf(newStr, newStrSize, "%.*s%s%s", (int)(strFound - (*str)), *str, strReplacement, strFound + strSearchLen);
ep_rt_utf8_string_free(*str);
*str = newStr;
return true;
}
return false;
}

static
ep_char16_t *
ep_rt_utf8_to_utf16_string (
Expand Down
47 changes: 47 additions & 0 deletions src/mono/mono/eventpipe/ep-rt-mono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,22 @@ ep_rt_utf8_string_dup (const ep_char8_t *str)
return g_strdup (str);
}

static
inline
ep_char8_t *
ep_rt_utf8_string_dup_range (const ep_char8_t *str, const ep_char8_t *strEnd)
{
ptrdiff_t byte_len = strEnd - str;
ep_char8_t *buffer = g_new(ep_char8_t, byte_len + 1);
if (buffer != NULL)
{
memcpy (buffer, str, byte_len);
buffer [byte_len] = '\0';
}
return buffer;
}


static
inline
ep_char8_t *
Expand All @@ -1844,6 +1860,37 @@ ep_rt_utf8_string_strtok (
format, ...) \
g_snprintf ((gchar *)str, (gulong)str_len, (const gchar *)format, __VA_ARGS__)

static
inline
bool
ep_rt_utf8_string_replace (
ep_char8_t **str,
const ep_char8_t *strSearch,
const ep_char8_t *strReplacement
)
{
if ((*str) == NULL)
return false;

ep_char8_t* strFound = strstr(*str, strSearch);
if (strFound != NULL)
{
size_t strSearchLen = strlen(strSearch);
size_t newStrSize = strlen(*str) + strlen(strReplacement) - strSearchLen + 1;
ep_char8_t *newStr = g_new(ep_char8_t, newStrSize);
if (newStr == NULL)
{
*str = NULL;
return false;
}
ep_rt_utf8_string_snprintf(newStr, newStrSize, "%.*s%s%s", (int)(strFound - (*str)), *str, strReplacement, strFound + strSearchLen);
ep_rt_utf8_string_free(*str);
*str = newStr;
return true;
}
return false;
}

static
inline
ep_char16_t *
Expand Down
12 changes: 12 additions & 0 deletions src/native/eventpipe/ep-rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ static
ep_char8_t *
ep_rt_utf8_string_dup (const ep_char8_t *str);

static
ep_char8_t *
ep_rt_utf8_string_dup_range (const ep_char8_t *str, const ep_char8_t *strEnd);

static
ep_char8_t *
ep_rt_utf8_string_strtok (
Expand All @@ -682,6 +686,14 @@ ep_rt_utf8_string_strtok (
str_len, \
format, ...) ep_redefine

static
inline bool
ep_rt_utf8_string_replace (
ep_char8_t **str,
const ep_char8_t *strSearch,
const ep_char8_t *strReplacement
);

static
ep_char16_t *
ep_rt_utf8_to_utf16_string (
Expand Down
27 changes: 20 additions & 7 deletions src/native/eventpipe/ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,20 +729,17 @@ get_next_config_value_as_utf8_string (const ep_char8_t **data)
{
EP_ASSERT (data != NULL);

uint8_t *buffer = NULL;
ep_char8_t *buffer = NULL;

const ep_char8_t *start = NULL;
const ep_char8_t *end = NULL;
*data = get_next_config_value (*data, &start, &end);

ptrdiff_t byte_len = end - start;
if (byte_len != 0) {
buffer = ep_rt_byte_array_alloc (byte_len + 1);
memcpy (buffer, start, byte_len);
buffer [byte_len] = '\0';
}
if (byte_len != 0)
buffer = ep_rt_utf8_string_dup_range(start, end);

return (ep_char8_t *)buffer;
return buffer;
}

static
Expand Down Expand Up @@ -792,6 +789,22 @@ enable_default_session_via_env_variables (void)
if (ep_rt_config_value_get_enable ()) {
ep_config = ep_rt_config_value_get_config ();
ep_config_output_path = ep_rt_config_value_get_output_path ();

ep_char8_t pidStr[24];
ep_rt_utf8_string_snprintf(pidStr, EP_ARRAY_SIZE (pidStr), "%u", (unsigned)ep_rt_current_process_get_id());

while (true)
{
if (ep_rt_utf8_string_replace(&ep_config_output_path, "{pid}", pidStr))
{
// In case there is a second use of {pid} in the output path
continue;
}

// No more instances of {pid} in the OutputPath
break;
}

ep_circular_mb = ep_rt_config_value_get_circular_mb ();
output_path = NULL;

Expand Down
85 changes: 85 additions & 0 deletions src/tests/tracing/eventpipe/complus_config/name_config_with_pid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

class NameConfigWithPid
{
static int Main(string[] args)
{
if (args.Length > 0 && args[0] == "waitforinput")
{
Console.Error.WriteLine("WaitingForInput in ErrorStream");
Console.WriteLine("WaitingForInput");
Console.ReadLine();
return 100;
}
else
{
Process process = null;
try
{
process = new Process();
}
catch (PlatformNotSupportedException)
{
// For platforms that do not support launching child processes, simply succeed the test
return 100;
}

string corerun = Path.Combine(Environment.GetEnvironmentVariable("CORE_ROOT"), "corerun");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
corerun = corerun + ".exe";

// Use dll directory as temp directory
string tempDir = Path.GetDirectoryName(typeof(NameConfigWithPid).Assembly.Location);
string outputPathBaseName = $"eventPipeStream{Thread.CurrentThread.ManagedThreadId}_{(ulong)Stopwatch.GetTimestamp()}";
string outputPathPattern = Path.Combine(tempDir, outputPathBaseName + "_{pid}_{pid}.nettrace");

process.StartInfo.FileName = corerun;
process.StartInfo.Arguments = typeof(NameConfigWithPid).Assembly.Location + " waitforinput";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;

process.StartInfo.Environment.Add("COMPlus_EnableEventPipe", "1");
process.StartInfo.Environment.Add("COMPlus_EventPipeConfig", "Microsoft-Windows-DotNETRuntime:4c14fccbd:4");
process.StartInfo.Environment.Add("COMPlus_EventPipeOutputPath", outputPathPattern);

process.Start();

process.StandardError.ReadLine();
uint pid = (uint)process.Id;
string expectedPath = outputPathPattern.Replace("{pid}", pid.ToString());

process.StandardInput.WriteLine("input");
process.WaitForExit();
if (!File.Exists(expectedPath))
{
Console.WriteLine($"{expectedPath} not found");
return 1;
}
else
{
Console.WriteLine($"{expectedPath} found");
for (int i = 0; i < 20; i++)
{
try
{
if (File.Exists(expectedPath))
File.Delete(expectedPath);
return 100;
}
catch { }
Thread.Sleep(1000);
}
Console.WriteLine($"Unable to delete {expectedPath}");
return 2;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should wrap this in a try/finally that attempts to delete the file it potentially created.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<CLRTestKind>BuildAndRun</CLRTestKind>
<CLRTestPriority>0</CLRTestPriority>
<UnloadabilityIncompatible>true</UnloadabilityIncompatible>
<GCStressIncompatible>true</GCStressIncompatible>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>