| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2388a40 commit 5127c70
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 5 | 5 | ||
| 6 | 6 | #include "debug_utils.h" | |
| 7 | + #include "env.h" | ||
| 7 | 8 | ||
| 8 | 9 | #include <type_traits> | |
| 9 | 10 | ||
@@ -90,6 +91,79 @@ void COLD_NOINLINE FPrintF(FILE* file, const char* format, Args&&... args) { | |||
| 90 | 91 | FWrite(file, SPrintF(format, std::forward<Args>(args)...)); | |
| 91 | 92 | } | |
| 92 | 93 | ||
| 94 | + template <typename... Args> | ||
| 95 | + inline void FORCE_INLINE Debug(EnabledDebugList* list, | ||
| 96 | + DebugCategory cat, | ||
| 97 | + const char* format, | ||
| 98 | + Args&&... args) { | ||
| 99 | + if (!UNLIKELY(list->enabled(cat))) return; | ||
| 100 | + FPrintF(stderr, format, std::forward<Args>(args)...); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + inline void FORCE_INLINE Debug(EnabledDebugList* list, | ||
| 104 | + DebugCategory cat, | ||
| 105 | + const char* message) { | ||
| 106 | + if (!UNLIKELY(list->enabled(cat))) return; | ||
| 107 | + FPrintF(stderr, "%s", message); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + template <typename... Args> | ||
| 111 | + inline void FORCE_INLINE | ||
| 112 | + Debug(Environment* env, DebugCategory cat, const char* format, Args&&... args) { | ||
| 113 | + Debug(env->enabled_debug_list(), cat, format, std::forward<Args>(args)...); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + inline void FORCE_INLINE Debug(Environment* env, | ||
| 117 | + DebugCategory cat, | ||
| 118 | + const char* message) { | ||
| 119 | + Debug(env->enabled_debug_list(), cat, message); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + template <typename... Args> | ||
| 123 | + inline void Debug(Environment* env, | ||
| 124 | + DebugCategory cat, | ||
| 125 | + const std::string& format, | ||
| 126 | + Args&&... args) { | ||
| 127 | + Debug(env->enabled_debug_list(), | ||
| 128 | + cat, | ||
| 129 | + format.c_str(), | ||
| 130 | + std::forward<Args>(args)...); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + // Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that | ||
| 134 | + // the FORCE_INLINE flag on them doesn't apply to the contents of this function | ||
| 135 | + // as well. | ||
| 136 | + // We apply COLD_NOINLINE to tell the compiler that it's not worth optimizing | ||
| 137 | + // this function for speed and it should rather focus on keeping it out of | ||
| 138 | + // hot code paths. In particular, we want to keep the string concatenating code | ||
| 139 | + // out of the function containing the original `Debug()` call. | ||
| 140 | + template <typename... Args> | ||
| 141 | + void COLD_NOINLINE UnconditionalAsyncWrapDebug(AsyncWrap* async_wrap, | ||
| 142 | + const char* format, | ||
| 143 | + Args&&... args) { | ||
| 144 | + Debug(async_wrap->env(), | ||
| 145 | + static_cast<DebugCategory>(async_wrap->provider_type()), | ||
| 146 | + async_wrap->diagnostic_name() + " " + format + "\n", | ||
| 147 | + std::forward<Args>(args)...); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + template <typename... Args> | ||
| 151 | + inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, | ||
| 152 | + const char* format, | ||
| 153 | + Args&&... args) { | ||
| 154 | + DCHECK_NOT_NULL(async_wrap); | ||
| 155 | + DebugCategory cat = static_cast<DebugCategory>(async_wrap->provider_type()); | ||
| 156 | + if (!UNLIKELY(async_wrap->env()->enabled_debug_list()->enabled(cat))) return; | ||
| 157 | + UnconditionalAsyncWrapDebug(async_wrap, format, std::forward<Args>(args)...); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + template <typename... Args> | ||
| 161 | + inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, | ||
| 162 | + const std::string& format, | ||
| 163 | + Args&&... args) { | ||
| 164 | + Debug(async_wrap, format.c_str(), std::forward<Args>(args)...); | ||
| 165 | + } | ||
| 166 | + | ||
| 93 | 167 | } // namespace node | |
| 94 | 168 | ||
| 95 | 169 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | #include "debug_utils-inl.h" // NOLINT(build/include) | |
| 2 | 2 | #include "env-inl.h" | |
| 3 | + #include "node_internals.h" | ||
| 3 | 4 | ||
| 4 | 5 | #ifdef __POSIX__ | |
| 5 | 6 | #if defined(__linux__) | |
@@ -54,6 +55,34 @@ | |||
| 54 | 55 | ||
| 55 | 56 | namespace node { | |
| 56 | 57 | ||
| 58 | + void EnabledDebugList::Parse(Environment* env) { | ||
| 59 | + std::string cats; | ||
| 60 | + credentials::SafeGetenv("NODE_DEBUG_NATIVE", &cats, env); | ||
| 61 | + Parse(cats, true); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + void EnabledDebugList::Parse(const std::string& cats, bool enabled) { | ||
| 65 | + std::string debug_categories = cats; | ||
| 66 | + while (!debug_categories.empty()) { | ||
| 67 | + std::string::size_type comma_pos = debug_categories.find(','); | ||
| 68 | + std::string wanted = ToLower(debug_categories.substr(0, comma_pos)); | ||
| 69 | + | ||
| 70 | + #define V(name) \ | ||
| 71 | + { \ | ||
| 72 | + static const std::string available_category = ToLower(#name); \ | ||
| 73 | + if (available_category.find(wanted) != std::string::npos) \ | ||
| 74 | + set_enabled(DebugCategory::name, enabled); \ | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + DEBUG_CATEGORY_NAMES(V) | ||
| 78 | + #undef V | ||
| 79 | + | ||
| 80 | + if (comma_pos == std::string::npos) break; | ||
| 81 | + // Use everything after the `,` as the list for the next iteration. | ||
| 82 | + debug_categories = debug_categories.substr(comma_pos + 1); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 57 | 86 | #ifdef __POSIX__ | |
| 58 | 87 | #if HAVE_EXECINFO_H | |
| 59 | 88 | class PosixSymbolDebuggingContext final : public NativeSymbolDebuggingContext { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,6 @@ | |||
| 4 | 4 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 5 | 5 | ||
| 6 | 6 | #include "async_wrap.h" | |
| 7 | - #include "env.h" | ||
| 8 | 7 | ||
| 9 | 8 | #include <sstream> | |
| 10 | 9 | #include <string> | |
@@ -21,6 +20,7 @@ | |||
| 21 | 20 | #endif | |
| 22 | 21 | ||
| 23 | 22 | namespace node { | |
| 23 | + class Environment; | ||
| 24 | 24 | ||
| 25 | 25 | template <typename T> | |
| 26 | 26 | inline std::string ToString(const T& value); | |
@@ -36,31 +36,71 @@ template <typename... Args> | |||
| 36 | 36 | inline void FPrintF(FILE* file, const char* format, Args&&... args); | |
| 37 | 37 | void FWrite(FILE* file, const std::string& str); | |
| 38 | 38 | ||
| 39 | + // Listing the AsyncWrap provider types first enables us to cast directly | ||
| 40 | + // from a provider type to a debug category. | ||
| 41 | + #define DEBUG_CATEGORY_NAMES(V) \ | ||
| 42 | + NODE_ASYNC_PROVIDER_TYPES(V) \ | ||
| 43 | + V(INSPECTOR_SERVER) \ | ||
| 44 | + V(INSPECTOR_PROFILER) \ | ||
| 45 | + V(WASI) | ||
| 46 | + | ||
| 47 | + enum class DebugCategory { | ||
| 48 | + #define V(name) name, | ||
| 49 | + DEBUG_CATEGORY_NAMES(V) | ||
| 50 | + #undef V | ||
| 51 | + CATEGORY_COUNT | ||
| 52 | + }; | ||
| 53 | + | ||
| 54 | + class EnabledDebugList { | ||
| 55 | + public: | ||
| 56 | + bool enabled(DebugCategory category) const { | ||
| 57 | + DCHECK_GE(static_cast<int>(category), 0); | ||
| 58 | + DCHECK_LT(static_cast<int>(category), | ||
| 59 | + static_cast<int>(DebugCategory::CATEGORY_COUNT)); | ||
| 60 | + return enabled_[static_cast<int>(category)]; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + // Uses NODE_DEBUG_NATIVE to initialize the categories. When env is not a | ||
| 64 | + // nullptr, the environment variables set in the Environment are used. | ||
| 65 | + // Otherwise the system environment variables are used. | ||
| 66 | + void Parse(Environment* env); | ||
| 67 | + | ||
| 68 | + private: | ||
| 69 | + // Set all categories matching cats to the value of enabled. | ||
| 70 | + void Parse(const std::string& cats, bool enabled); | ||
| 71 | + void set_enabled(DebugCategory category, bool enabled) { | ||
| 72 | + DCHECK_GE(static_cast<int>(category), 0); | ||
| 73 | + DCHECK_LT(static_cast<int>(category), | ||
| 74 | + static_cast<int>(DebugCategory::CATEGORY_COUNT)); | ||
| 75 | + enabled_[static_cast<int>(category)] = true; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + bool enabled_[static_cast<int>(DebugCategory::CATEGORY_COUNT)] = {false}; | ||
| 79 | + }; | ||
| 80 | + | ||
| 39 | 81 | template <typename... Args> | |
| 40 | - inline void FORCE_INLINE Debug(Environment* env, | ||
| 82 | + inline void FORCE_INLINE Debug(EnabledDebugList* list, | ||
| 41 | 83 | DebugCategory cat, | |
| 42 | 84 | const char* format, | |
| 43 | - Args&&... args) { | ||
| 44 | - if (!UNLIKELY(env->debug_enabled(cat))) | ||
| 45 | - return; | ||
| 46 | - FPrintF(stderr, format, std::forward<Args>(args)...); | ||
| 47 | - } | ||
| 85 | + Args&&... args); | ||
| 86 | + | ||
| 87 | + inline void FORCE_INLINE Debug(EnabledDebugList* list, | ||
| 88 | + DebugCategory cat, | ||
| 89 | + const char* message); | ||
| 90 | + | ||
| 91 | + template <typename... Args> | ||
| 92 | + inline void FORCE_INLINE | ||
| 93 | + Debug(Environment* env, DebugCategory cat, const char* format, Args&&... args); | ||
| 48 | 94 | ||
| 49 | 95 | inline void FORCE_INLINE Debug(Environment* env, | |
| 50 | 96 | DebugCategory cat, | |
| 51 | - const char* message) { | ||
| 52 | - if (!UNLIKELY(env->debug_enabled(cat))) | ||
| 53 | - return; | ||
| 54 | - FPrintF(stderr, "%s", message); | ||
| 55 | - } | ||
| 97 | + const char* message); | ||
| 56 | 98 | ||
| 57 | 99 | template <typename... Args> | |
| 58 | 100 | inline void Debug(Environment* env, | |
| 59 | 101 | DebugCategory cat, | |
| 60 | 102 | const std::string& format, | |
| 61 | - Args&&... args) { | ||
| 62 | - Debug(env, cat, format.c_str(), std::forward<Args>(args)...); | ||
| 63 | - } | ||
| 103 | + Args&&... args); | ||
| 64 | 104 | ||
| 65 | 105 | // Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that | |
| 66 | 106 | // the FORCE_INLINE flag on them doesn't apply to the contents of this function | |
@@ -72,31 +112,17 @@ inline void Debug(Environment* env, | |||
| 72 | 112 | template <typename... Args> | |
| 73 | 113 | void COLD_NOINLINE UnconditionalAsyncWrapDebug(AsyncWrap* async_wrap, | |
| 74 | 114 | const char* format, | |
| 75 | - Args&&... args) { | ||
| 76 | - Debug(async_wrap->env(), | ||
| 77 | - static_cast<DebugCategory>(async_wrap->provider_type()), | ||
| 78 | - async_wrap->diagnostic_name() + " " + format + "\n", | ||
| 79 | - std::forward<Args>(args)...); | ||
| 80 | - } | ||
| 115 | + Args&&... args); | ||
| 81 | 116 | ||
| 82 | 117 | template <typename... Args> | |
| 83 | 118 | inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, | |
| 84 | 119 | const char* format, | |
| 85 | - Args&&... args) { | ||
| 86 | - DCHECK_NOT_NULL(async_wrap); | ||
| 87 | - DebugCategory cat = | ||
| 88 | - static_cast<DebugCategory>(async_wrap->provider_type()); | ||
| 89 | - if (!UNLIKELY(async_wrap->env()->debug_enabled(cat))) | ||
| 90 | - return; | ||
| 91 | - UnconditionalAsyncWrapDebug(async_wrap, format, std::forward<Args>(args)...); | ||
| 92 | - } | ||
| 120 | + Args&&... args); | ||
| 93 | 121 | ||
| 94 | 122 | template <typename... Args> | |
| 95 | 123 | inline void FORCE_INLINE Debug(AsyncWrap* async_wrap, | |
| 96 | 124 | const std::string& format, | |
| 97 | - Args&&... args) { | ||
| 98 | - Debug(async_wrap, format.c_str(), std::forward<Args>(args)...); | ||
| 99 | - } | ||
| 125 | + Args&&... args); | ||
| 100 | 126 | ||
| 101 | 127 | // Debug helper for inspecting the currently running `node` executable. | |
| 102 | 128 | class NativeSymbolDebuggingContext { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -606,20 +606,6 @@ inline void Environment::set_http2_state( | |||
| 606 | 606 | http2_state_ = std::move(buffer); | |
| 607 | 607 | } | |
| 608 | 608 | ||
| 609 | - bool Environment::debug_enabled(DebugCategory category) const { | ||
| 610 | - DCHECK_GE(static_cast<int>(category), 0); | ||
| 611 | - DCHECK_LT(static_cast<int>(category), | ||
| 612 | - static_cast<int>(DebugCategory::CATEGORY_COUNT)); | ||
| 613 | - return debug_enabled_[static_cast<int>(category)]; | ||
| 614 | - } | ||
| 615 | - | ||
| 616 | - void Environment::set_debug_enabled(DebugCategory category, bool enabled) { | ||
| 617 | - DCHECK_GE(static_cast<int>(category), 0); | ||
| 618 | - DCHECK_LT(static_cast<int>(category), | ||
| 619 | - static_cast<int>(DebugCategory::CATEGORY_COUNT)); | ||
| 620 | - debug_enabled_[static_cast<int>(category)] = enabled; | ||
| 621 | - } | ||
| 622 | - | ||
| 623 | 609 | inline AliasedFloat64Array* Environment::fs_stats_field_array() { | |
| 624 | 610 | return &fs_stats_field_array_; | |
| 625 | 611 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | #include "env.h" | |
| 2 | 2 | ||
| 3 | 3 | #include "async_wrap.h" | |
| 4 | + #include "debug_utils-inl.h" | ||
| 4 | 5 | #include "memory_tracker-inl.h" | |
| 5 | 6 | #include "node_buffer.h" | |
| 6 | 7 | #include "node_context_data.h" | |
@@ -315,6 +316,7 @@ Environment::Environment(IsolateData* isolate_data, | |||
| 315 | 316 | Context::Scope context_scope(context); | |
| 316 | 317 | ||
| 317 | 318 | set_env_vars(per_process::system_environment); | |
| 319 | + enabled_debug_list_.Parse(this); | ||
| 318 | 320 | ||
| 319 | 321 | // We create new copies of the per-Environment option sets, so that it is | |
| 320 | 322 | // easier to modify them after Environment creation. The defaults are | |
@@ -375,10 +377,6 @@ Environment::Environment(IsolateData* isolate_data, | |||
| 375 | 377 | // By default, always abort when --abort-on-uncaught-exception was passed. | |
| 376 | 378 | should_abort_on_uncaught_toggle_[0] = 1; | |
| 377 | 379 | ||
| 378 | - std::string debug_cats; | ||
| 379 | - credentials::SafeGetenv("NODE_DEBUG_NATIVE", &debug_cats, this); | ||
| 380 | - set_debug_categories(debug_cats, true); | ||
| 381 | - | ||
| 382 | 380 | if (options_->no_force_async_hooks_checks) { | |
| 383 | 381 | async_hooks_.no_force_checks(); | |
| 384 | 382 | } | |
@@ -867,29 +865,6 @@ Local<Value> Environment::GetNow() { | |||
| 867 | 865 | return Number::New(isolate(), static_cast<double>(now)); | |
| 868 | 866 | } | |
| 869 | 867 | ||
| 870 | - void Environment::set_debug_categories(const std::string& cats, bool enabled) { | ||
| 871 | - std::string debug_categories = cats; | ||
| 872 | - while (!debug_categories.empty()) { | ||
| 873 | - std::string::size_type comma_pos = debug_categories.find(','); | ||
| 874 | - std::string wanted = ToLower(debug_categories.substr(0, comma_pos)); | ||
| 875 | - | ||
| 876 | - #define V(name) \ | ||
| 877 | - { \ | ||
| 878 | - static const std::string available_category = ToLower(#name); \ | ||
| 879 | - if (available_category.find(wanted) != std::string::npos) \ | ||
| 880 | - set_debug_enabled(DebugCategory::name, enabled); \ | ||
| 881 | - } | ||
| 882 | - | ||
| 883 | - DEBUG_CATEGORY_NAMES(V) | ||
| 884 | - #undef V | ||
| 885 | - | ||
| 886 | - if (comma_pos == std::string::npos) | ||
| 887 | - break; | ||
| 888 | - // Use everything after the `,` as the list for the next iteration. | ||
| 889 | - debug_categories = debug_categories.substr(comma_pos + 1); | ||
| 890 | - } | ||
| 891 | - } | ||
| 892 | - | ||
| 893 | 868 | void CollectExceptionInfo(Environment* env, | |
| 894 | 869 | Local<Object> obj, | |
| 895 | 870 | int errorno, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ | |||
| 29 | 29 | #include "inspector_agent.h" | |
| 30 | 30 | #include "inspector_profiler.h" | |
| 31 | 31 | #endif | |
| 32 | + #include "debug_utils.h" | ||
| 32 | 33 | #include "handle_wrap.h" | |
| 33 | 34 | #include "node.h" | |
| 34 | 35 | #include "node_binding.h" | |
@@ -550,20 +551,7 @@ struct ContextInfo { | |||
| 550 | 551 | bool is_default = false; | |
| 551 | 552 | }; | |
| 552 | 553 | ||
| 553 | - // Listing the AsyncWrap provider types first enables us to cast directly | ||
| 554 | - // from a provider type to a debug category. | ||
| 555 | - #define DEBUG_CATEGORY_NAMES(V) \ | ||
| 556 | - NODE_ASYNC_PROVIDER_TYPES(V) \ | ||
| 557 | - V(INSPECTOR_SERVER) \ | ||
| 558 | - V(INSPECTOR_PROFILER) \ | ||
| 559 | - V(WASI) | ||
| 560 | - | ||
| 561 | - enum class DebugCategory { | ||
| 562 | - #define V(name) name, | ||
| 563 | - DEBUG_CATEGORY_NAMES(V) | ||
| 564 | - #undef V | ||
| 565 | - CATEGORY_COUNT | ||
| 566 | - }; | ||
| 554 | + class EnabledDebugList; | ||
| 567 | 555 | ||
| 568 | 556 | // A unique-pointer-ish object that is compatible with the JS engine's | |
| 569 | 557 | // ArrayBuffer::Allocator. | |
@@ -1024,9 +1012,7 @@ class Environment : public MemoryRetainer { | |||
| 1024 | 1012 | inline http2::Http2State* http2_state() const; | |
| 1025 | 1013 | inline void set_http2_state(std::unique_ptr<http2::Http2State> state); | |
| 1026 | 1014 | ||
| 1027 | - inline bool debug_enabled(DebugCategory category) const; | ||
| 1028 | - inline void set_debug_enabled(DebugCategory category, bool enabled); | ||
| 1029 | - void set_debug_categories(const std::string& cats, bool enabled); | ||
| 1015 | + EnabledDebugList* enabled_debug_list() { return &enabled_debug_list_; } | ||
| 1030 | 1016 | ||
| 1031 | 1017 | inline AliasedFloat64Array* fs_stats_field_array(); | |
| 1032 | 1018 | inline AliasedBigUint64Array* fs_stats_field_bigint_array(); | |
@@ -1382,9 +1368,7 @@ class Environment : public MemoryRetainer { | |||
| 1382 | 1368 | bool http_parser_buffer_in_use_ = false; | |
| 1383 | 1369 | std::unique_ptr<http2::Http2State> http2_state_; | |
| 1384 | 1370 | ||
| 1385 | - bool debug_enabled_[static_cast<int>(DebugCategory::CATEGORY_COUNT)] = { | ||
| 1386 | - false}; | ||
| 1387 | - | ||
| 1371 | + EnabledDebugList enabled_debug_list_; | ||
| 1388 | 1372 | AliasedFloat64Array fs_stats_field_array_; | |
| 1389 | 1373 | AliasedBigUint64Array fs_stats_field_bigint_array_; | |
| 1390 | 1374 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments