Skip to content

Commit 36e4846

Browse files
committed
Commits from dotnet#24229 - Fixes for IBC profile data on Linux
Added public static function GetManagedCommandLine() and SaveManagedCommandLine() Moved GetManagedCommandLine and SaveManagedCommandLine to ceeload to link Disabled the setup for BBSweep on Linux as the PAL doesn’t support process named objects
1 parent 08bad50 commit 36e4846

File tree

6 files changed

+119
-68
lines changed

6 files changed

+119
-68
lines changed

src/vm/ceeload.cpp

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
#include "peimagelayout.inl"
7575
#include "ildbsymlib.h"
7676

77-
7877
#if defined(PROFILING_SUPPORTED)
7978
#include "profilermetadataemitvalidator.h"
8079
#endif
@@ -11653,6 +11652,102 @@ static bool GetBasename(LPCWSTR _src, __out_ecount(dstlen) __out_z LPWSTR _dst,
1165311652
return true;
1165411653
}
1165511654

11655+
static LPCWSTR s_pCommandLine = NULL;
11656+
11657+
// Rerieve the full command line for the current process.
11658+
LPCWSTR GetManagedCommandLine()
11659+
{
11660+
LIMITED_METHOD_CONTRACT;
11661+
return s_pCommandLine;
11662+
}
11663+
11664+
void Append_Next_Item(LPWSTR* ppCursor, SIZE_T* pRemainingLen, LPCWSTR pItem, bool addSpace)
11665+
{
11666+
// read the writeback args and setup pCursor and remainingLen
11667+
LPWSTR pCursor = *ppCursor;
11668+
SIZE_T remainingLen = *pRemainingLen;
11669+
11670+
// Calculate the length of pItem
11671+
SIZE_T itemLen = wcslen(pItem);
11672+
11673+
// Append pItem at pCursor
11674+
wcscpy_s(pCursor, remainingLen, pItem);
11675+
pCursor += itemLen;
11676+
remainingLen -= itemLen;
11677+
11678+
// Also append a space after pItem, if requested
11679+
if (addSpace)
11680+
{
11681+
// Append a space at pCursor
11682+
wcscpy_s(pCursor, remainingLen, W(" "));
11683+
pCursor += 1;
11684+
remainingLen -= 1;
11685+
}
11686+
11687+
// writeback and update ppCursor and pRemainingLen
11688+
*ppCursor = pCursor;
11689+
*pRemainingLen = remainingLen;
11690+
}
11691+
11692+
void SaveManagedCommandLine(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR *argv)
11693+
{
11694+
CONTRACTL
11695+
{
11696+
NOTHROW;
11697+
GC_NOTRIGGER;
11698+
MODE_ANY;
11699+
}
11700+
CONTRACTL_END;
11701+
11702+
// Get the command line.
11703+
LPCWSTR osCommandLine = GetCommandLineW();
11704+
11705+
#ifndef FEATURE_PAL
11706+
// On Windows, osCommandLine contains the executable and all arguments.
11707+
s_pCommandLine = osCommandLine;
11708+
#else
11709+
// On UNIX, the PAL doesn't have the command line arguments, so we must build the command line.
11710+
// osCommandLine contains the full path to the executable.
11711+
SIZE_T commandLineLen = (wcslen(osCommandLine) + 1);
11712+
11713+
// We will append pwzAssemblyPath to the 'corerun' osCommandLine
11714+
commandLineLen += (wcslen(pwzAssemblyPath) + 1);
11715+
11716+
for (int i = 0; i < argc; i++)
11717+
{
11718+
commandLineLen += (wcslen(argv[i]) + 1);
11719+
}
11720+
commandLineLen++; // Add 1 for the null-termination
11721+
11722+
// Allocate a new string for the command line.
11723+
LPWSTR pNewCommandLine = new WCHAR[commandLineLen];
11724+
SIZE_T remainingLen = commandLineLen;
11725+
LPWSTR pCursor = pNewCommandLine;
11726+
11727+
Append_Next_Item(&pCursor, &remainingLen, osCommandLine, true);
11728+
Append_Next_Item(&pCursor, &remainingLen, pwzAssemblyPath, true);
11729+
11730+
for (int i = 0; i < argc; i++)
11731+
{
11732+
bool moreArgs = (i < (argc-1));
11733+
Append_Next_Item(&pCursor, &remainingLen, argv[i], moreArgs);
11734+
}
11735+
11736+
s_pCommandLine = pNewCommandLine;
11737+
#endif
11738+
}
11739+
11740+
// Release any memory that we allocated for the managed command line
11741+
void ReleaseManagedCommandLine()
11742+
{
11743+
LIMITED_METHOD_CONTRACT;
11744+
11745+
#ifdef FEATURE_PAL
11746+
delete[] s_pCommandLine;
11747+
s_pCommandLine = NULL;
11748+
#endif
11749+
}
11750+
1165611751
static void ProfileDataAllocateScenarioInfo(ProfileEmitter * pEmitter, LPCSTR scopeName, GUID* pMvid)
1165711752
{
1165811753
CONTRACTL
@@ -11681,7 +11776,9 @@ static void ProfileDataAllocateScenarioInfo(ProfileEmitter * pEmitter, LPCSTR sc
1168111776
// Allocate and initialize the scenario header section
1168211777
//
1168311778
{
11684-
LPCWSTR pCmdLine = GetCommandLineW();
11779+
// Get the managed command line.
11780+
LPCWSTR pCmdLine = GetManagedCommandLine();
11781+
1168511782
S_SIZE_T cCmdLine = S_SIZE_T(wcslen(pCmdLine));
1168611783
cCmdLine += 1;
1168711784
if (cCmdLine.IsOverflow())

src/vm/ceeload.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,4 +3419,11 @@ struct VASigCookieEx : public VASigCookie
34193419
const BYTE *m_pArgs; // pointer to first unfixed unmanaged arg
34203420
};
34213421

3422+
// Rerieve the full command line for the current process.
3423+
LPCWSTR GetManagedCommandLine();
3424+
// Save the command line for the current process.
3425+
void SaveManagedCommandLine(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR *argv);
3426+
// Release any memory that we allocated for the managed command line
3427+
void ReleaseManagedCommandLine();
3428+
34223429
#endif // !CEELOAD_H_

src/vm/ceemain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,9 @@ void EEStartupHelper(COINITIEE fFlags)
770770

771771
#ifndef CROSSGEN_COMPILE
772772

773-
774-
#ifdef FEATURE_PREJIT
773+
// Cross-process named objects are not supported in PAL
774+
// (see CorUnix::InternalCreateEvent - src/pal/src/synchobj/event.cpp.272)
775+
#if defined(FEATURE_PREJIT) && !defined(FEATURE_PAL)
775776
// Initialize the sweeper thread.
776777
if (g_pConfig->GetZapBBInstr() != NULL)
777778
{
@@ -785,7 +786,7 @@ void EEStartupHelper(COINITIEE fFlags)
785786
_ASSERTE(hBBSweepThread);
786787
g_BBSweep.SetBBSweepThreadHandle(hBBSweepThread);
787788
}
788-
#endif // FEATURE_PREJIT
789+
#endif // FEATURE_PREJIT && FEATURE_PAL
789790

790791
#ifdef FEATURE_INTERPRETER
791792
Interpreter::Initialize();
@@ -2604,7 +2605,6 @@ INT32 GetLatchedExitCode (void)
26042605
return LatchedExitCode;
26052606
}
26062607

2607-
26082608
// ---------------------------------------------------------------------------
26092609
// Impl for UtilLoadStringRC Callback: In VM, we let the thread decide culture
26102610
// Return an int uniquely describing which language this thread is using for ui.

src/vm/corhost.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
#include "dwreport.h"
4040
#endif // !FEATURE_PAL
4141

42-
#include "stringarraylist.h"
43-
#ifdef FEATURE_PERFTRACING
44-
#include "eventpipe.h"
45-
#endif // FEATURE_PERFTRACING
46-
4742
#ifdef FEATURE_COMINTEROP
4843
#include "winrttypenameconverter.h"
4944
#endif
@@ -343,10 +338,8 @@ void SetCommandLineArgs(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR* argv)
343338
}
344339
CONTRACTL_END;
345340

