source: git/omalloc/omRet2Info.c @ cba516

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