1245614Sandrew//===-- sanitizer_stackdepot.h ----------------------------------*- C++ -*-===//
2245614Sandrew//
3245614Sandrew//                     The LLVM Compiler Infrastructure
4245614Sandrew//
5245614Sandrew// This file is distributed under the University of Illinois Open Source
6245614Sandrew// License. See LICENSE.TXT for details.
7245614Sandrew//
8245614Sandrew//===----------------------------------------------------------------------===//
9245614Sandrew//
10245614Sandrew// This file is shared between AddressSanitizer and ThreadSanitizer
11245614Sandrew// run-time libraries.
12245614Sandrew//===----------------------------------------------------------------------===//
13296417Sdim
14245614Sandrew#ifndef SANITIZER_STACKDEPOT_H
15245614Sandrew#define SANITIZER_STACKDEPOT_H
16245614Sandrew
17274201Sdim#include "sanitizer_common.h"
18251034Sed#include "sanitizer_internal_defs.h"
19276789Sdim#include "sanitizer_stacktrace.h"
20245614Sandrew
21245614Sandrewnamespace __sanitizer {
22245614Sandrew
23245614Sandrew// StackDepot efficiently stores huge amounts of stack traces.
24276789Sdimstruct StackDepotNode;
25276789Sdimstruct StackDepotHandle {
26276789Sdim  StackDepotNode *node_;
27296417Sdim  StackDepotHandle() : node_(nullptr) {}
28276789Sdim  explicit StackDepotHandle(StackDepotNode *node) : node_(node) {}
29276789Sdim  bool valid() { return node_; }
30276789Sdim  u32 id();
31276789Sdim  int use_count();
32276789Sdim  void inc_use_count_unsafe();
33276789Sdim};
34245614Sandrew
35276789Sdimconst int kStackDepotMaxUseCount = 1U << 20;
36245614Sandrew
37245614SandrewStackDepotStats *StackDepotGetStats();
38276789Sdimu32 StackDepotPut(StackTrace stack);
39276789SdimStackDepotHandle StackDepotPut_WithHandle(StackTrace stack);
40276789Sdim// Retrieves a stored stack trace by the id.
41276789SdimStackTrace StackDepotGet(u32 id);
42245614Sandrew
43276789Sdimvoid StackDepotLockAll();
44276789Sdimvoid StackDepotUnlockAll();
45274201Sdim
46274201Sdim// Instantiating this class creates a snapshot of StackDepot which can be
47274201Sdim// efficiently queried with StackDepotGet(). You can use it concurrently with
48274201Sdim// StackDepot, but the snapshot is only guaranteed to contain those stack traces
49274201Sdim// which were stored before it was instantiated.
50274201Sdimclass StackDepotReverseMap {
51274201Sdim public:
52274201Sdim  StackDepotReverseMap();
53276789Sdim  StackTrace Get(u32 id);
54274201Sdim
55274201Sdim private:
56274201Sdim  struct IdDescPair {
57274201Sdim    u32 id;
58276789Sdim    StackDepotNode *desc;
59274201Sdim
60274201Sdim    static bool IdComparator(const IdDescPair &a, const IdDescPair &b);
61274201Sdim  };
62274201Sdim
63274201Sdim  InternalMmapVector<IdDescPair> map_;
64274201Sdim
65274201Sdim  // Disallow evil constructors.
66274201Sdim  StackDepotReverseMap(const StackDepotReverseMap&);
67274201Sdim  void operator=(const StackDepotReverseMap&);
68274201Sdim};
69276789Sdim
70296417Sdim} // namespace __sanitizer
71245614Sandrew
72296417Sdim#endif // SANITIZER_STACKDEPOT_H
73