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.11 2000-12-12 15:26:18 obachman Exp $ |
---|
7 | *******************************************************************/ |
---|
8 | #include <stdio.h> |
---|
9 | #include <string.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 */ |
---|
30 | static char om_this_prog[MAXPATHLEN] = ""; |
---|
31 | |
---|
32 | #ifndef OM_MAX_BACKTRACE_DEPTH |
---|
33 | #define OM_MAX_BACKTRACE_DEPTH 16 |
---|
34 | #endif |
---|
35 | |
---|
36 | void 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 | |
---|
46 | |
---|
47 | int 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(stdin); |
---|
79 | pipe = popen(command, "r"); |
---|
80 | if (pipe != NULL) |
---|
81 | { |
---|
82 | /* An output entry of addr2line looks as follows: |
---|
83 | FunctionName |
---|
84 | File: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 | } |
---|
95 | #endif |
---|
96 | return filled; |
---|
97 | } |
---|
98 | |
---|
99 | int omPrintRetInfo(omRetInfo info, int max, FILE* fd, const char* fmt) |
---|
100 | { |
---|
101 | int i = 0; |
---|
102 | if (max <= 0 || info == NULL || fmt == NULL || fd == NULL) return 0; |
---|
103 | while (i < max && info[i].addr != NULL) |
---|
104 | { |
---|
105 | int l = 0; |
---|
106 | while (fmt[l] != 0) |
---|
107 | { |
---|
108 | if (fmt[l] == '%') |
---|
109 | { |
---|
110 | l++; |
---|
111 | if (fmt[l] == 'p') fprintf(fd, "%p", info[i].addr); |
---|
112 | else if (fmt[l] == 'f') fprintf(fd, "%-20s", (*info[i].file != '\0' ? info[i].file : "??")); |
---|
113 | else if (fmt[l] == 'F') fprintf(fd, "%-20s", (*info[i].func != '\0' ? info[i].func : "??")); |
---|
114 | else if (fmt[l] == 'l') fprintf(fd, "%d", info[i].line); |
---|
115 | else if (fmt[l] == 'N') |
---|
116 | { |
---|
117 | if (*info[i].func != '\0') |
---|
118 | { |
---|
119 | char* found = strchr(info[i].func, '('); |
---|
120 | if (found) *found = '\0'; |
---|
121 | fprintf(fd, "%-20s", info[i].func); |
---|
122 | if (found) *found = '('; |
---|
123 | } |
---|
124 | else |
---|
125 | fprintf(fd, "%-20s", "??"); |
---|
126 | } |
---|
127 | else if (fmt[l] == 'L') |
---|
128 | { |
---|
129 | int n = fprintf(fd, "%s:%d", (*info[i].func != '\0' ? info[i].file : "??"), info[i].line); |
---|
130 | if (n < 20) fprintf(fd, "%*s", 20-n, " "); |
---|
131 | } |
---|
132 | else if (fmt[l] == 'i') fprintf(fd, "%d", i); |
---|
133 | else |
---|
134 | { |
---|
135 | fputc('%', fd); |
---|
136 | l--; |
---|
137 | } |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | fputc(fmt[l], fd); |
---|
142 | } |
---|
143 | l++; |
---|
144 | } |
---|
145 | i++; |
---|
146 | } |
---|
147 | return i; |
---|
148 | } |
---|
149 | |
---|
150 | int omPrintBackTrace(void** bt, int max, FILE* fd) |
---|
151 | { |
---|
152 | int i; |
---|
153 | |
---|
154 | omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH]; |
---|
155 | if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH; |
---|
156 | |
---|
157 | i = omBackTrace_2_RetInfo(bt, info, max); |
---|
158 | #ifdef OM_PRINT_RETURN_ADDRESS |
---|
159 | return omPrintRetInfo(info, i, fd, " #%i at %L in %N ra=%p\n"); |
---|
160 | #else |
---|
161 | return omPrintRetInfo(info, i, fd, " #%i at %L in %N\n"); |
---|
162 | #endif |
---|
163 | } |
---|
164 | |
---|
165 | int omPrintCurrentBackTraceMax(FILE* fd, int max) |
---|
166 | { |
---|
167 | int i; |
---|
168 | void* bt[OM_MAX_BACKTRACE_DEPTH]; |
---|
169 | if (max > OM_MAX_BACKTRACE_DEPTH) |
---|
170 | max = OM_MAX_BACKTRACE_DEPTH; |
---|
171 | if (max <= 0) return 0; |
---|
172 | i = omGetBackTrace(bt, 1, max); |
---|
173 | return omPrintBackTrace(bt, i, fd); |
---|
174 | } |
---|
175 | |
---|
176 | /************************************************************* |
---|
177 | * |
---|
178 | * Various Filters |
---|
179 | * |
---|
180 | *************************************************************/ |
---|
181 | int omFilterRetInfo_i(omRetInfo info, int max, int i) |
---|
182 | { |
---|
183 | int j=0, k=i; |
---|
184 | |
---|
185 | while (k < max) |
---|
186 | { |
---|
187 | info[j] = info[k]; |
---|
188 | j++; |
---|
189 | k++; |
---|
190 | } |
---|
191 | return j; |
---|
192 | } |
---|
193 | |
---|
194 | /************************************************************* |
---|
195 | * |
---|
196 | * Low level routines |
---|
197 | * |
---|
198 | *************************************************************/ |
---|
199 | |
---|
200 | int _omPrintBackTrace(void** bt, int max, FILE* fd , OM_FLR_DECL) |
---|
201 | { |
---|
202 | int i = 0; |
---|
203 | |
---|
204 | omRetInfo_t info[OM_MAX_BACKTRACE_DEPTH]; |
---|
205 | if (max > OM_MAX_BACKTRACE_DEPTH) max = OM_MAX_BACKTRACE_DEPTH; |
---|
206 | if (bt != NULL) |
---|
207 | { |
---|
208 | for (; i<max; i++) |
---|
209 | { |
---|
210 | if (bt[i] == NULL) |
---|
211 | { |
---|
212 | max = i+1; |
---|
213 | break; |
---|
214 | } |
---|
215 | } |
---|
216 | i = omBackTrace_2_RetInfo(bt, info, max); |
---|
217 | } |
---|
218 | #ifdef OM_TRACK_RETURN |
---|
219 | if (i == 0) |
---|
220 | i = omBackTrace_2_RetInfo(&r,info, 1); |
---|
221 | #endif |
---|
222 | #ifndef OM_INTERNAL_DEBUG |
---|
223 | if (i > 1) |
---|
224 | { |
---|
225 | #ifdef OM_TRACK_RETURN |
---|
226 | if (r != NULL) |
---|
227 | omFilterRetInfo(info, i, addr_i == r); |
---|
228 | #endif |
---|
229 | #ifdef OM_TRACK_FILE_LINE |
---|
230 | if (f != NULL && l > 0) |
---|
231 | omFilterRetInfo(info, i, strcmp(f, file_i) == 0 && l + 2 >= line_i && l - 2 <= line_i); |
---|
232 | #endif |
---|
233 | /* if we have both, use overwrite what we got from return addressse -- |
---|
234 | they sometimes are wrong */ |
---|
235 | #if defined(OM_TRACK_RETURN) && defined(OM_TRACK_FILE_LINE) |
---|
236 | if (r != NULL && info[0].addr == r && l > 0 && f != 0) |
---|
237 | { |
---|
238 | strcpy(info[0].file, f); |
---|
239 | info[0].line = l; |
---|
240 | } |
---|
241 | #endif |
---|
242 | } |
---|
243 | if (i == 0) |
---|
244 | { |
---|
245 | #endif /* ! OM_INTERNAL_DEBUG */ |
---|
246 | |
---|
247 | #ifdef OM_TRACK_FILE_LINE |
---|
248 | fprintf(fd, " %s:%d", f, l); |
---|
249 | #endif |
---|
250 | #ifdef OM_TRACK_RETURN |
---|
251 | fprintf(fd," ra=%p", r); |
---|
252 | #endif |
---|
253 | |
---|
254 | #ifndef OM_INTERNAL_DEBUG |
---|
255 | return 1; |
---|
256 | } |
---|
257 | else |
---|
258 | #endif /* ! OM_INTERNAL_DEBUG */ |
---|
259 | #ifdef OM_PRINT_RETURN_ADDRESS |
---|
260 | return omPrintRetInfo(info, i, fd, "\n #%i at %L in %N ra=%p"); |
---|
261 | #else |
---|
262 | return omPrintRetInfo(info, i, fd, "\n #%i at %L in %N"); |
---|
263 | #endif |
---|
264 | } |
---|
265 | |
---|
266 | int _omPrintCurrentBackTrace(FILE* fd , OM_FLR_DECL) |
---|
267 | { |
---|
268 | int i; |
---|
269 | void* bt[OM_MAX_BACKTRACE_DEPTH]; |
---|
270 | |
---|
271 | i = omGetBackTrace(bt, 1, OM_MAX_BACKTRACE_DEPTH); |
---|
272 | return _omPrintBackTrace(bt, i, fd , OM_FLR_VAL); |
---|
273 | } |
---|
274 | |
---|
275 | #endif /* ! OM_NDEBUG */ |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | |
---|