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
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/StackFrameIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class StackFrameIterator
bool GetHijackedReturnValueLocation(PTR_OBJECTREF * pLocation, GCRefKind * pKind);
#endif
void SetControlPC(PTR_VOID controlPC);
PTR_VOID GetControlPC() { return m_ControlPC; }

static bool IsValidReturnAddress(PTR_VOID pvAddress);

Expand Down
72 changes: 70 additions & 2 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,29 @@ ep_rt_aot_walk_managed_stack_for_thread (
ep_rt_thread_handle_t thread,
EventPipeStackContents *stack_contents)
{
// NativeAOT does not support getting the call stack
return false;
STATIC_CONTRACT_NOTHROW;
EP_ASSERT (thread != NULL);
EP_ASSERT (stack_contents != NULL);

StackFrameIterator frameIterator(thread, thread->GetTransitionFrameForSampling());

while (frameIterator.IsValid())
{
frameIterator.CalculateCurrentMethodState();

// Get the IP.
uintptr_t control_pc = (uintptr_t)frameIterator.GetControlPC();

if (control_pc != 0)
{
// Add the IP to the captured stack.
ep_stack_contents_append (stack_contents, control_pc, NULL);
}

frameIterator.Next();
}

return true;
}

bool
Expand All @@ -62,6 +83,53 @@ ep_rt_aot_sample_profiler_write_sampling_event_for_threads (
ep_rt_thread_handle_t sampling_thread,
EventPipeEvent *sampling_event)
{
STATIC_CONTRACT_NOTHROW;
EP_ASSERT (sampling_thread != NULL);

ThreadStore *thread_store = GetThreadStore ();

// Check to see if we can suspend managed execution.
if (thread_store->GetSuspendingThread () != NULL)
return;

// Actually suspend managed execution.
thread_store->LockThreadStore ();
thread_store->SuspendAllThreads (false);

EventPipeStackContents stack_contents;
EventPipeStackContents *current_stack_contents;
current_stack_contents = ep_stack_contents_init (&stack_contents);

EP_ASSERT (current_stack_contents != NULL);

// Walk all managed threads and capture stacks.
FOREACH_THREAD (target_thread)
{
ep_stack_contents_reset (current_stack_contents);

// Walk the stack and write it out as an event.
if (ep_rt_aot_walk_managed_stack_for_thread (target_thread, current_stack_contents) && !ep_stack_contents_is_empty (current_stack_contents)) {
// Set the payload.
// TODO: We can actually detect whether we are in managed or external code but does it matter?!
uint32_t payload_data = EP_SAMPLE_PROFILER_SAMPLE_TYPE_EXTERNAL;

// Write the sample.
ep_write_sample_profile_event (
sampling_thread,
sampling_event,
target_thread,
current_stack_contents,
(uint8_t *)&payload_data,
sizeof (payload_data));
}
}
END_FOREACH_THREAD

ep_stack_contents_fini (current_stack_contents);

// Resume managed execution.
thread_store->ResumeAllThreads(false);
thread_store->UnlockThreadStore();
}

const ep_char8_t *
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ class Thread : private RuntimeThreadLocals
bool IsCurrentThreadInCooperativeMode();

PInvokeTransitionFrame* GetTransitionFrameForStackTrace();
PInvokeTransitionFrame* GetTransitionFrameForSampling() { return GetTransitionFrame(); }
void * GetCurrentThreadPInvokeReturnAddress();

//
Expand Down
Loading