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