This commit is contained in:
Radoslav Petkov 2025-02-25 22:11:49 +02:00 committed by GitHub
commit 0001f6c287
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -36,6 +36,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <limits>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -50,83 +51,79 @@ namespace google {
inline namespace glog_internal_namespace_ { inline namespace glog_internal_namespace_ {
// Implementation of fnmatch that does not need 0-termination // Optimized implementation of fnmatch that does not require 0-termination
// of arguments and does not allocate any memory, // of its arguments and does not allocate any memory.
// but we only support "*" and "?" wildcards, not the "[...]" patterns. // It supports only "*" and "?" wildcards.
// It's not a static function for the unittest. // This version is implemented iteratively rather than recursively.
GLOG_NO_EXPORT bool SafeFNMatch_(const char* pattern, size_t patt_len, GLOG_NO_EXPORT bool SafeFNMatch_(const char* pattern, size_t patt_len,
const char* str, size_t str_len) { const char* str, size_t str_len) {
size_t p = 0; size_t p = 0, s = 0;
size_t s = 0; // star_idx holds the index of the last '*' encountered.
while (true) { // match_idx holds the index in str corresponding to that '*' match.
if (p == patt_len && s == str_len) return true; size_t star_idx = std::numeric_limits<size_t>::max();
if (p == patt_len) return false; size_t match_idx = 0;
if (s == str_len) return p + 1 == patt_len && pattern[p] == '*';
if (pattern[p] == str[s] || pattern[p] == '?') { while (s < str_len) {
p += 1; if (p < patt_len && (pattern[p] == str[s] || pattern[p] == '?')) {
s += 1; // Characters match (or we have a '?') so advance both indices.
continue; ++p;
} ++s;
if (pattern[p] == '*') { } else if (p < patt_len && pattern[p] == '*') {
if (p + 1 == patt_len) return true; // Record the position of '*' and the current string index.
do { star_idx = p;
if (SafeFNMatch_(pattern + (p + 1), patt_len - (p + 1), str + s, match_idx = s;
str_len - s)) { ++p;
return true; } else if (star_idx != std::numeric_limits<size_t>::max()) {
} // No direct match, but we have seen a '*' before.
s += 1; // Backtrack: assume '*' matches one more character.
} while (s != str_len); p = star_idx + 1;
s = ++match_idx;
} else {
// No match and no '*' to backtrack to.
return false; return false;
} }
return false;
} }
// Check for remaining '*' in the pattern.
while (p < patt_len && pattern[p] == '*') {
++p;
}
return p == patt_len;
} }
} // namespace glog_internal_namespace_ } // namespace glog_internal_namespace_
using glog_internal_namespace_::SafeFNMatch_; using glog_internal_namespace_::SafeFNMatch_;
// List of per-module log levels from FLAGS_vmodule. // Structure holding per-module logging level info.
// Once created each element is never deleted/modified
// except for the vlog_level: other threads will read VModuleInfo blobs
// w/o locks and we'll store pointers to vlog_level at VLOG locations
// that will never go away.
// We can't use an STL struct here as we wouldn't know
// when it's safe to delete/update it: other threads need to use it w/o locks.
struct VModuleInfo { struct VModuleInfo {
string module_pattern; string module_pattern;
mutable int32 vlog_level; // Conceptually this is an AtomicWord, but it's mutable int32 vlog_level; // Conceptually atomic but kept simple for performance.
// too much work to use AtomicWord type here
// w/o much actual benefit.
const VModuleInfo* next; const VModuleInfo* next;
}; };
// This protects the following global variables. // Global variables controlling per-module logging levels.
static std::mutex vmodule_mutex; static std::mutex vmodule_mutex;
// Pointer to head of the VModuleInfo list.
// It's a map from module pattern to logging level for those module(s).
static VModuleInfo* vmodule_list = nullptr; static VModuleInfo* vmodule_list = nullptr;
static SiteFlag* cached_site_list = nullptr; static SiteFlag* cached_site_list = nullptr;
// Boolean initialization flag.
static bool inited_vmodule = false; static bool inited_vmodule = false;
// L >= vmodule_mutex. // Initializes the module-specific logging levels based on FLAGS_vmodule.
static void VLOG2Initializer() { static void VLOG2Initializer() {
// Can now parse --vmodule flag and initialize mapping of module-specific
// logging levels.
inited_vmodule = false; inited_vmodule = false;
const char* vmodule = FLAGS_vmodule.c_str(); const char* vmodule = FLAGS_vmodule.c_str();
const char* sep;
VModuleInfo* head = nullptr; VModuleInfo* head = nullptr;
VModuleInfo* tail = nullptr; VModuleInfo* tail = nullptr;
while ((sep = strchr(vmodule, '=')) != nullptr) { while (*vmodule != '\0') {
const char* sep = strchr(vmodule, '=');
if (sep == nullptr) break;
string pattern(vmodule, static_cast<size_t>(sep - vmodule)); string pattern(vmodule, static_cast<size_t>(sep - vmodule));
int module_level; int module_level;
if (sscanf(sep, "=%d", &module_level) == 1) { if (sscanf(sep, "=%d", &module_level) == 1) {
auto* info = new VModuleInfo; auto* info = new VModuleInfo;
info->module_pattern = pattern; info->module_pattern = pattern;
info->vlog_level = module_level; info->vlog_level = module_level;
info->next = nullptr;
if (head) { if (head) {
tail->next = info; tail->next = info;
} else { } else {
@ -134,37 +131,36 @@ static void VLOG2Initializer() {
} }
tail = info; tail = info;
} }
// Skip past this entry // Skip past this entry (find the next comma).
vmodule = strchr(sep, ','); vmodule = strchr(sep, ',');
if (vmodule == nullptr) break; if (vmodule == nullptr) break;
vmodule++; // Skip past "," ++vmodule; // Skip the comma.
} }
if (head) { // Put them into the list at the head: if (head) {
tail->next = vmodule_list; tail->next = vmodule_list;
vmodule_list = head; vmodule_list = head;
} }
inited_vmodule = true; inited_vmodule = true;
} }
// This can be called very early, so we use SpinLock and RAW_VLOG here. // Sets the VLOG level for a given module pattern.
int SetVLOGLevel(const char* module_pattern, int log_level) { int SetVLOGLevel(const char* module_pattern, int log_level) {
int result = FLAGS_v; int result = FLAGS_v;
size_t const pattern_len = strlen(module_pattern); const size_t pattern_len = strlen(module_pattern);
bool found = false; bool found = false;
{ {
std::lock_guard<std::mutex> l( std::lock_guard<std::mutex> l(vmodule_mutex);
vmodule_mutex); // protect whole read-modify-write for (const VModuleInfo* info = vmodule_list; info != nullptr; info = info->next) {
for (const VModuleInfo* info = vmodule_list; info != nullptr;
info = info->next) {
if (info->module_pattern == module_pattern) { if (info->module_pattern == module_pattern) {
if (!found) { if (!found) {
result = info->vlog_level; result = info->vlog_level;
found = true; found = true;
} }
info->vlog_level = log_level; info->vlog_level = log_level;
} else if (!found && SafeFNMatch_(info->module_pattern.c_str(), } else if (!found &&
info->module_pattern.size(), SafeFNMatch_(info->module_pattern.c_str(),
module_pattern, pattern_len)) { info->module_pattern.size(),
module_pattern, pattern_len)) {
result = info->vlog_level; result = info->vlog_level;
found = true; found = true;
} }
@ -176,15 +172,12 @@ int SetVLOGLevel(const char* module_pattern, int log_level) {
info->next = vmodule_list; info->next = vmodule_list;
vmodule_list = info; vmodule_list = info;
// Update any cached site flags that match this module.
SiteFlag** item_ptr = &cached_site_list; SiteFlag** item_ptr = &cached_site_list;
SiteFlag* item = cached_site_list; SiteFlag* item = cached_site_list;
// We traverse the list fully because the pattern can match several items
// from the list.
while (item) { while (item) {
if (SafeFNMatch_(module_pattern, pattern_len, item->base_name, if (SafeFNMatch_(module_pattern, pattern_len, item->base_name,
item->base_len)) { item->base_len)) {
// Redirect the cached value to its module override.
item->level = &info->vlog_level; item->level = &info->vlog_level;
*item_ptr = item->next; // Remove the item from the list. *item_ptr = item->next; // Remove the item from the list.
} else { } else {
@ -198,8 +191,7 @@ int SetVLOGLevel(const char* module_pattern, int log_level) {
return result; return result;
} }
// NOTE: Individual VLOG statements cache the integer log level pointers. // Initializes the VLOG site flag and returns whether logging should occur.
// NOTE: This function must not allocate memory or require any locks.
bool InitVLOG3__(SiteFlag* site_flag, int32* level_default, const char* fname, bool InitVLOG3__(SiteFlag* site_flag, int32* level_default, const char* fname,
int32 verbose_level) { int32 verbose_level) {
std::lock_guard<std::mutex> l(vmodule_mutex); std::lock_guard<std::mutex> l(vmodule_mutex);
@ -208,59 +200,40 @@ bool InitVLOG3__(SiteFlag* site_flag, int32* level_default, const char* fname,
VLOG2Initializer(); VLOG2Initializer();
} }
// protect the errno global in case someone writes: // Save errno in case any recoverable error occurs.
// VLOG(..) << "The last error was " << strerror(errno)
int old_errno = errno; int old_errno = errno;
// site_default normally points to FLAGS_v
int32* site_flag_value = level_default; int32* site_flag_value = level_default;
// Get basename for file // Get the base file name (strip directory path).
const char* base = strrchr(fname, '/'); const char* base = strrchr(fname, '/');
#ifdef _WIN32 #ifdef _WIN32
if (!base) { if (!base) {
base = strrchr(fname, '\\'); base = strrchr(fname, '\\');
} }
#endif #endif
base = base ? (base + 1) : fname; base = base ? (base + 1) : fname;
const char* base_end = strchr(base, '.'); const char* base_end = strchr(base, '.');
size_t base_length = size_t base_length = base_end ? static_cast<size_t>(base_end - base) : strlen(base);
base_end ? static_cast<size_t>(base_end - base) : strlen(base);
// Trim out trailing "-inl" if any // Trim any trailing "-inl" if present.
if (base_length >= 4 && (memcmp(base + base_length - 4, "-inl", 4) == 0)) { if (base_length >= 4 && memcmp(base + base_length - 4, "-inl", 4) == 0) {
base_length -= 4; base_length -= 4;
} }
// TODO: Trim out _unittest suffix? Perhaps it is better to have // Search for a matching module override.
// the extra control and just leave it there. for (const VModuleInfo* info = vmodule_list; info != nullptr; info = info->next) {
// find target in vector of modules, replace site_flag_value with
// a module-specific verbose level, if any.
for (const VModuleInfo* info = vmodule_list; info != nullptr;
info = info->next) {
if (SafeFNMatch_(info->module_pattern.c_str(), info->module_pattern.size(), if (SafeFNMatch_(info->module_pattern.c_str(), info->module_pattern.size(),
base, base_length)) { base, base_length)) {
site_flag_value = &info->vlog_level; site_flag_value = &info->vlog_level;
// value at info->vlog_level is now what controls
// the VLOG at the caller site forever
break; break;
} }
} }
// Cache the vlog value pointer if --vmodule flag has been parsed. // Cache the level pointer in the site flag.
ANNOTATE_BENIGN_RACE(site_flag, ANNOTATE_BENIGN_RACE(site_flag,
"*site_flag may be written by several threads," "*site_flag may be written by several threads, but the value will be the same");
" but the value will be the same");
if (read_vmodule_flag) { if (read_vmodule_flag) {
site_flag->level = site_flag_value; site_flag->level = site_flag_value;
// If VLOG flag has been cached to the default site pointer,
// we want to add to the cached list in order to invalidate in case
// SetVModule is called afterwards with new modules.
// The performance penalty here is neglible, because InitVLOG3__ is called
// once per site.
if (site_flag_value == level_default && !site_flag->base_name) { if (site_flag_value == level_default && !site_flag->base_name) {
site_flag->base_name = base; site_flag->base_name = base;
site_flag->base_len = base_length; site_flag->base_len = base_length;
@ -269,8 +242,7 @@ bool InitVLOG3__(SiteFlag* site_flag, int32* level_default, const char* fname,
} }
} }
// restore the errno in case something recoverable went wrong during // Restore errno and return whether logging should proceed.
// the initialization of the VLOG mechanism (see above note "protect the..")
errno = old_errno; errno = old_errno;
return *site_flag_value >= verbose_level; return *site_flag_value >= verbose_level;
} }