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"
33 #if defined(LINUX) || defined(SOLARIS)
35 #endif /* LINUX || SOLARIS */
37 #endif /* HAVE_DLFCN_H */
42 #ifndef INCLUDED_DLFCN_H
44 #define INCLUDED_DLFCN_H
47 #endif /* HAVE_DLFCN_H */
48 #include "osl/thread.h"
50 #ifndef INCLUDED_PTHREAD_H
52 #define INCLUDED_PTHREAD_H
55 #ifndef INCLUDED_STDDEF_H
57 #define INCLUDED_STDDEF_H
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
,
89 const char *fname
= 0, *sname
= 0;
90 void *fbase
= 0, *saddr
= 0;
94 #ifdef INCLUDED_DLFCN_H
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 */
106 offset
= (ptrdiff_t)(pc
) - (ptrdiff_t)(saddr
);
108 offset
= (ptrdiff_t)(pc
) - (ptrdiff_t)(fbase
);
110 offset
= (ptrdiff_t)(pc
);
112 snprintf (szMessage
, sizeof(szMessage
),
113 "Backtrace: [%d] %s: %s+0x%" SAL_PRI_PTRDIFFT
"x\n",
115 fname
? fname
: "<unknown>",
116 sname
? sname
: "???",
119 OSL_DIAGNOSE_OUTPUTMESSAGE(f
, szMessage
);
123 /************************************************************************/
124 /* osl_diagnose_backtrace_Impl */
125 /************************************************************************/
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
];
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)
149 #include <sys/frame.h>
155 #define FRAME_PTR_OFFSET 1
156 #define FRAME_OFFSET 0
157 #define STACK_BIAS 0x7ff
161 #define FRAME_PTR_OFFSET 1
162 #define FRAME_OFFSET 0
169 #define FRAME_PTR_OFFSET 3
170 #define FRAME_OFFSET 0
173 #endif /* (SPARC || INTEL) */
175 static void osl_diagnose_backtrace_Impl (oslDebugMessageFunc f
)
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
,
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
);
228 /* if SAL assertions are disabled in general, stop here */
229 if ( getenv("DISABLE_SAL_DBGBOX") )
232 /* format message into buffer */
235 snprintf(szMessage
, sizeof(szMessage
),
236 "Error: File %s, Line %" SAL_PRIdINT32
": %s\n",
237 pszFileName
, nLine
, pszMessage
);
241 snprintf(szMessage
, sizeof(szMessage
),
242 "Error: File %s, Line %" SAL_PRIdINT32
"\n",
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
);
260 /************************************************************************/
262 /************************************************************************/
263 void SAL_CALL
osl_breakDebug()
268 /************************************************************************/
269 /* osl_reportError */
270 /************************************************************************/
271 sal_Int32 SAL_CALL
osl_reportError (
273 const sal_Char
* pszMessage
)
275 (void) nType
; /* unused */
276 fputs(pszMessage
, stderr
);
280 /************************************************************************/
281 /* osl_setDebugMessageFunc */
282 /************************************************************************/
283 oslDebugMessageFunc SAL_CALL
osl_setDebugMessageFunc (
284 oslDebugMessageFunc pNewFunc
)
286 oslDebugMessageFunc pOldFunc
= g_pDebugMessageFunc
;
287 g_pDebugMessageFunc
= pNewFunc
;
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
;
302 /************************************************************************/
304 /************************************************************************/
305 void osl_trace(char const * pszFormat
, ...) {
307 va_start(args
, pszFormat
);
308 printTrace((unsigned long) getpid(), pszFormat
, args
);