summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Au <andrewau@microsoft.com>2019-05-03 15:43:43 -0700
committerAndrew Au <cshung@gmail.com>2019-06-02 23:17:41 -0700
commit360d41f4ff93b6507863cb324a247fd7380b4f79 (patch)
treefaa024f92b29c0f2e9b69937721346e093bfa7bc /src
parent6fcd6cc466d215662cfe7810ee438a0996a844e4 (diff)
downloadcoreclr-360d41f4ff93b6507863cb324a247fd7380b4f79.tar.gz
coreclr-360d41f4ff93b6507863cb324a247fd7380b4f79.tar.bz2
coreclr-360d41f4ff93b6507863cb324a247fd7380b4f79.zip
AutoTrace - launch dotnet trace to capture trace as soon as the runtime is available to trace
Diffstat (limited to 'src')
-rw-r--r--src/vm/CMakeLists.txt3
-rw-r--r--src/vm/autotrace.cpp69
-rw-r--r--src/vm/autotrace.h11
-rw-r--r--src/vm/diagnosticserver.cpp16
4 files changed, 98 insertions, 1 deletions
diff --git a/src/vm/CMakeLists.txt b/src/vm/CMakeLists.txt
index 9db2594201..f8048d3e1d 100644
--- a/src/vm/CMakeLists.txt
+++ b/src/vm/CMakeLists.txt
@@ -4,6 +4,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${ARCH_SOURCES_DIR})
+add_definitions(-DFEATURE_AUTO_TRACE)
add_definitions(-DUNICODE)
add_definitions(-D_UNICODE)
@@ -308,6 +309,7 @@ set(VM_SOURCES_WKS
comwaithandle.cpp
customattribute.cpp
custommarshalerinfo.cpp
+ autotrace.cpp
diagnosticserver.cpp
dllimportcallback.cpp
eeconfig.cpp
@@ -422,6 +424,7 @@ set(VM_HEADERS_WKS
comwaithandle.h
customattribute.h
custommarshalerinfo.h
+ autotrace.h
diagnosticserver.h
diagnosticsprotocol.h
dllimportcallback.h
diff --git a/src/vm/autotrace.cpp b/src/vm/autotrace.cpp
new file mode 100644
index 0000000000..e30585b592
--- /dev/null
+++ b/src/vm/autotrace.cpp
@@ -0,0 +1,69 @@
+#include "common.h" // Required for pre-compiled header
+
+#ifdef FEATURE_AUTO_TRACE
+
+#include "pal.h"
+
+HANDLE auto_trace_event;
+
+void auto_trace_init()
+{
+ auto_trace_event = CreateEventW(
+ /* lpEventAttributes = */ NULL,
+ /* bManualReset = */ FALSE,
+ /* bInitialState = */ FALSE,
+ /* lpName = */ nullptr
+ );
+}
+
+void auto_trace_launch()
+{
+ DWORD currentProcessId = GetCurrentProcessId();
+ STARTUPINFO si;
+ ZeroMemory(&si, sizeof(si));
+ si.cb = sizeof(STARTUPINFO);
+#ifndef FEATURE_PAL
+ si.dwFlags = STARTF_USESHOWWINDOW;
+ si.wShowWindow = SW_HIDE;
+#endif
+
+ PROCESS_INFORMATION result;
+ #ifdef FEATURE_PAL
+ const char16_t* commandFormat = u"/Users/andrewau/git/diagnostics/src/Tools/dotnet-trace/run.sh collect --providers Microsoft-Windows-DotNETRuntime:FFFFFFFFFFFFFFBF -p %d";
+ #else
+ const char16_t* commandFormat = u"C:\\Windows\\System32\\cmd.exe /c run.cmd collect --providers Microsoft-Windows-DotNETRuntime:FFFFFFFFFFFFFFBF -p %d";
+ #endif
+ size_t len = wcslen(commandFormat) + 10 + 1;
+ char16_t* command = new char16_t[len];
+ _snwprintf_s(command, len, _TRUNCATE, commandFormat, currentProcessId);
+ const char16_t* currentDirectory = u"C:\\Dev\\diagnostics\\src\\Tools\\dotnet-trace";
+
+ BOOL code = CreateProcessW(
+ /* lpApplicationName = */ nullptr,
+ /* lpCommandLine = */ command,
+ /* lpCommandLine = */ nullptr,
+ /* lpThreadAttributes = */ nullptr,
+ /* bInheritHandles = */ false,
+ /* dwCreationFlags = */ CREATE_NEW_CONSOLE,
+ /* lpEnvironment = */ nullptr,
+ /* lpCurrentDirectory = */ currentDirectory,
+ /* lpStartupInfo = */ &si,
+ /* lpProcessInformation = */ &result
+ );
+ delete[] command;
+}
+
+void auto_trace_wait()
+{
+ WaitForSingleObject(auto_trace_event, INFINITE);
+}
+
+void auto_trace_signal()
+{
+ #ifdef SetEvent
+ #undef SetEvent
+ #endif
+ SetEvent(auto_trace_event);
+}
+
+#endif // FEATURE_AUTO_TRACE \ No newline at end of file
diff --git a/src/vm/autotrace.h b/src/vm/autotrace.h
new file mode 100644
index 0000000000..0a3c721ce3
--- /dev/null
+++ b/src/vm/autotrace.h
@@ -0,0 +1,11 @@
+#ifndef __AUTO_TRACE_H__
+#define __AUTO_TRACE_H__
+#ifdef FEATURE_AUTO_TRACE
+
+void auto_trace_init();
+void auto_trace_launch();
+void auto_trace_wait();
+void auto_trace_signal();
+
+#endif // FEATURE_AUTO_TRACE
+#endif // __AUTO_TRACE_H__ \ No newline at end of file
diff --git a/src/vm/diagnosticserver.cpp b/src/vm/diagnosticserver.cpp
index ae26ec9ca2..5ec60aa491 100644
--- a/src/vm/diagnosticserver.cpp
+++ b/src/vm/diagnosticserver.cpp
@@ -13,6 +13,10 @@
#include "pal.h"
#endif // FEATURE_PAL
+#ifdef FEATURE_AUTO_TRACE
+#include "autotrace.h"
+#endif
+
#ifdef FEATURE_PERFTRACING
IpcStream::DiagnosticsIpc *DiagnosticServer::s_pIpc = nullptr;
@@ -45,9 +49,12 @@ static DWORD WINAPI DiagnosticsServerThread(LPVOID lpThreadParameter)
{
// FIXME: Ideally this would be something like a std::shared_ptr
IpcStream *pStream = pIpc->Accept(LoggingCallback);
+
if (pStream == nullptr)
continue;
-
+#ifdef FEATURE_AUTO_TRACE
+ auto_trace_signal();
+#endif
DiagnosticsIpc::IpcMessage message;
if (!message.Initialize(pStream))
{
@@ -128,6 +135,10 @@ bool DiagnosticServer::Initialize()
if (s_pIpc != nullptr)
{
+#ifdef FEATURE_AUTO_TRACE
+ auto_trace_init();
+ auto_trace_launch();
+#endif
DWORD dwThreadId = 0;
HANDLE hThread = ::CreateThread( // TODO: Is it correct to have this "lower" level call here?
nullptr, // no security attribute
@@ -148,6 +159,9 @@ bool DiagnosticServer::Initialize()
}
else
{
+#ifdef FEATURE_AUTO_TRACE
+ auto_trace_wait();
+#endif
// FIXME: Maybe hold on to the thread to abort/cleanup at exit?
::CloseHandle(hThread);