rename logger type. disable debug logging by default. extend test to

check pointer validity
This commit is contained in:
2020-04-25 08:05:45 +04:00
parent 7206abc6a9
commit 3e72295ee9
6 changed files with 34 additions and 37 deletions
+1 -1
View File
@@ -17,7 +17,7 @@
extern dwarf_reg_map reg_map[]; extern dwarf_reg_map reg_map[];
extern int regnum; extern int regnum;
pst_log logger; // logger for library pst_logger logger; // logger for library
pst_allocator allocator; // custom allocator for PST library pst_allocator allocator; // custom allocator for PST library
static void clean_print(pst_context* ctx) static void clean_print(pst_context* ctx)
+8 -1
View File
@@ -18,13 +18,20 @@
#include "utils/allocator.h" #include "utils/allocator.h"
#include "utils/log.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 extern pst_allocator allocator; // custom allocator for PST library
#define pst_alloc(TYPE) (TYPE*)allocator.alloc(&allocator, sizeof(TYPE)) #define pst_alloc(TYPE) (TYPE*)allocator.alloc(&allocator, sizeof(TYPE))
#define pst_free(NAME) allocator.free(&allocator, NAME) #define pst_free(NAME) allocator.free(&allocator, NAME)
#ifdef PST_DEBUG
#define pst_log(SEVERITY, FORMAT, ...) logger.log(&logger, SEVERITY, FORMAT, ##__VA_ARGS__) #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); char* pst_strdup(const char* str);
+1 -10
View File
@@ -117,14 +117,9 @@ static void clear(pst_function* fn)
static bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) 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_Attribute attr_mem;
Dwarf_Die origin; 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) { 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; Dwarf_Die child;
if(dwarf_child (&origin, &child) == 0) { if(dwarf_child (&origin, &child) == 0) {
do { do {
@@ -141,11 +136,7 @@ static bool handle_lexical_block(pst_function* fn, Dwarf_Die* result)
} while (dwarf_siblingof (&child, &child) == 0); } 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; Dwarf_Die child;
if(dwarf_child (result, &child) == 0) { if(dwarf_child (result, &child) == 0) {
do { do {
+16 -16
View File
@@ -12,12 +12,12 @@ const char * const severity_map[] = {
" [ERROR] : " " [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); 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; time_t rawTime;
struct tm * timeinfo; struct tm * timeinfo;
@@ -29,7 +29,7 @@ static void format_prefix(pst_log* log, SC_LogSeverity severity)
log->mStringLen += strlen(severity_map[(int)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) { if((log->mStringLen + 2) < log->mStringSize) {
log->mString[log->mStringLen++] = '\n'; log->mString[log->mStringLen++] = '\n';
@@ -38,7 +38,7 @@ static void format_postfix(pst_log* log)
} }
//variable argument number logging //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) if (severity < log->mCurrentSeverity)
return; return;
@@ -61,7 +61,7 @@ static void log_base(pst_log* log, SC_LogSeverity severity, const char* fmt, ...
pthread_mutex_unlock(&log->mLock); 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 // methods
plog->log = log_base; plog->log = log_base;
@@ -79,7 +79,7 @@ static void log_init_base(pst_log* plog, const char* source)
plog->child = 0; plog->child = 0;
} }
void pst_log_fini(pst_log* log) void pst_log_fini(pst_logger* log)
{ {
pthread_mutex_destroy(&log->mLock); pthread_mutex_destroy(&log->mLock);
log->close(log); log->close(log);
@@ -99,7 +99,7 @@ void pst_log_fini(pst_log* log)
#define NC "\e[0m" // No Color #define NC "\e[0m" // No Color
//actually sends message to the source //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; const char* color = NC;
switch(severity) { 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); 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 // do nothing
} }
static bool open_console(pst_log* log) { static bool open_console(pst_logger* log) {
return true; return true;
} }
static bool is_opened_console(pst_log* log) { static bool is_opened_console(pst_logger* log) {
return true; return true;
} }
void pst_log_init_console(pst_log* log) void pst_log_init_console(pst_logger* log)
{ {
log_init_base(log, NULL); log_init_base(log, NULL);
log->close = close_console; log->close = close_console;
@@ -157,7 +157,7 @@ typedef struct _file_spec {
} file_spec; } file_spec;
//open file for writing //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; file_spec* fsp = (file_spec*)log->child;
if(!fsp) { if(!fsp) {
@@ -187,7 +187,7 @@ static bool open_file(pst_log* log)
return (fsp->fd != 0); 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; file_spec* fsp = (file_spec*)log->child;
if(!fsp) { if(!fsp) {
@@ -203,7 +203,7 @@ static void close_file(pst_log* log)
fsp->max_bytes = 0; 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; file_spec* fsp = (file_spec*)log->child;
if(!fsp) { if(!fsp) {
@@ -213,7 +213,7 @@ static bool is_file_opened(pst_log* log)
return (fsp->fd != 0); 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; file_spec* fsp = (file_spec*)log->child;
if(!fsp) { 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); log_init_base(log, path);
file_spec* fsp = (file_spec*)malloc(sizeof(file_spec)); file_spec* fsp = (file_spec*)malloc(sizeof(file_spec));
+4 -4
View File
@@ -40,10 +40,10 @@ typedef struct __pst_log {
pthread_mutex_t mLock; pthread_mutex_t mLock;
void* child; void* child;
} pst_log; } pst_logger;
void pst_log_init_console(pst_log* log); void pst_log_init_console(pst_logger* log);
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);
void pst_log_fini(pst_log* log); void pst_log_fini(pst_logger* log);
#endif /* __PST_LOG_H_ */ #endif /* __PST_LOG_H_ */
+4 -5
View File
@@ -12,9 +12,8 @@ typedef enum {
DEF_3 DEF_3
} my_int; } my_int;
void Fun2(int arg1, uint32_t arg2) void Fun2(int arg1, uint32_t arg2, int* ptr)
{ {
int* ptr = NULL;
*ptr = arg1; *ptr = arg1;
void* ptr2 = ptr+1; void* ptr2 = ptr+1;
printf("%p\n", ptr2); 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; int my_local = arg1 + 2;
printf("%d\n", my_local); printf("%d\n", my_local);
Fun2(my_local, arg3); Fun2(my_local, arg3, (int*)0x12345);
return arg2; return arg2;
} }
@@ -70,9 +69,9 @@ void FatalSignalHandler(int sig, siginfo_t* info, void* context)
} }
if(pst_unwind_simple(handler)) { if(pst_unwind_simple(handler)) {
printf("%s", pst_print_simple(handler)); printf("\n%s", pst_print_simple(handler));
if(pst_unwind_pretty(handler)) { if(pst_unwind_pretty(handler)) {
printf("%s", pst_print_pretty(handler)); printf("\n%s", pst_print_pretty(handler));
} else { } else {
printf("Failed to use DWARF for unwind stack trace\n"); printf("Failed to use DWARF for unwind stack trace\n");
} }