346-
// Send the command line to EventPipe.
347-
#ifdef FEATURE_PERFTRACING
348-
EventPipe::SaveCommandLine(pwzAssemblyPath, argc, argv);
349-
#endif // FEATURE_PERFTRACING
341+
// Record the command line.
342+
SaveManagedCommandLine(pwzAssemblyPath, argc, argv);
350343

351344
// Send the command line to System.Environment.
352345
struct _gc
@@ -476,6 +469,11 @@ HRESULT CorHost2::ExecuteAssembly(DWORD dwAppDomainId,
476469

477470
GCPROTECT_END();
478471

472+
// When running under FEATURE_PAL, the SetCommandLineArgs call above will
473+
// call SaveManagedCommandLine which will allocate memory using new WCHAR[]
474+
// We can release this memory now.
475+
//
476+
ReleaseManagedCommandLine();
479477
}
480478

481479
UNINSTALL_UNWIND_AND_CONTINUE_HANDLER;

src/vm/eventpipe.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "eventtracebase.h"
2020
#include "sampleprofiler.h"
2121
#include "win32threadpool.h"
22+
#include "ceemain.h"
2223

2324
#ifdef FEATURE_PAL
2425
#include "pal.h"
@@ -33,7 +34,6 @@ EventPipeSession *EventPipe::s_pSession = NULL;
3334
EventPipeBufferManager *EventPipe::s_pBufferManager = NULL;
3435
EventPipeFile *EventPipe::s_pFile = NULL;
3536
EventPipeEventSource *EventPipe::s_pEventSource = NULL;
36-
LPCWSTR EventPipe::s_pCommandLine = NULL;
3737
HANDLE EventPipe::s_fileSwitchTimerHandle = NULL;
3838
ULONGLONG EventPipe::s_lastFlushSwitchTime = 0;
3939

