1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/* This is part of um-ViewOS
* The user-mode implementation of OSVIEW -- A Process with a View
*
* gdebug.c: debugging functions
*
* Copyright 2005 Ludovico Gardenghi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: gdebug.c 415 2007-11-29 18:16:54Z garden $
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <config.h>
//#include "defs.h"
#include "gdebug.h"
#ifdef MULTI_THREAD
#include <pthread.h>
#endif
FILE *gdebug_ofile = NULL;
#define BACKTRACE_INITIAL_SIZE 10
static void *libc_handle;
static int (*libc_fprintf)(FILE *stream, const char *format, ...);
static int (*libc_vfprintf)(FILE *stream, const char *format, va_list ap);
static FILE *(*libc_fopen)(const char *path, const char *mode);
static int (*libc_getpid)(void);
static void **backtrace_array = NULL;
static int backtrace_array_size = 0;
void gdebug_set_ofile(char* new_ofile)
{
gdebug_ofile = libc_fopen(new_ofile, "w");
if (!gdebug_ofile)
libc_fprintf(stderr, "gdebug: can't open log file %s: %s. Using stderr.\n",
new_ofile, strerror(errno));
else
setlinebuf(gdebug_ofile);
}
void fgdebug(FILE *ofile, int gdebug_level, int level, const char *file, const int line, const char *func, const char *fmt, ...)
{
va_list ap;
if (gdebug_level >= level)
{
va_start(ap, fmt);
#ifdef _PTHREAD_H
libc_fprintf(ofile, "[%d:%lu] %s:%d %s(): ", libc_getpid(), pthread_self(), file, line, func);
#else
libc_fprintf(ofile, "[%d] %s:%d %s(): ", libc_getpid(), file, line, func);
#endif
libc_vfprintf(ofile, fmt, ap);
libc_fprintf(ofile, "\n");
va_end(ap);
}
}
void fgmsg(FILE *ofile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
libc_vfprintf(ofile, fmt, ap);
libc_fprintf(ofile, "\n");
va_end(ap);
}
void fghexdump(FILE *ofile, int gdebug_level, int level, const char *file, const int line, const char *func, char* text, int len)
{
int i;
if (gdebug_level >= level)
{
#ifdef _PTHREAD_H
libc_fprintf(ofile, "[%d:%lu] %s:%d %s(): [%d] ", libc_getpid(), pthread_self(), file, line, func, len);
#else
libc_fprintf(ofile, "[%d] %s:%d %s(): [%d] ", libc_getpid(), file, line, func, len);
#endif
for (i = 0; i < len; i++)
{
if ((i != 0) && ((i % 4) == 0))
libc_fprintf(ofile, " ");
libc_fprintf(ofile, "%02x", (unsigned char)text[i]);
}
libc_fprintf(ofile, "\n");
}
}
void fgbacktrace(FILE *ofile, int gdebug_level, int level, const char *file, const int line, const char *func, int maxdepth)
{
int i;
int btdepth;
char **btstrings;
/* The first entry is always ignored (it's the call to fgbacktrace), the
* user wants maxdepth entries so we must add 1 */
maxdepth++;
if (gdebug_level >= level)
{
if (maxdepth > backtrace_array_size)
{
backtrace_array_size = maxdepth;
backtrace_array = realloc(backtrace_array, sizeof(void*) * backtrace_array_size);
}
btdepth = backtrace(backtrace_array, maxdepth);
btstrings = backtrace_symbols(backtrace_array, btdepth);
if (!btstrings)
{
fgdebug(ofile, gdebug_level, level, file, line, func, "can't obtain backtrace");
return;
}
/* 1 and not 0 (0 is fgbacktrace and it's not interesting) */
for (i = 1; i < btdepth; i++)
fgdebug(ofile, gdebug_level, level, file, line, func, "BT: #%d %s", i-1, btstrings[i]);
}
}
static void __attribute__ ((constructor)) init()
{
libc_handle = dlopen("libc.so.6", RTLD_LAZY);
if (!libc_handle)
{
fprintf(stderr, "dlopen: %s", dlerror());
fprintf(stderr, "dlopen in gdebug failed, reverting to original fprintf/vfprintf/fopen\n");
libc_fprintf = fprintf;
libc_vfprintf = vfprintf;
libc_fopen = fopen;
libc_getpid = getpid;
}
else
{
libc_fprintf = dlsym(libc_handle, "fprintf");
libc_vfprintf = dlsym(libc_handle, "vfprintf");
libc_fopen = dlsym(libc_handle, "fopen");
libc_getpid = dlsym(libc_handle,"getpid");
if (!libc_fprintf || !libc_vfprintf || !libc_fopen || !libc_getpid)
{
fprintf(stderr, "dlsym: %s", dlerror());
fprintf(stderr, "dlsym in gdebug failed, reverting to original fprintf/vfprintf/fopen\n");
libc_fprintf = fprintf;
libc_vfprintf = vfprintf;
libc_fopen = fopen;
libc_getpid = getpid;
}
}
backtrace_array = malloc(sizeof(void*) * BACKTRACE_INITIAL_SIZE);
backtrace_array_size = BACKTRACE_INITIAL_SIZE;
}
static void __attribute__ ((destructor)) fini()
{
dlclose(libc_handle);
if (backtrace_array)
free(backtrace_array);
backtrace_array = NULL;
backtrace_array_size = 0;
}
|