00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <execinfo.h>
00034 #include <sys/time.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <stdarg.h>
00038 #include <string.h>
00039 #include "debug.h"
00040
00041 void
00042 m_debug_print(char *file, int line, int fatal, const char *prefix,
00043 const char *strformat, ...)
00044 {
00045 char buf[512];
00046 va_list ap;
00047 int len;
00048 if (prefix) {
00049 len = sprintf(buf, "%s ", prefix);
00050 }
00051 if (file) {
00052 len += sprintf(&buf[len], "%s(%d)", file, line);
00053 }
00054 if (file || prefix) {
00055 len += sprintf(&buf[len], ": ");
00056 }
00057 va_start (ap, strformat);
00058 vsnprintf(&buf[len], sizeof (buf) - 1 - len, strformat, ap);
00059 va_end (ap);
00060 fprintf(M_DEBUG_OUT, "%s", buf);
00061 if (fatal) {
00062 exit(1);
00063 }
00064 }
00065
00066
00067 void
00068 m_debug_print_L(int debug_flag, const char *strformat, ...)
00069 {
00070 char msg[512];
00071 int len;
00072 va_list ap;
00073 struct timeval curtime;
00074 int xact_state;
00075 unsigned int tid;
00076 unsigned int tid_pthread;
00077
00078 if (!debug_flag) {
00079 return;
00080 }
00081
00082
00083
00084
00085
00086 xact_state = 0;
00087 tid = 0;
00088 tid_pthread = 0;
00089 gettimeofday(&curtime, NULL);
00090 len = sprintf(msg, "[M_DEBUG: T-%02u (%u) TS=%04u%06u TX=%d PC=%p] ",
00091 tid,
00092 tid_pthread,
00093 (unsigned int) curtime.tv_sec, (unsigned int) curtime.tv_usec,
00094 xact_state,
00095 __builtin_return_address(0));
00096 va_start (ap, strformat);
00097 vsnprintf(&msg[len], sizeof (msg) - 1 - len, strformat, ap);
00098 va_end (ap);
00099
00100 len = strlen(msg);
00101 msg[len] = '\0';
00102
00103 fprintf(M_DEBUG_OUT, "%s", msg);
00104 }
00105
00106
00107
00108 void
00109 m_print_trace (void)
00110 {
00111 void *array[10];
00112 int size;
00113 char **strings;
00114 size_t i;
00115
00116 size = backtrace (array, 10);
00117 strings = backtrace_symbols (array, size);
00118
00119 printf ("Obtained %d stack frames.\n", size);
00120
00121 for (i = 0; i < size; i++)
00122 printf ("%s\n", strings[i]);
00123
00124 free (strings);
00125 }