@@ -223,12 +223,6 @@ void EventPipe::Shutdown()
223223
delete s_pEventSource;
224224
s_pEventSource = NULL;
225225

226-
// On Windows, this is just a pointer to the return value from
227-
// GetCommandLineW(), so don't attempt to free it.
228-
#ifdef FEATURE_PAL
229-
delete[] s_pCommandLine;
230-
s_pCommandLine = NULL;
231-
#endif
232226
}
233227

234228
EventPipeSessionID EventPipe::Enable(
@@ -380,7 +374,7 @@ void EventPipe::DisableInternal(EventPipeSessionID id, EventPipeProviderCallback
380374
SampleProfiler::Disable();
381375

382376
// Log the process information event.
383-
s_pEventSource->SendProcessInfo(s_pCommandLine);
377+
s_pEventSource->SendProcessInfo(GetManagedCommandLine());
384378

385379
// Log the runtime information event.
386380
ETW::InfoLog::RuntimeInformation(ETW::InfoLog::InfoStructs::Normal);
@@ -890,47 +884,6 @@ StackWalkAction EventPipe::StackWalkCallback(CrawlFrame *pCf, StackContents *pDa
890884
return SWA_CONTINUE;
891885
}
892886

893-
void EventPipe::SaveCommandLine(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR *argv)
894-
{
895-
CONTRACTL
896-
{
897-
THROWS;
898-
GC_TRIGGERS;
899-
MODE_COOPERATIVE;
900-
PRECONDITION(pwzAssemblyPath != NULL);
901-
PRECONDITION(argc <= 0 || argv != NULL);
902-
}
903-
CONTRACTL_END;
904-
905-
// Get the command line.
906-
LPCWSTR osCommandLine = GetCommandLineW();
907-
908-
#ifndef FEATURE_PAL
909-
// On Windows, osCommandLine contains the executable and all arguments.
910-
s_pCommandLine = osCommandLine;
911-
#else
912-
// On UNIX, the PAL doesn't have the command line arguments, so we must build the command line.
913-
// osCommandLine contains the full path to the executable.
914-
SString commandLine(osCommandLine);
915-
commandLine.Append((WCHAR)' ');
916-
commandLine.Append(pwzAssemblyPath);
917-
918-
for (int i = 0; i < argc; i++)
919-
{
920-
commandLine.Append((WCHAR)' ');
921-
commandLine.Append(argv[i]);
922-
}
923-
924-
// Allocate a new string for the command line.
925-
SIZE_T commandLineLen = commandLine.GetCount();
926-
WCHAR *pCommandLine = new WCHAR[commandLineLen + 1];
927-
wcsncpy(pCommandLine, commandLine.GetUnicode(), commandLineLen);
928-
pCommandLine[commandLineLen] = '\0';
929-
930-
s_pCommandLine = pCommandLine;
931-
#endif
932-
}
933-
934887
EventPipeEventInstance *EventPipe::GetNextEvent()
935888
{
936889
CONTRACTL

src/vm/eventpipe.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,6 @@ class EventPipe
330330
// Get the managed call stack for the specified thread.
331331
static bool WalkManagedStackForThread(Thread *pThread, StackContents &stackContents);
332332

333-
// Save the command line for the current process.
334-
static void SaveCommandLine(LPCWSTR pwzAssemblyPath, int argc, LPCWSTR *argv);
335-
336333
// Get next event.
337334
static EventPipeEventInstance *GetNextEvent();
338335

@@ -399,7 +396,6 @@ class EventPipe
399396
static EventPipeBufferManager *s_pBufferManager;
400397
static EventPipeFile *s_pFile;
401398
static EventPipeEventSource *s_pEventSource;
402-
static LPCWSTR s_pCommandLine;
403399
static HANDLE s_fileSwitchTimerHandle;
404400
static ULONGLONG s_lastFlushSwitchTime;
405401
};

0 commit comments

Comments
 (0)