GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: debug_utils.h Lines: 7 10 70.0 %
Date: 2022-06-17 04:15:42 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_DEBUG_UTILS_H_
2
#define SRC_DEBUG_UTILS_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "async_wrap.h"
7
8
#include <algorithm>
9
#include <sstream>
10
#include <string>
11
12
// Use FORCE_INLINE on functions that have a debug-category-enabled check first
13
// and then ideally only a single function call following it, to maintain
14
// performance for the common case (no debugging used).
15
#ifdef __GNUC__
16
#define FORCE_INLINE __attribute__((always_inline))
17
#define COLD_NOINLINE __attribute__((cold, noinline))
18
#else
19
#define FORCE_INLINE
20
#define COLD_NOINLINE
21
#endif
22
23
namespace node {
24
class Environment;
25
26
template <typename T>
27
inline std::string ToString(const T& value);
28
29
// C++-style variant of sprintf()/fprintf() that:
30
// - Returns an std::string
31
// - Handles \0 bytes correctly
32
// - Supports %p and %s. %d, %i and %u are aliases for %s.
33
// - Accepts any class that has a ToString() method for stringification.
34
template <typename... Args>
35
inline std::string SPrintF(const char* format, Args&&... args);
36
template <typename... Args>
37
inline void FPrintF(FILE* file, const char* format, Args&&... args);
38
void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
39
40
// Listing the AsyncWrap provider types first enables us to cast directly
41
// from a provider type to a debug category.
42
#define DEBUG_CATEGORY_NAMES(V)                                                \
43
  NODE_ASYNC_PROVIDER_TYPES(V)                                                 \
44
  V(DIAGNOSTICS)                                                               \
45
  V(HUGEPAGES)                                                                 \
46
  V(INSPECTOR_SERVER)                                                          \
47
  V(INSPECTOR_PROFILER)                                                        \
48
  V(CODE_CACHE)                                                                \
49
  V(NGTCP2_DEBUG)                                                              \
50
  V(WASI)                                                                      \
51
  V(MKSNAPSHOT)
52
53
enum class DebugCategory {
54
#define V(name) name,
55
  DEBUG_CATEGORY_NAMES(V)
56
#undef V
57
      CATEGORY_COUNT
58
};
59
60
class NODE_EXTERN_PRIVATE EnabledDebugList {
61
 public:
62
2217665
  bool enabled(DebugCategory category) const {
63
    DCHECK_GE(static_cast<int>(category), 0);
64
    DCHECK_LT(static_cast<int>(category),
65
              static_cast<int>(DebugCategory::CATEGORY_COUNT));
66
2217665
    return enabled_[static_cast<int>(category)];
67
  }
68
69
  // Uses NODE_DEBUG_NATIVE to initialize the categories. When env is not a
70
  // nullptr, the environment variables set in the Environment are used.
71
  // Otherwise the system environment variables are used.
72
  void Parse(Environment* env);
73
74
 private:
75
  // Set all categories matching cats to the value of enabled.
76
  void Parse(const std::string& cats, bool enabled);
77
115
  void set_enabled(DebugCategory category, bool enabled) {
78
    DCHECK_GE(static_cast<int>(category), 0);
79
    DCHECK_LT(static_cast<int>(category),
80
              static_cast<int>(DebugCategory::CATEGORY_COUNT));
81
115
    enabled_[static_cast<int>(category)] = true;
82
115
  }
83
84
  bool enabled_[static_cast<int>(DebugCategory::CATEGORY_COUNT)] = {false};
85
};
86
87
template <typename... Args>
88
inline void FORCE_INLINE Debug(EnabledDebugList* list,
89
                               DebugCategory cat,
90
                               const char* format,
91
                               Args&&... args);
92
93
inline void FORCE_INLINE Debug(EnabledDebugList* list,
94
                               DebugCategory cat,
95
                               const char* message);
96
97
template <typename... Args>
98
inline void FORCE_INLINE
99
Debug(Environment* env, DebugCategory cat, const char* format, Args&&... args);
100
101
inline void FORCE_INLINE Debug(Environment* env,
102
                               DebugCategory cat,
103
                               const char* message);
104
105
template <typename... Args>
106
inline void Debug(Environment* env,
107
                  DebugCategory cat,
108
                  const std::string& format,
109
                  Args&&... args);
110
111
// Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that
112
// the FORCE_INLINE flag on them doesn't apply to the contents of this function
113
// as well.
114
// We apply COLD_NOINLINE to tell the compiler that it's not worth optimizing
115
// this function for speed and it should rather focus on keeping it out of
116
// hot code paths. In particular, we want to keep the string concatenating code
117
// out of the function containing the original `Debug()` call.
118
template <typename... Args>
119
void COLD_NOINLINE UnconditionalAsyncWrapDebug(AsyncWrap* async_wrap,
120
                                               const char* format,
121
                                               Args&&... args);
122
123
template <typename... Args>
124
inline void FORCE_INLINE Debug(AsyncWrap* async_wrap,
125
                               const char* format,
126
                               Args&&... args);
127
128
template <typename... Args>
129
inline void FORCE_INLINE Debug(AsyncWrap* async_wrap,
130
                               const std::string& format,
131
                               Args&&... args);
132
133
// Debug helper for inspecting the currently running `node` executable.
134
class NativeSymbolDebuggingContext {
135
 public:
136
  static std::unique_ptr<NativeSymbolDebuggingContext> New();
137
138
  class SymbolInfo {
139
   public:
140
    std::string name;
141
    std::string filename;
142
    size_t line = 0;
143
    size_t dis = 0;
144
145
    std::string Display() const;
146
  };
147
148
28
  NativeSymbolDebuggingContext() = default;
149
56
  virtual ~NativeSymbolDebuggingContext() = default;
150
151
  virtual SymbolInfo LookupSymbol(void* address) { return {}; }
152
  virtual bool IsMapped(void* address) { return false; }
153
  virtual int GetStackTrace(void** frames, int count) { return 0; }
154
155
  NativeSymbolDebuggingContext(const NativeSymbolDebuggingContext&) = delete;
156
  NativeSymbolDebuggingContext(NativeSymbolDebuggingContext&&) = delete;
157
  NativeSymbolDebuggingContext operator=(NativeSymbolDebuggingContext&)
158
    = delete;
159
  NativeSymbolDebuggingContext operator=(NativeSymbolDebuggingContext&&)
160
    = delete;
161
  static std::vector<std::string> GetLoadedLibraries();
162
};
163
164
// Variant of `uv_loop_close` that tries to be as helpful as possible
165
// about giving information on currently existing handles, if there are any,
166
// but still aborts the process.
167
void CheckedUvLoopClose(uv_loop_t* loop);
168
void PrintLibuvHandleInformation(uv_loop_t* loop, FILE* stream);
169
170
namespace per_process {
171
extern NODE_EXTERN_PRIVATE EnabledDebugList enabled_debug_list;
172
173
template <typename... Args>
174
inline void FORCE_INLINE Debug(DebugCategory cat,
175
                               const char* format,
176
                               Args&&... args);
177
178
inline void FORCE_INLINE Debug(DebugCategory cat, const char* message);
179
}  // namespace per_process
180
}  // namespace node
181
182
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
183
184
#endif  // SRC_DEBUG_UTILS_H_