source: git/omalloc/omRet2Info.c @ 4e654a2

fieker-DuValspielwiese
Last change on this file since 4e654a2 was 8291be, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
minor fixes in omalloc
  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*******************************************************************
2 *  File:    omRetur2Info.c
3 *  Purpose: translation of return addr to RetInfo
4 *  Author:  obachman (Olaf Bachmann)
5 *  Created: 11/99
6 *  Version: $Id$
7 *******************************************************************/
8#include <stdio.h>
9#include <string.h>
10
11#ifdef HAVE_CONFIG_H
12#include <omalloc/omConfig.h>
13#endif
14
15#ifndef OM_NDEBUG
16
17#include <omalloc/omDerivedConfig.h>
18#include <omalloc/omStructs.h>
19#include <omalloc/omRet2Info.h>
20#include <omalloc/omGetBackTrace.h>
21
22#ifndef MAXPATHLEN
23#define MAXPATHLEN 1024
24#endif
25
26/* define to also print return addresses in (default)
27   output of omPrintInfo */
28/* #define OM_PRINT_RETURN_ADDRESS */
29static char om_this_prog[MAXPATHLEN] = "";
30
31#ifndef OM_MAX_BACKTRACE_DEPTH
32#define OM_MAX_BACKTRACE_DEPTH 16
33#endif
34
35void omInitRet_2_Info(const char* argv0)
36{
37//  char buf[MAXPATHLEN];
38
39  if (argv0 != NULL) // // && omFindExec(argv0, buf))
40  {
41    strncpy(om_this_prog, argv0, MAXPATHLEN); // // buf);
42    om_this_prog[MAXPATHLEN - 1]= '\0';   
43  }
44}
45
46
47int omBackTrace_2_RetInfo(void** bt, omRetInfo info, int max)
48{
49  int i=0, j=0, filled = 0;
50  if (max <= 0 || bt == NULL || info == NULL) return 0;
51  if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
52  memset(info, 0, max*sizeof(omRetInfo_t));
53  while (i<max)
54  {
55    if (bt[i])
56    {
57      info[j].addr = bt[i];
58      j++;
59    }
60    i++;
61  }
62  if (j == 0) return 0;
63
64#if defined(HAVE_POPEN) && defined(OM_PROG_ADDR2LINE)
65  if (*om_this_prog != '\0')
66  {
67    char command[2*MAXPATHLEN + 15 + OM_MAX_BACKTRACE_DEPTH*(2*SIZEOF_VOIDP + 4)];
68    FILE *pipe;
69    int l;
70    l = sprintf(command, "%s -s -C -f -e %s",
71                OM_PROG_ADDR2LINE, om_this_prog);
72    i=0;
73    while (i<j)
74    {
75      l+=sprintf(&command[l], " %p", info[i].addr);
76      i++;
77    }
78    fflush(NULL);
79    pipe = popen(command, "r");
80    if (pipe != NULL)
81    {
82        /* An output entry of addr2line looks as follows:
83FunctionName
84File:Line
85        */
86      while ((filled < j) &&
87             (fscanf(pipe, "%200[^\n]\n%200[^:]:%d\n", info[filled].func, info[filled].file, &(info[filled].line)) == 3))
88      {
89        if (*info[filled].func != '?' && *info[filled].file != '?' && info[filled].line > 0)
90          filled++;
91      }
92      pclose(pipe);
93    }
94    return filled;
95  }
96#endif
97  return j;
98}
99
100int omPrintRetInfo(omRetInfo info, int max, FILE* fd, const char* fmt)
101{
102  int i = 0;
103  if (max <= 0 || info == NULL || fmt == NULL || fd == NULL) return 0;
104  while (i < max && info[i].addr != NULL)
105  {
106    int l = 0;
107    while (fmt[l] != 0)
108    {
109      if (fmt[l] == '%')
110      {
111        l++;
112        if (fmt[l] == 'p') fprintf(fd, "%p", info[i].addr);
113        else if (fmt[l] == 'f') fprintf(fd, "%-20s", (*info[i].file != '\0' ? info[i].file : "??"));
114        else if (fmt[l] == 'F') fprintf(fd, "%-20s", (*info[i].func != '\0' ? info[i].func : "??"));
115        else if (fmt[l] == 'l') fprintf(fd, "%d", info[i].line);
116        else if (fmt[l] == 'N')
117        {
118          if (*info[i].func != '\0')
119          {
120            char* found = (char*) strchr(info[i].func, '(');
121            if (found) *found = '\0';
122            fprintf(fd, "%-20s", info[i].func);
123            if (found) *found = '(';
124          }
125          else
126            fprintf(fd, "%-20s", "??");
127        }
128        else if (fmt[l] == 'L')
129        {
130          int n = fprintf(fd, "%s:%d", (*info[i].func != '\0' ? info[i].file : "??"), info[i].line);
131          if (n < 20) fprintf(fd, "%*s", 20-n, " ");
132        }
133        else if (fmt[l] == 'i') fprintf(fd, "%d", i);
134        else
135        {
136          fputc('%', fd);
137          l--;
138        }
139      }
140      else
141      {
142        fputc(fmt[l], fd);
143      }
144      l++;
145    }
146    i++;
147  }
148  return i;
149}
150
151int omPrintBackTrace(void** bt, int max, FILE* fd)
152{
153  int i;
154
155  omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH];
156  if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
157
158  i = omBackTrace_2_RetInfo(bt, info, max);
159#ifdef OM_PRINT_RETURN_ADDRESS
160  return omPrintRetInfo(info, i, fd, "  #%i at %L in %N ra=%p\n");
161#else
162  return omPrintRetInfo(info, i, fd, "  #%i at %L in %N\n");
163#endif
164}
165
166int omPrintCurrentBackTraceMax(FILE* fd, int max)
167{
168  int i;
169  void* bt[OM_MAX_BACKTRACE_DEPTH];
170  if (max > OM_MAX_BACKTRACE_DEPTH)
171    max = OM_MAX_BACKTRACE_DEPTH;
172  if (max <= 0) return 0;
173  i = omGetBackTrace(bt, 1, max);
174  return omPrintBackTrace(bt, i, fd);
175}
176
177/*************************************************************
178 *
179 * Various Filters
180 *
181 *************************************************************/
182int omFilterRetInfo_i(omRetInfo info, int max, int i)
183{
184  int j=0, k=i;
185
186  while (k < max)
187  {
188    info[j] = info[k];
189    j++;
190    k++;
191  }
192  return j;
193}
194
195/*************************************************************
196 *
197 * Low level routines
198 *
199 *************************************************************/
200
201int _omPrintBackTrace(void** bt, int max, FILE* fd , OM_FLR_DECL)
202{
203  int i = 0;
204
205  omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH];
206  if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH;
207  if (bt != NULL)
208  {
209  for (; i<max; i++)
210  {
211    if (bt[i] == NULL)
212    {
213      max = i+1;
214      break;
215    }
216  }
217  i = omBackTrace_2_RetInfo(bt, info, max);
218  }
219#ifdef OM_TRACK_RETURN
220  if (i == 0)
221    i = omBackTrace_2_RetInfo(((void*)&r),info, 1);
222#endif
223#ifndef OM_INTERNAL_DEBUG
224  if (i > 1)
225  {
226#ifdef OM_TRACK_RETURN
227    if (r != NULL)
228      omFilterRetInfo(info, i, addr_i == r);
229#endif
230#ifdef OM_TRACK_FILE_LINE
231    if (f != NULL && l > 0)
232      omFilterRetInfo(info, i, strcmp(f, file_i) == 0 && l + 2 >= line_i && l - 2 <= line_i);
233#endif
234    /* if we have both, use overwrite what we got from return addressse --
235       they sometimes are wrong */
236#if defined(OM_TRACK_RETURN) && defined(OM_TRACK_FILE_LINE)
237    if (r != NULL && info[0].addr == r && l > 0 && f != 0)
238    {
239      strcpy(info[0].file, f);
240      info[0].line = l;
241    }
242#endif
243  }
244  if (i == 0)
245  {
246#endif /* ! OM_INTERNAL_DEBUG */
247
248#ifdef OM_TRACK_FILE_LINE
249    fprintf(fd, " %s:%d", f, l);
250#endif
251#ifdef OM_TRACK_RETURN
252    fprintf(fd," ra=%p", r);
253#endif
254
255#ifndef OM_INTERNAL_DEBUG
256    return 1;
257  }
258  else
259#endif /* ! OM_INTERNAL_DEBUG */
260#ifdef OM_PRINT_RETURN_ADDRESS
261    return omPrintRetInfo(info, i, fd, "\n  #%i at %L in %N ra=%p");
262#else
263    return omPrintRetInfo(info, i, fd, "\n  #%i at %L in %N");
264#endif
265}
266
267int _omPrintCurrentBackTrace(FILE* fd , OM_FLR_DECL)
268{
269  int i;
270  void* bt[OM_MAX_BACKTRACE_DEPTH];
271
272  i = omGetBackTrace(bt, 1, OM_MAX_BACKTRACE_DEPTH);
273  return _omPrintBackTrace(bt, i, fd , OM_FLR_VAL);
274}
275#endif /* ! OM_NDEBUG */
Note: See TracBrowser for help on using the repository browser.