merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / diagnose.c
blob02967b3ad7f7c2731f977e6a0b3e164b5dd5dde0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 #include "osl/diagnose.h"
29 #include "system.h"
31 #ifndef HAVE_DLFCN_H
33 #if defined(LINUX) || defined(SOLARIS)
34 #define HAVE_DLFCN_H
35 #endif /* LINUX || SOLARIS */
37 #endif /* HAVE_DLFCN_H */
40 #ifdef HAVE_DLFCN_H
42 #ifndef INCLUDED_DLFCN_H
43 #include <dlfcn.h>
44 #define INCLUDED_DLFCN_H
45 #endif
47 #endif /* HAVE_DLFCN_H */
48 #include "osl/thread.h"
50 #ifndef INCLUDED_PTHREAD_H
51 #include <pthread.h>
52 #define INCLUDED_PTHREAD_H
53 #endif
55 #ifndef INCLUDED_STDDEF_H
56 #include <stddef.h>
57 #define INCLUDED_STDDEF_H
58 #endif
60 #include "printtrace.h"
62 /************************************************************************/
63 /* Internal data structures and functions */
64 /************************************************************************/
66 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
68 typedef pfunc_osl_printDebugMessage oslDebugMessageFunc;
69 static oslDebugMessageFunc volatile g_pDebugMessageFunc = 0;
71 typedef pfunc_osl_printDetailedDebugMessage oslDetailedDebugMessageFunc;
72 static oslDetailedDebugMessageFunc volatile g_pDetailedDebugMessageFunc = 0;
74 static void osl_diagnose_backtrace_Impl (
75 oslDebugMessageFunc f);
77 #define OSL_DIAGNOSE_OUTPUTMESSAGE(f, s) \
78 ((f != 0) ? (*(f))((s)) : (void)fprintf(stderr, "%s", (s)))
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)
89 const char *fname = 0, *sname = 0;
90 void *fbase = 0, *saddr = 0;
91 ptrdiff_t offset;
92 char szMessage[1024];
94 #ifdef INCLUDED_DLFCN_H
95 Dl_info dli;
96 if (dladdr (pc, &dli) != 0)
98 fname = dli.dli_fname;
99 fbase = dli.dli_fbase;
100 sname = dli.dli_sname;
101 saddr = dli.dli_saddr;
103 #endif /* INCLUDED_DLFCN_H */
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);
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);
119 OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
121 #endif
123 /************************************************************************/
124 /* osl_diagnose_backtrace_Impl */
125 /************************************************************************/
126 #if defined(LINUX)
128 #include <execinfo.h>
130 #define FRAME_COUNT 64
131 #define FRAME_OFFSET 1
133 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
135 void * ppFrames[FRAME_COUNT];
136 int i, n;
138 n = backtrace (ppFrames, FRAME_COUNT);
139 for (i = FRAME_OFFSET; i < n; i++)
141 osl_diagnose_frame_Impl (f, (i - FRAME_OFFSET), ppFrames[i]);
145 #elif defined(SOLARIS)
147 #include <pthread.h>
148 #include <setjmp.h>
149 #include <sys/frame.h>
151 #if defined(SPARC)
153 #if defined IS_LP64
155 #define FRAME_PTR_OFFSET 1
156 #define FRAME_OFFSET 0
157 #define STACK_BIAS 0x7ff
159 #else
161 #define FRAME_PTR_OFFSET 1
162 #define FRAME_OFFSET 0
163 #define STACK_BIAS 0
165 #endif
167 #elif defined(INTEL)
169 #define FRAME_PTR_OFFSET 3
170 #define FRAME_OFFSET 0
171 #define STACK_BIAS 0
173 #endif /* (SPARC || INTEL) */
175 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
177 jmp_buf ctx;
178 long fpval;
179 struct frame * fp;
180 int i;
182 #if defined(SPARC)
183 asm("ta 3");
184 #endif /* SPARC */
185 setjmp (ctx);
187 fpval = ((long*)(ctx))[FRAME_PTR_OFFSET];
188 fp = (struct frame*)((char*)(fpval) + STACK_BIAS);
190 for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++)
191 fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
193 for (i = 0; (fp != 0) && (fp->fr_savpc != 0); i++)
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;
201 #else /* (LINUX || SOLARIS) */
203 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
205 /* not yet implemented */
208 #endif /* (LINUX || SOLARIS) */
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)
218 oslDebugMessageFunc f = g_pDebugMessageFunc;
219 char szMessage[1024];
221 /* If there's a callback for detailed messages, use it */
222 if ( g_pDetailedDebugMessageFunc != NULL )
224 g_pDetailedDebugMessageFunc( pszFileName, nLine, pszMessage );
225 return sal_False;
228 /* if SAL assertions are disabled in general, stop here */
229 if ( getenv("DISABLE_SAL_DBGBOX") )
230 return sal_False;
232 /* format message into buffer */
233 if (pszMessage != 0)
235 snprintf(szMessage, sizeof(szMessage),
236 "Error: File %s, Line %" SAL_PRIdINT32 ": %s\n",
237 pszFileName, nLine, pszMessage);
239 else
241 snprintf(szMessage, sizeof(szMessage),
242 "Error: File %s, Line %" SAL_PRIdINT32 "\n",
243 pszFileName, nLine);
246 /* acquire lock to serialize output message(s) */
247 pthread_mutex_lock(&g_mutex);
249 /* output message buffer */
250 OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
252 /* output backtrace */
253 osl_diagnose_backtrace_Impl(f);
255 /* release lock and leave, w/o calling osl_breakDebug() */
256 pthread_mutex_unlock(&g_mutex);
257 return sal_False;
260 /************************************************************************/
261 /* osl_breakDebug */
262 /************************************************************************/
263 void SAL_CALL osl_breakDebug()
265 exit(0);
268 /************************************************************************/
269 /* osl_reportError */
270 /************************************************************************/
271 sal_Int32 SAL_CALL osl_reportError (
272 sal_uInt32 nType,
273 const sal_Char* pszMessage)
275 (void) nType; /* unused */
276 fputs(pszMessage, stderr);
277 return 0;
280 /************************************************************************/
281 /* osl_setDebugMessageFunc */
282 /************************************************************************/
283 oslDebugMessageFunc SAL_CALL osl_setDebugMessageFunc (
284 oslDebugMessageFunc pNewFunc)
286 oslDebugMessageFunc pOldFunc = g_pDebugMessageFunc;
287 g_pDebugMessageFunc = pNewFunc;
288 return pOldFunc;
291 /************************************************************************/
292 /* osl_setDetailedDebugMessageFunc */
293 /************************************************************************/
294 pfunc_osl_printDetailedDebugMessage SAL_CALL osl_setDetailedDebugMessageFunc (
295 pfunc_osl_printDetailedDebugMessage pNewFunc)
297 oslDetailedDebugMessageFunc pOldFunc = g_pDetailedDebugMessageFunc;
298 g_pDetailedDebugMessageFunc = pNewFunc;
299 return pOldFunc;
302 /************************************************************************/
303 /* osl_trace */
304 /************************************************************************/
305 void osl_trace(char const * pszFormat, ...) {
306 va_list args;
307 va_start(args, pszFormat);
308 printTrace((unsigned long) getpid(), pszFormat, args);
309 va_end(args);