/Users/deen/code/yugabyte-db/src/yb/util/debug-util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_UTIL_DEBUG_UTIL_H |
33 | | #define YB_UTIL_DEBUG_UTIL_H |
34 | | |
35 | | #include <sys/types.h> |
36 | | |
37 | | #include <functional> |
38 | | #include <string> |
39 | | #include <vector> |
40 | | |
41 | | #include "yb/gutil/strings/fastmem.h" |
42 | | |
43 | | #include "yb/util/enums.h" |
44 | | #include "yb/util/slice.h" |
45 | | #include "yb/util/stack_trace.h" |
46 | | |
47 | | namespace yb { |
48 | | |
49 | | // Return a list of all of the thread IDs currently running in this process. |
50 | | // Not async-safe. |
51 | | Status ListThreads(std::vector<pid_t>* tids); |
52 | | |
53 | | // Return the stack trace of the given thread, stringified and symbolized. |
54 | | // |
55 | | // Note that the symbolization happens on the calling thread, not the target |
56 | | // thread, so this is relatively low-impact on the target. |
57 | | // |
58 | | // This is safe to use against the current thread, the main thread, or any other |
59 | | // thread. It requires that the target thread has not blocked POSIX signals. If |
60 | | // it has, an error message will be returned. |
61 | | // |
62 | | // This function is thread-safe but coarsely synchronized: only one "dumper" thread |
63 | | // may be active at a time. |
64 | | std::string DumpThreadStack(ThreadIdForStack tid); |
65 | | |
66 | | // Return the current stack trace, stringified. |
67 | | std::string GetStackTrace( |
68 | | StackTraceLineFormat source_file_path_format = StackTraceLineFormat::DEFAULT, |
69 | | int num_top_frames_to_skip = 0); |
70 | | |
71 | | // Return the current stack trace without the top frame. Useful for implementing functions that |
72 | | // themselves have a "get/print stack trace at the call site" semantics. This is equivalent to |
73 | | // calling GetStackTrace(StackTraceLineFormat::DEFAULT, 1). |
74 | 0 | inline std::string GetStackTraceWithoutTopFrame() { |
75 | 0 | // Note that here we specify num_top_frames_to_skip = 2, because we don't want either this |
76 | 0 | // function itself or its _caller_ to show up in the stack trace. |
77 | 0 | return GetStackTrace(StackTraceLineFormat::DEFAULT, /* num_top_frames_to_skip */ 2); |
78 | 0 | } |
79 | | |
80 | | // Return the current stack trace, in hex form. This is significantly |
81 | | // faster than GetStackTrace() above, so should be used in performance-critical |
82 | | // places like TRACE() calls. If you really need blazing-fast speed, though, |
83 | | // use HexStackTraceToString() into a stack-allocated buffer instead -- |
84 | | // this call causes a heap allocation for the std::string. |
85 | | // |
86 | | // Note that this is much more useful in the context of a static binary, |
87 | | // since addr2line wouldn't know where shared libraries were mapped at |
88 | | // runtime. |
89 | | // |
90 | | // NOTE: This inherits the same async-safety issue as HexStackTraceToString() |
91 | | std::string GetStackTraceHex(); |
92 | | |
93 | | // This is the same as GetStackTraceHex(), except multi-line in a format that |
94 | | // looks very similar to GetStackTrace() but without symbols. |
95 | | std::string GetLogFormatStackTraceHex(); |
96 | | |
97 | | // Collect the current stack trace in hex form into the given buffer. |
98 | | // |
99 | | // The resulting trace just includes the hex addresses, space-separated. This is suitable |
100 | | // for later stringification by pasting into 'addr2line' for example. |
101 | | // |
102 | | // This function is not async-safe, since it uses the libc backtrace() function which |
103 | | // may invoke the dynamic loader. |
104 | | void HexStackTraceToString(char* buf, size_t size); |
105 | | |
106 | 0 | constexpr bool IsDebug() { |
107 | 0 | #ifdef NDEBUG |
108 | 0 | return false; |
109 | 0 | #else |
110 | 0 | return true; |
111 | 0 | #endif |
112 | 0 | } |
113 | | |
114 | | class ScopeLogger { |
115 | | public: |
116 | | ScopeLogger(const std::string& msg, std::function<void()> on_scope_bounds); |
117 | | |
118 | | ~ScopeLogger(); |
119 | | |
120 | | private: |
121 | | const std::string msg_; |
122 | | std::function<void()> on_scope_bounds_; |
123 | | }; |
124 | | |
125 | | std::string SymbolizeAddress( |
126 | | void *pc, |
127 | | const StackTraceLineFormat stack_trace_line_format = StackTraceLineFormat::DEFAULT); |
128 | | |
129 | | // Demangle a C++-mangled identifier. |
130 | | std::string DemangleName(const char* name); |
131 | | |
132 | | #define TEST_PAUSE_IF_FLAG(flag_name) \ |
133 | 99.4M | if (PREDICT_FALSE(ANNOTATE_UNPROTECTED_READ(BOOST_PP_CAT(FLAGS_, flag_name)))) { \ |
134 | 8 | LOG(INFO) << "Pausing due to flag " << #flag_name; \ |
135 | 191k | do { \ |
136 | 191k | SleepFor(MonoDelta::FromMilliseconds(100)); \ |
137 | 191k | } while (ANNOTATE_UNPROTECTED_READ(BOOST_PP_CAT(FLAGS_, flag_name))); \ |
138 | 8 | LOG(INFO) << "Resuming due to flag " << #flag_name; \ |
139 | 8 | } |
140 | | |
141 | | } // namespace yb |
142 | | |
143 | | #endif // YB_UTIL_DEBUG_UTIL_H |