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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*******************************************************************
* File: omRetur2Info.c
* Purpose: translation of return addr to RetInfo
* Author: obachman (Olaf Bachmann)
* Created: 11/99
*******************************************************************/
#include <stdio.h>
#include <string.h>
#ifndef PACKAGE
#include "omConfig.h"
#endif
#ifndef OM_NDEBUG
#include "omDerivedConfig.h"
#include "omStructs.h"
#include "omRet2Info.h"
#include "omGetBackTrace.h"
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
/* define to also print return addresses in (default)
output of omPrintInfo */
/* #define OM_PRINT_RETURN_ADDRESS */
static char om_this_prog[MAXPATHLEN] = "";
#ifndef OM_MAX_BACKTRACE_DEPTH
#define OM_MAX_BACKTRACE_DEPTH 16
#endif
void omInitRet_2_Info(const char* argv0)
{
// char buf[MAXPATHLEN];
if (argv0 != NULL) // // && omFindExec(argv0, buf))
{
strncpy(om_this_prog, argv0, MAXPATHLEN); // // buf);
om_this_prog[MAXPATHLEN - 1]= '\0';
}
}
int omBackTrace_2_RetInfo(void** bt, omRetInfo info, int max)
{
int i=0, j=0, filled = 0;
if (max <= 0 || bt == NULL || info == NULL) return 0;
if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
memset(info, 0, max*sizeof(omRetInfo_t));
while (i<max)
{
if (bt[i])
{
info[j].addr = bt[i];
j++;
}
i++;
}
if (j == 0) return 0;
#if defined(HAVE_POPEN) && defined(OM_PROG_ADDR2LINE)
if (*om_this_prog != '\0')
{
char command[2*MAXPATHLEN + 15 + OM_MAX_BACKTRACE_DEPTH*(2*SIZEOF_VOIDP + 4)];
FILE *pipe;
int l;
l = sprintf(command, "%s -s -C -f -e %s",
OM_PROG_ADDR2LINE, om_this_prog);
i=0;
while (i<j)
{
l+=sprintf(&command[l], " %p", info[i].addr);
i++;
}
fflush(NULL);
pipe = popen(command, "r");
if (pipe != NULL)
{
/* An output entry of addr2line looks as follows:
FunctionName
File:Line
*/
while ((filled < j) &&
(fscanf(pipe, "%200[^\n]\n%200[^:]:%d\n", info[filled].func, info[filled].file, &(info[filled].line)) == 3))
{
if (*info[filled].func != '?' && *info[filled].file != '?' && info[filled].line > 0)
filled++;
}
pclose(pipe);
}
return filled;
}
#endif
return j;
}
int omPrintRetInfo(omRetInfo info, int max, FILE* fd, const char* fmt)
{
int i = 0;
if (max <= 0 || info == NULL || fmt == NULL || fd == NULL) return 0;
while (i < max && info[i].addr != NULL)
{
int l = 0;
while (fmt[l] != 0)
{
if (fmt[l] == '%')
{
l++;
if (fmt[l] == 'p') fprintf(fd, "%p", info[i].addr);
else if (fmt[l] == 'f') fprintf(fd, "%-20s", (*info[i].file != '\0' ? info[i].file : "??"));
else if (fmt[l] == 'F') fprintf(fd, "%-20s", (*info[i].func != '\0' ? info[i].func : "??"));
else if (fmt[l] == 'l') fprintf(fd, "%d", info[i].line);
else if (fmt[l] == 'N')
{
if (*info[i].func != '\0')
{
char* found = (char*) strchr(info[i].func, '(');
if (found) *found = '\0';
fprintf(fd, "%-20s", info[i].func);
if (found) *found = '(';
}
else
fprintf(fd, "%-20s", "??");
}
else if (fmt[l] == 'L')
{
int n = fprintf(fd, "%s:%d", (*info[i].func != '\0' ? info[i].file : "??"), info[i].line);
if (n < 20) fprintf(fd, "%*s", 20-n, " ");
}
else if (fmt[l] == 'i') fprintf(fd, "%d", i);
else
{
fputc('%', fd);
l--;
}
}
else
{
fputc(fmt[l], fd);
}
l++;
}
i++;
}
return i;
}
int omPrintBackTrace(void** bt, int max, FILE* fd)
{
int i;
omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH];
if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
i = omBackTrace_2_RetInfo(bt, info, max);
#ifdef OM_PRINT_RETURN_ADDRESS
return omPrintRetInfo(info, i, fd, " #%i at %L in %N ra=%p\n");
#else
return omPrintRetInfo(info, i, fd, " #%i at %L in %N\n");
#endif
}
int omPrintCurrentBackTraceMax(FILE* fd, int max)
{
int i;
void* bt[OM_MAX_BACKTRACE_DEPTH];
if (max > OM_MAX_BACKTRACE_DEPTH)
max = OM_MAX_BACKTRACE_DEPTH;
if (max <= 0) return 0;
i = omGetBackTrace(bt, 1, max);
return omPrintBackTrace(bt, i, fd);
}
/*************************************************************
*
* Various Filters
*
*************************************************************/
int omFilterRetInfo_i(omRetInfo info, int max, int i)
{
int j=0, k=i;
while (k < max)
{
info[j] = info[k];
j++;
k++;
}
return j;
}
/*************************************************************
*
* Low level routines
*
*************************************************************/
int _omPrintBackTrace(void** bt, int max, FILE* fd , OM_FLR_DECL)
{
#ifndef __OPTIMIZE__
int i = 0;
omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH];
if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
if (bt != NULL)
{
for (; i<max; i++)
{
if (bt[i] == NULL)
{
max = i+1;
break;
}
}
i = omBackTrace_2_RetInfo(bt, info, max);
}
#ifdef OM_TRACK_RETURN
if (i == 0)
i = omBackTrace_2_RetInfo(((void*)&r),info, 1);
#endif
#ifndef OM_INTERNAL_DEBUG
if (i > 1)
{
#ifdef OM_TRACK_RETURN
if (r != NULL)
omFilterRetInfo(info, i, addr_i == r);
#endif
#ifdef OM_TRACK_FILE_LINE
if (f != NULL && l > 0)
omFilterRetInfo(info, i, strcmp(f, file_i) == 0 && l + 2 >= line_i && l - 2 <= line_i);
#endif
/* if we have both, use overwrite what we got from return addressse --
they sometimes are wrong */
#if defined(OM_TRACK_RETURN) && defined(OM_TRACK_FILE_LINE)
if (r != NULL && info[0].addr == r && l > 0 && f != 0)
{
strcpy(info[0].file, f);
info[0].line = l;
}
#endif
}
if (i == 0)
{
#endif /* ! OM_INTERNAL_DEBUG */
#ifdef OM_TRACK_FILE_LINE
fprintf(fd, " %s:%d", f, l);
#endif
#ifdef OM_TRACK_RETURN
fprintf(fd," ra=%p", r);
#endif
#ifndef OM_INTERNAL_DEBUG
return 1;
}
else
#endif /* ! OM_INTERNAL_DEBUG */
#ifdef OM_PRINT_RETURN_ADDRESS
return omPrintRetInfo(info, i, fd, "\n #%i at %L in %N ra=%p");
#else
return omPrintRetInfo(info, i, fd, "\n #%i at %L in %N");
#endif
#else
return 0;
#endif
}
int _omPrintCurrentBackTrace(FILE* fd , OM_FLR_DECL)
{
#ifdef __OPTIMIZE__
/* does not work without -g */
return 0;
#else
int i;
void* bt[OM_MAX_BACKTRACE_DEPTH];
i = omGetBackTrace(bt, 1, OM_MAX_BACKTRACE_DEPTH);
return _omPrintBackTrace(bt, i, fd , OM_FLR_VAL);
#endif
}
#endif /* ! OM_NDEBUG */
|