usermode/library/common/debug.c

00001 /*
00002     Copyright (C) 2011 Computer Sciences Department, 
00003     University of Wisconsin -- Madison
00004 
00005     ----------------------------------------------------------------------
00006 
00007     This file is part of Mnemosyne: Lightweight Persistent Memory, 
00008     originally developed at the University of Wisconsin -- Madison.
00009 
00010     Mnemosyne was originally developed primarily by Haris Volos
00011     with contributions from Andres Jaan Tack.
00012 
00013     ----------------------------------------------------------------------
00014 
00015     Mnemosyne is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU General Public License
00017     as published by the Free Software Foundation, version 2
00018     of the License.
00019  
00020     Mnemosyne is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU General Public License for more details.
00024 
00025     You should have received a copy of the GNU General Public License
00026     along with this program; if not, write to the Free Software
00027     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00028     Boston, MA  02110-1301, USA.
00029 
00030 ### END HEADER ###
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         //TODO: initialize with the right values
00083         //xact_state = mnemosyne_tx_get_xactstate(mnemosyne_l_txd);
00084         //tid = mnemosyne_thrdesc_get_tid(mnemosyne_l_txd); 
00085         //tid_pthread = mnemosyne_tx_get_tid_pthread(mnemosyne_l_txd); 
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 /* Obtain a backtrace and print it to stdout. */
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 }

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7