From 3e72295ee9f66fa8809ee03aced7fed99248dd74 Mon Sep 17 00:00:00 2001 From: Nikolai Nosov Date: Sat, 25 Apr 2020 08:05:45 +0400 Subject: [PATCH] rename logger type. disable debug logging by default. extend test to check pointer validity --- src/context.c | 2 +- src/context.h | 9 ++++++++- src/dwarf/dwarf_function.c | 11 +---------- src/utils/log.c | 32 ++++++++++++++++---------------- src/utils/log.h | 8 ++++---- tests/main.c | 9 ++++----- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/context.c b/src/context.c index 5d9670b..ef2844c 100644 --- a/src/context.c +++ b/src/context.c @@ -17,7 +17,7 @@ extern dwarf_reg_map reg_map[]; extern int regnum; -pst_log logger; // logger for library +pst_logger logger; // logger for library pst_allocator allocator; // custom allocator for PST library static void clean_print(pst_context* ctx) diff --git a/src/context.h b/src/context.h index 6d54e45..d33c9e3 100644 --- a/src/context.h +++ b/src/context.h @@ -18,13 +18,20 @@ #include "utils/allocator.h" #include "utils/log.h" -extern pst_log logger; // logger for whole PST library +// uncomment line below to enable debug output to stdout +//#define PST_DEBUG + +extern pst_logger logger; // logger for whole PST library extern pst_allocator allocator; // custom allocator for PST library #define pst_alloc(TYPE) (TYPE*)allocator.alloc(&allocator, sizeof(TYPE)) #define pst_free(NAME) allocator.free(&allocator, NAME) +#ifdef PST_DEBUG #define pst_log(SEVERITY, FORMAT, ...) logger.log(&logger, SEVERITY, FORMAT, ##__VA_ARGS__) +#else +#define pst_log(SEVERITY, FORMAT, ...) +#endif char* pst_strdup(const char* str); diff --git a/src/dwarf/dwarf_function.c b/src/dwarf/dwarf_function.c index 4099c17..dff65fe 100644 --- a/src/dwarf/dwarf_function.c +++ b/src/dwarf/dwarf_function.c @@ -117,14 +117,9 @@ static void clear(pst_function* fn) static bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) { - uint64_t lowpc = 0, highpc = 0; const char* origin_name = ""; - dwarf_lowpc(result, &lowpc); - dwarf_highpc(result, &lowpc); - Dwarf_Attribute attr_mem; Dwarf_Die origin; if(dwarf_hasattr (result, DW_AT_abstract_origin) && dwarf_formref_die (dwarf_attr (result, DW_AT_abstract_origin, &attr_mem), &origin) != NULL) { - origin_name = dwarf_diename(&origin); Dwarf_Die child; if(dwarf_child (&origin, &child) == 0) { do { @@ -141,11 +136,7 @@ static bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) } while (dwarf_siblingof (&child, &child) == 0); } } - const char* die_name = ""; - if(dwarf_diename(result)) { - die_name = dwarf_diename(result); - } - pst_log(SEVERITY_DEBUG, "Lexical block with name '%s', tag 0x%X and origin '%s' found. lowpc = 0x%lX, highpc = 0x%lX", die_name, dwarf_tag (result), origin_name, lowpc, highpc); + Dwarf_Die child; if(dwarf_child (result, &child) == 0) { do { diff --git a/src/utils/log.c b/src/utils/log.c index 34980a4..8878ae9 100644 --- a/src/utils/log.c +++ b/src/utils/log.c @@ -12,12 +12,12 @@ const char * const severity_map[] = { " [ERROR] : " }; -static void format_string(pst_log* log, const char* fmt, va_list args) +static void format_string(pst_logger* log, const char* fmt, va_list args) { log->mStringLen += vsnprintf(log->mString + log->mStringLen, log->mStringSize - log->mStringLen, fmt, args); } -static void format_prefix(pst_log* log, SC_LogSeverity severity) +static void format_prefix(pst_logger* log, SC_LogSeverity severity) { time_t rawTime; struct tm * timeinfo; @@ -29,7 +29,7 @@ static void format_prefix(pst_log* log, SC_LogSeverity severity) log->mStringLen += strlen(severity_map[(int)severity]); } -static void format_postfix(pst_log* log) +static void format_postfix(pst_logger* log) { if((log->mStringLen + 2) < log->mStringSize) { log->mString[log->mStringLen++] = '\n'; @@ -38,7 +38,7 @@ static void format_postfix(pst_log* log) } //variable argument number logging -static void log_base(pst_log* log, SC_LogSeverity severity, const char* fmt, ...) +static void log_base(pst_logger* log, SC_LogSeverity severity, const char* fmt, ...) { if (severity < log->mCurrentSeverity) return; @@ -61,7 +61,7 @@ static void log_base(pst_log* log, SC_LogSeverity severity, const char* fmt, ... pthread_mutex_unlock(&log->mLock); } -static void log_init_base(pst_log* plog, const char* source) +static void log_init_base(pst_logger* plog, const char* source) { // methods plog->log = log_base; @@ -79,7 +79,7 @@ static void log_init_base(pst_log* plog, const char* source) plog->child = 0; } -void pst_log_fini(pst_log* log) +void pst_log_fini(pst_logger* log) { pthread_mutex_destroy(&log->mLock); log->close(log); @@ -99,7 +99,7 @@ void pst_log_fini(pst_log* log) #define NC "\e[0m" // No Color //actually sends message to the source -static void send_msg_console(pst_log* log, SC_LogSeverity severity) +static void send_msg_console(pst_logger* log, SC_LogSeverity severity) { const char* color = NC; switch(severity) { @@ -123,19 +123,19 @@ static void send_msg_console(pst_log* log, SC_LogSeverity severity) fprintf(stderr, "%s%s%s", color, log->mString, NC); } -static void close_console(pst_log* log) { +static void close_console(pst_logger* log) { // do nothing } -static bool open_console(pst_log* log) { +static bool open_console(pst_logger* log) { return true; } -static bool is_opened_console(pst_log* log) { +static bool is_opened_console(pst_logger* log) { return true; } -void pst_log_init_console(pst_log* log) +void pst_log_init_console(pst_logger* log) { log_init_base(log, NULL); log->close = close_console; @@ -157,7 +157,7 @@ typedef struct _file_spec { } file_spec; //open file for writing -static bool open_file(pst_log* log) +static bool open_file(pst_logger* log) { file_spec* fsp = (file_spec*)log->child; if(!fsp) { @@ -187,7 +187,7 @@ static bool open_file(pst_log* log) return (fsp->fd != 0); } -static void close_file(pst_log* log) +static void close_file(pst_logger* log) { file_spec* fsp = (file_spec*)log->child; if(!fsp) { @@ -203,7 +203,7 @@ static void close_file(pst_log* log) fsp->max_bytes = 0; } -static bool is_file_opened(pst_log* log) +static bool is_file_opened(pst_logger* log) { file_spec* fsp = (file_spec*)log->child; if(!fsp) { @@ -213,7 +213,7 @@ static bool is_file_opened(pst_log* log) return (fsp->fd != 0); } -static void send_msg_file(pst_log* log, SC_LogSeverity severity) +static void send_msg_file(pst_logger* log, SC_LogSeverity severity) { file_spec* fsp = (file_spec*)log->child; if(!fsp) { @@ -231,7 +231,7 @@ static void send_msg_file(pst_log* log, SC_LogSeverity severity) } } -void pst_log_init_file(pst_log* log, const char* path, uint64_t max_bytes) +void pst_log_init_file(pst_logger* log, const char* path, uint64_t max_bytes) { log_init_base(log, path); file_spec* fsp = (file_spec*)malloc(sizeof(file_spec)); diff --git a/src/utils/log.h b/src/utils/log.h index e258edd..36836bf 100644 --- a/src/utils/log.h +++ b/src/utils/log.h @@ -40,10 +40,10 @@ typedef struct __pst_log { pthread_mutex_t mLock; void* child; -} pst_log; +} pst_logger; -void pst_log_init_console(pst_log* log); -void pst_log_init_file(pst_log* log, const char* path, uint64_t max_bytes); -void pst_log_fini(pst_log* log); +void pst_log_init_console(pst_logger* log); +void pst_log_init_file(pst_logger* log, const char* path, uint64_t max_bytes); +void pst_log_fini(pst_logger* log); #endif /* __PST_LOG_H_ */ diff --git a/tests/main.c b/tests/main.c index 436534d..004cc59 100644 --- a/tests/main.c +++ b/tests/main.c @@ -12,9 +12,8 @@ typedef enum { DEF_3 } my_int; -void Fun2(int arg1, uint32_t arg2) +void Fun2(int arg1, uint32_t arg2, int* ptr) { - int* ptr = NULL; *ptr = arg1; void* ptr2 = ptr+1; printf("%p\n", ptr2); @@ -24,7 +23,7 @@ uint32_t Fun1(const int arg1, my_int arg2, uint32_t arg3) { int my_local = arg1 + 2; printf("%d\n", my_local); - Fun2(my_local, arg3); + Fun2(my_local, arg3, (int*)0x12345); return arg2; } @@ -70,9 +69,9 @@ void FatalSignalHandler(int sig, siginfo_t* info, void* context) } if(pst_unwind_simple(handler)) { - printf("%s", pst_print_simple(handler)); + printf("\n%s", pst_print_simple(handler)); if(pst_unwind_pretty(handler)) { - printf("%s", pst_print_pretty(handler)); + printf("\n%s", pst_print_pretty(handler)); } else { printf("Failed to use DWARF for unwind stack trace\n"); }