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 "osl/diagnose.h"
25 #if defined(LINUX) || defined(SOLARIS)
27 #endif /* LINUX || SOLARIS */
29 #endif /* HAVE_DLFCN_H */
34 #ifndef INCLUDED_DLFCN_H
36 #define INCLUDED_DLFCN_H
39 #endif /* HAVE_DLFCN_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
,
71 const char *fname
= 0, *sname
= 0;
72 void *fbase
= 0, *saddr
= 0;
76 #ifdef INCLUDED_DLFCN_H
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 */
88 offset
= (ptrdiff_t)(pc
) - (ptrdiff_t)(saddr
);
90 offset
= (ptrdiff_t)(pc
) - (ptrdiff_t)(fbase
);
92 offset
= (ptrdiff_t)(pc
);
94 snprintf (szMessage
, sizeof(szMessage
),
95 "Backtrace: [%d] %s: %s+0x%" SAL_PRI_PTRDIFFT
"x\n",
97 fname
? fname
: "<unknown>",
98 sname
? sname
: "???",
101 OSL_DIAGNOSE_OUTPUTMESSAGE(f
, szMessage
);
105 /************************************************************************/
106 /* osl_diagnose_backtrace_Impl */
107 /************************************************************************/
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
];
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)
131 #include <sys/frame.h>
137 #define FRAME_PTR_OFFSET 1
138 #define FRAME_OFFSET 0
139 #define STACK_BIAS 0x7ff
143 #define FRAME_PTR_OFFSET 1
144 #define FRAME_OFFSET 0
151 #define FRAME_PTR_OFFSET 3
152 #define FRAME_OFFSET 0
155 #endif /* (SPARC || INTEL) */
157 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f
)
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
)
188 /* not yet implemented */
191 #endif /* (LINUX || SOLARIS) */
193 /************************************************************************/
194 /* osl_assertFailedLine */
195 /************************************************************************/
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
,
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
);
228 /* format message into buffer */
231 snprintf(szMessage
, sizeof(szMessage
),
232 "Error: File %s, Line %" SAL_PRIdINT32
": %s\n",
233 pszFileName
, nLine
, pszMessage
);
237 snprintf(szMessage
, sizeof(szMessage
),
238 "Error: File %s, Line %" SAL_PRIdINT32
"\n",
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? */
250 osl_diagnose_backtrace_Impl(f
);
252 /* release lock and leave */
253 pthread_mutex_unlock(&g_mutex
);
258 /************************************************************************/
260 /************************************************************************/
261 void SAL_CALL
osl_breakDebug()
266 /************************************************************************/
267 /* osl_reportError */
268 /************************************************************************/
269 sal_Int32 SAL_CALL
osl_reportError (
271 const sal_Char
* pszMessage
)
273 (void) nType
; /* unused */
274 fputs(pszMessage
, stderr
);
278 /************************************************************************/
279 /* osl_setDebugMessageFunc */
280 /************************************************************************/
281 oslDebugMessageFunc SAL_CALL
osl_setDebugMessageFunc (
282 oslDebugMessageFunc pNewFunc
)
284 oslDebugMessageFunc pOldFunc
= g_pDebugMessageFunc
;
285 g_pDebugMessageFunc
= pNewFunc
;
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
;
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */