update credits
[LibreOffice.git] / sal / osl / unx / diagnose.cxx
blob99cb9e94e63949d9830d0ed4e5299d0d32c83bc9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "osl/diagnose.h"
21 #include "system.h"
23 #ifndef HAVE_DLFCN_H
25 #if defined(LINUX) || defined(SOLARIS)
26 #define HAVE_DLFCN_H
27 #endif /* LINUX || SOLARIS */
29 #endif /* HAVE_DLFCN_H */
32 #ifdef HAVE_DLFCN_H
34 #ifndef INCLUDED_DLFCN_H
35 #include <dlfcn.h>
36 #define INCLUDED_DLFCN_H
37 #endif
39 #endif /* HAVE_DLFCN_H */
41 #include <pthread.h>
42 #include <stddef.h>
44 /************************************************************************/
45 /* Internal data structures and functions */
46 /************************************************************************/
48 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
50 typedef pfunc_osl_printDebugMessage oslDebugMessageFunc;
51 static oslDebugMessageFunc volatile g_pDebugMessageFunc = 0;
53 typedef pfunc_osl_printDetailedDebugMessage oslDetailedDebugMessageFunc;
54 static oslDetailedDebugMessageFunc volatile g_pDetailedDebugMessageFunc = 0;
56 static void osl_diagnose_backtrace_Impl (
57 oslDebugMessageFunc f);
59 #define OSL_DIAGNOSE_OUTPUTMESSAGE(f, s) \
60 ((f != 0) ? (*(f))((s)) : (void)fprintf(stderr, "%s", (s)))
62 #if defined (LINUX) || defined (SOLARIS)
63 /************************************************************************/
64 /* osl_diagnose_frame_Impl */
65 /************************************************************************/
66 static void osl_diagnose_frame_Impl (
67 oslDebugMessageFunc f,
68 int depth,
69 void * pc)
71 const char *fname = 0, *sname = 0;
72 void *fbase = 0, *saddr = 0;
73 ptrdiff_t offset;
74 char szMessage[1024];
76 #ifdef INCLUDED_DLFCN_H
77 Dl_info dli;
78 if (dladdr (pc, &dli) != 0)
80 fname = dli.dli_fname;
81 fbase = dli.dli_fbase;
82 sname = dli.dli_sname;
83 saddr = dli.dli_saddr;
85 #endif /* INCLUDED_DLFCN_H */
87 if (saddr)
88 offset = (ptrdiff_t)(pc) - (ptrdiff_t)(saddr);
89 else if (fbase)
90 offset = (ptrdiff_t)(pc) - (ptrdiff_t)(fbase);
91 else
92 offset = (ptrdiff_t)(pc);
94 snprintf (szMessage, sizeof(szMessage),
95 "Backtrace: [%d] %s: %s+0x%" SAL_PRI_PTRDIFFT "x\n",
96 depth,
97 fname ? fname : "<unknown>",
98 sname ? sname : "???",
99 offset);
101 OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
103 #endif
105 /************************************************************************/
106 /* osl_diagnose_backtrace_Impl */
107 /************************************************************************/
108 #if defined(LINUX)
110 #include <execinfo.h>
112 #define FRAME_COUNT 64
113 #define FRAME_OFFSET 1
115 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
117 void * ppFrames[FRAME_COUNT];
118 int i, n;
120 n = backtrace (ppFrames, FRAME_COUNT);
121 for (i = FRAME_OFFSET; i < n; i++)
123 osl_diagnose_frame_Impl (f, (i - FRAME_OFFSET), ppFrames[i]);
127 #elif defined(SOLARIS)
129 #include <pthread.h>
130 #include <setjmp.h>
131 #include <sys/frame.h>
133 #if defined(SPARC)
135 #if defined IS_LP64
137 #define FRAME_PTR_OFFSET 1
138 #define FRAME_OFFSET 0
139 #define STACK_BIAS 0x7ff
141 #else
143 #define FRAME_PTR_OFFSET 1
144 #define FRAME_OFFSET 0
145 #define STACK_BIAS 0
147 #endif
149 #elif defined(INTEL)
151 #define FRAME_PTR_OFFSET 3
152 #define FRAME_OFFSET 0
153 #define STACK_BIAS 0
155 #endif /* (SPARC || INTEL) */
157 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
159 jmp_buf ctx;
160 long fpval;
161 struct frame * fp;
162 int i;
164 #if defined(SPARC)
165 asm("ta 3");
166 #endif /* SPARC */
167 setjmp (ctx);
169 fpval = ((long*)(ctx))[FRAME_PTR_OFFSET];
170 fp = (struct frame*)((char*)(fpval) + STACK_BIAS);
172 for (i = 0; (i < FRAME_OFFSET) && (fp != 0); i++)
173 fp = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
175 for (i = 0; (fp != 0) && (fp->fr_savpc != 0); i++)
177 struct frame * prev = (struct frame*)((char*)(fp->fr_savfp) + STACK_BIAS);
178 osl_diagnose_frame_Impl (f, i, (void*)(fp->fr_savpc));
179 fp = (prev > fp) ? prev : 0;
183 #else /* (LINUX || SOLARIS) */
185 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f)
187 (void) f;
188 /* not yet implemented */
191 #endif /* (LINUX || SOLARIS) */
193 /************************************************************************/
194 /* osl_assertFailedLine */
195 /************************************************************************/
197 namespace {
199 // getenv is not thread safe, so minimize use of result:
200 bool isEnv(char const * name) {
201 char * p = getenv(name);
202 return p != NULL && *p != '\0';
207 sal_Bool SAL_CALL osl_assertFailedLine (
208 const sal_Char* pszFileName,
209 sal_Int32 nLine,
210 const sal_Char* pszMessage)
212 oslDebugMessageFunc f = g_pDebugMessageFunc;
213 char szMessage[1024];
215 // after reporting the assertion, abort if told so by SAL_DIAGNOSE_ABORT, but *not* if
216 // assertions are routed to some external instance
217 static bool envAbort = isEnv( "SAL_DIAGNOSE_ABORT" );
218 static bool envBacktrace = isEnv( "SAL_DIAGNOSE_BACKTRACE" );
219 sal_Bool const doAbort = envAbort && f == NULL;
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 /* format message into buffer */
229 if (pszMessage != 0)
231 snprintf(szMessage, sizeof(szMessage),
232 "Error: File %s, Line %" SAL_PRIdINT32 ": %s\n",
233 pszFileName, nLine, pszMessage);
235 else
237 snprintf(szMessage, sizeof(szMessage),
238 "Error: File %s, Line %" SAL_PRIdINT32 "\n",
239 pszFileName, nLine);
242 /* acquire lock to serialize output message(s) */
243 pthread_mutex_lock(&g_mutex);
245 /* output message buffer */
246 OSL_DIAGNOSE_OUTPUTMESSAGE(f, szMessage);
248 /* should we output backtrace? */
249 if( envBacktrace )
250 osl_diagnose_backtrace_Impl(f);
252 /* release lock and leave */
253 pthread_mutex_unlock(&g_mutex);
255 return doAbort;
258 /************************************************************************/
259 /* osl_breakDebug */
260 /************************************************************************/
261 void SAL_CALL osl_breakDebug()
263 abort();
266 /************************************************************************/
267 /* osl_reportError */
268 /************************************************************************/
269 sal_Int32 SAL_CALL osl_reportError (
270 sal_uInt32 nType,
271 const sal_Char* pszMessage)
273 (void) nType; /* unused */
274 fputs(pszMessage, stderr);
275 return 0;
278 /************************************************************************/
279 /* osl_setDebugMessageFunc */
280 /************************************************************************/
281 oslDebugMessageFunc SAL_CALL osl_setDebugMessageFunc (
282 oslDebugMessageFunc pNewFunc)
284 oslDebugMessageFunc pOldFunc = g_pDebugMessageFunc;
285 g_pDebugMessageFunc = pNewFunc;
286 return pOldFunc;
289 /************************************************************************/
290 /* osl_setDetailedDebugMessageFunc */
291 /************************************************************************/
292 pfunc_osl_printDetailedDebugMessage SAL_CALL osl_setDetailedDebugMessageFunc (
293 pfunc_osl_printDetailedDebugMessage pNewFunc)
295 oslDetailedDebugMessageFunc pOldFunc = g_pDetailedDebugMessageFunc;
296 g_pDetailedDebugMessageFunc = pNewFunc;
297 return pOldFunc;
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */