xref: /aoo42x/main/sal/osl/unx/backtrace.c (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #ifdef SOLARIS
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <dlfcn.h>
32*cdf0e10cSrcweir #include <pthread.h>
33*cdf0e10cSrcweir #include <setjmp.h>
34*cdf0e10cSrcweir #include <stdio.h>
35*cdf0e10cSrcweir #include <sys/frame.h>
36*cdf0e10cSrcweir #include "backtrace.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #if defined(SPARC)
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #if defined IS_LP64
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1
43*cdf0e10cSrcweir #define FRAME_OFFSET     0
44*cdf0e10cSrcweir #define STACK_BIAS       0x7ff
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #else
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1
49*cdf0e10cSrcweir #define FRAME_OFFSET     0
50*cdf0e10cSrcweir #define STACK_BIAS       0
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #endif
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #elif defined( INTEL )
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #define FRAME_PTR_OFFSET 3
57*cdf0e10cSrcweir #define FRAME_OFFSET     0
58*cdf0e10cSrcweir #define STACK_BIAS       0
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #else
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir #error Unknown Solaris target platform.
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir #endif /* defined SPARC or INTEL */
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir int backtrace( void **buffer, int max_frames )
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	jmp_buf       ctx;
70*cdf0e10cSrcweir 	long          fpval;
71*cdf0e10cSrcweir 	struct frame *fp;
72*cdf0e10cSrcweir 	int i;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	/* flush register windows */
75*cdf0e10cSrcweir #ifdef SPARC
76*cdf0e10cSrcweir 	asm("ta 3");
77*cdf0e10cSrcweir #endif
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	/* get stack- and framepointer */
80*cdf0e10cSrcweir 	setjmp(ctx);
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	fpval = ((long*)(ctx))[FRAME_PTR_OFFSET];
83*cdf0e10cSrcweir 	fp = (struct frame*)((char*)(fpval) + STACK_BIAS);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++)
86*cdf0e10cSrcweir 		fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	/* iterate through backtrace */
89*cdf0e10cSrcweir 	for (i = 0; (fp != 0) && (fp->fr_savpc != 0) && (i < max_frames); i++)
90*cdf0e10cSrcweir 	{
91*cdf0e10cSrcweir 		/* saved (prev) frame */
92*cdf0e10cSrcweir 		struct frame * prev = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 		/* store frame */
95*cdf0e10cSrcweir 		*(buffer++) = (void*)(fp->fr_savpc);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 		/* prev frame (w/ stack growing top down) */
98*cdf0e10cSrcweir 		fp = (prev > fp) ? prev : 0;
99*cdf0e10cSrcweir 	}
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	/* return number of frames stored */
102*cdf0e10cSrcweir 	return i;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	FILE	*fp = fdopen( fd, "w" );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	if ( fp )
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		void **pFramePtr;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
114*cdf0e10cSrcweir 		{
115*cdf0e10cSrcweir 			Dl_info		dli;
116*cdf0e10cSrcweir 			ptrdiff_t	offset;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 			if ( 0 != dladdr( *pFramePtr, &dli ) )
119*cdf0e10cSrcweir 			{
120*cdf0e10cSrcweir 				if ( dli.dli_fname && dli.dli_fbase )
121*cdf0e10cSrcweir 				{
122*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
123*cdf0e10cSrcweir 					fprintf( fp, "%s+0x%x", dli.dli_fname, offset );
124*cdf0e10cSrcweir 				}
125*cdf0e10cSrcweir 				if ( dli.dli_sname && dli.dli_saddr )
126*cdf0e10cSrcweir 				{
127*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
128*cdf0e10cSrcweir 					fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset );
129*cdf0e10cSrcweir 				}
130*cdf0e10cSrcweir 			}
131*cdf0e10cSrcweir 			fprintf( fp, "[0x%x]\n", *pFramePtr );
132*cdf0e10cSrcweir 		}
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		fflush( fp );
135*cdf0e10cSrcweir 		fclose( fp );
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir #endif /* defined SOLARIS */
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir #if defined FREEBSD || defined NETBSD
143*cdf0e10cSrcweir #include <dlfcn.h>
144*cdf0e10cSrcweir #include <pthread.h>
145*cdf0e10cSrcweir #include <setjmp.h>
146*cdf0e10cSrcweir #include <stddef.h>
147*cdf0e10cSrcweir #include <stdio.h>
148*cdf0e10cSrcweir #include "backtrace.h"
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1
151*cdf0e10cSrcweir #define FRAME_OFFSET 0
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir int backtrace( void **buffer, int max_frames )
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir 	struct frame *fp;
156*cdf0e10cSrcweir 	jmp_buf ctx;
157*cdf0e10cSrcweir 	int i;
158*cdf0e10cSrcweir 	/* get stack- and framepointer */
159*cdf0e10cSrcweir 	setjmp(ctx);
160*cdf0e10cSrcweir 	fp = (struct frame*)(((size_t*)(ctx))[FRAME_PTR_OFFSET]);
161*cdf0e10cSrcweir 	for ( i=0; (i<FRAME_OFFSET) && (fp!=0); i++)
162*cdf0e10cSrcweir 		fp = fp->fr_savfp;
163*cdf0e10cSrcweir 	/* iterate through backtrace */
164*cdf0e10cSrcweir 	for (i=0; fp && fp->fr_savpc && i<max_frames; i++)
165*cdf0e10cSrcweir 	{
166*cdf0e10cSrcweir 		/* store frame */
167*cdf0e10cSrcweir 		*(buffer++) = (void *)fp->fr_savpc;
168*cdf0e10cSrcweir 		/* next frame */
169*cdf0e10cSrcweir 		fp=fp->fr_savfp;
170*cdf0e10cSrcweir 	}
171*cdf0e10cSrcweir 	return i;
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd )
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir 	FILE	*fp = fdopen( fd, "w" );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	if ( fp )
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 		void **pFramePtr;
181*cdf0e10cSrcweir 		for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
182*cdf0e10cSrcweir 		{
183*cdf0e10cSrcweir 			Dl_info		dli;
184*cdf0e10cSrcweir 			ptrdiff_t	offset;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 			if ( 0 != dladdr( *pFramePtr, &dli ) )
187*cdf0e10cSrcweir 			{
188*cdf0e10cSrcweir 				if ( dli.dli_fname && dli.dli_fbase )
189*cdf0e10cSrcweir 				{
190*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
191*cdf0e10cSrcweir 					fprintf( fp, "%s+0x%x", dli.dli_fname, offset );
192*cdf0e10cSrcweir 				}
193*cdf0e10cSrcweir 				if ( dli.dli_sname && dli.dli_saddr )
194*cdf0e10cSrcweir 				{
195*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
196*cdf0e10cSrcweir 					fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset );
197*cdf0e10cSrcweir 				}
198*cdf0e10cSrcweir 			}
199*cdf0e10cSrcweir 			fprintf( fp, "[0x%x]\n", *pFramePtr );
200*cdf0e10cSrcweir 		}
201*cdf0e10cSrcweir 		fflush( fp );
202*cdf0e10cSrcweir 		fclose( fp );
203*cdf0e10cSrcweir 	}
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir #endif /* defined FREEBSD */
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir #ifdef LINUX
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir #ifndef _GNU_SOURCE
210*cdf0e10cSrcweir #define _GNU_SOURCE
211*cdf0e10cSrcweir #endif
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir #include <dlfcn.h>
214*cdf0e10cSrcweir #include <pthread.h>
215*cdf0e10cSrcweir #include <setjmp.h>
216*cdf0e10cSrcweir #include <stdio.h>
217*cdf0e10cSrcweir #include "backtrace.h"
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir #if defined(SPARC)
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir #define FRAME_PTR_OFFSET 1
222*cdf0e10cSrcweir #define FRAME_OFFSET 0
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir #else
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir #error Unknown Linux target platform.
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir #endif /* defined SPARC or INTEL */
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir typedef int ptrdiff_t;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir int backtrace( void **buffer, int max_frames )
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir 	struct frame *fp;
235*cdf0e10cSrcweir 	jmp_buf ctx;
236*cdf0e10cSrcweir 	int i;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	/* flush register windows */
239*cdf0e10cSrcweir #ifdef SPARC
240*cdf0e10cSrcweir 	asm("ta 3");
241*cdf0e10cSrcweir #endif
242*cdf0e10cSrcweir 	/* get stack- and framepointer */
243*cdf0e10cSrcweir 	setjmp(ctx);
244*cdf0e10cSrcweir 	fp = (struct frame*)(((size_t*)(ctx))[FRAME_PTR_OFFSET]);
245*cdf0e10cSrcweir 	for ( i=0; (i<FRAME_OFFSET) && (fp!=0); i++)
246*cdf0e10cSrcweir 		fp = fp->fr_savfp;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	/* iterate through backtrace */
249*cdf0e10cSrcweir 	for (i=0; fp && fp->fr_savpc && i<max_frames; i++)
250*cdf0e10cSrcweir 	{
251*cdf0e10cSrcweir 		/* store frame */
252*cdf0e10cSrcweir 		*(buffer++) = (void *)fp->fr_savpc;
253*cdf0e10cSrcweir 		/* next frame */
254*cdf0e10cSrcweir 		fp=fp->fr_savfp;
255*cdf0e10cSrcweir 	}
256*cdf0e10cSrcweir 	return i;
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd )
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir 	FILE	*fp = fdopen( fd, "w" );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 	if ( fp )
264*cdf0e10cSrcweir 	{
265*cdf0e10cSrcweir 		void **pFramePtr;
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 		for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
268*cdf0e10cSrcweir 		{
269*cdf0e10cSrcweir 			Dl_info		dli;
270*cdf0e10cSrcweir 			ptrdiff_t	offset;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 			if ( 0 != dladdr( *pFramePtr, &dli ) )
273*cdf0e10cSrcweir 			{
274*cdf0e10cSrcweir 				if ( dli.dli_fname && dli.dli_fbase )
275*cdf0e10cSrcweir 				{
276*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
277*cdf0e10cSrcweir 					fprintf( fp, "%s+0x%x", dli.dli_fname, offset );
278*cdf0e10cSrcweir 				}
279*cdf0e10cSrcweir 				if ( dli.dli_sname && dli.dli_saddr )
280*cdf0e10cSrcweir 				{
281*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
282*cdf0e10cSrcweir 					fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset );
283*cdf0e10cSrcweir 				}
284*cdf0e10cSrcweir 			}
285*cdf0e10cSrcweir 			fprintf( fp, "[0x%x]\n", *pFramePtr );
286*cdf0e10cSrcweir 		}
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 		fflush( fp );
289*cdf0e10cSrcweir 		fclose( fp );
290*cdf0e10cSrcweir 	}
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir #endif /* defined LINUX */
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir #if defined( MACOSX )
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir #include <dlfcn.h>
298*cdf0e10cSrcweir #include <stdio.h>
299*cdf0e10cSrcweir #include "backtrace.h"
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir typedef unsigned	 ptrdiff_t;
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir /* glib backtrace is only available on MacOsX 10.5 or higher
304*cdf0e10cSrcweir    so we do it on our own */
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir int backtrace( void **buffer, int max_frames )
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir     void **frame = (void **)__builtin_frame_address(0);
309*cdf0e10cSrcweir     void **bp = ( void **)(*frame);
310*cdf0e10cSrcweir     void *ip = frame[1];
311*cdf0e10cSrcweir     int	i;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 	for ( i = 0; bp && ip && i < max_frames; i++ )
314*cdf0e10cSrcweir 	{
315*cdf0e10cSrcweir 		*(buffer++) = ip;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         ip = bp[1];
318*cdf0e10cSrcweir         bp = (void**)(bp[0]);
319*cdf0e10cSrcweir 	}
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	return i;
322*cdf0e10cSrcweir }
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir void backtrace_symbols_fd( void **buffer, int size, int fd )
326*cdf0e10cSrcweir {
327*cdf0e10cSrcweir 	FILE	*fp = fdopen( fd, "w" );
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 	if ( fp )
330*cdf0e10cSrcweir 	{
331*cdf0e10cSrcweir 		void **pFramePtr;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		for ( pFramePtr = buffer; size > 0 && pFramePtr && *pFramePtr; pFramePtr++, size-- )
334*cdf0e10cSrcweir 		{
335*cdf0e10cSrcweir 			Dl_info		dli;
336*cdf0e10cSrcweir 			ptrdiff_t	offset;
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 			if ( 0 != dladdr( *pFramePtr, &dli ) )
339*cdf0e10cSrcweir 			{
340*cdf0e10cSrcweir 				if ( dli.dli_fname && dli.dli_fbase )
341*cdf0e10cSrcweir 				{
342*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_fbase;
343*cdf0e10cSrcweir 					fprintf( fp, "%s+0x%x", dli.dli_fname, offset );
344*cdf0e10cSrcweir 				}
345*cdf0e10cSrcweir 				if ( dli.dli_sname && dli.dli_saddr )
346*cdf0e10cSrcweir 				{
347*cdf0e10cSrcweir 					offset = (ptrdiff_t)*pFramePtr - (ptrdiff_t)dli.dli_saddr;
348*cdf0e10cSrcweir 					fprintf( fp, "(%s+0x%x)", dli.dli_sname, offset );
349*cdf0e10cSrcweir 				}
350*cdf0e10cSrcweir 			}
351*cdf0e10cSrcweir 			fprintf( fp, "[0x%x]\n", (unsigned int)*pFramePtr );
352*cdf0e10cSrcweir 		}
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir 		fflush( fp );
355*cdf0e10cSrcweir 		fclose( fp );
356*cdf0e10cSrcweir 	}
357*cdf0e10cSrcweir }
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir #endif /* defined MACOSX */
360