diff --git a/usr/fileio/debug.c b/usr/fileio/debug.c index db0b87fd3..fc0cfadf7 100644 --- a/usr/fileio/debug.c +++ b/usr/fileio/debug.c @@ -20,6 +20,8 @@ #include #include +#include +#include #include "debug.h" @@ -30,14 +32,14 @@ _syscall0(pid_t,gettid); #define TRACE_BUF_SIZE 512 static char trace_buf[TRACE_BUF_SIZE]; -//static spinlock_t trace_buf_lock = SPIN_LOCK_UNLOCKED; +static pthread_spinlock_t trace_buf_lock; int debug_print_prefix(unsigned long trace_flag, const char *func, int line) { int i = 0; -// spin_lock_irqsave(&trace_buf_lock, flags); + pthread_spin_lock(&trace_buf_lock); if (trace_flag & TRACE_TIME) { struct tm t; @@ -58,7 +60,7 @@ int debug_print_prefix(unsigned long trace_flag, const char *func, if (i > 0) PRINTN("%s", trace_buf); -// spin_unlock_irqrestore(&trace_buf_lock, flags); + pthread_spin_unlock(&trace_buf_lock); return i; } @@ -72,7 +74,7 @@ void debug_print_buffer(const void *data, int len) if (buf == NULL) return; -// spin_lock_irqsave(&trace_buf_lock, flags); + pthread_spin_lock(&trace_buf_lock); PRINT(" (h)___0__1__2__3__4__5__6__7__8__9__A__B__C__D__E__F"); for (z = 0, z1 = 0, i = 0; z < len; z++) { @@ -113,8 +115,26 @@ void debug_print_buffer(const void *data, int len) PRINT("%s", trace_buf); } -// spin_unlock_irqrestore(&trace_buf_lock, flags); + pthread_spin_unlock(&trace_buf_lock); return; } +int debug_init(void) +{ + int res; + + res = pthread_spin_init(&trace_buf_lock, PTHREAD_PROCESS_PRIVATE); + if (res != 0) { + res = errno; + PRINT_ERROR_PR("pthread_spin_init() failed: %s", strerror(res)); + } + + return res; +} + +void debug_done(void) +{ + pthread_spin_destroy(&trace_buf_lock); +} + #endif /* DEBUG || TRACING */ diff --git a/usr/fileio/debug.h b/usr/fileio/debug.h index 892e9ec63..e2dba9ede 100644 --- a/usr/fileio/debug.h +++ b/usr/fileio/debug.h @@ -64,6 +64,9 @@ extern char *app_name; extern unsigned long trace_flag; +extern int debug_init(void); +extern void debug_done(void); + extern int debug_print_prefix(unsigned long trace_flag, const char *func, int line); extern void debug_print_buffer(const void *data, int len); diff --git a/usr/fileio/fileio.c b/usr/fileio/fileio.c index dfb7e7cb0..6e392ff5d 100644 --- a/usr/fileio/fileio.c +++ b/usr/fileio/fileio.c @@ -166,6 +166,10 @@ int main(int argc, char **argv) setlinebuf(stdout); + res = debug_init(); + if (res != 0) + goto out; + app_name = argv[0]; memset(&dev, 0, sizeof(dev)); @@ -254,7 +258,7 @@ int main(int argc, char **argv) #endif case 'v': printf("%s version %s\n", app_name, VERSION_STR); - goto out; + goto out_done; default: goto out_usage; } @@ -276,7 +280,7 @@ int main(int argc, char **argv) res = errno; PRINT_ERROR_PR("Unable to open file %s (%s)", dev.file_name, strerror(res)); - goto out; + goto out_done; } dev.file_size = lseek64(fd, 0, SEEK_END); @@ -367,7 +371,7 @@ int main(int argc, char **argv) res = dev.scst_usr_fd; PRINT_ERROR_PR("Unable to open SCST device %s (%s)", DEV_USER_PATH DEV_USER_NAME, strerror(res)); - goto out; + goto out_done; } memset(&desc, 0, sizeof(desc)); @@ -479,10 +483,13 @@ int main(int argc, char **argv) out_close: close(dev.scst_usr_fd); +out_done: + debug_done(); + out: return res; out_usage: usage(); - goto out; + goto out_done; }