My Project
Toggle main menu visibility
Loading...
Searching...
No Matches
omalloc
omRet2Info.c
Go to the documentation of this file.
1
/*******************************************************************
2
* File: omRetur2Info.c
3
* Purpose: translation of return addr to RetInfo
4
* Author: obachman (Olaf Bachmann)
5
* Created: 11/99
6
*******************************************************************/
7
#include <stdio.h>
8
#include <string.h>
9
10
#ifndef PACKAGE
11
#include "omConfig.h"
12
#endif
13
14
#ifndef OM_NDEBUG
15
16
#include "omDerivedConfig.h"
17
#include "
omStructs.h
"
18
#include "
omRet2Info.h
"
19
#include "
omGetBackTrace.h
"
20
21
#ifndef MAXPATHLEN
22
#define MAXPATHLEN 1024
23
#endif
24
25
/* define to also print return addresses in (default)
26
output of omPrintInfo */
27
/* #define OM_PRINT_RETURN_ADDRESS */
28
static
char
om_this_prog
[
MAXPATHLEN
] =
""
;
29
30
#ifndef OM_MAX_BACKTRACE_DEPTH
31
#define OM_MAX_BACKTRACE_DEPTH 16
32
#endif
33
34
void
omInitRet_2_Info
(
const
char
*
argv0
)
35
{
36
// char buf[MAXPATHLEN];
37
38
if
(
argv0
!=
NULL
)
// // && omFindExec(argv0, buf))
39
{
40
strncpy(
om_this_prog
,
argv0
,
MAXPATHLEN
);
// // buf);
41
om_this_prog
[
MAXPATHLEN
- 1]=
'\0'
;
42
}
43
}
44
45
46
int
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
= snprintf(
command
,2*
MAXPATHLEN
+ 15 +
OM_MAX_BACKTRACE_DEPTH
*(2*SIZEOF_VOIDP + 4),
70
"%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:
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
return
filled;
95
}
96
#endif
97
return
j
;
98
}
99
100
int
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
151
int
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
166
int
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
*************************************************************/
182
int
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
201
int
_omPrintBackTrace
(
void
** bt,
int
max
, FILE*
fd
, OM_FLR_DECL)
202
{
203
#ifndef __OPTIMIZE__
204
int
i
= 0;
205
206
omRetInfo_t
info
[
OM_MAX_BACKTRACE_DEPTH
];
207
if
(
max
>
OM_MAX_BACKTRACE_DEPTH
)
max
=
OM_MAX_BACKTRACE_DEPTH
;
208
if
(bt !=
NULL
)
209
{
210
for
(;
i
<
max
;
i
++)
211
{
212
if
(bt[
i
] ==
NULL
)
213
{
214
max
=
i
+1;
215
break
;
216
}
217
}
218
i
=
omBackTrace_2_RetInfo
(bt,
info
,
max
);
219
}
220
#ifdef OM_TRACK_RETURN
221
if
(
i
== 0)
222
i
=
omBackTrace_2_RetInfo
(((
void
*)&r),
info
, 1);
223
#endif
224
#ifndef OM_INTERNAL_DEBUG
225
if
(
i
> 1)
226
{
227
#ifdef OM_TRACK_RETURN
228
if
(r !=
NULL
)
229
omFilterRetInfo
(
info
,
i
, addr_i == r);
230
#endif
231
#ifdef OM_TRACK_FILE_LINE
232
if
(
f
!=
NULL
&&
l
> 0)
233
omFilterRetInfo
(
info
,
i
, strcmp(
f
, file_i) == 0 &&
l
+ 2 >= line_i &&
l
- 2 <= line_i);
234
#endif
235
/* if we have both, use overwrite what we got from return addressse --
236
they sometimes are wrong */
237
#if defined(OM_TRACK_RETURN) && defined(OM_TRACK_FILE_LINE)
238
if
(r !=
NULL
&&
info
[0].addr == r &&
l
> 0 &&
f
!= 0)
239
{
240
strcpy(
info
[0].file,
f
);
241
info
[0].line =
l
;
242
}
243
#endif
244
}
245
if
(
i
== 0)
246
{
247
#endif
/* ! OM_INTERNAL_DEBUG */
248
249
#ifdef OM_TRACK_FILE_LINE
250
fprintf(
fd
,
" %s:%d"
,
f
,
l
);
251
#endif
252
#ifdef OM_TRACK_RETURN
253
fprintf(
fd
,
" ra=%p"
, r);
254
#endif
255
256
#ifndef OM_INTERNAL_DEBUG
257
return
1;
258
}
259
else
260
#endif
/* ! OM_INTERNAL_DEBUG */
261
#ifdef OM_PRINT_RETURN_ADDRESS
262
return
omPrintRetInfo
(
info
,
i
,
fd
,
"\n #%i at %L in %N ra=%p"
);
263
#else
264
return
omPrintRetInfo
(
info
,
i
,
fd
,
"\n #%i at %L in %N"
);
265
#endif
266
#else
267
return
0;
268
#endif
269
}
270
271
int
_omPrintCurrentBackTrace
(FILE*
fd
, OM_FLR_DECL)
272
{
273
#ifdef __OPTIMIZE__
274
/* does not work without -g */
275
return
0;
276
#else
277
int
i
;
278
void
* bt[
OM_MAX_BACKTRACE_DEPTH
];
279
280
i
=
omGetBackTrace
(bt, 1,
OM_MAX_BACKTRACE_DEPTH
);
281
return
_omPrintBackTrace
(bt,
i
,
fd
, OM_FLR_VAL);
282
#endif
283
}
284
#endif
/* ! OM_NDEBUG */
l
int l
Definition
cfEzgcd.cc:100
i
int i
Definition
cfEzgcd.cc:132
k
int k
Definition
cfEzgcd.cc:99
f
FILE * f
Definition
checklibs.c:9
argv0
char * argv0
Definition
coeffs_test_runner.cpp:14
found
bool found
Definition
facFactorize.cc:55
j
int j
Definition
facHensel.cc:110
max
static int max(int a, int b)
Definition
fast_mult.cc:264
command
ip_command * command
Definition
ipid.h:23
info
#define info
Definition
libparse.cc:1256
omGetBackTrace.h
omGetBackTrace
#define omGetBackTrace(bt, s, max)
Definition
omGetBackTrace.h:15
NULL
#define NULL
Definition
omList.c:12
omPrintRetInfo
int omPrintRetInfo(omRetInfo info, int max, FILE *fd, const char *fmt)
Definition
omRet2Info.c:100
omBackTrace_2_RetInfo
int omBackTrace_2_RetInfo(void **bt, omRetInfo info, int max)
Definition
omRet2Info.c:46
_omPrintBackTrace
int _omPrintBackTrace(void **bt, int max, FILE *fd, OM_FLR_DECL)
Definition
omRet2Info.c:201
_omPrintCurrentBackTrace
int _omPrintCurrentBackTrace(FILE *fd, OM_FLR_DECL)
Definition
omRet2Info.c:271
om_this_prog
static char om_this_prog[MAXPATHLEN]
Definition
omRet2Info.c:28
omFilterRetInfo_i
int omFilterRetInfo_i(omRetInfo info, int max, int i)
Definition
omRet2Info.c:182
omPrintBackTrace
int omPrintBackTrace(void **bt, int max, FILE *fd)
Definition
omRet2Info.c:151
OM_MAX_BACKTRACE_DEPTH
#define OM_MAX_BACKTRACE_DEPTH
Definition
omRet2Info.c:31
MAXPATHLEN
#define MAXPATHLEN
Definition
omRet2Info.c:22
omRet2Info.h
omFilterRetInfo
#define omFilterRetInfo(info, max, cond)
Definition
omRet2Info.h:44
omStructs.h
omRetInfo
omRetInfo_t * omRetInfo
Definition
omStructs.h:34
fd
int status int fd
Definition
si_signals.h:69
omPrintCurrentBackTraceMax
#define omPrintCurrentBackTraceMax(A, B)
Definition
xalloc.h:265
omInitRet_2_Info
#define omInitRet_2_Info(argv0)
Definition
xalloc.h:274
Generated on
for My Project by
doxygen 1.17.0
for
Singular