From 286f834ce60e4ebcaaf131cb7da97ebf79098fa0 Mon Sep 17 00:00:00 2001 From: Eric Kunze Date: Wed, 22 Jun 2022 11:30:23 -0700 Subject: Simplify the argument parsing and debug code Change-Id: Ia681aa0eb1d42fa31a4658badb376f7b32b2e338 Signed-off-by: Eric Kunze --- reference_model/src/func_debug.cc | 246 ++++++++++++-------------------------- 1 file changed, 77 insertions(+), 169 deletions(-) (limited to 'reference_model/src/func_debug.cc') diff --git a/reference_model/src/func_debug.cc b/reference_model/src/func_debug.cc index f5f045e..755f79f 100644 --- a/reference_model/src/func_debug.cc +++ b/reference_model/src/func_debug.cc @@ -20,6 +20,7 @@ #include #include #include +#include #ifndef _MSC_VER #include @@ -33,6 +34,12 @@ #define MAX_FRAMES 100 +static bool str_case_equal(const std::string& a, const std::string& b) +{ + return std::equal(a.begin(), a.end(), b.begin(), b.end(), + [](char ac, char bc) { return tolower(ac) == tolower(bc); }); +} + #ifndef _MSC_VER pid_t func_print_backtrace_helper(int num_tries, int sig); #endif @@ -144,113 +151,63 @@ void func_enable_signal_handlers() } } -const char* func_debug_mode_str_table[] = { -#define DEBUG_MODE(NAME, BIT) #NAME, -#include "debug_modes.def" -#undef DEBUG_MODE +static const std::vector> func_debug_verbosity_table = { + { "NONE", DEBUG_VERB_NONE }, { "INFO", DEBUG_VERB_INFO }, { "IFACE", DEBUG_VERB_IFACE }, + { "LOW", DEBUG_VERB_LOW }, { "MED", DEBUG_VERB_MED }, { "HIGH", DEBUG_VERB_HIGH } }; -#define DEBUG_MASK_COUNT (sizeof(func_debug_mode_str_table) / sizeof(const char*)) - -const char* func_debug_verbosity_str_table[] = { "NONE", "INFO", "IFACE", "LOW", "MED", "HIGH" }; - -const uint32_t func_debug_verbosity_mask_table[] = { DEBUG_VERB_NONE, DEBUG_VERB_INFO, DEBUG_VERB_IFACE, - DEBUG_VERB_LOW, DEBUG_VERB_MED, DEBUG_VERB_HIGH }; - -#define DEBUG_VERBOSITY_COUNT (sizeof(func_debug_verbosity_str_table) / sizeof(const char*)) - // Initialize the debug mode -int func_init_debug(func_debug_t* func_debug, uint64_t inst_id) +int func_debug_t::init_debug(uint64_t inst_id) { // Set the default debug settings - bzero(func_debug, sizeof(func_debug_t)); - func_debug_set_mask(func_debug, DEBUG_NONE); - func_debug_set_verbosity(func_debug, DEBUG_VERB_NONE); - func_debug_set_inst_mask(func_debug, DEBUG_INST_ALL); - func_debug->func_debug_file = stderr; - func_debug_set_captured_warnings(func_debug, 0); - func_debug_set_output_unbuffered(func_debug, false); - func_debug->inst_id = inst_id; + set_mask(static_cast(DEBUG_NONE)); + set_verbosity(DEBUG_VERB_NONE); + set_inst_mask(DEBUG_INST_ALL); + func_debug_file = stderr; + this->inst_id = inst_id; return 0; } -int func_fini_debug(func_debug_t* func_debug) +int func_debug_t::fini_debug() { - if (func_debug->record_warnings) - { - func_debug_set_captured_warnings(func_debug, 0); - } - -#ifndef _FUNC_INCLUDE_WINDOWS_SUPPORT_H - if (func_debug->is_gzip && func_debug->func_debug_file) - { - pclose(func_debug->func_debug_file); - func_debug->func_debug_file = NULL; - } -#endif - return 0; } -int func_debug_set_file(func_debug_t* func_debug, const char* filename) +int func_debug_t::set_file(const std::string& filename) { - int filenameLen = strlen(filename); - // Open the debug output file - ASSERT(filename != NULL); -#ifndef _FUNC_INCLUDE_WINDOWS_SUPPORT_H - if (filenameLen > 3 && strcmp(filename + filenameLen - 3, ".gz") == 0) - { - char cmd[256]; + func_debug_file = fopen(filename.c_str(), "w"); - snprintf(cmd, sizeof(cmd), "gzip > %s", filename); - func_debug->func_debug_file = popen(cmd, "w"); - func_debug->is_gzip = 1; - } - else - { -#else - { -#endif - func_debug->func_debug_file = fopen(filename, "w"); - } - - if (!func_debug->func_debug_file) + if (!func_debug_file) { perror(NULL); - FATAL_ERROR("Cannot open debug output file: %s\n", filename); + FATAL_ERROR("Cannot open debug output file: %s\n", filename.c_str()); return 1; } - if (func_debug->is_output_unbuffered) + if (is_output_unbuffered) { - setvbuf(func_debug->func_debug_file, nullptr, _IONBF, 0); + setvbuf(func_debug_file, nullptr, _IONBF, 0); } return 0; } -void func_debug_set_verbosity(func_debug_t* func_debug, const char* str) +void func_debug_t::set_verbosity(const std::string& str) { - if (!strcasecmp(str, "RESET")) - { - func_debug_set_verbosity(func_debug, DEBUG_VERB_NONE); - return; - } - - for (size_t i = 0; i < DEBUG_VERBOSITY_COUNT; i++) + for (auto& verb : func_debug_verbosity_table) { - if (!strcasecmp(str, func_debug_verbosity_str_table[i])) + if (str_case_equal(str, verb.first)) { - func_debug_set_verbosity(func_debug, func_debug_verbosity_mask_table[i]); + set_verbosity(verb.second); return; } } - FATAL_ERROR("Invalid debug verbosity: %s", str); + FATAL_ERROR("Invalid debug verbosity: %s", str.c_str()); } -void func_debug_set_verbosity(func_debug_t* func_debug, const uint32_t verb) +void func_debug_t::set_verbosity(const uint32_t verb) { uint32_t new_mask = verb; @@ -278,81 +235,76 @@ void func_debug_set_verbosity(func_debug_t* func_debug, const uint32_t verb) break; } - func_debug->func_debug_verbosity = new_mask; -} - -void func_debug_set_suppress_arch_error_mask(func_debug_t* func_debug, const uint32_t suppress) -{ - func_debug->func_suppress_arch_error_mask = suppress; + func_debug_verbosity = new_mask; } -void func_debug_set_mask(func_debug_t* func_debug, const uint64_t mask) +void func_debug_t::set_mask(const uint64_t mask) { if (mask == DEBUG_NONE) - func_debug->func_debug_mask = mask; + func_debug_mask = mask; else - func_debug->func_debug_mask |= mask; + func_debug_mask |= mask; // Set a minimum verbosity level - if (func_debug->func_debug_verbosity == DEBUG_VERB_NONE) - func_debug->func_debug_verbosity = DEBUG_VERB_INFO; + if (func_debug_verbosity == DEBUG_VERB_NONE) + func_debug_verbosity = DEBUG_VERB_INFO; } -void func_debug_set_inst_mask(func_debug_t* func_debug, const char* mask) +void func_debug_t::set_inst_mask(const char* mask) { uint64_t val; val = strtoul(mask, NULL, 0); - return func_debug_set_inst_mask(func_debug, val); + return set_inst_mask(val); } -void func_debug_set_inst_mask(func_debug_t* func_debug, const uint64_t mask) +void func_debug_t::set_inst_mask(const uint64_t mask) { if (mask == 0) - func_debug->func_debug_inst_mask = DEBUG_INST_ALL; + func_debug_inst_mask = DEBUG_INST_ALL; else - func_debug->func_debug_inst_mask = mask; + func_debug_inst_mask = mask; } -void func_debug_set_mask(func_debug_t* func_debug, const char* str) +std::vector> debug_str_table = { +#define DEBUG_MODE(NAME, BIT) {#NAME, BIT}, +#include "debug_modes.def" +#undef DEBUG_MODE +}; + +void func_debug_t::set_mask(const std::string& str) { - if (!strcasecmp(str, "all")) + if (str == "all") { - func_debug_set_mask(func_debug, UINT64_MAX - 1); + set_mask(UINT64_MAX - 1); return; } - - size_t i; - for (i = 0; i < DEBUG_MASK_COUNT; i++) + for (auto& mode : debug_str_table) { - if (!strcasecmp(str, func_debug_mode_str_table[i])) + if (mode.first == str) { - func_debug_set_mask(func_debug, 1ULL << i); + set_mask(mode.second); return; } } + print_masks(stderr); - func_debug_print_masks(stderr); - - FATAL_ERROR("Invalid debug mask: %s", str); + FATAL_ERROR("Invalid debug mask: %s", str.c_str()); } -void func_debug_print_masks(FILE* out) +void func_debug_t::print_masks(FILE* out) { - uint32_t i; - fprintf(out, "Available debug masks:\n"); - - for (i = 0; i < DEBUG_MASK_COUNT; i++) + for (auto& mode : debug_str_table) { - fprintf(out, "[%d] %s\n", i, func_debug_mode_str_table[i]); + fprintf(out, "[%d] %s\n", mode.second, mode.first.c_str()); } } -void func_debug_set_output_unbuffered(func_debug_t* func_debug, const bool is_unbuffered) +void func_debug_t::set_output_unbuffered(const bool is_unbuffered) { - func_debug->is_output_unbuffered = is_unbuffered; + is_output_unbuffered = is_unbuffered; } // Print warnings to the debug file or optionally store them in a buffer instead @@ -364,73 +316,29 @@ void func_debug_warning( va_list args; va_start(args, fmt); - if (func_debug->record_warnings) - { - // Record to the circular buffer - uint32_t len; - - len = snprintf(func_debug->warning_buffer[func_debug->warning_buffer_tail], WARNING_BUFFER_ENTRY_LENGTH, - "WARNING AT %s:%d %s(): ", file, line, func); - vsnprintf(func_debug->warning_buffer[func_debug->warning_buffer_tail] + len, WARNING_BUFFER_ENTRY_LENGTH - len, - fmt, args); - func_debug->warning_buffer_tail = (func_debug->warning_buffer_tail + 1) % WARNING_BUFFER_SIZE; - } - else - { - // Print to the debug file (e.g., stderr) - fprintf(func_debug->func_debug_file, "WARNING AT %s:%d %s():\n", file, line, func); - vfprintf(func_debug->func_debug_file, fmt, args); - fprintf(func_debug->func_debug_file, "\n"); - } + // Print to the debug file (e.g., stderr) + fprintf(func_debug->func_debug_file, "WARNING AT %s:%d %s():\n", file, line, func); + vfprintf(func_debug->func_debug_file, fmt, args); + fprintf(func_debug->func_debug_file, "\n"); + va_end(args); } -// Initialize the warning buffer capture -int func_debug_set_captured_warnings(func_debug_t* func_debug, uint32_t capture) +std::string func_debug_t::get_debug_mask_help_string() { - uint32_t i; - func_debug->record_warnings = capture; - if (capture) - { - func_debug->warning_buffer_head = 0; - func_debug->warning_buffer_tail = 0; - - for (i = 0; i < WARNING_BUFFER_SIZE; i++) - { - func_debug->warning_buffer[i] = (char*)calloc(1, WARNING_BUFFER_ENTRY_LENGTH); - } + std::string rval = "Set debug mask. Valid values are: "; + for (auto& mask : debug_str_table) { + rval += mask.first + " "; } - else - { - for (i = 0; i < WARNING_BUFFER_SIZE; i++) - { - if (func_debug->warning_buffer[i]) - { - free(func_debug->warning_buffer[i]); - func_debug->warning_buffer[i] = NULL; - } - } - } - - return 0; -} - -int func_debug_has_captured_warning(func_debug_t* func_debug) -{ - if (func_debug->record_warnings && func_debug->warning_buffer_head != func_debug->warning_buffer_tail) - return 1; - else - return 0; + return rval; } -int func_debug_get_captured_warning(func_debug_t* func_debug, char* buf_ptr, const uint32_t buf_len) +std::string func_debug_t::get_debug_verbosity_help_string() { - if (!func_debug_has_captured_warning(func_debug)) - return 1; - - strncpy(buf_ptr, func_debug->warning_buffer[func_debug->warning_buffer_head], buf_len); - - func_debug->warning_buffer_head = (func_debug->warning_buffer_head + 1) % WARNING_BUFFER_SIZE; - - return 0; -} + std::string rval = "Set logging level. Valid values are: "; + for (auto& verb : func_debug_verbosity_table) + { + rval += verb.first + " "; + } + return rval; +} \ No newline at end of file -- cgit v1.2.1