00001 #include "define.h"
00002
00003
00004 struct pst_debug_func {
00005 char * name;
00006 struct pst_debug_func *next;
00007 };
00008
00009
00010 #define NUM_COL 32
00011 #define MAX_DEPTH 32
00012
00013 static struct pst_debug_func *func_head = NULL;
00014 static int func_depth = 0;
00015 static char indent[MAX_DEPTH*4+1];
00016 static FILE *debug_fp = NULL;
00017 #ifdef HAVE_SEMAPHORE_H
00018 static sem_t* debug_mutex = NULL;
00019 #endif
00020
00021
00022 void pst_debug_lock()
00023 {
00024 #ifdef HAVE_SEMAPHORE_H
00025 if (debug_mutex) sem_wait(debug_mutex);
00026 #endif
00027 }
00028
00029
00030 void pst_debug_unlock()
00031 {
00032 #ifdef HAVE_SEMAPHORE_H
00033 if (debug_mutex) sem_post(debug_mutex);
00034 #endif
00035 }
00036
00037
00038 void pst_debug_init(const char* fname, void* output_mutex) {
00039 #ifdef HAVE_SEMAPHORE_H
00040 debug_mutex = (sem_t*)output_mutex;
00041 #endif
00042 memset(indent, ' ', MAX_DEPTH*4);
00043 indent[MAX_DEPTH*4] = '\0';
00044 if (debug_fp) pst_debug_close();
00045 if (!fname) return;
00046 if ((debug_fp = fopen(fname, "wb")) == NULL) {
00047 fprintf(stderr, "Opening of file %s failed\n", fname);
00048 exit(1);
00049 }
00050 }
00051
00052
00053 void pst_debug_func(const char* function) {
00054 struct pst_debug_func *func_ptr = pst_malloc (sizeof(struct pst_debug_func));
00055 func_ptr->name = strdup(function);
00056 func_ptr->next = func_head;
00057 func_head = func_ptr;
00058 func_depth++;
00059 }
00060
00061
00062 void pst_debug_func_ret() {
00063
00064 struct pst_debug_func *func_ptr = func_head;
00065 if (func_head) {
00066 func_head = func_head->next;
00067 free(func_ptr->name);
00068 free(func_ptr);
00069 func_depth--;
00070 } else {
00071 DIE(("function list is empty!\n"));
00072 }
00073 }
00074
00075
00076 static void pst_debug_info(int line, const char* file);
00077 static void pst_debug_info(int line, const char* file) {
00078 int le = (func_depth > MAX_DEPTH) ? MAX_DEPTH : func_depth;
00079 if (le > 0) le--;
00080 char *func = (func_head ? func_head->name : "No Function");
00081 pst_debug_lock();
00082 fprintf(debug_fp, "%06d %.*s%s %s(%d) ", getpid(), le*4, indent, func, file, line);
00083 }
00084
00085
00086 void pst_debug(int line, const char* file, const char *fmt, ...) {
00087 if (debug_fp) {
00088 pst_debug_info(line, file);
00089 va_list ap;
00090 va_start(ap,fmt);
00091 vfprintf(debug_fp, fmt, ap);
00092 va_end(ap);
00093 fflush(debug_fp);
00094 pst_debug_unlock();
00095 }
00096 }
00097
00098
00099 void pst_debug_hexdump(int line, const char *file, const char *buf, size_t size, int cols, int delta) {
00100 if (debug_fp) {
00101 pst_debug_info(line, file);
00102 pst_debug_hexdumper(debug_fp, buf, size, cols, delta);
00103 pst_debug_unlock();
00104 }
00105 }
00106
00107
00108 void pst_debug_hexdumper(FILE *out, const char *buf, size_t size, int cols, int delta) {
00109 int le = (func_depth > MAX_DEPTH) ? MAX_DEPTH : func_depth;
00110 size_t off = 0, toff;
00111 int count = 0;
00112
00113 if (!out) return;
00114
00115 if (cols == -1) cols = NUM_COL;
00116 fprintf(out, "\n");
00117 while (off < size) {
00118 fprintf(out, "%06d %.*s%06"PRIx64"\t:", getpid(), le*4, indent, (int64_t)(off+delta));
00119 toff = off;
00120 while (count < cols && off < size) {
00121 fprintf(out, "%02hhx ", (unsigned char)buf[off]);
00122 off++; count++;
00123 }
00124 off = toff;
00125 while (count < cols) {
00126
00127 fprintf(out, " ");
00128 count++;
00129 }
00130 count = 0;
00131 fprintf(out, ":");
00132 while (count < cols && off < size) {
00133 fprintf(out, "%c", isgraph(buf[off])?buf[off]:'.');
00134 off++; count ++;
00135 }
00136
00137 fprintf(out, "\n");
00138 count=0;
00139 }
00140
00141 fprintf(out, "\n");
00142 fflush(out);
00143 }
00144
00145
00146 void pst_debug_close(void) {
00147 while (func_head) {
00148 struct pst_debug_func *func_ptr = func_head;
00149 func_head = func_head->next;
00150 free(func_ptr->name);
00151 free(func_ptr);
00152 }
00153 if (debug_fp) fclose(debug_fp);
00154 debug_fp = NULL;
00155 }
00156
00157
00158 void *pst_malloc(size_t size) {
00159 void *mem = malloc(size);
00160 if (!mem) {
00161 fprintf(stderr, "pst_malloc: Out Of memory [req: %ld]\n", (long)size);
00162 exit(1);
00163 }
00164 return mem;
00165 }
00166
00167
00168 void *pst_realloc(void *ptr, size_t size) {
00169 void *mem = realloc(ptr, size);
00170 if (!mem) {
00171 fprintf(stderr, "pst_realloc: Out Of memory [req: %ld]\n", (long)size);
00172 exit(1);
00173 }
00174 return mem;
00175 }