1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "backtrace.h"
22 #if ! HAVE_FEATURE_BACKTRACE /* no GNU backtrace implementation available */
24 #include <sal/types.h>
26 #ifdef __sun /* Solaris */
32 #include <sys/frame.h>
38 #define FRAME_PTR_OFFSET 1
39 #define FRAME_OFFSET 0
40 #define STACK_BIAS 0x7ff
44 #define FRAME_PTR_OFFSET 1
45 #define FRAME_OFFSET 0
50 #elif defined( INTEL )
52 #define FRAME_PTR_OFFSET 3
53 #define FRAME_OFFSET 0
58 #error Unknown Solaris target platform.
60 #endif /* defined SPARC or INTEL */
62 int backtrace( void **buffer
, int max_frames
)
69 /* flush register windows */
74 /* get stack- and framepointer */
77 fpval
= ((long*)(ctx
))[FRAME_PTR_OFFSET
];
78 fp
= (struct frame
*)((char*)(fpval
) + STACK_BIAS
);
80 for (i
= 0; (i
< FRAME_OFFSET
) && (fp
!= 0); i
++)
81 fp
= (struct frame
*)((char*)(fp
->fr_savfp
) + STACK_BIAS
);
83 /* iterate through backtrace */
84 for (i
= 0; (fp
!= 0) && (fp
->fr_savpc
!= 0) && (i
< max_frames
); i
++)
86 /* saved (prev) frame */
87 struct frame
* prev
= (struct frame
*)((char*)(fp
->fr_savfp
) + STACK_BIAS
);
90 *(buffer
++) = (void*)(fp
->fr_savpc
);
92 /* prev frame (w/ stack growing top down) */
93 fp
= (prev
> fp
) ? prev
: 0;
96 /* return number of frames stored */
100 char ** backtrace_symbols(void * const * buffer
, int size
)
102 (void)buffer
; (void)size
;
103 return NULL
; /*TODO*/
106 void backtrace_symbols_fd( void **buffer
, int size
, int fd
)
108 FILE *fp
= fdopen( fd
, "w" );
114 for ( pFramePtr
= buffer
; size
> 0 && pFramePtr
&& *pFramePtr
; pFramePtr
++, size
-- )
119 if ( 0 != dladdr( *pFramePtr
, &dli
) )
121 if ( dli
.dli_fname
&& dli
.dli_fbase
)
123 offset
= (ptrdiff_t)*pFramePtr
- (ptrdiff_t)dli
.dli_fbase
;
124 fprintf( fp
, "%s+0x%" SAL_PRI_PTRDIFFT
"x", dli
.dli_fname
, offset
);
126 if ( dli
.dli_sname
&& dli
.dli_saddr
)
128 offset
= (ptrdiff_t)*pFramePtr
- (ptrdiff_t)dli
.dli_saddr
;
129 fprintf( fp
, "(%s+0x%" SAL_PRI_PTRDIFFT
"x)", dli
.dli_sname
, offset
);
132 fprintf( fp
, "[%p]\n", *pFramePtr
);
139 #elif defined FREEBSD || defined NETBSD || defined OPENBSD || defined(DRAGONFLY)
147 /* no frame.h on FreeBSD */
149 struct frame
*fr_savfp
;
153 #if defined(POWERPC) || defined(POWERPC64)
155 #define FRAME_PTR_OFFSET 1
156 #define FRAME_OFFSET 0
160 #define FRAME_PTR_OFFSET 3
161 #define FRAME_OFFSET 0
165 int backtrace( void **buffer
, int max_frames
)
170 /* get stack- and framepointer */
172 fp
= (struct frame
*)(((size_t*)(ctx
))[FRAME_PTR_OFFSET
]);
173 for ( i
=0; (i
<FRAME_OFFSET
) && (fp
!=0); i
++)
175 /* iterate through backtrace */
176 for (i
=0; fp
&& fp
->fr_savpc
&& i
<max_frames
; i
++)
179 *(buffer
++) = (void *)fp
->fr_savpc
;
186 char ** backtrace_symbols(void * const * buffer
, int size
)
188 (void)buffer
; (void)size
;
189 return NULL
; /*TODO*/
192 void backtrace_symbols_fd( void **buffer
, int size
, int fd
)
194 FILE *fp
= fdopen( fd
, "w" );
199 for ( pFramePtr
= buffer
; size
> 0 && pFramePtr
&& *pFramePtr
; pFramePtr
++, size
-- )
204 if ( 0 != dladdr( *pFramePtr
, &dli
) )
206 if ( dli
.dli_fname
&& dli
.dli_fbase
)
208 offset
= (ptrdiff_t)*pFramePtr
- (ptrdiff_t)dli
.dli_fbase
;
209 fprintf( fp
, "%s+0x%" SAL_PRI_PTRDIFFT
"x", dli
.dli_fname
, offset
);
211 if ( dli
.dli_sname
&& dli
.dli_saddr
)
213 offset
= (ptrdiff_t)*pFramePtr
- (ptrdiff_t)dli
.dli_saddr
;
214 fprintf( fp
, "(%s+0x%" SAL_PRI_PTRDIFFT
"x)", dli
.dli_sname
, offset
);
217 fprintf( fp
, "[%p]\n", *pFramePtr
);
223 #else /* not GNU/BSD/Solaris */
225 int backtrace( void **buffer
, int max_frames
)
227 (void)buffer
; (void)max_frames
;
231 char ** backtrace_symbols(void * const * buffer
, int size
)
233 (void)buffer
; (void)size
;
234 return NULL
; /*TODO*/
237 void backtrace_symbols_fd( void **buffer
, int size
, int fd
)
239 (void)buffer
; (void)size
; (void)fd
;
242 #endif /* not GNU/BSD/Solaris */
244 #endif /* ! HAVE_FEATURE_BACKTRACE */
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */