1//===-- memprof_thread.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of MemProfiler, a memory profiler.
10//
11// MemProf-private header for memprof_thread.cpp.
12//===----------------------------------------------------------------------===//
13
14#ifndef MEMPROF_THREAD_H
15#define MEMPROF_THREAD_H
16
17#include "memprof_allocator.h"
18#include "memprof_internal.h"
19#include "memprof_stats.h"
20#include "sanitizer_common/sanitizer_common.h"
21#include "sanitizer_common/sanitizer_libc.h"
22#include "sanitizer_common/sanitizer_thread_registry.h"
23
24namespace __sanitizer {
25struct DTLS;
26} // namespace __sanitizer
27
28namespace __memprof {
29
30class MemprofThread;
31
32// These objects are created for every thread and are never deleted,
33// so we can find them by tid even if the thread is long dead.
34struct MemprofThreadContext final : public ThreadContextBase {
35 explicit MemprofThreadContext(int tid)
36 : ThreadContextBase(tid), announced(false),
37 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
38 thread(nullptr) {}
39 bool announced;
40 u8 destructor_iterations;
41 u32 stack_id;
42 MemprofThread *thread;
43
44 void OnCreated(void *arg) override;
45 void OnFinished() override;
46
47 struct CreateThreadContextArgs {
48 MemprofThread *thread;
49 StackTrace *stack;
50 };
51};
52
53// MemprofThreadContext objects are never freed, so we need many of them.
54COMPILER_CHECK(sizeof(MemprofThreadContext) <= 256);
55
56// MemprofThread are stored in TSD and destroyed when the thread dies.
57class MemprofThread {
58public:
59 static MemprofThread *Create(thread_callback_t start_routine, void *arg,
60 u32 parent_tid, StackTrace *stack,
61 bool detached);
62 static void TSDDtor(void *tsd);
63 void Destroy();
64
65 struct InitOptions;
66 void Init(const InitOptions *options = nullptr);
67
68 thread_return_t ThreadStart(tid_t os_id,
69 atomic_uintptr_t *signal_thread_is_registered);
70
71 uptr stack_top();
72 uptr stack_bottom();
73 uptr stack_size();
74 uptr tls_begin() { return tls_begin_; }
75 uptr tls_end() { return tls_end_; }
76 DTLS *dtls() { return dtls_; }
77 u32 tid() { return context_->tid; }
78 MemprofThreadContext *context() { return context_; }
79 void set_context(MemprofThreadContext *context) { context_ = context; }
80
81 bool AddrIsInStack(uptr addr);
82
83 // True is this thread is currently unwinding stack (i.e. collecting a stack
84 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
85 // malloc internally. See PR17116 for more details.
86 bool isUnwinding() const { return unwinding_; }
87 void setUnwinding(bool b) { unwinding_ = b; }
88
89 MemprofThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
90 MemprofStats &stats() { return stats_; }
91
92private:
93 // NOTE: There is no MemprofThread constructor. It is allocated
94 // via mmap() and *must* be valid in zero-initialized state.
95
96 void SetThreadStackAndTls(const InitOptions *options);
97
98 struct StackBounds {
99 uptr bottom;
100 uptr top;
101 };
102 StackBounds GetStackBounds() const;
103
104 MemprofThreadContext *context_;
105 thread_callback_t start_routine_;
106 void *arg_;
107
108 uptr stack_top_;
109 uptr stack_bottom_;
110
111 uptr tls_begin_;
112 uptr tls_end_;
113 DTLS *dtls_;
114
115 MemprofThreadLocalMallocStorage malloc_storage_;
116 MemprofStats stats_;
117 bool unwinding_;
118};
119
120// Returns a single instance of registry.
121ThreadRegistry &memprofThreadRegistry();
122
123// Must be called under ThreadRegistryLock.
124MemprofThreadContext *GetThreadContextByTidLocked(u32 tid);
125
126// Get the current thread. May return 0.
127MemprofThread *GetCurrentThread();
128void SetCurrentThread(MemprofThread *t);
129u32 GetCurrentTidOrInvalid();
130
131// Used to handle fork().
132void EnsureMainThreadIDIsCorrect();
133} // namespace __memprof
134
135#endif // MEMPROF_THREAD_H
136