Skip to content

Commit 2ffaa9a

Browse files
committed
[sanitizer] Add facility to print the full StackDepot
Split out of D87120 (memory profiler). Added unit testing of the new printing facility. Differential Revision: https://reviews.llvm.org/D87792
1 parent 55edf70 commit 2ffaa9a

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ void StackDepotUnlockAll() {
115115
theDepot.UnlockAll();
116116
}
117117

118+
void StackDepotPrintAll() {
119+
#if !SANITIZER_GO
120+
theDepot.PrintAll();
121+
#endif
122+
}
123+
118124
bool StackDepotReverseMap::IdDescPair::IdComparator(
119125
const StackDepotReverseMap::IdDescPair &a,
120126
const StackDepotReverseMap::IdDescPair &b) {

compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ StackTrace StackDepotGet(u32 id);
4141

4242
void StackDepotLockAll();
4343
void StackDepotUnlockAll();
44+
void StackDepotPrintAll();
4445

4546
// Instantiating this class creates a snapshot of StackDepot which can be
4647
// efficiently queried with StackDepotGet(). You can use it concurrently with

compiler-rt/lib/sanitizer_common/sanitizer_stackdepotbase.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#ifndef SANITIZER_STACKDEPOTBASE_H
1414
#define SANITIZER_STACKDEPOTBASE_H
1515

16+
#include <stdio.h>
17+
18+
#include "sanitizer_atomic.h"
1619
#include "sanitizer_internal_defs.h"
1720
#include "sanitizer_mutex.h"
18-
#include "sanitizer_atomic.h"
1921
#include "sanitizer_persistent_allocator.h"
2022

2123
namespace __sanitizer {
@@ -34,6 +36,7 @@ class StackDepotBase {
3436

3537
void LockAll();
3638
void UnlockAll();
39+
void PrintAll();
3740

3841
private:
3942
static Node *find(Node *s, args_type args, u32 hash);
@@ -172,6 +175,21 @@ void StackDepotBase<Node, kReservedBits, kTabSizeLog>::UnlockAll() {
172175
}
173176
}
174177

178+
template <class Node, int kReservedBits, int kTabSizeLog>
179+
void StackDepotBase<Node, kReservedBits, kTabSizeLog>::PrintAll() {
180+
for (int i = 0; i < kTabSize; ++i) {
181+
atomic_uintptr_t *p = &tab[i];
182+
lock(p);
183+
uptr v = atomic_load(p, memory_order_relaxed);
184+
Node *s = (Node *)(v & ~1UL);
185+
for (; s; s = s->link) {
186+
Printf("Stack for id %u:\n", s->id);
187+
s->load().Print();
188+
}
189+
unlock(p, s);
190+
}
191+
}
192+
175193
} // namespace __sanitizer
176194

177195
#endif // SANITIZER_STACKDEPOTBASE_H

compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ TEST(SanitizerCommon, StackDepotSeveral) {
6464
EXPECT_NE(i1, i2);
6565
}
6666

67+
TEST(SanitizerCommon, StackDepotPrint) {
68+
uptr array1[] = {1, 2, 3, 4, 7};
69+
StackTrace s1(array1, ARRAY_SIZE(array1));
70+
u32 i1 = StackDepotPut(s1);
71+
uptr array2[] = {1, 2, 3, 4, 8, 9};
72+
StackTrace s2(array2, ARRAY_SIZE(array2));
73+
u32 i2 = StackDepotPut(s2);
74+
EXPECT_NE(i1, i2);
75+
EXPECT_EXIT(
76+
(StackDepotPrintAll(), exit(0)), ::testing::ExitedWithCode(0),
77+
"Stack for id .*#0 0x0.*#1 0x1.*#2 0x2.*#3 0x3.*#4 0x6.*Stack for id "
78+
".*#0 0x0.*#1 0x1.*#2 0x2.*#3 0x3.*#4 0x7.*#5 0x8.*");
79+
}
80+
6781
TEST(SanitizerCommon, StackDepotReverseMap) {
6882
uptr array1[] = {1, 2, 3, 4, 5};
6983
uptr array2[] = {7, 1, 3, 0};

0 commit comments

Comments
 (0)