Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1363)

Unified Diff: runtime/vm/malloc_hooks_test.cc

Issue 2680213002: Updated MallocHooks to collect stack traces when memory is allocated. (Closed)
Patch Set: Rough patch: addressed initial comments + converted sample filters from using Isolates to Dart_Port… Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/malloc_hooks_test.cc
diff --git a/runtime/vm/malloc_hooks_test.cc b/runtime/vm/malloc_hooks_test.cc
index 5054a5047dd0f09c6f4de145c68b79e9b8676740..44360ad6b62e8ce6dcb7a3bb99af0ec2fd06fb56 100644
--- a/runtime/vm/malloc_hooks_test.cc
+++ b/runtime/vm/malloc_hooks_test.cc
@@ -6,10 +6,14 @@
#if defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
+// #include <execinfo.h> TODO(bkonyi) remove before final patch.
zra 2017/02/08 18:14:14 If this is always going to be on Linux, I'd be fin
bkonyi 2017/02/09 21:22:46 This should basically always be on Linux since the
+
#include "platform/assert.h"
#include "vm/class_finalizer.h"
#include "vm/globals.h"
#include "vm/malloc_hooks.h"
+#include "vm/native_symbol.h"
+#include "vm/profiler.h"
#include "vm/symbols.h"
#include "vm/unit_test.h"
@@ -75,6 +79,59 @@ UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
MallocHooks::TearDown();
}
+/* TODO(bkonyi) Remove before final patch.
+// Helper used to add another frame to the stack before allocating.
+char* AllocationHelper(void* stack_trace[32]) {
+ backtrace(stack_trace, 32);
+ return static_cast<char*>(malloc(16 * sizeof(char)));
+}
+
+VM_UNIT_TEST_CASE(StackTraceMallocHookTest) {
+ MallocHooks::ResetStats();
+ ASSERT(OSThread::DoesCurrentThreadExist());
+
+ const intptr_t first_frame_count = 6;
+ void* stack_trace[32];
+
+ // Perform the memory allocation and grab the stack trace created by
+ // backtrace() for comparison purposes.
+ char* var = AllocationHelper(stack_trace);
+
+ // Update top pc of the stack trace to match the location that malloc was
+ // called in AllocationHelper. The rest of the pcs should match without any
+ // changes.
+ stack_trace[0] =
+ reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(stack_trace[0]) + 15);
+
+ Sample* sample = MallocHooks::GetSample(var);
+ ASSERT(sample != NULL);
+
+ intptr_t frame_count = 0;
+ while (sample != NULL) {
+ for (uintptr_t i = 0; i < 8; ++i) {
+ // Cease comparisons after we pass the frame for main.
+ if (sample->At(i) == 0) {
+ break;
+ }
+
+ // The first 6 frames in the stack trace we generate are all from the
+ // memory allocation. The frame for AllocationHelper is the 7th element,
+ // so we skip checking all the frames before that.
+ if (frame_count >= first_frame_count) {
+ EXPECT_EQ(reinterpret_cast<uword>(
+ stack_trace[frame_count - first_frame_count]),
+ sample->At(i));
+ }
+ ++frame_count;
+ }
+
+ if (!sample->is_continuation_sample()) {
+ break;
+ }
+ sample = Profiler::sample_buffer()->At(sample->continuation_index());
+ }
+}*/
+
}; // namespace dart
#endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)

Powered by Google App Engine
This is Rietveld 408576698