xref: /aoo41x/main/sal/osl/unx/diagnose.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "osl/diagnose.h"
29 #include "system.h"
30 
31 #ifndef HAVE_DLFCN_H
32 
33 #if defined(LINUX) || defined(SOLARIS)
34 #define HAVE_DLFCN_H
35 #endif  /* LINUX || SOLARIS */
36 
37 #endif  /* HAVE_DLFCN_H */
38 
39 
40 #ifdef  HAVE_DLFCN_H
41 
42 #ifndef INCLUDED_DLFCN_H
43 #include <dlfcn.h>
44 #define INCLUDED_DLFCN_H
45 #endif
46 
47 #endif  /* HAVE_DLFCN_H */
48 #include "osl/thread.h"
49 
50 #ifndef INCLUDED_PTHREAD_H
51 #include <pthread.h>
52 #define INCLUDED_PTHREAD_H
53 #endif
54 
55 #ifndef INCLUDED_STDDEF_H
56 #include <stddef.h>
57 #define INCLUDED_STDDEF_H
58 #endif
59 
60 #include "printtrace.h"
61 
62 /************************************************************************/
63 /* Internal data structures and functions */
64 /************************************************************************/
65 
66 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
67 
68 typedef pfunc_osl_printDebugMessage oslDebugMessageFunc;
69 static oslDebugMessageFunc volatile g_pDebugMessageFunc = 0;
70 
71 typedef pfunc_osl_printDetailedDebugMessage oslDetailedDebugMessageFunc;
72 static oslDetailedDebugMessageFunc volatile g_pDetailedDebugMessageFunc = 0;
73 
74 static void osl_diagnose_backtrace_Impl (
75 	oslDebugMessageFunc f);
76 
77 #define OSL_DIAGNOSE_OUTPUTMESSAGE(f, s) \
78 ((f != 0) ? (*(f))((s)) : (void)fprintf(stderr, "%s", (s)))
79 
80 #if defined (LINUX) || defined (SOLARIS)
81 /************************************************************************/
82 /* osl_diagnose_frame_Impl */
83 /************************************************************************/
84 static void osl_diagnose_frame_Impl (
85 	oslDebugMessageFunc f,
86 	int                 depth,
87 	void *              pc)
88 {
89 	const char *fname = 0, *sname = 0;
90 	void       *fbase = 0, *saddr = 0;
91 	ptrdiff_t   offset;
92 	char        szMessage[1024];
93 
94 #ifdef INCLUDED_DLFCN_H
95 	Dl_info dli;
96 	if (dladdr (pc, &dli) != 0)
97 	{
98 		fname = dli.dli_fname;
99 		fbase = dli.dli_fbase;
100 		sname = dli.dli_sname;
101 		saddr = dli.dli_saddr;
102 	}
103 #endif /* INCLUDED_DLFCN_H */
104 
105 	if (saddr)
106 		offset = (ptrdiff_t)(pc) - (ptrdiff_t)(saddr);
107 	else if (fbase)
108 		offset = (ptrdiff_t)(pc) - (ptrdiff_t)(fbase);
109 	else
110 		offset = (ptrdiff_t)(pc);
111 
112 	snprintf (szMessage, sizeof(szMessage),
113 			  "Backtrace: [%d] %s: %s+0x%" SAL_PRI_PTRDIFFT "x\n",
114 			  depth,
115 			  fname ? fname : "<unknown>",
116 			  sname ? sname : "???",
117 			  offset);
118 
119     OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
120 }
121 #endif
122 
123 /************************************************************************/
124 /* osl_diagnose_backtrace_Impl */
125 /************************************************************************/
126 #if defined(LINUX)
127 
128 #include <execinfo.h>
129 
130 #define FRAME_COUNT 64
131 #define FRAME_OFFSET 1
132 
133 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
134 {
135 	void * ppFrames[FRAME_COUNT];
136 	int    i, n;
137 
138 	n = backtrace (ppFrames, FRAME_COUNT);
139 	for (i = FRAME_OFFSET; i < n; i++)
140 	{
141 		osl_diagnose_frame_Impl (f, (i - FRAME_OFFSET), ppFrames[i]);
142 	}
143 }
144 
145 #elif defined(SOLARIS)
146 
147 #include <pthread.h>
148 #include <setjmp.h>
149 #include <sys/frame.h>
150 
151 #if defined(SPARC)
152 
153 #if defined IS_LP64
154 
155 #define FRAME_PTR_OFFSET 1
156 #define FRAME_OFFSET     0
157 #define STACK_BIAS       0x7ff
158 
159 #else
160 
161 #define FRAME_PTR_OFFSET 1
162 #define FRAME_OFFSET     0
163 #define STACK_BIAS       0
164 
165 #endif
166 
167 #elif defined(INTEL)
168 
169 #define FRAME_PTR_OFFSET 3
170 #define FRAME_OFFSET     0
171 #define STACK_BIAS       0
172 
173 #endif /* (SPARC || INTEL) */
174 
175 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
176 {
177 	jmp_buf        ctx;
178 	long           fpval;
179 	struct frame * fp;
180 	int            i;
181 
182 #if defined(SPARC)
183 	asm("ta 3");
184 #endif /* SPARC */
185 	setjmp (ctx);
186 
187 	fpval = ((long*)(ctx))[FRAME_PTR_OFFSET];
188 	fp = (struct frame*)((char*)(fpval) + STACK_BIAS);
189 
190 	for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++)
191 		fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
192 
193 	for (i = 0; (fp != 0) && (fp->fr_savpc != 0); i++)
194 	{
195 		struct frame * prev = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
196 		osl_diagnose_frame_Impl (f, i, (void*)(fp->fr_savpc));
197 		fp = (prev > fp) ? prev : 0;
198 	}
199 }
200 
201 #else  /* (LINUX || SOLARIS) */
202 
203 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
204 {
205 	/* not yet implemented */
206 }
207 
208 #endif /* (LINUX || SOLARIS) */
209 
210 /************************************************************************/
211 /* osl_assertFailedLine */
212 /************************************************************************/
213 sal_Bool SAL_CALL osl_assertFailedLine (
214 	const sal_Char* pszFileName,
215 	sal_Int32       nLine,
216 	const sal_Char* pszMessage)
217 {
218 	oslDebugMessageFunc f = g_pDebugMessageFunc;
219 	char                szMessage[1024];
220 
221     // after reporting the assertion, abort if told so by SAL_DIAGNOSE_ABORT, but *not* if
222     // assertions are routed to some external instance
223     char const * env = getenv( "SAL_DIAGNOSE_ABORT" );
224     sal_Bool const doAbort = ( ( env != NULL ) && ( *env != '\0' ) && ( f == NULL ) );
225 
226     /* If there's a callback for detailed messages, use it */
227     if ( g_pDetailedDebugMessageFunc != NULL )
228     {
229         g_pDetailedDebugMessageFunc( pszFileName, nLine, pszMessage );
230         return sal_False;
231     }
232 
233     /* if SAL assertions are disabled in general, stop here */
234     if ( getenv("DISABLE_SAL_DBGBOX") )
235         return doAbort;
236 
237 	/* format message into buffer */
238 	if (pszMessage != 0)
239 	{
240 		snprintf(szMessage, sizeof(szMessage),
241 				 "Error: File %s, Line %" SAL_PRIdINT32 ": %s\n",
242 				 pszFileName, nLine, pszMessage);
243 	}
244 	else
245 	{
246 		snprintf(szMessage, sizeof(szMessage),
247 				 "Error: File %s, Line %" SAL_PRIdINT32 "\n",
248 				 pszFileName, nLine);
249 	}
250 
251 	/* acquire lock to serialize output message(s) */
252 	pthread_mutex_lock(&g_mutex);
253 
254 	/* output message buffer */
255     OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
256 
257 	/* output backtrace */
258 	osl_diagnose_backtrace_Impl(f);
259 
260 	/* release lock and leave */
261 	pthread_mutex_unlock(&g_mutex);
262 
263     return doAbort;
264 }
265 
266 /************************************************************************/
267 /* osl_breakDebug */
268 /************************************************************************/
269 void SAL_CALL osl_breakDebug()
270 {
271 	abort();
272 }
273 
274 /************************************************************************/
275 /* osl_reportError */
276 /************************************************************************/
277 sal_Int32 SAL_CALL osl_reportError (
278 	sal_uInt32      nType,
279 	const sal_Char* pszMessage)
280 {
281     (void) nType; /* unused */
282 	fputs(pszMessage, stderr);
283 	return 0;
284 }
285 
286 /************************************************************************/
287 /* osl_setDebugMessageFunc */
288 /************************************************************************/
289 oslDebugMessageFunc SAL_CALL osl_setDebugMessageFunc (
290 	oslDebugMessageFunc pNewFunc)
291 {
292 	oslDebugMessageFunc pOldFunc = g_pDebugMessageFunc;
293 	g_pDebugMessageFunc = pNewFunc;
294 	return pOldFunc;
295 }
296 
297 /************************************************************************/
298 /* osl_setDetailedDebugMessageFunc */
299 /************************************************************************/
300 pfunc_osl_printDetailedDebugMessage SAL_CALL osl_setDetailedDebugMessageFunc (
301     pfunc_osl_printDetailedDebugMessage pNewFunc)
302 {
303     oslDetailedDebugMessageFunc pOldFunc = g_pDetailedDebugMessageFunc;
304     g_pDetailedDebugMessageFunc = pNewFunc;
305     return pOldFunc;
306 }
307 
308 /************************************************************************/
309 /* osl_trace */
310 /************************************************************************/
311 void osl_trace(char const * pszFormat, ...) {
312     va_list args;
313     va_start(args, pszFormat);
314     printTrace((unsigned long) getpid(), pszFormat, args);
315     va_end(args);
316 }
317